"use strict";
/**
 * Retry utilities, transient error detection and exponential backoff.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isTransientError = isTransientError;
exports.calcBackoff = calcBackoff;
const TRANSIENT_PATTERNS = [
    'timeout', 'econnreset', 'econnrefused', 'enotfound',
    'connection closed', 'connection lost', 'not connected',
    'websocket', 'rpc timeout', 'socket hang up',
    'network', 'epipe', 'ehostunreach'
];
/**
 * Check if an error is transient (retryable) vs permanent.
 * Transient: timeouts, connection resets, network issues.
 * Permanent: invalid data, unknown methods, logic errors.
 */
function isTransientError(err) {
    const message = err.message.toLowerCase();
    return TRANSIENT_PATTERNS.some(pattern => message.includes(pattern));
}
/**
 * Calculate exponential backoff delay with jitter.
 * Formula: baseDelay * 2^(attempt-1) + random jitter, capped at maxDelay.
 */
function calcBackoff(attempt, baseDelay, maxDelay = 30000) {
    const exponentialDelay = baseDelay * Math.pow(2, attempt - 1);
    const jitter = Math.random() * baseDelay * 0.5;
    return Math.min(exponentialDelay + jitter, maxDelay);
}
