{{ $page := . }}

{{ define "pagination" }}
    {{ $params := . }}

    {{ $page := .page }}
    {{ $pager := .pager }}

    {{ if or $pager.HasPrev $pager.HasNext }}
        <div id="paige-pagination">
            <div class="d-flex justify-content-center">
                <nav aria-label="{{ i18n `paige_page_list_navigation` }}">
                    <ul class="pagination">
                        {{ with $page.Paginator }}
                            <li class="page-item {{ if eq .PageNumber .First.PageNumber }} disabled {{ end }}">
                                <a aria-label="{{ i18n `paige_first` }}" class="page-link" href="{{ .First.URL }}"><span aria-hidden="true">&laquo;</span></a>
                            </li>

                            <li class="page-item {{ if not .Prev }} disabled {{ end }}">
                                <a {{ if .Prev }} aria-label="{{ i18n `paige_previous` }}" {{ end }} class="page-link" {{ with .Prev }} href="{{ .URL }}" {{ end }}>
                                    <span aria-hidden="true">&lsaquo;</span>
                                </a>
                            </li>

                            <li class="page-item {{ if not .Next }} disabled {{ end }}">
                                <a {{ if .Next }} aria-label="{{ i18n `paige_next` }}" {{ end }} class="page-link" {{ with .Next }} href="{{ .URL }}" {{ end }}>
                                    <span aria-hidden="true">&rsaquo;</span>
                                </a>
                            </li>

                            <li class="page-item {{ if eq .PageNumber .Last.PageNumber }} disabled {{ end }}">
                                <a aria-label="{{ i18n `paige_last` }}" class="page-link" href="{{ .Last.URL }}"><span aria-hidden="true">&raquo;</span></a>
                            </li>
                        {{ end }}
                    </ul>
                </nav>
            </div>
        </div>
    {{ end }}
{{ end }}

{{ $collections := slice }}
{{ $pages := $page.RegularPages | and ($page.Param "paige.list_page.disable_pages" | not) }}
{{ $sections := $page.Sections | and ($page.Param "paige.list_page.disable_sections" | not) }}

{{ if $page.IsHome }}
    {{ range $name, $taxonomy := site.Taxonomies }}
        {{ if $taxonomy.Page }}
            {{ $collections = $collections | append $taxonomy.Page }}
        {{ end }}
    {{ end }}
{{ end }}

{{ $collections = $collections | and ($page.Param "paige.list_page.disable_collections" | not) }}

{{ if and (not $pages) (in (slice "taxonomy" "term") $page.Kind) }}
    {{ $pages = $page.Pages }}
{{ end }}

{{ with $collections }}
    {{ $ordered := partial "paige/order.html" (dict "pages" .) }}

    <div id="paige-collections">
        {{ if or $sections $pages }}
            <h2 class="h5 text-center" id="paige-collections-header">{{ i18n "paige_collections" }}</h2>
        {{ end }}

        {{ if or $sections $pages }}
            {{ range $ordered }}
                {{ partial "paige/list-item.html" . }}
            {{ end }}
        {{ else }}
            {{ $pager := $page.Paginate $ordered }}

            {{ range $pager.Pages }}
                {{ partial "paige/list-item.html" . }}
            {{ end }}

            {{ template "pagination" (dict "page" $page "pager" $pager) }}
        {{ end }}
    </div>
{{ end }}

{{ with $sections }}
    {{ $ordered := partial "paige/order.html" (dict "pages" .) }}

    <div id="paige-sections">
        {{ if or $collections $pages }}
            <h2 class="h5 text-center" id="paige-sections-header">{{ i18n "paige_sections" }}</h2>
        {{ end }}

        {{ if $pages }}
            {{ range $ordered }}
                {{ partial "paige/list-item.html" . }}
            {{ end }}
        {{ else }}
            {{ $pager := $page.Paginate $ordered }}

            {{ range $pager.Pages }}
                {{ partial "paige/list-item.html" . }}
            {{ end }}

            {{ template "pagination" (dict "page" $page "pager" $pager) }}
        {{ end }}
    </div>
{{ end }}

{{ with $pages }}
    {{ $ordered := partial "paige/order.html" (dict "pages" .) }}

    <div id="paige-pages">
        {{ if or $collections $sections }}
            <h2 class="h5 text-center" id="paige-pages-header">{{ i18n "paige_pages" }}</h2>
        {{ end }}

        {{ $pager := $page.Paginate $ordered }}

        {{ range $pager.Pages }}
            {{ partial "paige/list-item.html" . }}
        {{ end }}

        {{ template "pagination" (dict "page" $page "pager" $pager) }}
    </div>
{{ end }}