"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildCoercedPersistedWorkflowStateBase = buildCoercedPersistedWorkflowStateBase;
exports.assignCoercedPersistedWorkflowStateHierarchyFields = assignCoercedPersistedWorkflowStateHierarchyFields;
exports.assignCoercedPersistedWorkflowStateSupplementalFields = assignCoercedPersistedWorkflowStateSupplementalFields;
const types_1 = require("../types");
const coercion_support_1 = require("./coercion-support");
function buildCoercedPersistedWorkflowStateBase(input) {
    const state = {
        version: types_1.WORKFLOW_STATE_VERSION,
        workflowSurface: input.workflowSurface,
        workflowTemplates: input.workflowTemplates,
        baseBranch: input.baseBranch,
        priorityRank: input.priorityRank,
        plan: input.plan,
        planRevision: input.planRevision,
        finalResult: input.finalResult,
        ...(input.executionGuidance !== undefined
            ? { executionGuidance: [...input.executionGuidance] }
            : {}),
        predictedTouchedPaths: input.predictedTouchedPaths,
        consecutiveAiRuns: (0, coercion_support_1.coerceConsecutiveAiRuns)(input.workflowState.consecutiveAiRuns),
        totalAiRunHours: (0, coercion_support_1.coerceTotalAiRunHours)(input.workflowState.totalAiRunHours),
        lastScannedCommentCursor: input.lastScannedCommentCursor,
    };
    if (input.schedule !== null && input.schedule !== undefined) {
        state.schedule = input.schedule;
    }
    if (input.scheduledRun !== undefined) {
        state.scheduledRun = input.scheduledRun;
    }
    if (input.nextRunAt !== undefined) {
        state.nextRunAt = input.nextRunAt;
    }
    if (input.lastRunAt !== undefined) {
        state.lastRunAt = input.lastRunAt;
    }
    if (input.lastOutcome !== undefined) {
        state.lastOutcome = input.lastOutcome;
    }
    return state;
}
function assignCoercedPersistedWorkflowStateHierarchyFields(target, input) {
    if (input.workflowState.parentLink !== undefined &&
        input.parentLink !== undefined) {
        target.parentLink = {
            parentRef: input.parentLink.parentRef,
            childKey: input.parentLink.childKey,
        };
    }
    if (input.workflowState.originLink !== undefined &&
        input.originLink !== undefined) {
        target.originLink = { ...input.originLink };
    }
    target.executionContract = {
        objective: input.executionContract.objective,
        doneWhen: [...input.executionContract.doneWhen],
        inputs: [...input.executionContract.inputs],
        outputs: [...input.executionContract.outputs],
    };
    if (input.executionContract.instructions !== undefined &&
        input.executionContract.instructions.length > 0) {
        target.executionContract.instructions = [
            ...input.executionContract.instructions,
        ];
    }
}
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: Supplemental workflow-state field coercion mirrors the persisted state schema in one pass.
function assignCoercedPersistedWorkflowStateSupplementalFields(target, input) {
    if (input.workflowState.configOverrides !== undefined) {
        target.configOverrides = input.configOverrides ?? {
            entries: [],
        };
    }
    if (input.workflowState.parentContractRevision !== undefined &&
        input.parentContractRevision !== undefined) {
        target.parentContractRevision = input.parentContractRevision;
    }
    if (input.workflowState.parentStepContractSnapshot !== undefined &&
        input.parentStepContractSnapshot !== undefined) {
        target.parentStepContractSnapshot = {
            ...input.parentStepContractSnapshot,
            ...(input.parentStepContractSnapshot.instructions !== undefined
                ? { instructions: [...input.parentStepContractSnapshot.instructions] }
                : {}),
            doneWhen: [...input.parentStepContractSnapshot.doneWhen],
            inputs: [...input.parentStepContractSnapshot.inputs],
            outputs: [...input.parentStepContractSnapshot.outputs],
        };
    }
    if (input.workflowState.pendingChildIssueDecisions !== undefined &&
        input.pendingChildIssueDecisions !== undefined) {
        target.pendingChildIssueDecisions = input.pendingChildIssueDecisions.map((decision) => ({ ...decision }));
    }
    if (input.workflowState.detachedFromParentReason !== undefined) {
        target.detachedFromParentReason = input.detachedFromParentReason ?? null;
    }
    if (input.workflowState.cancelledChildReason !== undefined) {
        target.cancelledChildReason = input.cancelledChildReason ?? null;
    }
    if (input.workflowState.dependencySyncState !== undefined) {
        target.dependencySyncState = input.dependencySyncState ?? {
            managedDependencies: [],
            appliedAutoBlockers: [],
        };
    }
    if (input.workflowState.executionOwnership !== undefined &&
        input.executionOwnership !== undefined &&
        input.executionOwnership !== null) {
        target.executionOwnership = input.executionOwnership;
    }
    if (input.workflowState.foreignInputApproval !== undefined &&
        input.foreignInputApproval !== undefined &&
        input.foreignInputApproval !== null) {
        target.foreignInputApproval = input.foreignInputApproval;
    }
    if (input.workflowState.needsHumanReason !== undefined) {
        target.needsHumanReason = input.needsHumanReason ?? null;
    }
    if (input.workflowState.pendingWorkflowStartApprovalIntent !== undefined) {
        if (input.pendingWorkflowStartApprovalIntent !== undefined) {
            target.pendingWorkflowStartApprovalIntent =
                input.pendingWorkflowStartApprovalIntent;
        }
    }
    if (input.workflowState.pendingPlanningIntent !== undefined) {
        if (input.pendingPlanningIntent !== undefined) {
            target.pendingPlanningIntent = input.pendingPlanningIntent;
        }
    }
    if (input.pendingPlanningReason !== undefined) {
        target.pendingPlanningReason = input.pendingPlanningReason ?? null;
    }
    if (input.workflowState.pendingPlanningSource !== undefined &&
        input.pendingPlanningSource !== undefined) {
        target.pendingPlanningSource = input.pendingPlanningSource;
    }
    if (input.workflowState.autoMergeState !== undefined &&
        input.autoMergeState !== undefined) {
        target.autoMergeState = input.autoMergeState;
    }
    assignCoercedMergeContext(target, input);
    if (input.workflowState.pullRequestReviewNecessity !== undefined &&
        input.pullRequestReviewNecessity !== undefined) {
        target.pullRequestReviewNecessity = input.pullRequestReviewNecessity;
    }
    if (input.followup !== undefined) {
        target.followup = input.followup;
    }
    if (input.workflowState.recurring !== undefined &&
        input.recurring !== undefined &&
        input.recurring !== null) {
        target.recurring = input.recurring;
    }
}
function assignCoercedMergeContext(target, input) {
    if (input.workflowState.mergeContext === undefined ||
        input.mergeContext === undefined) {
        return;
    }
    target.mergeContext = {
        ...input.mergeContext,
        conflictedFiles: [...input.mergeContext.conflictedFiles],
    };
}
