/* tweaks-app.jsx — shared Tweaks controller mounted on every page.
   Owns the three explorations the user asked for:
     • Bloom treatment (load / drift / cursor / calm)
     • Type pairing  (literary / modern / elegant)
     • Palette        (vivid / soft)
   Applies them to <html data-…> + the bloom engine. Renders nothing
   but the panel itself. */

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "bloom": "load",
  "type": "literary",
  "palette": "vivid"
}/*EDITMODE-END*/;

function applyTweaks(t) {
  const root = document.documentElement;
  root.dataset.type = t.type;
  root.dataset.palette = t.palette;
  if (window.__setBloomPalette) window.__setBloomPalette(t.palette);
  if (window.__setBloomMode) window.__setBloomMode(t.bloom);
  window.__BLOOM_MODE = t.bloom;
}

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

  // apply on every change + on mount (and re-apply once the bloom engine is ready)
  React.useEffect(() => { applyTweaks(t); }, [t]);
  React.useEffect(() => {
    const id = setTimeout(() => applyTweaks(t), 60);
    return () => clearTimeout(id);
  }, []); // eslint-disable-line

  return (
    <TweaksPanel title="Tweaks">
      <TweakSection label="The bloom" />
      <TweakRadio
        label="Motion"
        value={t.bloom}
        options={[
          { value: "load",   label: "Bloom in" },
          { value: "drift",  label: "Breeze" },
          { value: "cursor", label: "Follow" },
          { value: "calm",   label: "Calm" },
        ]}
        onChange={(v) => setTweak("bloom", v)}
      />
      <p className="tw-note">
        <b>Bloom in</b> settles from a seed · <b>Breeze</b> keeps swaying ·
        <b> Follow</b> blooms where you point · <b>Calm</b> barely breathes.
      </p>

      <TweakSection label="Typography" />
      <TweakRadio
        label="Pairing"
        value={t.type}
        options={[
          { value: "literary", label: "Literary" },
          { value: "modern",   label: "Modern" },
          { value: "elegant",  label: "Elegant" },
        ]}
        onChange={(v) => setTweak("type", v)}
      />

      <TweakSection label="Palette" />
      <TweakRadio
        label="Intensity"
        value={t.palette}
        options={[
          { value: "vivid", label: "Vivid" },
          { value: "soft",  label: "Soft" },
        ]}
        onChange={(v) => setTweak("palette", v)}
      />

      <style>{`
        .tw-note { font-size: 11.5px; line-height: 1.5; color: #8a7f70; margin: 8px 2px 2px; }
        .tw-note b { color: #5b5246; font-weight: 600; }
      `}</style>
    </TweaksPanel>
  );
}

(function mount() {
  const el = document.getElementById("tweaks-root");
  if (!el) return;
  ReactDOM.createRoot(el).render(<TweaksApp />);
})();
