"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sanitizeWorkerVisibleWorkflowState = exports.resolveWorkflowPlanningContextForSelection = exports.resolveNestedWorkflowPlanningContext = void 0;
exports.resolveWorkflowRunContext = resolveWorkflowRunContext;
const control_plane_config_1 = require("../config/control-plane-config");
const catalog_1 = require("../i18n/catalog");
const run_context_planning_1 = require("./run-context-planning");
const run_context_prepare_1 = require("./run-context-prepare");
const run_context_route_1 = require("./run-context-route");
var run_context_planning_2 = require("./run-context-planning");
Object.defineProperty(exports, "resolveNestedWorkflowPlanningContext", { enumerable: true, get: function () { return run_context_planning_2.resolveNestedWorkflowPlanningContext; } });
Object.defineProperty(exports, "resolveWorkflowPlanningContextForSelection", { enumerable: true, get: function () { return run_context_planning_2.resolveWorkflowPlanningContextForSelection; } });
var run_context_prepare_2 = require("./run-context-prepare");
Object.defineProperty(exports, "sanitizeWorkerVisibleWorkflowState", { enumerable: true, get: function () { return run_context_prepare_2.sanitizeWorkerVisibleWorkflowState; } });
async function resolveWorkflowRunContext(repoRoot, comments, locale = catalog_1.DEFAULT_APP_LOCALE, options = {}) {
    const maxNestedWorkflowDepth = options.maxNestedWorkflowDepth ?? control_plane_config_1.DEFAULT_CONFIG.maxNestedWorkflowDepth;
    const prepared = await (0, run_context_prepare_1.prepareWorkflowRoutingInputs)({
        repoRoot,
        comments,
        locale,
        issueBody: options.issueBody ?? null,
        bootstrapWorkflowState: options.bootstrapWorkflowState ?? null,
        workflowSurface: options.workflowSurface ?? "issue",
        foreignInputApprovalAssignee: options.foreignInputApprovalAssignee ?? null,
        baseBranchDefault: options.baseBranchDefault ?? control_plane_config_1.DEFAULT_CONFIG.repositoryHost.baseBranch,
        workflowTemplateDefaults: options.workflowTemplateDefaults ??
            (options.workflowStartApprovalConfig !== undefined
                ? (0, control_plane_config_1.resolveWorkflowTemplateDefaultsWithFollowup)(options.workflowStartApprovalConfig)
                : undefined) ??
            control_plane_config_1.DEFAULT_CONFIG.workflowTemplateDefaults,
        followupConfig: options.followupConfig ??
            options.workflowStartApprovalConfig?.followup ??
            control_plane_config_1.DEFAULT_CONFIG.followup,
    });
    if ("kind" in prepared) {
        return prepared;
    }
    const route = (0, run_context_route_1.resolveWorkflowRunRoute)({
        prepared,
    });
    const baseConfig = options.workflowStartApprovalConfig ?? control_plane_config_1.DEFAULT_CONFIG;
    const effectiveConfig = (0, control_plane_config_1.resolveIssueEffectiveConfigForWorkflowState)(baseConfig, prepared.appliedCommandState.configOverrides !== undefined
        ? {
            baseBranch: prepared.normalizedStickyState?.baseBranch ??
                baseConfig.repositoryHost.baseBranch,
            configOverrides: prepared.appliedCommandState.configOverrides,
        }
        : prepared.normalizedStickyState);
    const issueCompletion = options.issueCompletion ?? effectiveConfig.issueCompletion;
    return (0, run_context_planning_1.resolveWorkflowRunRouteResult)({
        repoRoot,
        locale,
        maxNestedWorkflowDepth,
        followupMaxIssuesPerSource: options.workflowStartApprovalConfig?.followup.maxIssuesPerSource ??
            control_plane_config_1.DEFAULT_CONFIG.followup.maxIssuesPerSource,
        issueNotesPath: effectiveConfig.paths.issueNotes,
        prepared,
        route,
        issueCompletion,
        ...(options.workflowStartApprovalConfig !== undefined
            ? { workflowStartApprovalConfig: options.workflowStartApprovalConfig }
            : {}),
        ...(options.workflowStartApprovalTarget !== undefined
            ? { workflowStartApprovalTarget: options.workflowStartApprovalTarget }
            : {}),
    });
}
