/* eslint-disable no-undef */
// Shared icons + small UI primitives

const Icon = {
  Menu: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round">
      <path d="M4 7h16M4 12h16M4 17h16"/>
    </svg>
  ),
  Close: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round">
      <path d="M6 6l12 12M18 6L6 18"/>
    </svg>
  ),
  Instagram: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="3" width="18" height="18" rx="5"/>
      <circle cx="12" cy="12" r="4"/>
      <circle cx="17.5" cy="6.5" r="0.6" fill="currentColor"/>
    </svg>
  ),
  Tiktok: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M14 4v9.5a3.5 3.5 0 1 1-3.5-3.5"/>
      <path d="M14 4c.4 2 2 3.6 4 4"/>
    </svg>
  ),
  Pinterest: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="9"/>
      <path d="M11 8c1.5-.5 4 0 4 3s-2 4-3.5 3.5"/>
      <path d="M11.5 14.5L9.5 21"/>
    </svg>
  ),
  Mail: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="5" width="18" height="14" rx="2"/>
      <path d="M3 7l9 6 9-6"/>
    </svg>
  ),
  Arrow: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" style={{width:16,height:16}}>
      <path d="M5 12h14M13 5l7 7-7 7"/>
    </svg>
  ),
  Check: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" style={{width:24,height:24}}>
      <path d="M5 12l5 5L20 7"/>
    </svg>
  )
};

// hook: in-view reveal. Re-scans the DOM only when `deps` changes (e.g. on
// route change, when a different set of .reveal elements mounts) instead of
// on every render.
function useReveal(deps) {
  React.useEffect(() => {
    const els = document.querySelectorAll('.reveal');
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add('in');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  }, deps);
}

// hook: floating hearts while scrolling down. Purely decorative brand
// flourish — throttled hard (one heart per MIN_GAP ms, capped concurrent
// count) so it reads as an occasional sprinkle rather than noise on a
// gesture that fires constantly. Each heart is a short-lived DOM node
// animated once via CSS (GPU-only transform/opacity), never retargeted, so
// plain keyframes are fine here — nothing about it needs to be interruptible.
// Skipped entirely under reduced-motion: it's decorative movement with no
// functional purpose, so there's nothing worth keeping gentler.
function useScrollHearts() {
  React.useEffect(() => {
    if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;

    const layer = document.createElement('div');
    layer.className = 'scroll-hearts-layer';
    layer.setAttribute('aria-hidden', 'true');
    document.body.appendChild(layer);

    // Half density on phones: small screens read the same heart count as
    // confetti over the text, and scrolling is the most frequent gesture.
    const small = window.matchMedia('(max-width: 540px)').matches;
    const MIN_GAP_MS = small ? 160 : 90;
    const MAX_HEARTS = small ? 12 : 24;
    const HEART_SVG =
      '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor">' +
      '<path d="M12 21s-6.7-4.35-9.3-8.28C1 10.2 1.7 6.9 4.4 5.4c2.1-1.15 4.6-.6 6 1.15L12 8.2l1.6-1.65c1.4-1.75 3.9-2.3 6-1.15 2.7 1.5 3.4 4.8 1.7 7.32C18.7 16.65 12 21 12 21z"/></svg>';

    let lastY = window.scrollY;
    let lastSpawn = 0;

    const spawn = () => {
      if (layer.children.length >= MAX_HEARTS) return;
      const heart = document.createElement('span');
      heart.className = 'scroll-heart';
      heart.style.left = `${8 + Math.random() * 84}%`;
      heart.style.setProperty('--drift', `${(Math.random() * 40 - 20).toFixed(0)}px`);
      heart.style.setProperty('--scale', (0.6 + Math.random() * 0.5).toFixed(2));
      heart.innerHTML = HEART_SVG;
      heart.addEventListener('animationend', () => heart.remove());
      layer.appendChild(heart);
    };

    const onScroll = () => {
      const y = window.scrollY;
      const goingDown = y > lastY;
      lastY = y;
      if (!goingDown) return;
      const now = performance.now();
      if (now - lastSpawn < MIN_GAP_MS) return;
      lastSpawn = now;
      spawn();
      spawn();
    };

    window.addEventListener('scroll', onScroll, { passive: true });
    return () => {
      window.removeEventListener('scroll', onScroll);
      layer.remove();
    };
  }, []);
}

window.Icon = Icon;
window.useReveal = useReveal;
window.useScrollHearts = useScrollHearts;
