"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.AgentRegistry = void 0;
const path = __importStar(require("path"));
const FileAgentProvider_1 = require("./providers/FileAgentProvider");
const CloudAgentProvider_1 = require("./providers/CloudAgentProvider");
const CompositeAgentProvider_1 = require("./providers/CompositeAgentProvider");
const defaultAgentRoots = () => {
    const roots = process.env.OPENLIFE_AGENT_ROOTS
        ? process.env.OPENLIFE_AGENT_ROOTS.split(':').filter(Boolean)
        : [];
    if (roots.length > 0)
        return roots;
    // Multi-root resolution (v1.8.3+): prefer the user's project-local .catalog
    // (override), fall back to the npm-package-bundled .catalog (defaults that
    // ship with @openlife/cli — the user's 314+ agents, etc.). FileAgentProvider
    // skips non-existent roots, so a clone-of-repo install ends up with both
    // paths resolving to the same dir and FileAgentProvider naturally dedupes
    // by id downstream.
    const cwdRoot = path.join(process.cwd(), '.catalog', 'agents');
    const packageRoot = path.join(__dirname, '..', '..', '.catalog', 'agents');
    return cwdRoot === packageRoot ? [cwdRoot] : [cwdRoot, packageRoot];
};
class AgentRegistry {
    provider;
    agents = [];
    constructor(provider = new CompositeAgentProvider_1.CompositeAgentProvider(new CloudAgentProvider_1.CloudAgentProvider(process.env.OPENLIFE_CLOUD_AGENTS_FILE || ''), new FileAgentProvider_1.FileAgentProvider(defaultAgentRoots()))) {
        this.provider = provider;
        this.load();
    }
    load() {
        this.agents = this.provider.load();
    }
    all() {
        return this.agents.sort((a, b) => b.priority - a.priority);
    }
    findByCapability(capability) {
        const normalized = capability.toLowerCase();
        return this.all().filter(agent => agent.capabilities.some(cap => cap.toLowerCase().includes(normalized)) ||
            agent.role.toLowerCase().includes(normalized) ||
            agent.name.toLowerCase().includes(normalized) ||
            agent.tags.some(tag => tag.toLowerCase().includes(normalized)));
    }
    registerTemporary(agent) {
        this.agents.push(agent);
    }
    findMiniSquad(capabilities) {
        const selected = [];
        for (const capability of capabilities) {
            const found = this.findByCapability(capability)[0];
            if (found && !selected.find(a => a.id === found.id))
                selected.push(found);
        }
        return selected;
    }
    findPrioritySet() {
        const priorityNames = [
            'ai-builder-orchestrator',
            'ai-builder-analyst',
            'ai-builder-builder',
            'ai-builder-qa-reviewer',
            'ai-builder-release-manager'
        ];
        return this.all().filter(agent => priorityNames.includes(agent.id));
    }
}
exports.AgentRegistry = AgentRegistry;
