document.getElementById('year').textContent = new Date().getFullYear();

// ─── Confirm Modal (Windows DL) ─────────────────
// data-confirm="windows-dl" 付きの要素クリックを横取りして、ダウンロード前に
// モーダルで挙動を説明する。GA4 計装はモーダル「ダウンロード」確定時に発火。
(function () {
  var modal = document.getElementById('confirm-modal');
  if (!modal) return;
  var btnConfirm = modal.querySelector('.modal-confirm');
  var btnCancel = modal.querySelector('.modal-cancel');
  var lastFocused = null;
  var pendingHref = '';
  var pendingPlatform = '';

  function openModal(href, platform) {
    pendingHref = href;
    pendingPlatform = platform;
    lastFocused = document.activeElement;
    modal.classList.add('is-open');
    modal.setAttribute('aria-hidden', 'false');
    document.body.style.overflow = 'hidden';
    setTimeout(function () { btnCancel.focus(); }, 0);
  }

  function closeModal() {
    modal.classList.remove('is-open');
    modal.setAttribute('aria-hidden', 'true');
    document.body.style.overflow = '';
    pendingHref = '';
    pendingPlatform = '';
    if (lastFocused && typeof lastFocused.focus === 'function') {
      lastFocused.focus();
    }
  }

  function confirmDownload() {
    var href = pendingHref;
    var platform = pendingPlatform;
    if (href && typeof window.gtag === 'function') {
      window.gtag('event', 'download_click', {
        platform: platform,
        link_url: href,
        outbound: false,
        confirmed: true
      });
    }
    closeModal();
    if (href) {
      window.location.href = href;
    }
  }

  // capture: true で先頭で横取り → GA4 listener が走る前に preventDefault
  document.addEventListener('click', function (e) {
    var el = e.target.closest('[data-confirm="windows-dl"]');
    if (!el) return;
    e.preventDefault();
    e.stopImmediatePropagation();
    openModal(el.getAttribute('href') || '', el.getAttribute('data-platform') || 'windows');
  }, true);

  btnConfirm.addEventListener('click', confirmDownload);
  btnCancel.addEventListener('click', closeModal);
  modal.addEventListener('click', function (e) {
    // overlay (modal 自体) クリックで閉じる、dialog 内クリックは閉じない
    if (e.target === modal) closeModal();
  });
  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape' && modal.classList.contains('is-open')) closeModal();
  });
})();

// ─── GA4 イベント計装 ───────────────────────────
// data-track="<event_name>" data-platform="<platform>" が付いた要素のクリックを
// gtag に送る。gtag が未ロード (例: GA4 ID 未設定 / ブロッカー) でも安全。
(function () {
  function track(eventName, params) {
    if (typeof window.gtag === 'function') {
      window.gtag('event', eventName, params);
    }
  }

  document.addEventListener('click', function (e) {
    var el = e.target.closest('[data-track]');
    if (!el) return;
    var eventName = el.getAttribute('data-track');
    var platform = el.getAttribute('data-platform') || '';
    var href = el.getAttribute('href') || '';
    track(eventName, {
      platform: platform,
      link_url: href,
      // GA4 推奨パラメータ名にも寄せる
      outbound: href.startsWith('http') && href.indexOf(location.hostname) === -1
    });
  }, { passive: true });
})();
