{#
/**
 * @file
 * Vartheme BS5 Starter Kit implementation for displaying a view as a table.
 *
 * Available variables:
 * - attributes: Remaining HTML attributes for the element.
 *   - class: HTML classes that can be used to style contextually through CSS.
 * - title: The title of this group of rows.
 * - header: The table header columns.
 *   - attributes: Remaining HTML attributes for the element.
 *   - content: HTML classes to apply to each header cell, indexed by
 *     the header's key.
 *   - default_classes: A flag indicating whether default classes should be
 *     used.
 * - caption_needed: Is the caption tag needed.
 * - caption: The caption for this table.
 * - accessibility_description: Extended description for the table details.
 * - accessibility_summary: Summary for the table details.
 * - rows: Table row items. Rows are keyed by row number.
 *   - attributes: HTML classes to apply to each row.
 *   - columns: Row column items. Columns are keyed by column number.
 *     - attributes: HTML classes to apply to each column.
 *     - content: The column content.
 *   - default_classes: A flag indicating whether default classes should be
 *     used.
 * - responsive: A flag indicating whether table is responsive.
 * - sticky: A flag indicating whether table header is sticky.
 * - summary_element: A render array with table summary information (if any).
 *
 * @see template_preprocess_views_view_table()
 *
 * @ingroup themeable
 */
#}

{# Build header cells from first row if available #}
{% set header_cells = [] %}
{% set first_row = rows|first %}
{% if first_row and first_row['#view'] and first_row['#view'].field %}
  {% for field_name, field_handler in first_row['#view'].field %}
    {# Check if field should be included in display #}
    {% if not field_handler.options.exclude and 
          first_row['#row'] is defined and 
          first_row['#row']._entity is defined and 
          first_row['#row']._entity.hasField(field_name) %}

      {% set field_definition = first_row['#row']._entity.getFieldDefinition(field_name) %}

      {# Build header cell attributes #}
      {% if not header_attributes %}
        {% set header_attributes = create_attribute() %}
      {% endif %}

      {# Determine header content based on label settings #}
      {% set header_content = '' %}
      {% if field_handler.options.label and field_handler.options.label != 'hidden' %}
        {% set header_content = field_handler.options.label %}
      {% else %}
        {% set header_content = field_definition.getLabel() %}
      {% endif %}

      {# Apply label colon setting #}
      {% if header_content and field_handler.options.element_label_colon %}
        {% set header_content = header_content ~ ':' %}
      {% endif %}

      {% set header_cell = {
        'tag': 'th',
        'attributes': header_attributes,
        'content': header_content
      } %}
      {% set header_cells = header_cells|merge([header_cell]) %}
    {% endif %}
  {% endfor %}
{% endif %}

{# Build table rows #}
{% set table_rows = [] %}
{% if rows is iterable %}
  {% for row in rows %}
    {% set cells = [] %}
    {# Validate row structure before processing #}
    {% if row['#row'] is defined and 
          row['#row']._entity is defined and 
          row['#view'] is defined and 
          row['#view'].field is defined %}

      {# Process each field in the current row #}
      {% for field_name, field_handler in row['#view'].field %}
        {# Check if field should be included #}
        {% if not field_handler.options.exclude and 
              row['#row']._entity.hasField(field_name) %}

          {% set field_value = row['#row']._entity.get(field_name) %}

          {# Build cell attributes #}
          {% set cell_attributes = create_attribute() %}
          {% if field_handler.options.element_class %}
            {% set cell_attributes = cell_attributes.addClass(field_handler.options.element_class) %}
          {% endif %}

          {# Determine cell content #}
          {% set cell_content = '' %}
          {% if not field_value.isEmpty() %}
            {# Apply field alterations if configured #}
            {% if field_handler.options.alter is defined and 
                  field_handler.options.alter.alter_text and 
                  field_handler.options.alter.text %}
              {% set cell_content = field_handler.options.alter.text %}
            {% else %}
              {# Use the rendered field content #}
              {% set cell_content = field_handler.last_render %}
            {% endif %}
          {% else %}
            {# Handle empty field display #}
            {% if field_handler.options.empty_field_behavior is defined and 
                  field_handler.options.empty_field_behavior.empty_text %}
              {% set cell_content = field_handler.options.empty_field_behavior.empty_text %}
            {% endif %}
          {% endif %}

          {% set cell = {
            'tag': 'td',
            'attributes': cell_attributes,
            'content': cell_content
          } %}
          {% set cells = cells|merge([cell]) %}
        {% endif %}
      {% endfor %}

      {# Add row only if it has cells #}
      {% if cells|length > 0 %}
        {# Build row attributes #}
        {% set row_attributes = create_attribute() %}
        {% if row['#row_class'] is defined and row['#row_class'] %}
          {% for class in row['#row_class'] %}
            {% set row_attributes = row_attributes.addClass(class) %}
          {% endfor %}
        {% endif %}

        {% set table_row = {
          'attributes': row_attributes,
          'cells': cells
        } %}

        {% set table_rows = table_rows|merge([table_row]) %}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endif %}

{# Render the table component #}
{{
  include('vartheme_bs5:table', {
    header: header_cells,
    rows: table_rows,
    footer: [],
    caption: caption ?? '',
    empty: "<p>Empty table!</p>"|t,
    responsive: responsive ?? false,
    sticky: sticky ?? false,
    variant: variant ?? null,
    striped_rows: striped_rows ?? false,
    striped_columns: striped_columns ?? false,
    no_striping: not (striped_rows ?? false or striped_columns ?? false),
    hover: hover ?? false,
    bordered: bordered ?? false,
    border_color: border_color ?? null,
    borderless: not (bordered ?? false),
    size: size ?? null,
    table_utility_classes: table_utility_classes ?? [],
    thead_utility_classes: thead_utility_classes ?? [],
    tbody_utility_classes: tbody_utility_classes ?? [],
    tfoot_utility_classes: tfoot_utility_classes ?? [],
    attributes: attributes ?? create_attribute(),
    thead_attributes: thead_attributes ?? create_attribute(),
    tbody_attributes: tbody_attributes ?? create_attribute(),
    tfoot_attributes: tfoot_attributes ?? create_attribute()
  }, with_context = false)
}}
