"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildContinuationContext = buildContinuationContext;
exports.resolvePublishSideRerunReason = resolvePublishSideRerunReason;
exports.isImplicitWorkflowRun = isImplicitWorkflowRun;
exports.collectWorkflowRequiredSkillIdsForGate = collectWorkflowRequiredSkillIdsForGate;
exports.collectWorkflowRequiredMcpRefsForGate = collectWorkflowRequiredMcpRefsForGate;
exports.resolveRouteGateAiRunAccounting = resolveRouteGateAiRunAccounting;
exports.inferWorkflowRunMode = inferWorkflowRunMode;
exports.isWorkspaceSyncBlockedError = isWorkspaceSyncBlockedError;
exports.buildWorkspaceSyncBlockedSummary = buildWorkspaceSyncBlockedSummary;
const catalog_1 = require("../../i18n/catalog");
const types_1 = require("../../worker/types");
const run_context_1 = require("../../workflows/run-context");
const persisted_state_1 = require("../../workflows/state/persisted-state");
const template_bindings_1 = require("../../workflows/template-bindings");
const default_system_workflows_1 = require("../../workflows/default-system-workflows");
const issue_logger_1 = require("../issue-logger");
function buildContinuationContext(workerExecution, reason) {
    return {
        reason,
        outcome: workerExecution.workerOutcome,
        result: workerExecution.result,
        workflowState: (0, run_context_1.sanitizeWorkerVisibleWorkflowState)(workerExecution.workflowState),
    };
}
function resolvePublishSideRerunReason(workerExecution) {
    return workerExecution.runMode === types_1.WORKER_RUN_MODES.workflowPlan
        ? "post_publish"
        : "post_push";
}
function isImplicitWorkflowRun(runMode, workflowState) {
    const binding = workflowState === null
        ? null
        : (0, template_bindings_1.getActiveWorkflowTemplateBinding)(workflowState);
    const templateName = binding === null ? null : (0, template_bindings_1.getWorkflowTemplateName)(binding);
    return ((runMode === types_1.WORKER_RUN_MODES.workflowPlan ||
        runMode === types_1.WORKER_RUN_MODES.workflowExecute) &&
        binding !== null &&
        binding.selector.mode !== "template" &&
        (templateName === null || (0, default_system_workflows_1.isDefaultSystemWorkflowTemplatePath)(templateName)));
}
function collectWorkflowRequiredSkillIdsForGate(workflow) {
    const skillIds = new Set(workflow.requiredSkills ?? []);
    return [...skillIds].sort();
}
function collectWorkflowRequiredMcpRefsForGate(workflow) {
    const mcpRefs = new Set(workflow.requiredMcp ?? []);
    return [...mcpRefs].sort();
}
function resolveRouteGateAiRunAccounting(routeGate) {
    if (isHumanInputResumeRoute(routeGate)) {
        return {
            consecutiveAiRuns: routeGate.snapshot.currentConsecutiveAiRuns,
            totalAiRunHours: routeGate.snapshot.currentTotalAiRunHours,
        };
    }
    return {
        consecutiveAiRuns: Math.max(routeGate.snapshot.currentConsecutiveAiRuns, routeGate.workflowState?.consecutiveAiRuns ?? 0),
        totalAiRunHours: Math.max(routeGate.snapshot.currentTotalAiRunHours, routeGate.workflowState?.totalAiRunHours ?? 0),
    };
}
function isHumanInputResumeRoute(routeGate) {
    return (0, persisted_state_1.isHumanInputResumeProjection)({
        snapshot: routeGate.snapshot.latestIssue.workflowState,
        workflowState: routeGate.workflowState,
    });
}
function inferWorkflowRunMode(workflowState) {
    if (workflowState === null) {
        return null;
    }
    return workflowState.plan === null
        ? types_1.WORKER_RUN_MODES.workflowPlan
        : types_1.WORKER_RUN_MODES.workflowExecute;
}
function isWorkspaceSyncBlockedError(error) {
    if (error === null || typeof error !== "object") {
        return false;
    }
    const reason = error.reason;
    return (reason === "dirty_workspace" ||
        reason === "merge_conflict" ||
        reason === "branch_diverged");
}
function buildWorkspaceSyncBlockedSummary(input) {
    const files = (0, issue_logger_1.formatList)(input.conflictedFiles);
    switch (input.reason) {
        case "dirty_workspace":
            return (0, catalog_1.translate)(input.responseLanguage, "run.workspaceSync.dirty");
        case "merge_conflict":
            return (0, catalog_1.translate)(input.responseLanguage, "run.workspaceSync.mergeConflict", {
                files,
            });
        case "branch_diverged":
            return (0, catalog_1.translate)(input.responseLanguage, "run.workspaceSync.branchDiverged");
        default:
            return (0, catalog_1.translate)(input.responseLanguage, "run.workspaceSync.dirty");
    }
}
