// smooth-scroll.jsx — fluid in-page navigation
// Intercepts all <a href="#...">, runs a JS-driven scroll with easing.

(function() {
  // easeInOutCubic — feels natural, neither linear nor too bouncy
  const easeInOutCubic = (t) =>
    t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;

  const HEADER_OFFSET = 72; // matches sticky nav height

  function smoothScrollTo(targetY, duration = 750) {
    const startY = window.scrollY || window.pageYOffset;
    const distance = targetY - startY;
    if (Math.abs(distance) < 4) return;

    // scale duration with distance — short jumps stay snappy, long jumps feel intentional
    const scaled = Math.min(
      1100,
      Math.max(450, duration + Math.abs(distance) * 0.25)
    );

    let startT = null;
    let cancelled = false;

    const cancel = () => { cancelled = true; };
    // user wheel / touch / keydown cancels the animation immediately
    window.addEventListener('wheel', cancel, { passive: true, once: true });
    window.addEventListener('touchstart', cancel, { passive: true, once: true });
    window.addEventListener('keydown', cancel, { once: true });

    function step(ts) {
      if (cancelled) return;
      if (startT === null) startT = ts;
      const elapsed = ts - startT;
      const t = Math.min(1, elapsed / scaled);
      const eased = easeInOutCubic(t);
      window.scrollTo(0, startY + distance * eased);
      if (t < 1) requestAnimationFrame(step);
    }
    requestAnimationFrame(step);
  }

  function handleClick(e) {
    // ignore modified clicks (open in new tab, etc.)
    if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) return;

    const a = e.target.closest('a[href]');
    if (!a) return;
    const href = a.getAttribute('href');
    if (!href || href === '#' || !href.startsWith('#')) return;

    const id = href.slice(1);
    const target = document.getElementById(id);
    if (!target) return;

    e.preventDefault();
    const rect = target.getBoundingClientRect();
    const targetY = rect.top + window.scrollY - HEADER_OFFSET;
    smoothScrollTo(Math.max(0, targetY));

    // update URL hash without jumping
    if (history.pushState) {
      history.pushState(null, '', href);
    }
  }

  document.addEventListener('click', handleClick);
})();
