import{a as v,c as b}from"./chunk-EEWVYRVF.js";import{b as w}from"./chunk-F7TB6D2I.js";import{D as f,Kc as d,M as m,R as i,X as s,g as h,l as S,ta as e,y as p}from"./chunk-5ONOZUNN.js";var r=class o{#o=s(w);#t=e("disconnected");#e=e(null);#n=e(-1);#s=e(0);#i=e(!1);#c=new h;connectionStatus=this.#t.asReadonly();serverState=this.#e.asReadonly();duration=s(v).relayIsEnabled()?"94s":"299s";start(){this.#i.set(!0),this.#c=new h,this.#r()}stop(){this.#c.next(),this.#c.complete(),this.#i.set(!1)}reconnect(){this.stop(),this.start()}reconnectIfNeeded(){console.log("reconnectIfNeeded"),!this.#i()&&this.reconnect()}#r(){this.#t.set("connecting"),this.#o.getApiV1Serverstate({lastEventId:this.#n(),longpoll:this.#n()>0,duration:this.duration}).pipe(m(this.#c),p(t=>(this.#l(t),S(null))),f(()=>{this.#h()})).subscribe(t=>{t&&this.#a(t)})}#a(t){this.#t.set("connected"),this.#e.set(t),this.#n.set(t.LastEventID||-1),this.#s.set(0)}#l(t){let n=this.#s()+1;this.#s.set(n),n>3&&this.#t.set("disconnected")}#h(){this.#i()&&this.#r()}static \u0275fac=function(n){return new(n||o)};static \u0275prov=i({token:o,factory:o.\u0275fac,providedIn:"root"})};var a=class o{#o=s(b);#t=null;#e=e("disconnected");#n=e(null);shouldConnect=e(!0);connectionStatus=this.#e.asReadonly();serverState=this.#n.asReadonly();start(){this.#s(),this.shouldConnect.set(!0)}#s(){let t=this.#o.token();if(!t){console.error("No access token available");return}let n=window.location.protocol==="https:"?"wss:":"ws:",u=window.location.port?`:${window.location.port}`:"";window.location.port==="4200"&&(u=":8200");let y=window.location.hostname,I=`${n}//${y}${u}/notifications?token=${t}`;this.#e.set("connecting"),this.#t=new WebSocket(I),this.#t.onopen=()=>{console.log("WebSocket connection established"),this.#e.set("connected")},this.#t.onmessage=c=>{try{let l=JSON.parse(c.data);this.#n.set(l)}catch(l){console.error("Error parsing WebSocket message",l)}},this.#t.onclose=c=>{console.log("WebSocket connection closed",c),this.#e.set("disconnected"),this.shouldConnect()&&setTimeout(()=>this.#s(),5e3)},this.#t.onerror=c=>{console.error("WebSocket error",c),this.#e.set("disconnected")}}stop(){this.shouldConnect.set(!1),this.#t?.close()}reconnect(){this.#t?.close(),this.start()}reconnectIfNeeded(){this.#t?.readyState!==WebSocket.OPEN&&this.reconnect()}static \u0275fac=function(n){return new(n||o)};static \u0275prov=i({token:o,factory:o.\u0275fac,providedIn:"root"})};var k=class o{#o=s(a);#t=s(r);#e=e("longpoll");connectionStatus=d(()=>this.#e()==="websocket"?this.#o.connectionStatus():this.#t.connectionStatus());serverState=d(()=>this.#e()==="websocket"?this.#o.serverState():this.#t.serverState());setConnectionMethod(t){t==="websocket"?(this.#o.reconnectIfNeeded(),this.#t.stop()):(this.#t.reconnectIfNeeded(),this.#o.stop()),this.#e.set(t)}getConnectionMethod(){return this.#e()}static \u0275fac=function(n){return new(n||o)};static \u0275prov=i({token:o,factory:o.\u0275fac,providedIn:"root"})};export{k as a};
