/**
 * Owns cli-v2 control-plane subscription handles and address deduping.
 */
export class ControlPlaneSessionSubscriptionService {
    options;
    sessionsSubscription;
    sessionSubscription;
    sessionAddress;
    constructor(options) {
        this.options = options;
    }
    subscribeToSessionList(workspaceId) {
        this.sessionsSubscription?.unsubscribe();
        this.sessionsSubscription = this.options.client.controlPlane.sessionsEvents.subscribe({ workspaceId }, {
            onData: (event) => {
                if (event.type === 'sessions.updated') {
                    this.options.onSessionsUpdated();
                }
            },
            onError: this.options.onSessionListError,
        });
    }
    subscribeToSessionEvents(workspaceId, sessionId) {
        if (this.sessionAddress?.workspaceId === workspaceId &&
            this.sessionAddress.sessionId === sessionId) {
            return;
        }
        this.sessionSubscription?.unsubscribe();
        this.sessionAddress = { workspaceId, sessionId };
        this.sessionSubscription = this.options.client.controlPlane.sessionEvents.subscribe({ workspaceId, sessionId }, {
            onStarted: this.options.onSessionStreamStarted,
            onData: (event) => {
                this.options.onSessionEvent(workspaceId, event);
            },
            onError: this.options.onSessionStreamError,
            onComplete: this.options.onSessionStreamComplete,
        });
    }
    dispose() {
        this.sessionsSubscription?.unsubscribe();
        this.sessionSubscription?.unsubscribe();
        this.sessionsSubscription = undefined;
        this.sessionSubscription = undefined;
        this.sessionAddress = undefined;
    }
}
//# sourceMappingURL=control-plane-session-subscription-service.js.map