{# LIBRARIES #}
{{ attach_library('ui_patterns/libraries.bs_nav') }}

{# VARIABLES #}
{% set nid = nid|default("menu-" ~ random()) %}
{% set attributes = attributes|default(create_attribute()).setAttribute('id', 'nav-' ~ nid) %}
{% set no_active = no_active|default(false) %}
{% set alignment = alignment|default('left') %}
{% set fill = fill|default('justify') %}
{% set style = style|default('tabs') %}
{% set type = type|default('links') %}
{% set nav_classes = nav_classes|default("") %}
{% set nav_prefix = nav_prefix|default(false) %}
{% set nav_suffix = nav_suffix|default(false) %}
{% set mobile_rows = mobile_rows|default(false) %}
{% set tab_list = tab_list|default(false) %}
{% set nv_tgl_down = nv_tgl_down|default("fal fa-angle-down") %}
{% set nv_tgl_up = nv_tgl_up|default("far fa-times-circle") %}

{% set owns_ids = '' %}
{% for item in items %}
  {% if item.tbid and item.tab_item %}
    {% set owns_ids = owns_ids ~ 'tbt-' ~ item.tbid ~ ' ' %}
  {% endif %}
{% endfor %}

{% set aria_tabs = false %}
{% if tab_list and owns_ids %}
  {% set attributes = attributes.setAttribute('role', 'tablist') %}
  {% set attributes = attributes.setAttribute('aria-owns', owns_ids|trim()) %}
  {% set aria_tabs = true %}
{% endif %}

{% set nav_classes = [
  nav_classes,
  'nav',
  'ulp',
  style ? 'nav-' ~ style : '',
  fill ? 'nav-' ~ fill : '',
  alignment == 'right' ? 'flex-row-reverse',
  alignment == 'center' ? 'justify-content-center',
  alignment == 'vertical' ? 'flex-column',
  mobile_rows ? 'mobrows',
] %}

{# TEMPLATE #}
{% if items %}
  <ul{{ attributes.addClass(nav_classes) }}>

    {% if nav_prefix %}
      <li class="nav-item nav-item-prefix" {{ aria_tabs ? 'role="presentation"' }}>
        {{ nav_prefix }}
      </li>
    {% endif %}

    {% for item in items %}

      {% set nav_item_classes = [
        'nav-item',
        item.is_expanded and item.below ? 'dropdown',
        item.split ? 'nav-split',
      ] %}
      {% set nav_link_classes = [
        item.class,
        'nav-link',
        item.in_active_trail and not no_active ? 'active'
      ] %}

      <li class="{{ nav_item_classes|join(' ') }}" {{ aria_tabs ? 'role="presentation"' }}>

        {% if item.is_expanded and item.below %}

          <a class="nav-link dropdown-toggle"
             data-bs-toggle="dropdown"
             href="#"
             role="button"
             aria-expanded="false">
            {{ item.title }}
          </a>

          {% if item.below %}
            {% include '@organisms/tm-bs/nav/_dropdown-menu.twig' with {
              items: item.below
            } only %}
          {% endif %}

        {% else %}

          {% set title = item.title %}
          {% if not item.link_only %}
            {% if 'nv-tgl' in nav_classes|join and ('none' not in nv_tgl_down) %}
              {% set title %}
                {{ item.title|raw }}<i class="{{ nv_tgl_down }} belir"></i><i class="{{ nv_tgl_up}} belirh"></i>
              {% endset %}
            {% endif %}
          {% endif %}

          {% include "@atoms/tm-links/standard/standard.twig" with {
            "text": title,
            "url": item.url,
            "class": nav_link_classes,
            "attributes": item.attributes|default(create_attribute()),
          } only %}

        {% endif %}
      </li>

    {% endfor %}

    {% if nav_suffix %}
      <li class="nav-item nav-item-suffix" {{ aria_tabs ? 'role="presentation"' }}>
        {{ nav_suffix }}
      </li>
    {% endif %}

  </ul>
{% endif %}
