(function initMyronPublicNavigation(global) {
  "use strict";

  function onReady(callback) {
    if (global.document.readyState === "loading") {
      global.document.addEventListener("DOMContentLoaded", callback, { once: true });
      return;
    }

    callback();
  }

  function toArray(list) {
    return Array.prototype.slice.call(list || []);
  }

  function getFocusable(container) {
    return toArray(
      container.querySelectorAll(
        'a[href], button:not([disabled]), input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
      )
    ).filter(function filterHidden(node) {
      return !node.hasAttribute("hidden") && node.getAttribute("aria-hidden") !== "true";
    });
  }

  function isAppearanceMode(value) {
    return value === "light" || value === "dark";
  }

  function storageAvailable() {
    try {
      var key = "__myron_appearance_probe__";
      global.localStorage.setItem(key, "1");
      global.localStorage.removeItem(key);
      return true;
    } catch (error) {
      return false;
    }
  }

  function readStoredAppearance(canStore) {
    if (!canStore) {
      return "";
    }
    try {
      var value = global.localStorage.getItem("myron-appearance") || "";
      return isAppearanceMode(value) ? value : "";
    } catch (error) {
      return "";
    }
  }

  function writeStoredAppearance(canStore, mode) {
    if (!canStore || !isAppearanceMode(mode)) {
      return;
    }
    try {
      global.localStorage.setItem("myron-appearance", mode);
    } catch (error) {
    }
  }

  function systemPrefersDark() {
    return !!(global.matchMedia && global.matchMedia("(prefers-color-scheme: dark)").matches);
  }

  function resolveInitialAppearance(policy, defaultMode, systemFollow, storedMode) {
    if (policy === "light" || policy === "dark") {
      return policy;
    }
    if (isAppearanceMode(storedMode)) {
      return storedMode;
    }
    if (systemFollow && systemPrefersDark()) {
      return "dark";
    }
    return isAppearanceMode(defaultMode) ? defaultMode : "light";
  }

  function syncAppearanceButtons(mode) {
    toArray(global.document.querySelectorAll("[data-myron-appearance-toggle]")).forEach(function sync(button) {
      button.setAttribute("data-appearance-state", mode);
      button.setAttribute("aria-pressed", mode === "dark" ? "true" : "false");
    });
  }

  function applyAppearance(mode) {
    var resolved = isAppearanceMode(mode) ? mode : "light";
    global.document.documentElement.setAttribute("data-appearance", resolved);
    syncAppearanceButtons(resolved);
  }

  function initAppearanceMode() {
    var html = global.document.documentElement;
    var policy = html.getAttribute("data-appearance-policy") || "auto";
    var defaultMode = html.getAttribute("data-appearance-default") || "light";
    var systemFollow = html.getAttribute("data-appearance-system-follow") === "true";
    var canStore = storageAvailable();

    if (policy === "light" || policy === "dark") {
      applyAppearance(policy);
      return;
    }

    var stored = readStoredAppearance(canStore);
    var bootstrapped = html.getAttribute("data-appearance") || "";
    var initial = isAppearanceMode(bootstrapped)
      ? bootstrapped
      : resolveInitialAppearance(policy, defaultMode, systemFollow, stored);
    applyAppearance(initial);

    if (systemFollow && !stored && global.matchMedia) {
      var colorQuery = global.matchMedia("(prefers-color-scheme: dark)");
      var onSystemChange = function onSystemChange(event) {
        if (readStoredAppearance(canStore)) {
          return;
        }
        applyAppearance(event.matches ? "dark" : "light");
      };
      if (typeof colorQuery.addEventListener === "function") {
        colorQuery.addEventListener("change", onSystemChange);
      } else if (typeof colorQuery.addListener === "function") {
        colorQuery.addListener(onSystemChange);
      }
    }

    toArray(global.document.querySelectorAll("[data-myron-appearance-toggle]")).forEach(function bindAppearance(button) {
      button.addEventListener("click", function onAppearanceClick(event) {
        event.preventDefault();
        var current = html.getAttribute("data-appearance") === "dark" ? "dark" : "light";
        var next = current === "dark" ? "light" : "dark";
        applyAppearance(next);
        writeStoredAppearance(canStore, next);
      });
    });
  }

  function bindNavigation(root) {
    var toggle = root.querySelector("[data-myron-public-nav-toggle]");
    var panel = root.querySelector("[data-myron-public-nav-panel]");
    var mobileQuery = global.matchMedia ? global.matchMedia("(max-width: 640px)") : null;
    var previousFocus = null;
    var suppressNextSubmenuFocusOpen = false;

    if (!toggle || !panel) {
      return;
    }

    var closeButtons = toArray(panel.querySelectorAll("[data-myron-public-nav-close]"));
    var submenuToggles = toArray(panel.querySelectorAll("[data-myron-public-submenu-toggle]"));
    var panelHome = panel.parentNode;
    var panelHomeNextSibling = panel.nextSibling;

    function isMobileMode() {
      return !mobileQuery || mobileQuery.matches;
    }

    function usesTakeoverMode() {
      return root.getAttribute("data-mobile-mode") === "takeover";
    }

    function portalPanel() {
      if (panel.parentNode === global.document.body) {
        return;
      }
      global.document.body.appendChild(panel);
      panel.setAttribute("data-myron-public-nav-portal", "viewport");
    }

    function restorePanel() {
      if (!panelHome || panel.parentNode === panelHome) {
        panel.removeAttribute("data-myron-public-nav-portal");
        return;
      }
      if (panelHomeNextSibling && panelHomeNextSibling.parentNode === panelHome) {
        panelHome.insertBefore(panel, panelHomeNextSibling);
      } else {
        panelHome.appendChild(panel);
      }
      panel.removeAttribute("data-myron-public-nav-portal");
    }

    function submenuForToggle(submenuToggle) {
      var id = submenuToggle.getAttribute("aria-controls") || "";
      return id ? global.document.getElementById(id) : null;
    }

    function setSubmenuOpen(submenuToggle, isOpen) {
      var submenu = submenuForToggle(submenuToggle);
      if (!submenu) {
        return;
      }
      submenu.dataset.state = isOpen ? "open" : "closed";
      submenuToggle.setAttribute("aria-expanded", isOpen ? "true" : "false");
    }

    function closeSubmenus(exceptToggle) {
      submenuToggles.forEach(function closeSubmenu(submenuToggle) {
        if (exceptToggle && submenuToggle === exceptToggle) {
          return;
        }
        setSubmenuOpen(submenuToggle, false);
      });
    }

    function firstOpenSubmenuToggle() {
      var openToggle = null;
      submenuToggles.some(function findOpen(submenuToggle) {
        if (submenuToggle.getAttribute("aria-expanded") === "true") {
          openToggle = submenuToggle;
          return true;
        }
        return false;
      });
      return openToggle;
    }

    function setDesktopOpen() {
      panel.dataset.state = "open";
      panel.setAttribute("aria-hidden", "false");
      panel.removeAttribute("inert");
      toggle.setAttribute("aria-expanded", "false");
      global.document.body.classList.remove("myron-public-nav-lock");
    }

    function setOpen(isOpen) {
      if (!isMobileMode()) {
        setDesktopOpen();
        return;
      }

      panel.dataset.state = isOpen ? "open" : "closed";
      panel.setAttribute("aria-hidden", isOpen ? "false" : "true");
      panel.toggleAttribute("inert", !isOpen);
      toggle.setAttribute("aria-expanded", isOpen ? "true" : "false");
      global.document.body.classList.toggle("myron-public-nav-lock", isOpen);
      if (!isOpen) {
        closeSubmenus();
      }

      if (isOpen) {
        previousFocus = global.document.activeElement;
        var focusable = getFocusable(panel);
        if (focusable.length > 0) {
          focusable[0].focus();
        }
      } else if (previousFocus && typeof previousFocus.focus === "function") {
        previousFocus.focus();
      }
    }

    function close() {
      setOpen(false);
    }

    function open() {
      setOpen(true);
    }

    function toggleOpen() {
      setOpen(toggle.getAttribute("aria-expanded") !== "true");
    }

    toggle.addEventListener("click", function onToggleClick(event) {
      event.preventDefault();
      toggleOpen();
    });

    closeButtons.forEach(function bindClose(button) {
      button.addEventListener("click", function onCloseClick(event) {
        event.preventDefault();
        close();
      });
    });

    submenuToggles.forEach(function bindSubmenu(submenuToggle) {
      var submenu = submenuForToggle(submenuToggle);
      var item = submenuToggle.closest ? submenuToggle.closest(".myron-site__nav-item--has-children") : null;
      if (!submenu || !item) {
        return;
      }

      setSubmenuOpen(submenuToggle, false);

      submenuToggle.addEventListener("click", function onSubmenuClick(event) {
        event.preventDefault();
        var isOpen = submenuToggle.getAttribute("aria-expanded") === "true";
        if (!isMobileMode()) {
          closeSubmenus(submenuToggle);
        }
        setSubmenuOpen(submenuToggle, !isOpen);
      });

      item.addEventListener("mouseenter", function onSubmenuPointerEnter() {
        if (!isMobileMode()) {
          closeSubmenus(submenuToggle);
          setSubmenuOpen(submenuToggle, true);
        }
      });

      item.addEventListener("mouseleave", function onSubmenuPointerLeave() {
        if (!isMobileMode()) {
          setSubmenuOpen(submenuToggle, false);
        }
      });

      item.addEventListener("focusin", function onSubmenuFocusIn() {
        if (suppressNextSubmenuFocusOpen) {
          return;
        }
        if (!isMobileMode()) {
          closeSubmenus(submenuToggle);
          setSubmenuOpen(submenuToggle, true);
        }
      });

      item.addEventListener("focusout", function onSubmenuFocusOut() {
        if (isMobileMode()) {
          return;
        }
        global.setTimeout(function closeAfterFocusLeaves() {
          if (!item.contains(global.document.activeElement)) {
            setSubmenuOpen(submenuToggle, false);
          }
        }, 0);
      });
    });

    panel.addEventListener("keydown", function onPanelKeydown(event) {
      if (event.key === "Escape") {
        var openSubmenuToggle = firstOpenSubmenuToggle();
        if (openSubmenuToggle) {
          event.preventDefault();
          suppressNextSubmenuFocusOpen = true;
          setSubmenuOpen(openSubmenuToggle, false);
          openSubmenuToggle.focus();
          global.setTimeout(function allowFocusOpenAgain() {
            suppressNextSubmenuFocusOpen = false;
          }, 0);
          return;
        }

        event.preventDefault();
        close();
        return;
      }

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

      var focusable = getFocusable(panel);
      if (focusable.length === 0) {
        event.preventDefault();
        return;
      }

      var first = focusable[0];
      var last = focusable[focusable.length - 1];
      var active = global.document.activeElement;

      if (event.shiftKey && active === first) {
        event.preventDefault();
        last.focus();
      } else if (!event.shiftKey && active === last) {
        event.preventDefault();
        first.focus();
      }
    });

    panel.addEventListener("click", function onPanelClick(event) {
      var target = event.target;
      if (target && target.closest && target.closest(".myron-site__nav a, .myron-site__utility a")) {
        close();
      }
    });

    global.document.addEventListener("click", function onDocumentClick(event) {
      var target = event.target;
      if (!target || !target.closest || root.contains(target) || panel.contains(target)) {
        return;
      }
      if (!isMobileMode()) {
        closeSubmenus();
      }
    });

    function syncViewportState() {
      closeSubmenus();
      if (isMobileMode()) {
        if (usesTakeoverMode()) {
          portalPanel();
        } else {
          restorePanel();
        }
        setOpen(false);
      } else {
        restorePanel();
        setDesktopOpen();
      }
    }

    if (mobileQuery && typeof mobileQuery.addEventListener === "function") {
      mobileQuery.addEventListener("change", syncViewportState);
    } else if (mobileQuery && typeof mobileQuery.addListener === "function") {
      mobileQuery.addListener(syncViewportState);
    }

    syncViewportState();
  }

  onReady(function setupPublicNavigation() {
    initAppearanceMode();
    global.document.body.classList.add("myron-public-nav-js");
    toArray(global.document.querySelectorAll("[data-myron-public-navigation]")).forEach(bindNavigation);
  });
})(window);
