<?php
/**
 * Copyright © Codazon, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
use \Magento\Catalog\Model\Product\ProductList\Toolbar as ToolbarModel;
?>
<?php
/**
 * Product list toolbar
 *
 * @var $block \Magento\Catalog\Block\Product\ProductList\Toolbar
 */
?>
<?php
    $ampHelper = $this->helper(Codazon\GoogleAmpManager\Helper\Data::class);
    $orderParamName = ToolbarModel::ORDER_PARAM_NAME;
?>
<div class="cdz-toolbar-sorter">
    <select id="sorter" data-role="sorter" class="sorter-options" on="change: AMP.navigateTo(url=event.value)">
        <?php foreach ($block->getAvailableOrders() as $_key => $_order) :?><?php
            $params = [$orderParamName => $_key];
            $url = $ampHelper->transformToAmpUrl($block->getPagerUrl($params));
        ?>
            <option value="<?= $url ?>" <?php if ($block->isOrderCurrent($_key)) :?>selected="selected"<?php endif; ?>><?= $block->escapeHtml(__($_order)) ?></option>
        <?php endforeach; ?>
    </select>
    <?php if ($block->getCurrentDirection() == 'desc') :?><?php
        $params = [ToolbarModel::DIRECTION_PARAM_NAME => 'asc'];
        $url = $ampHelper->transformToAmpUrl($block->getPagerUrl($params));
    ?>
        <a title="<?= $block->escapeHtmlAttr(__('Set Ascending Direction')) ?>"
           href="<?= $url ?>"
           class="action sorter-action sort-desc"
           data-role="direction-switcher"
           data-value="asc">
            <span><?= $block->escapeHtml(__('Set Ascending Direction')) ?></span>
        </a>
    <?php else :?><?php
        $params = [ToolbarModel::DIRECTION_PARAM_NAME => 'desc'];
        $url = $ampHelper->transformToAmpUrl($block->getPagerUrl($params));
    ?>
        <a title="<?= $block->escapeHtmlAttr(__('Set Descending Direction')) ?>"
           href="<?= $url ?>"
           class="action sorter-action sort-asc"
           data-role="direction-switcher"
           data-value="desc">
            <span><?= $block->escapeHtml(__('Set Descending Direction')) ?></span>
        </a>
    <?php endif; ?>
</div>
