---
# facets generator
# creates a csv of unique values and their counts of fields given in site.data.theme.metadata-facets-fields
#
# Requires CB's array_count_uniq.rb plugin!
---
{%- assign items = site.data[site.metadata] | where_exp: 'item','item.objectid' -%}
{%- assign fields = site.data.theme.metadata-facets-fields | split: "," -%}
{
    "facets": [ 
        {% for f in fields %}{%- assign uniqueArray = items | map: f | join: ";" | downcase | split: ";" | array_count_uniq -%} 
        {
            "field": {{ f | jsonify }},
            "terms": { {% for unique in uniqueArray %}
                {{ unique[0] | jsonify }}: {{ unique[1] | jsonify }}{% unless forloop.last %},{% endunless %}{% endfor %}
            }
        }{% unless forloop.last %},{% endunless %}
        {% endfor %}
    ]
}
