'use strict';



/**
 * MOBILE NAVBAR TOGGLE
 */

document.addEventListener('DOMContentLoaded', function () {
  const navbar = document.querySelector('[data-navbar]');
  const navToggler = document.querySelector('[data-nav-toggler]');

  if (navToggler && navbar) {
    navToggler.addEventListener('click', function (e) {
      e.preventDefault();
      navbar.classList.toggle('active');
      this.classList.toggle('active');
    });
  }

  // Alternatif checkbox tabanlı menü (varsa) için güvenli bağlama
  const check = document.getElementById('check-menu');
  const toggle = document.querySelector('.toggle-menu');
  const closeBtn = document.querySelector('.close-btn');
  const nav = document.getElementById('nav');

  if (toggle && check) {
    toggle.addEventListener('click', function (e) {
      e.preventDefault();
      check.checked = !check.checked;
    });
  }

  if (closeBtn) {
    closeBtn.addEventListener('click', function (e) {
      e.preventDefault();
      if (check) check.checked = false;
      if (navbar) navbar.classList.remove('active');
      if (navToggler) navToggler.classList.remove('active');
    });
  }

  if (nav && check) {
    nav.querySelectorAll('a').forEach(function (a) {
      a.addEventListener('click', function () { check.checked = false; });
    });
  }

  // ESC ile menüyü kapat
  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape') {
      if (check) check.checked = false;
      if (navbar) navbar.classList.remove('active');
      if (navToggler) navToggler.classList.remove('active');
    }
  });

  // Dış alan/overlay tıklaması ile menüyü kapat
  function closeMenu() {
    if (check) check.checked = false;
    if (navbar) navbar.classList.remove('active');
    if (navToggler) navToggler.classList.remove('active');
  }

  // Bilinen overlay adayları
  document.querySelectorAll('[data-overlay], .nav-overlay, .header-overlay').forEach(function (ov) {
    ov.addEventListener('click', function (e) {
      e.preventDefault();
      closeMenu();
    });
  });

  // Genel dış alan tıklaması (mobil menü açıksa ve nav dışında bir yere tıklandıysa kapat)
  document.addEventListener('click', function (e) {
    var menuOpen = (check && check.checked) || (navbar && navbar.classList.contains('active'));
    if (!menuOpen) return;
    var inside = e.target.closest('#nav') || e.target.closest('[data-navbar]') || e.target.closest('[data-nav-toggler]') || e.target.closest('.toggle-menu') || e.target.closest('.close-btn');
    if (!inside) {
      closeMenu();
    }
  });

  // Mobile submenu (drop-btn → drop-box) – event delegation to support AJAX-updated DOM
  document.addEventListener('click', function (e) {
    var btn = e.target.closest('.drop-btn');
    if (!btn) return;
    // Limit to mobile state: checkbox open or small viewport or navbar active
    var isMobile = (window.innerWidth <= 992) || (check && check.checked) || (navbar && navbar.classList.contains('active'));
    if (!isMobile) return;
    var li = btn.closest('li');
    if (!li) return;
    var box = li.querySelector('.drop-box');
    if (!box) return;
    e.preventDefault();
    // Close siblings
    if (li.parentElement) {
      Array.prototype.forEach.call(li.parentElement.children, function (sib) {
        if (sib !== li) sib.classList.remove('open');
      });
    }
    var isOpen = li.classList.contains('open');
    li.classList.toggle('open', !isOpen);
    btn.setAttribute('role', 'button');
    btn.setAttribute('tabindex', '0');
    btn.setAttribute('aria-expanded', String(!isOpen));
  }, false);

  document.addEventListener('keydown', function (e) {
    if (e.key !== 'Enter' && e.key !== ' ') return;
    var btn = e.target.closest('.drop-btn');
    if (!btn) return;
    var isMobile = (window.innerWidth <= 992) || (check && check.checked) || (navbar && navbar.classList.contains('active'));
    if (!isMobile) return;
    var li = btn.closest('li');
    if (!li) return;
    var box = li.querySelector('.drop-box');
    if (!box) return;
    e.preventDefault();
    var isOpen = li.classList.contains('open');
    if (li.parentElement) {
      Array.prototype.forEach.call(li.parentElement.children, function (sib) {
        if (sib !== li) sib.classList.remove('open');
      });
    }
    li.classList.toggle('open', !isOpen);
    btn.setAttribute('aria-expanded', String(!isOpen));
  }, false);
});