{% 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 toggle_open = toggle_open|default(false) %}

{# Status mapping #}
{% set statuses = [
  {'status':'good', 'icon_left': global_status_icon_good|default('fas fa-check-circle'), 'icon_right': null},
  {'status':'minor', 'icon_left': global_status_icon_minor|default('fas fa-exclamation-circle'), 'icon_right': null},
  {'status':'major', 'icon_left': global_status_icon_major|default('fas fa-exclamation-triangle'), 'icon_right': null},
  {'status':'stationalert', 'icon_left': global_status_icon_stationalert|default('fas fa-info-circle'), 'icon_right': null},
  {'status':'noservice', 'icon_left': global_status_icon_noservice|default('fas fa-exclamation-circle'), 'icon_right': null},
  {'status':'amended', 'icon_left': global_status_icon_amended|default('fas fa-exclamation-circle'), 'icon_right': null},
  {'status':'strike', 'icon_left': global_status_icon_strike|default('fas fa-exclamation-circle'), 'icon_right': null},
] %}

{# Status icons and classes #}
{% for item in statuses %}
  {% if status == item.status %}
    {% set icon_left = icon_left ? icon_left : item.icon_left %}
    {% set icon_right = icon_right ? icon_right : item.icon_right %}
    {% set class = [status, class]|join(' ') %}
  {% endif %}
{% endfor %}

{# Icon mapping, for when we just want to pass 'toggle=sign' for collapse triggers. #}
{% set toggles = [
  {'toggle':'sign', 'icon_right': 'fas fa-plus', 'icon_right_hidden': 'fas fa-minus'},
  {'toggle':'chevron', 'icon_right': 'fas fa-chevron-down', 'icon_right_hidden': 'fas fa-chevron-up'},
  {'toggle':'chevron_right', 'icon_right': 'fas fa-chevron-right', 'icon_right_hidden': '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 %}
      <span class="f-stack">
        <i class="{{ icon_left[0] }} beli belil"></i>
        <i class="{{ icon_left[1] }} beli belil"></i>
      </span>
    {% else %}
      <i class="{{ icon_left }} beli belil"></i>
    {% endif %}
  {% endif %}

  <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 %}

</{{ tag_name }}>
