"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "KeyDiffCache", {
    enumerable: true,
    get: function() {
        return KeyDiffCache;
    }
});
class KeyDiffCache {
    deletedKeys = [];
    updatedKeys = [];
    keyValueStore = {};
    async getAllValues() {
        const keys = Object.keys(this.keyValueStore);
        return await this.getValuesBatch(keys);
    }
    async getAllKeyValues() {
        return this.keyValueStore;
    }
    async getValue(key) {
        const keyString = key?.toString();
        if (!keyString?.length) {
            throw new Error("Key must be a non-empty serializable string");
        }
        return this.keyValueStore[keyString];
    }
    async processStateDiffs() {
        const updatedKeys = [
            ...this.updatedKeys
        ];
        const updatedValues = await this.getValuesBatch(updatedKeys);
        const deletedKeys = [
            ...this.deletedKeys
        ];
        this.resetDiffs();
        return {
            updatedKeys,
            updatedValues,
            deletedKeys
        };
    }
    async setKeyValuesBatch(keyValues, markUpdated = true) {
        keyValues.forEach(({ key, value })=>{
            this.setKeyValue(key, value);
        });
        if (markUpdated) {
            const updatedKeys = keyValues.map(({ key })=>key);
            this.batchMarkUpdated(updatedKeys);
        }
    }
    async deleteKeysBatch(keys, markDeleted = true) {
        keys.forEach((key)=>{
            this.deleteKeyValue(key);
        });
        if (markDeleted) {
            this.batchMarkDeleted(keys);
        }
    }
    async getValuesBatch(keys) {
        const keyStrings = keys.map((key)=>key?.toString());
        if (keyStrings.some((key)=>!key?.length)) {
            throw new Error("Key must be a non-empty serializable string, and one of them is not");
        }
        const values = [];
        for (const keyString of keyStrings){
            const value = await this.getValue(keyString.toString());
            values.push(value);
        }
        return values;
    }
    async setKeyValue(key, value, markUpdated = true) {
        const keyString = this.convertToKeyString(key);
        if (!keyString?.length) {
            throw new Error("Key must be a non-empty serializable string");
        }
        if (this.deletedKeys.includes(keyString)) {
            return;
        }
        this.keyValueStore[keyString] = value;
        if (markUpdated) {
            this.markUpdated(keyString);
        }
    }
    async deleteKeyValue(key, markDeleted = true) {
        const keyString = this.convertToKeyString(key);
        if (!keyString?.length) {
            throw new Error("Key must be a non-empty serializable string");
        }
        delete this.keyValueStore[keyString];
        if (markDeleted) {
            this.markDeleted(keyString);
        }
    }
    batchMarkDeleted(keys) {
        for (const key of keys){
            this.markDeleted(key);
        }
    }
    markUpdated(key) {
        if (this.deletedKeys.includes(key)) {
            this.deletedKeys.splice(this.deletedKeys.indexOf(key), 1);
        }
        if (!this.updatedKeys.includes(key)) {
            this.updatedKeys.push(key);
        }
    }
    markDeleted(key) {
        if (this.updatedKeys.includes(key)) {
            this.updatedKeys.splice(this.updatedKeys.indexOf(key), 1);
        }
        if (!this.deletedKeys.includes(key)) {
            this.deletedKeys.push(key);
        }
    }
    convertToKeyString(key) {
        return key?.toString();
    }
    resetDiffs() {
        this.deletedKeys = [];
        this.updatedKeys = [];
    }
    batchMarkUpdated(keys) {
        for (const key of keys){
            this.markUpdated(key);
        }
    }
}

//# sourceMappingURL=key-diff.cache.js.map