"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../node_modules/tslib/tslib.es6.js"),t=require("./janus.js"),r=require("../../store/index.js"),s=require("../../node_modules/webrtc-adapter/src/js/adapter_core.js"),a=require("../devices/devices.js");function o(e,s){return new Promise((function(a,o){var n=r.store.getState().webrtc.sipcall;n.createOffer({tracks:s,success:function(r){t.default.debug("Got SDP!"),t.default.debug(r),n.send({message:{request:"call",uri:e,autoaccept_reinvites:!1},jsep:r}),a(!0)},error:function(e){t.default.error("WebRTC error...",e),t.default.error("WebRTC error call on createOffer: ",e),o(!1)}})}))}exports.answerWebRTC=function(){var e,s=r.store.getState().webrtc,o=s.sipcall,n=s.jsepGlobal;if(console.log("[JSEP] answerWebRTC called",{hasSipcall:!!o,hasJsepGlobal:!!n,sipcallId:null===(e=null==o?void 0:o.getId)||void 0===e?void 0:e.call(o),timestamp:(new Date).toISOString()}),o&&n){var c=a.getCurrentAudioInputDeviceId(),i=[];c?i.push({type:"audio",capture:{deviceId:{exact:c}},recv:!0}):i.push({type:"audio",capture:!0,recv:!0});try{o.createAnswer({jsep:n,tracks:i,success:function(e){o.send({message:{request:"accept",autoaccept_reinvites:!1},jsep:e})},error:function(e){t.default.error("WebRTC error:",e),e&&("object"==typeof e&&469===e.code||"string"==typeof e&&e.includes("Unexpected ANSWER"))?(console.warn('Got "Unexpected ANSWER" error, forcing the answer anyway'),o.send({message:{request:"accept",autoaccept_reinvites:!1}})):o.send({message:{request:"decline",code:480}})}})}catch(e){console.error("Exception in createAnswer:",e),o.send({message:{request:"accept",autoaccept_reinvites:!1}})}}},exports.call=o,exports.callSipURI=function(r){var s=this;a.getSupportedDevices((function(){return e.__awaiter(s,void 0,void 0,(function(){var s,n;return e.__generator(this,(function(e){switch(e.label){case 0:return t.default.log("This is a SIP call"),s=a.getCurrentAudioInputDeviceId(),n=[],s?n.push({type:"audio",capture:{deviceId:{exact:s}},recv:!0}):n.push({type:"audio",capture:!0,recv:!0}),[4,o(r,n)];case 1:return e.sent(),[2]}}))}))}))},exports.detach=function(){var e=r.store.getState().webrtc.sipcall;if(e)return e.detach()},exports.handleRemote=function(e){var t=r.store.getState().webrtc.sipcall;t&&t.handleRemoteJsep({jsep:e,error:function(e){console.error("WebRTC error... "+JSON.stringify(e));t.send({message:{request:"hangup"}}),t.hangup()}})},exports.hangup=function(){var e=r.store.getState().webrtc.sipcall;e&&e.send({message:{request:"hangup"}})},exports.muteWebRTC=function(){var e=r.store.getState().webrtc.sipcall;return e.muteAudio(),e.isAudioMuted()},exports.pauseWebRTC=function(){var e=r.store.getState().webrtc.sipcall;try{return e.send({message:{request:"hold"}}),!0}catch(e){return console.error(e),!1}},exports.register=function(e){var t=e.sipExten,s=e.sipSecret,a=e.sipHost,o=e.sipPort,n=r.store.getState().webrtc,c=n.sipcall,i=n.registered,u=r.store.getState().currentUser.name;i?console.log("[REGISTER] Already registered, skipping registration request",{sipExten:t,timestamp:(new Date).toISOString()}):c&&(console.log("[REGISTER] Sending registration request",{sipExten:t,timestamp:(new Date).toISOString()}),c.send({message:{request:"register",username:"sip:".concat(t,"@").concat(a),display_name:u||"",secret:s,proxy:"sip:".concat(a,":").concat(o),outbound_proxy:"sip:".concat(a,":").concat(o),sips:!1,refresh:!1}}))},exports.sendDTMF=function(e){var t=r.store.getState().webrtc.sipcall;"chrome"===s.default.browserDetails.browser?t.dtmf({dtmf:{tones:e}}):t.send({message:{request:"dtmf_info",digit:"".concat(e)}})},exports.unmuteWebRTC=function(){var e=r.store.getState().webrtc.sipcall;return e.unmuteAudio(),!e.isAudioMuted()},exports.unpauseWebRTC=function(){var e=r.store.getState().webrtc.sipcall;try{return e.send({message:{request:"unhold"}}),!0}catch(e){return console.error(e),!1}},exports.unregister=function(){var e=r.store.getState().webrtc.sipcall;e&&e.send({message:{request:"unregister"}})};
//# sourceMappingURL=messages.js.map
