(function () {
  'use strict';

  function ready(fn) {
    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fn);
    else fn();
  }

  ready(function () {
    var body = document.body;
    var shell = document.querySelector('[data-account-shell]');
    var sidebar = document.getElementById('accountSidebar');

    function setSidebar(open) {
      body.classList.toggle('account-sidebar-open', !!open);
      if (sidebar) sidebar.setAttribute('aria-hidden', open ? 'false' : (window.innerWidth < 992 ? 'true' : 'false'));
    }

    document.addEventListener('click', function (event) {
      var opener = event.target.closest('[data-account-sidebar-toggle]');
      var closer = event.target.closest('[data-account-sidebar-close]');
      if (opener) {
        event.preventDefault();
        setSidebar(true);
      }
      if (closer) {
        event.preventDefault();
        setSidebar(false);
      }
      if (window.innerWidth < 992 && event.target.closest('.account-menu-link')) setSidebar(false);
    });

    document.addEventListener('keydown', function (event) {
      if (event.key === 'Escape') setSidebar(false);
    });

    window.addEventListener('resize', function () {
      if (window.innerWidth >= 992) setSidebar(false);
    });

    var search = document.querySelector('[data-account-menu-search]');
    var clear = document.querySelector('[data-account-menu-clear]');
    var groups = Array.prototype.slice.call(document.querySelectorAll('[data-menu-group]'));
    var empty = document.querySelector('[data-account-menu-empty]');

    function filterMenu() {
      if (!search) return;
      var query = search.value.trim().toLowerCase();
      var visibleCount = 0;
      groups.forEach(function (group) {
        var links = Array.prototype.slice.call(group.querySelectorAll('[data-menu-link]'));
        var matches = 0;
        links.forEach(function (link) {
          var text = (link.getAttribute('data-menu-text') || link.textContent || '').toLowerCase();
          var visible = !query || text.indexOf(query) !== -1;
          link.classList.toggle('d-none', !visible);
          if (visible) matches++;
        });
        group.classList.toggle('d-none', matches === 0);
        visibleCount += matches;
        var collapse = group.querySelector('[data-menu-collapse]');
        if (query && collapse && matches) {
          if (window.bootstrap && bootstrap.Collapse) bootstrap.Collapse.getOrCreateInstance(collapse, { toggle: false }).show();
          else collapse.classList.add('show');
        }
      });
      if (empty) empty.classList.toggle('d-none', visibleCount !== 0);
      if (clear) clear.classList.toggle('is-visible', query.length > 0);
    }

    if (search) {
      search.addEventListener('input', filterMenu);
      search.addEventListener('keydown', function (event) {
        if (event.key === 'Escape') {
          search.value = '';
          filterMenu();
          search.blur();
        }
      });
    }
    if (clear) clear.addEventListener('click', function () {
      if (!search) return;
      search.value = '';
      filterMenu();
      search.focus();
    });

    document.querySelectorAll('.account-menu-collapse').forEach(function (collapse) {
      collapse.addEventListener('shown.bs.collapse', function () {
        try { localStorage.setItem('ph-menu-' + collapse.id, 'open'); } catch (e) {}
      });
      collapse.addEventListener('hidden.bs.collapse', function () {
        try { localStorage.setItem('ph-menu-' + collapse.id, 'closed'); } catch (e) {}
      });
      if (!collapse.closest('.account-menu-group').classList.contains('is-active')) {
        try {
          if (localStorage.getItem('ph-menu-' + collapse.id) === 'open' && window.bootstrap && bootstrap.Collapse) {
            bootstrap.Collapse.getOrCreateInstance(collapse, { toggle: false }).show();
          }
        } catch (e) {}
      }
    });

    var active = document.querySelector('.account-menu-link.active');
    if (active) setTimeout(function () { active.scrollIntoView({ block: 'nearest' }); }, 80);

    document.querySelectorAll('.table-responsive').forEach(function (wrap) {
      wrap.setAttribute('tabindex', '0');
      wrap.setAttribute('role', 'region');
      wrap.setAttribute('aria-label', 'Scrollable data table');
    });

    document.querySelectorAll('form').forEach(function (form) {
      if (form.querySelector('input[type="file"]')) form.setAttribute('enctype', 'multipart/form-data');
    });

    if (shell) shell.classList.add('account-ui-ready');
  });
})();
