// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { convertSdkCallFeatureStreamToDeclarativeCallFeatureStream } from './Converter';
import { TogetherModeVideoStreamSubscriber } from './TogetherModeVideoStreamSubscriber';
/**
 * TogetherModeSubscriber is responsible for subscribing to together mode events and updating the call context accordingly.
 */
export class TogetherModeSubscriber {
    constructor(callIdRef, context, internalContext, togetherMode) {
        this._featureName = 'togetherMode';
        this.subscribe = () => {
            this._togetherMode.on('togetherModeStreamsUpdated', this.onTogetherModeStreamUpdated);
            this._togetherMode.on('togetherModeSceneUpdated', this.onSceneUpdated);
            this._togetherMode.on('togetherModeSeatingUpdated', this.onSeatUpdated);
        };
        this.unsubscribe = () => {
            this._togetherMode.off('togetherModeStreamsUpdated', this.onTogetherModeStreamUpdated);
            this._togetherMode.off('togetherModeSceneUpdated', this.onSceneUpdated);
            this._togetherMode.off('togetherModeSeatingUpdated', this.onSeatUpdated);
        };
        this.onSceneUpdated = (args) => {
            this._context.setTogetherModeSeatingCoordinates(this._callIdRef.callId, args);
        };
        this.onSeatUpdated = (args) => {
            this._context.setTogetherModeSeatingCoordinates(this._callIdRef.callId, args);
        };
        this.addRemoteVideoStreamSubscriber = (togetherModeVideoStream) => {
            if (this._togetherModeVideoStreamSubscribers.has(togetherModeVideoStream.id)) {
                return;
            }
            this._togetherModeVideoStreamSubscribers.set(togetherModeVideoStream.id, new TogetherModeVideoStreamSubscriber(this._callIdRef, togetherModeVideoStream, this._context));
        };
        this.updateTogetherModeStreams = (addedStreams, removedStreams) => {
            removedStreams.forEach(stream => {
                var _a;
                (_a = this._togetherModeVideoStreamSubscribers.get(stream.id)) === null || _a === void 0 ? void 0 : _a.unsubscribe();
                this._togetherModeVideoStreamSubscribers.delete(stream.id);
                this._internalContext.deleteCallFeatureRenderInfo(this._callIdRef.callId, this._featureName, stream.mediaStreamType);
            });
            addedStreams.filter(stream => stream.isAvailable).forEach(stream => {
                this._internalContext.setCallFeatureRenderInfo(this._callIdRef.callId, this._featureName, stream.mediaStreamType, stream, 'NotRendered', undefined);
                this.addRemoteVideoStreamSubscriber(stream);
            });
            this._context.setTogetherModeVideoStreams(this._callIdRef.callId, addedStreams.filter(stream => stream.isAvailable).map(stream => convertSdkCallFeatureStreamToDeclarativeCallFeatureStream(stream, this._featureName)), removedStreams.map(stream => convertSdkCallFeatureStreamToDeclarativeCallFeatureStream(stream, this._featureName)));
            const notificationTarget = this._togetherMode.togetherModeStream.length ? 'togetherModeStarted' : 'togetherModeEnded';
            this._context.setLatestNotification(this._callIdRef.callId, {
                target: notificationTarget,
                timestamp: new Date()
            });
        };
        this.onTogetherModeStreamUpdated = (args) => {
            this.updateTogetherModeStreams(args.added, args.removed);
        };
        this._callIdRef = callIdRef;
        this._context = context;
        this._internalContext = internalContext;
        this._togetherMode = togetherMode;
        this._togetherModeVideoStreamSubscribers = new Map();
        this.subscribe();
    }
}
//# sourceMappingURL=TogetherModeSubscriber.js.map