{# Renders a horizontal pill bar with each active filter and a "remove" link. #}

{% set q = app.request.query %}
{% set baseAll = q.all|merge({page: null}) %}

{% macro pill(label, url) %}
    <a href="{{ url }}"
       class="inline-flex items-center gap-1.5 rounded-full bg-gray-900 py-1 pl-3 pr-1 text-sm font-medium text-white hover:bg-gray-800">
        {{ label }}
        <span class="grid h-5 w-5 place-items-center rounded-full bg-white/15 text-white hover:bg-white/25">
            <twig:ux:icon name="lucide:x" class="h-3 w-3" />
        </span>
    </a>
{% endmacro %}

{% macro removeUrl(remove) %}
    {%- set q = app.request.query.all -%}
    {%- set q = q|merge({page: null}) -%}
    {%- for k in remove|keys -%}{% set q = q|merge({(k): remove[k]}) %}{%- endfor -%}
    {{- path(app.current_route, (app.current_route_parameters|merge(q))|filter(v => v))|raw -}}
{% endmacro %}

{# Read multi-value params via `q.all[…]` instead of `q.get(…)` —
   InputBag::get() throws BadRequestException for non-scalar input
   (e.g. a stale `?kategorie[]=foo` URL). Anything non-scalar here is
   treated as "no filter". #}
{% set qSearch = q.get('titel', '') %}
{% set qCatsRaw = q.all['kategorie'] ?? '' %}
{% set qCats = (qCatsRaw is iterable or qCatsRaw is empty) ? [] : qCatsRaw|split(',')|filter(v => v != '') %}
{% set qWhen = q.get('wann', '') %}
{% set qVon = q.get('von', '') %}
{% set qBis = q.get('bis', '') %}
{% set qAgesRaw = q.all['alter'] ?? '' %}
{% set qAges = (qAgesRaw is iterable or qAgesRaw is empty) ? [] : qAgesRaw|split(',')|filter(v => v != '') %}
{% set qPreis = q.get('preis', '') %}
{% set qGebuehr = q.get('gebuehr', '') %}
{# wishlist filter: server doesn't know the wishlist's name (it lives in
   localStorage). JS injects the pill — we just track presence here so the
   bar shows up on first paint. #}
{% set qListeRaw = q.all['liste'] ?? '' %}
{% set qListe = (qListeRaw is iterable or qListeRaw is empty) ? [] : qListeRaw|split(',')|filter(v => v != '') %}

{% set whenLabels = {
    heute: 'filter.when.today',
    wochenende: 'filter.when.weekend',
    woche: 'filter.when.week',
    monat: 'filter.when.month',
    eigen: 'filter.when.dateRange',
} %}
{% set priceLabels = {
    '0': 'filter.price.free',
    '10': 'filter.price.upTo10',
    '25': 'filter.price.upTo25',
    custom: 'filter.price.custom',
} %}

{% set hasAny = qSearch or qCats|length or qWhen or qVon or qBis or qAges|length or qPreis or qGebuehr or q.get('sofortbuchung') or q.get('ohne_anmeldung') or q.get('rollstuhl') or q.get('kostenlos') or qListe|length %}

{% if hasAny %}
    <div class="flex flex-wrap items-center gap-2" data-controller="ferienpass--cms-active-filters">
        <span class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400 mr-1">{{ 'filter.active'|trans({}, 'cms') }}</span>

        {% if qSearch %}
            {{ _self.pill('„' ~ qSearch ~ '“', _self.removeUrl({titel: null})) }}
        {% endif %}

        {% if categories is defined %}
            {% for c in qCats %}
                {% set cat = categories|filter(x => x.alias == c)|first|default(null) %}
                {% if cat %}
                    {% set newCats = qCats|filter(v => v != c)|sort %}
                    {{ _self.pill(cat.name, _self.removeUrl({kategorie: newCats|length ? newCats|join(',') : null})) }}
                {% endif %}
            {% endfor %}
        {% endif %}

        {% if qWhen and whenLabels[qWhen] is defined %}
            {{ _self.pill(whenLabels[qWhen]|trans({}, 'cms'), _self.removeUrl({wann: null, von: null, bis: null})) }}
        {% elseif qVon or qBis %}
            {{ _self.pill(whenLabels['eigen']|trans({}, 'cms'), _self.removeUrl({wann: null, von: null, bis: null})) }}
        {% endif %}

        {% set bandValues = ['3-6', '6-12', '12-16', '16+'] %}
        {% for a in qAges %}
            {% set newAges = qAges|filter(v => v != a)|sort %}
            {% if a in bandValues %}
                {% set lbl = ('filter.age.band.' ~ a|replace({'+': 'plus'}))|trans({}, 'cms') %}
            {% else %}
                {% set lbl = 'filter.age.years'|trans({value: a}, 'cms') %}
            {% endif %}
            {{ _self.pill(lbl, _self.removeUrl({alter: newAges|length ? newAges|join(',') : null})) }}
        {% endfor %}

        {% if qPreis and priceLabels[qPreis] is defined %}
            {{ _self.pill(priceLabels[qPreis]|trans({}, 'cms'), _self.removeUrl({preis: null, gebuehr: null, kostenlos: null})) }}
        {% elseif qGebuehr %}
            {{ _self.pill('filter.price.custom'|trans({}, 'cms'), _self.removeUrl({preis: null, gebuehr: null, kostenlos: null})) }}
        {% elseif q.get('kostenlos') %}
            {{ _self.pill('filter.price.free'|trans({}, 'cms'), _self.removeUrl({preis: null, gebuehr: null, kostenlos: null})) }}
        {% endif %}

        {% if q.get('sofortbuchung') %}
            {{ _self.pill('filter.chip.instant'|trans({}, 'cms'), _self.removeUrl({sofortbuchung: null})) }}
        {% endif %}
        {% if q.get('ohne_anmeldung') %}
            {{ _self.pill('filter.chip.noApplication'|trans({}, 'cms'), _self.removeUrl({ohne_anmeldung: null})) }}
        {% endif %}
        {% if q.get('rollstuhl') %}
            {{ _self.pill('filter.chip.wheelchair'|trans({}, 'cms'), _self.removeUrl({rollstuhl: null})) }}
        {% endif %}
        {# Wishlist pill is injected by the wishlists Stimulus controller
           because only the client knows the wishlist's name. #}
    </div>
{% endif %}
