import { ExecutionStatus } from '@mondaydotcomorg/atp-protocol';
import { isPauseError, getCallSequenceNumber, setPauseForClient, setReplayMode, getAPICallResults, clearAPICallResults, setCurrentExecutionId, } from '@mondaydotcomorg/atp-runtime';
import { isBatchPauseError } from '@mondaydotcomorg/atp-compiler';
import { randomUUID } from 'node:crypto';
import { categorizeError } from './error-handler.js';
import { PAUSE_EXECUTION_MARKER } from './constants.js';
export function handleExecutionError(error, pauseError, context, executionId, callbackHistory, memoryBefore, executionLogger, isolate, transformedCode) {
    const errMsg = error instanceof Error ? error.message : String(error);
    if (errMsg.includes(PAUSE_EXECUTION_MARKER) && pauseError) {
        error = pauseError;
    }
    setCurrentExecutionId(executionId);
    const apiResults = getAPICallResults();
    if (isBatchPauseError(error)) {
        const batchErr = error;
        executionLogger.info('Execution paused for batch callbacks', {
            batchId: batchErr.batchId,
            callCount: batchErr.calls.length,
            sequenceNumber: batchErr.sequenceNumber,
        });
        // Add any API call results to callback history FIRST (already retrieved above)
        for (const apiRecord of apiResults) {
            callbackHistory.push(apiRecord);
        }
        if (apiResults.length > 0) {
            executionLogger.debug('Added API results to callback history', {
                apiCallCount: apiResults.length,
            });
        }
        try {
            clearAPICallResults();
        }
        catch (e) {
            // Context might not be set
        }
        const batchCallbackRecord = {
            type: batchErr.calls[0]?.type || 'llm',
            operation: 'batch_parallel',
            payload: {
                batchId: batchErr.batchId,
                calls: batchErr.calls,
            },
            sequenceNumber: batchErr.sequenceNumber,
            result: undefined,
            timestamp: Date.now(),
        };
        callbackHistory.push(batchCallbackRecord);
        const memoryAfter = process.memoryUsage().heapUsed;
        const memoryUsed = Math.max(0, memoryAfter - memoryBefore);
        try {
            isolate.dispose();
        }
        catch (e) { }
        setPauseForClient(false);
        setReplayMode(undefined);
        return {
            executionId,
            status: ExecutionStatus.PAUSED,
            needsCallbacks: batchErr.calls.map((call) => ({
                id: randomUUID(),
                type: call.type,
                operation: call.operation,
                payload: call.payload,
            })),
            callbackHistory,
            stats: {
                duration: Date.now() - context.startTime,
                memoryUsed,
                llmCallsCount: context.llmCallCount,
                approvalCallsCount: context.approvalCallCount,
            },
            transformedCode,
        };
    }
    if (isPauseError(error)) {
        const pauseErr = error;
        for (const apiRecord of apiResults) {
            callbackHistory.push(apiRecord);
        }
        if (apiResults.length > 0) {
            executionLogger.debug('Added API results to callback history', {
                apiCallCount: apiResults.length,
            });
        }
        clearAPICallResults();
        let currentSequence;
        const payloadSequence = typeof pauseErr.payload?.sequenceNumber === 'number'
            ? pauseErr.payload.sequenceNumber
            : undefined;
        if (payloadSequence !== undefined) {
            currentSequence = payloadSequence;
        }
        else {
            currentSequence = getCallSequenceNumber() - 1;
        }
        const callbackRecord = {
            type: pauseErr.type,
            operation: pauseErr.operation,
            payload: pauseErr.payload,
            result: undefined,
            timestamp: Date.now(),
            sequenceNumber: currentSequence,
        };
        callbackHistory.push(callbackRecord);
        executionLogger.info('Execution paused for client callback', {
            type: pauseErr.type,
            operation: pauseErr.operation,
            sequenceNumber: currentSequence,
            historyLength: callbackHistory.length,
        });
        const memoryAfter = process.memoryUsage().heapUsed;
        const memoryUsed = Math.max(0, memoryAfter - memoryBefore);
        try {
            isolate.dispose();
        }
        catch (e) { }
        setPauseForClient(false);
        setReplayMode(undefined);
        return {
            executionId,
            status: ExecutionStatus.PAUSED,
            needsCallback: {
                type: pauseErr.type,
                operation: pauseErr.operation,
                payload: pauseErr.payload,
            },
            callbackHistory,
            stats: {
                duration: Date.now() - context.startTime,
                memoryUsed,
                llmCallsCount: context.llmCallCount,
                approvalCallsCount: context.approvalCallCount,
            },
            transformedCode,
        };
    }
    const err = error;
    const errorInfo = categorizeError(err);
    executionLogger.error('Code execution failed', {
        error: err.message,
        status: errorInfo.status,
        code: errorInfo.code,
        retryable: errorInfo.retryable,
    });
    const memoryAfter = process.memoryUsage().heapUsed;
    const memoryUsed = Math.max(0, memoryAfter - memoryBefore);
    try {
        isolate.dispose();
    }
    catch (e) { }
    setPauseForClient(false);
    setReplayMode(undefined);
    return {
        executionId,
        status: errorInfo.status,
        error: {
            message: err.message,
            code: errorInfo.code,
            stack: err.stack,
            retryable: errorInfo.retryable,
            suggestion: errorInfo.suggestion,
        },
        stats: {
            duration: Date.now() - context.startTime,
            memoryUsed,
            llmCallsCount: context.llmCallCount,
            approvalCallsCount: context.approvalCallCount,
        },
    };
}
//# sourceMappingURL=execution-error-handler.js.map