import { ConfigurationError } from '@pellux/goodvibes-errors';
export { forSession } from './companion-realtime.js';
import { createRemoteRuntimeEvents, createWebSocketConnector, } from '@pellux/goodvibes-transport-realtime';
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;
}
/**
 * Create a GoodVibes SDK instance for React Native.
 *
 * Key differences from the browser factory:
 * - Realtime is WebSocket-only (`realtime.runtime()` / `realtime.viaWebSocket()`).
 *   SSE is not available in React Native.
 * - Requires a `WebSocket` implementation (e.g. the global provided by the
 *   React Native runtime or the `react-native` package). Pass
 *   `options.WebSocketImpl` when the global is not available.
 * - Returns `ReactNativeGoodVibesSdk` (extends `GoodVibesSdk` with a
 *   React-Native-specific `realtime` namespace).
 *
 * @example
 * // Example only: replace baseUrl and authToken with your own values.
 * import { createReactNativeGoodVibesSdk } from '@pellux/goodvibes-sdk/react-native';
 *
 * const sdk = createReactNativeGoodVibesSdk({
 *   baseUrl: 'https://daemon.example.com',
 *   authToken: await SecureStore.getItemAsync('token'),
 * });
 *
 * const events = sdk.realtime.viaWebSocket();
 * events.agents.on('AGENT_SPAWNING', ({ agentId }) => console.log(agentId));
 */
export function createReactNativeGoodVibesSdk(options) {
    // Normalize baseUrl once — trimmed and validated — so both HTTP and WebSocket
    // connectors use the same value regardless of how the caller passed it.
    const baseUrl = options.baseUrl.trim();
    const base = createGoodVibesSdk({
        ...options,
        // Default retry: 3 attempts with exponential back-off capped at 2 s.
        // React Native links can be flaky (radio, background suspension) so a
        // conservative retry budget prevents cascading failures.
        retry: options.retry ?? {
            maxAttempts: 3,
            baseDelayMs: 250,
            maxDelayMs: 2_000,
        },
        realtime: {
            ...(options.realtime ?? {}),
            webSocketReconnect: {
                enabled: true,
                baseDelayMs: 500,
                maxDelayMs: 5_000,
                ...(options.realtime?.webSocketReconnect ?? {}),
            },
        },
        WebSocketImpl: options.WebSocketImpl ?? globalThis.WebSocket,
    });
    const getAuthToken = () => base.auth.getToken();
    return {
        ...base,
        realtime: {
            runtime() {
                return createRemoteRuntimeEvents(createWebSocketConnector(baseUrl, getAuthToken, requireReactNativeWebSocket(options.WebSocketImpl), {
                    reconnect: options.realtime?.webSocketReconnect,
                    onError: options.realtime?.onError,
                }), {
                    onError: (error) => options.realtime?.onError?.(error),
                });
            },
            viaWebSocket(webSocketImpl) {
                return createRemoteRuntimeEvents(createWebSocketConnector(baseUrl, getAuthToken, requireReactNativeWebSocket(webSocketImpl ?? options.WebSocketImpl), {
                    reconnect: options.realtime?.webSocketReconnect,
                    onError: options.realtime?.onError,
                }), {
                    onError: (error) => options.realtime?.onError?.(error),
                });
            },
        },
    };
}
export { createIOSKeychainTokenStore, } from './client-auth/ios-keychain-token-store.js';
export { createAndroidKeystoreTokenStore, } from './client-auth/android-keystore-token-store.js';
