// runtime.jsx — Single-page review runtime.
//
// Reads window.APABA_INIT_PAGE + window.APABA_TWEAKS (set by apaba.js before
// this script runs) and renders the matching screen wrapped in <LaptopFrame>.
// Cross-page navigation routes to a sibling URL via window.location.href so each
// "page" is a real preview-able URL on Vercel (cleanUrls is enabled).
//
// Variants are driven from the apaba.js Tweaks panel — applyTokens has already
// been called against :root, so the React tree only needs heroVariant +
// foundationGold passed down to Home (the rest are CSS).

const TWEAKS = window.APABA_TWEAKS || (window.APABA && window.APABA.TWEAK_DEFAULTS) || {};

function reviewNavigate(page) {
  if (page === 'home' || !page) {
    window.location.href = '/home';
    return;
  }
  window.location.href = '/' + page;
}

function ReviewApp() {
  const page   = window.APABA_INIT_PAGE || 'home';
  const entity = page === 'foundation' ? 'foundation' : 'apaba';

  const PAGES = {
    home:               <Home tweaks={TWEAKS} navigate={reviewNavigate} />,
    about:              <AboutPage navigate={reviewNavigate} />,
    membership:         <MembershipPage navigate={reviewNavigate} />,
    events:             <EventsPage navigate={reviewNavigate} />,
    clinic:             <ClinicPage navigate={reviewNavigate} />,
    statements:         <StatementsPage navigate={reviewNavigate} />,
    'statement-detail': <StatementDetailPage navigate={reviewNavigate} />,
    foundation:         <FoundationPage navigate={reviewNavigate} />,
  };

  // setEntity is a no-op in review mode — the utility bar's Foundation tab is a link.
  const setEntity = () => {};

  return (
    <LaptopFrame>
      <UtilityBar entity={entity} setEntity={setEntity} navigate={reviewNavigate} currentPage={page} />
      <Header navigate={reviewNavigate} currentPage={page} />
      <main>{PAGES[page] || PAGES.home}</main>
      <Footer navigate={reviewNavigate} />
    </LaptopFrame>
  );
}

ReactDOM.createRoot(document.getElementById('stage')).render(<ReviewApp />);
