"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pluginConfig = exports.PluginConfigStore = void 0;
const dragonfly_1 = require("../infra/cache/dragonfly");
const logger_1 = require("../config/logger");
const KEY_PREFIX = 'plugin:config:';
// Config de plugins não expira — TTL longo (1 ano)
const TTL_SECONDS = 365 * 24 * 3600;
/**
 * Config store para plugins, backed por DragonflyDB.
 * get() usa cache in-memory para evitar round-trips no hot-path.
 * set() persiste no Dragonfly e invalida o cache local.
 */
class PluginConfigStore {
    constructor() {
        this.cache = new Map();
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    /** Lê config de um plugin. Retorna {} se não houver config salva. */
    get(pluginName) {
        return this.cache.get(pluginName) ?? {};
    }
    /** Persiste config no Dragonfly e atualiza cache local. */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    async set(pluginName, config) {
        this.cache.set(pluginName, config);
        await dragonfly_1.dragonfly.setJson(`${KEY_PREFIX}${pluginName}`, config, TTL_SECONDS);
        logger_1.logger.debug({ pluginName }, '[PluginConfig] Config salva');
    }
    /** Carrega todas as configs do Dragonfly para o cache local no boot. */
    async loadAll(pluginNames) {
        await Promise.all(pluginNames.map(async (name) => {
            const stored = await dragonfly_1.dragonfly.getJson(`${KEY_PREFIX}${name}`);
            if (stored) {
                this.cache.set(name, stored);
                logger_1.logger.debug({ name }, '[PluginConfig] Config carregada do Dragonfly');
            }
        }));
    }
    /** Retorna config de todos os plugins (para a API admin). */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    getAllCached() {
        const result = {};
        for (const [name, config] of this.cache.entries()) {
            result[name] = config;
        }
        return result;
    }
}
exports.PluginConfigStore = PluginConfigStore;
exports.pluginConfig = new PluginConfigStore();
//# sourceMappingURL=plugin-config.js.map