{% apply spaceless %}
{#
  Parameters:
  - "color_mode" (string) (default: '') Name of the color mode
  - "id" (string) (default: random): unique id for the table; randomly generated if empty
  - "simple" (boolean) (default: false)
  - "sortable" (boolean) (default: false)
  - "filter" (boolean) (default: false)
  - "caption" (string) (default: ''): optional caption for the table
  - "label_sort_ascending" (string) (default: ''): label used for sorting ascending (screen reader only)
  - "label_sort_descending" (string) (default: ''): label used for sorting descending (screen reader only)
  - "label_sort_default" (string) (default: ''): label used for sorting default (screen reader only)
  - "label_filter" (string) (default: ''): label used for the column filter fields (screen reader only)
  - "zebra" (boolean) (default: false))
  - "headers" (array) (default: []): format: [
      {
        "label" (string or array of string)
        "colspan" (string) (default: ''),
        "rowspan" (string) (default: ''),
        "data-ecl-table-header-group" (string) (default: ''),
        "headers" (string) (default: ''): headers attribute to reference the relevant table headers ids (for multi headers table)
      },
      ...
    ]
  - "rows" (array) (default: []): format: [
      {
        "extra_attributes": (string) (default: ''),
        "extra_classes": (string) (default: ''),
        "data": [
          {
            "label" (string or array of string)
            "data-ecl-table-header" (string) (default: ''),
            "group" (bool) (default: false),
            "headers" (string) (default: ''): headers attribute to reference the relevant table headers ids (for multi headers table)
          }
        ],
      },
      ...
    ],
    ...
    ]
  - "extra_classes" (string) (default: '') Extra classes (space separated)
  - "extra_attributes" (array) (default: []): format: [
      {
        "name" (string) Attribute name, eg. 'data-test'
        "value" (string) Attribute value, eg: 'data-test-1'
      }, ...
    ]
#}

{# Internal properties #}

{% set _color_mode = color_mode|default('') %}
{% set _id = id|default('ecl-table-' ~ random()) %}
{% set _css_class = 'ecl-table' %}
{% set _extra_attributes = '' %}
{% set _simple_css_class = 'ecl-table--simple' %}
{% set _zebra_css_class = 'ecl-table--zebra' %}
{% set _head_css_class = 'ecl-table__head' %}
{% set _header_css_class = 'ecl-table__header' %}
{% set _body_css_class = 'ecl-table__body' %}
{% set _cell_header_group_class = 'ecl-table__cell--group' %}
{% set _row_css_class = 'ecl-table__row' %}
{% set _headers = headers|default([]) %}
{% set _simple = simple|default(false) %}
{% set _sortable = sortable|default(false) %}
{% set _caption = caption|default('') %}
{% set _label_sort_ascending = label_sort_ascending|default(label_sort|default('')) %}
{% set _label_sort_descending = label_sort_descending|default(label_sort|default('')) %}
{% set _label_sort_default = label_sort_default|default(label_sort|default('')) %}
{% set _rows = rows|default([]) %}
{% set _zebra = zebra|default(false) %}
{% set _filter = filter|default(false) %}
{% set _label_filter = label_filter|default('') %}

{# Internal logic - Process properties #}

{% if extra_classes is defined and extra_classes is not empty %}
  {% set _css_class = _css_class ~ ' ' ~ extra_classes %}
{% endif %}

{% if _sortable or _filter %}
  {% set extra_attributes = extra_attributes|default([])|merge([
    { name: 'data-ecl-table' },
    { name: 'data-ecl-auto-init', value: 'Table' },
  ]) %}
{% endif %}


{% if _sortable %}
  {% set extra_attributes = extra_attributes|default([])|merge([
    { name: 'data-ecl-table-sort' },
    { name: 'data-ecl-table-sort-label-asc', value: _label_sort_ascending },
    { name: 'data-ecl-table-sort-label-desc', value: _label_sort_descending },
    { name: 'data-ecl-table-sort-label-default', value: _label_sort_default }
  ]) %}
{% endif %}

{% if _filter %}
  {% set extra_attributes = extra_attributes|default([])|merge([
    { name: 'data-ecl-table-filter' },
    { name: 'data-ecl-table-filter-label', value: _label_filter },
  ]) %}
{% endif %}

{% if _color_mode is not empty %}
  {% set _css_class = _css_class ~ ' ' ~ 'ecl-color-mode--' ~ _color_mode %}
{% endif %}

{% if _simple %}
  {% set _css_class = _css_class ~ ' ' ~ _simple_css_class %}
{% endif %}

{% if _zebra %}
  {% set _css_class = _css_class ~ ' ' ~ _zebra_css_class %}
{% endif %}

{% if extra_attributes is defined and extra_attributes is not empty and extra_attributes is iterable %}
  {% for attr in extra_attributes %}
    {% if attr.value is defined %}
      {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name|e('html_attr') ~ '="' ~ attr.value|e('html_attr') ~ '"' %}
    {% else %}
      {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name|e('html_attr') %}
    {% endif %}
  {% endfor %}
{% endif %}

{# Print the result #}

<div class="ecl-table-responsive">
  <table
    class="{{ _css_class }}"
    id="{{ _id }}"
    {{ _extra_attributes|raw }}
  >
    {% if _caption is not empty %}
    <caption class="ecl-table__caption">{{ _caption }}</caption>
    {% endif %}

    {% set _index = 1 %}
    {% if _headers is not empty %}
    <thead class="{{ _head_css_class }}">
      {% for header in _headers %}
        <tr class="{{ _row_css_class }}">
          {% for cell in header %}
            {% set _heading_attribute = 'id="' ~ _id  ~ '-header-' ~ _index ~ '"' %}
            {% set _index = _index + 1 %}
            {% set _heading_scope = 'col' %}
            {% if cell.rowspan is not empty %}
              {% set _heading_attribute = _heading_attribute ~ ' rowspan="' ~ cell.rowspan|e('html_attr') ~ '"' %}
            {% endif %}
            {% if cell.colspan is not empty %}
              {% set _heading_attribute = _heading_attribute ~ ' colspan="' ~ cell.colspan|e('html_attr') ~ '"' %}
              {% set _heading_scope = 'colgroup' %}
            {% else %}
              {% if cell.label is not empty and _sortable %}
                {% set _heading_attribute = _heading_attribute ~ ' data-ecl-table-sort-toggle' %}
              {% endif %}
              {% if cell.label is not empty and _filter %}
                {% set _heading_attribute = _heading_attribute ~ ' data-ecl-table-filter-toggle' %}
              {% endif %}
            {% endif %}
            {% if cell.headers is not empty %}
              {% set _heading_attribute = _heading_attribute ~ ' headers="' ~ cell.headers|e('html_attr') ~ '"' %}
            {% endif %}
            {% set _heading_attribute = _heading_attribute ~ ' scope="' ~ _heading_scope ~ '"' %}
            <th {{ _heading_attribute|raw }} class="{{ _header_css_class }}">{{ cell.label }}</th>
          {% endfor %}
        </tr>
      {% endfor %}
    </thead>
    {% endif %}
    
    <tbody class="{{ _body_css_class }}">
      {% for row in _rows %}
        {% set _row_css_class = 'ecl-table__row' %}
        {% set _row_extra_attributes = '' %}
        {% if row.extra_classes is defined and row.extra_classes is not empty %}
          {% set _row_css_class = _row_css_class ~ ' ' ~ row.extra_classes %}
        {% endif %}
        {% if row.extra_attributes is defined and row.extra_attributes is not empty %}
          {% for attr in row.extra_attributes %}
            {% if attr.value is defined %}
              {% set _row_extra_attributes = _row_extra_attributes ~ ' ' ~ attr.name|e('html_attr') ~ '="' ~ attr.value|e('html_attr') ~ '"' %}
            {% else %}
              {% set _row_extra_attributes = _row_extra_attributes ~ ' ' ~ attr.name|e('html_attr') %}
            {% endif %}
          {% endfor %}
        {% endif %}
        <tr class="{{ _row_css_class }}"{{ _row_extra_attributes }}>
          {% for cell in row.data %}
            {% set _cell_css_class = 'ecl-table__cell' %}
            {% set _cell_attribute = cell['data-ecl-table-header'] is not empty ? 'data-ecl-table-header="' ~ cell['data-ecl-table-header']|e('html_attr') ~ '"' : '' %}
            {% if cell.group %}
              {% if cell['data-ecl-table-header-group'] is defined and cell['data-ecl-table-header-group'] is not empty %}
                {% set _cell_attribute = _cell_attribute ~ ' data-ecl-table-header-group="' ~ cell['data-ecl-table-header-group']|e('html_attr') ~ '"' %}
              {% endif %}
              {% set _cell_css_class = _cell_css_class ~ ' ' ~ _cell_header_group_class %}
            {% endif %}
            {% if cell.headers is not empty %}
              {% set _cell_attribute = _cell_attribute ~ 'headers="' ~ cell.headers|e('html_attr') ~ '"' %}
            {% endif %}
            <td {{ _cell_attribute|raw }} class="{{ _cell_css_class }}" >{{ cell.label }}</td>
          {% endfor %}
        </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

{% endapply %}
