"use strict";
/**
 * In-Memory Quota Cache
 *
 * Reduces external API calls by caching quota results with TTL.
 * Uses a simple Map-based cache with automatic expiration.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.QUOTA_CACHE_TTL_MS = exports.getQuotaCacheStats = exports.clearQuotaCache = exports.invalidateProviderCache = exports.invalidateQuotaCache = exports.setCachedQuota = exports.getCachedQuota = void 0;
/** Default TTL for quota cache entries (2 minutes) */
const DEFAULT_CACHE_TTL_MS = 2 * 60 * 1000;
/** In-memory cache store */
const quotaCache = new Map();
/**
 * Generate cache key for provider/account combination
 */
function getCacheKey(provider, accountId) {
    return `${provider}:${accountId}`;
}
/**
 * Get cached quota result if still valid
 * @param provider - Provider name (codex, gemini, agy)
 * @param accountId - Account identifier
 * @param ttlMs - Time-to-live in milliseconds (default: 2 minutes)
 * @returns Cached result or null if expired/missing
 */
function getCachedQuota(provider, accountId, ttlMs = DEFAULT_CACHE_TTL_MS) {
    const key = getCacheKey(provider, accountId);
    const entry = quotaCache.get(key);
    if (!entry) {
        return null;
    }
    // Check if cache is still valid
    if (Date.now() - entry.cachedAt < ttlMs) {
        return entry.data;
    }
    // Cache expired - remove entry
    quotaCache.delete(key);
    return null;
}
exports.getCachedQuota = getCachedQuota;
/**
 * Store quota result in cache
 * @param provider - Provider name (codex, gemini, agy)
 * @param accountId - Account identifier
 * @param data - Quota result to cache
 */
function setCachedQuota(provider, accountId, data) {
    const key = getCacheKey(provider, accountId);
    quotaCache.set(key, {
        data,
        cachedAt: Date.now(),
    });
}
exports.setCachedQuota = setCachedQuota;
/**
 * Invalidate cache for a specific account
 * @param provider - Provider name
 * @param accountId - Account identifier
 */
function invalidateQuotaCache(provider, accountId) {
    const key = getCacheKey(provider, accountId);
    quotaCache.delete(key);
}
exports.invalidateQuotaCache = invalidateQuotaCache;
/**
 * Invalidate all cache entries for a provider
 * @param provider - Provider name to clear
 */
function invalidateProviderCache(provider) {
    const prefix = `${provider}:`;
    for (const key of quotaCache.keys()) {
        if (key.startsWith(prefix)) {
            quotaCache.delete(key);
        }
    }
}
exports.invalidateProviderCache = invalidateProviderCache;
/**
 * Clear entire quota cache
 */
function clearQuotaCache() {
    quotaCache.clear();
}
exports.clearQuotaCache = clearQuotaCache;
/**
 * Get cache statistics for debugging
 */
function getQuotaCacheStats() {
    return {
        size: quotaCache.size,
        entries: Array.from(quotaCache.keys()),
    };
}
exports.getQuotaCacheStats = getQuotaCacheStats;
/** Export cache TTL for consumers */
exports.QUOTA_CACHE_TTL_MS = DEFAULT_CACHE_TTL_MS;
//# sourceMappingURL=quota-response-cache.js.map