{#
/**
 * @file
 * list-group.twig
 * Bootstrap 5.3 List Group component implementation
 *
 * Available properties:
 * - items: Array of list group items with content, active, disabled, href, badge, and variant properties
 * - flush: Boolean to remove borders and rounded corners
 * - numbered: Boolean to add numbers to list items
 * - horizontal: String for horizontal breakpoint (sm, md, lg, xl, xxl)
 * - utility_classes: Array of additional utility classes
 * - attributes: HTML attributes for the list group
 */
#}
{% apply spaceless %}
{% if items is iterable and items|length > 0 %}
  {% set utility_classes = utility_classes ? utility_classes : [] %}
  
  {# Build CSS classes #}
  {% set list_classes = ['list-group'] %}
  
  {% if flush %}
    {% set list_classes = list_classes|merge(['list-group-flush']) %}
  {% endif %}
  
  {% if numbered %}
    {% set list_classes = list_classes|merge(['list-group-numbered']) %}
  {% endif %}
  
  {% if horizontal %}
    {% if horizontal == 'sm' or horizontal == 'md' or horizontal == 'lg' or horizontal == 'xl' or horizontal == 'xxl' %}
      {% set list_classes = list_classes|merge(['list-group-horizontal-' ~ horizontal]) %}
    {% else %}
      {% set list_classes = list_classes|merge(['list-group-horizontal']) %}
    {% endif %}
  {% endif %}
  
  {% set list_classes = list_classes|merge(utility_classes) %}
  
  {# Set up attributes #}
  {% if not attributes %}
    {% set attributes = create_attribute() %}
  {% endif %}
  {% set attributes = attributes.addClass(list_classes) %}
  
  {# Determine if we need div or ul based on whether items have links #}
  {% set has_links = false %}
  {% for item in items %}
    {% if item.href is defined and item.href %}
      {% set has_links = true %}
    {% endif %}
  {% endfor %}
  
  {% if has_links %}
    <div{{ attributes }}>
  {% else %}
    <ul{{ attributes }}>
  {% endif %}
    
    {% for item in items %}
      {% set item_classes = ['list-group-item'] %}
      
      {# Add contextual classes #}
      {% if item.active is defined and item.active %}
        {% set item_classes = item_classes|merge(['active']) %}
      {% endif %}
      
      {% if item.disabled is defined and item.disabled %}
        {% set item_classes = item_classes|merge(['disabled']) %}
      {% endif %}
      
      {% if item.variant is defined and item.variant %}
        {% set item_classes = item_classes|merge(['list-group-item-' ~ item.variant]) %}
      {% endif %}
      
      {# Handle different item types #}
      {% if item.href is defined and item.href %}
        {% set item_classes = item_classes|merge(['list-group-item-action']) %}
        <a href="{{ item.href }}" class="{{ item_classes|join(' ') }}"{% if item.disabled is defined and item.disabled %} aria-disabled="true"{% endif %}>
          {{ item.content }}
          {% if item.badge is defined and item.badge.text %}
            <span class="badge bg-{{ item.badge.variant|default('primary') }} rounded-pill">{{ item.badge.text }}</span>
          {% endif %}
        </a>
      {% else %}
        {% if has_links %}
          <div class="{{ item_classes|join(' ') }}">
        {% else %}
          <li class="{{ item_classes|join(' ') }}">
        {% endif %}
          {% if item.content is iterable %}
            {{ item.content|render }}
          {% else %}
            {{ item.content }}
          {% endif %}
          {% if item.badge is defined and item.badge.text %}
            <span class="badge bg-{{ item.badge.variant|default('primary') }} rounded-pill">{{ item.badge.text }}</span>
          {% endif %}
        {% if has_links %}
          </div>
        {% else %}
          </li>
        {% endif %}
      {% endif %}
    {% endfor %}
    
  {% if has_links %}
    </div>
  {% else %}
    </ul>
  {% endif %}
{% endif %}
{% endapply %}