@telnyx/webrtc 2.27.0-beta.6 → 2.27.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bundle.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).TelnyxWebRTC={})}(this,(function(e){"use strict";function t(e,t){var i={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(i[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(n=Object.getOwnPropertySymbols(e);s<n.length;s++)t.indexOf(n[s])<0&&Object.prototype.propertyIsEnumerable.call(e,n[s])&&(i[n[s]]=e[n[s]])}return i}function i(e,t,i,n){return new(i||(i=Promise))((function(s,o){function r(e){try{c(n.next(e))}catch(e){o(e)}}function a(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}c((n=n.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;var n="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto),s=new Uint8Array(16);function o(){if(!n)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return n(s)}for(var r=[],a=0;a<256;++a)r[a]=(a+256).toString(16).substr(1);function c(e,t,i){var n=t&&i||0;"string"==typeof e&&(t="binary"===e?new Array(16):null,e=null);var s=(e=e||{}).random||(e.rng||o)();if(s[6]=15&s[6]|64,s[8]=63&s[8]|128,t)for(var a=0;a<16;++a)t[n+a]=s[a];return t||function(e,t){var i=t||0,n=r;return[n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]]].join("")}(s)}const d={SDP_CREATE_OFFER_FAILED:40001,SDP_CREATE_ANSWER_FAILED:40002,SDP_SET_LOCAL_DESCRIPTION_FAILED:40003,SDP_SET_REMOTE_DESCRIPTION_FAILED:40004,SDP_SEND_FAILED:40005,MEDIA_MICROPHONE_PERMISSION_DENIED:42001,MEDIA_DEVICE_NOT_FOUND:42002,MEDIA_GET_USER_MEDIA_FAILED:42003,HOLD_FAILED:44001,INVALID_CALL_PARAMETERS:44002,BYE_SEND_FAILED:44003,SUBSCRIBE_FAILED:44004,PEER_CLOSED_DURING_INIT:44005,WEBSOCKET_CONNECTION_FAILED:45001,WEBSOCKET_ERROR:45002,RECONNECTION_EXHAUSTED:45003,GATEWAY_FAILED:45004,LOGIN_FAILED:46001,INVALID_CREDENTIALS:46002,AUTHENTICATION_REQUIRED:46003,ICE_RESTART_FAILED:47001,NETWORK_OFFLINE:48001,UNEXPECTED_ERROR:49001},l={HIGH_RTT:31001,HIGH_JITTER:31002,HIGH_PACKET_LOSS:31003,LOW_MOS:31004,LOW_LOCAL_AUDIO:31005,LOW_BYTES_RECEIVED:32001,LOW_BYTES_SENT:32002,ICE_CONNECTIVITY_LOST:33001,ICE_GATHERING_TIMEOUT:33002,ICE_GATHERING_EMPTY:33003,PEER_CONNECTION_FAILED:33004,ONLY_HOST_ICE_CANDIDATES:33005,ANSWER_WHILE_PEER_ACTIVE:33006,ICE_CANDIDATE_PAIR_CHANGED:33008,DUPLICATE_INBOUND_ANSWER:33007,TOKEN_EXPIRING_SOON:34001,SESSION_NOT_REATTACHED:35001,SIGNALING_HEALTH_PROBE_TIMEOUT:36001,SIGNALING_REQUEST_TIMEOUT:36002,SIGNALING_RECOVERY_REQUIRED:36003,MEDIA_RECOVERY_REQUIRED:36004},{SDP_CREATE_OFFER_FAILED:h,SDP_CREATE_ANSWER_FAILED:u,SDP_SET_LOCAL_DESCRIPTION_FAILED:p,SDP_SET_REMOTE_DESCRIPTION_FAILED:g,SDP_SEND_FAILED:v,MEDIA_MICROPHONE_PERMISSION_DENIED:m,MEDIA_DEVICE_NOT_FOUND:f,MEDIA_GET_USER_MEDIA_FAILED:_,HOLD_FAILED:S,INVALID_CALL_PARAMETERS:b,BYE_SEND_FAILED:y,SUBSCRIBE_FAILED:I,PEER_CLOSED_DURING_INIT:E,WEBSOCKET_CONNECTION_FAILED:C,WEBSOCKET_ERROR:w,RECONNECTION_EXHAUSTED:T,GATEWAY_FAILED:k,LOGIN_FAILED:R,INVALID_CREDENTIALS:A,AUTHENTICATION_REQUIRED:O,ICE_RESTART_FAILED:N,NETWORK_OFFLINE:L,UNEXPECTED_ERROR:D}=d,{HIGH_RTT:P,HIGH_JITTER:M,HIGH_PACKET_LOSS:x,LOW_MOS:U,LOW_LOCAL_AUDIO:F,LOW_BYTES_RECEIVED:$,LOW_BYTES_SENT:H,ICE_CONNECTIVITY_LOST:j,ICE_GATHERING_TIMEOUT:G,ICE_GATHERING_EMPTY:W,PEER_CONNECTION_FAILED:B,ONLY_HOST_ICE_CANDIDATES:V,ANSWER_WHILE_PEER_ACTIVE:q,ICE_CANDIDATE_PAIR_CHANGED:Y,DUPLICATE_INBOUND_ANSWER:K,TOKEN_EXPIRING_SOON:J,SESSION_NOT_REATTACHED:Q,SIGNALING_HEALTH_PROBE_TIMEOUT:X,SIGNALING_REQUEST_TIMEOUT:z,SIGNALING_RECOVERY_REQUIRED:Z,MEDIA_RECOVERY_REQUIRED:ee}=l,te=/^a=candidate:.+typ (srflx|prflx|relay)/m,ie="wss://rtc.telnyx.com",ne={NORMAL_CLOSURE:1e3,GOING_AWAY:1001,PROTOCOL_ERROR:1002,UNSUPPORTED_DATA:1003,NO_STATUS_RECEIVED:1005,ABNORMAL_CLOSURE:1006,INVALID_FRAME_PAYLOAD:1007,POLICY_VIOLATION:1008,MESSAGE_TOO_BIG:1009,INTERNAL_ERROR:1011},se={urls:"stun:stun.l.google.com:19302"},oe=[{urls:"stun:stun.telnyx.com:3478"},se,{urls:"turn:turn.telnyx.com:3478?transport=udp",username:"testuser",credential:"testpassword"},{urls:"turn:turn.telnyx.com:3478?transport=tcp",username:"testuser",credential:"testpassword"}],re=[{urls:"stun:stundev.telnyx.com:3478"},se,{urls:"turn:turndev.telnyx.com:3478?transport=udp",username:"testuser",credential:"testpassword"},{urls:"turn:turndev.telnyx.com:3478?transport=tcp",username:"testuser",credential:"testpassword"}];var ae;(ae=e.SwEvent||(e.SwEvent={})).SocketOpen="telnyx.socket.open",ae.SocketClose="telnyx.socket.close",ae.SocketError="telnyx.socket.error",ae.SocketMessage="telnyx.socket.message",ae.SpeedTest="telnyx.internal.speedtest",ae.SocketActivity="telnyx.internal.socketActivity",ae.Ready="telnyx.ready",ae.Error="telnyx.error",ae.Warning="telnyx.warning",ae.Notification="telnyx.notification",ae.StatsFrame="telnyx.stats.frame",ae.StatsReport="telnyx.stats.report",ae.Messages="telnyx.messages",ae.Calls="telnyx.calls",ae.MediaError="telnyx.rtc.mediaError",ae.PeerConnectionFailureError="telnyx.rtc.peerConnectionFailureError",ae.PeerConnectionSignalingStateClosed="telnyx.rtc.peerConnectionSignalingStateClosed";const ce={40001:{name:"SDP_CREATE_OFFER_FAILED",message:"Failed to create call offer",description:"The browser was unable to generate a local SDP offer. This typically indicates a WebRTC API error or invalid media constraints.",causes:["Browser WebRTC API error","Missing or invalid media constraints"],solutions:["Check getUserMedia permissions","Verify ICE server configuration"]},40002:{name:"SDP_CREATE_ANSWER_FAILED",message:"Failed to answer the call",description:"The browser was unable to generate a local SDP answer. The remote offer may be invalid or the browser state inconsistent.",causes:["Browser WebRTC API error","Invalid remote SDP offer"],solutions:["Retry the call","Check browser WebRTC compatibility"]},40003:{name:"SDP_SET_LOCAL_DESCRIPTION_FAILED",message:"Failed to apply local call settings",description:"setLocalDescription() was rejected by the browser. The generated SDP may be malformed or the browser state may be inconsistent.",causes:["Malformed SDP","Browser state inconsistency"],solutions:["Retry the call"]},40004:{name:"SDP_SET_REMOTE_DESCRIPTION_FAILED",message:"Failed to apply remote call settings",description:"setRemoteDescription() was rejected by the browser. The remote SDP may be malformed or contain unsupported codecs.",causes:["Malformed remote SDP","Browser codec mismatch"],solutions:["Retry the call","Check codec configuration"]},40005:{name:"SDP_SEND_FAILED",message:"Failed to send call data to server",description:"The Invite or Answer message could not be delivered via the signaling WebSocket. The connection may have been lost.",causes:["WebSocket connection lost","Server error"],solutions:["Check network connectivity","Retry the call"]},42001:{name:"MEDIA_MICROPHONE_PERMISSION_DENIED",message:"Microphone access denied",description:"The user or operating system denied microphone permission. The browser permission prompt was dismissed or OS-level access is disabled.",causes:["User denied browser permission prompt","OS-level microphone access disabled"],solutions:["Ask user to grant microphone permission in browser settings"]},42002:{name:"MEDIA_DEVICE_NOT_FOUND",message:"No microphone found",description:"The requested audio input device is not available. No microphone is connected, the device was disconnected, or an invalid deviceId was specified.",causes:["No microphone connected","Device was disconnected","Invalid deviceId"],solutions:["Check that a microphone is connected","Select a valid audio input device"]},42003:{name:"MEDIA_GET_USER_MEDIA_FAILED",message:"Failed to access microphone",description:"getUserMedia() was rejected for an unexpected reason. The device may be in use by another application or the browser encountered an internal error.",causes:["Browser error","Device in use by another application"],solutions:["Close other applications using the microphone","Retry"]},44005:{name:"PEER_CLOSED_DURING_INIT",message:"Call was closed during setup",description:"The PeerConnection was closed (e.g. by hangup()) while peer.init() was still running. This is a race condition: an async operation such as setRemoteDescription, getUserMedia, or the media recovery flow yielded control, and close() ran during that gap. The init() cannot continue because the underlying RTCPeerConnection has been destroyed.",causes:["call.hangup() or call.close() was called while the call was still setting up","A WebSocket Bye message arrived during getUserMedia prompt or SDP negotiation","User clicked hangup/decline before media permissions were granted"],solutions:["This is expected if the user intentionally hung up during setup — no action needed","If this happens frequently without user action, check for automatic hangup triggers that may fire too early"]},44001:{name:"HOLD_FAILED",message:"Failed to hold the call",description:"The server rejected or did not respond to the hold request. The WebSocket connection may have been lost during the operation.",causes:["Server error","WebSocket connection lost during hold"],solutions:["Retry the hold operation","Check network connectivity"]},44002:{name:"INVALID_CALL_PARAMETERS",message:"Invalid call parameters",description:"The call could not be initiated because required parameters are missing or invalid. For example, no destination number was provided to newCall().",causes:["Missing destinationNumber in call options","Invalid or empty call parameters"],solutions:["Provide a valid destinationNumber when calling newCall()","Check the call options object for required fields"]},44003:{name:"BYE_SEND_FAILED",message:"Failed to hang up cleanly",description:"The hangup signal could not be delivered to the server. The call was terminated locally but the server may not be aware.",causes:["WebSocket connection lost before BYE sent"],solutions:["No action needed — call is terminated locally","Check network connectivity"]},44004:{name:"SUBSCRIBE_FAILED",message:"Failed to subscribe to call events",description:"The Verto subscribe request for the call channel failed. This may prevent receiving call state updates from the server.",causes:["WebSocket connection lost during subscribe","Server rejected the subscription request"],solutions:["Check network connectivity","Retry the call"]},45001:{name:"WEBSOCKET_CONNECTION_FAILED",message:"Unable to connect to server",description:"The WebSocket connection to the signaling server could not be established. The server may be unreachable, the URL may be incorrect, or a firewall may be blocking the connection.",causes:["Server unreachable","Incorrect WebSocket URL","Firewall blocking WebSocket connections","Network interruption"],solutions:["Check network connectivity","Verify the signaling server URL","Ensure WebSocket connections are not blocked by a firewall"]},45002:{name:"WEBSOCKET_ERROR",message:"Connection to server lost",description:"An error occurred on the WebSocket connection after it was established. The connection may have been dropped due to network issues or server-side closure.",causes:["Network interruption","Server closed the connection","Idle timeout"],solutions:["Check network connectivity","SDK will attempt automatic reconnection if configured"]},45003:{name:"RECONNECTION_EXHAUSTED",message:"Unable to reconnect to server",description:"All automatic reconnection attempts have been exhausted. The SDK tried to re-establish the WebSocket connection multiple times but failed on every attempt.",causes:["Prolonged network outage","Server unreachable","Firewall or proxy blocking reconnection"],solutions:["Check network connectivity","Call client.disconnect() and client.connect() to manually retry","Notify the user that the connection was lost"]},45004:{name:"GATEWAY_FAILED",message:"Gateway connection failed",description:"The upstream gateway reported a FAILED or FAIL_WAIT state. The signaling server could not establish or maintain a connection to the gateway. When autoReconnect is disabled, this is immediately fatal. When enabled, the SDK will retry until RECONNECTION_EXHAUSTED.",causes:["Gateway down or unreachable","Server-side infrastructure issue","Network partition between signaling server and gateway"],solutions:["Wait for automatic reconnection (if autoReconnect is enabled)","Call client.disconnect() and client.connect() to manually retry","Check Telnyx service status"]},46001:{name:"LOGIN_FAILED",message:"Authentication failed",description:"The login request was rejected by the server. The credentials may be invalid, expired, or the account may be suspended.",causes:["Invalid credentials (username/password or token)","Expired authentication token","Account suspended or disabled"],solutions:["Verify credentials","Generate a new authentication token","Check account status"]},46002:{name:"INVALID_CREDENTIALS",message:"Invalid credential parameters",description:"The SDK rejected the login options before sending any request to the server. This is an internal client-side validation guard — the credentials object is missing required fields or has an invalid structure. No network request was made.",causes:["Missing login and password fields","Missing or malformed authentication token","Invalid combination of credential fields in the options object"],solutions:["Provide valid login/password or a valid authentication token","Check the TelnyxRTC constructor options against the documentation","Ensure the credential object matches one of the supported auth modes (credentials, token, or anonymous)"]},46003:{name:"AUTHENTICATION_REQUIRED",message:"Authentication required",description:"The server rejected a request because the session is not authenticated. This can happen when the client sends a message (e.g. Invite, Subscribe, or Ping) before login completes, after a token expires mid-session, or after the server drops the authenticated state for any reason.",causes:["Message sent before login completed","Authentication token expired during the session","Server-side session was invalidated","WebSocket reconnected but re-authentication did not complete"],solutions:["Ensure the client is fully logged in before sending messages","Re-authenticate using client.login() with fresh credentials","Listen for telnyx.ready before making calls or sending requests"]},47001:{name:"ICE_RESTART_FAILED",message:"ICE restart failed",description:"The ICE restart Modify request could not be sent or the server returned an error. The media path could not be recovered via ICE restart.",causes:["WebSocket connection lost during ICE restart","Server rejected the Modify request","Timeout waiting for server response"],solutions:["The call may recover via WebSocket reconnect + Attach","If the call does not recover, hang up and retry"]},48001:{name:"NETWORK_OFFLINE",message:"Device is offline",description:"The browser reported that the device has lost network connectivity (navigator.onLine === false). All WebSocket and media connections will fail until the network is restored.",causes:["Wi-Fi or ethernet disconnected","Airplane mode enabled","Network interface went down"],solutions:["Check network connectivity","Reconnect to Wi-Fi or ethernet","Disable airplane mode"]},49001:{name:"UNEXPECTED_ERROR",message:"An unexpected error occurred",description:"An error was thrown that does not match any known SDK error category. This is a catch-all for unclassified failures.",causes:["Unknown or unhandled error condition"],solutions:["Check the originalError property for the underlying cause","Report the issue if it persists"]}},de={31001:{name:"HIGH_RTT",message:"High network latency detected",description:"Round-trip time (RTT) exceeded the threshold for multiple consecutive samples. High latency causes perceptible audio delays.",causes:["Poor network connection","Geographic distance to media server","Network congestion"],solutions:["Check network connectivity","Use a wired connection instead of Wi-Fi","Close bandwidth-heavy applications"]},31002:{name:"HIGH_JITTER",message:"High jitter detected",description:"Jitter (variability in packet arrival time) exceeded the threshold for multiple consecutive samples. High jitter causes crackling and choppy audio.",causes:["Network congestion","Unstable Wi-Fi connection","Overloaded network equipment"],solutions:["Use a wired connection instead of Wi-Fi","Close bandwidth-heavy applications","Check network equipment"]},31003:{name:"HIGH_PACKET_LOSS",message:"High packet loss detected",description:"Packet loss exceeded the threshold for multiple consecutive samples. High packet loss causes choppy audio or dropped calls.",causes:["Network congestion","Unstable connection","Firewall or QoS misconfiguration"],solutions:["Check network connectivity","Use a wired connection","Contact network administrator"]},31004:{name:"LOW_MOS",message:"Low call quality score",description:"Mean Opinion Score (MOS) dropped below the acceptable threshold for multiple consecutive samples. This is a composite indicator of overall call quality.",causes:["Combination of high latency, jitter, and/or packet loss","Poor network conditions"],solutions:["Check network connectivity","Use a wired connection","Close bandwidth-heavy applications"]},31005:{name:"LOW_LOCAL_AUDIO",message:"Low local microphone audio detected",description:"Local outbound audio level stayed below the acceptable threshold before the microphone produced real audio, or stayed silent for a long continuous window after audio was confirmed. This may indicate that the microphone is not capturing enough audio even while RTP is being sent.",causes:["Microphone input level is too low","Wrong microphone selected","Microphone is obstructed or too far from the speaker","Operating system input gain is muted or very low"],solutions:["Check the selected microphone","Increase microphone input gain","Move closer to the microphone","Verify the microphone is not muted at the operating system or hardware level"]},32001:{name:"LOW_BYTES_RECEIVED",message:"No audio data received",description:"No bytes have been received from the remote party for multiple consecutive seconds. This may indicate a network interruption or remote-side issue.",causes:["Network interruption","Remote party microphone issue","Firewall blocking inbound media"],solutions:["Check network connectivity","Ask remote party to check their microphone","Check firewall rules for media ports"]},32002:{name:"LOW_BYTES_SENT",message:"No audio data being sent",description:"No bytes have been sent for multiple consecutive seconds. This may indicate a local microphone issue or network interruption.",causes:["Microphone muted or disconnected","Network interruption","Local media track ended"],solutions:["Check that the microphone is not muted","Verify the microphone is still connected","Check network connectivity"]},33001:{name:"ICE_CONNECTIVITY_LOST",message:"Connection interrupted",description:"The ICE connection transitioned to the disconnected state. The previously selected connection path was lost and renegotiation may be required. The connection may recover automatically.",causes:["Temporary network interruption","Network interface change (e.g. Wi-Fi to cellular)","NAT rebinding"],solutions:["Wait for automatic recovery","Check network connectivity"]},33002:{name:"ICE_GATHERING_TIMEOUT",message:"ICE gathering timed out",description:"ICE candidate gathering did not complete within the safety timeout. This is typically caused by network restrictions blocking STUN/TURN. The call may still succeed if candidates arrive late.",causes:["Firewall blocking STUN/TURN","Network unreachable","STUN/TURN server not responding"],solutions:["Check STUN/TURN server reachability","Ensure UDP traffic is not blocked","Try forceRelayCandidate option"]},33003:{name:"ICE_GATHERING_EMPTY",message:"No ICE candidates gathered",description:"No ICE candidates were gathered after sending the initial SDP. This may indicate a firewall blocking all STUN/TURN traffic or no available network interface.",causes:["Firewall blocking all STUN/TURN traffic","No network interface available","VPN blocking UDP"],solutions:["Check STUN/TURN server reachability","Ensure UDP traffic is not blocked","Use forceRelayCandidate option"]},33004:{name:"PEER_CONNECTION_FAILED",message:"Connection failed",description:"RTCPeerConnection entered the failed state. This is a recoverable condition — the SDK may attempt ICE restart or the connection may recover. If it does not recover, the call will eventually be terminated.",causes:["ICE failure","DTLS handshake failure","Prolonged network interruption"],solutions:["Wait for automatic recovery","Check network connectivity","Verify TURN server credentials"]},33005:{name:"ONLY_HOST_ICE_CANDIDATES",message:"Only local network candidates available",description:"ICE gathering completed but only host (local network) candidates were collected — no server-reflexive (srflx) or relay (turn) candidates were found. This typically means the STUN/TURN servers are unreachable, which will prevent connections outside the local network.",causes:["STUN/TURN servers unreachable","Firewall blocking UDP traffic to STUN/TURN servers","Incorrect TURN server configuration or credentials","Restrictive corporate network or VPN"],solutions:["Verify STUN/TURN server URLs and credentials","Ensure UDP traffic to STUN/TURN ports is not blocked","Check firewall or VPN settings","Try using TCP-based TURN as a fallback"]},33006:{name:"ANSWER_WHILE_PEER_ACTIVE",message:"Call answer ignored because a peer connection is already active",description:"answer() was called on a call that already has an active or connecting peer connection. Creating a second peer connection for the same call would duplicate media negotiation, confuse the remote party, and break call reporting. This is typically caused by application code invoking answer() multiple times (e.g. from multiple event handlers).",causes:["Application called answer() twice on the same call object","Multiple click handlers or event listeners triggering answer()"],solutions:["Ensure answer() is called only once per call","Disable the answer button after the first click","Check that answer() is not invoked from multiple event handlers"]},33008:{name:"ICE_CANDIDATE_PAIR_CHANGED",message:"ICE candidate pair changed mid-call",description:"The selected ICE candidate pair changed during an active call. This indicates a network path shift — for example, a Wi-Fi to cellular handoff, a NAT rebinding, or a relay fallback. The call may continue normally, but the path change can briefly affect audio quality.",causes:["Network interface change (e.g. Wi-Fi to cellular)","NAT rebinding or IP address change","Previous candidate pair failed and ICE selected an alternative","Network topology change"],solutions:["Monitor for audio quality degradation after the path change","Check network stability if changes are frequent","Verify TURN server configuration for relay fallback"]},33007:{name:"DUPLICATE_INBOUND_ANSWER",message:"Call answer ignored because another inbound call is already being answered",description:"answer() was called on an inbound call while another inbound call is already answering or active in this JavaScript runtime. Answering both legs can trigger SIP 486 USER_BUSY / LOSE_RACE when duplicate WebSocket registrations receive the same incoming call.",causes:["Multiple TelnyxRTC instances in the same page","Application code recreating a client without disconnecting the previous instance","Duplicate inbound call notifications produced by duplicate WebSocket registrations"],solutions:["Keep a single active TelnyxRTC instance for inbound call handling","Call disconnect() before replacing an SDK client instance","Only call answer() for one inbound call notification at a time"]},34001:{name:"TOKEN_EXPIRING_SOON",message:"Authentication token expiring soon",description:"The authentication token is approaching its expiration time. If the token expires the connection will be lost and calls will fail. A new token should be generated before expiration.",causes:["Token was issued with a limited lifetime"],solutions:["Generate a new authentication token","Reconnect with fresh credentials before the token expires"]},36001:{name:"SIGNALING_HEALTH_PROBE_TIMEOUT",message:"Signaling health probe timed out",description:"A signaling liveness probe (Ping) was sent during an active call but no response was received within the timeout window. This indicates the WebSocket may be half-dead — the browser reports it as OPEN but data is not flowing. The SDK will force-close the socket to trigger reconnection.",causes:["Network interface removed mid-call while another interface remains","TCP connection bound to a removed IP/route became half-dead","Firewall or NAT state expired silently","WebSocket proxy or load balancer dropped the connection without a close frame"],solutions:["The SDK will automatically force-close the socket and reconnect","Check for network interface changes during the call","Verify firewall/NAT timeout settings"]},36002:{name:"SIGNALING_REQUEST_TIMEOUT",message:"Signaling request timed out",description:"A signaling-critical JSON-RPC request (e.g. ICE restart Modify) did not receive a response within the timeout window while the socket reports OPEN. This may indicate the WebSocket is half-dead or the server is unresponsive. The SDK will mark signaling as unhealthy and force reconnection.",causes:["Half-dead WebSocket (browser reports OPEN but data does not flow)","Server unresponsive or overloaded","Network path interruption without TCP RST"],solutions:["The SDK will automatically force-close the socket and reconnect","Check server health and response times","Check for network path issues"]},36003:{name:"SIGNALING_RECOVERY_REQUIRED",message:"Signaling recovery required",description:"The signaling (WebSocket) path has been detected as unhealthy and the SDK will force-close the socket and reconnect. If media was still flowing, the reconnect is delayed briefly to allow the application to notify the user about a short interruption. Active calls will be recovered via reattach after reconnection.",causes:["WebSocket probe timed out with no response","Critical signaling request timed out","Peer/media failure detected while signaling is also unhealthy"],solutions:["The SDK will automatically reconnect and recover the call","Check for network interface changes or interruptions","Verify firewall/NAT timeout settings"]},36004:{name:"MEDIA_RECOVERY_REQUIRED",message:"Media recovery required",description:"The peer connection or media flow has been detected as unhealthy while signaling is healthy. The SDK will attempt ICE restart to recover the media path. No socket reconnection is needed.",causes:["ICE connection state changed to failed","RTCPeerConnection state changed to failed","No RTP packets/bytes received while media should be active"],solutions:["The SDK will automatically attempt ICE restart","Check network connectivity and ICE candidate availability","Verify TURN server configuration"]},35001:{name:"SESSION_NOT_REATTACHED",message:"Active call lost after reconnect",description:"The WebSocket reconnected successfully but the server returned an empty reattached_sessions list while the SDK still has an active call. The server no longer knows about the call, so any subsequent call-control operation (hangup, hold, etc.) will fail with CALL_DOES_NOT_EXIST.",causes:["Server-side session expired during the disconnection window","Reconnect token was invalidated","Backend restarted or lost in-memory call state"],solutions:["Terminate the local call and notify the user","Start a new call","Investigate why the session was not preserved on the server"]}};function le(e,t){const i=de[e];return{code:e,name:i.name,message:t||i.message,description:i.description,causes:[...i.causes],solutions:[...i.solutions]}}class he extends Error{constructor(e){super(e.message||`[${e.code}] ${e.name}`),this.name=e.name,this.code=e.code,this.description=e.description,this.causes=e.causes,this.solutions=e.solutions,this.originalError=e.originalError,Object.setPrototypeOf(this,he.prototype)}toJSON(){return{code:this.code,name:this.name,description:this.description,message:this.message,causes:this.causes,solutions:this.solutions,originalError:this.originalError}}}function ue(e){if(e instanceof DOMException){if("NotAllowedError"===e.name)return m;if("NotFoundError"===e.name||"OverconstrainedError"===e.name)return f}return _}function pe(e,t,i){const n=ce[e],s=t instanceof Error?t:void 0!==t?new Error(String(t)):void 0;return new he({code:e,name:n.name,description:n.description,message:i||n.message,causes:[...n.causes],solutions:[...n.solutions],originalError:s})}class ge extends Error{constructor(e,t,i=""){super(`Signaling request timed out (id=${e}, method=${i||"unknown"}, timeout=${t}ms)`),this.name="RequestTimeoutError",this.requestId=e,this.timeoutMs=t,this.method=i}}class ve extends Error{constructor(e,t,i){super(`Stale request cancelled (id=${e}, gen=${t}, current=${i})`),this.name="StaleRequestError",this.requestId=e,this.staleGeneration=t,this.currentGeneration=i}}var me="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function fe(e,t){return e(t={exports:{}},t.exports),t.exports}var _e=fe((function(e){var t,i;t=me,i=function(){var e=function(){},t="undefined",i=typeof window!==t&&typeof window.navigator!==t&&/Trident\/|MSIE /.test(window.navigator.userAgent),n=["trace","debug","info","warn","error"];function s(e,t){var i=e[t];if("function"==typeof i.bind)return i.bind(e);try{return Function.prototype.bind.call(i,e)}catch(t){return function(){return Function.prototype.apply.apply(i,[e,arguments])}}}function o(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function r(t,i){for(var s=0;s<n.length;s++){var o=n[s];this[o]=s<t?e:this.methodFactory(o,t,i)}this.log=this.debug}function a(e,i,n){return function(){typeof console!==t&&(r.call(this,i,n),this[e].apply(this,arguments))}}function c(n,r,c){return function(n){return"debug"===n&&(n="log"),typeof console!==t&&("trace"===n&&i?o:void 0!==console[n]?s(console,n):void 0!==console.log?s(console,"log"):e)}(n)||a.apply(this,arguments)}function d(e,i,s){var o,a=this;i=null==i?"WARN":i;var d="loglevel";function l(){var e;if(typeof window!==t&&d){try{e=window.localStorage[d]}catch(e){}if(typeof e===t)try{var i=window.document.cookie,n=i.indexOf(encodeURIComponent(d)+"=");-1!==n&&(e=/^([^;]+)/.exec(i.slice(n))[1])}catch(e){}return void 0===a.levels[e]&&(e=void 0),e}}"string"==typeof e?d+=":"+e:"symbol"==typeof e&&(d=void 0),a.name=e,a.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},a.methodFactory=s||c,a.getLevel=function(){return o},a.setLevel=function(i,s){if("string"==typeof i&&void 0!==a.levels[i.toUpperCase()]&&(i=a.levels[i.toUpperCase()]),!("number"==typeof i&&i>=0&&i<=a.levels.SILENT))throw"log.setLevel() called with invalid level: "+i;if(o=i,!1!==s&&function(e){var i=(n[e]||"silent").toUpperCase();if(typeof window!==t&&d){try{return void(window.localStorage[d]=i)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"="+i+";"}catch(e){}}}(i),r.call(a,i,e),typeof console===t&&i<a.levels.SILENT)return"No console available for logging"},a.setDefaultLevel=function(e){i=e,l()||a.setLevel(e,!1)},a.resetLevel=function(){a.setLevel(i,!1),function(){if(typeof window!==t&&d){try{return void window.localStorage.removeItem(d)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch(e){}}}()},a.enableAll=function(e){a.setLevel(a.levels.TRACE,e)},a.disableAll=function(e){a.setLevel(a.levels.SILENT,e)};var h=l();null==h&&(h=i),a.setLevel(h,!1)}var l=new d,h={};l.getLogger=function(e){if("symbol"!=typeof e&&"string"!=typeof e||""===e)throw new TypeError("You must supply a name when creating a logger.");var t=h[e];return t||(t=h[e]=new d(e,l.getLevel(),l.methodFactory)),t};var u=typeof window!==t?window.log:void 0;return l.noConflict=function(){return typeof window!==t&&window.log===l&&(window.log=u),l},l.getLoggers=function(){return h},l.default=l,l},e.exports?e.exports=i():t.log=i()}));const Se={debug:0,info:1,warn:2,error:3};class be{constructor(e={}){var t,i,n;this.buffer=[],this.isCapturing=!1,this.options={enabled:null!==(t=e.enabled)&&void 0!==t&&t,level:null!==(i=e.level)&&void 0!==i?i:"debug",maxEntries:null!==(n=e.maxEntries)&&void 0!==n?n:1e3}}start(){this.options.enabled&&(this.isCapturing=!0,this.buffer=[])}stop(){this.isCapturing=!1}addEntry(e,t,i){if(!this.isCapturing||!this.options.enabled)return;if(Se[e]<Se[this.options.level])return;const n=Object.assign({timestamp:(new Date).toISOString(),level:e,message:t},i&&Object.keys(i).length>0?{context:i}:{});this.buffer.push(n),this.buffer.length>this.options.maxEntries&&this.buffer.shift()}getLogs(){return[...this.buffer]}getLogCount(){return this.buffer.length}drain(){const e=this.buffer;return this.buffer=[],e}clear(){this.buffer=[]}isActive(){return this.isCapturing}isEnabled(){return this.options.enabled}}let ye=null;const Ie=_e.getLogger("telnyx"),Ee={trace:0,debug:1,info:2,warn:3,error:4};let Ce=Ee.info;function we(e){if(null==e)return e;if("object"!=typeof e)return e;try{const t=JSON.stringify(e),i=JSON.parse(t);if("object"==typeof i&&null!==i&&Object.keys(i).length>1)return i}catch(e){}const t={};for(const i in e)try{const n=e[i];if("function"==typeof n)continue;if("object"==typeof n&&null!==n)try{t[i]=JSON.parse(JSON.stringify(n))}catch(e){t[i]=String(n)}else t[i]=n}catch(e){}return Object.keys(t).length>0?t:{value:String(e)}}const Te=Ie.methodFactory;Ie.methodFactory=(e,t,i)=>{const n=Te(e,t,i);return function(...t){if(Ee[e]>=Ce){const e=[(new Date).toISOString().replace("T"," ").replace("Z",""),"-"];for(const i of t)e.push(i);n(...e)}const i=ye;if(null==i?void 0:i.isActive()){const[n,...s]=t,o="string"==typeof n?n:JSON.stringify(n);let r;s.length>0&&(r=1===s.length&&"object"==typeof s[0]&&null!==s[0]?we(s[0]):{args:s.map(we)}),i.addEntry(e,o,r)}}},Ie.setLevel("debug",!1);const ke=e=>{const[t,i,n,s,o,r]=e;let a={};try{a=JSON.parse(o.replace(/ID"/g,'Id"'))}catch(e){Ie.warn("Verto LA invalid media JSON string:",o)}return{participantId:Number(t),participantNumber:i,participantName:n,codec:s,media:a,participantData:r}},Re=e=>{if("string"!=typeof e)return e;try{return JSON.parse(e)}catch(t){return e}},Ae=e=>e instanceof Function||"function"==typeof e,Oe=e=>"object"==typeof document&&"getElementById"in document?"string"==typeof e?document.getElementById(e)||null:"function"==typeof e?e():e instanceof HTMLMediaElement?e:null:null,Ne=/^(ws|wss):\/\//,Le=(e,t=null)=>{const{result:i={},error:n}=e;if(n)return{error:n};const{result:s=null}=i;if(null===s)return null!==t&&(i.node_id=t),{result:i};const{code:o=null,node_id:r=null,result:a=null}=s;return o&&"200"!==o?{error:s}:a?Le(a,r):{result:s}},De=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),Pe=({login:e,passwd:t,password:i,login_token:n})=>Boolean(e&&(t||i)||n),Me=({anonymous_login:e})=>Boolean(e)&&Boolean(e.target_id)&&Boolean(e.target_type),xe=e=>{var t,i,n,s,o,r;let a="",c="";(null===(i=null===(t=null==e?void 0:e.result)||void 0===t?void 0:t.params)||void 0===i?void 0:i.state)&&(a=null===(s=null===(n=null==e?void 0:e.result)||void 0===n?void 0:n.params)||void 0===s?void 0:s.state),(null===(o=null==e?void 0:e.params)||void 0===o?void 0:o.state)&&(c=null===(r=null==e?void 0:e.params)||void 0===r?void 0:r.state);return a||c};function Ue({debounceTime:e}){let t,i;return{promise:new Promise(((n,s)=>{t=e?Fe(n,e):n,i=s})),resolve:t,reject:i}}const Fe=(e,t)=>{let i;return(...n)=>{clearTimeout(i),i=window.setTimeout((()=>{e(...n)}),t)}},$e="telnyx-voice-sdk-id";function He(){return sessionStorage.getItem($e)}function je(){sessionStorage.removeItem($e)}var Ge,We,Be;"undefined"!=typeof window&&window.addEventListener("beforeunload",(()=>{je()})),function(e){e.Offer="offer",e.Answer="answer"}(Ge||(Ge={})),function(e){e.Inbound="inbound",e.Outbound="outbound"}(We||(We={})),function(e){e.Invite="telnyx_rtc.invite",e.Attach="telnyx_rtc.attach",e.Answer="telnyx_rtc.answer",e.Info="telnyx_rtc.info",e.Candidate="telnyx_rtc.candidate",e.EndOfCandidates="telnyx_rtc.endOfCandidates",e.Display="telnyx_rtc.display",e.Media="telnyx_rtc.media",e.Event="telnyx_rtc.event",e.Bye="telnyx_rtc.bye",e.Punt="telnyx_rtc.punt",e.Broadcast="telnyx_rtc.broadcast",e.Subscribe="telnyx_rtc.subscribe",e.Unsubscribe="telnyx_rtc.unsubscribe",e.ClientReady="telnyx_rtc.clientReady",e.Modify="telnyx_rtc.modify",e.Ringing="telnyx_rtc.ringing",e.GatewayState="telnyx_rtc.gatewayState",e.Ping="telnyx_rtc.ping",e.Pong="telnyx_rtc.pong"}(Be||(Be={}));const Ve={generic:"event",[Be.Display]:"participantData",[Be.Attach]:"participantData",conferenceUpdate:"conferenceUpdate",callUpdate:"callUpdate",vertoClientReady:"vertoClientReady",userMediaError:"userMediaError",peerConnectionFailureError:"peerConnectionFailureError",signalingStateClosed:"signalingStateClosed"},qe={invalidCredentialsOptions:"InvalidCredentialsOptions"},Ye={destinationNumber:"",remoteCallerName:"Outbound Call",remoteCallerNumber:"",callerName:"",callerNumber:"",audio:!0,useStereo:!1,debug:!1,debugOutput:"socket",attach:!1,screenShare:!1,userVariables:{},mediaSettings:{useSdpASBandwidthKbps:!1,sdpASBandwidthKbps:0},mutedMicOnStart:!1,prefetchIceCandidates:!0};var Ke,Je,Qe,Xe,ze,Ze;!function(e){e[e.New=0]="New",e[e.Requesting=1]="Requesting",e[e.Trying=2]="Trying",e[e.Recovering=3]="Recovering",e[e.Ringing=4]="Ringing",e[e.Answering=5]="Answering",e[e.Early=6]="Early",e[e.Active=7]="Active",e[e.Held=8]="Held",e[e.Hangup=9]="Hangup",e[e.Destroy=10]="Destroy",e[e.Purge=11]="Purge"}(Ke||(Ke={})),function(e){e.Participant="participant",e.Moderator="moderator"}(Je||(Je={})),function(e){e.Join="join",e.Leave="leave",e.Bootstrap="bootstrap",e.Add="add",e.Modify="modify",e.Delete="delete",e.Clear="clear",e.ChatMessage="chatMessage",e.LayerInfo="layerInfo",e.LogoInfo="logoInfo",e.LayoutInfo="layoutInfo",e.LayoutList="layoutList",e.ModCmdResponse="modCommandResponse"}(Qe||(Qe={})),function(e){e.Video="videoinput",e.AudioIn="audioinput",e.AudioOut="audiooutput"}(Xe||(Xe={})),function(e){e.REGED="REGED",e.UNREGED="UNREGED",e.NOREG="NOREG",e.FAILED="FAILED",e.FAIL_WAIT="FAIL_WAIT",e.REGISTER="REGISTER",e.TRYING="TRYING",e.EXPIRED="EXPIRED",e.UNREGISTER="UNREGISTER"}(ze||(ze={})),function(e){e.Hold="hold",e.Unhold="unhold",e.ToggleHold="toggleHold",e.UpdateMedia="updateMedia"}(Ze||(Ze={}));const et="GLOBAL",tt={},it=(e,t)=>`${e}|${t}`,nt=(e,t=et)=>it(e,t)in tt,st=(e,t,i=et)=>{const n=it(e,i);n in tt||(tt[n]=[]),tt[n].push(t)},ot=(e,t,i=et)=>{const n=function(s){rt(e,n,i),t(s)};return n.prototype.targetRef=t,st(e,n,i)},rt=(e,t,i=et)=>{if(!nt(e,i))return!1;const n=it(e,i);if(Ae(t)){for(let e=tt[n].length-1;e>=0;e--){const i=tt[n][e];(t===i||i.prototype&&t===i.prototype.targetRef)&&tt[n].splice(e,1)}}else tt[n]=[];return 0===tt[n].length&&delete tt[n],!0},at=(e,t,i=et,n=!0)=>{const s=n&&i!==et;if(!nt(e,i))return s&&at(e,t),!1;const o=it(e,i),r=tt[o].length;if(!r)return s&&at(e,t),!1;for(let e=r-1;e>=0;e--)tt[o][e](t);return s&&at(e,t),!0},ct=e=>{const t=it(e,"");Object.keys(tt).filter((e=>0===e.indexOf(t))).forEach((e=>delete tt[e]))};let dt="undefined"!=typeof WebSocket?WebSocket:null;const lt=0,ht=1,ut=2,pt=3;class gt{constructor(e){this.session=e,this.previousGatewayState="",this.lastInboundAt=0,this.socketGeneration=0,this._wsClient=null,this._host=ie,this._timers={},this._useCanaryRtcServer=!1,this._hasCanaryBeenUsed=!1,this._safetyTimeoutId=null,this._pendingRequestIds=new Set,this._pendingRequestTimers=new Map,this._pendingRequestRejecters=new Map,this.upDur=null,this.downDur=null;const{host:t,env:i,region:n,useCanaryRtcServer:s}=e.options;i&&(this._host="development"===i?"wss://rtcdev.telnyx.com":ie),t&&(this._host=(e=>`${Ne.test(e)?"":"wss://"}${e}`)(t)),n&&(this._host=this._host.replace(/rtc(dev)?/,`${n}.rtc$1`)),s&&(this._useCanaryRtcServer=!0)}get connected(){return!!this._wsClient&&this._wsClient.readyState===ht}get connecting(){return!!this._wsClient&&this._wsClient.readyState===lt}get closing(){return!!this._wsClient&&this._wsClient.readyState===ut}get closed(){return!!this._wsClient&&this._wsClient.readyState===pt}get isAlive(){return this.connecting||this.connected}get isDead(){return this.closing||this.closed}get host(){return this._host}connect(){const t=new URL(this._host);let i=He();this.session.options.rtcIp&&this.session.options.rtcPort&&(i=null,this._useCanaryRtcServer=!1,t.searchParams.set("rtc_ip",this.session.options.rtcIp),t.searchParams.set("rtc_port",this.session.options.rtcPort.toString())),i&&t.searchParams.set("voice_sdk_id",i),this._useCanaryRtcServer&&(t.searchParams.set("canary","true"),i&&!this._hasCanaryBeenUsed&&(t.searchParams.delete("voice_sdk_id"),Ie.debug("first canary connection. Refreshing voice_sdk_id")),this._hasCanaryBeenUsed=!0),this.session.callReportVoiceSdkId=t.searchParams.get("voice_sdk_id"),this.session.options.skipLastVoiceSdkId&&t.searchParams.has("voice_sdk_id")&&t.searchParams.set("skip_last_voice_sdk_id","true");try{this._wsClient=new dt(t.toString()),this.socketGeneration+=1,this.lastInboundAt=0,this._cleanupPendingRequests(),this._registerSocketEvents(this._wsClient)}catch(t){Ie.error("WebSocket connection failed:",t);const i=pe(C,t);at(e.SwEvent.Error,{error:i,sessionId:this.session.sessionid},this.session.uuid)}}sendRawText(e){var t;null===(t=this._wsClient)||void 0===t||t.send(e)}send(e,t){var i;const{request:n}=e,s=this.socketGeneration,o=n.method||"",r=new Promise(((e,i)=>{if(n.hasOwnProperty("result"))return e();let r=!1,a=null;const c=t=>{if(null!==a&&(clearTimeout(a),this._pendingRequestTimers.delete(n.id),a=null),this._pendingRequestIds.delete(n.id),this._pendingRequestRejecters.delete(n.id),r)return;const{result:s,error:o}=Le(t);return o?i(o):e(s)};ot(n.id,c),this._pendingRequestIds.add(n.id),this._pendingRequestRejecters.set(n.id,{reject:i,generation:s}),t&&t>0&&(a=setTimeout((()=>{if(r=!0,a=null,this._pendingRequestTimers.delete(n.id),this._pendingRequestRejecters.delete(n.id),rt(n.id,c),this._pendingRequestIds.delete(n.id),this.socketGeneration!==s)return Ie.debug(`Stale request timeout for ${n.id} (gen ${s}, current ${this.socketGeneration}) — settling with StaleRequestError`),void i(new ve(n.id,s,this.socketGeneration));i(new ge(n.id,t,o))}),t),this._pendingRequestTimers.set(n.id,a))}));return Ie.debug("SEND: \n",JSON.stringify(n,null,2),"\n"),null===(i=this._wsClient)||void 0===i||i.send(JSON.stringify(n)),r}close(){if(!this._wsClient||this.closing)return;this._cleanupPendingRequests();const e=this._wsClient;Ae(this._wsClient._beginClose)?this._wsClient._beginClose():this._wsClient.close(),this._safetyTimeoutId||(this._safetyTimeoutId=setTimeout((()=>this._handleCloseTimeout(e)),5e3))}_registerSocketEvents(t){t.onopen=t=>at(e.SwEvent.SocketOpen,t,this.session.uuid),t.onclose=i=>(this._clearSafetyTimeout(),this._safetyCleanupSocket(t,"close"),at(e.SwEvent.SocketClose,i,this.session.uuid)),t.onerror=i=>{this._clearSafetyTimeout(),this._safetyCleanupSocket(t,"error");const n=pe(w);return at(e.SwEvent.Error,{error:n,sessionId:this.session.sessionid},this.session.uuid),at(e.SwEvent.SocketError,{error:i,sessionId:this.session.sessionid},this.session.uuid)},t.onmessage=t=>{var i,n;this.lastInboundAt=Date.now(),at(e.SwEvent.SocketActivity,{timestamp:this.lastInboundAt},this.session.uuid);const s=Re(t.data);var o;if("string"!=typeof s){if(s.voice_sdk_id&&(this.session.callReportVoiceSdkId=s.voice_sdk_id,o=s.voice_sdk_id,sessionStorage.setItem($e,o)),this._unsetTimer(s.id),Ie.debug("RECV: \n",JSON.stringify(s,null,2),"\n"),ze[`${null===(n=null===(i=null==s?void 0:s.result)||void 0===i?void 0:i.params)||void 0===n?void 0:n.state}`]||!at(s.id,s)){const t=xe(s);at(e.SwEvent.SocketMessage,s,this.session.uuid),Boolean(t)&&(this.previousGatewayState=t)}}else this._handleStringResponse(s)}}_deregisterSocketEvents(e){e.onopen=null,e.onclose=null,e.onerror=null,e.onmessage=null}_handleCloseTimeout(t){this._safetyTimeoutId=null,t&&t.readyState!==pt?(Ie.warn("Socket stuck in CLOSING after 5s — forcefully cleaning up"),this._deregisterSocketEvents(t),this._safetyCleanupSocket(t,"timeout"),this._wsClient&&this._wsClient!==t||at(e.SwEvent.SocketClose,{code:ne.ABNORMAL_CLOSURE,reason:"STUCK_WS_TIMEOUT: Socket got stuck in CLOSING state and was forcefully cleaned up by safety timeout",wasClean:!1},this.session.uuid)):Ie.warn("Safety timeout fired but socket is already closed or cleaned up")}_clearSafetyTimeout(){this._safetyTimeoutId&&(Ie.debug("Clearing safety timeout"),clearTimeout(this._safetyTimeoutId),this._safetyTimeoutId=null)}_safetyCleanupSocket(e,t){this._wsClient===e?(Ie.debug(`Nulling socket reference (reason: ${t})`),this._wsClient=null):Ie.debug(`Skipping socket cleanup - old socket already replaced (reason: ${t})`)}_cleanupPendingRequests(){Array.from(this._pendingRequestIds).forEach((e=>{rt(e);const t=this._pendingRequestTimers.get(e),i=this._pendingRequestRejecters.get(e);t&&(clearTimeout(t),this._pendingRequestTimers.delete(e),i&&i.reject(new ve(e,i.generation,this.socketGeneration))),this._pendingRequestRejecters.delete(e)})),this._pendingRequestIds.clear()}_unsetTimer(e){clearTimeout(this._timers[e]),delete this._timers[e]}_handleStringResponse(t){if(/^#SP/.test(t))switch(t[3]){case"U":this.upDur=parseInt(t.substring(4));break;case"D":this.downDur=parseInt(t.substring(4)),at(e.SwEvent.SpeedTest,{upDur:this.upDur,downDur:this.downDur},this.session.uuid)}else Ie.warn("Unknown message from socket",t)}}gt.DEFAULT_REQUEST_TIMEOUT_MS=1e4;class vt{buildRequest(e){this.request=Object.assign({jsonrpc:"2.0",id:c()},e)}}const mt={id:"callID",destinationNumber:"destination_number",remoteCallerName:"remote_caller_id_name",remoteCallerNumber:"remote_caller_id_number",callerName:"caller_id_name",callerNumber:"caller_id_number",customHeaders:"custom_headers"};class ft extends vt{constructor(e={}){if(super(),e.hasOwnProperty("dialogParams")){const i=t(e.dialogParams,["remoteSdp","localStream","remoteStream","localElement","remoteElement","onNotification","camId","micId","speakerId"]);for(const e in mt)e&&i.hasOwnProperty(e)&&(i[mt[e]]=i[e],delete i[e]);e.dialogParams=i}this.buildRequest({method:this.toString(),params:e})}}class _t extends ft{constructor(e){super(),this.method=Be.GatewayState;this.buildRequest({method:this.method,voice_sdk_id:e,params:{}})}}class St{constructor(e){this.pendingRequestId=null,this.onSocketMessage=e=>i(this,void 0,void 0,(function*(){e.id===this.pendingRequestId&&this.gatewayStateTask.resolve(xe(e))})),this.getIsRegistered=()=>i(this,void 0,void 0,(function*(){const e=new _t(He());this.pendingRequestId=e.request.id,this.gatewayStateTask=Ue({}),this.session.execute(e);const t=yield this.gatewayStateTask.promise;return!!t&&[ze.REGISTER,ze.REGED].includes(t)})),this.session=e,this.gatewayStateTask=Ue({}),this.session.on("telnyx.socket.message",this.onSocketMessage)}}class bt extends ft{constructor(e){super(),this.method=Be.Ping;this.buildRequest({method:this.method,voice_sdk_id:e,params:{}})}}class yt{constructor(e){this._session=e,this._lastInboundAt=0,this._lastProbeSentAt=0,this._probeInFlight=!1,this._intervalId=null,this._recoveryGeneration=0,this._pendingMediaRecovery=null}static isCriticalMethod(e){return yt.CRITICAL_METHODS.has(e)}start(){this._intervalId||(Ie.debug("Signaling health: monitor started"),this._lastInboundAt=Date.now(),this._probeInFlight=!1,this._lastProbeSentAt=0,this._intervalId=setInterval((()=>this._check()),3e3))}stop(){this._intervalId&&(clearInterval(this._intervalId),this._intervalId=null,this._probeInFlight=!1,this._lastProbeSentAt=0),this._pendingMediaRecovery=null,Ie.debug("Signaling health: monitor stopped")}get isRunning(){return null!==this._intervalId}get isProbeInFlight(){return this._probeInFlight}onSocketActivity(){this._lastInboundAt=Date.now();const e=this._probeInFlight;if(e&&(this._probeInFlight=!1,this._lastProbeSentAt=0,Ie.debug("Signaling health: probe resolved by inbound activity")),e&&this._pendingMediaRecovery){const e=this._pendingMediaRecovery;this._pendingMediaRecovery=null,"healthy"===this._getSignalingHealthState()&&(Ie.info(`Signaling health: signaling probe resolved, triggering pending ICE restart for call ${e.callId}`),this._triggerIceRestart(e.callId,e.reason))}}_probeIfNeeded(e){var t;(null===(t=this._session.connection)||void 0===t?void 0:t.connected)&&(this._probeInFlight?Ie.debug(`Signaling health: probe already in flight, skipping duplicate probe (${e})`):(Ie.info(`Signaling health: ${e}, sending signaling probe`),this._sendProbe()))}onRequestTimeout(e,t,i=""){var n;if(!(null===(n=this._session.connection)||void 0===n?void 0:n.connected))return;yt.isCriticalMethod(i)?(Ie.warn(`Critical signaling request timed out (id=${e}, method=${i}, timeout=${t}ms) — declaring signaling unhealthy`),this._triggerSignalingRecovery(`Critical signaling request timed out (method=${i}, id=${e}, timeout=${t}ms)`,"request")):Ie.warn(`Non-critical signaling request timed out (id=${e}, method=${i||"unknown"}, timeout=${t}ms) — logging but not triggering signaling recovery`)}onPeerFailure(e,t){Ie.warn(`Signaling health: peer failure reported (callId=${e}, evidence=${t})`),this._recoverMediaOrSignaling(e,`Peer connection failure (${t})`,`Peer failure detected (${t}) while signaling is unhealthy`,"peer_failure")}onNoRtp(e,t){Ie.warn(`Signaling health: no RTP detected (callId=${e}, direction=${t})`),this._recoverMediaOrSignaling(e,`No RTP ${t} while media should be active`,`No RTP ${t} while signaling is unhealthy`,"no_rtp")}_check(){var e;if(!(null===(e=this._session.connection)||void 0===e?void 0:e.connected))return;const t=Date.now(),i=t-this._lastInboundAt;if(i<12e3)return;if(!this._probeInFlight)return Ie.info(`Signaling health: no inbound WS activity for ${Math.round(i/1e3)}s during active call, sending health probe`),void this._sendProbe();const n=t-this._lastProbeSentAt;n>=5e3&&(Ie.warn(`Signaling health: probe timed out after ${n}ms with no inbound activity — declaring signaling unhealthy`),this._triggerSignalingRecovery("Signaling health probe timed out: no inbound WS activity after probe","probe"))}_sendProbe(){var e;this._probeInFlight=!0,this._lastProbeSentAt=Date.now(),null===(e=this._session.connection)||void 0===e||e.send(new bt(He())).catch((e=>{Ie.warn("Signaling health: probe Ping failed to send",e)}))}_recoverMediaOrSignaling(e,t,i,n){if(!this._session.hasActiveCall())return void Ie.debug(`Signaling health: ignoring ${n} recovery without an active call`);const s=this._getSignalingHealthState();return"healthy"===s?(Ie.info(`Signaling health: signaling is healthy, triggering ICE restart for call ${e}`),void this._triggerIceRestart(e,t)):"unknown"===s?(Ie.info(`Signaling health: signaling health is unknown, deferring ICE restart decision for call ${e}`),this._pendingMediaRecovery={callId:e,reason:t},void this._probeIfNeeded(`${n} detected with stale/unknown signaling`)):(Ie.info("Signaling health: signaling is unhealthy, triggering socket reconnect instead of ICE restart"),void this._triggerSignalingRecovery(i,n))}_getSignalingHealthState(){var e;if(!(null===(e=this._session.connection)||void 0===e?void 0:e.connected))return"unhealthy";if(this._probeInFlight)return"unknown";const t=Date.now(),i=this._lastInboundAt||this._session.connection.lastInboundAt||0;return 0===i?"unknown":t-i<3e3?"healthy":"unknown"}_triggerSignalingRecovery(t,i="probe"){var n;let s;this._pendingMediaRecovery=null,this._probeInFlight=!1,this._lastProbeSentAt=0,this._recoveryGeneration++,s="probe"===i?X:"request"===i?z:Z;const o=le(s);at(e.SwEvent.Warning,{warning:o,reason:t,source:i,sessionId:this._session.sessionid},this._session.uuid),(null===(n=this._session.connection)||void 0===n?void 0:n.connected)&&(Ie.info("Signaling health: force-closing WebSocket to trigger reconnect"),this._session.socketDisconnect())}_triggerIceRestart(t,i){this._recoveryGeneration++;const n=le(ee);at(e.SwEvent.Warning,{warning:n,reason:i,callId:t,sessionId:this._session.sessionid},this._session.uuid),Ie.info(`Signaling health: triggering ICE restart for call ${t}`),this._session.triggerIceRestart(t)}}yt.CRITICAL_METHODS=new Set([Be.Modify,Be.Bye,Be.Ping]);var It="2.27.0-beta.6",Et=It;class Ct extends ft{constructor(e,t,i,n,s={},o){super(),this.method="login";const r={login:e,passwd:t,login_token:i,userVariables:s,reconnection:o,loginParams:{},"User-Agent":{sdkVersion:Et,data:navigator.userAgent}};n&&(r.sessid=n),this.buildRequest({method:this.method,params:r})}}class wt extends ft{constructor(e,t){super(),this.buildRequest({id:e,result:{method:t}})}}class Tt extends ft{toString(){return Be.Invite}}class kt extends ft{toString(){return Be.Answer}}class Rt extends ft{toString(){return Be.Attach}}class At extends ft{toString(){return Be.Bye}}class Ot extends ft{toString(){return Be.Candidate}}class Nt extends ft{toString(){return Be.EndOfCandidates}}class Lt extends ft{toString(){return Be.Modify}}class Dt extends ft{toString(){return Be.Info}}class Pt extends ft{toString(){return Be.Broadcast}}class Mt extends ft{toString(){return Be.Subscribe}}class xt extends ft{toString(){return Be.Unsubscribe}}class Ut extends ft{constructor(e){super(),this.method="anonymous_login";const{target_type:t,target_id:i,target_version_id:n,target_params:s,userVariables:o,sessionId:r,reconnection:a}=e,c={target_type:t,target_id:i,userVariables:o,reconnection:a,"User-Agent":{sdkVersion:Et,data:navigator.userAgent}};r&&(c.sessid=r),n&&(c.target_version_id=n),s&&(c.target_params=s),this.buildRequest({method:this.method,params:c})}}class Ft{constructor(e){if(this.options=e,this.uuid=c(),this.sessionid="",this.subscriptions={},this.signature=null,this.relayProtocol=null,this.contexts=[],this.timeoutErrorCode=-32e3,this.invalidMethodErrorCode=-32601,this.authenticationRequiredErrorCode=-32e3,this.callReportId=null,this.callReportVoiceSdkId=null,this.dc=null,this.region=null,this.connection=null,this._jwtAuth=!1,this._autoReconnect=!0,this._idle=!1,this._reconnectAttempts=0,this._tokenExpiryTimeout=null,this._pendingCallReportUploads=new Set,this._signalingHealthMonitor=new yt(this),this._executeQueue=[],!this.validateOptions())throw new Error("Invalid init options");var t,i;t=e.debug?"debug":"info",Ce=null!==(i=Ee[t])&&void 0!==i?i:Ee.info,this._onSocketOpen=this._onSocketOpen.bind(this),this.onNetworkClose=this.onNetworkClose.bind(this),this._onSocketMessage=this._onSocketMessage.bind(this),this._handleLoginError=this._handleLoginError.bind(this),this._onSocketActivity=this._onSocketActivity.bind(this),this._attachListeners(),this.connection=new gt(this),this.registerAgent=new St(this)}get __logger(){return Ie}get connected(){return this.connection&&this.connection.connected}getIsRegistered(){return i(this,void 0,void 0,(function*(){return this.registerAgent.getIsRegistered()}))}get reconnectDelay(){return 1e3*De(2,6)}execute(t){var n;if(this._idle)return new Promise((e=>this._executeQueue.push({resolve:e,msg:t})));if(!this.connected)return new Promise((e=>{this._executeQueue.push({resolve:e,msg:t}),Ie.debug("Calling connect from execute since not currently connected."),this.connect()}));const s=yt.isCriticalMethod((null===(n=t.request)||void 0===n?void 0:n.method)||"")?gt.DEFAULT_REQUEST_TIMEOUT_MS:void 0;return(null!=s?this.connection.send(t,s):this.connection.send(t)).catch((t=>i(this,void 0,void 0,(function*(){if(t instanceof ve)throw Ie.debug(`Stale request settled (id=${t.requestId}, gen=${t.staleGeneration}) — not triggering recovery`),t;if("RequestTimeoutError"===(null==t?void 0:t.name))throw this.onSignalingRequestTimeout(t.requestId,t.timeoutMs,t.method),t;if((null==t?void 0:t.code)===this.authenticationRequiredErrorCode){if(!this._autoReconnect){const i=pe(O,t);at(e.SwEvent.Error,{error:i,sessionId:this.sessionid},this.uuid)}yield this.login()}throw t}))))}executeRaw(e){this._idle?this._executeQueue.push({msg:e}):this.connection.sendRawText(e)}trackCallReportUpload(e){this._pendingCallReportUploads.add(e),e.then((()=>this._pendingCallReportUploads.delete(e)),(()=>this._pendingCallReportUploads.delete(e)))}_drainCallReportUploads(){return i(this,void 0,void 0,(function*(){if(0===this._pendingCallReportUploads.size)return;const e=Array.from(this._pendingCallReportUploads);let t=!1;yield Promise.race([Promise.all(e.map((e=>e.catch((()=>{}))))),new Promise((e=>setTimeout((()=>{t=!0,e()}),Ft.CALL_REPORT_UPLOAD_DRAIN_TIMEOUT_MS)))]),t&&Ie.warn("Timed out waiting for pending call report uploads",{pendingCount:this._pendingCallReportUploads.size})}))}validateOptions(){return Pe(this.options)||Me(this.options)}broadcast(e){}disconnect(){return i(this,void 0,void 0,(function*(){clearTimeout(this._reconnectTimeout),this._clearTokenExpiryTimeout(),this.subscriptions={},this._autoReconnect=!1,this._reconnectAttempts=0,this.relayProtocol=null,yield this._drainCallReportUploads(),this._closeConnection(),yield sessionStorage.removeItem(this.signature),this._executeQueue=[],this._detachListeners(),Ie.debug("Session disconnected. Cleaned up all listeners and subscriptions, closed connection, disabled auto-reconnect.")}))}on(e,t){return st(e,t,this.uuid),this}off(e,t){return rt(e,t,this.uuid),this}connect(){return i(this,void 0,void 0,(function*(){this.connection||(Ie.debug("No existing connection found, creating a new one."),this.connection=new gt(this)),this._attachListeners(),this._autoReconnect||(this._reconnectAttempts=0),this._autoReconnect=!0,this.connection.isAlive||(Ie.debug("Initiating connection to the server..."),this.connection.connect()),Ie.debug("Connect method called. Connection initiated.")}))}resetReconnectAttempts(){this._reconnectAttempts=0}_handleLoginError(t){const i=pe(R,t);at(e.SwEvent.Error,{error:i,sessionId:this.sessionid},this.uuid)}clearReconnectToken(){je()}_checkTokenExpiry(){this._clearTokenExpiryTimeout();const e=this.options.login_token;if(e&&"string"==typeof e)try{const t=e.split(".");if(3!==t.length)return;const i=JSON.parse(atob(t[1])).exp;if("number"!=typeof i)return;const n=i-Math.floor(Date.now()/1e3);if(n<=0)return;if(n<=Ft.TOKEN_EXPIRY_WARNING_SECONDS)this._emitTokenExpiryWarning();else{const e=1e3*(n-Ft.TOKEN_EXPIRY_WARNING_SECONDS);this._tokenExpiryTimeout=setTimeout((()=>{this._emitTokenExpiryWarning()}),e)}}catch(e){Ie.debug("login_token is not a decodable JWT, skipping expiry check")}}_emitTokenExpiryWarning(){const t=le(J);at(e.SwEvent.Warning,{warning:t,sessionId:this.sessionid},this.uuid)}_clearTokenExpiryTimeout(){null!==this._tokenExpiryTimeout&&(clearTimeout(this._tokenExpiryTimeout),this._tokenExpiryTimeout=null)}login({creds:t,onSuccess:n,onError:s}={}){return i(this,void 0,void 0,(function*(){if(this.connection&&this.connection.isAlive){if(t&&(void 0!==t.login&&(this.options.login=t.login),void 0!==t.password&&(this.options.password=t.password),void 0!==t.passwd&&(this.options.passwd=t.passwd),void 0!==t.login_token&&(this.options.login_token=t.login_token),void 0!==t.userVariables&&(this.options.userVariables=t.userVariables),void 0!==t.anonymous_login&&(this.options.anonymous_login=t.anonymous_login)),Pe(this.options))return this._login({type:"login",onSuccess:n,onError:s});if(Me(this.options))return this._login({type:"anonymous_login",onSuccess:n,onError:s});{const t="Invalid login options provided for authentication.";Ie.error(t);const i=pe(A,void 0,t);return void at(e.SwEvent.Error,{error:i,type:qe.invalidCredentialsOptions,sessionId:this.sessionid},this.uuid)}}}))}_login({type:e,onSuccess:t,onError:n}){return i(this,void 0,void 0,(function*(){let i;i="login"===e?new Ct(this.options.login,this.options.password||this.options.passwd,this.options.login_token,this.sessionid,this.options.userVariables,!!He()):new Ut({target_id:this.options.anonymous_login.target_id,target_type:this.options.anonymous_login.target_type,target_version_id:this.options.anonymous_login.target_version_id,target_params:this.options.anonymous_login.target_params,sessionId:this.sessionid,userVariables:this.options.userVariables,reconnection:!!He()});const s=yield this.execute(i).catch((e=>{this._handleLoginError(e),n&&n(e)}));s&&(this.sessionid=s.sessid,this._checkTokenExpiry(),t&&t())}))}_onSocketOpen(){return i(this,void 0,void 0,(function*(){this._signalingHealthMonitor.onSocketActivity(),this.startSignalingHealthMonitor()}))}_flushIntermediateCallReports(e){const t=this.calls;t&&Object.values(t).forEach((t=>{if(null==t?void 0:t.flushIntermediateCallReport)try{t.flushIntermediateCallReport(e)}catch(i){Ie.error("Failed to flush intermediate call report",{callId:t.id,flushReason:e,error:i})}}))}_getSocketCloseCodeName(e){if(void 0===e)return;const t=Object.entries(ne).find((([,t])=>t===e));return null==t?void 0:t[0]}_getSocketCloseError(e){if(e)return e instanceof Error?e.message:String(e)}_createSocketCloseFlushReason(e){return{type:(null==e?void 0:e.error)?"socket-error":"socket-close",socketClose:{code:null==e?void 0:e.code,codeName:this._getSocketCloseCodeName(null==e?void 0:e.code),reason:null==e?void 0:e.reason,wasClean:null==e?void 0:e.wasClean,error:this._getSocketCloseError(null==e?void 0:e.error)}}}onNetworkClose(t){var i;this._flushIntermediateCallReports(this._createSocketCloseFlushReason(t)),this.relayProtocol&&ct(this.relayProtocol);for(const e in this.subscriptions)ct(e);if(this.subscriptions={},this.contexts=[],clearTimeout(this._keepAliveTimeout),clearTimeout(this._reconnectTimeout),this.stopSignalingHealthMonitor(),this.connection&&(this.connection.previousGatewayState=""),this._autoReconnect){const t=null!==(i=this.options.maxReconnectAttempts)&&void 0!==i?i:10;if(this._reconnectAttempts+=1,t>0&&this._reconnectAttempts>t){Ie.info(`Reconnection exhausted after ${t} attempts. Stopping automatic reconnect.`),this._reconnectAttempts=0,this._autoReconnect=!1;const i=pe(T);return void at(e.SwEvent.Error,{error:i,sessionId:this.sessionid},this.uuid)}Ie.debug(`Reconnect attempt ${this._reconnectAttempts}${t>0?` of ${t}`:""}`),this._reconnectTimeout=setTimeout((()=>{Ie.debug("Calling connect due to network close and auto-reconnect enabled."),this.connect()}),this.reconnectDelay)}}_onSocketMessage(e){}_removeSubscription(e,t){this._existsSubscription(e,t)&&(t?(delete this.subscriptions[e][t],rt(e,null,t)):(delete this.subscriptions[e],ct(e)))}_addSubscription(e,t=null,i){this._existsSubscription(e,i)||(this._existsSubscription(e)||(this.subscriptions[e]={}),this.subscriptions[e][i]={},Ae(t)&&st(e,t,i))}_existsSubscription(e,t){return!(!this.subscriptions.hasOwnProperty(e)||!(!t||t&&this.subscriptions[e].hasOwnProperty(t)))}_attachListeners(){this._detachListeners(),this.on(e.SwEvent.SocketOpen,this._onSocketOpen),this.on(e.SwEvent.SocketClose,this.onNetworkClose),this.on(e.SwEvent.SocketError,this.onNetworkClose),this.on(e.SwEvent.SocketMessage,this._onSocketMessage),this.on(e.SwEvent.SocketActivity,this._onSocketActivity)}_detachListeners(){this.off(e.SwEvent.SocketOpen,this._onSocketOpen),this.off(e.SwEvent.SocketClose,this.onNetworkClose),this.off(e.SwEvent.SocketError,this.onNetworkClose),this.off(e.SwEvent.SocketMessage,this._onSocketMessage),this.off(e.SwEvent.SocketActivity,this._onSocketActivity)}_emptyExecuteQueues(){this._executeQueue.forEach((({resolve:e,msg:t})=>{"string"==typeof t?this.executeRaw(t):e(this.execute(t))}))}_closeConnection(){this._idle=!0,clearTimeout(this._keepAliveTimeout),this.stopSignalingHealthMonitor(),this.connection&&this.connection.close()}_resetKeepAlive(){!1===this._pong&&(Ie.warn("No ping/pong received, forcing PING ACK to keep alive"),this.execute(new bt(He()))),clearTimeout(this._keepAliveTimeout),this._triggerKeepAliveTimeoutCheck()}_triggerKeepAliveTimeoutCheck(){this._pong=!1,this._keepAliveTimeout=setTimeout((()=>this._resetKeepAlive()),35e3)}setPingReceived(){Ie.debug("Ping received"),this._pong=!0}_onSocketActivity(){this._signalingHealthMonitor.onSocketActivity()}hasActiveCall(){const e=this.calls;if(!e)return!1;const t=new Set([Ke.Early,Ke.Active,Ke.Held]);return Object.values(e).some((e=>e&&null!=e._state&&t.has(e._state)))}startSignalingHealthMonitor(){this._signalingHealthMonitor.start()}stopSignalingHealthMonitor(){this._signalingHealthMonitor.stop()}triggerIceRestart(e){const t=this.calls,i=null==t?void 0:t[e];if(!i)return void Ie.warn(`Signaling health: cannot trigger ICE restart — call ${e} not found`);const n=i.peer;if(!(null==n?void 0:n.restartIce))return void Ie.warn(`Signaling health: cannot trigger ICE restart — no peer for call ${e}`);n.restartIce()||Ie.debug(`Signaling health: ICE restart skipped for call ${e}`)}onSignalingRequestTimeout(e,t,i=""){this._signalingHealthMonitor.onRequestTimeout(e,t,i)}reportPeerFailure(e,t){this._signalingHealthMonitor.onPeerFailure(e,t)}reportNoRtp(e,t){this._signalingHealthMonitor.onNoRtp(e,t)}static on(e,t){st(e,t)}static off(e){rt(e)}static uuid(){return c()}clearConnection(){this.connection=null}hasAutoReconnect(){return this._autoReconnect}}Ft.TOKEN_EXPIRY_WARNING_SECONDS=120,Ft.CALL_REPORT_UPLOAD_DRAIN_TIMEOUT_MS=1e4;const $t=e=>navigator.mediaDevices.getUserMedia(e),Ht=e=>e&&e instanceof MediaStream,jt=(e,t)=>{const i=Oe(e);null!==i&&(i.getAttribute("autoplay")||i.setAttribute("autoplay","autoplay"),i.getAttribute("playsinline")||i.setAttribute("playsinline","playsinline"),i.srcObject=t)},Gt=(e,t)=>{const i=Oe(e);if(i){if(t&&i.srcObject!==t)return;i.srcObject=null}},Wt=(e,t)=>i(void 0,void 0,void 0,(function*(){const i=Oe(e);if(null===i)return Ie.info("No HTMLMediaElement to attach the speakerId"),!1;if("string"!=typeof t)return Ie.info(`Invalid speaker deviceId: '${t}'`),!1;try{return yield i.setSinkId(t),!0}catch(e){return!1}})),Bt=e=>{e&&"live"===e.readyState&&e.stop()},Vt=e=>{Ht(e)&&e.getTracks().forEach(Bt),e=null},qt=e=>i(void 0,void 0,void 0,(function*(){Ie.info("RTCService.getUserMedia",e);const{audio:i,video:n}=e;if(!i&&!n)return null;try{return yield $t(e)}catch(i){if(Ie.error("getUserMedia error: ",i),(e=>"NotReadableError"===e.name||"NotFoundError"===e.name||"OverconstrainedError"===e.name)(i)){const n=(e=>{const{audio:i,video:n}=e;let s=!1,o=i,r=n;if("object"==typeof i&&null!==i&&"deviceId"in i){s=!0;const e=t(i,["deviceId"]);o=0===Object.keys(e).length||e}if("object"==typeof n&&null!==n&&"deviceId"in n){s=!0;const e=t(n,["deviceId"]);r=0===Object.keys(e).length||e}return s?{audio:o,video:r}:null})(e);if(n){Ie.warn("Device not found or not readable, falling back to default device");try{return yield $t(n)}catch(e){throw Ie.error("Fallback getUserMedia also failed: ",e),i}}}throw i}})),Yt=(e=null,t=!1)=>i(void 0,void 0,void 0,(function*(){let i=[];const n=yield navigator.mediaDevices.getUserMedia(((e=null)=>({audio:!e||e===Xe.AudioIn||e===Xe.AudioOut,video:!e||e===Xe.Video}))(e)).catch((e=>(Ie.error(e),null)));if(n){if(Vt(n),i=yield navigator.mediaDevices.enumerateDevices(),e&&(i=i.filter((t=>t.kind===e))),!0===t)return i;const s=[];i=i.filter((e=>{if(!e.groupId)return!0;const t=`${e.kind}-${e.groupId}`;return!s.includes(t)&&(s.push(t),!0)}))}return i})),Kt=[[320,240],[640,360],[640,480],[1280,720],[1920,1080]],Jt=(e,t,n)=>i(void 0,void 0,void 0,(function*(){const i=yield Yt(n,!0);for(let n=0;n<i.length;n++){const{deviceId:s,label:o}=i[n];if(e===s||t===o)return s}return null})),Qt=e=>{const t=navigator.mediaDevices.getSupportedConstraints();Object.keys(e).map((i=>{t.hasOwnProperty(i)&&null!==e[i]&&void 0!==e[i]||delete e[i]}))},Xt=(e,t)=>{if(!e)return!1;const{subscribed:i,alreadySubscribed:n}=zt(e);return i.includes(t)||n.includes(t)},zt=e=>{const t={subscribed:[],alreadySubscribed:[],unauthorized:[],unsubscribed:[],notSubscribed:[]};return Object.keys(t).forEach((i=>{t[i]=e[`${i}Channels`]||[]})),t},Zt=(e,t=null,i=null)=>{if(!Ht(e))return null;let n=[];switch(t){case"audio":n=e.getAudioTracks();break;case"video":n=e.getVideoTracks();break;default:n=e.getTracks()}n.forEach((e=>{switch(i){case"on":case!0:e.enabled=!0;break;case"off":case!1:e.enabled=!1;break;default:e.enabled=!e.enabled}}))},ei=e=>{Zt(e,"audio",!0)},ti=e=>{Zt(e,"audio",!1)},ii=e=>{Zt(e,"audio",null)},ni=e=>((e,t=null)=>{if(!Ht(e))return null;let i=[];switch(t){case"audio":i=e.getAudioTracks();break;case"video":i=e.getVideoTracks();break;default:i=e.getTracks()}return i.some((e=>e.enabled))})(e,"audio");function si(){try{const{browserInfo:e,name:t,version:i,supportAudio:n,supportVideo:s}=function(){if(!window||!window.navigator||!window.navigator.userAgent)throw new Error("You should use @telnyx/webrtc in a web browser such as Chrome|Firefox|Safari");if(navigator.userAgent.match(/chrom(e|ium)/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)&&!navigator.userAgent.match(/edg/gim)){const e=navigator.userAgent.match(/chrom(e|ium)\/[0-9]+\./gim)[0].split("/"),t=e[0],i=parseInt(e[1],10);return{browserInfo:navigator.userAgent,name:t,version:i,supportAudio:!0,supportVideo:!0}}if(navigator.userAgent.match(/firefox/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)&&!navigator.userAgent.match(/edg/gim)){const e=navigator.userAgent.match(/firefox\/[0-9]+\./gim)[0].split("/"),t=e[0],i=parseInt(e[1],10);return{browserInfo:navigator.userAgent,name:t,version:i,supportAudio:!0,supportVideo:!1}}if(navigator.userAgent.match(/safari/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)&&!navigator.userAgent.match(/edg/gim)){const e=navigator.userAgent.match(/safari/gim)[0],t=navigator.userAgent.match(/version\/[0-9]+\./gim)[0].split("/"),i=parseInt(t[1],10);return{browserInfo:navigator.userAgent,name:e,version:i,supportAudio:!0,supportVideo:!0}}if(navigator.userAgent.match(/edg/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)){const e=navigator.userAgent.match(/edg\/[0-9]+\./gim)[0].split("/"),t=e[0],i=parseInt(e[1],10);return{browserInfo:navigator.userAgent,name:t,version:i,supportAudio:!0,supportVideo:!0}}throw new Error("This browser does not support @telnyx/webrtc. To see browser support list: `TelnyxRTC.webRTCSupportedBrowserList()`")}(),o=window.RTCPeerConnection,r=window.RTCSessionDescription,a=window.RTCIceCandidate,c=window.navigator&&window.navigator.mediaDevices,d=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.msGetUserMedia||navigator.mozGetUserMedia;return{browserInfo:e,browserName:t,browserVersion:i,supportWebRTC:!!(o&&r&&a&&c&&d),supportWebRTCAudio:n,supportWebRTCVideo:s,supportRTCPeerConnection:!!o,supportSessionDescription:!!r,supportIceCandidate:!!a,supportMediaDevices:!!c,supportGetUserMedia:!!qt}}catch(e){return e.message}}var oi;function ri(e,t){const i=document.getElementById(t);if(i)return i;if(e&&t){const i=document.createElement("audio");return i.id=t,i.loop=!0,i.src=e,i.preload="auto",i.load(),document.body.appendChild(i),i}return null}function ai(e){e&&(e._playFulfilled=!1,e._promise=e.play(),e._promise.then((()=>{e._playFulfilled=!0})).catch((t=>{Ie.error("playAudio",t),e._playFulfilled=!0})))}function ci(e){e&&(e._playFulfilled?(e.pause(),e.currentTime=0):e._promise&&e._promise.then?e._promise.then((()=>{e.pause(),e.currentTime=0})):setTimeout((()=>{e.pause(),e.currentTime=0}),1e3))}!function(e){e.not_supported="not supported",e.full="full",e.partial="partial"}(oi||(oi={}));const di=e=>{const t=[],i=[];return e&&0!==e.length?(e.forEach((e=>{const n=e.mimeType.toLocaleLowerCase();n.startsWith("audio/")?t.push(e):n.startsWith("video/")&&i.push(e)})),{audioCodecs:t,videoCodecs:i}):{audioCodecs:t,videoCodecs:i}};class li extends vt{constructor(e,t){super(),this.method="ai_conversation",this.buildRequest({method:this.method,params:{type:"conversation.item.create",previous_item_id:null,item:{id:c(),type:"message",role:"user",content:[{type:"input_text",text:e},...null==t?void 0:t.map((e=>({type:"image_url",image_url:{url:e}})))]}}})}}class hi{constructor(e,t){var i;this.peerConnection=null,this.intervalId=null,this.statsBuffer=[],this.intervalStartTime=null,this.callEndTime=null,this.logCollector=null,this.intervalAudioLevels={outbound:[],inbound:[]},this.intervalJitters=[],this.intervalRTTs=[],this.intervalBitrates={outbound:[],inbound:[]},this.previousStats={},this.previousCandidatePairSnapshot=null,this.MAX_BUFFER_SIZE=360,this.onFlushNeeded=null,this.onWarning=null,this._breachCounters={},this._activeWarnings=new Set,this._lastWarningEmitted={},this._prevPacketsReceived=null,this._prevPacketsLost=null,this._previousStatsEntryForWarnings=null,this._lastLocalAudioTrackSnapshotJson=null,this._hasConfirmedLocalAudio=!1,this._confirmedLocalAudioSilenceMs=0,this._segmentIndex=0,this._flushing=!1,this._stopped=!1,this.options=e,this.logCollectorOptions=t||{enabled:!1,level:"debug",maxEntries:1e3},this.callStartTime=new Date,this._lastIntermediateFlushTime=this.callStartTime,this.logCollectorOptions.enabled&&(this.logCollector=function(e){return new be(e)}(this.logCollectorOptions),this.logCollector.start(),i=this.logCollector,ye=i)}start(e){var t,i;this.options.enabled&&(this.peerConnection=e,this.intervalStartTime=new Date,this._lastIntermediateFlushTime=this.intervalStartTime,this._stopped=!1,Ie.info("CallReportCollector: Starting stats collection",{interval:this.options.interval,initialInterval:hi.INITIAL_COLLECTION_INTERVAL_MS,initialDuration:hi.INITIAL_COLLECTION_DURATION_MS,logCollectorActive:null!==(i=null===(t=this.logCollector)||void 0===t?void 0:t.isActive())&&void 0!==i&&i}),this._scheduleNextCollection())}stop(){var e,t;return i(this,void 0,void 0,(function*(){this._stopped=!0,this.intervalId&&(clearTimeout(this.intervalId),this.intervalId=null),this.callEndTime=new Date,this.peerConnection&&this.intervalStartTime&&(yield this._collectStats(!0));const i=null!==(t=null===(e=this.logCollector)||void 0===e?void 0:e.getLogCount())&&void 0!==t?t:0;this.logCollector&&this.logCollector.stop(),Ie.info("CallReportCollector: Stopped stats collection",{totalIntervals:this.statsBuffer.length,totalLogs:i,duration:this.callEndTime.getTime()-this.callStartTime.getTime()})}))}flush(e,t){var i,n;if(this._flushing||0===this.statsBuffer.length)return null;this._flushing=!0;try{const s=this._segmentIndex++,o=this.statsBuffer;this.statsBuffer=[];const r=null!==(n=null===(i=this.logCollector)||void 0===i?void 0:i.drain())&&void 0!==n?n:[],a=new Date;this._lastIntermediateFlushTime=a;const c=Object.assign(Object.assign(Object.assign({summary:Object.assign(Object.assign({},e),{durationSeconds:(a.getTime()-this.callStartTime.getTime())/1e3,startTimestamp:this.callStartTime.toISOString(),endTimestamp:a.toISOString()}),stats:o},r.length>0?{logs:r}:{}),{segment:s}),t?{flushReason:t}:{});return Ie.info("CallReportCollector: Flushed intermediate segment",{segment:s,intervals:o.length,logEntries:r.length,callId:e.callId,flushReason:t}),c}finally{this._flushing=!1}}postReport(e,t,n,s){var o,r;return i(this,void 0,void 0,(function*(){const i=null===(o=this.logCollector)||void 0===o?void 0:o.getLogs(),a=i&&i.length>0;if(!this.options.enabled)return void Ie.info("CallReportCollector: Skipping report — call reports disabled");if(0===this.statsBuffer.length&&!a)return void Ie.info("CallReportCollector: Skipping report — no stats or logs collected");const c=this._segmentIndex>0,d=this._segmentIndex,l=Object.assign(Object.assign({summary:Object.assign(Object.assign({},e),{durationSeconds:this.callEndTime&&this.callStartTime?(this.callEndTime.getTime()-this.callStartTime.getTime())/1e3:void 0,startTimestamp:this.callStartTime.toISOString(),endTimestamp:null===(r=this.callEndTime)||void 0===r?void 0:r.toISOString()}),stats:this.statsBuffer},i&&i.length>0?{logs:i}:{}),c?{segment:d}:{});yield this._sendPayload(l,t,n,s,!0)}))}sendPayload(e,t,n,s){return i(this,void 0,void 0,(function*(){yield this._sendPayload(e,t,n,s,!1)}))}_sendPayload(e,t,n,s,o=!0){var r,a;return i(this,void 0,void 0,(function*(){const i=new URL(n),c=`${i.protocol.replace(/^ws/,"http")}//${i.host}/call_report`,d=o?"final report":`intermediate segment ${e.segment}`;Ie.info(`CallReportCollector: Posting ${d}`,{endpoint:c,intervals:e.stats.length,logEntries:null!==(a=null===(r=e.logs)||void 0===r?void 0:r.length)&&void 0!==a?a:0,callId:e.summary.callId,segment:e.segment});const l={"Content-Type":"application/json","x-call-report-id":t,"x-call-id":e.summary.callId};s&&(l["x-voice-sdk-id"]=s);const h=JSON.stringify(e),u=o&&h.length<=hi.KEEPALIVE_BODY_LIMIT_BYTES;let p;for(let e=0;e<=hi.RETRY_DELAYS_MS.length;e++){try{const t=yield fetch(c,Object.assign({method:"POST",headers:l,body:h},u?{keepalive:!0}:{}));if(t.ok)return void Ie.info(`CallReportCollector: Successfully posted ${d}`,{attempt:e+1,status:t.status});{const i=yield t.text();p=new Error(`Call report POST failed with status ${t.status}`),Ie.error(`CallReportCollector: Failed to post ${d}`,{attempt:e+1,status:t.status,error:i})}}catch(t){p=t,Ie.warn(`CallReportCollector: Network error posting ${d}`,{attempt:e+1,error:t})}const t=hi.RETRY_DELAYS_MS[e];if(void 0===t)break;Ie.info(`CallReportCollector: Retrying ${d} in ${t}ms`,{attempt:e+2}),yield new Promise((e=>setTimeout(e,t)))}throw Ie.error(`CallReportCollector: Exhausted retries posting ${d}`,{error:p}),p instanceof Error?p:new Error("Call report POST failed after retries")}))}getStatsBuffer(){return this.statsBuffer}shouldForceRelayCandidateForRecovery(){var e,t,i,n,s,o,r,a,c,d,l,h,u,p,g,v;if(this.statsBuffer.length<2)return!1;const m=this.statsBuffer[this.statsBuffer.length-1],f=this.statsBuffer[this.statsBuffer.length-2],_=null===(e=m.ice)||void 0===e?void 0:e.local;if(!("vpn"===(null==_?void 0:_.networkType)&&"relay"!==(null==_?void 0:_.candidateType)))return!1;const S=!1===(null===(t=m.ice)||void 0===t?void 0:t.writable),b="disconnected"===(null===(i=m.transport)||void 0===i?void 0:i.iceState)||"failed"===(null===(n=m.transport)||void 0===n?void 0:n.iceState),y=this._positiveDelta(null===(s=m.ice)||void 0===s?void 0:s.requestsSent,null===(o=f.ice)||void 0===o?void 0:o.requestsSent),I=this._positiveDelta(null===(r=m.ice)||void 0===r?void 0:r.responsesReceived,null===(a=f.ice)||void 0===a?void 0:a.responsesReceived),E=y>0&&0===I,C=this._positiveDelta(null===(d=null===(c=m.audio)||void 0===c?void 0:c.outbound)||void 0===d?void 0:d.bytesSent,null===(h=null===(l=f.audio)||void 0===l?void 0:l.outbound)||void 0===h?void 0:h.bytesSent),w=this._positiveDelta(null===(p=null===(u=m.audio)||void 0===u?void 0:u.inbound)||void 0===p?void 0:p.bytesReceived,null===(v=null===(g=f.audio)||void 0===g?void 0:g.inbound)||void 0===v?void 0:v.bytesReceived);return S||b||E||C>0&&0===w}getLogs(){var e,t;return null!==(t=null===(e=this.logCollector)||void 0===e?void 0:e.getLogs())&&void 0!==t?t:[]}_positiveDelta(e,t){return void 0===e||void 0===t?0:Math.max(0,e-t)}cleanup(){this._lastLocalAudioTrackSnapshotJson=null,this.logCollector&&(this.logCollector.clear(),this.logCollector=null)}_scheduleNextCollection(){if(this._stopped||!this.peerConnection||!this.intervalStartTime||this.intervalId)return;const e=this._collectionIntervalFor(this.intervalStartTime);this.intervalId=setTimeout((()=>{this.intervalId=null,this._collectStats().finally((()=>{!this._stopped&&this.peerConnection&&this._scheduleNextCollection()}))}),e)}_collectionIntervalFor(e){const t=this._positiveInterval(this.options.interval,5e3);return e.getTime()-this.callStartTime.getTime()<hi.INITIAL_COLLECTION_DURATION_MS?Math.min(hi.INITIAL_COLLECTION_INTERVAL_MS,t):t}_positiveInterval(e,t){return"number"==typeof e&&Number.isFinite(e)&&e>0?e:t}_collectStats(e=!1){return i(this,void 0,void 0,(function*(){if(this.peerConnection&&this.intervalStartTime)try{const t=yield this.peerConnection.getStats(),i=new Date;let n,s,o,r,a,c=null,d=null,l=null,h=null;if(t.forEach((e=>{switch(e.type){case"outbound-rtp":"audio"===e.kind&&"audio"===e.mediaType&&(c=e);break;case"inbound-rtp":"audio"===e.kind&&"audio"===e.mediaType&&(d=e);break;case"candidate-pair":(e.nominated||"succeeded"===e.state)&&(l=e);break;case"transport":h=e}})),c){const e=this._getOutboundAudioLevel(t,c);if(null!==e&&this.intervalAudioLevels.outbound.push(e),void 0!==this.previousStats.outboundBytes&&void 0!==this.previousStats.timestamp){const e=(c.bytesSent||0)-this.previousStats.outboundBytes,t=(c.timestamp||i.getTime())-this.previousStats.timestamp;if(t>0){const i=8*e*1e3/t;this.intervalBitrates.outbound.push(i)}}this.previousStats.outboundBytes=c.bytesSent}if(d){const e=this._getInboundAudioLevel(t,d);if(null!==e&&this.intervalAudioLevels.inbound.push(e),void 0!==d.jitter&&this.intervalJitters.push(1e3*d.jitter),void 0!==this.previousStats.inboundBytes&&void 0!==this.previousStats.timestamp){const e=(d.bytesReceived||0)-this.previousStats.inboundBytes,t=(d.timestamp||i.getTime())-this.previousStats.timestamp;if(t>0){const i=8*e*1e3/t;this.intervalBitrates.inbound.push(i)}}this.previousStats.inboundBytes=d.bytesReceived}l&&void 0!==l.currentRoundTripTime&&this.intervalRTTs.push(l.currentRoundTripTime),l&&(n=this._resolveCandidate(t,l.localCandidateId),s=this._resolveCandidate(t,l.remoteCandidateId)),l&&(o=Object.assign(Object.assign({id:l.id,state:l.state,nominated:l.nominated,writable:l.writable,requestsSent:l.requestsSent,responsesReceived:l.responsesReceived},n?{local:n}:{}),s?{remote:s}:{}),null!==this.previousCandidatePairSnapshot&&l.id!==this.previousCandidatePairSnapshot.id&&(Ie.debug("CallReportCollector: ICE candidate pair changed mid-call",{previous:this.previousCandidatePairSnapshot,current:o}),this._emitWarning(Y)),this.previousCandidatePairSnapshot=o),c&&(r=this._getLocalAudioTrackSnapshot(),a=this._getOutboundAudioSourceStats(t,c)),this._logLocalAudioTrackSnapshot(r,a),this.previousStats.timestamp=i.getTime();const u=i.getTime()-this.intervalStartTime.getTime(),p=this._collectionIntervalFor(this.intervalStartTime);if(e||u>=p){const e=this._createStatsEntry(this.intervalStartTime,i,c,d,l,n,s,h,r,a);this.statsBuffer.push(e),this.statsBuffer.length>this.MAX_BUFFER_SIZE&&(this.statsBuffer.shift(),Ie.warn("CallReportCollector: Buffer size limit reached, removing oldest entry")),this._checkQualityWarnings(e,d),this._requestIntermediateFlushIfNeeded(i),this.intervalStartTime=i,this._resetIntervalAccumulators()}}catch(e){Ie.error("CallReportCollector: Error collecting stats",{error:e})}}))}_getIntermediateReportInterval(){var e;return null!==(e=this.options.intermediateReportInterval)&&void 0!==e?e:hi.DEFAULT_INTERMEDIATE_REPORT_INTERVAL_MS}_requestIntermediateFlushIfNeeded(e){var t,i;if(!this.onFlushNeeded||this._flushing||0===this.statsBuffer.length)return;const n=this.statsBuffer.length,s=null!==(i=null===(t=this.logCollector)||void 0===t?void 0:t.getLogCount())&&void 0!==i?i:0,o=this._getIntermediateReportInterval(),r=e.getTime()-this._lastIntermediateFlushTime.getTime();let a=null;if(n>=hi.STATS_FLUSH_THRESHOLD||s>=hi.LOGS_FLUSH_THRESHOLD?a="buffer-limit":o>0&&r>=o&&(a="safety-interval"),a){Ie.info("CallReportCollector: Requesting intermediate report flush",{reason:a,statsIntervals:n,logEntries:s,msSinceLastFlush:r}),this._lastIntermediateFlushTime=e;try{this.onFlushNeeded()}catch(e){Ie.error("CallReportCollector: onFlushNeeded callback error",{error:e})}}}_checkQualityWarnings(e,t){var i,n,s,o,r,a,c,d,l,h,u,p,g,v,m;if(!this.onWarning)return;const f=null===(i=e.connection)||void 0===i?void 0:i.roundTripTimeAvg,_=null===(s=null===(n=e.audio)||void 0===n?void 0:n.inbound)||void 0===s?void 0:s.jitterAvg;let S;if(t){const e=null!==(o=t.packetsReceived)&&void 0!==o?o:0,i=null!==(r=t.packetsLost)&&void 0!==r?r:0;if(null!==this._prevPacketsReceived&&null!==this._prevPacketsLost){const t=e-this._prevPacketsReceived,n=i-this._prevPacketsLost,s=t+n;s>0&&(S=n/s*100)}this._prevPacketsReceived=e,this._prevPacketsLost=i}if(this._trackBreach(P,void 0!==f&&f>hi.THRESHOLD_RTT_MS),this._trackBreach(M,void 0!==_&&_>hi.THRESHOLD_JITTER_MS),this._trackBreach(x,void 0!==S&&S>hi.THRESHOLD_PACKET_LOSS_PCT),this._trackLowLocalAudio(e),void 0!==f&&void 0!==_&&void 0!==S){const e=93.2-.11*_-2.5*S-.01*(1e3*f),t=Math.max(1,Math.min(4.5,1+.035*e+e*(e-60)*(100-e)*7e-6));this._trackBreach(U,t<hi.THRESHOLD_MOS)}else this._trackBreach(U,!1);if(void 0!==(null===(c=null===(a=e.audio)||void 0===a?void 0:a.inbound)||void 0===c?void 0:c.bytesReceived)){const t=null===(h=null===(l=null===(d=this._previousStatsEntryForWarnings)||void 0===d?void 0:d.audio)||void 0===l?void 0:l.inbound)||void 0===h?void 0:h.bytesReceived,i=e.audio.inbound.bytesReceived;this._trackBreach($,void 0!==t&&i-t==0)}if(void 0!==(null===(p=null===(u=e.audio)||void 0===u?void 0:u.outbound)||void 0===p?void 0:p.bytesSent)){const t=null===(m=null===(v=null===(g=this._previousStatsEntryForWarnings)||void 0===g?void 0:g.audio)||void 0===v?void 0:v.outbound)||void 0===m?void 0:m.bytesSent,i=e.audio.outbound.bytesSent;this._trackBreach(H,void 0!==t&&i-t==0)}this._previousStatsEntryForWarnings=e}_trackLowLocalAudio(e){var t;const i=null===(t=e.audio)||void 0===t?void 0:t.outbound,n=null==i?void 0:i.audioLevelAvg,s=null==i?void 0:i.localTrack;if(void 0===n||!1===(null==s?void 0:s.enabled)||!0===(null==s?void 0:s.muted))return void this._resetLowLocalAudioWarning();if(!(n<=hi.THRESHOLD_LOCAL_AUDIO_LEVEL))return this._hasConfirmedLocalAudio=!0,this._confirmedLocalAudioSilenceMs=0,void this._trackBreach(F,!1);this._hasConfirmedLocalAudio?(this._confirmedLocalAudioSilenceMs+=this._getStatsIntervalDurationMs(e),this._confirmedLocalAudioSilenceMs>=hi.CONFIRMED_LOCAL_AUDIO_SILENCE_MS&&this._emitWarningOncePerEpisode(F)):this._trackBreach(F,!0)}_resetLowLocalAudioWarning(){this._confirmedLocalAudioSilenceMs=0,this._trackBreach(F,!1)}_getStatsIntervalDurationMs(e){const t=new Date(e.intervalStartUtc).getTime(),i=new Date(e.intervalEndUtc).getTime()-t;return Number.isFinite(i)&&i>0?i:this.options.interval}_trackBreach(e,t){var i,n;if(t){if(this._breachCounters[e]=(null!==(i=this._breachCounters[e])&&void 0!==i?i:0)+1,this._breachCounters[e]>=hi.CONSECUTIVE_BREACHES_REQUIRED){this._activeWarnings.add(e);const t=Date.now();t-(null!==(n=this._lastWarningEmitted[e])&&void 0!==n?n:0)>=hi.WARNING_THROTTLE_MS&&(this._lastWarningEmitted[e]=t,this._emitWarning(e))}}else this._breachCounters[e]=0,this._activeWarnings.delete(e),delete this._lastWarningEmitted[e]}_emitWarningOncePerEpisode(e){this._activeWarnings.has(e)||(this._activeWarnings.add(e),this._lastWarningEmitted[e]=Date.now(),this._emitWarning(e))}_emitWarning(e){var t;try{const i=le(e);Ie.warn(`CallReportCollector: warning ${i.code}: ${i.message}`),null===(t=this.onWarning)||void 0===t||t.call(this,i)}catch(t){Ie.error(`CallReportCollector: Failed to emit warning ${e}`,{error:t})}}_createStatsEntry(e,t,i,n,s,o,r,a,c,d){const l={intervalStartUtc:e.toISOString(),intervalEndUtc:t.toISOString(),audio:{}};return i&&(l.audio.outbound=Object.assign(Object.assign({packetsSent:i.packetsSent,bytesSent:i.bytesSent,audioLevelAvg:this._average(this.intervalAudioLevels.outbound),bitrateAvg:this._average(this.intervalBitrates.outbound)},c?{localTrack:c}:{}),d?{mediaSource:d}:{})),n&&(l.audio.inbound={packetsReceived:n.packetsReceived,bytesReceived:n.bytesReceived,packetsLost:n.packetsLost,packetsDiscarded:n.packetsDiscarded,jitterBufferDelay:n.jitterBufferDelay,jitterBufferEmittedCount:n.jitterBufferEmittedCount,totalSamplesReceived:n.totalSamplesReceived,concealedSamples:n.concealedSamples,concealmentEvents:n.concealmentEvents,audioLevelAvg:this._average(this.intervalAudioLevels.inbound),jitterAvg:this._average(this.intervalJitters),bitrateAvg:this._average(this.intervalBitrates.inbound)}),s&&(l.connection={roundTripTimeAvg:this._average(this.intervalRTTs),packetsSent:s.packetsSent,packetsReceived:s.packetsReceived,bytesSent:s.bytesSent,bytesReceived:s.bytesReceived},l.ice=Object.assign(Object.assign({id:s.id,state:s.state,nominated:s.nominated,writable:s.writable,requestsSent:s.requestsSent,responsesReceived:s.responsesReceived},o?{local:o}:{}),r?{remote:r}:{})),a&&(l.transport=Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},void 0!==a.iceState?{iceState:a.iceState}:{}),void 0!==a.dtlsState?{dtlsState:a.dtlsState}:{}),void 0!==a.srtpCipher?{srtpCipher:a.srtpCipher}:{}),void 0!==a.tlsVersion?{tlsVersion:a.tlsVersion}:{}),void 0!==a.selectedCandidatePairChanges?{selectedCandidatePairChanges:a.selectedCandidatePairChanges}:{})),l}_resolveCandidate(e,t){if(!t)return void Ie.debug("CallReportCollector: candidateId is empty, skipping resolve");const i=e.get(t);if(!i)return void Ie.debug("CallReportCollector: candidate not found in stats report",{candidateId:t});const n={};if(void 0!==i.address?n.address=i.address:void 0!==i.ip&&(n.address=i.ip),void 0!==i.port&&(n.port=i.port),void 0!==i.candidateType&&(n.candidateType=i.candidateType),void 0!==i.protocol&&(n.protocol=i.protocol),void 0!==i.networkType&&(n.networkType=i.networkType),0!==Object.keys(n).length)return n;Ie.debug("CallReportCollector: candidate report has no usable fields",{candidateId:t})}_getOutboundMediaSource(e,t){let i;return t.mediaSourceId&&(i=e.get(t.mediaSourceId)),i||e.forEach((e=>{i||"media-source"!==e.type||"audio"!==e.kind||(i=e)})),i}_getLocalAudioTrackSnapshot(){var e,t;try{const i=null===(e=this.peerConnection)||void 0===e?void 0:e.getSenders().find((e=>{var t;return"audio"===(null===(t=e.track)||void 0===t?void 0:t.kind)})),n=null==i?void 0:i.track;if(!n)return;const s=null===(t=n.getSettings)||void 0===t?void 0:t.call(n),o=this._withoutUndefined({deviceId:null==s?void 0:s.deviceId,groupId:null==s?void 0:s.groupId,channelCount:null==s?void 0:s.channelCount,sampleRate:null==s?void 0:s.sampleRate,sampleSize:null==s?void 0:s.sampleSize,latency:null==s?void 0:s.latency,echoCancellation:null==s?void 0:s.echoCancellation,noiseSuppression:null==s?void 0:s.noiseSuppression,autoGainControl:null==s?void 0:s.autoGainControl}),r=this._withoutUndefined(Object.assign({id:n.id,label:n.label,enabled:n.enabled,muted:n.muted,readyState:n.readyState,contentHint:n.contentHint},Object.keys(o).length>0?{settings:o}:{}));return Object.keys(r).length>0?r:void 0}catch(e){return void Ie.debug("CallReportCollector: unable to snapshot local audio track",{error:e})}}_getOutboundAudioSourceStats(e,t){const i=this._getOutboundMediaSource(e,t);if(!i)return;const n=this._withoutUndefined({id:i.id,trackIdentifier:i.trackIdentifier,audioLevel:i.audioLevel,totalAudioEnergy:i.totalAudioEnergy,totalSamplesDuration:i.totalSamplesDuration,echoReturnLoss:i.echoReturnLoss,echoReturnLossEnhancement:i.echoReturnLossEnhancement});return Object.keys(n).length>0?n:void 0}_logLocalAudioTrackSnapshot(e,t){if(!e||0===Object.keys(e).length)return;const i=this._stableStringify(e);i!==this._lastLocalAudioTrackSnapshotJson&&(this._lastLocalAudioTrackSnapshotJson=i,Ie.debug("CallReportCollector: local audio track snapshot",{localTrack:e,mediaSource:t}))}_withoutUndefined(e){return Object.keys(e).reduce(((t,i)=>{const n=e[i];return void 0!==n&&(t[i]=n),t}),{})}_stableStringify(e){return JSON.stringify(this._sortObjectKeys(e))}_sortObjectKeys(e){return Array.isArray(e)?e.map((e=>this._sortObjectKeys(e))):e&&"object"==typeof e?Object.keys(e).sort().reduce(((t,i)=>(t[i]=this._sortObjectKeys(e[i]),t)),{}):e}_getOutboundAudioLevel(e,t){const i=this._getOutboundMediaSource(e,t);if(void 0!==(null==i?void 0:i.audioLevel))return i.audioLevel;if(i){const e=this._computeAudioLevelFromEnergy(i.totalAudioEnergy,i.totalSamplesDuration,"outbound");if(null!==e)return e}return this._getTrackAudioLevel(e,t.trackId)}_getInboundAudioLevel(e,t){if(void 0!==t.audioLevel)return t.audioLevel;const i=this._computeAudioLevelFromEnergy(t.totalAudioEnergy,t.totalSamplesDuration,"inbound");return null!==i?i:this._getTrackAudioLevel(e,t.trackId)}_computeAudioLevelFromEnergy(e,t,i){if(void 0===e||void 0===t)return null;const n="inbound"===i?"inboundAudioEnergy":"outboundAudioEnergy",s="inbound"===i?"inboundSamplesDuration":"outboundSamplesDuration",o=this.previousStats[n],r=this.previousStats[s];if(this.previousStats[n]=e,this.previousStats[s]=t,void 0===o||void 0===r)return null;const a=e-o,c=t-r;if(c<=0)return null;const d=Math.sqrt(a/c);return Math.min(1,Math.max(0,d))}_getTrackAudioLevel(e,t){var i;if(!t)return null;const n=e.get(t);return n&&null!==(i=n.audioLevel)&&void 0!==i?i:null}_average(e){if(0===e.length)return;const t=e.reduce(((e,t)=>e+t),0);return parseFloat((t/e.length).toFixed(4))}_resetIntervalAccumulators(){this.intervalAudioLevels={outbound:[],inbound:[]},this.intervalJitters=[],this.intervalRTTs=[],this.intervalBitrates={outbound:[],inbound:[]}}}hi.INITIAL_COLLECTION_INTERVAL_MS=1e3,hi.INITIAL_COLLECTION_DURATION_MS=1e4,hi.STATS_FLUSH_THRESHOLD=300,hi.LOGS_FLUSH_THRESHOLD=800,hi.DEFAULT_INTERMEDIATE_REPORT_INTERVAL_MS=18e4,hi.CONSECUTIVE_BREACHES_REQUIRED=3,hi.THRESHOLD_RTT_MS=.4,hi.THRESHOLD_JITTER_MS=30,hi.THRESHOLD_PACKET_LOSS_PCT=1,hi.THRESHOLD_MOS=3.5,hi.THRESHOLD_LOCAL_AUDIO_LEVEL=.001,hi.CONFIRMED_LOCAL_AUDIO_SILENCE_MS=3e4,hi.WARNING_THROTTLE_MS=15e3,hi.RETRY_DELAYS_MS=[500,1e3,2e3],hi.KEEPALIVE_BODY_LIMIT_BYTES=61440;var ui,pi=fe((function(e,t){var i;function n(){}function s(){s.init.call(this)}function o(e){return void 0===e._maxListeners?s.defaultMaxListeners:e._maxListeners}function r(e,t,i,s){var r,a,c;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),a=e._events),c=a[t]):(a=e._events=new n,e._eventsCount=0),c){if("function"==typeof c?c=a[t]=s?[i,c]:[c,i]:s?c.unshift(i):c.push(i),!c.warned&&((r=o(e))&&0<r&&c.length>r)){c.warned=!0;var d=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");d.name="MaxListenersExceededWarning",d.emitter=e,d.type=t,d.count=c.length,function(e){"function"==typeof console.warn?console.warn(e):console.log(e)}(d)}}else c=a[t]=i,++e._eventsCount;return e}function a(e,t,i){function n(){e.removeListener(t,n),s||(s=!0,i.apply(e,arguments))}var s=!1;return n.listener=i,n}function c(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function d(e,t){for(var i=Array(t);t--;)i[t]=e[t];return i}Object.defineProperty(t,"__esModule",{value:!0}),n.prototype=Object.create(null),s.EventEmitter=s,s.usingDomains=!1,s.prototype.domain=void 0,s.prototype._events=void 0,s.prototype._maxListeners=void 0,s.defaultMaxListeners=10,s.init=function(){this.domain=null,s.usingDomains&&i.active&&!(this instanceof i.Domain)&&(this.domain=i.active),this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new n,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},s.prototype.setMaxListeners=function(e){if("number"!=typeof e||0>e||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},s.prototype.getMaxListeners=function(){return o(this)},s.prototype.emit=function(e){var t,i,n,s,o,r,a,c="error"===e;if(r=this._events)c=c&&null==r.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var l=new Error('Uncaught, unspecified "error" event. ('+t+")");throw l.context=t,l}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(i=r[e]))return!1;var h="function"==typeof i;switch(n=arguments.length){case 1:!function(e,t,i){if(t)e.call(i);else for(var n=e.length,s=d(e,n),o=0;o<n;++o)s[o].call(i)}(i,h,this);break;case 2:!function(e,t,i,n){if(t)e.call(i,n);else for(var s=e.length,o=d(e,s),r=0;r<s;++r)o[r].call(i,n)}(i,h,this,arguments[1]);break;case 3:!function(e,t,i,n,s){if(t)e.call(i,n,s);else for(var o=e.length,r=d(e,o),a=0;a<o;++a)r[a].call(i,n,s)}(i,h,this,arguments[1],arguments[2]);break;case 4:!function(e,t,i,n,s,o){if(t)e.call(i,n,s,o);else for(var r=e.length,a=d(e,r),c=0;c<r;++c)a[c].call(i,n,s,o)}(i,h,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=Array(n-1),o=1;o<n;o++)s[o-1]=arguments[o];!function(e,t,i,n){if(t)e.apply(i,n);else for(var s=e.length,o=d(e,s),r=0;r<s;++r)o[r].apply(i,n)}(i,h,this,s)}return!0},s.prototype.addListener=function(e,t){return r(this,e,t,!1)},s.prototype.on=s.prototype.addListener,s.prototype.prependListener=function(e,t){return r(this,e,t,!0)},s.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,a(this,e,t)),this},s.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,a(this,e,t)),this},s.prototype.removeListener=function(e,t){var i,s,o,r,a;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(s=this._events))return this;if(!(i=s[e]))return this;if(i===t||i.listener&&i.listener===t)0==--this._eventsCount?this._events=new n:(delete s[e],s.removeListener&&this.emit("removeListener",e,i.listener||t));else if("function"!=typeof i){for(o=-1,r=i.length;0<r--;)if(i[r]===t||i[r].listener&&i[r].listener===t){a=i[r].listener,o=r;break}if(0>o)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new n,this;delete s[e]}else!function(e,t){for(var i=t,n=i+1,s=e.length;n<s;i+=1,n+=1)e[i]=e[n];e.pop()}(i,o);s.removeListener&&this.emit("removeListener",e,a||t)}return this},s.prototype.removeAllListeners=function(e){var t,i;if(!(i=this._events))return this;if(!i.removeListener)return 0===arguments.length?(this._events=new n,this._eventsCount=0):i[e]&&(0==--this._eventsCount?this._events=new n:delete i[e]),this;if(0===arguments.length){for(var s,o=Object.keys(i),r=0;r<o.length;++r)"removeListener"!==(s=o[r])&&this.removeAllListeners(s);return this.removeAllListeners("removeListener"),this._events=new n,this._eventsCount=0,this}if("function"==typeof(t=i[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},s.prototype.listeners=function(e){var t,i,n=this._events;return n?i=(t=n[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=Array(e.length),i=0;i<t.length;++i)t[i]=e[i].listener||e[i];return t}(t):[]:i=[],i},s.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):c.call(e,t)},s.prototype.listenerCount=c,s.prototype.eventNames=function(){return 0<this._eventsCount?Reflect.ownKeys(this._events):[]};var l,h=new Uint8Array(16);function u(){if(!l&&!(l="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return l(h)}var p=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;for(var g=[],v=0;256>v;++v)g.push((v+256).toString(16).substr(1));function m(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:0,i=(g[e[t+0]]+g[e[t+1]]+g[e[t+2]]+g[e[t+3]]+"-"+g[e[t+4]]+g[e[t+5]]+"-"+g[e[t+6]]+g[e[t+7]]+"-"+g[e[t+8]]+g[e[t+9]]+"-"+g[e[t+10]]+g[e[t+11]]+g[e[t+12]]+g[e[t+13]]+g[e[t+14]]+g[e[t+15]]).toLowerCase();if(!function(e){return"string"==typeof e&&p.test(e)}(i))throw TypeError("Stringified UUID is invalid");return i}function f(e,t,i){var n=(e=e||{}).random||(e.rng||u)();if(n[6]=64|15&n[6],n[8]=128|63&n[8],t){i=i||0;for(var s=0;16>s;++s)t[i+s]=n[s];return t}return m(n)}function _(e,t){if(!e||!t)return{};const i={...e};if(i.localCandidateId){const e=t.get(i.localCandidateId);i.local={...e}}if(i.remoteCandidateId){const e=t.get(i.remoteCandidateId);i.remote={...e}}return i}function S(e,t,i){return 8*function(e,t,i){const n=e[i],s=t?t[i]:null;return null===n||null===s?null:(n-s)/(e.timestamp-t.timestamp)*1e3}(e,t,i)}function b(e){if(!e.entries)return e;const t={};return e.forEach((function(e,i){t[i]=e})),t}function y(e,t,i={}){if(!e)return null;let n={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]},connection:{inbound:[],outbound:[]}};i.remote&&(n.remote={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]}});for(const t of e.values())switch(t.type){case"outbound-rtp":{const i=t.mediaType||t.kind,s={};let o={};if(!["audio","video"].includes(i))continue;if(t.codecId){const i=e.get(t.codecId);i&&(s.clockRate=i.clockRate,s.mimeType=i.mimeType,s.payloadType=i.payloadType)}o=e.get(t.mediaSourceId)||e.get(t.trackId)||{},n[i].outbound.push({...t,...s,track:{...o}});break}case"inbound-rtp":{let i=t.mediaType||t.kind,s={};const o={};if(!["audio","video"].includes(i))if(t.id.includes("Video"))i="video";else{if(!t.id.includes("Audio"))continue;i="audio"}if(t.codecId){const i=e.get(t.codecId);i&&(o.clockRate=i.clockRate,o.mimeType=i.mimeType,o.payloadType=i.payloadType)}if(!n.connection.id&&t.transportId){const i=e.get(t.transportId);if(i&&i.selectedCandidatePairId){const t=e.get(i.selectedCandidatePairId);n.connection=_(t,e)}}s=e.get(t.mediaSourceId)||e.get(t.trackId)||{},n[i].inbound.push({...t,...o,track:{...s}});break}case"peer-connection":n.connection.dataChannelsClosed=t.dataChannelsClosed,n.connection.dataChannelsOpened=t.dataChannelsOpened;break;case"remote-inbound-rtp":{if(!i.remote)break;let s=t.mediaType||t.kind;const o={};if(!["audio","video"].includes(s))if(t.id.includes("Video"))s="video";else{if(!t.id.includes("Audio"))continue;s="audio"}if(t.codecId){const i=e.get(t.codecId);i&&(o.clockRate=i.clockRate,o.mimeType=i.mimeType,o.payloadType=i.payloadType)}if(!n.connection.id&&t.transportId){const i=e.get(t.transportId);if(i&&i.selectedCandidatePairId){const t=e.get(i.selectedCandidatePairId);n.connection=_(t,e)}}n.remote[s].inbound.push({...t,...o});break}case"remote-outbound-rtp":{if(!i.remote)break;const s=t.mediaType||t.kind,o={};if(!["audio","video"].includes(s))continue;if(t.codecId){const i=e.get(t.codecId);i&&(o.clockRate=i.clockRate,o.mimeType=i.mimeType,o.payloadType=i.payloadType)}n.remote[s].outbound.push({...t,...o});break}}if(!n.connection.id)for(const t of e.values())"candidate-pair"===t.type&&t.nominated&&"succeeded"===t.state&&(n.connection=_(t,e));return n=function(e,t){return t?(e.audio.inbound.map((e=>{let i=t.audio.inbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesReceived"),e.packetRate=S(e,i,"packetsReceived")})),e.audio.outbound.map((e=>{let i=t.audio.outbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesSent"),e.packetRate=S(e,i,"packetsSent")})),e.video.inbound.map((e=>{let i=t.video.inbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesReceived"),e.packetRate=S(e,i,"packetsReceived")})),e.video.outbound.map((e=>{let i=t.video.outbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesSent"),e.packetRate=S(e,i,"packetsSent")})),e):e}(n,t),n}let I,E={},C=[];t.WebRTCStats=class extends s{constructor(e){if(super(),this.monitoringSetInterval=0,this.connectionMonitoringSetInterval=0,this.connectionMonitoringInterval=1e3,this.remote=!0,this.peersToMonitor={},this.timeline=[],this.statsToMonitor=["inbound-rtp","outbound-rtp","remote-inbound-rtp","remote-outbound-rtp","peer-connection","data-channel","stream","track","sender","receiver","transport","candidate-pair","local-candidate","remote-candidate"],"undefined"==typeof window)throw new Error("WebRTCStats only works in browser");const t={...e};this.isEdge=!!window.RTCIceGatherer,this.getStatsInterval=t.getStatsInterval||1e3,this.rawStats=!!t.rawStats,this.statsObject=!!t.statsObject,this.filteredStats=!!t.filteredStats,this.shouldWrapGetUserMedia=!!t.wrapGetUserMedia,"boolean"==typeof t.remote&&(this.remote=t.remote),this.debug=!!t.debug,this.logLevel=t.logLevel||"none",this.shouldWrapGetUserMedia&&this.wrapGetUserMedia()}async addPeer(e,t){return console.warn("The addPeer() method has been deprecated, please use addConnection()"),this.addConnection({peerId:e,pc:t})}async addConnection(e){const{pc:t,peerId:i}=e;let{connectionId:n,remote:s}=e;if(s="boolean"==typeof s?s:this.remote,!(t&&t instanceof RTCPeerConnection))throw new Error("Missing argument 'pc' or is not of instance RTCPeerConnection");if(!i)throw new Error("Missing argument peerId");if(this.isEdge)throw new Error("Can't monitor peers in Edge at this time.");if(this.peersToMonitor[i]){if(n&&n in this.peersToMonitor[i])throw new Error(`We are already monitoring connection with id ${n}.`);for(let e in this.peersToMonitor[i]){const n=this.peersToMonitor[i][e];if(n.pc===t)throw new Error(`We are already monitoring peer with id ${i}.`);"closed"===n.pc.connectionState&&this.removeConnection({pc:n.pc})}}const o=t.getConfiguration();return o.iceServers&&o.iceServers.forEach((function(e){delete e.credential})),n||(n=f()),this.emitEvent({event:"addConnection",tag:"peer",peerId:i,connectionId:n,data:{options:e,peerConfiguration:o}}),this.monitorPeer({peerId:i,connectionId:n,pc:t,remote:s}),{connectionId:n}}getTimeline(e){return this.timeline=this.timeline.sort(((e,t)=>e.timestamp.getTime()-t.timestamp.getTime())),e?this.timeline.filter((t=>t.tag===e)):this.timeline}get logger(){const e=e=>{const t=["none","error","warn","info","debug"];return t.slice(0,t.indexOf(this.logLevel)+1).indexOf(e)>-1};return{error(...t){this.debug&&e("error")&&console.error("[webrtc-stats][error] ",...t)},warn(...t){this.debug&&e("warn")&&console.warn("[webrtc-stats][warn] ",...t)},info(...t){this.debug&&e("info")&&console.log("[webrtc-stats][info] ",...t)},debug(...t){this.debug&&e("debug")&&console.debug("[webrtc-stats][debug] ",...t)}}}removeConnection(e){let t,{connectionId:i,pc:n}=e;if(!n&&!i)throw new Error("Missing arguments. You need to either send pc or a connectionId.");if(i){if("string"!=typeof i)throw new Error("connectionId must be a string.");for(let e in this.peersToMonitor)i in this.peersToMonitor[e]&&(n=this.peersToMonitor[e][i].pc,t=e)}else if(n){if(!(n instanceof RTCPeerConnection))throw new Error("pc must be an instance of RTCPeerConnection.");for(let e in this.peersToMonitor)for(let s in this.peersToMonitor[e])this.peersToMonitor[e][s].pc===n&&(i=s,t=e)}if(!n||!i)throw new Error("Could not find the desired connection.");return this.removePeerConnectionEventListeners(i,n),delete this.peersToMonitor[t][i],0===Object.values(this.peersToMonitor[t]).length&&delete this.peersToMonitor[t],{connectionId:i}}removeAllPeers(){for(let e in this.peersToMonitor)this.removePeer(e)}removePeer(e){if(this.logger.info(`Removing PeerConnection with id ${e}.`),this.peersToMonitor[e]){for(let t in this.peersToMonitor[e]){let i=this.peersToMonitor[e][t].pc;this.removePeerConnectionEventListeners(t,i)}delete this.peersToMonitor[e]}}destroy(){this.removeAllPeers(),C.forEach((e=>{this.removeTrackEventListeners(e)})),C=[],this.shouldWrapGetUserMedia&&I&&(navigator.mediaDevices.getUserMedia=I)}monitorPeer(e){let{peerId:t,connectionId:i,pc:n,remote:s}=e;if(!n)return void this.logger.warn("Did not receive pc argument when calling monitorPeer()");const o={pc:n,connectionId:i,stream:null,stats:{parsed:null,raw:null},options:{remote:s}};if(this.peersToMonitor[t]){if(i in this.peersToMonitor[t])return void this.logger.warn(`Already watching connection with ID ${i}`);this.peersToMonitor[t][i]=o}else this.peersToMonitor[t]={[i]:o};this.addPeerConnectionEventListeners(t,i,n),1===this.numberOfMonitoredPeers&&(this.startStatsMonitoring(),this.startConnectionStateMonitoring())}startStatsMonitoring(){this.monitoringSetInterval||(this.monitoringSetInterval=window.setInterval((()=>{this.numberOfMonitoredPeers||this.stopStatsMonitoring(),this.getStats().then((e=>{e.forEach((e=>{this.emitEvent(e)}))}))}),this._getStatsInterval))}stopStatsMonitoring(){this.monitoringSetInterval&&(window.clearInterval(this.monitoringSetInterval),this.monitoringSetInterval=0)}async getStats(e=null){this.logger.info(e?`Getting stats from peer ${e}`:"Getting stats from all peers");let t={};if(e){if(!this.peersToMonitor[e])throw new Error(`Cannot get stats. Peer with id ${e} does not exist`);t[e]=this.peersToMonitor[e]}else t=this.peersToMonitor;let i=[];for(const e in t)for(const n in t[e]){const s=t[e][n],o=s.pc;if(o&&!this.checkIfConnectionIsClosed(e,n,o))try{const t=this.getTimestamp(),r=o.getStats(null);if(r){const o=await r,a=this.getTimestamp(),c=b(o),d={remote:s.options.remote},l=y(o,s.stats.parsed,d),h={event:"stats",tag:"stats",peerId:e,connectionId:n,timeTaken:a-t,data:l};!0===this.rawStats&&(h.rawStats=o),!0===this.statsObject&&(h.statsObject=c),!0===this.filteredStats&&(h.filteredStats=this.filteroutStats(c)),i.push(h),s.stats.parsed=l}else this.logger.error(`PeerConnection from peer ${e} did not return any stats data`)}catch(e){this.logger.error(e)}}return i}startConnectionStateMonitoring(){this.connectionMonitoringSetInterval=window.setInterval((()=>{this.numberOfMonitoredPeers||this.stopConnectionStateMonitoring();for(const e in this.peersToMonitor)for(const t in this.peersToMonitor[e]){const i=this.peersToMonitor[e][t].pc;this.checkIfConnectionIsClosed(e,t,i)}}),this.connectionMonitoringInterval)}checkIfConnectionIsClosed(e,t,i){const n=this.isConnectionClosed(i);if(n){this.removeConnection({pc:i});let n="closed"===i.connectionState?"onconnectionstatechange":"oniceconnectionstatechange";this.emitEvent({event:n,peerId:e,connectionId:t,tag:"connection",data:"closed"})}return n}isConnectionClosed(e){return"closed"===e.connectionState||"closed"===e.iceConnectionState}stopConnectionStateMonitoring(){this.connectionMonitoringSetInterval&&(window.clearInterval(this.connectionMonitoringSetInterval),this.connectionMonitoringSetInterval=0)}wrapGetUserMedia(){if(!navigator.mediaDevices||!navigator.mediaDevices.getUserMedia)return void this.logger.warn("'navigator.mediaDevices.getUserMedia' is not available in browser. Will not wrap getUserMedia.");this.logger.info("Wrapping getUsermedia functions."),I=navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);const e=this.parseGetUserMedia.bind(this);navigator.mediaDevices.getUserMedia=function(){return e({constraints:arguments[0]}),I.apply(navigator.mediaDevices,arguments).then((t=>(e({stream:t}),t)),(t=>(e({error:t}),Promise.reject(t))))}.bind(navigator.mediaDevices)}filteroutStats(e={}){const t={...e};for(const e in t){var i=t[e];this.statsToMonitor.includes(i.type)||delete t[e]}return t}get peerConnectionListeners(){return{icecandidate:(e,t,i,n)=>{this.logger.debug("[pc-event] icecandidate | peerId: ${peerId}",n),this.emitEvent({event:"onicecandidate",tag:"connection",peerId:e,connectionId:t,data:n.candidate})},track:(e,t,i,n)=>{this.logger.debug(`[pc-event] track | peerId: ${e}`,n);const s=n.track,o=n.streams[0];e in this.peersToMonitor&&t in this.peersToMonitor[e]&&(this.peersToMonitor[e][t].stream=o),this.addTrackEventListeners(s,t),this.emitEvent({event:"ontrack",tag:"track",peerId:e,connectionId:t,data:{stream:o?this.getStreamDetails(o):null,track:s?this.getMediaTrackDetails(s):null,title:n.track.kind+":"+n.track.id+" "+n.streams.map((function(e){return"stream:"+e.id}))}})},signalingstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] signalingstatechange | peerId: ${e}`),this.emitEvent({event:"onsignalingstatechange",tag:"connection",peerId:e,connectionId:t,data:{signalingState:i.signalingState,localDescription:i.localDescription,remoteDescription:i.remoteDescription}})},iceconnectionstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] iceconnectionstatechange | peerId: ${e}`),this.emitEvent({event:"oniceconnectionstatechange",tag:"connection",peerId:e,connectionId:t,data:i.iceConnectionState})},icegatheringstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] icegatheringstatechange | peerId: ${e}`),this.emitEvent({event:"onicegatheringstatechange",tag:"connection",peerId:e,connectionId:t,data:i.iceGatheringState})},icecandidateerror:(e,t,i,n)=>{this.logger.debug(`[pc-event] icecandidateerror | peerId: ${e}`),this.emitEvent({event:"onicecandidateerror",tag:"connection",peerId:e,connectionId:t,error:{errorCode:n.errorCode}})},connectionstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] connectionstatechange | peerId: ${e}`),this.emitEvent({event:"onconnectionstatechange",tag:"connection",peerId:e,connectionId:t,data:i.connectionState})},negotiationneeded:(e,t,i)=>{this.logger.debug(`[pc-event] negotiationneeded | peerId: ${e}`),this.emitEvent({event:"onnegotiationneeded",tag:"connection",peerId:e,connectionId:t})},datachannel:(e,t,i,n)=>{this.logger.debug(`[pc-event] datachannel | peerId: ${e}`,n),this.emitEvent({event:"ondatachannel",tag:"datachannel",peerId:e,connectionId:t,data:n.channel})}}}addPeerConnectionEventListeners(e,t,i){this.logger.debug(`Adding event listeners for peer ${e} and connection ${t}.`),E[t]={},Object.keys(this.peerConnectionListeners).forEach((n=>{E[t][n]=this.peerConnectionListeners[n].bind(this,e,t,i),i.addEventListener(n,E[t][n],!1)}))}parseGetUserMedia(e){try{const t={event:"getUserMedia",tag:"getUserMedia",data:{...e}};e.stream&&(t.data.details=this.parseStream(e.stream),e.stream.getTracks().map((e=>{this.addTrackEventListeners(e),C.push(e)}))),this.emitEvent(t)}catch(e){}}parseStream(e){const t={audio:[],video:[]};return e.getTracks().forEach((e=>{t[e.kind].push(this.getMediaTrackDetails(e))})),t}getMediaTrackDetails(e){return{enabled:e.enabled,id:e.id,contentHint:e.contentHint,kind:e.kind,label:e.label,muted:e.muted,readyState:e.readyState,constructorName:e.constructor.name,capabilities:e.getCapabilities?e.getCapabilities():{},constraints:e.getConstraints?e.getConstraints():{},settings:e.getSettings?e.getSettings():{},_track:e}}getStreamDetails(e){return{active:e.active,id:e.id,_stream:e}}getTrackEventObject(e){return{mute:t=>{this.emitEvent({event:"mute",tag:"track",connectionId:e,data:{event:t}})},unmute:t=>{this.emitEvent({event:"unmute",tag:"track",connectionId:e,data:{event:t}})},overconstrained:t=>{this.emitEvent({event:"overconstrained",tag:"track",connectionId:e,data:{event:t}})},ended:t=>{this.emitEvent({event:"ended",tag:"track",connectionId:e,data:{event:t}}),this.removeTrackEventListeners(t.target)}}}addTrackEventListeners(e,t){E[e.id]={};const i=this.getTrackEventObject(t);Object.keys(i).forEach((t=>{E[e.id][t]=i[t].bind(this),e.addEventListener(t,E[e.id][t])})),E[e.id].readyState=setInterval((()=>{if("ended"===e.readyState){let t=new CustomEvent("ended",{detail:{check:"readyState"}});e.dispatchEvent(t)}}),1e3)}removeTrackEventListeners(e){if(e.id in E){const t=this.getTrackEventObject();Object.keys(t).forEach((t=>{e.removeEventListener(t,E[e.id][t])})),clearInterval(E[e.id].readyState),delete E[e.id]}}addToTimeline(e){this.timeline.push(e),this.emit("timeline",e)}emitEvent(e){const t={...e,timestamp:new Date};this.addToTimeline(t),t.tag&&this.emit(t.tag,t)}set getStatsInterval(e){if(!Number.isInteger(e))throw new Error(`getStatsInterval should be an integer, got: ${e}`);this._getStatsInterval=e,this.monitoringSetInterval&&(this.stopStatsMonitoring(),this.startStatsMonitoring())}get numberOfMonitoredPeers(){return Object.keys(this.peersToMonitor).length}removePeerConnectionEventListeners(e,t){e in E&&(Object.keys(this.peerConnectionListeners).forEach((i=>{t.removeEventListener(i,E[e][i],!1)})),delete E[e]),t.getSenders().forEach((e=>{e.track&&this.removeTrackEventListeners(e.track)})),t.getReceivers().forEach((e=>{e.track&&this.removeTrackEventListeners(e.track)}))}getTimestamp(){return Date.now()}wrapGetDisplayMedia(){const e=this;if(navigator.mediaDevices&&navigator.mediaDevices.getDisplayMedia){const t=navigator.mediaDevices.getDisplayMedia.bind(navigator.mediaDevices),i=function(){return e.debug("navigator.mediaDevices.getDisplayMedia",null,arguments[0]),t.apply(navigator.mediaDevices,arguments).then((function(e){return e}),(function(t){return e.debug("navigator.mediaDevices.getDisplayMediaOnFailure",null,t.name),Promise.reject(t)}))};navigator.mediaDevices.getDisplayMedia=i.bind(navigator.mediaDevices)}}}}));(ui=pi)&&ui.__esModule&&Object.prototype.hasOwnProperty.call(ui,"default")&&ui.default;var gi=pi.WebRTCStats;function vi(e){const{packetsLost:t,packetsReceived:i,jitter:n,rtt:s}=e,o=function(e){const{jitter:t,rtt:i}=e,n=t+i/2;return.024*n+.11*(n-177.3)*(n>177.3?1:0)}({rtt:s,jitter:n}),r=function(e){const{packetsLost:t,packetsReceived:i}=e,n=t/(i+t)*100;return 20*Math.log(1+n)}({packetsLost:t,packetsReceived:i}),a=93.2-o-r+0,c=1+.035*a+7e-6*a*(a-60)*(100-a);return Math.min(Math.max(c,1),5)}function mi(e){return isNaN(e)?null:e>4.2?"excellent":e>=4.1&&e<=4.2?"good":e>=3.7&&e<=4?"fair":e>=3.1&&e<=3.6?"poor":"bad"}class fi extends vt{constructor(e,t){super(),this.buildRequest({type:"debug_report_start",debug_report_id:e,debug_report_version:1,call_id:t})}}class _i extends vt{constructor(e,t){super(),this.buildRequest({type:"debug_report_stop",debug_report_id:e,debug_report_version:1,call_id:t})}}class Si extends vt{constructor(e,t){super(),this.buildRequest({type:"debug_report_data",debug_report_id:e,debug_report_version:1,debug_report_data:t})}}function bi(t,n){const s=c();let o=!1;const r=new gi({getStatsInterval:1e3,rawStats:!1,statsObject:!0,filteredStats:!1,remote:!0,debug:!1,logLevel:"warn"}),a=n=>i(this,void 0,void 0,(function*(){"stats"===n.event&&at(e.SwEvent.StatsFrame,function({data:e}){var t,i,n,s,o,r,a,c;const{audio:d,remote:l}=e,{audio:h}=l,u=null!==(i=null===(t=h.inbound[0])||void 0===t?void 0:t.jitter)&&void 0!==i?i:1/0,p=null!==(s=null===(n=h.inbound[0])||void 0===n?void 0:n.roundTripTime)&&void 0!==s?s:1/0,g=null!==(r=null===(o=d.inbound[0])||void 0===o?void 0:o.packetsReceived)&&void 0!==r?r:-1,v=null!==(c=null===(a=d.inbound[0])||void 0===a?void 0:a.packetsLost)&&void 0!==c?c:-1,m=vi({jitter:1e3*u,rtt:1e3*p,packetsLost:v,packetsReceived:g});return{jitter:u,rtt:p,mos:m,quality:mi(m),inboundAudio:d.inbound[0],outboundAudio:d.outbound[0],remoteInboundAudio:h.inbound[0],remoteOutboundAudio:h.outbound[0]}}(n),t.uuid),yield t.execute(new Si(s,n))}));return{get isRunning(){return o},start:(e,c,d)=>i(this,void 0,void 0,(function*(){if(o)Ie.debug(`[${n}] Stats reporter already running, skipping start`);else{yield t.execute(new fi(s,n)),r.on("timeline",a);try{yield r.addConnection({pc:e,peerId:c,connectionId:d}),o=!0}catch(e){Ie.error(`[${n}] Failed to start stats reporter:`,e),r.removeAllPeers(),r.destroy()}}})),stop:a=>i(this,void 0,void 0,(function*(){const i=r.getTimeline();if(at(e.SwEvent.StatsReport,i,t.uuid),"file"===a){!function(e,t){const i=new Blob([JSON.stringify(e)],{type:"application/json"}),n=URL.createObjectURL(i),s=document.createElement("a");s.href=n,s.download=`${t}.json`,s.click(),URL.revokeObjectURL(n)}(i,`webrtc-stats-${s}-${Date.now()}`)}yield t.execute(new _i(s,n)),r.removeAllPeers(),r.destroy(),o=!1})),reportConnectionStateChange:e=>{const t={event:"connectionstatechange-detailed",tag:"connection",timestamp:(new Date).toISOString(),data:e};a(t)},reportIceCandidateError:e=>{const t={event:"icecandidateerror-detailed",tag:"connection",timestamp:(new Date).toISOString(),data:e};a(t)}}}const yi=(t,i)=>{const{contentType:n,canvasType:s,callID:o,canvasInfo:r=null,currentLayerIdx:a=-1}=i;r&&"mcu-personal-canvas"!==s&&delete r.memberID;const c={type:Ve.conferenceUpdate,call:t.calls[o],canvasInfo:Ii(r),currentLayerIdx:a};switch(n){case"layer-info":{const i=Object.assign({action:Qe.LayerInfo},c);at(e.SwEvent.Notification,i,t.uuid);break}case"layout-info":{const i=Object.assign({action:Qe.LayoutInfo},c);at(e.SwEvent.Notification,i,t.uuid);break}}},Ii=e=>{const t=JSON.stringify(e).replace(/memberID/g,"participantId").replace(/ID"/g,'Id"').replace(/POS"/g,'Pos"');return Re(t)},Ei=["new-call-start","new-peer","get-user-media","peer-creation-end","start-negotiation","create-offer","create-answer","set-local-description","ice-gathering-started","first-candidate","first-non-host-candidate","send-sdp","ice-gathering-completed","ringing","telnyx-rtc-media","first-remote-media-track","set-remote-description","telnyx-rtc-answer","ice-connected","dtls-connected","call-active","answer-called"],Ci={"new-call-start":"Call Start","new-peer":"Peer object created","get-user-media":"Media devices acquired","peer-creation-end":"Peer setup complete","start-negotiation":"SDP negotiation started","create-offer":"SDP offer generated","create-answer":"SDP answer generated","set-local-description":"Local description applied","ice-gathering-started":"ICE candidate gathering started","first-candidate":"First ICE candidate found","first-non-host-candidate":"First server-reflexive/relay candidate found","send-sdp":"SDP sent to server","ice-gathering-completed":"All ICE candidates gathered",ringing:"Remote side ringing","telnyx-rtc-media":"Early media received from server","first-remote-media-track":"First remote audio/video track received","set-remote-description":"Remote description applied","telnyx-rtc-answer":"Call answered by remote side","ice-connected":"ICE connection established","dtls-connected":"Secure media channel established (DTLS)","call-active":"Call is active","answer-called":"Answer delay (invite → call.answer)"};function wi(e,t){return`telnyx:call:${e}:${t}`}function Ti(e){try{const t=performance.getEntriesByName(e,"mark");return t.length>0?t[0].startTime:void 0}catch(e){return}}function ki(e){for(const t of Ei)try{performance.clearMarks(wi(e,t))}catch(e){Ie.warn("Clearing performance marks is failed")}}class Ri{constructor(t,n,s,o,r){this.type=t,this.options=n,this.onSdpReadyTwice=null,this.statsReporter=null,this.isIceRestarting=!1,this.iceDone=!1,this._negotiating=!1,this._prevConnectionState=null,this._restartedIceOnConnectionStateFailed=!1,this._sleepWakeupIntervalId=null,this._iceGatheringSafetyTimeout=null,this._gatheredCandidatesCount=0,this._firstMediaTrackMarked=!1,this._timingsCollected=!1,this._iceRestartTimeoutId=null,this._hadOfflineEvent=!1,this._offlineHandler=null,this.handleConnectionStateChange=()=>i(this,void 0,void 0,(function*(){var t,n;const{connectionState:s}=this.instance;if(Ie.info(`[${(new Date).toISOString()}] Connection State changed: ${this._prevConnectionState} -> ${s}`),"failed"!==s&&"disconnected"!==s||(this.isDebugEnabled&&this.statsReporter&&function(e,t){return i(this,void 0,void 0,(function*(){const i={connectionState:e.connectionState,previousConnectionState:t,iceConnectionState:e.iceConnectionState,iceGatheringState:e.iceGatheringState,signalingState:e.signalingState},n=e.getTransceivers();if(n.length>0){const e=n[0].sender,t=null==e?void 0:e.transport;t&&(i.dtlsState=t.state)}e.sctp&&(i.sctpState=e.sctp.state);try{const t=yield e.getStats();t.forEach((e=>{"candidate-pair"===e.type&&"succeeded"===e.state&&(i.candidatePairState=e.state,t.forEach((t=>{"local-candidate"===t.type&&t.id===e.localCandidateId&&(i.localCandidateType=t.candidateType,i.selectedCandidatePair=i.selectedCandidatePair||{local:{},remote:{}},i.selectedCandidatePair.local={address:t.address,port:t.port,protocol:t.protocol,candidateType:t.candidateType}),"remote-candidate"===t.type&&t.id===e.remoteCandidateId&&(i.remoteCandidateType=t.candidateType,i.selectedCandidatePair=i.selectedCandidatePair||{local:{},remote:{}},i.selectedCandidatePair.remote={address:t.address,port:t.port,protocol:t.protocol,candidateType:t.candidateType})}))),"transport"===e.type&&(i.dtlsCipher=e.dtlsCipher,i.srtpCipher=e.srtpCipher,i.tlsVersion=e.tlsVersion,e.dtlsState&&(i.dtlsState=e.dtlsState))}))}catch(e){Ie.error("Error gathering connection state details:",e)}return i}))}(this.instance,this._prevConnectionState).then((e=>{this.statsReporter.reportConnectionStateChange(e)})),null===(n=(t=this._session).reportPeerFailure)||void 0===n||n.call(t,this.options.id,"connection_failed")),"disconnected"===s){const t=le(j);at(e.SwEvent.Warning,{warning:t,callId:this.options.id,sessionId:this._session.sessionid},this.options.id)}if("failed"===s){const t=le(B);at(e.SwEvent.PeerConnectionFailureError,{warning:t,error:new Error(`Peer Connection failed. previous state: ${this._prevConnectionState}, current state: ${s}`),sessionId:this._session.sessionid},this.options.id)}this._prevConnectionState=s,"connected"===s&&(performance.mark(wi(this.options.id,"dtls-connected")),this.tryCollectTimings(),this._restartedIceOnConnectionStateFailed=!1,this._hadOfflineEvent=!1),this._isTrickleIce()&&("connecting"===s&&performance.mark(wi(this.options.id,"peer-connection-connecting")),"connected"===s&&(this._clearIceGatheringSafetyTimeout(),performance.mark(wi(this.options.id,"peer-connection-connected"))))})),this._handleIceConnectionStateChange=()=>{var e,t;const i=this.instance.iceConnectionState;Ie.debug(`[${(new Date).toISOString()}] ICE Connection State`,i),"connected"===i&&performance.mark(wi(this.options.id,"ice-connected")),"failed"===i&&(null===(t=(e=this._session).reportPeerFailure)||void 0===t||t.call(e,this.options.id,"ice_failed"))},this._handleIceGatheringStateChange=()=>{const e=this.instance.iceGatheringState;Ie.debug(`[${(new Date).toISOString()}] ICE Gathering State`,e),"gathering"===e?(this._gatheredCandidatesCount=0,this._startIceGatheringSafetyTimeout()):"complete"===e&&this._clearIceGatheringSafetyTimeout()},this._setCodecs=(e,t)=>{if(e.setCodecPreferences)return e.setCodecPreferences(t)},Ie.debug("New Peer with type:",this.type,"Options:",this.options),this._constraints={offerToReceiveAudio:!0,offerToReceiveVideo:!!n.video},this._sdpReady=this._sdpReady.bind(this),this.handleSignalingStateChangeEvent=this.handleSignalingStateChangeEvent.bind(this),this.handleNegotiationNeededEvent=this.handleNegotiationNeededEvent.bind(this),this.handleTrackEvent=this.handleTrackEvent.bind(this),this.createPeerConnection=this.createPeerConnection.bind(this),this._session=s,this._trickleIceSdpFn=o,this._registerPeerEvents=r,"undefined"!=typeof window&&(this._offlineHandler=()=>{this._hadOfflineEvent=!0},window.addEventListener("offline",this._offlineHandler))}finishIceRestart(){this.isIceRestarting&&(this.isIceRestarting=!1,this._iceRestartTimeoutId&&(clearTimeout(this._iceRestartTimeoutId),this._iceRestartTimeoutId=null))}restartIce(){return this.instance?this.isIceRestarting?(Ie.debug("ICE restart: already in progress, skipping"),!1):this._hadOfflineEvent?(Ie.debug("ICE restart: offline event detected, deferring to Attach"),!1):this._session.connected?(this.isIceRestarting=!0,this._restartedIceOnConnectionStateFailed=!0,this.instance.restartIce(),this.iceDone=!1,this._iceRestartTimeoutId=setTimeout((()=>{this.isIceRestarting&&(Ie.warn("ICE restart: Modify exchange timed out, clearing isIceRestarting flag"),this.isIceRestarting=!1),this._iceRestartTimeoutId=null}),Ri.ICE_RESTART_TIMEOUT_MS),Ie.info("ICE restart: initiated by signaling health monitor"),!0):(Ie.debug("ICE restart: session not connected, skipping"),!1):(Ie.warn("ICE restart: no RTCPeerConnection instance"),!1)}get isOffer(){return this.type===Ge.Offer}get isAnswer(){return this.type===Ge.Answer}get isDebugEnabled(){return this.options.debug||this._session.options.debug}get debugOutput(){return this.options.debugOutput||this._session.options.debugOutput}get restartedIceOnConnectionStateFailed(){return this._restartedIceOnConnectionStateFailed}isConnectionHealthy(){return"connected"===this.instance.connectionState&&"connected"===this.instance.iceConnectionState&&"closed"!==this.instance.signalingState}startNegotiation(){performance.mark(wi(this.options.id,"start-negotiation")),this._negotiating=!0,this._isOffer()||this.isIceRestarting?this._createOffer():this._createAnswer()}startTrickleIceNegotiation(){return i(this,void 0,void 0,(function*(){performance.mark(wi(this.options.id,"start-negotiation")),this._negotiating=!0,this._isOffer()||this.isIceRestarting?yield this._createOffer().then(this._trickleIceSdpFn.bind(this)):yield this._createAnswer().then(this._trickleIceSdpFn.bind(this))}))}_logTransceivers(){this.instance?(Ie.info("Number of transceivers:",this.instance.getTransceivers().length),this.instance.getTransceivers().forEach(((e,t)=>{Ie.info(`>> Transceiver [${t}]:`,e.mid,e.direction,e.stopped),Ie.info(`>> Sender Params [${t}]:`,JSON.stringify(e.sender.getParameters(),null,2))}))):Ie.warn("Cannot log transceivers: peer connection is null")}handleSignalingStateChangeEvent(){switch(Ie.info("signalingState:",this.instance.signalingState),this.instance.signalingState){case"stable":this._negotiating=!1;break;case"closed":at(e.SwEvent.PeerConnectionSignalingStateClosed,{sessionId:this._session.sessionid},this.options.id),this.instance&&(Ie.debug(`[${this.options.id}] Closing peer due to signalingState closed`),this.close());break;default:this._negotiating=!0}}handleNegotiationNeededEvent(){Ie.info("Negotiation needed event"),"stable"!==this.instance.signalingState||this._negotiating?Ie.debug("Skipping negotiation, state:",this.instance.signalingState,"negotiating:",this._negotiating):this._isTrickleIce()&&!this.isIceRestarting?this.startTrickleIceNegotiation():this.startNegotiation()}handleTrackEvent(e){this._firstMediaTrackMarked||(performance.mark(wi(this.options.id,"first-remote-media-track")),this._firstMediaTrackMarked=!0);const{streams:[t]}=e,{remoteElement:i,screenShare:n}=this.options;this.options.remoteStream=t,!1===n&&jt(i,this.options.remoteStream)}tryCollectTimings(){if(this._timingsCollected)return;if(!(performance.getEntriesByName(wi(this.options.id,"call-active"),"mark").length>0)||"connected"!==this.instance.connectionState)return;this._timingsCollected=!0;const e=this._isTrickleIce()?"trickle":"non-trickle",t=this.isOffer?"outbound":"inbound",i=function(e,t,i){const n=Ti(wi(e,"new-call-start"));if(void 0===n)return{mode:t,direction:i,steps:[]};const s=[];for(const t of Ei){if("new-call-start"===t)continue;const i=Ti(wi(e,t));void 0!==i&&s.push({label:Ci[t]||t,fromStart:i-n})}s.sort(((e,t)=>e.fromStart-t.fromStart));const o=[];let r=0;for(const e of s)o.push({label:e.label,fromStart:e.fromStart,delta:e.fromStart-r}),r=e.fromStart;return{mode:t,direction:i,steps:o}}(this.options.id,e,t);!function(e){const{mode:t,direction:i,steps:n}=e,s=`[CallTimings][${i}][${t}]`;if(0===n.length)return void Ie.info(`${s} No timing data collected`);const o=Math.max(...n.map((e=>e.label.length)),4)+2,r=(e,t)=>{for(;e.length<t;)e+=" ";return e},a=(e,t)=>{for(;e.length<t;)e=" "+e;return e},c=r("Step",o)+a("Delta",14)+a("From Start",14);let d="";for(let e=0;e<c.length;e++)d+="-";Ie.info(`${s} Call establishment timing breakdown:`),Ie.info(`${s} ${c}`),Ie.info(`${s} ${d}`),Ie.info(`${s} ${r("Call Start",o)}${a("-",14)}${a("0.00ms",14)}`);for(const e of n){const t=e.delta.toFixed(2)+"ms",i=e.fromStart.toFixed(2)+"ms";Ie.info(`${s} ${r(e.label,o)}${a(t,14)}${a(i,14)}`)}Ie.info(`${s} ${d}`)}(i),ki(this.options.id)}createPeerConnection(){return i(this,void 0,void 0,(function*(){var t;if(this.instance=(t=this._config(),new window.RTCPeerConnection(t)),this.instance.onsignalingstatechange=this.handleSignalingStateChangeEvent,this.instance.onnegotiationneeded=this.handleNegotiationNeededEvent,this.instance.ontrack=this.handleTrackEvent,this.instance.addEventListener("connectionstatechange",this.handleConnectionStateChange),this.instance.addEventListener("iceconnectionstatechange",this._handleIceConnectionStateChange),this.instance.addEventListener("icegatheringstatechange",this._handleIceGatheringStateChange),this.instance.addEventListener("addstream",(e=>{this.options.remoteStream=e.stream})),this._registerPeerEvents(this.instance),this._prevConnectionState=this.instance.connectionState,this.isAnswer&&(yield this._setRemoteDescription({sdp:this.options.remoteSdp,type:Ge.Offer}),performance.mark(wi(this.options.id,"set-remote-description")),!this.instance))throw pe(E);const n=Boolean(this.options.receiveOnlyAudio)&&!this.options.audio;let s=null;if(this.options.localStream=yield this._retrieveLocalStream().catch((t=>i(this,void 0,void 0,(function*(){const n=this._session.options.mediaPermissionsRecovery;if((null==n?void 0:n.enabled)&&this._isAnswer()){let o=null,r=null;return yield new Promise(((i,s)=>{r=setTimeout((()=>s(new Error("Media recovery flow timed out!"))),n.timeout),at(e.SwEvent.Error,{error:pe(ue(t),t),callId:this.options.id,sessionId:this._session.sessionid,recoverable:!0,retryDeadline:Date.now()+n.timeout,resume:()=>{i()},reject:()=>{s(new Error("Call was rejected during media recovery flow!"))}},this._session.uuid)})).then((()=>i(this,void 0,void 0,(function*(){var e;r&&(clearTimeout(r),r=null),o=yield this._retrieveLocalStream(),null===(e=n.onSuccess)||void 0===e||e.call(n)})))).catch((e=>{var t;r&&(clearTimeout(r),r=null),s=e,null===(t=n.onError)||void 0===t||t.call(n,e)})),o}return s=t,null})))),!this.instance)throw pe(E);if(!this.options.localStream&&!n){throw pe(s?ue(s):_,null!=s?s:void 0)}performance.mark(wi(this.options.id,"get-user-media")),this.options.mutedMicOnStart&&Ht(this.options.localStream)&&(Ie.info("Muting local audio tracks on start"),ti(this.options.localStream)),performance.mark(wi(this.options.id,"peer-creation-end"))}))}incrementGatheredCandidates(){this._gatheredCandidatesCount++}_startIceGatheringSafetyTimeout(){this._clearIceGatheringSafetyTimeout(),this._iceGatheringSafetyTimeout=setTimeout((()=>{if(this.instance)if(0===this._gatheredCandidatesCount){const t=le(W);at(e.SwEvent.Warning,{warning:t,callId:this.options.id,sessionId:this._session.sessionid},this.options.id)}else if("complete"!==this.instance.iceGatheringState){const t=le(G);at(e.SwEvent.Warning,{warning:t,callId:this.options.id,sessionId:this._session.sessionid},this.options.id)}}),Ri.ICE_GATHERING_SAFETY_TIMEOUT_MS)}_clearIceGatheringSafetyTimeout(){null!==this._iceGatheringSafetyTimeout&&(clearTimeout(this._iceGatheringSafetyTimeout),this._iceGatheringSafetyTimeout=null)}init(){var e;return i(this,void 0,void 0,(function*(){if(yield this.createPeerConnection(),!this.instance)throw pe(E);this.isDebugEnabled&&(this.statsReporter=bi(this._session,this.options.id),yield null===(e=this.statsReporter)||void 0===e?void 0:e.start(this.instance,this._session.sessionid,this._session.sessionid));const{localElement:t,localStream:i=null,screenShare:n=!1}=this.options;if(Ht(i)){const e=i.getAudioTracks();let s=[...e];if(Ie.info("Local audio tracks: ",e),"object"==typeof this.options.audio&&e.forEach((e=>{Ie.info("Local audio tracks constraints: ",e.getConstraints())})),this.options.video){const t=i.getVideoTracks();s=[...e,...t],Ie.info("Local video tracks: ",t),"object"==typeof this.options.video&&t.forEach((e=>{Ie.info("Local video tracks constraints: ",e.getConstraints())}))}const{audioCodecs:o,videoCodecs:r}=di(this.options.preferred_codecs);if(this.isOffer&&"function"==typeof this.instance.addTransceiver){const e={direction:"sendrecv",streams:[i]};s.forEach((t=>{"audio"===t.kind&&(this.options.userVariables.microphoneLabel=t.label),"video"===t.kind&&(this.options.userVariables.cameraLabel=t.label);const i=this.instance.addTransceiver(t,e);"audio"===t.kind&&o.length>0&&this._setCodecs(i,o),"video"===t.kind&&r.length>0&&this._setCodecs(i,r)}))}else"function"==typeof this.instance.addTrack?(s.forEach((e=>{"audio"===e.kind&&(this.options.userVariables.microphoneLabel=e.label),"video"===e.kind&&(this.options.userVariables.cameraLabel=e.label),this.instance.addTrack(e,i)})),this.instance.getTransceivers().forEach((e=>{"audio"===e.receiver.track.kind&&o.length>0&&this._setCodecs(e,o),"video"===e.receiver.track.kind&&r.length>0&&this._setCodecs(e,r)}))):this.instance.addStream(i);!1===n&&jt(t,i)}else if(this.options.receiveOnlyAudio&&"function"==typeof this.instance.addTransceiver){const e=this.instance.addTransceiver("audio",{direction:"recvonly"});Ie.info("Added recvonly audio transceiver for receive-only mode",e);const{audioCodecs:t}=di(this.options.preferred_codecs);t.length>0&&this._setCodecs(e,t)}this.isOffer?(this.options.negotiateAudio&&this._checkMediaToNegotiate("audio"),this.options.negotiateVideo&&this._checkMediaToNegotiate("video")):this._isTrickleIce()||this.startNegotiation(),this._isTrickleIce()&&this.startTrickleIceNegotiation(),this._logTransceivers()}))}_getSenderByKind(e){return this.instance.getSenders().find((({track:t})=>t&&t.kind===e))}_checkMediaToNegotiate(e){if(!this._getSenderByKind(e)){const t=this.instance.addTransceiver(e);Ie.info("Add transceiver",e,t)}}_createOffer(){return i(this,void 0,void 0,(function*(){if(this._isOffer()||this.isIceRestarting){this._constraints.offerToReceiveAudio=!1!==this.options.audio||Boolean(this.options.receiveOnlyAudio),this._constraints.offerToReceiveVideo=Boolean(this.options.video),Ie.info("_createOffer - this._constraints",this._constraints);try{const e=yield this.instance.createOffer(this._constraints);return performance.mark(wi(this.options.id,"create-offer")),yield this._setLocalDescription(e),performance.mark(wi(this.options.id,"set-local-description")),performance.mark(wi(this.options.id,"ice-gathering-started")),this._isTrickleIce()||this._sdpReady(),e}catch(t){Ie.error("Peer _createOffer error:",t);const i=pe(h,t);at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id)}}}))}_setRemoteDescription(t){return i(this,void 0,void 0,(function*(){Ie.debug("Setting remote description",t);try{yield this.instance.setRemoteDescription(t)}catch(t){Ie.error("Peer _setRemoteDescription error:",t);const i=pe(g,t);throw at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id),t}}))}_createAnswer(){return i(this,void 0,void 0,(function*(){if(this._isAnswer()){if("stable"!==this.instance.signalingState&&"have-remote-offer"!==this.instance.signalingState)return Ie.debug("Skipping negotiation, state:",this.instance.signalingState),Ie.debug(" - But the signaling state isn't stable, so triggering rollback"),void(yield Promise.all([this.instance.setLocalDescription({type:"rollback"}),this.instance.setRemoteDescription({sdp:this.options.remoteSdp,type:Ge.Offer})]));this._logTransceivers();try{const e=yield this.instance.createAnswer();return performance.mark(wi(this.options.id,"create-answer")),yield this._setLocalDescription(e),performance.mark(wi(this.options.id,"set-local-description")),performance.mark(wi(this.options.id,"ice-gathering-started")),e}catch(t){Ie.error("Peer _createAnswer error:",t);const i=pe(u,t);at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id)}}}))}_setLocalDescription(t){return i(this,void 0,void 0,(function*(){try{yield this.instance.setLocalDescription(t)}catch(t){Ie.error("Peer _setLocalDescription error:",t);const i=pe(p,t);throw at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id),t}}))}_sdpReady(){Ae(this.onSdpReadyTwice)&&this.onSdpReadyTwice(this.instance.localDescription)}_retrieveLocalStream(){return i(this,void 0,void 0,(function*(){if(Ht(this.options.localStream))return this.options.localStream;const e=yield(t=this.options,i(void 0,void 0,void 0,(function*(){let{audio:e=!0,micId:i,video:n=!1,camId:s}=t;const{micLabel:o="",camLabel:r=""}=t;return i&&(i=yield Jt(i,o,Xe.AudioIn).catch((e=>null)),i&&("boolean"==typeof e&&(e={}),e.deviceId={exact:i})),s&&(s=yield Jt(s,r,Xe.Video).catch((e=>null)),s&&("boolean"==typeof n&&(n={}),n.deviceId={exact:s})),{audio:e,video:n}})));var t;return qt(e)}))}_isOffer(){return this.type===Ge.Offer}_isAnswer(){return this.type===Ge.Answer}_isTrickleIce(){return!0===this.options.trickleIce}_config(){const{prefetchIceCandidates:e,forceRelayCandidate:t,iceServers:i}=this.options,n={bundlePolicy:"balanced",iceCandidatePoolSize:e?10:0,iceServers:i,iceTransportPolicy:t?"relay":"all"};return Ie.info("RTC config",n),n}restartStatsReporter(){return i(this,void 0,void 0,(function*(){this.isDebugEnabled&&this.statsReporter&&(this.instance?this.statsReporter.isRunning?Ie.debug(`[${this.options.id}] Stats reporter already running, skipping restart`):(Ie.debug(`[${this.options.id}] Restarting stats reporter after reconnect`),yield this.statsReporter.start(this.instance,this._session.sessionid,this._session.sessionid)):Ie.debug(`[${this.options.id}] Cannot restart stats reporter - no peer connection instance`))}))}close(){return i(this,void 0,void 0,(function*(){ki(this.options.id),this.finishIceRestart(),this._clearIceGatheringSafetyTimeout(),this._offlineHandler&&"undefined"!=typeof window&&(window.removeEventListener("offline",this._offlineHandler),this._offlineHandler=null),null!==this._sleepWakeupIntervalId&&(clearInterval(this._sleepWakeupIntervalId),this._sleepWakeupIntervalId=null),this.isDebugEnabled&&this.statsReporter&&(yield this.statsReporter.stop(this.debugOutput)),this.instance&&(this.instance.close(),this.instance=null)}))}}Ri.ICE_GATHERING_SAFETY_TIMEOUT_MS=15e3,Ri.ICE_RESTART_TIMEOUT_MS=15e3;const Ai=Et;class Oi{constructor(e,t){this.session=e,this._callReportCollector=null,this.id="",this.recoveredCallId="",this.state=Ke[Ke.New],this.prevState="",this.channels=[],this.role=Je.Participant,this.extension=null,this._state=Ke.New,this._prevState=Ke.New,this.gotAnswer=!1,this.gotEarly=!1,this._lastSerno=0,this._targetNodeId=null,this._iceTimeout=null,this._statsBindings=[],this._statsIntervalId=null,this._pendingIceCandidates=[],this._isRemoteDescriptionSet=!1,this._signalingStateClosed=!1,this._creatingPeer=!1,this._firstCandidateSent=!1,this._firstNonHostCandidateSent=!1,this._isRecovering=!1,this._checkConferenceSerno=e=>{const t=e<0||!this._lastSerno||this._lastSerno&&e===this._lastSerno+1;return t&&e>=0&&(this._lastSerno=e),t},this._doStats=()=>{this.peer&&this.peer.instance&&0!==this._statsBindings.length&&this.peer.instance.getStats().then((e=>{e.forEach((e=>{this._statsBindings.forEach((t=>{if(t.callback){if(t.constraints)for(const i in t.constraints)if(t.constraints.hasOwnProperty(i)&&t.constraints[i]!==e[i])return;t.callback(e)}}))}))}))};const{iceServers:i,speaker:n,micId:s,micLabel:o,camId:r,camLabel:a,localElement:c,remoteElement:d,options:l,mediaConstraints:{audio:h,video:u},ringtoneFile:p,ringbackFile:g}=e;this.options=Object.assign({},Ye,{audio:h,video:u,iceServers:(null==t?void 0:t.iceServers)&&Array.isArray(t.iceServers)?t.iceServers:i,localElement:c,remoteElement:d,micId:s,micLabel:o,camId:r,camLabel:a,speakerId:n,ringtoneFile:p,ringbackFile:g,debug:l.debug,debugOutput:l.debugOutput,trickleIce:l.trickleIce,prefetchIceCandidates:l.prefetchIceCandidates,forceRelayCandidate:l.forceRelayCandidate,keepConnectionAliveOnSocketClose:l.keepConnectionAliveOnSocketClose,mutedMicOnStart:l.mutedMicOnStart},t),this._onMediaError=this._onMediaError.bind(this),this._onPeerConnectionFailureError=this._onPeerConnectionFailureError.bind(this),this._onPeerConnectionSignalingStateClosed=this._onPeerConnectionSignalingStateClosed.bind(this),this._onTrickleIceSdp=this._onTrickleIceSdp.bind(this),this._registerPeerEvents=this._registerPeerEvents.bind(this),this._init(),this.options&&(this._ringtone=ri(this.options.ringtoneFile,"_ringtone"),this._ringback=ri(this.options.ringbackFile,"_ringback"))}get creatingPeer(){return this._creatingPeer}get signalingStateClosed(){return this._signalingStateClosed}_captureHangupCallerStack(){const e=new Error("Call.hangup caller").stack;return e?e.split("\n").map((e=>e.trim())).filter(Boolean).slice(1,11):[]}get nodeId(){return this._targetNodeId}set nodeId(e){this._targetNodeId=e}get isVideoCall(){return!!this.options.video}get telnyxIDs(){return{telnyxCallControlId:this.options.telnyxCallControlId,telnyxSessionId:this.options.telnyxSessionId,telnyxLegId:this.options.telnyxLegId}}get localStream(){return this.options.localStream}get remoteStream(){return this.options.remoteStream}get memberChannel(){return`conference-member.${this.id}`}get isAudioMuted(){return!ni(this.options.localStream)}_hasActiveUnmutedLocalAudioTrack(){const e=this.options.localStream;return!!(null==e?void 0:e.getAudioTracks)&&e.getAudioTracks().some((e=>!0===e.enabled&&!0!==e.muted&&"live"===e.readyState))}shouldForceRelayCandidateForRecovery(){var e,t;return!this.options.forceRelayCandidate&&(!!this.recoveredCallId&&(null!==(t=null===(e=this._callReportCollector)||void 0===e?void 0:e.shouldForceRelayCandidateForRecovery())&&void 0!==t&&t))}invite(){return i(this,void 0,void 0,(function*(){this._creatingPeer=!0,this.direction=We.Outbound,this.options.trickleIce&&this._resetTrickleIceCandidateState(),performance.mark(wi(this.id,"new-peer")),this.peer=new Ri(Ge.Offer,this.options,this.session,this._onTrickleIceSdp,this._registerPeerEvents);try{yield this.peer.init()}catch(t){Ie.error("Peer init failed, aborting call",t),this._creatingPeer=!1;const i=t instanceof he?t:pe(D,t instanceof Error?t:void 0);return at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid,recoverable:!1},this.session.uuid),void this.hangup({initiator:"sdk:peer-init-failed"},!1)}this._creatingPeer=!1}))}answer(t={}){var n,s,o,r,a;return i(this,void 0,void 0,(function*(){if(this._creatingPeer||(null===(n=this.peer)||void 0===n?void 0:n.instance)&&"closed"!==this.peer.instance.signalingState){const t=le(q);return at(e.SwEvent.Warning,{warning:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),void Ie.warn(`[${this.id}] answer() ignored: peer connection already exists or is being created (signalingState: ${null!==(r=null===(o=null===(s=this.peer)||void 0===s?void 0:s.instance)||void 0===o?void 0:o.signalingState)&&void 0!==r?r:"creating"})`)}if(this._registerInboundAnswerAttempt()){performance.mark(wi(this.id,"answer-called")),this._creatingPeer=!0,this.stopRingtone(),this.direction=We.Inbound,(null===(a=null==t?void 0:t.customHeaders)||void 0===a?void 0:a.length)>0&&(this.options=Object.assign(Object.assign({},this.options),{customHeaders:t.customHeaders})),this.options.trickleIce&&this._resetTrickleIceCandidateState(),performance.mark(wi(this.id,"new-peer")),this.peer=new Ri(Ge.Answer,this.options,this.session,this._onTrickleIceSdp,this._registerPeerEvents);try{yield this.peer.init()}catch(t){Ie.error("Peer init failed, aborting call",t),this._creatingPeer=!1;const i=t instanceof he?t:pe(D,t instanceof Error?t:void 0);return at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid,recoverable:!1},this.session.uuid),void(yield this.hangup({initiator:"sdk:peer-init-failed"},!0))}this._creatingPeer=!1}}))}playRingtone(){ai(this._ringtone)}stopRingtone(){ci(this._ringtone)}playRingback(){ai(this._ringback)}stopRingback(){ci(this._ringback)}hangup(t,n){var s,o,r,a,c,d;return i(this,void 0,void 0,(function*(){const i=t||{},l=!1!==n,h=this.state,u=this.prevState,p=this._captureHangupCallerStack(),g=i.initiator||"app:call.hangup",v=this._state<Ke.Active?{cause:"USER_BUSY",causeCode:17}:{cause:"NORMAL_CLEARING",causeCode:16};if(this.cause=i.cause||v.cause,this.causeCode=i.causeCode||v.causeCode,this.sipCode=i.sipCode||null,this.sipReason=i.sipReason||null,this.sipCallId=i.sip_call_id||null,this.options.customHeaders=[...null!==(s=this.options.customHeaders)&&void 0!==s?s:[],...null!==(r=null===(o=null==i?void 0:i.dialogParams)||void 0===o?void 0:o.customHeaders)&&void 0!==r?r:[]],Ie.debug(`[${this.id}] hangup() invoked`,{callId:this.id,execute:l,state:h,prevState:u,cause:this.cause,causeCode:this.causeCode,initiator:g,sipCode:this.sipCode,sipReason:this.sipReason,sipCallId:this.sipCallId,isRecovering:Boolean(i.isRecovering),hasDialogCustomHeaders:Boolean(null===(c=null===(a=i.dialogParams)||void 0===a?void 0:a.customHeaders)||void 0===c?void 0:c.length),callerStack:p}),i.isRecovering)return this._isRecovering=!0,this.setState(Ke.Recovering),void this._finalize();if(this.setState(Ke.Hangup),this.stopRingtone(),this.stopRingback(),l){const t=new At({sipCode:this.sipCode,sip_call_id:this.sipCallId,sessid:this.session.sessionid,dialogParams:this.options,cause:this.cause,causeCode:this.causeCode});try{yield this._execute(t)}catch(t){Ie.error("telnyx_rtc.bye failed!",t);const i=pe(y,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}}Ie.debug(`[${this.id}] Closing peer from hangup`),null===(d=this.peer)||void 0===d||d.close(),this.setState(Ke.Destroy)}))}hold(){const e=new Lt({sessid:this.session.sessionid,action:Ze.Hold,dialogParams:this.options});return this._execute(e).then(this._handleChangeHoldStateSuccess.bind(this)).catch(this._handleChangeHoldStateError.bind(this))}unhold(){const e=new Lt({sessid:this.session.sessionid,action:Ze.Unhold,dialogParams:this.options});return this._execute(e).then(this._handleChangeHoldStateSuccess.bind(this)).catch(this._handleChangeHoldStateError.bind(this))}toggleHold(){const e=new Lt({sessid:this.session.sessionid,action:Ze.ToggleHold,dialogParams:this.options});return this._execute(e).then(this._handleChangeHoldStateSuccess.bind(this)).catch(this._handleChangeHoldStateError.bind(this))}dtmf(e){const t=new Dt({sessid:this.session.sessionid,dtmf:e,dialogParams:this.options});this._execute(t)}message(e,t){const i={from:this.session.options.login,to:e,body:t},n=new Dt({sessid:this.session.sessionid,msg:i,dialogParams:this.options});this._execute(n)}muteAudio(){ti(this.options.localStream)}unmuteAudio(){ei(this.options.localStream)}toggleAudioMute(){ii(this.options.localStream)}setAudioInDevice(t,n=this.options.mutedMicOnStart){var s;return i(this,void 0,void 0,(function*(){const{instance:i}=this.peer,o=i.getSenders().find((({track:{kind:e}})=>"audio"===e));if(o){let i;try{i=yield $t({audio:{deviceId:{exact:t}}})}catch(t){const i=pe(ue(t),t);return void at(e.SwEvent.MediaError,i,(null===(s=this.options)||void 0===s?void 0:s.id)||this.id)}const r=i.getAudioTracks()[0];r.enabled=!n,o.replaceTrack(r),this.options.micId=t;const{localStream:a}=this.options;a.getAudioTracks().forEach((e=>e.stop())),a.getVideoTracks().forEach((e=>i.addTrack(e))),this.options.localStream=i}}))}muteVideo(){var e;e=this.options.localStream,Zt(e,"video",!1)}unmuteVideo(){var e;e=this.options.localStream,Zt(e,"video",!0)}toggleVideoMute(){var e;e=this.options.localStream,Zt(e,"video",null)}setVideoDevice(e){return i(this,void 0,void 0,(function*(){const{instance:t}=this.peer,i=t.getSenders().find((({track:{kind:e}})=>"video"===e));if(i){const t=yield $t({video:{deviceId:{exact:e}}}),n=t.getVideoTracks()[0];i.replaceTrack(n);const{localElement:s,localStream:o}=this.options;jt(s,t),this.options.camId=e,o.getAudioTracks().forEach((e=>t.addTrack(e))),o.getVideoTracks().forEach((e=>e.stop())),this.options.localStream=t}}))}deaf(){ti(this.options.remoteStream)}undeaf(){ei(this.options.remoteStream)}toggleDeaf(){ii(this.options.remoteStream)}setBandwidthEncodingsMaxBps(e,t){return i(this,void 0,void 0,(function*(){if(!this||!this.peer)return void Ie.error("Could not set bandwidth (reason: no peer connection). Dynamic bandwidth can only be set when there is a call running - is there any call running?)");const{instance:i}=this.peer,n=i.getSenders();if(!n)return void Ie.error("Could not set bandwidth (reason: no senders). Dynamic bandwidth can only be set when there is a call running - is there any call running?)");const s=n.find((({track:{kind:e}})=>e===t));if(s){const i=s.getParameters();i.encodings||(i.encodings=[{rid:"h"}]),Ie.info("Parameters: ",i),Ie.info("Setting max ","audio"===t?"audio":"video"," bandwidth to: ",e," [bps]"),i.encodings[0].maxBitrate=e,yield s.setParameters(i).then((()=>{Ie.info("audio"===t?"New audio":"New video"," bandwidth settings in use: ",s.getParameters())})).catch((e=>Ie.error(e)))}else Ie.error("Could not set bandwidth (reason: no "+t+" sender). Dynamic bandwidth can only be set when there is a call running - is there any call running?)")}))}setAudioBandwidthEncodingsMaxBps(e){this.setBandwidthEncodingsMaxBps(e,"audio")}setVideoBandwidthEncodingsMaxBps(e){this.setBandwidthEncodingsMaxBps(e,"video")}_isTerminatingOrTerminated(){return[Ke.Hangup,Ke.Destroy,Ke.Purge].includes(this._state)}getStats(e,t){if(!e)return;const i={callback:e,constraints:t};if(this._statsBindings.push(i),!this._statsIntervalId){const e=2e3;this._startStats(e)}}setState(e){var t,i;switch(this._prevState=this._state,this._state=e,this.state=Ke[this._state].toLowerCase(),this.prevState=Ke[this._prevState].toLowerCase(),Ie.debug(`Call ${this.id} state change from ${this.prevState} to ${this.state}`),this._dispatchNotification({type:Ve.callUpdate,call:this}),e){case Ke.Purge:Ie.info(`[${this.id}] Entering Purge state.`);break;case Ke.Active:performance.mark(wi(this.id,"call-active")),null===(t=this.peer)||void 0===t||t.tryCollectTimings(),this._isRecovering&&(this._isRecovering=!1,Ie.debug(`[${this.id}] Recovery complete, call is active`)),this.session.startSignalingHealthMonitor(),setTimeout((()=>{const{remoteElement:e,speakerId:t}=this.options;e&&t&&Wt(e,t)}),0),this._callReportCollector&&(null===(i=this.peer)||void 0===i?void 0:i.instance)&&this.session.callReportId&&this._callReportCollector.start(this.peer.instance);break;case Ke.Destroy:this._finalize()}}handleMessage(t){const{method:i,params:n}=t;switch(i){case Be.Answer:if(performance.mark(wi(this.id,"telnyx-rtc-answer")),this.gotAnswer=!0,n.telnyx_call_control_id&&(this.options.telnyxCallControlId=n.telnyx_call_control_id),n.telnyx_session_id&&(this.options.telnyxSessionId=n.telnyx_session_id),n.telnyx_leg_id&&(this.options.telnyxLegId=n.telnyx_leg_id),this._state>=Ke.Active)return;this._state>=Ke.Early&&this.setState(Ke.Active),this.gotEarly||this._onRemoteSdp(n.sdp),this.stopRingback(),this.stopRingtone();break;case Be.Media:if(performance.mark(wi(this.id,"telnyx-rtc-media")),this._state>=Ke.Early)return;this.gotEarly=!0,this._onRemoteSdp(n.sdp);break;case Be.Display:{const{display_name:t,display_number:s,display_direction:o}=n;this.extension=s;const r=o===We.Inbound?We.Outbound:We.Inbound,a={type:Ve[i],call:this,displayName:t,displayNumber:s,displayDirection:r};at(e.SwEvent.Notification,a,this.id)||at(e.SwEvent.Notification,a,this.session.uuid);break}case Be.Candidate:this._addIceCandidate(n);break;case Be.Info:case Be.Event:{const t=Object.assign(Object.assign({},n),{type:Ve.generic,call:this});at(e.SwEvent.Notification,t,this.id)||at(e.SwEvent.Notification,t,this.session.uuid);break}case Be.Ringing:performance.mark(wi(this.id,"ringing")),this.playRingback(),n.telnyx_call_control_id&&(this.options.telnyxCallControlId=n.telnyx_call_control_id),n.telnyx_session_id&&(this.options.telnyxSessionId=n.telnyx_session_id),n.telnyx_leg_id&&(this.options.telnyxLegId=n.telnyx_leg_id);break;case Be.Bye:const t=n.client_state||n.clientState;t&&(this.options.clientState=t),this.stopRingback(),this.stopRingtone(),this.hangup(Object.assign(Object.assign({},n),{initiator:"remote:telnyx_rtc.bye"}),!1)}}handleConferenceUpdate(e,t){return i(this,void 0,void 0,(function*(){if(!this._checkConferenceSerno(e.wireSerno)&&e.name!==t.laName)return Ie.error("ConferenceUpdate invalid wireSerno or packet name:",e),"INVALID_PACKET";const{action:i,data:n,hashKey:s=String(this._lastSerno),arrIndex:o}=e;switch(i){case"bootObj":{this._lastSerno=0;const{chatChannel:e,infoChannel:i,modChannel:s,laName:o,conferenceMemberID:r,role:a}=t;this._dispatchConferenceUpdate({action:Qe.Join,conferenceName:o,participantId:Number(r),role:a}),e&&(yield this._subscribeConferenceChat(e)),i&&(yield this._subscribeConferenceInfo(i));const c=[];for(const e in n)c.push(Object.assign({callId:n[e][0],index:Number(e)},ke(n[e][1])));this._dispatchConferenceUpdate({action:Qe.Bootstrap,participants:c});break}case"add":this._dispatchConferenceUpdate(Object.assign({action:Qe.Add,callId:s,index:o},ke(n)));break;case"modify":this._dispatchConferenceUpdate(Object.assign({action:Qe.Modify,callId:s,index:o},ke(n)));break;case"del":this._dispatchConferenceUpdate(Object.assign({action:Qe.Delete,callId:s,index:o},ke(n)));break;case"clear":this._dispatchConferenceUpdate({action:Qe.Clear});break;default:this._dispatchConferenceUpdate({action:i,data:n,callId:s,index:o})}}))}_addChannel(e){this.channels.includes(e)||this.channels.push(e);const t=this.session.relayProtocol;this.session._existsSubscription(t,e)&&(this.session.subscriptions[t][e]=Object.assign(Object.assign({},this.session.subscriptions[t][e]),{callId:this.id}))}_subscribeConferenceChat(e){return i(this,void 0,void 0,(function*(){const t={nodeId:this.nodeId,channels:[e],handler:e=>{const{direction:t,from:i,fromDisplay:n,message:s,type:o}=e.data;this._dispatchConferenceUpdate({action:Qe.ChatMessage,direction:t,participantNumber:i,participantName:n,messageText:s,messageType:o,messageId:e.eventSerno})}},i=yield this.session.vertoSubscribe(t).catch((e=>{Ie.error("ConfChat subscription error:",e)}));Xt(i,e)&&(this._addChannel(e),Object.defineProperties(this,{sendChatMessage:{configurable:!0,value:(t,i)=>{this.session.vertoBroadcast({nodeId:this.nodeId,channel:e,data:{action:"send",message:t,type:i}})}}}))}))}_subscribeConferenceInfo(e){return i(this,void 0,void 0,(function*(){const t={nodeId:this.nodeId,channels:[e],handler:e=>{const{eventData:t}=e;if("layout-info"===t.contentType)t.callID=this.id,yi(this.session,t);else Ie.error("Conference-Info unknown contentType",e)}},i=yield this.session.vertoSubscribe(t).catch((e=>{Ie.error("ConfInfo subscription error:",e)}));Xt(i,e)&&this._addChannel(e)}))}_confControl(e,t={}){const i=Object.assign({application:"conf-control",callID:this.id,value:null},t);this.session.vertoBroadcast({nodeId:this.nodeId,channel:e,data:i})}_handleChangeHoldStateSuccess(e){return"active"===e.holdState?this.setState(Ke.Active):this.setState(Ke.Held),!0}_handleChangeHoldStateError(t){Ie.error(`Failed to ${t.action} on call ${this.id}`);const i=pe(S,t);return at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),!1}_sendIceRestartModify(e){const t=new Lt({sessid:this.session.sessionid,action:Ze.UpdateMedia,callID:this.options.id,sdp:e,dialogParams:this.options});Ie.info("ICE restart: sending Modify with new offer SDP"),this._execute(t).then((e=>i(this,void 0,void 0,(function*(){var t;(null==e?void 0:e.sdp)?(Ie.info("ICE restart Modify response received"),null===(t=this.peer)||void 0===t||t.finishIceRestart(),yield this._onRemoteSdp(e.sdp)):this._onIceRestartFailed("ICE restart Modify response missing SDP")})))).catch((e=>{this._onIceRestartFailed("ICE restart Modify failed",e)}))}_onIceRestartFailed(t,i){var n;Ie.error(t,i),null===(n=this.peer)||void 0===n||n.finishIceRestart();const s=pe(N,i);at(e.SwEvent.Error,{error:s,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}_onRemoteSdp(t){return i(this,void 0,void 0,(function*(){const n=new RTCSessionDescription({sdp:t,type:Ge.Answer});yield this.peer.instance.setRemoteDescription(n).then((()=>{performance.mark(wi(this.id,"set-remote-description")),this.options.trickleIce&&(this._isRemoteDescriptionSet=!0,this._flushPendingTrickleIceCandidates()),this.gotEarly&&this.setState(Ke.Early),this.gotAnswer&&this.setState(Ke.Active)})).catch((t=>i(this,void 0,void 0,(function*(){Ie.error("Call setRemoteDescription Error: ",t);const i=pe(g,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid);try{yield this.hangup({cause:"USER_BUSY",causeCode:17,initiator:"sdk:set-remote-description-failure"},!0)}catch(e){Ie.error("Error during hangup after setRemoteDescription failure:",e)}}))))}))}_requestAnotherLocalDescription(){Ae(this.peer.onSdpReadyTwice)?at(e.SwEvent.Error,{error:new Error("SDP without candidates for the second time!"),sessionId:this.session.sessionid},this.session.uuid):(Object.defineProperty(this.peer,"onSdpReadyTwice",{value:this._onIceSdp.bind(this)}),this.peer.iceDone=!1,this.peer.startNegotiation())}_onIceSdp(t){var n,s,o;if(this._iceTimeout&&clearTimeout(this._iceTimeout),this._iceTimeout=null,this.peer&&(this.peer.iceDone=!0),!t)return void Ie.warn("localDescription is null — PeerConnection may have been closed during ICE gathering");const{sdp:r,type:a}=t;if(-1===r.indexOf("candidate"))return Ie.info("No candidate - retry \n"),void this._requestAnotherLocalDescription();if(null===(s=null===(n=this.peer)||void 0===n?void 0:n.instance)||void 0===s||s.removeEventListener("icecandidate",this._onIce),!te.test(r)){const t=le(V);Ie.warn(`[${this.id}] Warning ${t.code}: ${t.message}`),at(e.SwEvent.Warning,{warning:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}performance.mark(wi(this.id,"ice-gathering-end"));let c=null;const d={sessid:this.session.sessionid,sdp:r,dialogParams:this.options,"User-Agent":`Web-${Ai}`};if(null===(o=this.peer)||void 0===o?void 0:o.isIceRestarting)this._sendIceRestartModify(r);else{switch(a){case Ge.Offer:this.setState(Ke.Requesting),c=new Tt(d);break;case Ge.Answer:this._isRecovering||this.setState(Ke.Answering),c=!0===this.options.attach?new Rt(d):new kt(d);break;default:return Ie.error(`${this.id} - Unknown local SDP type:`,t),void this.hangup({initiator:"sdk:unknown-local-sdp-type"},!1)}performance.mark(wi(this.id,"send-sdp")),this._execute(c).then((e=>{if(this._isTerminatingOrTerminated())return void Ie.debug(`[${this.id}] Ignoring ${a} response because call is ${this.state}`);const{node_id:t=null}=e;this._targetNodeId=t,a===Ge.Offer?this.setState(Ke.Trying):this.setState(Ke.Active)})).catch((t=>i(this,void 0,void 0,(function*(){Ie.error(`${this.id} - Sending ${a} error:`,t);const i=pe(v,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid);try{yield this.hangup({cause:"USER_BUSY",causeCode:17,initiator:"sdk:sdp-send-failure"},!0)}catch(e){Ie.error("Error during hangup after SDP send failure:",e)}}))))}}_onTrickleIceSdp(t){var n;if(!t)return Ie.error("No SDP data provided"),void this.hangup({initiator:"sdk:missing-local-sdp"},!1);const{sdp:s,type:o}=t;let r=null;const a={sessid:this.session.sessionid,sdp:s,dialogParams:this.options,trickle:!0,"User-Agent":`Web-${Ai}`};if(null===(n=this.peer)||void 0===n?void 0:n.isIceRestarting)this._sendIceRestartModify(s);else{switch(o){case Ge.Offer:this.setState(Ke.Requesting),r=new Tt(a);break;case Ge.Answer:this._isRecovering||this.setState(Ke.Answering),r=!0===this.options.attach?new Rt(a):new kt(a);break;default:return Ie.error(`${this.id} - Unknown local SDP type:`,t),void this.hangup({initiator:"sdk:unknown-local-sdp-type"},!1)}performance.mark(wi(this.id,"send-sdp")),this._execute(r).then((e=>{if(this._isTerminatingOrTerminated())return void Ie.debug(`[${this.id}] Ignoring ${o} response because call is ${this.state}`);const{node_id:t=null}=e;this._targetNodeId=t,o===Ge.Offer?this.setState(Ke.Trying):this.setState(Ke.Active)})).catch((t=>i(this,void 0,void 0,(function*(){Ie.error(`${this.id} - Sending ${o} error:`,t);const i=pe(v,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid);try{yield this.hangup({cause:"USER_BUSY",causeCode:17,initiator:"sdk:sdp-send-failure"},!0)}catch(e){Ie.error("Error during hangup after SDP send failure:",e)}}))))}}_onIce(e){var t;const{instance:i}=this.peer;if(null===this._iceTimeout){const e=this.options.attach?5e3:1e3;this._iceTimeout=setTimeout((()=>this._onIceSdp(i.localDescription)),e)}e.candidate?(Ie.debug("RTCPeer Candidate:",e.candidate),null===(t=this.peer)||void 0===t||t.incrementGatheredCandidates(),this._trackCandidateMarks(e.candidate)):this._onIceSdp(i.localDescription)}_onTrickleIce(e){var t;e.candidate&&e.candidate.candidate?(Ie.debug("RTCPeer Candidate:",e.candidate),null===(t=this.peer)||void 0===t||t.incrementGatheredCandidates(),this._trackCandidateMarks(e.candidate),this._sendIceCandidate(e.candidate)):this._sendEndOfCandidates()}_sendIceCandidate(e){const t=new Ot({sessid:this.session.sessionid,candidate:e.candidate,sdpMLineIndex:e.sdpMLineIndex,sdpMid:e.sdpMid,dialogParams:this.options});this._execute(t)}_addIceCandidate(e){if(!this._isRemoteDescriptionSet)return Ie.debug("Remote description not set. Queued ICE candidate.",e),void this._pendingIceCandidates.push(e);this._addIceCandidateToPeer(e)}_addIceCandidateToPeer(e){const t=this.peer.instance.addIceCandidate(e);Promise.resolve(t).then((()=>{Ie.debug("Successfully added ICE candidate:",e)})).catch((t=>{Ie.error("Failed to add ICE candidate:",t,e)}))}_sendEndOfCandidates(){const e=new Nt({sessid:this.session.sessionid,endOfCandidates:!0,dialogParams:this.options});this._execute(e)}_trackCandidateMarks(e){var t;if(this._firstCandidateSent||(performance.mark(wi(this.id,"first-candidate")),this._firstCandidateSent=!0),!this._firstNonHostCandidateSent){const i=null===(t=e.candidate.match(/typ (\w+)/))||void 0===t?void 0:t[1];i&&"host"!==i&&(performance.mark(wi(this.id,"first-non-host-candidate")),this._firstNonHostCandidateSent=!0)}}_resetTrickleIceCandidateState(){this._pendingIceCandidates=[],this._isRemoteDescriptionSet=!1,this._firstCandidateSent=!1,this._firstNonHostCandidateSent=!1}_flushPendingTrickleIceCandidates(){if(!this._pendingIceCandidates.length)return;const e=[...this._pendingIceCandidates];this._pendingIceCandidates=[],e.forEach((e=>{this._addIceCandidateToPeer(e)}))}_registerPeerEvents(e){e.onicecandidate=e=>{var t,i;if(this.options.trickleIce&&!(null===(t=this.peer)||void 0===t?void 0:t.isIceRestarting))this._onTrickleIce(e);else{if(null===(i=this.peer)||void 0===i?void 0:i.iceDone)return;this._onIce(e)}},e.onicegatheringstatechange=t=>{Ie.debug("ICE gathering state changed:",e.iceGatheringState,t),"complete"===e.iceGatheringState&&(Ie.debug("Finished gathering candidates"),performance.mark(wi(this.id,"ice-gathering-completed")))},e.onicecandidateerror=t=>{var i;if(Ie.debug("ICE candidate error:",t),null===(i=this.peer)||void 0===i?void 0:i.statsReporter){const i=function(e,t){var i,n;return{errorCode:e.errorCode,errorText:e.errorText,url:e.url,address:e.address,port:e.port,connectionState:t.connectionState,iceConnectionState:t.iceConnectionState,iceGatheringState:t.iceGatheringState,signalingState:t.signalingState,localDescriptionType:null===(i=t.localDescription)||void 0===i?void 0:i.type,remoteDescriptionType:null===(n=t.remoteDescription)||void 0===n?void 0:n.type}}(t,e);this.peer.statsReporter.reportIceCandidateError(i)}},e.addEventListener("addstream",(e=>{this.options.remoteStream=e.stream})),e.addEventListener("track",(e=>{this.options.remoteStream=e.streams[0];const{remoteElement:t,remoteStream:i,screenShare:n}=this.options;!1===n&&jt(t,i)}))}_onMediaError(t){const i=(null==t?void 0:t.name)||"UnknownError",n=(null==t?void 0:t.message)||"Unknown media error",s=(null==t?void 0:t.originalError)||t;this._dispatchNotification({type:Ve.userMediaError,error:s,call:this,errorName:i,errorMessage:n}),Ie.error(`Media error (${i}): ${n}`,t),at(e.SwEvent.Error,{error:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),this.hangup({initiator:"sdk:media-error"},!1)}_onPeerConnectionFailureError(t){this._dispatchNotification({type:Ve.peerConnectionFailureError,error:t.error}),Ie.error("Peer connection failure error"),t.warning&&at(e.SwEvent.Warning,{warning:t.warning,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}_onPeerConnectionSignalingStateClosed(e){this._signalingStateClosed=!0,this._dispatchNotification(Object.assign({type:Ve.signalingStateClosed},e)),Ie.debug("Peer connection signaling state closed, call is not recoverable")}_dispatchConferenceUpdate(e){this._dispatchNotification(Object.assign({type:Ve.conferenceUpdate,call:this},e))}_dispatchNotification(t){!0!==this.options.screenShare&&(at(e.SwEvent.Notification,t,this.id,!1)||at(e.SwEvent.Notification,t,this.session.uuid))}_execute(e){return this.nodeId&&(e.targetNodeId=this.nodeId),this.session.execute(e)}_registerInboundAnswerAttempt(){if(this.options.attach||this._isRecovering)return!0;for(const t of this._getSessionInboundAnswerCalls()){if(t.id===this.id)continue;if(!t._isBlockingInboundAnswer())continue;const i=le(K);return at(e.SwEvent.Warning,{warning:i,callId:this.id,activeCallId:t.id,sessionId:this.session.sessionid,activeSessionId:t.session.sessionid},this.session.uuid),Ie.warn(`[${this.id}] answer() ignored: inbound call ${t.id} is already answering or active`),!1}return!0}_getSessionInboundAnswerCalls(){return Object.values(this.session.calls).filter((e=>Boolean(e))).filter((e=>!e.options.attach&&!e._isRecovering&&e.direction===We.Inbound))}_isBlockingInboundAnswer(){var e;if([Ke.Hangup,Ke.Destroy,Ke.Purge].includes(this._state))return!1;const t=[Ke.Answering,Ke.Early,Ke.Active,Ke.Held].includes(this._state);return!(!this._creatingPeer&&!t)&&(!(!this._creatingPeer||(null===(e=this.peer)||void 0===e?void 0:e.instance))||this._hasUsablePeerConnection())}_hasUsablePeerConnection(){var e;const t=null===(e=this.peer)||void 0===e?void 0:e.instance;return!!t&&("closed"!==t.signalingState&&("closed"!==t.connectionState&&"closed"!==t.iceConnectionState))}_init(){var t,i,n;const{id:s,userVariables:o,remoteCallerNumber:r,onNotification:a,recoveredCallId:d}=this.options;var l;this.options.id=s?s.toString():c(),this.id=this.options.id,d&&(this.recoveredCallId=d,this._isRecovering=!0),o&&(l=o,0!==Object.keys(l).length)||(this.options.userVariables=this.session.options.userVariables||{}),r||(this.options.remoteCallerNumber=this.options.destinationNumber),this.session.calls[this.id]=this,st(e.SwEvent.MediaError,this._onMediaError,this.id),st(e.SwEvent.PeerConnectionFailureError,this._onPeerConnectionFailureError,this.id),st(e.SwEvent.PeerConnectionSignalingStateClosed,this._onPeerConnectionSignalingStateClosed,this.id),Ae(a)&&st(e.SwEvent.Notification,a.bind(this),this.id);const h=!1!==this.session.options.enableCallReports,u=this.session.options.callReportInterval||5e3,p=null!==(t=this.session.options.callReportFlushInterval)&&void 0!==t?t:18e4,g=this.session.options.debugLogLevel||"debug",v=this.session.options.debugLogMaxEntries||1e3;h&&(this._callReportCollector=new hi({enabled:!0,interval:u,intermediateReportInterval:p},{enabled:!0,level:g,maxEntries:v}),this._callReportCollector.onFlushNeeded=()=>{this._flushIntermediateReport()},this._callReportCollector.onWarning=t=>{var i,n,s,o;at(e.SwEvent.Warning,{warning:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),t.code===$?null===(n=(i=this.session).reportNoRtp)||void 0===n||n.call(i,this.id,"inbound"):t.code===H&&this._hasActiveUnmutedLocalAudioTrack()&&(null===(o=(s=this.session).reportNoRtp)||void 0===o||o.call(s,this.id,"outbound"))}),this._isRecovering?this.setState(Ke.Recovering):this.setState(Ke.New),Ie.info(`New Call — region: ${null!==(i=this.session.region)&&void 0!==i?i:"unknown"}, dc: ${null!==(n=this.session.dc)&&void 0!==n?n:"unknown"}`,this.options)}_finalize(){var t;this._stopStats(),ki(this.id),Ie.debug(`[${this.id}] Closing peer from _finalize`),null===(t=this.peer)||void 0===t||t.close();const{remoteStream:i,localStream:n,remoteElement:s,localElement:o}=this.options;Vt(i),Vt(n),Gt(s,i),Gt(o,n),rt(e.SwEvent.MediaError,null,this.id),rt(e.SwEvent.PeerConnectionFailureError,null,this.id),rt(e.SwEvent.PeerConnectionSignalingStateClosed,null,this.id),this.session.calls[this.id]=null,delete this.session.calls[this.id];const r=this._postCallReport().catch((e=>{Ie.error("Unexpected error in _postCallReport",{error:e})}));this.session.trackCallReportUpload(r)}_getCallReportVoiceSdkId(){return this.session.callReportVoiceSdkId||void 0}flushIntermediateCallReport(e={type:"manual"}){this._flushIntermediateReport(e)}_flushIntermediateReport(e={type:"buffer-limit"}){var t;if(!this._callReportCollector)return;const i=this.session.callReportId;if(!i)return void Ie.debug("Cannot flush intermediate report: call_report_id not available");const n=null===(t=this.session.connection)||void 0===t?void 0:t.host;if(!n)return void Ie.debug("Cannot flush intermediate report: connection host not available");const s={callId:this.id,destinationNumber:this.options.destinationNumber,callerNumber:this.options.callerNumber,direction:this.direction===We.Inbound?"inbound":"outbound",state:this.state,telnyxSessionId:this.options.telnyxSessionId,telnyxLegId:this.options.telnyxLegId,sdkVersion:Ai},o=this._callReportCollector.flush(s,e);if(!o)return;Ie.info("Flushing intermediate call report",{callId:this.id,flushReason:e,segment:o.segment});const r=this._getCallReportVoiceSdkId(),a=this._callReportCollector.sendPayload(o,i,n,r).catch((e=>{Ie.error("Failed to post intermediate call report segment",{error:e})}));this.session.trackCallReportUpload(a)}_postCallReport(){var e,t;return i(this,void 0,void 0,(function*(){if(!this._callReportCollector)return void Ie.warn("Call report collector not initialized");yield this._callReportCollector.stop();const i=this.session.callReportId;if(!i)return Ie.debug("Cannot post call report: call_report_id not available"),void this._callReportCollector.cleanup();const n={callId:this.id,destinationNumber:this.options.destinationNumber,callerNumber:this.options.callerNumber,direction:this.direction===We.Inbound?"inbound":"outbound",state:this.state,telnyxSessionId:this.options.telnyxSessionId,telnyxLegId:this.options.telnyxLegId,sdkVersion:Ai},s=null===(e=this.session.connection)||void 0===e?void 0:e.host;if(!s)return void Ie.error("Cannot post call report: connection host not available");const o=this._getCallReportVoiceSdkId();try{yield this._callReportCollector.postReport(n,i,s,o)}catch(e){throw Ie.error("Failed to post call report",{error:e}),e}finally{null===(t=this._callReportCollector)||void 0===t||t.cleanup()}}))}_startStats(e){this._statsIntervalId=setInterval(this._doStats,e),Ie.info("Stats started")}_stopStats(){this._statsIntervalId&&(clearInterval(this._statsIntervalId),this._statsIntervalId=null),Ie.debug("Stats stopped")}}Oi.setStateTelnyx=e=>{if(e){switch(e._state){case Ke.Recovering:e.state="recovering";break;case Ke.Requesting:case Ke.Trying:case Ke.Early:e.state="connecting";break;case Ke.Active:e.state="active";break;case Ke.Held:e.state="held";break;case Ke.Hangup:case Ke.Destroy:e.state="done";break;case Ke.Answering:e.state="ringing";break;case Ke.New:e.state="new"}return e}};class Ni extends Oi{constructor(){super(...arguments),this._statsInterval=null,this.sendConversationMessage=(e,t)=>this.session.execute(new li(e,t))}hangup(e={},t=!0){const n=Object.create(null,{hangup:{get:()=>super.hangup}});return i(this,void 0,void 0,(function*(){this.screenShare instanceof Ni&&(yield this.screenShare.hangup(e,t)),yield n.hangup.call(this,e,t)}))}startScreenShare(e){return i(this,void 0,void 0,(function*(){const t=yield(n={video:!0},navigator.mediaDevices.getDisplayMedia(n));var n;t.getTracks().forEach((e=>{e.addEventListener("ended",(()=>i(this,void 0,void 0,(function*(){this.screenShare&&(yield this.screenShare.hangup({initiator:"sdk:screenshare-track-ended"}))}))))}));const{remoteCallerName:s,remoteCallerNumber:o,callerName:r,callerNumber:a}=this.options,c=Object.assign({screenShare:!0,localStream:t,destinationNumber:`${this.extension}-screen`,remoteCallerName:s,remoteCallerNumber:`${o}-screen`,callerName:`${r} (Screen)`,callerNumber:`${a} (Screen)`},e);return this.screenShare=new Ni(this.session,c),this.screenShare.invite(),this.screenShare}))}stopScreenShare(){return i(this,void 0,void 0,(function*(){this.screenShare instanceof Ni&&(yield this.screenShare.hangup({initiator:"app:stopScreenShare"}))}))}setAudioOutDevice(e){return i(this,void 0,void 0,(function*(){this.options.speakerId=e;const{remoteElement:t,speakerId:i}=this.options;return!(!t||!i)&&Wt(t,i)}))}_finalize(){this._stats(!1),super._finalize()}_stats(e=!0){if(!1===e)return clearInterval(this._statsInterval);this._statsInterval=window.setInterval((()=>i(this,void 0,void 0,(function*(){const e=yield this.peer.instance.getStats(null);let t="";const i=["certificate","codec","peer-connection","stream","local-candidate","remote-candidate"],n=["id","type","timestamp"];e.forEach((e=>{i.includes(e.type)||(t+=`\n${e.type}\n`,Object.keys(e).forEach((i=>{n.includes(i)||(t+=`\t${i}: ${e[i]}\n`)})))})),Ie.info(t)}))),2e3)}}class Li extends Ft{constructor(e){super(e),this.calls={},this.autoRecoverCalls=!0,this._iceServers=[],this._localElement=null,this._remoteElement=null,this._jwtAuth=!0,this._audioConstraints=!0,this._previousAudioConstraints=!0,this._videoConstraints=!1,this._speaker=null,this._onlineHandler=null,this._offlineHandler=null,this._wasOffline=!1,this._videoConstraints=e.video||!1,this.iceServers=e.iceServers,this.ringtoneFile=e.ringtoneFile,this.ringbackFile=e.ringbackFile,this._setupNetworkListeners()}get reconnectDelay(){return 1e3}getIsRegistered(){const e=Object.create(null,{getIsRegistered:{get:()=>super.getIsRegistered}});return i(this,void 0,void 0,(function*(){return e.getIsRegistered.call(this)}))}connect(){const e=Object.create(null,{connect:{get:()=>super.connect}});return i(this,void 0,void 0,(function*(){e.connect.call(this)}))}checkPermissions(e=!0,t=!0){return i(this,void 0,void 0,(function*(){try{const i=yield qt({audio:e,video:t});return Vt(i),!0}catch(e){return!1}}))}logout(){this.disconnect()}disconnect(){const e=Object.create(null,{disconnect:{get:()=>super.disconnect}});return i(this,void 0,void 0,(function*(){Ie.info("[disconnect] Client-initiated disconnect — setting Purge with BYE on all active calls.");for(const e in this.calls){const t=this.calls[e];t.setState(Ke.Purge),Ie.info("Start hangup for ",t),yield t.hangup({initiator:"app:client.disconnect"},!0)}this.calls={},this._cleanupNetworkListeners(),yield e.disconnect.call(this)}))}serverDisconnect(){const e=Object.create(null,{disconnect:{get:()=>super.disconnect}});return i(this,void 0,void 0,(function*(){Ie.info("[serverDisconnect] Server-initiated disconnect — setting Purge without BYE on all active calls.");for(const e in this.calls){const t=this.calls[e];t.setState(Ke.Purge),t.hangup({initiator:"sdk:server-disconnect"},!1)}this.calls={},this._cleanupNetworkListeners(),yield e.disconnect.call(this)}))}socketDisconnect(){this._closeConnection()}handleLoginError(e){super._handleLoginError(e)}speedTest(t){return new Promise(((i,n)=>{if(ot(e.SwEvent.SpeedTest,(e=>{const{upDur:n,downDur:s}=e,o=s?8*t/(s/1e3)/1024:0;i({upDur:n,downDur:s,upKps:(n?8*t/(n/1e3)/1024:0).toFixed(0),downKps:o.toFixed(0)})}),this.uuid),!(t=Number(t)))return n(`Invalid parameter 'bytes': ${t}`);this.executeRaw(`#SPU ${t}`);let s=t/1024;t%1024&&s++;const o=".".repeat(1024);for(let e=0;e<s;e++)this.executeRaw(`#SPB ${o}`);this.executeRaw("#SPE")}))}getDevices(){return Yt().catch((t=>{const i=pe(ue(t),t);return at(e.SwEvent.MediaError,i,this.uuid),[]}))}getVideoDevices(){return Yt(Xe.Video).catch((t=>(at(e.SwEvent.MediaError,t,this.uuid),[])))}getAudioInDevices(){return Yt(Xe.AudioIn).catch((t=>{const i=pe(ue(t),t);return at(e.SwEvent.MediaError,i,this.uuid),[]}))}getAudioOutDevices(){return Yt(Xe.AudioOut).catch((t=>(Ie.error("getAudioOutDevices",t),at(e.SwEvent.MediaError,t,this.uuid),[])))}validateDeviceId(e,t,i){return Jt(e,t,i)}getDeviceResolutions(e){return i(this,void 0,void 0,(function*(){try{return yield(e=>i(void 0,void 0,void 0,(function*(){const t=[],i=yield qt({video:{deviceId:{exact:e}}}),n=i.getVideoTracks()[0];for(let e=0;e<Kt.length;e++){const[i,s]=Kt[e];(yield n.applyConstraints({width:{exact:i},height:{exact:s}}).then((()=>!0)).catch((()=>!1)))&&t.push({resolution:`${i}x${s}`,width:i,height:s})}return Vt(i),t})))(e)}catch(e){throw e}}))}get mediaConstraints(){return{audio:this._audioConstraints,video:this._videoConstraints}}setAudioSettings(e){return i(this,void 0,void 0,(function*(){if(!e)throw new Error("You need to provide the settings object");const{micId:n,micLabel:s}=e,o=t(e,["micId","micLabel"]);return Qt(o),this._audioConstraints=yield((e,t,n,s)=>i(void 0,void 0,void 0,(function*(){const{deviceId:i}=s;if(void 0===i&&(e||t)){const i=yield Jt(e,t,n).catch((e=>null));i&&(s.deviceId={exact:i})}return s})))(n,s,"audioinput",o),this.micId=n,this.micLabel=s,this._audioConstraints}))}disableMicrophone(){this._previousAudioConstraints=this._audioConstraints,this._audioConstraints=!1}enableMicrophone(){this._audioConstraints=this._previousAudioConstraints||!0}set iceServers(e){if(e&&Array.isArray(e))this._iceServers=e;else{const e="development"===this.options.env;this._iceServers=e?re:oe}}get iceServers(){return this._iceServers}set speaker(e){this._speaker=e}get speaker(){return this._speaker}set localElement(e){this._localElement=Oe(e)}get localElement(){return this._localElement}set remoteElement(e){this._remoteElement=Oe(e)}get remoteElement(){return this._remoteElement}vertoBroadcast({nodeId:e,channel:t="",data:i}){if(!t)throw new Error(`Invalid channel for broadcast: ${t}`);const n=new Pt({sessid:this.sessionid,eventChannel:t,data:i});e&&(n.targetNodeId=e),this.execute(n).catch((e=>e))}vertoSubscribe({nodeId:e,channels:t=[],handler:n}){return i(this,void 0,void 0,(function*(){if(!(t=t.filter((e=>e&&!this._existsSubscription(this.relayProtocol,e)))).length)return{};const i=new Mt({sessid:this.sessionid,eventChannel:t});e&&(i.targetNodeId=e);const s=yield this.execute(i),{unauthorized:o=[],subscribed:r=[]}=zt(s);return o.length&&o.forEach((e=>this._removeSubscription(this.relayProtocol,e))),r.forEach((e=>this._addSubscription(this.relayProtocol,n,e))),s}))}vertoUnsubscribe({nodeId:e,channels:t=[]}){return i(this,void 0,void 0,(function*(){if(!(t=t.filter((e=>e&&this._existsSubscription(this.relayProtocol,e)))).length)return{};const i=new xt({sessid:this.sessionid,eventChannel:t});e&&(i.targetNodeId=e);const n=yield this.execute(i),{unsubscribed:s=[],notSubscribed:o=[]}=zt(n);return s.forEach((e=>this._removeSubscription(this.relayProtocol,e))),o.forEach((e=>this._removeSubscription(this.relayProtocol,e))),n}))}_setupNetworkListeners(){"undefined"!=typeof window&&(this._onlineHandler=()=>{this._wasOffline&&(Ie.debug(`Network connectivity restored for session ${this.sessionid}. Reconnecting...`),this._wasOffline=!1,this._autoReconnect=!0,this.socketDisconnect())},this._offlineHandler=()=>{this._wasOffline=!0,Ie.debug(`Network connectivity lost for session ${this.sessionid}`);const t=pe(L);at(e.SwEvent.Error,{error:t,sessionId:this.sessionid},this.uuid)},window.addEventListener("online",this._onlineHandler),window.addEventListener("offline",this._offlineHandler))}_cleanupNetworkListeners(){"undefined"!=typeof window&&this._onlineHandler&&this._offlineHandler&&(window.removeEventListener("online",this._onlineHandler),window.removeEventListener("offline",this._offlineHandler),this._onlineHandler=null,this._offlineHandler=null)}static telnyxStateCall(e){return Ni.setStateTelnyx(e)}}class Di{constructor(e,t){this.code=t,this.message=e}}class Pi{constructor(e){this.session=e,this.retriedConnect=0,this.retriedRegister=0}_ack(e,t){const i=new wt(e,t);this.nodeId&&(i.targetNodeId=this.nodeId),this.session.execute(i)}reconnectDelay(){return 1e3*De(2,6)}handleMessage(t){var i,n,s,o,r,a,c,d,l,h,u;const{session:p}=this;p.setPingReceived();const{id:g,method:v,params:m={},voice_sdk_id:f}=t,_=null==m?void 0:m.callID,S=null==m?void 0:m.eventChannel,b=null==m?void 0:m.eventType,y=p.calls[_],I=null===(i=null==y?void 0:y.peer)||void 0===i?void 0:i.isConnectionHealthy();if(Array.isArray(null==m?void 0:m.reattached_sessions)&&0===m.reattached_sessions.length&&Object.keys(p.calls).length>0){const t=le(Q);at(e.SwEvent.Warning,{warning:t,sessionId:p.sessionid},p.uuid)}if("channelPvtData"===b)return this._handlePvtEvent(m.pvtData);const E=(e,t)=>{var i,n,s,o,r;const a={audio:!0,video:p.options.video,remoteSdp:m.sdp,destinationNumber:m.callee_id_number,remoteCallerName:m.caller_id_name,remoteCallerNumber:m.caller_id_number,callerName:m.callee_id_name,callerNumber:m.callee_id_number,attach:v===Be.Attach,mediaSettings:m.mediaSettings,debug:null!==(i=p.options.debug)&&void 0!==i&&i,debugOutput:null!==(n=p.options.debugOutput)&&void 0!==n?n:"socket",trickleIce:null!==(s=p.options.trickleIce)&&void 0!==s&&s,prefetchIceCandidates:null===(o=p.options.prefetchIceCandidates)||void 0===o||o,forceRelayCandidate:t||p.options.forceRelayCandidate||!1,keepConnectionAliveOnSocketClose:null!==(r=p.options.keepConnectionAliveOnSocketClose)&&void 0!==r&&r};_&&(a.id=_),m.telnyx_call_control_id&&(a.telnyxCallControlId=m.telnyx_call_control_id),m.telnyx_session_id&&(a.telnyxSessionId=m.telnyx_session_id),m.telnyx_leg_id&&(a.telnyxLegId=m.telnyx_leg_id),m.client_state&&(a.clientState=m.client_state),m.dialogParams&&m.dialogParams.custom_headers&&m.dialogParams.custom_headers.length&&(a.customHeaders=m.dialogParams.custom_headers),e&&(a.recoveredCallId=e),performance.mark(wi(a.id,"new-call-start"));const c=new Ni(p,a);return c.nodeId=this.nodeId,c},C=new _t(f),w=new bt(f);switch(v){case Be.Answer:case Be.Display:case Be.Candidate:case Be.Ringing:case Be.Bye:case Be.Media:if(!_||!y)return void Ie.error(`Received ${v} for non existing call:`,m);y.handleMessage(t),this._ack(g,v);break;case Be.Ping:this.session.setPingReceived(),this.session.execute(w);break;case Be.Punt:p.options.keepConnectionAliveOnSocketClose&&I?(Ie.info("[punt] Received PUNT from server. keepConnectionAliveOnSocketClose=true — disconnecting socket only, keeping calls alive."),p.socketDisconnect(),this._ack(g,v)):(Ie.info("[punt] Received PUNT from server — calling serverDisconnect() to purge all calls without BYE."),p.serverDisconnect());break;case Be.Invite:{const e=E();e.direction=We.Inbound,e.playRingtone(),e.setState(Ke.Ringing),this._ack(g,v);break}case Be.Attach:{if(!y){return E().answer(),void this._ack(g,v)}const e=y.id,t=null!==(s=null===(n=y.shouldForceRelayCandidateForRecovery)||void 0===n?void 0:n.call(y))&&void 0!==s&&s;t&&Ie.warn(`[${(new Date).toISOString()}][${_}] Attach: forcing relay candidate because recovered VPN media path is still stalled`),Ie.info(`[${(new Date).toISOString()}][${_}] closing existing call on ATTACH.`),y.hangup({isRecovering:!0,initiator:"sdk:attach-recovery"},!1),Ie.info(`[${(new Date).toISOString()}][${_}] Attach: Creating new call for recovery (recoveredCallId: ${e})`);E(e,t).answer(),this._ack(g,v);break}case Be.Event:case"webrtc.event":if(!S)return void Ie.error("Verto received an unknown event:",m);const i=p.relayProtocol,f=S.split(".")[0];p._existsSubscription(i,S)?at(i,m,S):S===p.sessionid?this._handleSessionEvent(m.eventData):p._existsSubscription(i,f)?at(i,m,f):p.calls.hasOwnProperty(S)?p.calls[S].handleMessage(t):at(e.SwEvent.Notification,m,p.uuid);break;case Be.Info:m.type=Ve.generic,at(e.SwEvent.Notification,m,p.uuid);break;case Be.ClientReady:this.session.execute(C);break;default:{const i=xe(t);if(i){switch(i){case ze.REGISTER:case ze.REGED:if(p.connection.previousGatewayState!==ze.REGED&&p.connection.previousGatewayState!==ze.REGISTER){this.session._triggerKeepAliveTimeoutCheck(),this.retriedRegister=0,i===ze.REGED&&this.session.resetReconnectAttempts();const n=null===(r=null===(o=null==t?void 0:t.result)||void 0===o?void 0:o.params)||void 0===r?void 0:r.call_report_id;n&&(p.callReportId=n,Ie.debug("Captured call_report_id from REGED:",n));const s=null===(c=null===(a=null==t?void 0:t.result)||void 0===a?void 0:a.params)||void 0===c?void 0:c.dc;s&&(p.dc=s);const g=null===(l=null===(d=null==t?void 0:t.result)||void 0===d?void 0:d.params)||void 0===l?void 0:l.region;g&&(p.region=g),Ie.info(`Connected to Telnyx — region: ${null!==(h=p.region)&&void 0!==h?h:"unknown"}, dc: ${null!==(u=p.dc)&&void 0!==u?u:"unknown"}`),m.type=Ve.vertoClientReady,at(e.SwEvent.Ready,m,p.uuid)}break;case ze.UNREGED:case ze.NOREG:if(this.retriedRegister+=1,5===this.retriedRegister){this.retriedRegister=0;const t=new Di("Fail to register the user, the server tried 5 times","UNREGED|NOREG"),i=pe(R,t);at(e.SwEvent.Error,{error:i,sessionId:p.sessionid},p.uuid);break}setTimeout((()=>{this.session.execute(C)}),this.reconnectDelay());break;case ze.FAILED:case ze.FAIL_WAIT:if(p.connection.previousGatewayState!==ze.FAILED&&p.connection.previousGatewayState!==ze.FAIL_WAIT){const t=pe(k,new Error(`Gateway state: ${i}`));if(at(e.SwEvent.Error,{error:t,sessionId:p.sessionid},p.uuid),!this.session.hasAutoReconnect()){this.retriedConnect=0;const t=new Di("Fail to connect the server, the server tried 5 times","FAILED|FAIL_WAIT"),i=pe(T,t);at(e.SwEvent.Error,{error:i,sessionId:p.sessionid},p.uuid);break}if(this.retriedConnect+=1,5===this.retriedConnect){this.retriedConnect=0;const t=pe(45003,new Error("Connection Retry Failed"));at(e.SwEvent.Error,{error:t,sessionId:p.sessionid},p.uuid);break}setTimeout((()=>{if(Ie.debug(`Reconnecting... Retry ${this.retriedConnect} of 5`),this.session.options.keepConnectionAliveOnSocketClose){const e=Object.values(p.calls).some((e=>{var t;return(null===(t=e.peer)||void 0===t?void 0:t.instance)&&!e.signalingStateClosed}));if(e)return Ie.debug("Reconnecting by keeping the existing session due to keepConnectionAliveOnSocketClose option being set."),void this.session.socketDisconnect();Ie.debug("keepConnectionAliveOnSocketClose is set but all peer connections have signalingState closed, doing full reconnect")}this.session.disconnect().then((()=>{this.session.clearConnection(),this.session.connect()}))}),this.reconnectDelay())}break;default:Ie.warn("GatewayState message unknown method:",t)}break}Ie.debug("Verto message unknown method:",t);break}}}_retrieveCallId(e,t){const i=Object.keys(this.session.calls);if("bootObj"!==e.action)return i.find((e=>this.session.calls[e].channels.includes(t)));{const t=e.data.find((e=>i.includes(e[0])));if(t instanceof Array)return t[0]}}_handlePvtEvent(t){return i(this,void 0,void 0,(function*(){const{session:i}=this,n=i.relayProtocol,{action:s,laChannel:o,laName:r,chatChannel:a,infoChannel:c,modChannel:d,conferenceMemberID:l,role:h,callID:u}=t;switch(s){case"conference-liveArray-join":{const n=()=>{i.vertoBroadcast({nodeId:this.nodeId,channel:o,data:{liveArray:{command:"bootstrap",context:o,name:r}}})},s={nodeId:this.nodeId,channels:[o],handler:({data:e})=>{const s=u||this._retrieveCallId(e,o);if(s&&i.calls.hasOwnProperty(s)){const a=i.calls[s];a._addChannel(o),a.extension=r,a.handleConferenceUpdate(e,t).then((e=>{"INVALID_PACKET"===e&&n()}))}}},a=yield i.vertoSubscribe(s).catch((t=>{Ie.error("liveArray subscription error:",t);const n=pe(I,t);at(e.SwEvent.Error,{error:n,sessionId:i.sessionid},i.uuid)}));Xt(a,o)&&n();break}case"conference-liveArray-part":{let t=null;if(o&&i._existsSubscription(n,o)){const{callId:s=null}=i.subscriptions[n][o];if(t=i.calls[s]||null,null!==s){const n={type:Ve.conferenceUpdate,action:Qe.Leave,conferenceName:r,participantId:Number(l),role:h};at(e.SwEvent.Notification,n,s,!1)||at(e.SwEvent.Notification,n,i.uuid),null===t&&rt(e.SwEvent.Notification,null,s)}}const s=[o,a,c,d];i.vertoUnsubscribe({nodeId:this.nodeId,channels:s}).then((({unsubscribedChannels:e=[]})=>{t&&(t.channels=t.channels.filter((t=>!e.includes(t))))})).catch((e=>{Ie.error("liveArray unsubscribe error:",e)}));break}}}))}_handleSessionEvent(t){switch(t.contentType){case"layout-info":case"layer-info":yi(this.session,t);break;case"logo-info":{const i={type:Ve.conferenceUpdate,action:Qe.LogoInfo,logo:t.logoURL};at(e.SwEvent.Notification,i,this.session.uuid);break}}}}class Mi extends Li{constructor(e){super(e),this.relayProtocol="verto-protocol",this.timeoutErrorCode=-329990,this.handleLoginOnSocketOpen=()=>i(this,void 0,void 0,(function*(){this._idle=!1;const{autoReconnect:e=!0}=this.options;yield this.login({onSuccess:()=>{this._autoReconnect=e}})})),this.handleAnonymousLoginOnSocketOpen=()=>i(this,void 0,void 0,(function*(){this._idle=!1,yield this.login()})),this._vertoHandler=new Pi(this),window.addEventListener("beforeunload",(e=>{this.calls&&Object.keys(this.calls).forEach((e=>{this.calls[e]&&(Ie.info(`Hanging up call due to window unload: ${e}`),this.calls[e].hangup({initiator:"sdk:beforeunload"},!0))}))}))}validateOptions(){return Pe(this.options)||Me(this.options)}newCall(e){if(!this.validateCallOptions(e)){throw pe(b,void 0,"Error: destinationNumber is required")}const t=new Ni(this,e);return performance.mark(wi(t.id,"new-call-start")),t.invite(),t}broadcast(e){return this.vertoBroadcast(e)}subscribe(e){return this.vertoSubscribe(e)}unsubscribe(e){return this.vertoUnsubscribe(e)}validateCallOptions(e){return!!Me(this.options)||Boolean(e.destinationNumber)}_onSocketOpen(){const e=Object.create(null,{_onSocketOpen:{get:()=>super._onSocketOpen}});return i(this,void 0,void 0,(function*(){return yield e._onSocketOpen.call(this),Pe(this.options)?this.handleLoginOnSocketOpen():Me(this.options)?this.handleAnonymousLoginOnSocketOpen():void 0}))}_onSocketMessage(e){this._vertoHandler.handleMessage(e)}}class xi extends Mi{constructor(e){super(e),Ie.info(`SDK version: ${It}`)}newCall(e){return super.newCall(e)}static webRTCInfo(){return si()}static webRTCSupportedBrowserList(){return[{operationSystem:"Android",supported:[{browserName:"Chrome",features:["audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",supported:oi.not_supported},{browserName:"Edge",supported:oi.not_supported}]},{operationSystem:"iOS",supported:[{browserName:"Chrome",supported:oi.not_supported},{browserName:"Firefox",supported:oi.not_supported},{browserName:"Safari",features:["video","audio"],supported:oi.full},{browserName:"Edge",supported:oi.not_supported}]},{operationSystem:"Linux",supported:[{browserName:"Chrome",features:["video","audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",supported:oi.not_supported},{browserName:"Edge",supported:oi.not_supported}]},{operationSystem:"MacOS",supported:[{browserName:"Chrome",features:["video","audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",features:["video","audio"],supported:oi.full},{browserName:"Edge",features:["audio"],supported:oi.partial}]},{operationSystem:"Windows",supported:[{browserName:"Chrome",features:["video","audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",supported:oi.not_supported},{browserName:"Edge",features:["audio"],supported:oi.partial}]}]}}class Ui{static run(t){return i(this,void 0,void 0,(function*(){const i=Ue({}),n=Ue({}),s=new xi(t.credentials);yield s.connect(),s.on(e.SwEvent.Ready,i.resolve),s.on(e.SwEvent.Error,i.reject),s.on(e.SwEvent.MediaError,i.reject),s.on(e.SwEvent.MediaError,i.reject),s.on(e.SwEvent.Notification,(e=>{e.call&&e.call.sipCode>=400&&n.reject(new Error(e.call.sipReason))})),st(e.SwEvent.StatsReport,(e=>{n.resolve(Ui.mapReport(e))})),yield i.promise,yield s.newCall({destinationNumber:t.texMLApplicationNumber,debug:!0});const o=yield n.promise;return yield s.disconnect(),o}))}static mapReport(e){var t,i,n,s,o,r,a,c,d,l,h,u,p;const g=[],v=[];for(const t of e)switch(t.event){case"onicecandidate":t.data&&g.push(t.data);break;case"stats":v.push(t.data)}let m=0,f=1/0,_=-1/0,S=0,b=1/0,y=-1/0,I=0;v.forEach((e=>{var t,i,n;if(!(null===(t=e.remote.audio.inbound)||void 0===t?void 0:t[0]))return;m+=1;const s=null!==(i=e.remote.audio.inbound[0].jitter)&&void 0!==i?i:0,o=null!==(n=e.remote.audio.inbound[0].roundTripTime)&&void 0!==n?n:0;S+=s,I+=o,_=Math.max(_,s),f=Math.min(f,s),y=Math.max(y,o),b=Math.min(b,o)}));const E=I/m,C=S/m,w=v[v.length-1],T=vi({jitter:1e3*C,rtt:1e3*E,packetsReceived:null!==(n=null===(i=null===(t=w.audio.inbound)||void 0===t?void 0:t[0])||void 0===i?void 0:i.packetsReceived)&&void 0!==n?n:0,packetsLost:null!==(r=null===(o=null===(s=w.audio.inbound)||void 0===s?void 0:s[0])||void 0===o?void 0:o.packetsLost)&&void 0!==r?r:0});return{iceCandidatePairStats:v[v.length-1].connection,summaryStats:{mos:T,jitter:{average:C,max:_,min:f},rtt:{average:E,max:y,min:b},quality:mi(T)},sessionStats:{packetsSent:null!==(a=w.connection.packetsSent)&&void 0!==a?a:0,bytesSent:null!==(c=w.connection.bytesSent)&&void 0!==c?c:0,bytesReceived:null!==(d=w.connection.bytesReceived)&&void 0!==d?d:0,packetsLost:null!==(u=null===(h=null===(l=w.remote.audio.inbound)||void 0===l?void 0:l[0])||void 0===h?void 0:h.packetsLost)&&void 0!==u?u:0,packetsReceived:null!==(p=w.connection.packetsReceived)&&void 0!==p?p:0},iceCandidateStats:g}}getTelnyxIds(){return{telnyxCallControlId:"",telnyxSessionId:"",telnyxLegId:""}}}e.Call=Ni,e.ERROR_TYPE=qe,e.NOTIFICATION_TYPE=Ve,e.PreCallDiagnosis=Ui,e.SDK_ERRORS=ce,e.SDK_WARNINGS=de,e.TELNYX_ERROR_CODES=d,e.TELNYX_WARNING_CODES=l,e.TelnyxError=he,e.TelnyxRTC=xi,e.isMediaRecoveryErrorEvent=function(e){return!0===e.recoverable},Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).TelnyxWebRTC={})}(this,(function(e){"use strict";function t(e,t){var i={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(i[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(n=Object.getOwnPropertySymbols(e);s<n.length;s++)t.indexOf(n[s])<0&&Object.prototype.propertyIsEnumerable.call(e,n[s])&&(i[n[s]]=e[n[s]])}return i}function i(e,t,i,n){return new(i||(i=Promise))((function(s,o){function r(e){try{c(n.next(e))}catch(e){o(e)}}function a(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}c((n=n.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;var n="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto),s=new Uint8Array(16);function o(){if(!n)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return n(s)}for(var r=[],a=0;a<256;++a)r[a]=(a+256).toString(16).substr(1);function c(e,t,i){var n=t&&i||0;"string"==typeof e&&(t="binary"===e?new Array(16):null,e=null);var s=(e=e||{}).random||(e.rng||o)();if(s[6]=15&s[6]|64,s[8]=63&s[8]|128,t)for(var a=0;a<16;++a)t[n+a]=s[a];return t||function(e,t){var i=t||0,n=r;return[n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],"-",n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]],n[e[i++]]].join("")}(s)}const d={SDP_CREATE_OFFER_FAILED:40001,SDP_CREATE_ANSWER_FAILED:40002,SDP_SET_LOCAL_DESCRIPTION_FAILED:40003,SDP_SET_REMOTE_DESCRIPTION_FAILED:40004,SDP_SEND_FAILED:40005,MEDIA_MICROPHONE_PERMISSION_DENIED:42001,MEDIA_DEVICE_NOT_FOUND:42002,MEDIA_GET_USER_MEDIA_FAILED:42003,HOLD_FAILED:44001,INVALID_CALL_PARAMETERS:44002,BYE_SEND_FAILED:44003,SUBSCRIBE_FAILED:44004,PEER_CLOSED_DURING_INIT:44005,WEBSOCKET_CONNECTION_FAILED:45001,WEBSOCKET_ERROR:45002,RECONNECTION_EXHAUSTED:45003,GATEWAY_FAILED:45004,LOGIN_FAILED:46001,INVALID_CREDENTIALS:46002,AUTHENTICATION_REQUIRED:46003,ICE_RESTART_FAILED:47001,NETWORK_OFFLINE:48001,UNEXPECTED_ERROR:49001},l={HIGH_RTT:31001,HIGH_JITTER:31002,HIGH_PACKET_LOSS:31003,LOW_MOS:31004,LOW_LOCAL_AUDIO:31005,LOW_BYTES_RECEIVED:32001,LOW_BYTES_SENT:32002,ICE_CONNECTIVITY_LOST:33001,ICE_GATHERING_TIMEOUT:33002,ICE_GATHERING_EMPTY:33003,PEER_CONNECTION_FAILED:33004,ONLY_HOST_ICE_CANDIDATES:33005,ANSWER_WHILE_PEER_ACTIVE:33006,ICE_CANDIDATE_PAIR_CHANGED:33008,DUPLICATE_INBOUND_ANSWER:33007,TOKEN_EXPIRING_SOON:34001,SESSION_NOT_REATTACHED:35001,SIGNALING_HEALTH_PROBE_TIMEOUT:36001,SIGNALING_REQUEST_TIMEOUT:36002,SIGNALING_RECOVERY_REQUIRED:36003,MEDIA_RECOVERY_REQUIRED:36004},{SDP_CREATE_OFFER_FAILED:h,SDP_CREATE_ANSWER_FAILED:u,SDP_SET_LOCAL_DESCRIPTION_FAILED:p,SDP_SET_REMOTE_DESCRIPTION_FAILED:g,SDP_SEND_FAILED:v,MEDIA_MICROPHONE_PERMISSION_DENIED:m,MEDIA_DEVICE_NOT_FOUND:f,MEDIA_GET_USER_MEDIA_FAILED:_,HOLD_FAILED:S,INVALID_CALL_PARAMETERS:b,BYE_SEND_FAILED:y,SUBSCRIBE_FAILED:I,PEER_CLOSED_DURING_INIT:E,WEBSOCKET_CONNECTION_FAILED:C,WEBSOCKET_ERROR:w,RECONNECTION_EXHAUSTED:T,GATEWAY_FAILED:k,LOGIN_FAILED:R,INVALID_CREDENTIALS:A,AUTHENTICATION_REQUIRED:O,ICE_RESTART_FAILED:N,NETWORK_OFFLINE:L,UNEXPECTED_ERROR:D}=d,{HIGH_RTT:P,HIGH_JITTER:M,HIGH_PACKET_LOSS:x,LOW_MOS:U,LOW_LOCAL_AUDIO:F,LOW_BYTES_RECEIVED:$,LOW_BYTES_SENT:j,ICE_CONNECTIVITY_LOST:H,ICE_GATHERING_TIMEOUT:G,ICE_GATHERING_EMPTY:W,PEER_CONNECTION_FAILED:B,ONLY_HOST_ICE_CANDIDATES:V,ANSWER_WHILE_PEER_ACTIVE:q,ICE_CANDIDATE_PAIR_CHANGED:Y,DUPLICATE_INBOUND_ANSWER:K,TOKEN_EXPIRING_SOON:J,SESSION_NOT_REATTACHED:Q,SIGNALING_HEALTH_PROBE_TIMEOUT:X,SIGNALING_REQUEST_TIMEOUT:z,SIGNALING_RECOVERY_REQUIRED:Z,MEDIA_RECOVERY_REQUIRED:ee}=l,te=/^a=candidate:.+typ (srflx|prflx|relay)/m,ie="wss://rtc.telnyx.com",ne={NORMAL_CLOSURE:1e3,GOING_AWAY:1001,PROTOCOL_ERROR:1002,UNSUPPORTED_DATA:1003,NO_STATUS_RECEIVED:1005,ABNORMAL_CLOSURE:1006,INVALID_FRAME_PAYLOAD:1007,POLICY_VIOLATION:1008,MESSAGE_TOO_BIG:1009,INTERNAL_ERROR:1011},se={urls:"stun:stun.l.google.com:19302"},oe=[{urls:"stun:stun.telnyx.com:3478"},se,{urls:"turn:turn.telnyx.com:3478?transport=udp",username:"testuser",credential:"testpassword"},{urls:"turn:turn.telnyx.com:3478?transport=tcp",username:"testuser",credential:"testpassword"}],re=[{urls:"stun:stundev.telnyx.com:3478"},se,{urls:"turn:turndev.telnyx.com:3478?transport=udp",username:"testuser",credential:"testpassword"},{urls:"turn:turndev.telnyx.com:3478?transport=tcp",username:"testuser",credential:"testpassword"}];var ae;(ae=e.SwEvent||(e.SwEvent={})).SocketOpen="telnyx.socket.open",ae.SocketClose="telnyx.socket.close",ae.SocketError="telnyx.socket.error",ae.SocketMessage="telnyx.socket.message",ae.SpeedTest="telnyx.internal.speedtest",ae.SocketActivity="telnyx.internal.socketActivity",ae.Ready="telnyx.ready",ae.Error="telnyx.error",ae.Warning="telnyx.warning",ae.Notification="telnyx.notification",ae.StatsFrame="telnyx.stats.frame",ae.StatsReport="telnyx.stats.report",ae.Messages="telnyx.messages",ae.Calls="telnyx.calls",ae.MediaError="telnyx.rtc.mediaError",ae.PeerConnectionFailureError="telnyx.rtc.peerConnectionFailureError",ae.PeerConnectionSignalingStateClosed="telnyx.rtc.peerConnectionSignalingStateClosed";const ce={40001:{name:"SDP_CREATE_OFFER_FAILED",message:"Failed to create call offer",description:"The browser was unable to generate a local SDP offer. This typically indicates a WebRTC API error or invalid media constraints.",causes:["Browser WebRTC API error","Missing or invalid media constraints"],solutions:["Check getUserMedia permissions","Verify ICE server configuration"]},40002:{name:"SDP_CREATE_ANSWER_FAILED",message:"Failed to answer the call",description:"The browser was unable to generate a local SDP answer. The remote offer may be invalid or the browser state inconsistent.",causes:["Browser WebRTC API error","Invalid remote SDP offer"],solutions:["Retry the call","Check browser WebRTC compatibility"]},40003:{name:"SDP_SET_LOCAL_DESCRIPTION_FAILED",message:"Failed to apply local call settings",description:"setLocalDescription() was rejected by the browser. The generated SDP may be malformed or the browser state may be inconsistent.",causes:["Malformed SDP","Browser state inconsistency"],solutions:["Retry the call"]},40004:{name:"SDP_SET_REMOTE_DESCRIPTION_FAILED",message:"Failed to apply remote call settings",description:"setRemoteDescription() was rejected by the browser. The remote SDP may be malformed or contain unsupported codecs.",causes:["Malformed remote SDP","Browser codec mismatch"],solutions:["Retry the call","Check codec configuration"]},40005:{name:"SDP_SEND_FAILED",message:"Failed to send call data to server",description:"The Invite or Answer message could not be delivered via the signaling WebSocket. The connection may have been lost.",causes:["WebSocket connection lost","Server error"],solutions:["Check network connectivity","Retry the call"]},42001:{name:"MEDIA_MICROPHONE_PERMISSION_DENIED",message:"Microphone access denied",description:"The user or operating system denied microphone permission. The browser permission prompt was dismissed or OS-level access is disabled.",causes:["User denied browser permission prompt","OS-level microphone access disabled"],solutions:["Ask user to grant microphone permission in browser settings"]},42002:{name:"MEDIA_DEVICE_NOT_FOUND",message:"No microphone found",description:"The requested audio input device is not available. No microphone is connected, the device was disconnected, or an invalid deviceId was specified.",causes:["No microphone connected","Device was disconnected","Invalid deviceId"],solutions:["Check that a microphone is connected","Select a valid audio input device"]},42003:{name:"MEDIA_GET_USER_MEDIA_FAILED",message:"Failed to access microphone",description:"getUserMedia() was rejected for an unexpected reason. The device may be in use by another application or the browser encountered an internal error.",causes:["Browser error","Device in use by another application"],solutions:["Close other applications using the microphone","Retry"]},44005:{name:"PEER_CLOSED_DURING_INIT",message:"Call was closed during setup",description:"The PeerConnection was closed (e.g. by hangup()) while peer.init() was still running. This is a race condition: an async operation such as setRemoteDescription, getUserMedia, or the media recovery flow yielded control, and close() ran during that gap. The init() cannot continue because the underlying RTCPeerConnection has been destroyed.",causes:["call.hangup() or call.close() was called while the call was still setting up","A WebSocket Bye message arrived during getUserMedia prompt or SDP negotiation","User clicked hangup/decline before media permissions were granted"],solutions:["This is expected if the user intentionally hung up during setup — no action needed","If this happens frequently without user action, check for automatic hangup triggers that may fire too early"]},44001:{name:"HOLD_FAILED",message:"Failed to hold the call",description:"The server rejected or did not respond to the hold request. The WebSocket connection may have been lost during the operation.",causes:["Server error","WebSocket connection lost during hold"],solutions:["Retry the hold operation","Check network connectivity"]},44002:{name:"INVALID_CALL_PARAMETERS",message:"Invalid call parameters",description:"The call could not be initiated because required parameters are missing or invalid. For example, no destination number was provided to newCall().",causes:["Missing destinationNumber in call options","Invalid or empty call parameters"],solutions:["Provide a valid destinationNumber when calling newCall()","Check the call options object for required fields"]},44003:{name:"BYE_SEND_FAILED",message:"Failed to hang up cleanly",description:"The hangup signal could not be delivered to the server. The call was terminated locally but the server may not be aware.",causes:["WebSocket connection lost before BYE sent"],solutions:["No action needed — call is terminated locally","Check network connectivity"]},44004:{name:"SUBSCRIBE_FAILED",message:"Failed to subscribe to call events",description:"The Verto subscribe request for the call channel failed. This may prevent receiving call state updates from the server.",causes:["WebSocket connection lost during subscribe","Server rejected the subscription request"],solutions:["Check network connectivity","Retry the call"]},45001:{name:"WEBSOCKET_CONNECTION_FAILED",message:"Unable to connect to server",description:"The WebSocket connection to the signaling server could not be established. The server may be unreachable, the URL may be incorrect, or a firewall may be blocking the connection.",causes:["Server unreachable","Incorrect WebSocket URL","Firewall blocking WebSocket connections","Network interruption"],solutions:["Check network connectivity","Verify the signaling server URL","Ensure WebSocket connections are not blocked by a firewall"]},45002:{name:"WEBSOCKET_ERROR",message:"Connection to server lost",description:"An error occurred on the WebSocket connection after it was established. The connection may have been dropped due to network issues or server-side closure.",causes:["Network interruption","Server closed the connection","Idle timeout"],solutions:["Check network connectivity","SDK will attempt automatic reconnection if configured"]},45003:{name:"RECONNECTION_EXHAUSTED",message:"Unable to reconnect to server",description:"All automatic reconnection attempts have been exhausted. The SDK tried to re-establish the WebSocket connection multiple times but failed on every attempt.",causes:["Prolonged network outage","Server unreachable","Firewall or proxy blocking reconnection"],solutions:["Check network connectivity","Call client.disconnect() and client.connect() to manually retry","Notify the user that the connection was lost"]},45004:{name:"GATEWAY_FAILED",message:"Gateway connection failed",description:"The upstream gateway reported a FAILED or FAIL_WAIT state. The signaling server could not establish or maintain a connection to the gateway. When autoReconnect is disabled, this is immediately fatal. When enabled, the SDK will retry until RECONNECTION_EXHAUSTED.",causes:["Gateway down or unreachable","Server-side infrastructure issue","Network partition between signaling server and gateway"],solutions:["Wait for automatic reconnection (if autoReconnect is enabled)","Call client.disconnect() and client.connect() to manually retry","Check Telnyx service status"]},46001:{name:"LOGIN_FAILED",message:"Authentication failed",description:"The login request was rejected by the server. The credentials may be invalid, expired, or the account may be suspended.",causes:["Invalid credentials (username/password or token)","Expired authentication token","Account suspended or disabled"],solutions:["Verify credentials","Generate a new authentication token","Check account status"]},46002:{name:"INVALID_CREDENTIALS",message:"Invalid credential parameters",description:"The SDK rejected the login options before sending any request to the server. This is an internal client-side validation guard — the credentials object is missing required fields or has an invalid structure. No network request was made.",causes:["Missing login and password fields","Missing or malformed authentication token","Invalid combination of credential fields in the options object"],solutions:["Provide valid login/password or a valid authentication token","Check the TelnyxRTC constructor options against the documentation","Ensure the credential object matches one of the supported auth modes (credentials, token, or anonymous)"]},46003:{name:"AUTHENTICATION_REQUIRED",message:"Authentication required",description:"The server rejected a request because the session is not authenticated. This can happen when the client sends a message (e.g. Invite, Subscribe, or Ping) before login completes, after a token expires mid-session, or after the server drops the authenticated state for any reason.",causes:["Message sent before login completed","Authentication token expired during the session","Server-side session was invalidated","WebSocket reconnected but re-authentication did not complete"],solutions:["Ensure the client is fully logged in before sending messages","Re-authenticate using client.login() with fresh credentials","Listen for telnyx.ready before making calls or sending requests"]},47001:{name:"ICE_RESTART_FAILED",message:"ICE restart failed",description:"The ICE restart Modify request could not be sent or the server returned an error. The media path could not be recovered via ICE restart.",causes:["WebSocket connection lost during ICE restart","Server rejected the Modify request","Timeout waiting for server response"],solutions:["The call may recover via WebSocket reconnect + Attach","If the call does not recover, hang up and retry"]},48001:{name:"NETWORK_OFFLINE",message:"Device is offline",description:"The browser reported that the device has lost network connectivity (navigator.onLine === false). All WebSocket and media connections will fail until the network is restored.",causes:["Wi-Fi or ethernet disconnected","Airplane mode enabled","Network interface went down"],solutions:["Check network connectivity","Reconnect to Wi-Fi or ethernet","Disable airplane mode"]},49001:{name:"UNEXPECTED_ERROR",message:"An unexpected error occurred",description:"An error was thrown that does not match any known SDK error category. This is a catch-all for unclassified failures.",causes:["Unknown or unhandled error condition"],solutions:["Check the originalError property for the underlying cause","Report the issue if it persists"]}},de={31001:{name:"HIGH_RTT",message:"High network latency detected",description:"Round-trip time (RTT) exceeded the threshold for multiple consecutive samples. High latency causes perceptible audio delays.",causes:["Poor network connection","Geographic distance to media server","Network congestion"],solutions:["Check network connectivity","Use a wired connection instead of Wi-Fi","Close bandwidth-heavy applications"]},31002:{name:"HIGH_JITTER",message:"High jitter detected",description:"Jitter (variability in packet arrival time) exceeded the threshold for multiple consecutive samples. High jitter causes crackling and choppy audio.",causes:["Network congestion","Unstable Wi-Fi connection","Overloaded network equipment"],solutions:["Use a wired connection instead of Wi-Fi","Close bandwidth-heavy applications","Check network equipment"]},31003:{name:"HIGH_PACKET_LOSS",message:"High packet loss detected",description:"Packet loss exceeded the threshold for multiple consecutive samples. High packet loss causes choppy audio or dropped calls.",causes:["Network congestion","Unstable connection","Firewall or QoS misconfiguration"],solutions:["Check network connectivity","Use a wired connection","Contact network administrator"]},31004:{name:"LOW_MOS",message:"Low call quality score",description:"Mean Opinion Score (MOS) dropped below the acceptable threshold for multiple consecutive samples. This is a composite indicator of overall call quality.",causes:["Combination of high latency, jitter, and/or packet loss","Poor network conditions"],solutions:["Check network connectivity","Use a wired connection","Close bandwidth-heavy applications"]},31005:{name:"LOW_LOCAL_AUDIO",message:"Low local microphone audio detected",description:"Local outbound audio level stayed below the acceptable threshold before the microphone produced real audio, or stayed silent for a long continuous window after audio was confirmed. This may indicate that the microphone is not capturing enough audio even while RTP is being sent.",causes:["Microphone input level is too low","Wrong microphone selected","Microphone is obstructed or too far from the speaker","Operating system input gain is muted or very low"],solutions:["Check the selected microphone","Increase microphone input gain","Move closer to the microphone","Verify the microphone is not muted at the operating system or hardware level"]},32001:{name:"LOW_BYTES_RECEIVED",message:"No audio data received",description:"No bytes have been received from the remote party for multiple consecutive seconds. This may indicate a network interruption or remote-side issue.",causes:["Network interruption","Remote party microphone issue","Firewall blocking inbound media"],solutions:["Check network connectivity","Ask remote party to check their microphone","Check firewall rules for media ports"]},32002:{name:"LOW_BYTES_SENT",message:"No audio data being sent",description:"No bytes have been sent for multiple consecutive seconds. This may indicate a local microphone issue or network interruption.",causes:["Microphone muted or disconnected","Network interruption","Local media track ended"],solutions:["Check that the microphone is not muted","Verify the microphone is still connected","Check network connectivity"]},33001:{name:"ICE_CONNECTIVITY_LOST",message:"Connection interrupted",description:"The ICE connection transitioned to the disconnected state. The previously selected connection path was lost and renegotiation may be required. The connection may recover automatically.",causes:["Temporary network interruption","Network interface change (e.g. Wi-Fi to cellular)","NAT rebinding"],solutions:["Wait for automatic recovery","Check network connectivity"]},33002:{name:"ICE_GATHERING_TIMEOUT",message:"ICE gathering timed out",description:"ICE candidate gathering did not complete within the safety timeout. This is typically caused by network restrictions blocking STUN/TURN. The call may still succeed if candidates arrive late.",causes:["Firewall blocking STUN/TURN","Network unreachable","STUN/TURN server not responding"],solutions:["Check STUN/TURN server reachability","Ensure UDP traffic is not blocked","Try forceRelayCandidate option"]},33003:{name:"ICE_GATHERING_EMPTY",message:"No ICE candidates gathered",description:"No ICE candidates were gathered after sending the initial SDP. This may indicate a firewall blocking all STUN/TURN traffic or no available network interface.",causes:["Firewall blocking all STUN/TURN traffic","No network interface available","VPN blocking UDP"],solutions:["Check STUN/TURN server reachability","Ensure UDP traffic is not blocked","Use forceRelayCandidate option"]},33004:{name:"PEER_CONNECTION_FAILED",message:"Connection failed",description:"RTCPeerConnection entered the failed state. This is a recoverable condition — the SDK may attempt ICE restart or the connection may recover. If it does not recover, the call will eventually be terminated.",causes:["ICE failure","DTLS handshake failure","Prolonged network interruption"],solutions:["Wait for automatic recovery","Check network connectivity","Verify TURN server credentials"]},33005:{name:"ONLY_HOST_ICE_CANDIDATES",message:"Only local network candidates available",description:"ICE gathering completed but only host (local network) candidates were collected — no server-reflexive (srflx) or relay (turn) candidates were found. This typically means the STUN/TURN servers are unreachable, which will prevent connections outside the local network.",causes:["STUN/TURN servers unreachable","Firewall blocking UDP traffic to STUN/TURN servers","Incorrect TURN server configuration or credentials","Restrictive corporate network or VPN"],solutions:["Verify STUN/TURN server URLs and credentials","Ensure UDP traffic to STUN/TURN ports is not blocked","Check firewall or VPN settings","Try using TCP-based TURN as a fallback"]},33006:{name:"ANSWER_WHILE_PEER_ACTIVE",message:"Call answer ignored because a peer connection is already active",description:"answer() was called on a call that already has an active or connecting peer connection. Creating a second peer connection for the same call would duplicate media negotiation, confuse the remote party, and break call reporting. This is typically caused by application code invoking answer() multiple times (e.g. from multiple event handlers).",causes:["Application called answer() twice on the same call object","Multiple click handlers or event listeners triggering answer()"],solutions:["Ensure answer() is called only once per call","Disable the answer button after the first click","Check that answer() is not invoked from multiple event handlers"]},33008:{name:"ICE_CANDIDATE_PAIR_CHANGED",message:"ICE candidate pair changed mid-call",description:"The selected ICE candidate pair changed during an active call. This indicates a network path shift — for example, a Wi-Fi to cellular handoff, a NAT rebinding, or a relay fallback. The call may continue normally, but the path change can briefly affect audio quality.",causes:["Network interface change (e.g. Wi-Fi to cellular)","NAT rebinding or IP address change","Previous candidate pair failed and ICE selected an alternative","Network topology change"],solutions:["Monitor for audio quality degradation after the path change","Check network stability if changes are frequent","Verify TURN server configuration for relay fallback"]},33007:{name:"DUPLICATE_INBOUND_ANSWER",message:"Call answer ignored because another inbound call is already being answered",description:"answer() was called on an inbound call while another inbound call is already answering or active in this JavaScript runtime. Answering both legs can trigger SIP 486 USER_BUSY / LOSE_RACE when duplicate WebSocket registrations receive the same incoming call.",causes:["Multiple TelnyxRTC instances in the same page","Application code recreating a client without disconnecting the previous instance","Duplicate inbound call notifications produced by duplicate WebSocket registrations"],solutions:["Keep a single active TelnyxRTC instance for inbound call handling","Call disconnect() before replacing an SDK client instance","Only call answer() for one inbound call notification at a time"]},34001:{name:"TOKEN_EXPIRING_SOON",message:"Authentication token expiring soon",description:"The authentication token is approaching its expiration time. If the token expires the connection will be lost and calls will fail. A new token should be generated before expiration.",causes:["Token was issued with a limited lifetime"],solutions:["Generate a new authentication token","Reconnect with fresh credentials before the token expires"]},36001:{name:"SIGNALING_HEALTH_PROBE_TIMEOUT",message:"Signaling health probe timed out",description:"A signaling liveness probe (Ping) was sent during an active call but no response was received within the timeout window. This indicates the WebSocket may be half-dead — the browser reports it as OPEN but data is not flowing. The SDK will force-close the socket to trigger reconnection.",causes:["Network interface removed mid-call while another interface remains","TCP connection bound to a removed IP/route became half-dead","Firewall or NAT state expired silently","WebSocket proxy or load balancer dropped the connection without a close frame"],solutions:["The SDK will automatically force-close the socket and reconnect","Check for network interface changes during the call","Verify firewall/NAT timeout settings"]},36002:{name:"SIGNALING_REQUEST_TIMEOUT",message:"Signaling request timed out",description:"A signaling-critical JSON-RPC request (e.g. ICE restart Modify) did not receive a response within the timeout window while the socket reports OPEN. This may indicate the WebSocket is half-dead or the server is unresponsive. The SDK will mark signaling as unhealthy and force reconnection.",causes:["Half-dead WebSocket (browser reports OPEN but data does not flow)","Server unresponsive or overloaded","Network path interruption without TCP RST"],solutions:["The SDK will automatically force-close the socket and reconnect","Check server health and response times","Check for network path issues"]},36003:{name:"SIGNALING_RECOVERY_REQUIRED",message:"Signaling recovery required",description:"The signaling (WebSocket) path has been detected as unhealthy and the SDK will force-close the socket and reconnect. If media was still flowing, the reconnect is delayed briefly to allow the application to notify the user about a short interruption. Active calls will be recovered via reattach after reconnection.",causes:["WebSocket probe timed out with no response","Critical signaling request timed out","Peer/media failure detected while signaling is also unhealthy"],solutions:["The SDK will automatically reconnect and recover the call","Check for network interface changes or interruptions","Verify firewall/NAT timeout settings"]},36004:{name:"MEDIA_RECOVERY_REQUIRED",message:"Media recovery required",description:"The peer connection or media flow has been detected as unhealthy while signaling is healthy. The SDK will attempt ICE restart to recover the media path. No socket reconnection is needed.",causes:["ICE connection state changed to failed","RTCPeerConnection state changed to failed","No RTP packets/bytes received while media should be active"],solutions:["The SDK will automatically attempt ICE restart","Check network connectivity and ICE candidate availability","Verify TURN server configuration"]},35001:{name:"SESSION_NOT_REATTACHED",message:"Active call lost after reconnect",description:"The WebSocket reconnected successfully but the server returned an empty reattached_sessions list while the SDK still has an active call. The server no longer knows about the call, so any subsequent call-control operation (hangup, hold, etc.) will fail with CALL_DOES_NOT_EXIST.",causes:["Server-side session expired during the disconnection window","Reconnect token was invalidated","Backend restarted or lost in-memory call state"],solutions:["Terminate the local call and notify the user","Start a new call","Investigate why the session was not preserved on the server"]}};function le(e,t){const i=de[e];return{code:e,name:i.name,message:t||i.message,description:i.description,causes:[...i.causes],solutions:[...i.solutions]}}class he extends Error{constructor(e){super(e.message||`[${e.code}] ${e.name}`),this.name=e.name,this.code=e.code,this.description=e.description,this.causes=e.causes,this.solutions=e.solutions,this.originalError=e.originalError,Object.setPrototypeOf(this,he.prototype)}toJSON(){return{code:this.code,name:this.name,description:this.description,message:this.message,causes:this.causes,solutions:this.solutions,originalError:this.originalError}}}function ue(e){if(e instanceof DOMException){if("NotAllowedError"===e.name)return m;if("NotFoundError"===e.name||"OverconstrainedError"===e.name)return f}return _}function pe(e,t,i){const n=ce[e],s=t instanceof Error?t:void 0!==t?new Error(String(t)):void 0;return new he({code:e,name:n.name,description:n.description,message:i||n.message,causes:[...n.causes],solutions:[...n.solutions],originalError:s})}class ge extends Error{constructor(e,t,i=""){super(`Signaling request timed out (id=${e}, method=${i||"unknown"}, timeout=${t}ms)`),this.name="RequestTimeoutError",this.requestId=e,this.timeoutMs=t,this.method=i}}class ve extends Error{constructor(e,t,i){super(`Stale request cancelled (id=${e}, gen=${t}, current=${i})`),this.name="StaleRequestError",this.requestId=e,this.staleGeneration=t,this.currentGeneration=i}}var me="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function fe(e,t){return e(t={exports:{}},t.exports),t.exports}var _e=fe((function(e){var t,i;t=me,i=function(){var e=function(){},t="undefined",i=typeof window!==t&&typeof window.navigator!==t&&/Trident\/|MSIE /.test(window.navigator.userAgent),n=["trace","debug","info","warn","error"];function s(e,t){var i=e[t];if("function"==typeof i.bind)return i.bind(e);try{return Function.prototype.bind.call(i,e)}catch(t){return function(){return Function.prototype.apply.apply(i,[e,arguments])}}}function o(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function r(t,i){for(var s=0;s<n.length;s++){var o=n[s];this[o]=s<t?e:this.methodFactory(o,t,i)}this.log=this.debug}function a(e,i,n){return function(){typeof console!==t&&(r.call(this,i,n),this[e].apply(this,arguments))}}function c(n,r,c){return function(n){return"debug"===n&&(n="log"),typeof console!==t&&("trace"===n&&i?o:void 0!==console[n]?s(console,n):void 0!==console.log?s(console,"log"):e)}(n)||a.apply(this,arguments)}function d(e,i,s){var o,a=this;i=null==i?"WARN":i;var d="loglevel";function l(){var e;if(typeof window!==t&&d){try{e=window.localStorage[d]}catch(e){}if(typeof e===t)try{var i=window.document.cookie,n=i.indexOf(encodeURIComponent(d)+"=");-1!==n&&(e=/^([^;]+)/.exec(i.slice(n))[1])}catch(e){}return void 0===a.levels[e]&&(e=void 0),e}}"string"==typeof e?d+=":"+e:"symbol"==typeof e&&(d=void 0),a.name=e,a.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},a.methodFactory=s||c,a.getLevel=function(){return o},a.setLevel=function(i,s){if("string"==typeof i&&void 0!==a.levels[i.toUpperCase()]&&(i=a.levels[i.toUpperCase()]),!("number"==typeof i&&i>=0&&i<=a.levels.SILENT))throw"log.setLevel() called with invalid level: "+i;if(o=i,!1!==s&&function(e){var i=(n[e]||"silent").toUpperCase();if(typeof window!==t&&d){try{return void(window.localStorage[d]=i)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"="+i+";"}catch(e){}}}(i),r.call(a,i,e),typeof console===t&&i<a.levels.SILENT)return"No console available for logging"},a.setDefaultLevel=function(e){i=e,l()||a.setLevel(e,!1)},a.resetLevel=function(){a.setLevel(i,!1),function(){if(typeof window!==t&&d){try{return void window.localStorage.removeItem(d)}catch(e){}try{window.document.cookie=encodeURIComponent(d)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch(e){}}}()},a.enableAll=function(e){a.setLevel(a.levels.TRACE,e)},a.disableAll=function(e){a.setLevel(a.levels.SILENT,e)};var h=l();null==h&&(h=i),a.setLevel(h,!1)}var l=new d,h={};l.getLogger=function(e){if("symbol"!=typeof e&&"string"!=typeof e||""===e)throw new TypeError("You must supply a name when creating a logger.");var t=h[e];return t||(t=h[e]=new d(e,l.getLevel(),l.methodFactory)),t};var u=typeof window!==t?window.log:void 0;return l.noConflict=function(){return typeof window!==t&&window.log===l&&(window.log=u),l},l.getLoggers=function(){return h},l.default=l,l},e.exports?e.exports=i():t.log=i()}));const Se={debug:0,info:1,warn:2,error:3};class be{constructor(e={}){var t,i,n;this.buffer=[],this.isCapturing=!1,this.options={enabled:null!==(t=e.enabled)&&void 0!==t&&t,level:null!==(i=e.level)&&void 0!==i?i:"debug",maxEntries:null!==(n=e.maxEntries)&&void 0!==n?n:1e3}}start(){this.options.enabled&&(this.isCapturing=!0,this.buffer=[])}stop(){this.isCapturing=!1}addEntry(e,t,i){if(!this.isCapturing||!this.options.enabled)return;if(Se[e]<Se[this.options.level])return;const n=Object.assign({timestamp:(new Date).toISOString(),level:e,message:t},i&&Object.keys(i).length>0?{context:i}:{});this.buffer.push(n),this.buffer.length>this.options.maxEntries&&this.buffer.shift()}getLogs(){return[...this.buffer]}getLogCount(){return this.buffer.length}drain(){const e=this.buffer;return this.buffer=[],e}clear(){this.buffer=[]}isActive(){return this.isCapturing}isEnabled(){return this.options.enabled}}let ye=null;const Ie=_e.getLogger("telnyx"),Ee={trace:0,debug:1,info:2,warn:3,error:4};let Ce=Ee.info;function we(e){if(null==e)return e;if("object"!=typeof e)return e;try{const t=JSON.stringify(e),i=JSON.parse(t);if("object"==typeof i&&null!==i&&Object.keys(i).length>1)return i}catch(e){}const t={};for(const i in e)try{const n=e[i];if("function"==typeof n)continue;if("object"==typeof n&&null!==n)try{t[i]=JSON.parse(JSON.stringify(n))}catch(e){t[i]=String(n)}else t[i]=n}catch(e){}return Object.keys(t).length>0?t:{value:String(e)}}const Te=Ie.methodFactory;Ie.methodFactory=(e,t,i)=>{const n=Te(e,t,i);return function(...t){if(Ee[e]>=Ce){const e=[(new Date).toISOString().replace("T"," ").replace("Z",""),"-"];for(const i of t)e.push(i);n(...e)}const i=ye;if(null==i?void 0:i.isActive()){const[n,...s]=t,o="string"==typeof n?n:JSON.stringify(n);let r;s.length>0&&(r=1===s.length&&"object"==typeof s[0]&&null!==s[0]?we(s[0]):{args:s.map(we)}),i.addEntry(e,o,r)}}},Ie.setLevel("debug",!1);const ke=e=>{const[t,i,n,s,o,r]=e;let a={};try{a=JSON.parse(o.replace(/ID"/g,'Id"'))}catch(e){Ie.warn("Verto LA invalid media JSON string:",o)}return{participantId:Number(t),participantNumber:i,participantName:n,codec:s,media:a,participantData:r}},Re=e=>{if("string"!=typeof e)return e;try{return JSON.parse(e)}catch(t){return e}},Ae=e=>e instanceof Function||"function"==typeof e,Oe=e=>"object"==typeof document&&"getElementById"in document?"string"==typeof e?document.getElementById(e)||null:"function"==typeof e?e():e instanceof HTMLMediaElement?e:null:null,Ne=/^(ws|wss):\/\//,Le=(e,t=null)=>{const{result:i={},error:n}=e;if(n)return{error:n};const{result:s=null}=i;if(null===s)return null!==t&&(i.node_id=t),{result:i};const{code:o=null,node_id:r=null,result:a=null}=s;return o&&"200"!==o?{error:s}:a?Le(a,r):{result:s}},De=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),Pe=({login:e,passwd:t,password:i,login_token:n})=>Boolean(e&&(t||i)||n),Me=({anonymous_login:e})=>Boolean(e)&&Boolean(e.target_id)&&Boolean(e.target_type),xe=e=>{var t,i,n,s,o,r;let a="",c="";(null===(i=null===(t=null==e?void 0:e.result)||void 0===t?void 0:t.params)||void 0===i?void 0:i.state)&&(a=null===(s=null===(n=null==e?void 0:e.result)||void 0===n?void 0:n.params)||void 0===s?void 0:s.state),(null===(o=null==e?void 0:e.params)||void 0===o?void 0:o.state)&&(c=null===(r=null==e?void 0:e.params)||void 0===r?void 0:r.state);return a||c};function Ue({debounceTime:e}){let t,i;return{promise:new Promise(((n,s)=>{t=e?Fe(n,e):n,i=s})),resolve:t,reject:i}}const Fe=(e,t)=>{let i;return(...n)=>{clearTimeout(i),i=window.setTimeout((()=>{e(...n)}),t)}},$e="telnyx-voice-sdk-id";function je(){return sessionStorage.getItem($e)}function He(){sessionStorage.removeItem($e)}var Ge,We,Be;"undefined"!=typeof window&&window.addEventListener("beforeunload",(()=>{He()})),function(e){e.Offer="offer",e.Answer="answer"}(Ge||(Ge={})),function(e){e.Inbound="inbound",e.Outbound="outbound"}(We||(We={})),function(e){e.Invite="telnyx_rtc.invite",e.Attach="telnyx_rtc.attach",e.Answer="telnyx_rtc.answer",e.Info="telnyx_rtc.info",e.Candidate="telnyx_rtc.candidate",e.EndOfCandidates="telnyx_rtc.endOfCandidates",e.Display="telnyx_rtc.display",e.Media="telnyx_rtc.media",e.Event="telnyx_rtc.event",e.Bye="telnyx_rtc.bye",e.Punt="telnyx_rtc.punt",e.Broadcast="telnyx_rtc.broadcast",e.Subscribe="telnyx_rtc.subscribe",e.Unsubscribe="telnyx_rtc.unsubscribe",e.ClientReady="telnyx_rtc.clientReady",e.Modify="telnyx_rtc.modify",e.Ringing="telnyx_rtc.ringing",e.GatewayState="telnyx_rtc.gatewayState",e.Ping="telnyx_rtc.ping",e.Pong="telnyx_rtc.pong"}(Be||(Be={}));const Ve={generic:"event",[Be.Display]:"participantData",[Be.Attach]:"participantData",conferenceUpdate:"conferenceUpdate",callUpdate:"callUpdate",vertoClientReady:"vertoClientReady",userMediaError:"userMediaError",peerConnectionFailureError:"peerConnectionFailureError",signalingStateClosed:"signalingStateClosed"},qe={invalidCredentialsOptions:"InvalidCredentialsOptions"},Ye={destinationNumber:"",remoteCallerName:"Outbound Call",remoteCallerNumber:"",callerName:"",callerNumber:"",audio:!0,useStereo:!1,debug:!1,debugOutput:"socket",attach:!1,screenShare:!1,userVariables:{},mediaSettings:{useSdpASBandwidthKbps:!1,sdpASBandwidthKbps:0},mutedMicOnStart:!1,prefetchIceCandidates:!0};var Ke,Je,Qe,Xe,ze,Ze;!function(e){e[e.New=0]="New",e[e.Requesting=1]="Requesting",e[e.Trying=2]="Trying",e[e.Recovering=3]="Recovering",e[e.Ringing=4]="Ringing",e[e.Answering=5]="Answering",e[e.Early=6]="Early",e[e.Active=7]="Active",e[e.Held=8]="Held",e[e.Hangup=9]="Hangup",e[e.Destroy=10]="Destroy",e[e.Purge=11]="Purge"}(Ke||(Ke={})),function(e){e.Participant="participant",e.Moderator="moderator"}(Je||(Je={})),function(e){e.Join="join",e.Leave="leave",e.Bootstrap="bootstrap",e.Add="add",e.Modify="modify",e.Delete="delete",e.Clear="clear",e.ChatMessage="chatMessage",e.LayerInfo="layerInfo",e.LogoInfo="logoInfo",e.LayoutInfo="layoutInfo",e.LayoutList="layoutList",e.ModCmdResponse="modCommandResponse"}(Qe||(Qe={})),function(e){e.Video="videoinput",e.AudioIn="audioinput",e.AudioOut="audiooutput"}(Xe||(Xe={})),function(e){e.REGED="REGED",e.UNREGED="UNREGED",e.NOREG="NOREG",e.FAILED="FAILED",e.FAIL_WAIT="FAIL_WAIT",e.REGISTER="REGISTER",e.TRYING="TRYING",e.EXPIRED="EXPIRED",e.UNREGISTER="UNREGISTER"}(ze||(ze={})),function(e){e.Hold="hold",e.Unhold="unhold",e.ToggleHold="toggleHold",e.UpdateMedia="updateMedia"}(Ze||(Ze={}));const et="GLOBAL",tt={},it=(e,t)=>`${e}|${t}`,nt=(e,t=et)=>it(e,t)in tt,st=(e,t,i=et)=>{const n=it(e,i);n in tt||(tt[n]=[]),tt[n].push(t)},ot=(e,t,i=et)=>{const n=function(s){rt(e,n,i),t(s)};return n.prototype.targetRef=t,st(e,n,i)},rt=(e,t,i=et)=>{if(!nt(e,i))return!1;const n=it(e,i);if(Ae(t)){for(let e=tt[n].length-1;e>=0;e--){const i=tt[n][e];(t===i||i.prototype&&t===i.prototype.targetRef)&&tt[n].splice(e,1)}}else tt[n]=[];return 0===tt[n].length&&delete tt[n],!0},at=(e,t,i=et,n=!0)=>{const s=n&&i!==et;if(!nt(e,i))return s&&at(e,t),!1;const o=it(e,i),r=tt[o].length;if(!r)return s&&at(e,t),!1;for(let e=r-1;e>=0;e--)tt[o][e](t);return s&&at(e,t),!0},ct=e=>{const t=it(e,"");Object.keys(tt).filter((e=>0===e.indexOf(t))).forEach((e=>delete tt[e]))};let dt="undefined"!=typeof WebSocket?WebSocket:null;const lt=0,ht=1,ut=2,pt=3;class gt{constructor(e){this.session=e,this.previousGatewayState="",this.lastInboundAt=0,this.socketGeneration=0,this._wsClient=null,this._host=ie,this._timers={},this._useCanaryRtcServer=!1,this._hasCanaryBeenUsed=!1,this._safetyTimeoutId=null,this._pendingRequestIds=new Set,this._pendingRequestTimers=new Map,this._pendingRequestRejecters=new Map,this.upDur=null,this.downDur=null;const{host:t,env:i,region:n,useCanaryRtcServer:s}=e.options;i&&(this._host="development"===i?"wss://rtcdev.telnyx.com":ie),t&&(this._host=(e=>`${Ne.test(e)?"":"wss://"}${e}`)(t)),n&&(this._host=this._host.replace(/rtc(dev)?/,`${n}.rtc$1`)),s&&(this._useCanaryRtcServer=!0)}get connected(){return!!this._wsClient&&this._wsClient.readyState===ht}get connecting(){return!!this._wsClient&&this._wsClient.readyState===lt}get closing(){return!!this._wsClient&&this._wsClient.readyState===ut}get closed(){return!!this._wsClient&&this._wsClient.readyState===pt}get isAlive(){return this.connecting||this.connected}get isDead(){return this.closing||this.closed}get host(){return this._host}connect(){const t=new URL(this._host);let i=je();this.session.options.rtcIp&&this.session.options.rtcPort&&(i=null,this._useCanaryRtcServer=!1,t.searchParams.set("rtc_ip",this.session.options.rtcIp),t.searchParams.set("rtc_port",this.session.options.rtcPort.toString())),i&&t.searchParams.set("voice_sdk_id",i),this._useCanaryRtcServer&&(t.searchParams.set("canary","true"),i&&!this._hasCanaryBeenUsed&&(t.searchParams.delete("voice_sdk_id"),Ie.debug("first canary connection. Refreshing voice_sdk_id")),this._hasCanaryBeenUsed=!0),this.session.callReportVoiceSdkId=t.searchParams.get("voice_sdk_id"),this.session.options.skipLastVoiceSdkId&&t.searchParams.has("voice_sdk_id")&&t.searchParams.set("skip_last_voice_sdk_id","true");try{const e=this.socketGeneration;this._wsClient=new dt(t.toString()),this.socketGeneration+=1,Ie.debug("WebSocket connection created",{sessionId:this.session.sessionid,voiceSdkId:this.session.callReportVoiceSdkId,socketGeneration:this.socketGeneration,reconnectCount:e}),this.lastInboundAt=0,this._cleanupPendingRequests(),this._registerSocketEvents(this._wsClient)}catch(t){Ie.error("WebSocket connection failed:",t);const i=pe(C,t);at(e.SwEvent.Error,{error:i,sessionId:this.session.sessionid},this.session.uuid)}}sendRawText(e){var t;null===(t=this._wsClient)||void 0===t||t.send(e)}send(e,t){var i;const{request:n}=e,s=this.socketGeneration,o=n.method||"",r=new Promise(((e,i)=>{if(n.hasOwnProperty("result"))return e();let r=!1,a=null;const c=t=>{if(null!==a&&(clearTimeout(a),this._pendingRequestTimers.delete(n.id),a=null),this._pendingRequestIds.delete(n.id),this._pendingRequestRejecters.delete(n.id),r)return;const{result:s,error:o}=Le(t);return o?i(o):e(s)};ot(n.id,c),this._pendingRequestIds.add(n.id),this._pendingRequestRejecters.set(n.id,{reject:i,generation:s}),t&&t>0&&(a=setTimeout((()=>{if(r=!0,a=null,this._pendingRequestTimers.delete(n.id),this._pendingRequestRejecters.delete(n.id),rt(n.id,c),this._pendingRequestIds.delete(n.id),this.socketGeneration!==s)return Ie.debug(`Stale request timeout for ${n.id} (gen ${s}, current ${this.socketGeneration}) — settling with StaleRequestError`),void i(new ve(n.id,s,this.socketGeneration));i(new ge(n.id,t,o))}),t),this._pendingRequestTimers.set(n.id,a))}));return Ie.debug("SEND: \n",JSON.stringify(n,null,2),"\n"),null===(i=this._wsClient)||void 0===i||i.send(JSON.stringify(n)),r}close(){if(!this._wsClient||this.closing)return;this._cleanupPendingRequests();const e=this._wsClient;Ae(this._wsClient._beginClose)?this._wsClient._beginClose():this._wsClient.close(),this._safetyTimeoutId||(this._safetyTimeoutId=setTimeout((()=>this._handleCloseTimeout(e)),5e3))}_registerSocketEvents(t){t.onopen=t=>at(e.SwEvent.SocketOpen,t,this.session.uuid),t.onclose=i=>(this._clearSafetyTimeout(),this._safetyCleanupSocket(t,"close"),at(e.SwEvent.SocketClose,i,this.session.uuid)),t.onerror=i=>{this._clearSafetyTimeout(),this._safetyCleanupSocket(t,"error");const n=pe(w);return at(e.SwEvent.Error,{error:n,sessionId:this.session.sessionid},this.session.uuid),at(e.SwEvent.SocketError,{error:i,sessionId:this.session.sessionid},this.session.uuid)},t.onmessage=t=>{var i,n;this.lastInboundAt=Date.now(),at(e.SwEvent.SocketActivity,{timestamp:this.lastInboundAt},this.session.uuid);const s=Re(t.data);var o;if("string"!=typeof s){if(s.voice_sdk_id&&(this.session.callReportVoiceSdkId=s.voice_sdk_id,o=s.voice_sdk_id,sessionStorage.setItem($e,o)),this._unsetTimer(s.id),Ie.debug("RECV: \n",JSON.stringify(s,null,2),"\n"),ze[`${null===(n=null===(i=null==s?void 0:s.result)||void 0===i?void 0:i.params)||void 0===n?void 0:n.state}`]||!at(s.id,s)){const t=xe(s);at(e.SwEvent.SocketMessage,s,this.session.uuid),Boolean(t)&&(this.previousGatewayState=t)}}else this._handleStringResponse(s)}}_deregisterSocketEvents(e){e.onopen=null,e.onclose=null,e.onerror=null,e.onmessage=null}_handleCloseTimeout(t){this._safetyTimeoutId=null,t&&t.readyState!==pt?(Ie.warn("Socket stuck in CLOSING after 5s — forcefully cleaning up"),this._deregisterSocketEvents(t),this._safetyCleanupSocket(t,"timeout"),this._wsClient&&this._wsClient!==t||at(e.SwEvent.SocketClose,{code:ne.ABNORMAL_CLOSURE,reason:"STUCK_WS_TIMEOUT: Socket got stuck in CLOSING state and was forcefully cleaned up by safety timeout",wasClean:!1},this.session.uuid)):Ie.warn("Safety timeout fired but socket is already closed or cleaned up")}_clearSafetyTimeout(){this._safetyTimeoutId&&(Ie.debug("Clearing safety timeout"),clearTimeout(this._safetyTimeoutId),this._safetyTimeoutId=null)}_safetyCleanupSocket(e,t){this._wsClient===e?(Ie.debug(`Nulling socket reference (reason: ${t})`),this._wsClient=null):Ie.debug(`Skipping socket cleanup - old socket already replaced (reason: ${t})`)}_cleanupPendingRequests(){Array.from(this._pendingRequestIds).forEach((e=>{rt(e);const t=this._pendingRequestTimers.get(e),i=this._pendingRequestRejecters.get(e);t&&(clearTimeout(t),this._pendingRequestTimers.delete(e),i&&i.reject(new ve(e,i.generation,this.socketGeneration))),this._pendingRequestRejecters.delete(e)})),this._pendingRequestIds.clear()}_unsetTimer(e){clearTimeout(this._timers[e]),delete this._timers[e]}_handleStringResponse(t){if(/^#SP/.test(t))switch(t[3]){case"U":this.upDur=parseInt(t.substring(4));break;case"D":this.downDur=parseInt(t.substring(4)),at(e.SwEvent.SpeedTest,{upDur:this.upDur,downDur:this.downDur},this.session.uuid)}else Ie.warn("Unknown message from socket",t)}}gt.DEFAULT_REQUEST_TIMEOUT_MS=1e4;class vt{buildRequest(e){this.request=Object.assign({jsonrpc:"2.0",id:c()},e)}}const mt={id:"callID",destinationNumber:"destination_number",remoteCallerName:"remote_caller_id_name",remoteCallerNumber:"remote_caller_id_number",callerName:"caller_id_name",callerNumber:"caller_id_number",customHeaders:"custom_headers"};class ft extends vt{constructor(e={}){if(super(),e.hasOwnProperty("dialogParams")){const i=t(e.dialogParams,["remoteSdp","localStream","remoteStream","localElement","remoteElement","onNotification","camId","micId","speakerId"]);for(const e in mt)e&&i.hasOwnProperty(e)&&(i[mt[e]]=i[e],delete i[e]);e.dialogParams=i}this.buildRequest({method:this.toString(),params:e})}}class _t extends ft{constructor(e){super(),this.method=Be.GatewayState;this.buildRequest({method:this.method,voice_sdk_id:e,params:{}})}}class St{constructor(e){this.pendingRequestId=null,this.onSocketMessage=e=>i(this,void 0,void 0,(function*(){e.id===this.pendingRequestId&&this.gatewayStateTask.resolve(xe(e))})),this.getIsRegistered=()=>i(this,void 0,void 0,(function*(){const e=new _t(je());this.pendingRequestId=e.request.id,this.gatewayStateTask=Ue({}),this.session.execute(e);const t=yield this.gatewayStateTask.promise;return!!t&&[ze.REGISTER,ze.REGED].includes(t)})),this.session=e,this.gatewayStateTask=Ue({}),this.session.on("telnyx.socket.message",this.onSocketMessage)}}class bt extends ft{constructor(e){super(),this.method=Be.Ping;this.buildRequest({method:this.method,voice_sdk_id:e,params:{}})}}class yt{constructor(e){this._session=e,this._lastInboundAt=0,this._lastProbeSentAt=0,this._probeInFlight=!1,this._intervalId=null,this._pendingMediaRecovery=null}static isCriticalMethod(e){return yt.CRITICAL_METHODS.has(e)}start(){this._intervalId||(Ie.debug("Signaling health: monitor started"),this._lastInboundAt=Date.now(),this._probeInFlight=!1,this._lastProbeSentAt=0,this._intervalId=setInterval((()=>this._check()),3e3))}stop(){this._intervalId&&(clearInterval(this._intervalId),this._intervalId=null,this._probeInFlight=!1,this._lastProbeSentAt=0),this._pendingMediaRecovery=null,Ie.debug("Signaling health: monitor stopped")}get isRunning(){return null!==this._intervalId}get isProbeInFlight(){return this._probeInFlight}onSocketActivity(){this._lastInboundAt=Date.now()}_resolveProbe(){if(!this._probeInFlight)return;if(this._probeInFlight=!1,this._lastProbeSentAt=0,this._lastInboundAt=Date.now(),Ie.debug("Signaling health: probe resolved by matching Ping response"),!this._pendingMediaRecovery)return;const e=this._pendingMediaRecovery;this._pendingMediaRecovery=null,"healthy"===this._getSignalingHealthState()&&(Ie.info(`Signaling health: signaling probe resolved, triggering pending ICE restart for call ${e.callId}`),this._triggerIceRestart(e.callId,e.reason,e.source))}_probeIfNeeded(e){var t;(null===(t=this._session.connection)||void 0===t?void 0:t.connected)&&(this._probeInFlight?Ie.debug(`Signaling health: probe already in flight, skipping duplicate probe (${e})`):(Ie.info(`Signaling health: ${e}, sending signaling probe`),this._sendProbe()))}onRequestTimeout(e,t,i=""){var n;if(!(null===(n=this._session.connection)||void 0===n?void 0:n.connected))return;yt.isCriticalMethod(i)?(Ie.warn(`Critical signaling request timed out (id=${e}, method=${i}, timeout=${t}ms) — declaring signaling unhealthy`),this._triggerSignalingRecovery(`Critical signaling request timed out (method=${i}, id=${e}, timeout=${t}ms)`,"request")):Ie.warn(`Non-critical signaling request timed out (id=${e}, method=${i||"unknown"}, timeout=${t}ms) — logging but not triggering signaling recovery`)}onPeerFailure(e,t){Ie.warn(`Signaling health: peer failure reported (callId=${e}, evidence=${t})`),this._recoverMediaOrSignaling(e,`Peer connection failure (${t})`,`Peer failure detected (${t}) while signaling is unhealthy`,"peer_failure")}onNoRtp(e,t){Ie.warn(`Signaling health: no RTP detected (callId=${e}, direction=${t})`),this._recoverMediaOrSignaling(e,`No RTP ${t} while media should be active`,`No RTP ${t} while signaling is unhealthy`,"no_rtp")}_check(){var e;if(!(null===(e=this._session.connection)||void 0===e?void 0:e.connected))return;const t=Date.now(),i=t-this._lastInboundAt;if(i<12e3)return;if(!this._probeInFlight)return Ie.info(`Signaling health: no inbound WS activity for ${Math.round(i/1e3)}s during active call, sending health probe`),void this._sendProbe();const n=t-this._lastProbeSentAt;n>=5e3&&(Ie.warn(`Signaling health: probe timed out after ${n}ms with no inbound activity — declaring signaling unhealthy`),this._triggerSignalingRecovery("Signaling health probe timed out: no inbound WS activity after probe","probe"))}_sendProbe(){var e;this._probeInFlight=!0,this._lastProbeSentAt=Date.now();const t=new bt(je());null===(e=this._session.connection)||void 0===e||e.send(t).then((()=>this._resolveProbe())).catch((e=>{Ie.warn("Signaling health: probe Ping failed to send",e)}))}_recoverMediaOrSignaling(e,t,i,n){if(!this._session.hasActiveCall())return void Ie.debug(`Signaling health: ignoring ${n} recovery without an active call`);const s=this._getSignalingHealthState();return"healthy"===s?(Ie.info(`Signaling health: signaling is healthy, triggering ICE restart for call ${e}`),void this._triggerIceRestart(e,t,n)):"unknown"===s?(Ie.info(`Signaling health: signaling health is unknown, deferring ICE restart decision for call ${e}`),this._pendingMediaRecovery={callId:e,reason:t,source:n},void this._probeIfNeeded(`${n} detected with stale/unknown signaling`)):(Ie.info("Signaling health: signaling is unhealthy, triggering socket reconnect instead of ICE restart"),void this._triggerSignalingRecovery(i,n))}_getSignalingHealthState(){var e;if(!(null===(e=this._session.connection)||void 0===e?void 0:e.connected))return"unhealthy";if(this._probeInFlight)return"unknown";const t=Date.now(),i=this._lastInboundAt||this._session.connection.lastInboundAt||0;return 0===i?"unknown":t-i<3e3?"healthy":"unknown"}_triggerSignalingRecovery(t,i="probe"){var n;let s;this._pendingMediaRecovery=null,this._probeInFlight=!1,this._lastProbeSentAt=0,s="probe"===i?X:"request"===i?z:Z;const o=le(s);at(e.SwEvent.Warning,{warning:o,reason:t,source:i,sessionId:this._session.sessionid},this._session.uuid),(null===(n=this._session.connection)||void 0===n?void 0:n.connected)&&(Ie.info("Signaling health: force-closing WebSocket to trigger reconnect"),this._session.socketDisconnect())}_triggerIceRestart(t,i,n){Ie.info(`Signaling health: triggering ICE restart for call ${t}`);const s=this._session.triggerIceRestart(t);if(!s.started)return Ie.info(`Signaling health: ICE restart not started for call ${t}: ${s.reason}`),void(s.recoverSignaling&&this._triggerSignalingRecovery(`${i}; ICE restart disabled for reattached call, forcing socket reconnect`,n));const o=le(ee);at(e.SwEvent.Warning,{warning:o,reason:i,callId:t,sessionId:this._session.sessionid},this._session.uuid)}}yt.CRITICAL_METHODS=new Set([Be.Modify,Be.Bye,Be.Ping]);var It="2.27.1-beta.0",Et=It;class Ct extends ft{constructor(e,t,i,n,s={},o){super(),this.method="login";const r={login:e,passwd:t,login_token:i,userVariables:s,reconnection:o,loginParams:{},"User-Agent":{sdkVersion:Et,data:navigator.userAgent}};n&&(r.sessid=n),this.buildRequest({method:this.method,params:r})}}class wt extends ft{constructor(e,t){super(),this.buildRequest({id:e,result:{method:t}})}}class Tt extends ft{toString(){return Be.Invite}}class kt extends ft{toString(){return Be.Answer}}class Rt extends ft{toString(){return Be.Attach}}class At extends ft{toString(){return Be.Bye}}class Ot extends ft{toString(){return Be.Candidate}}class Nt extends ft{toString(){return Be.EndOfCandidates}}class Lt extends ft{toString(){return Be.Modify}}class Dt extends ft{toString(){return Be.Info}}class Pt extends ft{toString(){return Be.Broadcast}}class Mt extends ft{toString(){return Be.Subscribe}}class xt extends ft{toString(){return Be.Unsubscribe}}class Ut extends ft{constructor(e){super(),this.method="anonymous_login";const{target_type:t,target_id:i,target_version_id:n,target_params:s,userVariables:o,sessionId:r,reconnection:a}=e,c={target_type:t,target_id:i,userVariables:o,reconnection:a,"User-Agent":{sdkVersion:Et,data:navigator.userAgent}};r&&(c.sessid=r),n&&(c.target_version_id=n),s&&(c.target_params=s),this.buildRequest({method:this.method,params:c})}}class Ft{constructor(e){if(this.options=e,this.uuid=c(),this.sessionid="",this.subscriptions={},this.signature=null,this.relayProtocol=null,this.contexts=[],this.timeoutErrorCode=-32e3,this.invalidMethodErrorCode=-32601,this.authenticationRequiredErrorCode=-32e3,this.callReportId=null,this.callReportVoiceSdkId=null,this.dc=null,this.region=null,this.connection=null,this._jwtAuth=!1,this._autoReconnect=!0,this._idle=!1,this._reconnectAttempts=0,this._tokenExpiryTimeout=null,this._pendingCallReportUploads=new Set,this._signalingHealthMonitor=new yt(this),this._executeQueue=[],!this.validateOptions())throw new Error("Invalid init options");var t,i;t=e.debug?"debug":"info",Ce=null!==(i=Ee[t])&&void 0!==i?i:Ee.info,this._onSocketOpen=this._onSocketOpen.bind(this),this.onNetworkClose=this.onNetworkClose.bind(this),this._onSocketMessage=this._onSocketMessage.bind(this),this._handleLoginError=this._handleLoginError.bind(this),this._onSocketActivity=this._onSocketActivity.bind(this),this._attachListeners(),this.connection=new gt(this),this.registerAgent=new St(this)}get __logger(){return Ie}get connected(){return this.connection&&this.connection.connected}getIsRegistered(){return i(this,void 0,void 0,(function*(){return this.registerAgent.getIsRegistered()}))}get reconnectDelay(){return 1e3*De(2,6)}execute(t){var n;if(this._idle)return new Promise((e=>this._executeQueue.push({resolve:e,msg:t})));if(!this.connected)return new Promise((e=>{this._executeQueue.push({resolve:e,msg:t}),Ie.debug("Calling connect from execute since not currently connected."),this.connect()}));const s=yt.isCriticalMethod((null===(n=t.request)||void 0===n?void 0:n.method)||"")?gt.DEFAULT_REQUEST_TIMEOUT_MS:void 0;return(null!=s?this.connection.send(t,s):this.connection.send(t)).catch((t=>i(this,void 0,void 0,(function*(){if(t instanceof ve)throw Ie.debug(`Stale request settled (id=${t.requestId}, gen=${t.staleGeneration}) — not triggering recovery`),t;if("RequestTimeoutError"===(null==t?void 0:t.name))throw this.onSignalingRequestTimeout(t.requestId,t.timeoutMs,t.method),t;if((null==t?void 0:t.code)===this.authenticationRequiredErrorCode){if(!this._autoReconnect){const i=pe(O,t);at(e.SwEvent.Error,{error:i,sessionId:this.sessionid},this.uuid)}yield this.login()}throw t}))))}executeRaw(e){this._idle?this._executeQueue.push({msg:e}):this.connection.sendRawText(e)}trackCallReportUpload(e){this._pendingCallReportUploads.add(e),e.then((()=>this._pendingCallReportUploads.delete(e)),(()=>this._pendingCallReportUploads.delete(e)))}_drainCallReportUploads(){return i(this,void 0,void 0,(function*(){if(0===this._pendingCallReportUploads.size)return;const e=Array.from(this._pendingCallReportUploads);let t=!1;yield Promise.race([Promise.all(e.map((e=>e.catch((()=>{}))))),new Promise((e=>setTimeout((()=>{t=!0,e()}),Ft.CALL_REPORT_UPLOAD_DRAIN_TIMEOUT_MS)))]),t&&Ie.warn("Timed out waiting for pending call report uploads",{pendingCount:this._pendingCallReportUploads.size})}))}validateOptions(){return Pe(this.options)||Me(this.options)}broadcast(e){}disconnect(){return i(this,void 0,void 0,(function*(){clearTimeout(this._reconnectTimeout),this._clearTokenExpiryTimeout(),this.subscriptions={},this._autoReconnect=!1,this._reconnectAttempts=0,this.relayProtocol=null,yield this._drainCallReportUploads(),this._closeConnection(),yield sessionStorage.removeItem(this.signature),this._executeQueue=[],this._detachListeners(),Ie.debug("Session disconnected. Cleaned up all listeners and subscriptions, closed connection, disabled auto-reconnect.")}))}on(e,t){return st(e,t,this.uuid),this}off(e,t){return rt(e,t,this.uuid),this}connect(){return i(this,void 0,void 0,(function*(){this.connection||(Ie.debug("No existing connection found, creating a new one."),this.connection=new gt(this)),this._attachListeners(),this._autoReconnect||(this._reconnectAttempts=0),this._autoReconnect=!0,this.connection.isAlive||(Ie.debug("Initiating connection to the server..."),this.connection.connect()),Ie.debug("Connect method called. Connection initiated.")}))}resetReconnectAttempts(){this._reconnectAttempts=0}_handleLoginError(t){const i=pe(R,t);at(e.SwEvent.Error,{error:i,sessionId:this.sessionid},this.uuid)}clearReconnectToken(){He()}_checkTokenExpiry(){this._clearTokenExpiryTimeout();const e=this.options.login_token;if(e&&"string"==typeof e)try{const t=e.split(".");if(3!==t.length)return;const i=JSON.parse(atob(t[1])).exp;if("number"!=typeof i)return;const n=i-Math.floor(Date.now()/1e3);if(n<=0)return;if(n<=Ft.TOKEN_EXPIRY_WARNING_SECONDS)this._emitTokenExpiryWarning();else{const e=1e3*(n-Ft.TOKEN_EXPIRY_WARNING_SECONDS);this._tokenExpiryTimeout=setTimeout((()=>{this._emitTokenExpiryWarning()}),e)}}catch(e){Ie.debug("login_token is not a decodable JWT, skipping expiry check")}}_emitTokenExpiryWarning(){const t=le(J);at(e.SwEvent.Warning,{warning:t,sessionId:this.sessionid},this.uuid)}_clearTokenExpiryTimeout(){null!==this._tokenExpiryTimeout&&(clearTimeout(this._tokenExpiryTimeout),this._tokenExpiryTimeout=null)}login({creds:t,onSuccess:n,onError:s}={}){return i(this,void 0,void 0,(function*(){if(this.connection&&this.connection.isAlive){if(t&&(void 0!==t.login&&(this.options.login=t.login),void 0!==t.password&&(this.options.password=t.password),void 0!==t.passwd&&(this.options.passwd=t.passwd),void 0!==t.login_token&&(this.options.login_token=t.login_token),void 0!==t.userVariables&&(this.options.userVariables=t.userVariables),void 0!==t.anonymous_login&&(this.options.anonymous_login=t.anonymous_login)),Pe(this.options))return this._login({type:"login",onSuccess:n,onError:s});if(Me(this.options))return this._login({type:"anonymous_login",onSuccess:n,onError:s});{const t="Invalid login options provided for authentication.";Ie.error(t);const i=pe(A,void 0,t);return void at(e.SwEvent.Error,{error:i,type:qe.invalidCredentialsOptions,sessionId:this.sessionid},this.uuid)}}}))}_login({type:e,onSuccess:t,onError:n}){return i(this,void 0,void 0,(function*(){let i;i="login"===e?new Ct(this.options.login,this.options.password||this.options.passwd,this.options.login_token,this.sessionid,this.options.userVariables,!!je()):new Ut({target_id:this.options.anonymous_login.target_id,target_type:this.options.anonymous_login.target_type,target_version_id:this.options.anonymous_login.target_version_id,target_params:this.options.anonymous_login.target_params,sessionId:this.sessionid,userVariables:this.options.userVariables,reconnection:!!je()});const s=yield this.execute(i).catch((e=>{this._handleLoginError(e),n&&n(e)}));s&&(this.sessionid=s.sessid,this._checkTokenExpiry(),t&&t())}))}_onSocketOpen(){return i(this,void 0,void 0,(function*(){this.startSignalingHealthMonitor()}))}_flushIntermediateCallReports(e){const t=this.calls;t&&Object.values(t).forEach((t=>{if(null==t?void 0:t.flushIntermediateCallReport)try{t.flushIntermediateCallReport(e)}catch(i){Ie.error("Failed to flush intermediate call report",{callId:t.id,flushReason:e,error:i})}}))}_getSocketCloseCodeName(e){if(void 0===e)return;const t=Object.entries(ne).find((([,t])=>t===e));return null==t?void 0:t[0]}_getSocketCloseError(e){if(e)return e instanceof Error?e.message:String(e)}_createSocketCloseFlushReason(e){return{type:(null==e?void 0:e.error)?"socket-error":"socket-close",socketClose:{code:null==e?void 0:e.code,codeName:this._getSocketCloseCodeName(null==e?void 0:e.code),reason:null==e?void 0:e.reason,wasClean:null==e?void 0:e.wasClean,error:this._getSocketCloseError(null==e?void 0:e.error)}}}onNetworkClose(t){var i;this._flushIntermediateCallReports(this._createSocketCloseFlushReason(t)),this.relayProtocol&&ct(this.relayProtocol);for(const e in this.subscriptions)ct(e);if(this.subscriptions={},this.contexts=[],clearTimeout(this._keepAliveTimeout),clearTimeout(this._reconnectTimeout),this.stopSignalingHealthMonitor(),this.connection&&(this.connection.previousGatewayState=""),this._autoReconnect){const t=null!==(i=this.options.maxReconnectAttempts)&&void 0!==i?i:10;if(this._reconnectAttempts+=1,t>0&&this._reconnectAttempts>t){Ie.info(`Reconnection exhausted after ${t} attempts. Stopping automatic reconnect.`),this._reconnectAttempts=0,this._autoReconnect=!1;const i=pe(T);return void at(e.SwEvent.Error,{error:i,sessionId:this.sessionid},this.uuid)}Ie.debug(`Reconnect attempt ${this._reconnectAttempts}${t>0?` of ${t}`:""}`),this._reconnectTimeout=setTimeout((()=>{Ie.debug("Calling connect due to network close and auto-reconnect enabled."),this.connect()}),this.reconnectDelay)}}_onSocketMessage(e){}_removeSubscription(e,t){this._existsSubscription(e,t)&&(t?(delete this.subscriptions[e][t],rt(e,null,t)):(delete this.subscriptions[e],ct(e)))}_addSubscription(e,t=null,i){this._existsSubscription(e,i)||(this._existsSubscription(e)||(this.subscriptions[e]={}),this.subscriptions[e][i]={},Ae(t)&&st(e,t,i))}_existsSubscription(e,t){return!(!this.subscriptions.hasOwnProperty(e)||!(!t||t&&this.subscriptions[e].hasOwnProperty(t)))}_attachListeners(){this._detachListeners(),this.on(e.SwEvent.SocketOpen,this._onSocketOpen),this.on(e.SwEvent.SocketClose,this.onNetworkClose),this.on(e.SwEvent.SocketError,this.onNetworkClose),this.on(e.SwEvent.SocketMessage,this._onSocketMessage),this.on(e.SwEvent.SocketActivity,this._onSocketActivity)}_detachListeners(){this.off(e.SwEvent.SocketOpen,this._onSocketOpen),this.off(e.SwEvent.SocketClose,this.onNetworkClose),this.off(e.SwEvent.SocketError,this.onNetworkClose),this.off(e.SwEvent.SocketMessage,this._onSocketMessage),this.off(e.SwEvent.SocketActivity,this._onSocketActivity)}_emptyExecuteQueues(){this._executeQueue.forEach((({resolve:e,msg:t})=>{"string"==typeof t?this.executeRaw(t):e(this.execute(t))}))}_closeConnection(){this._idle=!0,clearTimeout(this._keepAliveTimeout),this.stopSignalingHealthMonitor(),this.connection&&this.connection.close()}_resetKeepAlive(){!1===this._pong&&(Ie.warn("No ping/pong received, forcing PING ACK to keep alive"),this.execute(new bt(je()))),clearTimeout(this._keepAliveTimeout),this._triggerKeepAliveTimeoutCheck()}_triggerKeepAliveTimeoutCheck(){this._pong=!1,this._keepAliveTimeout=setTimeout((()=>this._resetKeepAlive()),35e3)}setPingReceived(){Ie.debug("Ping received"),this._pong=!0}_onSocketActivity(){this._signalingHealthMonitor.onSocketActivity()}hasActiveCall(){const e=this.calls;if(!e)return!1;const t=new Set([Ke.Early,Ke.Active,Ke.Held]);return Object.values(e).some((e=>e&&null!=e._state&&t.has(e._state)))}startSignalingHealthMonitor(){this._signalingHealthMonitor.start()}stopSignalingHealthMonitor(){this._signalingHealthMonitor.stop()}triggerIceRestart(e){var t;const i=this.calls,n=null==i?void 0:i[e];if(!n)return Ie.warn(`Signaling health: cannot trigger ICE restart — call ${e} not found`),{started:!1,reason:"call not found"};if((null===(t=n.options)||void 0===t?void 0:t.attach)||n.recoveredCallId)return Ie.warn(`Signaling health: ICE restart skipped for call ${e}: call was recovered via attach`),{started:!1,reason:"call recovered via attach",recoverSignaling:!0};const s=n.peer;if(!(null==s?void 0:s.restartIce))return Ie.warn(`Signaling health: cannot trigger ICE restart — no peer for call ${e}`),{started:!1,reason:"no peer"};const o=s.restartIce();return o.started||Ie.debug(`Signaling health: ICE restart skipped for call ${e}: ${o.reason}`),o}onSignalingRequestTimeout(e,t,i=""){this._signalingHealthMonitor.onRequestTimeout(e,t,i)}reportPeerFailure(e,t){this._signalingHealthMonitor.onPeerFailure(e,t)}reportNoRtp(e,t){this._signalingHealthMonitor.onNoRtp(e,t)}static on(e,t){st(e,t)}static off(e){rt(e)}static uuid(){return c()}clearConnection(){this.connection=null}hasAutoReconnect(){return this._autoReconnect}}Ft.TOKEN_EXPIRY_WARNING_SECONDS=120,Ft.CALL_REPORT_UPLOAD_DRAIN_TIMEOUT_MS=1e4;const $t=e=>navigator.mediaDevices.getUserMedia(e),jt=e=>e&&e instanceof MediaStream,Ht=(e,t)=>{const i=Oe(e);null!==i&&(i.getAttribute("autoplay")||i.setAttribute("autoplay","autoplay"),i.getAttribute("playsinline")||i.setAttribute("playsinline","playsinline"),i.srcObject=t)},Gt=(e,t)=>{const i=Oe(e);if(i){if(t&&i.srcObject!==t)return;i.srcObject=null}},Wt=(e,t)=>i(void 0,void 0,void 0,(function*(){const i=Oe(e);if(null===i)return Ie.info("No HTMLMediaElement to attach the speakerId"),!1;if("string"!=typeof t)return Ie.info(`Invalid speaker deviceId: '${t}'`),!1;try{return yield i.setSinkId(t),!0}catch(e){return!1}})),Bt=e=>{e&&"live"===e.readyState&&e.stop()},Vt=e=>{jt(e)&&e.getTracks().forEach(Bt),e=null},qt=e=>i(void 0,void 0,void 0,(function*(){Ie.info("RTCService.getUserMedia",e);const{audio:i,video:n}=e;if(!i&&!n)return null;try{return yield $t(e)}catch(i){if(Ie.error("getUserMedia error: ",i),(e=>"NotReadableError"===e.name||"NotFoundError"===e.name||"OverconstrainedError"===e.name)(i)){const n=(e=>{const{audio:i,video:n}=e;let s=!1,o=i,r=n;if("object"==typeof i&&null!==i&&"deviceId"in i){s=!0;const e=t(i,["deviceId"]);o=0===Object.keys(e).length||e}if("object"==typeof n&&null!==n&&"deviceId"in n){s=!0;const e=t(n,["deviceId"]);r=0===Object.keys(e).length||e}return s?{audio:o,video:r}:null})(e);if(n){Ie.warn("Device not found or not readable, falling back to default device");try{return yield $t(n)}catch(e){throw Ie.error("Fallback getUserMedia also failed: ",e),i}}}throw i}})),Yt=(e=null,t=!1)=>i(void 0,void 0,void 0,(function*(){let i=[];const n=yield navigator.mediaDevices.getUserMedia(((e=null)=>({audio:!e||e===Xe.AudioIn||e===Xe.AudioOut,video:!e||e===Xe.Video}))(e)).catch((e=>(Ie.error(e),null)));if(n){if(Vt(n),i=yield navigator.mediaDevices.enumerateDevices(),e&&(i=i.filter((t=>t.kind===e))),!0===t)return i;const s=[];i=i.filter((e=>{if(!e.groupId)return!0;const t=`${e.kind}-${e.groupId}`;return!s.includes(t)&&(s.push(t),!0)}))}return i})),Kt=[[320,240],[640,360],[640,480],[1280,720],[1920,1080]],Jt=(e,t,n)=>i(void 0,void 0,void 0,(function*(){const i=yield Yt(n,!0);for(let n=0;n<i.length;n++){const{deviceId:s,label:o}=i[n];if(e===s||t===o)return s}return null})),Qt=e=>{const t=navigator.mediaDevices.getSupportedConstraints();Object.keys(e).map((i=>{t.hasOwnProperty(i)&&null!==e[i]&&void 0!==e[i]||delete e[i]}))},Xt=(e,t)=>{if(!e)return!1;const{subscribed:i,alreadySubscribed:n}=zt(e);return i.includes(t)||n.includes(t)},zt=e=>{const t={subscribed:[],alreadySubscribed:[],unauthorized:[],unsubscribed:[],notSubscribed:[]};return Object.keys(t).forEach((i=>{t[i]=e[`${i}Channels`]||[]})),t},Zt=(e,t=null,i=null)=>{if(!jt(e))return null;let n=[];switch(t){case"audio":n=e.getAudioTracks();break;case"video":n=e.getVideoTracks();break;default:n=e.getTracks()}n.forEach((e=>{switch(i){case"on":case!0:e.enabled=!0;break;case"off":case!1:e.enabled=!1;break;default:e.enabled=!e.enabled}}))},ei=e=>{Zt(e,"audio",!0)},ti=e=>{Zt(e,"audio",!1)},ii=e=>{Zt(e,"audio",null)},ni=e=>((e,t=null)=>{if(!jt(e))return null;let i=[];switch(t){case"audio":i=e.getAudioTracks();break;case"video":i=e.getVideoTracks();break;default:i=e.getTracks()}return i.some((e=>e.enabled))})(e,"audio");function si(){try{const{browserInfo:e,name:t,version:i,supportAudio:n,supportVideo:s}=function(){if(!window||!window.navigator||!window.navigator.userAgent)throw new Error("You should use @telnyx/webrtc in a web browser such as Chrome|Firefox|Safari");if(navigator.userAgent.match(/chrom(e|ium)/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)&&!navigator.userAgent.match(/edg/gim)){const e=navigator.userAgent.match(/chrom(e|ium)\/[0-9]+\./gim)[0].split("/"),t=e[0],i=parseInt(e[1],10);return{browserInfo:navigator.userAgent,name:t,version:i,supportAudio:!0,supportVideo:!0}}if(navigator.userAgent.match(/firefox/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)&&!navigator.userAgent.match(/edg/gim)){const e=navigator.userAgent.match(/firefox\/[0-9]+\./gim)[0].split("/"),t=e[0],i=parseInt(e[1],10);return{browserInfo:navigator.userAgent,name:t,version:i,supportAudio:!0,supportVideo:!1}}if(navigator.userAgent.match(/safari/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)&&!navigator.userAgent.match(/edg/gim)){const e=navigator.userAgent.match(/safari/gim)[0],t=navigator.userAgent.match(/version\/[0-9]+\./gim)[0].split("/"),i=parseInt(t[1],10);return{browserInfo:navigator.userAgent,name:e,version:i,supportAudio:!0,supportVideo:!0}}if(navigator.userAgent.match(/edg/gim)&&!navigator.userAgent.match(/OPR\/[0-9]{2}/gi)){const e=navigator.userAgent.match(/edg\/[0-9]+\./gim)[0].split("/"),t=e[0],i=parseInt(e[1],10);return{browserInfo:navigator.userAgent,name:t,version:i,supportAudio:!0,supportVideo:!0}}throw new Error("This browser does not support @telnyx/webrtc. To see browser support list: `TelnyxRTC.webRTCSupportedBrowserList()`")}(),o=window.RTCPeerConnection,r=window.RTCSessionDescription,a=window.RTCIceCandidate,c=window.navigator&&window.navigator.mediaDevices,d=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.msGetUserMedia||navigator.mozGetUserMedia;return{browserInfo:e,browserName:t,browserVersion:i,supportWebRTC:!!(o&&r&&a&&c&&d),supportWebRTCAudio:n,supportWebRTCVideo:s,supportRTCPeerConnection:!!o,supportSessionDescription:!!r,supportIceCandidate:!!a,supportMediaDevices:!!c,supportGetUserMedia:!!qt}}catch(e){return e.message}}var oi;function ri(e,t){const i=document.getElementById(t);if(i)return i;if(e&&t){const i=document.createElement("audio");return i.id=t,i.loop=!0,i.src=e,i.preload="auto",i.load(),document.body.appendChild(i),i}return null}function ai(e){e&&(e._playFulfilled=!1,e._promise=e.play(),e._promise.then((()=>{e._playFulfilled=!0})).catch((t=>{Ie.error("playAudio",t),e._playFulfilled=!0})))}function ci(e){e&&(e._playFulfilled?(e.pause(),e.currentTime=0):e._promise&&e._promise.then?e._promise.then((()=>{e.pause(),e.currentTime=0})):setTimeout((()=>{e.pause(),e.currentTime=0}),1e3))}!function(e){e.not_supported="not supported",e.full="full",e.partial="partial"}(oi||(oi={}));const di=e=>{const t=[],i=[];return e&&0!==e.length?(e.forEach((e=>{const n=e.mimeType.toLocaleLowerCase();n.startsWith("audio/")?t.push(e):n.startsWith("video/")&&i.push(e)})),{audioCodecs:t,videoCodecs:i}):{audioCodecs:t,videoCodecs:i}};class li extends vt{constructor(e,t){super(),this.method="ai_conversation",this.buildRequest({method:this.method,params:{type:"conversation.item.create",previous_item_id:null,item:{id:c(),type:"message",role:"user",content:[{type:"input_text",text:e},...null==t?void 0:t.map((e=>({type:"image_url",image_url:{url:e}})))]}}})}}class hi{constructor(e,t){var i;this.peerConnection=null,this.intervalId=null,this.statsBuffer=[],this.intervalStartTime=null,this.callEndTime=null,this.logCollector=null,this.intervalAudioLevels={outbound:[],inbound:[]},this.intervalJitters=[],this.intervalRTTs=[],this.intervalBitrates={outbound:[],inbound:[]},this.previousStats={},this.previousCandidatePairSnapshot=null,this.MAX_BUFFER_SIZE=360,this.onFlushNeeded=null,this.onWarning=null,this._breachCounters={},this._activeWarnings=new Set,this._lastWarningEmitted={},this._prevPacketsReceived=null,this._prevPacketsLost=null,this._previousStatsEntryForWarnings=null,this._lastLocalAudioTrackSnapshotJson=null,this._hasConfirmedLocalAudio=!1,this._confirmedLocalAudioSilenceMs=0,this._segmentIndex=0,this._flushing=!1,this._stopped=!1,this.options=e,this.logCollectorOptions=t||{enabled:!1,level:"debug",maxEntries:1e3},this.callStartTime=new Date,this._lastIntermediateFlushTime=this.callStartTime,this.logCollectorOptions.enabled&&(this.logCollector=function(e){return new be(e)}(this.logCollectorOptions),this.logCollector.start(),i=this.logCollector,ye=i)}start(e){var t,i;this.options.enabled&&(this.peerConnection=e,this.intervalStartTime=new Date,this._lastIntermediateFlushTime=this.intervalStartTime,this._stopped=!1,Ie.info("CallReportCollector: Starting stats collection",{interval:this.options.interval,initialInterval:hi.INITIAL_COLLECTION_INTERVAL_MS,initialDuration:hi.INITIAL_COLLECTION_DURATION_MS,logCollectorActive:null!==(i=null===(t=this.logCollector)||void 0===t?void 0:t.isActive())&&void 0!==i&&i}),this._scheduleNextCollection())}stop(){var e,t;return i(this,void 0,void 0,(function*(){this._stopped=!0,this.intervalId&&(clearTimeout(this.intervalId),this.intervalId=null),this.callEndTime=new Date,this.peerConnection&&this.intervalStartTime&&(yield this._collectStats(!0));const i=null!==(t=null===(e=this.logCollector)||void 0===e?void 0:e.getLogCount())&&void 0!==t?t:0;this.logCollector&&this.logCollector.stop(),Ie.info("CallReportCollector: Stopped stats collection",{totalIntervals:this.statsBuffer.length,totalLogs:i,duration:this.callEndTime.getTime()-this.callStartTime.getTime()})}))}flush(e,t){var i,n;if(this._flushing||0===this.statsBuffer.length)return null;this._flushing=!0;try{const s=this._segmentIndex++,o=this.statsBuffer;this.statsBuffer=[];const r=null!==(n=null===(i=this.logCollector)||void 0===i?void 0:i.drain())&&void 0!==n?n:[],a=new Date;this._lastIntermediateFlushTime=a;const c=Object.assign(Object.assign(Object.assign({summary:Object.assign(Object.assign({},e),{durationSeconds:(a.getTime()-this.callStartTime.getTime())/1e3,startTimestamp:this.callStartTime.toISOString(),endTimestamp:a.toISOString()}),stats:o},r.length>0?{logs:r}:{}),{segment:s}),t?{flushReason:t}:{});return Ie.info("CallReportCollector: Flushed intermediate segment",{segment:s,intervals:o.length,logEntries:r.length,callId:e.callId,flushReason:t}),c}finally{this._flushing=!1}}postReport(e,t,n,s){var o,r;return i(this,void 0,void 0,(function*(){const i=null===(o=this.logCollector)||void 0===o?void 0:o.getLogs(),a=i&&i.length>0;if(!this.options.enabled)return void Ie.info("CallReportCollector: Skipping report — call reports disabled");if(0===this.statsBuffer.length&&!a)return void Ie.info("CallReportCollector: Skipping report — no stats or logs collected");const c=this._segmentIndex>0,d=this._segmentIndex,l=Object.assign(Object.assign({summary:Object.assign(Object.assign({},e),{durationSeconds:this.callEndTime&&this.callStartTime?(this.callEndTime.getTime()-this.callStartTime.getTime())/1e3:void 0,startTimestamp:this.callStartTime.toISOString(),endTimestamp:null===(r=this.callEndTime)||void 0===r?void 0:r.toISOString()}),stats:this.statsBuffer},i&&i.length>0?{logs:i}:{}),c?{segment:d}:{});yield this._sendPayload(l,t,n,s,!0)}))}sendPayload(e,t,n,s){return i(this,void 0,void 0,(function*(){yield this._sendPayload(e,t,n,s,!1)}))}_sendPayload(e,t,n,s,o=!0){var r,a;return i(this,void 0,void 0,(function*(){const i=new URL(n),c=`${i.protocol.replace(/^ws/,"http")}//${i.host}/call_report`,d=o?"final report":`intermediate segment ${e.segment}`;Ie.info(`CallReportCollector: Posting ${d}`,{endpoint:c,intervals:e.stats.length,logEntries:null!==(a=null===(r=e.logs)||void 0===r?void 0:r.length)&&void 0!==a?a:0,callId:e.summary.callId,segment:e.segment});const l={"Content-Type":"application/json","x-call-report-id":t,"x-call-id":e.summary.callId};s&&(l["x-voice-sdk-id"]=s);const h=JSON.stringify(e),u=o&&h.length<=hi.KEEPALIVE_BODY_LIMIT_BYTES;let p;for(let e=0;e<=hi.RETRY_DELAYS_MS.length;e++){try{const t=yield fetch(c,Object.assign({method:"POST",headers:l,body:h},u?{keepalive:!0}:{}));if(t.ok)return void Ie.info(`CallReportCollector: Successfully posted ${d}`,{attempt:e+1,status:t.status});{const i=yield t.text();p=new Error(`Call report POST failed with status ${t.status}`),Ie.error(`CallReportCollector: Failed to post ${d}`,{attempt:e+1,status:t.status,error:i})}}catch(t){p=t,Ie.warn(`CallReportCollector: Network error posting ${d}`,{attempt:e+1,error:t})}const t=hi.RETRY_DELAYS_MS[e];if(void 0===t)break;Ie.info(`CallReportCollector: Retrying ${d} in ${t}ms`,{attempt:e+2}),yield new Promise((e=>setTimeout(e,t)))}throw Ie.error(`CallReportCollector: Exhausted retries posting ${d}`,{error:p}),p instanceof Error?p:new Error("Call report POST failed after retries")}))}getStatsBuffer(){return this.statsBuffer}shouldForceRelayCandidateForRecovery(){var e,t,i,n,s,o,r,a,c,d,l,h,u,p,g,v;if(this.statsBuffer.length<2)return!1;const m=this.statsBuffer[this.statsBuffer.length-1],f=this.statsBuffer[this.statsBuffer.length-2],_=null===(e=m.ice)||void 0===e?void 0:e.local;if(!("vpn"===(null==_?void 0:_.networkType)&&"relay"!==(null==_?void 0:_.candidateType)))return!1;const S=!1===(null===(t=m.ice)||void 0===t?void 0:t.writable),b="disconnected"===(null===(i=m.transport)||void 0===i?void 0:i.iceState)||"failed"===(null===(n=m.transport)||void 0===n?void 0:n.iceState),y=this._positiveDelta(null===(s=m.ice)||void 0===s?void 0:s.requestsSent,null===(o=f.ice)||void 0===o?void 0:o.requestsSent),I=this._positiveDelta(null===(r=m.ice)||void 0===r?void 0:r.responsesReceived,null===(a=f.ice)||void 0===a?void 0:a.responsesReceived),E=y>0&&0===I,C=this._positiveDelta(null===(d=null===(c=m.audio)||void 0===c?void 0:c.outbound)||void 0===d?void 0:d.bytesSent,null===(h=null===(l=f.audio)||void 0===l?void 0:l.outbound)||void 0===h?void 0:h.bytesSent),w=this._positiveDelta(null===(p=null===(u=m.audio)||void 0===u?void 0:u.inbound)||void 0===p?void 0:p.bytesReceived,null===(v=null===(g=f.audio)||void 0===g?void 0:g.inbound)||void 0===v?void 0:v.bytesReceived);return S||b||E||C>0&&0===w}getLogs(){var e,t;return null!==(t=null===(e=this.logCollector)||void 0===e?void 0:e.getLogs())&&void 0!==t?t:[]}_positiveDelta(e,t){return void 0===e||void 0===t?0:Math.max(0,e-t)}cleanup(){this._lastLocalAudioTrackSnapshotJson=null,this.logCollector&&(this.logCollector.clear(),this.logCollector=null)}_scheduleNextCollection(){if(this._stopped||!this.peerConnection||!this.intervalStartTime||this.intervalId)return;const e=this._collectionIntervalFor(this.intervalStartTime);this.intervalId=setTimeout((()=>{this.intervalId=null,this._collectStats().finally((()=>{!this._stopped&&this.peerConnection&&this._scheduleNextCollection()}))}),e)}_collectionIntervalFor(e){const t=this._positiveInterval(this.options.interval,5e3);return e.getTime()-this.callStartTime.getTime()<hi.INITIAL_COLLECTION_DURATION_MS?Math.min(hi.INITIAL_COLLECTION_INTERVAL_MS,t):t}_positiveInterval(e,t){return"number"==typeof e&&Number.isFinite(e)&&e>0?e:t}_collectStats(e=!1){return i(this,void 0,void 0,(function*(){if(this.peerConnection&&this.intervalStartTime)try{const t=yield this.peerConnection.getStats(),i=new Date;let n,s,o,r,a,c=null,d=null,l=null,h=null;if(t.forEach((e=>{switch(e.type){case"outbound-rtp":"audio"===e.kind&&"audio"===e.mediaType&&(c=e);break;case"inbound-rtp":"audio"===e.kind&&"audio"===e.mediaType&&(d=e);break;case"candidate-pair":(e.nominated||"succeeded"===e.state)&&(l=e);break;case"transport":h=e}})),c){const e=this._getOutboundAudioLevel(t,c);if(null!==e&&this.intervalAudioLevels.outbound.push(e),void 0!==this.previousStats.outboundBytes&&void 0!==this.previousStats.timestamp){const e=(c.bytesSent||0)-this.previousStats.outboundBytes,t=(c.timestamp||i.getTime())-this.previousStats.timestamp;if(t>0){const i=8*e*1e3/t;this.intervalBitrates.outbound.push(i)}}this.previousStats.outboundBytes=c.bytesSent}if(d){const e=this._getInboundAudioLevel(t,d);if(null!==e&&this.intervalAudioLevels.inbound.push(e),void 0!==d.jitter&&this.intervalJitters.push(1e3*d.jitter),void 0!==this.previousStats.inboundBytes&&void 0!==this.previousStats.timestamp){const e=(d.bytesReceived||0)-this.previousStats.inboundBytes,t=(d.timestamp||i.getTime())-this.previousStats.timestamp;if(t>0){const i=8*e*1e3/t;this.intervalBitrates.inbound.push(i)}}this.previousStats.inboundBytes=d.bytesReceived}l&&void 0!==l.currentRoundTripTime&&this.intervalRTTs.push(l.currentRoundTripTime),l&&(n=this._resolveCandidate(t,l.localCandidateId),s=this._resolveCandidate(t,l.remoteCandidateId)),l&&(o=Object.assign(Object.assign({id:l.id,state:l.state,nominated:l.nominated,writable:l.writable,requestsSent:l.requestsSent,responsesReceived:l.responsesReceived},n?{local:n}:{}),s?{remote:s}:{}),null!==this.previousCandidatePairSnapshot&&l.id!==this.previousCandidatePairSnapshot.id&&(Ie.debug("CallReportCollector: ICE candidate pair changed mid-call",{previous:this.previousCandidatePairSnapshot,current:o}),this._emitWarning(Y)),this.previousCandidatePairSnapshot=o),c&&(r=this._getLocalAudioTrackSnapshot(),a=this._getOutboundAudioSourceStats(t,c)),this._logLocalAudioTrackSnapshot(r,a),this.previousStats.timestamp=i.getTime();const u=i.getTime()-this.intervalStartTime.getTime(),p=this._collectionIntervalFor(this.intervalStartTime);if(e||u>=p){const e=this._createStatsEntry(this.intervalStartTime,i,c,d,l,n,s,h,r,a);this.statsBuffer.push(e),this.statsBuffer.length>this.MAX_BUFFER_SIZE&&(this.statsBuffer.shift(),Ie.warn("CallReportCollector: Buffer size limit reached, removing oldest entry")),this._checkQualityWarnings(e,d),this._requestIntermediateFlushIfNeeded(i),this.intervalStartTime=i,this._resetIntervalAccumulators()}}catch(e){Ie.error("CallReportCollector: Error collecting stats",{error:e})}}))}_getIntermediateReportInterval(){var e;return null!==(e=this.options.intermediateReportInterval)&&void 0!==e?e:hi.DEFAULT_INTERMEDIATE_REPORT_INTERVAL_MS}_requestIntermediateFlushIfNeeded(e){var t,i;if(!this.onFlushNeeded||this._flushing||0===this.statsBuffer.length)return;const n=this.statsBuffer.length,s=null!==(i=null===(t=this.logCollector)||void 0===t?void 0:t.getLogCount())&&void 0!==i?i:0,o=this._getIntermediateReportInterval(),r=e.getTime()-this._lastIntermediateFlushTime.getTime();let a=null;if(n>=hi.STATS_FLUSH_THRESHOLD||s>=hi.LOGS_FLUSH_THRESHOLD?a="buffer-limit":o>0&&r>=o&&(a="safety-interval"),a){Ie.info("CallReportCollector: Requesting intermediate report flush",{reason:a,statsIntervals:n,logEntries:s,msSinceLastFlush:r}),this._lastIntermediateFlushTime=e;try{this.onFlushNeeded()}catch(e){Ie.error("CallReportCollector: onFlushNeeded callback error",{error:e})}}}_checkQualityWarnings(e,t){var i,n,s,o,r,a,c,d,l,h,u,p,g,v,m;if(!this.onWarning)return;const f=null===(i=e.connection)||void 0===i?void 0:i.roundTripTimeAvg,_=null===(s=null===(n=e.audio)||void 0===n?void 0:n.inbound)||void 0===s?void 0:s.jitterAvg;let S;if(t){const e=null!==(o=t.packetsReceived)&&void 0!==o?o:0,i=null!==(r=t.packetsLost)&&void 0!==r?r:0;if(null!==this._prevPacketsReceived&&null!==this._prevPacketsLost){const t=e-this._prevPacketsReceived,n=i-this._prevPacketsLost,s=t+n;s>0&&(S=n/s*100)}this._prevPacketsReceived=e,this._prevPacketsLost=i}if(this._trackBreach(P,void 0!==f&&f>hi.THRESHOLD_RTT_MS),this._trackBreach(M,void 0!==_&&_>hi.THRESHOLD_JITTER_MS),this._trackBreach(x,void 0!==S&&S>hi.THRESHOLD_PACKET_LOSS_PCT),this._trackLowLocalAudio(e),void 0!==f&&void 0!==_&&void 0!==S){const e=93.2-.11*_-2.5*S-.01*(1e3*f),t=Math.max(1,Math.min(4.5,1+.035*e+e*(e-60)*(100-e)*7e-6));this._trackBreach(U,t<hi.THRESHOLD_MOS)}else this._trackBreach(U,!1);if(void 0!==(null===(c=null===(a=e.audio)||void 0===a?void 0:a.inbound)||void 0===c?void 0:c.bytesReceived)){const t=null===(h=null===(l=null===(d=this._previousStatsEntryForWarnings)||void 0===d?void 0:d.audio)||void 0===l?void 0:l.inbound)||void 0===h?void 0:h.bytesReceived,i=e.audio.inbound.bytesReceived;this._trackBreach($,void 0!==t&&i-t==0)}if(void 0!==(null===(p=null===(u=e.audio)||void 0===u?void 0:u.outbound)||void 0===p?void 0:p.bytesSent)){const t=null===(m=null===(v=null===(g=this._previousStatsEntryForWarnings)||void 0===g?void 0:g.audio)||void 0===v?void 0:v.outbound)||void 0===m?void 0:m.bytesSent,i=e.audio.outbound.bytesSent;this._trackBreach(j,void 0!==t&&i-t==0)}this._previousStatsEntryForWarnings=e}_trackLowLocalAudio(e){var t;const i=null===(t=e.audio)||void 0===t?void 0:t.outbound,n=null==i?void 0:i.audioLevelAvg,s=null==i?void 0:i.localTrack;if(void 0===n||!1===(null==s?void 0:s.enabled)||!0===(null==s?void 0:s.muted))return void this._resetLowLocalAudioWarning();if(!(n<=hi.THRESHOLD_LOCAL_AUDIO_LEVEL))return this._hasConfirmedLocalAudio=!0,this._confirmedLocalAudioSilenceMs=0,void this._trackBreach(F,!1);this._hasConfirmedLocalAudio?(this._confirmedLocalAudioSilenceMs+=this._getStatsIntervalDurationMs(e),this._confirmedLocalAudioSilenceMs>=hi.CONFIRMED_LOCAL_AUDIO_SILENCE_MS&&this._emitWarningOncePerEpisode(F)):this._trackBreach(F,!0)}_resetLowLocalAudioWarning(){this._confirmedLocalAudioSilenceMs=0,this._trackBreach(F,!1)}_getStatsIntervalDurationMs(e){const t=new Date(e.intervalStartUtc).getTime(),i=new Date(e.intervalEndUtc).getTime()-t;return Number.isFinite(i)&&i>0?i:this.options.interval}_trackBreach(e,t){var i,n;if(t){if(this._breachCounters[e]=(null!==(i=this._breachCounters[e])&&void 0!==i?i:0)+1,this._breachCounters[e]>=hi.CONSECUTIVE_BREACHES_REQUIRED){this._activeWarnings.add(e);const t=Date.now();t-(null!==(n=this._lastWarningEmitted[e])&&void 0!==n?n:0)>=hi.WARNING_THROTTLE_MS&&(this._lastWarningEmitted[e]=t,this._emitWarning(e))}}else this._breachCounters[e]=0,this._activeWarnings.delete(e),delete this._lastWarningEmitted[e]}_emitWarningOncePerEpisode(e){this._activeWarnings.has(e)||(this._activeWarnings.add(e),this._lastWarningEmitted[e]=Date.now(),this._emitWarning(e))}_emitWarning(e){var t;try{const i=le(e);Ie.warn(`CallReportCollector: warning ${i.code}: ${i.message}`),null===(t=this.onWarning)||void 0===t||t.call(this,i)}catch(t){Ie.error(`CallReportCollector: Failed to emit warning ${e}`,{error:t})}}_createStatsEntry(e,t,i,n,s,o,r,a,c,d){const l={intervalStartUtc:e.toISOString(),intervalEndUtc:t.toISOString(),audio:{}};return i&&(l.audio.outbound=Object.assign(Object.assign({packetsSent:i.packetsSent,bytesSent:i.bytesSent,audioLevelAvg:this._average(this.intervalAudioLevels.outbound),bitrateAvg:this._average(this.intervalBitrates.outbound)},c?{localTrack:c}:{}),d?{mediaSource:d}:{})),n&&(l.audio.inbound={packetsReceived:n.packetsReceived,bytesReceived:n.bytesReceived,packetsLost:n.packetsLost,packetsDiscarded:n.packetsDiscarded,jitterBufferDelay:n.jitterBufferDelay,jitterBufferEmittedCount:n.jitterBufferEmittedCount,totalSamplesReceived:n.totalSamplesReceived,concealedSamples:n.concealedSamples,concealmentEvents:n.concealmentEvents,audioLevelAvg:this._average(this.intervalAudioLevels.inbound),jitterAvg:this._average(this.intervalJitters),bitrateAvg:this._average(this.intervalBitrates.inbound)}),s&&(l.connection={roundTripTimeAvg:this._average(this.intervalRTTs),packetsSent:s.packetsSent,packetsReceived:s.packetsReceived,bytesSent:s.bytesSent,bytesReceived:s.bytesReceived},l.ice=Object.assign(Object.assign({id:s.id,state:s.state,nominated:s.nominated,writable:s.writable,requestsSent:s.requestsSent,responsesReceived:s.responsesReceived},o?{local:o}:{}),r?{remote:r}:{})),a&&(l.transport=Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},void 0!==a.iceState?{iceState:a.iceState}:{}),void 0!==a.dtlsState?{dtlsState:a.dtlsState}:{}),void 0!==a.srtpCipher?{srtpCipher:a.srtpCipher}:{}),void 0!==a.tlsVersion?{tlsVersion:a.tlsVersion}:{}),void 0!==a.selectedCandidatePairChanges?{selectedCandidatePairChanges:a.selectedCandidatePairChanges}:{})),l}_resolveCandidate(e,t){if(!t)return void Ie.debug("CallReportCollector: candidateId is empty, skipping resolve");const i=e.get(t);if(!i)return void Ie.debug("CallReportCollector: candidate not found in stats report",{candidateId:t});const n={};if(void 0!==i.address?n.address=i.address:void 0!==i.ip&&(n.address=i.ip),void 0!==i.port&&(n.port=i.port),void 0!==i.candidateType&&(n.candidateType=i.candidateType),void 0!==i.protocol&&(n.protocol=i.protocol),void 0!==i.networkType&&(n.networkType=i.networkType),0!==Object.keys(n).length)return n;Ie.debug("CallReportCollector: candidate report has no usable fields",{candidateId:t})}_getOutboundMediaSource(e,t){let i;return t.mediaSourceId&&(i=e.get(t.mediaSourceId)),i||e.forEach((e=>{i||"media-source"!==e.type||"audio"!==e.kind||(i=e)})),i}_getLocalAudioTrackSnapshot(){var e,t;try{const i=null===(e=this.peerConnection)||void 0===e?void 0:e.getSenders().find((e=>{var t;return"audio"===(null===(t=e.track)||void 0===t?void 0:t.kind)})),n=null==i?void 0:i.track;if(!n)return;const s=null===(t=n.getSettings)||void 0===t?void 0:t.call(n),o=this._withoutUndefined({deviceId:null==s?void 0:s.deviceId,groupId:null==s?void 0:s.groupId,channelCount:null==s?void 0:s.channelCount,sampleRate:null==s?void 0:s.sampleRate,sampleSize:null==s?void 0:s.sampleSize,latency:null==s?void 0:s.latency,echoCancellation:null==s?void 0:s.echoCancellation,noiseSuppression:null==s?void 0:s.noiseSuppression,autoGainControl:null==s?void 0:s.autoGainControl}),r=this._withoutUndefined(Object.assign({id:n.id,label:n.label,enabled:n.enabled,muted:n.muted,readyState:n.readyState,contentHint:n.contentHint},Object.keys(o).length>0?{settings:o}:{}));return Object.keys(r).length>0?r:void 0}catch(e){return void Ie.debug("CallReportCollector: unable to snapshot local audio track",{error:e})}}_getOutboundAudioSourceStats(e,t){const i=this._getOutboundMediaSource(e,t);if(!i)return;const n=this._withoutUndefined({id:i.id,trackIdentifier:i.trackIdentifier,audioLevel:i.audioLevel,totalAudioEnergy:i.totalAudioEnergy,totalSamplesDuration:i.totalSamplesDuration,echoReturnLoss:i.echoReturnLoss,echoReturnLossEnhancement:i.echoReturnLossEnhancement});return Object.keys(n).length>0?n:void 0}_logLocalAudioTrackSnapshot(e,t){if(!e||0===Object.keys(e).length)return;const i=this._stableStringify(e);i!==this._lastLocalAudioTrackSnapshotJson&&(this._lastLocalAudioTrackSnapshotJson=i,Ie.debug("CallReportCollector: local audio track snapshot",{localTrack:e,mediaSource:t}))}_withoutUndefined(e){return Object.keys(e).reduce(((t,i)=>{const n=e[i];return void 0!==n&&(t[i]=n),t}),{})}_stableStringify(e){return JSON.stringify(this._sortObjectKeys(e))}_sortObjectKeys(e){return Array.isArray(e)?e.map((e=>this._sortObjectKeys(e))):e&&"object"==typeof e?Object.keys(e).sort().reduce(((t,i)=>(t[i]=this._sortObjectKeys(e[i]),t)),{}):e}_getOutboundAudioLevel(e,t){const i=this._getOutboundMediaSource(e,t);if(void 0!==(null==i?void 0:i.audioLevel))return i.audioLevel;if(i){const e=this._computeAudioLevelFromEnergy(i.totalAudioEnergy,i.totalSamplesDuration,"outbound");if(null!==e)return e}return this._getTrackAudioLevel(e,t.trackId)}_getInboundAudioLevel(e,t){if(void 0!==t.audioLevel)return t.audioLevel;const i=this._computeAudioLevelFromEnergy(t.totalAudioEnergy,t.totalSamplesDuration,"inbound");return null!==i?i:this._getTrackAudioLevel(e,t.trackId)}_computeAudioLevelFromEnergy(e,t,i){if(void 0===e||void 0===t)return null;const n="inbound"===i?"inboundAudioEnergy":"outboundAudioEnergy",s="inbound"===i?"inboundSamplesDuration":"outboundSamplesDuration",o=this.previousStats[n],r=this.previousStats[s];if(this.previousStats[n]=e,this.previousStats[s]=t,void 0===o||void 0===r)return null;const a=e-o,c=t-r;if(c<=0)return null;const d=Math.sqrt(a/c);return Math.min(1,Math.max(0,d))}_getTrackAudioLevel(e,t){var i;if(!t)return null;const n=e.get(t);return n&&null!==(i=n.audioLevel)&&void 0!==i?i:null}_average(e){if(0===e.length)return;const t=e.reduce(((e,t)=>e+t),0);return parseFloat((t/e.length).toFixed(4))}_resetIntervalAccumulators(){this.intervalAudioLevels={outbound:[],inbound:[]},this.intervalJitters=[],this.intervalRTTs=[],this.intervalBitrates={outbound:[],inbound:[]}}}hi.INITIAL_COLLECTION_INTERVAL_MS=1e3,hi.INITIAL_COLLECTION_DURATION_MS=1e4,hi.STATS_FLUSH_THRESHOLD=300,hi.LOGS_FLUSH_THRESHOLD=800,hi.DEFAULT_INTERMEDIATE_REPORT_INTERVAL_MS=18e4,hi.CONSECUTIVE_BREACHES_REQUIRED=3,hi.THRESHOLD_RTT_MS=.4,hi.THRESHOLD_JITTER_MS=30,hi.THRESHOLD_PACKET_LOSS_PCT=1,hi.THRESHOLD_MOS=3.5,hi.THRESHOLD_LOCAL_AUDIO_LEVEL=.001,hi.CONFIRMED_LOCAL_AUDIO_SILENCE_MS=3e4,hi.WARNING_THROTTLE_MS=15e3,hi.RETRY_DELAYS_MS=[500,1e3,2e3],hi.KEEPALIVE_BODY_LIMIT_BYTES=61440;var ui,pi=fe((function(e,t){var i;function n(){}function s(){s.init.call(this)}function o(e){return void 0===e._maxListeners?s.defaultMaxListeners:e._maxListeners}function r(e,t,i,s){var r,a,c;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),a=e._events),c=a[t]):(a=e._events=new n,e._eventsCount=0),c){if("function"==typeof c?c=a[t]=s?[i,c]:[c,i]:s?c.unshift(i):c.push(i),!c.warned&&((r=o(e))&&0<r&&c.length>r)){c.warned=!0;var d=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");d.name="MaxListenersExceededWarning",d.emitter=e,d.type=t,d.count=c.length,function(e){"function"==typeof console.warn?console.warn(e):console.log(e)}(d)}}else c=a[t]=i,++e._eventsCount;return e}function a(e,t,i){function n(){e.removeListener(t,n),s||(s=!0,i.apply(e,arguments))}var s=!1;return n.listener=i,n}function c(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function d(e,t){for(var i=Array(t);t--;)i[t]=e[t];return i}Object.defineProperty(t,"__esModule",{value:!0}),n.prototype=Object.create(null),s.EventEmitter=s,s.usingDomains=!1,s.prototype.domain=void 0,s.prototype._events=void 0,s.prototype._maxListeners=void 0,s.defaultMaxListeners=10,s.init=function(){this.domain=null,s.usingDomains&&i.active&&!(this instanceof i.Domain)&&(this.domain=i.active),this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new n,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},s.prototype.setMaxListeners=function(e){if("number"!=typeof e||0>e||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},s.prototype.getMaxListeners=function(){return o(this)},s.prototype.emit=function(e){var t,i,n,s,o,r,a,c="error"===e;if(r=this._events)c=c&&null==r.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var l=new Error('Uncaught, unspecified "error" event. ('+t+")");throw l.context=t,l}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(i=r[e]))return!1;var h="function"==typeof i;switch(n=arguments.length){case 1:!function(e,t,i){if(t)e.call(i);else for(var n=e.length,s=d(e,n),o=0;o<n;++o)s[o].call(i)}(i,h,this);break;case 2:!function(e,t,i,n){if(t)e.call(i,n);else for(var s=e.length,o=d(e,s),r=0;r<s;++r)o[r].call(i,n)}(i,h,this,arguments[1]);break;case 3:!function(e,t,i,n,s){if(t)e.call(i,n,s);else for(var o=e.length,r=d(e,o),a=0;a<o;++a)r[a].call(i,n,s)}(i,h,this,arguments[1],arguments[2]);break;case 4:!function(e,t,i,n,s,o){if(t)e.call(i,n,s,o);else for(var r=e.length,a=d(e,r),c=0;c<r;++c)a[c].call(i,n,s,o)}(i,h,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=Array(n-1),o=1;o<n;o++)s[o-1]=arguments[o];!function(e,t,i,n){if(t)e.apply(i,n);else for(var s=e.length,o=d(e,s),r=0;r<s;++r)o[r].apply(i,n)}(i,h,this,s)}return!0},s.prototype.addListener=function(e,t){return r(this,e,t,!1)},s.prototype.on=s.prototype.addListener,s.prototype.prependListener=function(e,t){return r(this,e,t,!0)},s.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,a(this,e,t)),this},s.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,a(this,e,t)),this},s.prototype.removeListener=function(e,t){var i,s,o,r,a;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(s=this._events))return this;if(!(i=s[e]))return this;if(i===t||i.listener&&i.listener===t)0==--this._eventsCount?this._events=new n:(delete s[e],s.removeListener&&this.emit("removeListener",e,i.listener||t));else if("function"!=typeof i){for(o=-1,r=i.length;0<r--;)if(i[r]===t||i[r].listener&&i[r].listener===t){a=i[r].listener,o=r;break}if(0>o)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new n,this;delete s[e]}else!function(e,t){for(var i=t,n=i+1,s=e.length;n<s;i+=1,n+=1)e[i]=e[n];e.pop()}(i,o);s.removeListener&&this.emit("removeListener",e,a||t)}return this},s.prototype.removeAllListeners=function(e){var t,i;if(!(i=this._events))return this;if(!i.removeListener)return 0===arguments.length?(this._events=new n,this._eventsCount=0):i[e]&&(0==--this._eventsCount?this._events=new n:delete i[e]),this;if(0===arguments.length){for(var s,o=Object.keys(i),r=0;r<o.length;++r)"removeListener"!==(s=o[r])&&this.removeAllListeners(s);return this.removeAllListeners("removeListener"),this._events=new n,this._eventsCount=0,this}if("function"==typeof(t=i[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},s.prototype.listeners=function(e){var t,i,n=this._events;return n?i=(t=n[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=Array(e.length),i=0;i<t.length;++i)t[i]=e[i].listener||e[i];return t}(t):[]:i=[],i},s.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):c.call(e,t)},s.prototype.listenerCount=c,s.prototype.eventNames=function(){return 0<this._eventsCount?Reflect.ownKeys(this._events):[]};var l,h=new Uint8Array(16);function u(){if(!l&&!(l="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return l(h)}var p=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;for(var g=[],v=0;256>v;++v)g.push((v+256).toString(16).substr(1));function m(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:0,i=(g[e[t+0]]+g[e[t+1]]+g[e[t+2]]+g[e[t+3]]+"-"+g[e[t+4]]+g[e[t+5]]+"-"+g[e[t+6]]+g[e[t+7]]+"-"+g[e[t+8]]+g[e[t+9]]+"-"+g[e[t+10]]+g[e[t+11]]+g[e[t+12]]+g[e[t+13]]+g[e[t+14]]+g[e[t+15]]).toLowerCase();if(!function(e){return"string"==typeof e&&p.test(e)}(i))throw TypeError("Stringified UUID is invalid");return i}function f(e,t,i){var n=(e=e||{}).random||(e.rng||u)();if(n[6]=64|15&n[6],n[8]=128|63&n[8],t){i=i||0;for(var s=0;16>s;++s)t[i+s]=n[s];return t}return m(n)}function _(e,t){if(!e||!t)return{};const i={...e};if(i.localCandidateId){const e=t.get(i.localCandidateId);i.local={...e}}if(i.remoteCandidateId){const e=t.get(i.remoteCandidateId);i.remote={...e}}return i}function S(e,t,i){return 8*function(e,t,i){const n=e[i],s=t?t[i]:null;return null===n||null===s?null:(n-s)/(e.timestamp-t.timestamp)*1e3}(e,t,i)}function b(e){if(!e.entries)return e;const t={};return e.forEach((function(e,i){t[i]=e})),t}function y(e,t,i={}){if(!e)return null;let n={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]},connection:{inbound:[],outbound:[]}};i.remote&&(n.remote={audio:{inbound:[],outbound:[]},video:{inbound:[],outbound:[]}});for(const t of e.values())switch(t.type){case"outbound-rtp":{const i=t.mediaType||t.kind,s={};let o={};if(!["audio","video"].includes(i))continue;if(t.codecId){const i=e.get(t.codecId);i&&(s.clockRate=i.clockRate,s.mimeType=i.mimeType,s.payloadType=i.payloadType)}o=e.get(t.mediaSourceId)||e.get(t.trackId)||{},n[i].outbound.push({...t,...s,track:{...o}});break}case"inbound-rtp":{let i=t.mediaType||t.kind,s={};const o={};if(!["audio","video"].includes(i))if(t.id.includes("Video"))i="video";else{if(!t.id.includes("Audio"))continue;i="audio"}if(t.codecId){const i=e.get(t.codecId);i&&(o.clockRate=i.clockRate,o.mimeType=i.mimeType,o.payloadType=i.payloadType)}if(!n.connection.id&&t.transportId){const i=e.get(t.transportId);if(i&&i.selectedCandidatePairId){const t=e.get(i.selectedCandidatePairId);n.connection=_(t,e)}}s=e.get(t.mediaSourceId)||e.get(t.trackId)||{},n[i].inbound.push({...t,...o,track:{...s}});break}case"peer-connection":n.connection.dataChannelsClosed=t.dataChannelsClosed,n.connection.dataChannelsOpened=t.dataChannelsOpened;break;case"remote-inbound-rtp":{if(!i.remote)break;let s=t.mediaType||t.kind;const o={};if(!["audio","video"].includes(s))if(t.id.includes("Video"))s="video";else{if(!t.id.includes("Audio"))continue;s="audio"}if(t.codecId){const i=e.get(t.codecId);i&&(o.clockRate=i.clockRate,o.mimeType=i.mimeType,o.payloadType=i.payloadType)}if(!n.connection.id&&t.transportId){const i=e.get(t.transportId);if(i&&i.selectedCandidatePairId){const t=e.get(i.selectedCandidatePairId);n.connection=_(t,e)}}n.remote[s].inbound.push({...t,...o});break}case"remote-outbound-rtp":{if(!i.remote)break;const s=t.mediaType||t.kind,o={};if(!["audio","video"].includes(s))continue;if(t.codecId){const i=e.get(t.codecId);i&&(o.clockRate=i.clockRate,o.mimeType=i.mimeType,o.payloadType=i.payloadType)}n.remote[s].outbound.push({...t,...o});break}}if(!n.connection.id)for(const t of e.values())"candidate-pair"===t.type&&t.nominated&&"succeeded"===t.state&&(n.connection=_(t,e));return n=function(e,t){return t?(e.audio.inbound.map((e=>{let i=t.audio.inbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesReceived"),e.packetRate=S(e,i,"packetsReceived")})),e.audio.outbound.map((e=>{let i=t.audio.outbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesSent"),e.packetRate=S(e,i,"packetsSent")})),e.video.inbound.map((e=>{let i=t.video.inbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesReceived"),e.packetRate=S(e,i,"packetsReceived")})),e.video.outbound.map((e=>{let i=t.video.outbound.find((t=>t.id===e.id));e.bitrate=S(e,i,"bytesSent"),e.packetRate=S(e,i,"packetsSent")})),e):e}(n,t),n}let I,E={},C=[];t.WebRTCStats=class extends s{constructor(e){if(super(),this.monitoringSetInterval=0,this.connectionMonitoringSetInterval=0,this.connectionMonitoringInterval=1e3,this.remote=!0,this.peersToMonitor={},this.timeline=[],this.statsToMonitor=["inbound-rtp","outbound-rtp","remote-inbound-rtp","remote-outbound-rtp","peer-connection","data-channel","stream","track","sender","receiver","transport","candidate-pair","local-candidate","remote-candidate"],"undefined"==typeof window)throw new Error("WebRTCStats only works in browser");const t={...e};this.isEdge=!!window.RTCIceGatherer,this.getStatsInterval=t.getStatsInterval||1e3,this.rawStats=!!t.rawStats,this.statsObject=!!t.statsObject,this.filteredStats=!!t.filteredStats,this.shouldWrapGetUserMedia=!!t.wrapGetUserMedia,"boolean"==typeof t.remote&&(this.remote=t.remote),this.debug=!!t.debug,this.logLevel=t.logLevel||"none",this.shouldWrapGetUserMedia&&this.wrapGetUserMedia()}async addPeer(e,t){return console.warn("The addPeer() method has been deprecated, please use addConnection()"),this.addConnection({peerId:e,pc:t})}async addConnection(e){const{pc:t,peerId:i}=e;let{connectionId:n,remote:s}=e;if(s="boolean"==typeof s?s:this.remote,!(t&&t instanceof RTCPeerConnection))throw new Error("Missing argument 'pc' or is not of instance RTCPeerConnection");if(!i)throw new Error("Missing argument peerId");if(this.isEdge)throw new Error("Can't monitor peers in Edge at this time.");if(this.peersToMonitor[i]){if(n&&n in this.peersToMonitor[i])throw new Error(`We are already monitoring connection with id ${n}.`);for(let e in this.peersToMonitor[i]){const n=this.peersToMonitor[i][e];if(n.pc===t)throw new Error(`We are already monitoring peer with id ${i}.`);"closed"===n.pc.connectionState&&this.removeConnection({pc:n.pc})}}const o=t.getConfiguration();return o.iceServers&&o.iceServers.forEach((function(e){delete e.credential})),n||(n=f()),this.emitEvent({event:"addConnection",tag:"peer",peerId:i,connectionId:n,data:{options:e,peerConfiguration:o}}),this.monitorPeer({peerId:i,connectionId:n,pc:t,remote:s}),{connectionId:n}}getTimeline(e){return this.timeline=this.timeline.sort(((e,t)=>e.timestamp.getTime()-t.timestamp.getTime())),e?this.timeline.filter((t=>t.tag===e)):this.timeline}get logger(){const e=e=>{const t=["none","error","warn","info","debug"];return t.slice(0,t.indexOf(this.logLevel)+1).indexOf(e)>-1};return{error(...t){this.debug&&e("error")&&console.error("[webrtc-stats][error] ",...t)},warn(...t){this.debug&&e("warn")&&console.warn("[webrtc-stats][warn] ",...t)},info(...t){this.debug&&e("info")&&console.log("[webrtc-stats][info] ",...t)},debug(...t){this.debug&&e("debug")&&console.debug("[webrtc-stats][debug] ",...t)}}}removeConnection(e){let t,{connectionId:i,pc:n}=e;if(!n&&!i)throw new Error("Missing arguments. You need to either send pc or a connectionId.");if(i){if("string"!=typeof i)throw new Error("connectionId must be a string.");for(let e in this.peersToMonitor)i in this.peersToMonitor[e]&&(n=this.peersToMonitor[e][i].pc,t=e)}else if(n){if(!(n instanceof RTCPeerConnection))throw new Error("pc must be an instance of RTCPeerConnection.");for(let e in this.peersToMonitor)for(let s in this.peersToMonitor[e])this.peersToMonitor[e][s].pc===n&&(i=s,t=e)}if(!n||!i)throw new Error("Could not find the desired connection.");return this.removePeerConnectionEventListeners(i,n),delete this.peersToMonitor[t][i],0===Object.values(this.peersToMonitor[t]).length&&delete this.peersToMonitor[t],{connectionId:i}}removeAllPeers(){for(let e in this.peersToMonitor)this.removePeer(e)}removePeer(e){if(this.logger.info(`Removing PeerConnection with id ${e}.`),this.peersToMonitor[e]){for(let t in this.peersToMonitor[e]){let i=this.peersToMonitor[e][t].pc;this.removePeerConnectionEventListeners(t,i)}delete this.peersToMonitor[e]}}destroy(){this.removeAllPeers(),C.forEach((e=>{this.removeTrackEventListeners(e)})),C=[],this.shouldWrapGetUserMedia&&I&&(navigator.mediaDevices.getUserMedia=I)}monitorPeer(e){let{peerId:t,connectionId:i,pc:n,remote:s}=e;if(!n)return void this.logger.warn("Did not receive pc argument when calling monitorPeer()");const o={pc:n,connectionId:i,stream:null,stats:{parsed:null,raw:null},options:{remote:s}};if(this.peersToMonitor[t]){if(i in this.peersToMonitor[t])return void this.logger.warn(`Already watching connection with ID ${i}`);this.peersToMonitor[t][i]=o}else this.peersToMonitor[t]={[i]:o};this.addPeerConnectionEventListeners(t,i,n),1===this.numberOfMonitoredPeers&&(this.startStatsMonitoring(),this.startConnectionStateMonitoring())}startStatsMonitoring(){this.monitoringSetInterval||(this.monitoringSetInterval=window.setInterval((()=>{this.numberOfMonitoredPeers||this.stopStatsMonitoring(),this.getStats().then((e=>{e.forEach((e=>{this.emitEvent(e)}))}))}),this._getStatsInterval))}stopStatsMonitoring(){this.monitoringSetInterval&&(window.clearInterval(this.monitoringSetInterval),this.monitoringSetInterval=0)}async getStats(e=null){this.logger.info(e?`Getting stats from peer ${e}`:"Getting stats from all peers");let t={};if(e){if(!this.peersToMonitor[e])throw new Error(`Cannot get stats. Peer with id ${e} does not exist`);t[e]=this.peersToMonitor[e]}else t=this.peersToMonitor;let i=[];for(const e in t)for(const n in t[e]){const s=t[e][n],o=s.pc;if(o&&!this.checkIfConnectionIsClosed(e,n,o))try{const t=this.getTimestamp(),r=o.getStats(null);if(r){const o=await r,a=this.getTimestamp(),c=b(o),d={remote:s.options.remote},l=y(o,s.stats.parsed,d),h={event:"stats",tag:"stats",peerId:e,connectionId:n,timeTaken:a-t,data:l};!0===this.rawStats&&(h.rawStats=o),!0===this.statsObject&&(h.statsObject=c),!0===this.filteredStats&&(h.filteredStats=this.filteroutStats(c)),i.push(h),s.stats.parsed=l}else this.logger.error(`PeerConnection from peer ${e} did not return any stats data`)}catch(e){this.logger.error(e)}}return i}startConnectionStateMonitoring(){this.connectionMonitoringSetInterval=window.setInterval((()=>{this.numberOfMonitoredPeers||this.stopConnectionStateMonitoring();for(const e in this.peersToMonitor)for(const t in this.peersToMonitor[e]){const i=this.peersToMonitor[e][t].pc;this.checkIfConnectionIsClosed(e,t,i)}}),this.connectionMonitoringInterval)}checkIfConnectionIsClosed(e,t,i){const n=this.isConnectionClosed(i);if(n){this.removeConnection({pc:i});let n="closed"===i.connectionState?"onconnectionstatechange":"oniceconnectionstatechange";this.emitEvent({event:n,peerId:e,connectionId:t,tag:"connection",data:"closed"})}return n}isConnectionClosed(e){return"closed"===e.connectionState||"closed"===e.iceConnectionState}stopConnectionStateMonitoring(){this.connectionMonitoringSetInterval&&(window.clearInterval(this.connectionMonitoringSetInterval),this.connectionMonitoringSetInterval=0)}wrapGetUserMedia(){if(!navigator.mediaDevices||!navigator.mediaDevices.getUserMedia)return void this.logger.warn("'navigator.mediaDevices.getUserMedia' is not available in browser. Will not wrap getUserMedia.");this.logger.info("Wrapping getUsermedia functions."),I=navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);const e=this.parseGetUserMedia.bind(this);navigator.mediaDevices.getUserMedia=function(){return e({constraints:arguments[0]}),I.apply(navigator.mediaDevices,arguments).then((t=>(e({stream:t}),t)),(t=>(e({error:t}),Promise.reject(t))))}.bind(navigator.mediaDevices)}filteroutStats(e={}){const t={...e};for(const e in t){var i=t[e];this.statsToMonitor.includes(i.type)||delete t[e]}return t}get peerConnectionListeners(){return{icecandidate:(e,t,i,n)=>{this.logger.debug("[pc-event] icecandidate | peerId: ${peerId}",n),this.emitEvent({event:"onicecandidate",tag:"connection",peerId:e,connectionId:t,data:n.candidate})},track:(e,t,i,n)=>{this.logger.debug(`[pc-event] track | peerId: ${e}`,n);const s=n.track,o=n.streams[0];e in this.peersToMonitor&&t in this.peersToMonitor[e]&&(this.peersToMonitor[e][t].stream=o),this.addTrackEventListeners(s,t),this.emitEvent({event:"ontrack",tag:"track",peerId:e,connectionId:t,data:{stream:o?this.getStreamDetails(o):null,track:s?this.getMediaTrackDetails(s):null,title:n.track.kind+":"+n.track.id+" "+n.streams.map((function(e){return"stream:"+e.id}))}})},signalingstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] signalingstatechange | peerId: ${e}`),this.emitEvent({event:"onsignalingstatechange",tag:"connection",peerId:e,connectionId:t,data:{signalingState:i.signalingState,localDescription:i.localDescription,remoteDescription:i.remoteDescription}})},iceconnectionstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] iceconnectionstatechange | peerId: ${e}`),this.emitEvent({event:"oniceconnectionstatechange",tag:"connection",peerId:e,connectionId:t,data:i.iceConnectionState})},icegatheringstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] icegatheringstatechange | peerId: ${e}`),this.emitEvent({event:"onicegatheringstatechange",tag:"connection",peerId:e,connectionId:t,data:i.iceGatheringState})},icecandidateerror:(e,t,i,n)=>{this.logger.debug(`[pc-event] icecandidateerror | peerId: ${e}`),this.emitEvent({event:"onicecandidateerror",tag:"connection",peerId:e,connectionId:t,error:{errorCode:n.errorCode}})},connectionstatechange:(e,t,i)=>{this.logger.debug(`[pc-event] connectionstatechange | peerId: ${e}`),this.emitEvent({event:"onconnectionstatechange",tag:"connection",peerId:e,connectionId:t,data:i.connectionState})},negotiationneeded:(e,t,i)=>{this.logger.debug(`[pc-event] negotiationneeded | peerId: ${e}`),this.emitEvent({event:"onnegotiationneeded",tag:"connection",peerId:e,connectionId:t})},datachannel:(e,t,i,n)=>{this.logger.debug(`[pc-event] datachannel | peerId: ${e}`,n),this.emitEvent({event:"ondatachannel",tag:"datachannel",peerId:e,connectionId:t,data:n.channel})}}}addPeerConnectionEventListeners(e,t,i){this.logger.debug(`Adding event listeners for peer ${e} and connection ${t}.`),E[t]={},Object.keys(this.peerConnectionListeners).forEach((n=>{E[t][n]=this.peerConnectionListeners[n].bind(this,e,t,i),i.addEventListener(n,E[t][n],!1)}))}parseGetUserMedia(e){try{const t={event:"getUserMedia",tag:"getUserMedia",data:{...e}};e.stream&&(t.data.details=this.parseStream(e.stream),e.stream.getTracks().map((e=>{this.addTrackEventListeners(e),C.push(e)}))),this.emitEvent(t)}catch(e){}}parseStream(e){const t={audio:[],video:[]};return e.getTracks().forEach((e=>{t[e.kind].push(this.getMediaTrackDetails(e))})),t}getMediaTrackDetails(e){return{enabled:e.enabled,id:e.id,contentHint:e.contentHint,kind:e.kind,label:e.label,muted:e.muted,readyState:e.readyState,constructorName:e.constructor.name,capabilities:e.getCapabilities?e.getCapabilities():{},constraints:e.getConstraints?e.getConstraints():{},settings:e.getSettings?e.getSettings():{},_track:e}}getStreamDetails(e){return{active:e.active,id:e.id,_stream:e}}getTrackEventObject(e){return{mute:t=>{this.emitEvent({event:"mute",tag:"track",connectionId:e,data:{event:t}})},unmute:t=>{this.emitEvent({event:"unmute",tag:"track",connectionId:e,data:{event:t}})},overconstrained:t=>{this.emitEvent({event:"overconstrained",tag:"track",connectionId:e,data:{event:t}})},ended:t=>{this.emitEvent({event:"ended",tag:"track",connectionId:e,data:{event:t}}),this.removeTrackEventListeners(t.target)}}}addTrackEventListeners(e,t){E[e.id]={};const i=this.getTrackEventObject(t);Object.keys(i).forEach((t=>{E[e.id][t]=i[t].bind(this),e.addEventListener(t,E[e.id][t])})),E[e.id].readyState=setInterval((()=>{if("ended"===e.readyState){let t=new CustomEvent("ended",{detail:{check:"readyState"}});e.dispatchEvent(t)}}),1e3)}removeTrackEventListeners(e){if(e.id in E){const t=this.getTrackEventObject();Object.keys(t).forEach((t=>{e.removeEventListener(t,E[e.id][t])})),clearInterval(E[e.id].readyState),delete E[e.id]}}addToTimeline(e){this.timeline.push(e),this.emit("timeline",e)}emitEvent(e){const t={...e,timestamp:new Date};this.addToTimeline(t),t.tag&&this.emit(t.tag,t)}set getStatsInterval(e){if(!Number.isInteger(e))throw new Error(`getStatsInterval should be an integer, got: ${e}`);this._getStatsInterval=e,this.monitoringSetInterval&&(this.stopStatsMonitoring(),this.startStatsMonitoring())}get numberOfMonitoredPeers(){return Object.keys(this.peersToMonitor).length}removePeerConnectionEventListeners(e,t){e in E&&(Object.keys(this.peerConnectionListeners).forEach((i=>{t.removeEventListener(i,E[e][i],!1)})),delete E[e]),t.getSenders().forEach((e=>{e.track&&this.removeTrackEventListeners(e.track)})),t.getReceivers().forEach((e=>{e.track&&this.removeTrackEventListeners(e.track)}))}getTimestamp(){return Date.now()}wrapGetDisplayMedia(){const e=this;if(navigator.mediaDevices&&navigator.mediaDevices.getDisplayMedia){const t=navigator.mediaDevices.getDisplayMedia.bind(navigator.mediaDevices),i=function(){return e.debug("navigator.mediaDevices.getDisplayMedia",null,arguments[0]),t.apply(navigator.mediaDevices,arguments).then((function(e){return e}),(function(t){return e.debug("navigator.mediaDevices.getDisplayMediaOnFailure",null,t.name),Promise.reject(t)}))};navigator.mediaDevices.getDisplayMedia=i.bind(navigator.mediaDevices)}}}}));(ui=pi)&&ui.__esModule&&Object.prototype.hasOwnProperty.call(ui,"default")&&ui.default;var gi=pi.WebRTCStats;function vi(e){const{packetsLost:t,packetsReceived:i,jitter:n,rtt:s}=e,o=function(e){const{jitter:t,rtt:i}=e,n=t+i/2;return.024*n+.11*(n-177.3)*(n>177.3?1:0)}({rtt:s,jitter:n}),r=function(e){const{packetsLost:t,packetsReceived:i}=e,n=t/(i+t)*100;return 20*Math.log(1+n)}({packetsLost:t,packetsReceived:i}),a=93.2-o-r+0,c=1+.035*a+7e-6*a*(a-60)*(100-a);return Math.min(Math.max(c,1),5)}function mi(e){return isNaN(e)?null:e>4.2?"excellent":e>=4.1&&e<=4.2?"good":e>=3.7&&e<=4?"fair":e>=3.1&&e<=3.6?"poor":"bad"}class fi extends vt{constructor(e,t){super(),this.buildRequest({type:"debug_report_start",debug_report_id:e,debug_report_version:1,call_id:t})}}class _i extends vt{constructor(e,t){super(),this.buildRequest({type:"debug_report_stop",debug_report_id:e,debug_report_version:1,call_id:t})}}class Si extends vt{constructor(e,t){super(),this.buildRequest({type:"debug_report_data",debug_report_id:e,debug_report_version:1,debug_report_data:t})}}function bi(t,n){const s=c();let o=!1;const r=new gi({getStatsInterval:1e3,rawStats:!1,statsObject:!0,filteredStats:!1,remote:!0,debug:!1,logLevel:"warn"}),a=n=>i(this,void 0,void 0,(function*(){"stats"===n.event&&at(e.SwEvent.StatsFrame,function({data:e}){var t,i,n,s,o,r,a,c;const{audio:d,remote:l}=e,{audio:h}=l,u=null!==(i=null===(t=h.inbound[0])||void 0===t?void 0:t.jitter)&&void 0!==i?i:1/0,p=null!==(s=null===(n=h.inbound[0])||void 0===n?void 0:n.roundTripTime)&&void 0!==s?s:1/0,g=null!==(r=null===(o=d.inbound[0])||void 0===o?void 0:o.packetsReceived)&&void 0!==r?r:-1,v=null!==(c=null===(a=d.inbound[0])||void 0===a?void 0:a.packetsLost)&&void 0!==c?c:-1,m=vi({jitter:1e3*u,rtt:1e3*p,packetsLost:v,packetsReceived:g});return{jitter:u,rtt:p,mos:m,quality:mi(m),inboundAudio:d.inbound[0],outboundAudio:d.outbound[0],remoteInboundAudio:h.inbound[0],remoteOutboundAudio:h.outbound[0]}}(n),t.uuid),yield t.execute(new Si(s,n))}));return{get isRunning(){return o},start:(e,c,d)=>i(this,void 0,void 0,(function*(){if(o)Ie.debug(`[${n}] Stats reporter already running, skipping start`);else{yield t.execute(new fi(s,n)),r.on("timeline",a);try{yield r.addConnection({pc:e,peerId:c,connectionId:d}),o=!0}catch(e){Ie.error(`[${n}] Failed to start stats reporter:`,e),r.removeAllPeers(),r.destroy()}}})),stop:a=>i(this,void 0,void 0,(function*(){const i=r.getTimeline();if(at(e.SwEvent.StatsReport,i,t.uuid),"file"===a){!function(e,t){const i=new Blob([JSON.stringify(e)],{type:"application/json"}),n=URL.createObjectURL(i),s=document.createElement("a");s.href=n,s.download=`${t}.json`,s.click(),URL.revokeObjectURL(n)}(i,`webrtc-stats-${s}-${Date.now()}`)}yield t.execute(new _i(s,n)),r.removeAllPeers(),r.destroy(),o=!1})),reportConnectionStateChange:e=>{const t={event:"connectionstatechange-detailed",tag:"connection",timestamp:(new Date).toISOString(),data:e};a(t)},reportIceCandidateError:e=>{const t={event:"icecandidateerror-detailed",tag:"connection",timestamp:(new Date).toISOString(),data:e};a(t)}}}const yi=(t,i)=>{const{contentType:n,canvasType:s,callID:o,canvasInfo:r=null,currentLayerIdx:a=-1}=i;r&&"mcu-personal-canvas"!==s&&delete r.memberID;const c={type:Ve.conferenceUpdate,call:t.calls[o],canvasInfo:Ii(r),currentLayerIdx:a};switch(n){case"layer-info":{const i=Object.assign({action:Qe.LayerInfo},c);at(e.SwEvent.Notification,i,t.uuid);break}case"layout-info":{const i=Object.assign({action:Qe.LayoutInfo},c);at(e.SwEvent.Notification,i,t.uuid);break}}},Ii=e=>{const t=JSON.stringify(e).replace(/memberID/g,"participantId").replace(/ID"/g,'Id"').replace(/POS"/g,'Pos"');return Re(t)},Ei=["new-call-start","new-peer","get-user-media","peer-creation-end","start-negotiation","create-offer","create-answer","set-local-description","ice-gathering-started","first-candidate","first-non-host-candidate","send-sdp","ice-gathering-completed","ringing","telnyx-rtc-media","first-remote-media-track","set-remote-description","telnyx-rtc-answer","ice-connected","dtls-connected","call-active","answer-called"],Ci={"new-call-start":"Call Start","new-peer":"Peer object created","get-user-media":"Media devices acquired","peer-creation-end":"Peer setup complete","start-negotiation":"SDP negotiation started","create-offer":"SDP offer generated","create-answer":"SDP answer generated","set-local-description":"Local description applied","ice-gathering-started":"ICE candidate gathering started","first-candidate":"First ICE candidate found","first-non-host-candidate":"First server-reflexive/relay candidate found","send-sdp":"SDP sent to server","ice-gathering-completed":"All ICE candidates gathered",ringing:"Remote side ringing","telnyx-rtc-media":"Early media received from server","first-remote-media-track":"First remote audio/video track received","set-remote-description":"Remote description applied","telnyx-rtc-answer":"Call answered by remote side","ice-connected":"ICE connection established","dtls-connected":"Secure media channel established (DTLS)","call-active":"Call is active","answer-called":"Answer delay (invite → call.answer)"};function wi(e,t){return`telnyx:call:${e}:${t}`}function Ti(e){try{const t=performance.getEntriesByName(e,"mark");return t.length>0?t[0].startTime:void 0}catch(e){return}}function ki(e){for(const t of Ei)try{performance.clearMarks(wi(e,t))}catch(e){Ie.warn("Clearing performance marks is failed")}}class Ri{constructor(t,n,s,o,r){this.type=t,this.options=n,this.onSdpReadyTwice=null,this.statsReporter=null,this.isIceRestarting=!1,this.iceDone=!1,this._negotiating=!1,this._prevConnectionState=null,this._restartedIceOnConnectionStateFailed=!1,this._sleepWakeupIntervalId=null,this._iceGatheringSafetyTimeout=null,this._gatheredCandidatesCount=0,this._firstMediaTrackMarked=!1,this._timingsCollected=!1,this._iceRestartTimeoutId=null,this.handleConnectionStateChange=()=>i(this,void 0,void 0,(function*(){var t,n;const{connectionState:s}=this.instance;if(Ie.info(`[${(new Date).toISOString()}] Connection State changed: ${this._prevConnectionState} -> ${s}`),"failed"!==s&&"disconnected"!==s||(this.isDebugEnabled&&this.statsReporter&&function(e,t){return i(this,void 0,void 0,(function*(){const i={connectionState:e.connectionState,previousConnectionState:t,iceConnectionState:e.iceConnectionState,iceGatheringState:e.iceGatheringState,signalingState:e.signalingState},n=e.getTransceivers();if(n.length>0){const e=n[0].sender,t=null==e?void 0:e.transport;t&&(i.dtlsState=t.state)}e.sctp&&(i.sctpState=e.sctp.state);try{const t=yield e.getStats();t.forEach((e=>{"candidate-pair"===e.type&&"succeeded"===e.state&&(i.candidatePairState=e.state,t.forEach((t=>{"local-candidate"===t.type&&t.id===e.localCandidateId&&(i.localCandidateType=t.candidateType,i.selectedCandidatePair=i.selectedCandidatePair||{local:{},remote:{}},i.selectedCandidatePair.local={address:t.address,port:t.port,protocol:t.protocol,candidateType:t.candidateType}),"remote-candidate"===t.type&&t.id===e.remoteCandidateId&&(i.remoteCandidateType=t.candidateType,i.selectedCandidatePair=i.selectedCandidatePair||{local:{},remote:{}},i.selectedCandidatePair.remote={address:t.address,port:t.port,protocol:t.protocol,candidateType:t.candidateType})}))),"transport"===e.type&&(i.dtlsCipher=e.dtlsCipher,i.srtpCipher=e.srtpCipher,i.tlsVersion=e.tlsVersion,e.dtlsState&&(i.dtlsState=e.dtlsState))}))}catch(e){Ie.error("Error gathering connection state details:",e)}return i}))}(this.instance,this._prevConnectionState).then((e=>{this.statsReporter.reportConnectionStateChange(e)})),null===(n=(t=this._session).reportPeerFailure)||void 0===n||n.call(t,this.options.id,"connection_failed")),"disconnected"===s){const t=le(H);at(e.SwEvent.Warning,{warning:t,callId:this.options.id,sessionId:this._session.sessionid},this.options.id)}if("failed"===s){const t=le(B);at(e.SwEvent.PeerConnectionFailureError,{warning:t,error:new Error(`Peer Connection failed. previous state: ${this._prevConnectionState}, current state: ${s}`),sessionId:this._session.sessionid},this.options.id)}this._prevConnectionState=s,"connected"===s&&(performance.mark(wi(this.options.id,"dtls-connected")),this.tryCollectTimings(),this._restartedIceOnConnectionStateFailed=!1),this._isTrickleIce()&&("connecting"===s&&performance.mark(wi(this.options.id,"peer-connection-connecting")),"connected"===s&&(this._clearIceGatheringSafetyTimeout(),performance.mark(wi(this.options.id,"peer-connection-connected"))))})),this._handleIceConnectionStateChange=()=>{var e,t;const i=this.instance.iceConnectionState;Ie.debug(`[${(new Date).toISOString()}] ICE Connection State`,i),"connected"===i&&performance.mark(wi(this.options.id,"ice-connected")),"failed"===i&&(null===(t=(e=this._session).reportPeerFailure)||void 0===t||t.call(e,this.options.id,"ice_failed"))},this._handleIceGatheringStateChange=()=>{const e=this.instance.iceGatheringState;Ie.debug(`[${(new Date).toISOString()}] ICE Gathering State`,e),"gathering"===e?(this._gatheredCandidatesCount=0,this._startIceGatheringSafetyTimeout()):"complete"===e&&this._clearIceGatheringSafetyTimeout()},this._setCodecs=(e,t)=>{if(e.setCodecPreferences)return e.setCodecPreferences(t)},Ie.debug("New Peer with type:",this.type,"Options:",this.options),this._constraints={offerToReceiveAudio:!0,offerToReceiveVideo:!!n.video},this._sdpReady=this._sdpReady.bind(this),this.handleSignalingStateChangeEvent=this.handleSignalingStateChangeEvent.bind(this),this.handleNegotiationNeededEvent=this.handleNegotiationNeededEvent.bind(this),this.handleTrackEvent=this.handleTrackEvent.bind(this),this.createPeerConnection=this.createPeerConnection.bind(this),this._session=s,this._trickleIceSdpFn=o,this._registerPeerEvents=r}finishIceRestart(){this.isIceRestarting&&(this.isIceRestarting=!1,this._iceRestartTimeoutId&&(clearTimeout(this._iceRestartTimeoutId),this._iceRestartTimeoutId=null))}restartIce(){return this.instance?this.isIceRestarting?(Ie.debug("ICE restart: already in progress, skipping"),{started:!1,reason:"already in progress"}):this._session.connected?(this.isIceRestarting=!0,this._restartedIceOnConnectionStateFailed=!0,this.instance.restartIce(),this.iceDone=!1,this._iceRestartTimeoutId=setTimeout((()=>{this.isIceRestarting&&(Ie.warn("ICE restart: Modify exchange timed out, clearing isIceRestarting flag"),this.isIceRestarting=!1),this._iceRestartTimeoutId=null}),Ri.ICE_RESTART_TIMEOUT_MS),Ie.info("ICE restart: initiated by signaling health monitor"),{started:!0}):(Ie.debug("ICE restart: session not connected, skipping"),{started:!1,reason:"session not connected"}):(Ie.warn("ICE restart: no RTCPeerConnection instance"),{started:!1,reason:"no RTCPeerConnection instance"})}get isOffer(){return this.type===Ge.Offer}get isAnswer(){return this.type===Ge.Answer}get isDebugEnabled(){return this.options.debug||this._session.options.debug}get debugOutput(){return this.options.debugOutput||this._session.options.debugOutput}get restartedIceOnConnectionStateFailed(){return this._restartedIceOnConnectionStateFailed}isConnectionHealthy(){return"connected"===this.instance.connectionState&&"connected"===this.instance.iceConnectionState&&"closed"!==this.instance.signalingState}startNegotiation(){performance.mark(wi(this.options.id,"start-negotiation")),this._negotiating=!0,this._isOffer()||this.isIceRestarting?this._createOffer():this._createAnswer()}startTrickleIceNegotiation(){return i(this,void 0,void 0,(function*(){performance.mark(wi(this.options.id,"start-negotiation")),this._negotiating=!0,this._isOffer()||this.isIceRestarting?yield this._createOffer().then(this._trickleIceSdpFn.bind(this)):yield this._createAnswer().then(this._trickleIceSdpFn.bind(this))}))}_logTransceivers(){this.instance?(Ie.info("Number of transceivers:",this.instance.getTransceivers().length),this.instance.getTransceivers().forEach(((e,t)=>{Ie.info(`>> Transceiver [${t}]:`,e.mid,e.direction,e.stopped),Ie.info(`>> Sender Params [${t}]:`,JSON.stringify(e.sender.getParameters(),null,2))}))):Ie.warn("Cannot log transceivers: peer connection is null")}handleSignalingStateChangeEvent(){switch(Ie.info("signalingState:",this.instance.signalingState),this.instance.signalingState){case"stable":this._negotiating=!1;break;case"closed":at(e.SwEvent.PeerConnectionSignalingStateClosed,{sessionId:this._session.sessionid},this.options.id),this.instance&&(Ie.debug(`[${this.options.id}] Closing peer due to signalingState closed`),this.close());break;default:this._negotiating=!0}}handleNegotiationNeededEvent(){Ie.info("Negotiation needed event"),"stable"!==this.instance.signalingState||this._negotiating?Ie.debug("Skipping negotiation, state:",this.instance.signalingState,"negotiating:",this._negotiating):this._isTrickleIce()&&!this.isIceRestarting?this.startTrickleIceNegotiation():this.startNegotiation()}handleTrackEvent(e){this._firstMediaTrackMarked||(performance.mark(wi(this.options.id,"first-remote-media-track")),this._firstMediaTrackMarked=!0);const{streams:[t]}=e,{remoteElement:i,screenShare:n}=this.options;this.options.remoteStream=t,!1===n&&Ht(i,this.options.remoteStream)}tryCollectTimings(){if(this._timingsCollected)return;if(!(performance.getEntriesByName(wi(this.options.id,"call-active"),"mark").length>0)||"connected"!==this.instance.connectionState)return;this._timingsCollected=!0;const e=this._isTrickleIce()?"trickle":"non-trickle",t=this.isOffer?"outbound":"inbound",i=function(e,t,i){const n=Ti(wi(e,"new-call-start"));if(void 0===n)return{mode:t,direction:i,steps:[]};const s=[];for(const t of Ei){if("new-call-start"===t)continue;const i=Ti(wi(e,t));void 0!==i&&s.push({label:Ci[t]||t,fromStart:i-n})}s.sort(((e,t)=>e.fromStart-t.fromStart));const o=[];let r=0;for(const e of s)o.push({label:e.label,fromStart:e.fromStart,delta:e.fromStart-r}),r=e.fromStart;return{mode:t,direction:i,steps:o}}(this.options.id,e,t);!function(e){const{mode:t,direction:i,steps:n}=e,s=`[CallTimings][${i}][${t}]`;if(0===n.length)return void Ie.info(`${s} No timing data collected`);const o=Math.max(...n.map((e=>e.label.length)),4)+2,r=(e,t)=>{for(;e.length<t;)e+=" ";return e},a=(e,t)=>{for(;e.length<t;)e=" "+e;return e},c=r("Step",o)+a("Delta",14)+a("From Start",14);let d="";for(let e=0;e<c.length;e++)d+="-";Ie.info(`${s} Call establishment timing breakdown:`),Ie.info(`${s} ${c}`),Ie.info(`${s} ${d}`),Ie.info(`${s} ${r("Call Start",o)}${a("-",14)}${a("0.00ms",14)}`);for(const e of n){const t=e.delta.toFixed(2)+"ms",i=e.fromStart.toFixed(2)+"ms";Ie.info(`${s} ${r(e.label,o)}${a(t,14)}${a(i,14)}`)}Ie.info(`${s} ${d}`)}(i),ki(this.options.id)}createPeerConnection(){return i(this,void 0,void 0,(function*(){var t;if(this.instance=(t=this._config(),new window.RTCPeerConnection(t)),this.instance.onsignalingstatechange=this.handleSignalingStateChangeEvent,this.instance.onnegotiationneeded=this.handleNegotiationNeededEvent,this.instance.ontrack=this.handleTrackEvent,this.instance.addEventListener("connectionstatechange",this.handleConnectionStateChange),this.instance.addEventListener("iceconnectionstatechange",this._handleIceConnectionStateChange),this.instance.addEventListener("icegatheringstatechange",this._handleIceGatheringStateChange),this.instance.addEventListener("addstream",(e=>{this.options.remoteStream=e.stream})),this._registerPeerEvents(this.instance),this._prevConnectionState=this.instance.connectionState,this.isAnswer&&(yield this._setRemoteDescription({sdp:this.options.remoteSdp,type:Ge.Offer}),performance.mark(wi(this.options.id,"set-remote-description")),!this.instance))throw pe(E);const n=Boolean(this.options.receiveOnlyAudio)&&!this.options.audio;let s=null;if(this.options.localStream=yield this._retrieveLocalStream().catch((t=>i(this,void 0,void 0,(function*(){const n=this._session.options.mediaPermissionsRecovery;if((null==n?void 0:n.enabled)&&this._isAnswer()){let o=null,r=null;return yield new Promise(((i,s)=>{r=setTimeout((()=>s(new Error("Media recovery flow timed out!"))),n.timeout),at(e.SwEvent.Error,{error:pe(ue(t),t),callId:this.options.id,sessionId:this._session.sessionid,recoverable:!0,retryDeadline:Date.now()+n.timeout,resume:()=>{i()},reject:()=>{s(new Error("Call was rejected during media recovery flow!"))}},this._session.uuid)})).then((()=>i(this,void 0,void 0,(function*(){var e;r&&(clearTimeout(r),r=null),o=yield this._retrieveLocalStream(),null===(e=n.onSuccess)||void 0===e||e.call(n)})))).catch((e=>{var t;r&&(clearTimeout(r),r=null),s=e,null===(t=n.onError)||void 0===t||t.call(n,e)})),o}return s=t,null})))),!this.instance)throw pe(E);if(!this.options.localStream&&!n){throw pe(s?ue(s):_,null!=s?s:void 0)}performance.mark(wi(this.options.id,"get-user-media")),this.options.mutedMicOnStart&&jt(this.options.localStream)&&(Ie.info("Muting local audio tracks on start"),ti(this.options.localStream)),performance.mark(wi(this.options.id,"peer-creation-end"))}))}incrementGatheredCandidates(){this._gatheredCandidatesCount++}_startIceGatheringSafetyTimeout(){this._clearIceGatheringSafetyTimeout(),this._iceGatheringSafetyTimeout=setTimeout((()=>{if(this.instance)if(0===this._gatheredCandidatesCount){const t=le(W);at(e.SwEvent.Warning,{warning:t,callId:this.options.id,sessionId:this._session.sessionid},this.options.id)}else if("complete"!==this.instance.iceGatheringState){const t=le(G);at(e.SwEvent.Warning,{warning:t,callId:this.options.id,sessionId:this._session.sessionid},this.options.id)}}),Ri.ICE_GATHERING_SAFETY_TIMEOUT_MS)}_clearIceGatheringSafetyTimeout(){null!==this._iceGatheringSafetyTimeout&&(clearTimeout(this._iceGatheringSafetyTimeout),this._iceGatheringSafetyTimeout=null)}init(){var e;return i(this,void 0,void 0,(function*(){if(yield this.createPeerConnection(),!this.instance)throw pe(E);this.isDebugEnabled&&(this.statsReporter=bi(this._session,this.options.id),yield null===(e=this.statsReporter)||void 0===e?void 0:e.start(this.instance,this._session.sessionid,this._session.sessionid));const{localElement:t,localStream:i=null,screenShare:n=!1}=this.options;if(jt(i)){const e=i.getAudioTracks();let s=[...e];if(Ie.info("Local audio tracks: ",e),"object"==typeof this.options.audio&&e.forEach((e=>{Ie.info("Local audio tracks constraints: ",e.getConstraints())})),this.options.video){const t=i.getVideoTracks();s=[...e,...t],Ie.info("Local video tracks: ",t),"object"==typeof this.options.video&&t.forEach((e=>{Ie.info("Local video tracks constraints: ",e.getConstraints())}))}const{audioCodecs:o,videoCodecs:r}=di(this.options.preferred_codecs);if(this.isOffer&&"function"==typeof this.instance.addTransceiver){const e={direction:"sendrecv",streams:[i]};s.forEach((t=>{"audio"===t.kind&&(this.options.userVariables.microphoneLabel=t.label),"video"===t.kind&&(this.options.userVariables.cameraLabel=t.label);const i=this.instance.addTransceiver(t,e);"audio"===t.kind&&o.length>0&&this._setCodecs(i,o),"video"===t.kind&&r.length>0&&this._setCodecs(i,r)}))}else"function"==typeof this.instance.addTrack?(s.forEach((e=>{"audio"===e.kind&&(this.options.userVariables.microphoneLabel=e.label),"video"===e.kind&&(this.options.userVariables.cameraLabel=e.label),this.instance.addTrack(e,i)})),this.instance.getTransceivers().forEach((e=>{"audio"===e.receiver.track.kind&&o.length>0&&this._setCodecs(e,o),"video"===e.receiver.track.kind&&r.length>0&&this._setCodecs(e,r)}))):this.instance.addStream(i);!1===n&&Ht(t,i)}else if(this.options.receiveOnlyAudio&&"function"==typeof this.instance.addTransceiver){const e=this.instance.addTransceiver("audio",{direction:"recvonly"});Ie.info("Added recvonly audio transceiver for receive-only mode",e);const{audioCodecs:t}=di(this.options.preferred_codecs);t.length>0&&this._setCodecs(e,t)}this.isOffer?(this.options.negotiateAudio&&this._checkMediaToNegotiate("audio"),this.options.negotiateVideo&&this._checkMediaToNegotiate("video")):this._isTrickleIce()||this.startNegotiation(),this._isTrickleIce()&&this.startTrickleIceNegotiation(),this._logTransceivers()}))}_getSenderByKind(e){return this.instance.getSenders().find((({track:t})=>t&&t.kind===e))}_checkMediaToNegotiate(e){if(!this._getSenderByKind(e)){const t=this.instance.addTransceiver(e);Ie.info("Add transceiver",e,t)}}_createOffer(){return i(this,void 0,void 0,(function*(){if(this._isOffer()||this.isIceRestarting){this._constraints.offerToReceiveAudio=!1!==this.options.audio||Boolean(this.options.receiveOnlyAudio),this._constraints.offerToReceiveVideo=Boolean(this.options.video),Ie.info("_createOffer - this._constraints",this._constraints);try{const e=yield this.instance.createOffer(this._constraints);return performance.mark(wi(this.options.id,"create-offer")),yield this._setLocalDescription(e),performance.mark(wi(this.options.id,"set-local-description")),performance.mark(wi(this.options.id,"ice-gathering-started")),this._isTrickleIce()||this._sdpReady(),e}catch(t){Ie.error("Peer _createOffer error:",t);const i=pe(h,t);at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id)}}}))}_setRemoteDescription(t){return i(this,void 0,void 0,(function*(){Ie.debug("Setting remote description",t);try{yield this.instance.setRemoteDescription(t)}catch(t){Ie.error("Peer _setRemoteDescription error:",t);const i=pe(g,t);throw at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id),t}}))}_createAnswer(){return i(this,void 0,void 0,(function*(){if(this._isAnswer()){if("stable"!==this.instance.signalingState&&"have-remote-offer"!==this.instance.signalingState)return Ie.debug("Skipping negotiation, state:",this.instance.signalingState),Ie.debug(" - But the signaling state isn't stable, so triggering rollback"),void(yield Promise.all([this.instance.setLocalDescription({type:"rollback"}),this.instance.setRemoteDescription({sdp:this.options.remoteSdp,type:Ge.Offer})]));this._logTransceivers();try{const e=yield this.instance.createAnswer();return performance.mark(wi(this.options.id,"create-answer")),yield this._setLocalDescription(e),performance.mark(wi(this.options.id,"set-local-description")),performance.mark(wi(this.options.id,"ice-gathering-started")),e}catch(t){Ie.error("Peer _createAnswer error:",t);const i=pe(u,t);at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id)}}}))}_setLocalDescription(t){return i(this,void 0,void 0,(function*(){try{yield this.instance.setLocalDescription(t)}catch(t){Ie.error("Peer _setLocalDescription error:",t);const i=pe(p,t);throw at(e.SwEvent.Error,{error:i,sessionId:this._session.sessionid},this.options.id),t}}))}_sdpReady(){Ae(this.onSdpReadyTwice)&&this.onSdpReadyTwice(this.instance.localDescription)}_retrieveLocalStream(){return i(this,void 0,void 0,(function*(){if(jt(this.options.localStream))return this.options.localStream;const e=yield(t=this.options,i(void 0,void 0,void 0,(function*(){let{audio:e=!0,micId:i,video:n=!1,camId:s}=t;const{micLabel:o="",camLabel:r=""}=t;return i&&(i=yield Jt(i,o,Xe.AudioIn).catch((e=>null)),i&&("boolean"==typeof e&&(e={}),e.deviceId={exact:i})),s&&(s=yield Jt(s,r,Xe.Video).catch((e=>null)),s&&("boolean"==typeof n&&(n={}),n.deviceId={exact:s})),{audio:e,video:n}})));var t;return qt(e)}))}_isOffer(){return this.type===Ge.Offer}_isAnswer(){return this.type===Ge.Answer}_isTrickleIce(){return!0===this.options.trickleIce}_config(){const{prefetchIceCandidates:e,forceRelayCandidate:t,iceServers:i}=this.options,n={bundlePolicy:"balanced",iceCandidatePoolSize:e?10:0,iceServers:i,iceTransportPolicy:t?"relay":"all"};return Ie.info("RTC config",n),n}restartStatsReporter(){return i(this,void 0,void 0,(function*(){this.isDebugEnabled&&this.statsReporter&&(this.instance?this.statsReporter.isRunning?Ie.debug(`[${this.options.id}] Stats reporter already running, skipping restart`):(Ie.debug(`[${this.options.id}] Restarting stats reporter after reconnect`),yield this.statsReporter.start(this.instance,this._session.sessionid,this._session.sessionid)):Ie.debug(`[${this.options.id}] Cannot restart stats reporter - no peer connection instance`))}))}close(){return i(this,void 0,void 0,(function*(){ki(this.options.id),this.finishIceRestart(),this._clearIceGatheringSafetyTimeout(),null!==this._sleepWakeupIntervalId&&(clearInterval(this._sleepWakeupIntervalId),this._sleepWakeupIntervalId=null),this.isDebugEnabled&&this.statsReporter&&(yield this.statsReporter.stop(this.debugOutput)),this.instance&&(this.instance.close(),this.instance=null)}))}}Ri.ICE_GATHERING_SAFETY_TIMEOUT_MS=15e3,Ri.ICE_RESTART_TIMEOUT_MS=15e3;const Ai=Et;class Oi{constructor(e,t){this.session=e,this._callReportCollector=null,this.id="",this.recoveredCallId="",this.state=Ke[Ke.New],this.prevState="",this.channels=[],this.role=Je.Participant,this.extension=null,this._state=Ke.New,this._prevState=Ke.New,this.gotAnswer=!1,this.gotEarly=!1,this._lastSerno=0,this._targetNodeId=null,this._iceTimeout=null,this._statsBindings=[],this._statsIntervalId=null,this._pendingIceCandidates=[],this._isRemoteDescriptionSet=!1,this._signalingStateClosed=!1,this._creatingPeer=!1,this._firstCandidateSent=!1,this._firstNonHostCandidateSent=!1,this._isRecovering=!1,this._checkConferenceSerno=e=>{const t=e<0||!this._lastSerno||this._lastSerno&&e===this._lastSerno+1;return t&&e>=0&&(this._lastSerno=e),t},this._doStats=()=>{this.peer&&this.peer.instance&&0!==this._statsBindings.length&&this.peer.instance.getStats().then((e=>{e.forEach((e=>{this._statsBindings.forEach((t=>{if(t.callback){if(t.constraints)for(const i in t.constraints)if(t.constraints.hasOwnProperty(i)&&t.constraints[i]!==e[i])return;t.callback(e)}}))}))}))};const{iceServers:i,speaker:n,micId:s,micLabel:o,camId:r,camLabel:a,localElement:c,remoteElement:d,options:l,mediaConstraints:{audio:h,video:u},ringtoneFile:p,ringbackFile:g}=e;this.options=Object.assign({},Ye,{audio:h,video:u,iceServers:(null==t?void 0:t.iceServers)&&Array.isArray(t.iceServers)?t.iceServers:i,localElement:c,remoteElement:d,micId:s,micLabel:o,camId:r,camLabel:a,speakerId:n,ringtoneFile:p,ringbackFile:g,debug:l.debug,debugOutput:l.debugOutput,trickleIce:l.trickleIce,prefetchIceCandidates:l.prefetchIceCandidates,forceRelayCandidate:l.forceRelayCandidate,keepConnectionAliveOnSocketClose:l.keepConnectionAliveOnSocketClose,mutedMicOnStart:l.mutedMicOnStart},t),this._onMediaError=this._onMediaError.bind(this),this._onPeerConnectionFailureError=this._onPeerConnectionFailureError.bind(this),this._onPeerConnectionSignalingStateClosed=this._onPeerConnectionSignalingStateClosed.bind(this),this._onTrickleIceSdp=this._onTrickleIceSdp.bind(this),this._registerPeerEvents=this._registerPeerEvents.bind(this),this._init(),this.options&&(this._ringtone=ri(this.options.ringtoneFile,"_ringtone"),this._ringback=ri(this.options.ringbackFile,"_ringback"))}get creatingPeer(){return this._creatingPeer}get signalingStateClosed(){return this._signalingStateClosed}_captureHangupCallerStack(){const e=new Error("Call.hangup caller").stack;return e?e.split("\n").map((e=>e.trim())).filter(Boolean).slice(1,11):[]}get nodeId(){return this._targetNodeId}set nodeId(e){this._targetNodeId=e}get isVideoCall(){return!!this.options.video}get telnyxIDs(){return{telnyxCallControlId:this.options.telnyxCallControlId,telnyxSessionId:this.options.telnyxSessionId,telnyxLegId:this.options.telnyxLegId}}get localStream(){return this.options.localStream}get remoteStream(){return this.options.remoteStream}get memberChannel(){return`conference-member.${this.id}`}get isAudioMuted(){return!ni(this.options.localStream)}_hasActiveUnmutedLocalAudioTrack(){const e=this.options.localStream;return!!(null==e?void 0:e.getAudioTracks)&&e.getAudioTracks().some((e=>!0===e.enabled&&!0!==e.muted&&"live"===e.readyState))}shouldForceRelayCandidateForRecovery(){var e,t;return!this.options.forceRelayCandidate&&(!!this.recoveredCallId&&(null!==(t=null===(e=this._callReportCollector)||void 0===e?void 0:e.shouldForceRelayCandidateForRecovery())&&void 0!==t&&t))}invite(){return i(this,void 0,void 0,(function*(){this._creatingPeer=!0,this.direction=We.Outbound,this.options.trickleIce&&this._resetTrickleIceCandidateState(),performance.mark(wi(this.id,"new-peer")),this.peer=new Ri(Ge.Offer,this.options,this.session,this._onTrickleIceSdp,this._registerPeerEvents);try{yield this.peer.init()}catch(t){Ie.error("Peer init failed, aborting call",t),this._creatingPeer=!1;const i=t instanceof he?t:pe(D,t instanceof Error?t:void 0);return at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid,recoverable:!1},this.session.uuid),void this.hangup({initiator:"sdk:peer-init-failed"},!1)}this._creatingPeer=!1}))}answer(t={}){var n,s,o,r,a;return i(this,void 0,void 0,(function*(){if(this._creatingPeer||(null===(n=this.peer)||void 0===n?void 0:n.instance)&&"closed"!==this.peer.instance.signalingState){const t=le(q);return at(e.SwEvent.Warning,{warning:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),void Ie.warn(`[${this.id}] answer() ignored: peer connection already exists or is being created (signalingState: ${null!==(r=null===(o=null===(s=this.peer)||void 0===s?void 0:s.instance)||void 0===o?void 0:o.signalingState)&&void 0!==r?r:"creating"})`)}if(this._registerInboundAnswerAttempt()){performance.mark(wi(this.id,"answer-called")),this._creatingPeer=!0,this.stopRingtone(),this.direction=We.Inbound,(null===(a=null==t?void 0:t.customHeaders)||void 0===a?void 0:a.length)>0&&(this.options=Object.assign(Object.assign({},this.options),{customHeaders:t.customHeaders})),this.options.trickleIce&&this._resetTrickleIceCandidateState(),performance.mark(wi(this.id,"new-peer")),this.peer=new Ri(Ge.Answer,this.options,this.session,this._onTrickleIceSdp,this._registerPeerEvents);try{yield this.peer.init()}catch(t){Ie.error("Peer init failed, aborting call",t),this._creatingPeer=!1;const i=t instanceof he?t:pe(D,t instanceof Error?t:void 0);return at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid,recoverable:!1},this.session.uuid),void(yield this.hangup({initiator:"sdk:peer-init-failed"},!0))}this._creatingPeer=!1}}))}playRingtone(){ai(this._ringtone)}stopRingtone(){ci(this._ringtone)}playRingback(){ai(this._ringback)}stopRingback(){ci(this._ringback)}hangup(t,n){var s,o,r,a,c,d;return i(this,void 0,void 0,(function*(){const i=t||{},l=!1!==n,h=this.state,u=this.prevState,p=this._captureHangupCallerStack(),g=i.initiator||"app:call.hangup",v=this._state<Ke.Active?{cause:"USER_BUSY",causeCode:17}:{cause:"NORMAL_CLEARING",causeCode:16};if(this.cause=i.cause||v.cause,this.causeCode=i.causeCode||v.causeCode,this.sipCode=i.sipCode||null,this.sipReason=i.sipReason||null,this.sipCallId=i.sip_call_id||null,this.options.customHeaders=[...null!==(s=this.options.customHeaders)&&void 0!==s?s:[],...null!==(r=null===(o=null==i?void 0:i.dialogParams)||void 0===o?void 0:o.customHeaders)&&void 0!==r?r:[]],Ie.debug(`[${this.id}] hangup() invoked`,{callId:this.id,execute:l,state:h,prevState:u,cause:this.cause,causeCode:this.causeCode,initiator:g,sipCode:this.sipCode,sipReason:this.sipReason,sipCallId:this.sipCallId,isRecovering:Boolean(i.isRecovering),hasDialogCustomHeaders:Boolean(null===(c=null===(a=i.dialogParams)||void 0===a?void 0:a.customHeaders)||void 0===c?void 0:c.length),callerStack:p}),i.isRecovering)return this._isRecovering=!0,this.setState(Ke.Recovering),void this._finalize();if(this.setState(Ke.Hangup),this.stopRingtone(),this.stopRingback(),l){const t=new At({sipCode:this.sipCode,sip_call_id:this.sipCallId,sessid:this.session.sessionid,dialogParams:this.options,cause:this.cause,causeCode:this.causeCode});try{yield this._execute(t)}catch(t){Ie.error("telnyx_rtc.bye failed!",t);const i=pe(y,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}}Ie.debug(`[${this.id}] Closing peer from hangup`),null===(d=this.peer)||void 0===d||d.close(),this.setState(Ke.Destroy)}))}hold(){const e=new Lt({sessid:this.session.sessionid,action:Ze.Hold,dialogParams:this.options});return this._execute(e).then(this._handleChangeHoldStateSuccess.bind(this)).catch(this._handleChangeHoldStateError.bind(this))}unhold(){const e=new Lt({sessid:this.session.sessionid,action:Ze.Unhold,dialogParams:this.options});return this._execute(e).then(this._handleChangeHoldStateSuccess.bind(this)).catch(this._handleChangeHoldStateError.bind(this))}toggleHold(){const e=new Lt({sessid:this.session.sessionid,action:Ze.ToggleHold,dialogParams:this.options});return this._execute(e).then(this._handleChangeHoldStateSuccess.bind(this)).catch(this._handleChangeHoldStateError.bind(this))}dtmf(e){const t=new Dt({sessid:this.session.sessionid,dtmf:e,dialogParams:this.options});this._execute(t)}message(e,t){const i={from:this.session.options.login,to:e,body:t},n=new Dt({sessid:this.session.sessionid,msg:i,dialogParams:this.options});this._execute(n)}muteAudio(){ti(this.options.localStream)}unmuteAudio(){ei(this.options.localStream)}toggleAudioMute(){ii(this.options.localStream)}setAudioInDevice(t,n=this.options.mutedMicOnStart){var s;return i(this,void 0,void 0,(function*(){const{instance:i}=this.peer,o=i.getSenders().find((({track:{kind:e}})=>"audio"===e));if(o){let i;try{i=yield $t({audio:{deviceId:{exact:t}}})}catch(t){const i=pe(ue(t),t);return void at(e.SwEvent.MediaError,i,(null===(s=this.options)||void 0===s?void 0:s.id)||this.id)}const r=i.getAudioTracks()[0];r.enabled=!n,o.replaceTrack(r),this.options.micId=t;const{localStream:a}=this.options;a.getAudioTracks().forEach((e=>e.stop())),a.getVideoTracks().forEach((e=>i.addTrack(e))),this.options.localStream=i}}))}muteVideo(){var e;e=this.options.localStream,Zt(e,"video",!1)}unmuteVideo(){var e;e=this.options.localStream,Zt(e,"video",!0)}toggleVideoMute(){var e;e=this.options.localStream,Zt(e,"video",null)}setVideoDevice(e){return i(this,void 0,void 0,(function*(){const{instance:t}=this.peer,i=t.getSenders().find((({track:{kind:e}})=>"video"===e));if(i){const t=yield $t({video:{deviceId:{exact:e}}}),n=t.getVideoTracks()[0];i.replaceTrack(n);const{localElement:s,localStream:o}=this.options;Ht(s,t),this.options.camId=e,o.getAudioTracks().forEach((e=>t.addTrack(e))),o.getVideoTracks().forEach((e=>e.stop())),this.options.localStream=t}}))}deaf(){ti(this.options.remoteStream)}undeaf(){ei(this.options.remoteStream)}toggleDeaf(){ii(this.options.remoteStream)}setBandwidthEncodingsMaxBps(e,t){return i(this,void 0,void 0,(function*(){if(!this||!this.peer)return void Ie.error("Could not set bandwidth (reason: no peer connection). Dynamic bandwidth can only be set when there is a call running - is there any call running?)");const{instance:i}=this.peer,n=i.getSenders();if(!n)return void Ie.error("Could not set bandwidth (reason: no senders). Dynamic bandwidth can only be set when there is a call running - is there any call running?)");const s=n.find((({track:{kind:e}})=>e===t));if(s){const i=s.getParameters();i.encodings||(i.encodings=[{rid:"h"}]),Ie.info("Parameters: ",i),Ie.info("Setting max ","audio"===t?"audio":"video"," bandwidth to: ",e," [bps]"),i.encodings[0].maxBitrate=e,yield s.setParameters(i).then((()=>{Ie.info("audio"===t?"New audio":"New video"," bandwidth settings in use: ",s.getParameters())})).catch((e=>Ie.error(e)))}else Ie.error("Could not set bandwidth (reason: no "+t+" sender). Dynamic bandwidth can only be set when there is a call running - is there any call running?)")}))}setAudioBandwidthEncodingsMaxBps(e){this.setBandwidthEncodingsMaxBps(e,"audio")}setVideoBandwidthEncodingsMaxBps(e){this.setBandwidthEncodingsMaxBps(e,"video")}_isTerminatingOrTerminated(){return[Ke.Hangup,Ke.Destroy,Ke.Purge].includes(this._state)}getStats(e,t){if(!e)return;const i={callback:e,constraints:t};if(this._statsBindings.push(i),!this._statsIntervalId){const e=2e3;this._startStats(e)}}setState(e){var t,i;switch(this._prevState=this._state,this._state=e,this.state=Ke[this._state].toLowerCase(),this.prevState=Ke[this._prevState].toLowerCase(),Ie.debug(`Call ${this.id} state change from ${this.prevState} to ${this.state}`),this._dispatchNotification({type:Ve.callUpdate,call:this}),e){case Ke.Purge:Ie.info(`[${this.id}] Entering Purge state.`);break;case Ke.Active:performance.mark(wi(this.id,"call-active")),null===(t=this.peer)||void 0===t||t.tryCollectTimings(),this._isRecovering&&(this._isRecovering=!1,Ie.debug(`[${this.id}] Recovery complete, call is active`)),this.session.startSignalingHealthMonitor(),setTimeout((()=>{const{remoteElement:e,speakerId:t}=this.options;e&&t&&Wt(e,t)}),0),this._callReportCollector&&(null===(i=this.peer)||void 0===i?void 0:i.instance)&&this.session.callReportId&&this._callReportCollector.start(this.peer.instance);break;case Ke.Destroy:this._finalize()}}handleMessage(t){const{method:i,params:n}=t;switch(i){case Be.Answer:if(performance.mark(wi(this.id,"telnyx-rtc-answer")),this.gotAnswer=!0,n.telnyx_call_control_id&&(this.options.telnyxCallControlId=n.telnyx_call_control_id),n.telnyx_session_id&&(this.options.telnyxSessionId=n.telnyx_session_id),n.telnyx_leg_id&&(this.options.telnyxLegId=n.telnyx_leg_id),this._state>=Ke.Active)return;this._state>=Ke.Early&&this.setState(Ke.Active),this.gotEarly||this._onRemoteSdp(n.sdp),this.stopRingback(),this.stopRingtone();break;case Be.Media:if(performance.mark(wi(this.id,"telnyx-rtc-media")),this._state>=Ke.Early)return;this.gotEarly=!0,this._onRemoteSdp(n.sdp);break;case Be.Display:{const{display_name:t,display_number:s,display_direction:o}=n;this.extension=s;const r=o===We.Inbound?We.Outbound:We.Inbound,a={type:Ve[i],call:this,displayName:t,displayNumber:s,displayDirection:r};at(e.SwEvent.Notification,a,this.id)||at(e.SwEvent.Notification,a,this.session.uuid);break}case Be.Candidate:this._addIceCandidate(n);break;case Be.Info:case Be.Event:{const t=Object.assign(Object.assign({},n),{type:Ve.generic,call:this});at(e.SwEvent.Notification,t,this.id)||at(e.SwEvent.Notification,t,this.session.uuid);break}case Be.Ringing:performance.mark(wi(this.id,"ringing")),this.playRingback(),n.telnyx_call_control_id&&(this.options.telnyxCallControlId=n.telnyx_call_control_id),n.telnyx_session_id&&(this.options.telnyxSessionId=n.telnyx_session_id),n.telnyx_leg_id&&(this.options.telnyxLegId=n.telnyx_leg_id);break;case Be.Bye:const t=n.client_state||n.clientState;t&&(this.options.clientState=t),this.stopRingback(),this.stopRingtone(),this.hangup(Object.assign(Object.assign({},n),{initiator:"remote:telnyx_rtc.bye"}),!1)}}handleConferenceUpdate(e,t){return i(this,void 0,void 0,(function*(){if(!this._checkConferenceSerno(e.wireSerno)&&e.name!==t.laName)return Ie.error("ConferenceUpdate invalid wireSerno or packet name:",e),"INVALID_PACKET";const{action:i,data:n,hashKey:s=String(this._lastSerno),arrIndex:o}=e;switch(i){case"bootObj":{this._lastSerno=0;const{chatChannel:e,infoChannel:i,modChannel:s,laName:o,conferenceMemberID:r,role:a}=t;this._dispatchConferenceUpdate({action:Qe.Join,conferenceName:o,participantId:Number(r),role:a}),e&&(yield this._subscribeConferenceChat(e)),i&&(yield this._subscribeConferenceInfo(i));const c=[];for(const e in n)c.push(Object.assign({callId:n[e][0],index:Number(e)},ke(n[e][1])));this._dispatchConferenceUpdate({action:Qe.Bootstrap,participants:c});break}case"add":this._dispatchConferenceUpdate(Object.assign({action:Qe.Add,callId:s,index:o},ke(n)));break;case"modify":this._dispatchConferenceUpdate(Object.assign({action:Qe.Modify,callId:s,index:o},ke(n)));break;case"del":this._dispatchConferenceUpdate(Object.assign({action:Qe.Delete,callId:s,index:o},ke(n)));break;case"clear":this._dispatchConferenceUpdate({action:Qe.Clear});break;default:this._dispatchConferenceUpdate({action:i,data:n,callId:s,index:o})}}))}_addChannel(e){this.channels.includes(e)||this.channels.push(e);const t=this.session.relayProtocol;this.session._existsSubscription(t,e)&&(this.session.subscriptions[t][e]=Object.assign(Object.assign({},this.session.subscriptions[t][e]),{callId:this.id}))}_subscribeConferenceChat(e){return i(this,void 0,void 0,(function*(){const t={nodeId:this.nodeId,channels:[e],handler:e=>{const{direction:t,from:i,fromDisplay:n,message:s,type:o}=e.data;this._dispatchConferenceUpdate({action:Qe.ChatMessage,direction:t,participantNumber:i,participantName:n,messageText:s,messageType:o,messageId:e.eventSerno})}},i=yield this.session.vertoSubscribe(t).catch((e=>{Ie.error("ConfChat subscription error:",e)}));Xt(i,e)&&(this._addChannel(e),Object.defineProperties(this,{sendChatMessage:{configurable:!0,value:(t,i)=>{this.session.vertoBroadcast({nodeId:this.nodeId,channel:e,data:{action:"send",message:t,type:i}})}}}))}))}_subscribeConferenceInfo(e){return i(this,void 0,void 0,(function*(){const t={nodeId:this.nodeId,channels:[e],handler:e=>{const{eventData:t}=e;if("layout-info"===t.contentType)t.callID=this.id,yi(this.session,t);else Ie.error("Conference-Info unknown contentType",e)}},i=yield this.session.vertoSubscribe(t).catch((e=>{Ie.error("ConfInfo subscription error:",e)}));Xt(i,e)&&this._addChannel(e)}))}_confControl(e,t={}){const i=Object.assign({application:"conf-control",callID:this.id,value:null},t);this.session.vertoBroadcast({nodeId:this.nodeId,channel:e,data:i})}_handleChangeHoldStateSuccess(e){return"active"===e.holdState?this.setState(Ke.Active):this.setState(Ke.Held),!0}_handleChangeHoldStateError(t){Ie.error(`Failed to ${t.action} on call ${this.id}`);const i=pe(S,t);return at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),!1}_sendIceRestartModify(e){const t=new Lt({sessid:this.session.sessionid,action:Ze.UpdateMedia,callID:this.options.id,sdp:e,dialogParams:this.options});Ie.info("ICE restart: sending Modify with new offer SDP"),this._execute(t).then((e=>i(this,void 0,void 0,(function*(){var t;(null==e?void 0:e.sdp)?(Ie.info("ICE restart Modify response received"),null===(t=this.peer)||void 0===t||t.finishIceRestart(),yield this._onRemoteSdp(e.sdp)):this._onIceRestartFailed("ICE restart Modify response missing SDP")})))).catch((e=>{this._onIceRestartFailed("ICE restart Modify failed",e)}))}_onIceRestartFailed(t,i){var n;Ie.error(t,i),null===(n=this.peer)||void 0===n||n.finishIceRestart();const s=pe(N,i);at(e.SwEvent.Error,{error:s,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}_onRemoteSdp(t){return i(this,void 0,void 0,(function*(){const n=new RTCSessionDescription({sdp:t,type:Ge.Answer});yield this.peer.instance.setRemoteDescription(n).then((()=>{performance.mark(wi(this.id,"set-remote-description")),this.options.trickleIce&&(this._isRemoteDescriptionSet=!0,this._flushPendingTrickleIceCandidates()),this.gotEarly&&this.setState(Ke.Early),this.gotAnswer&&this.setState(Ke.Active)})).catch((t=>i(this,void 0,void 0,(function*(){Ie.error("Call setRemoteDescription Error: ",t);const i=pe(g,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid);try{yield this.hangup({cause:"USER_BUSY",causeCode:17,initiator:"sdk:set-remote-description-failure"},!0)}catch(e){Ie.error("Error during hangup after setRemoteDescription failure:",e)}}))))}))}_requestAnotherLocalDescription(){Ae(this.peer.onSdpReadyTwice)?at(e.SwEvent.Error,{error:new Error("SDP without candidates for the second time!"),sessionId:this.session.sessionid},this.session.uuid):(Object.defineProperty(this.peer,"onSdpReadyTwice",{value:this._onIceSdp.bind(this)}),this.peer.iceDone=!1,this.peer.startNegotiation())}_onIceSdp(t){var n,s,o;if(this._iceTimeout&&clearTimeout(this._iceTimeout),this._iceTimeout=null,this.peer&&(this.peer.iceDone=!0),!t)return void Ie.warn("localDescription is null — PeerConnection may have been closed during ICE gathering");const{sdp:r,type:a}=t;if(-1===r.indexOf("candidate"))return Ie.info("No candidate - retry \n"),void this._requestAnotherLocalDescription();if(null===(s=null===(n=this.peer)||void 0===n?void 0:n.instance)||void 0===s||s.removeEventListener("icecandidate",this._onIce),!te.test(r)){const t=le(V);Ie.warn(`[${this.id}] Warning ${t.code}: ${t.message}`),at(e.SwEvent.Warning,{warning:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}performance.mark(wi(this.id,"ice-gathering-end"));let c=null;const d={sessid:this.session.sessionid,sdp:r,dialogParams:this.options,"User-Agent":`Web-${Ai}`};if(null===(o=this.peer)||void 0===o?void 0:o.isIceRestarting)this._sendIceRestartModify(r);else{switch(a){case Ge.Offer:this.setState(Ke.Requesting),c=new Tt(d);break;case Ge.Answer:this._isRecovering||this.setState(Ke.Answering),c=!0===this.options.attach?new Rt(d):new kt(d);break;default:return Ie.error(`${this.id} - Unknown local SDP type:`,t),void this.hangup({initiator:"sdk:unknown-local-sdp-type"},!1)}performance.mark(wi(this.id,"send-sdp")),this._execute(c).then((e=>{if(this._isTerminatingOrTerminated())return void Ie.debug(`[${this.id}] Ignoring ${a} response because call is ${this.state}`);const{node_id:t=null}=e;this._targetNodeId=t,a===Ge.Offer?this.setState(Ke.Trying):this.setState(Ke.Active)})).catch((t=>i(this,void 0,void 0,(function*(){Ie.error(`${this.id} - Sending ${a} error:`,t);const i=pe(v,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid);try{yield this.hangup({cause:"USER_BUSY",causeCode:17,initiator:"sdk:sdp-send-failure"},!0)}catch(e){Ie.error("Error during hangup after SDP send failure:",e)}}))))}}_onTrickleIceSdp(t){var n;if(!t)return Ie.error("No SDP data provided"),void this.hangup({initiator:"sdk:missing-local-sdp"},!1);const{sdp:s,type:o}=t;let r=null;const a={sessid:this.session.sessionid,sdp:s,dialogParams:this.options,trickle:!0,"User-Agent":`Web-${Ai}`};if(null===(n=this.peer)||void 0===n?void 0:n.isIceRestarting)this._sendIceRestartModify(s);else{switch(o){case Ge.Offer:this.setState(Ke.Requesting),r=new Tt(a);break;case Ge.Answer:this._isRecovering||this.setState(Ke.Answering),r=!0===this.options.attach?new Rt(a):new kt(a);break;default:return Ie.error(`${this.id} - Unknown local SDP type:`,t),void this.hangup({initiator:"sdk:unknown-local-sdp-type"},!1)}performance.mark(wi(this.id,"send-sdp")),this._execute(r).then((e=>{if(this._isTerminatingOrTerminated())return void Ie.debug(`[${this.id}] Ignoring ${o} response because call is ${this.state}`);const{node_id:t=null}=e;this._targetNodeId=t,o===Ge.Offer?this.setState(Ke.Trying):this.setState(Ke.Active)})).catch((t=>i(this,void 0,void 0,(function*(){Ie.error(`${this.id} - Sending ${o} error:`,t);const i=pe(v,t);at(e.SwEvent.Error,{error:i,callId:this.id,sessionId:this.session.sessionid},this.session.uuid);try{yield this.hangup({cause:"USER_BUSY",causeCode:17,initiator:"sdk:sdp-send-failure"},!0)}catch(e){Ie.error("Error during hangup after SDP send failure:",e)}}))))}}_onIce(e){var t;const{instance:i}=this.peer;if(null===this._iceTimeout){const e=this.options.attach?5e3:1e3;this._iceTimeout=setTimeout((()=>this._onIceSdp(i.localDescription)),e)}e.candidate?(Ie.debug("RTCPeer Candidate:",e.candidate),null===(t=this.peer)||void 0===t||t.incrementGatheredCandidates(),this._trackCandidateMarks(e.candidate)):this._onIceSdp(i.localDescription)}_onTrickleIce(e){var t;e.candidate&&e.candidate.candidate?(Ie.debug("RTCPeer Candidate:",e.candidate),null===(t=this.peer)||void 0===t||t.incrementGatheredCandidates(),this._trackCandidateMarks(e.candidate),this._sendIceCandidate(e.candidate)):this._sendEndOfCandidates()}_sendIceCandidate(e){const t=new Ot({sessid:this.session.sessionid,candidate:e.candidate,sdpMLineIndex:e.sdpMLineIndex,sdpMid:e.sdpMid,dialogParams:this.options});this._execute(t)}_addIceCandidate(e){if(!this._isRemoteDescriptionSet)return Ie.debug("Remote description not set. Queued ICE candidate.",e),void this._pendingIceCandidates.push(e);this._addIceCandidateToPeer(e)}_addIceCandidateToPeer(e){const t=this.peer.instance.addIceCandidate(e);Promise.resolve(t).then((()=>{Ie.debug("Successfully added ICE candidate:",e)})).catch((t=>{Ie.error("Failed to add ICE candidate:",t,e)}))}_sendEndOfCandidates(){const e=new Nt({sessid:this.session.sessionid,endOfCandidates:!0,dialogParams:this.options});this._execute(e)}_trackCandidateMarks(e){var t;if(this._firstCandidateSent||(performance.mark(wi(this.id,"first-candidate")),this._firstCandidateSent=!0),!this._firstNonHostCandidateSent){const i=null===(t=e.candidate.match(/typ (\w+)/))||void 0===t?void 0:t[1];i&&"host"!==i&&(performance.mark(wi(this.id,"first-non-host-candidate")),this._firstNonHostCandidateSent=!0)}}_resetTrickleIceCandidateState(){this._pendingIceCandidates=[],this._isRemoteDescriptionSet=!1,this._firstCandidateSent=!1,this._firstNonHostCandidateSent=!1}_flushPendingTrickleIceCandidates(){if(!this._pendingIceCandidates.length)return;const e=[...this._pendingIceCandidates];this._pendingIceCandidates=[],e.forEach((e=>{this._addIceCandidateToPeer(e)}))}_registerPeerEvents(e){e.onicecandidate=e=>{var t,i;if(this.options.trickleIce&&!(null===(t=this.peer)||void 0===t?void 0:t.isIceRestarting))this._onTrickleIce(e);else{if(null===(i=this.peer)||void 0===i?void 0:i.iceDone)return;this._onIce(e)}},e.onicegatheringstatechange=t=>{Ie.debug("ICE gathering state changed:",e.iceGatheringState,t),"complete"===e.iceGatheringState&&(Ie.debug("Finished gathering candidates"),performance.mark(wi(this.id,"ice-gathering-completed")))},e.onicecandidateerror=t=>{var i;if(Ie.debug("ICE candidate error:",t),null===(i=this.peer)||void 0===i?void 0:i.statsReporter){const i=function(e,t){var i,n;return{errorCode:e.errorCode,errorText:e.errorText,url:e.url,address:e.address,port:e.port,connectionState:t.connectionState,iceConnectionState:t.iceConnectionState,iceGatheringState:t.iceGatheringState,signalingState:t.signalingState,localDescriptionType:null===(i=t.localDescription)||void 0===i?void 0:i.type,remoteDescriptionType:null===(n=t.remoteDescription)||void 0===n?void 0:n.type}}(t,e);this.peer.statsReporter.reportIceCandidateError(i)}},e.addEventListener("addstream",(e=>{this.options.remoteStream=e.stream})),e.addEventListener("track",(e=>{this.options.remoteStream=e.streams[0];const{remoteElement:t,remoteStream:i,screenShare:n}=this.options;!1===n&&Ht(t,i)}))}_onMediaError(t){const i=(null==t?void 0:t.name)||"UnknownError",n=(null==t?void 0:t.message)||"Unknown media error",s=(null==t?void 0:t.originalError)||t;this._dispatchNotification({type:Ve.userMediaError,error:s,call:this,errorName:i,errorMessage:n}),Ie.error(`Media error (${i}): ${n}`,t),at(e.SwEvent.Error,{error:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),this.hangup({initiator:"sdk:media-error"},!1)}_onPeerConnectionFailureError(t){this._dispatchNotification({type:Ve.peerConnectionFailureError,error:t.error}),Ie.error("Peer connection failure error"),t.warning&&at(e.SwEvent.Warning,{warning:t.warning,callId:this.id,sessionId:this.session.sessionid},this.session.uuid)}_onPeerConnectionSignalingStateClosed(e){this._signalingStateClosed=!0,this._dispatchNotification(Object.assign({type:Ve.signalingStateClosed},e)),Ie.debug("Peer connection signaling state closed, call is not recoverable")}_dispatchConferenceUpdate(e){this._dispatchNotification(Object.assign({type:Ve.conferenceUpdate,call:this},e))}_dispatchNotification(t){!0!==this.options.screenShare&&(at(e.SwEvent.Notification,t,this.id,!1)||at(e.SwEvent.Notification,t,this.session.uuid))}_execute(e){return this.nodeId&&(e.targetNodeId=this.nodeId),this.session.execute(e)}_registerInboundAnswerAttempt(){if(this.options.attach||this._isRecovering)return!0;for(const t of this._getSessionInboundAnswerCalls()){if(t.id===this.id)continue;if(!t._isBlockingInboundAnswer())continue;const i=le(K);return at(e.SwEvent.Warning,{warning:i,callId:this.id,activeCallId:t.id,sessionId:this.session.sessionid,activeSessionId:t.session.sessionid},this.session.uuid),Ie.warn(`[${this.id}] answer() ignored: inbound call ${t.id} is already answering or active`),!1}return!0}_getSessionInboundAnswerCalls(){return Object.values(this.session.calls).filter((e=>Boolean(e))).filter((e=>!e.options.attach&&!e._isRecovering&&e.direction===We.Inbound))}_isBlockingInboundAnswer(){var e;if([Ke.Hangup,Ke.Destroy,Ke.Purge].includes(this._state))return!1;const t=[Ke.Answering,Ke.Early,Ke.Active,Ke.Held].includes(this._state);return!(!this._creatingPeer&&!t)&&(!(!this._creatingPeer||(null===(e=this.peer)||void 0===e?void 0:e.instance))||this._hasUsablePeerConnection())}_hasUsablePeerConnection(){var e;const t=null===(e=this.peer)||void 0===e?void 0:e.instance;return!!t&&("closed"!==t.signalingState&&("closed"!==t.connectionState&&"closed"!==t.iceConnectionState))}_init(){var t,i,n;const{id:s,userVariables:o,remoteCallerNumber:r,onNotification:a,recoveredCallId:d}=this.options;var l;this.options.id=s?s.toString():c(),this.id=this.options.id,d&&(this.recoveredCallId=d,this._isRecovering=!0),o&&(l=o,0!==Object.keys(l).length)||(this.options.userVariables=this.session.options.userVariables||{}),r||(this.options.remoteCallerNumber=this.options.destinationNumber),this.session.calls[this.id]=this,st(e.SwEvent.MediaError,this._onMediaError,this.id),st(e.SwEvent.PeerConnectionFailureError,this._onPeerConnectionFailureError,this.id),st(e.SwEvent.PeerConnectionSignalingStateClosed,this._onPeerConnectionSignalingStateClosed,this.id),Ae(a)&&st(e.SwEvent.Notification,a.bind(this),this.id);const h=!1!==this.session.options.enableCallReports,u=this.session.options.callReportInterval||5e3,p=null!==(t=this.session.options.callReportFlushInterval)&&void 0!==t?t:18e4,g=this.session.options.debugLogLevel||"debug",v=this.session.options.debugLogMaxEntries||1e3;h&&(this._callReportCollector=new hi({enabled:!0,interval:u,intermediateReportInterval:p},{enabled:!0,level:g,maxEntries:v}),this._callReportCollector.onFlushNeeded=()=>{this._flushIntermediateReport()},this._callReportCollector.onWarning=t=>{var i,n,s,o;at(e.SwEvent.Warning,{warning:t,callId:this.id,sessionId:this.session.sessionid},this.session.uuid),t.code===$?null===(n=(i=this.session).reportNoRtp)||void 0===n||n.call(i,this.id,"inbound"):t.code===j&&this._hasActiveUnmutedLocalAudioTrack()&&(null===(o=(s=this.session).reportNoRtp)||void 0===o||o.call(s,this.id,"outbound"))}),this._isRecovering?this.setState(Ke.Recovering):this.setState(Ke.New),Ie.info(`New Call — region: ${null!==(i=this.session.region)&&void 0!==i?i:"unknown"}, dc: ${null!==(n=this.session.dc)&&void 0!==n?n:"unknown"}`,this.options)}_finalize(){var t;this._stopStats(),ki(this.id),Ie.debug(`[${this.id}] Closing peer from _finalize`),null===(t=this.peer)||void 0===t||t.close();const{remoteStream:i,localStream:n,remoteElement:s,localElement:o}=this.options;Vt(i),Vt(n),Gt(s,i),Gt(o,n),rt(e.SwEvent.MediaError,null,this.id),rt(e.SwEvent.PeerConnectionFailureError,null,this.id),rt(e.SwEvent.PeerConnectionSignalingStateClosed,null,this.id),this.session.calls[this.id]=null,delete this.session.calls[this.id];const r=this._postCallReport().catch((e=>{Ie.error("Unexpected error in _postCallReport",{error:e})}));this.session.trackCallReportUpload(r)}_getCallReportVoiceSdkId(){return this.session.callReportVoiceSdkId||void 0}flushIntermediateCallReport(e={type:"manual"}){this._flushIntermediateReport(e)}_flushIntermediateReport(e={type:"buffer-limit"}){var t;if(!this._callReportCollector)return;const i=this.session.callReportId;if(!i)return void Ie.debug("Cannot flush intermediate report: call_report_id not available");const n=null===(t=this.session.connection)||void 0===t?void 0:t.host;if(!n)return void Ie.debug("Cannot flush intermediate report: connection host not available");const s={callId:this.id,destinationNumber:this.options.destinationNumber,callerNumber:this.options.callerNumber,direction:this.direction===We.Inbound?"inbound":"outbound",state:this.state,telnyxSessionId:this.options.telnyxSessionId,telnyxLegId:this.options.telnyxLegId,sdkVersion:Ai},o=this._callReportCollector.flush(s,e);if(!o)return;Ie.info("Flushing intermediate call report",{callId:this.id,flushReason:e,segment:o.segment});const r=this._getCallReportVoiceSdkId(),a=this._callReportCollector.sendPayload(o,i,n,r).catch((e=>{Ie.error("Failed to post intermediate call report segment",{error:e})}));this.session.trackCallReportUpload(a)}_postCallReport(){var e,t;return i(this,void 0,void 0,(function*(){if(!this._callReportCollector)return void Ie.warn("Call report collector not initialized");yield this._callReportCollector.stop();const i=this.session.callReportId;if(!i)return Ie.debug("Cannot post call report: call_report_id not available"),void this._callReportCollector.cleanup();const n={callId:this.id,destinationNumber:this.options.destinationNumber,callerNumber:this.options.callerNumber,direction:this.direction===We.Inbound?"inbound":"outbound",state:this.state,telnyxSessionId:this.options.telnyxSessionId,telnyxLegId:this.options.telnyxLegId,sdkVersion:Ai},s=null===(e=this.session.connection)||void 0===e?void 0:e.host;if(!s)return void Ie.error("Cannot post call report: connection host not available");const o=this._getCallReportVoiceSdkId();try{yield this._callReportCollector.postReport(n,i,s,o)}catch(e){throw Ie.error("Failed to post call report",{error:e}),e}finally{null===(t=this._callReportCollector)||void 0===t||t.cleanup()}}))}_startStats(e){this._statsIntervalId=setInterval(this._doStats,e),Ie.info("Stats started")}_stopStats(){this._statsIntervalId&&(clearInterval(this._statsIntervalId),this._statsIntervalId=null),Ie.debug("Stats stopped")}}Oi.setStateTelnyx=e=>{if(e){switch(e._state){case Ke.Recovering:e.state="recovering";break;case Ke.Requesting:case Ke.Trying:case Ke.Early:e.state="connecting";break;case Ke.Active:e.state="active";break;case Ke.Held:e.state="held";break;case Ke.Hangup:case Ke.Destroy:e.state="done";break;case Ke.Answering:e.state="ringing";break;case Ke.New:e.state="new"}return e}};class Ni extends Oi{constructor(){super(...arguments),this._statsInterval=null,this.sendConversationMessage=(e,t)=>this.session.execute(new li(e,t))}hangup(e={},t=!0){const n=Object.create(null,{hangup:{get:()=>super.hangup}});return i(this,void 0,void 0,(function*(){this.screenShare instanceof Ni&&(yield this.screenShare.hangup(e,t)),yield n.hangup.call(this,e,t)}))}startScreenShare(e){return i(this,void 0,void 0,(function*(){const t=yield(n={video:!0},navigator.mediaDevices.getDisplayMedia(n));var n;t.getTracks().forEach((e=>{e.addEventListener("ended",(()=>i(this,void 0,void 0,(function*(){this.screenShare&&(yield this.screenShare.hangup({initiator:"sdk:screenshare-track-ended"}))}))))}));const{remoteCallerName:s,remoteCallerNumber:o,callerName:r,callerNumber:a}=this.options,c=Object.assign({screenShare:!0,localStream:t,destinationNumber:`${this.extension}-screen`,remoteCallerName:s,remoteCallerNumber:`${o}-screen`,callerName:`${r} (Screen)`,callerNumber:`${a} (Screen)`},e);return this.screenShare=new Ni(this.session,c),this.screenShare.invite(),this.screenShare}))}stopScreenShare(){return i(this,void 0,void 0,(function*(){this.screenShare instanceof Ni&&(yield this.screenShare.hangup({initiator:"app:stopScreenShare"}))}))}setAudioOutDevice(e){return i(this,void 0,void 0,(function*(){this.options.speakerId=e;const{remoteElement:t,speakerId:i}=this.options;return!(!t||!i)&&Wt(t,i)}))}_finalize(){this._stats(!1),super._finalize()}_stats(e=!0){if(!1===e)return clearInterval(this._statsInterval);this._statsInterval=window.setInterval((()=>i(this,void 0,void 0,(function*(){const e=yield this.peer.instance.getStats(null);let t="";const i=["certificate","codec","peer-connection","stream","local-candidate","remote-candidate"],n=["id","type","timestamp"];e.forEach((e=>{i.includes(e.type)||(t+=`\n${e.type}\n`,Object.keys(e).forEach((i=>{n.includes(i)||(t+=`\t${i}: ${e[i]}\n`)})))})),Ie.info(t)}))),2e3)}}class Li extends Ft{constructor(e){super(e),this.calls={},this.autoRecoverCalls=!0,this._iceServers=[],this._localElement=null,this._remoteElement=null,this._jwtAuth=!0,this._audioConstraints=!0,this._previousAudioConstraints=!0,this._videoConstraints=!1,this._speaker=null,this._onlineHandler=null,this._offlineHandler=null,this._wasOffline=!1,this._videoConstraints=e.video||!1,this.iceServers=e.iceServers,this.ringtoneFile=e.ringtoneFile,this.ringbackFile=e.ringbackFile,this._setupNetworkListeners()}get reconnectDelay(){return 1e3}getIsRegistered(){const e=Object.create(null,{getIsRegistered:{get:()=>super.getIsRegistered}});return i(this,void 0,void 0,(function*(){return e.getIsRegistered.call(this)}))}connect(){const e=Object.create(null,{connect:{get:()=>super.connect}});return i(this,void 0,void 0,(function*(){e.connect.call(this)}))}checkPermissions(e=!0,t=!0){return i(this,void 0,void 0,(function*(){try{const i=yield qt({audio:e,video:t});return Vt(i),!0}catch(e){return!1}}))}logout(){this.disconnect()}disconnect(){const e=Object.create(null,{disconnect:{get:()=>super.disconnect}});return i(this,void 0,void 0,(function*(){Ie.info("[disconnect] Client-initiated disconnect — setting Purge with BYE on all active calls.");for(const e in this.calls){const t=this.calls[e];t.setState(Ke.Purge),Ie.info("Start hangup for ",t),yield t.hangup({initiator:"app:client.disconnect"},!0)}this.calls={},this._cleanupNetworkListeners(),yield e.disconnect.call(this)}))}serverDisconnect(){const e=Object.create(null,{disconnect:{get:()=>super.disconnect}});return i(this,void 0,void 0,(function*(){Ie.info("[serverDisconnect] Server-initiated disconnect — setting Purge without BYE on all active calls.");for(const e in this.calls){const t=this.calls[e];t.setState(Ke.Purge),t.hangup({initiator:"sdk:server-disconnect"},!1)}this.calls={},this._cleanupNetworkListeners(),yield e.disconnect.call(this)}))}socketDisconnect(){this._closeConnection()}handleLoginError(e){super._handleLoginError(e)}speedTest(t){return new Promise(((i,n)=>{if(ot(e.SwEvent.SpeedTest,(e=>{const{upDur:n,downDur:s}=e,o=s?8*t/(s/1e3)/1024:0;i({upDur:n,downDur:s,upKps:(n?8*t/(n/1e3)/1024:0).toFixed(0),downKps:o.toFixed(0)})}),this.uuid),!(t=Number(t)))return n(`Invalid parameter 'bytes': ${t}`);this.executeRaw(`#SPU ${t}`);let s=t/1024;t%1024&&s++;const o=".".repeat(1024);for(let e=0;e<s;e++)this.executeRaw(`#SPB ${o}`);this.executeRaw("#SPE")}))}getDevices(){return Yt().catch((t=>{const i=pe(ue(t),t);return at(e.SwEvent.MediaError,i,this.uuid),[]}))}getVideoDevices(){return Yt(Xe.Video).catch((t=>(at(e.SwEvent.MediaError,t,this.uuid),[])))}getAudioInDevices(){return Yt(Xe.AudioIn).catch((t=>{const i=pe(ue(t),t);return at(e.SwEvent.MediaError,i,this.uuid),[]}))}getAudioOutDevices(){return Yt(Xe.AudioOut).catch((t=>(Ie.error("getAudioOutDevices",t),at(e.SwEvent.MediaError,t,this.uuid),[])))}validateDeviceId(e,t,i){return Jt(e,t,i)}getDeviceResolutions(e){return i(this,void 0,void 0,(function*(){try{return yield(e=>i(void 0,void 0,void 0,(function*(){const t=[],i=yield qt({video:{deviceId:{exact:e}}}),n=i.getVideoTracks()[0];for(let e=0;e<Kt.length;e++){const[i,s]=Kt[e];(yield n.applyConstraints({width:{exact:i},height:{exact:s}}).then((()=>!0)).catch((()=>!1)))&&t.push({resolution:`${i}x${s}`,width:i,height:s})}return Vt(i),t})))(e)}catch(e){throw e}}))}get mediaConstraints(){return{audio:this._audioConstraints,video:this._videoConstraints}}setAudioSettings(e){return i(this,void 0,void 0,(function*(){if(!e)throw new Error("You need to provide the settings object");const{micId:n,micLabel:s}=e,o=t(e,["micId","micLabel"]);return Qt(o),this._audioConstraints=yield((e,t,n,s)=>i(void 0,void 0,void 0,(function*(){const{deviceId:i}=s;if(void 0===i&&(e||t)){const i=yield Jt(e,t,n).catch((e=>null));i&&(s.deviceId={exact:i})}return s})))(n,s,"audioinput",o),this.micId=n,this.micLabel=s,this._audioConstraints}))}disableMicrophone(){this._previousAudioConstraints=this._audioConstraints,this._audioConstraints=!1}enableMicrophone(){this._audioConstraints=this._previousAudioConstraints||!0}set iceServers(e){if(e&&Array.isArray(e))this._iceServers=e;else{const e="development"===this.options.env;this._iceServers=e?re:oe}}get iceServers(){return this._iceServers}set speaker(e){this._speaker=e}get speaker(){return this._speaker}set localElement(e){this._localElement=Oe(e)}get localElement(){return this._localElement}set remoteElement(e){this._remoteElement=Oe(e)}get remoteElement(){return this._remoteElement}vertoBroadcast({nodeId:e,channel:t="",data:i}){if(!t)throw new Error(`Invalid channel for broadcast: ${t}`);const n=new Pt({sessid:this.sessionid,eventChannel:t,data:i});e&&(n.targetNodeId=e),this.execute(n).catch((e=>e))}vertoSubscribe({nodeId:e,channels:t=[],handler:n}){return i(this,void 0,void 0,(function*(){if(!(t=t.filter((e=>e&&!this._existsSubscription(this.relayProtocol,e)))).length)return{};const i=new Mt({sessid:this.sessionid,eventChannel:t});e&&(i.targetNodeId=e);const s=yield this.execute(i),{unauthorized:o=[],subscribed:r=[]}=zt(s);return o.length&&o.forEach((e=>this._removeSubscription(this.relayProtocol,e))),r.forEach((e=>this._addSubscription(this.relayProtocol,n,e))),s}))}vertoUnsubscribe({nodeId:e,channels:t=[]}){return i(this,void 0,void 0,(function*(){if(!(t=t.filter((e=>e&&this._existsSubscription(this.relayProtocol,e)))).length)return{};const i=new xt({sessid:this.sessionid,eventChannel:t});e&&(i.targetNodeId=e);const n=yield this.execute(i),{unsubscribed:s=[],notSubscribed:o=[]}=zt(n);return s.forEach((e=>this._removeSubscription(this.relayProtocol,e))),o.forEach((e=>this._removeSubscription(this.relayProtocol,e))),n}))}_setupNetworkListeners(){"undefined"!=typeof window&&(this._onlineHandler=()=>{this._wasOffline&&(Ie.debug(`Network connectivity restored for session ${this.sessionid}. Reconnecting...`),this._wasOffline=!1,this._autoReconnect=!0,this.socketDisconnect())},this._offlineHandler=()=>{this._wasOffline=!0,Ie.debug(`Network connectivity lost for session ${this.sessionid}`);const t=pe(L);at(e.SwEvent.Error,{error:t,sessionId:this.sessionid},this.uuid)},window.addEventListener("online",this._onlineHandler),window.addEventListener("offline",this._offlineHandler))}_cleanupNetworkListeners(){"undefined"!=typeof window&&this._onlineHandler&&this._offlineHandler&&(window.removeEventListener("online",this._onlineHandler),window.removeEventListener("offline",this._offlineHandler),this._onlineHandler=null,this._offlineHandler=null)}static telnyxStateCall(e){return Ni.setStateTelnyx(e)}}class Di{constructor(e,t){this.code=t,this.message=e}}class Pi{constructor(e){this.session=e,this.retriedConnect=0,this.retriedRegister=0}_ack(e,t){const i=new wt(e,t);this.nodeId&&(i.targetNodeId=this.nodeId),this.session.execute(i)}reconnectDelay(){return 1e3*De(2,6)}handleMessage(t){var i,n,s,o,r,a,c,d,l,h,u;const{session:p}=this;p.setPingReceived();const{id:g,method:v,params:m={},voice_sdk_id:f}=t,_=null==m?void 0:m.callID,S=null==m?void 0:m.eventChannel,b=null==m?void 0:m.eventType,y=p.calls[_],I=null===(i=null==y?void 0:y.peer)||void 0===i?void 0:i.isConnectionHealthy();if(Array.isArray(null==m?void 0:m.reattached_sessions)&&0===m.reattached_sessions.length&&Object.keys(p.calls).length>0){const t=le(Q);at(e.SwEvent.Warning,{warning:t,sessionId:p.sessionid},p.uuid)}if("channelPvtData"===b)return this._handlePvtEvent(m.pvtData);const E=(e,t)=>{var i,n,s,o,r;const a={audio:!0,video:p.options.video,remoteSdp:m.sdp,destinationNumber:m.callee_id_number,remoteCallerName:m.caller_id_name,remoteCallerNumber:m.caller_id_number,callerName:m.callee_id_name,callerNumber:m.callee_id_number,attach:v===Be.Attach,mediaSettings:m.mediaSettings,debug:null!==(i=p.options.debug)&&void 0!==i&&i,debugOutput:null!==(n=p.options.debugOutput)&&void 0!==n?n:"socket",trickleIce:null!==(s=p.options.trickleIce)&&void 0!==s&&s,prefetchIceCandidates:null===(o=p.options.prefetchIceCandidates)||void 0===o||o,forceRelayCandidate:t||p.options.forceRelayCandidate||!1,keepConnectionAliveOnSocketClose:null!==(r=p.options.keepConnectionAliveOnSocketClose)&&void 0!==r&&r};_&&(a.id=_),m.telnyx_call_control_id&&(a.telnyxCallControlId=m.telnyx_call_control_id),m.telnyx_session_id&&(a.telnyxSessionId=m.telnyx_session_id),m.telnyx_leg_id&&(a.telnyxLegId=m.telnyx_leg_id),m.client_state&&(a.clientState=m.client_state),m.dialogParams&&m.dialogParams.custom_headers&&m.dialogParams.custom_headers.length&&(a.customHeaders=m.dialogParams.custom_headers),e&&(a.recoveredCallId=e),performance.mark(wi(a.id,"new-call-start"));const c=new Ni(p,a);return c.nodeId=this.nodeId,c},C=new _t(f),w=new bt(f);switch(v){case Be.Answer:case Be.Display:case Be.Candidate:case Be.Ringing:case Be.Bye:case Be.Media:if(!_||!y)return void Ie.error(`Received ${v} for non existing call:`,m);y.handleMessage(t),this._ack(g,v);break;case Be.Ping:this.session.setPingReceived(),this.session.execute(w);break;case Be.Punt:p.options.keepConnectionAliveOnSocketClose&&I?(Ie.info("[punt] Received PUNT from server. keepConnectionAliveOnSocketClose=true — disconnecting socket only, keeping calls alive."),p.socketDisconnect(),this._ack(g,v)):(Ie.info("[punt] Received PUNT from server — calling serverDisconnect() to purge all calls without BYE."),p.serverDisconnect());break;case Be.Invite:{const e=E();e.direction=We.Inbound,e.playRingtone(),e.setState(Ke.Ringing),this._ack(g,v);break}case Be.Attach:{if(!y){return E().answer(),void this._ack(g,v)}const e=y.id,t=null!==(s=null===(n=y.shouldForceRelayCandidateForRecovery)||void 0===n?void 0:n.call(y))&&void 0!==s&&s;t&&Ie.warn(`[${(new Date).toISOString()}][${_}] Attach: forcing relay candidate because recovered VPN media path is still stalled`),Ie.info(`[${(new Date).toISOString()}][${_}] closing existing call on ATTACH.`),y.hangup({isRecovering:!0,initiator:"sdk:attach-recovery"},!1),Ie.info(`[${(new Date).toISOString()}][${_}] Attach: Creating new call for recovery (recoveredCallId: ${e})`);E(e,t).answer(),this._ack(g,v);break}case Be.Event:case"webrtc.event":if(!S)return void Ie.error("Verto received an unknown event:",m);const i=p.relayProtocol,f=S.split(".")[0];p._existsSubscription(i,S)?at(i,m,S):S===p.sessionid?this._handleSessionEvent(m.eventData):p._existsSubscription(i,f)?at(i,m,f):p.calls.hasOwnProperty(S)?p.calls[S].handleMessage(t):at(e.SwEvent.Notification,m,p.uuid);break;case Be.Info:m.type=Ve.generic,at(e.SwEvent.Notification,m,p.uuid);break;case Be.ClientReady:this.session.execute(C);break;default:{const i=xe(t);if(i){switch(i){case ze.REGISTER:case ze.REGED:if(p.connection.previousGatewayState!==ze.REGED&&p.connection.previousGatewayState!==ze.REGISTER){this.session._triggerKeepAliveTimeoutCheck(),this.retriedRegister=0,i===ze.REGED&&this.session.resetReconnectAttempts();const n=null===(r=null===(o=null==t?void 0:t.result)||void 0===o?void 0:o.params)||void 0===r?void 0:r.call_report_id;n&&(p.callReportId=n,Ie.debug("Captured call_report_id from REGED:",n));const s=null===(c=null===(a=null==t?void 0:t.result)||void 0===a?void 0:a.params)||void 0===c?void 0:c.dc;s&&(p.dc=s);const g=null===(l=null===(d=null==t?void 0:t.result)||void 0===d?void 0:d.params)||void 0===l?void 0:l.region;g&&(p.region=g),Ie.info(`Connected to Telnyx — region: ${null!==(h=p.region)&&void 0!==h?h:"unknown"}, dc: ${null!==(u=p.dc)&&void 0!==u?u:"unknown"}`),m.type=Ve.vertoClientReady,at(e.SwEvent.Ready,m,p.uuid)}break;case ze.UNREGED:case ze.NOREG:if(this.retriedRegister+=1,5===this.retriedRegister){this.retriedRegister=0;const t=new Di("Fail to register the user, the server tried 5 times","UNREGED|NOREG"),i=pe(R,t);at(e.SwEvent.Error,{error:i,sessionId:p.sessionid},p.uuid);break}setTimeout((()=>{this.session.execute(C)}),this.reconnectDelay());break;case ze.FAILED:case ze.FAIL_WAIT:if(p.connection.previousGatewayState!==ze.FAILED&&p.connection.previousGatewayState!==ze.FAIL_WAIT){const t=pe(k,new Error(`Gateway state: ${i}`));if(at(e.SwEvent.Error,{error:t,sessionId:p.sessionid},p.uuid),!this.session.hasAutoReconnect()){this.retriedConnect=0;const t=new Di("Fail to connect the server, the server tried 5 times","FAILED|FAIL_WAIT"),i=pe(T,t);at(e.SwEvent.Error,{error:i,sessionId:p.sessionid},p.uuid);break}if(this.retriedConnect+=1,5===this.retriedConnect){this.retriedConnect=0;const t=pe(45003,new Error("Connection Retry Failed"));at(e.SwEvent.Error,{error:t,sessionId:p.sessionid},p.uuid);break}setTimeout((()=>{if(Ie.debug(`Reconnecting... Retry ${this.retriedConnect} of 5`),this.session.options.keepConnectionAliveOnSocketClose){const e=Object.values(p.calls).some((e=>{var t;return(null===(t=e.peer)||void 0===t?void 0:t.instance)&&!e.signalingStateClosed}));if(e)return Ie.debug("Reconnecting by keeping the existing session due to keepConnectionAliveOnSocketClose option being set."),void this.session.socketDisconnect();Ie.debug("keepConnectionAliveOnSocketClose is set but all peer connections have signalingState closed, doing full reconnect")}this.session.disconnect().then((()=>{this.session.clearConnection(),this.session.connect()}))}),this.reconnectDelay())}break;default:Ie.warn("GatewayState message unknown method:",t)}break}Ie.debug("Verto message unknown method:",t);break}}}_retrieveCallId(e,t){const i=Object.keys(this.session.calls);if("bootObj"!==e.action)return i.find((e=>this.session.calls[e].channels.includes(t)));{const t=e.data.find((e=>i.includes(e[0])));if(t instanceof Array)return t[0]}}_handlePvtEvent(t){return i(this,void 0,void 0,(function*(){const{session:i}=this,n=i.relayProtocol,{action:s,laChannel:o,laName:r,chatChannel:a,infoChannel:c,modChannel:d,conferenceMemberID:l,role:h,callID:u}=t;switch(s){case"conference-liveArray-join":{const n=()=>{i.vertoBroadcast({nodeId:this.nodeId,channel:o,data:{liveArray:{command:"bootstrap",context:o,name:r}}})},s={nodeId:this.nodeId,channels:[o],handler:({data:e})=>{const s=u||this._retrieveCallId(e,o);if(s&&i.calls.hasOwnProperty(s)){const a=i.calls[s];a._addChannel(o),a.extension=r,a.handleConferenceUpdate(e,t).then((e=>{"INVALID_PACKET"===e&&n()}))}}},a=yield i.vertoSubscribe(s).catch((t=>{Ie.error("liveArray subscription error:",t);const n=pe(I,t);at(e.SwEvent.Error,{error:n,sessionId:i.sessionid},i.uuid)}));Xt(a,o)&&n();break}case"conference-liveArray-part":{let t=null;if(o&&i._existsSubscription(n,o)){const{callId:s=null}=i.subscriptions[n][o];if(t=i.calls[s]||null,null!==s){const n={type:Ve.conferenceUpdate,action:Qe.Leave,conferenceName:r,participantId:Number(l),role:h};at(e.SwEvent.Notification,n,s,!1)||at(e.SwEvent.Notification,n,i.uuid),null===t&&rt(e.SwEvent.Notification,null,s)}}const s=[o,a,c,d];i.vertoUnsubscribe({nodeId:this.nodeId,channels:s}).then((({unsubscribedChannels:e=[]})=>{t&&(t.channels=t.channels.filter((t=>!e.includes(t))))})).catch((e=>{Ie.error("liveArray unsubscribe error:",e)}));break}}}))}_handleSessionEvent(t){switch(t.contentType){case"layout-info":case"layer-info":yi(this.session,t);break;case"logo-info":{const i={type:Ve.conferenceUpdate,action:Qe.LogoInfo,logo:t.logoURL};at(e.SwEvent.Notification,i,this.session.uuid);break}}}}class Mi extends Li{constructor(e){super(e),this.relayProtocol="verto-protocol",this.timeoutErrorCode=-329990,this.handleLoginOnSocketOpen=()=>i(this,void 0,void 0,(function*(){this._idle=!1;const{autoReconnect:e=!0}=this.options;yield this.login({onSuccess:()=>{this._autoReconnect=e}})})),this.handleAnonymousLoginOnSocketOpen=()=>i(this,void 0,void 0,(function*(){this._idle=!1,yield this.login()})),this._vertoHandler=new Pi(this),!1!==e.hangupOnBeforeUnload&&window.addEventListener("beforeunload",(e=>{this.calls&&Object.keys(this.calls).forEach((e=>{this.calls[e]&&(Ie.info(`Hanging up call due to window unload: ${e}`),this.calls[e].hangup({initiator:"sdk:beforeunload"},!0))}))}))}validateOptions(){return Pe(this.options)||Me(this.options)}newCall(e){if(!this.validateCallOptions(e)){throw pe(b,void 0,"Error: destinationNumber is required")}const t=new Ni(this,e);return performance.mark(wi(t.id,"new-call-start")),t.invite(),t}broadcast(e){return this.vertoBroadcast(e)}subscribe(e){return this.vertoSubscribe(e)}unsubscribe(e){return this.vertoUnsubscribe(e)}validateCallOptions(e){return!!Me(this.options)||Boolean(e.destinationNumber)}_onSocketOpen(){const e=Object.create(null,{_onSocketOpen:{get:()=>super._onSocketOpen}});return i(this,void 0,void 0,(function*(){return yield e._onSocketOpen.call(this),Pe(this.options)?this.handleLoginOnSocketOpen():Me(this.options)?this.handleAnonymousLoginOnSocketOpen():void 0}))}_onSocketMessage(e){this._vertoHandler.handleMessage(e)}}class xi extends Mi{constructor(e){super(e),Ie.info(`SDK version: ${It}`)}newCall(e){return super.newCall(e)}static webRTCInfo(){return si()}static webRTCSupportedBrowserList(){return[{operationSystem:"Android",supported:[{browserName:"Chrome",features:["audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",supported:oi.not_supported},{browserName:"Edge",supported:oi.not_supported}]},{operationSystem:"iOS",supported:[{browserName:"Chrome",supported:oi.not_supported},{browserName:"Firefox",supported:oi.not_supported},{browserName:"Safari",features:["video","audio"],supported:oi.full},{browserName:"Edge",supported:oi.not_supported}]},{operationSystem:"Linux",supported:[{browserName:"Chrome",features:["video","audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",supported:oi.not_supported},{browserName:"Edge",supported:oi.not_supported}]},{operationSystem:"MacOS",supported:[{browserName:"Chrome",features:["video","audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",features:["video","audio"],supported:oi.full},{browserName:"Edge",features:["audio"],supported:oi.partial}]},{operationSystem:"Windows",supported:[{browserName:"Chrome",features:["video","audio"],supported:oi.full},{browserName:"Firefox",features:["audio"],supported:oi.partial},{browserName:"Safari",supported:oi.not_supported},{browserName:"Edge",features:["audio"],supported:oi.partial}]}]}}class Ui{static run(t){return i(this,void 0,void 0,(function*(){const i=Ue({}),n=Ue({}),s=new xi(t.credentials);yield s.connect(),s.on(e.SwEvent.Ready,i.resolve),s.on(e.SwEvent.Error,i.reject),s.on(e.SwEvent.MediaError,i.reject),s.on(e.SwEvent.MediaError,i.reject),s.on(e.SwEvent.Notification,(e=>{e.call&&e.call.sipCode>=400&&n.reject(new Error(e.call.sipReason))})),st(e.SwEvent.StatsReport,(e=>{n.resolve(Ui.mapReport(e))})),yield i.promise,yield s.newCall({destinationNumber:t.texMLApplicationNumber,debug:!0});const o=yield n.promise;return yield s.disconnect(),o}))}static mapReport(e){var t,i,n,s,o,r,a,c,d,l,h,u,p;const g=[],v=[];for(const t of e)switch(t.event){case"onicecandidate":t.data&&g.push(t.data);break;case"stats":v.push(t.data)}let m=0,f=1/0,_=-1/0,S=0,b=1/0,y=-1/0,I=0;v.forEach((e=>{var t,i,n;if(!(null===(t=e.remote.audio.inbound)||void 0===t?void 0:t[0]))return;m+=1;const s=null!==(i=e.remote.audio.inbound[0].jitter)&&void 0!==i?i:0,o=null!==(n=e.remote.audio.inbound[0].roundTripTime)&&void 0!==n?n:0;S+=s,I+=o,_=Math.max(_,s),f=Math.min(f,s),y=Math.max(y,o),b=Math.min(b,o)}));const E=I/m,C=S/m,w=v[v.length-1],T=vi({jitter:1e3*C,rtt:1e3*E,packetsReceived:null!==(n=null===(i=null===(t=w.audio.inbound)||void 0===t?void 0:t[0])||void 0===i?void 0:i.packetsReceived)&&void 0!==n?n:0,packetsLost:null!==(r=null===(o=null===(s=w.audio.inbound)||void 0===s?void 0:s[0])||void 0===o?void 0:o.packetsLost)&&void 0!==r?r:0});return{iceCandidatePairStats:v[v.length-1].connection,summaryStats:{mos:T,jitter:{average:C,max:_,min:f},rtt:{average:E,max:y,min:b},quality:mi(T)},sessionStats:{packetsSent:null!==(a=w.connection.packetsSent)&&void 0!==a?a:0,bytesSent:null!==(c=w.connection.bytesSent)&&void 0!==c?c:0,bytesReceived:null!==(d=w.connection.bytesReceived)&&void 0!==d?d:0,packetsLost:null!==(u=null===(h=null===(l=w.remote.audio.inbound)||void 0===l?void 0:l[0])||void 0===h?void 0:h.packetsLost)&&void 0!==u?u:0,packetsReceived:null!==(p=w.connection.packetsReceived)&&void 0!==p?p:0},iceCandidateStats:g}}getTelnyxIds(){return{telnyxCallControlId:"",telnyxSessionId:"",telnyxLegId:""}}}e.Call=Ni,e.ERROR_TYPE=qe,e.NOTIFICATION_TYPE=Ve,e.PreCallDiagnosis=Ui,e.SDK_ERRORS=ce,e.SDK_WARNINGS=de,e.TELNYX_ERROR_CODES=d,e.TELNYX_WARNING_CODES=l,e.TelnyxError=he,e.TelnyxRTC=xi,e.isMediaRecoveryErrorEvent=function(e){return!0===e.recoverable},Object.defineProperty(e,"__esModule",{value:!0})}));