// tweaks-app.jsx — Tweaks panel wiring for the preserve-root portfolio (vanilla site bridge)
const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "heroScene": "console",
  "accent": "#4f8ef7",
  "motion": 1
}/*EDITMODE-END*/;

function hexToRgb(hex){
  hex = hex.replace('#','');
  if(hex.length===3) hex = hex.split('').map(c=>c+c).join('');
  const n = parseInt(hex,16);
  return [(n>>16)&255,(n>>8)&255,n&255];
}

let _prev = {};
function applyTweaks(t){
  const root = document.documentElement.style;
  // accent + derived tokens
  if(t.accent !== _prev.accent){
    const [r,g,b] = hexToRgb(t.accent);
    root.setProperty('--accent', t.accent);
    root.setProperty('--accent-soft', `rgba(${r},${g},${b},0.14)`);
    root.setProperty('--accent-line', `rgba(${r},${g},${b},0.34)`);
    root.setProperty('--accent-glow', `rgba(${r},${g},${b},0.55)`);
    if(window.HeroScene) window.HeroScene.setAccent(t.accent);
    try{ window.__radarAccent = t.accent; }catch(e){}
  }
  // hero scene
  if(t.heroScene !== _prev.heroScene && window.HeroScene){
    window.HeroScene.setMode(t.heroScene);
    const tag = document.getElementById('sceneTagName');
    if(tag) tag.textContent = t.heroScene;
  }
  // motion
  if(t.motion !== _prev.motion){
    root.setProperty('--motion', String(t.motion));
    if(window.HeroScene) window.HeroScene.setMotion(parseFloat(t.motion));
  }
  _prev = {...t};
}

function App(){
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  React.useEffect(()=>{ applyTweaks(t); }, [t]);
  return (
    <TweaksPanel title="Tweaks">
      <TweakSection label="SOC Console" />
      <TweakRadio label="View" value={t.heroScene}
        options={['console','network','audit']}
        onChange={(v)=>setTweak('heroScene', v)} />
      <TweakSection label="Theme" />
      <TweakColor label="Accent" value={t.accent}
        options={['#4f8ef7','#2ec4b6','#a78bfa','#3dd68c']}
        onChange={(v)=>setTweak('accent', v)} />
      <TweakSection label="Motion" />
      <TweakSlider label="Intensity" value={t.motion} min={0} max={1.4} step={0.1}
        onChange={(v)=>setTweak('motion', v)} />
    </TweaksPanel>
  );
}

(function mount(){
  const root = document.getElementById('tweaks-root');
  if(!root){ return; }
  // apply defaults immediately even before panel is opened
  applyTweaks(TWEAK_DEFAULTS);
  ReactDOM.createRoot(root).render(<App/>);
})();
