{% if variant and variant|lower != 'default' %}
  {% set attributes = attributes.addClass('navbar-nav-' ~ variant|lower|replace({'_': '-'})) %}
{% endif %}

{% set attributes = attributes.addClass('navbar-nav') %}
{% set list_opened = false %}

{% if items %}
  {% for item in items %}
    {#
    When finding a text item. it should be outside the list. So handle text
    items by opening/closing lists around if needed.
    #}
    {% set item_is_link = false %}
    {% if item.below or item.url or item.megamenu %}
      {% set item_is_link = true %}
    {% endif %}

    {% if item_is_link and not list_opened %}
      {% set list_opened = true %}
    <ul{{ attributes }}>
    {% elseif not item_is_link and list_opened %}
      {% set list_opened = false %}
      </ul>
    {% endif %}

    {% set item_attributes = create_attribute(item.attributes|default({})) %}
    {% set link_attributes = create_attribute(item.link_attributes|default({})) %}
    {% if item.megamenu %}
      {{ pattern('dropdown_megamenu', {
        'variant': 'dropdown',
        'title': item.title,
        'megamenu': item.megamenu,
        'dropdown_id': dropdown_id|default("dropdown-" ~ random())  ~ "-" ~ loop.index,
        'auto_close': 'true',
        'dropdown_navbar': true,
        'attributes': create_attribute({
          'class': ['nav-item']
        }),
        'button_attributes': link_attributes
      }) }}
    {% elseif item.below %}
      {{ pattern('dropdown', {
        'variant': 'dropdown',
        'title': item.title,
        'content': item.below,
        'dropdown_id': dropdown_id|default("dropdown-" ~ random())  ~ "-" ~ loop.index,
        'auto_close': 'true',
        'dropdown_navbar': true,
        'attributes': create_attribute({
          'class': ['nav-item']
        }),
        'button_attributes': link_attributes
      }) }}
    {% elseif item.url %}
      <li{{ item_attributes.addClass('nav-item') }}>
        <a{{ link_attributes.setAttribute('href', item.url).addClass('nav-link') }}>{{ item.title }}</a>
      </li>
    {% else %}
      <span{{ link_attributes.addClass('navbar-text') }}>{{ item.title }}</span>
    {% endif %}

    {% if loop.last and list_opened %}
      </ul>
    {% endif %}
  {% endfor %}
{% endif %}
