"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatWorkflowTemplateValidationSummary = formatWorkflowTemplateValidationSummary;
exports.formatWorkflowTemplateValidationReport = formatWorkflowTemplateValidationReport;
const catalog_1 = require("../i18n/catalog");
function formatWorkflowTemplateValidationSummary(input) {
    const locale = input.locale ?? catalog_1.DEFAULT_APP_LOCALE;
    const maxDiagnostics = input.maxDiagnostics ?? input.diagnostics.length;
    const shownDiagnostics = input.diagnostics.slice(0, maxDiagnostics);
    const lines = [
        input.templatePath
            ? (0, catalog_1.translate)(locale, "workflow.validation.summary.templateInvalid", {
                templatePath: input.templatePath,
            })
            : (0, catalog_1.translate)(locale, "workflow.validation.summary.catalogInvalid"),
    ];
    if (shownDiagnostics.length < input.diagnostics.length) {
        lines.push((0, catalog_1.translate)(locale, "workflow.validation.summary.shownCount", {
            shownCount: shownDiagnostics.length,
            totalCount: input.diagnostics.length,
        }));
    }
    for (const diagnostic of shownDiagnostics) {
        lines.push(formatWorkflowTemplateValidationDiagnostic(diagnostic, locale));
    }
    if (input.includeLintHint) {
        lines.push(input.templatePath
            ? (0, catalog_1.translate)(locale, "workflow.validation.summary.lintHint.target", {
                templatePath: input.templatePath,
            })
            : (0, catalog_1.translate)(locale, "workflow.validation.summary.lintHint.all"));
    }
    return lines.join("\n");
}
function formatWorkflowTemplateValidationReport(input) {
    const locale = input.locale ?? catalog_1.DEFAULT_APP_LOCALE;
    if (input.diagnostics.length === 0) {
        return input.templatePath
            ? (0, catalog_1.translate)(locale, "workflow.validation.summary.cleanTemplate", {
                templatePath: input.templatePath,
            })
            : (0, catalog_1.translate)(locale, "workflow.validation.summary.cleanCatalog");
    }
    return formatWorkflowTemplateValidationSummary({
        locale,
        diagnostics: input.diagnostics,
        ...(input.templatePath !== undefined
            ? { templatePath: input.templatePath }
            : {}),
    });
}
function formatWorkflowTemplateValidationDiagnostic(diagnostic, locale) {
    const lineNumber = diagnostic.sourceLineNumber ?? diagnostic.lineNumber;
    const location = diagnostic.columnNumber
        ? `${diagnostic.templatePath}:${lineNumber}:${diagnostic.columnNumber}`
        : `${diagnostic.templatePath}:${lineNumber}`;
    const message = resolveDiagnosticMessage(diagnostic, locale);
    const suggestedFix = resolveSuggestedFix(diagnostic, locale);
    return suggestedFix
        ? `${location} [${diagnostic.code}] ${message} ${(0, catalog_1.translate)(locale, "workflow.validation.fixPrefix", {
            fix: suggestedFix,
        })}`
        : `${location} [${diagnostic.code}] ${message}`;
}
function resolveDiagnosticMessage(diagnostic, locale) {
    switch (diagnostic.code) {
        case "nested.invalid-inline-directive":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.invalidInline");
        case "nested.invalid-trailing-text":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.invalidTrailingText");
        case "nested.invalid-path":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.invalidPath", {
                templatePath: diagnostic.referencedTemplatePath ?? "",
            });
        case "nested.duplicate-template":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.duplicateTemplate", {
                templatePath: diagnostic.referencedTemplatePath ?? "",
            });
        case "nested.missing-template":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.missingTemplate", {
                templatePath: diagnostic.referencedTemplatePath ?? "",
            });
        case "nested.surface-mismatch":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.surfaceMismatch", {
                templatePath: diagnostic.referencedTemplatePath ?? "",
            });
        case "nested.cycle":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.cycle", {
                templatePath: diagnostic.referencedTemplatePath ?? "",
                path: diagnostic.cyclePath?.join(" -> ") ?? "",
            });
        case "nested.depth-limit":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.depthLimit", {
                templatePath: diagnostic.referencedTemplatePath ?? "",
                depth: diagnostic.depth ?? 0,
                maxDepth: diagnostic.maxDepth ?? 0,
            });
        case "structure.empty-body":
            return (0, catalog_1.translate)(locale, "workflow.validation.diag.emptyBody");
    }
    return diagnostic.code;
}
function resolveSuggestedFix(diagnostic, locale) {
    switch (diagnostic.code) {
        case "nested.invalid-inline-directive":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.useStandalone");
        case "nested.invalid-trailing-text":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.removeTrailingText");
        case "nested.invalid-path":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.useValidPath");
        case "nested.duplicate-template":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.removeDuplicateTemplate");
        case "nested.missing-template":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.createTemplate");
        case "nested.surface-mismatch":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.useSameSurface");
        case "nested.cycle":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.breakCycle");
        case "nested.depth-limit":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.reduceDepth");
        case "structure.empty-body":
            return (0, catalog_1.translate)(locale, "workflow.validation.fix.addBodyContent");
    }
    return null;
}
