export class InMemoryKVStore {
    store;
    constructor() {
        this.store = new Map();
    }
    async get(key) {
        const res = this.store.get(key);
        if (res === undefined) {
            return null;
        }
        if (this.isExpired(res)) {
            this.store.delete(key);
            return null;
        }
        return Promise.resolve(res.value);
    }
    async set(key, value, canOverride = true, ttlInMs = 0) {
        const res = this.store.get(key);
        const isExpired = res && this.isExpired(res);
        if (isExpired || canOverride || res === undefined) {
            this.store.set(key, { value: value, timestamp: Date.now(), ttlInMs: ttlInMs });
            return Promise.resolve();
        }
        return Promise.reject(new Error('Key already exists'));
    }
    async delete(key) {
        this.store.delete(key);
        return Promise.resolve();
    }
    async exists(key) {
        return Promise.resolve(this.store.has(key));
    }
    isExpired(value) {
        if (value.ttlInMs > 0 && value.timestamp + value.ttlInMs < Date.now()) {
            return true;
        }
        return false;
    }
}
//# sourceMappingURL=InMemoryStore.js.map