/* ═══════════════════════════════════════════════════════════════
   AMBER BLAKE — Tweaks app (drives window.AB.apply)
   ═══════════════════════════════════════════════════════════════ */

// Prototype-4 is multi-page: each page declares its own world / hero / identity
// on <html data-*>. Seed the panel from those attributes so the control always
// opens reflecting the page you're on, and the project-wide defaults (subtle
// motion · Couture · AB monogram) are the fallback when an attribute is absent.
const TWEAK_DEFAULTS = (function () {
  const r = document.documentElement;
  return {
    palette: r.getAttribute("data-palette") || "heritage",
    typeset: r.getAttribute("data-type") || "couture",
    motion:  r.getAttribute("data-motion") || "calm",
    hero:    r.getAttribute("data-hero") || "liquidgold",
    name:    r.getAttribute("data-name") || "monogram",
    // Drifting-particle layer — on by default (data-particles="off" to start hidden).
    particles: r.getAttribute("data-particles") !== "off",
    // Mobile menu concept — Editorial index (02) is the default.
    menu: r.getAttribute("data-menu") || "editorial",
    // Gallery image grade (Locations) — percentages; saturation eased back by default.
    gallerySat: 85, galleryBri: 100, galleryCon: 100
  };
})();

// Only the Locations page carries the gallery, so its sliders show only there.
const HAS_GALLERY = !!document.getElementById("galTrack");

// [bg-deep, accent, gold] per palette — drives the three-band swatch chip.
// The original four "worlds" plus the four named handoff palettes (prototype-5).
const AB_SWATCHES = {
  heritage: ["#EAD6CC", "#C2867F", "#B68B4C"],
  midnight: ["#15110E", "#CBA463", "#C28F87"],
  coastal:  ["#D6D0C2", "#7C8B90", "#A98F63"],
  horizon:  ["#0F1A22", "#5FA8B0", "#D9A066"],
  warm:     ["#E3D8CC", "#7BA8D1", "#D4AF75"],
  cool:     ["#DCE2EA", "#1E40AF", "#B8B8B8"],
  luxury:   ["#180E08", "#B07B53", "#D4AF75"],
  rosegold: ["#EBD4C9", "#A84E2A", "#C17A4E"],
  pool:     ["#CDE9EE", "#0E7FA8", "#13A8C6"],
  tropical: ["#EBDFC8", "#0E8FA0", "#13B0A0"]
};

const AB_PALETTE_LABEL = {
  heritage: "Heritage", midnight: "Midnight", coastal: "Coastal", horizon: "Horizon",
  warm: "Warm", cool: "Cool", luxury: "Luxury", rosegold: "Rose-Gold",
  pool: "Pool", tropical: "Tropical"
};

function PaletteChips({ value, onChange }) {
  const keys = Object.keys(AB_SWATCHES);
  return (
    <div className="twk-row">
      <div className="twk-lbl"><span>Color world</span>
        <span className="twk-val">{AB_PALETTE_LABEL[value] || value}</span></div>
      {/* 8 palettes → wrap into rows of four so each swatch stays legible. */}
      <div className="twk-chips" role="radiogroup" style={{ flexWrap: "wrap" }}>
        {keys.map((k) => {
          const cols = AB_SWATCHES[k];
          const on = k === value;
          return (
            <button key={k} type="button" className="twk-chip" role="radio"
              aria-checked={on} data-on={on ? "1" : "0"} title={AB_PALETTE_LABEL[k] || k}
              style={{ background: cols[0], flex: "1 1 calc(25% - 6px)", minWidth: "0" }}
              onClick={() => onChange(k)}>
              <span>
                <i style={{ background: cols[1] }} />
                <i style={{ background: cols[2] }} />
              </span>
            </button>
          );
        })}
      </div>
    </div>
  );
}

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

  React.useEffect(() => {
    if (window.AB && window.AB.apply) window.AB.apply(t);
  }, [t]);

  return (
    <TweaksPanel title="Amber Blake — Studio">
      <TweakSection label="Atmosphere" />
      <PaletteChips value={t.palette} onChange={(v) => setTweak("palette", v)} />
      <TweakSelect label="Hero concept" value={t.hero}
        options={[
          { value: "caustics", label: "I · Light through water" },
          { value: "liquidgold", label: "II · Liquid gold" },
          { value: "aurora", label: "III · Aurora mist" },
          { value: "particles", label: "IV · Drifting particles" },
          { value: "moltengold", label: "V · Molten gold" },
          { value: "nacre", label: "VI · Iridescent nacre" },
          { value: "kintsugi", label: "VII · Kintsugi" },
          { value: "ink", label: "VIII · Ink in water" },
          { value: "rosequartz1", label: "IX · Rose quartz i" },
          { value: "rosequartz", label: "X · Rose quartz ii" },
          { value: "icepalace", label: "XI · Ice palace" }
        ]}
        onChange={(v) => setTweak("hero", v)} />
      <TweakRadio label="Motion" value={t.motion}
        options={[
          { value: "calm", label: "Calm" },
          { value: "subtle", label: "Subtle" },
          { value: "rich", label: "Rich" },
          { value: "immersive", label: "Immersive" }
        ]}
        onChange={(v) => setTweak("motion", v)} />
      <TweakToggle label="Drifting particles" value={t.particles}
        onChange={(v) => setTweak("particles", v)} />

      <TweakSection label="Identity" />
      <TweakSelect label="Typeface" value={t.typeset}
        options={[
          { value: "couture", label: "Couture · Cormorant" },
          { value: "editorial", label: "Editorial · Bodoni" },
          { value: "literary", label: "Literary · EB Garamond" },
          { value: "maison", label: "Maison · Playfair" },
          { value: "atelier", label: "Atelier · DM Serif" },
          { value: "broadsheet", label: "Broadsheet · Fraunces" },
          { value: "monument", label: "Monument · Marcellus" },
          { value: "runway", label: "Runway · Prata" },
          { value: "journal", label: "Journal · Spectral" },
          { value: "riviera", label: "Riviera · Italiana" },
          { value: "manor", label: "Manor · Libre Caslon" },
          { value: "gazette", label: "Gazette · Newsreader" },
          { value: "memoir", label: "Memoir · Lora" }
        ]}
        onChange={(v) => setTweak("typeset", v)} />
      <TweakRadio label="Wordmark" value={t.name}
        options={[
          { value: "wordmark", label: "Amber·Blake" },
          { value: "monogram", label: "AB mono" },
          { value: "logo", label: "Logo" }
        ]}
        onChange={(v) => setTweak("name", v)} />
      <TweakSelect label="Mobile menu" value={t.menu}
        options={[
          { value: "radial", label: "01 · Radial bloom" },
          { value: "editorial", label: "02 · Editorial index" },
          { value: "curtain", label: "03 · Pull-down curtain" },
          { value: "liquid", label: "04 · Liquid gold" },
          { value: "panels", label: "05 · Unfolding panels" },
          { value: "dial", label: "06 · Rotary dial" }
        ]}
        onChange={(v) => setTweak("menu", v)} />

      {HAS_GALLERY && (
        <>
          <TweakSection label="Gallery photos" />
          <TweakSlider label="Saturation" value={t.gallerySat} min={0} max={160} step={5} unit="%"
            onChange={(v) => setTweak("gallerySat", v)} />
          <TweakSlider label="Brightness" value={t.galleryBri} min={70} max={130} step={1} unit="%"
            onChange={(v) => setTweak("galleryBri", v)} />
          <TweakSlider label="Contrast" value={t.galleryCon} min={70} max={130} step={1} unit="%"
            onChange={(v) => setTweak("galleryCon", v)} />
        </>
      )}
    </TweaksPanel>
  );
}

(function mount() {
  function go() {
    var host = document.createElement("div");
    host.id = "ab-tweaks-root";
    document.body.appendChild(host);
    ReactDOM.createRoot(host).render(React.createElement(AmberTweaks));
  }
  if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", go);
  else go();
})();
