{# LIBRARIES #}
{{ attach_library('ui_patterns/libraries.bs_collapse') }}
{{ attach_library('ui_patterns/bs-collapse.collapse_lib') }}

{# VARIABLES #}
{% set level = level|default("block") %}
{% set tag_name = tag_name|default("span") %}
{% set active = active|default(false) %}
{% set label = label|default('Show') %}
{% set hide_label = hide_label|default('') %}
{% set tgl_label = tgl_label|default(false) %}
{% set content_first = content_first|default(false) %}
{% set content_fade = content_fade|default(false) %}

{% if hide_label != '' %}
  {% set label = '<span class="show">' ~ label ~ '</span><span class="hide">' ~ hide_label ~ '</span>' %}
{% endif %}

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

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

{% set content_attributes = content_attributes|default(create_attribute()) %}
{% set content_attributes = content_attributes
  .setAttribute("id", "aci-#{ aiid }")
%}
{# Bad namespacing here - fix! #}
{% set content_class = content_class|default("") %}
{% set content_classes = [
  content_classes,
  'collapse',
  active ? 'show',
  content_fade ? 'col-fade',
] %}

{# TEMPLATE #}

{% if not content_first %}
{% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
  "text": label,
  "tgl_text": tgl_label,
  "class": link_class | join(' ') | trim,
  "toggle": toggle,
  "attr": link_attributes,
  "tag_name": tag_name,
  "toggle_open": active,
  "is_collapse": true,
} only %}
{% endif %}


{% if prefix or suffix %}
<div class="collapse-wrapper">
{{ prefix }}
{% endif %}

<div {{ content_attributes.addClass(content_classes) }} >
  <div class="{{ content_class }}">
    {% if show_close %}
      <a href="#"
         class="aci-close"
         type="button"
         data-bs-toggle="collapse"
         data-bs-target="#aci-{{ aiid }}"
      >
        <i class="{{ global_icon_times|default('fas fa-times') ~ "-circle" }}"></i>
      </a>
    {% endif %}
    {{ content|raw }}
  </div>
</div>

{% if prefix or suffix %}
{{ suffix }}
</div>
{% endif %}

{% if content_first %}
  {% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
    "text": label,
    "tgl_text": tgl_label,
    "class": link_class | join(' ') | trim,
    "toggle": toggle,
    "attr": link_attributes,
    "tag_name": tag_name,
    "toggle_open": active,
    "is_collapse": true,
  } only %}
{% endif %}
