// no prior work found 2026-05-09
(() => {
  const player = document.querySelector('[data-demo-player]');
  if (!player) return;

  const video = player.querySelector('video');
  const volume = player.querySelector('input[type="range"]');
  const previewButtons = Array.from(player.querySelectorAll('[data-preview-target]'));
  const progress = player.querySelector('[data-progress]');
  if (!video || !volume || previewButtons.length !== 3 || !progress) return;

  let restartTimer = 0;
  let allowMutedFallback = true;
  let triedFallback = false;
  let autoPreviewEnabled = true;
  let pendingStartMode = 'auto';

  const stopRestartTimer = () => {
    window.clearTimeout(restartTimer);
    restartTimer = 0;
  };

  const setPreviewState = (state) => {
    player.dataset.previewState = state;
    progress.hidden = state !== 'video';
    previewButtons.forEach((button) => {
      const isActive = button.dataset.previewTarget === state;
      button.classList.toggle('is-active', isActive);
      button.setAttribute('aria-pressed', String(isActive));
    });
  };

  const updateProgress = () => {
    const duration = Number.isFinite(video.duration) && video.duration > 0 ? video.duration : 0;
    const currentTime = Math.max(0, Math.min(video.currentTime || 0, duration || video.currentTime || 0));
    const percent = duration ? (currentTime / duration) * 100 : 0;
    progress.value = Math.round(percent);
  };

  const setVolumeFill = () => {
    const value = Number(volume.value || 0);
    video.volume = Math.max(0, Math.min(1, value / 100));
  };

  const stopVideo = ({ reset = false } = {}) => {
    stopRestartTimer();
    video.pause();
    if (reset) {
      video.currentTime = 0;
      updateProgress();
    }
  };

  const playDemo = ({ manual = false, restart = false } = {}) => {
    if (!manual && !autoPreviewEnabled) return;
    pendingStartMode = null;
    stopRestartTimer();
    if (restart) {
      video.currentTime = 0;
      updateProgress();
    }

    const activateVideoPreview = () => {
      setPreviewState('video');
      updateProgress();
    };

    const attempt = video.play();
    if (attempt && typeof attempt.catch === 'function') {
      attempt
        .then(activateVideoPreview)
        .catch(() => {
          if (!allowMutedFallback) return;
          allowMutedFallback = false;
          video.muted = true;
          video.play().then(activateVideoPreview).catch(() => {});
        });
      return;
    }
    activateVideoPreview();
  };

  const requestVideoStart = (mode) => {
    pendingStartMode = mode;
    if (video.readyState >= 2) {
      playDemo({ manual: mode === 'manual', restart: mode === 'manual' });
      return;
    }
    video.load();
  };

  const restartCycle = () => {
    if (!autoPreviewEnabled) {
      updateProgress();
      return;
    }

    setPreviewState('logo');
    restartTimer = window.setTimeout(() => {
      pendingStartMode = 'auto';
      video.currentTime = 0;
      updateProgress();
      playDemo();
    }, 2000);
  };

  const showManualPreview = (state) => {
    autoPreviewEnabled = false;
    if (state === 'video') {
      requestVideoStart('manual');
      return;
    }

    stopVideo({ reset: true });
    setPreviewState(state);
  };

  volume.addEventListener('input', () => {
    video.muted = false;
    setVolumeFill();
  });

  previewButtons.forEach((button) => {
    button.addEventListener('click', () => {
      showManualPreview(button.dataset.previewTarget);
    });
  });

  video.addEventListener('ended', restartCycle);
  video.addEventListener('timeupdate', updateProgress);
  video.addEventListener('loadedmetadata', updateProgress);
  video.addEventListener('durationchange', updateProgress);
  video.addEventListener('loadeddata', () => {
    updateProgress();
    if (pendingStartMode === 'manual') {
      playDemo({ manual: true, restart: true });
      return;
    }
    if (pendingStartMode === 'auto' && autoPreviewEnabled) {
      playDemo();
    }
  });
  video.addEventListener('error', () => {
    const fallbackSrc = video.dataset.fallbackSrc;
    if (!triedFallback && fallbackSrc) {
      triedFallback = true;
      setPreviewState('logo');
      video.src = fallbackSrc;
      video.load();
      return;
    }
    stopVideo({ reset: true });
    setPreviewState('logo');
  });

  setPreviewState('logo');
  setVolumeFill();
  updateProgress();
  requestVideoStart('auto');
})();