/* Shared hooks + small helpers for Swift Labs */

// Reveal-on-scroll: attaches IntersectionObserver to all [data-reveal] once mounted
function useReveal() {
  React.useEffect(() => {
    const els = document.querySelectorAll('.reveal');
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) {
          const el = e.target;
          const delay = el.getAttribute('data-delay') || 0;
          setTimeout(() => el.classList.add('in'), delay);
          io.unobserve(el);
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -6% 0px' });
    els.forEach((el) => io.observe(el));
    // Safety net: if anything is still hidden after 2.6s (observer missed,
    // element already fully in view, tab restored, etc.) reveal it.
    const fallback = setTimeout(() => {
      document.querySelectorAll('.reveal:not(.in)').forEach((el) => el.classList.add('in'));
    }, 2600);
    return () => { io.disconnect(); clearTimeout(fallback); };
  }, []);
}

// Magnetic hover for an element ref
function useMagnetic(strength = 0.35) {
  const ref = React.useRef(null);
  React.useEffect(() => {
    const el = ref.current;
    if (!el || window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
    const onMove = (e) => {
      const r = el.getBoundingClientRect();
      const x = e.clientX - (r.left + r.width / 2);
      const y = e.clientY - (r.top + r.height / 2);
      el.style.transform = `translate(${x * strength}px, ${y * strength}px)`;
    };
    const onLeave = () => { el.style.transform = 'translate(0,0)'; };
    el.addEventListener('mousemove', onMove);
    el.addEventListener('mouseleave', onLeave);
    return () => { el.removeEventListener('mousemove', onMove); el.removeEventListener('mouseleave', onLeave); };
  }, [strength]);
  return ref;
}

// Brand mark — the wordmark IS the logo: "swiftlabs." with a lime period
function BrandMark() {
  return (
    <span className="brandmark" aria-label="swiftlabs">
      swiftlabs<span className="dot" aria-hidden="true">.</span>
    </span>
  );
}

Object.assign(window, { useReveal, useMagnetic, BrandMark });
