/// file: public/assets/js/modules/home-ambient-lights.js

/**
 * Home hero: soft sliding light streaks — starts after preloader, 25s total then hidden.
 */

const DONE_MS = 26_000;

export function initHomeAmbientLights() {
  const layer = document.querySelector('[data-home-ambient-lights]');
  if (!layer) return;

  if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
    return;
  }

  const start = () => {
    if (layer.classList.contains('hero__ambient-lights--playing')) return;
    layer.classList.add('hero__ambient-lights--playing');
    window.setTimeout(() => {
      layer.classList.add('hero__ambient-lights--done');
    }, DONE_MS);
  };

  const preloader = document.getElementById('preloader');
  if (!preloader || preloader.classList.contains('is-done')) {
    requestAnimationFrame(start);
    return;
  }

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