<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Magento\Framework\Escaper;
use Magento\Theme\Block\Html\Pager;

/** @var Pager $block */
/** @var Escaper $escaper */

/** @var \FoundationCommerce\ThemeViewModels\ViewModels\ThemeIcons $icons */
$icons = $viewModels->require(\FoundationCommerce\ThemeViewModels\ViewModels\ThemeIcons::class);

?>
<?php if ($block->getCollection()->getSize()): ?>

    <?php if ($block->getUseContainer()): ?>
        <div class="grid items-center grid-flow-row grid-cols-4 gap-2 pager
            sm:grid-cols-8 md:grid-cols-4 lg:grid-cols-8">
    <?php endif ?>

    <?php if ($block->getShowAmounts()): ?>
        <p class="flex order-3 col-span-2 text-sm leading-5 toolbar-amount sm:order-2 md:order-3 lg:order-2 gap-x-1">
            <span class="toolbar-number">
            <?php if ($block->getLastPageNum() > 1): ?>
                <?= $escaper->escapeHtml(
                    __('Items %1 to %2 of %3 total', $block->getFirstNum(), $block->getLastNum(), $block->getTotalNum())
                ) ?>
            <?php elseif ($block->getTotalNum() == 1): ?>
                <?= $escaper->escapeHtml(__('%1 Item', $block->getTotalNum())) ?>
            <?php else: ?>
                <?= $escaper->escapeHtml(__('%1 Item(s)', $block->getTotalNum())) ?>
            <?php endif; ?>
            </span>
        </p>
    <?php endif ?>

    <div class="flex justify-center order-2 col-span-4 pages text-black">
        <?php if ($block->getLastPageNum() > 1): ?>
            <strong class="sr-only label pages-label"><?= $escaper->escapeHtml(__('Page')) ?></strong>
            <ul class="relative z-0 inline-flex items pages-items"
                aria-label="<?= $escaper->escapeHtml(__('Page')) ?>"
            >
                <?php if (!$block->isFirstPage()): ?>
                    <li class="relative inline-flex items-center px-3 py-2 text-sm font-medium leading-5 text-gray-500
                    transition duration-150 ease-in-out bg-white item pages-item-previous
                    rounded-l-md hover:text-gray-400 focus:z-10 focus:outline-none
                    focus:shadow-outline-blue active:bg-gray-100 active:text-gray-500">
                        <?php $text = $block->getAnchorTextForPrevious() ? $block->getAnchorTextForPrevious() : ''; ?>
                        <a class="<?= $escaper->escapeHtmlAttr($text ? 'link ' : 'action ') ?> previous hover:text-gray-700 text-black"
                           href="<?= $escaper->escapeUrl($block->getPreviousPageUrl()) ?>"
                           title="<?= $escaper->escapeHtmlAttr($text ? $text : __('Previous')) ?>">
                            <span class="sr-only label"><?= $escaper->escapeHtml(__('Page')) ?></span>
                            <span>
                            <?php if ($text): ?>
                                <?= $escaper->escapeHtml($text) ?>
                            <?php else: ?>
                                <?= $icons->prevHtml(" ", 20, 20) ?>
                            <?php endif; ?>
                        </span>
                        </a>
                    </li>
                <?php endif; ?>

                <?php if ($block->canShowFirst()): ?>
                    <li class="relative inline-flex items-center px-3 py-2 -ml-px text-sm font-medium leading-5 transition duration-150 ease-in-out bg-white item
                    hover:text-gray-500 focus:z-10 focus:outline-none focus:shadow-outline-blue
                    active:bg-gray-100 active:text-gray-700">
                        <a class="page first" href="<?= $escaper->escapeUrl($block->getFirstPageUrl()) ?>">
                            <span class="sr-only label"><?= $escaper->escapeHtml(__('Page')) ?></span>
                            <span>1</span>
                        </a>
                    </li>
                <?php endif; ?>

                <?php if ($block->canShowPreviousJump()): ?>
                    <li class="relative inline-flex items-center -ml-px text-sm font-medium leading-5 bg-white item">
                        <a class="px-3 py-2 page previous jump"
                           title=""
                           href="<?= $escaper->escapeUrl($block->getPreviousJumpUrl()) ?>">
                            <span>...</span>
                        </a>
                    </li>
                <?php endif; ?>

                <?php foreach ($block->getFramePages() as $page): ?>
                    <?php if ($block->isPageCurrent($page)): ?>
                        <li class="relative z-10 inline-flex items-center -ml-px text-sm font-medium leading-5 transition duration-150 ease-in-out bg-white item current focus:outline-none">
                            <strong class="px-3 py-2 cursor-default page">
                                <span class="sr-only label">
                                    <?= $escaper->escapeHtml(
                                        __('You\'re currently reading page')
                                    ) ?>
                                </span>
                                <span><?= $escaper->escapeHtml($page) ?></span>
                            </strong>
                        </li>
                    <?php else: ?>
                        <li class="relative inline-flex items-center -ml-px text-sm font-medium leading-5 transition duration-150 ease-in-out bg-white item hover:text-gray-500
                            focus:z-10 focus:outline-none focus:shadow-outline-blue
                            active:bg-gray-100 active:text-gray-700">
                            <a href="<?= $escaper->escapeUrl($block->getPageUrl($page)) ?>" class="px-3 py-2 page">
                                <span class="sr-only label"><?= $escaper->escapeHtml(__('Page')) ?></span>
                                <span><?= $escaper->escapeHtml($page) ?></span>
                            </a>
                        </li>
                    <?php endif; ?>
                <?php endforeach; ?>

                <?php if ($block->canShowNextJump()): ?>
                    <li class="item">
                        <a class="page next jump" title="" href="<?= $escaper->escapeUrl($block->getNextJumpUrl()) ?>">
                            <span>...</span>
                        </a>
                    </li>
                <?php endif; ?>

                <?php if ($block->canShowLast()): ?>
                    <li class="relative inline-flex items-center -ml-px text-sm font-medium leading-5 bg-white item">
                        <a class="px-3 py-2 page last" href="<?= $escaper->escapeUrl($block->getLastPageUrl()) ?>">
                            <span class="sr-only label"><?= $escaper->escapeHtml(__('Page')) ?></span>
                            <span><?= $escaper->escapeHtml($block->getLastPageNum()) ?></span>
                        </a>
                    </li>
                <?php endif; ?>

                <?php if (!$block->isLastPage()): ?>
                    <li class="relative inline-flex items-center px-3 py-2 -ml-px text-sm font-medium
                        leading-5 text-gray-500 transition duration-150 ease-in-out bg-white item pages-item-next rounded-r-md hover:text-gray-400 focus:z-10 focus:outline-none focus:shadow-outline-blue active:bg-gray-100 active:text-gray-500">
                        <?php $text = $block->getAnchorTextForNext() ? $block->getAnchorTextForNext() : ''; ?>
                        <a class="<?= /* @noEscape */ $text ? 'link ' : 'action ' ?> next hover:text-gray-700 text-black"
                           href="<?= $escaper->escapeUrl($block->getNextPageUrl()) ?>"
                           title="<?= $escaper->escapeHtmlAttr($text ? $text : __('Next')) ?>">
                            <span class="sr-only label"><?= $escaper->escapeHtml(__('Page')) ?></span>
                            <span>
                            <?php if ($text): ?>
                                <?= $escaper->escapeHtml($text) ?>
                            <?php else: ?>
                                <?= $icons->nextHtml(" ", 20, 20) ?>
                            <?php endif; ?>
                        </span>
                        </a>
                    </li>
                <?php endif; ?>
            </ul>

        <?php endif; ?>
    </div>

    <?php if ($block->isShowPerPage()): ?>
        <div class="flex items-center justify-end order-3 col-span-2 limiter sm:order-2 md:order-3 lg:order-2">
            <label class="mr-2 text-sm label" for="limiter">
                <span><?= $escaper->escapeHtml(__('Show')) ?></span>
            </label>
            <select id="limiter" class="mr-2 form-select" onchange="location.href = this.value">
                <?php foreach ($block->getAvailableLimit() as $key => $limit): ?>
                    <option value="<?= $escaper->escapeUrl($block->getLimitUrl($key)) ?>"
                        <?php if ($block->isLimitCurrent($key)): ?>
                            selected="selected"<?php endif ?>>
                        <?= $escaper->escapeHtml($limit) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <span class="sr-only limiter-text"><?= $escaper->escapeHtml(__('per page')) ?></span>
        </div>
    <?php endif ?>

    <?php if ($block->getUseContainer()): ?>
        </div>
    <?php endif ?>

<?php endif ?>
