/* App entry: composes everything + Tweaks panel */
const { useEffect: useEffectApp } = React;

function TweaksPanelMP() {
  const { TweaksPanel, useTweaks, TweakSection, TweakRadio, TweakSelect, TweakSlider } = window;

  const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
    "vibe": "zine",
    "scrollSpeed": 2.4,
    "grain": true,
    "cursor": true
  }/*EDITMODE-END*/;

  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  useEffectApp(() => {
    // scroll speed
    if (window.__lenis) window.__lenis.options.duration = t.scrollSpeed;
    // grain
    document.body.classList.toggle("no-grain", !t.grain);
    // cursor
    document.body.classList.toggle("no-cursor", !t.cursor);
    // vibe controls a chaos multiplier
    document.body.dataset.vibe = t.vibe;
  }, [t.scrollSpeed, t.grain, t.cursor, t.vibe]);

  return (
    <TweaksPanel title="Tweaks">
      <TweakSection title="Vibe">
        <TweakRadio label="Energy" value={t.vibe} onChange={(v)=>setTweak("vibe",v)}
          options={[{value:"calm",label:"Calm"},{value:"zine",label:"Zine"},{value:"chaos",label:"Chaos"}]} />
      </TweakSection>
      <TweakSection title="Motion">
        <TweakSlider label="Scroll smoothness" value={t.scrollSpeed} onChange={(v)=>setTweak("scrollSpeed",v)} min={0.4} max={2.4} step={0.05} format={(v)=>v.toFixed(2)+"s"} />
      </TweakSection>
      <TweakSection title="Texture">
        <TweakRadio label="Photocopy grain" value={t.grain ? "on" : "off"} onChange={(v)=>setTweak("grain", v==="on")}
          options={[{value:"on",label:"On"},{value:"off",label:"Off"}]} />
        <TweakRadio label="Cursor blob" value={t.cursor ? "on" : "off"} onChange={(v)=>setTweak("cursor", v==="on")}
          options={[{value:"on",label:"On"},{value:"off",label:"Off"}]} />
      </TweakSection>
    </TweaksPanel>
  );
}

function App() {
  return (
    <>
      <CursorBlob />
      <Nav />
      <main>
        <Hero />
        <Marquee />
        <Edge />
        <Work />
        <Process />
        <Capabilities />
        <About />
        <Contact />
      </main>
      <TweaksPanelMP />
    </>
  );
}

function bootApp() {
  if (window.Lenis) {
    const lenis = new window.Lenis({ duration: 1.15, easing: (t)=>Math.min(1, 1.001 - Math.pow(2,-10*t)), smoothWheel: true });
    window.__lenis = lenis;
    function raf(time){ lenis.raf(time); requestAnimationFrame(raf); }
    requestAnimationFrame(raf);
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<App />);
}

if (document.readyState === "loading") {
  window.addEventListener("DOMContentLoaded", bootApp);
} else {
  bootApp();
}
