"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var vdom_1 = require("@dojo/framework/core/vdom");
var icache_1 = require("@dojo/framework/core/middleware/icache");
var icache = icache_1.createICacheMiddleware();
var factory = vdom_1.create({ icache: icache });
exports.checkboxGroup = factory(function (_a) {
    var icache = _a.middleware.icache;
    return function (onValue, initialValue, value) {
        if (initialValue === void 0) { initialValue = []; }
        if (value === undefined) {
            var existingInitialValue = icache.get('initial');
            if (JSON.stringify(existingInitialValue) !== JSON.stringify(initialValue)) {
                icache.set('values', initialValue.reduce(function (existing, value) {
                    existing[value] = true;
                    return existing;
                }, {}));
                icache.set('initial', initialValue);
            }
        }
        else {
            icache.set('values', value.reduce(function (existing, value) {
                existing[value] = true;
                return existing;
            }, {}));
        }
        function getAllValues() {
            var currentValues = icache.get('values') || {};
            var keys = Object.keys(currentValues);
            return keys.reduce(function (values, key) {
                if (currentValues[key]) {
                    values.push(key);
                }
                return values;
            }, []);
        }
        return function (key) {
            function checked(checked) {
                var _a;
                var values = icache.getOrSet('values', {});
                if (checked === undefined) {
                    return !!values[key];
                }
                else if (values[key] !== checked) {
                    icache.set('values', tslib_1.__assign({}, values, (_a = {}, _a[key] = checked, _a)));
                    onValue(getAllValues());
                }
            }
            return {
                checked: checked
            };
        };
    };
});

/*# sourceMappingURL=middleware.js.map*/