import { EventSourceMessage } from '@microsoft/fetch-event-source';

interface FetchEventSourceWithTokenRefreshOptions {
    url: string;
    getToken: () => Promise<string | undefined>;
    headers?: Record<string, string>;
    signal: AbortSignal;
    /** Delay in ms before reconnecting after token refresh. Defaults to 2000. */
    tokenRefreshDelayMs?: number;
    onopen?: (response: Response) => void | Promise<void>;
    onmessage?: (event: EventSourceMessage) => void;
    onerror?: (error: unknown) => void;
    onclose?: () => void;
}
/**
 * Wrapper around fetchEventSource that handles token expiration and automatic retry with fresh token.
 *
 * When a 401 response is received:
 * 1. Aborts current connection
 * 2. Waits for tokenRefreshDelayMs (default 2s)
 * 3. Fetches fresh token
 * 4. Reconnects with new token
 */
declare function fetchEventSourceWithTokenRefresh(options: FetchEventSourceWithTokenRefreshOptions): Promise<void>;

interface SSEConnectionManagerOptions {
    /** Grace period in ms before closing idle connections. Defaults to 5000. */
    closeGracePeriodMs?: number;
}
interface SSEConnectionManagerLike {
    subscribe(key: string, subscriberId: string, options: Omit<FetchEventSourceWithTokenRefreshOptions, 'signal'>): () => void;
    closeConnection(key: string): void;
    getConnectionInfo(): Map<string, {
        url: string;
        subscribers: number;
    }>;
}
/**
 * SSE Connection Manager
 *
 * Ensures only ONE SSE connection exists per endpoint, preventing duplicate
 * connections when components re-render or remount.
 *
 * Benefits:
 * - Prevents resource waste from duplicate connections
 * - Eliminates race conditions from overlapping connections
 * - Automatically manages connection lifecycle
 * - Shares single connection across multiple subscribers
 */
declare class SSEConnectionManager implements SSEConnectionManagerLike {
    private connections;
    private closeGracePeriodMs;
    constructor(options?: SSEConnectionManagerOptions);
    /**
     * Subscribe to an SSE endpoint
     *
     * If a connection already exists for this endpoint, reuses it.
     * Otherwise, creates a new connection.
     *
     * @param key - Unique identifier for the connection (e.g., 'notifications', 'resource-executive-agent')
     * @param subscriberId - Unique identifier for this subscriber (usually component instance)
     * @param options - SSE connection options
     * @returns Unsubscribe function to call when component unmounts
     */
    subscribe(key: string, subscriberId: string, options: Omit<FetchEventSourceWithTokenRefreshOptions, 'signal'>): () => void;
    /**
     * Unsubscribe from an SSE endpoint
     *
     * If this is the last subscriber, closes the connection after grace period.
     */
    private unsubscribe;
    /**
     * Force close a connection and all its subscribers
     */
    closeConnection(key: string): void;
    /**
     * Get current connection status
     */
    getConnectionInfo(): Map<string, {
        url: string;
        subscribers: number;
    }>;
}

export { SSEConnectionManager, fetchEventSourceWithTokenRefresh };
export type { FetchEventSourceWithTokenRefreshOptions, SSEConnectionManagerLike, SSEConnectionManagerOptions };
