export class AsyncTtlCache {
    #cache = new Map();
    #inflight = new Map();
    #defaultSuccessTtlMs;
    #defaultErrorTtlMs;
    constructor(params) {
        this.#defaultSuccessTtlMs = Math.max(0, params.successTtlMs);
        this.#defaultErrorTtlMs = Math.max(0, params.errorTtlMs);
    }
    get(key) {
        return this.#cache.get(key) ?? null;
    }
    keys() {
        return this.#cache.keys();
    }
    delete(key) {
        this.#cache.delete(key);
        this.#inflight.delete(key);
    }
    clear() {
        this.#cache.clear();
        this.#inflight.clear();
    }
    isFresh(entry, nowMs = Date.now()) {
        return nowMs >= 0 && nowMs < entry.expiresAt;
    }
    setSuccess(key, value, params) {
        const nowMs = typeof params?.nowMs === 'number' ? params.nowMs : Date.now();
        const ttlMs = typeof params?.ttlMs === 'number' ? Math.max(0, params.ttlMs) : this.#defaultSuccessTtlMs;
        this.#cache.set(key, { kind: 'success', updatedAt: nowMs, expiresAt: nowMs + ttlMs, value });
    }
    setError(key, params) {
        const nowMs = typeof params?.nowMs === 'number' ? params.nowMs : Date.now();
        const ttlMs = typeof params?.ttlMs === 'number' ? Math.max(0, params.ttlMs) : this.#defaultErrorTtlMs;
        this.#cache.set(key, { kind: 'error', updatedAt: nowMs, expiresAt: nowMs + ttlMs });
    }
    async runDedupe(key, run) {
        const existing = this.#inflight.get(key);
        if (existing)
            return await existing;
        const pending = (async () => {
            try {
                return await run();
            }
            finally {
                this.#inflight.delete(key);
            }
        })();
        this.#inflight.set(key, pending);
        return await pending;
    }
}
//# sourceMappingURL=asyncTtlCache.js.map