"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
var BringgDashboardSDK_1 = require("./BringgDashboardSDK");
var DataStore = /** @class */ (function () {
    function DataStore(groupFilters, options) {
        var e_1, _a;
        if (groupFilters === void 0) { groupFilters = []; }
        if (options === void 0) { options = {}; }
        this.itemsByGroups = new Map();
        this.allItems = {};
        if (!options.shallow) {
            (0, BringgDashboardSDK_1.getGlobals)().mobx.makeAutoObservable(this);
        }
        this.groupFilters = groupFilters || [];
        this.initializedGroups = new Set();
        try {
            for (var _b = __values(this.groupFilters), _c = _b.next(); !_c.done; _c = _b.next()) {
                var groupFilter = _c.value;
                this.itemsByGroups.set(groupFilter.groupType, new Map());
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    DataStore.prototype.isGroupInitialized = function (groupType, groupValue) {
        return this.isAllInitialized || this.initializedGroups.has(this.getGroupKey(groupType, groupValue));
    };
    DataStore.prototype.get = function (id) {
        return this.allItems[id];
    };
    DataStore.prototype.isInGroup = function (id, groupType, groupValue) {
        var _a;
        var itemsInGroup = (_a = this.itemsByGroups.get(groupType)) === null || _a === void 0 ? void 0 : _a.get(groupValue);
        if (!itemsInGroup) {
            return false;
        }
        return Boolean(itemsInGroup[id]);
    };
    DataStore.prototype.getMany = function (ids) {
        var all = this.getAllById();
        return ids.reduce(function (acc, id) {
            acc[id] = all[id];
            return acc;
        }, {});
    };
    DataStore.prototype.getAll = function () {
        return Object.values(this.allItems);
    };
    DataStore.prototype.getAllById = function () {
        return this.allItems;
    };
    DataStore.prototype.getGroup = function (group, groupValue) {
        return Object.values(this.getGroupItems(group, groupValue));
    };
    DataStore.prototype.set = function (item) {
        var previousItem = this.get(item.id);
        this.allItems[item.id] = item;
        this.updateItemInGroups(item, previousItem);
    };
    DataStore.prototype.remove = function (id) {
        var item = this.get(id);
        delete this.allItems[id];
        this.updateItemInGroups(undefined, item);
    };
    DataStore.prototype.setMany = function (items) {
        var e_2, _a;
        try {
            for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
                var item = items_1_1.value;
                this.allItems[item.current.id] = item.current;
                this.updateItemInGroups(item.current, item.diff);
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
    };
    DataStore.prototype.setAll = function (items) {
        this.allItems = {};
        this.setAllItems(items);
        this.isAllInitialized = true;
    };
    DataStore.prototype.setGroup = function (groupType, groupValue, items) {
        var _this = this;
        var previousItems = this.getGroup(groupType, groupValue);
        this.removeMany(previousItems);
        if (Array.isArray(groupValue)) {
            groupValue.forEach(function (group) {
                _this.itemsByGroups.get(groupType).set(group, {});
            });
        }
        else {
            this.itemsByGroups.get(groupType).set(groupValue, {});
        }
        this.setAllItems(items);
        this.setGroupInitialized(groupType, groupValue);
    };
    DataStore.prototype.setGroupInitialized = function (groupType, groupValue) {
        var _this = this;
        if (Array.isArray(groupValue)) {
            groupValue.forEach(function (group) {
                _this.initializedGroups.add(_this.getGroupKey(groupType, group));
            });
        }
        else {
            this.initializedGroups.add(this.getGroupKey(groupType, groupValue));
        }
    };
    DataStore.prototype.setAllItems = function (items) {
        var e_3, _a;
        try {
            for (var items_2 = __values(items), items_2_1 = items_2.next(); !items_2_1.done; items_2_1 = items_2.next()) {
                var item = items_2_1.value;
                this.allItems[item.current.id] = item.current;
                this.updateItemInGroups(item.current, item.diff);
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (items_2_1 && !items_2_1.done && (_a = items_2.return)) _a.call(items_2);
            }
            finally { if (e_3) throw e_3.error; }
        }
    };
    DataStore.prototype.removeMany = function (items) {
        var e_4, _a;
        var allItemsLength = Object.keys(this.allItems).length;
        if (items.length === allItemsLength) {
            this.allItems = {};
            return;
        }
        try {
            for (var items_3 = __values(items), items_3_1 = items_3.next(); !items_3_1.done; items_3_1 = items_3.next()) {
                var item = items_3_1.value;
                delete this.allItems[item.id];
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (items_3_1 && !items_3_1.done && (_a = items_3.return)) _a.call(items_3);
            }
            finally { if (e_4) throw e_4.error; }
        }
    };
    DataStore.prototype.removeAll = function () {
        var e_5, _a;
        this.removeMany(this.getAll());
        try {
            for (var _b = __values(this.groupFilters), _c = _b.next(); !_c.done; _c = _b.next()) {
                var groupFilter = _c.value;
                this.itemsByGroups.get(groupFilter.groupType).clear();
            }
        }
        catch (e_5_1) { e_5 = { error: e_5_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_5) throw e_5.error; }
        }
        this.initializedGroups.clear();
    };
    DataStore.prototype.updateItemInGroups = function (item, diff) {
        var e_6, _a;
        var _this = this;
        var _loop_1 = function (groupFilter) {
            var groupValue = item && groupFilter.keyFunc(item);
            groupValue = Array.isArray(groupValue) ? groupValue : [groupValue];
            if (item) {
                var groupItems_1 = this_1.itemsByGroups.get(groupFilter.groupType);
                groupValue.forEach(function (group) {
                    if (!groupItems_1.get(group)) {
                        groupItems_1.set(group, {});
                    }
                    groupItems_1.get(group)[item.id] = _this.allItems[item.id];
                });
            }
            if (diff) {
                var previousGroupValue = groupFilter.keyFunc(__assign(__assign({}, item), diff));
                previousGroupValue = Array.isArray(previousGroupValue) ? previousGroupValue : [previousGroupValue];
                previousGroupValue
                    .filter(function (diffItem) { return groupValue.indexOf(diffItem) === -1; })
                    .forEach(function (diffItem) {
                    var _a;
                    var previousGroupItems = _this.itemsByGroups.get(groupFilter.groupType);
                    (_a = previousGroupItems.get(diffItem)) === null || _a === void 0 ? true : delete _a[(item === null || item === void 0 ? void 0 : item.id) || diff.id];
                });
            }
        };
        var this_1 = this;
        try {
            for (var _b = __values(this.groupFilters), _c = _b.next(); !_c.done; _c = _b.next()) {
                var groupFilter = _c.value;
                _loop_1(groupFilter);
            }
        }
        catch (e_6_1) { e_6 = { error: e_6_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_6) throw e_6.error; }
        }
    };
    DataStore.prototype.getGroupItems = function (groupType, groupValue) {
        var _this = this;
        var _a;
        if (Array.isArray(groupValue)) {
            return groupValue.reduce(function (all, group) {
                var groupItems = _this.itemsByGroups.get(groupType).get(group) || {};
                Object.assign(all, groupItems);
                return all;
            }, {});
        }
        return ((_a = this.itemsByGroups.get(groupType)) === null || _a === void 0 ? void 0 : _a.get(groupValue)) || {};
    };
    DataStore.prototype.getGroupKey = function (groupType, groupValue) {
        return "".concat(groupType, "-").concat(groupValue);
    };
    return DataStore;
}());
exports.default = DataStore;
//# sourceMappingURL=data-store.js.map