{% set queryParameters = app.request.query.all %}
{% set baseParameters = app.request.attributes.get('_route_params')|merge(queryParameters) %}

{%- set appliedFilters -%}
    {%- for filter in result.filters -%}
        {% if filter.type != "range" %}
            {%- for value in filter.values -%}
                {% if value.isApplied %}
                    {% if filter.type is not empty %}
                        {%- set parameters = baseParameters|unset_nested_value([filter.type, filter.code], value.slug) -%}
                    {% else %}
                        {%- set parameters = baseParameters|unset_nested_value([filter.code], value.slug) -%}
                    {% endif %}

                    <a href="{{ path(app.request.attributes.get('_route'), parameters, true) }}"
                       class="filter-selected">
                        {% include '@SyliusShop/Icons/cross.html.twig' %}
                        {{ value.label|replace({ '+': ' ' }) }}
                    </a>
                {% endif %}
            {%- endfor -%}
        {% else %}
            {%- for value in filter.values -%}
                {% if value.isApplied %}
                    {% set valueType = filter.valueType(value.label) %}
                    {%- set parameters = baseParameters|unset_nested_value([filter.code], value.value) -%}
                    <a href="{{ path(app.request.attributes.get('_route'), parameters, true) }}"
                       class="filter-selected">
                        {% include '@SyliusShop/Icons/cross.html.twig' %}
                        {{ value.label | trans }} {{ value.value|replace({ '+': ' ' }) }} {{ currencySymbol }}
                    </a>
                {% endif %}
            {%- endfor -%}
        {% endif %}
    {%- endfor -%}
{%- endset -%}

{% if appliedFilters != '' %}
    <li class="filter-selected-wrapper">
        {{ appliedFilters }}

        {% set reinitParameters = app.request.attributes.get('_route_params') %}
        {% if queryParameters.limit is defined %}
            {% set reinitParameters = reinitParameters|merge({'limit': queryParameters.limit}) %}
        {% endif %}
        {% if queryParameters.sorting is defined %}
            {% set reinitParameters = reinitParameters|merge({'sorting': queryParameters.sorting}) %}
        {% endif %}

        <a href="{{ path(app.request.attributes.get('_route'), reinitParameters) }}"
           class="link-secondary">{{ 'monsieurbiz_searchplugin.filters.reinit_btn'|trans }}</a>
    </li>
{% endif %}
