{# LIBRARIES #}
{{ attach_library('ui_patterns/libraries.bs_dropdown') }}

{# VARIABLES #}
{% set status = status|default('default') %}
{% set cards_per_slide = cards_per_slide|default(3) %}
{% set controls_type = controls_type|default('dropdown') %}

{# TEMPLATE #}
{% if stations_list|length > 1 %}
  <div class="carousel-select d-inline-block controls-{{ controls_type }}">
    <button class="btn btn-{{ status }} dropdown-toggle mb-0"
            type="button"
            id="crsls-best-fares"
            data-bs-toggle="dropdown" >
      Station
    </button>
    <ul class="dropdown-menu ulp{% if tab_functionality%} nav nav-tabs{% endif %}" aria-labelledby="crsls-best-fares">
      {% set cards = 0 %}
      {% set slides = 0 %}
      {% for crs, ticket_group in tickets %}
        {% set cards = (ticket_group|length / cards_per_slide) | round(0, 'ceil') %}
        {% set button_attributes = create_attribute() %}
        {% set button_attributes = button_attributes
          .setAttribute('type', 'button')
          .setAttribute('aria-label', "Go to "~stations_list[crs])
        %}
        {% if tab_functionality %}
          {% set button_attributes = button_attributes
            .setAttribute("id", crs~"-tab")
            .setAttribute("data-bs-toggle", "tab")
            .setAttribute("data-bs-target", "#fares-"~crs)
            .setAttribute("aria-controls", "fares-"~crs)
            .addClass(["dropdown-item nav-link"])
          %}
        {% else %}
          {% set cards = (ticket_group|length / cards_per_slide) | round(0, 'ceil') %}
          {% set button_attributes = button_attributes
            .setAttribute("id", "crsls-"~crs~"-btn")
            .setAttribute("data-bs-target", "#crslc-best-fares")
            .setAttribute("data-bs-slide-to", slides~"")
            .setAttribute("aria-controls", "fares-"~crs)
            .addClass(["dropdown-item crslsb"])
          %}
        {% endif %}
        {% if loop.first %}
          {% set button_attributes = button_attributes
            .addClass(["active"])
          %}
        {% endif %}
        <li>
          <button {{ button_attributes }}>
            {{ stations_list[crs] }}
          </button>
        </li>
        {% set slides = slides + cards %}
      {% endfor %}
    </ul>
  </div>
{% else %}
  {% for crs, ticket_group in tickets %}
    {{ stations_list[crs] }}
  {% endfor %}
{% endif %}
