/**
 * CreateCustomAgentUseCase
 *
 * Creates a brand-new custom agent type the user can attach prompt and
 * graph overrides to. Optionally seeds the first prompt slot so the agent
 * is immediately useful in the editor.
 *
 * Rejects names that collide with built-in agent types — built-ins live
 * in code and must not be shadowed.
 */
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 { randomUUID } from 'node:crypto';
import { listBuiltinAgentTypes } from '../../services/builtin-prompt-registry.js';
const AGENT_TYPE_PATTERN = /^[a-z][a-z0-9-]{1,63}$/;
let CreateCustomAgentUseCase = class CreateCustomAgentUseCase {
    agents;
    overrides;
    constructor(agents, overrides) {
        this.agents = agents;
        this.overrides = overrides;
    }
    async execute(input) {
        if (!input.agentType.trim())
            throw new Error('agentType is required');
        if (!AGENT_TYPE_PATTERN.test(input.agentType)) {
            throw new Error('agentType must be lowercase kebab-case (a-z, 0-9, -), starting with a letter, max 64 chars');
        }
        if (!input.name.trim())
            throw new Error('name is required');
        if (!input.description.trim())
            throw new Error('description is required');
        if (isBuiltinAgentType(input.agentType)) {
            throw new Error(`agentType "${input.agentType}" collides with a built-in agent and cannot be used`);
        }
        const existing = await this.agents.findByType(input.agentType);
        if (existing) {
            throw new Error(`agentType "${input.agentType}" already exists`);
        }
        const now = new Date();
        const agent = {
            id: randomUUID(),
            agentType: input.agentType,
            name: input.name,
            description: input.description,
            createdBy: input.createdBy ?? 'user',
            createdAt: now,
            updatedAt: now,
        };
        await this.agents.create(agent);
        let initialPromptOverride;
        if (input.initialPrompt) {
            const promptId = input.initialPrompt.promptId.trim();
            if (!promptId)
                throw new Error('initialPrompt.promptId must be non-empty');
            if (!input.initialPrompt.body.length) {
                throw new Error('initialPrompt.body must be non-empty');
            }
            initialPromptOverride = {
                id: randomUUID(),
                agentType: agent.agentType,
                promptId,
                body: input.initialPrompt.body,
                version: 1,
                createdBy: agent.createdBy,
                createdAt: now,
                updatedAt: now,
            };
            await this.overrides.create(initialPromptOverride);
        }
        return initialPromptOverride === undefined ? { agent } : { agent, initialPromptOverride };
    }
};
CreateCustomAgentUseCase = __decorate([
    injectable(),
    __param(0, inject('ICustomAgentRepository')),
    __param(1, inject('IAgentPromptOverrideRepository')),
    __metadata("design:paramtypes", [Object, Object])
], CreateCustomAgentUseCase);
export { CreateCustomAgentUseCase };
function isBuiltinAgentType(agentType) {
    return listBuiltinAgentTypes().some((t) => t.agentType === agentType);
}
