/**
 * HTTP Client implementation for React Native
 *
 * This implementation properly implements the core SDK's HttpClient interface
 * and uses fetch API which is available in React Native.
 */
export class ReactNativeHttpClient {
    constructor(baseURL) {
        this.baseURL = baseURL;
    }
    async get(url, options) {
        return this.request('GET', url, undefined, options);
    }
    async post(url, body, options) {
        return this.request('POST', url, body, options);
    }
    async put(url, body, options) {
        return this.request('PUT', url, body, options);
    }
    async delete(url, options) {
        return this.request('DELETE', url, undefined, options);
    }
    async request(method, url, body, options) {
        const fullUrl = this.buildUrl(url, options?.params);
        const config = {
            method,
            headers: {
                'Content-Type': 'application/json',
                ...options?.headers,
            },
        };
        // Add timeout if specified
        if (options?.timeout) {
            const controller = new AbortController();
            config.signal = controller.signal;
            setTimeout(() => controller.abort(), options.timeout);
        }
        // Add body for POST/PUT requests
        if (body && (method === 'POST' || method === 'PUT')) {
            config.body = JSON.stringify(body);
        }
        try {
            const response = await fetch(fullUrl, config);
            if (!response.ok) {
                // Get raw error response for ErrorUtils to handle
                let errorText = '';
                try {
                    errorText = await response.text();
                }
                catch (e) {
                    // Ignore errors when reading error body
                }
                // Create simple error with HTTP status and raw response
                const httpError = new Error(`HTTP ${response.status}: ${response.statusText} - ${errorText}`);
                httpError.status = response.status;
                throw httpError;
            }
            // Handle different response types
            switch (options?.responseType) {
                case 'text':
                    return response.text();
                case 'blob':
                    return response.blob();
                case 'arraybuffer':
                    return response.arrayBuffer();
                case 'json':
                default:
                    // Check if response has content
                    const contentLength = response.headers.get('content-length');
                    if (contentLength === '0' || response.status === 204) {
                        return undefined;
                    }
                    return response.json();
            }
        }
        catch (error) {
            if (error instanceof Error && error.name === 'AbortError') {
                throw new Error(`Request timeout after ${options?.timeout}ms`);
            }
            throw error;
        }
    }
    buildUrl(url, params) {
        const fullUrl = this.baseURL ? `${this.baseURL}${url}` : url;
        if (!params || Object.keys(params).length === 0) {
            return fullUrl;
        }
        const urlObj = new URL(fullUrl);
        Object.entries(params).forEach(([key, value]) => {
            urlObj.searchParams.append(key, value);
        });
        return urlObj.toString();
    }
}
