var f=Object.defineProperty;var p=(s,e,t)=>e in s?f(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var n=(s,e,t)=>(p(s,typeof e!="symbol"?e+"":e,t),t);import{A as b}from"./contracts.generated.aefe8d0a.js";import{H as u,r as d,I as m,J as I,K as c}from"./jwt-decode.esm.54a1ea22.js";(function(){try{var s=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},e=new Error().stack;e&&(s._sentryDebugIds=s._sentryDebugIds||{},s._sentryDebugIds[e]="cb733c9d-1f18-4d40-9396-03cd4a55cf0a",s._sentryDebugIdIdentifier="sentry-dbid-cb733c9d-1f18-4d40-9396-03cd4a55cf0a")}catch{}})();const r=class{constructor(){n(this,"ws",null);n(this,"reconnectTimer");n(this,"keepaliveInterval");n(this,"messageHandlers",new Set);n(this,"KEEPALIVE_INTERVAL",3e4);n(this,"RECONNECT_DELAY",1e3);this.connect()}static getInstance(){return r.instance||(r.instance=new r),r.instance}get url(){return"/_editor/api/codebase/events"}handleMessage(e){try{const t=JSON.parse(e.data),a=b.parse(t);this.messageHandlers.forEach(l=>{try{l(a)}catch(i){u(i)}})}catch(t){u(t)}}handleClose(){this.clearTimers(),this.scheduleReconnect()}handleError(){this.clearTimers(),this.scheduleReconnect()}clearTimers(){this.keepaliveInterval&&(clearInterval(this.keepaliveInterval),this.keepaliveInterval=void 0),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=void 0)}scheduleReconnect(){this.reconnectTimer||(this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=void 0,this.connect()},this.RECONNECT_DELAY))}startKeepalive(){this.keepaliveInterval=setInterval(()=>{this.sendKeepalive()},this.KEEPALIVE_INTERVAL)}sendKeepalive(){if(!this.ws||this.ws.readyState!==WebSocket.OPEN){this.handleError();return}try{this.ws.send(JSON.stringify({type:"keepalive"}))}catch(e){console.error("CodebaseEventsService: Failed to send keepalive:",e),this.handleError()}}async connect(){var e;if(((e=this.ws)==null?void 0:e.readyState)!==WebSocket.OPEN)try{this.ws=new WebSocket(this.url),this.ws.onopen=()=>this.startKeepalive(),this.ws.onclose=()=>this.handleClose(),this.ws.onerror=()=>this.handleError(),this.ws.onmessage=t=>this.handleMessage(t)}catch(t){console.error("CodebaseEventsService: Failed to create WebSocket:",t),this.scheduleReconnect()}}subscribe(e){return this.messageHandlers.add(e),()=>this.messageHandlers.delete(e)}isConnected(){var e;return((e=this.ws)==null?void 0:e.readyState)===WebSocket.OPEN}};let o=r;n(o,"instance",null);function T(){const s=d(!1),e=d(null),t=d(null),a=o.getInstance(),l=E=>{e.value=E},i=()=>{s.value=a.isConnected()};let h;const v=setInterval(i,1e3);return m(()=>{h(),clearInterval(v)}),I(()=>{h=a.subscribe(l),i()}),{isConnected:c(s),lastEvent:c(e),error:c(t),service:c(a)}}export{T as u};
//# sourceMappingURL=useCodebaseEvents.276cc8f0.js.map
