export async function withRetry(operation, options) {
    const { maxRetries, baseDelayMs, maxDelayMs = 30000, shouldRetry } = options;
    let lastError;
    for (let attempt = 0; attempt <= maxRetries; attempt++) {
        try {
            return await operation();
        }
        catch (error) {
            lastError = error;
            if (shouldRetry && !shouldRetry(lastError)) {
                throw lastError;
            }
            if (attempt < maxRetries) {
                const delay = Math.min(baseDelayMs * 2 ** attempt, maxDelayMs);
                await new Promise((resolve) => setTimeout(resolve, delay));
            }
        }
    }
    throw lastError;
}
