{#
  Parameters:
  - title (string) (default: '')
  - title_tag (string) (default: 'h2')
  - title_link: (link object) (default: {})
  - title_attributes: (drupal attrs)
  - id (int) (default: random(10000))
  - flush (boolean) (default: false)
  - items (array) (default: []): format: [
      {
        title: (string),
        title_tag: (string),
        content: (block),
        stay_open (boolean) (default: false)
      },
    ]
  - expand_button: (button object) (default: {})
  - collapse_button: (button object) (default: {})
  - open_item_id (int) (default: 0)
  - attributes (drupal attrs)
#}

{%- set _title = title|default('') %}
{% set _title_tag = title_tag|default('h2') %}
{% set _title_link = title_link|default({}) %}
{% set _title_attributes = title_attributes ?: create_attribute() %}
{% set _id = id|default(random(10000)) %}
{% set _flush = flush ?? false %}
{% set _items = items|default([]) %}
{% set _expand_button = expand_button|default({}) %}
{% set _collapse_button = collapse_button|default({}) %}
{% set _open_item_id = open_item_id|default(0) %}
{% set _classes = ['accordion'] %}
{% if _flush %}
  {%- set _classes = _classes|merge(['accordion-flush']) %}
{% endif %}

{%- if attributes is empty %}
  {%- set attributes = create_attribute() %}
{% endif %}

{%- set attributes = attributes.addClass(_classes).setAttribute('id', 'accordion-' ~ _id) %}

{%- if _items is not empty -%}
<div
  {{ attributes }}
>
  {%- if _title is not empty -%}
    {% include '@oe-bcl/bcl-heading/heading.html.twig' with {
      title: _title,
      title_tag: _title_tag,
      title_link: _title_link,
      attributes: _title_attributes,
    } only %}
  {%- endif -%}

  {% if _items|length > 1 and (_expand_button is not empty or _collapse_button is not empty) %}
  {%- set wrapper_attributes = create_attribute().addClass(['d-flex', 'justify-content-end', 'gap-3', 'mb-3']) -%}
  <div
    {{ wrapper_attributes }}
    >
    {%- if _expand_button is not empty %}
      {%- if _expand_button.attributes is empty %}
        {%- set _expand_button = _expand_button|merge({
            attributes: create_attribute()
          })
        %}
      {%- endif %}
      {%- include '@oe-bcl/bcl-button/button.html.twig' with _expand_button|merge({
        attributes: _expand_button.attributes
                    .setAttribute('data-target', 'accordion-' ~ _id)
                    .setAttribute('data-action', 'expand')
      }) only %}
    {%- endif %}
    {%- if _collapse_button is not empty %}
      {%- if _collapse_button.attributes is empty %}
        {%- set _collapse_button = _collapse_button|merge({
            attributes: create_attribute()
          })
        %}
      {%- endif %}
      {%- include '@oe-bcl/bcl-button/button.html.twig' with _collapse_button|merge({
        attributes: _collapse_button.attributes
                    .setAttribute('data-target', 'accordion-' ~ _id)
                    .setAttribute('data-action', 'collapse')
      }) only %}
    {%- endif -%}
  </div>
  {%- endif -%}
  <div class="accordion-items-wrapper">
    {%- for _item in _items %}
      {%- set _open_item = _open_item_id == loop.index %}
      {%- set _button_classes = ['accordion-button'] %}
      {%- if not _open_item %}
        {%- set _button_classes = _button_classes|merge(['collapsed']) %}
      {%- endif -%}
      <div class="accordion-item">
        {%- set _item_title_tag = _item.title_tag|default('h2') -%}
        <{{ _item_title_tag }}
          class="accordion-header"
          id="heading-{{ _id }}-{{ loop.index }}"
        >
          {%- set button_attributes = create_attribute()
                                    .addClass(_button_classes)
                                    .setAttribute('data-bs-toggle', 'collapse')
                                    .setAttribute('autocomplete', 'off')
                                    .setAttribute('data-bs-target', '#collapse-' ~ _id ~ '-' ~ loop.index)
                                    .setAttribute('aria-controls', 'collapse-' ~ _id ~ '-' ~ loop.index)
                                    .setAttribute('aria-expanded', open_item ? 'true' : 'false')
          %}
          {%- set item_title -%}
            <span>
              {{- _item.title -}}
            </span>
          {%- endset %}
          {%- include '@oe-bcl/bcl-button/button.html.twig' with {
            label: item_title,
            clean_class: true,
            attributes: button_attributes
          } only -%}
        </{{ _item_title_tag }}>{# -#}
        <div
          id="collapse-{{ _id }}-{{ loop.index }}"
          class="accordion-collapse collapse{{ _open_item ? ' show' }}"
          aria-labelledby="heading-{{ _id }}-{{ loop.index }}"
          role="region"
        {% if not _item.stay_open and _expand_button is empty %}
          data-bs-parent="#accordion-{{ _id }}"
        {% endif %}
        >{# -#}
          <div class="accordion-body">
            {%- set _content = _item.content|default('') %}
            {%- block content _content -%}
          </div>{# -#}
        </div>{# -#}
      </div>
    {%- endfor -%}
  </div>{# -#}
</div>
{%- endif -%}
