/**
 * Retry Logic Utilities
 * Handles exponential backoff and retry decision logic for streaming errors
 */
/**
 * Calculate retry delay using exponential backoff
 *
 * @param attempt - Current retry attempt number (0-indexed)
 * @param config - Retry configuration options
 * @returns Delay in milliseconds before next retry
 *
 * @example
 * calculateRetryDelay(0) // Returns: 1000ms
 * calculateRetryDelay(1) // Returns: 2000ms
 * calculateRetryDelay(2) // Returns: 4000ms
 * calculateRetryDelay(3) // Returns: 8000ms
 * calculateRetryDelay(4) // Returns: 10000ms (capped at maxDelay)
 */
const calculateRetryDelay = (attempt, config) => {
    const initialDelay = (config === null || config === void 0 ? void 0 : config.initialDelay) || 1000;
    const multiplier = (config === null || config === void 0 ? void 0 : config.backoffMultiplier) || 2;
    const maxDelay = (config === null || config === void 0 ? void 0 : config.maxDelay) || 10000;
    const delay = initialDelay * Math.pow(multiplier, attempt);
    return Math.min(delay, maxDelay);
};
/**
 * Determine if a streaming error should be retried
 *
 * @param error - The streaming error that occurred
 * @param attempt - Current retry attempt number (0-indexed)
 * @param config - Retry configuration options
 * @returns True if should retry, false otherwise
 *
 * @example
 * shouldRetry(networkError, 0) // Returns: true
 * shouldRetry(networkError, 3) // Returns: false (max attempts reached)
 * shouldRetry(parseError, 0) // Returns: false (not retryable)
 */
const shouldRetry = (error, attempt, config) => {
    const maxAttempts = (config === null || config === void 0 ? void 0 : config.maxAttempts) || 3;
    // Use custom retry logic if provided
    if (config === null || config === void 0 ? void 0 : config.shouldRetry) {
        return config.shouldRetry(error, attempt);
    }
    // Don't retry if max attempts reached
    if (attempt >= maxAttempts) {
        return false;
    }
    // Only retry if error is marked as retryable
    return error.retryable;
};

export { calculateRetryDelay, shouldRetry };
