/* =========================================================
   P3 Desktop Mega Menu Close Fix
   Real Zoho mega menu selector: .zpmm
   Active class: .mmactive
========================================================= */

(function () {
  "use strict";

  var DESKTOP_MIN = 992;
  var CLOSE_CLASS = "p3-zpmm-closed";

  function isDesktop() {
    return window.innerWidth >= DESKTOP_MIN;
  }

  function getRedMenu() {
    return (
      document.querySelector("[data-zp-menu-top]") ||
      document.querySelector(".theme-menu") ||
      document.querySelector(".theme-menu-area")
    );
  }

  function getMegaMenus() {
    return Array.prototype.slice.call(document.querySelectorAll(".zpmm"));
  }

  function closeMegaMenu() {
    if (!isDesktop()) return;

    document.body.classList.add(CLOSE_CLASS);

    getMegaMenus().forEach(function (menu) {
      menu.classList.remove("mmactive");
      menu.style.setProperty("display", "none", "important");
      menu.style.setProperty("visibility", "hidden", "important");
      menu.style.setProperty("opacity", "0", "important");
      menu.style.setProperty("pointer-events", "none", "important");
    });

    document.querySelectorAll("[data-zp-menu-top] li").forEach(function (item) {
      item.classList.remove(
        "theme-menu-selected",
        "theme-menu-active",
        "active",
        "open",
        "show",
        "hover"
      );
    });

    document.querySelectorAll("[aria-expanded='true']").forEach(function (link) {
      link.setAttribute("aria-expanded", "false");
    });
  }

  function unlockMegaMenu() {
    if (!isDesktop()) return;

    document.body.classList.remove(CLOSE_CLASS);

    getMegaMenus().forEach(function (menu) {
      menu.style.removeProperty("display");
      menu.style.removeProperty("visibility");
      menu.style.removeProperty("opacity");
      menu.style.removeProperty("pointer-events");
    });
  }

  function isInsideMegaMenu(target) {
    if (!target || !target.closest) return false;

    return Boolean(
      target.closest(".zpmm") ||
      target.closest("[data-zp-menu-top]") ||
      target.closest(".theme-menu") ||
      target.closest(".theme-menu-area")
    );
  }

  document.addEventListener(
    "pointerover",
    function (event) {
      if (!isDesktop()) return;

      if (
        event.target.closest &&
        (
          event.target.closest("[data-zp-menu-top]") ||
          event.target.closest(".theme-menu") ||
          event.target.closest(".theme-menu-area")
        )
      ) {
        unlockMegaMenu();
      }
    },
    true
  );

  document.addEventListener(
    "pointermove",
    function (event) {
      if (!isDesktop()) return;

      var redMenu = getRedMenu();
      if (!redMenu) return;

      var rect = redMenu.getBoundingClientRect();

      /*
        Main rule:
        If cursor goes above the red navigation menu,
        close the mega menu.
      */
      if (event.clientY < rect.top) {
        closeMegaMenu();
        return;
      }

      /*
        If cursor is outside both red menu and white mega menu,
        close it.
      */
      if (!isInsideMegaMenu(event.target)) {
        closeMegaMenu();
      }
    },
    true
  );

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

  window.addEventListener("scroll", function () {
    closeMegaMenu();
  });

  window.addEventListener("resize", function () {
    if (!isDesktop()) {
      document.body.classList.remove(CLOSE_CLASS);
    }
  });
})();