<?php
$toolbarHelper = $this->helper('Wyomind\ElasticsearchLayeredNavigation\Helper\Toolbar');
$configHelper = $this->helper('Wyomind\ElasticsearchLayeredNavigation\Helper\Config');
$labels = $configHelper->getLabels();
$search = $block->getType() == "search";
$currentDirection = $toolbarHelper->getCurrentDirection($block->getType());
$currentOrder = $toolbarHelper->getCurrentOrder($block->getType());
$_orders = $toolbarHelper->getAvailableOrders();
?>
<div class="toolbar-sorter sorter">
    <label class="sorter-label" for="sorter"><?= $block->escapeHtml($labels['sort_by']) ?></label>
    <select id="sorter" class="sorter-options">
        <?php foreach ($_orders as $_key => $_order): ?>
            <?php
            if ($search && $_key == "position") :
                $_key = "score";
                $_order = "Relevance";
            endif;
            ?>
            <option value="<?= $block->escapeHtml($_key) ?>"
                    <?php if ($_key == $currentOrder): ?>selected="selected"<?php endif; ?>><?= $block->escapeHtml(__($_order)) ?></option>
        <?php endforeach; ?>
        <?php if ($configHelper->isRatingSortEnabled($block->getType())): ?>
            <option value="rating"
                    <?php if ("rating" == $currentOrder): ?>selected="selected"<?php endif; ?>><?= $block->escapeHtml(__("Rating")) ?></option>
        <?php endif; ?>
    </select>
    <a title="<?= $block->escapeHtml(__('Set Ascending Direction')) ?>" href="#"
       class="action sorter-action sort-desc <?= ($currentDirection == 'desc') ? 'active' : ''; ?>">
        <span><?= $block->escapeHtml(__('Set Ascending Direction')) ?></span>
    </a>
    <a title="<?= $block->escapeHtml(__('Set Descending Direction')) ?>" href="#"
       class="action sorter-action sort-asc <?= ($currentDirection == 'asc') ? 'active' : ''; ?>">
        <span><?= $block->escapeHtml(__('Set Descending Direction')) ?></span>
    </a>
</div>
