(function () {
  'use strict';

  if (window.__nexdirectoryExperienceReady) {
    return;
  }
  window.__nexdirectoryExperienceReady = true;

  const syncNavigation = function () {
    const navigation = document.querySelector('.nexdirectory-platform-nav');
    if (navigation) {
      navigation.classList.toggle('is-scrolled', window.scrollY > 12);
    }
  };

  const init = function () {
    const body = document.body;
    if (!body || !body.classList.contains('nexdirectory-plugin-page')) {
      return;
    }

    body.dataset.nexdirectoryExperience = '1';

    syncNavigation();

    document.querySelectorAll('form').forEach(function (form) {
      if (!form.closest('.nexdirectory-form, .nexdirectory-account-dashboard, .nexdirectory-global-auth-modal, [role="tabpanel"]')) {
        return;
      }
      if (form.dataset.nexdirectoryExperienceBound === '1') {
        return;
      }
      form.dataset.nexdirectoryExperienceBound = '1';
      form.addEventListener('submit', function (event) {
        window.setTimeout(function () {
          if (!event.defaultPrevented) {
            form.setAttribute('aria-busy', 'true');
          }
        }, 0);
      });
    });

    document.querySelectorAll('details').forEach(function (details) {
      if (details.dataset.nexdirectoryExperienceBound === '1') {
        return;
      }
      details.dataset.nexdirectoryExperienceBound = '1';
      details.addEventListener('keydown', function (event) {
        if (event.key === 'Escape' && details.open) {
          details.open = false;
          const summary = details.querySelector('summary');
          if (summary) {
            summary.focus();
          }
        }
      });
    });
  };

  let navigationFrame = 0;
  window.addEventListener('scroll', function () {
    if (navigationFrame) {
      return;
    }
    navigationFrame = window.requestAnimationFrame(function () {
      navigationFrame = 0;
      syncNavigation();
    });
  }, { passive: true });

  document.addEventListener('keydown', function (event) {
    if (event.key !== 'Tab') {
      return;
    }
    const dialogs = Array.from(document.querySelectorAll('[role="dialog"][aria-modal="true"]')).filter(function (dialog) {
      const style = window.getComputedStyle(dialog);
      return !dialog.hidden && dialog.getAttribute('aria-hidden') !== 'true' && style.display !== 'none' && style.visibility !== 'hidden';
    });
    const dialog = dialogs.length ? dialogs[dialogs.length - 1] : null;
    if (!dialog) {
      return;
    }
    const focusable = Array.from(dialog.querySelectorAll('a[href], button:not([disabled]), input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])')).filter(function (element) {
      const style = window.getComputedStyle(element);
      return !element.hidden && style.display !== 'none' && style.visibility !== 'hidden';
    });
    if (!focusable.length) {
      event.preventDefault();
      dialog.setAttribute('tabindex', '-1');
      dialog.focus();
      return;
    }
    const first = focusable[0];
    const last = focusable[focusable.length - 1];
    if (event.shiftKey && document.activeElement === first) {
      event.preventDefault();
      last.focus();
    } else if (!event.shiftKey && document.activeElement === last) {
      event.preventDefault();
      first.focus();
    } else if (!dialog.contains(document.activeElement)) {
      event.preventDefault();
      first.focus();
    }
  });

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init, { once: true });
  } else {
    init();
  }

  window.addEventListener('pageshow', function () {
    document.querySelectorAll('form[aria-busy="true"]').forEach(function (form) {
      form.removeAttribute('aria-busy');
    });
  });

  document.addEventListener('nexdirectory:spa:navigated', init);
})();
