import{l as t,a as s,b as e}from"./p-SyQT2Pr3.js";import{b as i,c as n}from"./p-BCkDiHIY.js";import{E as o}from"./p-BNpVu1WU.js";class h{constructor(t,s=""){this.socketId="",this.socket=null,this.mode=o.DATA_MODE,this.connectionPromise=null,this.apiKey="",this.protocol="legacy",this.authenticated=!1,this.mode=t,this.apiKey=s,this.protocol=i(t),this.initSocket()}initSocket(){if(this.socket&&(this.clearSocketListeners(),this.socket.disconnect(),this.socket=null),!this.socket){let i=n(this.mode);this.connectionPromise=new Promise(((n,o)=>{let c=!1;const r=setTimeout((()=>{c||(c=!0,console.error("Socket connection timed out"),window.dispatchEvent(new CustomEvent("socketError",{detail:{message:"Unable to connect to server. Please try again later.",code:"CONNECTION_TIMEOUT"}})),o(new Error("Unable to connect to server. Please try again later.")))}),3e4);this.socket=t(i,{transports:"v2"===this.protocol?["websocket","polling"]:["polling"]}),this.socket.on("connect",(()=>{var t;this.socketId=null===(t=this.socket)||void 0===t?void 0:t.id,this.socketId&&h.instances.set(this.socketId,this),"v2"===this.protocol&&this.apiKey?this.socket.emit("authenticate",{api_key:this.apiKey}):c||(c=!0,clearTimeout(r),n())})),"v2"===this.protocol?(this.socket.on("authenticated",(()=>{this.authenticated=!0,c||(c=!0,clearTimeout(r),n())})),this.socket.on("chunk",(t=>{const e=s(t);window.dispatchEvent(new CustomEvent("streamResponse",{detail:e}))})),this.socket.on("complete",(t=>{const s=e(t);window.dispatchEvent(new CustomEvent("endResponse",{detail:s}))}))):(this.socket.on("response",(t=>{window.dispatchEvent(new CustomEvent("streamResponse",{detail:t}))})),this.socket.on("end_response",(t=>{window.dispatchEvent(new CustomEvent("endResponse",{detail:t}))}))),this.socket.on("close",(()=>{this.clearSocketListeners(),this.authenticated=!1,this.socket=null,setTimeout((()=>this.initSocket()),3e3)})),this.socket.on("error",(t=>{console.error("WebSocket error:",t),"v2"===this.protocol&&window.dispatchEvent(new CustomEvent("socketError",{detail:t}))})),this.socket.on("disconnect",(()=>{this.socketId&&h.instances.delete(this.socketId),this.authenticated=!1}))})),this.connectionPromise=this.connectionPromise.catch((t=>{console.error("Socket connection failed:",t.message)}))}}clearSocketListeners(){this.socket&&(this.socket.off("connect"),this.socket.off("response"),this.socket.off("end_response"),this.socket.off("chunk"),this.socket.off("complete"),this.socket.off("authenticated"),this.socket.off("close"),this.socket.off("error"),this.socket.off("disconnect"))}static createInstance(t,s=""){return new h(t,s)}static getInstanceById(t){return h.instances.get(t)}getSocket(){return this.socket}getSocketId(){return this.socketId}getProtocol(){return this.protocol}isAuthenticated(){return"legacy"===this.protocol||this.authenticated}async waitForConnection(){this.connectionPromise&&await this.connectionPromise}isConnected(){var t,s;return null!==(s=null===(t=this.socket)||void 0===t?void 0:t.connected)&&void 0!==s&&s}disconnect(){this.socket&&(this.clearSocketListeners(),this.socket.disconnect(),this.socket=null),this.socketId&&(h.instances.delete(this.socketId),this.socketId=""),this.authenticated=!1}static disconnectSocketById(t){const s=h.instances.get(t);s&&s.socket?(s.socket.disconnect(),h.instances.delete(t)):console.error(`Socket with ID ${t} not found`)}}h.instances=new Map;export{h as S}