const LAUNCH = new Date('2026-04-30T16:00:00+03:00');


function pad2(n) {
  return String(n).padStart(2, '0');
}


function tickCountdown() {
  const root = document.getElementById('countdown');
  if (!root) {
    return false;
  }
  const doneEl = document.getElementById('countdown-done');
  const diff = LAUNCH.getTime() - Date.now();
  if (diff <= 0) {
    root.querySelectorAll('.countdown__units, .countdown__title').forEach((el) => {
      el.setAttribute('hidden', '');
    });
    if (doneEl) {
      doneEl.removeAttribute('hidden');
    }
    return false;
  }
  const totalSeconds = Math.floor(diff / 1000);
  const days = Math.floor(totalSeconds / 86400);
  const hours = Math.floor((totalSeconds % 86400) / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  const seconds = totalSeconds % 60;
  const map = {
    days: days < 100 ? pad2(days) : String(days),
    hours: pad2(hours),
    minutes: pad2(minutes),
    seconds: pad2(seconds),
  };
  Object.keys(map).forEach((key) => {
    const el = root.querySelector(`[data-unit="${key}"]`);
    if (el) {
      el.textContent = map[key];
    }
  });
  return true;
}


function initNav() {
  const btn = document.querySelector('.burger');
  if (!btn) {
    return;
  }
  btn.addEventListener('click', () => {
    document.body.classList.toggle('nav-open');
  });
  document.querySelectorAll('.nav__link').forEach((link) => {
    link.addEventListener('click', () => {
      document.body.classList.remove('nav-open');
    });
  });
}


function startCountdown() {
  if (!tickCountdown()) {
    return;
  }
  const countdownTimer = setInterval(() => {
    if (!tickCountdown()) {
      clearInterval(countdownTimer);
    }
  }, 1000);
}


startCountdown();
initNav();
