"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listCache = exports.schemaCache = void 0;
exports.getSchemaFromCache = getSchemaFromCache;
exports.saveSchemaToCache = saveSchemaToCache;
exports.getListFromCache = getListFromCache;
exports.saveListToCache = saveListToCache;
const lru_cache_1 = require("lru-cache");
exports.schemaCache = new lru_cache_1.LRUCache({
    max: 2000,
    ttl: 1000 * 60 * 60
});
function getSchemaCacheKey(name, subscriptionMetadata) {
    return `${subscriptionMetadata.actionConfigId}-${name}`;
}
function getSchemaFromCache(name, subscriptionMetadata, statsContext) {
    if (!subscriptionMetadata || !subscriptionMetadata?.actionConfigId) {
        statsContext?.statsClient?.incr('cache.get.error', 1, statsContext?.tags);
        return undefined;
    }
    const schema = exports.schemaCache.get(getSchemaCacheKey(name, subscriptionMetadata)) ?? undefined;
    return schema;
}
async function saveSchemaToCache(schema, subscriptionMetadata, statsContext) {
    if (!subscriptionMetadata || !subscriptionMetadata?.actionConfigId) {
        statsContext?.statsClient?.incr('cache.save.error', 1, statsContext?.tags);
        return;
    }
    exports.schemaCache.set(getSchemaCacheKey(schema.object_details.object_type, subscriptionMetadata), schema);
    statsContext?.statsClient?.incr('cache.save.success', 1, statsContext?.tags);
}
exports.listCache = new lru_cache_1.LRUCache({
    max: 2000,
    ttl: 1000 * 60 * 30
});
function getListCacheKey(listName, objectType, subscriptionMetadata) {
    return `${subscriptionMetadata.actionConfigId}-listName-${listName}-objectType-${objectType}`;
}
function getListFromCache(name, objectType, subscriptionMetadata, statsContext) {
    if (!subscriptionMetadata || !subscriptionMetadata?.actionConfigId) {
        statsContext?.statsClient?.incr('cache.getListFromCache.error', 1, statsContext?.tags);
        return undefined;
    }
    const cachableList = exports.listCache.get(getListCacheKey(name, objectType, subscriptionMetadata)) ?? undefined;
    return cachableList;
}
async function saveListToCache(cachableList, subscriptionMetadata, statsContext) {
    if (!subscriptionMetadata || !subscriptionMetadata?.actionConfigId) {
        statsContext?.statsClient?.incr('cache.saveListToCache.error', 1, statsContext?.tags);
        return;
    }
    exports.listCache.set(getListCacheKey(cachableList.name, cachableList.objectType, subscriptionMetadata), cachableList);
    statsContext?.statsClient?.incr('cache.saveListToCache.success', 1, statsContext?.tags);
}
//# sourceMappingURL=cache-functions.js.map