/**
 * Retry Policy Module
 *
 * Provides exponential backoff retry logic, circuit breaker pattern,
 * and comprehensive error recovery for streaming operations.
 */
import { EventEmitter } from 'events';
export interface RetryConfig {
    /** Maximum number of retry attempts */
    maxAttempts: number;
    /** Initial delay in milliseconds */
    initialDelayMs: number;
    /** Maximum delay in milliseconds */
    maxDelayMs: number;
    /** Backoff multiplier (exponential factor) */
    backoffMultiplier: number;
    /** Add jitter to prevent thundering herd */
    jitter: boolean;
    /** Jitter factor (0-1) */
    jitterFactor: number;
    /** Timeout for each attempt in milliseconds */
    attemptTimeoutMs?: number;
    /** Error types that should trigger retry */
    retryableErrors?: string[];
    /** Error types that should NOT trigger retry */
    nonRetryableErrors?: string[];
}
export declare const DEFAULT_RETRY_CONFIG: RetryConfig;
export interface RetryAttempt {
    attempt: number;
    startTime: number;
    endTime?: number;
    success: boolean;
    error?: Error;
    delayMs?: number;
}
export interface RetryResult<T> {
    success: boolean;
    value?: T;
    error?: Error;
    attempts: RetryAttempt[];
    totalDurationMs: number;
}
export interface CircuitBreakerConfig {
    /** Number of failures before opening circuit */
    failureThreshold: number;
    /** Time window for counting failures (ms) */
    failureWindowMs: number;
    /** Time to wait before half-opening circuit (ms) */
    resetTimeoutMs: number;
    /** Number of successes needed to close circuit from half-open */
    successThreshold: number;
    /** Timeout for probe requests in half-open state (ms) */
    probeTimeoutMs: number;
}
export declare const DEFAULT_CIRCUIT_BREAKER_CONFIG: CircuitBreakerConfig;
export type CircuitState = 'closed' | 'open' | 'half-open';
export interface CircuitBreakerStats {
    state: CircuitState;
    failures: number;
    successes: number;
    lastFailureTime?: number;
    lastSuccessTime?: number;
    openedAt?: number;
    totalRequests: number;
    successfulRequests: number;
    failedRequests: number;
    rejectedRequests: number;
}
export interface CircuitBreakerEvents {
    'state-change': (from: CircuitState, to: CircuitState) => void;
    'failure': (error: Error) => void;
    'success': () => void;
    'rejected': () => void;
    'probe': () => void;
}
/**
 * Calculate delay for a given attempt with exponential backoff
 */
export declare function calculateDelay(attempt: number, config: RetryConfig): number;
/**
 * Check if an error is retryable based on config
 */
export declare function isRetryable(error: Error, config: RetryConfig): boolean;
/**
 * Sleep for a given duration
 */
export declare function sleep(ms: number): Promise<void>;
/**
 * Create a timeout promise
 */
export declare function withTimeout<T>(promise: Promise<T>, timeoutMs: number, message?: string): Promise<T>;
/**
 * Execute an operation with retry logic
 */
export declare function retry<T>(operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<RetryResult<T>>;
/**
 * Execute with retry and return value or throw
 */
export declare function retryOrThrow<T>(operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<T>;
export declare class CircuitBreaker extends EventEmitter {
    private config;
    private state;
    private failures;
    private successes;
    private openedAt?;
    private stats;
    constructor(config?: Partial<CircuitBreakerConfig>);
    /**
     * Execute an operation through the circuit breaker
     */
    execute<T>(operation: () => Promise<T>): Promise<T>;
    /**
     * Record a successful operation
     */
    private recordSuccess;
    /**
     * Record a failed operation
     */
    private recordFailure;
    /**
     * Check if we should probe (transition from open to half-open)
     */
    private shouldProbe;
    /**
     * Transition to a new state
     */
    private transitionTo;
    /**
     * Remove old failures outside the window
     */
    private cleanupFailures;
    /**
     * Get current state
     */
    getState(): CircuitState;
    /**
     * Get statistics
     */
    getStats(): CircuitBreakerStats;
    /**
     * Manually reset the circuit breaker
     */
    reset(): void;
    /**
     * Force open the circuit
     */
    trip(): void;
}
/**
 * Error thrown when circuit is open
 */
export declare class CircuitOpenError extends Error {
    constructor(message: string);
}
export interface RetryManagerConfig {
    retry: Partial<RetryConfig>;
    circuitBreaker: Partial<CircuitBreakerConfig>;
    useCircuitBreaker: boolean;
}
export declare const DEFAULT_RETRY_MANAGER_CONFIG: RetryManagerConfig;
export declare class RetryManager extends EventEmitter {
    private config;
    private circuitBreakers;
    constructor(config?: Partial<RetryManagerConfig>);
    /**
     * Get or create a circuit breaker for a service
     */
    private getCircuitBreaker;
    /**
     * Execute an operation with retry and circuit breaker
     */
    execute<T>(serviceId: string, operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<RetryResult<T>>;
    /**
     * Execute and return value or throw
     */
    executeOrThrow<T>(serviceId: string, operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<T>;
    /**
     * Get circuit breaker state for a service
     */
    getCircuitState(serviceId: string): CircuitState | undefined;
    /**
     * Get circuit breaker stats for a service
     */
    getCircuitStats(serviceId: string): CircuitBreakerStats | undefined;
    /**
     * Get all circuit breaker stats
     */
    getAllStats(): Map<string, CircuitBreakerStats>;
    /**
     * Reset a specific circuit breaker
     */
    resetCircuit(serviceId: string): boolean;
    /**
     * Reset all circuit breakers
     */
    resetAllCircuits(): void;
    /**
     * Clear a circuit breaker
     */
    clearCircuit(serviceId: string): boolean;
    /**
     * Clear all circuit breakers
     */
    clearAllCircuits(): void;
}
export declare function getRetryManager(config?: Partial<RetryManagerConfig>): RetryManager;
export declare function resetRetryManager(): void;
/**
 * Create a retryable version of a function
 */
export declare function withRetry<T extends (...args: unknown[]) => Promise<unknown>>(fn: T, config?: Partial<RetryConfig>): T;
/**
 * Create a function protected by circuit breaker
 */
export declare function withCircuitBreaker<T extends (...args: unknown[]) => Promise<unknown>>(fn: T, serviceId: string, config?: Partial<CircuitBreakerConfig>): T;
/**
 * Create a function with both retry and circuit breaker
 */
export declare function withRetryAndCircuitBreaker<T extends (...args: unknown[]) => Promise<unknown>>(fn: T, serviceId: string, config?: Partial<RetryManagerConfig>): T;
