// Yew — app root
// Wires together Landing + Dashboard with shared theme state + Tweaks.

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "palette": "yew",
  "twilight": false,
  "tagline": 0,
  "accent": "restrained",
  "view": "landing"
}/*EDITMODE-END*/;

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

  // Apply theme attributes to <html> so palette/dark vars cascade everywhere.
  React.useEffect(() => {
    const html = document.documentElement;
    html.setAttribute("data-palette", t.palette || "yew");
    html.setAttribute("data-theme", t.twilight ? "twilight" : "light");
    html.setAttribute("data-accent", t.accent || "restrained");
  }, [t.palette, t.twilight, t.accent]);

  const view = t.view || "landing";
  const setView = (v) => setTweak("view", v);

  // Cycle taglines every 6s on landing if "auto" (we keep it static — Tweaks controls index)
  const taglineIdx = (t.tagline || 0) % TAGLINES.length;

  return (
    <React.Fragment>
      {view === "landing"
        ? <Landing
            onSignIn={() => setView("dashboard")}
            taglineIdx={taglineIdx}
            setTaglineIdx={(i) => setTweak("tagline", i)}
          />
        : <Dashboard onSignOut={() => setView("landing")} />
      }

      <TweaksPanel title="Yew · Tweaks">
        <TweakSection label="View">
          <TweakRadio
            label="Surface"
            value={view}
            options={["landing", "dashboard"]}
            onChange={(v) => setView(v)}
          />
        </TweakSection>

        <TweakSection label="Brand" />
        <TweakRadio
          label="Palette"
          value={t.palette}
          options={["yew", "moss", "loden"]}
          onChange={(v) => setTweak("palette", v)}
        />
        <TweakToggle
          label="Twilight (dark)"
          value={!!t.twilight}
          onChange={(v) => setTweak("twilight", v)}
        />

        <TweakSection label="Voice" />
        <TweakSelect
          label="Tagline"
          value={TAGLINES[taglineIdx].primary}
          options={TAGLINES.map(x => x.primary)}
          onChange={(v) => setTweak("tagline", TAGLINES.findIndex(x => x.primary === v))}
        />
      </TweaksPanel>
    </React.Fragment>
  );
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<YewApp />);
