"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkWorkflowTemplates = checkWorkflowTemplates;
const branding_1 = require("../core/branding");
const catalog_1 = require("../i18n/catalog");
const check_names_1 = require("../commands/doctor/check-names");
const template_registry_1 = require("./template-registry");
const validation_1 = require("./validation");
const control_plane_config_1 = require("../config/control-plane-config");
const required_skills_1 = require("./required-skills");
const required_mcp_1 = require("./required-mcp");
const required_capability_intersection_1 = require("./required-capability-intersection");
const required_capability_reports_1 = require("./required-capability-reports");
async function checkWorkflowTemplates(cwd, locale, options = {}) {
    const workflowRoot = (0, template_registry_1.getWorkflowTemplatesDirectoryPath)(cwd);
    try {
        const catalog = await (0, template_registry_1.loadWorkflowTemplates)(workflowRoot, locale);
        const diagnostics = (0, validation_1.validateWorkflowTemplateCatalog)({
            catalog,
            maxNestedWorkflowDepth: options.maxNestedWorkflowDepth ?? control_plane_config_1.DEFAULT_CONFIG.maxNestedWorkflowDepth,
        });
        if (diagnostics.length > 0) {
            return {
                name: (0, check_names_1.getDoctorCheckName)("workflowTemplates", locale),
                status: "fail",
                message: (0, validation_1.formatWorkflowTemplateValidationSummary)({
                    locale,
                    diagnostics,
                    maxDiagnostics: 10,
                    includeLintHint: true,
                }),
            };
        }
        const requiredSkillReport = await (0, required_skills_1.checkRequiredSkillAvailability)({
            repoRoot: cwd,
            workers: options.workers ?? [...control_plane_config_1.DEFAULT_CONFIG.workers.default],
            skillRefs: catalog.templates.flatMap((template) => template.metadata?.requiredSkills ?? []),
        });
        const requiredMcpRefs = catalog.templates.flatMap((template) => template.metadata?.requiredMcp ?? []);
        let requiredMcpReport = null;
        let capabilityIntersectionReport = null;
        if (requiredMcpRefs.length > 0) {
            requiredMcpReport = await (0, required_mcp_1.checkRequiredMcpAvailability)({
                repoRoot: cwd,
                workers: options.workers ?? [...control_plane_config_1.DEFAULT_CONFIG.workers.default],
                mcpRefs: requiredMcpRefs,
            });
            capabilityIntersectionReport = (0, required_capability_intersection_1.checkRequiredCapabilityWorkerIntersection)({
                skillReport: requiredSkillReport,
                mcpReport: requiredMcpReport,
            });
        }
        const requirementWarnings = (0, required_capability_reports_1.formatRequiredCapabilityMessages)({
            requiredSkillReport,
            requiredMcpReport,
            capabilityIntersectionReport,
            locale,
        });
        if (requirementWarnings.length > 0) {
            return {
                name: (0, check_names_1.getDoctorCheckName)("workflowTemplates", locale),
                status: "warning",
                message: requirementWarnings.join("\n"),
            };
        }
        return {
            name: (0, check_names_1.getDoctorCheckName)("workflowTemplates", locale),
            status: "ok",
            message: (0, catalog_1.translate)(locale, "workflow.templateHealth.configuredSummary", {
                directory: branding_1.WORKFLOW_TEMPLATES_DIRECTORY,
                count: String(catalog.templates.length),
            }),
        };
    }
    catch (error) {
        return {
            name: (0, check_names_1.getDoctorCheckName)("workflowTemplates", locale),
            status: "fail",
            message: error instanceof Error
                ? error.message
                : (0, catalog_1.translate)(locale, "workflow.templateHealth.readFailed"),
        };
    }
}
