const root = document.documentElement;
root.dataset.js = "ready";

const menuToggle = document.querySelector("[data-menu-toggle]");
const menuPanel = document.querySelector("[data-menu-panel]");
const megaTriggers = [...document.querySelectorAll("[data-mega-trigger]")];
const megaMenus = [...document.querySelectorAll("[data-mega-menu]")];

if (menuToggle && menuPanel) {
  const closeMenu = () => {
    menuPanel.classList.add("hidden");
    menuToggle.setAttribute("aria-expanded", "false");
  };

  const openMenu = () => {
    menuPanel.classList.remove("hidden");
    menuToggle.setAttribute("aria-expanded", "true");
  };

  menuToggle.addEventListener("click", () => {
    const expanded = menuToggle.getAttribute("aria-expanded") === "true";
    if (expanded) {
      closeMenu();
      return;
    }
    openMenu();
  });

  window.addEventListener("resize", () => {
    if (window.innerWidth >= 1024) {
      menuPanel.classList.remove("hidden");
      menuToggle.setAttribute("aria-expanded", "true");
      return;
    }
    closeMenu();
  });
}

if (megaTriggers.length > 0 && megaMenus.length > 0) {
  const closeMegaMenus = () => {
    megaTriggers.forEach((trigger) => {
      trigger.setAttribute("aria-expanded", "false");
      trigger.closest(".desktop-nav__entry")?.classList.remove("desktop-nav__entry--open");
    });
    megaMenus.forEach((menu) => {
      menu.classList.add("hidden");
    });
  };

  const openMegaMenu = (trigger) => {
    const target = trigger.dataset.megaTarget;
    if (!target) return;
    closeMegaMenus();
    trigger.setAttribute("aria-expanded", "true");
    trigger.closest(".desktop-nav__entry")?.classList.add("desktop-nav__entry--open");
    document.getElementById(target)?.classList.remove("hidden");
  };

  megaTriggers.forEach((trigger) => {
    trigger.addEventListener("click", (event) => {
      if (window.innerWidth < 1024) return;
      const expanded = trigger.getAttribute("aria-expanded") === "true";
      const isLinkTrigger = trigger instanceof HTMLAnchorElement;
      if (isLinkTrigger && expanded) {
        return;
      }
      event.preventDefault();
      if (expanded) {
        closeMegaMenus();
        return;
      }
      openMegaMenu(trigger);
    });
  });

  document.addEventListener("click", (event) => {
    if (window.innerWidth < 1024) return;
    const target = event.target;
    if (!(target instanceof Node)) return;
    const insideTrigger = megaTriggers.some((trigger) => trigger.contains(target));
    const insideMenu = megaMenus.some((menu) => menu.contains(target));
    if (!insideTrigger && !insideMenu) {
      closeMegaMenus();
    }
  });

  document.addEventListener("keydown", (event) => {
    if (event.key === "Escape") {
      closeMegaMenus();
    }
  });

  window.addEventListener("resize", () => {
    if (window.innerWidth < 1024) {
      closeMegaMenus();
    }
  });
}

const cartButton = document.querySelector("button[type='submit']");
if (cartButton) {
  cartButton.addEventListener("mouseenter", () => {
    root.style.setProperty("--cta-glow", "0 0 0 4px rgba(252, 211, 77, 0.12)");
  });
  cartButton.addEventListener("mouseleave", () => {
    root.style.setProperty("--cta-glow", "0 0 0 0 rgba(0,0,0,0)");
  });
}
