#!/usr/bin/env python3
"""Generate PUBLIC_API.json from the live __all__ of mgf.standard (DOC-08)."""

from __future__ import annotations

import importlib
import json
from pathlib import Path
from typing import Any


def build_payload() -> dict[str, Any]:
    mod = importlib.import_module("mgf.standard")
    names = sorted(n for n in mod.__all__ if n != "__version__")
    if "__version__" not in names:
        names.append("__version__")
        names.sort()
    return {
        "__version__": mod.__version__,
        "modules": {"mgf.standard": names},
        "__generated_total_names": len(names),
    }


def write_json(path: Path) -> None:
    path.write_text(json.dumps(build_payload(), indent=2, sort_keys=True) + "\n", encoding="utf-8")


if __name__ == "__main__":
    out = Path(__file__).resolve().parent.parent / "PUBLIC_API.json"
    write_json(out)
    print(f"wrote {out}")
