((Drupal, once) => {
  Drupal.behaviors.convene_themeNavigation = {
    attach: (context) => {
      const navItems = once('convene-theme-navigation', '#block-convene-theme-mainnavigation li', context);
      const navBlock = document.querySelector('#block-convene-theme-mainnavigation');

      navItems.forEach((item) => {
        const submenu = item.querySelector('ul');
        if (submenu) {
          item.classList.add('has-submenu');

          const backBtnLi = document.createElement('li');
          backBtnLi.classList.add('mobile-back-item');
          const backBtn = document.createElement('button');
          backBtn.textContent = 'Back';
          backBtn.classList.add('mobile-back-btn');

          backBtnLi.appendChild(backBtn);
          submenu.insertBefore(backBtnLi, submenu.firstChild);

          const link = item.querySelector('a');
          if (link) {
            link.addEventListener('click', (e) => {
              if (window.innerWidth < 992) {
                e.preventDefault();
                item.classList.add('is-open');
                navBlock.classList.add('submenu-active');
              }
            });
          }

          backBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            item.classList.remove('is-open');
            const parentList = item.parentElement;
            if (parentList && !parentList.closest('li.has-submenu')) {
              navBlock.classList.remove('submenu-active');
            }
          });
        }
      });

      const headerInner = document.querySelector('.site-header__inner');
      if (headerInner && navBlock) {
        let mobileToggle = headerInner.querySelector('.mobile-toggle');
        if (!mobileToggle) {
          mobileToggle = document.createElement('button');
          mobileToggle.classList.add('mobile-toggle');
          mobileToggle.setAttribute('aria-label', 'Open menu');
          mobileToggle.setAttribute('aria-expanded', 'false');
          mobileToggle.innerHTML = `<span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>`;

          headerInner.appendChild(mobileToggle);

          mobileToggle.addEventListener('click', () => {
            const isOpen = document.body.classList.toggle('mobile-menu-open');
            mobileToggle.setAttribute('aria-expanded', isOpen.toString());
            mobileToggle.classList.toggle('is-active');
          });
        }

        let mobileRegister = headerInner.querySelector('.mobile-register-btn');
        if (!mobileRegister) {
          const mainUl = navBlock.querySelector('ul:not(.contextual-links)');
          if (mainUl && mainUl.children.length > 0) {
            const lastLi = mainUl.children[mainUl.children.length - 1];
            const link = lastLi.querySelector('a');
            if (link) {
              mobileRegister = link.cloneNode(true);
              mobileRegister.classList.add('mobile-register-btn');
              headerInner.insertBefore(mobileRegister, mobileToggle);
            }
          }
        }
      }

      document.addEventListener('click', (e) => {
        if (!e.target.closest('#block-convene-theme-mainnavigation') && !e.target.closest('.mobile-toggle')) {
          const openItems = document.querySelectorAll('#block-convene-theme-mainnavigation li.is-open');
          openItems.forEach((item) => {
            item.classList.remove('is-open');
            const link = item.querySelector('a');
            if (link) {
              link.setAttribute('aria-expanded', 'false');
            }
          });

          if (document.body.classList.contains('mobile-menu-open') && !e.target.closest('#block-convene-theme-mainnavigation')) {
          }
        }
      });
    },
  };
})(Drupal, once);