<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Marketplace
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
?>
<?php
$helper = $this->helper(\Webkul\Marketplace\Helper\Data::class);
$defaultCategoryId = $helper->getRootCategoryIdByStoreId();
?>
<?php $_filters = $block->getActiveFilters() ?>
<?php if (!empty($_filters)): ?>
    <div class="filter-current" data-mage-init='{"collapsible":{"openedState": "active", "collapsible": true, "active": false }}'>
        <strong class="block-subtitle filter-current-subtitle"
            role="heading"
            aria-level="2"
            data-role="title"
            data-count="<?= /* @noEscape */ count($_filters) ?>"><?= /* @noEscape */ __('Now Shopping by') ?></strong>
        <ol class="items">
            <?php foreach ($_filters as $_filter): ?>
                <li class="item">
                    <span class="filter-label"><?= $block->escapeHtml(__($_filter->getName())) ?></span>
                    <span class="filter-value"><?= /* @noEscape */ $block->stripTags($_filter->getLabel()) ?></span>
                    <?php
                    $clearLinkUrl = $_filter->getClearLinkUrl();
                    $currentFilterName = $block->escapeHtml(__($_filter->getName())) . " " . $block->stripTags($_filter->getLabel());
                    if ($clearLinkUrl):
                        ?>
                        <a class="action previous" href="<?= /* @noEscape */ $_filter->getRemoveUrl() ?>"
                        title="<?= /* @noEscape */ __('Previous') ?>">
                            <span><?= /* @noEscape */ __('Previous') ?></span>
                        </a>
                        <a class="action remove"
                        title="<?= $block->escapeHtml($_filter->getFilter()->getClearLinkText()) ?>"
                        href="<?= /* @noEscape */ $clearLinkUrl ?>">
                            <span><?= $block->escapeHtml($_filter->getFilter()->getClearLinkText()) ?></span>
                        </a>
                    <?php else: ?>
                        <?php
                        // update remove filter url for default category
                        $removeUrl = $_filter->getRemoveUrl();
                        $parsedUrl = parse_url($_filter->getRemoveUrl());
                        if (isset($parsedUrl['query'])) {
                            $parsedQuery = $parsedUrl['query'];
                            parse_str($parsedQuery, $query);
                            if (isset($query['cat']) && $query['cat'] == $defaultCategoryId) {
                                if (count($query)>1) {
                                    $parsedQuery = str_replace('cat='.$defaultCategoryId.'&', '', $parsedQuery);
                                    $parsedQuery = str_replace('amp;', '', $parsedQuery);
                                    $query = !empty($query) ? '?'. $parsedQuery : '';
                                } else {
                                    $query = '';
                                }
                                $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
                                $removeUrl = $parsedUrl['scheme']. '://'. $parsedUrl['host']. $path. $query;
                            }
                        }
                        ?>
                        <a class="action remove" href="<?= /* @noEscape */ $removeUrl ?>"
                        title="<?= /* @noEscape */ $block->escapeHtml(__('Remove')) . " " . $currentFilterName ?>">
                            <span><?= /* @noEscape */ __('Remove This Item') ?></span>
                        </a>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ol>
    </div>
<?php endif; ?>
