/* global React */
const { useState, useEffect, useRef } = React;

/* ============================================================
   Mouse parallax — gentle, RAF-smoothed
============================================================ */
function useMouseParallax(strength = 14) {
  const target = useRef({ x: 0, y: 0 });
  const [pos, setPos] = useState({ x: 0, y: 0 });
  const raf = useRef(null);
  useEffect(() => {
    const onMove = (e) => {
      target.current = {
        x: (e.clientX / window.innerWidth  - 0.5) * strength,
        y: (e.clientY / window.innerHeight - 0.5) * strength,
      };
    };
    window.addEventListener("mousemove", onMove, { passive: true });
    const cur = { x: 0, y: 0 };
    const tick = () => {
      cur.x += (target.current.x - cur.x) * 0.06;
      cur.y += (target.current.y - cur.y) * 0.06;
      setPos({ x: +cur.x.toFixed(2), y: +cur.y.toFixed(2) });
      raf.current = requestAnimationFrame(tick);
    };
    raf.current = requestAnimationFrame(tick);
    return () => {
      window.removeEventListener("mousemove", onMove);
      if (raf.current) cancelAnimationFrame(raf.current);
    };
  }, [strength]);
  return pos;
}

/* ============================================================
   Reveal-on-scroll
============================================================ */
function useReveal() {
  useEffect(() => {
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => { if (e.isIntersecting) e.target.classList.add("in"); });
    }, { threshold: 0.12 });
    document.querySelectorAll(".reveal").forEach(el => io.observe(el));
    return () => io.disconnect();
  }, []);
}

window.useMouseParallax = useMouseParallax;
window.useReveal = useReveal;
