"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addAiRunDurationToTotal = addAiRunDurationToTotal;
exports.resolveAttemptedAiRunAccounting = resolveAttemptedAiRunAccounting;
exports.applyAiRunAccountingToWorkflowState = applyAiRunAccountingToWorkflowState;
exports.resolveWorkerExecutionTotalAiRunHours = resolveWorkerExecutionTotalAiRunHours;
const AI_RUN_HOUR_MS = 60 * 60 * 1000;
function addAiRunDurationToTotal(totalAiRunHours, durationMs) {
    return totalAiRunHours + (durationMs ?? 0) / AI_RUN_HOUR_MS;
}
function resolveAttemptedAiRunAccounting(input) {
    return {
        consecutiveAiRuns: input.currentConsecutiveAiRuns + (input.attemptedWorkerExecution ? 1 : 0),
        totalAiRunHours: input.attemptedWorkerExecution
            ? addAiRunDurationToTotal(input.currentTotalAiRunHours, input.workerDurationMs)
            : input.currentTotalAiRunHours,
    };
}
function applyAiRunAccountingToWorkflowState(input) {
    if (input.workflowState === null ||
        (input.consecutiveAiRuns === undefined &&
            input.totalAiRunHours === undefined)) {
        return input.workflowState;
    }
    return {
        ...input.workflowState,
        ...(input.consecutiveAiRuns !== undefined
            ? { consecutiveAiRuns: input.consecutiveAiRuns }
            : {}),
        ...(input.totalAiRunHours !== undefined
            ? { totalAiRunHours: input.totalAiRunHours }
            : {}),
    };
}
function resolveWorkerExecutionTotalAiRunHours(workerExecution) {
    const currentTotalAiRunHours = workerExecution.workflowState?.totalAiRunHours ?? 0;
    if (!workerExecution.incrementsConsecutiveAiRuns) {
        return currentTotalAiRunHours;
    }
    return addAiRunDurationToTotal(currentTotalAiRunHours, workerExecution.workerDurationMs);
}
