{#
/* cspell:ignore syncscroll */
/**
 * @file
 * Theme override to display a table.
 *
 * Available variables:
 * - attributes: HTML attributes to apply to the <table> tag.
 * - caption: A localized string for the <caption> tag.
 * - colgroups: Column groups. Each group contains the following properties:
 *   - attributes: HTML attributes to apply to the <col> tag.
 *     Note: Drupal currently supports only one table header row.
 * - header: Table header cells. Each cell contains the following properties:
 *   - tag: The HTML tag name to use; either 'th' or 'td'.
 *   - attributes: HTML attributes to apply to the tag.
 *   - content: A localized string for the title of the column.
 *   - field: Field name (required for column sorting).
 *   - sort: Default sort order for this column ("asc" or "desc").
 * - sticky: A flag indicating whether to use a "sticky" table header.
 * - rows: Table rows. Each row contains the following properties:
 *   - attributes: HTML attributes to apply to the <tr> tag.
 *   - data: Table cells.
 *   - no_striping: A flag indicating that the row should receive no
 *     'even / odd' styling. Defaults to FALSE.
 *   - cells: Table cells of the row. Each cell contains the following keys:
 *     - tag: The HTML tag name to use; either 'th' or 'td'.
 *     - attributes: Any HTML attributes, such as "colspan", to apply to the
 *       table cell.
 *     - content: The string to display in the table cell.
 *     - active_table_sort: A boolean indicating whether the cell is the active
         table sort.
 * - footer: Table footer rows, in the same format as the rows variable.
 * - empty: The message to display in an extra row if table does not have
 *   any rows.
 * - no_striping: A boolean indicating that the row should receive no striping.
 * - header_columns: The number of columns in the header.
 *
 * @see template_preprocess_table()
 */
#}

{% import _self as macros %}

{% macro table_header(header) %}
  <thead>
    <tr>
      {% for cell in header %}
        {% if '<a' in cell.content|render|render %}
          {%
            set cell_classes = [
              'th__item',
              cell.active_table_sort ? 'is-active',
              'select-all' in cell.attributes ? 'gin--sticky-bulk-select',
            ]
          %}
        {% else %}
          {%
            set cell_classes = [
              cell.content|render|clean_class ? 'th__' ~ cell.content|render|clean_class,
              cell.active_table_sort ? 'is-active',
              'select-all' in cell.attributes ? 'gin--sticky-bulk-select',
            ]
          %}
        {% endif %}
        <{{ cell.tag }}{{ cell.attributes.addClass(cell_classes) }}>
          {{- cell.content -}}
          {% if 'gin--sticky-bulk-select' in cell_classes %}
            <input
              type="checkbox"
              class="form-checkbox form-boolean form-boolean--type-checkbox"
              title="{{ 'Select all rows in this table'|t }}"
            />
          {% endif %}
        </{{ cell.tag }}>
      {% endfor %}
    </tr>
  </thead>
{% endmacro %}

<div class="layer-wrapper gin-layer-wrapper">
  {% if header %}
    {% if sticky %}
      <table class="gin--sticky-table-header syncscroll" name="gin-sticky-header" hidden>
        {{ macros.table_header(header) }}
      </table>
    {% endif %}
  <div class="gin-table-scroll-wrapper gin-horizontal-scroll-shadow syncscroll" name="gin-sticky-header">
  {% endif %}
    <table{{ attributes }}>
      {% if caption %}
        <caption>{{ caption }}</caption>
      {% endif %}

      {% for colgroup in colgroups %}
        {% if colgroup.cols %}
          <colgroup{{ colgroup.attributes }}>
            {% for col in colgroup.cols %}
              <col{{ col.attributes }} />
            {% endfor %}
          </colgroup>
        {% else %}
          <colgroup{{ colgroup.attributes }} />
        {% endif %}
      {% endfor %}

      {% if header %}
        {{ macros.table_header(header) }}
      {% endif %}

      {% if rows %}
        <tbody>
          {% for row in rows %}
            {%
              set row_classes = [
                not no_striping ? cycle(['odd', 'even'], loop.index0),
              ]
            %}
            <tr{{ row.attributes.addClass(row_classes) }}>
              {% for cell in row.cells %}
                <{{ cell.tag }}{{ cell.attributes }}>
                  {{- cell.content -}}
                </{{ cell.tag }}>
              {% endfor %}
            </tr>
          {% endfor %}
        </tbody>
      {% elseif empty %}
        <tbody>
          <tr class="odd">
            <td colspan="{{ header_columns }}" class="empty message">{{ empty }}</td>
          </tr>
        </tbody>
      {% endif %}
      {% if footer %}
        <tfoot>
          {% for row in footer %}
            <tr{{ row.attributes }}>
              {% for cell in row.cells %}
                <{{ cell.tag }}{{ cell.attributes }}>
                  {{- cell.content -}}
                </{{ cell.tag }}>
              {% endfor %}
            </tr>
          {% endfor %}
        </tfoot>
      {% endif %}
    </table>
  {% if header %}
  </div>
  {% endif %}
</div>
