(function () {
    "use strict";
    function init(view) {
        var events = Array.prototype.slice.call(view.querySelectorAll("[data-h4bs-calendar-event]"));
        var search = view.querySelector("[data-h4bs-calendar-search]");
        var filter = view.querySelector("[data-h4bs-calendar-filter]");
        var empty = view.querySelector("[data-h4bs-calendar-empty]");
        var status = view.querySelector("[data-h4bs-calendar-status]");
        function update() {
            var query = search ? String(search.value || "").trim().toLowerCase() : "";
            var category = filter ? String(filter.value || "").toLowerCase() : "";
            var count = 0;
            events.forEach(function (event) {
                var shown = (query === "" || String(event.dataset.search || "").indexOf(query) >= 0) && (category === "" || String(event.dataset.category || "") === category);
                event.hidden = !shown;
                if (shown) count += 1;
            });
            if (empty) empty.hidden = count !== 0;
            if (status) status.textContent = count + (count === 1 ? " event shown" : " events shown");
        }
        if (search) search.addEventListener("input", update);
        if (filter) filter.addEventListener("change", update);
        update();
    }
    function boot() { document.querySelectorAll("[data-h4bs-calendar-view]").forEach(init); }
    if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot, { once: true }); else boot();
})();
