"""PyScript entrypoint - boots the SPA (broaduction idiom: top-level await).

Boot order matters: language first (so the first paint of the app is already in the
user's language and direction), then mount, then the perf marks the week-one
measurement reads (docs/DEVOPS.md §Performance).
"""
from pyscript import document

from framework import mark, measure, mount

from app import state
from app.i18n import on_change, wire_language_controls
from app.views import App


async def main() -> None:
    mark("shell-visible")
    wire_language_controls()                      # saved/device language + dir + [data-lang]
    on_change(lambda _lang: state.refresh_for_language())
    from app.i18n import core as i18n_core

    state.lang.set(i18n_core.current_lang)        # seed the reactive mirror post-detect

    from pyscript import window

    if str(window.location.hash) == "#catalog":
        state.screen.set("catalog")

    splash = document.getElementById("splash")
    if splash:
        splash.remove()
    mount("#app", App)

    mark("pyscript-ready")
    measure("pyscript-boot", "shell-visible", "pyscript-ready")


await main()
