"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runWorkflowUpdateCommand = runWorkflowUpdateCommand;
const catalog_1 = require("../i18n/catalog");
const template_registry_1 = require("../workflows/template-registry");
const prompts_1 = require("./prompts");
const workflow_template_authoring_1 = require("./workflow-template-authoring");
const workflow_template_mutation_1 = require("./workflow-template-mutation");
async function runWorkflowUpdateCommand(cwd, args, locale, deps = {}) {
    return (0, workflow_template_mutation_1.runWorkflowTemplateMutationCommand)({
        cwd,
        locale,
        command: "update",
        args,
        validateInitialCatalog: false,
        ...(deps.homeDirectory !== undefined
            ? { homeDirectory: deps.homeDirectory }
            : {}),
        resolveDraft: async ({ config, catalog, maxNestedWorkflowDepth }, mutationArgs) => {
            const resolvedArgs = await resolveWorkflowUpdatePromptArgs({
                args: mutationArgs,
                catalog,
                locale,
                prompter: deps.prompter ?? (0, prompts_1.createCliPrompter)({ locale }),
            });
            return (0, workflow_template_authoring_1.resolveWorkflowTemplateUpdateDraft)({
                cwd,
                config,
                catalog,
                args: resolvedArgs,
                locale,
                maxNestedWorkflowDepth,
            });
        },
    });
}
async function resolveWorkflowUpdatePromptArgs(input) {
    if (input.args.workflowTemplatePath !== undefined) {
        return {
            ...input.args,
            workflowTemplatePath: input.args.workflowTemplatePath,
        };
    }
    if (!input.prompter.interactive) {
        throw new Error((0, catalog_1.translate)(input.locale, "cli.parser.missingRequiredOption", {
            option: "--path",
        }));
    }
    const templates = getSelectableWorkflowUpdateTemplates(input.catalog);
    const [defaultTemplate] = templates;
    if (!defaultTemplate) {
        throw new Error((0, catalog_1.translate)(input.locale, "workflow.command.update.none"));
    }
    const selectedPath = await input.prompter.select({
        label: (0, catalog_1.translate)(input.locale, "workflow.command.update.prompt.template"),
        options: templates.map((template) => ({
            value: template.templatePath,
            label: formatWorkflowUpdateTemplateOption(template),
        })),
        defaultValue: defaultTemplate.templatePath,
    });
    return {
        ...input.args,
        workflowTemplatePath: selectedPath,
    };
}
function getSelectableWorkflowUpdateTemplates(catalog) {
    const templates = catalog.templates.filter((template) => {
        const updateMode = template.updateMode ?? template_registry_1.DEFAULT_WORKFLOW_TEMPLATE_UPDATE_MODE;
        return updateMode === "flexible";
    });
    const userTemplates = templates.filter((template) => !(0, template_registry_1.isSystemWorkflowTemplatePath)(template.templatePath));
    return userTemplates.length > 0 ? userTemplates : templates;
}
function formatWorkflowUpdateTemplateOption(template) {
    const targets = (0, template_registry_1.resolveWorkflowTemplateRecordTargets)(template).join(",");
    const description = template.metadata?.description;
    return description
        ? `${template.templatePath} [${targets}] - ${description}`
        : `${template.templatePath} [${targets}]`;
}
