import { StreamClient } from './client';
import type { UR, WSConnectionError } from './types';
import type { LogLevel } from '@stream-io/logger';
import type { ConnectedEvent } from '../../gen/coordinator';
/**
 * StableWSConnection - A WS connection that reconnects upon failure.
 * - the browser will sometimes report that you're online or offline
 * - the WS connection can break and fail (there is a 30s health check)
 * - sometimes your WS connection will seem to work while the user is in fact offline
 * - to speed up online/offline detection you can use the window.addEventListener('offline');
 *
 * There are 4 ways in which a connection can become unhealthy:
 * - websocket.onerror is called
 * - websocket.onclose is called
 * - the health check fails and no event is received for ~40 seconds
 * - the browser indicates the connection is now offline
 *
 * There are 2 assumptions we make about the server:
 * - state can be recovered by querying the channel again
 * - if the servers fails to publish a message to the client, the WS connection is destroyed
 */
export declare class StableWSConnection {
    client: StreamClient;
    ws?: WebSocket;
    /** Incremented when a new WS connection is made */
    wsID: number;
    /** We only make 1 attempt to reconnect at the same time.. */
    isConnecting: boolean;
    /** To avoid reconnect if client is disconnected */
    isDisconnected: boolean;
    /** Boolean that indicates if we have a working connection to the server */
    isHealthy: boolean;
    connectionID?: string;
    private connectionOpenSafe?;
    resolveConnectionOpen?: (value: ConnectedEvent) => void;
    rejectConnectionOpen?: (reason?: WSConnectionError) => void;
    /** Boolean that indicates if the connection promise is resolved */
    isConnectionOpenResolved?: boolean;
    /** consecutive failures influence the duration of the timeout */
    consecutiveFailures: number;
    /** keep track of the total number of failures */
    totalFailures: number;
    /** Send a health check message every 25 seconds */
    pingInterval: number;
    healthCheckTimeoutRef?: number;
    connectionCheckTimeout: number;
    connectionCheckTimeoutRef?: NodeJS.Timeout;
    /** Store the last event time for health checks */
    lastEvent: Date | null;
    constructor(client: StreamClient);
    _log: (msg: string, extra?: UR | Error, level?: LogLevel) => void;
    setClient: (client: StreamClient) => void;
    /**
     * connect - Connect to the WS URL
     * the default 15s timeout allows between 2~3 tries
     * @return Promise that completes once the first health check message is received
     */
    connect: (timeout?: number) => Promise<ConnectedEvent | undefined>;
    /**
     * _waitForHealthy polls the promise connection to see if its resolved until it times out
     * the default 15s timeout allows between 2~3 tries
     * @param timeout duration(ms)
     */
    _waitForHealthy: (timeout?: number) => Promise<ConnectedEvent | undefined>;
    /**
     * Builds and returns the url for websocket.
     * @private
     * @returns url string
     */
    _buildUrl: () => string;
    /**
     * disconnect - Disconnect the connection and doesn't recover...
     */
    disconnect: (timeout?: number) => Promise<void>;
    /**
     * _connect - Connect to the WS endpoint
     *
     * @param timeoutMs handshake watchdog deadline in ms. Defaults to
     *   `client.defaultWSTimeout` when not provided. Top-level `connect(timeout)`
     *   passes its own timeout through so caller-supplied deadlines are honored.
     * @return Promise that completes once the first health check message is received
     */
    _connect: (timeoutMs?: number) => Promise<ConnectedEvent | undefined>;
    /**
     * _reconnect - Retry the connection to WS endpoint
     *
     * @param {{ interval?: number; refreshToken?: boolean }} options Following options are available
     *
     * - `interval`	{int}			number of ms that function should wait before reconnecting
     * - `refreshToken` {boolean}	reload/refresh user token be refreshed before attempting reconnection.
     */
    _reconnect(options?: {
        interval?: number;
        refreshToken?: boolean;
    }): Promise<void>;
    /**
     * onlineStatusChanged - this function is called when the browser connects or disconnects from the internet.
     *
     * @param {Event} event Event with type online or offline
     */
    onlineStatusChanged: (event: Event) => void;
    onopen: (wsID: number) => void;
    onmessage: (wsID: number, event: MessageEvent) => void;
    onclose: (wsID: number, event: CloseEvent) => void;
    onerror: (wsID: number, event: Event) => void;
    /**
     * _setHealth - Sets the connection to healthy or unhealthy.
     * Broadcasts an event in case the connection status changed.
     *
     * @param {boolean} healthy boolean indicating if the connection is healthy or not
     * @param {boolean} dispatchImmediately boolean indicating to dispatch event immediately even if the connection is unhealthy
     */
    _setHealth: (healthy: boolean, dispatchImmediately?: boolean) => void;
    /**
     * _errorFromWSEvent - Creates an error object for the WS event
     */
    private _errorFromWSEvent;
    /**
     * _destroyCurrentWSConnection - Removes the current WS connection
     *
     */
    _destroyCurrentWSConnection(): void;
    /**
     * _setupPromise - sets up the this.connectOpen promise
     */
    _setupConnectionPromise: () => void;
    get connectionOpen(): Promise<ConnectedEvent> | undefined;
    /**
     * Schedules a next health check ping for websocket.
     */
    scheduleNextPing: () => void;
    /**
     * scheduleConnectionCheck - schedules a check for time difference between last received event and now.
     * If the difference is more than 35 seconds, it means our health check logic has failed and websocket needs
     * to be reconnected.
     */
    scheduleConnectionCheck: () => void;
}
