var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import HeadsetService from "softphone-vendor-headsets";
import { SdkHeadsetBase } from "./sdk-headset-base";
import { HeadsetChangesQueue } from '../headsets/headset-utils';
export class SdkHeadsetService extends SdkHeadsetBase {
    constructor(sdk) {
        super(sdk);
        this.headsetLibrary = HeadsetService.getInstance({ logger: this.createDecoratedLogger(), appName: sdk._config.originAppName });
        this.headsetEvents$ = this.headsetLibrary.headsetEvents$;
    }
    createDecoratedLogger() {
        const fns = ['debug', 'log', 'info', 'warn', 'error'];
        const customLogger = {};
        fns.forEach(fn => {
            const orig = this.sdk.logger[fn].bind(this.sdk.logger);
            customLogger[fn] = (message, details, opts) => {
                orig(message, Object.assign(Object.assign({}, details), { conversationInfo: this.sdk.sessionManager.getAllActiveConversations() }), opts);
            };
        });
        return customLogger;
    }
    deviceIsSupported(params) {
        return this.headsetLibrary.deviceIsSupported(params);
    }
    /**
     * Updates the selected device and implementation within the headset library
     * @param newMicId ID associated with the newly selected device
     * @returns void
     */
    updateAudioInputDevice(newMicId, changeReason) {
        var _a;
        const completeDeviceInfo = this.sdk.media.findCachedDeviceByIdAndKind(newMicId, 'audioinput');
        HeadsetChangesQueue.clearQueue();
        this.headsetLibrary.activeMicChange((_a = completeDeviceInfo === null || completeDeviceInfo === void 0 ? void 0 : completeDeviceInfo.label) === null || _a === void 0 ? void 0 : _a.toLowerCase(), changeReason);
    }
    /**
     * Gets the currently selected vendor implementation from the headset library
     * @params none
     * @returns VendorImplementation
     */
    get currentSelectedImplementation() {
        return this.headsetLibrary.selectedImplementation;
    }
    /**
     * Determines if the retry button is necessary to be rendered
     * @params none
     * @returns boolean
     */
    showRetry() {
        const selectedImplementation = this.currentSelectedImplementation;
        if (selectedImplementation === null || selectedImplementation === void 0 ? void 0 : selectedImplementation.disableRetry) {
            return false;
        }
        return !!selectedImplementation
            && !selectedImplementation.isConnected
            && !selectedImplementation.isConnecting;
    }
    /**
     * Attempts to reconnect to the selected vendor's SDK
     * @param micLabel the label that matches the currently selected device
     * @returns Promise<void>
     */
    retryConnection(micLabel) {
        return __awaiter(this, void 0, void 0, function* () {
            return micLabel && this.headsetLibrary.retryConnection(micLabel);
        });
    }
    /**
     * Calls the headset library's incomingCall function to signal to the device
     * to flash the answer call button's light to show an incoming call
     * @param callInfo an object containing the conversationId and possible
     * contactName for the incoming call that will be accepted or rejected
     * @param hasOtherActiveCalls boolean determining if there are other active calls
     * @returns Promise<void>
     */
    setRinging(callInfo, hasOtherActiveCalls) {
        return this.headsetLibrary.incomingCall(callInfo, hasOtherActiveCalls);
    }
    /**
     * Calls the headset library's outgoingCall function to signal to the device
     * to switch on the answer call button's light to show an active call
     * @param callInfo an object containing the conversationId and possible
     * contactName for the call that is outgoing
     * @returns Promise<void>
     */
    outgoingCall(callInfo) {
        return this.headsetLibrary.outgoingCall(callInfo);
    }
    /**
     * Calls the headset library's endCall function to signal to the device
     * to switch off the answer call button's light to show the active call has ended
     * @param conversationId a string representing the conversation that needs to be ended
     * @returns Promise<void>
     */
    endCurrentCall(conversationId, hasOtherActiveCalls) {
        return __awaiter(this, void 0, void 0, function* () {
            if (conversationId) {
                return this.headsetLibrary.endCall(conversationId, hasOtherActiveCalls);
            }
        });
    }
    /**
     * Calls the headset library's endAllCalls() function to signal the device
     * to switch off the answer call button's light to show the active calls have all ended
     * @returns Promise<void>
     */
    endAllCalls() {
        return this.headsetLibrary.endAllCalls();
    }
    /**
     * Calls the headset library's answerIncomingCall function to signal the device
     * to switch on the answer call button's light to show the call is now active
     * @param conversationId a string representing the incoming call that is being
     * answered
     * @returns Promise<void>
     */
    answerIncomingCall(conversationId, autoAnswer) {
        return this.headsetLibrary.answerCall(conversationId, autoAnswer);
    }
    /**
     * Calls the headset library's rejectIncomingCall function to signal the device
     * to switch on the answer call button's light to show the call has been rejected
     * @param conversationId a string representing the incoming call that is being
     * rejected
     * @returns Promise<void>
     */
    rejectIncomingCall(conversationId, expectExistingConversation = true) {
        return this.headsetLibrary.rejectCall(conversationId, expectExistingConversation);
    }
    /**
     * Calls the headset library's setMute function to signal the device to switch on
     * or off (depending on the value of the passed in param) the mute call button's
     * light to show the call has been muted or unmuted respectively
     * @param isMuted boolean to show if the call should be muted(true) or unmuted(false)
     * @returns Promise<void>
     */
    setMute(isMuted) {
        return this.headsetLibrary.setMute(isMuted);
    }
    /**
     * Calls the headset library's setHold function to signal the device to switch on
     * or off (depending on the value of the passed in param, isHeld) the hold call button's
     * light to show the call has been held or resumed respectively
     * @param conversationId string representing the call that is to be held or resumed
     * @param isHeld boolean to show if the call should be held(true) or resumed(false)
     * @returns Promise<void>
     */
    setHold(conversationId, isHeld) {
        return this.headsetLibrary.setHold(conversationId, isHeld);
    }
    /**
     * Calls the headset library's resetHeadsetStateForCall function to effectively rest
     * the selected device's state.  This is to help with certain scenarios around headset
     * orchestration where a device is signaled to start ringing but never stops ringing
     * @param conversationId string representing the call whose state will be reset
     * @returns Promise<void>
     */
    resetHeadsetStateForCall(conversationId) {
        return this.headsetLibrary.resetHeadsetStateForCall(conversationId);
    }
}
//# sourceMappingURL=sdk-headset-service.js.map