// Home page: composition and boot. Content comes from /api/content (Supabase);
// the shipped DEFAULT_CONTENT renders instantly and is the offline fallback.
function App({ offline = false }) {
  const [content, setContent] = React.useState(window.DEFAULT_CONTENT);
  const [introDone, setIntroDone] = React.useState(false);

  React.useEffect(() => {
    if (offline) return;
    let alive = true;
    fetch("/api/content", { headers: { accept: "application/json" } })
      .then((r) => (r.ok ? r.json() : null))
      .then((d) => { if (alive && d && (d.site || (d.projects && d.projects.length))) setContent(window.mergeContent(d)); })
      .catch(() => {});
    return () => { alive = false; };
  }, []);

  React.useEffect(() => { window.applyMeta(content.meta); }, [content]);

  // motion boots once the intro has handed the mark to the header
  React.useEffect(() => {
    if (!introDone) return;
    const id = requestAnimationFrame(() => requestAnimationFrame(() => window.bootMotion()));
    return () => cancelAnimationFrame(id);
  }, [introDone]);

  // late content swaps DOM under existing tweens: bind what is new, re-measure
  React.useEffect(() => {
    if (!window.__motionBooted) return;
    const id = requestAnimationFrame(() => window.dhRefreshMotion());
    return () => cancelAnimationFrame(id);
  }, [content]);

  const CC = window.ContentContext;
  return (
    <CC.Provider value={content}>
      <Intro onDone={() => setIntroDone(true)} />
      <Nav />
      <main id="main">
        <Hero />
        <Reel />
        <Work />
        <Statement />
        <Services />
        <Clients />
        <Studio />
        <Contact />
      </main>
      <Footer />
    </CC.Provider>
  );
}

// Pre-hide animated elements only when motion will actually run, so a script
// failure can never leave the page blank.
if (window.gsap && window.ScrollTrigger && !DH_REDUCED()) document.documentElement.classList.add("anim-ready");

ReactDOM.createRoot(document.getElementById("root")).render(<DhBoundary fallback={<App offline />}><App /></DhBoundary>);
