"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "KeyDiffCache", {
    enumerable: true,
    get: function() {
        return KeyDiffCache;
    }
});
class KeyDiffCache {
    deletedKeys = [];
    updatedKeys = [];
    keyValueStore = new Map();
    async getAllValues() {
        return Array.from(this.keyValueStore.values());
    }
    async getAllKeyValues() {
        return Object.fromEntries(this.keyValueStore);
    }
    async getValue(key) {
        return this.keyValueStore.get(key);
    }
    async setKeyValuesBatch(keyValues, markUpdated = true) {
        for (const { key, value } of keyValues){
            await this.setKeyValue(key, value, false);
        }
        if (markUpdated) {
            const updatedKeys = keyValues.map(({ key })=>key);
            this.batchMarkUpdated(updatedKeys);
        }
    }
    async deleteKeysBatch(keys, markDeleted = true) {
        for (const key of keys){
            await this.deleteKeyValue(key, false);
        }
        if (markDeleted) {
            this.batchMarkDeleted(keys);
        }
    }
    async setKeyValue(key, value, markUpdated = true) {
        this.keyValueStore.set(key, value);
        if (markUpdated) {
            this.markUpdated(key);
        }
    }
    async deleteKeyValue(key, markDeleted = true) {
        this.keyValueStore.delete(key);
        if (markDeleted) {
            this.markDeleted(key);
        }
    }
    batchMarkDeleted(keys) {
        for (const key of keys){
            this.markDeleted(key);
        }
    }
    markUpdated(key) {
        const deletedIndex = this.deletedKeys.indexOf(key);
        if (deletedIndex !== -1) {
            this.deletedKeys.splice(deletedIndex, 1);
        }
        if (!this.updatedKeys.includes(key)) {
            this.updatedKeys.push(key);
        }
    }
    markDeleted(key) {
        const updatedIndex = this.updatedKeys.indexOf(key);
        if (updatedIndex !== -1) {
            this.updatedKeys.splice(updatedIndex, 1);
        }
        if (!this.deletedKeys.includes(key)) {
            this.deletedKeys.push(key);
        }
    }
    resetDiffs() {
        this.deletedKeys = [];
        this.updatedKeys = [];
    }
    batchMarkUpdated(keys) {
        for (const key of keys){
            this.markUpdated(key);
        }
    }
}

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