{%- from "kerko/_attributes.html.jinja2" import title_aria_label %}

{%- macro facet_item(
            missing_label,
            item,
            extra_markup,
            add_link_icon='<span class="far fa-square" aria-hidden="true"></span>',
            remove_link_icon='<span class="far fa-check-square" aria-hidden="true"></span>',
            item_tag='li',
            add_link_title=_("Add the '{}' filter"),
            remove_link_title=_("Remove the '{}' filter")
        )
    -%}
    {%- if item.label or missing_label -%}
        <{{ item_tag }}>
            {%- set label = item.label|escape or (('<em>' + missing_label + '</em>')|safe) -%}
            {%- if item.remove_url -%}
                <a class="no-decorate" href="{{ item.remove_url }}" {{ title_aria_label(remove_link_title.format(label|striptags|escape)) }} rel="nofollow">
                    {{ remove_link_icon }}
                    <span class="facet-item-label parent-decorate">{{ label }}</span>
                </a>
            {%- elif item.add_url -%}
                <a class="no-decorate" href="{{ item.add_url }}" {{ title_aria_label(add_link_title.format(label|striptags|escape)) }} rel="nofollow">
                    {{ add_link_icon }}
                    <span class="facet-item-label parent-decorate">{{ label }}</span>
                </a>
            {%- else -%}
                {{- label }}
            {%- endif -%}
            {%- if not item.remove_url and item.count > 0 %}<span class="facet-item-count"> ({{ item.count_formatted|safe }})</span>{%- endif -%}
            {%- if extra_markup %}
                {{ extra_markup|safe }}
            {%- endif %}
        </{{ item_tag }}>
    {%- endif -%}
{%- endmacro %}

{%- macro facet_items(
            missing_label,
            items,
            top_level=True,
            expand=False,
            list_tag='ul', list_class_top='list-unstyled',
            item_tag='li'
        )
    -%}
    {%- if items %}
        <{{ list_tag }} class="{% if top_level %}{% if list_class_top %}{{ list_class_top }}{% endif %}{% else %}list-no-bullets{% endif %}">
            {%- for item in items %}
                {%- if (item.remove_url or expand) and item.children %}
                    {%- set subtree = facet_items(missing_label, item.children, False, expand, list_tag, item_tag, **kwargs) %}
                {%- else %}
                    {%- set subtree = '' %}
                {%- endif %}
                {{- facet_item(missing_label, item, subtree, item_tag=item_tag, **kwargs) }}
            {%- endfor %}
        </{{ list_tag }}>
    {%- endif %}
{%- endmacro %}

{%- macro facet(
            title,
            missing_label,
            items,
            collapsible=False,
            collapse_id=None,
            collapsed=True,
            wrap_tag='div', wrap_class='',
            heading_tag='h3', heading_class='h6 font-weight-bold',
            body_tag='div', body_class=''
        )
    -%}
    {#- TODO: Kill this clumsy macro; just put the macro expansions in place of its calls. #}
    {%- if items %}
        <{{ wrap_tag }} {% if wrap_class %}class="{{ wrap_class }}"{% endif %}>
            <{{ heading_tag }} {% if heading_class %}class="{{ heading_class }}"{% endif %}>
                {{ title }}
                {%- if collapsible %}
                    <a class="collapse-toggle {% if collapsed %}collapsed{% endif %}" href="#{{ collapse_id }}" data-toggle="collapse" role="button" aria-expanded="{{ (not collapsed)|lower }}" aria-controls="{{ collapse_id }}" {{ title_aria_label(_('Toggle filters display')) }}>
                        <span class="collapse-icon fas" aria-hidden="true"></span>
                    </a>
                {%- endif %}
            </{{ heading_tag }}>
            <{{ body_tag }} {% if collapsible %}id="{{ collapse_id }}"{% endif %} {% if body_class or collapsible %}class="{% if body_class %}{{ body_class }}{% endif %} {% if collapsible %}{% if collapsed %}collapse{% else %}collapse show{% endif %}{% endif %}"{% endif %}>
                {{- facet_items(missing_label, items, **kwargs) }}
            </{{ body_tag }}>
        </{{ wrap_tag }}>
    {%- endif %}
{%- endmacro %}
