/**
 * UpsertAgentPromptOverrideUseCase
 *
 * Creates a new override or replaces the existing one for the given
 * (agentType, promptId) slot. Rejects unknown slots — the user can only
 * override prompts the runtime registry knows about (FR-35).
 */
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 { isKnownPromptSlot } from '../../services/builtin-prompt-registry.js';
let UpsertAgentPromptOverrideUseCase = class UpsertAgentPromptOverrideUseCase {
    overrides;
    customAgents;
    constructor(overrides, customAgents) {
        this.overrides = overrides;
        this.customAgents = customAgents;
    }
    async execute(input) {
        if (!input.agentType.trim())
            throw new Error('agentType is required');
        if (!input.promptId.trim())
            throw new Error('promptId is required');
        if (typeof input.body !== 'string' || input.body.length === 0) {
            throw new Error('body must be a non-empty string');
        }
        if (!isKnownPromptSlot(input.agentType, input.promptId)) {
            // Custom agents accept any slot the user invents.
            const custom = await this.customAgents.findByType(input.agentType);
            if (!custom) {
                throw new Error(`Unknown prompt slot: ${input.agentType}/${input.promptId}. ` +
                    'Only slots registered in the built-in prompt registry or owned by a custom agent ' +
                    'can be overridden.');
            }
        }
        const existing = await this.overrides.findActive(input.agentType, input.promptId);
        const now = new Date();
        const override = {
            id: existing?.id ?? randomUUID(),
            agentType: input.agentType,
            promptId: input.promptId,
            body: input.body,
            version: (existing?.version ?? 0) + 1,
            createdBy: input.createdBy ?? existing?.createdBy ?? 'user',
            createdAt: existing?.createdAt ?? now,
            updatedAt: now,
        };
        if (existing) {
            await this.overrides.update(override);
        }
        else {
            await this.overrides.create(override);
        }
        return override;
    }
};
UpsertAgentPromptOverrideUseCase = __decorate([
    injectable(),
    __param(0, inject('IAgentPromptOverrideRepository')),
    __param(1, inject('ICustomAgentRepository')),
    __metadata("design:paramtypes", [Object, Object])
], UpsertAgentPromptOverrideUseCase);
export { UpsertAgentPromptOverrideUseCase };
