import{a as I,b as E,c as p,f as i,g as v,h as R,o as H}from"./chunk-2NZJ7CN2.js";import{v as w}from"./chunk-KRYMOHYF.js";import{J as f,O as m,e as u,l as h,m as d,na as l,ra as b,sa as g,tb as S,v as o}from"./chunk-4BRW6LCZ.js";var O=class e{constructor(r){this.zone=r}ws;url=i.production===!0?`ws:${window.location.host}/ws`:"ws://127.0.0.1:3210/ws";pending=[];state$=new d("idle");inbound$=new h;reconnectAttempt=0;manualClose=!1;opening=!1;reconnectSub;stateChanges(){return this.state$.asObservable()}messages(){return this.inbound$.asObservable()}connect(){this.opening||this.ws&&(this.ws.readyState===WebSocket.OPEN||this.ws.readyState===WebSocket.CONNECTING)||(this.manualClose=!1,this.open())}close(){this.manualClose=!0,this.cancelReconnect(),this.stopHeartbeat(),this.ws?.close(),this.ws=void 0,this.opening=!1,this.state$.next("closed")}send(r){let s=JSON.stringify(r);this.ws?.readyState===WebSocket.OPEN?this.ws.send(s):this.pending.push(r)}isOpen(){return this.state$.value==="open"}open(){this.opening||this.ws&&(this.ws.readyState===WebSocket.OPEN||this.ws.readyState===WebSocket.CONNECTING)||(this.opening=!0,this.state$.next("connecting"),this.cancelReconnect(),this.zone.runOutsideAngular(()=>{let r=new WebSocket(this.url);this.ws=r,r.onopen=()=>{this.reconnectAttempt=0,this.opening=!1,this.cancelReconnect(),this.pending.splice(0).forEach(n=>this.send(n)),this.zone.run(()=>this.state$.next("open")),this.startHeartbeat()},r.onmessage=s=>{let n;try{n=JSON.parse(s.data)}catch{return}this.zone.run(()=>this.inbound$.next(n))},r.onerror=()=>{this.zone.run(()=>this.state$.next("error"));try{r.close()}catch{}},r.onclose=()=>{this.opening=!1,this.ws=void 0,this.stopHeartbeat(),this.zone.run(()=>this.state$.next("closed")),this.manualClose||this.scheduleReconnect()}}))}heartbeatTimer;startHeartbeat(){this.stopHeartbeat(),this.heartbeatTimer=setInterval(()=>{this.send({op:"ping"})},2e4)}stopHeartbeat(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=void 0)}scheduleReconnect(){if(this.reconnectSub)return;this.reconnectAttempt++;let r=Math.min(1e3*Math.pow(2,this.reconnectAttempt),15e3);this.reconnectSub=f(r).subscribe(()=>{this.reconnectSub=void 0,!this.manualClose&&this.open()})}cancelReconnect(){this.reconnectSub?.unsubscribe(),this.reconnectSub=void 0}static \u0275fac=function(s){return new(s||e)(b(S))};static \u0275prov=l({token:e,factory:e.\u0275fac,providedIn:"root"})};var a=u(I());var N=u(H());function c(e){return typeof e=="object"&&e!==null}function W(e){if(c(e.error)){let r=e.error.error,s=c(r)?r:void 0,n=p(e.error.code);if(n!==void 0)return n;let t=p(s?.code);if(t!==void 0)return t}return e.status===0?E.HTTP_ERROR:e.status===401?a.GlobalErrorCodes.UNAUTHORIZED:a.GlobalErrorCodes.INTERNAL_ERROR}function T(e){if(c(e.error)){let r=e.error.error,n=(c(r)?r:void 0)?.message;if(typeof n=="string"&&n.trim())return n;let t=e.error.message;if(typeof t=="string"&&t.trim())return t}return typeof e.message=="string"&&e.message.trim()?e.message:"\u8BF7\u6C42\u5931\u8D25"}var X=(e,r)=>{let s=g(v),n=N.uniqueId("req-");return e=e.clone({setHeaders:{"X-Request-Id":n}}),r(e).pipe(m(t=>{if(i.production||console.error(`[HTTP][${n}] Error:`,t),t instanceof R)return s.dispatch(t.code,t.message,t.details),o(()=>t);if(t instanceof w){let C=W(t),y=T(t);return s.dispatch(C,y,t.error),o(()=>t)}let x=t instanceof Error?t.message:"\u672A\u77E5\u9519\u8BEF";return s.dispatch(a.GlobalErrorCodes.INTERNAL_ERROR,x,t),o(()=>t)}))};export{O as a,X as b};
