{%- from "kerko/_attributes.html.jinja2" import title_aria_label %}

{%- macro page_prev_item(url) %}
    <li class="page-item">
        <a class="page-link" href="{{ url }}" rel="prev" {{ title_aria_label(_('Previous page')) }}>
            <span class="fas fa-angle-left" aria-hidden="true"></span>
        </a>
    </li>
{%- endmacro %}

{%- macro page_next_item(url) %}
    <li class="page-item">
        <a class="page-link" href="{{ url }}" rel="next" {{ title_aria_label(_('Next page')) }}>
            <span class="fas fa-angle-right" aria-hidden="true"></span>
        </a>
    </li>
{%- endmacro %}

{%- macro page_num_label(page_num, active, last) %}
    {%- trans page_num=page_num %}Page {{ page_num }}{% endtrans %}{% if active %} ({{ _('Current page') }}){% elif last %} ({{ _('Last page') }}){% endif -%}
{%- endmacro %}

{%- macro page_num_item(url, page_num, last=False) %}
    <li class="page-item">
        <a class="page-link" href="{{ url }}" {{ title_aria_label(page_num_label(page_num, last=last)) }}>
            {{- page_num|safe -}}
        </a>
    </li>
{%- endmacro %}

{%- macro page_active_item(page_num) %}
    <li class="page-item active">
        <span class="page-link" aria-current="page" {{ title_aria_label(page_num_label(page_num, active=True)) }}>
            {{- page_num|safe -}}
        </span>
    </li>
{%- endmacro %}

{%- macro page_ellipsis_item() %}
    <li class="page-item disabled" aria-hidden="true">
        <span class="page-link">...</span>
    </li>
{%- endmacro %}

{%- if pager %}
    <nav class="mb-4 d-print-none" aria-label="{{ _('Search results pagination') }}">
        <div class="d-none d-md-block">
            <ul class="pagination mb-0 flex-wrap">
                {%- if pager.previous %}
                    {{ page_prev_item(pager.previous[0].url) }}
                {%- endif %}

                {%- if pager.first and pager.first[0] != pager.current[0] and pager.first[0] not in pager.before %}
                    {{ page_num_item(pager.first[0].url, pager.first[0].page_num_formatted) }}
                    {%- if pager.first[0].page_num + 1 < pager.before[0].page_num %}
                        {{ page_ellipsis_item() }}
                    {%- endif %}
                {%- endif %}

                {%- for e in pager.before %}
                    {{ page_num_item(e.url, e.page_num_formatted) }}
                {%- endfor %}

                {{ page_active_item(pager.current[0].page_num_formatted) }}

                {%- for e in pager.after %}
                    {{ page_num_item(e.url, e.page_num_formatted, last=(e.page_num == pager.last.page_num)) }}
                {%- endfor %}

                {%- if pager.last and pager.last[0] != pager.current[0] and pager.last[0] not in pager.after %}
                    {%- if pager.last[0].page_num - 1 > pager.after[-1].page_num %}
                        {{ page_ellipsis_item() }}
                    {%- endif %}
                    {{ page_num_item(pager.last[0].url, pager.last[0].page_num_formatted, last=True) }}
                {%- endif %}

                {%- if pager.next %}
                    {{ page_next_item(pager.next[0].url) }}
                {%- endif %}
            </ul>
        </div>
        <div class="d-md-none">
            <ul class="pagination mb-0 flex-wrap">
                {%- if pager.previous %}
                    {{ page_prev_item(pager.previous[0].url) }}
                {%- endif %}

                <li class="page-item disabled">
                    <span class="page-link">{% trans current=pager.current[0].page_num_formatted|safe, total=page_count_formatted|safe %}Page {{ current }} of {{ total }}{% endtrans %}</span>
                </li>

                {%- if pager.next %}
                    {{ page_next_item(pager.next[0].url) }}
                {%- endif %}
            </ul>
        </div>
    </nav>
{%- endif %}
