/// file: public/assets/js/modules/home-intro.js

/**
 * Full-viewport home gate: after preloader, scroll is locked until the user clicks
 * the overlay.
 */

const EXIT_MS = 1180;

/** Set in wireHomeIntroGate */
let runIntroExit = /** @type {(() => void) | null} */ (null);

/** Same exit as overlay click (shatter + site reveal); reserved for future gestures. */
export function requestHomeIntroExit() {
  runIntroExit?.();
}

function scrollHomeToMainContent() {
  const below = document.querySelector('[data-home-below-hero]');
  if (!below) return;
  const header = document.getElementById('site-header');
  const hh = Math.round(header?.getBoundingClientRect().height ?? 0);
  const gap = 12;
  const doc = document.documentElement;
  const maxScroll = Math.max(0, doc.scrollHeight - window.innerHeight);
  const desired = Math.max(
    0,
    Math.round(
      below.getBoundingClientRect().top + window.pageYOffset - hh - gap
    )
  );
  /* Short home (little below the hero): snapping to manifesto would clamp to page bottom and feel like scroll is broken. */
  const y = desired > maxScroll ? 0 : desired;
  window.scrollTo(0, y);
  doc.scrollTop = y;
  document.body.scrollTop = y;
}

function scheduleScrollHomeToContent() {
  const run = () => scrollHomeToMainContent();
  document.documentElement.offsetHeight;
  requestAnimationFrame(() => {
    run();
    requestAnimationFrame(() => {
      run();
      window.setTimeout(run, 0);
      window.setTimeout(run, 120);
      window.setTimeout(run, 320);
    });
  });
}

function wireHomeIntroGate() {
  const preloader = document.getElementById('preloader');
  const body = document.body;
  const html = document.documentElement;
  const hit = document.querySelector('[data-home-intro-hit]');
  if (!(hit instanceof HTMLButtonElement)) return;

  const lock = () => {
    html.classList.add('home-intro-pending');
    body.classList.add('home-intro-pending');
    hit.disabled = false;
    hit.setAttribute('aria-hidden', 'false');
  };

  if (preloader && !preloader.classList.contains('is-done')) {
    const mo = new MutationObserver(() => {
      if (preloader.classList.contains('is-done')) {
        mo.disconnect();
        lock();
      }
    });
    mo.observe(preloader, { attributes: true, attributeFilter: ['class'] });
  } else {
    lock();
  }

  const finish = () => {
    window.__saneksaIntroSwipeProgress = 0;
    html.classList.remove('home-intro-pending', 'home-intro-leaving');
    body.classList.remove('home-intro-pending', 'home-intro-leaving');
    html.classList.add('home-intro-done');
    body.classList.add('home-intro-done');
    hit.disabled = true;
    hit.classList.add('hidden');
    hit.setAttribute('aria-hidden', 'true');
    scheduleScrollHomeToContent();
  };

  const onEnter = () => {
    if (body.classList.contains('home-intro-leaving')) return;
    document.querySelector('.hero-resn__title-wrap')?.style.removeProperty('transform');
    html.classList.add('home-intro-leaving');
    body.classList.add('home-intro-leaving');
    window.dispatchEvent(new CustomEvent('saneksa:hero-intro-exit'));
    window.setTimeout(finish, EXIT_MS);
  };

  runIntroExit = onEnter;
  hit.addEventListener('click', onEnter);
}

export function initHomeIntro() {
  const root = document.querySelector('[data-home-intro]');
  const hit = document.querySelector('[data-home-intro-hit]');
  if (!root || !(hit instanceof HTMLButtonElement)) return;

  const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  if (reduced) {
    document.documentElement.classList.add('home-intro-done');
    document.body.classList.add('home-intro-done');
    hit.disabled = true;
    hit.classList.add('hidden');
    hit.setAttribute('aria-hidden', 'true');
    scheduleScrollHomeToContent();
    return;
  }

  wireHomeIntroGate();
}
