{#
Construct a facet module populated with links to filtered results.

name
The field name identifying the facet field, eg. "tags"

title
The title of the facet, eg. "Tags", or "Tag Cloud"

label_function
Renders the human-readable label for each facet value.
If defined, this should be a callable that accepts a `facet_item`.
eg. lambda facet_item: facet_item.display_name.upper()
By default it displays the facet item's display name, which should
usually be good enough

if_empty
A string, which if defined, and the list of possible facet items is empty,
is displayed in lieu of an empty list.

count_label
A callable which accepts an integer, and returns a string.  This controls
how a facet-item's count is displayed.

extras
Extra info passed into the add/remove params to make the url

alternative_url
URL to use when building the necessary URLs, instead of the default
ones returned by url_for. Useful eg for dataset types.

hide_empty
Do not show facet if there are none, Default: false.

search_facets
Dictionary with search facets

#}
{% block facet_list %}
    {% set hide_empty = hide_empty or false %}
    {% with items = items or h.get_facet_items_dict(name, search_facets) %}
	{% if items or not hide_empty %}
	    {% block facet_list_item %}
		<section class="module module-narrow module-shallow">
		    {% block facet_list_heading %}
			<h2 class="module-heading">
			    <i class="fa fa-filter"></i>
			    {{ title }}
			</h2>
		    {% endblock %}
		    {% block facet_list_items %}
			{% with items = items or h.get_facet_items_dict(name, search_facets) %}
			    {% if items %}
				<nav aria-label="{{ _('Filter by: {title}').format(title=title) }}">
				    <ul class="list-unstyled nav nav-simple nav-facet">
					{% for item in items %}
					    {% set href = h.remove_url_param(name, item.name, extras=extras, alternative_url=alternative_url) if item.active else h.add_url_param(new_params={name: item.name}, extras=extras, alternative_url=alternative_url) %}
					    {% set label = label_function(item) if label_function else item.display_name %}
					    {% set label_truncated = label|truncate(22) if not label_function else label %}
					    {% set count = count_label(item['count']) if count_label else ('%d' % item['count']) %}
					    <li class="nav-item {% if item.active %} active{% endif %}">
						<a hx-boost="true" href="{{ href }}" aria-label="{{ _('Search datasets by {name}: {label}').format(name=name, label=label_truncated) }}" {% if label != label_truncated %} data-bs-title="{{ label }}" data-bs-toggle="tooltip" {% endif %}>
						    <span class="item-label">{{ label_truncated }}</span>
						    <span class="hidden separator"> - </span>
						    <span class="item-count badge">{{ count }}</span>

							{% if item.active %}<span class="facet-close"> <i class="fa fa-solid fa-circle-xmark"></i></span>{% endif %}
						</a>
					    </li>
					{% endfor %}
				    </ul>
				</nav>

				<p class="module-footer">
				    {% if h.get_param_int('_%s_limit' % name) %}
					{% if h.has_more_facets(name, search_facets) %}
					    <a hx-boost="true" href="{{ h.remove_url_param('_%s_limit' % name, replace=0, extras=extras, alternative_url=alternative_url) }}" class="read-more">{{ _('Show More {facet_type}').format(facet_type=title) }}</a>
					{% endif %}
				    {% else %}
					<a hx-boost="true" href="{{ h.remove_url_param('_%s_limit' % name, extras=extras, alternative_url=alternative_url) }}" class="read-more">{{ _('Show Only Popular {facet_type}').format(facet_type=title) }}</a>
				    {% endif %}
				</p>
			    {% else %}
				<p class="module-content empty">{{ _('There are no {facet_type} that match this search').format(facet_type=title) }}</p>
			    {% endif %}
			{% endwith %}
		    {% endblock %}
		</section>
	    {% endblock %}
	{% endif %}
    {% endwith %}
{% endblock %}
