#!/usr/bin/env python
"""Export valid units from yaozarrs for JavaScript consumption.

This script extracts the valid space and time units from the yaozarrs
pydantic models and writes them to a JavaScript module that the OME explorer
web component can import directly.

Usage:
    python scripts/export_units.py

Output:
    docs/javascripts/ome_units.js
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import get_args

from yaozarrs._axis import SpaceUnits, TimeUnits

# Output path relative to repo root
OUTPUT_PATH = Path(__file__).parent.parent / "docs" / "javascripts" / "ome_units.js"


def export_units() -> None:
    """Export space and time units to JS module for direct import."""
    space_units = sorted(get_args(SpaceUnits))
    time_units = sorted(get_args(TimeUnits))

    # Generate JS module content
    js_content = f"""\
// Auto-generated by scripts/export_units.py - do not edit manually
export const units = {{
  space: {json.dumps(space_units)},
  time: {json.dumps(time_units)}
}};
"""

    # Ensure output directory exists
    OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)

    OUTPUT_PATH.write_text(js_content)
    print(f"Exported units to {OUTPUT_PATH}")
    print(f"  Space units: {len(space_units)}")
    print(f"  Time units: {len(time_units)}")


if __name__ == "__main__":
    export_units()
