{#
/**
 * @file
 * Theme override to display a menu.
 *
 * 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.
 */
#}

{% set classes = 'social-media-menu flex justify-start relative' %}
{% set attributes = attributes ? attributes.addClass(classes) : ' class="' ~ classes ~ '"' %}

{# No mapping required as per user request, icon name comes from Link class #}

<nav {{ attributes }} aria-label="{{ menu_label }}">
  <ul class="flex gap-2 p-0 w-auto" data-level="1">
    {% for item in items %}
      {% set title = item.title is not empty ? item.title : item.text %}
      {# Capture the rendered URL before testing it for emptiness, since it's a Drupal object under the hood.
      The - prefix and suffix prevent errant whitespace in the resulting `url` variable. #}
      {% set url %}
        {{- item.url -}}
      {% endset %}

      {% set item_classes =
        [
          item.in_active_trail ? 'bg-muted/50' : '',
          'flex flex-wrap justify-between md:flex-nowrap',
          'md:w-auto md:relative md:max-w-[375px]',
          'font-sds-primary-font text-sds-base-size leading-sds-line-height text-white'
        ]|filter(c => c is not empty)
      %}

      {% set li_attributes = item.attributes ? item.attributes.addClass(item_classes).setAttribute('data-level', 1) : create_attribute().addClass(item_classes).setAttribute('data-level', 1) %}

      <li {{ li_attributes }}>
        {% if url is not empty %}
          {% set link_attrs = item.url.options.attributes|default({}) %}
          {# Extract icon name from classes assigned to the menu link #}
          {% set icon_name = link_attrs.class is iterable ? link_attrs.class|first : link_attrs.class %}

          <a
            href="{{ url }}"
            {{ create_attribute(link_attrs).addClass('flex min-h-[44px] shrink-1 grow-1 basis-0 items-center duration-300 md:min-h-auto text-sds-neutral').setAttribute('data-level', 1) }}
          >
            {% if icon_name %}
              {% include 'pulse:space-icon' with {
                icon: icon_name,
                icon_size: 'extra-small',
                alt: title
              } only %}
            {% endif %}
          </a>
        {% endif %}
      </li>
    {% endfor %}
  </ul>
</nav>
