var Wick = Wick || {};

Wick.ProductCounter = (function () {
    let _count = 0;

    function triggerChange() {
        $(document).trigger('productCounter:change', [_count]);
    }

    function setCount(val) {
        const next = Number(val) || 0;
        if (_count === next) return;

        _count = next;
        triggerChange();
    }

    function recalcFromFilters() {
        const checked = $('.filter-results input[type="checkbox"]:checked').length;

        if (checked === 0) setCount(0);
        else if (checked === 1) setCount(1);
        else setCount(1838);
    }

    function bind() {
        $(document).on('change', '.filter-results input[type="checkbox"]', recalcFromFilters);

        recalcFromFilters();
    }

    return {
        get count() {
            return _count;
        },
        set count(val) {
            setCount(val);
        },
        recalc: recalcFromFilters,
        bind: bind,
    };
})();

$(document).ready(function () {
    if (Wick.ProjectListMultiple && typeof Wick.ProjectListMultiple.bindAll === 'function') {
        Wick.ProjectListMultiple.bindAll();
    }

    Wick.ProductCounter.bind();
});
