/**
 * ListAgentPromptsUseCase
 *
 * Returns the merged view of every built-in prompt for an agent type
 * with its currently active override (when one exists). Powers the
 * agent editor's Prompts tab (FR-37).
 */
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 { listBuiltinPromptsForAgent, listBuiltinAgentTypes, } from '../../services/builtin-prompt-registry.js';
let ListAgentPromptsUseCase = class ListAgentPromptsUseCase {
    overrides;
    customAgents;
    constructor(overrides, customAgents) {
        this.overrides = overrides;
        this.customAgents = customAgents;
    }
    /**
     * List every registered agent type (built-ins + customs) with its slot
     * count. Built-ins always come first; customs follow in alphabetical
     * order. Only the in-memory built-in catalog is consulted synchronously
     * — call {@link listAgentTypesAsync} to include customs.
     */
    listAgentTypes() {
        return listBuiltinAgentTypes();
    }
    async listAgentTypesAsync() {
        const builtins = listBuiltinAgentTypes().map((b) => ({ ...b, isCustom: false }));
        const customs = await this.customAgents.listAll();
        const overrideRows = await this.overrides.listAll();
        const customCounts = new Map();
        for (const row of overrideRows) {
            if (!builtins.some((b) => b.agentType === row.agentType)) {
                customCounts.set(row.agentType, (customCounts.get(row.agentType) ?? 0) + 1);
            }
        }
        const customEntries = customs.map((agent) => ({
            agentType: agent.agentType,
            promptCount: customCounts.get(agent.agentType) ?? 0,
            isCustom: true,
            name: agent.name,
        }));
        return [...builtins, ...customEntries];
    }
    async execute(input) {
        const slots = listBuiltinPromptsForAgent(input.agentType);
        const overrideRows = await this.overrides.listForAgent(input.agentType);
        if (slots.length > 0) {
            const overrideBySlot = new Map(overrideRows.map((row) => [row.promptId, row]));
            return slots.map((slot) => buildEntry(slot, overrideBySlot.get(slot.promptId)));
        }
        // Custom agent: every override row is a slot — there is no bundled
        // body to fall back to, so the override body IS the effective body.
        const custom = await this.customAgents.findByType(input.agentType);
        if (!custom)
            return [];
        return overrideRows.map((row) => ({
            agentType: row.agentType,
            promptId: row.promptId,
            name: row.promptId,
            description: `Custom prompt for ${custom.name}`,
            bundledBody: '',
            effectiveBody: row.body,
            hasOverride: true,
            override: row,
        }));
    }
};
ListAgentPromptsUseCase = __decorate([
    injectable(),
    __param(0, inject('IAgentPromptOverrideRepository')),
    __param(1, inject('ICustomAgentRepository')),
    __metadata("design:paramtypes", [Object, Object])
], ListAgentPromptsUseCase);
export { ListAgentPromptsUseCase };
function buildEntry(slot, override) {
    return {
        agentType: slot.agentType,
        promptId: slot.promptId,
        name: slot.name,
        description: slot.description,
        bundledBody: slot.body,
        effectiveBody: override?.body ?? slot.body,
        hasOverride: override !== undefined,
        ...(override !== undefined && { override }),
    };
}
