"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveRequiredMcpPreWorkerGate = resolveRequiredMcpPreWorkerGate;
const control_plane_config_1 = require("../../config/control-plane-config");
const runtime_failover_1 = require("../../worker/runtime-failover");
const required_mcp_1 = require("../../workflows/required-mcp");
const required_skills_1 = require("../../workflows/required-skills");
const required_capability_intersection_1 = require("../../workflows/required-capability-intersection");
const preflight_resolution_marker_1 = require("./preflight-resolution-marker");
const preflight_resolution_plans_1 = require("./preflight-resolution-plans");
const run_state_helpers_1 = require("./run-state-helpers");
async function resolveRequiredMcpPreWorkerGate(input) {
    if ((0, preflight_resolution_marker_1.isPreflightResolutionIssue)(input.issue)) {
        return null;
    }
    if (input.linkedIssue !== undefined &&
        input.linkedIssue !== null &&
        (0, preflight_resolution_marker_1.isPreflightResolutionIssue)(input.linkedIssue)) {
        return null;
    }
    const requiredMcpRefs = (0, run_state_helpers_1.collectWorkflowRequiredMcpRefsForGate)(input.workflow);
    if (requiredMcpRefs.length === 0) {
        return null;
    }
    const report = await (0, required_mcp_1.checkRequiredMcpAvailability)({
        repoRoot: input.repoRoot,
        workers: (0, control_plane_config_1.resolveWorkersForPurpose)(input.effectiveConfig, (0, runtime_failover_1.resolveWorkerRuntimePurposeForWorkflowMode)(input.workflow.mode)),
        mcpRefs: requiredMcpRefs,
    });
    if (!report.ok) {
        return buildRequiredMcpGate({
            input,
            summary: (0, required_mcp_1.formatRequiredMcpAvailabilityReport)({
                report,
                locale: input.effectiveConfig.i18n.defaultLanguage,
            }),
            requiredMcpRefs: report.runnableWorkers.length === 0
                ? report.mcpRefs
                : [...new Set(report.missing.map((entry) => entry.mcpRef))].sort(),
        });
    }
    const requiredSkillRefs = (0, run_state_helpers_1.collectWorkflowRequiredSkillIdsForGate)(input.workflow);
    if (requiredSkillRefs.length === 0) {
        return null;
    }
    const skillReport = await (0, required_skills_1.checkRequiredSkillAvailability)({
        repoRoot: input.repoRoot,
        workers: (0, control_plane_config_1.resolveWorkersForPurpose)(input.effectiveConfig, (0, runtime_failover_1.resolveWorkerRuntimePurposeForWorkflowMode)(input.workflow.mode)),
        skillRefs: requiredSkillRefs,
    });
    const intersectionReport = (0, required_capability_intersection_1.checkRequiredCapabilityWorkerIntersection)({
        skillReport,
        mcpReport: report,
    });
    if (intersectionReport.ok) {
        return null;
    }
    return buildRequiredMcpGate({
        input,
        summary: (0, required_capability_intersection_1.formatRequiredCapabilityWorkerIntersectionReport)({
            report: intersectionReport,
            locale: input.effectiveConfig.i18n.defaultLanguage,
        }),
        requiredMcpRefs: report.mcpRefs,
    });
}
function buildRequiredMcpGate(input) {
    if (input.input.effectiveConfig.approvals.requirementsSetup.mcp === "confirm") {
        return { kind: "handoff", summary: input.summary };
    }
    return {
        kind: "preflight",
        summary: input.summary,
        resolution: (0, preflight_resolution_plans_1.buildRequiredMcpPreflightResolution)({
            parentIssue: input.input.issue,
            workflowTemplateName: input.input.workflowTemplateName,
            requiredMcpRefs: input.requiredMcpRefs,
            summary: input.summary,
            locale: input.input.effectiveConfig.i18n.defaultLanguage,
        }),
    };
}
