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

{# VARIABLES #}
{% set parts = parts|default("both") %}
{% set level = level|default("block") %}
{% set tag_name = tag_name|default("a") %}
{% set show_close = show_close|default(false) %}
{% set scroll_to = scroll_to|default(false) %}
{% set active = active|default(false) %}

{% set link_attributes = link_attributes|default(create_attribute()) %}
{% set link_attributes = link_attributes
  .setAttribute("data-bs-toggle", "collapse")
  .setAttribute("data-bs-target", "#aci-#{ aiid }")
  .setAttribute("aria-controls", "aci-#{ aiid }")
  .setAttribute("role", "button")
  .setAttribute("tabindex", "0")
  .setAttribute("id", "act-#{ aiid }")
%}

{% if active %}
  {% set link_attributes = link_attributes.setAttribute("aria-expanded", "true") %}
{% else %}
  {% set link_attributes = link_attributes.setAttribute("aria-expanded", "false") %}
{% endif %}

{% if scroll_to %}
  {% set link_attributes = link_attributes
    .setAttribute("data-alt-toggle", "scrollto")
    .setAttribute("data-alt-target", "self")
    .setAttribute("data-alt-delay", 350)
  %}
{% endif %}

{% set toggle = toggle|default("") %}
{% set link_class = link_class|default("") %}
{% set link_class = [
  link_class,
  "belirf",
  toggle == 'chevron' ? "belian",
  toggle == 'sign' ? "beliag",
  level == 'inline' ? 'belinline',
  (not active and not toggle) ? 'collapsed',
] %}

{% set content_attributes = content_attributes|default(create_attribute()) %}
{% set content_attributes = content_attributes
  .setAttribute("data-bs-parent", "#ac-#{ pid }")
  .setAttribute("aria-labelledby", "act-#{ aiid }")
  .setAttribute("role", "region")
  .setAttribute("id", "aci-#{ aiid }")
%}
{% if active %}
  {% set content_attributes = content_attributes.addClass("show") %}
{% endif %}
{% set content_class = content_class|default("") %}

{# TEMPLATE #}
{% if parts in ['both', 'link'] %}
  {% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
    "text": label,
    "class": link_class | join(' ') | trim,
    "toggle": toggle,
    "attr": link_attributes,
    "tag_name": tag_name,
    "toggle_open": active,
    "is_collapse": true,
  } only %}
{% endif %}
{% if parts in ['both', 'content'] %}
  <div {{ content_attributes.addClass('collapse') }}>
    <div class="{{ content_class }}">
      {% if show_close %}
        <a href="#"
           class="aci-close"
           role="button"
           data-bs-toggle="collapse"
           data-bs-target="#aci-{{ aiid }}"
        >
          <span class="sr-only">Close panel</span>
          <i class="{{ global_icon_times|default('fas fa-times') ~ "-circle" }}"></i>
        </a>
      {% endif %}
      {{ content|raw }}
    </div>
  </div>
{% endif %}
