"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKFLOW_TEMPLATE_SET_DRAFT_SCHEMA_VERSION = exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION = exports.WORKFLOW_TEMPLATE_DRAFT_KIND = void 0;
exports.buildWorkflowTemplateDraftFile = buildWorkflowTemplateDraftFile;
exports.stringifyWorkflowTemplateDraftFile = stringifyWorkflowTemplateDraftFile;
exports.readWorkflowTemplateDraftFile = readWorkflowTemplateDraftFile;
exports.parseWorkflowTemplateDraftFile = parseWorkflowTemplateDraftFile;
const zod_1 = require("zod");
const catalog_1 = require("../i18n/catalog");
const workflow_template_command_support_1 = require("./workflow-template-command-support");
const draft_file_support_1 = require("./draft-file-support");
exports.WORKFLOW_TEMPLATE_DRAFT_KIND = "alpaca.workflowTemplateDraft";
exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION = 4;
exports.WORKFLOW_TEMPLATE_SET_DRAFT_SCHEMA_VERSION = 6;
const workflowTemplateDraftFileRequiresSchema = zod_1.z
    .object({
    skills: zod_1.z.array(zod_1.z.string()),
    mcp: zod_1.z.array(zod_1.z.string()),
})
    .strict();
const workflowTemplateDraftFileBaseSchemaShape = {
    kind: zod_1.z.literal(exports.WORKFLOW_TEMPLATE_DRAFT_KIND),
    schemaVersion: zod_1.z.literal(exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION),
    command: zod_1.z.enum(["create", "update"]),
    preparedAt: zod_1.z.string().min(1),
    sourcePrompt: zod_1.z.string().nullable().optional(),
    path: zod_1.z.string().min(1),
};
const workflowTemplateDraftFileContentSchemaShape = {
    description: zod_1.z.string().nullable(),
    requires: workflowTemplateDraftFileRequiresSchema,
    targets: zod_1.z.array(zod_1.z.string()),
    autoSelect: zod_1.z.boolean(),
    body: zod_1.z.string().min(1),
};
const workflowTemplateDraftFileSchema = zod_1.z.discriminatedUnion("mode", [
    zod_1.z
        .object({
        ...workflowTemplateDraftFileBaseSchemaShape,
        mode: zod_1.z.literal("create"),
        ...workflowTemplateDraftFileContentSchemaShape,
    })
        .strict(),
    zod_1.z
        .object({
        ...workflowTemplateDraftFileBaseSchemaShape,
        mode: zod_1.z.literal("update"),
        ...workflowTemplateDraftFileContentSchemaShape,
        baseDigest: zod_1.z.string().min(1),
        impactSummary: zod_1.z.string().nullable().optional(),
    })
        .strict(),
    zod_1.z
        .object({
        ...workflowTemplateDraftFileBaseSchemaShape,
        mode: zod_1.z.literal("reuse"),
        reason: zod_1.z.string().min(1),
    })
        .strict(),
]);
const workflowTemplateDraftFileItemSchema = zod_1.z.discriminatedUnion("action", [
    zod_1.z
        .object({
        action: zod_1.z.literal("create_new"),
        path: zod_1.z.string().min(1),
        description: zod_1.z.string().nullable(),
        requires: workflowTemplateDraftFileRequiresSchema,
        targets: zod_1.z.array(zod_1.z.string()),
        autoSelect: zod_1.z.boolean(),
        body: zod_1.z.string().min(1),
    })
        .strict(),
    zod_1.z
        .object({
        action: zod_1.z.literal("update_existing"),
        targetPath: zod_1.z.string().min(1),
        path: zod_1.z.string().min(1),
        description: zod_1.z.string().nullable(),
        requires: workflowTemplateDraftFileRequiresSchema,
        targets: zod_1.z.array(zod_1.z.string()),
        autoSelect: zod_1.z.boolean(),
        body: zod_1.z.string().min(1),
        baseDigest: zod_1.z.string().min(1),
        impactSummary: zod_1.z.string().nullable().optional(),
    })
        .strict(),
    zod_1.z
        .object({
        action: zod_1.z.literal("reuse_existing"),
        targetPath: zod_1.z.string().min(1),
        reason: zod_1.z.string().min(1),
    })
        .strict(),
    zod_1.z
        .object({
        action: zod_1.z.literal("delete_existing"),
        targetPath: zod_1.z.string().min(1),
        baseDigest: zod_1.z.string().min(1),
        reason: zod_1.z.string().min(1),
    })
        .strict(),
]);
const workflowTemplateSetDraftFileSchema = zod_1.z
    .object({
    kind: zod_1.z.literal(exports.WORKFLOW_TEMPLATE_DRAFT_KIND),
    schemaVersion: zod_1.z.literal(exports.WORKFLOW_TEMPLATE_SET_DRAFT_SCHEMA_VERSION),
    command: zod_1.z.enum(["create", "update", "delete"]),
    templateSet: zod_1.z.literal(true),
    preparedAt: zod_1.z.string().min(1),
    sourcePrompt: zod_1.z.string().nullable().optional(),
    anchorPath: zod_1.z.string().min(1).optional(),
    items: zod_1.z.array(workflowTemplateDraftFileItemSchema).min(1),
})
    .strict();
function buildWorkflowTemplateDraftFile(input) {
    if ("templateSet" in input) {
        return {
            kind: exports.WORKFLOW_TEMPLATE_DRAFT_KIND,
            schemaVersion: exports.WORKFLOW_TEMPLATE_SET_DRAFT_SCHEMA_VERSION,
            command: input.command,
            templateSet: true,
            preparedAt: input.preparedAt,
            ...(input.sourcePrompt !== undefined
                ? { sourcePrompt: input.sourcePrompt }
                : {}),
            ...(input.anchorPath !== undefined
                ? { anchorPath: input.anchorPath }
                : {}),
            items: input.items.map((item) => ({ ...item })),
        };
    }
    const base = {
        kind: exports.WORKFLOW_TEMPLATE_DRAFT_KIND,
        schemaVersion: exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION,
        command: input.command,
        preparedAt: input.preparedAt,
        ...(input.sourcePrompt !== undefined
            ? { sourcePrompt: input.sourcePrompt }
            : {}),
        path: input.path,
    };
    if (input.mode === "reuse") {
        return {
            ...base,
            mode: "reuse",
            reason: input.reason,
        };
    }
    const content = {
        description: input.description,
        requiredSkills: [...input.requiredSkills],
        requiredMcp: [...input.requiredMcp],
        targets: [...input.targets],
        autoSelect: input.autoSelect,
        body: input.body,
    };
    if (input.mode === "update") {
        return {
            ...base,
            ...content,
            mode: "update",
            baseDigest: input.baseDigest,
            ...(input.impactSummary !== undefined
                ? { impactSummary: input.impactSummary }
                : {}),
        };
    }
    return {
        ...base,
        ...content,
        mode: input.mode,
    };
}
function stringifyWorkflowTemplateDraftFile(draftFile) {
    return (0, draft_file_support_1.stringifyReusableDraftFile)(toSerializableWorkflowTemplateDraftFile(draftFile));
}
function toSerializableWorkflowTemplateDraftFile(draftFile) {
    if ("templateSet" in draftFile) {
        return {
            ...draftFile,
            schemaVersion: exports.WORKFLOW_TEMPLATE_SET_DRAFT_SCHEMA_VERSION,
            items: draftFile.items.map(toSerializableWorkflowTemplateDraftFileItem),
        };
    }
    if (draftFile.mode === "reuse") {
        return {
            ...draftFile,
            schemaVersion: exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION,
        };
    }
    const content = toSerializableWorkflowTemplateContentSource(draftFile);
    if (draftFile.mode === "update") {
        return {
            kind: draftFile.kind,
            schemaVersion: exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION,
            command: draftFile.command,
            mode: draftFile.mode,
            preparedAt: draftFile.preparedAt,
            ...(draftFile.sourcePrompt !== undefined
                ? { sourcePrompt: draftFile.sourcePrompt }
                : {}),
            path: draftFile.path,
            ...content,
            baseDigest: draftFile.baseDigest,
            ...(draftFile.impactSummary !== undefined
                ? { impactSummary: draftFile.impactSummary }
                : {}),
        };
    }
    return {
        kind: draftFile.kind,
        schemaVersion: exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION,
        command: draftFile.command,
        mode: draftFile.mode,
        preparedAt: draftFile.preparedAt,
        ...(draftFile.sourcePrompt !== undefined
            ? { sourcePrompt: draftFile.sourcePrompt }
            : {}),
        path: draftFile.path,
        ...content,
    };
}
function toSerializableWorkflowTemplateDraftFileItem(item) {
    if (item.action === "reuse_existing" || item.action === "delete_existing") {
        return item;
    }
    const content = toSerializableWorkflowTemplateContentSource(item);
    if (item.action === "update_existing") {
        return {
            action: item.action,
            targetPath: item.targetPath,
            path: item.path,
            ...content,
            baseDigest: item.baseDigest,
            ...(item.impactSummary !== undefined
                ? { impactSummary: item.impactSummary }
                : {}),
        };
    }
    return {
        action: item.action,
        path: item.path,
        ...content,
    };
}
function toSerializableWorkflowTemplateContentSource(content) {
    return {
        description: content.description,
        requires: {
            skills: content.requiredSkills,
            mcp: content.requiredMcp,
        },
        targets: content.targets,
        autoSelect: content.autoSelect,
        body: content.body,
    };
}
async function readWorkflowTemplateDraftFile(draftFilePath, locale) {
    return parseWorkflowTemplateDraftFile(await (0, draft_file_support_1.readReusableDraftFile)(draftFilePath), locale);
}
function parseWorkflowTemplateDraftFile(text, locale) {
    return (0, draft_file_support_1.parseReusableDraftFileJson)({
        text,
        invalidDraftFileMessage: (0, catalog_1.translate)(locale, "workflow.command.template.invalidDraftFile"),
        parse: (parsedJson) => {
            const setParsed = workflowTemplateSetDraftFileSchema.safeParse(parsedJson);
            if (setParsed.success) {
                return normalizeWorkflowTemplateSetDraftFile(setParsed.data, locale);
            }
            const parsed = workflowTemplateDraftFileSchema.parse(parsedJson);
            const path = (0, workflow_template_command_support_1.normalizeWorkflowTemplateCommandPath)(parsed.path, locale);
            const base = {
                kind: parsed.kind,
                schemaVersion: exports.WORKFLOW_TEMPLATE_DRAFT_SCHEMA_VERSION,
                command: parsed.command,
                preparedAt: parsed.preparedAt,
                ...(parsed.sourcePrompt !== undefined
                    ? { sourcePrompt: parsed.sourcePrompt }
                    : {}),
                path,
            };
            if (parsed.mode === "reuse") {
                return {
                    ...base,
                    mode: "reuse",
                    reason: parsed.reason,
                };
            }
            const content = (0, workflow_template_command_support_1.normalizeWorkflowTemplateContentSource)({
                description: parsed.description,
                requiredSkills: parsed.requires.skills,
                requiredMcp: parsed.requires.mcp,
                targets: parsed.targets,
                autoSelect: parsed.autoSelect,
                body: parsed.body,
            }, locale);
            if (parsed.mode === "update") {
                return {
                    ...base,
                    ...content,
                    mode: "update",
                    baseDigest: parsed.baseDigest,
                    ...(parsed.impactSummary !== undefined
                        ? { impactSummary: parsed.impactSummary }
                        : {}),
                };
            }
            return {
                ...base,
                ...content,
                mode: "create",
            };
        },
    });
}
function normalizeWorkflowTemplateSetDraftFile(parsed, locale) {
    return {
        kind: parsed.kind,
        schemaVersion: exports.WORKFLOW_TEMPLATE_SET_DRAFT_SCHEMA_VERSION,
        command: parsed.command,
        templateSet: true,
        preparedAt: parsed.preparedAt,
        ...(parsed.sourcePrompt !== undefined
            ? { sourcePrompt: parsed.sourcePrompt }
            : {}),
        ...(parsed.anchorPath !== undefined
            ? {
                anchorPath: (0, workflow_template_command_support_1.normalizeWorkflowTemplateCommandPath)(parsed.anchorPath, locale),
            }
            : {}),
        items: parsed.items.map((item) => normalizeWorkflowTemplateDraftFileItem(item, locale)),
    };
}
function normalizeWorkflowTemplateDraftFileItem(item, locale) {
    if (item.action === "reuse_existing") {
        return {
            action: "reuse_existing",
            targetPath: (0, workflow_template_command_support_1.normalizeWorkflowTemplateCommandPath)(item.targetPath, locale),
            reason: item.reason,
        };
    }
    if (item.action === "delete_existing") {
        return {
            action: "delete_existing",
            targetPath: (0, workflow_template_command_support_1.normalizeWorkflowTemplateCommandPath)(item.targetPath, locale),
            baseDigest: item.baseDigest,
            reason: item.reason,
        };
    }
    const content = (0, workflow_template_command_support_1.normalizeWorkflowTemplateContentSource)({
        description: item.description,
        requiredSkills: item.requires.skills,
        requiredMcp: item.requires.mcp,
        targets: item.targets,
        autoSelect: item.autoSelect,
        body: item.body,
    }, locale);
    if (item.action === "update_existing") {
        return {
            action: "update_existing",
            targetPath: (0, workflow_template_command_support_1.normalizeWorkflowTemplateCommandPath)(item.targetPath, locale),
            path: (0, workflow_template_command_support_1.normalizeWorkflowTemplateCommandPath)(item.path, locale),
            ...content,
            baseDigest: item.baseDigest,
            ...(item.impactSummary !== undefined
                ? { impactSummary: item.impactSummary }
                : {}),
        };
    }
    return {
        action: "create_new",
        path: (0, workflow_template_command_support_1.normalizeWorkflowTemplateCommandPath)(item.path, locale),
        ...content,
    };
}
