// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
const CALL_REJECTED_CODE = 603;
const CALL_ENDED_CODE = 0;
const CALL_TRANSFER_SUBCODE = 7015;
/**
 * @private
 */
export class CallingSoundSubscriber {
    constructor(call, callees, sounds) {
        this.playingSounds = false;
        this.onCallStateChanged = () => {
            this.call.on('stateChanged', () => {
                var _a, _b, _c, _d, _e, _f, _g;
                if (shouldPlayRinging(this.call, this.callees) && ((_a = this.soundsLoaded) === null || _a === void 0 ? void 0 : _a.callRingingSound)) {
                    this.soundsLoaded.callRingingSound.loop = true;
                    this.playSound(this.soundsLoaded.callRingingSound);
                    this.playingSounds = true;
                }
                if (!shouldPlayRinging(this.call, this.callees) && ((_b = this.soundsLoaded) === null || _b === void 0 ? void 0 : _b.callRingingSound)) {
                    this.soundsLoaded.callRingingSound.loop = false;
                    this.soundsLoaded.callRingingSound.pause();
                    this.playingSounds = false;
                }
                if (this.call.state === 'Disconnected') {
                    if (((_c = this.soundsLoaded) === null || _c === void 0 ? void 0 : _c.callBusySound) && ((_d = this.call.callEndReason) === null || _d === void 0 ? void 0 : _d.code) === CALL_REJECTED_CODE) {
                        this.playSound(this.soundsLoaded.callBusySound);
                        this.playingSounds = true;
                    }
                    else if (((_e = this.soundsLoaded) === null || _e === void 0 ? void 0 : _e.callEndedSound) && ((_f = this.call.callEndReason) === null || _f === void 0 ? void 0 : _f.code) === CALL_ENDED_CODE && ((_g = this.call.callEndReason) === null || _g === void 0 ? void 0 : _g.subCode) !== CALL_TRANSFER_SUBCODE) {
                        this.playSound(this.soundsLoaded.callEndedSound);
                        this.playingSounds = true;
                    }
                }
            });
        };
        this.call = call;
        this.callees = callees;
        if (sounds) {
            this.soundsLoaded = this.loadSounds(sounds);
            this.subscribeCallSoundEvents();
        }
    }
    subscribeCallSoundEvents() {
        this.onCallStateChanged();
    }
    unsubscribeAll() {
        var _a, _b;
        (_a = this.call) === null || _a === void 0 ? void 0 : _a.off('stateChanged', this.onCallStateChanged);
        if ((_b = this.soundsLoaded) === null || _b === void 0 ? void 0 : _b.callRingingSound) {
            this.soundsLoaded.callRingingSound.pause();
        }
    }
    pauseSounds() {
        var _a, _b, _c;
        if ((_a = this.soundsLoaded) === null || _a === void 0 ? void 0 : _a.callRingingSound) {
            this.soundsLoaded.callRingingSound.pause();
            this.playingSounds = false;
        }
        if ((_b = this.soundsLoaded) === null || _b === void 0 ? void 0 : _b.callEndedSound) {
            this.soundsLoaded.callEndedSound.pause();
            this.playingSounds = false;
        }
        if ((_c = this.soundsLoaded) === null || _c === void 0 ? void 0 : _c.callBusySound) {
            this.soundsLoaded.callBusySound.pause();
            this.playingSounds = false;
        }
    }
    loadSounds(sounds) {
        var _a, _b, _c;
        let callEndedSound;
        if (sounds === null || sounds === void 0 ? void 0 : sounds.callEnded) {
            callEndedSound = new Audio((_a = sounds === null || sounds === void 0 ? void 0 : sounds.callEnded) === null || _a === void 0 ? void 0 : _a.url);
            callEndedSound.preload = 'auto';
        }
        let callRingingSound;
        if (sounds === null || sounds === void 0 ? void 0 : sounds.callRinging) {
            callRingingSound = new Audio((_b = sounds === null || sounds === void 0 ? void 0 : sounds.callRinging) === null || _b === void 0 ? void 0 : _b.url);
            callRingingSound.preload = 'auto';
        }
        let callBusySound;
        if (sounds === null || sounds === void 0 ? void 0 : sounds.callBusy) {
            callBusySound = new Audio((_c = sounds === null || sounds === void 0 ? void 0 : sounds.callBusy) === null || _c === void 0 ? void 0 : _c.url);
            callBusySound.preload = 'auto';
        }
        return {
            callEndedSound,
            callRingingSound,
            callBusySound
        };
    }
    playSound(sound) {
        sound.play().catch(e => {
            console.error(e, 'Failed to play sound, check loader config to make sure it is correct');
        });
    }
}
/**
 * Helper function to allow the calling sound subscriber to determine when to play the ringing
 * sound when making an outbound call.
 */
const shouldPlayRinging = (call, callees) => {
    if (callees && callees[0] && (call.state === 'Ringing' || call.state === 'Connecting')) {
        return true;
    }
    else {
        return false;
    }
};
//# sourceMappingURL=CallingSoundSubscriber.js.map