@veltdev/sdk
Version:
Velt is an SDK to add collaborative features to your product within minutes. Example: Comments like Figma, Frame.io, Google docs or sheets, Recording like Loom, Huddles like Slack and much more.
1 lines • 191 kB
JavaScript
import{a as cn}from"./chunk-IN7LJXNX.js";import{a as ln}from"./chunk-FHPKMTSO.js";import{c as _t,d as ft}from"./chunk-KEET32BV.js";import{a as sn}from"./chunk-BWEYL3QS.js";import{a as dn}from"./chunk-6YXU2Y3T.js";import{a as gn}from"./chunk-FBBI2VKM.js";import"./chunk-O76AOF76.js";import{b as It,c as wt,g as Mt,l as Et,m as Ot,s as Dt,u as Ut,v as Lt,w as Bt}from"./chunk-U4YN7DB7.js";import{b as Q,c as Te,d as pe}from"./chunk-WTYACTR5.js";import{a as an}from"./chunk-6E3N35RF.js";import{a as on}from"./chunk-KEM6PWSD.js";import{a as xt}from"./chunk-UJRDSNTY.js";import{A as yt,G as xe,H as ke,I as bt,Y as Pt,ba as Vt,ea as nn,ga as rn}from"./chunk-KU45CEXD.js";import{$ as Se,Aa as tt,Ca as fe,Cf as jt,Db as ot,E as te,Eb as at,Ec as ht,Ed as kt,Ee as M,Fa as d,Fb as lt,Fc as ge,G as qe,Ga as X,Gc as J,Gh as Jt,Hc as vt,Hh as Yt,Hi as en,Ib as F,Ic as Ct,Ii as tn,J as he,Jb as ye,Kb as q,L as Be,Lb as st,Nb as A,Nf as Ne,Ob as N,Od as Ht,Of as He,Pa as I,Pb as ie,Pc as St,Qb as be,Sb as dt,T as Y,Ta as nt,Tb as ct,Td as Tt,Te as Nt,Ua as G,Ub as gt,Ud as At,Ue as Rt,Va as it,Vg as we,W as Xe,Ya as rt,Yb as ut,Zb as pt,_a as _,a as p,aa as Qe,ac as ve,b as m,be as Ft,bf as $t,bi as Me,ca as B,ci as le,da as et,db as K,dc as mt,di as w,ei as Ee,g as y,ha as v,hi as Kt,i as Je,ia as C,ie as Fe,ja as k,ji as qt,ka as re,kc as b,l as L,lb as g,lc as x,li as ue,mb as l,mi as se,nb as u,ob as S,oe as ae,pc as Z,q as Ye,qa as W,qg as Ie,r as P,sb as D,sh as Wt,tb as U,ti as Xt,ua as ne,v as z,vb as E,vh as V,w as Ke,wa as _e,wg as zt,wh as Gt,xb as f,yh as Zt,yi as Qt,zb as s,zi as Oe}from"./chunk-GR3NESUB.js";var Ae=(()=>{let o=class o{constructor(e,t,i){this.coreActionsService=e,this.loggingService=t,this.usersService=i}getUserIdsToResolveFromHuddleAttendees(e){try{this.loggingService.log("%c[WB] Calling HuddleUtilsService FUNCTION: getUserIdsToResolveFromHuddleAttendees","color: green;");let t=new Set;for(let i of e)i?.userId&&t.add(i?.userId);return Array.from(t)}catch(t){return this.loggingService.catch("Error in HuddleUtilsService getUserIdsToResolveFromHuddleAttendees:",t),[]}}mergeHuddleAttendeeWithUser(e){try{if(this.loggingService.log("%c[WB] Calling HuddleUtilsService FUNCTION: mergeHuddleAttendeeWithUser","color: green;"),e?.userId){let t=this.usersService.getUserById(e?.userId);t&&(e=p(p({},e),t))}return e}catch(t){return this.loggingService.catch("Error in HuddleUtilsService mergeHuddleAttendeeWithUser:",t),e}}formatHuddleAttendeeData(e){try{if(this.usersService.userDataProviderAvailable()){let t=["userId","userSnippylyId","state","timestamp","initialHuddleMode","streamMetadata","initialHuddleType","huddleOnCursorMode"];Object.keys(e).forEach(i=>{t.includes(i)||(e[i]=null)})}return e}catch(t){return this.loggingService.catch("Error in HuddleUtilsService formatHuddleAttendeeData:",t),e}}getUserIdsToResolveFromHuddleMessages(e){try{this.loggingService.log("%c[WB] Calling HuddleUtilsService FUNCTION: getUserIdsToResolveFromHuddleMessages","color: green;");let t=new Set;for(let i of e)i?.from?.userId&&t.add(i?.from?.userId);return Array.from(t)}catch(t){return this.loggingService.catch("Error in HuddleUtilsService getUserIdsToResolveFromHuddleMessages:",t),[]}}mergeHuddleMessageWithUser(e){try{if(this.loggingService.log("%c[WB] Calling HuddleUtilsService FUNCTION: mergeHuddleMessageWithUser","color: green;"),e?.from?.userId){let t=this.usersService.getUserById(e?.from?.userId);t&&(e.from=t)}return e}catch(t){return this.loggingService.catch("Error in HuddleUtilsService mergeHuddleMessageWithUser:",t),e}}resolveUsersFromHuddleMessages(e){return y(this,null,function*(){try{if(this.loggingService.log("%c[WB] Calling HuddleUtilsService FUNCTION: resolveUsersFromHuddleMessages","color: green;"),this.usersService.userDataProviderAvailable()){let t=this.getUserIdsToResolveFromHuddleMessages(e),i=He(),a=Rt.GET_HUDDLE_USERS;this.coreActionsService.triggerAction(Ft.USER_RESOLVER,{event:V.Events.Resolver.USER_RESOLUTION_REQUEST_FORMED,timestamp:new Date().getTime(),uniqueId:i,methodName:Nt.RESOLVE_USERS_FROM_HUDDLE_MESSAGES,moduleName:a,source:"internal",payload:{userIds:t}});let c=yield this.usersService.resolveUsers({userIds:t,uniqueId:i,moduleName:a});for(let h of e)h?.from?.userId&&c[h.from.userId]&&(h.from=c[h.from.userId])}return e}catch(t){return this.loggingService.catch("Error in HuddleUtilsService resolveUsersFromHuddleMessages:",t),e}})}formatHuddleMessageData(e){try{return this.loggingService.log("%c[WB] Calling HuddleUtilsService FUNCTION: formatHuddleMessageData","color: green;"),this.usersService.userDataProviderAvailable()&&e?.from?.userId&&(e.from={userId:e.from.userId}),e}catch(t){return this.loggingService.catch("Error in HuddleUtilsService formatHuddleMessageData:",t),e}}};o.\u0275fac=function(t){return new(t||o)(B(qt),B(w),B(Oe))},o.\u0275prov=Se({token:o,factory:o.\u0275fac,providedIn:"root"});let r=o;return r})();var R=(()=>{let o=class o{constructor(e,t,i,a,c,h,H,$,T,ee,mn,hn,vn,Cn){this.afAuth=e,this.analyticsService=t,this.authService=i,this.databaseService=a,this.docService=c,this.configService=h,this.domService=H,this.huddleUtilsService=$,this.functions=T,this.loggingService=ee,this.commonDbService=mn,this.usersService=hn,this.serviceBook=vn,this.realtimeAdapter=Cn,this.endpointsService=et(Kt),this.documentPaths=null,this.localStream$=new L(null),this.peerConnectionMap={},this.localStream=new MediaStream,this.screenSharingStream=new MediaStream,this.screenSharingStream$=new L(null),this.remoteStreamMap={},this.remoteStreamMap$=new L(null),this.localStreamState$=new L({}),this.peerConnectionStateMap={},this.peerConnectionStateMap$=new L({}),this.hudleToolComponentId$=new L(null),this.attendees$=new L([]),this.invitees$=new L([]),this.huddleJoined$=new L(!1),this.userHuddleStartTime$=new L(null),this.messages$=new L([]),this.messagesPanelVisible$=new L(!1),this.huddleOnCursorMode$=new L(!1),this.flockModeOnAvatarClickEnabled$=new L(!1),this.chatEnabled$=new L(!0),this.serverFallback$=new L(!0),this.huddleOnCursorModeByAttendeeId$=new L({}),this.huddleCursorAvailableByAttendeeId$=new L({}),this.remoteStreamsByUserId$=new L({}),this.messagesSubscription=null,this.huddleJoined=!1,this.onAnswerConnectionIdHistory={},this.onOfferUidHistory={},this.senders={},this.db=null,this.dbRefsRemoveOnDropCall=[],this.servers={iceServers:[{urls:["stun:stun1.l.google.com:19302","stun:stun2.l.google.com:19302"]}]},this.databaseService.getDb().pipe(te(O=>!!O),he(1)).subscribe(O=>{try{O&&(this.db=O,this.isHuddleTagInstalled()&&this.getServerFallback()&&this.getTwilioIceServers(),this.getUser(),this.getDocumentPaths())}catch(j){this.loggingService.catch("Error in HuddleService getDb:",j)}},O=>{this.loggingService.catch("Error in HuddleService getDb subscription: ",O)}),this.docService.addPreDocumentIdChangeFunction("huddle-service",O=>y(this,null,function*(){this.huddleJoined$.value&&(yield this.dropCall(),yield this.removeLocalStream(),yield this.removeAttendee(),this.attendees$.next([]))})),this.getAttendees().subscribe(),this.getInvitees().subscribe(),this.localStream$.subscribe(O=>{O&&(this.localStream=O)}),this.screenSharingStream$.subscribe(O=>{O&&(this.screenSharingStream=O)}),this.configService.getConfig$().subscribe(O=>{try{!O?.featureAllowList?.find(ce=>ce===ae.CURSOR)&&this.huddleOnCursorMode$.next(!1)}catch(j){this.loggingService.catch("Error in HuddleService configService.getConfig$:",j)}},O=>{this.loggingService.catch("Error in HuddleService configService.getConfig$ subscription: ",O)}),this.attendees$.subscribe(O=>{try{if(O?.length){if(this.huddleOnCursorMode$?.value){let j=this.huddleOnCursorModeByAttendeeId$.getValue()||{};O.forEach(ce=>{j[ce.userSnippylyId]=!0}),this.huddleOnCursorModeByAttendeeId$.next(j)}this.huddleJoined$?.value&&(O?.find(j=>j?.huddleOnCursorMode)?this.addCursorTag():this.removeCursorTag())}}catch(j){this.loggingService.catch("Error in HuddleService attendees$:",j)}},O=>{this.loggingService.catch("Error in HuddleService attendees$ subscription: ",O)}),Ke([this.huddleJoined$,this.huddleOnCursorMode$]).subscribe(([O,j])=>{try{if(O){if(j){let ce=this.attendees$.getValue()||[],Sn=this.huddleOnCursorModeByAttendeeId$.getValue()||{};ce.forEach(_n=>{Sn[_n.userSnippylyId]=!0}),document?.body?.setAttribute(M.ATTRIBUTES.VELT_HUDDLE_ON_CURSOR_MODE,"true")}else this.huddleOnCursorModeByAttendeeId$.next({}),document?.body?.removeAttribute(M.ATTRIBUTES.VELT_HUDDLE_ON_CURSOR_MODE);this.updateHuddleOnCursorMode()}else this.removeCursorTag(),document?.body?.removeAttribute(M.ATTRIBUTES.VELT_HUDDLE_ON_CURSOR_MODE)}catch(ce){this.loggingService.catch("Error in HuddleService huddleJoined$:",ce)}},O=>{this.loggingService.catch("Error in HuddleService huddleJoined$ subscription: ",O)}),this.huddleOnCursorModeByAttendeeId$.subscribe(O=>{try{this.huddleJoined&&(Object.keys(O).length?this.addCursorTag():this.removeCursorTag())}catch(j){this.loggingService.catch("Error in HuddleService huddleOnCursorModeByAttendeeId$:",j)}},O=>{this.loggingService.catch("Error in HuddleService huddleOnCursorModeByAttendeeId$ subscription: ",O)}),this.authService.addPreSignOutFuction("huddle-signout",this.signOutUser.bind(this))}isHuddleTagInstalled(){try{return!!document?.querySelector(`${M.TAGS.VELT_HUDDLE}`)||!!document?.querySelector(`${M.TAGS.SNIPPYLY_HUDDLE}`)}catch(e){return this.loggingService.catch("Error in HuddleService isHuddleTagInstalled:",e),!1}}getTwilioIceServers(){return y(this,null,function*(){try{let t=(yield Gt(this.functions,Wt({url:this.endpointsService.getCloudFunctionUrl("getIceServers"),apiProxyDomain:this.configService?.getApiProxyDomain()}),{timeout:12e5})())?.data;t&&t?.iceServers&&(this.servers.iceServers=t.iceServers)}catch(e){this.loggingService.catch("Error in HuddleService getTwilioIceServers:",e)}})}getRemoteStream(){return this.loggingService.log("%c[WB] Calling FUNCTION: getRemoteStream","color: green;"),this.remoteStreamMap$.asObservable()}getPeerConnectionStateMap(){return this.loggingService.log("%c[WB] Calling FUNCTION: getPeerConnectionStateMap","color: green;"),this.peerConnectionStateMap$.asObservable()}getLocalStreamState(){return this.loggingService.log("%c[WB] Calling FUNCTION: getLocalStreamState","color: green;"),this.localStreamState$.asObservable()}getScreenSharingStream(){return this.loggingService.log("%c[WB] Calling FUNCTION: getScreenSharingStream","color: green;"),this.screenSharingStream$.asObservable()}getAttendees(){return this.authService.getAuthState$().pipe(te(e=>!!e),z(e=>({firebaseUser:e})),Y(e=>this.configService.getConfig$().pipe(te(t=>!!t),z(t=>m(p({},e),{config:t})))),Y(e=>this.authService.getUser$().pipe(z(t=>m(p({},e),{user:t})))),Y(e=>Ne(e.config,e.user,ae.HUDDLE)?P(m(p({},e),{allowed:!0})):(this.loggingService.warn("Huddle feature is not allowed."),P(m(p({},e),{allowed:!1})))),Be((e,t)=>e?.allowed===t?.allowed),Y(e=>this.docService.getDocumentPaths$().pipe(z(t=>(this.documentPaths=t,e)))),Y(e=>e?.allowed?e?.user?.isAnonymous?P(null):this.documentPaths?.huddle?this.commonDbService.dbListener({feature:"allHuddleAttendees",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,skipFirestore:!0}}).pipe(z(t=>t?.data),z(t=>t?(this.attendees$.next(Object.values(t)),Object.values(t)):(this.attendees$.next([]),null))):P(null):P([])))}getInvitees(){try{return this.authService.getAuthState$().pipe(te(e=>!!e),z(e=>({firebaseUser:e})),Y(e=>this.configService.getConfig$().pipe(te(t=>!!t),z(t=>m(p({},e),{config:t})))),Y(e=>this.authService.getUser$().pipe(z(t=>m(p({},e),{user:t})))),Y(e=>Ne(e.config,e.user,ae.HUDDLE)?P(m(p({},e),{allowed:!0})):(this.loggingService.warn("Huddle feature is not allowed."),P(m(p({},e),{allowed:!1})))),Be((e,t)=>e?.allowed===t?.allowed),Y(e=>this.docService.getDocumentPaths$().pipe(z(t=>(this.documentPaths=t,e)))),Y(e=>e?.allowed?e?.user?.isAnonymous?P(null):this.documentPaths?.huddle?this.commonDbService.dbListener({feature:"allHuddleInvitees",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,skipFirestore:!0}}).pipe(z(t=>t?.data),z(t=>t?(this.invitees$.next(Object.values(t)),Object.values(t)):(this.invitees$.next([]),null))):P(null):P([])))}catch(e){return this.loggingService.catch("Error in HuddleService getInvitees:",e),P([])}}clearHuddleInvitees(){try{this.documentPaths?.huddle&&this.commonDbService.deleteData({feature:"allHuddleInvitees",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,skipFirestore:!0}})}catch(e){this.loggingService.catch("Error in HuddleService clearHuddleInvitees:",e)}}getUser(){this.loggingService.log("%c[WB] Calling FUNCTION: getUser","color: green;"),this.authService.getUser$().pipe(te(e=>!!e)).subscribe(e=>{e&&(this.user=e)},e=>{this.loggingService.catch("Error in getUser: ",e)})}getDocumentPaths(){this.loggingService.log("%c[WB] Calling FUNCTION: getDocumentPaths","color: green;"),this.docService.getDocumentPaths$().pipe(Xe(e=>{this.documentPaths=e})).subscribe(()=>{},e=>{this.loggingService.catch("Error in getDocumentPaths: ",e)})}setHuddleToolComponentId(e){this.hudleToolComponentId$.next(e||null)}getHuddleToolComponentId(){return this.hudleToolComponentId$.asObservable()}joinHuddle(e){return y(this,null,function*(){try{this.loggingService.log("%c[WB] Calling FUNCTION: joinHuddle","color: green;"),this.documentPaths?.huddle?(this.initialHuddleType=e,this.huddleJoined=!0,this.huddleJoined$.next(!0),this.initLocalStreams(),this.huddle=this.realtimeAdapter.rtdbRef(this.db,this.documentPaths?.huddle),this.iceCandidates=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/iceCandidates`),this.connections=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/connections`),this.attendees=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/attendees`),this.messages=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/messages`),this.setHuddleListener(),this.addAttendee(),this.callExistingAttendees(),this.userHuddleStartTime$.next(new Date().getTime())):this.loggingService.catch("Error in joinHuddle: documentPaths not found")}catch(t){this.loggingService.catch("Error in HuddleService joinHuddle:",t)}})}initLocalStreams(){try{if(!this.localStream$?.value?.getTracks()?.length){let e=new MediaStream([this.silence(),this.black()]);this.localStream$.next(e)}if(!this.screenSharingStream$?.value?.getTracks()?.length){let e=new MediaStream([this.black()]);this.screenSharingStream$.next(e)}}catch(e){this.loggingService.catch("Error in HuddleService initLocalStreams:",e)}}setHuddleListener(){this.loggingService.log("%c[WB] Calling FUNCTION: setHuddleListener","color: green;"),this.realtimeAdapter.rtdbOnValue("huddle",this.connections,e=>{try{if(e.exists()){let t=e.val();Object.values(t).forEach(a=>{let c=Object.values(a);if(c&&c.length>0){let H=c[0]?.connectionId;this.onHuddleSnapshot(H,a)}}),Object.keys(this.remoteStreamMap).forEach(a=>y(this,null,function*(){t[a]||(this.remoteStreamMap[a]&&Object.values(this.remoteStreamMap[a]).forEach(c=>{c.getTracks().forEach(h=>{h.stop()})}),delete this.remoteStreamMap[a],yield this.removeIceCandidates(a))})),this.remoteStreamMap$.next(this.remoteStreamMap)}else Object.keys(this.remoteStreamMap).forEach(t=>y(this,null,function*(){Object.values(this.remoteStreamMap[t]).forEach(i=>{i.getTracks().forEach(a=>{a.stop()})}),yield this.removeIceCandidates(t)})),this.remoteStreamMap={},this.remoteStreamMap$.next(this.remoteStreamMap)}catch(t){this.loggingService.catch("Error in HuddleService setHuddleLister onValue:",t)}}),this.realtimeAdapter.rtdbStateChanges$("huddle",this.iceCandidates).subscribe(e=>{try{e.event==="child_added"&&this.onIceCandidateSnapshot(e.snapshot)}catch(t){this.loggingService.catch("Error in HuddleService iceCandidates stateChanges:",t)}}),this.realtimeAdapter.rtdbStateChanges$("huddle",this.attendees).subscribe(e=>{try{if(e.event==="child_removed"){let t=e?.snapshot?.val();if(t){delete this.onOfferUidHistory[t?.userSnippylyId];let i=this.getCombinedId(this.user.userSnippylyId,t?.userSnippylyId);delete this.onAnswerConnectionIdHistory[i],this.closeConnection(i)}}}catch(t){this.loggingService.catch("Error in HuddleService iceCandidates stateChanges:",t)}}),this.messagesSubscription&&this.messagesSubscription.unsubscribe(),this.messagesSubscription=new Je(e=>{let t=this.realtimeAdapter.rtdbOnValue("huddle",this.messages,i=>e.next(i),i=>e.error(i));return()=>t()}).pipe(Y(e=>this.usersService.getUserById$().pipe(z(()=>e))),Y(e=>{if(e.exists()){let t=e.val();return Ye(this.huddleUtilsService.resolveUsersFromHuddleMessages(Object.values(t||{})))}return P([])}),qe(e=>(this.loggingService.catch("Error in HuddleService messages onValue:",e),P([])))).subscribe(e=>{this.messages$.next(e)})}onHuddleSnapshot(e,t){try{this.loggingService.log("%c[WB] Calling FUNCTION: onHuddleSnapshot","color: green;"),t.offerData&&!t.answerData&&t.offerData.from!==this.user.userSnippylyId&&t.offerData.to===this.user.userSnippylyId&&!(e in this.peerConnectionMap)&&!this.onOfferUidHistory[t.offerData.from]?(this.loggingService.log("offer received"),this.loggingService.log(t),this.onOfferUidHistory[t.offerData.from]=!0,this.onOffer(e,t.offerData)):t.answerData&&t.answerData.from!==this.user.userSnippylyId&&t.answerData.to===this.user.userSnippylyId&&this.peerConnectionMap[e]&&this.peerConnectionMap[e]?.connectionState!=="connected"&&!this.onAnswerConnectionIdHistory[e]&&(this.loggingService.log("answer received"),this.loggingService.log(t),this.onAnswerConnectionIdHistory[e]=!0,this.onAnswer(e,t.answerData))}catch(i){this.loggingService.catch("Error in HuddleService onHuddleSnapshot:",i)}}onIceCandidateSnapshot(e){try{this.loggingService.log("%c[WB] Calling FUNCTION: onIceCandidateSnapshot","color: green;"),this.loggingService.log("ice candidate received");let t=e.val(),i=t.connectionId;t?.from!==this.user.userSnippylyId&&i in this.peerConnectionMap&&this.onIceCandidateChange(i,t)}catch(t){this.loggingService.catch("Error in HuddleService onIceCandidateSnapshot:",t)}}onOffer(e,t){return y(this,null,function*(){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: onOffer","color: green;"),this.createPeerConnection(e,t.from)){this.addLocalStreamToPeerConnection(e,!0);let a=t.offer;yield this.setRemoteDescription(e,a),yield this.sendAnswer(e,t.from)}}catch(i){this.loggingService.catch("Error in HuddleService onOffer:",i)}})}sendAnswer(e,t){return y(this,null,function*(){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: sendAnswer","color: green;"),this.loggingService.log(e),this.documentPaths?.huddle&&e){let i=yield this.peerConnectionMap[e].createAnswer();yield this.setLocalDescription(e,i);let c={answer:{type:i.type,sdp:i.sdp},from:this.user.userSnippylyId,connectionId:e,to:t},h=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/connections/${e}`);this.dbRefsRemoveOnDropCall.push({type:"connection",dbRef:h}),yield this.realtimeAdapter.rtdbUpdate("huddle",h,{answerData:c}),this.realtimeAdapter.rtdbOnDisconnectRemove("huddle",h)}else this.loggingService.catch("Error in sendAnswer: documentPaths not found")}catch(i){this.loggingService.catch("Error in HuddleService onHuddleSnapshot:",i)}})}onAnswer(e,t){return y(this,null,function*(){try{this.loggingService.log("%c[WB] Calling FUNCTION: onAnswer","color: green;");let i=t.answer;yield this.setRemoteDescription(e,i)}catch(i){this.loggingService.catch("Error in HuddleService onAnswer:",i)}})}onIceCandidateChange(e,t){return y(this,null,function*(){try{this.loggingService.log("%c[WB] Calling FUNCTION: onIceCandidateChange","color: green;");let i=t.candidate;this.loggingService.log("onIceCandidateChange",i),yield this.peerConnectionMap[e].addIceCandidate(new RTCIceCandidate(i))}catch(i){this.loggingService.catch("Error in HuddleService onIceCandidateChange:",i)}})}setRemoteDescription(e,t){return y(this,null,function*(){try{this.loggingService.log("%c[WB] Calling FUNCTION: setRemoteDescription","color: green;"),yield this.peerConnectionMap[e].setRemoteDescription(new RTCSessionDescription(t))}catch(i){this.loggingService.catch("Error in HuddleService setRemoteDescription:",i)}})}addAttendee(){return y(this,null,function*(){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: addAttendee","color: green;"),this.documentPaths?.huddle&&this.user?.userSnippylyId){let e=m(p({},this.user),{timestamp:this.timestamp,streamMetadata:{webcam:this.localStream?.id,screen:this.screenSharingStream?.id},initialHuddleType:this.initialHuddleType,huddleOnCursorMode:this.huddleOnCursorMode$?.value});e=this.huddleUtilsService.formatHuddleAttendeeData(e);let t=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/attendees/${this.user.userSnippylyId}`);yield this.realtimeAdapter.rtdbSet("huddle",t,JSON.parse(JSON.stringify(e))),this.dbRefsRemoveOnDropCall.push({type:"attendee",dbRef:t}),this.realtimeAdapter.rtdbOnDisconnectRemove("huddle",t),yield this.updateUserHuddleState()}else this.loggingService.catch("Error in addAttendee: documentPaths not found")}catch(e){this.loggingService.catch("Error in HuddleService addAttendee:",e)}})}callExistingAttendees(){try{this.loggingService.log("%c[WB] Calling FUNCTION: callExistingAttendees","color: green;"),this.realtimeAdapter.rtdbListVal$("huddle",this.attendees).pipe(he(1)).subscribe(e=>{e&&e.length>1?(e.forEach(t=>{t.userSnippylyId!==this.user.userSnippylyId&&!this.onOfferUidHistory[t.userSnippylyId]&&(this.loggingService.log("calling"),this.loggingService.log(t),this.initOffer(t.userSnippylyId))}),this.addLocalStreamToAllPeerConnections()):this.deleteMessages()})}catch(e){this.loggingService.catch("Error in HuddleService callExistingAttendees:",e)}}addLocalStreamToAllPeerConnections(){return y(this,null,function*(){try{this.loggingService.log("%c[WB] Calling FUNCTION: addLocalStreamToAllPeerConnections","color: green;"),Object.keys(this.senders).forEach(e=>{this.loggingService.log(this.senders[e]),this.localStream.getTracks().forEach(t=>{this.loggingService.log(t),this.senders[e].replaceTrack(t)})})}catch(e){this.loggingService.catch("Error in HuddleService addLocalStreamToAllPeerConnections:",e)}})}initOffer(e){return y(this,null,function*(){try{this.loggingService.log("%c[WB] Calling FUNCTION: initOffer","color: green;");let t=this.createPeerConnection(null,e);t&&(this.addLocalStreamToPeerConnection(t,!1),yield this.sendOffer(t,e))}catch(t){this.loggingService.catch("Error in HuddleService initOffer:",t)}})}createPeerConnection(e,t){try{return this.loggingService.log("%c[WB] Calling FUNCTION: createPeerConnection","color: green;"),e||(e=this.getCombinedId(this.user?.userSnippylyId,t)),e in this.peerConnectionMap?null:(this.loggingService.log("creating pc"),this.peerConnectionMap[e]=new RTCPeerConnection(this.servers),this.peerConnectionMap[e].onicecandidateerror=i=>{this.loggingService.log("Huddle: onicecandidateerror",i)},this.peerConnectionMap[e].onicecandidate=i=>{if(this.loggingService.log("pc onicecandidate"),i.candidate){let a={candidate:i.candidate.toJSON(),from:this.user.userSnippylyId,connectionId:e},c=this.realtimeAdapter.rtdbPush("huddle",this.iceCandidates,a);this.dbRefsRemoveOnDropCall.push({type:"iceCandidate",dbRef:c}),this.realtimeAdapter.rtdbOnDisconnectRemove("huddle",c)}},this.peerConnectionMap[e].ontrack=i=>{this.loggingService.log("pc ontrack"),this.onTrack(e,i)},this.peerConnectionMap[e].onconnectionstatechange=i=>y(this,null,function*(){this.logStatuses("onconnectionstatechange",e,i),this.peerConnectionMap[e].connectionState==="failed"&&(this.removeConnection(e),this.closeConnection(e),this.checkForRemovingAttendee())}),this.peerConnectionMap[e].onicegatheringstatechange=i=>y(this,null,function*(){this.logStatuses("onicegatheringstatechange",e,i)}),this.peerConnectionMap[e].onsignalingstatechange=i=>y(this,null,function*(){this.logStatuses("onsignalingstatechange",e,i)}),this.peerConnectionMap[e].oniceconnectionstatechange=i=>y(this,null,function*(){this.loggingService.log("pc oniceconnectionstatechange"),this.logStatuses("oniceconnectionstatechange",e,i);try{this.peerConnectionStateMap[e]=this.peerConnectionMap[e].iceConnectionState,this.peerConnectionStateMap$.next(this.peerConnectionStateMap),this.peerConnectionMap[e]?.iceConnectionState==="connected"?(this.loggingService.log("connected"),this.updateTransceiverDirection(e)):this.peerConnectionMap[e]?.iceConnectionState==="disconnected"?this.loggingService.log("Disconnected"):this.peerConnectionMap[e].iceConnectionState==="closed"&&(this.loggingService.log("Closed"),this.closeConnection(e))}catch(a){this.loggingService.catch("Error in HuddleService oniceconnectionstatechange:",a)}}),this.peerConnectionMap[e].onnegotiationneeded=()=>{this.loggingService.log("pc onnegotiationneeded"),this.logStatuses("onnegotiationneeded",e)},e)}catch(i){return this.loggingService.catch("Error in HuddleService createPeerConnection:",i),""}}logStatuses(e,t,i){let a={};["connectionState","iceConnectionState","iceGatheringState","signalingState"].forEach(h=>{a[h]=this.peerConnectionMap[t][h]}),this.loggingService.log("Huddle: logStatues: ",t,e,a,i,this.peerConnectionMap[t])}checkForRemovingAttendee(){return y(this,null,function*(){try{this.documentPaths?.huddle&&this.user?.userSnippylyId&&(Object.keys(this.peerConnectionMap)?.length||(this.commonDbService.deleteData({feature:"huddleAttendee",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,id:this.user?.userSnippylyId,skipFirestore:!0}}),this.joinHuddle(this.initialHuddleType)))}catch(e){this.loggingService.catch("Error in HuddleService checkForRemovingAttendee:",e)}})}closeConnection(e){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: closeConnection","color: green;"),this.peerConnectionMap[e]){let t=this.peerConnectionMap[e];t.getTransceivers()?.forEach(a=>{a.stop()}),t.getReceivers()?.forEach(a=>{a.track?.stop()}),t.close(),delete this.peerConnectionMap[e],delete this.onAnswerConnectionIdHistory[e],delete this.senders[e];let i=e.split("__").find(a=>a!==this.user?.userSnippylyId);i&&delete this.onOfferUidHistory[i]}}catch(t){this.loggingService.catch("Error in HuddleService closeConnection:",t)}}removeConnection(e){return y(this,null,function*(){try{this.documentPaths?.huddle&&e&&this.commonDbService.deleteData({feature:"huddleConnection",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,id:e,skipFirestore:!0}})}catch(t){this.loggingService.catch("Error in HuddleService removeConnection:",t)}})}addLocalStreamToPeerConnection(e,t){try{this.loggingService.log("%c[WB] Calling FUNCTION: addLocalStreamToPeerConnection","color: green;"),t?(this.localStream.getTracks().forEach(i=>{this.peerConnectionMap[e].addTrack(i,this.localStream)}),this.screenSharingStream?.getVideoTracks()?.forEach(i=>{this.peerConnectionMap[e].addTrack(i,this.screenSharingStream)})):(this.localStream.getTracks().forEach(i=>{this.peerConnectionMap[e].addTrack(i,this.localStream)}),this.screenSharingStream?.getVideoTracks()?.forEach(i=>{this.peerConnectionMap[e].addTrack(i,this.screenSharingStream)}))}catch(i){this.loggingService.catch("Error in HuddleService addLocalStreamToPeerConnection:",i)}}updateTransceiverDirection(e){try{this.loggingService.log("%c[WB] Calling FUNCTION: updateTransceiverDirection","color: green;");let t=this.peerConnectionMap[e].getTransceivers();this.loggingService.log(t),t.map(i=>{this.loggingService.log(i.currentDirection),i.currentDirection!=="sendrecv"&&(i.direction="sendrecv"),this.senders[e]=i.sender})}catch(t){this.loggingService.catch("Error in HuddleService updateTransceiverDirection:",t)}}generateKey(){this.loggingService.log("%c[WB] Calling FUNCTION: generateKey","color: green;");let e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",t="";for(let i=0;i<20;i++)t+=e.charAt(Math.floor(Math.random()*e.length));return t}getCombinedId(e,t){try{return e.localeCompare(t)===-1?`${e}__${t}`:`${t}__${e}`}catch(i){return this.loggingService.catch("Error in HuddleService getCombinedId:",i),""}}sendOffer(e,t){return y(this,null,function*(){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: sendOffer","color: green;"),this.documentPaths?.huddle&&e){this.onOfferUidHistory[t]=!0;let i=yield this.peerConnectionMap[e].createOffer();yield this.setLocalDescription(e,i);let a={sdp:i.sdp,type:i.type};t||(t="all");let c={offer:a,from:this.user.userSnippylyId,connectionId:e,to:t},h=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/connections/${e}`);yield this.realtimeAdapter.rtdbSet("huddle",h,{offerData:c}),this.dbRefsRemoveOnDropCall.push({type:"connection",dbRef:h}),this.realtimeAdapter.rtdbOnDisconnectRemove("huddle",h)}}catch(i){this.loggingService.catch("Error in HuddleService sendOffer:",i)}})}setLocalDescription(e,t){return y(this,null,function*(){try{this.loggingService.log("%c[WB] Calling FUNCTION: setLocalDescription","color: green;"),yield this.peerConnectionMap[e].setLocalDescription(t)}catch(i){this.loggingService.catch("Error in HuddleService setLocalDescription:",i)}})}onTrack(e,t){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: onTrack","color: green;"),this.remoteStreamMap[e]||(this.remoteStreamMap[e]={}),this.loggingService.log(t.receiver.track),t?.streams?.length){this.remoteStreamMap[e][t.streams[0].id]=t.streams[0];let i=e.split("__").find(c=>c!==this.user?.userSnippylyId),a=this.attendees$.value?.find(c=>c?.userSnippylyId===i);a&&(a?.streamMetadata?.webcam===t.streams[0].id&&(this.remoteStreamMap[e].webcam=t.streams[0]),a?.streamMetadata?.screen===t.streams[0].id&&(this.remoteStreamMap[e].screen=t.streams[0])),this.remoteStreamMap$.next(this.remoteStreamMap)}}catch(i){this.loggingService.catch("Error in HuddleService onTrack:",i)}}toggleScreenSharing(e){return y(this,null,function*(){try{if(e)this.screenSharingStream=yield navigator.mediaDevices?.getDisplayMedia({video:!0}),Object.keys(this.peerConnectionMap).forEach(t=>{this.peerConnectionMap[t].getSenders().find(c=>c?.track?.kind==this.screenSharingStream?.getVideoTracks()[0].kind)?.replaceTrack(this.screenSharingStream?.getVideoTracks()[0])});else{let t=this.localStream;Object.keys(this.peerConnectionMap).forEach(i=>{this.peerConnectionMap[i].getSenders().find(h=>h?.track?.kind==t.getVideoTracks()[0].kind)?.replaceTrack(t.getVideoTracks()[0])}),this.screenSharingStream?.getTracks().forEach(i=>i.stop())}}catch(t){this.loggingService.catch("Error in HuddleService toggleScreenSharing:",t)}})}setLocalStream(e){return y(this,null,function*(){try{if(this.localStream=new MediaStream([this.silence(),this.black()]),e?.audio||e?.video){let t=e,i=we();e?.audio&&(e?.audio instanceof Object?t.audio=m(p({},e?.audio),{deviceId:i?.selectedAudioDevice?.deviceId}):t.audio={deviceId:i?.selectedAudioDevice?.deviceId}),e?.video&&(e?.video instanceof Object?t.video=m(p({},e?.video),{deviceId:i?.selectedVideoDevice?.deviceId}):t.video={deviceId:i?.selectedVideoDevice?.deviceId});let a=yield navigator.mediaDevices.getUserMedia(t);a?.getAudioTracks()?.length&&(this.localStream.getAudioTracks().forEach(c=>this.localStream.removeTrack(c)),a.getAudioTracks().forEach(c=>this.localStream.addTrack(c))),a?.getVideoTracks()?.length&&(this.localStream.getVideoTracks().forEach(c=>this.localStream.removeTrack(c)),a.getVideoTracks().forEach(c=>this.localStream.addTrack(c)))}this.localStream$.next(this.localStream)}catch(t){this.loggingService.catch("Error in HuddleService setLocalStream:",t)}})}setScreenSharingStream(){return y(this,null,function*(){try{let e=yield navigator.mediaDevices?.getDisplayMedia({video:!0});e&&this.screenSharingStream$.next(e)}catch(e){this.loggingService.catch("Error in HuddleService setScreenSharingStream:",e)}})}removeLocalStream(){return y(this,null,function*(){try{this.localStream$?.value&&(this.localStream$.value.getTracks().forEach(t=>{t.stop()}),this.localStream$.next(null))}catch(e){this.loggingService.catch("Error in HuddleService removeLocalStream:",e)}})}toggleLocalStreamAudio(e){return y(this,null,function*(){try{let t=this.localStream$.value;if(t){let i=t.getAudioTracks()[0];if(i.enabled&&!e)i.stop(),t.getAudioTracks()?.forEach(a=>{t.removeTrack(a)}),t.addTrack(this.silence()),this.analyticsService.trackEvent(V.Events.Huddle.HUDDLE_AUDIO_MUTED,{huddleType:this.initialHuddleType,totalUsers:this.getHuddleAttendees()?.length||0,totalUserHuddleDuration:this.getTotalUserHuddleDuration(),huddleSnippylyUserIds:this.getHuddleAttendees()?.map(a=>a?.userSnippylyId),huddleClientUserIds:this.getHuddleAttendees()?.map(a=>a?.userId)});else{let a=we(),c=yield navigator.mediaDevices.getUserMedia({audio:{deviceId:a?.selectAudioDevice?.deviceId}});c&&(t.getAudioTracks()?.forEach(h=>{t.removeTrack(h)}),c.getAudioTracks()?.forEach(h=>{t.addTrack(h)}),this.analyticsService.trackEvent(V.Events.Huddle.HUDDLE_AUDIO_UNMUTED,{huddleType:this.initialHuddleType,totalUsers:this.getHuddleAttendees()?.length||0,totalUserHuddleDuration:this.getTotalUserHuddleDuration(),huddleSnippylyUserIds:this.getHuddleAttendees()?.map(h=>h?.userSnippylyId),huddleClientUserIds:this.getHuddleAttendees()?.map(h=>h?.userId)}))}Object.keys(this.peerConnectionMap).forEach(a=>{this.peerConnectionMap[a].getSenders().find(H=>H?.track?.kind==t?.getAudioTracks()[0].kind)?.replaceTrack(t?.getAudioTracks()[0])}),this.updateUserHuddleState()}}catch(t){this.loggingService.catch("Error in HuddleService toggleLocalStreamAudio:",t)}})}toggleLocalStreamVideo(e){return y(this,null,function*(){try{let t=this.localStream$.value;if(t){let i=t.getVideoTracks()[0];if(i.enabled&&!e)i.stop(),t.getVideoTracks()?.forEach(a=>{t.removeTrack(a)}),t.addTrack(this.black()),this.analyticsService.trackEvent(V.Events.Huddle.HUDDLE_VIDEO_TURNED_OFF,{huddleType:this.initialHuddleType,totalUsers:this.getHuddleAttendees()?.length||0,totalUserHuddleDuration:this.getTotalUserHuddleDuration(),huddleSnippylyUserIds:this.getHuddleAttendees()?.map(a=>a?.userSnippylyId),huddleClientUserIds:this.getHuddleAttendees()?.map(a=>a?.userId)});else{let a=we(),c=yield navigator.mediaDevices.getUserMedia({video:{deviceId:a?.selectedVideoDevice?.deviceId}});c&&(t.getVideoTracks()?.forEach(h=>{t.removeTrack(h)}),c.getVideoTracks()?.forEach(h=>{t.addTrack(h)}),this.analyticsService.trackEvent(V.Events.Huddle.HUDDLE_VIDEO_TURNED_ON,{huddleType:this.initialHuddleType,totalUsers:this.getHuddleAttendees()?.length||0,totalUserHuddleDuration:this.getTotalUserHuddleDuration(),huddleSnippylyUserIds:this.getHuddleAttendees()?.map(h=>h?.userSnippylyId),huddleClientUserIds:this.getHuddleAttendees()?.map(h=>h?.userId)}))}Object.keys(this.peerConnectionMap).forEach(a=>{this.peerConnectionMap[a].getSenders().find(H=>H?.track?.kind==t?.getVideoTracks()[0].kind)?.replaceTrack(t?.getVideoTracks()[0])}),this.updateUserHuddleState()}}catch(t){this.loggingService.catch("Error in HuddleService toggleLocalStreamVideo:",t)}})}toggleLocalScreenSharing(){return y(this,null,function*(){try{let e=this.screenSharingStream;if(e){if(e.getVideoTracks()[0].enabled)this.stopScreenSharingStream();else{let i=yield navigator.mediaDevices?.getDisplayMedia({video:!0});i&&(e.getVideoTracks()?.forEach(a=>{e.removeTrack(a)}),i.getVideoTracks()?.forEach(a=>{e.addTrack(a),a.onended=()=>{this.stopScreenSharingStream(),this.updateScreenSharingTracksToPeers(),this.updateUserHuddleState()}})),this.screenSharingStream$.next(e),this.analyticsService.trackEvent(V.Events.Huddle.HUDDLE_SCREENSHARING_STARTED,{huddleType:this.initialHuddleType,totalUsers:this.getHuddleAttendees()?.length||0,totalUserHuddleDuration:this.getTotalUserHuddleDuration(),huddleSnippylyUserIds:this.getHuddleAttendees()?.map(a=>a?.userSnippylyId),huddleClientUserIds:this.getHuddleAttendees()?.map(a=>a?.userId)})}this.updateScreenSharingTracksToPeers(),this.updateUserHuddleState()}}catch(e){this.loggingService.catch("Error in HuddleService toggleLocalScreenSharing:",e)}})}stopScreenSharingStream(){return y(this,null,function*(){let e=this.screenSharingStream;if(e){let t=e.getVideoTracks()[0];t.enabled&&(t.stop(),e.getVideoTracks()?.forEach(i=>{e.removeTrack(i)}),e.addTrack(this.black()),this.analyticsService.trackEvent(V.Events.Huddle.HUDDLE_SCREENSHARING_STOPPED,{huddleType:this.initialHuddleType,totalUsers:this.getHuddleAttendees()?.length||0,totalUserHuddleDuration:this.getTotalUserHuddleDuration(),huddleSnippylyUserIds:this.getHuddleAttendees()?.map(i=>i?.userSnippylyId),huddleClientUserIds:this.getHuddleAttendees()?.map(i=>i?.userId)}))}})}updateScreenSharingTracksToPeers(){return y(this,null,function*(){Object.keys(this.peerConnectionMap).forEach(e=>{let i=this.peerConnectionMap[e].getSenders().filter(a=>a?.track?.kind==this.screenSharingStream?.getVideoTracks()[0].kind);i?.length>1&&i[1]?.replaceTrack(this.screenSharingStream?.getVideoTracks()[0])})})}dropCall(){return y(this,null,function*(){try{this.messagesSubscription&&this.messagesSubscription.unsubscribe(),this.removeHuddleSessionData(),this.huddleJoined=!1,this.huddleJoined$.next(!1),this.peerConnectionMap&&Object.keys(this.peerConnectionMap).forEach(e=>{let t=this.peerConnectionMap[e];t.getTransceivers()?.forEach(i=>{i.stop()}),t.getSenders()?.forEach(i=>{i.track?.stop()}),t.getReceivers()?.forEach(i=>{i.track?.stop()}),t.close()}),this.peerConnectionMap={},this.dbRefsRemoveOnDropCall.forEach(e=>y(this,null,function*(){yield this.realtimeAdapter.rtdbRemove("huddle",e.dbRef)})),this.onOfferUidHistory={},this.onAnswerConnectionIdHistory={},this.senders={},this.peerConnectionStateMap={},this.peerConnectionStateMap$.next(this.peerConnectionStateMap),this.localStream.getTracks().forEach(e=>{e.stop()}),this.screenSharingStream.getTracks().forEach(e=>{e.stop()}),this.localStream=new MediaStream([this.silence(),this.black()]),this.screenSharingStream=new MediaStream([this.black()]),this.localStream$.next(this.localStream),this.screenSharingStream$.next(this.screenSharingStream),this.localStreamState$.next({audioState:!1,videoState:!1,screenSharingState:!1}),this.analyticsService.trackEvent(V.Events.Huddle.HUDDLE_LEFT,{huddleType:this.initialHuddleType,totalUsers:this.getHuddleAttendees()?.length||0,totalUserHuddleDuration:this.getTotalUserHuddleDuration(),huddleSnippylyUserIds:this.getHuddleAttendees()?.map(e=>e?.userSnippylyId),huddleClientUserIds:this.getHuddleAttendees()?.map(e=>e?.userId)})}catch(e){this.loggingService.catch("Error in HuddleService dropCall:",e)}})}updateHuddleSessionData(e,t,i){try{let a=JSON.parse(sessionStorage.getItem(M.SESSION_STORAGE.SNIPPYLY_HUDDLE_DATA)||"{}");i?sessionStorage.setItem(M.SESSION_STORAGE.SNIPPYLY_HUDDLE_DATA,JSON.stringify(i)):(a[e]=t,sessionStorage.setItem(M.SESSION_STORAGE.SNIPPYLY_HUDDLE_DATA,JSON.stringify(a)))}catch(a){this.loggingService.catch("Error in HuddleService updateHuddleSessionData:",a)}}getHuddleSessionData(){try{return sessionStorage.getItem(M.SESSION_STORAGE.SNIPPYLY_HUDDLE_DATA)?JSON.parse(sessionStorage.getItem(M.SESSION_STORAGE.SNIPPYLY_HUDDLE_DATA)):null}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleSessionData:",e),null}}removeHuddleSessionData(){try{sessionStorage.removeItem(M.SESSION_STORAGE.SNIPPYLY_HUDDLE_DATA)}catch(e){this.loggingService.catch("Error in HuddleService removeHuddleSessionData:",e)}}removeAttendee(){return y(this,null,function*(){try{if(this.documentPaths?.huddle&&this.user.userSnippylyId){yield this.commonDbService.deleteData({feature:"huddleAttendee",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,id:this.user?.userSnippylyId,skipFirestore:!0}});return}else return}catch(e){return this.loggingService.catch("Error in HuddleService removeAttendee:",e),Promise.resolve(null)}})}updateUserHuddleState(){return y(this,null,function*(){try{if(this.attendees&&this.user&&this.documentPaths?.huddle){let e=this.getAudioState(),t=this.getVideoState(),i=this.getScreenSharingState();this.localStreamState$.next({audioState:e,videoState:t,screenSharingState:i});let a={audioState:e,videoState:t,screenSharingState:i};yield this.commonDbService.setData({feature:"huddleAttendeeState",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,id:this.user?.userSnippylyId,skipFirestore:!0},data:a})}}catch(e){this.loggingService.catch("Error in HuddleService updateUserHuddleState:",e)}})}updateHuddleOnCursorMode(){return y(this,null,function*(){try{if(this.attendees&&this.user&&this.documentPaths?.huddle){let e=this.realtimeAdapter.rtdbRef(this.db,`${this.documentPaths?.huddle}/attendees/${this.user.userSnippylyId}/huddleOnCursorMode`);yield this.realtimeAdapter.rtdbSet("huddle",e,!!this.huddleOnCursorMode$.value)}}catch(e){this.loggingService.catch("Error in HuddleService updateHuddleOnCursorMode:",e)}})}getAudioState(){try{let e=this.localStream$.value;if(e){let t=e?.getAudioTracks();return!!(t?.length?t[0]:null)?.enabled}return!1}catch(e){return this.loggingService.catch("Error in HuddleService getAudioState:",e),!1}}getVideoState(){try{let e=this.localStream$.value;if(e){let t=e?.getVideoTracks();return!!(t?.length?t[0]:null)?.enabled}return!1}catch(e){return this.loggingService.catch("Error in HuddleService getVideoState:",e),!1}}getScreenSharingState(){try{let e=this.screenSharingStream;if(e){let t=e?.getVideoTracks();return!!(t?.length?t[0]:null)?.enabled}return!1}catch(e){return this.loggingService.catch("Error in HuddleService getVideoState:",e),!1}}removeIceCandidates(e){return y(this,null,function*(){try{if(this.documentPaths?.huddle&&e){let t=yield this.commonDbService?.getData({feature:"allHuddleIceCandidates",properties:{documentId:this.documentPaths?.documentId??"",organizationId:this.documentPaths?.organizationId??"",skipFirestore:!0}});Object.keys(t).length>0&&Object.keys(t).forEach(i=>y(this,null,function*(){t[i].connectionId===e&&this.commonDbService.deleteData({feature:"huddleIceCandidate",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,id:i,skipFirestore:!0}})}))}}catch(t){this.loggingService.catch("Error in HuddleService dropCall:",t)}})}get timestamp(){return this.commonDbService.getServerTimestamp()}silence(){try{let e=new AudioContext,t=e.createOscillator(),i=t.connect(e.createMediaStreamDestination());return t.start(),Object.assign(i.stream.getAudioTracks()[0],{enabled:!1})}catch(e){this.loggingService.catch("Error in HuddleService silence:",e)}}black({width:e=640,height:t=480}={}){try{let i=Object.assign(document.createElement("canvas"),{width:e,height:t});i.getContext("2d")?.fillRect(0,0,e,t);let a=i.captureStream();return Object.assign(a.getVideoTracks()[0],{enabled:!1})}catch(i){this.loggingService.catch("Error in HuddleService black:",i)}}getInitialHuddleType(){try{return this.initialHuddleType}catch(e){return this.loggingService.catch("Error in HuddleService getInitialHuddleType:",e),null}}getHuddleAttendees$(){try{return this.attendees$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleAttendees$:",e),P([])}}getHuddleAttendees(){try{return this.attendees$.value}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleAttendees:",e),null}}getHuddleInvitees$(){try{return this.invitees$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleInvitees$:",e),P([])}}getHuddleInvitees(){try{return this.invitees$.value}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleInvitees:",e),null}}getHuddleStartTime(){try{return this.userHuddleStartTime$.value}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleStartTime:",e),null}}getTotalUserHuddleDuration(){try{return this.userHuddleStartTime$.value?new Date().getTime()-this.userHuddleStartTime$.value:null}catch(e){return this.loggingService.catch("Error in HuddleService gettotalUserHuddleDuration:",e),null}}inviteUsers(e){try{this.updateInvitees(e);let t={};t.notificationSource="huddleInvite",t.huddleInvitees=e,t.huddleType=this.getInitialHuddleType(),this.updateNotificationsDatabase(t)}catch(t){this.loggingService.catch("Error in HuddleService inviteUsers:",t)}}updateInvitees(e){try{if(this.documentPaths?.huddle){let t={};e.forEach(i=>{if(i?.email){let a=jt(i.email);t[a]=i}}),this.commonDbService.setData({feature:"allHuddleInvitees",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,skipFirestore:!0},data:t})}}catch(t){this.loggingService.catch("Error in HuddleService updateInvitees:",t)}}updateNotificationsDatabase(e={}){try{let t=p({notificationSource:"huddle"},e);this.serviceBook.get(Zt)?.updateNotificationDatabase(t)}catch(t){this.loggingService.catch("Error in HuddleService updateNotificationsDatabase: ",t)}}getHuddleJoined$(){try{return this.huddleJoined$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleJoined$:",e),P(!1)}}getHuddleCursorAvailableByAttendeeId(){try{return this.huddleCursorAvailableByAttendeeId$.value}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleCursorAvailableByAttendeeId:",e),null}}getHuddleCursorAvailableByAttendeeId$(){try{return this.huddleCursorAvailableByAttendeeId$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleCursorAvailableByAttendeeId$:",e),P({})}}updateHuddleCursorAvailableByAttendeeId(e,t){try{let i=this.huddleCursorAvailableByAttendeeId$.value;i[e]=t,this.huddleCursorAvailableByAttendeeId$.next(i)}catch(i){this.loggingService.catch("Error in HuddleService updateCursorUserAvailableByUserId:",i)}}setRemoteStreamsByUserId(e){try{this.remoteStreamsByUserId$.next(e)}catch(t){this.loggingService.catch("Error in HuddleService setRemoteStreamsByUserId:",t)}}getRemoteStreamsByUserId$(){try{return this.remoteStreamsByUserId$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getRemoteStreamsByUserId$:",e),P({})}}enableHuddleOnCursorMode(){try{this.huddleOnCursorMode$.next(!0)}catch(e){this.loggingService.catch("Error in HuddleService enableHuddleOnCursorMode:",e)}}disableHuddleOnCursorMode(){try{this.huddleOnCursorMode$.next(!1)}catch(e){this.loggingService.catch("Error in HuddleService disableHuddleOnCursorMode:",e)}}getHuddleOnCursorMode$(){try{return this.huddleOnCursorMode$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleOnCursorMode$:",e),P(!1)}}enableFlockModeOnAvatarClick(){try{this.flockModeOnAvatarClickEnabled$.next(!0)}catch(e){this.loggingService.catch("Error in HuddleService enableFlockModeOnAvatarClick:",e)}}disableFlockModeOnAvatarClick(){try{this.flockModeOnAvatarClickEnabled$.next(!1)}catch(e){this.loggingService.catch("Error in HuddleService disableFlockModeOnAvatarClick:",e)}}getFlockModeOnAvatarClick$(){try{return this.flockModeOnAvatarClickEnabled$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getFlockModeOnAvatarClick$:",e),P(!1)}}getFlockModeOnAvatarClick(){try{return this.flockModeOnAvatarClickEnabled$.value}catch(e){return this.loggingService.catch("Error in HuddleService getFlockModeOnAvatarClick:",e),!1}}enableChat(){try{this.chatEnabled$.next(!0)}catch(e){this.loggingService.catch("Error in HuddleService enableChat:",e)}}disableChat(){try{this.chatEnabled$.next(!1)}catch(e){this.loggingService.catch("Error in HuddleService disableChat:",e)}}getChatEnabled$(){try{return this.chatEnabled$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getChatEnabled$:",e),P(!1)}}getChatEnabled(){try{return this.chatEnabled$.value}catch(e){return this.loggingService.catch("Error in HuddleService getChatEnabled:",e),!1}}enableServerFallback(){try{if(this.serverFallback$.value===!0)return;this.serverFallback$.next(!0)}catch(e){this.loggingService.catch("Error in HuddleService enableServerFallback:",e)}}disableServerFallback(){try{if(this.serverFallback$.value===!1)return;this.serverFallback$.next(!1)}catch(e){this.loggingService.catch("Error in HuddleService disableServerFallback:",e)}}getServerFallback$(){try{return this.serverFallback$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getServerFallback$:",e),P(!1)}}getServerFallback(){try{return this.serverFallback$.value}catch(e){return this.loggingService.catch("Error in HuddleService getServerFallback:",e),!1}}getHuddleOnCursorModeByAttendeeId$(){try{return this.huddleOnCursorModeByAttendeeId$.asObservable()}catch(e){return this.loggingService.catch("Error in HuddleService getHuddleOnCursorModeByAttendeeId$:",e),P({})}}toggleHuddleOnCursorModeByAttendeeId(e){try{let t=this.huddleOnCursorModeByAttendeeId$.value;t[e]?delete t[e]:t[e]=!0,this.huddleOnCursorModeByAttendeeId$.next(t)}catch(t){this.loggingService.catch("Error in HuddleService toggleHuddleOnCursorModeByAttendeeId:",t)}}addCursorTag(){try{if(!document.querySelector(`${M.TAGS.SNIPPYLY_CURSOR}`)){let e=document.createElement(M.TAGS.SNIPPYLY_CURSOR);e.setAttribute("snippyly-temporary","true"),document.body.appendChild(e)}}catch(e){this.loggingService.catch("Error in HuddleService addCursorTag:",e)}}removeCursorTag(){try{let e=document.querySelector(`${M.TAGS.SNIPPYLY_CURSOR}`);e?.hasAttribute("snippyly-temporary")&&e.remove()}catch(e){this.loggingService.catch("Error in HuddleService removeCursorTag:",e)}}sendMessage(e){try{if(this.loggingService.log("%c[WB] Calling FUNCTION: sendMessage","color: green;"),this.documentPaths?.huddle&&this.user){let t=new $t;t.message=e,t.from=this.user,t.timestamp=this.timestamp,this.commonDbService.setData({feature:"huddleMessage",properties:{documentId:this.documentPaths?.documentId,organizationId:this.documentPaths?.organizationId,id:t.id,skipFirestore:!0},data:t})}}catch(t){this.loggingService.catch("Error in HuddleService sendMessage:",