{#
/**
 * @file
 * Template for a Pagination components.
 *
 * Available config:
 * - utility_classes: An array of utility classes.
 * - size: sm, md, lg
 * - alignment: left | right | center | vertical.
 */
 */
#}

{# LIBRARIES #}
{{ attach_library('ui_patterns/libraries.bs_pagination') }}
{{ attach_library('ui_patterns/pager.pager_lib') }}

{# VARIABLES #}
{% if alignment == 'right' %}
  {% set alignment = 'justify-content-end' %}
{% elseif alignment == 'center' %}
  {% set alignment = 'justify-content-center' %}
{% else %}
  {% set alignment = '' %}
{% endif %}

{# TEMPLATE #}
{% if items %}
  <nav class="{{ utility_classes|join(' ') }}" role="navigation" aria-labelledby="pagination-heading">
    <h2 id="pagination-heading" class="visually-hidden">{{ 'Pagination'|t }}</h2>
    <ul class="ulp pagination pager__items js-pager__items {{ size ? 'pagination-' ~ size }} {{ alignment }}">
      {# Print first item if we are not on the first page. #}
      {% if items.first %}
        <li class="page-item pager__item pager__item--first">
          {% set attrs = '' %}
          {% if items.first.attributes is iterable %}
            {% set attrs = items.first.attributes|without('href', 'title') %}
          {% endif %}
          <a href="{{ items.first.href }}"
             title="{{ 'Go to first page'|t }}"
             class="page-link"
            {{ attrs }} >
            <span class="visually-hidden">{{ 'First page'|t }}</span>
            <span aria-hidden="true">{{ items.first.text|default('<span class="nav-arrow">«</span> First')|raw }}</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">
          {% set attrs = '' %}
          {% if items.previous.attributes is iterable %}
            {% set attrs = items.previous.attributes|without('href', 'title', 'rel') %}
          {% endif %}
          <a href="{{ items.previous.href }}"
             title="{{ 'Go to previous page'|t }}"
             rel="prev"
             class="page-link"
            {{ attrs }} >
            <span class="visually-hidden">{{ 'Previous page'|t }}</span>
            <span aria-hidden="true">{{ items.previous.text|default('<span class="nav-arrow">‹</span> Previous')|raw }}</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 dtod" 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' : '' }} dtod">
          {% if current == key %}
            {% set title = 'Current page'|t %}
          {% else %}
            {% set title = 'Go to page @key'|t({'@key': key}) %}
          {% endif %}
          {% set attrs = '' %}
          {% if item.attributes is iterable %}
            {% set attrs = item.attributes|without('href', 'title') %}
          {% endif %}
          <a href="{{ item.href }}"
             title="{{ title }}"
             class="page-link"
            {{ attrs }} >
            <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 }}</span>
        </li>
      {% endif %}

      {# Add an ellipsis if there are further next pages. #}
      {% if ellipses_next %}
        <li class="page-item pager__item pager__item--ellipsis dtod" 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">
          {% set attrs = '' %}
          {% if items.next.attributes is iterable %}
            {% set attrs =  items.next.attributes|without('href', 'title', 'rel') %}
          {% endif %}
          <a href="{{ items.next.href }}"
             title="{{ 'Go to next page'|t }}"
             rel="next"
             class="page-link"
            {{ attrs }} >
            <span class="visually-hidden">{{ 'Next page'|t }}</span>
            <span aria-hidden="true">{{ items.next.text|default('Next <span class="nav-arrow">›</span>')|raw }}</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">
          {% set attrs = '' %}
          {% if items.last.attributes is iterable %}
            {% set attrs =  items.last.attributes|without('href', 'title') %}
          {% endif %}
          <a href="{{ items.last.href }}"
             title="{{ 'Go to last page'|t }}"
             class="page-link"
            {{ attrs }} >
            <span class="visually-hidden">{{ 'Last page'|t }}</span>
            <span aria-hidden="true">{{ items.last.text|default('Last <span class="nav-arrow">»</span>')|raw }}</span>
          </a>
        </li>
      {% endif %}
    </ul>
  </nav>
{% endif %}
