{% extends '@KnpMenu/menu.html.twig' %}

{% block root %}
  {% set listAttributes = item.childrenAttributes %}
  {% set rootClasses = options.rootWrapClasses|default('navbar navbar-expand-lg navbar-light') %}
  {% set noRootWrap = options.noRootWrap|default(false) == true %}
  {% if not noRootWrap %}
    <nav class="{{ rootClasses }}">
  {% endif %}
  {{ block('list') -}}
  {% if not noRootWrap %}
    </nav>
  {% endif %}
{% endblock %}

{% block list %}
  {% set noRootWrap = false %}
  {% if item.parent and item.parent.parent is null %}
    {% set listAttributes = listAttributes|merge({'class': listAttributes.class|default('') ~ ' navbar-nav '}) %}
    {% set noRootWrap = options.noRootWrap|default(false) == true %}
  {% endif %}

  {% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
    {% import _self as knp_menu %}

    {% if not noRootWrap %}
      <ul{{ knp_menu.attributes(listAttributes) }}>
    {% endif %}
    {{ block('children') }}
    {% if not noRootWrap %}
      </ul>
    {% endif %}
  {% endif %}
{% endblock %}

{% block label %}
  {% set translation_domain = item.extra('translation_domain', 'messages') -%}
  {% set label = item.label -%}
  {% if translation_domain is not same as(false) -%}
    {% set label = label|trans(item.extra('translation_params', {}), translation_domain)|raw -%}
  {% endif -%}
  {% if options.allow_safe_labels and item.extra('safe_label', false) %}{{ label|raw }}{% else %}{{ label|raw }}{% endif -%}
{% endblock %}

{% block item %}
  {% set classes = [] %}
  {% if item.displayed %}
    {# building the class of the item #}
    {% set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %}
    {% if matcher.isCurrent(item) %}
      {% set classes = classes|merge([options.currentClass]) %}
    {% elseif matcher.isAncestor(item, options.matchingDepth) %}
      {% set classes = classes|merge([options.ancestorClass]) %}
    {% endif %}
    {% if item.actsLikeFirst %}
      {% set classes = classes|merge([options.firstClass]) %}
    {% endif %}
    {% if item.actsLikeLast %}
      {% set classes = classes|merge([options.lastClass]) %}
    {% endif %}

    {# Mark item as "leaf" (no children) or as "branch" (has children that are displayed) #}
    {% if item.hasChildren and options.depth is not same as(0) %}
      {% if options.branch_class is not empty and item.displayChildren %}
        {% set classes = classes|merge([options.branch_class]) %}
      {% endif %}
    {% elseif options.leaf_class is not empty %}
      {% set classes = classes|merge([options.leaf_class]) %}
    {% endif %}

    {% set liClasses = classes %}
    {% if matcher.isCurrent(item) %}
      {% set liClasses = liClasses|merge(['active']) %}
    {% endif %}
    {% if item.hasChildren %}
      {% set classes = classes|merge(['dropdown-toggle']) %}
      {% set liClasses = liClasses|merge(['dropdown']) %}
    {% endif %}

    {% set classes = classes|merge(['nav-item']) %}

    {% set attributes = item.attributes %}
    {% set labelAttributes = item.labelAttributes %}
    {% set linkAttributes = item.linkAttributes %}

    {% if classes is not empty %}
      {% set attributes = attributes|merge({'class': liClasses|join(' ')}) %}
    {% endif %}


    {% if item.uri is not empty and (not matcher.isCurrent(item) or options.currentAsLink) %}
      {# link type #}
      {% set linkAttributes = linkAttributes|merge({'class': linkAttributes.class|default('')~' nav-link'}) %}
      {% if item.hasChildren %}
        {% set linkAttributes = linkAttributes
          |merge({'class': linkAttributes.class|default('')~' dropdown-toggle'})
          |merge({'data-bs-toggle': 'dropdown'}) %}
      {% endif %}
    {% else %}
      {# text type #}
      {% set labelAttributes = labelAttributes|merge({'class': labelAttributes.class|default('')~' nav-text'}) %}
      {% if item.hasChildren %}
        {% set labelAttributes = labelAttributes
          |merge({'class': labelAttributes.class|default('')~' dropdown-toggle'})
          |merge({'data-bs-toggle': 'dropdown'}) %}
      {% endif %}
    {% endif %}

    {# displaying the item #}
    {% import _self as knp_menu %}
    <li{{ knp_menu.attributes(attributes) }}>
      {# handle item type #}
      {% if item.uri is not empty and (not matcher.isCurrent(item) or options.currentAsLink) %}
        {{ block('linkElement') }}
      {% else %}
        {{ block('spanElement') }}
      {% endif %}
      {# render the list of children#}
      {% set childrenWrapClass = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
      {% set childrenWrapClass = childrenWrapClass|merge(['depth-' ~ item.level]) %}
      {% set childrenWrapClass = childrenWrapClass|merge(['dropdown-menu']) %}
      {% set listAttributes = item.childrenAttributes|merge({'class': childrenWrapClass|join(' ') }) %}
      {{ block('list') }}
    </li>
  {% endif %}
{% endblock %}

{% block linkElement %}
  {% import _self as knp_menu %}
  <a {{ knp_menu.attributes(linkAttributes) }} href="{{ item.uri }}">
    {{ block('label') }}
  </a>
{% endblock %}

{% block spanElement %}
  {% import _self as knp_menu %}
  <span{{ knp_menu.attributes(labelAttributes) }}>
    {{ block('label') }}
  </span>
{% endblock %}