import{n as e}from"./rolldown-runtime.CYVBeYYp.js";var t=class{constructor(e){if(this._retries=0,!Number.isInteger(e)||e<0)throw Error(`Backoff must be a positive integer`);this.backoff=e}get retries(){return this._retries}get current(){return this.backoff}next(){return this._retries++,this.backoff}reset(){this._retries=0}},n=class{constructor(e,t){if(this._retries=0,!Number.isInteger(e)||e<0)throw Error(`Base must be a positive integer or zero`);if(t!==void 0&&(!Number.isInteger(t)||t<0))throw Error(`ExpMax must be a undefined, a positive integer or zero`);this.base=e,this.expMax=t,this.i=0}get retries(){return this._retries}get current(){return this.base*2**this.i}next(){return this._retries++,this.i=this.expMax===void 0?this.i+1:Math.min(this.i+1,this.expMax),this.current}reset(){this._retries=0,this.i=0}},r=class{constructor(e,t,n){if(this.i=0,this._retries=0,e<0)throw Error(`Initial must be a positive number or zero`);if(t<0)throw Error(`Increment must be a positive number or zero`);if(n!==void 0&&n<0)throw Error(`Max must be undefined, a positive number or zero`);if(n!==void 0&&nthis.handleEvent(o.open,e),this.handleErrorEvent=e=>this.handleEvent(o.error,e),this.handleCloseEvent=e=>this.handleEvent(o.close,e),this.handleMessageEvent=e=>this.handleEvent(o.message,e),this._url=e,this._protocols=t,this._options={buffer:n?.buffer,retry:{maxRetries:n?.retry?.maxRetries,instantReconnect:n?.retry?.instantReconnect,backoff:n?.retry?.backoff},listeners:{open:[...n?.listeners?.open??[]],close:[...n?.listeners?.close??[]],error:[...n?.listeners?.error??[]],message:[...n?.listeners?.message??[]],retry:[...n?.listeners?.retry??[]],reconnect:[...n?.listeners?.reconnect??[]]}},this._underlyingWebsocket=this.tryConnect()}get url(){return this._url}get protocols(){return this._protocols}get buffer(){return this._options.buffer}get maxRetries(){return this._options.retry.maxRetries}get instantReconnect(){return this._options.retry.instantReconnect}get backoff(){return this._options.retry.backoff}get closedByUser(){return this._closedByUser}get lastConnection(){return this._lastConnection}get underlyingWebsocket(){return this._underlyingWebsocket}get readyState(){return this._underlyingWebsocket.readyState}get bufferedAmount(){return this._underlyingWebsocket.bufferedAmount}get extensions(){return this._underlyingWebsocket.extensions}get binaryType(){return this._underlyingWebsocket.binaryType}set binaryType(e){this._underlyingWebsocket.binaryType=e}send(e){this.closedByUser||(this._underlyingWebsocket.readyState===this._underlyingWebsocket.OPEN?this._underlyingWebsocket.send(e):this.buffer!==void 0&&this.buffer.add(e))}close(e,t){this.cancelScheduledConnectionRetry(),this._closedByUser=!0,this._underlyingWebsocket.close(e,t)}addEventListener(e,t,n){this._options.listeners[e].push({listener:t,options:n})}removeEventListener(e,t,n){let r=e=>e.listener!==t||e.options!==n;this._options.listeners[e]=this._options.listeners[e].filter(r)}tryConnect(){return this._underlyingWebsocket=new WebSocket(this.url,this.protocols),this._underlyingWebsocket.addEventListener(o.open,this.handleOpenEvent),this._underlyingWebsocket.addEventListener(o.close,this.handleCloseEvent),this._underlyingWebsocket.addEventListener(o.error,this.handleErrorEvent),this._underlyingWebsocket.addEventListener(o.message,this.handleMessageEvent),this._underlyingWebsocket}clearWebsocket(){this._underlyingWebsocket.removeEventListener(o.open,this.handleOpenEvent),this._underlyingWebsocket.removeEventListener(o.close,this.handleCloseEvent),this._underlyingWebsocket.removeEventListener(o.error,this.handleErrorEvent),this._underlyingWebsocket.removeEventListener(o.message,this.handleMessageEvent),this._underlyingWebsocket.close()}dispatchEvent(e,t){let n=this._options.listeners[e],r=[];n.forEach(({listener:e,options:n})=>{e(this,t),(n===void 0||n.once===void 0||!n.once)&&r.push({listener:e,options:n})}),this._options.listeners[e]=r}handleEvent(e,t){switch(e){case o.close:this.dispatchEvent(e,t),this.scheduleConnectionRetryIfNeeded();break;case o.open:if(this.backoff!==void 0&&this._lastConnection!==void 0){let e={retries:this.backoff.retries,lastConnection:new Date(this._lastConnection)},t=new CustomEvent(o.reconnect,{detail:e});this.dispatchEvent(o.reconnect,t),this.backoff.reset()}this._lastConnection=new Date,this.dispatchEvent(e,t),this.sendBufferedData();break;case o.retry:this.dispatchEvent(e,t),this.clearWebsocket(),this.tryConnect();break;default:this.dispatchEvent(e,t);break}}sendBufferedData(){if(this.buffer!==void 0)for(let e=this.buffer.read();e!==void 0;e=this.buffer.read())this.send(e)}scheduleConnectionRetryIfNeeded(){if(this.closedByUser||this.backoff===void 0)return;let e=e=>{let t=new CustomEvent(o.retry,{detail:e});this.handleEvent(o.retry,t)},t={backoff:this._options.retry.instantReconnect===!0?0:this.backoff.next(),retries:this._options.retry.instantReconnect===!0?0:this.backoff.retries,lastConnection:this._lastConnection};(this._options.retry.maxRetries===void 0||t.retries<=this._options.retry.maxRetries)&&(this.retryTimeout=globalThis.setTimeout(()=>e(t),t.backoff))}cancelScheduledConnectionRetry(){globalThis.clearTimeout(this.retryTimeout)}},c=class{constructor(e){this._url=e}get url(){return this._url}withProtocols(e){return this._protocols=e,this}get protocols(){return this._protocols}withMaxRetries(e){return this._options=Object.assign(Object.assign({},this._options),{retry:Object.assign(Object.assign({},this._options?.retry),{maxRetries:e})}),this}get maxRetries(){return this._options?.retry?.maxRetries}withInstantReconnect(e){return this._options=Object.assign(Object.assign({},this._options),{retry:Object.assign(Object.assign({},this._options?.retry),{instantReconnect:e})}),this}get instantReconnect(){return this._options?.retry?.instantReconnect}withBackoff(e){return this._options=Object.assign(Object.assign({},this._options),{retry:Object.assign(Object.assign({},this._options?.retry),{backoff:e})}),this}get backoff(){return this._options?.retry?.backoff}withBuffer(e){return this._options=Object.assign(Object.assign({},this._options),{buffer:e}),this}get buffer(){return this._options?.buffer}onOpen(e,t){return this.addListener(o.open,e,t),this}onClose(e,t){return this.addListener(o.close,e,t),this}onError(e,t){return this.addListener(o.error,e,t),this}onMessage(e,t){return this.addListener(o.message,e,t),this}onRetry(e,t){return this.addListener(o.retry,e,t),this}onReconnect(e,t){return this.addListener(o.reconnect,e,t),this}build(){return new s(this._url,this._protocols,this._options)}addListener(e,t,n){return this._options=Object.assign(Object.assign({},this._options),{listeners:{open:this._options?.listeners?.open??[],close:this._options?.listeners?.close??[],error:this._options?.listeners?.error??[],message:this._options?.listeners?.message??[],retry:this._options?.listeners?.retry??[],reconnect:this._options?.listeners?.reconnect??[],[e]:[...this._options?.listeners?.[e]??[],{listener:t,options:n}]}}),this}},l=e({ArrayQueue:()=>i,ConstantBackoff:()=>t,ExponentialBackoff:()=>n,LinearBackoff:()=>r,RingQueue:()=>a,Websocket:()=>s,WebsocketBuilder:()=>c,WebsocketEvent:()=>o});export{l as t};