#!/usr/bin/env python3
"""
Bootstrap compiler: reference SX evaluator → JavaScript.

This is now a thin shim that delegates to run_js_sx.py (the self-hosting
bootstrapper). The hand-written JSEmitter has been replaced by js.sx.

Usage:
    python bootstrap_js.py
"""
from __future__ import annotations

import os
import sys

_HERE = os.path.dirname(os.path.abspath(__file__))
_PROJECT = os.path.abspath(os.path.join(_HERE, "..", "..", ".."))
if _PROJECT not in sys.path:
    sys.path.insert(0, _PROJECT)

# Re-export everything that consumers import from this module.
# Canonical source is now run_js_sx.py (self-hosting via js.sx) and platform_js.py.
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
from hosts.javascript.bootstrap import compile_ref_to_js, load_js_sx  # noqa: F401
from hosts.javascript.platform import (  # noqa: F401
    extract_defines,
    ADAPTER_FILES, ADAPTER_DEPS, SPEC_MODULES, EXTENSION_NAMES,
    PREAMBLE, PLATFORM_JS_PRE, PLATFORM_JS_POST,
    PRIMITIVES_JS_MODULES, _ALL_JS_MODULES, _assemble_primitives_js,
    PLATFORM_DEPS_JS, PLATFORM_PARSER_JS, PLATFORM_DOM_JS,
    PLATFORM_ENGINE_PURE_JS, PLATFORM_ORCHESTRATION_JS, PLATFORM_BOOT_JS,
    CONTINUATIONS_JS, ASYNC_IO_JS,
    fixups_js, public_api_js, EPILOGUE,
)


if __name__ == "__main__":
    import argparse
    p = argparse.ArgumentParser(description="Bootstrap-compile SX reference spec to JavaScript")
    p.add_argument("--adapters", "-a",
                    help="Comma-separated adapter list (html,sx,dom,engine). Default: all")
    p.add_argument("--modules", "-m",
                    help="Comma-separated primitive modules (core.* always included). Default: all")
    p.add_argument("--extensions",
                    help="Comma-separated extensions (continuations). Default: none.")
    p.add_argument("--spec-modules",
                    help="Comma-separated spec modules (deps). Default: none.")
    default_output = os.path.join(_HERE, "..", "..", "shared", "static", "scripts", "sx-browser.js")
    p.add_argument("--output", "-o", default=default_output,
                    help="Output file (default: shared/static/scripts/sx-browser.js)")
    args = p.parse_args()

    adapters = args.adapters.split(",") if args.adapters else None
    modules = args.modules.split(",") if args.modules else None
    extensions = args.extensions.split(",") if args.extensions else None
    spec_modules = args.spec_modules.split(",") if args.spec_modules else None
    js = compile_ref_to_js(adapters, modules, extensions, spec_modules)

    with open(args.output, "w") as f:
        f.write(js)
    included = ", ".join(adapters) if adapters else "all"
    mods = ", ".join(modules) if modules else "all"
    ext_label = ", ".join(extensions) if extensions else "none"
    print(f"Wrote {args.output} ({len(js)} bytes, adapters: {included}, modules: {mods}, extensions: {ext_label})",
          file=sys.stderr)
