{#
/**
 * @file
 * Vartheme BS5 Starter Kit implementation for the pagination component.
 *
 * Available variables (From Drupal views):
 * - heading_id: Pagination heading ID.
 * - items: List of pager items.
 *   The list is keyed by the following elements:
 *   - first: Item for the first page; not present on the first page of results.
 *   - previous: Item for the previous page; not present on the first page
 *     of results.
 *   - next: Item for the next page; not present on the last page of results.
 *   - last: Item for the last page; not present on the last page of results.
 *   - pages: List of pages, keyed by page number.
 *   Sub-sub elements:
 *   items.first, items.previous, items.next, items.last, and each item inside
 *   items.pages contain the following elements:
 *   - href: URL with appropriate query parameters for the item.
 *   - attributes: A keyed list of HTML attributes for the item.
 *   - text: The visible text used for the item link, such as "Previous"
 *     or "Next".
 * - current: The page number of the current page.
 * - ellipses: If there are more pages than the quantity allows, then an
 *   ellipsis before or after the listed pages may be present.
 *   - previous: Present if the currently visible list of pages does not start
 *     at the first page.
 *   - next: Present if the visible list of pages ends before the last page.
 *
 * Available properties:
 * - size: sm, lg
 * - alignment: start | end | center | vertical.
 * - pagination_utility_classes: An array of utility classes. Use this property to add
 *   extra Bootstrap utility classes or your custom class over to this to this component.
 *
 * Available attributes:        
 * - attributes: HTML attributes for the pagination element.
 *
 * Available slots:
 * - N/A 
 *
 */
#}

{% set size = size ? ['pagination-' ~ size] : [] %}
{% if alignment == 'end' %}
  {% set alignment = ['justify-content-end'] %}
{% elseif alignment == 'center' %}
  {% set alignment = ['justify-content-center'] %}
{% else %}
  {% set alignment = [] %}
{% endif %}
{% set pagination_utility_classes = pagination_utility_classes ? pagination_utility_classes : [] %}

{% set classes = []|merge(pagination_utility_classes) %}

{% if not attributes %}
  {% set attributes = create_attribute() %}
{% endif %}
{% set attributes = attributes.addClass(classes) %}
{% set attributes = attributes.setAttribute('role', 'navigation') %}
{% set attributes = attributes.setAttribute('aria-label', 'Pagination'|t) %}

{%
  set pagination_classes = [
    'pagination',
    'pager__items',
    'js-pager__items',
  ]|merge(size)|merge(alignment)|merge(pagination_utility_classes)
%}

{% if not pagination_attributes %}
  {% set pagination_attributes = create_attribute() %}
{% endif %}
{% set pagination_attributes = pagination_attributes.addClass(pagination_classes) %}

{% if items %}
  <navs{{ attributes }}>
    <ul{{ pagination_attributes }}>
      {# Print first item if we are not on the first page. #}
      {% if items.first %}
        <li class="page-item pager__item pager__item--first">
          <a href="{{ items.first.href }}" title="{{ 'Go to first page'|t }}"{{ items.first.attributes|without('href', 'title') }} class="page-link">
            <span class="visually-hidden">{{ 'First page'|t }}</span>
            <span aria-hidden="true">{{ 'First'|t }}</span>
          </a>
        </li>
      {% endif %}
      {# Print previous item if we are not on the first page. #}
      {% if items.previous %}
        <li class="page-item pager__item pager__item--previous">
          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }} class="page-link">
            {% include 'vartheme_bs5:icon' with { name: 'bi-chevron-left', rtl: 'bi-chevron-right', lang_dir: lang_dir } only %}
            <span class="visually-hidden">{{ 'Previous page'|t }}</span>
          </a>
        </li>
      {% endif %}
      {# Add an ellipsis if there are further previous pages. #}
      {% if ellipses.previous %}
        <li class="page-item pager__item pager__item--ellipsis disabled" role="presentation"><span class="page-link">&hellip;</span></li>
      {% endif %}
      {# Now generate the actual pager piece. #}
      {% for key, item in items.pages %}
        <li class="page-item pager__item{{ current == key ? ' is-active active' : '' }}">
          {% if current == key %}
            {% set title = 'Current page'|t %}
          {% else %}
            {% set title = 'Go to page @key'|t({'@key': key}) %}
          {% endif %}
          <a href="{{ item.href }}" title="{{ title }}"{{ item.attributes|without('href', 'title') }} class="page-link">
            <span class="visually-hidden">
              {{ current == key ? 'Current page'|t : 'Page'|t }}
            </span>
            {{- key -}}
          </a>
        </li>
      {% endfor %}


      {% if items.current and (items.previous or items.next) %}
        <li class="page-item disabled">
          <span class="page-link">
            {{ 'Page' ~ items.current |t }}
          </span>
        </li>
      {% endif %}

      {# Add an ellipsis if there are further next pages. #}
      {% if ellipses.next %}
        <li class="page-item pager__item pager__item--ellipsis disabled" role="presentation"><span class="page-link">&hellip;</span></li>
      {% endif %}
      {# Print next item if we are not on the last page. #}
      {% if items.next %}
        <li class="page-item pager__item pager__item--next">
          <a href="{{ items.next.href }}" title="{{ 'Go to next page'|t }}" rel="next"{{ items.next.attributes|without('href', 'title', 'rel') }} class="page-link">
            {% include 'vartheme_bs5:icon' with { name: 'bi-chevron-right', rtl: 'bi-chevron-left', lang_dir: lang_dir } only %}
            <span class="visually-hidden">{{ 'Next page'|t }}</span>
          </a>
        </li>
      {% endif %}
      {# Print last item if we are not on the last page. #}
      {% if items.last %}
        <li class="page-item pager__item pager__item--last">
          <a href="{{ items.last.href }}" title="{{ 'Go to last page'|t }}"{{ items.last.attributes|without('href', 'title') }} class="page-link">
            <span class="visually-hidden">{{ 'Last page'|t }}</span>
            <span aria-hidden="true">{{ 'Last'|t }}</span>
          </a>
        </li>
      {% endif %}
    </ul>
  </nav>
{% endif %}
