// Shared hooks
const { useState, useEffect, useRef, useCallback, useMemo } = React;

// Cursor tracking with smoothing (exposed globally via window.__mouse__)
function useMouse() {
  const [pos, setPos] = useState({ x: window.innerWidth / 2, y: window.innerHeight / 2 });
  useEffect(() => {
    const move = (e) => {
      const p = { x: e.clientX, y: e.clientY };
      setPos(p);
      window.__mouse__ = p;
    };
    window.__mouse__ = { x: window.innerWidth / 2, y: window.innerHeight / 2 };
    window.addEventListener('mousemove', move);
    return () => window.removeEventListener('mousemove', move);
  }, []);
  return pos;
}

function useTweaks() {
  const [t, setT] = useState(window.__TWEAKS__ || {});
  useEffect(() => {
    const handler = (e) => { if (e.detail) setT((prev) => ({ ...prev, ...e.detail })); };
    window.addEventListener('tweak:change', handler);
    return () => window.removeEventListener('tweak:change', handler);
  }, []);
  return t;
}

// Animation frame hook
function useRaf(cb, deps = []) {
  useEffect(() => {
    let raf;
    let last = performance.now();
    const loop = (t) => {
      const dt = Math.min(t - last, 50);
      last = t;
      cb(t, dt);
      raf = requestAnimationFrame(loop);
    };
    raf = requestAnimationFrame(loop);
    return () => cancelAnimationFrame(raf);
  }, deps);
}

window.useMouse = useMouse;
window.useTweaks = useTweaks;
window.useRaf = useRaf;
