// site/site-app.jsx, Router (hash-based) + shell.

function App() {
  const REGISTRY = {
    home: PageHome,
    'who-we-are': PageWhoWeAre,
    platform: PagePlatform,
    websites: PageWebsites, ticketing: PageTicketing, social: PageSocial,
    marketing: PageMarketing,
    vendors: PageVendorManagement, sponsors: PageSponsorManagement,
    hardware: PageKiosks,
    // INTEGRATIONS-DISABLED (2026-07-09): page temporarily hidden. Un-comment this line +
    // the two tagged lines in site-chrome.jsx to restore. While disabled, the route falls
    // through to Home so #integrations can't be reached. See TODO.md.
    // integrations: PageIntegrations,
    solutions: PageSolutions, managers: PageManagers, boards: PageBoards, marketers: PageMarketers,
    about: PageAbout, contact: PageContact,
    resources: PageBlog,
    blog: PageBlog,
    'case-studies': PageCaseStudies,
    news: PageNews,
    'rv-tour': PageRVTour,
    ymtd: PageYMTD,
    // Unlisted legal page, deep-linked from the Fairs.com platform only.
    // Reached via /ticketholder-agreement (path) or #ticketholder-agreement (hash).
    'ticketholder-agreement': PageTicketholderAgreement,
    // Legal pages linked from the footer AND from the Fairs.com platform.
    // Reached via /privacy · /terms (path, see vercel.json rewrites/redirects)
    // or #privacy · #terms (hash). The old /policies/* URLs 301 to these.
    privacy: PagePrivacy,
    terms: PageTerms,
  };
  const getBaseId = (hashOrPath) => (hashOrPath || '').replace(/^#/, '').split('/')[0];
  // Resolve initial route from hash first, then fall back to the URL pathname
  // (e.g. /ticketholder-agreement served via Vercel rewrite) so deep links work.
  const pathBase = (typeof location !== 'undefined' ? location.pathname || '' : '')
    .replace(/^\//, '').replace(/\/$/, '').split('/')[0];
  const initial = getBaseId(typeof location !== 'undefined' ? location.hash : '')
    || (REGISTRY[pathBase] ? pathBase : '')
    || 'home';
  const [page, setPage] = React.useState(REGISTRY[initial] ? initial : 'home');

  const navigate = React.useCallback((p) => {
    const baseId = getBaseId(p);
    const valid = REGISTRY[baseId];
    setPage(valid ? baseId : 'home');
    history.pushState(null, '', '#' + (valid ? p : 'home'));
    // pushState doesn't fire hashchange, but sub-route-aware pages
    // (e.g. PageBlog) listen for it. Dispatch it manually so they update.
    window.dispatchEvent(new Event('hashchange'));
    window.scrollTo({ top: 0, behavior: 'instant' });
  }, []);

  React.useEffect(() => {
    const onHash = () => {
      const id = getBaseId(location.hash) || 'home';
      if (REGISTRY[id]) {
        setPage(id);
        window.scrollTo({ top: 0, behavior: 'instant' });
      }
    };
    window.addEventListener('popstate', onHash);
    window.addEventListener('hashchange', onHash);
    return () => {
      window.removeEventListener('popstate', onHash);
      window.removeEventListener('hashchange', onHash);
    };
  }, []);

  const Page = REGISTRY[page] || REGISTRY.home;

  return (
    <div style={{ minHeight: '100vh', background: S.cream, color: S.ink }}>
      <SiteNav current={page} onNav={navigate} />
      <main key={page} className="page-enter">
        <Page onNav={navigate} />
      </main>
      <SiteFooter onNav={navigate} />
    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
