"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applySameIssueReplanPlanningState = applySameIssueReplanPlanningState;
exports.deriveSameIssueReplanWorkflowState = deriveSameIssueReplanWorkflowState;
exports.deriveSameIssueReplanTransitionWorkflowState = deriveSameIssueReplanTransitionWorkflowState;
const publisher_events_1 = require("../../workflows/publisher-events");
const pending_planning_support_1 = require("../../workflows/pending-planning-support");
function applySameIssueReplanPlanningState(input) {
    return (0, pending_planning_support_1.applyPendingPlanningState)(input.workflowState, {
        kind: "set",
        pendingPlanningIntent: "replan",
        pendingPlanningReason: input.reason,
        pendingPlanningSource: input.pendingPlanningSource,
    });
}
function deriveSameIssueReplanWorkflowState(input) {
    switch (input.kind) {
        case "run_limit_downgrade":
            return applySameIssueReplanPlanningState({
                workflowState: input.nextWorkflowState,
                reason: input.reason,
                pendingPlanningSource: input.nextWorkflowState.pendingPlanningSource,
            });
        case "worker_replan_same_issue":
            return applySameIssueReplanPlanningState({
                workflowState: input.nextWorkflowState,
                reason: input.reason,
            });
        case "dependency_completion_requeue":
            return applySameIssueReplanPlanningState({
                workflowState: input.nextWorkflowState,
                reason: input.reason,
                pendingPlanningSource: input.nextWorkflowState.pendingPlanningSource,
            });
        case "preflight_template_drift":
            return applySameIssueReplanPlanningState({
                workflowState: applyPreflightTemplateDriftCarryForward(input.nextWorkflowState, input.previousWorkflowState),
                reason: input.reason,
            });
        case "service_implicit_replan":
            return applySameIssueReplanPlanningState({
                workflowState: {
                    ...input.previousWorkflowState,
                    finalResult: null,
                    consecutiveAiRuns: 0,
                    totalAiRunHours: 0,
                    needsHumanReason: null,
                    lastScannedCommentCursor: input.triggerCursor,
                },
                reason: input.reason,
            });
        case "revise_plan":
            return (0, pending_planning_support_1.applyPendingPlanningState)({
                ...input.previousWorkflowState,
                needsHumanReason: publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.startApproval,
                consecutiveAiRuns: 0,
                totalAiRunHours: 0,
                lastScannedCommentCursor: input.triggerCursor,
            }, {
                kind: "set",
                pendingPlanningIntent: "revise_plan",
                pendingPlanningReason: input.reason,
            });
        case "replan_completion":
            return (0, pending_planning_support_1.applyPendingPlanningState)(input.nextWorkflowState, {
                kind: "clear",
            });
    }
}
function deriveSameIssueReplanTransitionWorkflowState(input) {
    return deriveSameIssueReplanWorkflowState({
        kind: input.kind,
        previousWorkflowState: input.previousWorkflowState,
        nextWorkflowState: {
            ...input.workflowState,
            consecutiveAiRuns: input.consecutiveAiRuns,
            lastScannedCommentCursor: input.lastScannedCommentCursor,
        },
        reason: input.reason,
    });
}
function applyPreflightTemplateDriftCarryForward(workflowState, previousWorkflowState) {
    return {
        ...workflowState,
        plan: previousWorkflowState.plan,
        finalResult: previousWorkflowState.finalResult ?? null,
        predictedTouchedPaths: [...previousWorkflowState.predictedTouchedPaths],
        lastRunAt: previousWorkflowState.lastRunAt ?? null,
        lastOutcome: previousWorkflowState.lastOutcome ?? null,
    };
}
