/* NutriDMS, API Sandbox Tweaks
   Three expressive controls that reshape the feel of the page:
   1. Vibe  , Studio (default clinical), Terminal (mono on dark), Editorial (warm cream + display serif)
   2. Accent, palette swatches that re-tint the green identity (Forest, Sandstone, Cobalt, Plum)
   3. Hero  , Cover photo, Banner strip, or Hidden (chrome-first)

   Applied as data-* attributes on <html>; overrides live in sandbox-tweaks.css. */

const SBOX_TWEAK_DEFAULTS = window.SBOX_TWEAK_DEFAULTS || { vibe: "studio", accent: "forest", hero: "cover" };

function SandboxTweaks() {
  const [t, setTweak] = useTweaks(SBOX_TWEAK_DEFAULTS);

  React.useEffect(() => {
    const r = document.documentElement;
    r.setAttribute("data-sb-vibe", t.vibe);
    r.setAttribute("data-sb-accent", t.accent);
    r.setAttribute("data-sb-hero", t.hero);
  }, [t.vibe, t.accent, t.hero]);

  return (
    <TweaksPanel title="Tweaks">
      <TweakSection label="Vibe" />
      <TweakRadio
        label="Mode"
        value={t.vibe}
        options={[
          { value: "studio", label: "Studio" },
          { value: "terminal", label: "Terminal" },
          { value: "editorial", label: "Editorial" },
        ]}
        onChange={(v) => setTweak("vibe", v)} />
      <TweakSection label="Accent" />
      <AccentChips value={t.accent} onChange={(v) => setTweak("accent", v)} />
      <TweakSection label="Response hero" />
      <TweakRadio
        label="Style"
        value={t.hero}
        options={[
          { value: "cover", label: "Cover" },
          { value: "banner", label: "Banner" },
          { value: "hidden", label: "None" },
        ]}
        onChange={(v) => setTweak("hero", v)} />
    </TweaksPanel>);

}

/* Accent picker rendered as palette swatch chips. We bypass TweakColor's
   value-equality (which compares the full array) because our stored value is
   a short string id like "sandstone", easier to read in the EDITMODE block. */
function AccentChips({ value, onChange }) {
  const ACCENTS = [
    { id: "forest", colors: ["#3B7C0F", "#669F2A", "#E8F4D4"] },
    { id: "sandstone", colors: ["#C05621", "#ED8936", "#FFF6EB"] },
    { id: "cobalt", colors: ["#1E4FCC", "#3B82F6", "#E0EAFF"] },
    { id: "plum", colors: ["#6B21A8", "#A855F7", "#F3E8FF"] }];

  return (
    <div className="twk-chips" role="radiogroup" style={{ marginTop: -6 }}>
      {ACCENTS.map((a) => {
        const on = a.id === value;
        const [hero, ...rest] = a.colors;
        return (
          <button
            key={a.id}
            type="button"
            className="twk-chip"
            role="radio"
            aria-checked={on}
            data-on={on ? "1" : "0"}
            aria-label={a.id}
            title={a.id}
            style={{ background: hero }}
            onClick={() => onChange(a.id)}>

            <span>
              {rest.map((c, j) => <i key={j} style={{ background: c }} />)}
            </span>
            {on &&
            <svg viewBox="0 0 14 14" aria-hidden="true">
                <path d="M3 7.2 5.8 10 11 4.2" fill="none" strokeWidth="2.2"
                strokeLinecap="round" strokeLinejoin="round" stroke="#fff" />
              </svg>}

          </button>);

      })}
    </div>);

}

/* Mount in its own root so we never touch sandbox.jsx. */
(function () {
  const host = document.createElement("div");
  host.id = "sandbox-tweaks-root";
  document.body.appendChild(host);
  ReactDOM.createRoot(host).render(<SandboxTweaks />);
})();
