"use strict";
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 common_utils_1 = require("@bringg/common-utils");
var itemFieldsHashCache = new WeakMap();
function generateHashWithCache(fields) {
    if (typeof fields !== 'object' || fields === null) {
        return (0, common_utils_1.generateStringHash)(JSON.stringify(fields));
    }
    var hash = itemFieldsHashCache.get(fields);
    if (hash === undefined) {
        hash = (0, common_utils_1.generateStringHash)(JSON.stringify(fields));
        itemFieldsHashCache.set(fields, hash);
    }
    return hash;
}
var LightDataStore = /** @class */ (function () {
    function LightDataStore() {
        this.itemsByLoadedFields = new Map();
    }
    LightDataStore.prototype.isFull = function (item) {
        var _a;
        return (_a = this.itemsByLoadedFields.get(LightDataStore.FULL_FIELD)) === null || _a === void 0 ? void 0 : _a.has(item.id);
    };
    LightDataStore.prototype.has = function (item, fields) {
        var _a;
        if (!fields) {
            return this.isFull(item);
        }
        return (_a = this.itemsByLoadedFields.get(generateHashWithCache(fields))) === null || _a === void 0 ? void 0 : _a.has(item.id);
    };
    LightDataStore.prototype.set = function (item, fields) {
        this.setMany([item], fields);
    };
    LightDataStore.prototype.setMany = function (items, fields) {
        if (!fields) {
            this.setField(items, LightDataStore.FULL_FIELD);
            return;
        }
        this.setField(items, generateHashWithCache(fields));
    };
    LightDataStore.prototype.remove = function (itemId) {
        var e_1, _a;
        try {
            for (var _b = __values(this.itemsByLoadedFields.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var items = _c.value;
                items.delete(itemId);
            }
        }
        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; }
        }
    };
    LightDataStore.prototype.setField = function (items, fieldHash) {
        var e_2, _a;
        var fieldHashSet = this.itemsByLoadedFields.get(fieldHash);
        if (!fieldHashSet) {
            fieldHashSet = new Set();
            this.itemsByLoadedFields.set(fieldHash, fieldHashSet);
        }
        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;
                fieldHashSet.add(item.current.id);
            }
        }
        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; }
        }
    };
    LightDataStore.FULL_FIELD = -1;
    return LightDataStore;
}());
exports.default = LightDataStore;
//# sourceMappingURL=light-data-store.js.map