{%- comment -%}

    Generates a subject cloud using js to process the terms for display on "cloud" layout.
    Requires CB's array_count_uniq.rb plugin!

{%- endcomment -%}
{% if site.data.theme.browse-child-objects == true %}
{%- assign items = site.data[site.metadata] | where_exp: 'item','item.objectid' -%}
{% else %}
{%- assign items = site.data[site.metadata] | where_exp: 'item','item.objectid and item.parentid == nil' -%}
{% endif %}
{%- assign termsMin = include.min | plus: 0 | default: 1 -%}
{%- assign cloud_id = include.id | default: "cloud" -%}
{%- assign cloud-fields = include.fields | split: ";" -%}

{%- comment -%} capture terms from all cloud fields {%- endcomment -%}
{%- assign terms = "" -%}
{%- for c in cloud-fields -%}
{% assign new = items | map: c | compact | join: ";" | split: ";" %}
{% capture field_terms %}{% for n in new %}{{n}}~{{c}};{% endfor %}{% endcapture %}
{% assign terms = terms | append: ";" | append: field_terms %}
{%- endfor -%}
{%- comment -%} find unique terms and counts {%- endcomment -%}
{%- assign uniqTerms = terms | downcase | split: ";" | array_count_uniq | sort | where_exp: 't','t[1] >= termsMin' -%}

<script>
(function(){    
    /* subject terms + count */
    var terms = [
        {% for t in uniqTerms %}{% assign term_field = t[0] | split: "~" %}[{{ term_field[0] | jsonify }}, {{ t[1] | jsonify }},{{term_field[1] | jsonify}}]{% unless forloop.last %}, {% endunless %}{% endfor %}
    ];

    {% if include.stopwords %}/* apply stopwords */
    var stopWords = {{ include.stopwords | downcase | split: ';' | jsonify }};
    terms = terms.filter(function(a) { return stopWords.indexOf(a[0]) < 0;});{% endif %}
    /* calculate max size */
    var counts = terms.map(function(obj){ return obj[1]; });
    var countMax = counts.reduce(function(a, b) { return Math.max(a, b); });
    var cloud = document.getElementById("{{ cloud_id }}");
    {% if include.shuffle == true %}
    /* Fisher-Yates shuffle https://bost.ocks.org/mike/shuffle/ */
    function shuffle(array) {
        var m = array.length, t, i;
        while (m) {
            i = Math.floor(Math.random() * m--);
            t = array[m];
            array[m] = array[i];
            array[i] = t;
        }
        return array;
    }
    {% endif %}

    function mapSize(x) { return Math.round(x * 9 / countMax + 1); }
    /* create cloud */
    function makeGrid(array) {
        var i, size;
        var items = "";
        {% if include.shuffle == true %}shuffle(array);{% endif %}
        for (let i = 0; i < array.length; i++) {
            size = mapSize(array[i][1]);
            items += '<a class="btn btn-{{ include.button | default: "outline-primary" }} m-2 tagcloud' + size + '" href="{{ "/browse/" | relative_url }}#' + array[i][2] + ':' + encodeURIComponent(array[i][0]) + '" >' + array[i][0] + '</a>';
        }
        cloud.innerHTML = items;
    }
    makeGrid(terms);

})();
</script>