!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("eventemitter3"),require("livekit-client")):"function"==typeof define&&define.amd?define(["exports","eventemitter3","livekit-client"],t):t((e||self).retellClientJsSdk={},e.eventemitter3,e.livekitClient)}(this,function(e,t,n){function o(e,t){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},o(e,t)}var i=new TextDecoder;e.RetellWebClient=/*#__PURE__*/function(e){var t,a;function r(){var t;return(t=e.call(this)||this).room=void 0,t.connected=!1,t.isAgentTalking=!1,t.analyzerComponent=void 0,t.captureAudioFrame=void 0,t}a=e,(t=r).prototype=Object.create(a.prototype),t.prototype.constructor=t,o(t,a);var c=r.prototype;return c.startCall=function(e){try{var t=this,o=function(o,i){try{var a=(t.room=new n.Room({audioCaptureDefaults:{autoGainControl:!0,echoCancellation:!0,noiseSuppression:!0,channelCount:1,deviceId:e.captureDeviceId,sampleRate:e.sampleRate},audioOutput:{deviceId:e.playbackDeviceId}}),t.handleRoomEvents(),t.handleAudioEvents(e),t.handleDataEvents(),Promise.resolve(t.room.connect("wss://retell-ai-4ihahnq7.livekit.cloud",e.accessToken)).then(function(){console.log("connected to room",t.room.name),t.room.localParticipant.setMicrophoneEnabled(!0),t.connected=!0,t.emit("call_started")}))}catch(e){return i(e)}return a&&a.then?a.then(void 0,i):a}(0,function(e){t.emit("error","Error starting call"),console.error("Error starting call",e),t.stopCall()});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},c.startAudioPlayback=function(){try{return Promise.resolve(this.room.startAudio()).then(function(){})}catch(e){return Promise.reject(e)}},c.stopCall=function(){var e;this.connected&&(this.connected=!1,this.emit("call_ended"),null==(e=this.room)||e.disconnect(),this.isAgentTalking=!1,delete this.room,this.analyzerComponent&&(this.analyzerComponent.cleanup(),delete this.analyzerComponent),this.captureAudioFrame&&(window.cancelAnimationFrame(this.captureAudioFrame),delete this.captureAudioFrame))},c.mute=function(){this.connected&&this.room.localParticipant.setMicrophoneEnabled(!1)},c.unmute=function(){this.connected&&this.room.localParticipant.setMicrophoneEnabled(!0)},c.captureAudioSamples=function(){var e=this;if(this.connected&&this.analyzerComponent){var t=new Float32Array(this.analyzerComponent.analyser.fftSize);this.analyzerComponent.analyser.getFloatTimeDomainData(t),this.emit("audio",t),this.captureAudioFrame=window.requestAnimationFrame(function(){return e.captureAudioSamples()})}},c.handleRoomEvents=function(){var e=this;this.room.on(n.RoomEvent.ParticipantDisconnected,function(t){"server"===(null==t?void 0:t.identity)&&setTimeout(function(){e.stopCall()},500)}),this.room.on(n.RoomEvent.Disconnected,function(){e.stopCall()})},c.handleAudioEvents=function(e){var t=this;this.room.on(n.RoomEvent.TrackSubscribed,function(o,i,a){o.kind===n.Track.Kind.Audio&&o instanceof n.RemoteAudioTrack&&("agent_audio"===i.trackName&&(t.emit("call_ready"),e.emitRawAudioSamples&&(t.analyzerComponent=n.createAudioAnalyser(o),t.captureAudioFrame=window.requestAnimationFrame(function(){return t.captureAudioSamples()}))),o.attach())})},c.handleDataEvents=function(){var e=this;this.room.on(n.RoomEvent.DataReceived,function(t,n,o,a){try{if("server"!==(null==n?void 0:n.identity))return;var r=i.decode(t),c=JSON.parse(r);"update"===c.event_type?e.emit("update",c):"metadata"===c.event_type?e.emit("metadata",c):"agent_start_talking"===c.event_type?(e.isAgentTalking=!0,e.emit("agent_start_talking")):"agent_stop_talking"===c.event_type?(e.isAgentTalking=!1,e.emit("agent_stop_talking")):"node_transition"===c.event_type&&e.emit("node_transition",c)}catch(e){console.error("Error decoding data received",e)}})},r}(t.EventEmitter)});
