{#
/**
 * @file
 * Template for a Pagination components.
 *
 * Available config:
 * - pagination_utility_classes: An array of utility classes.
 * - size: sm, lg
 * - alignment: start | end | center | vertical.
 */
 */
#}
{%
  set pagination_classes = [
  'pagination-wrapper',
]|merge(pagination_utility_classes ?: [])
%}

{% set pagination_attributes = attributes ?: create_attribute() %}

{%
  set alignment_classes = {
  'end': 'justify-content-end',
  'center': 'justify-content-center',
  'vertical': 'flex-column',
  'start': ''
}
%}

{% set alignment = alignment_classes[alignment] ?? '' %}
{% set show_last = show_last ?? true %}
{% set show_first = show_first ?? true %}
{% set show_ellipsis = show_ellipsis ?? true %}

{% if items %}
  <nav {{ pagination_attributes.addClass(pagination_classes)}} role="navigation" aria-label="{{ 'Pagination'|t }}">
    <ul
      class="pagination pager__items js-pager__items d-flex flex-wrap {{ size ? 'pagination-' ~ size }} {{ alignment }}">
      {# Print first item if we are not on the first page. #}
      {% if items.first and show_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">{{ items.first.text|default('«'|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">
          {% block pagination_previous %}
            <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev" {{ items.previous.attributes|without('href', 'title', 'rel') }} class="page-link">
              <span class="visually-hidden">{{ 'Previous page'|t }}</span>
              <span aria-hidden="true">{{ items.previous.text|default('‹'|t) }}</span>
            </a>
          {% endblock %}
        </li>
      {% endif %}

      {# Add an ellipsis if there are further previous pages. #}
      {% if ellipses.previous and show_ellipsis %}
        <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'|t }} {{ items.current }}
          </span>
        </li>
      {% endif %}

      {# Add an ellipsis if there are further next pages. #}
      {% if ellipses.next and show_ellipsis %}
        <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">
          {% block pagination_next %}
            <a href="{{ items.next.href }}" title="{{ 'Go to next page'|t }}" rel="next" {{ items.next.attributes|without('href', 'title', 'rel') }} class="page-link">
              <span class="visually-hidden">{{ 'Next page'|t }}</span>
              <span aria-hidden="true">{{ items.next.text|default('›'|t) }}</span>
            </a>
          {% endblock %}
        </li>
      {% endif %}

      {# Print last item if we are not on the last page. #}
      {% if items.last and show_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">{{ items.last.text|default('»'|t) }}</span>
          </a>
        </li>
      {% endif %}
    </ul>
  </nav>
{% endif %}
