/* Tweaks panel for the EHC Africa West homepage.
   Drives <html> data-attributes + CSS vars that the page CSS responds to. */
const { useEffect } = React;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "palette": "warm",
  "hero": "split",
  "map": "filled",
  "showContext": true,
  "headline": "Carrying Christ to everyone, everywhere, in every generation.",
  "highlight": "everyone, everywhere"
}/*EDITMODE-END*/;

function applyHeadline(full, hl) {
  const el = document.querySelector('.hero h1');
  if (!el) return;
  const safe = (s) => s.replace(/[&<>]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' }[c]));
  let html = safe(full);
  if (hl && full.includes(hl)) {
    html = safe(full).replace(safe(hl), '<span class="hl">' + safe(hl) + '</span>');
  }
  el.innerHTML = html;
  // keep the i18n key from clobbering the custom headline on EN
  if (window.DICT && window.DICT.en) window.DICT.en['hero.title'] = html;
}

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

  useEffect(() => { document.documentElement.dataset.palette = t.palette; }, [t.palette]);
  useEffect(() => { document.documentElement.dataset.hero = t.hero; }, [t.hero]);
  useEffect(() => { document.documentElement.dataset.map = t.map; }, [t.map]);
  useEffect(() => { document.documentElement.dataset.noContext = String(!t.showContext); }, [t.showContext]);
  useEffect(() => {
    // only apply custom headline while in English
    if ((window.__LANG || 'en') === 'en') applyHeadline(t.headline, t.highlight);
  }, [t.headline, t.highlight]);

  return (
    <TweaksPanel title="Tweaks">
      <TweakSection label="Visual direction" />
      <TweakRadio label="Palette" value={t.palette}
        options={['warm', 'restrained', 'energetic']}
        onChange={(v) => setTweak('palette', v)} />
      <p style={{ margin: '-4px 2px 8px', fontSize: 11.5, color: '#8a847f', lineHeight: 1.4 }}>
        warm = clay + gold · restrained = brand sand + sage · energetic = bright CTA green
      </p>

      <TweakSection label="Hero layout" />
      <TweakRadio label="Arrangement" value={t.hero}
        options={['split', 'overlay', 'centered']}
        onChange={(v) => setTweak('hero', v)} />

      <TweakSection label="West Africa map" />
      <TweakRadio label="Style" value={t.map}
        options={['filled', 'outlined', 'minimal']}
        onChange={(v) => setTweak('map', v)} />
      <TweakToggle label="Show neighbouring countries" value={t.showContext}
        onChange={(v) => setTweak('showContext', v)} />

      <TweakSection label="Hero headline (EN)" />
      <TweakText label="Headline" value={t.headline}
        onChange={(v) => setTweak('headline', v)} />
      <TweakText label="Highlighted phrase" value={t.highlight}
        onChange={(v) => setTweak('highlight', v)} />
    </TweaksPanel>
  );
}

ReactDOM.createRoot(document.getElementById('tweaks-root')).render(<TweaksApp />);
