// Loads the product's own files and publishes window.TalentDeskApp.
//
// Three hard-won details:
//   1. The global is published SYNCHRONOUSLY, on the first line, as a stub that
//      re-renders itself when the real App lands. <x-import
//      component-from-global-scope="TalentDeskApp"> resolves the global with a
//      bounded wait, and on a cold cache the load below takes longer than that
//      window — the placeholder never retries, so the first person to open the
//      template would otherwise see a blank page for good.
//   2. React, ReactDOM and Babel are NOT fetched here when the page is a Design
//      Component — the DC runtime puts its own copies on window, and a second
//      React breaks every hook ("Invalid hook call"). We wait for them and only
//      fall back to the CDN if nothing else provides them.
//   3. Each file is evaluated in its own function scope rather than appended as
//      a <script>. The DC runtime can re-run this loader against a rebuilt
//      document, and script tags in a replaced <head> either vanish mid-flight
//      or re-declare every top-level const. Everything files need from each
//      other is on window already, so function scope costs nothing.
(() => {
  const base = new URL(".", document.currentScript.src).href;

  /* --- 1. the stub, published before anything is fetched ----------------- */
  const READY = "talent-desk:ready";
  if (!window.TalentDeskApp) {
    window.TalentDeskApp = function TalentDeskAppStub(props) {
      const [, bump] = window.React.useState(0);
      window.React.useEffect(() => {
        if (window.__talentDeskReal) return undefined;
        const on = () => bump((n) => n + 1);
        window.addEventListener(READY, on);
        return () => window.removeEventListener(READY, on);
      }, []);
      const Real = window.__talentDeskReal;
      return Real ? window.React.createElement(Real, props) : null;
    };
  }

  /* --- 2. the load ------------------------------------------------------- */
  const load = (src) => new Promise((res, rej) => { const s = document.createElement("script"); s.src = src; s.crossOrigin = "anonymous"; s.onload = res; s.onerror = () => rej(new Error("failed to load " + src)); document.head.appendChild(s); });
  const wait = async (test, ms) => { for (let i = 0; i < ms / 25; i++) { if (test()) return true; await new Promise((r) => setTimeout(r, 25)); } return !!test(); };
  const FILES = [
    "lib/contracts.js", "enums.js", "nav.js", "fixtures.js", "metrics.js",
    "lib/ui-runtime.js", "lib/command-palette.js", "lib/template-menu.js",
    "MarqueParts.jsx", "MarqueShell.jsx",
    "RosterScreen.jsx", "NewCreatorScreen.jsx", "TalentScreen.jsx", "StatementScreen.jsx", "BriefsScreen.jsx", "NewBriefScreen.jsx", "MatchScreen.jsx",
    "BrandScreen.jsx", "DealScreen.jsx",
    "DeliverablesScreen.jsx", "DeliverableScreen.jsx",
    "PerformanceScreen.jsx", "WrapScreen.jsx", "MoneyScreen.jsx",
    "InboxScreen.jsx", "SearchScreen.jsx",
    "MarqueAccountScreens.jsx", "MarqueResetScreens.jsx", "TeamScreen.jsx",
    "App.jsx",
  ];
  (async () => {
    /* Every file is fetched at once — the sequential round-trips were most of
       the cold-start latency. Only the transpile-and-run is ordered. */
    const sources = FILES.map((f) => fetch(base + f).then((r) => r.text()));
    if (!window.React || !window.ReactDOM) {
      if (!(await wait(() => window.React && window.ReactDOM, 1500))) {
        if (!window.React) await load("https://unpkg.com/react@18.3.1/umd/react.development.js");
        if (!window.ReactDOM) await load("https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js");
      }
    }
    if (!window.Babel) await load("https://unpkg.com/@babel/standalone@7.29.0/babel.min.js");
    if (!window.lucide) await load("https://unpkg.com/lucide@0.544.0/dist/umd/lucide.min.js");
    await wait(() => Object.keys(window).some((k) => k.indexOf("OriginDesignSystem") === 0), 10000);
    for (let i = 0; i < FILES.length; i++) {
      const src = await sources[i];
      const out = window.Babel.transform(src, { presets: ["react"], filename: FILES[i] }).code;
      try { new Function(out)(); } catch (e) { console.error("boot.js: " + FILES[i], e); throw e; }
    }
    /* App.jsx assigns window.__talentDeskReal; wake the stub. */
    window.dispatchEvent(new Event(READY));
  })().catch((e) => console.error("boot.js:", e));
})();
