"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CapabilityRouter = void 0;
const IntentClassifier_1 = require("./IntentClassifier");
const CapabilityIndex_1 = require("./CapabilityIndex");
class CapabilityRouter {
    registry;
    index;
    constructor(registry, index) {
        this.registry = registry;
        // Phase 3: a real capability index over the catalog drives agent
        // selection. Built once per router (lazy disk read of AGENT.md bodies).
        this.index = index ?? new CapabilityIndex_1.CapabilityIndex();
    }
    route(task, input = '') {
        const normalized = input.toLowerCase();
        if (task.intent === IntentClassifier_1.TaskIntent.RESEARCH_ANALYSIS) {
            return this.buildRoute(['analysis', 'review', 'synthesis'], 'parallel', 'Research tende a se beneficiar de múltiplos olhares e síntese.', input);
        }
        if (task.intent === IntentClassifier_1.TaskIntent.ENGINEERING_BUILD) {
            return this.buildRoute(['orchestration', 'build', 'review', 'qa'], 'review_loop', 'Build complexo pede execução com validação crítica.', input);
        }
        if (normalized.includes('release')) {
            return this.buildRoute(['release', 'review', 'qa'], 'review_loop', 'Release exige quality gate e validação final.', input);
        }
        return this.buildRoute(['orchestration', 'analysis'], 'single', 'Tarefa simples o suficiente para rota enxuta.', input);
    }
    buildRoute(capabilities, mode, rationale, input) {
        // 1. Semantic selection — rank the catalog agents by relevance to the
        //    actual task text + capability keywords. Replaces the old
        //    "first substring match, else hardcoded 5 ai-builder agents" path.
        const query = [input, ...capabilities].filter(Boolean).join(' ');
        let selection = 'semantic';
        let selected = this.resolveSemantic(query);
        // 2. Fallback: capability-keyword substring match (legacy behaviour).
        if (selected.length === 0) {
            selected = this.registry.findMiniSquad(capabilities);
            selection = 'capability';
        }
        // 3. Last resort: the priority set, so a route always has agents.
        if (selected.length === 0) {
            selected = this.registry.findPrioritySet().slice(0, 4);
            selection = 'priority-fallback';
        }
        return { mode, capabilities, agents: selected, rationale, selection };
    }
    /** Map the index's top relevance hits back to RegisteredAgent records. */
    resolveSemantic(query) {
        if (!query.trim())
            return [];
        const ranked = this.index.search(query, 4);
        if (ranked.length === 0)
            return [];
        const byId = new Map(this.registry.all().map(a => [a.id, a]));
        const out = [];
        for (const hit of ranked) {
            const agent = byId.get(hit.id);
            if (agent && !out.find(a => a.id === agent.id))
                out.push(agent);
        }
        return out;
    }
}
exports.CapabilityRouter = CapabilityRouter;
