/**
 * Realtime Manager - WebSocket subscription support
 * @generated by @constructive-io/graphql-codegen
 * DO NOT EDIT - changes will be overwritten
 */
// Minimal type shims so this module compiles without graphql-ws
// installed.  Consumers supply a WsClient via RealtimeConfig;
// the SDK itself never imports or requires graphql-ws.
// ============================================================================
// RealtimeManager
// ============================================================================
/**
 * Manages a graphql-ws WebSocket client and multiplexes
 * subscriptions over it. Created by OrmClient when `realtime`
 * config is provided.
 */
export class RealtimeManager {
    wsClient;
    connectionState = 'disconnected';
    stateListeners = new Set();
    activeSubscriptions = 0;
    constructor(config) {
        this.wsClient = config.client;
    }
    /**
     * Subscribe to a GraphQL subscription operation.
     * Models call this with typed metadata and documents.
     */
    subscribe(meta, document, variables, options) {
        this.activeSubscriptions++;
        let disposed = false;
        const cleanup = this.wsClient.subscribe({ query: document, variables }, {
            next: (result) => {
                if (disposed)
                    return;
                if (result.errors) {
                    options.onError?.(new Error(result.errors.map((e) => e.message).join('; ')));
                    return;
                }
                const payload = result.data?.[meta.fieldName];
                if (!payload)
                    return;
                const event = {
                    operation: payload.event ?? 'UPDATE',
                    data: payload[meta.dataFieldName] ?? null,
                    previousValues: payload.previousValues,
                    timestamp: payload.timestamp ?? new Date().toISOString(),
                };
                options.onEvent(event);
            },
            error: (err) => {
                if (disposed)
                    return;
                options.onError?.(err instanceof Error ? err : new Error(String(err)));
            },
            complete: () => {
                if (disposed)
                    return;
                options.onComplete?.();
            },
        });
        return () => {
            if (disposed)
                return;
            disposed = true;
            this.activeSubscriptions--;
            cleanup();
        };
    }
    /** Register a listener for connection state changes */
    onConnectionStateChange(listener) {
        this.stateListeners.add(listener);
        return () => {
            this.stateListeners.delete(listener);
        };
    }
    /** Get current connection state */
    getConnectionState() {
        return this.connectionState;
    }
    /** Number of active subscriptions */
    getActiveSubscriptionCount() {
        return this.activeSubscriptions;
    }
    /** Dispose the manager and close the WebSocket connection */
    dispose() {
        this.wsClient.dispose();
        this.stateListeners.clear();
        this.activeSubscriptions = 0;
        this.setConnectionState('disconnected');
    }
    setConnectionState(state) {
        if (this.connectionState === state)
            return;
        this.connectionState = state;
        for (const listener of this.stateListeners) {
            listener(state);
        }
    }
}
