{{ attach_library('gesso/dropdown_menu') }}

{% macro menu_links(items, attributes, menu_level, menu_name, menu_class, item_class, link_class) %}
  {% import _self as dropdown_menu %}
  {% if items %}
    {% if menu_level == 0 %}
      {% set additional_classes = [
        'c-dropdown-menu'
      ] %}
      {% if menu_class %}
        {% set additional_classes = additional_classes|merge(menu_class) %}
      {% endif %}
      {% set additional_attributes = {
        'class': additional_classes
      } %}
<ul {{ add_attributes(additional_attributes) }}>
  {% else %}
  <ul {{  add_attributes({class: ['c-dropdown-menu__subnav']}) }}>
    {% endif %}
    {% for item in items %}
      {% set item_classes = ['c-dropdown-menu__item'] %}
      {% set link_classes = ['c-dropdown-menu__link'] %}
      {% if menu_level == 0 %}
        {% set link_classes = link_classes|merge(['js-top-level']) %}
      {% endif %}

      {% if item.below %}
        {% set item_classes = item_classes|merge(['has-subnav']) %}
        {% set link_classes = link_classes|merge(['has-subnav']) %}
      {% endif %}
      {% if item.in_active_trail %}
        {% set item_classes = item_classes|merge(['is-active-trail']) %}
        {% set link_classes = link_classes|merge(['is-active-trail']) %}
      {% endif %}
      {% if item.is_collapsed %}
        {% set item_classes = item_classes|merge(['is-collapsed']) %}
        {% set link_classes = link_classes|merge(['is-collapsed']) %}
      {% endif %}
      {% if item.is_expanded %}
        {% set item_classes = item_classes|merge(['is-expanded']) %}
        {% set link_classes = link_classes|merge(['is-expanded']) %}
      {% endif %}
      {% if item['original_link'].options.attributes.class %}
        {% set original_link_class = item['original_link'].options.attributes.class %}
        {% if original_link_class is not iterable %}
          {% set original_link_class = [original_link_class] %}
        {% endif %}
        {% set link_classes = link_classes|merge(original_link_class) %}
      {% endif %}
      {% if item_class %}
        {% set item_classes = item_classes|merge(item_class) %}
      {% endif %}
      {% if link_class %}
        {% set link_classes = link_classes|merge(link_class) %}
      {% endif %}

      {% set additional_item_attributes = {
        'class': item_classes,
      } %}

      {% set link_attributes = {
        'class': link_classes,
        'aria-current': item.is_active ? 'page' : null,
      } %}

      <li {{ add_attributes(additional_item_attributes) }}>
        {% if item.url == '<button>' %}
          {# This is needed to work in Storybook. #}
          <button {{ add_attributes(link_attributes) }}>{{ item.title }}</button>
        {% else %}
          {{ link(item.title, item.url, link_attributes) }}
        {% endif %}

        {% if item.below %}
          {{ dropdown_menu.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      </li>
    {% endfor %}
  </ul>
  {% endif %}
{% endmacro %}

{% import _self as dropdown_menu %}

{{ dropdown_menu.menu_links(items, attributes, 0, menu_name, menu_class, item_class, link_class) }}
