"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkflowCatalogErrorSummary = buildWorkflowCatalogErrorSummary;
exports.buildInvalidWorkflowCommandSummary = buildInvalidWorkflowCommandSummary;
exports.buildMissingWorkflowTemplateSummary = buildMissingWorkflowTemplateSummary;
exports.buildUnavailableWorkerConfigOverrideSummary = buildUnavailableWorkerConfigOverrideSummary;
exports.buildWorkflowLinkResolutionSummary = buildWorkflowLinkResolutionSummary;
exports.buildNestedWorkflowChildHandoffSummary = buildNestedWorkflowChildHandoffSummary;
exports.buildNestedWorkflowExpansionSummary = buildNestedWorkflowExpansionSummary;
const catalog_1 = require("../i18n/catalog");
const template_registry_1 = require("./template-registry");
const validation_1 = require("./validation");
function buildWorkflowCatalogErrorSummary(error, locale) {
    if (error instanceof template_registry_1.WorkflowTemplateCatalogError) {
        return [
            (0, catalog_1.translate)(locale, "workflow.runContext.catalogInvalid"),
            formatWorkflowCatalogError(error, locale),
        ].join("\n");
    }
    if (error instanceof Error) {
        return `${(0, catalog_1.translate)(locale, "workflow.runContext.catalogLoadFailed")}\n${error.message}`;
    }
    return (0, catalog_1.translate)(locale, "workflow.runContext.catalogLoadFailed");
}
function formatWorkflowCatalogError(error, locale) {
    if (error.kind === "duplicate-template-id") {
        const { templatePath = "", firstFile = "", secondFile = "", } = error.details;
        return (0, catalog_1.translate)(locale, "workflow.templateHealth.duplicateId", {
            templatePath,
            firstFile,
            secondFile,
        });
    }
    const { fileName, reasonKey } = error.details;
    if (error.kind === "invalid-template-name" && fileName) {
        return (0, catalog_1.translate)(locale, "workflow.templateHealth.invalidId", {
            fileName,
            reason: (0, catalog_1.translate)(locale, reasonKey ?? "workflow.templateHealth.invalidIdentifier"),
        });
    }
    return error.message;
}
function buildInvalidWorkflowCommandSummary(resolution, locale) {
    const reason = (0, catalog_1.translate)(locale, resolveInvalidCommandReasonKey(resolution.command.reason));
    return [
        reason,
        (0, catalog_1.translate)(locale, "workflow.runContext.invalidCommand.usage"),
        (0, catalog_1.translate)(locale, "workflow.runContext.available", {
            value: formatWorkflowTemplateIdList(resolution.availableTemplatePaths, locale),
        }),
    ].join("\n");
}
function resolveInvalidCommandReasonKey(reason) {
    switch (reason) {
        case "missing-options":
            return "workflow.runContext.invalidCommand.missingOptions";
        case "missing-option-value":
            return "workflow.runContext.invalidCommand.missingOptionValue";
        case "invalid-option-value":
            return "workflow.runContext.invalidCommand.invalidOptionValue";
        case "duplicate-option":
            return "workflow.runContext.invalidCommand.duplicateOption";
        case "conflicting-schedule-options":
            return "workflow.runContext.invalidCommand.conflictingScheduleOptions";
        case "unsupported-command-body":
            return "workflow.runContext.invalidCommand.unsupportedCommandBody";
        default:
            return "workflow.runContext.invalidCommand.unexpectedArgument";
    }
}
function buildMissingWorkflowTemplateSummary(resolution, locale) {
    const lines = [
        (0, catalog_1.translate)(locale, "workflow.runContext.missingTemplate", {
            templatePath: resolution.missingTemplatePath,
        }),
        (0, catalog_1.translate)(locale, "workflow.runContext.available", {
            value: formatWorkflowTemplateIdList(resolution.availableTemplatePaths, locale),
        }),
    ];
    if (resolution.suggestions.length > 0) {
        lines.push((0, catalog_1.translate)(locale, "workflow.runContext.didYouMean", {
            value: formatWorkflowTemplateIdList(resolution.suggestions, locale),
        }));
    }
    return lines.join("\n");
}
function buildUnavailableWorkerConfigOverrideSummary(locale, worker) {
    return (0, catalog_1.translate)(locale, "workflow.runContext.configOverride.workerMissing", {
        worker,
    });
}
function formatWorkflowTemplateIdList(templatePaths, locale) {
    if (templatePaths.length === 0) {
        return (0, catalog_1.translate)(locale, "workflow.runContext.none");
    }
    return templatePaths.map((templatePath) => `\`${templatePath}\``).join(", ");
}
function buildWorkflowLinkResolutionSummary(error, locale) {
    if (error instanceof Error && error.message.trim().length > 0) {
        return `${(0, catalog_1.translate)(locale, "workflow.runContext.linkResolutionFailed")}\n${error.message}`;
    }
    return (0, catalog_1.translate)(locale, "workflow.runContext.linkResolutionFailed");
}
function buildNestedWorkflowChildHandoffSummary(detail, parentStepTitle, childTemplateName, locale) {
    const prefix = (0, catalog_1.translate)(locale, "workflow.runContext.childWorkflowHandoffPrefix", {
        parentStepTitle,
        childTemplateName,
    });
    return `${prefix}\n${detail}`;
}
function buildNestedWorkflowExpansionSummary(expansion, templatePath, locale) {
    return (0, validation_1.formatWorkflowTemplateValidationSummary)({
        locale,
        diagnostics: expansion.diagnostics,
        templatePath,
        maxDiagnostics: 10,
        includeLintHint: true,
    });
}
