/**
 * GetAgentGraphUseCase
 *
 * Returns the merged LangGraph descriptor for an agent type — the override
 * stored in agent_graph_overrides when present, otherwise the bundled
 * descriptor from the registry. Powers the agent editor's Graph tab and
 * any future runtime consumer.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { inject, injectable } from 'tsyringe';
import { getBuiltinGraph, } from '../../services/builtin-graph-registry.js';
let GetAgentGraphUseCase = class GetAgentGraphUseCase {
    overrides;
    customAgents;
    constructor(overrides, customAgents) {
        this.overrides = overrides;
        this.customAgents = customAgents;
    }
    async execute(input) {
        const bundled = getBuiltinGraph(input.agentType);
        // Built-in: bundled descriptor exists; honour its shape.
        if (bundled) {
            const override = await this.overrides.findActive(input.agentType);
            if (!override) {
                return {
                    agentType: input.agentType,
                    nodes: bundled.nodes,
                    edges: bundled.edges,
                    hasOverride: false,
                    bundled,
                };
            }
            const nodes = parseJsonArray(override.nodesJson, bundled.nodes);
            const edges = parseJsonArray(override.edgesJson, bundled.edges);
            return {
                agentType: input.agentType,
                nodes,
                edges,
                hasOverride: true,
                bundled,
            };
        }
        // Custom agent: bundled is an empty descriptor — the user authors
        // every node and edge themselves.
        const custom = await this.customAgents.findByType(input.agentType);
        if (!custom)
            return null;
        const emptyBundled = {
            agentType: input.agentType,
            nodes: [{ id: 'start', label: 'Start', description: 'edit me' }],
            edges: [],
        };
        const override = await this.overrides.findActive(input.agentType);
        if (!override) {
            return {
                agentType: input.agentType,
                nodes: emptyBundled.nodes,
                edges: emptyBundled.edges,
                hasOverride: false,
                bundled: emptyBundled,
            };
        }
        const nodes = parseJsonArray(override.nodesJson, emptyBundled.nodes);
        const edges = parseJsonArray(override.edgesJson, emptyBundled.edges);
        return {
            agentType: input.agentType,
            nodes,
            edges,
            hasOverride: true,
            bundled: emptyBundled,
        };
    }
};
GetAgentGraphUseCase = __decorate([
    injectable(),
    __param(0, inject('IAgentGraphOverrideRepository')),
    __param(1, inject('ICustomAgentRepository')),
    __metadata("design:paramtypes", [Object, Object])
], GetAgentGraphUseCase);
export { GetAgentGraphUseCase };
function parseJsonArray(raw, fallback) {
    try {
        const parsed = JSON.parse(raw);
        return Array.isArray(parsed) ? parsed : fallback;
    }
    catch {
        return fallback;
    }
}
