{# LIBRARIES #}
{{ attach_library('ui_patterns/libraries.bs_collapse') }}
{{ attach_library('ui_patterns/menu_ft.menu_ft_lib') }}

{# VARIABLES #}
{% set icon = icon|default('') %}
{% set mid = mid|default(random()) %}
{% set tid = tid|default(mid) %}
{% set url = url|default(null) %}
{% set collapse = collapse|default(false) %}
{% set title_attributes = create_attribute() %}
{% set nav_attributes = create_attribute() %}
{% set wrapper_attributes = wrapper_attributes|default(create_attribute())%}
{% set wrapper_classes = [
  wrapper_class ? wrapper_class : "",
  "nav-footer"
]%}
{% set toggle = null %}
{% set title_class = "ft-title-link" %}

{% set list_icon_left = global_icon_right|default('fas fa-chevron-right') %}
{% if no_list_icon_left %}
  {% set list_icon_left = '' %}
{% endif %}

{% if collapse %}
  {% set title_attributes = title_attributes
    .setAttribute('data-bs-toggle', 'collapse')
    .setAttribute('tabindex', '0')
    .setAttribute('role', 'button')
    .setAttribute('data-bs-target', "#ftml-#{ mid }")
    .setAttribute('aria-expanded', 'false')
  %}
  {% set nav_attributes = nav_attributes
    .setAttribute('id', "ftml-#{ mid }")
    .setAttribute('aria-expanded', 'false')
    .setAttribute('aria-label', "Menu footer #{ tid }")
    .addClass('collapse')
  %}
  {% set toggle = "sign" %}
  {% set title_class = "ft-title-link collapsed belirf beliag" %}
{% endif %}

{# TEMPLATE #}
<div {{ wrapper_attributes.addClass(wrapper_classes) }}>
  {# Allow header to be single link #}
  {% if url %}
    <h2 class="ft-title-link collapsed belirf beliag">
      {% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
        "text": title,
        "url": url,
        "icon_left": icon,
        "display": 'inline',
      } only %}
      {% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
        "attr": title_attributes,
        "text": '<span class="sr-only">Open menu for ' ~ title ~ '</span>',
        "class": 'belirf beliag fr',
        "toggle": toggle,
        "display": 'noflex',
      } only %}
    </h2>
  {% else %}
    {# Header as collapse #}
    <h2>
      {% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
        "attr": title_attributes,
        "text": title,
        "tag_name": 'a',
        "icon_left": icon,
        "class": title_class,
        "toggle": toggle,
      } only %}
    </h2>
  {% endif %}

  {% if links %}
    <nav {{ nav_attributes }}>
      <ul class="ulc">
        {% for link in links %}
          {% if link['title'] != '<none>' %}
            <li class="lic">
              {% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
                "text": link['title'],
                "url": link['url'],
                "icon_left": list_icon_left,
                "class": "ft-list-link",
              } only %}
            </li>
          {% endif %}
        {% endfor %}
      </ul>
    </nav>
  {% endif %}
</div>
