/**
 * glitch.js
 * Random glitch effect on .hero-name every 4-11 seconds
 */

export function initGlitch() {
  'use strict';

  const heroName = document.querySelector('.hero-name');
  if (!heroName) return;

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

  function triggerGlitch() {
    heroName.classList.add('is-glitching');
    setTimeout(() => {
      heroName.classList.remove('is-glitching');
    }, 450);
  }

  function scheduleNextGlitch() {
    const delay = Math.random() * 7000 + 4000; // 4-11 seconds
    setTimeout(() => {
      triggerGlitch();
      scheduleNextGlitch();
    }, delay);
  }

  scheduleNextGlitch();
}
