// Synced from packages/transport-http/src/retry.ts
// Extracted from legacy source: src/runtime/transports/http-retry.ts
import { computeBackoffDelay, normalizeBackoffPolicy } from './backoff.js';
export const DEFAULT_HTTP_RETRY_POLICY = {
    maxAttempts: 1,
    baseDelayMs: 250,
    maxDelayMs: 2_000,
    backoffFactor: 2,
    retryOnStatuses: [408, 429, 500, 502, 503, 504],
    retryOnMethods: ['GET', 'HEAD', 'OPTIONS'],
    retryOnNetworkError: true,
};
export function normalizeHttpRetryPolicy(policy) {
    const normalized = normalizeBackoffPolicy(policy, DEFAULT_HTTP_RETRY_POLICY);
    return {
        ...normalized,
        retryOnStatuses: [...(policy?.retryOnStatuses ?? DEFAULT_HTTP_RETRY_POLICY.retryOnStatuses)],
        retryOnMethods: [...(policy?.retryOnMethods ?? DEFAULT_HTTP_RETRY_POLICY.retryOnMethods)].map((method) => method.toUpperCase()),
        retryOnNetworkError: policy?.retryOnNetworkError ?? DEFAULT_HTTP_RETRY_POLICY.retryOnNetworkError,
    };
}
export function resolveHttpRetryPolicy(defaultPolicy, override) {
    if (override === false) {
        return normalizeHttpRetryPolicy({ maxAttempts: 1 });
    }
    const base = normalizeHttpRetryPolicy(defaultPolicy);
    if (!override)
        return base;
    return {
        ...base,
        ...normalizeBackoffPolicy(override, base),
        retryOnStatuses: override.retryOnStatuses ? [...override.retryOnStatuses] : base.retryOnStatuses,
        retryOnMethods: override.retryOnMethods ? override.retryOnMethods.map((method) => method.toUpperCase()) : base.retryOnMethods,
        retryOnNetworkError: override.retryOnNetworkError ?? base.retryOnNetworkError,
    };
}
export function getHttpRetryDelay(attempt, policy) {
    return computeBackoffDelay(attempt, policy);
}
export function isRetryableHttpStatus(method, status, policy) {
    return policy.retryOnMethods.includes(method.toUpperCase())
        && policy.retryOnStatuses.includes(status);
}
export function isRetryableNetworkError(method, policy) {
    return policy.retryOnMethods.includes(method.toUpperCase()) && policy.retryOnNetworkError;
}
