{% macro menu_links(items, attributes, menu_level, menu_name, item_title) %}
  {% import _self as menus %}

  {% if items %}
    {% if menu_level == 0 %}
      {%
        set classes = [
          'menu',
          'menu--' ~ menu_name|clean_class,
          'menu--level-' ~ menu_level
        ]
      %}
      <ul{{ attributes.addClass(classes) }}>
    {% else %}
      {%
        set classes = [
          'menu',
          'menu--' ~ menu_name|clean_class,
          'menu--level-' ~ menu_level,
          'menu--submenu'
        ]
      %}
      <ul class="{{ classes|join(' ') }}">
    {% endif %}

    {% 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' %}
      {% elseif item.url.isrouted and item.below and menu_level == 0 %}
        {% set menu_item_type = 'details_summary' %}
      {% else %}
        {% set menu_item_type = 'link' %}
      {% endif %}

      {%
        set item_classes = [
          'menu__item',
          item.below ? 'menu__item--has-submenu' : '',
          item.in_active_trail ? 'menu__item--active' : '',
        ]
      %}

      <li{{ item.attributes.addClass(item_classes) }}>
        {% if menu_item_type == 'details_summary' %}
          {% set link_text = item.title %}

          {% if menu_name == 'main' and menu_level == 0 %}
            {% set tmp = item.title ~ '<div class="menu__expand"></div>' %}
            {% set link_text %}{{ tmp|raw }}{% endset %}
          {% endif %}

          <details class="menu__details">
            <summary class="menu__summary">
              {{ link(link_text, item.url, (item.in_active_trail ? { 'aria-current': ['page'] } : {})) }}
            </summary>
            <div class="menu__submenu-wrapper">
              <div class="menu__submenu-wrapper-content">
                {{ menus.menu_links(item.below, attributes, menu_level + 1, menu_name, item.title) }}
              </div>
            </div>
          </details>
        {% elseif menu_item_type == 'link' %}
          {{ link(item.title, item.url, (item.in_active_trail ? { 'aria-current': ['page'] } : {})) }}
        {% elseif menu_item_type == 'nolink' %}
          <span>{{ item.title }}</span>
        {% elseif menu_item_type == 'button' %}
          <button>{{ item.title }}</button>
        {% endif %}

        {% if menu_item_type != 'details_summary' %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1, menu_name, item.title) }}
        {% endif %}
      </li>
    {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}

{% import _self as menus %}

{{ menus.menu_links(items, attributes, 0, menu_name, '') }}
