"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildPrePlanWorkflowState = buildPrePlanWorkflowState;
exports.buildSelectionTransitionWorkflowState = buildSelectionTransitionWorkflowState;
const hierarchical_state_transition_1 = require("./hierarchical-state-transition");
const build_1 = require("./state/build");
const workflow_contract_1 = require("./workflow-contract");
function buildPrePlanWorkflowState(input) {
    const state = (0, build_1.buildPersistedWorkflowState)(buildPrePlanWorkflowStateInput(input));
    return state;
}
function buildSelectionTransitionWorkflowState(input) {
    const workflowStateInput = {
        previousState: input.previousState,
        workflowSurface: input.workflowSurface ?? input.previousState?.workflowSurface ?? "issue",
        workflowTemplates: input.workflowTemplates,
        predictedTouchedPaths: input.predictedTouchedPaths ??
            (isIncrementalPlanningIntent(input.pendingPlanningIntent)
                ? (input.previousState?.predictedTouchedPaths ?? [])
                : []),
    };
    const baseBranch = input.baseBranch ?? input.previousState?.baseBranch;
    if (baseBranch !== undefined) {
        workflowStateInput.baseBranch = baseBranch;
    }
    if (input.schedule !== undefined) {
        workflowStateInput.schedule = input.schedule;
    }
    if (input.scheduledRun !== undefined) {
        workflowStateInput.scheduledRun = input.scheduledRun;
    }
    if (input.recurring !== undefined) {
        workflowStateInput.recurring = input.recurring;
    }
    else if (input.schedule !== undefined && input.schedule.mode !== "off") {
        workflowStateInput.recurring = input.previousState?.recurring ?? {
            kind: "parent",
        };
    }
    if (input.configOverrides !== undefined) {
        workflowStateInput.configOverrides = input.configOverrides;
    }
    if (input.foreignInputApproval !== undefined) {
        workflowStateInput.foreignInputApproval = input.foreignInputApproval;
    }
    if (input.pendingWorkflowStartApprovalIntent !== undefined) {
        workflowStateInput.pendingWorkflowStartApprovalIntent =
            input.pendingWorkflowStartApprovalIntent;
    }
    if (input.pendingPlanningIntent !== undefined) {
        workflowStateInput.pendingPlanningIntent = input.pendingPlanningIntent;
    }
    if (input.pendingPlanningReason !== undefined) {
        workflowStateInput.pendingPlanningReason = input.pendingPlanningReason;
    }
    if (input.pendingPlanningSource !== undefined) {
        workflowStateInput.pendingPlanningSource = input.pendingPlanningSource;
    }
    if (input.plan !== undefined) {
        workflowStateInput.plan = input.plan;
    }
    if (input.mergeContext !== undefined) {
        workflowStateInput.mergeContext = input.mergeContext;
    }
    if (input.followup !== undefined) {
        workflowStateInput.followup = input.followup;
    }
    if (input.consecutiveAiRuns !== undefined) {
        workflowStateInput.consecutiveAiRuns = input.consecutiveAiRuns;
    }
    if (input.totalAiRunHours !== undefined) {
        workflowStateInput.totalAiRunHours = input.totalAiRunHours;
    }
    if (input.lastScannedCommentCursor !== undefined) {
        workflowStateInput.lastScannedCommentCursor =
            input.lastScannedCommentCursor;
    }
    return buildPrePlanWorkflowState(workflowStateInput);
}
function buildPrePlanWorkflowStateInput(input) {
    const previousState = input.previousState;
    const workflowStateInput = {
        workflowSurface: input.workflowSurface ?? previousState?.workflowSurface ?? "issue",
        workflowTemplates: input.workflowTemplates,
        baseBranch: input.baseBranch ?? previousState?.baseBranch ?? "main",
        priorityRank: resolvePrePlanCarryForward(input.priorityRank, previousState?.priorityRank),
        nextRunAt: input.schedule?.mode === "off"
            ? null
            : resolvePrePlanCarryForward(input.nextRunAt, previousState?.nextRunAt),
        lastRunAt: resolvePrePlanCarryForward(input.lastRunAt, previousState?.lastRunAt),
        lastOutcome: resolvePrePlanCarryForward(input.lastOutcome, previousState?.lastOutcome),
        executionContract: previousState === null
            ? (0, workflow_contract_1.createDefaultWorkflowExecutionContract)()
            : previousState.executionContract,
        plan: resolvePrePlanPlan({
            previousState,
            ...(input.pendingPlanningIntent !== undefined
                ? { pendingPlanningIntent: input.pendingPlanningIntent }
                : {}),
            ...(input.plan !== undefined ? { plan: input.plan } : {}),
        }),
        predictedTouchedPaths: input.predictedTouchedPaths ?? [],
        consecutiveAiRuns: resolvePrePlanCarryForwardCount(input.consecutiveAiRuns, previousState?.consecutiveAiRuns),
        totalAiRunHours: resolvePrePlanCarryForwardCount(input.totalAiRunHours, previousState?.totalAiRunHours),
        lastScannedCommentCursor: resolvePrePlanCarryForward(input.lastScannedCommentCursor, previousState?.lastScannedCommentCursor),
        ...(0, hierarchical_state_transition_1.buildWorkflowHierarchicalStateFields)({
            previousState,
        }),
    };
    if (input.schedule !== undefined) {
        workflowStateInput.schedule = input.schedule;
    }
    else if (previousState?.schedule !== undefined) {
        workflowStateInput.schedule = previousState.schedule;
    }
    if (input.scheduledRun !== undefined && input.scheduledRun !== null) {
        workflowStateInput.scheduledRun = input.scheduledRun;
    }
    else if (input.scheduledRun === undefined &&
        previousState?.scheduledRun !== undefined) {
        workflowStateInput.scheduledRun = previousState.scheduledRun;
    }
    if (input.recurring !== undefined && input.recurring !== null) {
        workflowStateInput.recurring = input.recurring;
    }
    else if (input.recurring === undefined &&
        previousState?.recurring !== undefined &&
        input.schedule?.mode !== "off" &&
        input.scheduledRun === undefined) {
        workflowStateInput.recurring = previousState.recurring;
    }
    if (input.configOverrides !== undefined) {
        workflowStateInput.configOverrides = input.configOverrides;
    }
    if (input.foreignInputApproval !== undefined) {
        workflowStateInput.foreignInputApproval = input.foreignInputApproval;
    }
    const needsHumanReason = input.needsHumanReason !== undefined
        ? input.needsHumanReason
        : previousState?.needsHumanReason;
    if (needsHumanReason !== undefined) {
        workflowStateInput.needsHumanReason = needsHumanReason;
    }
    const pendingWorkflowStartApprovalIntent = resolvePrePlanOptionalCarryForward(input.pendingWorkflowStartApprovalIntent, previousState?.pendingWorkflowStartApprovalIntent);
    if (pendingWorkflowStartApprovalIntent !== undefined) {
        workflowStateInput.pendingWorkflowStartApprovalIntent =
            pendingWorkflowStartApprovalIntent;
    }
    const pendingPlanningIntent = resolvePrePlanOptionalCarryForward(input.pendingPlanningIntent, previousState?.pendingPlanningIntent);
    if (pendingPlanningIntent !== undefined) {
        workflowStateInput.pendingPlanningIntent = pendingPlanningIntent;
    }
    const pendingPlanningReason = resolvePrePlanOptionalCarryForward(input.pendingPlanningReason, previousState?.pendingPlanningReason);
    if (pendingPlanningReason !== undefined) {
        workflowStateInput.pendingPlanningReason = pendingPlanningReason;
    }
    const pendingPlanningSource = resolvePrePlanOptionalCarryForward(input.pendingPlanningSource, previousState?.pendingPlanningSource);
    if (pendingPlanningSource !== undefined) {
        workflowStateInput.pendingPlanningSource = pendingPlanningSource;
    }
    const mergeContext = resolvePrePlanOptionalCarryForward(input.mergeContext, previousState?.mergeContext);
    if (mergeContext !== undefined) {
        workflowStateInput.mergeContext = mergeContext;
    }
    const followup = resolvePrePlanOptionalCarryForward(input.followup, previousState?.followup);
    if (followup !== undefined) {
        workflowStateInput.followup = followup;
    }
    return workflowStateInput;
}
function resolvePrePlanPlan(input) {
    if (input.plan !== undefined) {
        return input.plan;
    }
    return isIncrementalPlanningIntent(input.pendingPlanningIntent)
        ? (input.previousState?.plan ?? null)
        : null;
}
function isIncrementalPlanningIntent(intent) {
    return intent === "replan" || intent === "revise_plan";
}
function resolvePrePlanCarryForward(value, previousValue) {
    return value ?? previousValue ?? null;
}
function resolvePrePlanOptionalCarryForward(value, previousValue) {
    return value ?? previousValue;
}
function resolvePrePlanCarryForwardCount(value, previousValue) {
    return value ?? previousValue ?? 0;
}
