// Synced from packages/transport-http/src/http.ts
import { ConfigurationError, ContractError, createHttpStatusError } from '../errors/index.js';
import { mergeHeaders, resolveAuthToken, resolveHeaders, } from './auth.js';
import { computeBackoffDelay, normalizeBackoffPolicy, sleepWithSignal, } from './backoff.js';
import { DEFAULT_HTTP_RETRY_POLICY, getHttpRetryDelay, isRetryableHttpStatus, isRetryableNetworkError, normalizeHttpRetryPolicy, resolveHttpRetryPolicy, } from './retry.js';
import { DEFAULT_STREAM_RECONNECT_POLICY, getStreamReconnectDelay, normalizeStreamReconnectPolicy, } from './reconnect.js';
import { createFetch, createHttpJsonTransport, createJsonInit, createJsonRequestInit, readJsonBody, requestJson, } from './http-core.js';
export { createFetch, createJsonInit, createJsonRequestInit, readJsonBody, requestJson, mergeHeaders, resolveAuthToken, resolveHeaders, computeBackoffDelay, normalizeBackoffPolicy, sleepWithSignal, DEFAULT_HTTP_RETRY_POLICY, getHttpRetryDelay, isRetryableHttpStatus, isRetryableNetworkError, normalizeHttpRetryPolicy, resolveHttpRetryPolicy, DEFAULT_STREAM_RECONNECT_POLICY, getStreamReconnectDelay, normalizeStreamReconnectPolicy, };
function isTransportError(error) {
    return Boolean(error
        && typeof error === 'object'
        && 'transport' in error
        && error.transport
        && typeof error.transport.status === 'number'
        && typeof error.transport.url === 'string');
}
export function normalizeTransportError(error) {
    if (isTransportError(error)) {
        return Object.assign(createHttpStatusError(error.transport.status, error.transport.url, typeof error.transport.method === 'string' ? error.transport.method : 'GET', error.transport.body), { transport: error.transport });
    }
    if (error instanceof Error) {
        if (error.message === 'Fetch implementation is required' || error.message === 'Transport baseUrl is required') {
            return new ConfigurationError(error.message);
        }
        if (error.message.startsWith('Missing required path parameter')) {
            return new ContractError(error.message);
        }
    }
    return error instanceof Error ? error : new Error(String(error));
}
export function createHttpTransport(options) {
    const transport = createHttpJsonTransport(options);
    return {
        ...transport,
        async requestJson(pathOrUrl, requestOptions = {}) {
            try {
                return await transport.requestJson(pathOrUrl, requestOptions);
            }
            catch (error) {
                throw normalizeTransportError(error);
            }
        },
        resolveContractRequest(method, path, input = {}) {
            try {
                return transport.resolveContractRequest(method, path, input);
            }
            catch (error) {
                throw normalizeTransportError(error);
            }
        },
    };
}
