{% import '@protons/macros/status.macros.twig' as status_macros %}

{% set attributes = attr|default(create_attribute()) %}
{% set status = status|default('none') %}
{% set display = display|default('flex') %}
{% set class = class|default('') %}
{% set icon_left = icon_left ? icon_left : null %}
{% set icon_right = icon_right ? icon_right : null %}
{% set icon_right_hidden = icon_right_hidden ? icon_right_hidden : null %}
{% set icon_stack_fa = icon_stack_fa|default(false) %}
{% set toggle_open = toggle_open|default(false) %}
{% set is_collapse = is_collapse|default(false) %}

{# Icon mapping, for when we just want to pass 'toggle=sign' for collapse triggers. #}
{% set toggles = [
  {
    'toggle':'sign',
    'icon_right': global_icon_plus|default('fas fa-plus'),
    'icon_right_hidden': global_icon_minus|default('fas fa-minus')
  },
  {
    'toggle':'chevron',
    'icon_right': global_icon_down|default('fas fa-chevron-down'),
    'icon_right_hidden': global_icon_up|default('fas fa-chevron-up')
  },
  {
    'toggle':'chevron_close',
    'icon_right': global_icon_down|default('fas fa-chevron-down'),
    'icon_right_hidden': global_icon_times_circle|default('fas fa-times-circle')
  },
  {
    'toggle':'chevron_right',
    'icon_right': global_icon_right|default('fas fa-chevron-right'),
    'icon_right_hidden': global_icon_right|default('fas fa-chevron-right')
  }
] %}

{% for item in toggles %}
  {% if toggle == item.toggle %}
    {% set icon_right = item.icon_right %}
    {% set icon_right_hidden = item.icon_right_hidden %}
    {% set class = [
      class,
      toggle_open ? '' : 'collapsed',
      'toggle',
    ]|join(' ') %}
  {% endif %}
{% endfor %}

{# URL settings, make <span> if not link. #}
{% set tag_name = tag_name|default('span') %}
{% if url %}
  {% set attributes = attributes.setAttribute('href', url) %}
  {% set tag_name = 'a' %}
{% endif %}

{# Set level/display of flex item. #}
{% if display != 'flex' %}
  {% set class = ['bel' ~ display, class]|join(' ') %}
{% endif %}

{# Template #}
<{{ tag_name }} {{ attributes }} class="bel {{ class }}">
  {% if icon_left is not empty %}
    {% if icon_left is iterable %}
      {% if icon_stack_fa %}
        <span class="fa-stack">
          <i class="{{ icon_left[0] }} fa-stack-2x icon-stacked belil"></i>
          <i class="{{ icon_left[1] }} fa-stack-1x icon-stacked belil"></i>
        </span>
      {% else %}
        <span class="f-stack">
          <i class="{{ icon_left[0] }} beli belil"></i>
          <i class="{{ icon_left[1] }} beli belil"></i>
        </span>
      {% endif %}
    {% else %}
      <i class="{{ icon_left }} beli belil"></i>
    {% endif %}
  {% endif %}
{% set class = ['bel', class]|join(' ') %}

{% set content %}
  <span class="bell {% if tgl_text is not empty %}tgl-opn{% endif %}">
    {{ text|raw }}
  </span>

  {% if tgl_text is not empty %}
    <span class="bell tgl-cls">
      {{ tgl_text|raw }}
    </span>
  {% endif %}

  {% if icon_right is not empty %}<i class="{{ icon_right }} beli belir"></i>{% endif %}
  {% if icon_right_hidden is not empty %}<i class="{{ icon_right_hidden }} beli belir belih"></i>{% endif %}
{% endset %}

{# Template #}
{{ status_macros.status(status, icon_left, tag_name, class, attributes, 'beli belil', content) }}
