@signalwire/web-components 1.0.0-dev-20260504192904 → 1.0.0-dev-20260508182243

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.
@@ -69,7 +69,7 @@ var SignalWireUI=(function(l){"use strict";var Ym=Object.defineProperty;var zl=l
69
69
  `)}return n.join(`\r
70
70
  `)}function Ag(t,e=[],i=[]){return Sg(t,i,e)}function Tg(t){return t.split(/(?=m=)/)}function ic(t,e){var h;const i=t.split(/\r?\n/),r=i[0].split(" ");if(r.length<4)return t;const n=r.slice(3),s=new Map;for(const p of i){const f=/^a=rtpmap:(\d+)\s+([^\s/]+)/.exec(p);f&&s.set(f[1],f[2])}const a=e.map(p=>p.toUpperCase()),o=[],c=[];for(const p of n){const f=((h=s.get(p))==null?void 0:h.toUpperCase())??"";a.indexOf(f)>=0?o.push(p):c.push(p)}o.sort((p,f)=>{var y,I;const m=((y=s.get(p))==null?void 0:y.toUpperCase())??"",R=((I=s.get(f))==null?void 0:I.toUpperCase())??"";return a.indexOf(m)-a.indexOf(R)});const u=[...o,...c];return[[...r.slice(0,3),...u].join(" "),...i.slice(1)].join(`\r
71
71
  `)}function Eg(t){const e=/a=rtpmap:(\d+)\s+opus\/48000/i.exec(t);return e?e[1]:null}function kg(t,e){let i=t;return i.includes("stereo=")||(i+=";stereo=1"),i.includes("sprop-stereo=")||(i+=";sprop-stereo=1"),i.includes("maxaveragebitrate=")||(i+=`;maxaveragebitrate=${e}`),i}const Ne=W();var xg=class extends ie{constructor(t,e,i={}){super(),this.peerConnection=t,this.peerConnectionControllerNegotiating$=e,this.onicegatheringstatechangeHandler=()=>{const{iceGatheringState:r}=this.peerConnection;Ne.debug(`[ICEGatheringController] ICE gathering state changed to: ${r}`),r==="gathering"&&this._iceCandidatesState.next({state:"gathering",validSDP:!1})},this.onicecandidateHandler=r=>{Ne.debug("[ICEGatheringController] ICE candidate event received:",r.candidate),this.removeTimer("iceCandidateTimer"),r.candidate?this.iceCandidateTimer=setTimeout(()=>{this.peerConnection.iceGatheringState!=="complete"&&(Ne.warn("[ICEGatheringController] ICE candidate timeout, using current SDP"),this.handleICECandidateTimeout())},this.iceCandidateTimeout):(Ne.debug("[ICEGatheringController] ICE gathering completed: null candidate received"),this.removeTimer("iceGatheringTimer"),this.handleICEGatheringComplete())},this._iceCandidatesState=this.createBehaviorSubject({state:"new",validSDP:!1}),this.iceCandidateTimeout=i.iceCandidateTimeout??ho,this.iceGatheringTimeout=i.iceGatheringTimeout??po,this.relayOnly=i.relayOnly??!1,this.setupEventListeners(),this.subscribeTo(this.peerConnectionControllerNegotiating$.pipe(B(r=>r)),r=>{r&&(this.setupEventListeners(),this.iceGatheringTimer=setTimeout(()=>{this.peerConnection.iceGatheringState!=="complete"&&(Ne.warn("[ICEGatheringController] ICE gathering timeout, using current SDP"),this.handleICEGatheringTimeout())},this.iceGatheringTimeout))})}setupEventListeners(){this.peerConnection.removeEventListener("icecandidate",this.onicecandidateHandler),this.peerConnection.addEventListener("icecandidate",this.onicecandidateHandler),this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.addEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler)}get iceCandidatesState$(){return this._iceCandidatesState.pipe(Za(this.peerConnectionControllerNegotiating$),B(([t,e])=>e),E(([t,e])=>t.state))}get hasValidLocalDescriptionSDP(){var e;const t=(e=this.peerConnection.localDescription)==null?void 0:e.sdp;return tc(t??"")}get isRelayOnly(){return this.relayOnly}setRelayOnly(t){this.relayOnly=t}handleICEGatheringComplete(){Ne.debug("[ICEGatheringController] Handling ICE gathering complete"),Ne.debug(`[ICEGatheringController] Checking ICE gathering state: ${this.peerConnection.iceGatheringState}`),Ne.debug("[ICEGatheringController] ICE gathering complete"),this._iceCandidatesState.next({state:"complete",validSDP:this.hasValidLocalDescriptionSDP}),this.stopGathering()}stopGathering(){this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.removeEventListener("icecandidate",this.onicecandidateHandler),this.clearAllTimers()}handleICEGatheringTimeout(){var e;this.removeTimer("iceGatheringTimer");const t=this.hasValidLocalDescriptionSDP;t?(Ne.debug("[ICEGatheringController] Local SDP is valid"),this._iceCandidatesState.next({state:"timeout",validSDP:t}),this.stopGathering()):Ne.debug(`### ICE gathering timeout
72
- `,(e=this.peerConnection.localDescription)==null?void 0:e.sdp)}handleICECandidateTimeout(){this.iceCandidateTimer&&this.removeTimer("iceCandidateTimer"),Ne.warn("[ICEGatheringController] ICE candidate timeout");const t=this.hasValidLocalDescriptionSDP;!t&&!this.relayOnly?this.restartICEGatheringWithRelayOnly():(Ne.debug("[ICEGatheringController] Using current SDP due to ICE candidate timeout"),this._iceCandidatesState.next({state:"timeout",validSDP:t}),this.stopGathering())}restartICEGatheringWithRelayOnly(){Ne.debug("[ICEGatheringController] Restarting ICE gathering with relay-only candidates"),this.relayOnly=!0,this.peerConnection.setConfiguration({...this.peerConnection.getConfiguration(),iceTransportPolicy:"relay"}),this.peerConnection.restartIce()}removeTimer(t){this[t]&&(clearTimeout(this[t]),this[t]=void 0)}clearAllTimers(){Ne.debug("[ICEGatheringController] Clearing all timers"),this.removeTimer("iceGatheringTimer"),this.removeTimer("iceCandidateTimer")}removeEventListeners(){this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.removeEventListener("icecandidate",this.onicecandidateHandler)}destroy(){Ne.debug("[ICEGatheringController] Destroying ICEGatheringController"),this.clearAllTimers(),this.removeEventListeners(),super.destroy()}};const cs=W();var Dg=class extends ie{constructor(t={}){super(),this._inputSource=null,this._inputStream=null,this._lastSpokeAt=0,this._gain$=this.createBehaviorSubject(1),this._pttMultiplier=1,this._audioContext=(t.audioContextFactory??(()=>new AudioContext))(),this._gainNode=this._audioContext.createGain(),this._analyser=this._audioContext.createAnalyser(),this._analyser.fftSize=2048,this._analyser.smoothingTimeConstant=.3,this._analyserBuffer=new Uint8Array(new ArrayBuffer(this._analyser.fftSize)),this._destination=this._audioContext.createMediaStreamDestination(),this._gainNode.connect(this._analyser),this._analyser.connect(this._destination),this._speakingThreshold=t.speakingThreshold??Yh,this._speakingHoldMs=t.speakingHoldMs??Kh,this._pollIntervalMs=t.pollIntervalMs??Co;const e=t.initialGain??1;this._gain$.next(e),this.applyEffectiveGain()}get gain$(){return this._gain$.asObservable()}get gain(){return this._gain$.value}get outputTrack(){const[t]=this._destination.stream.getAudioTracks();return t}get level$(){return this.deferEmission(Wr(this._pollIntervalMs,Ta).pipe(E(()=>this.computeLevel())))}get speaking$(){return this.deferEmission(this.level$.pipe(E(t=>this.evaluateSpeaking(t)),L()))}setGain(t){const e=Math.max(0,Math.min(2,t));this._gain$.next(e),this.applyEffectiveGain()}setPTTActive(t){this._pttMultiplier=t?1:0,this.applyEffectiveGain()}applyEffectiveGain(){this._gainNode.gain.value=this._gain$.value*this._pttMultiplier}setInputTrack(t){if(this._inputSource){try{this._inputSource.disconnect()}catch(e){cs.debug("[LocalAudioPipeline] input disconnect warning:",e)}this._inputSource=null}this._inputStream&&(this._inputStream=null),t&&(this._inputStream=new MediaStream([t]),this._inputSource=this._audioContext.createMediaStreamSource(this._inputStream),this._inputSource.connect(this._gainNode),this._audioContext.state==="suspended"&&this._audioContext.resume().catch(e=>{cs.warn("[LocalAudioPipeline] AudioContext resume failed:",e)}))}destroy(){if(this._inputSource){try{this._inputSource.disconnect()}catch{}this._inputSource=null}try{this._gainNode.disconnect(),this._analyser.disconnect()}catch{}this._audioContext.close().catch(t=>{cs.debug("[LocalAudioPipeline] audio context close warning:",t)}),super.destroy()}computeLevel(){if(!this._inputSource)return 0;this._analyser.getByteTimeDomainData(this._analyserBuffer);let t=0;for(const e of this._analyserBuffer){const i=(e-128)/128;t+=i*i}return Math.sqrt(t/this._analyserBuffer.length)}evaluateSpeaking(t){const e=Date.now();return t>=this._speakingThreshold?(this._lastSpokeAt=e,!0):e-this._lastSpokeAt<this._speakingHoldMs}};const Ft=W();var Ig=class extends ie{constructor(t){super(),this.options=t,this.mediaTrackEndedHandler=e=>{this._mediaTrackEnded$.next(e)},this._localStream$=this.createBehaviorSubject(null),this._localAudioTracks$=this.createBehaviorSubject([]),this._localVideoTracks$=this.createBehaviorSubject([]),this._mediaTrackEnded$=this.createSubject()}get localStream$(){return this._localStream$.asObservable().pipe(S(this.destroyed$))}get localAudioTracks$(){return this._localAudioTracks$.asObservable().pipe(S(this.destroyed$))}get localVideoTracks$(){return this._localVideoTracks$.asObservable().pipe(S(this.destroyed$))}get mediaTrackEnded$(){return this._mediaTrackEnded$.asObservable().pipe(S(this.destroyed$))}get localStream(){return this._localStream$.value}get localAudioTracks(){return this._localAudioTracks$.value}get localVideoTracks(){return this._localVideoTracks$.value}async buildLocalStream(){var e,i;Ft.debug("[LocalStreamController] Building local media stream.");let t;if(this.options.inputAudioStream??this.options.inputVideoStream){const r=[...((e=this.options.inputAudioStream)==null?void 0:e.getTracks())??[],...((i=this.options.inputVideoStream)==null?void 0:i.getTracks())??[]];t=new MediaStream(r)}else if(this.options.propose==="screenshare")Ft.debug("[LocalStreamController] Requesting display media for screen sharing with audio:",!!this.options.inputAudioDeviceConstraints),t=await this.options.getDisplayMedia({video:!0,audio:!!this.options.inputAudioDeviceConstraints}),Ft.debug("[LocalStreamController] Screen share media obtained:",t);else{const r={audio:this.options.inputAudioDeviceConstraints,video:this.options.inputVideoDeviceConstraints};Ft.debug("[LocalStreamController] Requesting user media with constraints:",r),t=await this.options.getUserMedia(r),Ft.debug("[LocalStreamController] User media obtained:",t)}return this._localStream$.next(t),this._localAudioTracks$.next(t.getAudioTracks()),this._localVideoTracks$.next(t.getVideoTracks()),t}addTrack(t){const e=this._localStream$.value??new MediaStream;return t.addEventListener("ended",this.mediaTrackEndedHandler),e.addTrack(t),this._localStream$.next(e),t.kind==="video"?this._localVideoTracks$.next(e.getVideoTracks()):this._localAudioTracks$.next(e.getAudioTracks()),Ft.debug(`[LocalStreamController] ${t.kind} track added:`,t.id),e}removeTrack(t){const e=this._localStream$.value,i=e==null?void 0:e.getTracks().find(r=>r.id===t);if(!i){Ft.debug(`[LocalStreamController] track not found: ${t}`);return}return i.removeEventListener("ended",this.mediaTrackEndedHandler),e==null||e.removeTrack(i),i.stop(),this._localStream$.next(e),i.kind==="video"?this._localVideoTracks$.next((e==null?void 0:e.getVideoTracks())??[]):this._localAudioTracks$.next((e==null?void 0:e.getAudioTracks())??[]),Ft.debug(`[LocalStreamController] ${i.kind} track removed:`,t),i}getOrCreateLocalStream(){return this._localStream$.value??new MediaStream}setLocalStream(t){this._localStream$.next(t)}addTrackEndedListener(t){t.addEventListener("ended",this.mediaTrackEndedHandler)}updateOptions(t){this.options={...this.options,...t}}stopAllTracks(){var t;(t=this._localStream$.value)==null||t.getTracks().forEach(e=>{Ft.debug(`[LocalStreamController] Stopping local track: ${e.kind}`),e.removeEventListener("ended",this.mediaTrackEndedHandler),e.stop()})}destroy(){this.stopAllTracks(),super.destroy()}};const Be=W(),rc=(t,e)=>t&&e?"sendrecv":t&&!e?"sendonly":!t&&e?"recvonly":"inactive";var Mg=class extends ie{constructor(t){super(),this.peerConnection=t.peerConnection,this.options=t}get useAddTransceivers(){return typeof this.peerConnection.addTransceiver=="function"}get useAddTrack(){return typeof this.peerConnection.addTrack=="function"}get useAddStream(){return typeof this.peerConnection.addStream=="function"&&!this.useAddTransceivers&&!this.useAddTrack}get propose(){return this.options.propose}get isAdditionalDevice(){return this.propose==="additional-device"}get isScreenShare(){return this.propose==="screenshare"}get isSimulcast(){return!!this.options.simulcast}get isSFU(){return!!this.options.sfu}get receiveVideo(){return!!this.options.receiveVideo}get receiveAudio(){return!!this.options.receiveAudio}get localStream(){return this.options.localStreamController.localStream}get inputAudioDeviceConstraints(){return this.options.getInputAudioDeviceConstraints()}get inputVideoDeviceConstraints(){return this.options.getInputVideoDeviceConstraints()}get audioDirection(){if(this.isAdditionalDevice)return"sendonly";const{localStream:t}=this,e=t==null?void 0:t.getAudioTracks().some(n=>n.enabled),i=!!this.inputAudioDeviceConstraints,r=!!this.receiveAudio;return rc(e||i,r)}get videoDirection(){if(this.isAdditionalDevice||this.isScreenShare)return"sendonly";if(this.isSFU)return"recvonly";const{localStream:t}=this,e=t==null?void 0:t.getVideoTracks().some(n=>n.enabled),i=!!this.inputVideoDeviceConstraints,r=!!this.receiveVideo;return rc(e||i,r)}get sendEncodings(){if(this.isSimulcast)return["0","1","2"].map(t=>({active:!0,rid:t,scaleResolutionDownBy:Number(t)*6||1}))}getConstraintsFor(t){const e=t==="audio"?this.inputAudioDeviceConstraints:this.inputVideoDeviceConstraints;return typeof e=="boolean"?{}:e}transceiverByKind(t){return this.peerConnection.getTransceivers().filter(e=>t==="both"||e.receiver.track.kind===t)}get audioTransceivers(){return this.transceiverByKind("audio")}get videoTransceivers(){return this.transceiverByKind("video")}async setupTransceiverSender(t,e,i){var a;const r=t.kind==="audio",n=r?this.audioDirection:this.videoDirection,s={direction:n,sendEncodings:r?void 0:this.sendEncodings,streams:n==="recvonly"?void 0:[e]};Be.debug(`[TransceiverController] Setting up transceiver sender for local ${t.kind} track:`,{transceiver:i,transceiverParams:s}),s.direction&&["sendonly","sendrecv"].includes(s.direction)&&(i?(await i.sender.replaceTrack(t),i.direction=s.direction,(a=s.streams)!=null&&a.some(o=>!!o)&&(Be.debug(`[TransceiverController] Setting streams for transceiver sender for local ${t.kind} track:`,s.streams),i.sender.setStreams(...s.streams))):(Be.debug(`[TransceiverController] Adding new transceiver for local ${t.kind} track:`,t.id),this.peerConnection.addTransceiver(t,s)))}stopTrackSender(t,e={updateTransceiverDirection:!1}){var i,r,n;try{const s=this.transceiverByKind(t);for(const a of s)if(((i=a.sender.track)==null?void 0:i.readyState)==="live"){const o=a.sender.track.id;a.sender.track.stop(),this.options.localStreamController.removeTrack(o),e.updateTransceiverDirection&&(a.direction="inactive")}}catch(s){Be.error("[TransceiverController] stopTrackSender error",t,s),(n=(r=this.options).onError)==null||n.call(r,new Mi("stopTrackSender",t,s))}}async restoreTrackSender(t){var e,i;try{Be.debug("[TransceiverController] restoreTrackSender called",t);const r={},n=this.transceiverByKind(t);for(const a of n){const{track:o}=a.sender;if(!o||o.readyState==="ended"){const c=(o==null?void 0:o.kind)??a.receiver.track.kind;(c==="audio"||c==="video")&&(r[c]=this.getConstraintsFor(c))}}if(Be.debug("[TransceiverController] restoreTrackSender constraints:",r),Object.keys(r).length===0){Be.warn("[TransceiverController] restoreTrackSender: no tracks need restoration",t);return}const s=(await this.options.getUserMedia(r)).getTracks();Be.debug("[TransceiverController] restoreTrackSender new tracks:",s);for(const a of s){this.options.localStreamController.addTrack(a);const o=a.kind,c=this.transceiverByKind(o)[0];c.direction=o==="audio"?this.audioDirection:this.videoDirection,Be.debug("[TransceiverController] restoreTrackSender setting direction for",o,c.direction),await c.sender.replaceTrack(a)}}catch(r){Be.error("[TransceiverController] restoreTrackSender error",t,r),(i=(e=this.options).onError)==null||i.call(e,new Mi("restoreTrackSender",t,r))}}async replaceSenderTrack(t,e){const i=t==="audio"?this.audioTransceivers:this.videoTransceivers;for(const r of i)await r.sender.replaceTrack(e)}async setupRemoteTransceivers(t){if(t!=="answer"){for(const e of["audio","video"]){const i=e==="audio"?this.audioTransceivers:this.videoTransceivers;for(const r of i){const n=e==="audio"?this.audioDirection:this.videoDirection;["inactive","recvonly"].includes(n)&&(r.direction=n,await r.sender.replaceTrack(null),r.sender.setStreams())}}if(this.videoDirection==="recvonly"&&this.isSFU&&this.useAddTransceivers){const{msStreamsNumber:e=5}=this.options;for(let i=0;i<Number(e);i++)this.peerConnection.addTransceiver("video",{direction:"recvonly"})}}}async updateSendersConstraints(t,e){var r,n;if(!e)return this.stopTrackSender(t),Promise.resolve();const i=this.peerConnection.getSenders().filter(s=>{var a;return((a=s.track)==null?void 0:a.kind)===t&&s.track.readyState==="live"});for(const s of i){const{track:a}=s;if(a){const o={...a.getConstraints(),...e};try{await a.applyConstraints(o),Be.debug(`[TransceiverController] Updated ${t} sender constraints:`,o),Be.debug(`[TransceiverController] Updated ${t} sender constraints:`,a.getConstraints())}catch(c){Be.warn(`[TransceiverController] applyConstraints failed for ${t} track ${a.id}, attempting track replacement fallback:`,c);try{await this.replaceTrackFallback(s,a,t,o)}catch(u){Be.warn(`[TransceiverController] Track replacement fallback also failed for ${t} track:`,u),(n=(r=this.options).onError)==null||n.call(r,new Mi("updateSendersConstraints",t,u))}}}}}async replaceTrackFallback(t,e,i,r){const{deviceId:n}=e.getSettings(),s={...r,...n?{deviceId:{exact:n}}:{}},a=e.id;e.stop(),this.options.localStreamController.removeTrack(a);const o=(await this.options.getUserMedia({[i]:s})).getTracks().find(c=>c.kind===i);if(!o)throw new Mi("replaceTrackFallback",i,new Error("getUserMedia returned no track of the requested kind"));await t.replaceTrack(o),this.options.localStreamController.addTrack(o),Be.debug(`[TransceiverController] Track replacement fallback succeeded for ${i}. New track: ${o.id}`)}getMediaDirections(){return this.peerConnection.connectionState==="connected"?this.peerConnection.getTransceivers().reduce((t,e)=>({...t,[e.receiver.track.kind]:e.direction}),{audio:"inactive",video:"inactive"}):{audio:this.audioDirection,video:this.videoDirection}}updatePeerConnection(t){this.peerConnection=t}updateOptions(t){this.options={...this.options,...t}}};const z=W();var nc=class extends ie{constructor(t={},e,i){super(),this.options=t,this.firstSDPExchangeCompleted=!1,this.negotiationNeeded$=this.createSubject(),this.localDescription$=ei(()=>Ie(this.init())).pipe(Ke(()=>this.iceGatheringController.iceCandidatesState$.pipe(B(n=>!["new","gathering"].includes(n)),Se(()=>{this.negotiationEnded()}),B(()=>this.shouldEmitLocalDescription),E(()=>{var n;return(n=this.peerConnection)==null?void 0:n.localDescription}),ue(),Se(n=>{n.type==="answer"&&(this._type="offer")}))),et(1),S(this.destroyed$)),this.connectionTimeout=3e3,this.oniceconnectionstatechangeHandler=()=>{if(this.peerConnection){const{iceConnectionState:n}=this.peerConnection;z.debug(`[RTCPeerConnectionController] ICE connection state changed to: ${n}`),this._iceConnectionState$.next(this.peerConnection.iceConnectionState)}},this.onconnectionstatechangeHandler=()=>{if(this.peerConnection){const{connectionState:n}=this.peerConnection;z.debug(`[RTCPeerConnectionController] Connection state changed to: ${n}`),n==="connected"&&this.removeConnectionTimer(),this._connectionState$.next(this.peerConnection.connectionState)}},this.onsignalingstatechangeHandler=()=>{var n;z.debug(`[RTCPeerConnectionController] Signaling state changed to: ${(n=this.peerConnection)==null?void 0:n.signalingState}`)},this.onicegatheringstatechangeHandler=()=>{this.peerConnection&&this._iceGatheringState$.next(this.peerConnection.iceGatheringState)},this.onnegotiationneededHandler=n=>{z.debug("[RTCPeerConnectionController] Negotiation needed event received.",n),this.negotiationNeeded$.next()},this.updateSelectedInputDevice=async(n,s)=>{var a,o,c;try{const{localStream:u}=this;if(!u){z.warn("[RTCPeerConnectionController] No local stream available to update input device.");return}z.debug(`[RTCPeerConnectionController] Updating selected ${n} input device:`,u.getTracks());const h=u.getTracks().find(p=>p.kind===n);if(h){if((a=this.transceiverController)==null||a.stopTrackSender(n),this.localStreamController.removeTrack(h.id),z.debug(`[RTCPeerConnectionController] Stopped existing ${n} track: ${h.id}`,u.getTracks()),!s){z.debug(`[RTCPeerConnectionController] ${n} input device selected: none`);return}const p=(await this.getUserMedia({[n]:{...h.getConstraints(),...this.deviceController.deviceInfoToConstraints(s)}})).getTracks().find(f=>f.kind===n);p&&(z.debug(`[RTCPeerConnectionController] Adding new ${n} track: ${p.id}`),this.localStreamController.addTrack(p),await((o=this.transceiverController)==null?void 0:o.replaceSenderTrack(n,p)),z.debug(`[RTCPeerConnectionController] Added new ${n} track: ${p.id}`,(c=this.localStream)==null?void 0:c.getTracks()))}z.debug(`[RTCPeerConnectionController] ${n} input device selected:`,s==null?void 0:s.label)}catch(u){throw z.error(`[RTCPeerConnectionController] Failed to select ${n} input device:`,u),this._errors$.next(bt(u)),u}},this._isNegotiating$=this.createBehaviorSubject(!1),this._memberId=null,this._iceConnectionState$=this.createReplaySubject(1),this._connectionState$=this.createReplaySubject(1),this._signalingState$=this.createReplaySubject(1),this._iceGatheringState$=this.createReplaySubject(1),this._errors$=this.createReplaySubject(1),this._iceCandidates$=this.createReplaySubject(1),this._initialized$=this.createReplaySubject(1),this._remoteDescription$=this.createReplaySubject(1),this._remoteStream$=this.createBehaviorSubject(null),this._remoteOfferMediaDirections=null,this._localAudioPipeline=null,this.deviceController=i??{},this.id=t.callId??Jr(),this._type=e?"answer":"offer",this.sdpInit=e?{type:"offer",sdp:e}:void 0,this._remoteOfferMediaDirections=e?wg(e):null;const r=this._remoteOfferMediaDirections?{audio:this._remoteOfferMediaDirections.audio.includes("recv"),video:this._remoteOfferMediaDirections.video.includes("recv"),receiveAudio:this._remoteOfferMediaDirections.audio.includes("send"),receiveVideo:this._remoteOfferMediaDirections.video.includes("send")}:{};this.options={...t,audio:t.audio??r.audio,video:t.video??r.video,receiveAudio:t.receiveAudio??r.receiveAudio??g.instance.receiveAudio,receiveVideo:t.receiveVideo??r.receiveVideo??g.instance.receiveVideo},this.localStreamController=new Ig({propose:this.propose,inputAudioStream:this.options.inputAudioStream,inputVideoStream:this.options.inputVideoStream,inputAudioDeviceConstraints:this.inputAudioDeviceConstraints,inputVideoDeviceConstraints:this.inputVideoDeviceConstraints,getUserMedia:async n=>this.getUserMedia(n),getDisplayMedia:async n=>this.getDisplayMedia(n)})}get iceGatheringController(){if(!this._iceGatheringController)throw new Q("ICEGatheringController is not initialized");return this._iceGatheringController}get shouldEmitLocalDescription(){if(!this.peerConnection)return!1;const{localDescription:t,signalingState:e}=this.peerConnection;return!t||!tc(t.sdp)?!1:t.type==="offer"&&e==="have-local-offer"||t.type==="answer"&&e==="stable"}removeConnectionTimer(){this.connectionTimer&&(clearTimeout(this.connectionTimer),this.connectionTimer=void 0)}setMemberId(t){this._memberId=t}get memberId(){return this._memberId}stopTrackSender(t,e={updateTransceiverDirection:!1}){var r,n,s;const i=t==="audio"||t==="both";i&&this._localAudioPipeline&&this.stopRawAudioInputForPipeline(),i?t==="both"?(n=this.transceiverController)==null||n.stopTrackSender("video",e):this._localAudioPipeline||(s=this.transceiverController)==null||s.stopTrackSender(t,e):(r=this.transceiverController)==null||r.stopTrackSender(t,e)}stopRawAudioInputForPipeline(){var e;const t=this.localStreamController.localAudioTracks;for(const i of t)i.readyState==="live"&&(i.stop(),this.localStreamController.removeTrack(i.id));(e=this._localAudioPipeline)==null||e.setInputTrack(null)}get isNegotiating$(){return this._isNegotiating$.asObservable()}get isNegotiating(){return this._isNegotiating$.value}updateMediaDevicesOptions(t){this.options={...this.options,...t}}get iceGatheringState$(){return this.cachedObservable("iceGatheringState$",()=>this._iceGatheringState$.asObservable().pipe(S(this.destroyed$)))}get mediaTrackEnded$(){return this.cachedObservable("mediaTrackEnded$",()=>this.localStreamController.mediaTrackEnded$.pipe(S(this.destroyed$)))}get errors$(){return this.cachedObservable("errors$",()=>this._errors$.asObservable().pipe(S(this.destroyed$)))}get iceCandidates$(){return this.cachedObservable("iceCandidates$",()=>this._iceCandidates$.asObservable().pipe(S(this.destroyed$)))}get initialized$(){return this.cachedObservable("initialized$",()=>this._initialized$.asObservable().pipe(B(t=>t),S(this.destroyed$)))}get remoteDescription$(){return this.cachedObservable("remoteDescription$",()=>this._remoteDescription$.asObservable().pipe(S(this.destroyed$)))}get localStream$(){return this.cachedObservable("localStream$",()=>this.localStreamController.localStream$.pipe(S(this.destroyed$)))}get remoteStream$(){return this.cachedObservable("remoteStream$",()=>this._remoteStream$.asObservable().pipe(S(this.destroyed$)))}get localAudioTracks$(){return this.cachedObservable("localAudioTracks$",()=>this.localStreamController.localAudioTracks$.pipe(S(this.destroyed$)))}get localVideoTracks$(){return this.cachedObservable("localVideoTracks$",()=>this.localStreamController.localVideoTracks$.pipe(S(this.destroyed$)))}get iceConnectionState$(){return this.cachedObservable("iceConnectionState$",()=>this._iceConnectionState$.asObservable().pipe(S(this.destroyed$)))}get connectionState$(){return this.cachedObservable("connectionState$",()=>this._connectionState$.asObservable().pipe(S(this.destroyed$)))}get signalingState$(){return this.cachedObservable("signalingState$",()=>this._signalingState$.asObservable().pipe(S(this.destroyed$)))}get type(){return this._type}get propose(){return this.options.propose??"main"}get isAdditionalDevice(){return this.propose==="additional-device"}get isMainDevice(){return this.propose==="main"}get isScreenShare(){return this.propose==="screenshare"}get iceServers(){if(!this.options.disableUdpIceServers)return this.options.iceServers??[];const t="transport=tcp";return(this.options.iceServers??[]).map(e=>{const i=Array.isArray(e.urls)?e.urls:[e.urls];return{...e,urls:i.filter(r=>r.includes(t))}})}get rtcConfiguration(){const{iceServers:t,...e}=this.options;return{bundlePolicy:"max-compat",iceCandidatePoolSize:10,iceServers:this.iceServers,iceTransportPolicy:this.options.relayOnly?"relay":"all",sdpSemantics:"unified-plan",...e}}get receiveVideo(){return!!this.options.receiveVideo}get receiveAudio(){return!!this.options.receiveAudio}get localStream(){return this.localStreamController.localStream}get remoteStream(){return this._remoteStream$.value}get inputAudioDeviceConstraints(){if(this.options.audio===!1&&!this.options.inputAudioDeviceConstraints)return!1;const t=this.deviceController.selectedAudioInputDeviceConstraints;return t===!1?!1:{...typeof this.options.inputAudioDeviceConstraints=="object"?this.options.inputAudioDeviceConstraints:{},...typeof t=="object"?t:{}}}get inputVideoDeviceConstraints(){if(!this.options.video&&!this.options.inputVideoDeviceConstraints)return!1;const t=this.deviceController.selectedVideoInputDeviceConstraints;return t===!1?!1:{...typeof this.options.inputVideoDeviceConstraints=="object"?this.options.inputVideoDeviceConstraints:{},...typeof t=="object"?t:{}}}get WebRTCPeerConnectionConstructor(){var t;return((t=this.options.webRTCApiProvider)==null?void 0:t.RTCPeerConnection)??RTCPeerConnection}get offerOptions(){const t={iceRestart:this.firstSDPExchangeCompleted?!0:void 0};switch(this.propose){case"screenshare":case"additional-device":return{...t,offerToReceiveAudio:!1,offerToReceiveVideo:!1};case"main":default:return{...t,offerToReceiveAudio:!0,offerToReceiveVideo:this.options.receiveVideo??!!this.inputVideoDeviceConstraints}}}get answerOptions(){return{iceRestart:this.firstSDPExchangeCompleted?!0:void 0}}async init(){return this.initPromise??(this.initPromise=this.doInit()),this.initPromise}async doInit(){try{this.setupPeerConnection(),this.subscribeTo(this.negotiationNeeded$.pipe(wd(0),Xn(async()=>this.startNegotiation())),{next:()=>{z.debug("[RTCPeerConnectionController] Start Negotiation completed successfully")},error:t=>{z.error("[RTCPeerConnectionController] Start Negotiation error:",t),this._errors$.next(bt(t))}}),this.subscribeTo(Di(this.deviceController.selectedAudioInputDevice$.pipe(E(t=>["audio",t])),this.deviceController.selectedVideoInputDevice$.pipe(E(t=>["video",t]))).pipe(Dd(()=>!this.localStreamController.localStream)),async([t,e])=>{z.debug("[RTCPeerConnectionController] Selected input device changed for:",{kind:t,deviceInfo:e}),await this.updateSelectedInputDevice(t,e)}),this.type==="answer"&&this.sdpInit?(await this.setupRemoteTracks(),this._initialized$.next(!0),this.setupEventListeners(),this._isNegotiating$.next(!0),await this._setRemoteDescription(this.sdpInit)):(await this.setupTrackHandling(),this._initialized$.next(!0))}catch(t){z.error("[RTCPeerConnectionController] Initialization error:",t),this._errors$.next(bt(t)),this.destroy()}}setupPeerConnection(){this.peerConnection=new this.WebRTCPeerConnectionConstructor(this.rtcConfiguration),this.peerConnection.addEventListener("negotiationneeded",this.onnegotiationneededHandler),this._iceGatheringController=new xg(this.peerConnection,this.isNegotiating$,{iceCandidateTimeout:this.options.iceCandidateTimeout,iceGatheringTimeout:this.options.iceGatheringTimeout,relayOnly:this.options.relayOnly}),this.transceiverController=new Mg({peerConnection:this.peerConnection,propose:this.propose,simulcast:this.options.simulcast,sfu:this.options.sfu,msStreamsNumber:this.options.msStreamsNumber,receiveAudio:this.receiveAudio,receiveVideo:this.receiveVideo,localStreamController:this.localStreamController,getInputAudioDeviceConstraints:()=>this.inputAudioDeviceConstraints,getInputVideoDeviceConstraints:()=>this.inputVideoDeviceConstraints,getUserMedia:async t=>this.getUserMedia(t),onError:t=>{this._errors$.next(t)}})}async startNegotiation(){if(this.isNegotiating){z.debug("[RTCPeerConnectionController] Negotiation already in progress, skipping.");return}if(this.setupEventListeners(),this.type==="answer"){z.debug("[RTCPeerConnectionController] This is an answer type still, skipping offer creation.");return}this._isNegotiating$.next(!0),z.debug("[RTCPeerConnectionController] Starting negotiation.");try{const{offerOptions:t}=this;z.debug("[RTCPeerConnectionController] Creating offer with options:",t),await this.createOffer(t)}catch(t){z.error("[RTCPeerConnectionController] Error during negotiation:",t),this._errors$.next(bt(t))}}async createOffer(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=await this.peerConnection.createOffer(t);await this.setLocalDescription(e)}async updateAnswerStatus({status:t,sdp:e}){let i=t!=="failed";try{t==="received"&&e&&(z.debug("[RTCPeerConnectionController] Received answer SDP:",e),await this._setRemoteDescription({type:"answer",sdp:e}))}catch(r){z.error("[RTCPeerConnectionController] Error updating answer status:",r),this._errors$.next(bt(r)),i=!1}finally{i?this.readyToConnect():this.iceGatheringController.restartICEGatheringWithRelayOnly()}}async updateOfferStatus({status:t,sdp:e}){switch(t){case"received":this._type="answer",this.sdpInit={type:"offer",sdp:e},await this.handleOfferReceived();break;case"failed":z.error("[RTCPeerConnectionController] Offer failed to be processed by remote.");break}}async acceptInbound(t){var i;if(t){const{audio:r,video:n,receiveAudio:s,receiveVideo:a}=t;this.options={...this.options,...r!==void 0?{audio:r}:{},...n!==void 0?{video:n}:{},...s!==void 0?{receiveAudio:s}:{},...a!==void 0?{receiveVideo:a}:{}},(i=this.transceiverController)==null||i.updateOptions({receiveAudio:this.receiveAudio,receiveVideo:this.receiveVideo}),this.localStreamController.updateOptions({inputAudioDeviceConstraints:this.inputAudioDeviceConstraints,inputVideoDeviceConstraints:this.inputVideoDeviceConstraints})}await this.setupLocalTracks();const{answerOptions:e}=this;z.debug("[RTCPeerConnectionController] Creating inbound answer with options:",e),await this.createAnswer(e)}async handleOfferReceived(){if(!this.sdpInit)throw new Q("SDP initialization parameters are not set");this._isNegotiating$.next(!0),await this._setRemoteDescription(this.sdpInit);const{answerOptions:t}=this;z.debug("[RTCPeerConnectionController] Creating answer with options:",t),await this.createAnswer(t)}readyToConnect(){this.firstSDPExchangeCompleted=!0,this.connectionTimer=setTimeout(()=>{var t;this.removeConnectionTimer(),((t=this.peerConnection)==null?void 0:t.connectionState)!=="connected"&&(z.debug("[RTCPeerConnectionController] Connection timeout, restarting ICE gathering with relay only."),this.iceGatheringController.restartICEGatheringWithRelayOnly())},this.connectionTimeout)}async setRemoteDescriptionBefore(t=""){return Promise.resolve(t)}async setLocalDescription(t){var i;const e=await this.setLocalDescriptionBefore(t.sdp);return(i=this.peerConnection)==null?void 0:i.setLocalDescription({...t,sdp:e})}async setLocalDescriptionBefore(t=""){let e=t;const i=this.options.preferredAudioCodecs??g.instance.preferredAudioCodecs,r=this.options.preferredVideoCodecs??g.instance.preferredVideoCodecs,n=this.options.stereo??g.instance.stereoAudio;return(i.length>0||r.length>0)&&(e=Ag(e,i,r),z.debug("[RTCPeerConnectionController] Applied codec preferences to SDP",{preferredAudioCodecs:i,preferredVideoCodecs:r})),n&&(e=Cg(e),z.debug("[RTCPeerConnectionController] Applied stereo Opus to SDP")),Promise.resolve(e)}async createAnswer(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=await this.peerConnection.createAnswer(t);await this.setLocalDescription(e)}setupEventListeners(){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");this._iceConnectionState$.next(this.peerConnection.iceConnectionState),this._connectionState$.next(this.peerConnection.connectionState),this._signalingState$.next(this.peerConnection.signalingState),this._iceGatheringState$.next(this.peerConnection.iceGatheringState),this._remoteDescription$.next(this.peerConnection.remoteDescription),this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.addEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.removeEventListener("iceconnectionstatechange",this.oniceconnectionstatechangeHandler),this.peerConnection.addEventListener("iceconnectionstatechange",this.oniceconnectionstatechangeHandler),this.peerConnection.removeEventListener("connectionstatechange",this.onconnectionstatechangeHandler),this.peerConnection.addEventListener("connectionstatechange",this.onconnectionstatechangeHandler),this.peerConnection.removeEventListener("signalingstatechange",this.onsignalingstatechangeHandler),this.peerConnection.addEventListener("signalingstatechange",this.onsignalingstatechangeHandler)}negotiationEnded(){this._isNegotiating$.next(!1)}async triggerIceRestart(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=t&&!this.options.relayOnly;if(e)try{this.peerConnection.setConfiguration({...this.peerConnection.getConfiguration(),iceTransportPolicy:"relay"}),z.debug("[RTCPeerConnectionController] ICE transport policy set to relay-only")}catch(i){z.warn("[RTCPeerConnectionController] Failed to set relay-only policy:",i)}this.setupEventListeners(),this._isNegotiating$.next(!0),z.debug(`[RTCPeerConnectionController] Triggering ICE restart${t?" (relay-only)":""}.`);try{const i=await this.peerConnection.createOffer({iceRestart:!0});await this.setLocalDescription(i)}catch(i){throw z.error("[RTCPeerConnectionController] ICE restart offer failed:",i),this._errors$.next(bt(i)),this.negotiationEnded(),e&&this.restoreIceTransportPolicy(),i}e&&Ge(cr(this._iceGatheringState$.pipe(B(i=>i==="complete"),Me(1)),xi(Gh).pipe(E(()=>"timeout")))).then(()=>this.restoreIceTransportPolicy()).catch(i=>{z.warn("[RTCPeerConnectionController] Error waiting for ICE gathering to complete:",i),this.restoreIceTransportPolicy()})}restoreIceTransportPolicy(){var t;try{(t=this.peerConnection)==null||t.setConfiguration({...this.peerConnection.getConfiguration(),iceTransportPolicy:this.options.relayOnly?"relay":"all"}),z.debug("[RTCPeerConnectionController] ICE transport policy restored")}catch(e){z.warn("[RTCPeerConnectionController] Failed to restore ICE transport policy:",e)}}async setupTrackHandling(){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");await this.setupLocalTracks(),await this.setupRemoteTracks()}async setupLocalTracks(){var e,i,r,n,s,a,o;z.debug("[RTCPeerConnectionController] Setting up local tracks/transceivers.");const t=this.localStream??await this.localStreamController.buildLocalStream();if(((e=this.transceiverController)==null?void 0:e.useAddStream)??!1){z.warn("[RTCPeerConnectionController] Using deprecated addStream API to add local stream."),(i=this.peerConnection)==null||i.addStream(t),this.isNegotiating||(z.debug("[RTCPeerConnectionController] Forcing negotiationneeded after local tracks setup."),this.negotiationNeeded$.next());return}for(const c of["audio","video"]){const u=(c==="audio"?t.getAudioTracks():t.getVideoTracks()).map((h,p)=>({index:p,track:h}));for(const{index:h,track:p}of u)if(this.localStreamController.addTrackEndedListener(p),((r=this.transceiverController)==null?void 0:r.useAddTransceivers)??!1){const f=(c==="audio"?(n=this.transceiverController)==null?void 0:n.audioTransceivers:(s=this.transceiverController)==null?void 0:s.videoTransceivers)??[];await((a=this.transceiverController)==null?void 0:a.setupTransceiverSender(p,t,f[h]))}else z.debug(`[RTCPeerConnectionController] Using addTrack for local ${c} track:`,p.id),(o=this.peerConnection)==null||o.addTrack(p,t)}}async getUserMedia(t){var e;return(((e=this.options.webRTCApiProvider)==null?void 0:e.mediaDevices)??navigator.mediaDevices).getUserMedia(t)}async getDisplayMedia(t){var i;const e=((i=this.options.webRTCApiProvider)==null?void 0:i.mediaDevices)??navigator.mediaDevices;if(!e.getDisplayMedia)throw new Q("getDisplayMedia is not supported by the current WebRTC provider");return e.getDisplayMedia(t)}async setupRemoteTracks(){var t;if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");this.peerConnection.ontrack=e=>{var i;if(z.debug("[RTCPeerConnectionController] Remote track received:",e.track.kind),e.streams[0])this._remoteStream$.next(e.streams[0]);else{const r=((i=this._remoteStream$.value)==null?void 0:i.getTracks())??[],n=new MediaStream([...r,e.track]);this._remoteStream$.next(n)}},await((t=this.transceiverController)==null?void 0:t.setupRemoteTransceivers(this.type))}async restoreTrackSender(t){var i,r,n;const e=t==="audio"||t==="both";e&&this._localAudioPipeline&&await this.restoreRawAudioInputForPipeline(),e?t==="both"?await((r=this.transceiverController)==null?void 0:r.restoreTrackSender("video")):this._localAudioPipeline||await((n=this.transceiverController)==null?void 0:n.restoreTrackSender(t)):await((i=this.transceiverController)==null?void 0:i.restoreTrackSender(t))}async restoreRawAudioInputForPipeline(){var r;if(!this._localAudioPipeline)return;const t=((r=this.transceiverController)==null?void 0:r.getConstraintsFor("audio"))??{};let e;try{e=await this.getUserMedia({audio:t})}catch(n){z.error("[RTCPeerConnectionController] Failed to re-acquire mic for pipeline restore:",n),this._errors$.next(bt(n));return}const i=e.getAudioTracks().at(0);i&&(this.localStreamController.addTrack(i),this._localAudioPipeline.setInputTrack(i))}ensureLocalAudioPipeline(){if(this._localAudioPipeline)return this._localAudioPipeline;if(!this.peerConnection)return null;try{this._localAudioPipeline=new Dg}catch(t){return z.warn("[RTCPeerConnectionController] Failed to create LocalAudioPipeline:",t),null}return this.subscribeTo(this.localStreamController.localAudioTracks$,()=>{this.applyLocalAudioPipelineToSender()}),this.applyLocalAudioPipelineToSender(),this._localAudioPipeline}get localAudioPipeline(){return this._localAudioPipeline}async applyLocalAudioPipelineToSender(){var i,r;if(!this._localAudioPipeline||!this.peerConnection)return;const t=this.localStreamController.localAudioTracks.at(0);this._localAudioPipeline.setInputTrack(t??null);const e=((r=(i=this.transceiverController)==null?void 0:i.audioTransceivers.at(0))==null?void 0:r.sender)??this.peerConnection.getSenders().find(n=>{var s;return((s=n.track)==null?void 0:s.kind)==="audio"});if(!(!e||!t))try{await e.replaceTrack(this._localAudioPipeline.outputTrack)}catch(n){z.warn("[RTCPeerConnectionController] Failed to route audio sender through pipeline:",n)}}addLocalTrack(t){if(!this.peerConnection){const e=new Q("RTCPeerConnection is not initialized");throw this._errors$.next(e),e}try{const e=this.localStreamController.addTrack(t);this.peerConnection.addTrack(t,e),z.debug(`[RTCPeerConnectionController] ${t.kind} track added:`,t.id)}catch(e){throw z.error(`[RTCPeerConnectionController] Failed to add ${t.kind} track:`,e),this._errors$.next(bt(e)),e}}removeLocalTrack(t){var i,r;if(!this.peerConnection){const n=new Q("RTCPeerConnection is not initialized");throw this._errors$.next(n),n}const e=this.peerConnection.getSenders().find(n=>{var s;return((s=n.track)==null?void 0:s.id)===t});if(!e){z.debug(`[RTCPeerConnectionController] track not found: ${t}`);return}try{this.peerConnection.removeTrack(e),this.localStreamController.removeTrack(t),z.debug(`[RTCPeerConnectionController] ${(i=e.track)==null?void 0:i.kind} track removed:`,t)}catch(n){throw z.error(`[RTCPeerConnectionController] Failed to remove ${(r=e.track)==null?void 0:r.kind} track:`,n),this._errors$.next(bt(n)),n}}setLocalTrack(t){const e=[...t.kind==="audio"?this.localStreamController.localAudioTracks:this.localStreamController.localVideoTracks];for(const i of e)this.removeLocalTrack(i.id);this.addLocalTrack(t)}async updateSendersConstraints(t,e){var i;await((i=this.transceiverController)==null?void 0:i.updateSendersConstraints(t,e))}async replaceAudioTrackWithConstraints(t){var i;const e=(i=this.peerConnection)==null?void 0:i.getSenders().filter(r=>{var n;return((n=r.track)==null?void 0:n.kind)==="audio"&&r.track.readyState==="live"});if(!e||e.length===0){z.warn("[RTCPeerConnectionController] No live audio sender to replace");return}for(const r of e){const n=r.track;if(!n)continue;const{deviceId:s}=n.getSettings(),a={...n.getConstraints(),...t,...s?{deviceId:{exact:s}}:{}},o=n.id;n.stop(),this.localStreamController.removeTrack(o);const c=(await this.getUserMedia({audio:a})).getAudioTracks()[0];await r.replaceTrack(c),this.localStreamController.addTrack(c),z.debug(`[RTCPeerConnectionController] Audio track replaced for server-pushed params. New track: ${c.id}`)}}destroy(){var t,e,i;z.debug(`[RTCPeerConnectionController] Destroying RTCPeerConnectionController. ${this.propose}`),this.removeConnectionTimer(),(t=this._iceGatheringController)==null||t.destroy(),(e=this._localAudioPipeline)==null||e.destroy(),this._localAudioPipeline=null,this.localStreamController.destroy(),(i=this.transceiverController)==null||i.destroy(),this.peerConnection&&(this.stopRemoteTracks(),this.removeAllListeners(),this.peerConnection.close(),this.peerConnection=void 0),super.destroy()}removeAllListeners(){this.peerConnection&&(this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.removeEventListener("iceconnectionstatechange",this.oniceconnectionstatechangeHandler),this.peerConnection.removeEventListener("connectionstatechange",this.onconnectionstatechangeHandler),this.peerConnection.removeEventListener("signalingstatechange",this.onsignalingstatechangeHandler),this.peerConnection.removeEventListener("negotiationneeded",this.onnegotiationneededHandler))}stopRemoteTracks(){var t;(t=this._remoteStream$.value)==null||t.getTracks().forEach(e=>{z.debug(`[RTCPeerConnectionController] Stopping remote track: ${e.kind}`),e.stop()})}get mediaDirections(){var t;return((t=this.transceiverController)==null?void 0:t.getMediaDirections())??this._remoteOfferMediaDirections??{audio:"inactive",video:"inactive"}}async _setRemoteDescription(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=await this.setRemoteDescriptionBefore(t.sdp),i={...t,sdp:e};return z.debug("[RTCPeerConnectionController] Setting remote description:",i),this.peerConnection.setRemoteDescription(i)}};function ls(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"id")}function sc(t){if(!ls(t))return!1;const e=t;return e.method==="verto.invite"&&ke(e.params)&&G(e.params,"sdp")&&G(e.params,"callID")}function us(t){return ls(t)?t.method==="verto.bye":!1}function ac(t){return ls(t)?t.method==="verto.attach":!1}function Rg(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.answer"&&ke(t.params)&&G(t.params,"callID")}function Pg(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.media"&&ke(t.params)&&G(t.params,"callID")&&G(t.params,"sdp")}function Og(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.mediaParams"&&ke(t.params)&&G(t.params,"mediaParams")}function _g(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.ping"}const V=W();function Lg(t){return t.isInvite&&!t.reattach&&!t.explicitNodeId?"":t.currentNodeId??""}var Fg=class extends ie{constructor(t){super(),this.callSession=t}destroy(){this.callSession=void 0,super.destroy()}},Ug=class extends Fg{constructor(t,e,i,r,n={}){super(t),this.webRtcCallSession=t,this.attachManager=e,this.deviceController=i,this.webRTCApiProvider=r,this._rtcPeerConnections$=this.createBehaviorSubject([]),this._selfId$=this.createBehaviorSubject(null),this._signalingStatus$=this.createReplaySubject(1),this._screenShareStatus$=this.createBehaviorSubject("none"),this._rtcPeerConnectionsMap=new Map,this._screenShareTimeoutMs=5e4,this._nodeId$=this.createBehaviorSubject(n.nodeId??null),this.onError=n.onError,this.onModifyFailed=n.onModifyFailed,this.initSubscriptions(),this.initMainPeerConnection()}async hold(){const t=nn({sessid:this.webRtcCallSession.id,dialogParams:{callID:this.webRtcCallSession.id},action:"hold"});try{await this.executeVerto(t)}catch(e){throw V.warn("[WebRTCManager] Call might already be disconnected, error sending Verto hold:",e),e}}async unhold(){const t=nn({sessid:this.webRtcCallSession.id,dialogParams:{callID:this.webRtcCallSession.id},action:"unhold"});try{await this.executeVerto(t)}catch(e){throw V.warn("[WebRTCManager] Call might already be disconnected, error sending Verto unhold:",e),e}}get mediaDirections(){return this.mainPeerConnection.mediaDirections}get rtcPeerConnections$(){return this._rtcPeerConnections$.asObservable()}get rtcPeerConnections(){return this._rtcPeerConnections$.value}get nodeId$(){return this._nodeId$.asObservable()}get selfId$(){return this._selfId$.asObservable()}get localStream(){var t;return((t=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id))==null?void 0:t.localStream)??null}get remoteStream(){var t;return((t=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id))==null?void 0:t.remoteStream)??null}get nodeId(){return this._nodeId$.value}get screenShareStatus(){return this._screenShareStatus$.value}get screenShareStatus$(){return this._screenShareStatus$.asObservable()}get mainPeerConnection(){const t=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id);if(!t)throw new Q("Main peer connection not found");return t}get signalingStatus$(){return this.cachedObservable("signalingStatus$",()=>Di(this._signalingStatus$.asObservable(),this.mainPeerConnection.connectionState$.pipe(B(t=>["connected","disconnected","failed"].includes(t)))))}initSubscriptions(){this.subscribeTo(this.callJoinedEvent$,t=>{var i;const e=(i=t.room_session.members.find(r=>r.call_id===t.call_id))==null?void 0:i.node_id;e&&this.setNodeIdIfNull(e),t.member_id&&this.setSelfIdIfNull(t.member_id)}),this.subscribeTo(this.vertoMedia$,t=>{var r;V.debug("[WebRTCManager] Received Verto media event (early media SDP):",t),this._signalingStatus$.next("ringing");const{sdp:e,callID:i}=t;(r=this._rtcPeerConnectionsMap.get(i))==null||r.updateAnswerStatus({status:"received",sdp:e})}),this.subscribeTo(this.vertoAnswer$,t=>{var r;V.debug("[WebRTCManager] Received Verto answer event:",t),this._signalingStatus$.next("connecting");const{sdp:e,callID:i}=t;(r=this._rtcPeerConnectionsMap.get(i))==null||r.updateAnswerStatus({status:"received",sdp:e})}),this.subscribeTo(this.vertoMediaParams$,t=>{V.debug("[WebRTCManager] Received Verto mediaParams event:",t);const{mediaParams:e,callID:i}=t,r=this._rtcPeerConnectionsMap.get(i),{audio:n,video:s}=e;(async()=>{var a;try{n&&r&&await r.replaceAudioTrackWithConstraints(n),s&&await(r==null?void 0:r.updateSendersConstraints("video",s)),this.webRtcCallSession.emitMediaParamsUpdated({audio:n,video:s,timestamp:Date.now()})}catch(o){V.warn("[WebRTCManager] Error applying server-pushed media params:",o),(a=this.onError)==null||a.call(this,o instanceof Error?o:new Error(String(o),{cause:o}))}})()}),this.subscribeTo(this.vertoPing$,t=>{this.attachManager.attach(this.buildAttachableCall()),this.sendVertoPong(t)})}setNodeIdIfNull(t){!this._nodeId$.value&&t&&(V.debug(`[WebRTCManager] Early node_id set: ${t}`),this._nodeId$.next(t))}setSelfIdIfNull(t){!this._selfId$.value&&t&&(V.debug(`[WebRTCManager] Early selfId set: ${t}`),this._selfId$.next(t))}async sendVertoPong(t){var e;try{const i=$p({...t});await this.executeVerto(i)}catch(i){V.warn("[WebRTCManager] Call might disconnect, error sending Verto pong:",i),(e=this.onError)==null||e.call(this,new so(i))}}async updateMediaConstraints(t={}){var r;const{audio:e,video:i}=t;try{e&&await this.mainPeerConnection.updateSendersConstraints("audio",e),i&&await this.mainPeerConnection.updateSendersConstraints("video",i)}catch(n){throw V.warn("[WebRTCManager] Error updating media constraints:",n),(r=this.onError)==null||r.call(this,n instanceof Error?n:new Error(String(n),{cause:n})),n}}get selfId(){return this._selfId$.value}buildAttachableCall(t){return{nodeId:this.nodeId??void 0,id:t??this.webRtcCallSession.id,to:this.webRtcCallSession.to,mediaDirections:this.webRtcCallSession.mediaDirections}}requestKeyframe(){try{const t=this.mainPeerConnection.peerConnection;if(!t){V.warn("[WebRTCManager] No peer connection for keyframe request");return}const e=t.getReceivers().find(i=>i.track.kind==="video");if(!e){V.warn("[WebRTCManager] No video receiver for keyframe request");return}typeof e.requestKeyFrame=="function"?(e.requestKeyFrame(),V.debug("[WebRTCManager] Keyframe requested via RTCRtpReceiver.requestKeyFrame()")):V.debug("[WebRTCManager] requestKeyFrame() not supported, skipping")}catch(t){V.warn("[WebRTCManager] Keyframe request failed (non-fatal):",t)}}async requestIceRestart(t){try{const e=this.mainPeerConnection;if(!e.peerConnection){V.warn("[WebRTCManager] No peer connection for ICE restart");return}await e.triggerIceRestart(t),V.info(`[WebRTCManager] ICE restart initiated${t?" (relay-only)":""}`)}catch(e){throw V.error("[WebRTCManager] ICE restart failed:",e),e}}async requestIceRestartAll(t){const e=Array.from(this._rtcPeerConnectionsMap.entries());for(const[i,r]of e)try{if(!r.peerConnection){V.debug(`[WebRTCManager] No peer connection for leg ${i}, skipping ICE restart`);continue}await r.triggerIceRestart(t),V.info(`[WebRTCManager] ICE restart initiated for leg ${i}${t?" (relay-only)":""}`)}catch(n){V.warn(`[WebRTCManager] ICE restart failed for leg ${i}:`,n)}}requestKeyframeAll(){for(const[t,e]of this._rtcPeerConnectionsMap){if(e.isScreenShare){V.debug(`[WebRTCManager] Skipping keyframe for send-only screen share leg ${t}`);continue}try{const i=e.peerConnection;if(!i)continue;const r=i.getReceivers().find(n=>n.track.kind==="video");if(!r)continue;typeof r.requestKeyFrame=="function"&&(r.requestKeyFrame(),V.debug(`[WebRTCManager] Keyframe requested for leg ${t}`))}catch(i){V.warn(`[WebRTCManager] Keyframe request failed for leg ${t} (non-fatal):`,i)}}}get callJoinedEvent$(){return this.webRtcCallSession.callEvent$.pipe(B(ec),S(this.destroyed$))}get vertoMedia$(){return this.webRtcCallSession.webrtcMessages$.pipe(fe(Pg,"params"),S(this.destroyed$))}get vertoAnswer$(){return this.cachedObservable("vertoAnswer$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(Rg,"params"),S(this.destroyed$)))}get vertoMediaParams$(){return this.cachedObservable("vertoMediaParams$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(Og,"params"),S(this.destroyed$)))}get vertoBye$(){return this.cachedObservable("vertoBye$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(us,"params"),S(this.destroyed$)))}get vertoAttach$(){return this.cachedObservable("vertoAttach$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(ac,"params"),S(this.destroyed$)))}get vertoPing$(){return this.cachedObservable("vertoPing$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(_g,"params"),S(this.destroyed$)))}async executeVerto(t,e={}){var s,a;const i=No({callID:e.callID??this.webRtcCallSession.id,node_id:e.node_id??this._nodeId$.value??"",message:t,subscribe:e.subscribe}),r=await this.webRtcCallSession.execute(i);if(r.error){const o=new Lt(r.error.code,r.error.message,r.error.data);return(s=this.onError)==null||s.call(this,o),r}const n=tt(r,"result.result");if(n!=null&&n.error){const o=new Lt(n.error.code,n.error.message,n.error.data);return(a=this.onError)==null||a.call(this,o),r}return r}async sendLocalDescription(t,e){var n,s;const i=t.method,r=this.getSendLocalSDPOptionalParams(e,t);try{const a=await this.executeVerto(t,r);switch(i){case"verto.invite":this.processInviteResponse(a,e);break;case"verto.modify":await this.processModifyResponse(a,e);break;default:}}catch(a){V.error(`[WebRTCManager] Error sending Verto ${i}:`,a),(n=this.onError)==null||n.call(this,a instanceof Error?a:new Error(String(a),{cause:a})),i==="verto.modify"&&((s=this.onModifyFailed)==null||s.call(this))}}async processModifyResponse(t,e){var i;if(!t.error){const r=tt(t,"result.result.result.action"),n=tt(t,"result.result.result.sdp");if(r==="updateMedia"&&n)try{await e.updateAnswerStatus({status:"received",sdp:n})}catch(s){V.warn("[WebRTCManager] Error processing modify response:",s);const a=s instanceof Error?s:new Error(String(s),{cause:s});(i=this.onError)==null||i.call(this,a)}}}processInviteResponse(t,e){var i;if(!t.error&&tt(t,"result.result.result.message")==="CALL CREATED"){this._signalingStatus$.next("trying"),this._nodeId$.next(tt(t,"result.node_id")??null);const r=tt(t,"result.result.result.memberID")??null,n=tt(t,"result.result.result.callID")??null;V.debug("[WebRTCManager] Verto invite response:",{callId:n,memberId:r,response:t}),this._selfId$.next(r),e.setMemberId(r),n?(this.webRtcCallSession.addCallId(n),this.attachManager.attach(this.buildAttachableCall(n))):V.warn("[WebRTCManager] Cannot attach call, missing callId:",{nodeId:this.nodeId,callId:n}),V.info("[WebRTCManager] Verto invite successful"),V.debug(`[WebRTCManager] nodeid: ${this._nodeId$.value}, selfId: ${this._selfId$.value}`)}else{V.error("[WebRTCManager] Verto invite failed:",t);const r=t.error?new Lt(t.error.code,t.error.message,t.error.data):new Error("Verto invite failed: unexpected response");(i=this.onError)==null||i.call(this,r)}}get RTCPeerConnectionConfig(){return{iceServers:this.webRtcCallSession.clientSession.iceServers??g.instance.iceServers,relayOnly:g.instance.relayOnly||g.instance.disableUdpIceServers,disableUdpIceServers:g.instance.disableUdpIceServers,iceCandidateTimeout:g.instance.iceCandidateTimeout,iceGatheringTimeout:g.instance.iceGatheringTimeout}}initMainPeerConnection(){const{options:t}=this.webRtcCallSession,e=new nc({propose:"main",callId:this.webRtcCallSession.id,audio:t.audio,video:t.video,inputAudioDeviceConstraints:t.inputAudioDeviceConstraints,inputVideoDeviceConstraints:t.inputVideoDeviceConstraints,inputAudioStream:t.inputAudioStream,inputVideoStream:t.inputVideoStream,receiveAudio:t.receiveAudio,receiveVideo:t.receiveVideo,webRTCApiProvider:this.webRTCApiProvider,preferredVideoCodecs:t.preferredVideoCodecs,preferredAudioCodecs:t.preferredAudioCodecs,stereo:t.stereo,...this.RTCPeerConnectionConfig},t.initOffer,this.deviceController);this.setupLocalDescriptionHandler(e),this.setupVertoByeHandler(),this.setupVertoAttachHandler(),this.initObservables(e),this._rtcPeerConnectionsMap.set(e.id,e),this._rtcPeerConnections$.next(Array.from(this._rtcPeerConnectionsMap.values())),this.subscribeTo(e.errors$,i=>{var r;(r=this.onError)==null||r.call(this,i)}),t.initOffer&&this.handleInboundAnswer(e)}async handleInboundAnswer(t){var i,r,n;V.debug("[WebRTCManager] Waiting for inbound call to be accepted or rejected");const e=await Ge(cr(this.vertoBye$,this.webRtcCallSession.answered$).pipe(S(this.destroyed$))).catch(()=>null);if(e===null){V.debug("[WebRTCManager] Inbound answer handler aborted (destroyed).");return}if(us(e))V.info("[WebRTCManager] Inbound call ended by remote before answer."),(i=this.callSession)==null||i.destroy();else if(e){V.debug("[WebRTCManager] Inbound call accepted, creating SDP answer");const s=this.webRtcCallSession.answerMediaOptions;try{await t.acceptInbound(s)}catch(a){V.error("[WebRTCManager] Error creating inbound answer:",a),(n=this.onError)==null||n.call(this,a instanceof Error?a:new Error(String(a),{cause:a}))}}else{V.info("[WebRTCManager] Inbound call rejected by user.");try{await this.bye("USER_BUSY")}finally{this._signalingStatus$.next("disconnected"),(r=this.callSession)==null||r.destroy()}}}setupVertoAttachHandler(){this.subscribeTo(this.vertoAttach$,async t=>{V.debug("[WebRTCManager] Received Verto attach event for existing call:",t);const{callID:e}=t;await this.attachManager.attach({nodeId:this.nodeId??void 0,id:e,to:t.callee_id_number,mediaDirections:{audio:"sendrecv",video:"inactive"}})})}initObservables(t){this.mediaDirections$=t.connectionState$.pipe(B(e=>e==="connected"),E(()=>t.mediaDirections),Id(t.mediaDirections),S(this.destroyed$)),this.localStream$=t.localStream$.pipe(ue(),S(this.destroyed$)),this.remoteStream$=t.remoteStream$.pipe(ue(),S(this.destroyed$))}setupLocalDescriptionHandler(t){this.subscribeTo(t.localDescription$.pipe(B(e=>e!==null),S(this.destroyed$)),e=>{const{type:i,sdp:r}=e,n=this.dialogParams(t),s=!t.firstSDPExchangeCompleted;if(i==="answer"){const a=Qp({dialogParams:n,sdp:r});this.sendLocalDescriptionOnceAccepted(a,t)}else if(s){const a=Yp({dialogParams:n,sdp:r});this.sendLocalDescription(a,t)}else{const a=nn({dialogParams:n,sdp:r,action:"updateMedia"});this.sendLocalDescription(a,t)}})}setupVertoByeHandler(){this.subscribeTo(this.vertoBye$,()=>{var t;this._signalingStatus$.next("disconnected"),this.attachManager.detach(this.buildAttachableCall()),(t=this.callSession)==null||t.destroy()})}getSendLocalSDPOptionalParams(t,e){let i;return t.firstSDPExchangeCompleted||(i=[],t.isMainDevice?i.push(...g.instance.inviteSubscribeMainDevice):t.isAdditionalDevice?i.push(...g.instance.inviteSubscribeAdditionalDevice):t.isScreenShare&&i.push(...g.instance.inviteSubscribeScreenshare)),{callID:t.id,node_id:Lg({isInvite:sc(e),reattach:this.webRtcCallSession.options.reattach===!0,explicitNodeId:this.webRtcCallSession.options.nodeId,currentNodeId:this._nodeId$.value}),subscribe:i}}async sendLocalDescriptionOnceAccepted(t,e){var r,n,s;V.debug("[WebRTCManager] Waiting for call to be accepted or ended before sending answer");const i=await Ge(cr(this.vertoBye$,this.webRtcCallSession.answered$).pipe(S(this.destroyed$))).catch(()=>null);if(i===null){V.debug("[WebRTCManager] Destroyed while waiting for call acceptance");return}if(us(i))V.info("[WebRTCManager] Call ended before answer was sent."),(r=this.callSession)==null||r.destroy();else if(i){V.debug("[WebRTCManager] Call accepted, sending answer");try{this._signalingStatus$.next("connecting"),await this.sendLocalDescription(t,e),await e.updateAnswerStatus({status:"sent"}),await this.attachManager.attach(this.buildAttachableCall())}catch(a){V.error("[WebRTCManager] Error sending Verto answer:",a),(s=this.onError)==null||s.call(this,a instanceof Error?a:new Error(String(a),{cause:a})),await e.updateAnswerStatus({status:"failed"})}}else{V.info("[WebRTCManager] Call was not accepted, sending verto.bye.");try{await this.bye("USER_BUSY")}finally{this._signalingStatus$.next("disconnected"),(n=this.callSession)==null||n.destroy()}}}dialogParams(t){const e=t.memberId??this._selfId$.value??void 0,i=t.propose==="main"&&!t.firstSDPExchangeCompleted&&this.webRtcCallSession.options.reattach;return{id:t.isMainDevice?this.webRtcCallSession.id:t.id,destinationNumber:this.webRtcCallSession.to??this.webRtcCallSession.from,attach:i,reattaching:i,callerName:this.webRtcCallSession.fromName,callerNumber:this.webRtcCallSession.from,remoteCallerName:this.webRtcCallSession.toName,remoteCallerNumber:this.webRtcCallSession.to,userVariables:{memberCallId:this.webRtcCallSession.id,memberId:e,...this.webRtcCallSession.userVariables},screenShare:t.isScreenShare,additionalDevice:t.isAdditionalDevice,pingSupported:!0,version:gh}}muteMainAudioInputDevice(){return this.mainPeerConnection.stopTrackSender("audio")}muteMainVideoInputDevice(){return this.mainPeerConnection.stopTrackSender("video")}async unmuteMainAudioInputDevice(){return this.mainPeerConnection.restoreTrackSender("audio")}async unmuteMainVideoInputDevice(){return this.mainPeerConnection.restoreTrackSender("video")}ensureLocalAudioPipeline(){return this.mainPeerConnection.ensureLocalAudioPipeline()}get localAudioPipeline(){return this.mainPeerConnection.localAudioPipeline}async addInputDevice(t={audio:!1,video:!0}){return this.initAdditionalPeerConnection("additional-device",t)}async addMainInputDevices(t={audio:!0}){var r;let e;const{mediaDirections:i}=this.mainPeerConnection;if((t.audio??t.inputAudioDeviceConstraints??(t.inputAudioStream&&i.audio.startsWith("send")))&&(e="audio"),(t.video??t.inputVideoDeviceConstraints??(t.inputVideoStream&&!i.video.startsWith("send")))&&(e=e==="audio"?"both":"video"),e)this.mainPeerConnection.updateMediaDevicesOptions(t),await this.mainPeerConnection.restoreTrackSender(e);else{const n=new ro("No valid device to be added");throw(r=this.onError)==null||r.call(this,n),n}}async addScreenMedia(t={audio:!1}){await this.initAdditionalPeerConnection("screenshare",t)}async initAdditionalPeerConnection(t,e){var r;let i=null;try{return this._screenShareStatus$.next("starting"),i=new nc({...e,...this.RTCPeerConnectionConfig,propose:t,webRTCApiProvider:this.webRTCApiProvider},void 0,this.deviceController),this.setupLocalDescriptionHandler(i),t==="screenshare"&&(this._screenShareId=i.id),this._rtcPeerConnectionsMap.set(i.id,i),this._rtcPeerConnections$.next(Array.from(this._rtcPeerConnectionsMap.values())),this.subscribeTo(i.errors$,n=>{var s;(s=this.onError)==null||s.call(this,n)}),await Ge(i.connectionState$.pipe(B(n=>n==="connected"),Me(1),or(this._screenShareTimeoutMs),S(this.destroyed$))),this._screenShareStatus$.next("started"),V.info("[WebRTCManager] Screen share started successfully."),i.id}catch(n){V.warn("[WebRTCManager] Error initializing additional peer connection:",n),(r=this.onError)==null||r.call(this,n instanceof Error?n:new Error(String(n),{cause:n})),i&&i.destroy(),this._screenShareStatus$.next("none")}}async removeInputDevices(t){return this.removeAdditionalPeerConnection(t)}removeMainInputDevice(t={removeAudio:!1,removeVideo:!0}){let e;if(t.removeAudio&&(e="audio"),t.removeVideo&&(e=e==="audio"?"both":"video"),e)return this.mainPeerConnection.stopTrackSender(e,{updateTransceiverDirection:!0})}async removeScreenMedia(){if(["starting","started"].includes(this._screenShareStatus$.value)||V.warn("[WebRTCManager] No active screen share to stop."),!this._screenShareId){V.debug("[WebRTCManager] No screen share peer connection found.");return}this._screenShareStatus$.next("stopping"),await this.removeAdditionalPeerConnection(this._screenShareId),this._screenShareId=void 0,this._screenShareStatus$.next("none")}async removeAdditionalPeerConnection(t){const e=this._rtcPeerConnectionsMap.get(t);try{e&&await this.executeVertoBye(e)}finally{e==null||e.destroy(),this._rtcPeerConnectionsMap.delete(t),this._rtcPeerConnections$.next(Array.from(this._rtcPeerConnectionsMap.values()))}}async executeVertoBye(t,e){try{const i=e?{cause:e,causeCode:eg[e]}:{};await this.executeVerto(Kp({...i,dialogParams:this.dialogParams(t)}))}catch(i){throw V.warn("[WebRTCManager] Call might already be disconnected, error sending Verto bye:",i),i}}async bye(t){this.attachManager.detach(this.buildAttachableCall());const e=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id);e&&await this.executeVertoBye(e,t)}async sendDigits(t){const e=Zp({sessid:this.webRtcCallSession.id,dialogParams:{callID:this.webRtcCallSession.id},dtmf:t});try{await this.executeVerto(e)}catch(i){throw V.warn("[WebRTCManager] Error sending DTMF digits:",i),i}}async transfer(t){const e=nn({...t,dialogParams:this.dialogParams(this.mainPeerConnection),action:"transfer"});try{V.debug("[WebRTCManager] Transferring call with options:",t),await this.executeVerto(e)}catch(i){throw V.error("[WebRTCManager] Error transferring call:",i),i}}destroy(){this._rtcPeerConnectionsMap.forEach(t=>{t.destroy()}),this._rtcPeerConnectionsMap.clear(),this._rtcPeerConnections$.complete(),super.destroy()}};const oc=W();var Ng=class extends ie{constructor(t={}){super(),this._source=null,this._stream=null,this._audioContext=(t.audioContextFactory??(()=>new AudioContext))(),this._analyser=this._audioContext.createAnalyser(),this._analyser.fftSize=2048,this._analyser.smoothingTimeConstant=.3,this._analyserBuffer=new Uint8Array(new ArrayBuffer(this._analyser.fftSize)),this._pollIntervalMs=t.pollIntervalMs??Co}get level$(){return this.deferEmission(Wr(this._pollIntervalMs,Ta).pipe(E(()=>this.computeLevel())))}setStream(t){if(this._source){try{this._source.disconnect()}catch(e){oc.debug("[RemoteAudioMeter] source disconnect warning:",e)}this._source=null,this._stream=null}!t||t.getAudioTracks().length===0||(this._stream=new MediaStream(t.getAudioTracks()),this._source=this._audioContext.createMediaStreamSource(this._stream))}destroy(){if(this._source){try{this._source.disconnect()}catch{}this._source=null}this._audioContext.close().catch(t=>{oc.debug("[RemoteAudioMeter] audio context close warning:",t)}),super.destroy()}computeLevel(){if(!this._source)return 0;this._analyser.getByteTimeDomainData(this._analyserBuffer);let t=0;for(const e of this._analyserBuffer){const i=(e-128)/128;t+=i*i}return Math.sqrt(t/this._analyserBuffer.length)}};const dr=W(),Bg=1e3,zg=10,Vg=2e3,Hg=3e3,jg=3,Wg=5,Gg=5,qg=10,Xg=4,Yg=30;function Kg(t,e){const i=t+e;return i===0?0:t/i*100}function Zg(t){return typeof t=="object"&&t!==null&&"type"in t&&t.type==="inbound-rtp"}function Qg(t){return typeof t=="object"&&t!==null&&"type"in t&&t.type==="candidate-pair"}var Jg=class extends ie{constructor(t,e={}){super(),this.peerConnection=t,this.running=!1,this.lastAudioPacketsReceived=0,this.lastAudioPacketChangeTime=0,this.lastVideoPacketsReceived=0,this.lastVideoPacketChangeTime=0,this.lastRoundTripTime=0,this.lastAvailableOutgoingBitrate=void 0,this._sample$=this.createReplaySubject(1),this._baseline$=this.createBehaviorSubject({rtt:0,jitter:0,ready:!1}),this._networkIssues$=this.createBehaviorSubject([]),this._networkMetrics$=this.createBehaviorSubject([]),this.pollingIntervalMs=e.pollingIntervalMs??Bg,this.baselineSampleCount=e.baselineSamples??zg,this.noAudioPacketThresholdMs=e.noAudioPacketThresholdMs??Vg,this.noVideoPacketThresholdMs=e.noVideoPacketThresholdMs??Hg,this.rttSpikeWarningMultiplier=e.rttSpikeWarningMultiplier??jg,this.rttSpikeCriticalMultiplier=e.rttSpikeCriticalMultiplier??Wg,this.packetLossWarningPercent=e.packetLossWarningPercent??Gg,this.packetLossCriticalPercent=e.packetLossCriticalPercent??qg,this.jitterSpikeMultiplier=e.jitterSpikeMultiplier??Xg,this.historyWindowSeconds=e.historyWindowSeconds??Yg}get networkIssues$(){return this._networkIssues$.asObservable()}get networkIssues(){return[...this._networkIssues$.value]}get isNetworkHealthy$(){return this.cachedObservable("isNetworkHealthy$",()=>this._networkIssues$.pipe(E(t=>t.length===0),L(),S(this.destroyed$)))}get isNetworkHealthy(){return this._networkIssues$.value.length===0}get networkMetrics$(){return this._networkMetrics$.asObservable()}get networkMetrics(){return[...this._networkMetrics$.value]}get criticalIssue$(){return this.cachedObservable("criticalIssue$",()=>this._networkIssues$.pipe(qn(t=>Ie(t.filter(e=>e.severity==="critical"))),S(this.destroyed$)))}get sample$(){return this._sample$.asObservable()}start(){if(this.running)return;this.running=!0;const t=Date.now();this.lastAudioPacketChangeTime=t,this.lastVideoPacketChangeTime=t,dr.debug("[RTCStatsMonitor] Starting stats monitoring"),this.subscribeTo(Wr(this.pollingIntervalMs).pipe(B(()=>this.running),Ke(()=>Ie(this.peerConnection.getStats()).pipe(ti(e=>(dr.warn("[RTCStatsMonitor] Failed to get stats:",e),$t)))),B(()=>this.running),E(e=>this.extractSample(e))),e=>this._sample$.next(e)),this.subscribeTo(this._sample$.pipe(Me(this.baselineSampleCount),Ad(),E(e=>({rtt:e.reduce((i,r)=>i+r.roundTripTime,0)/e.length,jitter:e.reduce((i,r)=>i+r.audioJitter,0)/e.length,ready:!0}))),e=>{dr.debug(`[RTCStatsMonitor] Baseline established: rtt=${e.rtt.toFixed(1)}ms, jitter=${e.jitter.toFixed(1)}ms`),this._baseline$.next(e)}),this.subscribeTo(this._sample$.pipe(Ka((e,i)=>({prev:e.current,current:i}),{prev:null,current:null}),B(e=>e.current!==null)),({prev:e,current:i})=>{const r=i.timestamp;this.updatePacketTracking(i,r);const n=this.detectIssues(i,e,r);this._networkIssues$.next(n)}),this.subscribeTo(this._sample$.pipe(Ka((e,i)=>{const r=i.timestamp-this.historyWindowSeconds*1e3,n={timestamp:i.timestamp,audio:{packetsReceived:i.audioPacketsReceived,packetsLost:i.audioPacketsLost,jitter:i.audioJitter},video:{packetsReceived:i.videoPacketsReceived,packetsLost:i.videoPacketsLost},roundTripTime:i.roundTripTime,availableOutgoingBitrate:i.availableOutgoingBitrate};return[...e.filter(s=>s.timestamp>r),n]},[])),e=>this._networkMetrics$.next(e))}stop(){this.running&&(this.running=!1,dr.debug("[RTCStatsMonitor] Stopping stats monitoring"))}destroy(){dr.debug("[RTCStatsMonitor] Destroying RTCStatsMonitor"),this.stop(),super.destroy()}extractSample(t){let e=0,i=0,r=0,n=0,s=0,a=0,o;return t.forEach(c=>{Zg(c)&&(c.kind==="audio"?(e+=c.packetsReceived??0,i+=c.packetsLost??0,r=Math.max(r,(c.jitter??0)*1e3)):(n+=c.packetsReceived??0,s+=c.packetsLost??0)),Qg(c)&&c.state==="succeeded"&&c.nominated&&(a=c.currentRoundTripTime?c.currentRoundTripTime*1e3:this.lastRoundTripTime,o=c.availableOutgoingBitrate??this.lastAvailableOutgoingBitrate)}),{audioPacketsReceived:e,audioPacketsLost:i,audioJitter:r,videoPacketsReceived:n,videoPacketsLost:s,roundTripTime:a,availableOutgoingBitrate:o,timestamp:Date.now()}}updatePacketTracking(t,e){t.audioPacketsReceived!==this.lastAudioPacketsReceived&&(this.lastAudioPacketsReceived=t.audioPacketsReceived,this.lastAudioPacketChangeTime=e),t.videoPacketsReceived!==this.lastVideoPacketsReceived&&(this.lastVideoPacketsReceived=t.videoPacketsReceived,this.lastVideoPacketChangeTime=e),t.roundTripTime>0&&(this.lastRoundTripTime=t.roundTripTime),t.availableOutgoingBitrate!==void 0&&(this.lastAvailableOutgoingBitrate=t.availableOutgoingBitrate)}detectIssues(t,e,i){const r=[],n=this._baseline$.value,s=i-this.lastAudioPacketChangeTime;s>this.noAudioPacketThresholdMs&&r.push({type:"no_inbound_audio",severity:"critical",timestamp:i,value:s,threshold:this.noAudioPacketThresholdMs});const a=i-this.lastVideoPacketChangeTime;if(a>this.noVideoPacketThresholdMs&&r.push({type:"no_inbound_video",severity:"warning",timestamp:i,value:a,threshold:this.noVideoPacketThresholdMs}),n.ready){const o=t.roundTripTime,c=n.rtt;if(c>0){const p=o/c;p>this.rttSpikeCriticalMultiplier?r.push({type:"high_rtt",severity:"critical",timestamp:i,value:o,threshold:c*this.rttSpikeCriticalMultiplier}):p>this.rttSpikeWarningMultiplier&&r.push({type:"high_rtt",severity:"warning",timestamp:i,value:o,threshold:c*this.rttSpikeWarningMultiplier})}const u=t.audioJitter,h=n.jitter;h>0&&u/h>this.jitterSpikeMultiplier&&r.push({type:"high_jitter",severity:"warning",timestamp:i,value:u,threshold:h*this.jitterSpikeMultiplier})}if(e){const o=Math.max(0,t.audioPacketsReceived-e.audioPacketsReceived),c=Math.max(0,t.audioPacketsLost-e.audioPacketsLost),u=Math.max(0,t.videoPacketsReceived-e.videoPacketsReceived),h=Math.max(0,t.videoPacketsLost-e.videoPacketsLost),p=o+u,f=Kg(c+h,p);f>this.packetLossCriticalPercent?r.push({type:"high_packet_loss",severity:"critical",timestamp:i,value:f,threshold:this.packetLossCriticalPercent}):f>this.packetLossWarningPercent&&r.push({type:"high_packet_loss",severity:"warning",timestamp:i,value:f,threshold:this.packetLossWarningPercent})}return r}};const oe=W(),$g=2e3,ef=1e4,tf=3e3,rf=5e3,nf=3,sf=3,af=3e3,of=1e4,cf=150,lf=300,uf=5,df=new Set(["high_rtt","high_jitter","high_packet_loss"]);var hf=class extends ie{constructor(t,e,i={}){super(),this._recoveryState$=this.createBehaviorSubject("idle"),this._recoveryEvent$=this.createSubject(),this._bandwidthConstrained$=this.createBehaviorSubject(!1),this._hasPacketLoss$=this.createBehaviorSubject(!1),this._trigger$=this.createSubject(),this._attemptCount=0,this._keyframeBurstCount=0,this._keyframeBurstStart=0,this._keyframeCooldownUntil=0,this._cooldownUntil=0,this._pipelineStop$=this.createSubject(),this._callbacks=t,this._inputs=e,this._config={debounceTimeMs:i.debounceTimeMs??$g,cooldownMs:i.cooldownMs??ef,iceGracePeriodMs:i.iceGracePeriodMs??tf,iceRestartTimeoutMs:i.iceRestartTimeoutMs??rf,maxAttempts:i.maxAttempts??nf,enableRelayFallback:i.enableRelayFallback??!0,keyframeMaxBurst:i.keyframeMaxBurst??sf,keyframeBurstWindowMs:i.keyframeBurstWindowMs??af,keyframeCooldownMs:i.keyframeCooldownMs??of,degradationBitrateThreshold:i.degradationBitrateThreshold??cf,degradationRecoveryThreshold:i.degradationRecoveryThreshold??lf,enableAutoDegradation:i.enableAutoDegradation??!0,packetLossRecoveryDelaySec:i.packetLossRecoveryDelaySec??uf},this.initPipeline(),this.initDegradationRecoveryPipeline()}get recoveryState$(){return this._recoveryState$.asObservable().pipe(S(this._destroyed$))}get recoveryState(){return this._recoveryState$.value}get recoveryEvent$(){return this._recoveryEvent$.asObservable().pipe(S(this._destroyed$))}get bandwidthConstrained$(){return this._bandwidthConstrained$.asObservable().pipe(S(this._destroyed$))}get bandwidthConstrained(){return this._bandwidthConstrained$.value}pushTrigger(t){this._trigger$.next(t)}async requestIceRestart(){if(this._recoveryState$.value==="recovering"){oe.info("CallRecoveryManager: manual ICE restart skipped — recovery already in progress");return}oe.info("CallRecoveryManager: manual ICE restart requested"),this.transitionTo("recovering"),await this.executeIceRestart(!1),this.startCooldown()}requestKeyframe(){this.executeKeyframe("manual request")}reset(){oe.info("CallRecoveryManager: resetting counters"),this._attemptCount=0,this._keyframeBurstCount=0,this._keyframeBurstStart=0,this._keyframeCooldownUntil=0,this._cooldownUntil=0,this.transitionTo("idle")}notifyModifyFailed(){(this._recoveryState$.value==="cooldown"||this._recoveryState$.value==="idle")&&(oe.info("CallRecoveryManager: verto.modify failed — re-entering recovery"),this._cooldownUntil=0,this.transitionTo("idle"),this.pushTrigger({source:"network",detail:"modify_failed_during_recovery"}))}reportBandwidth(t){if(!this._config.enableAutoDegradation)return;const e=this._bandwidthConstrained$.value;!e&&t<this._config.degradationBitrateThreshold?(this._bandwidthConstrained$.next(!0),this._callbacks.disableVideo(),this.emitEvent({action:"video_disabled",reason:`bandwidth ${t}kbps below threshold ${this._config.degradationBitrateThreshold}kbps`,timestamp:Date.now()}),oe.warn(`CallRecoveryManager: disabling video — bandwidth ${t}kbps < ${this._config.degradationBitrateThreshold}kbps`)):e&&t>=this._config.degradationRecoveryThreshold&&(this._bandwidthConstrained$.next(!1),this._callbacks.enableVideo(),this.emitEvent({action:"video_restored",reason:`bandwidth ${t}kbps recovered above ${this._config.degradationRecoveryThreshold}kbps`,timestamp:Date.now()}),oe.info(`CallRecoveryManager: restoring video — bandwidth ${t}kbps >= ${this._config.degradationRecoveryThreshold}kbps`))}reportNetworkIssues(t){const e=t.some(i=>i.type==="high_packet_loss");e!==this._hasPacketLoss$.value&&this._hasPacketLoss$.next(e)}handleWebSocketReconnect(){const t=this._callbacks.getPeerConnectionState();t==="connected"||t==="completed"?(oe.info("CallRecoveryManager: signal-only reconnect — peer connection still alive"),this.emitEvent({action:"signal_reconnect",reason:"WebSocket reconnected, peer connection still connected",timestamp:Date.now()})):(oe.info("CallRecoveryManager: full reconnect — peer connection also down"),this.emitEvent({action:"full_reconnect",reason:"WebSocket reconnected, peer connection not connected — ICE restart needed",timestamp:Date.now()}),this.pushTrigger({source:"network",detail:"full_reconnect_after_ws"})),this.reset()}destroy(){this._pipelineStop$.next(),this._pipelineStop$.complete(),super.destroy()}initPipeline(){this.subscribeTo(this._trigger$.pipe(Se(()=>{this._recoveryState$.value==="idle"&&this.transitionTo("debouncing")}),Xa(this._config.debounceTimeMs),Za(this._inputs.signalingReady$),B(([,t])=>this.passGateChecks(t)),E(([t])=>t),Xn(t=>this.executeTieredRecovery(t)),S(Di(this._destroyed$,this._pipelineStop$))),{next:()=>{},error:t=>{oe.error("CallRecoveryManager: pipeline error",t),this.transitionTo("idle")}})}initDegradationRecoveryPipeline(){if(!this._config.enableAutoDegradation)return;const t=this._config.packetLossRecoveryDelaySec*1e3;this.subscribeTo(Ha([this._bandwidthConstrained$,this._hasPacketLoss$]).pipe(Ke(([e,i])=>e&&!i?xi(t):$t),S(this._destroyed$)),()=>{this._bandwidthConstrained$.next(!1),this._callbacks.enableVideo(),this.emitEvent({action:"video_restored",reason:`no packet loss for ${this._config.packetLossRecoveryDelaySec}s — restoring video`,timestamp:Date.now()}),oe.info(`CallRecoveryManager: restoring video — no packet loss for ${this._config.packetLossRecoveryDelaySec}s`)})}passGateChecks(t){return this._callbacks.isNegotiating()?(oe.debug("CallRecoveryManager: gate blocked — negotiation in progress"),this.transitionTo("idle"),!1):t?this._callbacks.isCallConnected()?this.isCooldownActive()?(oe.debug("CallRecoveryManager: gate blocked — cooldown active"),this.transitionTo("cooldown"),!1):!0:(oe.debug("CallRecoveryManager: gate blocked — call not connected"),this.transitionTo("idle"),!1):(oe.debug("CallRecoveryManager: gate blocked — signaling not ready"),this.transitionTo("idle"),!1)}isCooldownActive(){return Date.now()<this._cooldownUntil}executeTieredRecovery(t){return this.transitionTo("recovering"),oe.info(`CallRecoveryManager: starting tiered recovery — source=${t.source} detail=${t.detail}`),Ie(this.runTiers(t)).pipe(Se(()=>this.startCooldown()),ti(e=>(oe.error("CallRecoveryManager: tiered recovery failed",e),this.startCooldown(),$t)))}async runTiers(t){if(this.executeKeyframe(t.detail),t.issueType&&df.has(t.issueType)){oe.debug(`CallRecoveryManager: degradation-only issue (${t.issueType}) — Tier 1 only, skipping ICE restart`);return}this._attemptCount<this._config.maxAttempts&&await this.executeIceRestart(!1)||this._config.enableRelayFallback&&this._attemptCount<this._config.maxAttempts&&await this.executeIceRestart(!0)||this._attemptCount>=this._config.maxAttempts&&(this.emitEvent({action:"max_attempts_reached",reason:`all ${this._config.maxAttempts} recovery attempts exhausted`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.warn("CallRecoveryManager: max recovery attempts reached"))}executeKeyframe(t){const e=Date.now();if(e<this._keyframeCooldownUntil){oe.debug("CallRecoveryManager: keyframe request skipped — cooldown active");return}if(e-this._keyframeBurstStart>this._config.keyframeBurstWindowMs&&(this._keyframeBurstCount=0,this._keyframeBurstStart=e),this._keyframeBurstCount>=this._config.keyframeMaxBurst){this._keyframeCooldownUntil=e+this._config.keyframeCooldownMs,oe.debug(`CallRecoveryManager: keyframe burst limit reached (${this._config.keyframeMaxBurst}), cooldown until ${this._keyframeCooldownUntil}`);return}this._keyframeBurstCount+=1,this._callbacks.requestKeyframe(),this.emitEvent({action:"keyframe_requested",reason:t,timestamp:e}),oe.debug(`CallRecoveryManager: keyframe requested (burst ${this._keyframeBurstCount}/${this._config.keyframeMaxBurst})`)}async executeIceRestart(t){this._attemptCount+=1;const e=t?"Tier 3 (relay-only)":"Tier 2 (standard)";oe.info(`CallRecoveryManager: ${e} ICE restart — attempt ${this._attemptCount}/${this._config.maxAttempts}`),this.emitEvent({action:"reinvite_started",reason:`${e} ICE restart`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()});try{return await this.withTimeout(this._callbacks.requestIceRestart(t),this._config.iceRestartTimeoutMs)?(this.emitEvent({action:"reinvite_succeeded",reason:`${e} ICE restart succeeded`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.info(`CallRecoveryManager: ${e} ICE restart succeeded`),this._attemptCount=0,!0):(this.emitEvent({action:"reinvite_failed",reason:`${e} ICE restart returned false`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.warn(`CallRecoveryManager: ${e} ICE restart failed`),!1)}catch{return this.emitEvent({action:"reinvite_timeout",reason:`${e} ICE restart timed out after ${this._config.iceRestartTimeoutMs}ms`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.warn(`CallRecoveryManager: ${e} ICE restart timed out`),!1}}async withTimeout(t,e){return new Promise((i,r)=>{const n=setTimeout(()=>{r(new Error(`Timeout after ${e}ms`))},e);t.then(s=>{clearTimeout(n),i(s)},s=>{clearTimeout(n),r(s instanceof Error?s:new Error(String(s)))})})}transitionTo(t){const e=this._recoveryState$.value;e!==t&&(oe.debug(`CallRecoveryManager: state ${e} -> ${t}`),this._recoveryState$.next(t))}startCooldown(){this._cooldownUntil=Date.now()+this._config.cooldownMs,this.transitionTo("cooldown"),this._cooldownSubscription&&this._cooldownSubscription.unsubscribe(),this._cooldownSubscription=xi(this._config.cooldownMs).pipe(Me(1),S(this._destroyed$),B(()=>this._recoveryState$.value==="cooldown")).subscribe(()=>this.transitionTo("idle"))}emitEvent(t){this._recoveryEvent$.next(t)}},pf=class{constructor(t,e,i){this.executeMethod=t,this.vertoManager=e,this.deviceController=i}createSelfParticipant(t){return new jo(t,this.executeMethod,this.vertoManager,this.deviceController)}createParticipant(t){return new Ho(t,this.executeMethod,this.deviceController)}};const gf=93.2,ff=.024,vf=2.5,bf=0,mf=100,yf=1,wf=5,Sf=4,Cf=3.5,Af=3,Tf=2;function Ef(t,e,i){const r=gf-(t/2+e)*ff-i*vf,n=Math.min(mf,Math.max(bf,r)),s=1+.035*n+n*(n-60)*(100-n)*7e-6;return Math.min(wf,Math.max(yf,s))}function kf(t){return t>=Sf?"excellent":t>=Cf?"good":t>=Af?"fair":t>=Tf?"poor":"critical"}const me=W(),xf=5/3,Df=t=>{if(!t)return{};try{const e=new URL(`destination:${t}`),i={};return e.searchParams.forEach((r,n)=>{i[n]=r}),i}catch(e){return me.warn(`Failed to parse destination URI: ${t}`,e),{}}};var If=class extends ie{constructor(t,e,i,r){super(),this.clientSession=t,this.options=e,this.address=r,this._errors$=this.createReplaySubject(1),this._lastMergedStatus="new",this._answered$=this.createReplaySubject(),this._holdState=!1,this._userVariables$=this.createBehaviorSubject({...g.instance.userVariables}),this._networkIssues$=this.createBehaviorSubject([]),this._networkMetrics$=this.createBehaviorSubject([]),this._isNetworkHealthy$=this.createBehaviorSubject(!0),this._qualityScore$=this.createBehaviorSubject(5),this._qualityLevel$=this.createBehaviorSubject("excellent"),this._recoveryState$=this.createBehaviorSubject("idle"),this._recoveryEvent$=this.createSubject(),this._bandwidthConstrained$=this.createBehaviorSubject(!1),this._mediaParamsUpdated$=this.createSubject(),this._customSubscriptions=new Map,this._pushToTalkEnabled=!1,this._remoteAudioMeter=null,this.id=e.callId??Jr(),this.to=e.to,this._userVariables$.next({...this._userVariables$.value,...Df(e.to),...e.userVariables}),this.subscribeTo(this.webrtcMessages$,o=>{const c=tt(o,"params.userVariables");c&&this._userVariables$.next({...this._userVariables$.value,...c})});const n=i.initializeManagers(this);this.vertoManager=n.vertoManager,this.callEventsManager=n.callEventsManager,e.initOffer?(this._status$=this.createBehaviorSubject("ringing"),this._lastMergedStatus="ringing"):this._status$=this.createBehaviorSubject("new");const{deviceController:s,networkChange$:a}=i;this._networkChange$=a,this.participantFactory=new pf(this.executeMethod.bind(this),this.vertoManager,s),this.subscribeTo(Di(this._status$.asObservable(),this.vertoManager.signalingStatus$).pipe(L(),S(this._destroyed$)),o=>{var c;this._lastMergedStatus=o,o==="connected"&&!this._statsMonitor?this.initResilienceSubsystems():o==="disconnected"?((c=this._statsMonitor)==null||c.destroy(),this._statsMonitor=void 0):(o==="destroyed"||o==="failed")&&this.stopResilienceSubsystems()})}get errors$(){return this.deferEmission(this._errors$.asObservable())}emitError(t){this._status$.value==="destroyed"||this._status$.value==="failed"||(this._errors$.next(t),t.fatal&&(this._status$.next("failed"),this.destroy()))}notifyModifyFailed(){var t;(t=this._recoveryManager)==null||t.notifyModifyFailed()}get direction(){return this.options.initOffer?"inbound":"outbound"}get address$(){return this.deferEmission(Ie([this.address])).pipe(S(this._destroyed$))}get fromName(){return this.options.fromName}get from(){return this.options.from}get toName(){return this.options.toName}async toggleIncomingVideo(){throw new vt}async toggleIncomingAudio(){throw new vt}addCallId(t){this.callEventsManager.addCallId(t)}get capabilities(){return this.callEventsManager.capabilities}get participants(){return this.callEventsManager.participants}get self(){return this.callEventsManager.self}async toggleLock(){const t=this.locked?"call.unlock":"call.lock";await this.executeMethod(this.selfId??"",t,{})}async toggleHold(){this._holdState?await this.vertoManager.unhold():await this.vertoManager.hold(),this._holdState=!this._holdState}async startRecording(){throw new vt}async startStreaming(){throw new vt}async setMeta(t){throw new vt}async updateMeta(t){throw new vt}get layoutLayers$(){return this.deferEmission(this.callEventsManager.layoutLayers$).pipe(S(this._destroyed$))}get layoutLayers(){return this.callEventsManager.layoutLayers}async executeMethod(t,e,i){var s,a,o;const r=this.buildMethodParams(t,i),n=_i({method:e,params:r});try{const c=await this.clientSession.execute(n);if(cg(c))throw new Lt(parseInt(((s=c.result)==null?void 0:s.code)??"0"),`Error response from method ${e}: ${(a=c.result)==null?void 0:a.code} ${(o=c.result)==null?void 0:o.message}`,void 0,void 0,n.id);return c}catch(c){throw me.error(`[Call] Error executing method ${e} with params`,r,c),c}}buildMethodParams(t,e){const i={node_id:this.nodeId??"",call_id:this.id,member_id:this.vertoManager.selfId??""};return typeof t=="object"?{...e,self:i,targets:[t]}:{...e,self:i,target:{node_id:this.nodeId??"",call_id:this.id,member_id:t}}}get status$(){return this.publicCachedObservable("status$",()=>Di(this._status$.asObservable(),this.vertoManager.signalingStatus$).pipe(L(),Se(t=>{this._lastMergedStatus=t})))}get participants$(){return this.deferEmission(this.callEventsManager.participants$).pipe(S(this._destroyed$))}get self$(){return this.deferEmission(this.callEventsManager.self$).pipe(S(this._destroyed$))}get recording$(){return this.deferEmission(this.callEventsManager.recording$).pipe(S(this._destroyed$))}get streaming$(){return this.deferEmission(this.callEventsManager.streaming$).pipe(S(this._destroyed$))}get raiseHandPriority$(){return this.deferEmission(this.callEventsManager.raiseHandPriority$).pipe(S(this._destroyed$))}get locked$(){return this.deferEmission(this.callEventsManager.locked$).pipe(S(this._destroyed$))}get meta$(){return this.deferEmission(this.callEventsManager.meta$).pipe(S(this._destroyed$))}get capabilities$(){return this.deferEmission(this.callEventsManager.capabilities$).pipe(S(this._destroyed$))}get layout$(){return this.deferEmission(this.callEventsManager.layout$).pipe(S(this._destroyed$))}get status(){return this._lastMergedStatus}get recording(){return this.callEventsManager.recording}get streaming(){return this.callEventsManager.streaming}get raiseHandPriority(){return this.callEventsManager.raiseHandPriority}get locked(){return this.callEventsManager.locked}get meta(){return this.callEventsManager.meta}get layout(){return this.callEventsManager.layout}get layouts$(){return this.deferEmission(this.callEventsManager.layouts$).pipe(S(this._destroyed$))}get layouts(){return this.callEventsManager.layouts}get localStream$(){return this.deferEmission(this.vertoManager.localStream$).pipe(S(this._destroyed$))}get localStream(){return this.vertoManager.localStream}get remoteStream$(){return this.deferEmission(this.vertoManager.remoteStream$).pipe(S(this._destroyed$))}get remoteStream(){return this.vertoManager.remoteStream}get userVariables$(){return this.deferEmission(this._userVariables$.asObservable())}get userVariables(){return{...this._userVariables$.value}}set userVariables(t){this._userVariables$.next({...this._userVariables$.value,...t})}get networkIssues$(){return this.deferEmission(this._networkIssues$.asObservable())}get networkIssues(){return this._networkIssues$.value}get isNetworkHealthy$(){return this.deferEmission(this._isNetworkHealthy$.asObservable())}get isNetworkHealthy(){return this._isNetworkHealthy$.value}get networkMetrics$(){return this.deferEmission(this._networkMetrics$.asObservable())}get networkMetrics(){return this._networkMetrics$.value}get qualityScore$(){return this.deferEmission(this._qualityScore$.asObservable())}get qualityLevel$(){return this.deferEmission(this._qualityLevel$.asObservable())}get recoveryState$(){return this.deferEmission(this._recoveryState$.asObservable())}get recoveryEvent$(){return this.deferEmission(this._recoveryEvent$.asObservable())}get bandwidthConstrained$(){return this.deferEmission(this._bandwidthConstrained$.asObservable())}get mediaParamsUpdated$(){return this.deferEmission(this._mediaParamsUpdated$.asObservable())}emitMediaParamsUpdated(t){this._mediaParamsUpdated$.next(t)}requestKeyframe(){var t,e;(e=(t=this.vertoManager).requestKeyframe)==null||e.call(t)}async requestIceRestart(){var t,e;await((e=(t=this.vertoManager).requestIceRestart)==null?void 0:e.call(t))}initResilienceSubsystems(){const t=this.rtcPeerConnection;if(me.debug(`[Call] initResilienceSubsystems: pc=${t?"exists":"undefined"}, connectionState=${t==null?void 0:t.connectionState}`),!t){me.warn("[Call] No peer connection available, skipping resilience init");return}try{const e=g.instance;this._statsMonitor=new Jg(t,{pollingIntervalMs:e.statsPollingInterval,baselineSamples:e.statsBaselineSamples,noAudioPacketThresholdMs:e.statsNoPacketThreshold,rttSpikeWarningMultiplier:e.statsRttSpikeMultiplier,rttSpikeCriticalMultiplier:e.statsRttSpikeMultiplier*xf,packetLossWarningPercent:e.statsPacketLossThreshold*100,packetLossCriticalPercent:e.statsPacketLossThreshold*200,jitterSpikeMultiplier:e.statsJitterSpikeMultiplier,historyWindowSeconds:e.statsHistorySize}),this._recoveryManager=new hf({requestKeyframe:()=>{var i,r;try{this.vertoManager.requestKeyframeAll?this.vertoManager.requestKeyframeAll():(r=(i=this.vertoManager).requestKeyframe)==null||r.call(i)}catch{}},requestIceRestart:async i=>{var r,n;try{this.vertoManager.requestIceRestartAll?await this.vertoManager.requestIceRestartAll(i):await((n=(r=this.vertoManager).requestIceRestart)==null?void 0:n.call(r,i))}catch{return!1}return this.waitForPeerConnectionConnected()},disableVideo:()=>{try{this.vertoManager.muteMainVideoInputDevice(),me.debug("[Call] Recovery manager disabled video")}catch{me.debug("[Call] Recovery manager failed to disable video")}},enableVideo:()=>{this.vertoManager.unmuteMainVideoInputDevice().catch(()=>{me.debug("[Call] Recovery manager failed to enable video")})},isNegotiating:()=>this.vertoManager.mainPeerConnection.isNegotiating,isCallConnected:()=>this._lastMergedStatus==="connected",getPeerConnectionState:()=>t.connectionState},{signalingReady$:this.clientSession.authenticated$},{debounceTimeMs:e.recoveryDebounceTime,cooldownMs:e.recoveryCooldown,iceGracePeriodMs:e.iceDisconnectedGracePeriod,iceRestartTimeoutMs:e.iceRestartTimeout,maxAttempts:e.maxRecoveryAttempts,enableRelayFallback:e.enableRelayFallback,keyframeMaxBurst:e.keyframeMaxBurst,keyframeBurstWindowMs:e.keyframeBurstWindow,keyframeCooldownMs:e.keyframeCooldown}),this.subscribeTo(this._statsMonitor.networkIssues$,i=>{var r;this._networkIssues$.next(i),(r=this._recoveryManager)==null||r.reportNetworkIssues(i)}),this.subscribeTo(this._statsMonitor.isNetworkHealthy$,i=>{this._isNetworkHealthy$.next(i)}),this.subscribeTo(this._statsMonitor.networkMetrics$,i=>{var r;if(this._networkMetrics$.next(i),i.length>0){const n=i[i.length-1],s=n.audio.packetsReceived+n.video.packetsReceived,a=n.audio.packetsLost+n.video.packetsLost,o=s+a>0?a/(s+a)*100:0,c=Ef(n.roundTripTime,n.audio.jitter,o);this._qualityScore$.next(c),this._qualityLevel$.next(kf(c)),n.availableOutgoingBitrate!==void 0&&((r=this._recoveryManager)==null||r.reportBandwidth(n.availableOutgoingBitrate/1e3))}}),this.subscribeTo(this._statsMonitor.criticalIssue$,i=>{var r;(r=this._recoveryManager)==null||r.pushTrigger({source:"stats",detail:`${i.type}: ${i.severity}`,issueType:i.type})}),this.subscribeTo(this._recoveryManager.recoveryState$,i=>{this._recoveryState$.next(i)}),this.subscribeTo(this._recoveryManager.recoveryEvent$,i=>{this._recoveryEvent$.next(i),i.action==="max_attempts_reached"&&(me.warn("[Call] All recovery attempts exhausted, terminating call"),this.emitError({kind:"network",fatal:!0,error:new Error("Call recovery failed: all attempts exhausted"),callId:this.id}))}),this.subscribeTo(this._recoveryManager.bandwidthConstrained$,i=>{this._bandwidthConstrained$.next(i)}),this._networkChange$&&this.subscribeTo(this._networkChange$,i=>{var r,n;i.type==="offline"?(r=this._recoveryManager)==null||r.pushTrigger({source:"network",detail:"browser went offline"}):i.type==="online"&&((n=this._recoveryManager)==null||n.handleWebSocketReconnect())}),this.subscribeTo(this.clientSession.authenticated$.pipe(Gr(1),B(Boolean)),()=>{var i;me.debug("[Call] WebSocket reconnected — notifying recovery manager"),(i=this._recoveryManager)==null||i.handleWebSocketReconnect()}),this._statsMonitor.start(),me.debug("[Call] Resilience subsystems initialized for call",this.id)}catch(e){me.warn("[Call] Failed to initialize resilience subsystems:",e)}}async waitForPeerConnectionConnected(){const t=this.rtcPeerConnection;if(!t)return!1;const e=Date.now()+qh;for(;;){const i=t.connectionState;if(i==="connected")return!0;if(i==="failed"||i==="closed"||Date.now()>=e)return!1;await new Promise(r=>setTimeout(r,Xh))}}stopResilienceSubsystems(){var t,e;try{(t=this._statsMonitor)==null||t.destroy(),(e=this._recoveryManager)==null||e.destroy()}catch{}this._statsMonitor=void 0,this._recoveryManager=void 0}createParticipant(t,e){return t===(e??this.vertoManager.selfId)?this.participantFactory.createSelfParticipant(t):this.participantFactory.createParticipant(t)}get mediaDirections$(){return this.deferEmission(this.vertoManager.mediaDirections$).pipe(S(this._destroyed$))}get mediaDirections(){return this.vertoManager.mediaDirections}get participantsId$(){return this.cachedObservable("participantsId$",()=>this.participants$.pipe(E(t=>t.map(e=>e.id))))}async execute(t,e){return this.clientSession.execute(t,e)}get selfId$(){return this.vertoManager.selfId$}get selfId(){return this.vertoManager.selfId}get nodeId$(){return this.vertoManager.nodeId$}get nodeId(){return this.vertoManager.nodeId}isCallSessionEvent(t){try{me.debug("[Call] Checking if event is for this call session:",t);const e=tt(t,"params.params.callID")??tt(t,"params.call_id"),i=tt(t,"params.room_session_id");return me.debug(`[Call] Extracted session identifiers callID: ${e} and roomSessionID: ${i} from event:`),e===this.id||!!e&&this.callEventsManager.isCallIdValid(e)||!!i&&this.callEventsManager.isRoomSessionIdValid(i)}catch(e){return me.error("[Call] Error checking if event is for this call session:",e),!1}}get callSessionEvents$(){return this.cachedObservable("callSessionEvents$",()=>this.clientSession.signalingEvent$.pipe(B(t=>this.isCallSessionEvent(t)),Se(t=>{me.debug("[Call] Received call session event:",t)}),S(this.destroyed$),kt()))}get callUpdated$(){return this.publicCachedObservable("callUpdated$",()=>this.callSessionEvents$.pipe(fe(qo,"params"),S(this.destroyed$)))}get memberJoined$(){return this.publicCachedObservable("memberJoined$",()=>this.callSessionEvents$.pipe(fe(Ko,"params"),S(this.destroyed$)))}get memberLeft$(){return this.publicCachedObservable("memberLeft$",()=>this.callSessionEvents$.pipe(fe(Zo,"params"),S(this.destroyed$)))}get memberUpdated$(){return this.publicCachedObservable("memberUpdated$",()=>this.callSessionEvents$.pipe(fe(Yo,"params"),S(this.destroyed$)))}get memberTalking$(){return this.publicCachedObservable("memberTalking$",()=>this.callSessionEvents$.pipe(fe(Qo,"params"),S(this.destroyed$)))}get callStates$(){return this.publicCachedObservable("callStates$",()=>this.callSessionEvents$.pipe(fe(Xo,"params"),S(this.destroyed$)))}get layoutUpdates$(){return this.publicCachedObservable("layoutUpdates$",()=>this.callSessionEvents$.pipe(fe(os,"params"),S(this.destroyed$)))}get rtcPeerConnection(){return this.vertoManager.mainPeerConnection.peerConnection}get signalingEvent$(){return this.publicCachedObservable("signalingEvent$",()=>this.callEvent$.pipe(E(t=>JSON.parse(JSON.stringify(t)))))}subscribe(t){const e=this._customSubscriptions.get(t);if(e)return e;const i=this.callSessionEvents$.pipe(B(r=>r.event_type===t),E(r=>JSON.parse(JSON.stringify(r))),S(this._destroyed$));return this._sendVertoSubscribe(t).then(()=>{this._customSubscriptions.set(t,i)},r=>{this._customSubscriptions.delete(t),me.warn(`[Call] verto.subscribe for '${t}' failed, not caching:`,r)}),this._customSubscriptions.set(t,i),i}get webrtcMessages$(){return this.cachedObservable("webrtcMessages$",()=>this.callSessionEvents$.pipe(fe(sn,"params"),Se(t=>me.debug("[Call] Event is a WebRTC message event:",t)),S(this.destroyed$),kt()))}get callEvent$(){return this.cachedObservable("callEvent$",()=>this.callSessionEvents$.pipe(fe(lg,"params"),Se(t=>me.debug("[Call] Event is a call event:",t)),S(this.destroyed$),kt()))}get layoutEvent$(){return this.cachedObservable("layoutEvent$",()=>this.callEvent$.pipe(fe(os,"params")))}async hangup(){this._status$.next("disconnecting");try{await this.vertoManager.bye()}finally{this.destroy()}}async sendDigits(t){return this.vertoManager.sendDigits(t)}answer(t){this._answerMediaOptions=t,this._answered$.next(!0)}get answerMediaOptions(){return this._answerMediaOptions}reject(){this._answered$.next(!1)}get answered$(){return this.deferEmission(this._answered$.asObservable())}async setLayout(t,e){if(!this.layouts.includes(t))throw new ro(`Layout ${t} is not available in the current call layouts: ${this.layouts.join(", ")}`);const i=await Ge(this.selfId$.pipe(B(r=>r!==null)));await this.executeMethod(i,"call.layout.set",{layout:t,positions:e})}async transfer(t){return this.vertoManager.transfer(t)}setLocalMicrophoneGain(t){const e=this.vertoManager.ensureLocalAudioPipeline();if(!e){me.warn("[Call] setLocalMicrophoneGain: audio pipeline unavailable");return}const i=Math.max(0,Math.min(200,t));e.setGain(i/100)}get localMicrophoneGain$(){const t=this.vertoManager.ensureLocalAudioPipeline();return t?this.publicCachedObservable("localMicrophoneGain$",()=>t.gain$.pipe(E(e=>e*100),S(this._destroyed$))):Et(100).pipe(S(this._destroyed$))}get localAudioLevel$(){const t=this.vertoManager.ensureLocalAudioPipeline();return t?this.publicCachedObservable("localAudioLevel$",()=>t.level$.pipe(S(this._destroyed$),kt())):Et(0).pipe(S(this._destroyed$))}get localSpeaking$(){const t=this.vertoManager.ensureLocalAudioPipeline();return t?this.publicCachedObservable("localSpeaking$",()=>t.speaking$.pipe(S(this._destroyed$),kt())):Et(!1).pipe(S(this._destroyed$))}enablePushToTalk(){const t=this.vertoManager.ensureLocalAudioPipeline();if(!t){me.warn("[Call] enablePushToTalk: audio pipeline unavailable");return}t.setPTTActive(!1),this._pushToTalkEnabled=!0}disablePushToTalk(){var t;(t=this.vertoManager.localAudioPipeline)==null||t.setPTTActive(!0),this._pushToTalkEnabled=!1}setPushToTalkActive(t){var e;this._pushToTalkEnabled&&((e=this.vertoManager.localAudioPipeline)==null||e.setPTTActive(t))}async setEchoCancellation(t){await this.vertoManager.updateMediaConstraints({audio:{echoCancellation:t}})}async setNoiseSuppression(t){await this.vertoManager.updateMediaConstraints({audio:{noiseSuppression:t}})}async setAutoGainControl(t){await this.vertoManager.updateMediaConstraints({audio:{autoGainControl:t}})}get remoteAudioLevel$(){return this.publicCachedObservable("remoteAudioLevel$",()=>{this._remoteAudioMeter??(this._remoteAudioMeter=new Ng);const t=this._remoteAudioMeter;return this.subscribeTo(this.vertoManager.remoteStream$,e=>{t.setStream(e)}),t.level$.pipe(S(this._destroyed$),kt())})}destroy(){var t;this._status$.value!=="destroyed"&&(this._status$.next("destroyed"),this.stopResilienceSubsystems(),(t=this._remoteAudioMeter)==null||t.destroy(),this._remoteAudioMeter=null,this.vertoManager.destroy(),this.callEventsManager.destroy(),super.destroy())}async _sendVertoSubscribe(t){const e=Jp({sessid:this.id,eventChannel:[t]}),i={callID:this.id,node_id:this.vertoManager.nodeId??"",message:e};await this.clientSession.execute(No(i))}};function Mf(t){return t instanceof Yr?"timeout":t instanceof Lt?"signaling":t instanceof Mi?"media":t instanceof $a||t instanceof eo?"network":"internal"}const Rf=new Set([yo,So,wo]);function Pf(t){return!(t instanceof so||t instanceof Mi||t instanceof Yr||t instanceof Lt&&Rf.has(t.code))}var Of=class{constructor(t,e,i,r,n){this.sessionManager=t,this.deviceController=e,this.attachManager=i,this.webRTCApiProvider=r,this.networkChange$=n}createCall(t,e){return new If(this.sessionManager,e,{initializeManagers:i=>({vertoManager:new Ug(i,this.attachManager,this.deviceController,this.webRTCApiProvider,{nodeId:e.nodeId,onError:r=>{const n={kind:Mf(r),fatal:Pf(r),error:r,callId:i.id};i.emitError(n)},onModifyFailed:()=>{i.notifyModifyFailed()}}),callEventsManager:new mg(i)}),deviceController:this.deviceController,networkChange$:this.networkChange$},t)}};const ds=W();var cc=class{constructor(t,e,i){this.endpoint=t,this.http=i,this.filter=r=>!0,this.mapper=r=>r,this.nextUrl=`${this.endpoint}?${e}`}async next(){if(!this.nextUrl)return this.hasMore=!1,[];const t=await this.http.request({...$n,url:this.nextUrl});if(t.ok&&t.body){const e=JSON.parse(t.body);return this.nextUrl=e.links.next,this.hasMore=!!this.nextUrl,e.data.filter(this.filter).map(this.mapper)}return ds.error("Failed to fetch entity"),[]}async id(t){const e=await this.http.request({...$n,url:`${this.endpoint}/${String(t)}`});if(e.ok&&e.body)return JSON.parse(e.body)}},lc=class extends ie{constructor(t,e,i){super(),this.fetchController=t,this.update$=e,this.onError=i,this.collectionData=new Map,this.observablesRegistry=new Map,this.upsertData=r=>{var o;if(!r.id)return;const n=this.collectionData.get(r.id)??{},s={},a=new Set([...Object.keys(n),...Object.keys(r)]);for(const c of a){const u=n[c],h=r[c];h!==void 0&&u!==void 0&&typeof u=="object"&&u!==null&&!Array.isArray(u)&&typeof h=="object"&&h!==null&&!Array.isArray(h)?s[c]={...u,...h}:h!==void 0?s[c]=h:s[c]=u}this.collectionData.set(r.id,s),(o=this.observablesRegistry.get(r.id))==null||o.next(s),this._values$.next(Array.from(this.collectionData.values()))},this._loading$=this.createBehaviorSubject(!1),this._values$=this.createReplaySubject(1),this._hasMore$=this.createBehaviorSubject(!0),this.subscribeTo(this.update$,this.upsertData),this.hasMore$=ei(()=>Ie(this.init())).pipe(Ke(()=>this._hasMore$),L(),et(1),S(this.destroyed$))}get loading$(){return this._loading$.asObservable()}get loading(){return this._loading$.value}get values$(){return this._values$.asObservable()}get hasMore(){return this.fetchController.hasMore??!0}get updated$(){return this.cachedObservable("updated$",()=>this._loading$.pipe(L(),Gr(1),B(t=>!t),E(()=>{}),S(this.destroyed$)))}get values(){return Array.from(this.collectionData.values())}async init(){if(this.fetchController.hasMore===!1){this._hasMore$.next(!1);return}await this.fetchMore()}async fetchMore(){var t;try{this._loading$.next(!0),(await this.fetchController.next()).forEach(this.upsertData),this._hasMore$.next(this.fetchController.hasMore??!1),this._loading$.next(!1)}catch(e){ds.error("Failed to fetch initial collection data",e),this._hasMore$.next(this.fetchController.hasMore??!1),this._loading$.next(!1),(t=this.onError)==null||t.call(this,new ao("fetchMore",e))}}async tryFetch(t,e){var i,r,n;try{this._loading$.next(!0);const s=await((r=(i=this.fetchController)[t])==null?void 0:r.call(i,e));return this._loading$.next(!1),s&&this.upsertData(s),s}catch(s){ds.error(`Failed to fetch data for (${String(t)}:${String(e)}) :`,s),this._loading$.next(!1),(n=this.onError)==null||n.call(this,new ao(`tryFetch(${String(t)})`,s))}}get$(t){var e,i;if(!this.observablesRegistry.has(t)){this.observablesRegistry.set(t,new zn(1));const r=this.collectionData.get(t);r?(e=this.observablesRegistry.get(t))==null||e.next(r):this.tryFetch("id",t)}return(i=this.observablesRegistry.get(t))==null?void 0:i.asObservable()}async find$(t,e){const i=Array.from(this.collectionData.values()).find(r=>r[t]===e)??await this.tryFetch(t,e);return i?this.get$(i.id):void 0}loadMore(){this.fetchController.hasMore!==!1&&this.fetchMore()}destroy(){this.observablesRegistry.forEach(t=>t.complete()),this.observablesRegistry.clear(),super.destroy()}},uc=class{constructor(t,e=r=>!!r,i=r=>r){this.originalCollection=t,this.filter=e,this.mapper=i}get loading$(){return this.originalCollection.loading$}get loading(){return this.originalCollection.loading}get hasMore$(){return this.originalCollection.hasMore$}get hasMore(){return this.originalCollection.hasMore}get values(){return this.originalCollection.values.filter(this.filter).map(this.mapper)}get values$(){return this._values$??(this._values$=this.originalCollection.values$.pipe(E(t=>t.filter(this.filter).map(this.mapper))))}get$(t){const e=this.originalCollection.get$(t);return e&&e.pipe(Vr(B(this.filter),E(this.mapper)))}async find$(t,e){const i=await this.originalCollection.find$(t,e);return i&&i.pipe(Vr(B(this.filter),E(this.mapper)))}loadMore(){this.originalCollection.loadMore()}destroy(){this.originalCollection.destroy()}},dc=class extends ie{constructor(t,e,i){super(),this.addressId=t,this.conversationManager=e,this.addressProvider=i,this.initConversationMessages=async()=>(this._conversationMessages=this._conversationMessages??await this.conversationManager.getConversationMessageCollection(this.id),this._conversationMessages.hasMore&&this._conversationMessages.loadMore(),this._conversationMessages),this.textMessages$=ei(this.initConversationMessages).pipe(E(()=>this.textMessage),et(1),S(this.destroyed$)),this.history$=ei(this.initConversationMessages).pipe(E(()=>this.history),et(1),S(this.destroyed$)),this._state$=this.createBehaviorSubject(null)}upnext(t){const e={...this._state$.value,...t};this._state$.next(e)}get state(){return this._state$.value}get id(){return this.addressId}get name(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.name}get createdAt(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.created_at}get defaultChannel(){return this.type==="room"?this.channels.video:this.channels.audio}get displayName$(){return this.cachedObservable("displayName$",()=>this._state$.pipe(ue(),E(t=>t.display_name),S(this.destroyed$)))}get displayName(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.display_name}get previewUrl$(){return this.cachedObservable("previewUrl$",()=>this._state$.pipe(ue(),E(t=>t.preview_url),S(this.destroyed$)))}get previewUrl(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.preview_url}get coverUrl$(){return this.cachedObservable("coverUrl$",()=>this._state$.pipe(ue(),et(1),E(t=>t.cover_url),S(this.destroyed$)))}get coverUrl(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.cover_url}get resourceId$(){return this.cachedObservable("resourceId$",()=>this._state$.pipe(ue(),et(1),E(t=>t.resource_id),S(this.destroyed$)))}get resourceId(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.resource_id}get type$(){return this.cachedObservable("type$",()=>this._state$.pipe(ue(),et(1),E(t=>t.type),S(this.destroyed$)))}get type(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.type}get channels$(){return this.cachedObservable("channels$",()=>this._state$.pipe(ue(),et(1),E(t=>t.channels),S(this.destroyed$)))}get channels(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.channels}get locked(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.locked}get locked$(){return this.cachedObservable("locked$",()=>this._state$.pipe(ue(),et(1),E(t=>t.locked),S(this.destroyed$)))}async sendText(t){return this.conversationManager.sendText(t,this.id)}get textMessage(){if(this._conversationMessages)return this._textMessages$=this._textMessages$??new uc(this._conversationMessages,t=>t.subtype==="chat",t=>({id:t.id,text:t.text,created:t.ts,fromAddress$:this.addressProvider.get$(t.from_fabric_address_id)})),this._textMessages$}get history(){if(this._conversationMessages)return this._history$=this._history$??new uc(this._conversationMessages,t=>t.subtype==="log",t=>({id:t.id,kind:t.kind,status:t.details.status,started:t.details.start_time,ended:t.details.end_time,fromAddress$:this.addressProvider.get$(t.from_fabric_address_id)})),this._history$}get activity$(){throw new vt}get activity(){throw new vt}};const ci=W(),_f=t=>{if(ci.debug("isRPCConnectResult check:",t),!t||typeof t!="object")return!1;const e=t,i=typeof e.identity=="string"&&typeof e.protocol=="string"&&typeof e.authorization=="object"&&typeof e.authorization.jti=="string"&&typeof e.authorization.project_id=="string"&&typeof e.authorization.fabric_subscriber=="object";return ci.debug("isRPCConnectResult check result:",i),i};var Lf=(gi=class{constructor(e,i,r){this.id=Jr(),ci.debug(`[PendingRPC(${this.id}) request:${e.id}: method:${e.method}] Creating PendingRPC`),this.request=e;const n=(r==null?void 0:r.timeoutMs)??gi.defaultTimeoutMs,s=r==null?void 0:r.signal;this.promise=new Promise((a,o)=>{if(s!=null&&s.aborted){o(new DOMException("The operation was aborted","AbortError"));return}let c=!1;const u=cr(i.pipe(B(h=>h.id===e.id),Me(1)),new be(h=>{const p=setTimeout(()=>{h.error(new Yr(e.id,n))},n);return()=>clearTimeout(p)}),s?new be(h=>{const p=()=>{h.error(new DOMException("The operation was aborted","AbortError"))};return s.addEventListener("abort",p),()=>s.removeEventListener("abort",p)}):fd).subscribe({next:h=>{if(c=!0,h.error){const p=new Lt(h.error.code,h.error.message,h.error.data,void 0,e.id);ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Rejecting promise with RPC error:`,p),o(p)}else ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Resolving promise with response:`,h),a(h);u.unsubscribe()},error:h=>{ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Rejecting promise with error:`,h),c=!0,o(h),u.unsubscribe()},complete:()=>{ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Observable completed`),c||o(new Yr(e.id,n)),u.unsubscribe()}})})}async then(e,i){return this.promise.then(e,i)}async catch(e){return this.promise.catch(e)}async finally(e){return this.promise.finally(e)}},gi.defaultTimeoutMs=5e3,gi);const X=W(),Ff=t=>{var n,s;const e=(n=t.to)==null?void 0:n.split("?")[0],i=(s=t.from)!=null&&s.startsWith("subscriber://")?t.from.replace("subscriber://",""):t.from,r=e??i;if(!r)throw new Ii("Error building Address name");return r};var Uf=class extends ie{constructor(t,e,i,r,n,s,a,o,c){super(),this.getCredential=t,this.transport=e,this.storage=i,this.authorizationStateKey=r,this.attachManager=s,this.dpopManager=o,this.callCreateTimeout=6e3,this.agent="signalwire-js/4.0.0",this.eventAcks=!0,this.authorizationState$=this.createReplaySubject(1),this.connectVersion={major:4,minor:0,revision:0},this._authorization$=this.createBehaviorSubject(void 0),this._errors$=this.createReplaySubject(1),this._authState$=this.createBehaviorSubject({kind:"unauthenticated"}),this._wasClientBound=!1,this._userInfo$=this.createBehaviorSubject(null),this._calls$=this.createBehaviorSubject({}),this._iceServers$=this.createBehaviorSubject([]),s.setSession(this),this.callFactory=new Of(this,n,s,a,c),this.initialized$=ei(()=>Ie(this.init())).pipe(et(1),S(this.destroyed$))}get incomingCalls$(){return this.cachedObservable("incomingCalls$",()=>this.calls$.pipe(E(t=>t.filter(e=>e.direction==="inbound"))))}get incomingCalls(){return Object.values(this._calls$.value).filter(t=>t.direction==="inbound")}get userInfo$(){return this._userInfo$.asObservable()}get userInfo(){return this._userInfo$.value}get calls$(){return this.cachedObservable("calls$",()=>this._calls$.pipe(E(t=>Object.values(t))))}get calls(){return Object.values(this._calls$.value)}get iceServers(){return this._iceServers$.value}get authorization$(){return this._authorization$.asObservable()}get authorization(){return this._authorization$.value}get errors$(){return this._errors$.asObservable()}get authenticated$(){return this._authState$.pipe(E(t=>t.kind==="authenticated"),L())}get authenticated(){return this._authState$.value.kind==="authenticated"}get clientBound(){return this._wasClientBound}get authState(){return this._authState$.value}setDirectory(t){this._directory=t}async execute(t,e){try{return await this.transport.execute(t,e)}catch(i){throw X.debug("[Session] Execute Error",i),this._errors$.next(i instanceof Error?i:new Error(String(i),{cause:i})),i}}send(t){this.transport.send(t)}async init(){return await this.loadAuthorizationStateFromStorage(),this.setupMessageHandlers(),!0}setupMessageHandlers(){X.debug("[Session] Setting up message handlers"),this.subscribeTo(this.authStateEvent$,async t=>{X.debug("[Session] Authorization state event received:",t);try{await this.updateAuthorizationStateInStorage(t.authorization_state)}catch(e){X.error("[Session] Failed to handle authorization state update:",e),this._errors$.next(new Kn(e))}}),this.subscribeTo(this.transport.connectionStatus$.pipe(B(t=>t==="disconnected"||t==="reconnecting")),()=>{this._authState$.value.kind==="authenticated"&&this._authState$.next({kind:"unauthenticated"})}),this.subscribeTo(this.transport.connectionStatus$.pipe(B(t=>t==="connected"),Xn(()=>(X.debug("[Session] Connection established, initiating authentication"),Ie(this.authenticate()).pipe(ti(t=>(this.handleAuthenticationError(t).catch(e=>{X.error("[Session] Error handling authentication failure:",e)}),$t)))))),void 0),this.subscribeTo(this.vertoInvite$,async t=>{X.debug("[Session] Verto invite received:",t);try{await this.createInboundCall(t)}catch(e){X.error("[Session] Error handling Verto invite:",e),this._errors$.next(new Bd(e))}}),this.subscribeTo(this.vertoAttach$,async t=>{X.debug("[Session] Verto attach received:",t);try{await this.handleVertoAttach(t)}catch(e){X.error("[Session] Error handling Verto attach:",e),this._errors$.next(new zd(e))}})}async loadAuthorizationStateFromStorage(){try{const t=await this.storage.getItem(this.authorizationStateKey);this.authorizationState$.next(t??void 0)}catch(t){X.error("Failed to retrieve authorization state from storage:",t),this.authorizationState$.next(void 0)}}async updateAuthorizationStateInStorage(t){if(!t){X.debug("[Session] Removing authorization state from storage");try{await this.storage.removeItem(this.authorizationStateKey),this.authorizationState$.next(void 0)}catch(e){throw X.error("Failed to remove authorization state from storage:",e),e}return}try{X.debug("[Session] Updating authorization state in storage"),await this.storage.setItem(this.authorizationStateKey,t),this.authorizationState$.next(t)}catch(e){throw X.error("Failed to retrieve authorization state from storage:",e),e}}get authStateEvent$(){return this.cachedObservable("authStateEvent$",()=>this.signalingEvent$.pipe(Se(t=>{X.debug("[Session] Received incoming message:",t)}),fe(ug,"params"),Se(t=>{X.debug("[Session] Authorization state event received:",t.authorization_state)})))}get signalingEvent$(){return this.cachedObservable("signalingEvent$",()=>this.transport.incomingEvent$.pipe(fe(as,"params"),kt()))}get vertoInvite$(){return this.cachedObservable("vertoInvite$",()=>this.signalingEvent$.pipe(B(sn),B(t=>sc(t.params)),E(t=>({node_id:t.node_id,...t.params.params}))))}get vertoAttach$(){return this.cachedObservable("vertoAttach$",()=>this.signalingEvent$.pipe(B(sn),B(t=>ac(t.params)),E(t=>({node_id:t.node_id,...t.params.params}))))}get contexts(){return[]}get eventing(){return[]}get topics(){return[]}get authentication(){const t=this.getCredential();if(!t.token)throw new Q("Credential token is undefined");return{jwt_token:t.token}}async connect(){await Ge(this.initialized$),await this.transport.connect(),await Ge(this.authenticated$.pipe(S(this.destroyed$),B(Boolean),Me(1),or({first:15e3})))}async handleAuthenticationError(t){X.error("Authentication error:",t);const e=t instanceof Lt&&(t.code===yo||t.code===wo||t.code===So),i=await Ge(this.authorizationState$.pipe(Me(1)))!==void 0;if(e&&i){X.debug("[Session] Recoverable auth error — cleaning up stored state and reconnecting fresh");try{await this.cleanupStoredConnectionParams()}catch(r){X.error("Failed to cleanup stored connection params:",r)}finally{this.transport.reconnect()}}else this._errors$.next(t)}async cleanupStoredConnectionParams(){await this.transport.setProtocol(void 0),await this.updateAuthorizationStateInStorage(void 0),this._authorization$.next(void 0)}async updateAuthState(t){try{await this.storage.setItem(this.authorizationStateKey,t)}catch(e){X.error("Failed to update authorization state in storage:",e),this._errors$.next(new Kn(e))}}async reauthenticate(t,e,i){var r,n;X.debug("[Session] Re-authenticating session");try{let s=e;if(!s&&((r=this.dpopManager)!=null&&r.initialized))try{s=await this.dpopManager.createRpcProof({method:"signalwire.reauthenticate"})}catch(o){if(this.clientBound)throw o;X.warn("[Session] Failed to create DPoP proof for reauthenticate:",o)}const a=jp({project:((n=this._authorization$.value)==null?void 0:n.project_id)??"",jwt_token:t,...s?{dpop_token:s}:{}});await Ba(Ie(this.transport.execute(a)).pipe(uo(),Me(1),ti(o=>{throw X.error("[Session] Re-authentication RPC failed:",o),o}))),i!=null&&i.clientBound&&(this._wasClientBound=!0),X.debug("[Session] Re-authentication successful, updating stored auth state")}catch(s){throw X.error("[Session] Re-authentication failed:",s),this._errors$.next(new Kn(s)),s}}async authenticate(){var o,c;X.debug("[Session] Starting authentication process");const t=await Ge(Ha({protocol:this.transport.protocol$,authorization_state:this.authorizationState$}).pipe(Me(1)));X.debug(`[Session] Persisted params:
72
+ `,(e=this.peerConnection.localDescription)==null?void 0:e.sdp)}handleICECandidateTimeout(){this.iceCandidateTimer&&this.removeTimer("iceCandidateTimer"),Ne.warn("[ICEGatheringController] ICE candidate timeout");const t=this.hasValidLocalDescriptionSDP;!t&&!this.relayOnly?this.restartICEGatheringWithRelayOnly():(Ne.debug("[ICEGatheringController] Using current SDP due to ICE candidate timeout"),this._iceCandidatesState.next({state:"timeout",validSDP:t}),this.stopGathering())}restartICEGatheringWithRelayOnly(){Ne.debug("[ICEGatheringController] Restarting ICE gathering with relay-only candidates"),this.relayOnly=!0,this.peerConnection.setConfiguration({...this.peerConnection.getConfiguration(),iceTransportPolicy:"relay"}),this.peerConnection.restartIce()}removeTimer(t){this[t]&&(clearTimeout(this[t]),this[t]=void 0)}clearAllTimers(){Ne.debug("[ICEGatheringController] Clearing all timers"),this.removeTimer("iceGatheringTimer"),this.removeTimer("iceCandidateTimer")}removeEventListeners(){this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.removeEventListener("icecandidate",this.onicecandidateHandler)}destroy(){Ne.debug("[ICEGatheringController] Destroying ICEGatheringController"),this.clearAllTimers(),this.removeEventListeners(),super.destroy()}};const cs=W();var Dg=class extends ie{constructor(t={}){super(),this._inputSource=null,this._inputStream=null,this._lastSpokeAt=0,this._gain$=this.createBehaviorSubject(1),this._pttMultiplier=1,this._audioContext=(t.audioContextFactory??(()=>new AudioContext))(),this._gainNode=this._audioContext.createGain(),this._analyser=this._audioContext.createAnalyser(),this._analyser.fftSize=2048,this._analyser.smoothingTimeConstant=.3,this._analyserBuffer=new Uint8Array(new ArrayBuffer(this._analyser.fftSize)),this._destination=this._audioContext.createMediaStreamDestination(),this._gainNode.connect(this._analyser),this._analyser.connect(this._destination),this._speakingThreshold=t.speakingThreshold??Yh,this._speakingHoldMs=t.speakingHoldMs??Kh,this._pollIntervalMs=t.pollIntervalMs??Co;const e=t.initialGain??1;this._gain$.next(e),this.applyEffectiveGain()}get gain$(){return this._gain$.asObservable()}get gain(){return this._gain$.value}get outputTrack(){const[t]=this._destination.stream.getAudioTracks();return t}get level$(){return this.deferEmission(Wr(this._pollIntervalMs,Ta).pipe(E(()=>this.computeLevel())))}get speaking$(){return this.deferEmission(this.level$.pipe(E(t=>this.evaluateSpeaking(t)),L()))}setGain(t){const e=Math.max(0,Math.min(2,t));this._gain$.next(e),this.applyEffectiveGain()}setPTTActive(t){this._pttMultiplier=t?1:0,this.applyEffectiveGain()}applyEffectiveGain(){this._gainNode.gain.value=this._gain$.value*this._pttMultiplier}setInputTrack(t){if(this._inputSource){try{this._inputSource.disconnect()}catch(e){cs.debug("[LocalAudioPipeline] input disconnect warning:",e)}this._inputSource=null}this._inputStream&&(this._inputStream=null),t&&(this._inputStream=new MediaStream([t]),this._inputSource=this._audioContext.createMediaStreamSource(this._inputStream),this._inputSource.connect(this._gainNode),this._audioContext.state==="suspended"&&this._audioContext.resume().catch(e=>{cs.warn("[LocalAudioPipeline] AudioContext resume failed:",e)}))}destroy(){if(this._inputSource){try{this._inputSource.disconnect()}catch{}this._inputSource=null}try{this._gainNode.disconnect(),this._analyser.disconnect()}catch{}this._audioContext.close().catch(t=>{cs.debug("[LocalAudioPipeline] audio context close warning:",t)}),super.destroy()}computeLevel(){if(!this._inputSource)return 0;this._analyser.getByteTimeDomainData(this._analyserBuffer);let t=0;for(const e of this._analyserBuffer){const i=(e-128)/128;t+=i*i}return Math.sqrt(t/this._analyserBuffer.length)}evaluateSpeaking(t){const e=Date.now();return t>=this._speakingThreshold?(this._lastSpokeAt=e,!0):e-this._lastSpokeAt<this._speakingHoldMs}};const Ft=W();var Ig=class extends ie{constructor(t){super(),this.options=t,this.mediaTrackEndedHandler=e=>{this._mediaTrackEnded$.next(e)},this._localStream$=this.createBehaviorSubject(null),this._localAudioTracks$=this.createBehaviorSubject([]),this._localVideoTracks$=this.createBehaviorSubject([]),this._mediaTrackEnded$=this.createSubject()}get localStream$(){return this._localStream$.asObservable().pipe(S(this.destroyed$))}get localAudioTracks$(){return this._localAudioTracks$.asObservable().pipe(S(this.destroyed$))}get localVideoTracks$(){return this._localVideoTracks$.asObservable().pipe(S(this.destroyed$))}get mediaTrackEnded$(){return this._mediaTrackEnded$.asObservable().pipe(S(this.destroyed$))}get localStream(){return this._localStream$.value}get localAudioTracks(){return this._localAudioTracks$.value}get localVideoTracks(){return this._localVideoTracks$.value}async buildLocalStream(){var e,i;Ft.debug("[LocalStreamController] Building local media stream.");let t;if(this.options.inputAudioStream??this.options.inputVideoStream){const r=[...((e=this.options.inputAudioStream)==null?void 0:e.getTracks())??[],...((i=this.options.inputVideoStream)==null?void 0:i.getTracks())??[]];t=new MediaStream(r)}else if(this.options.propose==="screenshare")Ft.debug("[LocalStreamController] Requesting display media for screen sharing with audio:",!!this.options.inputAudioDeviceConstraints),t=await this.options.getDisplayMedia({video:!0,audio:!!this.options.inputAudioDeviceConstraints}),Ft.debug("[LocalStreamController] Screen share media obtained:",t);else{const r={audio:this.options.inputAudioDeviceConstraints,video:this.options.inputVideoDeviceConstraints};Ft.debug("[LocalStreamController] Requesting user media with constraints:",r),t=await this.options.getUserMedia(r),Ft.debug("[LocalStreamController] User media obtained:",t)}return this._localStream$.next(t),this._localAudioTracks$.next(t.getAudioTracks()),this._localVideoTracks$.next(t.getVideoTracks()),t}addTrack(t){const e=this._localStream$.value??new MediaStream;return t.addEventListener("ended",this.mediaTrackEndedHandler),e.addTrack(t),this._localStream$.next(e),t.kind==="video"?this._localVideoTracks$.next(e.getVideoTracks()):this._localAudioTracks$.next(e.getAudioTracks()),Ft.debug(`[LocalStreamController] ${t.kind} track added:`,t.id),e}removeTrack(t){const e=this._localStream$.value,i=e==null?void 0:e.getTracks().find(r=>r.id===t);if(!i){Ft.debug(`[LocalStreamController] track not found: ${t}`);return}return i.removeEventListener("ended",this.mediaTrackEndedHandler),e==null||e.removeTrack(i),i.stop(),this._localStream$.next(e),i.kind==="video"?this._localVideoTracks$.next((e==null?void 0:e.getVideoTracks())??[]):this._localAudioTracks$.next((e==null?void 0:e.getAudioTracks())??[]),Ft.debug(`[LocalStreamController] ${i.kind} track removed:`,t),i}getOrCreateLocalStream(){return this._localStream$.value??new MediaStream}setLocalStream(t){this._localStream$.next(t)}addTrackEndedListener(t){t.addEventListener("ended",this.mediaTrackEndedHandler)}updateOptions(t){this.options={...this.options,...t}}stopAllTracks(){var t;(t=this._localStream$.value)==null||t.getTracks().forEach(e=>{Ft.debug(`[LocalStreamController] Stopping local track: ${e.kind}`),e.removeEventListener("ended",this.mediaTrackEndedHandler),e.stop()})}destroy(){this.stopAllTracks(),super.destroy()}};const Be=W(),rc=(t,e)=>t&&e?"sendrecv":t&&!e?"sendonly":!t&&e?"recvonly":"inactive";var Mg=class extends ie{constructor(t){super(),this.peerConnection=t.peerConnection,this.options=t}get useAddTransceivers(){return typeof this.peerConnection.addTransceiver=="function"}get useAddTrack(){return typeof this.peerConnection.addTrack=="function"}get useAddStream(){return typeof this.peerConnection.addStream=="function"&&!this.useAddTransceivers&&!this.useAddTrack}get propose(){return this.options.propose}get isAdditionalDevice(){return this.propose==="additional-device"}get isScreenShare(){return this.propose==="screenshare"}get isSimulcast(){return!!this.options.simulcast}get isSFU(){return!!this.options.sfu}get receiveVideo(){return!!this.options.receiveVideo}get receiveAudio(){return!!this.options.receiveAudio}get localStream(){return this.options.localStreamController.localStream}get inputAudioDeviceConstraints(){return this.options.getInputAudioDeviceConstraints()}get inputVideoDeviceConstraints(){return this.options.getInputVideoDeviceConstraints()}get audioDirection(){if(this.isAdditionalDevice)return"sendonly";const{localStream:t}=this,e=t==null?void 0:t.getAudioTracks().some(n=>n.enabled),i=!!this.inputAudioDeviceConstraints,r=!!this.receiveAudio;return rc(e||i,r)}get videoDirection(){if(this.isAdditionalDevice||this.isScreenShare)return"sendonly";if(this.isSFU)return"recvonly";const{localStream:t}=this,e=t==null?void 0:t.getVideoTracks().some(n=>n.enabled),i=!!this.inputVideoDeviceConstraints,r=!!this.receiveVideo;return rc(e||i,r)}get sendEncodings(){if(this.isSimulcast)return["0","1","2"].map(t=>({active:!0,rid:t,scaleResolutionDownBy:Number(t)*6||1}))}getConstraintsFor(t){const e=t==="audio"?this.inputAudioDeviceConstraints:this.inputVideoDeviceConstraints;return typeof e=="boolean"?{}:e}transceiverByKind(t){return this.peerConnection.getTransceivers().filter(e=>t==="both"||e.receiver.track.kind===t)}get audioTransceivers(){return this.transceiverByKind("audio")}get videoTransceivers(){return this.transceiverByKind("video")}async setupTransceiverSender(t,e,i){var a;const r=t.kind==="audio",n=r?this.audioDirection:this.videoDirection,s={direction:n,sendEncodings:r?void 0:this.sendEncodings,streams:n==="recvonly"?void 0:[e]};Be.debug(`[TransceiverController] Setting up transceiver sender for local ${t.kind} track:`,{transceiver:i,transceiverParams:s}),s.direction&&["sendonly","sendrecv"].includes(s.direction)&&(i?(await i.sender.replaceTrack(t),i.direction=s.direction,(a=s.streams)!=null&&a.some(o=>!!o)&&(Be.debug(`[TransceiverController] Setting streams for transceiver sender for local ${t.kind} track:`,s.streams),i.sender.setStreams(...s.streams))):(Be.debug(`[TransceiverController] Adding new transceiver for local ${t.kind} track:`,t.id),this.peerConnection.addTransceiver(t,s)))}stopTrackSender(t,e={updateTransceiverDirection:!1}){var i,r,n;try{const s=this.transceiverByKind(t);for(const a of s)if(((i=a.sender.track)==null?void 0:i.readyState)==="live"){const o=a.sender.track.id;a.sender.track.stop(),this.options.localStreamController.removeTrack(o),e.updateTransceiverDirection&&(a.direction="inactive")}}catch(s){Be.error("[TransceiverController] stopTrackSender error",t,s),(n=(r=this.options).onError)==null||n.call(r,new Mi("stopTrackSender",t,s))}}async restoreTrackSender(t){var e,i;try{Be.debug("[TransceiverController] restoreTrackSender called",t);const r={},n=this.transceiverByKind(t);for(const a of n){const{track:o}=a.sender;if(!o||o.readyState==="ended"){const c=(o==null?void 0:o.kind)??a.receiver.track.kind;(c==="audio"||c==="video")&&(r[c]=this.getConstraintsFor(c))}}if(Be.debug("[TransceiverController] restoreTrackSender constraints:",r),Object.keys(r).length===0){Be.warn("[TransceiverController] restoreTrackSender: no tracks need restoration",t);return}const s=(await this.options.getUserMedia(r)).getTracks();Be.debug("[TransceiverController] restoreTrackSender new tracks:",s);for(const a of s){this.options.localStreamController.addTrack(a);const o=a.kind,c=this.transceiverByKind(o)[0];c.direction=o==="audio"?this.audioDirection:this.videoDirection,Be.debug("[TransceiverController] restoreTrackSender setting direction for",o,c.direction),await c.sender.replaceTrack(a)}}catch(r){Be.error("[TransceiverController] restoreTrackSender error",t,r),(i=(e=this.options).onError)==null||i.call(e,new Mi("restoreTrackSender",t,r))}}async replaceSenderTrack(t,e){const i=t==="audio"?this.audioTransceivers:this.videoTransceivers;for(const r of i)await r.sender.replaceTrack(e)}async setupRemoteTransceivers(t){if(t!=="answer"){for(const e of["audio","video"]){const i=e==="audio"?this.audioTransceivers:this.videoTransceivers;for(const r of i){const n=e==="audio"?this.audioDirection:this.videoDirection;["inactive","recvonly"].includes(n)&&(r.direction=n,await r.sender.replaceTrack(null),r.sender.setStreams())}}if(this.videoDirection==="recvonly"&&this.isSFU&&this.useAddTransceivers){const{msStreamsNumber:e=5}=this.options;for(let i=0;i<Number(e);i++)this.peerConnection.addTransceiver("video",{direction:"recvonly"})}}}async updateSendersConstraints(t,e){var r,n;if(!e)return this.stopTrackSender(t),Promise.resolve();const i=this.peerConnection.getSenders().filter(s=>{var a;return((a=s.track)==null?void 0:a.kind)===t&&s.track.readyState==="live"});for(const s of i){const{track:a}=s;if(a){const o={...a.getConstraints(),...e};try{await a.applyConstraints(o),Be.debug(`[TransceiverController] Updated ${t} sender constraints:`,o),Be.debug(`[TransceiverController] Updated ${t} sender constraints:`,a.getConstraints())}catch(c){Be.warn(`[TransceiverController] applyConstraints failed for ${t} track ${a.id}, attempting track replacement fallback:`,c);try{await this.replaceTrackFallback(s,a,t,o)}catch(u){Be.warn(`[TransceiverController] Track replacement fallback also failed for ${t} track:`,u),(n=(r=this.options).onError)==null||n.call(r,new Mi("updateSendersConstraints",t,u))}}}}}async replaceTrackFallback(t,e,i,r){const{deviceId:n}=e.getSettings(),s={...r,...n?{deviceId:{exact:n}}:{}},a=e.id;e.stop(),this.options.localStreamController.removeTrack(a);const o=(await this.options.getUserMedia({[i]:s})).getTracks().find(c=>c.kind===i);if(!o)throw new Mi("replaceTrackFallback",i,new Error("getUserMedia returned no track of the requested kind"));await t.replaceTrack(o),this.options.localStreamController.addTrack(o),Be.debug(`[TransceiverController] Track replacement fallback succeeded for ${i}. New track: ${o.id}`)}getMediaDirections(){return this.peerConnection.connectionState==="connected"?this.peerConnection.getTransceivers().reduce((t,e)=>({...t,[e.receiver.track.kind]:e.direction}),{audio:"inactive",video:"inactive"}):{audio:this.audioDirection,video:this.videoDirection}}updatePeerConnection(t){this.peerConnection=t}updateOptions(t){this.options={...this.options,...t}}};const z=W();var nc=class extends ie{constructor(t={},e,i){super(),this.options=t,this.firstSDPExchangeCompleted=!1,this.negotiationNeeded$=this.createSubject(),this.localDescription$=ei(()=>Ie(this.init())).pipe(Ke(()=>this.iceGatheringController.iceCandidatesState$.pipe(B(n=>!["new","gathering"].includes(n)),Se(()=>{this.negotiationEnded()}),B(()=>this.shouldEmitLocalDescription),E(()=>{var n;return(n=this.peerConnection)==null?void 0:n.localDescription}),ue(),Se(n=>{n.type==="answer"&&(this._type="offer")}))),et(1),S(this.destroyed$)),this.connectionTimeout=3e3,this.oniceconnectionstatechangeHandler=()=>{if(this.peerConnection){const{iceConnectionState:n}=this.peerConnection;z.debug(`[RTCPeerConnectionController] ICE connection state changed to: ${n}`),this._iceConnectionState$.next(this.peerConnection.iceConnectionState)}},this.onconnectionstatechangeHandler=()=>{if(this.peerConnection){const{connectionState:n}=this.peerConnection;z.debug(`[RTCPeerConnectionController] Connection state changed to: ${n}`),n==="connected"&&this.removeConnectionTimer(),this._connectionState$.next(this.peerConnection.connectionState)}},this.onsignalingstatechangeHandler=()=>{var n;z.debug(`[RTCPeerConnectionController] Signaling state changed to: ${(n=this.peerConnection)==null?void 0:n.signalingState}`)},this.onicegatheringstatechangeHandler=()=>{this.peerConnection&&this._iceGatheringState$.next(this.peerConnection.iceGatheringState)},this.onnegotiationneededHandler=n=>{z.debug("[RTCPeerConnectionController] Negotiation needed event received.",n),this.negotiationNeeded$.next()},this.updateSelectedInputDevice=async(n,s)=>{var a,o,c;try{const{localStream:u}=this;if(!u){z.warn("[RTCPeerConnectionController] No local stream available to update input device.");return}z.debug(`[RTCPeerConnectionController] Updating selected ${n} input device:`,u.getTracks());const h=u.getTracks().find(p=>p.kind===n);if(h){if((a=this.transceiverController)==null||a.stopTrackSender(n),this.localStreamController.removeTrack(h.id),z.debug(`[RTCPeerConnectionController] Stopped existing ${n} track: ${h.id}`,u.getTracks()),!s){z.debug(`[RTCPeerConnectionController] ${n} input device selected: none`);return}const p=(await this.getUserMedia({[n]:{...h.getConstraints(),...this.deviceController.deviceInfoToConstraints(s)}})).getTracks().find(f=>f.kind===n);p&&(z.debug(`[RTCPeerConnectionController] Adding new ${n} track: ${p.id}`),this.localStreamController.addTrack(p),await((o=this.transceiverController)==null?void 0:o.replaceSenderTrack(n,p)),z.debug(`[RTCPeerConnectionController] Added new ${n} track: ${p.id}`,(c=this.localStream)==null?void 0:c.getTracks()))}z.debug(`[RTCPeerConnectionController] ${n} input device selected:`,s==null?void 0:s.label)}catch(u){throw z.error(`[RTCPeerConnectionController] Failed to select ${n} input device:`,u),this._errors$.next(bt(u)),u}},this._isNegotiating$=this.createBehaviorSubject(!1),this._memberId=null,this._iceConnectionState$=this.createReplaySubject(1),this._connectionState$=this.createReplaySubject(1),this._signalingState$=this.createReplaySubject(1),this._iceGatheringState$=this.createReplaySubject(1),this._errors$=this.createReplaySubject(1),this._iceCandidates$=this.createReplaySubject(1),this._initialized$=this.createReplaySubject(1),this._remoteDescription$=this.createReplaySubject(1),this._remoteStream$=this.createBehaviorSubject(null),this._remoteOfferMediaDirections=null,this._localAudioPipeline=null,this.deviceController=i??{},this.id=t.callId??Jr(),this._type=e?"answer":"offer",this.sdpInit=e?{type:"offer",sdp:e}:void 0,this._remoteOfferMediaDirections=e?wg(e):null;const r=this._remoteOfferMediaDirections?{audio:this._remoteOfferMediaDirections.audio.includes("recv"),video:this._remoteOfferMediaDirections.video.includes("recv"),receiveAudio:this._remoteOfferMediaDirections.audio.includes("send"),receiveVideo:this._remoteOfferMediaDirections.video.includes("send")}:{};this.options={...t,audio:t.audio??r.audio,video:t.video??r.video,receiveAudio:t.receiveAudio??r.receiveAudio??g.instance.receiveAudio,receiveVideo:t.receiveVideo??r.receiveVideo??g.instance.receiveVideo},this.localStreamController=new Ig({propose:this.propose,inputAudioStream:this.options.inputAudioStream,inputVideoStream:this.options.inputVideoStream,inputAudioDeviceConstraints:this.inputAudioDeviceConstraints,inputVideoDeviceConstraints:this.inputVideoDeviceConstraints,getUserMedia:async n=>this.getUserMedia(n),getDisplayMedia:async n=>this.getDisplayMedia(n)})}get iceGatheringController(){if(!this._iceGatheringController)throw new Q("ICEGatheringController is not initialized");return this._iceGatheringController}get shouldEmitLocalDescription(){if(!this.peerConnection)return!1;const{localDescription:t,signalingState:e}=this.peerConnection;return!t||!tc(t.sdp)?!1:t.type==="offer"&&e==="have-local-offer"||t.type==="answer"&&e==="stable"}removeConnectionTimer(){this.connectionTimer&&(clearTimeout(this.connectionTimer),this.connectionTimer=void 0)}setMemberId(t){this._memberId=t}get memberId(){return this._memberId}stopTrackSender(t,e={updateTransceiverDirection:!1}){var r,n,s;const i=t==="audio"||t==="both";i&&this._localAudioPipeline&&this.stopRawAudioInputForPipeline(),i?t==="both"?(n=this.transceiverController)==null||n.stopTrackSender("video",e):this._localAudioPipeline||(s=this.transceiverController)==null||s.stopTrackSender(t,e):(r=this.transceiverController)==null||r.stopTrackSender(t,e)}stopRawAudioInputForPipeline(){var e;const t=this.localStreamController.localAudioTracks;for(const i of t)i.readyState==="live"&&(i.stop(),this.localStreamController.removeTrack(i.id));(e=this._localAudioPipeline)==null||e.setInputTrack(null)}get isNegotiating$(){return this._isNegotiating$.asObservable()}get isNegotiating(){return this._isNegotiating$.value}updateMediaDevicesOptions(t){this.options={...this.options,...t}}get iceGatheringState$(){return this.cachedObservable("iceGatheringState$",()=>this._iceGatheringState$.asObservable().pipe(S(this.destroyed$)))}get mediaTrackEnded$(){return this.cachedObservable("mediaTrackEnded$",()=>this.localStreamController.mediaTrackEnded$.pipe(S(this.destroyed$)))}get errors$(){return this.cachedObservable("errors$",()=>this._errors$.asObservable().pipe(S(this.destroyed$)))}get iceCandidates$(){return this.cachedObservable("iceCandidates$",()=>this._iceCandidates$.asObservable().pipe(S(this.destroyed$)))}get initialized$(){return this.cachedObservable("initialized$",()=>this._initialized$.asObservable().pipe(B(t=>t),S(this.destroyed$)))}get remoteDescription$(){return this.cachedObservable("remoteDescription$",()=>this._remoteDescription$.asObservable().pipe(S(this.destroyed$)))}get localStream$(){return this.cachedObservable("localStream$",()=>this.localStreamController.localStream$.pipe(S(this.destroyed$)))}get remoteStream$(){return this.cachedObservable("remoteStream$",()=>this._remoteStream$.asObservable().pipe(S(this.destroyed$)))}get localAudioTracks$(){return this.cachedObservable("localAudioTracks$",()=>this.localStreamController.localAudioTracks$.pipe(S(this.destroyed$)))}get localVideoTracks$(){return this.cachedObservable("localVideoTracks$",()=>this.localStreamController.localVideoTracks$.pipe(S(this.destroyed$)))}get iceConnectionState$(){return this.cachedObservable("iceConnectionState$",()=>this._iceConnectionState$.asObservable().pipe(S(this.destroyed$)))}get connectionState$(){return this.cachedObservable("connectionState$",()=>this._connectionState$.asObservable().pipe(S(this.destroyed$)))}get signalingState$(){return this.cachedObservable("signalingState$",()=>this._signalingState$.asObservable().pipe(S(this.destroyed$)))}get type(){return this._type}get propose(){return this.options.propose??"main"}get isAdditionalDevice(){return this.propose==="additional-device"}get isMainDevice(){return this.propose==="main"}get isScreenShare(){return this.propose==="screenshare"}get iceServers(){if(!this.options.disableUdpIceServers)return this.options.iceServers??[];const t="transport=tcp";return(this.options.iceServers??[]).map(e=>{const i=Array.isArray(e.urls)?e.urls:[e.urls];return{...e,urls:i.filter(r=>r.includes(t))}})}get rtcConfiguration(){const{iceServers:t,...e}=this.options;return{bundlePolicy:"max-compat",iceCandidatePoolSize:10,iceServers:this.iceServers,iceTransportPolicy:this.options.relayOnly?"relay":"all",sdpSemantics:"unified-plan",...e}}get receiveVideo(){return!!this.options.receiveVideo}get receiveAudio(){return!!this.options.receiveAudio}get localStream(){return this.localStreamController.localStream}get remoteStream(){return this._remoteStream$.value}get inputAudioDeviceConstraints(){if(this.options.audio===!1&&!this.options.inputAudioDeviceConstraints)return!1;const t=this.deviceController.selectedAudioInputDeviceConstraints;return t===!1?!1:{...typeof this.options.inputAudioDeviceConstraints=="object"?this.options.inputAudioDeviceConstraints:{},...typeof t=="object"?t:{}}}get inputVideoDeviceConstraints(){if(!this.options.video&&!this.options.inputVideoDeviceConstraints)return!1;const t=this.deviceController.selectedVideoInputDeviceConstraints;return t===!1?!1:{...typeof this.options.inputVideoDeviceConstraints=="object"?this.options.inputVideoDeviceConstraints:{},...typeof t=="object"?t:{}}}get WebRTCPeerConnectionConstructor(){var t;return((t=this.options.webRTCApiProvider)==null?void 0:t.RTCPeerConnection)??RTCPeerConnection}get offerOptions(){const t={iceRestart:this.firstSDPExchangeCompleted?!0:void 0};switch(this.propose){case"screenshare":case"additional-device":return{...t,offerToReceiveAudio:!1,offerToReceiveVideo:!1};case"main":default:return{...t,offerToReceiveAudio:!0,offerToReceiveVideo:this.options.receiveVideo??!!this.inputVideoDeviceConstraints}}}get answerOptions(){return{iceRestart:this.firstSDPExchangeCompleted?!0:void 0}}async init(){return this.initPromise??(this.initPromise=this.doInit()),this.initPromise}async doInit(){try{this.setupPeerConnection(),this.subscribeTo(this.negotiationNeeded$.pipe(wd(0),Xn(async()=>this.startNegotiation())),{next:()=>{z.debug("[RTCPeerConnectionController] Start Negotiation completed successfully")},error:t=>{z.error("[RTCPeerConnectionController] Start Negotiation error:",t),this._errors$.next(bt(t))}}),this.subscribeTo(Di(this.deviceController.selectedAudioInputDevice$.pipe(E(t=>["audio",t])),this.deviceController.selectedVideoInputDevice$.pipe(E(t=>["video",t]))).pipe(Dd(()=>!this.localStreamController.localStream)),async([t,e])=>{z.debug("[RTCPeerConnectionController] Selected input device changed for:",{kind:t,deviceInfo:e}),await this.updateSelectedInputDevice(t,e)}),this.type==="answer"&&this.sdpInit?(await this.setupRemoteTracks(),this._initialized$.next(!0),this.setupEventListeners(),this._isNegotiating$.next(!0),await this._setRemoteDescription(this.sdpInit)):(await this.setupTrackHandling(),this._initialized$.next(!0))}catch(t){z.error("[RTCPeerConnectionController] Initialization error:",t),this._errors$.next(bt(t)),this.destroy()}}setupPeerConnection(){this.peerConnection=new this.WebRTCPeerConnectionConstructor(this.rtcConfiguration),this.peerConnection.addEventListener("negotiationneeded",this.onnegotiationneededHandler),this._iceGatheringController=new xg(this.peerConnection,this.isNegotiating$,{iceCandidateTimeout:this.options.iceCandidateTimeout,iceGatheringTimeout:this.options.iceGatheringTimeout,relayOnly:this.options.relayOnly}),this.transceiverController=new Mg({peerConnection:this.peerConnection,propose:this.propose,simulcast:this.options.simulcast,sfu:this.options.sfu,msStreamsNumber:this.options.msStreamsNumber,receiveAudio:this.receiveAudio,receiveVideo:this.receiveVideo,localStreamController:this.localStreamController,getInputAudioDeviceConstraints:()=>this.inputAudioDeviceConstraints,getInputVideoDeviceConstraints:()=>this.inputVideoDeviceConstraints,getUserMedia:async t=>this.getUserMedia(t),onError:t=>{this._errors$.next(t)}})}async startNegotiation(){if(this.isNegotiating){z.debug("[RTCPeerConnectionController] Negotiation already in progress, skipping.");return}if(this.setupEventListeners(),this.type==="answer"){z.debug("[RTCPeerConnectionController] This is an answer type still, skipping offer creation.");return}this._isNegotiating$.next(!0),z.debug("[RTCPeerConnectionController] Starting negotiation.");try{const{offerOptions:t}=this;z.debug("[RTCPeerConnectionController] Creating offer with options:",t),await this.createOffer(t)}catch(t){z.error("[RTCPeerConnectionController] Error during negotiation:",t),this._errors$.next(bt(t))}}async createOffer(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=await this.peerConnection.createOffer(t);await this.setLocalDescription(e)}async updateAnswerStatus({status:t,sdp:e}){let i=t!=="failed";try{t==="received"&&e&&(z.debug("[RTCPeerConnectionController] Received answer SDP:",e),await this._setRemoteDescription({type:"answer",sdp:e}))}catch(r){z.error("[RTCPeerConnectionController] Error updating answer status:",r),this._errors$.next(bt(r)),i=!1}finally{i?this.readyToConnect():this.iceGatheringController.restartICEGatheringWithRelayOnly()}}async updateOfferStatus({status:t,sdp:e}){switch(t){case"received":this._type="answer",this.sdpInit={type:"offer",sdp:e},await this.handleOfferReceived();break;case"failed":z.error("[RTCPeerConnectionController] Offer failed to be processed by remote.");break}}async acceptInbound(t){var i;if(t){const{audio:r,video:n,receiveAudio:s,receiveVideo:a}=t;this.options={...this.options,...r!==void 0?{audio:r}:{},...n!==void 0?{video:n}:{},...s!==void 0?{receiveAudio:s}:{},...a!==void 0?{receiveVideo:a}:{}},(i=this.transceiverController)==null||i.updateOptions({receiveAudio:this.receiveAudio,receiveVideo:this.receiveVideo}),this.localStreamController.updateOptions({inputAudioDeviceConstraints:this.inputAudioDeviceConstraints,inputVideoDeviceConstraints:this.inputVideoDeviceConstraints})}await this.setupLocalTracks();const{answerOptions:e}=this;z.debug("[RTCPeerConnectionController] Creating inbound answer with options:",e),await this.createAnswer(e)}async handleOfferReceived(){if(!this.sdpInit)throw new Q("SDP initialization parameters are not set");this._isNegotiating$.next(!0),await this._setRemoteDescription(this.sdpInit);const{answerOptions:t}=this;z.debug("[RTCPeerConnectionController] Creating answer with options:",t),await this.createAnswer(t)}readyToConnect(){this.firstSDPExchangeCompleted=!0,this.connectionTimer=setTimeout(()=>{var t;this.removeConnectionTimer(),((t=this.peerConnection)==null?void 0:t.connectionState)!=="connected"&&(z.debug("[RTCPeerConnectionController] Connection timeout, restarting ICE gathering with relay only."),this.iceGatheringController.restartICEGatheringWithRelayOnly())},this.connectionTimeout)}async setRemoteDescriptionBefore(t=""){return Promise.resolve(t)}async setLocalDescription(t){var i;const e=await this.setLocalDescriptionBefore(t.sdp);return(i=this.peerConnection)==null?void 0:i.setLocalDescription({...t,sdp:e})}async setLocalDescriptionBefore(t=""){let e=t;const i=this.options.preferredAudioCodecs??g.instance.preferredAudioCodecs,r=this.options.preferredVideoCodecs??g.instance.preferredVideoCodecs,n=this.options.stereo??g.instance.stereoAudio;return(i.length>0||r.length>0)&&(e=Ag(e,i,r),z.debug("[RTCPeerConnectionController] Applied codec preferences to SDP",{preferredAudioCodecs:i,preferredVideoCodecs:r})),n&&(e=Cg(e),z.debug("[RTCPeerConnectionController] Applied stereo Opus to SDP")),Promise.resolve(e)}async createAnswer(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=await this.peerConnection.createAnswer(t);await this.setLocalDescription(e)}setupEventListeners(){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");this._iceConnectionState$.next(this.peerConnection.iceConnectionState),this._connectionState$.next(this.peerConnection.connectionState),this._signalingState$.next(this.peerConnection.signalingState),this._iceGatheringState$.next(this.peerConnection.iceGatheringState),this._remoteDescription$.next(this.peerConnection.remoteDescription),this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.addEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.removeEventListener("iceconnectionstatechange",this.oniceconnectionstatechangeHandler),this.peerConnection.addEventListener("iceconnectionstatechange",this.oniceconnectionstatechangeHandler),this.peerConnection.removeEventListener("connectionstatechange",this.onconnectionstatechangeHandler),this.peerConnection.addEventListener("connectionstatechange",this.onconnectionstatechangeHandler),this.peerConnection.removeEventListener("signalingstatechange",this.onsignalingstatechangeHandler),this.peerConnection.addEventListener("signalingstatechange",this.onsignalingstatechangeHandler)}negotiationEnded(){this._isNegotiating$.next(!1)}async triggerIceRestart(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=t&&!this.options.relayOnly;if(e)try{this.peerConnection.setConfiguration({...this.peerConnection.getConfiguration(),iceTransportPolicy:"relay"}),z.debug("[RTCPeerConnectionController] ICE transport policy set to relay-only")}catch(i){z.warn("[RTCPeerConnectionController] Failed to set relay-only policy:",i)}this.setupEventListeners(),this._isNegotiating$.next(!0),z.debug(`[RTCPeerConnectionController] Triggering ICE restart${t?" (relay-only)":""}.`);try{const i=await this.peerConnection.createOffer({iceRestart:!0});await this.setLocalDescription(i)}catch(i){throw z.error("[RTCPeerConnectionController] ICE restart offer failed:",i),this._errors$.next(bt(i)),this.negotiationEnded(),e&&this.restoreIceTransportPolicy(),i}e&&Ge(cr(this._iceGatheringState$.pipe(B(i=>i==="complete"),Me(1)),xi(Gh).pipe(E(()=>"timeout")))).then(()=>this.restoreIceTransportPolicy()).catch(i=>{z.warn("[RTCPeerConnectionController] Error waiting for ICE gathering to complete:",i),this.restoreIceTransportPolicy()})}restoreIceTransportPolicy(){var t;try{(t=this.peerConnection)==null||t.setConfiguration({...this.peerConnection.getConfiguration(),iceTransportPolicy:this.options.relayOnly?"relay":"all"}),z.debug("[RTCPeerConnectionController] ICE transport policy restored")}catch(e){z.warn("[RTCPeerConnectionController] Failed to restore ICE transport policy:",e)}}async setupTrackHandling(){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");await this.setupLocalTracks(),await this.setupRemoteTracks()}async setupLocalTracks(){var e,i,r,n,s,a,o;z.debug("[RTCPeerConnectionController] Setting up local tracks/transceivers.");const t=this.localStream??await this.localStreamController.buildLocalStream();if(((e=this.transceiverController)==null?void 0:e.useAddStream)??!1){z.warn("[RTCPeerConnectionController] Using deprecated addStream API to add local stream."),(i=this.peerConnection)==null||i.addStream(t),this.isNegotiating||(z.debug("[RTCPeerConnectionController] Forcing negotiationneeded after local tracks setup."),this.negotiationNeeded$.next());return}for(const c of["audio","video"]){const u=(c==="audio"?t.getAudioTracks():t.getVideoTracks()).map((h,p)=>({index:p,track:h}));for(const{index:h,track:p}of u)if(this.localStreamController.addTrackEndedListener(p),((r=this.transceiverController)==null?void 0:r.useAddTransceivers)??!1){const f=(c==="audio"?(n=this.transceiverController)==null?void 0:n.audioTransceivers:(s=this.transceiverController)==null?void 0:s.videoTransceivers)??[];await((a=this.transceiverController)==null?void 0:a.setupTransceiverSender(p,t,f[h]))}else z.debug(`[RTCPeerConnectionController] Using addTrack for local ${c} track:`,p.id),(o=this.peerConnection)==null||o.addTrack(p,t)}}async getUserMedia(t){var e;return(((e=this.options.webRTCApiProvider)==null?void 0:e.mediaDevices)??navigator.mediaDevices).getUserMedia(t)}async getDisplayMedia(t){var i;const e=((i=this.options.webRTCApiProvider)==null?void 0:i.mediaDevices)??navigator.mediaDevices;if(!e.getDisplayMedia)throw new Q("getDisplayMedia is not supported by the current WebRTC provider");return e.getDisplayMedia(t)}async setupRemoteTracks(){var t;if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");this.peerConnection.ontrack=e=>{var i;if(z.debug("[RTCPeerConnectionController] Remote track received:",e.track.kind),e.streams[0])this._remoteStream$.next(e.streams[0]);else{const r=((i=this._remoteStream$.value)==null?void 0:i.getTracks())??[],n=new MediaStream([...r,e.track]);this._remoteStream$.next(n)}},await((t=this.transceiverController)==null?void 0:t.setupRemoteTransceivers(this.type))}async restoreTrackSender(t){var i,r,n;const e=t==="audio"||t==="both";e&&this._localAudioPipeline&&await this.restoreRawAudioInputForPipeline(),e?t==="both"?await((r=this.transceiverController)==null?void 0:r.restoreTrackSender("video")):this._localAudioPipeline||await((n=this.transceiverController)==null?void 0:n.restoreTrackSender(t)):await((i=this.transceiverController)==null?void 0:i.restoreTrackSender(t))}async restoreRawAudioInputForPipeline(){var r;if(!this._localAudioPipeline)return;const t=((r=this.transceiverController)==null?void 0:r.getConstraintsFor("audio"))??{};let e;try{e=await this.getUserMedia({audio:t})}catch(n){z.error("[RTCPeerConnectionController] Failed to re-acquire mic for pipeline restore:",n),this._errors$.next(bt(n));return}const i=e.getAudioTracks().at(0);i&&(this.localStreamController.addTrack(i),this._localAudioPipeline.setInputTrack(i))}ensureLocalAudioPipeline(){if(this._localAudioPipeline)return this._localAudioPipeline;if(!this.peerConnection)return null;try{this._localAudioPipeline=new Dg}catch(t){return z.warn("[RTCPeerConnectionController] Failed to create LocalAudioPipeline:",t),null}return this.subscribeTo(this.localStreamController.localAudioTracks$,()=>{this.applyLocalAudioPipelineToSender()}),this.applyLocalAudioPipelineToSender(),this._localAudioPipeline}get localAudioPipeline(){return this._localAudioPipeline}async applyLocalAudioPipelineToSender(){var i,r;if(!this._localAudioPipeline||!this.peerConnection)return;const t=this.localStreamController.localAudioTracks.at(0);this._localAudioPipeline.setInputTrack(t??null);const e=((r=(i=this.transceiverController)==null?void 0:i.audioTransceivers.at(0))==null?void 0:r.sender)??this.peerConnection.getSenders().find(n=>{var s;return((s=n.track)==null?void 0:s.kind)==="audio"});if(!(!e||!t))try{await e.replaceTrack(this._localAudioPipeline.outputTrack)}catch(n){z.warn("[RTCPeerConnectionController] Failed to route audio sender through pipeline:",n)}}addLocalTrack(t){if(!this.peerConnection){const e=new Q("RTCPeerConnection is not initialized");throw this._errors$.next(e),e}try{const e=this.localStreamController.addTrack(t);this.peerConnection.addTrack(t,e),z.debug(`[RTCPeerConnectionController] ${t.kind} track added:`,t.id)}catch(e){throw z.error(`[RTCPeerConnectionController] Failed to add ${t.kind} track:`,e),this._errors$.next(bt(e)),e}}removeLocalTrack(t){var i,r;if(!this.peerConnection){const n=new Q("RTCPeerConnection is not initialized");throw this._errors$.next(n),n}const e=this.peerConnection.getSenders().find(n=>{var s;return((s=n.track)==null?void 0:s.id)===t});if(!e){z.debug(`[RTCPeerConnectionController] track not found: ${t}`);return}try{this.peerConnection.removeTrack(e),this.localStreamController.removeTrack(t),z.debug(`[RTCPeerConnectionController] ${(i=e.track)==null?void 0:i.kind} track removed:`,t)}catch(n){throw z.error(`[RTCPeerConnectionController] Failed to remove ${(r=e.track)==null?void 0:r.kind} track:`,n),this._errors$.next(bt(n)),n}}setLocalTrack(t){const e=[...t.kind==="audio"?this.localStreamController.localAudioTracks:this.localStreamController.localVideoTracks];for(const i of e)this.removeLocalTrack(i.id);this.addLocalTrack(t)}async updateSendersConstraints(t,e){var i;await((i=this.transceiverController)==null?void 0:i.updateSendersConstraints(t,e))}async replaceAudioTrackWithConstraints(t){var i;const e=(i=this.peerConnection)==null?void 0:i.getSenders().filter(r=>{var n;return((n=r.track)==null?void 0:n.kind)==="audio"&&r.track.readyState==="live"});if(!e||e.length===0){z.warn("[RTCPeerConnectionController] No live audio sender to replace");return}for(const r of e){const n=r.track;if(!n)continue;const{deviceId:s}=n.getSettings(),a={...n.getConstraints(),...t,...s?{deviceId:{exact:s}}:{}},o=n.id;n.stop(),this.localStreamController.removeTrack(o);const c=(await this.getUserMedia({audio:a})).getAudioTracks()[0];await r.replaceTrack(c),this.localStreamController.addTrack(c),z.debug(`[RTCPeerConnectionController] Audio track replaced for server-pushed params. New track: ${c.id}`)}}destroy(){var t,e,i;z.debug(`[RTCPeerConnectionController] Destroying RTCPeerConnectionController. ${this.propose}`),this.removeConnectionTimer(),(t=this._iceGatheringController)==null||t.destroy(),(e=this._localAudioPipeline)==null||e.destroy(),this._localAudioPipeline=null,this.localStreamController.destroy(),(i=this.transceiverController)==null||i.destroy(),this.peerConnection&&(this.stopRemoteTracks(),this.removeAllListeners(),this.peerConnection.close(),this.peerConnection=void 0),super.destroy()}removeAllListeners(){this.peerConnection&&(this.peerConnection.removeEventListener("icegatheringstatechange",this.onicegatheringstatechangeHandler),this.peerConnection.removeEventListener("iceconnectionstatechange",this.oniceconnectionstatechangeHandler),this.peerConnection.removeEventListener("connectionstatechange",this.onconnectionstatechangeHandler),this.peerConnection.removeEventListener("signalingstatechange",this.onsignalingstatechangeHandler),this.peerConnection.removeEventListener("negotiationneeded",this.onnegotiationneededHandler))}stopRemoteTracks(){var t;(t=this._remoteStream$.value)==null||t.getTracks().forEach(e=>{z.debug(`[RTCPeerConnectionController] Stopping remote track: ${e.kind}`),e.stop()})}get mediaDirections(){var t;return((t=this.transceiverController)==null?void 0:t.getMediaDirections())??this._remoteOfferMediaDirections??{audio:"inactive",video:"inactive"}}async _setRemoteDescription(t){if(!this.peerConnection)throw new Q("RTCPeerConnection is not initialized");const e=await this.setRemoteDescriptionBefore(t.sdp),i={...t,sdp:e};return z.debug("[RTCPeerConnectionController] Setting remote description:",i),this.peerConnection.setRemoteDescription(i)}};function ls(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"id")}function sc(t){if(!ls(t))return!1;const e=t;return e.method==="verto.invite"&&ke(e.params)&&G(e.params,"sdp")&&G(e.params,"callID")}function us(t){return ls(t)?t.method==="verto.bye":!1}function ac(t){return ls(t)?t.method==="verto.attach":!1}function Rg(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.answer"&&ke(t.params)&&G(t.params,"callID")}function Pg(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.media"&&ke(t.params)&&G(t.params,"callID")&&G(t.params,"sdp")}function Og(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.mediaParams"&&ke(t.params)&&G(t.params,"mediaParams")}function _g(t){return ke(t)&&G(t,"jsonrpc")&&t.jsonrpc==="2.0"&&G(t,"method")&&t.method==="verto.ping"}const V=W();function Lg(t){return t.isInvite&&!t.reattach&&!t.explicitNodeId?"":t.currentNodeId??""}var Fg=class extends ie{constructor(t){super(),this.callSession=t}destroy(){this.callSession=void 0,super.destroy()}},Ug=class extends Fg{constructor(t,e,i,r,n={}){super(t),this.webRtcCallSession=t,this.attachManager=e,this.deviceController=i,this.webRTCApiProvider=r,this._rtcPeerConnections$=this.createBehaviorSubject([]),this._selfId$=this.createBehaviorSubject(null),this._signalingStatus$=this.createReplaySubject(1),this._screenShareStatus$=this.createBehaviorSubject("none"),this._rtcPeerConnectionsMap=new Map,this._screenShareTimeoutMs=5e4,this._nodeId$=this.createBehaviorSubject(n.nodeId??null),this.onError=n.onError,this.onModifyFailed=n.onModifyFailed,this.initSubscriptions(),this.initMainPeerConnection()}async hold(){const t=nn({sessid:this.webRtcCallSession.id,dialogParams:{callID:this.webRtcCallSession.id},action:"hold"});try{await this.executeVerto(t)}catch(e){throw V.warn("[WebRTCManager] Call might already be disconnected, error sending Verto hold:",e),e}}async unhold(){const t=nn({sessid:this.webRtcCallSession.id,dialogParams:{callID:this.webRtcCallSession.id},action:"unhold"});try{await this.executeVerto(t)}catch(e){throw V.warn("[WebRTCManager] Call might already be disconnected, error sending Verto unhold:",e),e}}get mediaDirections(){return this.mainPeerConnection.mediaDirections}get rtcPeerConnections$(){return this._rtcPeerConnections$.asObservable()}get rtcPeerConnections(){return this._rtcPeerConnections$.value}get nodeId$(){return this._nodeId$.asObservable()}get selfId$(){return this._selfId$.asObservable()}get localStream(){var t;return((t=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id))==null?void 0:t.localStream)??null}get remoteStream(){var t;return((t=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id))==null?void 0:t.remoteStream)??null}get nodeId(){return this._nodeId$.value}get screenShareStatus(){return this._screenShareStatus$.value}get screenShareStatus$(){return this._screenShareStatus$.asObservable()}get mainPeerConnection(){const t=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id);if(!t)throw new Q("Main peer connection not found");return t}get signalingStatus$(){return this.cachedObservable("signalingStatus$",()=>Di(this._signalingStatus$.asObservable(),this.mainPeerConnection.connectionState$.pipe(B(t=>["connected","disconnected","failed"].includes(t)))))}initSubscriptions(){this.subscribeTo(this.callJoinedEvent$,t=>{var i;const e=(i=t.room_session.members.find(r=>r.call_id===t.call_id))==null?void 0:i.node_id;e&&this.setNodeIdIfNull(e),t.member_id&&this.setSelfIdIfNull(t.member_id)}),this.subscribeTo(this.vertoMedia$,t=>{var r;V.debug("[WebRTCManager] Received Verto media event (early media SDP):",t);const{sdp:e,callID:i}=t;this.emitMainSignalingStatus(i,"ringing"),(r=this._rtcPeerConnectionsMap.get(i))==null||r.updateAnswerStatus({status:"received",sdp:e})}),this.subscribeTo(this.vertoAnswer$,t=>{var r;V.debug("[WebRTCManager] Received Verto answer event:",t);const{sdp:e,callID:i}=t;this.emitMainSignalingStatus(i,"connecting"),(r=this._rtcPeerConnectionsMap.get(i))==null||r.updateAnswerStatus({status:"received",sdp:e})}),this.subscribeTo(this.vertoMediaParams$,t=>{V.debug("[WebRTCManager] Received Verto mediaParams event:",t);const{mediaParams:e,callID:i}=t,r=this._rtcPeerConnectionsMap.get(i),{audio:n,video:s}=e;(async()=>{var a;try{n&&r&&await r.replaceAudioTrackWithConstraints(n),s&&await(r==null?void 0:r.updateSendersConstraints("video",s)),this.webRtcCallSession.emitMediaParamsUpdated({audio:n,video:s,timestamp:Date.now()})}catch(o){V.warn("[WebRTCManager] Error applying server-pushed media params:",o),(a=this.onError)==null||a.call(this,o instanceof Error?o:new Error(String(o),{cause:o}))}})()}),this.subscribeTo(this.vertoPing$,t=>{this.attachManager.attach(this.buildAttachableCall()),this.sendVertoPong(t)})}setNodeIdIfNull(t){!this._nodeId$.value&&t&&(V.debug(`[WebRTCManager] Early node_id set: ${t}`),this._nodeId$.next(t))}setSelfIdIfNull(t){!this._selfId$.value&&t&&(V.debug(`[WebRTCManager] Early selfId set: ${t}`),this._selfId$.next(t))}async sendVertoPong(t){var e;try{const i=$p({...t});await this.executeVerto(i)}catch(i){V.warn("[WebRTCManager] Call might disconnect, error sending Verto pong:",i),(e=this.onError)==null||e.call(this,new so(i))}}async updateMediaConstraints(t={}){var r;const{audio:e,video:i}=t;try{e&&await this.mainPeerConnection.updateSendersConstraints("audio",e),i&&await this.mainPeerConnection.updateSendersConstraints("video",i)}catch(n){throw V.warn("[WebRTCManager] Error updating media constraints:",n),(r=this.onError)==null||r.call(this,n instanceof Error?n:new Error(String(n),{cause:n})),n}}get selfId(){return this._selfId$.value}buildAttachableCall(t){return{nodeId:this.nodeId??void 0,id:t??this.webRtcCallSession.id,to:this.webRtcCallSession.to,mediaDirections:this.webRtcCallSession.mediaDirections}}requestKeyframe(){try{const t=this.mainPeerConnection.peerConnection;if(!t){V.warn("[WebRTCManager] No peer connection for keyframe request");return}const e=t.getReceivers().find(i=>i.track.kind==="video");if(!e){V.warn("[WebRTCManager] No video receiver for keyframe request");return}typeof e.requestKeyFrame=="function"?(e.requestKeyFrame(),V.debug("[WebRTCManager] Keyframe requested via RTCRtpReceiver.requestKeyFrame()")):V.debug("[WebRTCManager] requestKeyFrame() not supported, skipping")}catch(t){V.warn("[WebRTCManager] Keyframe request failed (non-fatal):",t)}}async requestIceRestart(t){try{const e=this.mainPeerConnection;if(!e.peerConnection){V.warn("[WebRTCManager] No peer connection for ICE restart");return}await e.triggerIceRestart(t),V.info(`[WebRTCManager] ICE restart initiated${t?" (relay-only)":""}`)}catch(e){throw V.error("[WebRTCManager] ICE restart failed:",e),e}}async requestIceRestartAll(t){const e=Array.from(this._rtcPeerConnectionsMap.entries());for(const[i,r]of e)try{if(!r.peerConnection){V.debug(`[WebRTCManager] No peer connection for leg ${i}, skipping ICE restart`);continue}await r.triggerIceRestart(t),V.info(`[WebRTCManager] ICE restart initiated for leg ${i}${t?" (relay-only)":""}`)}catch(n){V.warn(`[WebRTCManager] ICE restart failed for leg ${i}:`,n)}}requestKeyframeAll(){for(const[t,e]of this._rtcPeerConnectionsMap){if(e.isScreenShare){V.debug(`[WebRTCManager] Skipping keyframe for send-only screen share leg ${t}`);continue}try{const i=e.peerConnection;if(!i)continue;const r=i.getReceivers().find(n=>n.track.kind==="video");if(!r)continue;typeof r.requestKeyFrame=="function"&&(r.requestKeyFrame(),V.debug(`[WebRTCManager] Keyframe requested for leg ${t}`))}catch(i){V.warn(`[WebRTCManager] Keyframe request failed for leg ${t} (non-fatal):`,i)}}}get callJoinedEvent$(){return this.webRtcCallSession.callEvent$.pipe(B(ec),S(this.destroyed$))}get vertoMedia$(){return this.webRtcCallSession.webrtcMessages$.pipe(fe(Pg,"params"),S(this.destroyed$))}get vertoAnswer$(){return this.cachedObservable("vertoAnswer$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(Rg,"params"),S(this.destroyed$)))}get vertoMediaParams$(){return this.cachedObservable("vertoMediaParams$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(Og,"params"),S(this.destroyed$)))}get vertoBye$(){return this.cachedObservable("vertoBye$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(us,"params"),S(this.destroyed$)))}get vertoAttach$(){return this.cachedObservable("vertoAttach$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(ac,"params"),S(this.destroyed$)))}get vertoPing$(){return this.cachedObservable("vertoPing$",()=>this.webRtcCallSession.webrtcMessages$.pipe(fe(_g,"params"),S(this.destroyed$)))}async executeVerto(t,e={}){var s,a;const i=No({callID:e.callID??this.webRtcCallSession.id,node_id:e.node_id??this._nodeId$.value??"",message:t,subscribe:e.subscribe}),r=await this.webRtcCallSession.execute(i);if(r.error){const o=new Lt(r.error.code,r.error.message,r.error.data);return(s=this.onError)==null||s.call(this,o),r}const n=tt(r,"result.result");if(n!=null&&n.error){const o=new Lt(n.error.code,n.error.message,n.error.data);return(a=this.onError)==null||a.call(this,o),r}return r}async sendLocalDescription(t,e){var n,s;const i=t.method,r=this.getSendLocalSDPOptionalParams(e,t);try{const a=await this.executeVerto(t,r);switch(i){case"verto.invite":this.processInviteResponse(a,e);break;case"verto.modify":await this.processModifyResponse(a,e);break;default:}}catch(a){V.error(`[WebRTCManager] Error sending Verto ${i}:`,a),(n=this.onError)==null||n.call(this,a instanceof Error?a:new Error(String(a),{cause:a})),i==="verto.modify"&&((s=this.onModifyFailed)==null||s.call(this))}}async processModifyResponse(t,e){var i;if(!t.error){const r=tt(t,"result.result.result.action"),n=tt(t,"result.result.result.sdp");if(r==="updateMedia"&&n)try{await e.updateAnswerStatus({status:"received",sdp:n})}catch(s){V.warn("[WebRTCManager] Error processing modify response:",s);const a=s instanceof Error?s:new Error(String(s),{cause:s});(i=this.onError)==null||i.call(this,a)}}}emitMainSignalingStatus(t,e){var r;const i=this._rtcPeerConnectionsMap.get(t);if(!i){const n=new Q(`Cannot emit signaling status, RTCPeerConnectionController not found for callID: ${t}`);V.error("[WebRTCManager] Failed to emit signaling status:",{callId:t,status:e,signalingError:n}),(r=this.onError)==null||r.call(this,n);return}i.isMainDevice&&this._signalingStatus$.next(e)}processInviteResponse(t,e){var i;if(!t.error&&tt(t,"result.result.result.message")==="CALL CREATED"){this.emitMainSignalingStatus(e.id,"trying"),this._nodeId$.next(tt(t,"result.node_id")??null);const r=tt(t,"result.result.result.memberID")??null,n=tt(t,"result.result.result.callID")??null;V.debug("[WebRTCManager] Verto invite response:",{callId:n,memberId:r,response:t}),this._selfId$.next(r),e.setMemberId(r),n?(this.webRtcCallSession.addCallId(n),this.attachManager.attach(this.buildAttachableCall(n))):V.warn("[WebRTCManager] Cannot attach call, missing callId:",{nodeId:this.nodeId,callId:n}),V.info("[WebRTCManager] Verto invite successful"),V.debug(`[WebRTCManager] nodeid: ${this._nodeId$.value}, selfId: ${this._selfId$.value}`)}else{V.error("[WebRTCManager] Verto invite failed:",t);const r=t.error?new Lt(t.error.code,t.error.message,t.error.data):new Error("Verto invite failed: unexpected response");(i=this.onError)==null||i.call(this,r)}}get RTCPeerConnectionConfig(){return{iceServers:this.webRtcCallSession.clientSession.iceServers??g.instance.iceServers,relayOnly:g.instance.relayOnly||g.instance.disableUdpIceServers,disableUdpIceServers:g.instance.disableUdpIceServers,iceCandidateTimeout:g.instance.iceCandidateTimeout,iceGatheringTimeout:g.instance.iceGatheringTimeout}}initMainPeerConnection(){const{options:t}=this.webRtcCallSession,e=new nc({propose:"main",callId:this.webRtcCallSession.id,audio:t.audio,video:t.video,inputAudioDeviceConstraints:t.inputAudioDeviceConstraints,inputVideoDeviceConstraints:t.inputVideoDeviceConstraints,inputAudioStream:t.inputAudioStream,inputVideoStream:t.inputVideoStream,receiveAudio:t.receiveAudio,receiveVideo:t.receiveVideo,webRTCApiProvider:this.webRTCApiProvider,preferredVideoCodecs:t.preferredVideoCodecs,preferredAudioCodecs:t.preferredAudioCodecs,stereo:t.stereo,...this.RTCPeerConnectionConfig},t.initOffer,this.deviceController);this.setupLocalDescriptionHandler(e),this.setupVertoByeHandler(),this.setupVertoAttachHandler(),this.initObservables(e),this._rtcPeerConnectionsMap.set(e.id,e),this._rtcPeerConnections$.next(Array.from(this._rtcPeerConnectionsMap.values())),this.subscribeTo(e.errors$,i=>{var r;(r=this.onError)==null||r.call(this,i)}),t.initOffer&&this.handleInboundAnswer(e)}async handleInboundAnswer(t){var i,r,n;V.debug("[WebRTCManager] Waiting for inbound call to be accepted or rejected");const e=await Ge(cr(this.vertoBye$,this.webRtcCallSession.answered$).pipe(S(this.destroyed$))).catch(()=>null);if(e===null){V.debug("[WebRTCManager] Inbound answer handler aborted (destroyed).");return}if(us(e))V.info("[WebRTCManager] Inbound call ended by remote before answer."),(i=this.callSession)==null||i.destroy();else if(e){V.debug("[WebRTCManager] Inbound call accepted, creating SDP answer");const s=this.webRtcCallSession.answerMediaOptions;try{await t.acceptInbound(s)}catch(a){V.error("[WebRTCManager] Error creating inbound answer:",a),(n=this.onError)==null||n.call(this,a instanceof Error?a:new Error(String(a),{cause:a}))}}else{V.info("[WebRTCManager] Inbound call rejected by user.");try{await this.bye("USER_BUSY")}finally{this._signalingStatus$.next("disconnected"),(r=this.callSession)==null||r.destroy()}}}setupVertoAttachHandler(){this.subscribeTo(this.vertoAttach$,async t=>{V.debug("[WebRTCManager] Received Verto attach event for existing call:",t);const{callID:e}=t;await this.attachManager.attach({nodeId:this.nodeId??void 0,id:e,to:t.callee_id_number,mediaDirections:{audio:"sendrecv",video:"inactive"}})})}initObservables(t){this.mediaDirections$=t.connectionState$.pipe(B(e=>e==="connected"),E(()=>t.mediaDirections),Id(t.mediaDirections),S(this.destroyed$)),this.localStream$=t.localStream$.pipe(ue(),S(this.destroyed$)),this.remoteStream$=t.remoteStream$.pipe(ue(),S(this.destroyed$))}setupLocalDescriptionHandler(t){this.subscribeTo(t.localDescription$.pipe(B(e=>e!==null),S(this.destroyed$)),e=>{const{type:i,sdp:r}=e,n=this.dialogParams(t),s=!t.firstSDPExchangeCompleted;if(i==="answer"){const a=Qp({dialogParams:n,sdp:r});this.sendLocalDescriptionOnceAccepted(a,t)}else if(s){const a=Yp({dialogParams:n,sdp:r});this.sendLocalDescription(a,t)}else{const a=nn({dialogParams:n,sdp:r,action:"updateMedia"});this.sendLocalDescription(a,t)}})}setupVertoByeHandler(){this.subscribeTo(this.vertoBye$,()=>{var t;this._signalingStatus$.next("disconnected"),this.attachManager.detach(this.buildAttachableCall()),(t=this.callSession)==null||t.destroy()})}getSendLocalSDPOptionalParams(t,e){let i;return t.firstSDPExchangeCompleted||(i=[],t.isMainDevice?i.push(...g.instance.inviteSubscribeMainDevice):t.isAdditionalDevice?i.push(...g.instance.inviteSubscribeAdditionalDevice):t.isScreenShare&&i.push(...g.instance.inviteSubscribeScreenshare)),{callID:t.id,node_id:Lg({isInvite:sc(e),reattach:this.webRtcCallSession.options.reattach===!0,explicitNodeId:this.webRtcCallSession.options.nodeId,currentNodeId:this._nodeId$.value}),subscribe:i}}async sendLocalDescriptionOnceAccepted(t,e){var r,n,s;V.debug("[WebRTCManager] Waiting for call to be accepted or ended before sending answer");const i=await Ge(cr(this.vertoBye$,this.webRtcCallSession.answered$).pipe(S(this.destroyed$))).catch(()=>null);if(i===null){V.debug("[WebRTCManager] Destroyed while waiting for call acceptance");return}if(us(i))V.info("[WebRTCManager] Call ended before answer was sent."),(r=this.callSession)==null||r.destroy();else if(i){V.debug("[WebRTCManager] Call accepted, sending answer");try{this.emitMainSignalingStatus(e.id,"connecting"),await this.sendLocalDescription(t,e),await e.updateAnswerStatus({status:"sent"}),await this.attachManager.attach(this.buildAttachableCall())}catch(a){V.error("[WebRTCManager] Error sending Verto answer:",a),(s=this.onError)==null||s.call(this,a instanceof Error?a:new Error(String(a),{cause:a})),await e.updateAnswerStatus({status:"failed"})}}else{V.info("[WebRTCManager] Call was not accepted, sending verto.bye.");try{await this.bye("USER_BUSY")}finally{this._signalingStatus$.next("disconnected"),(n=this.callSession)==null||n.destroy()}}}dialogParams(t){const e=t.memberId??this._selfId$.value??void 0,i=t.propose==="main"&&!t.firstSDPExchangeCompleted&&this.webRtcCallSession.options.reattach;return{id:t.isMainDevice?this.webRtcCallSession.id:t.id,destinationNumber:this.webRtcCallSession.to??this.webRtcCallSession.from,attach:i,reattaching:i,callerName:this.webRtcCallSession.fromName,callerNumber:this.webRtcCallSession.from,remoteCallerName:this.webRtcCallSession.toName,remoteCallerNumber:this.webRtcCallSession.to,userVariables:{memberCallId:this.webRtcCallSession.id,memberId:e,...this.webRtcCallSession.userVariables},screenShare:t.isScreenShare,additionalDevice:t.isAdditionalDevice,pingSupported:!0,version:gh}}muteMainAudioInputDevice(){return this.mainPeerConnection.stopTrackSender("audio")}muteMainVideoInputDevice(){return this.mainPeerConnection.stopTrackSender("video")}async unmuteMainAudioInputDevice(){return this.mainPeerConnection.restoreTrackSender("audio")}async unmuteMainVideoInputDevice(){return this.mainPeerConnection.restoreTrackSender("video")}ensureLocalAudioPipeline(){return this.mainPeerConnection.ensureLocalAudioPipeline()}get localAudioPipeline(){return this.mainPeerConnection.localAudioPipeline}async addInputDevice(t={audio:!1,video:!0}){return this.initAdditionalPeerConnection("additional-device",t)}async addMainInputDevices(t={audio:!0}){var r;let e;const{mediaDirections:i}=this.mainPeerConnection;if((t.audio??t.inputAudioDeviceConstraints??(t.inputAudioStream&&i.audio.startsWith("send")))&&(e="audio"),(t.video??t.inputVideoDeviceConstraints??(t.inputVideoStream&&!i.video.startsWith("send")))&&(e=e==="audio"?"both":"video"),e)this.mainPeerConnection.updateMediaDevicesOptions(t),await this.mainPeerConnection.restoreTrackSender(e);else{const n=new ro("No valid device to be added");throw(r=this.onError)==null||r.call(this,n),n}}async addScreenMedia(t={audio:!1}){await this.initAdditionalPeerConnection("screenshare",t)}async initAdditionalPeerConnection(t,e){var r;let i=null;try{return this._screenShareStatus$.next("starting"),i=new nc({...e,...this.RTCPeerConnectionConfig,propose:t,webRTCApiProvider:this.webRTCApiProvider},void 0,this.deviceController),this.setupLocalDescriptionHandler(i),t==="screenshare"&&(this._screenShareId=i.id),this._rtcPeerConnectionsMap.set(i.id,i),this._rtcPeerConnections$.next(Array.from(this._rtcPeerConnectionsMap.values())),this.subscribeTo(i.errors$,n=>{var s;(s=this.onError)==null||s.call(this,n)}),await Ge(i.connectionState$.pipe(B(n=>n==="connected"),Me(1),or(this._screenShareTimeoutMs),S(this.destroyed$))),this._screenShareStatus$.next("started"),V.info("[WebRTCManager] Screen share started successfully."),i.id}catch(n){V.warn("[WebRTCManager] Error initializing additional peer connection:",n),(r=this.onError)==null||r.call(this,n instanceof Error?n:new Error(String(n),{cause:n})),i&&i.destroy(),this._screenShareStatus$.next("none")}}async removeInputDevices(t){return this.removeAdditionalPeerConnection(t)}removeMainInputDevice(t={removeAudio:!1,removeVideo:!0}){let e;if(t.removeAudio&&(e="audio"),t.removeVideo&&(e=e==="audio"?"both":"video"),e)return this.mainPeerConnection.stopTrackSender(e,{updateTransceiverDirection:!0})}async removeScreenMedia(){if(["starting","started"].includes(this._screenShareStatus$.value)||V.warn("[WebRTCManager] No active screen share to stop."),!this._screenShareId){V.debug("[WebRTCManager] No screen share peer connection found.");return}this._screenShareStatus$.next("stopping"),await this.removeAdditionalPeerConnection(this._screenShareId),this._screenShareId=void 0,this._screenShareStatus$.next("none")}async removeAdditionalPeerConnection(t){const e=this._rtcPeerConnectionsMap.get(t);try{e&&await this.executeVertoBye(e)}finally{e==null||e.destroy(),this._rtcPeerConnectionsMap.delete(t),this._rtcPeerConnections$.next(Array.from(this._rtcPeerConnectionsMap.values()))}}async executeVertoBye(t,e){try{const i=e?{cause:e,causeCode:eg[e]}:{};await this.executeVerto(Kp({...i,dialogParams:this.dialogParams(t)}))}catch(i){throw V.warn("[WebRTCManager] Call might already be disconnected, error sending Verto bye:",i),i}}async bye(t){this.attachManager.detach(this.buildAttachableCall());const e=this._rtcPeerConnectionsMap.get(this.webRtcCallSession.id);e&&await this.executeVertoBye(e,t)}async sendDigits(t){const e=Zp({sessid:this.webRtcCallSession.id,dialogParams:{callID:this.webRtcCallSession.id},dtmf:t});try{await this.executeVerto(e)}catch(i){throw V.warn("[WebRTCManager] Error sending DTMF digits:",i),i}}async transfer(t){const e=nn({...t,dialogParams:this.dialogParams(this.mainPeerConnection),action:"transfer"});try{V.debug("[WebRTCManager] Transferring call with options:",t),await this.executeVerto(e)}catch(i){throw V.error("[WebRTCManager] Error transferring call:",i),i}}destroy(){this._rtcPeerConnectionsMap.forEach(t=>{t.destroy()}),this._rtcPeerConnectionsMap.clear(),this._rtcPeerConnections$.complete(),super.destroy()}};const oc=W();var Ng=class extends ie{constructor(t={}){super(),this._source=null,this._stream=null,this._audioContext=(t.audioContextFactory??(()=>new AudioContext))(),this._analyser=this._audioContext.createAnalyser(),this._analyser.fftSize=2048,this._analyser.smoothingTimeConstant=.3,this._analyserBuffer=new Uint8Array(new ArrayBuffer(this._analyser.fftSize)),this._pollIntervalMs=t.pollIntervalMs??Co}get level$(){return this.deferEmission(Wr(this._pollIntervalMs,Ta).pipe(E(()=>this.computeLevel())))}setStream(t){if(this._source){try{this._source.disconnect()}catch(e){oc.debug("[RemoteAudioMeter] source disconnect warning:",e)}this._source=null,this._stream=null}!t||t.getAudioTracks().length===0||(this._stream=new MediaStream(t.getAudioTracks()),this._source=this._audioContext.createMediaStreamSource(this._stream))}destroy(){if(this._source){try{this._source.disconnect()}catch{}this._source=null}this._audioContext.close().catch(t=>{oc.debug("[RemoteAudioMeter] audio context close warning:",t)}),super.destroy()}computeLevel(){if(!this._source)return 0;this._analyser.getByteTimeDomainData(this._analyserBuffer);let t=0;for(const e of this._analyserBuffer){const i=(e-128)/128;t+=i*i}return Math.sqrt(t/this._analyserBuffer.length)}};const dr=W(),Bg=1e3,zg=10,Vg=2e3,Hg=3e3,jg=3,Wg=5,Gg=5,qg=10,Xg=4,Yg=30;function Kg(t,e){const i=t+e;return i===0?0:t/i*100}function Zg(t){return typeof t=="object"&&t!==null&&"type"in t&&t.type==="inbound-rtp"}function Qg(t){return typeof t=="object"&&t!==null&&"type"in t&&t.type==="candidate-pair"}var Jg=class extends ie{constructor(t,e={}){super(),this.peerConnection=t,this.running=!1,this.lastAudioPacketsReceived=0,this.lastAudioPacketChangeTime=0,this.lastVideoPacketsReceived=0,this.lastVideoPacketChangeTime=0,this.lastRoundTripTime=0,this.lastAvailableOutgoingBitrate=void 0,this._sample$=this.createReplaySubject(1),this._baseline$=this.createBehaviorSubject({rtt:0,jitter:0,ready:!1}),this._networkIssues$=this.createBehaviorSubject([]),this._networkMetrics$=this.createBehaviorSubject([]),this.pollingIntervalMs=e.pollingIntervalMs??Bg,this.baselineSampleCount=e.baselineSamples??zg,this.noAudioPacketThresholdMs=e.noAudioPacketThresholdMs??Vg,this.noVideoPacketThresholdMs=e.noVideoPacketThresholdMs??Hg,this.rttSpikeWarningMultiplier=e.rttSpikeWarningMultiplier??jg,this.rttSpikeCriticalMultiplier=e.rttSpikeCriticalMultiplier??Wg,this.packetLossWarningPercent=e.packetLossWarningPercent??Gg,this.packetLossCriticalPercent=e.packetLossCriticalPercent??qg,this.jitterSpikeMultiplier=e.jitterSpikeMultiplier??Xg,this.historyWindowSeconds=e.historyWindowSeconds??Yg}get networkIssues$(){return this._networkIssues$.asObservable()}get networkIssues(){return[...this._networkIssues$.value]}get isNetworkHealthy$(){return this.cachedObservable("isNetworkHealthy$",()=>this._networkIssues$.pipe(E(t=>t.length===0),L(),S(this.destroyed$)))}get isNetworkHealthy(){return this._networkIssues$.value.length===0}get networkMetrics$(){return this._networkMetrics$.asObservable()}get networkMetrics(){return[...this._networkMetrics$.value]}get criticalIssue$(){return this.cachedObservable("criticalIssue$",()=>this._networkIssues$.pipe(qn(t=>Ie(t.filter(e=>e.severity==="critical"))),S(this.destroyed$)))}get sample$(){return this._sample$.asObservable()}start(){if(this.running)return;this.running=!0;const t=Date.now();this.lastAudioPacketChangeTime=t,this.lastVideoPacketChangeTime=t,dr.debug("[RTCStatsMonitor] Starting stats monitoring"),this.subscribeTo(Wr(this.pollingIntervalMs).pipe(B(()=>this.running),Ke(()=>Ie(this.peerConnection.getStats()).pipe(ti(e=>(dr.warn("[RTCStatsMonitor] Failed to get stats:",e),$t)))),B(()=>this.running),E(e=>this.extractSample(e))),e=>this._sample$.next(e)),this.subscribeTo(this._sample$.pipe(Me(this.baselineSampleCount),Ad(),E(e=>({rtt:e.reduce((i,r)=>i+r.roundTripTime,0)/e.length,jitter:e.reduce((i,r)=>i+r.audioJitter,0)/e.length,ready:!0}))),e=>{dr.debug(`[RTCStatsMonitor] Baseline established: rtt=${e.rtt.toFixed(1)}ms, jitter=${e.jitter.toFixed(1)}ms`),this._baseline$.next(e)}),this.subscribeTo(this._sample$.pipe(Ka((e,i)=>({prev:e.current,current:i}),{prev:null,current:null}),B(e=>e.current!==null)),({prev:e,current:i})=>{const r=i.timestamp;this.updatePacketTracking(i,r);const n=this.detectIssues(i,e,r);this._networkIssues$.next(n)}),this.subscribeTo(this._sample$.pipe(Ka((e,i)=>{const r=i.timestamp-this.historyWindowSeconds*1e3,n={timestamp:i.timestamp,audio:{packetsReceived:i.audioPacketsReceived,packetsLost:i.audioPacketsLost,jitter:i.audioJitter},video:{packetsReceived:i.videoPacketsReceived,packetsLost:i.videoPacketsLost},roundTripTime:i.roundTripTime,availableOutgoingBitrate:i.availableOutgoingBitrate};return[...e.filter(s=>s.timestamp>r),n]},[])),e=>this._networkMetrics$.next(e))}stop(){this.running&&(this.running=!1,dr.debug("[RTCStatsMonitor] Stopping stats monitoring"))}destroy(){dr.debug("[RTCStatsMonitor] Destroying RTCStatsMonitor"),this.stop(),super.destroy()}extractSample(t){let e=0,i=0,r=0,n=0,s=0,a=0,o;return t.forEach(c=>{Zg(c)&&(c.kind==="audio"?(e+=c.packetsReceived??0,i+=c.packetsLost??0,r=Math.max(r,(c.jitter??0)*1e3)):(n+=c.packetsReceived??0,s+=c.packetsLost??0)),Qg(c)&&c.state==="succeeded"&&c.nominated&&(a=c.currentRoundTripTime?c.currentRoundTripTime*1e3:this.lastRoundTripTime,o=c.availableOutgoingBitrate??this.lastAvailableOutgoingBitrate)}),{audioPacketsReceived:e,audioPacketsLost:i,audioJitter:r,videoPacketsReceived:n,videoPacketsLost:s,roundTripTime:a,availableOutgoingBitrate:o,timestamp:Date.now()}}updatePacketTracking(t,e){t.audioPacketsReceived!==this.lastAudioPacketsReceived&&(this.lastAudioPacketsReceived=t.audioPacketsReceived,this.lastAudioPacketChangeTime=e),t.videoPacketsReceived!==this.lastVideoPacketsReceived&&(this.lastVideoPacketsReceived=t.videoPacketsReceived,this.lastVideoPacketChangeTime=e),t.roundTripTime>0&&(this.lastRoundTripTime=t.roundTripTime),t.availableOutgoingBitrate!==void 0&&(this.lastAvailableOutgoingBitrate=t.availableOutgoingBitrate)}detectIssues(t,e,i){const r=[],n=this._baseline$.value,s=i-this.lastAudioPacketChangeTime;s>this.noAudioPacketThresholdMs&&r.push({type:"no_inbound_audio",severity:"critical",timestamp:i,value:s,threshold:this.noAudioPacketThresholdMs});const a=i-this.lastVideoPacketChangeTime;if(a>this.noVideoPacketThresholdMs&&r.push({type:"no_inbound_video",severity:"warning",timestamp:i,value:a,threshold:this.noVideoPacketThresholdMs}),n.ready){const o=t.roundTripTime,c=n.rtt;if(c>0){const p=o/c;p>this.rttSpikeCriticalMultiplier?r.push({type:"high_rtt",severity:"critical",timestamp:i,value:o,threshold:c*this.rttSpikeCriticalMultiplier}):p>this.rttSpikeWarningMultiplier&&r.push({type:"high_rtt",severity:"warning",timestamp:i,value:o,threshold:c*this.rttSpikeWarningMultiplier})}const u=t.audioJitter,h=n.jitter;h>0&&u/h>this.jitterSpikeMultiplier&&r.push({type:"high_jitter",severity:"warning",timestamp:i,value:u,threshold:h*this.jitterSpikeMultiplier})}if(e){const o=Math.max(0,t.audioPacketsReceived-e.audioPacketsReceived),c=Math.max(0,t.audioPacketsLost-e.audioPacketsLost),u=Math.max(0,t.videoPacketsReceived-e.videoPacketsReceived),h=Math.max(0,t.videoPacketsLost-e.videoPacketsLost),p=o+u,f=Kg(c+h,p);f>this.packetLossCriticalPercent?r.push({type:"high_packet_loss",severity:"critical",timestamp:i,value:f,threshold:this.packetLossCriticalPercent}):f>this.packetLossWarningPercent&&r.push({type:"high_packet_loss",severity:"warning",timestamp:i,value:f,threshold:this.packetLossWarningPercent})}return r}};const oe=W(),$g=2e3,ef=1e4,tf=3e3,rf=5e3,nf=3,sf=3,af=3e3,of=1e4,cf=150,lf=300,uf=5,df=new Set(["high_rtt","high_jitter","high_packet_loss"]);var hf=class extends ie{constructor(t,e,i={}){super(),this._recoveryState$=this.createBehaviorSubject("idle"),this._recoveryEvent$=this.createSubject(),this._bandwidthConstrained$=this.createBehaviorSubject(!1),this._hasPacketLoss$=this.createBehaviorSubject(!1),this._trigger$=this.createSubject(),this._attemptCount=0,this._keyframeBurstCount=0,this._keyframeBurstStart=0,this._keyframeCooldownUntil=0,this._cooldownUntil=0,this._pipelineStop$=this.createSubject(),this._callbacks=t,this._inputs=e,this._config={debounceTimeMs:i.debounceTimeMs??$g,cooldownMs:i.cooldownMs??ef,iceGracePeriodMs:i.iceGracePeriodMs??tf,iceRestartTimeoutMs:i.iceRestartTimeoutMs??rf,maxAttempts:i.maxAttempts??nf,enableRelayFallback:i.enableRelayFallback??!0,keyframeMaxBurst:i.keyframeMaxBurst??sf,keyframeBurstWindowMs:i.keyframeBurstWindowMs??af,keyframeCooldownMs:i.keyframeCooldownMs??of,degradationBitrateThreshold:i.degradationBitrateThreshold??cf,degradationRecoveryThreshold:i.degradationRecoveryThreshold??lf,enableAutoDegradation:i.enableAutoDegradation??!0,packetLossRecoveryDelaySec:i.packetLossRecoveryDelaySec??uf},this.initPipeline(),this.initDegradationRecoveryPipeline()}get recoveryState$(){return this._recoveryState$.asObservable().pipe(S(this._destroyed$))}get recoveryState(){return this._recoveryState$.value}get recoveryEvent$(){return this._recoveryEvent$.asObservable().pipe(S(this._destroyed$))}get bandwidthConstrained$(){return this._bandwidthConstrained$.asObservable().pipe(S(this._destroyed$))}get bandwidthConstrained(){return this._bandwidthConstrained$.value}pushTrigger(t){this._trigger$.next(t)}async requestIceRestart(){if(this._recoveryState$.value==="recovering"){oe.info("CallRecoveryManager: manual ICE restart skipped — recovery already in progress");return}oe.info("CallRecoveryManager: manual ICE restart requested"),this.transitionTo("recovering"),await this.executeIceRestart(!1),this.startCooldown()}requestKeyframe(){this.executeKeyframe("manual request")}reset(){oe.info("CallRecoveryManager: resetting counters"),this._attemptCount=0,this._keyframeBurstCount=0,this._keyframeBurstStart=0,this._keyframeCooldownUntil=0,this._cooldownUntil=0,this.transitionTo("idle")}notifyModifyFailed(){(this._recoveryState$.value==="cooldown"||this._recoveryState$.value==="idle")&&(oe.info("CallRecoveryManager: verto.modify failed — re-entering recovery"),this._cooldownUntil=0,this.transitionTo("idle"),this.pushTrigger({source:"network",detail:"modify_failed_during_recovery"}))}reportBandwidth(t){if(!this._config.enableAutoDegradation)return;const e=this._bandwidthConstrained$.value;!e&&t<this._config.degradationBitrateThreshold?(this._bandwidthConstrained$.next(!0),this._callbacks.disableVideo(),this.emitEvent({action:"video_disabled",reason:`bandwidth ${t}kbps below threshold ${this._config.degradationBitrateThreshold}kbps`,timestamp:Date.now()}),oe.warn(`CallRecoveryManager: disabling video — bandwidth ${t}kbps < ${this._config.degradationBitrateThreshold}kbps`)):e&&t>=this._config.degradationRecoveryThreshold&&(this._bandwidthConstrained$.next(!1),this._callbacks.enableVideo(),this.emitEvent({action:"video_restored",reason:`bandwidth ${t}kbps recovered above ${this._config.degradationRecoveryThreshold}kbps`,timestamp:Date.now()}),oe.info(`CallRecoveryManager: restoring video — bandwidth ${t}kbps >= ${this._config.degradationRecoveryThreshold}kbps`))}reportNetworkIssues(t){const e=t.some(i=>i.type==="high_packet_loss");e!==this._hasPacketLoss$.value&&this._hasPacketLoss$.next(e)}handleWebSocketReconnect(){const t=this._callbacks.getPeerConnectionState();t==="connected"||t==="completed"?(oe.info("CallRecoveryManager: signal-only reconnect — peer connection still alive"),this.emitEvent({action:"signal_reconnect",reason:"WebSocket reconnected, peer connection still connected",timestamp:Date.now()})):(oe.info("CallRecoveryManager: full reconnect — peer connection also down"),this.emitEvent({action:"full_reconnect",reason:"WebSocket reconnected, peer connection not connected — ICE restart needed",timestamp:Date.now()}),this.pushTrigger({source:"network",detail:"full_reconnect_after_ws"})),this.reset()}destroy(){this._pipelineStop$.next(),this._pipelineStop$.complete(),super.destroy()}initPipeline(){this.subscribeTo(this._trigger$.pipe(Se(()=>{this._recoveryState$.value==="idle"&&this.transitionTo("debouncing")}),Xa(this._config.debounceTimeMs),Za(this._inputs.signalingReady$),B(([,t])=>this.passGateChecks(t)),E(([t])=>t),Xn(t=>this.executeTieredRecovery(t)),S(Di(this._destroyed$,this._pipelineStop$))),{next:()=>{},error:t=>{oe.error("CallRecoveryManager: pipeline error",t),this.transitionTo("idle")}})}initDegradationRecoveryPipeline(){if(!this._config.enableAutoDegradation)return;const t=this._config.packetLossRecoveryDelaySec*1e3;this.subscribeTo(Ha([this._bandwidthConstrained$,this._hasPacketLoss$]).pipe(Ke(([e,i])=>e&&!i?xi(t):$t),S(this._destroyed$)),()=>{this._bandwidthConstrained$.next(!1),this._callbacks.enableVideo(),this.emitEvent({action:"video_restored",reason:`no packet loss for ${this._config.packetLossRecoveryDelaySec}s — restoring video`,timestamp:Date.now()}),oe.info(`CallRecoveryManager: restoring video — no packet loss for ${this._config.packetLossRecoveryDelaySec}s`)})}passGateChecks(t){return this._callbacks.isNegotiating()?(oe.debug("CallRecoveryManager: gate blocked — negotiation in progress"),this.transitionTo("idle"),!1):t?this._callbacks.isCallConnected()?this.isCooldownActive()?(oe.debug("CallRecoveryManager: gate blocked — cooldown active"),this.transitionTo("cooldown"),!1):!0:(oe.debug("CallRecoveryManager: gate blocked — call not connected"),this.transitionTo("idle"),!1):(oe.debug("CallRecoveryManager: gate blocked — signaling not ready"),this.transitionTo("idle"),!1)}isCooldownActive(){return Date.now()<this._cooldownUntil}executeTieredRecovery(t){return this.transitionTo("recovering"),oe.info(`CallRecoveryManager: starting tiered recovery — source=${t.source} detail=${t.detail}`),Ie(this.runTiers(t)).pipe(Se(()=>this.startCooldown()),ti(e=>(oe.error("CallRecoveryManager: tiered recovery failed",e),this.startCooldown(),$t)))}async runTiers(t){if(this.executeKeyframe(t.detail),t.issueType&&df.has(t.issueType)){oe.debug(`CallRecoveryManager: degradation-only issue (${t.issueType}) — Tier 1 only, skipping ICE restart`);return}this._attemptCount<this._config.maxAttempts&&await this.executeIceRestart(!1)||this._config.enableRelayFallback&&this._attemptCount<this._config.maxAttempts&&await this.executeIceRestart(!0)||this._attemptCount>=this._config.maxAttempts&&(this.emitEvent({action:"max_attempts_reached",reason:`all ${this._config.maxAttempts} recovery attempts exhausted`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.warn("CallRecoveryManager: max recovery attempts reached"))}executeKeyframe(t){const e=Date.now();if(e<this._keyframeCooldownUntil){oe.debug("CallRecoveryManager: keyframe request skipped — cooldown active");return}if(e-this._keyframeBurstStart>this._config.keyframeBurstWindowMs&&(this._keyframeBurstCount=0,this._keyframeBurstStart=e),this._keyframeBurstCount>=this._config.keyframeMaxBurst){this._keyframeCooldownUntil=e+this._config.keyframeCooldownMs,oe.debug(`CallRecoveryManager: keyframe burst limit reached (${this._config.keyframeMaxBurst}), cooldown until ${this._keyframeCooldownUntil}`);return}this._keyframeBurstCount+=1,this._callbacks.requestKeyframe(),this.emitEvent({action:"keyframe_requested",reason:t,timestamp:e}),oe.debug(`CallRecoveryManager: keyframe requested (burst ${this._keyframeBurstCount}/${this._config.keyframeMaxBurst})`)}async executeIceRestart(t){this._attemptCount+=1;const e=t?"Tier 3 (relay-only)":"Tier 2 (standard)";oe.info(`CallRecoveryManager: ${e} ICE restart — attempt ${this._attemptCount}/${this._config.maxAttempts}`),this.emitEvent({action:"reinvite_started",reason:`${e} ICE restart`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()});try{return await this.withTimeout(this._callbacks.requestIceRestart(t),this._config.iceRestartTimeoutMs)?(this.emitEvent({action:"reinvite_succeeded",reason:`${e} ICE restart succeeded`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.info(`CallRecoveryManager: ${e} ICE restart succeeded`),this._attemptCount=0,!0):(this.emitEvent({action:"reinvite_failed",reason:`${e} ICE restart returned false`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.warn(`CallRecoveryManager: ${e} ICE restart failed`),!1)}catch{return this.emitEvent({action:"reinvite_timeout",reason:`${e} ICE restart timed out after ${this._config.iceRestartTimeoutMs}ms`,attempt:this._attemptCount,maxAttempts:this._config.maxAttempts,timestamp:Date.now()}),oe.warn(`CallRecoveryManager: ${e} ICE restart timed out`),!1}}async withTimeout(t,e){return new Promise((i,r)=>{const n=setTimeout(()=>{r(new Error(`Timeout after ${e}ms`))},e);t.then(s=>{clearTimeout(n),i(s)},s=>{clearTimeout(n),r(s instanceof Error?s:new Error(String(s)))})})}transitionTo(t){const e=this._recoveryState$.value;e!==t&&(oe.debug(`CallRecoveryManager: state ${e} -> ${t}`),this._recoveryState$.next(t))}startCooldown(){this._cooldownUntil=Date.now()+this._config.cooldownMs,this.transitionTo("cooldown"),this._cooldownSubscription&&this._cooldownSubscription.unsubscribe(),this._cooldownSubscription=xi(this._config.cooldownMs).pipe(Me(1),S(this._destroyed$),B(()=>this._recoveryState$.value==="cooldown")).subscribe(()=>this.transitionTo("idle"))}emitEvent(t){this._recoveryEvent$.next(t)}},pf=class{constructor(t,e,i){this.executeMethod=t,this.vertoManager=e,this.deviceController=i}createSelfParticipant(t){return new jo(t,this.executeMethod,this.vertoManager,this.deviceController)}createParticipant(t){return new Ho(t,this.executeMethod,this.deviceController)}};const gf=93.2,ff=.024,vf=2.5,bf=0,mf=100,yf=1,wf=5,Sf=4,Cf=3.5,Af=3,Tf=2;function Ef(t,e,i){const r=gf-(t/2+e)*ff-i*vf,n=Math.min(mf,Math.max(bf,r)),s=1+.035*n+n*(n-60)*(100-n)*7e-6;return Math.min(wf,Math.max(yf,s))}function kf(t){return t>=Sf?"excellent":t>=Cf?"good":t>=Af?"fair":t>=Tf?"poor":"critical"}const me=W(),xf=5/3,Df=t=>{if(!t)return{};try{const e=new URL(`destination:${t}`),i={};return e.searchParams.forEach((r,n)=>{i[n]=r}),i}catch(e){return me.warn(`Failed to parse destination URI: ${t}`,e),{}}};var If=class extends ie{constructor(t,e,i,r){super(),this.clientSession=t,this.options=e,this.address=r,this._errors$=this.createReplaySubject(1),this._lastMergedStatus="new",this._answered$=this.createReplaySubject(),this._holdState=!1,this._userVariables$=this.createBehaviorSubject({...g.instance.userVariables}),this._networkIssues$=this.createBehaviorSubject([]),this._networkMetrics$=this.createBehaviorSubject([]),this._isNetworkHealthy$=this.createBehaviorSubject(!0),this._qualityScore$=this.createBehaviorSubject(5),this._qualityLevel$=this.createBehaviorSubject("excellent"),this._recoveryState$=this.createBehaviorSubject("idle"),this._recoveryEvent$=this.createSubject(),this._bandwidthConstrained$=this.createBehaviorSubject(!1),this._mediaParamsUpdated$=this.createSubject(),this._customSubscriptions=new Map,this._pushToTalkEnabled=!1,this._remoteAudioMeter=null,this.id=e.callId??Jr(),this.to=e.to,this._userVariables$.next({...this._userVariables$.value,...Df(e.to),...e.userVariables}),this.subscribeTo(this.webrtcMessages$,o=>{const c=tt(o,"params.userVariables");c&&this._userVariables$.next({...this._userVariables$.value,...c})});const n=i.initializeManagers(this);this.vertoManager=n.vertoManager,this.callEventsManager=n.callEventsManager,e.initOffer?(this._status$=this.createBehaviorSubject("ringing"),this._lastMergedStatus="ringing"):this._status$=this.createBehaviorSubject("new");const{deviceController:s,networkChange$:a}=i;this._networkChange$=a,this.participantFactory=new pf(this.executeMethod.bind(this),this.vertoManager,s),this.subscribeTo(Di(this._status$.asObservable(),this.vertoManager.signalingStatus$).pipe(L(),S(this._destroyed$)),o=>{var c;this._lastMergedStatus=o,o==="connected"&&!this._statsMonitor?this.initResilienceSubsystems():o==="disconnected"?((c=this._statsMonitor)==null||c.destroy(),this._statsMonitor=void 0):(o==="destroyed"||o==="failed")&&this.stopResilienceSubsystems()})}get errors$(){return this.deferEmission(this._errors$.asObservable())}emitError(t){this._status$.value==="destroyed"||this._status$.value==="failed"||(this._errors$.next(t),t.fatal&&(this._status$.next("failed"),this.destroy()))}notifyModifyFailed(){var t;(t=this._recoveryManager)==null||t.notifyModifyFailed()}get direction(){return this.options.initOffer?"inbound":"outbound"}get address$(){return this.deferEmission(Ie([this.address])).pipe(S(this._destroyed$))}get fromName(){return this.options.fromName}get from(){return this.options.from}get toName(){return this.options.toName}async toggleIncomingVideo(){throw new vt}async toggleIncomingAudio(){throw new vt}addCallId(t){this.callEventsManager.addCallId(t)}get capabilities(){return this.callEventsManager.capabilities}get participants(){return this.callEventsManager.participants}get self(){return this.callEventsManager.self}async toggleLock(){const t=this.locked?"call.unlock":"call.lock";await this.executeMethod(this.selfId??"",t,{})}async toggleHold(){this._holdState?await this.vertoManager.unhold():await this.vertoManager.hold(),this._holdState=!this._holdState}async startRecording(){throw new vt}async startStreaming(){throw new vt}async setMeta(t){throw new vt}async updateMeta(t){throw new vt}get layoutLayers$(){return this.deferEmission(this.callEventsManager.layoutLayers$).pipe(S(this._destroyed$))}get layoutLayers(){return this.callEventsManager.layoutLayers}async executeMethod(t,e,i){var s,a,o;const r=this.buildMethodParams(t,i),n=_i({method:e,params:r});try{const c=await this.clientSession.execute(n);if(cg(c))throw new Lt(parseInt(((s=c.result)==null?void 0:s.code)??"0"),`Error response from method ${e}: ${(a=c.result)==null?void 0:a.code} ${(o=c.result)==null?void 0:o.message}`,void 0,void 0,n.id);return c}catch(c){throw me.error(`[Call] Error executing method ${e} with params`,r,c),c}}buildMethodParams(t,e){const i={node_id:this.nodeId??"",call_id:this.id,member_id:this.vertoManager.selfId??""};return typeof t=="object"?{...e,self:i,targets:[t]}:{...e,self:i,target:{node_id:this.nodeId??"",call_id:this.id,member_id:t}}}get status$(){return this.publicCachedObservable("status$",()=>Di(this._status$.asObservable(),this.vertoManager.signalingStatus$).pipe(L(),Se(t=>{this._lastMergedStatus=t})))}get participants$(){return this.deferEmission(this.callEventsManager.participants$).pipe(S(this._destroyed$))}get self$(){return this.deferEmission(this.callEventsManager.self$).pipe(S(this._destroyed$))}get recording$(){return this.deferEmission(this.callEventsManager.recording$).pipe(S(this._destroyed$))}get streaming$(){return this.deferEmission(this.callEventsManager.streaming$).pipe(S(this._destroyed$))}get raiseHandPriority$(){return this.deferEmission(this.callEventsManager.raiseHandPriority$).pipe(S(this._destroyed$))}get locked$(){return this.deferEmission(this.callEventsManager.locked$).pipe(S(this._destroyed$))}get meta$(){return this.deferEmission(this.callEventsManager.meta$).pipe(S(this._destroyed$))}get capabilities$(){return this.deferEmission(this.callEventsManager.capabilities$).pipe(S(this._destroyed$))}get layout$(){return this.deferEmission(this.callEventsManager.layout$).pipe(S(this._destroyed$))}get status(){return this._lastMergedStatus}get recording(){return this.callEventsManager.recording}get streaming(){return this.callEventsManager.streaming}get raiseHandPriority(){return this.callEventsManager.raiseHandPriority}get locked(){return this.callEventsManager.locked}get meta(){return this.callEventsManager.meta}get layout(){return this.callEventsManager.layout}get layouts$(){return this.deferEmission(this.callEventsManager.layouts$).pipe(S(this._destroyed$))}get layouts(){return this.callEventsManager.layouts}get localStream$(){return this.deferEmission(this.vertoManager.localStream$).pipe(S(this._destroyed$))}get localStream(){return this.vertoManager.localStream}get remoteStream$(){return this.deferEmission(this.vertoManager.remoteStream$).pipe(S(this._destroyed$))}get remoteStream(){return this.vertoManager.remoteStream}get userVariables$(){return this.deferEmission(this._userVariables$.asObservable())}get userVariables(){return{...this._userVariables$.value}}set userVariables(t){this._userVariables$.next({...this._userVariables$.value,...t})}get networkIssues$(){return this.deferEmission(this._networkIssues$.asObservable())}get networkIssues(){return this._networkIssues$.value}get isNetworkHealthy$(){return this.deferEmission(this._isNetworkHealthy$.asObservable())}get isNetworkHealthy(){return this._isNetworkHealthy$.value}get networkMetrics$(){return this.deferEmission(this._networkMetrics$.asObservable())}get networkMetrics(){return this._networkMetrics$.value}get qualityScore$(){return this.deferEmission(this._qualityScore$.asObservable())}get qualityLevel$(){return this.deferEmission(this._qualityLevel$.asObservable())}get recoveryState$(){return this.deferEmission(this._recoveryState$.asObservable())}get recoveryEvent$(){return this.deferEmission(this._recoveryEvent$.asObservable())}get bandwidthConstrained$(){return this.deferEmission(this._bandwidthConstrained$.asObservable())}get mediaParamsUpdated$(){return this.deferEmission(this._mediaParamsUpdated$.asObservable())}emitMediaParamsUpdated(t){this._mediaParamsUpdated$.next(t)}requestKeyframe(){var t,e;(e=(t=this.vertoManager).requestKeyframe)==null||e.call(t)}async requestIceRestart(){var t,e;await((e=(t=this.vertoManager).requestIceRestart)==null?void 0:e.call(t))}initResilienceSubsystems(){const t=this.rtcPeerConnection;if(me.debug(`[Call] initResilienceSubsystems: pc=${t?"exists":"undefined"}, connectionState=${t==null?void 0:t.connectionState}`),!t){me.warn("[Call] No peer connection available, skipping resilience init");return}try{const e=g.instance;this._statsMonitor=new Jg(t,{pollingIntervalMs:e.statsPollingInterval,baselineSamples:e.statsBaselineSamples,noAudioPacketThresholdMs:e.statsNoPacketThreshold,rttSpikeWarningMultiplier:e.statsRttSpikeMultiplier,rttSpikeCriticalMultiplier:e.statsRttSpikeMultiplier*xf,packetLossWarningPercent:e.statsPacketLossThreshold*100,packetLossCriticalPercent:e.statsPacketLossThreshold*200,jitterSpikeMultiplier:e.statsJitterSpikeMultiplier,historyWindowSeconds:e.statsHistorySize}),this._recoveryManager=new hf({requestKeyframe:()=>{var i,r;try{this.vertoManager.requestKeyframeAll?this.vertoManager.requestKeyframeAll():(r=(i=this.vertoManager).requestKeyframe)==null||r.call(i)}catch{}},requestIceRestart:async i=>{var r,n;try{this.vertoManager.requestIceRestartAll?await this.vertoManager.requestIceRestartAll(i):await((n=(r=this.vertoManager).requestIceRestart)==null?void 0:n.call(r,i))}catch{return!1}return this.waitForPeerConnectionConnected()},disableVideo:()=>{try{this.vertoManager.muteMainVideoInputDevice(),me.debug("[Call] Recovery manager disabled video")}catch{me.debug("[Call] Recovery manager failed to disable video")}},enableVideo:()=>{this.vertoManager.unmuteMainVideoInputDevice().catch(()=>{me.debug("[Call] Recovery manager failed to enable video")})},isNegotiating:()=>this.vertoManager.mainPeerConnection.isNegotiating,isCallConnected:()=>this._lastMergedStatus==="connected",getPeerConnectionState:()=>t.connectionState},{signalingReady$:this.clientSession.authenticated$},{debounceTimeMs:e.recoveryDebounceTime,cooldownMs:e.recoveryCooldown,iceGracePeriodMs:e.iceDisconnectedGracePeriod,iceRestartTimeoutMs:e.iceRestartTimeout,maxAttempts:e.maxRecoveryAttempts,enableRelayFallback:e.enableRelayFallback,keyframeMaxBurst:e.keyframeMaxBurst,keyframeBurstWindowMs:e.keyframeBurstWindow,keyframeCooldownMs:e.keyframeCooldown}),this.subscribeTo(this._statsMonitor.networkIssues$,i=>{var r;this._networkIssues$.next(i),(r=this._recoveryManager)==null||r.reportNetworkIssues(i)}),this.subscribeTo(this._statsMonitor.isNetworkHealthy$,i=>{this._isNetworkHealthy$.next(i)}),this.subscribeTo(this._statsMonitor.networkMetrics$,i=>{var r;if(this._networkMetrics$.next(i),i.length>0){const n=i[i.length-1],s=n.audio.packetsReceived+n.video.packetsReceived,a=n.audio.packetsLost+n.video.packetsLost,o=s+a>0?a/(s+a)*100:0,c=Ef(n.roundTripTime,n.audio.jitter,o);this._qualityScore$.next(c),this._qualityLevel$.next(kf(c)),n.availableOutgoingBitrate!==void 0&&((r=this._recoveryManager)==null||r.reportBandwidth(n.availableOutgoingBitrate/1e3))}}),this.subscribeTo(this._statsMonitor.criticalIssue$,i=>{var r;(r=this._recoveryManager)==null||r.pushTrigger({source:"stats",detail:`${i.type}: ${i.severity}`,issueType:i.type})}),this.subscribeTo(this._recoveryManager.recoveryState$,i=>{this._recoveryState$.next(i)}),this.subscribeTo(this._recoveryManager.recoveryEvent$,i=>{this._recoveryEvent$.next(i),i.action==="max_attempts_reached"&&(me.warn("[Call] All recovery attempts exhausted, terminating call"),this.emitError({kind:"network",fatal:!0,error:new Error("Call recovery failed: all attempts exhausted"),callId:this.id}))}),this.subscribeTo(this._recoveryManager.bandwidthConstrained$,i=>{this._bandwidthConstrained$.next(i)}),this._networkChange$&&this.subscribeTo(this._networkChange$,i=>{var r,n;i.type==="offline"?(r=this._recoveryManager)==null||r.pushTrigger({source:"network",detail:"browser went offline"}):i.type==="online"&&((n=this._recoveryManager)==null||n.handleWebSocketReconnect())}),this.subscribeTo(this.clientSession.authenticated$.pipe(Gr(1),B(Boolean)),()=>{var i;me.debug("[Call] WebSocket reconnected — notifying recovery manager"),(i=this._recoveryManager)==null||i.handleWebSocketReconnect()}),this._statsMonitor.start(),me.debug("[Call] Resilience subsystems initialized for call",this.id)}catch(e){me.warn("[Call] Failed to initialize resilience subsystems:",e)}}async waitForPeerConnectionConnected(){const t=this.rtcPeerConnection;if(!t)return!1;const e=Date.now()+qh;for(;;){const i=t.connectionState;if(i==="connected")return!0;if(i==="failed"||i==="closed"||Date.now()>=e)return!1;await new Promise(r=>setTimeout(r,Xh))}}stopResilienceSubsystems(){var t,e;try{(t=this._statsMonitor)==null||t.destroy(),(e=this._recoveryManager)==null||e.destroy()}catch{}this._statsMonitor=void 0,this._recoveryManager=void 0}createParticipant(t,e){return t===(e??this.vertoManager.selfId)?this.participantFactory.createSelfParticipant(t):this.participantFactory.createParticipant(t)}get mediaDirections$(){return this.deferEmission(this.vertoManager.mediaDirections$).pipe(S(this._destroyed$))}get mediaDirections(){return this.vertoManager.mediaDirections}get participantsId$(){return this.cachedObservable("participantsId$",()=>this.participants$.pipe(E(t=>t.map(e=>e.id))))}async execute(t,e){return this.clientSession.execute(t,e)}get selfId$(){return this.vertoManager.selfId$}get selfId(){return this.vertoManager.selfId}get nodeId$(){return this.vertoManager.nodeId$}get nodeId(){return this.vertoManager.nodeId}isCallSessionEvent(t){try{me.debug("[Call] Checking if event is for this call session:",t);const e=tt(t,"params.params.callID")??tt(t,"params.call_id"),i=tt(t,"params.room_session_id");return me.debug(`[Call] Extracted session identifiers callID: ${e} and roomSessionID: ${i} from event:`),e===this.id||!!e&&this.callEventsManager.isCallIdValid(e)||!!i&&this.callEventsManager.isRoomSessionIdValid(i)}catch(e){return me.error("[Call] Error checking if event is for this call session:",e),!1}}get callSessionEvents$(){return this.cachedObservable("callSessionEvents$",()=>this.clientSession.signalingEvent$.pipe(B(t=>this.isCallSessionEvent(t)),Se(t=>{me.debug("[Call] Received call session event:",t)}),S(this.destroyed$),kt()))}get callUpdated$(){return this.publicCachedObservable("callUpdated$",()=>this.callSessionEvents$.pipe(fe(qo,"params"),S(this.destroyed$)))}get memberJoined$(){return this.publicCachedObservable("memberJoined$",()=>this.callSessionEvents$.pipe(fe(Ko,"params"),S(this.destroyed$)))}get memberLeft$(){return this.publicCachedObservable("memberLeft$",()=>this.callSessionEvents$.pipe(fe(Zo,"params"),S(this.destroyed$)))}get memberUpdated$(){return this.publicCachedObservable("memberUpdated$",()=>this.callSessionEvents$.pipe(fe(Yo,"params"),S(this.destroyed$)))}get memberTalking$(){return this.publicCachedObservable("memberTalking$",()=>this.callSessionEvents$.pipe(fe(Qo,"params"),S(this.destroyed$)))}get callStates$(){return this.publicCachedObservable("callStates$",()=>this.callSessionEvents$.pipe(fe(Xo,"params"),S(this.destroyed$)))}get layoutUpdates$(){return this.publicCachedObservable("layoutUpdates$",()=>this.callSessionEvents$.pipe(fe(os,"params"),S(this.destroyed$)))}get rtcPeerConnection(){return this.vertoManager.mainPeerConnection.peerConnection}get signalingEvent$(){return this.publicCachedObservable("signalingEvent$",()=>this.callEvent$.pipe(E(t=>JSON.parse(JSON.stringify(t)))))}subscribe(t){const e=this._customSubscriptions.get(t);if(e)return e;const i=this.callSessionEvents$.pipe(B(r=>r.event_type===t),E(r=>JSON.parse(JSON.stringify(r))),S(this._destroyed$));return this._sendVertoSubscribe(t).then(()=>{this._customSubscriptions.set(t,i)},r=>{this._customSubscriptions.delete(t),me.warn(`[Call] verto.subscribe for '${t}' failed, not caching:`,r)}),this._customSubscriptions.set(t,i),i}get webrtcMessages$(){return this.cachedObservable("webrtcMessages$",()=>this.callSessionEvents$.pipe(fe(sn,"params"),Se(t=>me.debug("[Call] Event is a WebRTC message event:",t)),S(this.destroyed$),kt()))}get callEvent$(){return this.cachedObservable("callEvent$",()=>this.callSessionEvents$.pipe(fe(lg,"params"),Se(t=>me.debug("[Call] Event is a call event:",t)),S(this.destroyed$),kt()))}get layoutEvent$(){return this.cachedObservable("layoutEvent$",()=>this.callEvent$.pipe(fe(os,"params")))}async hangup(){this._status$.next("disconnecting");try{await this.vertoManager.bye()}finally{this.destroy()}}async sendDigits(t){return this.vertoManager.sendDigits(t)}answer(t){this._answerMediaOptions=t,this._answered$.next(!0)}get answerMediaOptions(){return this._answerMediaOptions}reject(){this._answered$.next(!1)}get answered$(){return this.deferEmission(this._answered$.asObservable())}async setLayout(t,e){if(!this.layouts.includes(t))throw new ro(`Layout ${t} is not available in the current call layouts: ${this.layouts.join(", ")}`);const i=await Ge(this.selfId$.pipe(B(r=>r!==null)));await this.executeMethod(i,"call.layout.set",{layout:t,positions:e})}async transfer(t){return this.vertoManager.transfer(t)}setLocalMicrophoneGain(t){const e=this.vertoManager.ensureLocalAudioPipeline();if(!e){me.warn("[Call] setLocalMicrophoneGain: audio pipeline unavailable");return}const i=Math.max(0,Math.min(200,t));e.setGain(i/100)}get localMicrophoneGain$(){const t=this.vertoManager.ensureLocalAudioPipeline();return t?this.publicCachedObservable("localMicrophoneGain$",()=>t.gain$.pipe(E(e=>e*100),S(this._destroyed$))):Et(100).pipe(S(this._destroyed$))}get localAudioLevel$(){const t=this.vertoManager.ensureLocalAudioPipeline();return t?this.publicCachedObservable("localAudioLevel$",()=>t.level$.pipe(S(this._destroyed$),kt())):Et(0).pipe(S(this._destroyed$))}get localSpeaking$(){const t=this.vertoManager.ensureLocalAudioPipeline();return t?this.publicCachedObservable("localSpeaking$",()=>t.speaking$.pipe(S(this._destroyed$),kt())):Et(!1).pipe(S(this._destroyed$))}enablePushToTalk(){const t=this.vertoManager.ensureLocalAudioPipeline();if(!t){me.warn("[Call] enablePushToTalk: audio pipeline unavailable");return}t.setPTTActive(!1),this._pushToTalkEnabled=!0}disablePushToTalk(){var t;(t=this.vertoManager.localAudioPipeline)==null||t.setPTTActive(!0),this._pushToTalkEnabled=!1}setPushToTalkActive(t){var e;this._pushToTalkEnabled&&((e=this.vertoManager.localAudioPipeline)==null||e.setPTTActive(t))}async setEchoCancellation(t){await this.vertoManager.updateMediaConstraints({audio:{echoCancellation:t}})}async setNoiseSuppression(t){await this.vertoManager.updateMediaConstraints({audio:{noiseSuppression:t}})}async setAutoGainControl(t){await this.vertoManager.updateMediaConstraints({audio:{autoGainControl:t}})}get remoteAudioLevel$(){return this.publicCachedObservable("remoteAudioLevel$",()=>{this._remoteAudioMeter??(this._remoteAudioMeter=new Ng);const t=this._remoteAudioMeter;return this.subscribeTo(this.vertoManager.remoteStream$,e=>{t.setStream(e)}),t.level$.pipe(S(this._destroyed$),kt())})}destroy(){var t;this._status$.value!=="destroyed"&&(this._status$.next("destroyed"),this.stopResilienceSubsystems(),(t=this._remoteAudioMeter)==null||t.destroy(),this._remoteAudioMeter=null,this.vertoManager.destroy(),this.callEventsManager.destroy(),super.destroy())}async _sendVertoSubscribe(t){const e=Jp({sessid:this.id,eventChannel:[t]}),i={callID:this.id,node_id:this.vertoManager.nodeId??"",message:e};await this.clientSession.execute(No(i))}};function Mf(t){return t instanceof Yr?"timeout":t instanceof Lt?"signaling":t instanceof Mi?"media":t instanceof $a||t instanceof eo?"network":"internal"}const Rf=new Set([yo,So,wo]);function Pf(t){return!(t instanceof so||t instanceof Mi||t instanceof Yr||t instanceof Lt&&Rf.has(t.code))}var Of=class{constructor(t,e,i,r,n){this.sessionManager=t,this.deviceController=e,this.attachManager=i,this.webRTCApiProvider=r,this.networkChange$=n}createCall(t,e){return new If(this.sessionManager,e,{initializeManagers:i=>({vertoManager:new Ug(i,this.attachManager,this.deviceController,this.webRTCApiProvider,{nodeId:e.nodeId,onError:r=>{const n={kind:Mf(r),fatal:Pf(r),error:r,callId:i.id};i.emitError(n)},onModifyFailed:()=>{i.notifyModifyFailed()}}),callEventsManager:new mg(i)}),deviceController:this.deviceController,networkChange$:this.networkChange$},t)}};const ds=W();var cc=class{constructor(t,e,i){this.endpoint=t,this.http=i,this.filter=r=>!0,this.mapper=r=>r,this.nextUrl=`${this.endpoint}?${e}`}async next(){if(!this.nextUrl)return this.hasMore=!1,[];const t=await this.http.request({...$n,url:this.nextUrl});if(t.ok&&t.body){const e=JSON.parse(t.body);return this.nextUrl=e.links.next,this.hasMore=!!this.nextUrl,e.data.filter(this.filter).map(this.mapper)}return ds.error("Failed to fetch entity"),[]}async id(t){const e=await this.http.request({...$n,url:`${this.endpoint}/${String(t)}`});if(e.ok&&e.body)return JSON.parse(e.body)}},lc=class extends ie{constructor(t,e,i){super(),this.fetchController=t,this.update$=e,this.onError=i,this.collectionData=new Map,this.observablesRegistry=new Map,this.upsertData=r=>{var o;if(!r.id)return;const n=this.collectionData.get(r.id)??{},s={},a=new Set([...Object.keys(n),...Object.keys(r)]);for(const c of a){const u=n[c],h=r[c];h!==void 0&&u!==void 0&&typeof u=="object"&&u!==null&&!Array.isArray(u)&&typeof h=="object"&&h!==null&&!Array.isArray(h)?s[c]={...u,...h}:h!==void 0?s[c]=h:s[c]=u}this.collectionData.set(r.id,s),(o=this.observablesRegistry.get(r.id))==null||o.next(s),this._values$.next(Array.from(this.collectionData.values()))},this._loading$=this.createBehaviorSubject(!1),this._values$=this.createReplaySubject(1),this._hasMore$=this.createBehaviorSubject(!0),this.subscribeTo(this.update$,this.upsertData),this.hasMore$=ei(()=>Ie(this.init())).pipe(Ke(()=>this._hasMore$),L(),et(1),S(this.destroyed$))}get loading$(){return this._loading$.asObservable()}get loading(){return this._loading$.value}get values$(){return this._values$.asObservable()}get hasMore(){return this.fetchController.hasMore??!0}get updated$(){return this.cachedObservable("updated$",()=>this._loading$.pipe(L(),Gr(1),B(t=>!t),E(()=>{}),S(this.destroyed$)))}get values(){return Array.from(this.collectionData.values())}async init(){if(this.fetchController.hasMore===!1){this._hasMore$.next(!1);return}await this.fetchMore()}async fetchMore(){var t;try{this._loading$.next(!0),(await this.fetchController.next()).forEach(this.upsertData),this._hasMore$.next(this.fetchController.hasMore??!1),this._loading$.next(!1)}catch(e){ds.error("Failed to fetch initial collection data",e),this._hasMore$.next(this.fetchController.hasMore??!1),this._loading$.next(!1),(t=this.onError)==null||t.call(this,new ao("fetchMore",e))}}async tryFetch(t,e){var i,r,n;try{this._loading$.next(!0);const s=await((r=(i=this.fetchController)[t])==null?void 0:r.call(i,e));return this._loading$.next(!1),s&&this.upsertData(s),s}catch(s){ds.error(`Failed to fetch data for (${String(t)}:${String(e)}) :`,s),this._loading$.next(!1),(n=this.onError)==null||n.call(this,new ao(`tryFetch(${String(t)})`,s))}}get$(t){var e,i;if(!this.observablesRegistry.has(t)){this.observablesRegistry.set(t,new zn(1));const r=this.collectionData.get(t);r?(e=this.observablesRegistry.get(t))==null||e.next(r):this.tryFetch("id",t)}return(i=this.observablesRegistry.get(t))==null?void 0:i.asObservable()}async find$(t,e){const i=Array.from(this.collectionData.values()).find(r=>r[t]===e)??await this.tryFetch(t,e);return i?this.get$(i.id):void 0}loadMore(){this.fetchController.hasMore!==!1&&this.fetchMore()}destroy(){this.observablesRegistry.forEach(t=>t.complete()),this.observablesRegistry.clear(),super.destroy()}},uc=class{constructor(t,e=r=>!!r,i=r=>r){this.originalCollection=t,this.filter=e,this.mapper=i}get loading$(){return this.originalCollection.loading$}get loading(){return this.originalCollection.loading}get hasMore$(){return this.originalCollection.hasMore$}get hasMore(){return this.originalCollection.hasMore}get values(){return this.originalCollection.values.filter(this.filter).map(this.mapper)}get values$(){return this._values$??(this._values$=this.originalCollection.values$.pipe(E(t=>t.filter(this.filter).map(this.mapper))))}get$(t){const e=this.originalCollection.get$(t);return e&&e.pipe(Vr(B(this.filter),E(this.mapper)))}async find$(t,e){const i=await this.originalCollection.find$(t,e);return i&&i.pipe(Vr(B(this.filter),E(this.mapper)))}loadMore(){this.originalCollection.loadMore()}destroy(){this.originalCollection.destroy()}},dc=class extends ie{constructor(t,e,i){super(),this.addressId=t,this.conversationManager=e,this.addressProvider=i,this.initConversationMessages=async()=>(this._conversationMessages=this._conversationMessages??await this.conversationManager.getConversationMessageCollection(this.id),this._conversationMessages.hasMore&&this._conversationMessages.loadMore(),this._conversationMessages),this.textMessages$=ei(this.initConversationMessages).pipe(E(()=>this.textMessage),et(1),S(this.destroyed$)),this.history$=ei(this.initConversationMessages).pipe(E(()=>this.history),et(1),S(this.destroyed$)),this._state$=this.createBehaviorSubject(null)}upnext(t){const e={...this._state$.value,...t};this._state$.next(e)}get state(){return this._state$.value}get id(){return this.addressId}get name(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.name}get createdAt(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.created_at}get defaultChannel(){return this.type==="room"?this.channels.video:this.channels.audio}get displayName$(){return this.cachedObservable("displayName$",()=>this._state$.pipe(ue(),E(t=>t.display_name),S(this.destroyed$)))}get displayName(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.display_name}get previewUrl$(){return this.cachedObservable("previewUrl$",()=>this._state$.pipe(ue(),E(t=>t.preview_url),S(this.destroyed$)))}get previewUrl(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.preview_url}get coverUrl$(){return this.cachedObservable("coverUrl$",()=>this._state$.pipe(ue(),et(1),E(t=>t.cover_url),S(this.destroyed$)))}get coverUrl(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.cover_url}get resourceId$(){return this.cachedObservable("resourceId$",()=>this._state$.pipe(ue(),et(1),E(t=>t.resource_id),S(this.destroyed$)))}get resourceId(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.resource_id}get type$(){return this.cachedObservable("type$",()=>this._state$.pipe(ue(),et(1),E(t=>t.type),S(this.destroyed$)))}get type(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.type}get channels$(){return this.cachedObservable("channels$",()=>this._state$.pipe(ue(),et(1),E(t=>t.channels),S(this.destroyed$)))}get channels(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.channels}get locked(){if(!this._state$.value)throw new Q("state not initialized");return this._state$.value.locked}get locked$(){return this.cachedObservable("locked$",()=>this._state$.pipe(ue(),et(1),E(t=>t.locked),S(this.destroyed$)))}async sendText(t){return this.conversationManager.sendText(t,this.id)}get textMessage(){if(this._conversationMessages)return this._textMessages$=this._textMessages$??new uc(this._conversationMessages,t=>t.subtype==="chat",t=>({id:t.id,text:t.text,created:t.ts,fromAddress$:this.addressProvider.get$(t.from_fabric_address_id)})),this._textMessages$}get history(){if(this._conversationMessages)return this._history$=this._history$??new uc(this._conversationMessages,t=>t.subtype==="log",t=>({id:t.id,kind:t.kind,status:t.details.status,started:t.details.start_time,ended:t.details.end_time,fromAddress$:this.addressProvider.get$(t.from_fabric_address_id)})),this._history$}get activity$(){throw new vt}get activity(){throw new vt}};const ci=W(),_f=t=>{if(ci.debug("isRPCConnectResult check:",t),!t||typeof t!="object")return!1;const e=t,i=typeof e.identity=="string"&&typeof e.protocol=="string"&&typeof e.authorization=="object"&&typeof e.authorization.jti=="string"&&typeof e.authorization.project_id=="string"&&typeof e.authorization.fabric_subscriber=="object";return ci.debug("isRPCConnectResult check result:",i),i};var Lf=(gi=class{constructor(e,i,r){this.id=Jr(),ci.debug(`[PendingRPC(${this.id}) request:${e.id}: method:${e.method}] Creating PendingRPC`),this.request=e;const n=(r==null?void 0:r.timeoutMs)??gi.defaultTimeoutMs,s=r==null?void 0:r.signal;this.promise=new Promise((a,o)=>{if(s!=null&&s.aborted){o(new DOMException("The operation was aborted","AbortError"));return}let c=!1;const u=cr(i.pipe(B(h=>h.id===e.id),Me(1)),new be(h=>{const p=setTimeout(()=>{h.error(new Yr(e.id,n))},n);return()=>clearTimeout(p)}),s?new be(h=>{const p=()=>{h.error(new DOMException("The operation was aborted","AbortError"))};return s.addEventListener("abort",p),()=>s.removeEventListener("abort",p)}):fd).subscribe({next:h=>{if(c=!0,h.error){const p=new Lt(h.error.code,h.error.message,h.error.data,void 0,e.id);ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Rejecting promise with RPC error:`,p),o(p)}else ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Resolving promise with response:`,h),a(h);u.unsubscribe()},error:h=>{ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Rejecting promise with error:`,h),c=!0,o(h),u.unsubscribe()},complete:()=>{ci.debug(`[PendingRPC(${this.id}) request:${e.id}] Observable completed`),c||o(new Yr(e.id,n)),u.unsubscribe()}})})}async then(e,i){return this.promise.then(e,i)}async catch(e){return this.promise.catch(e)}async finally(e){return this.promise.finally(e)}},gi.defaultTimeoutMs=5e3,gi);const X=W(),Ff=t=>{var n,s;const e=(n=t.to)==null?void 0:n.split("?")[0],i=(s=t.from)!=null&&s.startsWith("subscriber://")?t.from.replace("subscriber://",""):t.from,r=e??i;if(!r)throw new Ii("Error building Address name");return r};var Uf=class extends ie{constructor(t,e,i,r,n,s,a,o,c){super(),this.getCredential=t,this.transport=e,this.storage=i,this.authorizationStateKey=r,this.attachManager=s,this.dpopManager=o,this.callCreateTimeout=6e3,this.agent="signalwire-js/4.0.0",this.eventAcks=!0,this.authorizationState$=this.createReplaySubject(1),this.connectVersion={major:4,minor:0,revision:0},this._authorization$=this.createBehaviorSubject(void 0),this._errors$=this.createReplaySubject(1),this._authState$=this.createBehaviorSubject({kind:"unauthenticated"}),this._wasClientBound=!1,this._userInfo$=this.createBehaviorSubject(null),this._calls$=this.createBehaviorSubject({}),this._iceServers$=this.createBehaviorSubject([]),s.setSession(this),this.callFactory=new Of(this,n,s,a,c),this.initialized$=ei(()=>Ie(this.init())).pipe(et(1),S(this.destroyed$))}get incomingCalls$(){return this.cachedObservable("incomingCalls$",()=>this.calls$.pipe(E(t=>t.filter(e=>e.direction==="inbound"))))}get incomingCalls(){return Object.values(this._calls$.value).filter(t=>t.direction==="inbound")}get userInfo$(){return this._userInfo$.asObservable()}get userInfo(){return this._userInfo$.value}get calls$(){return this.cachedObservable("calls$",()=>this._calls$.pipe(E(t=>Object.values(t))))}get calls(){return Object.values(this._calls$.value)}get iceServers(){return this._iceServers$.value}get authorization$(){return this._authorization$.asObservable()}get authorization(){return this._authorization$.value}get errors$(){return this._errors$.asObservable()}get authenticated$(){return this._authState$.pipe(E(t=>t.kind==="authenticated"),L())}get authenticated(){return this._authState$.value.kind==="authenticated"}get clientBound(){return this._wasClientBound}get authState(){return this._authState$.value}setDirectory(t){this._directory=t}async execute(t,e){try{return await this.transport.execute(t,e)}catch(i){throw X.debug("[Session] Execute Error",i),this._errors$.next(i instanceof Error?i:new Error(String(i),{cause:i})),i}}send(t){this.transport.send(t)}async init(){return await this.loadAuthorizationStateFromStorage(),this.setupMessageHandlers(),!0}setupMessageHandlers(){X.debug("[Session] Setting up message handlers"),this.subscribeTo(this.authStateEvent$,async t=>{X.debug("[Session] Authorization state event received:",t);try{await this.updateAuthorizationStateInStorage(t.authorization_state)}catch(e){X.error("[Session] Failed to handle authorization state update:",e),this._errors$.next(new Kn(e))}}),this.subscribeTo(this.transport.connectionStatus$.pipe(B(t=>t==="disconnected"||t==="reconnecting")),()=>{this._authState$.value.kind==="authenticated"&&this._authState$.next({kind:"unauthenticated"})}),this.subscribeTo(this.transport.connectionStatus$.pipe(B(t=>t==="connected"),Xn(()=>(X.debug("[Session] Connection established, initiating authentication"),Ie(this.authenticate()).pipe(ti(t=>(this.handleAuthenticationError(t).catch(e=>{X.error("[Session] Error handling authentication failure:",e)}),$t)))))),void 0),this.subscribeTo(this.vertoInvite$,async t=>{X.debug("[Session] Verto invite received:",t);try{await this.createInboundCall(t)}catch(e){X.error("[Session] Error handling Verto invite:",e),this._errors$.next(new Bd(e))}}),this.subscribeTo(this.vertoAttach$,async t=>{X.debug("[Session] Verto attach received:",t);try{await this.handleVertoAttach(t)}catch(e){X.error("[Session] Error handling Verto attach:",e),this._errors$.next(new zd(e))}})}async loadAuthorizationStateFromStorage(){try{const t=await this.storage.getItem(this.authorizationStateKey);this.authorizationState$.next(t??void 0)}catch(t){X.error("Failed to retrieve authorization state from storage:",t),this.authorizationState$.next(void 0)}}async updateAuthorizationStateInStorage(t){if(!t){X.debug("[Session] Removing authorization state from storage");try{await this.storage.removeItem(this.authorizationStateKey),this.authorizationState$.next(void 0)}catch(e){throw X.error("Failed to remove authorization state from storage:",e),e}return}try{X.debug("[Session] Updating authorization state in storage"),await this.storage.setItem(this.authorizationStateKey,t),this.authorizationState$.next(t)}catch(e){throw X.error("Failed to retrieve authorization state from storage:",e),e}}get authStateEvent$(){return this.cachedObservable("authStateEvent$",()=>this.signalingEvent$.pipe(Se(t=>{X.debug("[Session] Received incoming message:",t)}),fe(ug,"params"),Se(t=>{X.debug("[Session] Authorization state event received:",t.authorization_state)})))}get signalingEvent$(){return this.cachedObservable("signalingEvent$",()=>this.transport.incomingEvent$.pipe(fe(as,"params"),kt()))}get vertoInvite$(){return this.cachedObservable("vertoInvite$",()=>this.signalingEvent$.pipe(B(sn),B(t=>sc(t.params)),E(t=>({node_id:t.node_id,...t.params.params}))))}get vertoAttach$(){return this.cachedObservable("vertoAttach$",()=>this.signalingEvent$.pipe(B(sn),B(t=>ac(t.params)),E(t=>({node_id:t.node_id,...t.params.params}))))}get contexts(){return[]}get eventing(){return[]}get topics(){return[]}get authentication(){const t=this.getCredential();if(!t.token)throw new Q("Credential token is undefined");return{jwt_token:t.token}}async connect(){await Ge(this.initialized$),await this.transport.connect(),await Ge(this.authenticated$.pipe(S(this.destroyed$),B(Boolean),Me(1),or({first:15e3})))}async handleAuthenticationError(t){X.error("Authentication error:",t);const e=t instanceof Lt&&(t.code===yo||t.code===wo||t.code===So),i=await Ge(this.authorizationState$.pipe(Me(1)))!==void 0;if(e&&i){X.debug("[Session] Recoverable auth error — cleaning up stored state and reconnecting fresh");try{await this.cleanupStoredConnectionParams()}catch(r){X.error("Failed to cleanup stored connection params:",r)}finally{this.transport.reconnect()}}else this._errors$.next(t)}async cleanupStoredConnectionParams(){await this.transport.setProtocol(void 0),await this.updateAuthorizationStateInStorage(void 0),this._authorization$.next(void 0)}async updateAuthState(t){try{await this.storage.setItem(this.authorizationStateKey,t)}catch(e){X.error("Failed to update authorization state in storage:",e),this._errors$.next(new Kn(e))}}async reauthenticate(t,e,i){var r,n;X.debug("[Session] Re-authenticating session");try{let s=e;if(!s&&((r=this.dpopManager)!=null&&r.initialized))try{s=await this.dpopManager.createRpcProof({method:"signalwire.reauthenticate"})}catch(o){if(this.clientBound)throw o;X.warn("[Session] Failed to create DPoP proof for reauthenticate:",o)}const a=jp({project:((n=this._authorization$.value)==null?void 0:n.project_id)??"",jwt_token:t,...s?{dpop_token:s}:{}});await Ba(Ie(this.transport.execute(a)).pipe(uo(),Me(1),ti(o=>{throw X.error("[Session] Re-authentication RPC failed:",o),o}))),i!=null&&i.clientBound&&(this._wasClientBound=!0),X.debug("[Session] Re-authentication successful, updating stored auth state")}catch(s){throw X.error("[Session] Re-authentication failed:",s),this._errors$.next(new Kn(s)),s}}async authenticate(){var o,c;X.debug("[Session] Starting authentication process");const t=await Ge(Ha({protocol:this.transport.protocol$,authorization_state:this.authorizationState$}).pipe(Me(1)));X.debug(`[Session] Persisted params:
73
73
  `,{protocol:t.protocol,authStateLength:(o=t.authorization_state)==null?void 0:o.length});const e=t.authorization_state&&t.protocol,i=this.getCredential().token,r=e&&i;let n;if(r?X.debug("[Session] Reconnecting with stored jwt_token + authorization_state"):this.onBeforeReconnect&&this.clientBound&&(X.debug("[Session] Refreshing credentials before fresh connect"),await this.onBeforeReconnect()),(!r||this.clientBound)&&((c=this.dpopManager)!=null&&c.initialized))try{n=await this.dpopManager.createRpcProof({method:"signalwire.connect"})}catch(u){if(this.clientBound)throw u;X.warn("[Session] Failed to create DPoP proof for connect, proceeding without:",u)}const s=Hp({authentication:r?{jwt_token:i}:this.authentication,version:this.connectVersion,agent:this.agent,contexts:this.contexts,eventing:this.eventing,topics:this.topics,event_acks:this.eventAcks,...n?{dpop_token:n}:{},...r?{authorization_state:t.authorization_state,protocol:t.protocol}:{}}),a=await Ba(Ie(this.transport.execute(s)).pipe(uo(),E(u=>u.result),B(_f),Se(()=>{X.debug("[Session] Response passed filter, processing authentication result")}),Me(1),ti(u=>{throw X.error("[Session] Authentication RPC failed:",u),u})));X.debug("[Session] Processing authentication result:",{hasProtocol:!!a.protocol,hasAuthorization:!!a.authorization,hasIceServers:!!a.ice_servers}),a.protocol&&await this.transport.setProtocol(a.protocol),this._authorization$.next(a.authorization),this._iceServers$.next(a.ice_servers??[]),this._authState$.next({kind:"authenticated"}),X.debug("[Session] Authentication completed successfully")}async disconnect(){this.transport.disconnect(),this._authState$.next({kind:"unauthenticated"}),await this.cleanupStoredConnectionParams()}async createInboundCall(t){const e=await this.createCall({nodeId:t.node_id,callId:t.callID,initOffer:t.sdp,toName:t.callee_id_name,to:t.callee_id_number,fromName:t.caller_id_name,from:t.caller_id_number,displayDirection:t.display_direction,userVariables:t.userVariables});this._calls$.next({[`${e.id}`]:e,...this._calls$.value})}async handleVertoAttach(t){const{callID:e}=t;if(e in this._calls$.value){X.debug(`[Session] Verto attach for existing call ${e}, deferring to per-call handler`);return}const i=await this.attachManager.consumePendingAttachment(e);X.debug(`[Session] Creating reattached call for callID: ${e}`);const r=await this.createCall({nodeId:t.node_id,callId:e,toName:t.callee_id_name,to:t.callee_id_number,fromName:t.caller_id_name,from:t.caller_id_number,reattach:!0,...i});this._calls$.next({[`${r.id}`]:r,...this._calls$.value})}async createOutboundCall(t,e={}){const i=t instanceof dc?t.defaultChannel:t;let r;try{return r=await this.createCall({to:i,...e}),await Ge(cr(r.selfId$.pipe(B(n=>!!n),Me(1),or(this.callCreateTimeout)),r.errors$.pipe(Me(1),Ke(n=>ed(()=>n.error))))),this._calls$.next({[`${r.id}`]:r,...this._calls$.value}),r}catch(n){X.error("[Session] Error creating outbound call:",n),r==null||r.destroy();const s=new io(n instanceof Va?"Call create timeout":"Call creation failed",n,"outbound");throw this._errors$.next(s),s}}async createCall(t={}){try{const e=Ff(t);let i;try{if(!this._directory)throw new Q("Directory not initialized");const n=await this._directory.findAddressIdByURI(e);if(!n)throw new Q(`Address name: ${e} not found`);if(i=this._directory.get(n),!i)throw new Q(`Address ID: ${n} not found`)}catch{X.warn(`[Session] Directory lookup failed for ${e}, proceeding with raw URI`)}const r=this.callFactory.createCall(i,{...t});return this.subscribeTo(r.status$.pipe(B(n=>n==="destroyed"),Me(1)),()=>{const{[`${r.id}`]:n,...s}=this._calls$.value;this._calls$.next(s)}),r}catch(e){throw X.error("[Session] Error creating call session:",e),new io("Call create error",e,t.initOffer?"inbound":"outbound")}}destroy(){for(const t of Object.values(this._calls$.value))t.hangup();super.destroy()}},Nf=class{constructor(t){this.clientSessionManager=t}get authenticated$(){return this.clientSessionManager.authenticated$}get authenticated(){return this.clientSessionManager.authenticated}get signalingEvent$(){return this.clientSessionManager.signalingEvent$}get iceServers(){return this.clientSessionManager.iceServers}async execute(t,e){return this.clientSessionManager.execute(t,e)}get incomingCalls$(){return this.clientSessionManager.incomingCalls$}get incomingCalls(){return this.clientSessionManager.incomingCalls}get calls$(){return this.clientSessionManager.calls$}get calls(){return this.clientSessionManager.calls}};const Bf=t=>typeof t=="string",hs=W();var zf=class extends cc{constructor(t,e){super(`/api/fabric/conversations/${t}/messages`,"page_size=100",e),this.groupId=t}},Vf=class extends lc{constructor(t,e,i,r){super(new zf(t,i),e,r)}},Hf=class{constructor(t,e,i,r){this.clientSession=t,this.http=e,this.getUserAddressId=i,this.onError=r,this.groupIds=new Map}async join(t){const e=this.getUserAddressId();try{const i=await this.http.request({...$r,url:"/api/fabric/conversations/join",body:JSON.stringify({from_fabric_address_id:e,fabric_address_ids:[t,e]})});if(i.ok&&i.body){const r=JSON.parse(i.body);if(Bf(r.group_id))return this.groupIds.set(t,r.group_id),r.group_id}throw new no("Join Failed - Unexpected response")}catch(i){throw hs.error("[ConversationsManager] Failed to join conversation:",i),i}}async getConversationMessageCollection(t){const e=this.groupIds.get(t)??await this.join(t);return Promise.resolve(new Vf(e,this.clientSession.signalingEvent$.pipe(fe(Jo,"params"),Se(i=>hs.debug("[ConversationsManager ] Conversation Event:",i)),E(i=>({...i}))),this.http,this.onError))}async sendText(t,e){const i=this.groupIds.get(e)??await this.join(e),r=this.getUserAddressId();try{if((await this.http.request({...$r,url:"/api/fabric/messages",body:JSON.stringify({group_id:i,from_fabric_address_id:r,text:t})})).ok)return;throw new no("Send Text Failed - Unexpected response")}catch(n){throw hs.error("[ConversationsManager] Failed to send text message:",n),n}}};const st=W();function jf(t){return t.expires_at?t.expires_at:t.expires_in?Math.floor(Date.now()/1e3)+t.expires_in:(st.warn("[DeviceToken] Could not determine token expiry, using default"),Math.floor(Date.now()/1e3)+ur)}function hc(t){return t.expires_in?t.expires_in:t.expires_at?Math.max(t.expires_at-Math.floor(Date.now()/1e3),1):ur}var Wf=class extends ie{constructor(t,e,i,r){super(),this.dpopManager=t,this.http=e,this.errorHandler=i,this.getCredential=r,this._currentToken$=this.createBehaviorSubject(null),this._refreshInProgress=!1,this._effectiveExpireIn=ur,this.subscribeTo(this._currentToken$.pipe(B(Boolean),Ke(n=>{const s=jf(n),a=Math.max(s*1e3-Date.now()-Ch,1e3);return st.debug(`[DeviceToken] Scheduling Client Bound SAT refresh in ${a}ms`),xi(a)})),()=>{this.executeRefresh()})}get effectiveExpireIn(){return this._effectiveExpireIn}async activate(t,e,i){var n;const{satClaims:r}=t;if(!((n=r==null?void 0:r.scope)!=null&&n.includes(Sh))){st.debug("[DeviceToken] No sat:refresh scope, skipping Client Bound SAT activation");return}this._session=e,this._updateCredential=i;try{const s=await this.obtainToken();!s.expires_at&&!s.expires_in&&r.expires_at&&(s.expires_at=r.expires_at),this._effectiveExpireIn=hc(s);const a=await this.dpopManager.createRpcProof({method:"signalwire.reauthenticate"});await e.reauthenticate(s.token,a,{clientBound:!0}),i({token:s.token}),st.info("[DeviceToken] Client Bound SAT activated successfully"),this._currentToken$.next(s)}catch(s){st.error("[DeviceToken] Failed to activate Client Bound SAT:",s),this.errorHandler(new Zr(s,"Failed to activate Client Bound SAT"));const a=this.getCredential(),o=r.expires_at??(a.expiry_at?a.expiry_at/1e3:Date.now()/1e3+ur/1e3);this._currentToken$.next({token:a.token??"",expires_at:o})}}async obtainToken(){const t=await this.dpopManager.createHttpProof({method:"POST",uri:fo}),e=await this.http.request({url:fo,...$r,body:JSON.stringify({dpop_token:t,expire_in:ur})});if(!e.ok||!e.body)throw new oo(`Failed to obtain device token: ${e.status} ${e.statusText}`);const i=JSON.parse(e.body);if(!i.token)throw new oo("Device token response missing token field");return i}async refreshToken(t,e,i){st.debug("[DeviceToken] Refreshing Client Bound SAT");const r=await this.dpopManager.createHttpProof({method:"POST",uri:vo,accessToken:e}),n=await this.http.request({url:vo,...$r,body:JSON.stringify({dpop_token:r,expire_in:this._effectiveExpireIn})});if(!n.ok||!n.body)throw new ri(`Failed to refresh device token: ${n.status} ${n.statusText}`);const s=JSON.parse(n.body);if(!s.token)throw new ri("Device token refresh response missing token field");!s.expires_at&&!s.expires_in&&(s.expires_in=this._effectiveExpireIn),this._effectiveExpireIn=hc(s);const a=await this.dpopManager.createRpcProof({method:"signalwire.reauthenticate"});return await t.reauthenticate(s.token,a),i({token:s.token}),st.info("[DeviceToken] Client Bound SAT refreshed successfully"),s}async executeRefresh(){if(this._refreshInProgress){st.debug("[DeviceToken] Refresh already in progress, skipping");return}const t=this._session,e=this._updateCredential;if(!t||!e){st.warn("[DeviceToken] Cannot refresh: session or updateCredential not set");return}if(!t.authenticated){st.debug("[DeviceToken] Session not authenticated, deferring refresh");return}this._refreshInProgress=!0;try{const i=this.getCredential().token;if(!i)throw new ri("No current token available for refresh");const r=await this.retryRefresh(t,i,e);this._currentToken$.next(r)}catch(i){st.error("[DeviceToken] Automatic Client Bound SAT refresh failed:",i),this.errorHandler(i instanceof ri?i:new ri("Automatic token refresh failed",i))}finally{this._refreshInProgress=!1}}async retryRefresh(t,e,i){let r;for(let n=0;n<bo;n++)try{return await this.refreshToken(t,e,i)}catch(s){if(r=s,n<bo-1){const a=Ah*Math.pow(2,n);st.warn(`[DeviceToken] Refresh attempt ${n+1} failed, retrying in ${a}ms`),await new Promise(o=>setTimeout(o,a))}}throw r instanceof Error?r:new ri("All refresh retries exhausted",r)}destroy(){super.destroy()}};const ps=W(),Gf=1e3;function qf(){try{if(typeof navigator<"u"&&navigator.userAgent)return navigator.userAgent}catch{}return"unknown"}var Xf=class extends ie{constructor(t){super(),this._events=[],this._calls=[],this._deviceChanges=[],this._eventRecorded$=this.createSubject(),this._sdkVersion=t.sdkVersion,this._maxEvents=t.maxEvents??Gf,ps.debug("DiagnosticsCollector initialized",{sdkVersion:this._sdkVersion,maxEvents:this._maxEvents})}get eventRecorded$(){return this._eventRecorded$.asObservable()}record(t,e,i){const r={timestamp:Date.now(),category:t,event:e,...i!==void 0?{details:i}:{}};this._events=this._appendToBuffer(this._events,r),this._eventRecorded$.next(r)}recordDeviceChange(t,e){const i={timestamp:Date.now(),category:"device",event:t,...e!==void 0?{details:e}:{}};this._deviceChanges=this._appendToBuffer(this._deviceChanges,i),this._events=this._appendToBuffer(this._events,i),this._eventRecorded$.next(i)}recordCallSummary(t){this._calls=this._appendCallToBuffer(this._calls,t),this.record("call","call_summary",{callId:t.callId,direction:t.direction,duration:t.duration,finalStatus:t.finalStatus})}export(){return{sdkVersion:this._sdkVersion,userAgent:qf(),exportedAt:Date.now(),events:[...this._events],calls:[...this._calls],deviceChanges:[...this._deviceChanges]}}clear(){this._events=[],this._calls=[],this._deviceChanges=[],ps.debug("DiagnosticsCollector buffers cleared")}destroy(){ps.debug("DiagnosticsCollector destroyed"),super.destroy()}_appendToBuffer(t,e){const i=[...t,e];return i.length>this._maxEvents?i.slice(i.length-this._maxEvents):i}_appendCallToBuffer(t,e){const i=[...t,e];return i.length>this._maxEvents?i.slice(i.length-this._maxEvents):i}};const pc=t=>((t==null?void 0:t.length)??0)===0,Yf=t=>{t.pipe(Me(1)).subscribe()},Kf=W();var Zf=class extends cc{constructor(t){super("/api/fabric/addresses","sort_by=name&sort_order=asc",t)}async name(t){const e=await this.http.request({...$n,url:`${this.endpoint}?name=${encodeURIComponent(t)}`});if(e.ok&&e.body){const i=JSON.parse(e.body);if(!pc(i.data))return i.data[0]}Kf.error("Failed to fetch addresses")}},Qf=class extends lc{constructor(t,e,i){super(new Zf(e),t,i)}},Jf=class extends ie{constructor(t,e,i,r){super(),this.http=t,this.conversationManager=i,this.onError=r,this.addNewAddress=n=>{var o;const s=new dc(n,this.conversationManager,this),a=(o=this._statesCollection.get$(n))==null?void 0:o.pipe(ue(),E(c=>(s.upnext(c),s)));a&&(Yf(a),this._observableRegistry.set(n,a)),this._addressesInstances.set(n,s)},this._addresses$=this.createBehaviorSubject([]),this._addressesInstances=new Map,this._observableRegistry=new Map,this._statesCollection=new Qf(e.signalingEvent$.pipe(fe($o,"params"),E(n=>({}))),this.http,this.onError),this.initSubscriptions()}get loading(){return this._statesCollection.loading}initSubscriptions(){this.subscribeTo(this._statesCollection.updated$,()=>{const t=Array.from(this._addressesInstances.values().map(i=>i.id)),e=this._statesCollection.values.filter(i=>!t.includes(i.id));pc(e)||(e.forEach(i=>this.addNewAddress(i.id)),this._addresses$.next(Array.from(this._addressesInstances.values())))})}get addresses$(){return this._addresses$.asObservable()}get addresses(){return this._addresses$.value}get hasMore$(){return this._statesCollection.hasMore$}get loading$(){return this._statesCollection.loading$}loadMore(){this._statesCollection.hasMore&&this._statesCollection.loadMore()}get$(t){return this._observableRegistry.has(t)||this.addNewAddress(t),this._observableRegistry.get(t)}get(t){return this._addressesInstances.get(t)}async findAddressIdByURI(t){var i;let e=(i=this._addressesInstances.values().find(r=>r.name===t))==null?void 0:i.id;if(!e){const r=await this._statesCollection.find$("name",t);if(r){const n=await Ge(r);this.addNewAddress(n.id),e=n.id}}return e}};const gc=W();var $f=(wt=class extends ie{constructor(e,i,r,n={}){super(),this.WebSocketConstructor=e,this.endpoint=i,this.outgoingMessages$=r,this.messageQueue=[],this.shouldReconnect=!1,this.boundHandleOpen=()=>this.handleOpen(),this.boundHandleClose=s=>this.handleClose(s),this.boundHandleError=()=>this.handleError(),this.boundHandleMessage=s=>this.handleMessage(s),this._status$=this.createBehaviorSubject("disconnected"),this._incomingMessages$=this.createSubject(),this._errors$=this.createReplaySubject(1),this.reconnectDelayMin=n.reconnectDelayMin??wt.DEFAULT_RECONNECT_DELAY_MIN_MS,this.reconnectDelayMax=n.reconnectDelayMax??wt.DEFAULT_RECONNECT_DELAY_MAX_MS,this.connectionTimeout=n.connectionTimeout??wt.DEFAULT_CONNECTION_TIMEOUT_MS,this.currentReconnectDelay=this.reconnectDelayMin,this.subscriptions.push(this.outgoingMessages$.subscribe(s=>{this.send(s)}))}get status$(){return this._status$.asObservable()}get incomingMessages$(){return this._incomingMessages$.asObservable()}get errors$(){return this._errors$.asObservable()}connect(){this._status$.value==="connecting"||this._status$.value==="connected"||(this.shouldReconnect=!0,this._status$.next("connecting"),this.createWebSocket())}disconnect(){this.shouldReconnect=!1,this.clearReconnectTimer(),this.clearConnectionTimeout();const e=this._status$.value;e==="connected"||e==="connecting"||e==="reconnecting"?this.socket?(this._status$.next("disconnecting"),this.socket.close()):this._status$.next("disconnected"):this._status$.next("disconnected")}reconnect(){this.shouldReconnect=!0,this._status$.next("reconnecting"),this.scheduleReconnection()}send(e){var i;this._status$.value==="connected"&&((i=this.socket)==null?void 0:i.readyState)===1?(gc.wsTraffic({type:"send",raw:e}),this.socket.send(e)):this.messageQueue.push(e)}createWebSocket(){try{this.closeExistingSocket(),this.socket=new this.WebSocketConstructor(this.endpoint),this.setupWebSocketListeners(),this.startConnectionTimeout()}catch(e){const i=e instanceof Error?e:new Ii("Failed to create WebSocket");this._errors$.next(i),this.handleConnectionError()}}closeExistingSocket(){if(!this.socket)return;const e=this.socket;this.socket=void 0,this.removeWebSocketListeners(e);try{e.close()}catch{}}setupWebSocketListeners(){this.socket&&(this.socket.addEventListener("open",this.boundHandleOpen),this.socket.addEventListener("close",this.boundHandleClose),this.socket.addEventListener("error",this.boundHandleError),this.socket.addEventListener("message",this.boundHandleMessage))}removeWebSocketListeners(e){try{e.removeEventListener("open",this.boundHandleOpen),e.removeEventListener("close",this.boundHandleClose),e.removeEventListener("error",this.boundHandleError),e.removeEventListener("message",this.boundHandleMessage)}catch{}}handleOpen(){this.clearConnectionTimeout(),this._status$.next("connected"),this.currentReconnectDelay=this.reconnectDelayMin,this.flushMessageQueue()}handleClose(e){this.clearConnectionTimeout(),this.shouldReconnect?(this._status$.next("reconnecting"),this.scheduleReconnection()):this._status$.next("disconnected")}handleError(){const e=new $a("WebSocket connection error");this._errors$.next(e),this.handleConnectionError()}handleMessage(e){gc.wsTraffic({type:"recv",raw:e.data}),this._incomingMessages$.next(e)}handleConnectionError(){this.reconnect()}scheduleReconnection(){this.clearReconnectTimer();const e=this.currentReconnectDelay*(.5+Math.random()*.5);this.reconnectTimer=setTimeout(()=>{this.shouldReconnect&&(this._status$.next("connecting"),this.createWebSocket(),this.increaseReconnectDelay())},e)}increaseReconnectDelay(){this.currentReconnectDelay=Math.min(this.currentReconnectDelay*2,this.reconnectDelayMax)}startConnectionTimeout(){this.clearConnectionTimeout(),this.connectionTimeoutTimer=setTimeout(()=>{if(this._status$.value==="connecting"){const e=new _d("WebSocket connection timeout");this._errors$.next(e),this.socket&&this.socket.close()}},this.connectionTimeout)}clearConnectionTimeout(){this.connectionTimeoutTimer&&(clearTimeout(this.connectionTimeoutTimer),this.connectionTimeoutTimer=void 0)}clearReconnectTimer(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=void 0)}flushMessageQueue(){var e;for(;this.messageQueue.length>0&&((e=this.socket)==null?void 0:e.readyState)===1;){const i=this.messageQueue.shift();i!==void 0&&this.socket.send(i)}}},wt.DEFAULT_RECONNECT_DELAY_MIN_MS=1e3,wt.DEFAULT_RECONNECT_DELAY_MAX_MS=3e4,wt.DEFAULT_CONNECTION_TIMEOUT_MS=1e4,wt);function ev(t){return ns(t)&&t.method==="signalwire.ping"}const Pe=W();var tv=class extends ie{constructor(t,e,i,r,n){super(),this.storage=t,this.protocolKey=e,this.onError=n,this.protocol$=this.createReplaySubject(1),this.isConnecting=!1,this.isConnected=!1,this.ackEvent=()=>Se(s=>{if(as(s))try{Pe.debug("[Transport] Sending event ack",{eventId:s.id}),this.send(tg(s.id))}catch(a){Pe.error("[Transport] Failed to send event acknowledgment:",a)}}),this.replySignalwirePing=()=>B(s=>{if(ev(s)){try{Pe.debug("[Transport] Received ping, sending pong",{pingId:s.id}),this.send(Wp(s.id))}catch(a){Pe.error("[Transport] Failed to send ping response:",a)}return!1}return!0}),this.discardStaleEvents=()=>B(s=>{if(!as(s))return!0;const a=s.params.event_channel;if(!a)return!0;const o=this._currentProtocol;if(!o)return!0;if(!a.includes(o)){const c=s.params.event_type;return Pe.warn(`[Transport] Discarding stale event: ${c} (event_channel does not match current protocol)`),!1}return!0}),this._outgoingMessages$=this.createSubject(),this._webSocketConnections=new $f(i,r,this._outgoingMessages$.asObservable(),{connectionTimeout:g.instance.connectionTimeout,reconnectDelayMin:g.instance.reconnectDelayMin,reconnectDelayMax:g.instance.reconnectDelayMax}),this.subscribeTo(this._webSocketConnections.errors$,s=>{var a;(a=this.onError)==null||a.call(this,s)}),this.initialized$=ei(()=>Ie(this._init())).pipe(et(1),S(this.destroyed$)),this._jsonRPCMessage$=this._webSocketConnections.incomingMessages$.pipe(E(s=>{var a;try{return JSON.parse(s.data)}catch(o){return Pe.error("[Transport] Failed to parse incoming message:",o),(a=this.onError)==null||a.call(this,new Vd(o)),null}}),B(s=>s!==null&&(ss(s)||ns(s))),ti(s=>{var a;return Pe.error("[Transport] Message processing error:",s),(a=this.onError)==null||a.call(this,s instanceof Error?s:new Error(String(s),{cause:s})),$t}),kt(),S(this.destroyed$)),this._jsonRPCResponse$=this._jsonRPCMessage$.pipe(B(ss)),this._incomingEvent$=this._jsonRPCMessage$.pipe(this.ackEvent(),this.replySignalwirePing(),B(s=>!ss(s)),this.discardStaleEvents(),kt(),S(this.destroyed$))}async setProtocol(t){this._currentProtocol=t,this.protocol$.next(t),await this._updateProtocolInStorage(t)}get incomingEvent$(){return this._incomingEvent$}get connectionStatus$(){return this._webSocketConnections.status$}async connect(){return this.isConnecting||this.isConnected?(Pe.warn("[Transport] Already connecting or connected"),Promise.resolve()):new Promise((t,e)=>{this.isConnecting=!0,this.subscribeTo(this.initialized$,()=>{this._webSocketConnections.connect();const i=this._webSocketConnections.status$.pipe(B(r=>r==="connected"||r==="disconnected"),Me(1),or(1e4)).subscribe({next:r=>{var n;if(r==="connected")this.isConnecting=!1,this.isConnected=!0,Pe.debug("[Transport] Connection established"),t();else{this.isConnecting=!1;const s=new eo("Failed to connect");Pe.error("[Transport] Connection failed"),(n=this.onError)==null||n.call(this,s),e(s)}},error:r=>{var n;this.isConnecting=!1,Pe.error("[Transport] Connection error:",r),(n=this.onError)==null||n.call(this,r instanceof Error?r:new Error(String(r),{cause:r})),e(r)}});this.subscriptions.push(i),this.subscribeTo(this._webSocketConnections.status$.pipe(B(r=>r==="disconnected")),()=>{Pe.debug("[Transport] Disconnected"),this.isConnected=!1})})})}reconnect(){this._webSocketConnections.reconnect()}async execute(t,e){return this.send(t),new Lf(t,this._jsonRPCResponse$,e).promise}send(t){const e=JSON.stringify(t);this._outgoingMessages$.next(e)}disconnect(){Pe.debug("[Transport] Disconnecting"),this.isConnected=!1,this.isConnecting=!1,this._webSocketConnections.disconnect()}destroy(){Pe.debug("[Transport] Destroying"),this.disconnect(),super.destroy(),this._webSocketConnections.destroy()}async _loadProtocolFromStorage(){try{const t=await this.storage.getItem(this.protocolKey);this._currentProtocol=t??void 0,this.protocol$.next(t??void 0)}catch(t){throw Pe.error("Failed to retrieve protocol from storage:",t),t}}async _updateProtocolInStorage(t){if(!t){try{await this.storage.removeItem(this.protocolKey)}catch(e){throw Pe.error("Failed to remove protocol from storage:",e),e}return}try{const e=await this.storage.getItem(this.protocolKey);(!e||e!==t)&&await this.storage.setItem(this.protocolKey,t)}catch(e){throw Pe.error("Failed to update protocol in storage:",e),e}}async _init(){return await this._loadProtocolFromStorage(),!0}};const Y=W(),iv=t=>{if(typeof t=="string"){const e=t.indexOf("?");if(e!==-1){const i=t.substring(e+1),r=new URLSearchParams(i).get("channel");if(r==="video")return{audio:!0,video:!0,receiveVideo:!0};if(r==="audio")return{audio:!0,video:!1}}}return{}};var fc=class extends ie{constructor(t,e={}){super(),this.preferences=new vp,this._user$=this.createBehaviorSubject(void 0),this._directory$=this.createBehaviorSubject(void 0),this._isConnected$=this.createBehaviorSubject(!1),this._isRegistered$=this.createBehaviorSubject(!1),this._errors$=this.createReplaySubject(1),this._options={},this._deps=new wp,this._credentialProvider=t,this._options={...g.instance.defaultSignalWireOptions,...e},this._options.storageImplementation&&(this._deps.storageImpl=this._options.storageImplementation),this._options.persistSession&&(this._deps.persistSession=!0),this._options.webSocketConstructor&&(this._deps.WebSocket=this._options.webSocketConstructor),this._options.savePreferences&&this.preferences.enableSavePreferences(this._deps.storage),this._options.webRTCApiProvider&&(this._deps.webRTCApiProvider=this._options.webRTCApiProvider),this._options.logger!==void 0&&qd(this._options.logger),this._options.logLevel&&Yd(this._options.logLevel),this._options.debug&&Xd(this._options.debug),this._deviceController=this._deps.deviceController,this._options.skipDeviceMonitoring||this._deviceController.enableDeviceMonitoring(),this.subscribeTo(this._deviceController.errors$,i=>{this._errors$.next(i)}),this.initResilienceSubsystems(),this.resolveCredentials().then(()=>{this.init().catch(i=>{Y.error("[SignalWire] Initialization error:",i),this._deps.storage.removeItem("sw:cached_credential"),this._deps.storage.removeItem("sw:cached_credential","local"),this._errors$.next(i instanceof Error?i:new Error(String(i),{cause:i}))})}).catch(i=>{Y.error("[SignalWire] Initialization error:",i),this._errors$.next(i instanceof Error?i:new Error(String(i),{cause:i}))})}async initDPoP(){var t;if((t=this._dpopManager)!=null&&t.initialized)return this._dpopManager.fingerprint;try{this._dpopManager=new Ep;const e=await this._dpopManager.init();return Y.debug("[SignalWire] DPoP initialized, fingerprint available"),e}catch(e){Y.warn("[SignalWire] DPoP initialization failed, proceeding without DPoP:",e),this._dpopManager=void 0;return}}async resolveCredentials(){const t=await this.initDPoP();if(this._credentialProvider)return this.validateCredentials(this._credentialProvider,void 0,t);for(const e of this._deps.persistSession?["local","session"]:["session"])try{const i=await this._deps.storage.getItem("sw:cached_credential",e);if(i!=null&&i.token)return Y.debug(`[SignalWire] Using cached credential from ${e}Storage`),await this.validateCredentials(void 0,i)}catch{}throw new ii("No credential provider and no cached session. Provide a CredentialProvider or enable persistSession with a prior login.")}async validateCredentials(t,e,i){var s;const r=i??((s=this._dpopManager)!=null&&s.initialized?this._dpopManager.fingerprint:void 0),n=e??(t?await t.authenticate(r?{fingerprint:r}:void 0):void 0);if(!n)throw new ii("No credentials available.");if(n.token)try{const a=eh(n.token,{header:!0});this._deps.ch=a.ch}catch(a){throw Y.error("[SignalWire] Invalid JWT token provided in credentials:",a),new ii("Invalid JWT token provided in credentials.",{cause:a})}if(!n.token&&!n.authorizationState)throw Y.error("[SignalWire] No valid authentication credentials provided."),new ii("No valid authentication credentials provided.");if(!this._deps.persistSession&&n.expiry_at&&n.expiry_at<Date.now())throw Y.error("[SignalWire] Provided credentials have expired."),new ii("Provided credentials have expired.");if(n.expiry_at&&(t!=null&&t.refresh)&&this.scheduleCredentialRefresh(t,n.expiry_at),this._deps.credential=n,this.persistCredential(n),this.isConnected&&this._clientSession.authenticated&&n.token)try{await this._clientSession.reauthenticate(n.token),Y.info("[SignalWire] Session refreshed with new credentials.")}catch(a){Y.error("[SignalWire] Failed to refresh session with new credentials:",a),this._errors$.next(a instanceof Error?a:new Error(String(a),{cause:a}))}}scheduleCredentialRefresh(t,e,i=0){this._refreshTimerId!==void 0&&clearTimeout(this._refreshTimerId);const r=i===0?Math.max(e-Date.now()-kh,1e3):Math.min(Th*Math.pow(2,i)*(.5+Math.random()*.5),Eh);this._refreshTimerId=setTimeout(async()=>{try{if(!t.refresh)throw new ii("Credential provider does not support refresh");const n=await t.refresh();this._deps.credential=n,this.persistCredential(n),Y.info("[SignalWire] Credentials refreshed successfully."),n.expiry_at&&this.scheduleCredentialRefresh(t,n.expiry_at,0)}catch(n){const s=i+1;Y.error(`[SignalWire] Credential refresh failed (attempt ${s}/${mo}):`,n),this._errors$.next(n instanceof Error?n:new Error(String(n),{cause:n})),s<mo?this.scheduleCredentialRefresh(t,e,s):(Y.error("[SignalWire] Credential refresh exhausted all retries. Disconnecting."),this._errors$.next(new ri("Credential refresh failed after max retries")),this.disconnect())}},r)}persistCredential(t){t.token&&(this._deps.storage.setItem("sw:cached_credential",t),this._deps.persistSession&&this._deps.storage.setItem("sw:cached_credential",t,"local"))}async init(){if(this._user$.next(new zp(this._deps.http)),this._options.skipConnection||await this.connect(),!this._options.reconnectAttachedCalls&&this._attachManager&&await this._attachManager.flush(),!this._options.skipRegister)try{await this.register()}catch(t){Y.error("[SignalWire] Registration failed:",t),this._errors$.next(t instanceof Error?t:new Error(String(t),{cause:t}))}this.handleAttachments()}async handleAttachments(){if(!this._attachManager){Y.error("[SignalWire] AttachManager not initialized");return}if(this._options.reconnectAttachedCalls)try{await this._attachManager.reattachCalls()}catch(t){Y.error("[SignalWire] Failed to reattach calls:",t),this._errors$.next(t instanceof Error?t:new Error(String(t),{cause:t}))}}async connect(){var r,n,s;await this.teardownTransportAndSession();try{const a=this._user$.value;if(!a)throw new Ii("User not initialized before connect");if(!await Ge(a.fetched$))throw new Ii("Failed to fetch user information - fetched$ emitted false");this._deps.user=a}catch(a){throw Y.error(`[SignalWire] Failed to fetch user information: ${a instanceof Error?a.message:"Unknown error"}. This usually means the user token is invalid or expired.`),new Ii("Error fetching user information",{cause:a})}const t=a=>{this._errors$.next(a)};this._transport=new tv(this._deps.storage,this._deps.protocolKey,this._deps.WebSocket,g.instance.relayHost??this._deps.relayHost,t),this._attachManager=new ig(this._deps.storage,this._deps.deviceController,g.instance.reconnectCallsTimeout,this._deps.attachedCallsKey),this._clientSession=new Uf(()=>this._deps.credential,this._transport,this._deps.storage,this._deps.authorizationStateKey,this._deps.deviceController,this._attachManager,this._deps.webRTCApiProvider,this._dpopManager,(r=this._networkMonitor)==null?void 0:r.networkChange$),this._publicSession=new Nf(this._clientSession),this._clientSession.onBeforeReconnect=async()=>{var a;if(this._credentialProvider)try{const o=(a=this._dpopManager)!=null&&a.initialized?this._dpopManager.fingerprint:void 0;Y.debug("[SignalWire] Credential expired, refreshing before reconnect");const c=await this._credentialProvider.authenticate(o?{fingerprint:o}:void 0);this._deps.credential=c,Y.debug("[SignalWire] Credential refreshed successfully for reconnect")}catch(o){throw Y.error("[SignalWire] Failed to refresh credentials for reconnect:",o),this._errors$.next(o instanceof Error?o:new Error(String(o),{cause:o})),o}},this.subscribeTo(this._clientSession.errors$,a=>{this._errors$.next(a)}),await this._clientSession.connect(),(n=this._dpopManager)!=null&&n.initialized&&(this._refreshTimerId&&(clearTimeout(this._refreshTimerId),this._refreshTimerId=void 0,Y.debug("[SignalWire] Developer refresh disabled — Client Bound SAT activation starting")),this._deviceTokenManager=new Wf(this._dpopManager,this._deps.http,a=>this._errors$.next(a),()=>this._deps.credential),await this._deviceTokenManager.activate(this._deps.user,this._clientSession,a=>{this._deps.credential={...this._deps.credential,...a}})),this.subscribeTo(this._clientSession.authenticated$.pipe(Gr(1),B(Boolean)),async()=>{try{this._deviceTokenManager&&(await this._deviceTokenManager.activate(this._deps.user,this._clientSession,a=>{this._deps.credential={...this._deps.credential,...a}}),Y.debug("[SignalWire] Client Bound SAT re-activated after reconnect"))}catch(a){Y.error("[SignalWire] Client Bound SAT re-activation failed (non-fatal):",a),this._errors$.next(a instanceof Error?a:new Error(String(a),{cause:a}))}try{Y.debug("[SignalWire] Re-registering user after reconnect"),await this.register(),Y.debug("[SignalWire] User re-registered successfully after reconnect")}catch(a){Y.error("[SignalWire] Re-registration failed after reconnect:",a),this._errors$.next(a instanceof Error?a:new Error(String(a),{cause:a}))}});const e=new Hf(this._clientSession,this._deps.http,()=>this._deps.getUserFromAddressId(),t),i=new Jf(this._deps.http,this._clientSession,e,t);this._directory$.next(i),this._clientSession.setDirectory(i),this._isConnected$.next(!0),(s=this._diagnosticsCollector)==null||s.record("connection","connected"),this.subscribeTo(this._clientSession.authenticated$.pipe(Gr(2),B(Boolean)),()=>{var a;(a=this._diagnosticsCollector)==null||a.record("connection","reconnected")})}get user$(){return this.deferEmission(this._user$.asObservable())}get user(){return this._user$.value}get directory$(){return this.deferEmission(this._directory$.asObservable())}get directory(){return this._directory$.value}get isRegistered$(){return this.deferEmission(this._isRegistered$.asObservable())}get isRegistered(){return this._isRegistered$.value}get isConnected(){return this._isConnected$.value}get isConnected$(){return this.deferEmission(this._isConnected$.asObservable())}get ready$(){return this.publicCachedObservable("ready$",()=>this._isConnected$.pipe(Ke(t=>t?this._clientSession.authenticated$:Et(!1))))}get errors$(){return this.deferEmission(this._errors$.asObservable())}get platformCapabilities(){return this._platformCapabilities??(this._platformCapabilities=_o(this._options.webRTCApiProvider)),this._platformCapabilities}get deviceRecovered$(){return this.deferEmission(this._deviceController.deviceRecovered$)}exportDiagnostics(){const t={audioInput:this.audioInputDevices,audioOutput:this.audioOutputDevices,videoInput:this.videoInputDevices},e={sdkVersion:"unknown",userAgent:typeof navigator<"u"?navigator.userAgent:"unknown",capabilities:this.platformCapabilities,events:[],calls:[],deviceChanges:[],devices:t};if(!this._diagnosticsCollector)return e;const i=this._diagnosticsCollector.export();return{...e,sdkVersion:i.sdkVersion,userAgent:i.userAgent,events:i.events,calls:i.calls,deviceChanges:i.deviceChanges,devices:t}}initResilienceSubsystems(){try{this._platformCapabilities=_o(this._options.webRTCApiProvider)}catch(t){Y.warn("[SignalWire] Failed to detect platform capabilities:",t)}try{this._networkMonitor=new kp}catch(t){Y.warn("[SignalWire] Failed to initialize NetworkMonitor:",t)}try{this._visibilityController=new Np,this.subscribeTo(this._visibilityController.visibilityChange$.pipe(B(t=>t.to==="visible"&&g.instance.refreshDevicesOnVisible)),()=>{Y.debug("[SignalWire] Page visible, re-enumerating devices");try{this._deviceController.disableDeviceMonitoring(),this._deviceController.enableDeviceMonitoring()}catch{}})}catch(t){Y.warn("[SignalWire] Failed to initialize VisibilityController:",t)}try{this._diagnosticsCollector=new Xf({sdkVersion:"3.30.0"})}catch(t){Y.warn("[SignalWire] Failed to initialize DiagnosticsCollector:",t)}}async disconnect(){var t;this._refreshTimerId&&(clearTimeout(this._refreshTimerId),this._refreshTimerId=void 0),(t=this._diagnosticsCollector)==null||t.record("connection","disconnected"),await this.teardownTransportAndSession(),this._isConnected$.next(!1)}async teardownTransportAndSession(){const t=this._clientSession,e=this._transport;if(t){try{await t.disconnect()}catch(i){Y.warn("[SignalWire] Error disconnecting previous session:",i)}t.destroy()}e&&e.destroy(),this._clientSession=void 0,this._publicSession=void 0,this._transport=void 0,this._attachManager=void 0}async waitAuthentication(){await Ge(this.ready$.pipe(B(t=>t===!0)))}async register(){try{await this.waitAuthentication(),await this._transport.execute(is({method:"subscriber.online",params:{}})),this._isRegistered$.next(!0);return}catch(t){if(!this._deps.credential.token)throw this._errors$.next(t instanceof Error?t:new Error(String(t),{cause:t})),t;Y.debug("[SignalWire] Failed to register user, trying reauthentication...");try{await this._clientSession.reauthenticate(this._deps.credential.token),Y.debug("[SignalWire] Reauthentication successful, retrying register()"),await this._transport.execute(is({method:"subscriber.online",params:{}})),this._isRegistered$.next(!0)}catch(e){Y.error("[SignalWire] Reauthentication failed during register():",e);const i=new ii("Failed to register user, and reauthentication attempt also failed. Please check your credentials.",{cause:e instanceof Error?e:new Error(String(e),{cause:e})});throw this._errors$.next(i),i}}}async unregister(){try{await this._transport.execute(is({method:"subscriber.offline",params:{}})),this._isRegistered$.next(!1)}catch(t){throw Y.error("[SignalWire] Failed to unregister user:",t),this._errors$.next(t instanceof Error?t:new Error(String(t),{cause:t})),t}}async dial(t,e={}){const i={...g.instance.preferredMediaOptions,...iv(t),...e};return await this.waitAuthentication(),Y.debug("[SignalWire] Dialing with options:",i),this._clientSession.createOutboundCall(t,i)}async preflight(t,e){const i=this._clientSession.iceServers??g.instance.iceServers??[],r=this._isConnected$.value;return new Fp(this._deviceController,i,r,0,async(n,s)=>this.dial(n,s),e).run(t)}get session(){return this._publicSession}get audioInputDevices$(){return this.deferEmission(this._deviceController.audioInputDevices$)}get audioInputDevices(){return this._deviceController.audioInputDevices}get audioOutputDevices$(){return this.deferEmission(this._deviceController.audioOutputDevices$)}get audioOutputDevices(){return this._deviceController.audioOutputDevices}get videoInputDevices$(){return this.deferEmission(this._deviceController.videoInputDevices$)}get videoInputDevices(){return this._deviceController.videoInputDevices}get selectedAudioInputDevice$(){return this.deferEmission(this._deviceController.selectedAudioInputDevice$)}get selectedAudioOutputDevice$(){return this.deferEmission(this._deviceController.selectedAudioOutputDevice$)}get selectedVideoInputDevice$(){return this.deferEmission(this._deviceController.selectedVideoInputDevice$)}get selectedAudioInputDevice(){return this._deviceController.selectedAudioInputDevice}get selectedAudioOutputDevice(){return this._deviceController.selectedAudioOutputDevice}get selectedVideoInputDevice(){return this._deviceController.selectedVideoInputDevice}get selectedAudioInputDeviceConstraints(){return this._deviceController.selectedAudioInputDeviceConstraints}get selectedVideoInputDeviceConstraints(){return this._deviceController.selectedVideoInputDeviceConstraints}deviceInfoToConstraints(t){return this._deviceController.deviceInfoToConstraints(t)}selectAudioInputDevice(t){this._deviceController.selectAudioInputDevice(t)}selectVideoInputDevice(t){this._deviceController.selectVideoInputDevice(t)}selectAudioOutputDevice(t){this._deviceController.selectAudioOutputDevice(t)}async applySelectedAudioOutputDevice(t){const e=this._deviceController.selectedAudioOutputDevice;if(!(e!=null&&e.deviceId))return!1;const i=t;if(typeof i.setSinkId!="function")return Y.warn("[SignalWire] setSinkId not supported on this element / browser"),!1;try{return await i.setSinkId(e.deviceId),!0}catch(r){return Y.warn("[SignalWire] Failed to apply audio output device:",r),!1}}enableDeviceMonitoring(){this._deviceController.enableDeviceMonitoring()}disableDeviceMonitoring(){this._deviceController.disableDeviceMonitoring()}async getDeviceCapabilities(t){return this._deviceController.getDeviceCapabilities(t)}async isValidDevice(t){return this._deviceController.isValidDevice(t)}setStorageManager(t){this._deviceController.setStorageManager(t)}async clearDeviceState(){return this._deviceController.clearDeviceState()}async enumerateDevices(){return this._deviceController.enumerateDevices()}disableAudioInput(){this._deviceController.disableAudioInput()}enableAudioInput(){this._deviceController.enableAudioInput()}disableVideoInput(){this._deviceController.disableVideoInput()}enableVideoInput(){this._deviceController.enableVideoInput()}get videoInputDisabled$(){return this.deferEmission(this._deviceController.videoInputDisabled$)}get audioInputDisabled$(){return this.deferEmission(this._deviceController.audioInputDisabled$)}get videoInputDisabled(){return this._deviceController.videoInputDisabled}get audioInputDisabled(){return this._deviceController.audioInputDisabled}async requestMediaPermissions(t={audio:!0,video:!0}){const e={audio:t.audio??!1,video:t.video??!1};let i=!1,r=!1,n,s;try{const a=(await this._deps.webRTCApiProvider.mediaDevices.getUserMedia(e)).getTracks();for(const o of a){const c=o.getSettings();o.kind==="audio"?(i=!0,c.deviceId&&(n=this.audioInputDevices.find(u=>u.deviceId===c.deviceId))):o.kind==="video"&&(r=!0,c.deviceId&&(s=this.videoInputDevices.find(u=>u.deviceId===c.deviceId))),o.stop()}}catch(a){Y.warn("[SignalWire] Media permission request failed:",a)}if(await this._deviceController.enumerateDevices(),i&&n){const a=n.deviceId;n=this.audioInputDevices.find(o=>o.deviceId===a)??n}if(r&&s){const a=s.deviceId;s=this.videoInputDevices.find(o=>o.deviceId===a)??s}return i&&n&&!this.selectedAudioInputDevice&&this.selectAudioInputDevice(n),r&&s&&!this.selectedVideoInputDevice&&this.selectVideoInputDevice(s),{audio:i,video:r,selectedAudioDevice:n,selectedVideoDevice:s}}async resetToDefaults(){await this._deps.storage.clearAll();const t=g.instance;t.preferredAudioInput=null,t.preferredAudioOutput=null,t.preferredVideoInput=null,await this._deviceController.clearDeviceState()}destroy(){var t,e,i,r,n;this._refreshTimerId&&(clearTimeout(this._refreshTimerId),this._refreshTimerId=void 0),(t=this._deviceTokenManager)==null||t.destroy(),(e=this._dpopManager)==null||e.destroy(),this._attachManager&&this._attachManager.detachAll(),this._transport.destroy(),this._clientSession.destroy();try{(i=this._networkMonitor)==null||i.destroy()}catch{}try{(r=this._visibilityController)==null||r.destroy()}catch{}try{(n=this._diagnosticsCollector)==null||n.destroy()}catch{}this._networkMonitor=void 0,this._visibilityController=void 0,this._diagnosticsCollector=void 0,super.destroy()}};const rv=W();var vc=class{constructor(t,e){this.host=t,this.embedToken=e}async fetchSAT(){const t=`https://${this.host}/api/fabric/embeds/tokens`,e=1e4,i=new AbortController,r=setTimeout(()=>i.abort(),e);try{const n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:this.embedToken}),signal:i.signal});if(clearTimeout(r),n.ok)return(await n.json()).token;throw new Ld(`Failed to fetch SAT using embed token: ${n.status} ${n.statusText}`)}catch(n){throw clearTimeout(r),n instanceof Error&&n.name==="AbortError"?new to(`Request timeout after ${e}ms`,{cause:n}):(rv.error("[EmbedCredentialProvider] Request failed:",n),n)}}async authenticate(){return{token:await this.fetchSAT(),expiry_at:Date.now()+3600*1e3}}async refresh(){return this.authenticate()}},bc=class{constructor(t){this.credentials=t}async authenticate(){return Promise.resolve(this.credentials)}};(()=>{if(typeof window<"u"){const t=new CustomEvent("signalwire:js:ready",{detail:{version:"3.30.0"}});window.dispatchEvent(t)}})();var nv=Object.defineProperty,sv=Object.getOwnPropertyDescriptor,hr=(t,e,i,r)=>{for(var n=r>1?void 0:r?sv(e,i):e,s=t.length-1,a;s>=0;s--)(a=t[s])&&(n=(r?a(e,i,n):a(n))||n);return r&&n&&nv(e,i,n),n};const av=W();l.SwCallMedia=class extends Z{constructor(){super(...arguments),this.stream=null,this._remoteStreamValue=null,this._lastTrackSignature="",this._subscriptions=[]}connectedCallback(){super.connectedCallback(),!this.stream&&this.call&&this._setupDirectSubscriptions(),this.stream&&(this._remoteStreamValue=this.stream,this._lastTrackSignature=this._computeTrackSignature(this.stream))}updated(e){var i,r,n,s,a;if(super.updated(e),e.has("call")&&(this._cleanupDirectSubscriptions(),!this.stream&&this.call&&this._setupDirectSubscriptions()),e.has("stream")){this._cleanupDirectSubscriptions();const o=this.stream,c=this._computeTrackSignature(o);if(o!==this._remoteStreamValue||c!==this._lastTrackSignature){this._remoteStreamValue=o,this._lastTrackSignature=c;const u=(i=this.shadowRoot)==null?void 0:i.querySelector("video.mcu-video");u&&Fr(u,o)}}if(!this.stream&&!this.call&&e.has("_callState")){const o=((r=this._callState)==null?void 0:r.remoteStream)??null,c=this._computeTrackSignature(o);if(o!==this._remoteStreamValue||c!==this._lastTrackSignature){this._remoteStreamValue=o,this._lastTrackSignature=c;const u=(n=this.shadowRoot)==null?void 0:n.querySelector("video.mcu-video");u&&Fr(u,o)}}e.has("_devicesState")&&this._applySinkId(((a=(s=this._devicesState)==null?void 0:s.selectedAudioOutput)==null?void 0:a.deviceId)??"")}disconnectedCallback(){super.disconnectedCallback(),this._cleanupDirectSubscriptions(),this._cleanupResizeObserver(),this._cleanupVideoElement()}firstUpdated(){var i;const e=(i=this.shadowRoot)==null?void 0:i.querySelector("video.mcu-video");e&&vu(e).then(()=>{this.isConnected&&this._setupResizeObserver()})}_setupDirectSubscriptions(){this.call&&this._subscriptions.push(this.call.remoteStream$.subscribe(e=>{var n;const i=this._computeTrackSignature(e);if(e===this._remoteStreamValue&&i===this._lastTrackSignature)return;this._remoteStreamValue=e,this._lastTrackSignature=i,this.requestUpdate();const r=(n=this.shadowRoot)==null?void 0:n.querySelector("video.mcu-video");r&&Fr(r,e)}))}_computeTrackSignature(e){return e?e.getTracks().map(i=>`${i.kind}:${i.id}`).sort().join("|"):""}_cleanupDirectSubscriptions(){this._subscriptions.forEach(e=>e.unsubscribe()),this._subscriptions=[]}_applySinkId(e){var r;const i=(r=this.shadowRoot)==null?void 0:r.querySelector("video.mcu-video");i!=null&&i.setSinkId&&i.setSinkId(e).catch(n=>{av.error("[SwCallMedia] Failed to set audio output device:",n)})}_setupResizeObserver(){var r,n;const e=(r=this.shadowRoot)==null?void 0:r.querySelector("video.mcu-video"),i=(n=this.shadowRoot)==null?void 0:n.querySelector(".padding-wrapper");!e||!i||(this._videoElement=e,this._paddingWrapper=i,this._resizeObserver=new ResizeObserver(gu(()=>this._recalculateDimensions(),50)),this._resizeObserver.observe(this),this._videoResizeHandler=()=>this._recalculateDimensions(),e.addEventListener("resize",this._videoResizeHandler),this._windowResizeHandler=()=>{requestAnimationFrame(()=>this._recalculateDimensions())},window.addEventListener("resize",this._windowResizeHandler),this._recalculateDimensions())}_recalculateDimensions(){const e=this._videoElement,i=this._paddingWrapper;if(!e||!i)return;if(!e.videoWidth||!e.videoHeight){i.style.width="100%",i.style.height="100%",i.style.paddingBottom="0",i.style.transform="none";return}const r=window.innerWidth,n=window.innerHeight,s=this.getBoundingClientRect(),a=s.width,o=s.height;if(a<=0||o<=0)return;const c=Math.min(1,(r-s.left)/a),u=Math.min(1,(n-s.top)/o),h=Math.min(c,u),p=a*h,f=o*h;if(p<=0||f<=0)return;const m=e.videoWidth/e.videoHeight,R=p/f;let y,I;m>R?(y=p,I=y/m):(I=f,y=I*m);const x=Math.max(0,s.left),v=Math.max(0,s.top),b=Math.min(r,s.right),w=Math.min(n,s.bottom),A=(x+b)/2,D=(v+w)/2,F=s.left+a/2,K=s.top+o/2,_=A-F,te=D-K;i.style.width=`${y}px`,i.style.height=`${I}px`,i.style.paddingBottom="0",i.style.transform=`translate(${_}px, ${te}px)`}_cleanupResizeObserver(){var i;this._resizeObserver&&(this._resizeObserver.disconnect(),this._resizeObserver=void 0);const e=(i=this.shadowRoot)==null?void 0:i.querySelector("video.mcu-video");e&&this._videoResizeHandler&&(e.removeEventListener("resize",this._videoResizeHandler),this._videoResizeHandler=void 0),this._windowResizeHandler&&(window.removeEventListener("resize",this._windowResizeHandler),this._windowResizeHandler=void 0)}_cleanupVideoElement(){var i;const e=(i=this.shadowRoot)==null?void 0:i.querySelector("video.mcu-video");e&&ca(e)}render(){return k`
74
74
  <div class="mcu-content" part="container">
75
75
  <div class="padding-wrapper">