"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ltConfig = void 0;
const configService = __importStar(require("../services/config"));
const defaults_1 = require("./defaults");
const TTL_MS = defaults_1.CONFIG_CACHE_TTL_MS;
class LTConfigCache {
    constructor() {
        this.configs = null;
        this.loadedAt = 0;
        this.loadPromise = null;
    }
    async ensureLoaded() {
        if (this.configs && Date.now() - this.loadedAt < TTL_MS) {
            return this.configs;
        }
        if (!this.loadPromise) {
            this.loadPromise = configService
                .loadAllConfigs()
                .then((configs) => {
                this.configs = configs;
                this.loadedAt = Date.now();
                return configs;
            })
                .finally(() => {
                this.loadPromise = null;
            });
        }
        return this.loadPromise;
    }
    async get(name) {
        const configs = await this.ensureLoaded();
        return configs.get(name);
    }
    async isInvocable(name) {
        const config = await this.get(name);
        return config?.invocable ?? false;
    }
    async getInvocationRoles(name) {
        const config = await this.get(name);
        return config?.invocationRoles ?? [];
    }
    async getTargetEscalationRole(name) {
        const config = await this.get(name);
        return config?.role ?? 'reviewer';
    }
    async getAllowedEscalationRoles(name) {
        const config = await this.get(name);
        return config?.roles ?? [];
    }
    async getToolTags(name) {
        const config = await this.get(name);
        return config?.toolTags ?? [];
    }
    async getProviders(name) {
        const config = await this.get(name);
        return config?.consumes ?? [];
    }
    async getProviderData(name, originId) {
        const consumes = await this.getProviders(name);
        if (!consumes.length || !originId)
            return {};
        return configService.getProviderData(consumes, originId);
    }
    /** Force cache reload on next access. Call after config mutations. */
    invalidate() {
        this.configs = null;
        this.loadedAt = 0;
        this.loadPromise = null;
    }
    /**
     * Get the resolved config for a workflow, but only if certified.
     *
     * The interceptor uses this to decide whether to wrap the workflow
     * with task tracking, escalation handling, and re-run detection.
     * Configured-but-not-certified workflows (no roles, no consumes)
     * return null so the interceptor skips them.
     */
    async getResolvedConfig(name) {
        const config = await this.get(name);
        if (!config)
            return null;
        const isCertified = (config.roles?.length ?? 0) > 0 || (config.consumes?.length ?? 0) > 0;
        return isCertified ? config : null;
    }
}
exports.ltConfig = new LTConfigCache();
