import { ConfigurationError } from './_internal/errors/index.js';
import { createRemoteRuntimeEvents, createWebSocketConnector, } from './_internal/transport-realtime/index.js';
import { createGoodVibesSdk, } from './client.js';
function requireReactNativeWebSocket(webSocketImpl) {
    const resolved = webSocketImpl ?? globalThis.WebSocket;
    if (typeof resolved !== 'function') {
        throw new ConfigurationError('React Native realtime requires a WebSocket implementation. Pass options.WebSocketImpl or use a runtime that provides global WebSocket.');
    }
    return resolved;
}
export function createReactNativeGoodVibesSdk(options) {
    const base = createGoodVibesSdk({
        ...options,
        retry: options.retry ?? {
            maxAttempts: 3,
            baseDelayMs: 250,
            maxDelayMs: 2_000,
        },
        realtime: {
            sseReconnect: {
                enabled: true,
                baseDelayMs: 500,
                maxDelayMs: 5_000,
                ...(options.realtime?.sseReconnect ?? {}),
            },
            webSocketReconnect: {
                enabled: true,
                baseDelayMs: 500,
                maxDelayMs: 5_000,
                ...(options.realtime?.webSocketReconnect ?? {}),
            },
            ...(options.realtime ?? {}),
        },
        WebSocketImpl: options.WebSocketImpl ?? globalThis.WebSocket,
    });
    const getAuthToken = () => base.auth.getToken();
    return {
        ...base,
        realtime: {
            runtime() {
                return createRemoteRuntimeEvents(createWebSocketConnector(options.baseUrl, getAuthToken, requireReactNativeWebSocket(options.WebSocketImpl), {
                    reconnect: options.realtime?.webSocketReconnect,
                    onError: options.realtime?.onError,
                }));
            },
            viaWebSocket(webSocketImpl) {
                return createRemoteRuntimeEvents(createWebSocketConnector(options.baseUrl, getAuthToken, requireReactNativeWebSocket(webSocketImpl ?? options.WebSocketImpl), {
                    reconnect: options.realtime?.webSocketReconnect,
                    onError: options.realtime?.onError,
                }));
            },
        },
    };
}
