/**
 * @file
 * Main menu functionality
 */

(function (once) {
  Drupal.behaviors.themekitMobileMainNavigation = {
    attach(context) {
      const { body } = document;
      const header = (context.classList && context.classList.contains('region-header')) ? context : context.querySelector('.region-header');
      if (!header) return;

      const navigationWrapper = header.querySelector('.navigation-wrapper');
      if (!navigationWrapper) return;

      const buttonToggleMenu = once('navigation-toggle', '.navigation-toggle', header);
      if (buttonToggleMenu.length) {
        buttonToggleMenu.forEach((button) => {
          button.addEventListener('click', (event) => {
            event.preventDefault();

            body.classList.toggle('is-overflow');
            navigationWrapper.classList.toggle('is-active');

            const isExpanded = button.getAttribute('aria-expanded') === 'true';
            button.setAttribute('aria-expanded', String(!isExpanded));
          });
        });
      }
    },
  };
}(once));
