export interface RetryPolicy {
  maxRetries: number
  initialDelay: number
  maxDelay: number
  backoffFactor: number
  jitter: boolean
}

export function calculateBackoff(attempt: number, policy: RetryPolicy): number {
  const { initialDelay, maxDelay, backoffFactor, jitter } = policy
  
  let delay = initialDelay * Math.pow(backoffFactor, attempt)
  delay = Math.min(delay, maxDelay)
  
  if (jitter) {
    delay = Math.random() * delay
  }
  
  return Math.floor(delay)
}

export interface RetryPolicies {
  default: RetryPolicy
  policies?: Record<string, RetryPolicy>
}

export const DEFAULT_RETRY_POLICY: RetryPolicy = {
  maxRetries: 3,
  initialDelay: 1000,
  maxDelay: 30000,
  backoffFactor: 2,
  jitter: true,
}
