"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureWorkflowTemplateUpdateModesAllowChanges = ensureWorkflowTemplateUpdateModesAllowChanges;
const catalog_1 = require("../i18n/catalog");
const template_registry_1 = require("../workflows/template-registry");
function ensureWorkflowTemplateUpdateModesAllowChanges(input) {
    for (const item of input.preparedItems) {
        if (item.action === "delete_existing") {
            continue;
        }
        const updateMode = getPreparedChangeUpdateMode(input.catalog, item);
        if (updateMode === "flexible") {
            continue;
        }
        const allowed = updateMode === "strict" &&
            input.draft.command === "update" &&
            input.draft.templateSet === false &&
            input.preparedItems.length === 1 &&
            item.action === "update_existing";
        if (allowed) {
            continue;
        }
        throw new Error((0, catalog_1.translate)(input.locale, "workflow.command.template.updateModeBlocked", {
            templatePath: item.templatePath,
            updateMode,
        }));
    }
}
function getPreparedChangeUpdateMode(catalog, item) {
    if (item.action === "create_new") {
        return template_registry_1.DEFAULT_WORKFLOW_TEMPLATE_UPDATE_MODE;
    }
    const template = catalog.templateByPath.get(item.action === "update_existing" ? item.targetPath : item.templatePath);
    return template?.updateMode ?? template_registry_1.DEFAULT_WORKFLOW_TEMPLATE_UPDATE_MODE;
}
