{#
/**
 * @file
 * Theme implementation for the menus in the primary_menu region.
 *
 * Available variables:
 * - menu_name: The machine name of the menu.
 * - items: A nested list of menu items. Each menu item contains:
 *   - attributes: HTML attributes for the menu item.
 *   - below: The menu item child items.
 *   - title: The menu link title.
 *   - url: The menu link url, instance of \Drupal\Core\Url
 *   - localized_options: Menu link localized options.
 *   - is_expanded: TRUE if the link has visible children within the current
 *     menu tree.
 *   - is_collapsed: TRUE if the link has children within the current menu tree
 *     that are not currently visible.
 *   - in_active_trail: TRUE if the link is in the active trail.
 *
 * @ingroup themeable
 */
#}
<div class="menu-wrapper">
  <a href="#" id="toggles" class="menu-toggle">
    <s class="bars"></s>
    <s class="bars"></s>
    <div class="element-invisible">toggle</div>
  </a>
<div class="pure-menu pure-menu-horizontal menu-transform">
{% import _self as menus %}
{#
  We call a macro which calls itself to render the full tree.
  @see https://twig.symfony.com/doc/1.x/tags/macro.html
#}
{% set attributes = attributes.addClass('menu') %}
{{ menus.menu_links(items, attributes, 0) }}
{% macro menu_links(items, attributes, menu_level) %}
  {% set primary_nav_level = 'primary-nav__menu--level-' ~ (menu_level + 1) %}
  {% set selector_primary_nav_level = 'primary-nav-menu--level-' ~ (menu_level + 1) %}
  {% import _self as menus %}
  {% if items %}
    {#
      Place the menu arrow (caret) outside of the submenu because the submenu
      has the overflow:hidden CSS rule applied.
    #}
    {% if menu_level == 1 %}
      <span data-selector="primary-nav-menu-🥕" class="primary-nav__menu-🥕"></span>
    {% endif %}
    {# Set <ul> classes #}
    {% if menu_level == 0 %}
    {% set navul_classes = ['pure-menu-list'] %}
    {% else %}
    {% set attributes = attributes.removeClass('pure-menu-list') %}
    {% set navul_classes = ['pure-menu-children'] %}
    {% endif %}
    <ul {{ attributes.addClass('primary-nav__menu', navul_classes, primary_nav_level).setAttribute('data-selector', selector_primary_nav_level) }}>
      {% set attributes = attributes.removeClass(primary_nav_level) %}
      {% for item in items %}
        {% if item.url.isRouted and item.url.routeName == '<nolink>' %}
          {% set menu_item_type = 'nolink' %}
        {% elseif item.url.isRouted and item.url.routeName == '<button>' %}
          {% set menu_item_type = 'button' %}
        {% else %}
          {% set menu_item_type = 'link' %}
        {% endif %}
        {% set item_classes = [
            'pure-menu-item',
            'primary-nav__menu-item',
            'primary-nav__menu-item--' ~ menu_item_type,
            'primary-nav__menu-item--level-' ~ (menu_level + 1),
            item.is_expanded and item.below ? 'pure-menu-has-children pure-menu-allow-hover primary-nav__menu-item--has-children',
            item.is_expanded? 'expanded',
            item.is_collapsed ? 'collapsed',
            item.in_active_trail ? 'active',
            loop.first ? 'first',
            loop.last ? 'last',
          ]
        %}
        {% set link_classes = [
            'pure-menu-link',
            'primary-nav__menu-link',
            'primary-nav__menu-link--' ~ menu_item_type,
            'primary-nav__menu-link--level-' ~ (menu_level + 1),
            item.in_active_trail ? 'primary-nav__menu-link--active-trail',
            item.below ? 'primary-nav__menu-link--has-children',
          ]
        %}
        <li{{ item.attributes.addClass(item_classes).setAttribute('data-selector', item.below ? 'primary-nav-menu-item-has-children' : false) }}>
          {#
            A unique HTML ID should be used, but that isn't available through
            Twig yet, so the |clean_id filter is used for now.
            @see https://www.drupal.org/project/drupal/issues/3115445
          #}
          {% set aria_id = (item.title ~ '-submenu-' ~ loop.index )|clean_id %}
          {% set link_title %}
            <span class="primary-nav__menu-link-inner">{{ item.title }}</span>
          {% endset %}
          {% if menu_item_type == 'link' or menu_item_type == 'nolink' %}
            {{ link(menu_item_type == 'link' ? link_title : item.title, item.url, {
              'class': link_classes,
              'data-selector': 'primary-nav-menu-link-has-children',
              })
            }}
            {% if item.below %}
              {#
                Aria-hidden and tabindex attributes are removed via JS. Button is non-functional,
                but still visible in non-JS environments so that chevron can indicate presence of
                drop menu.
              #}
              {%
                set toggle_button_attributes = create_attribute({
                  'class': 'primary-nav__button-toggle',
                  'data-selector': 'primary-nav-submenu-toggle-button',
                  'aria-controls': aria_id,
                  'aria-expanded': 'false',
                  'aria-hidden': 'true',
                  'tabindex': '-1',
                })
              %}
              <button{{ toggle_button_attributes }}>
                <span class="visually-hidden">{{ '@title sub-navigation'|t({'@title': item.title}) }}</span>
                <span class="icon--menu-toggle"></span>
              </button>
              {% set attributes = attributes.setAttribute('id', aria_id) %}
              {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
            {% endif %}
          {% elseif menu_item_type == 'button' %}
            {{ link(link_title, item.url, {
              'class': link_classes,
              'aria-controls': item.below ? aria_id : false,
              'aria-expanded': item.below ? 'false' : false,
              'data-selector': item.below ? 'primary-nav-submenu-toggle-button' : false,
              })
            }}
            {% set attributes = attributes.setAttribute('id', aria_id) %}
            {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
          {% endif %}
        </li>
      {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}
</div>
</div>
