/**
 * Agent Registry Service
 *
 * Infrastructure implementation of the IAgentRegistry port.
 * Manages agent workflow definitions in an in-memory map,
 * enabling runtime discovery and instantiation of agent workflows.
 *
 * Pre-registers built-in agents (analyze-repository) at construction time.
 */
export class AgentRegistryService {
    agents = new Map();
    lazyAgents = [];
    constructor() {
        // Register built-in agents as lazy definitions to avoid importing
        // @langchain/langgraph (~300ms) at CLI startup.
        this.lazyAgents.push({
            name: 'analyze-repository',
            description: 'Analyze repository structure, dependencies, and architecture',
            importFn: () => import('../analyze-repo/analyze-repository-graph.js'),
            factoryExport: 'createAnalyzeRepositoryGraph',
        }, {
            name: 'feature-agent',
            description: 'Autonomous SDLC agent: analyze → requirements → research → plan → implement',
            importFn: () => import('../feature-agent/feature-agent-graph.js'),
            factoryExport: 'createFeatureAgentGraph',
        });
    }
    async resolveLazy(name) {
        const lazy = this.lazyAgents.find((a) => a.name === name);
        if (!lazy)
            return undefined;
        const mod = await lazy.importFn();
        const definition = {
            name: lazy.name,
            description: lazy.description,
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            graphFactory: mod[lazy.factoryExport],
        };
        this.agents.set(name, definition);
        return definition;
    }
    register(definition) {
        this.agents.set(definition.name, definition);
    }
    async get(name) {
        const eager = this.agents.get(name);
        if (eager)
            return eager;
        return this.resolveLazy(name);
    }
    list() {
        // Returns eagerly-registered + cached placeholder metadata for lazy agents.
        const all = new Map(this.agents);
        for (const lazy of this.lazyAgents) {
            if (!all.has(lazy.name)) {
                lazy.placeholder ??= {
                    name: lazy.name,
                    description: lazy.description,
                    graphFactory: () => {
                        throw new Error(`Agent '${lazy.name}' must be resolved via get() before use`);
                    },
                };
                all.set(lazy.name, lazy.placeholder);
            }
        }
        return Array.from(all.values());
    }
}
