"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MERGE_INTEGRATION_STEP_ID = void 0;
exports.resolveAutoMergeWorkflowState = resolveAutoMergeWorkflowState;
exports.resolvePullRequestMergeReplanWorkflowState = resolvePullRequestMergeReplanWorkflowState;
const same_issue_replan_state_support_1 = require("./same-issue-replan-state-support");
exports.MERGE_INTEGRATION_STEP_ID = "merge-integration";
function resolveAutoMergeWorkflowState(input) {
    if (input.workflowState === null) {
        return null;
    }
    const nextWorkflowState = {
        ...input.workflowState,
    };
    if (input.autoMergeState !== undefined) {
        if (input.autoMergeState !== null) {
            nextWorkflowState.autoMergeState = input.autoMergeState;
        }
        else {
            delete nextWorkflowState.autoMergeState;
        }
    }
    if (input.pendingPlanningReason !== undefined) {
        return (0, same_issue_replan_state_support_1.applySameIssueReplanPlanningState)({
            workflowState: nextWorkflowState,
            reason: input.pendingPlanningReason,
            pendingPlanningSource: nextWorkflowState.pendingPlanningSource,
        });
    }
    return nextWorkflowState;
}
function resolvePullRequestMergeReplanWorkflowState(input) {
    const nextWorkflowState = resolveAutoMergeWorkflowState({
        workflowState: input.workflowState,
        autoMergeState: null,
        pendingPlanningReason: input.pendingPlanningReason,
    });
    if (nextWorkflowState === null) {
        throw new Error("Expected workflow state when resolving merge replan.");
    }
    nextWorkflowState.workflowSurface = "merge";
    delete nextWorkflowState.pendingWorkflowStartApprovalIntent;
    return withMergeIntegrationPlan((0, same_issue_replan_state_support_1.applySameIssueReplanPlanningState)({
        workflowState: nextWorkflowState,
        reason: input.pendingPlanningReason,
        pendingPlanningSource: input.pendingPlanningSource,
    }));
}
function withMergeIntegrationPlan(workflowState) {
    return {
        ...workflowState,
        executionContract: createMergeIntegrationExecutionContract(),
        plan: createMergeIntegrationPlan(),
        finalResult: null,
        executionGuidance: [
            "Use the merge context as authoritative runtime input for merge integration work.",
            "Apply repository merge guidance from system prompts when present.",
        ],
        predictedTouchedPaths: [],
    };
}
function createMergeIntegrationExecutionContract() {
    return {
        objective: "Integrate the latest base branch or dependency changes into the pull request branch.",
        instructions: [
            "Use the merge context as authoritative runtime input before editing.",
            "Apply repository merge guidance from system prompts when present.",
        ],
        doneWhen: [
            "The pull request branch includes the required base/dependency changes, or a concrete human blocker is reported.",
            "Verification evidence or the blocking reason is included in the worker result.",
        ],
        inputs: ["Merge context", "Current pull request workspace"],
        outputs: ["Updated pull request branch integration state"],
    };
}
function createMergeIntegrationPlan() {
    return {
        steps: [
            {
                id: exports.MERGE_INTEGRATION_STEP_ID,
                kind: "task",
                title: "Integrate base branch changes",
                objective: "Resolve or verify the merge integration for the pull request branch.",
                instructions: [
                    "Use the provided merge context as the source of truth for the base branch and conflict status.",
                    "Apply repository merge guidance from system prompts when present.",
                ],
                doneWhen: [
                    "The base/dependency changes are integrated into the pull request branch, or a concrete human blocker is reported.",
                    "The worker result includes verification evidence or the exact blocker.",
                ],
                humanHandoffWhen: [
                    "A conflict requires product or domain judgment that cannot be inferred from the repository.",
                    "Required verification or credentials are unavailable and no safe local alternative exists.",
                ],
                dependsOnStepIds: [],
                result: null,
            },
        ],
    };
}
