(function () {
  "use strict";

  var modal = document.getElementById("polanger-shield-menu-info-modal");
  var modalBody;
  var modalTitle;
  var activeTrigger = null;
  var closeTimer = null;

  if (!modal) {
    return;
  }

  modalBody = modal.querySelector(".polanger-shield-menu-modal__body");
  modalTitle = modal.querySelector("#polanger-shield-menu-modal-title");

  function openModal(trigger) {
    var sourceId = trigger.getAttribute("data-polanger-shield-menu-source");
    var source = sourceId ? document.getElementById(sourceId) : null;

    if (!source || !modalBody || !modalTitle) {
      return;
    }

    if (closeTimer) {
      window.clearTimeout(closeTimer);
      closeTimer = null;
    }

    activeTrigger = trigger;
    modalBody.innerHTML = source.innerHTML;
    modalTitle.textContent = trigger.getAttribute("data-polanger-shield-menu-title") || "";
    modal.hidden = false;
    modal.setAttribute("aria-hidden", "false");
    trigger.setAttribute("aria-expanded", "true");
    document.documentElement.classList.add("polanger-shield-menu-modal-open");

    window.requestAnimationFrame(function () {
      modal.classList.add("is-open");
      var closeButton = modal.querySelector(".polanger-shield-menu-modal__close");
      if (closeButton) {
        closeButton.focus();
      }
    });
  }

  function closeModal() {
    if (modal.hidden) {
      return;
    }

    modal.classList.remove("is-open");
    modal.setAttribute("aria-hidden", "true");
    document.documentElement.classList.remove("polanger-shield-menu-modal-open");

    if (activeTrigger) {
      activeTrigger.setAttribute("aria-expanded", "false");
      activeTrigger.focus();
    }

    activeTrigger = null;
    closeTimer = window.setTimeout(function () {
      modal.hidden = true;
      if (modalBody) {
        modalBody.innerHTML = "";
      }
      closeTimer = null;
    }, 160);
  }

  function trapFocus(event) {
    var focusable;
    var firstFocusable;
    var lastFocusable;

    if (event.key !== "Tab" || modal.hidden) {
      return;
    }

    focusable = modal.querySelectorAll(
      'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
    );

    if (!focusable.length) {
      event.preventDefault();
      modal.querySelector(".polanger-shield-menu-modal__dialog").focus();
      return;
    }

    firstFocusable = focusable[0];
    lastFocusable = focusable[focusable.length - 1];

    if (event.shiftKey && document.activeElement === firstFocusable) {
      event.preventDefault();
      lastFocusable.focus();
    } else if (!event.shiftKey && document.activeElement === lastFocusable) {
      event.preventDefault();
      firstFocusable.focus();
    }
  }

  document.addEventListener("click", function (event) {
    var trigger = event.target.closest(".polanger-shield-menu-indicator");

    if (trigger) {
      event.preventDefault();
      event.stopPropagation();
      openModal(trigger);
      return;
    }

    if (event.target.closest("[data-polanger-shield-menu-close]")) {
      event.preventDefault();
      closeModal();
    }
  });

  document.addEventListener("keydown", function (event) {
    if (event.key === "Escape" && !modal.hidden) {
      event.preventDefault();
      closeModal();
      return;
    }

    trapFocus(event);
  });
})();
