{# requires pageInfo variable #}

{# settings #}
{% set neighbours = 1 %}

{# symbols #}
{% set dots = '&hellip;' %}

{# single numeric link #}
{% macro numericLink(url, number, current) %}
    <li class="{{ current ? 'm-active' : 'pag-numb' }}">
        <a href="{{ url }}"
           data-number="{{ number }}"
           class="pag-text"
           aria-label="{{ current ? 'Página atual'|t : ('Ir para página'|t) ~ ' ' ~ number }}"
                {{ current ? 'aria-current="page"' }}>
            {{ number }}
        </a>
    </li>
{% endmacro %}

{# next/prev link #}
{% macro textLink(url, content, aria, number) %}
    <div class="pag-arrow">
        <a href="{{ url }}" data-number="{{ number }}" class="pag-arrow-icon" aria-label="{{ aria }}">
            {{ content|raw }}
        </a>
    </div>
{% endmacro %}

{# ellipsis #}
{% macro ellipsis(content) %}
    <li>
        <span class="pagination-ellipsis">{{ content|raw }}</span>
    </li>
{% endmacro %}

{# pagination logic #}
{% if pageInfo is defined and pageInfo.totalPages > 1 %}
    {% import _self as self %}

    {# seomatic #}
    {# https://github.com/nystudio107/craft-seomatic#pagination-and-seo #}
    {% if seomatic is defined %}
        {% do seomatic.helper.paginate(pageInfo) %}
    {% endif %}

    <div class="c-pagination">
        <ul class="pag-wrap">
            {# first #}
            {% if pageInfo.currentPage - neighbours > 1 %}
                {{ self.numericLink(pageInfo.firstUrl, '1') }}
            {% endif %}

            {# ellipsis before current #}
            {% if pageInfo.currentPage - neighbours > 2 %}
                {{ self.ellipsis(dots) }}
            {% endif %}

            {# links before current #}
            {% for page, url in pageInfo.getPrevUrls(neighbours) %}
                {{ self.numericLink(url, page) }}
            {% endfor %}

            {# current #}
            {{ self.numericLink('', pageInfo.currentPage, true) }}

            {# links after current #}
            {% for page, url in pageInfo.getNextUrls(neighbours) %}
                {{ self.numericLink(url, page) }}
            {% endfor %}

            {# ellipsis after current #}
            {% if pageInfo.totalPages - pageInfo.currentPage > neighbours + 1 %}
                {{ self.ellipsis(dots) }}
            {% endif %}

            {# last #}
            {% if pageInfo.currentPage + neighbours < pageInfo.totalPages %}
                {{ self.numericLink(pageInfo.lastUrl, pageInfo.totalPages) }}
            {% endif %}
        </ul>
    </div>
{% endif %}
<style>
    li {
        list-style-type: none;
    }
</style>
