import{i as d}from"./contracts.generated-Df9OlTQX.js";import{aY as i,z as e}from"./jwt-decode.esm-rEBc1yJt.js";(function(){try{var n=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},t=new Error().stack;t&&(n._sentryDebugIds=n._sentryDebugIds||{},n._sentryDebugIds[t]="ed81cfe7-4b02-48e8-83c7-5b07b199d1e0",n._sentryDebugIdIdentifier="sentry-dbid-ed81cfe7-4b02-48e8-83c7-5b07b199d1e0")}catch{}})();class r{static instances=new Map;ws=null;reconnectTimer;keepaliveTimer;messageHandlers=new Set;hasConnectedBefore=!1;url;parse;keepaliveInterval;reconnectDelay;onReconnect;constructor(t){this.url=t.url,this.parse=t.parse,this.keepaliveInterval=t.keepaliveInterval??3e4,this.reconnectDelay=t.reconnectDelay??1e3,this.onReconnect=t.onReconnect,this.connect()}static getInstance(t){const s=r.instances.get(t.url);if(s)return s;const a=new r(t);return r.instances.set(t.url,a),a}handleMessage(t){try{const s=JSON.parse(t.data),a=this.parse?this.parse(s):s;this.messageHandlers.forEach(l=>{try{Promise.resolve(l(a)).catch(i)}catch(h){i(h)}})}catch(s){i(s)}}handleClose(){this.clearTimers(),this.scheduleReconnect()}handleError(){this.clearTimers();try{this.ws?.close()}catch{}this.ws=null,this.scheduleReconnect()}clearTimers(){this.keepaliveTimer&&(clearInterval(this.keepaliveTimer),this.keepaliveTimer=void 0),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=void 0)}scheduleReconnect(){this.reconnectTimer||(this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=void 0,this.connect()},this.reconnectDelay))}startKeepalive(){this.keepaliveTimer=setInterval(()=>{if(!this.ws||this.ws.readyState!==WebSocket.OPEN){this.handleError();return}try{this.ws.send(JSON.stringify({type:"keepalive"}))}catch{this.handleError()}},this.keepaliveInterval)}connect(){if(!(this.ws?.readyState===WebSocket.OPEN||this.ws?.readyState===WebSocket.CONNECTING))try{const t=new WebSocket(this.url);this.ws=t,t.onopen=()=>{this.ws===t&&(this.startKeepalive(),this.hasConnectedBefore&&this.onReconnect?.(),this.hasConnectedBefore=!0)},t.onclose=()=>{this.ws===t&&this.handleClose()},t.onerror=()=>{this.ws===t&&this.handleError()},t.onmessage=s=>{this.ws===t&&this.handleMessage(s)}}catch{this.scheduleReconnect()}}subscribe(t){return this.messageHandlers.add(t),()=>this.messageHandlers.delete(t)}isConnected(){return this.ws?.readyState===WebSocket.OPEN}}const c=e.object({filePath:e.string(),holderEmail:e.string(),holderName:e.string(),sessionId:e.string(),acquiredAt:e.number(),lastHeartbeatAt:e.number()}),o=e.object({sessionId:e.string(),email:e.string(),name:e.string(),currentFilePath:e.string().nullable(),lastHeartbeatAt:e.number()}),v=e.object({locks:e.array(c)}),y=e.object({granted:e.boolean(),holder:c.nullish()}),k=e.object({released:e.boolean()}),w=e.object({stillHeld:e.boolean(),lock:c.nullish()}),S=e.object({users:e.array(o)}),u=e.object({filepath:e.string(),event:e.enum(["lock_acquired","lock_released"]),lock:c}),m=e.object({filepath:e.string(),event:e.enum(["presence_joined","presence_left","presence_update"]),presence:o}),b=e.union([u,m,d]),p="/_editor/api/codebase/events";function T(){return r.getInstance({url:p,parse:n=>b.parse(n)})}export{y as L,S as P,r as W,k as a,w as b,v as c,T as g};
//# sourceMappingURL=codebaseEvents-CnQo2e5C.js.map
