// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
 * @private
 */
export class CapabilitiesSubscriber {
    constructor(callIdRef, context, capabilities) {
        this._capabilitiesInitialized = false;
        this.subscribe = () => {
            this._capabilitiesFeature.on('capabilitiesChanged', this.capabilitiesChanged);
        };
        this.unsubscribe = () => {
            this._capabilitiesFeature.off('capabilitiesChanged', this.capabilitiesChanged);
        };
        this.capabilitiesChanged = (data) => {
            this._context.setCapabilities(this._callIdRef.callId, this._capabilitiesFeature.capabilities, data);
            if (data.oldValue.viewAttendeeNames !== data.newValue.viewAttendeeNames) {
                this._context.setHideAttendeeNames(this._callIdRef.callId, data);
            }
            this.setUnmuteMicAndTurnVideoOnNotification(data);
            this._capabilitiesInitialized = true;
        };
        this.setUnmuteMicAndTurnVideoOnNotification = (data) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
            if (((_a = data.oldValue.turnVideoOn) === null || _a === void 0 ? void 0 : _a.isPresent) !== ((_b = data.newValue.turnVideoOn) === null || _b === void 0 ? void 0 : _b.isPresent)) {
                if (((_c = data.oldValue.turnVideoOn) === null || _c === void 0 ? void 0 : _c.isPresent) === false && ((_d = data.newValue.turnVideoOn) === null || _d === void 0 ? void 0 : _d.isPresent) && this._capabilitiesInitialized) {
                    const capabilityTurnVideoOnAbsent = this._context.getState().latestNotifications.capabilityTurnVideoOnAbsent;
                    if (capabilityTurnVideoOnAbsent) {
                        this._context.deleteLatestNotification('capabilityTurnVideoOnAbsent', this._callIdRef.callId);
                    }
                    const capabilityTurnVideoOnPresent = this._context.getState().latestNotifications.capabilityTurnVideoOnPresent;
                    if (!capabilityTurnVideoOnPresent) {
                        this._context.setLatestNotification(this._callIdRef.callId, {
                            target: 'capabilityTurnVideoOnPresent',
                            timestamp: new Date(Date.now())
                        });
                    }
                }
                if (((_e = data.newValue.turnVideoOn) === null || _e === void 0 ? void 0 : _e.isPresent) === false) {
                    const capabilityTurnVideoOnPresent = this._context.getState().latestNotifications.capabilityTurnVideoOnPresent;
                    if (capabilityTurnVideoOnPresent) {
                        this._context.deleteLatestNotification('capabilityTurnVideoOnPresent', this._callIdRef.callId);
                    }
                    const capabilityTurnVideoOnAbsent = this._context.getState().latestNotifications.capabilityTurnVideoOnAbsent;
                    if (!capabilityTurnVideoOnAbsent) {
                        this._context.setLatestNotification(this._callIdRef.callId, {
                            target: 'capabilityTurnVideoOnAbsent',
                            timestamp: new Date(Date.now())
                        });
                    }
                }
            }
            if (((_f = data.oldValue.unmuteMic) === null || _f === void 0 ? void 0 : _f.isPresent) !== ((_g = data.newValue.unmuteMic) === null || _g === void 0 ? void 0 : _g.isPresent)) {
                if (((_h = data.oldValue.unmuteMic) === null || _h === void 0 ? void 0 : _h.isPresent) === false && ((_j = data.newValue.unmuteMic) === null || _j === void 0 ? void 0 : _j.isPresent) && this._capabilitiesInitialized) {
                    const capabilityUnmuteMicAbsent = this._context.getState().latestNotifications.capabilityUnmuteMicAbsent;
                    if (capabilityUnmuteMicAbsent) {
                        this._context.deleteLatestNotification('capabilityUnmuteMicAbsent', this._callIdRef.callId);
                    }
                    const capabilityUnmuteMicPresent = this._context.getState().latestNotifications.capabilityUnmuteMicPresent;
                    if (!capabilityUnmuteMicPresent) {
                        this._context.setLatestNotification(this._callIdRef.callId, {
                            target: 'capabilityUnmuteMicPresent',
                            timestamp: new Date(Date.now())
                        });
                    }
                }
                if (((_k = data.newValue.unmuteMic) === null || _k === void 0 ? void 0 : _k.isPresent) === false) {
                    const capabilityUnmuteMicPresent = this._context.getState().latestNotifications.capabilityUnmuteMicPresent;
                    if (capabilityUnmuteMicPresent) {
                        this._context.deleteLatestNotification('capabilityUnmuteMicPresent', this._callIdRef.callId);
                    }
                    const capabilityUnmuteMicAbsent = this._context.getState().latestNotifications.capabilityUnmuteMicAbsent;
                    if (!capabilityUnmuteMicAbsent) {
                        this._context.setLatestNotification(this._callIdRef.callId, {
                            target: 'capabilityUnmuteMicAbsent',
                            timestamp: new Date(Date.now())
                        });
                    }
                }
            }
        };
        this._callIdRef = callIdRef;
        this._context = context;
        this._capabilitiesFeature = capabilities;
        this.subscribe();
    }
}
//# sourceMappingURL=CapabilitiesSubscriber.js.map