"use strict";var p=Object.defineProperty;var f=(c,t,e)=>t in c?p(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e;var d=(c,t,e)=>f(c,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("../index.js"),u=require("../utils/wsManager.js"),l=require("../utils/utils.js");class m{constructor(t,e,s,i,n,o){d(this,"ws",null);d(this,"reconnectAttempts",0);d(this,"timeoutTimer");d(this,"isDisconnecting",!1);d(this,"config");this.source=t,this.id=e,this.noUseMapping=s,this.tId=i,this.onData=n,this.onEvent=o;const{websocket:r}=t.source;if(!r){n==null||n(l.DataSourceUtils.noneData(e)),this.config={url:"",reconnect:!1,timeout:3e4};return}this.config=r,this.connect()}getId(){return this.id}isConnected(){return this.ws!==null&&this.ws.readyState===WebSocket.OPEN}processProtocols(){const{protocols:t=""}=this.config;return t?Array.isArray(t)?t:t.split(/\s*,\s*/):[]}injectHeadersToUrl(t){try{const e=new URL(t);return Object.entries(this.config.headers||{}).forEach(([s,i])=>{e.searchParams.append(s,i)}),e.toString()}catch{return t}}connect(){var r,a;if(this.isDisconnecting)return;const{url:t,isHeaders:e=!1,binaryType:s="blob",timeout:i=3e4}=this.config,n=e?this.injectHeadersToUrl(t):t,o=this.processProtocols();u.wsManager.closeByComponentIds(this.tId);try{this.ws=o.length>0?new WebSocket(n,o):new WebSocket(n),this.ws&&(u.wsManager.add(this.ws,this.tId),this.ws.binaryType=s,this.timeoutTimer=setTimeout(()=>{var h,g;((h=this.ws)==null?void 0:h.readyState)===WebSocket.CONNECTING&&(this.ws.close(),(g=this.onEvent)==null||g.call(this,"error","Connection timeout"),this.handleClose({code:4001,reason:"Timeout"}))},i),this.ws.onopen=this.handleOpen.bind(this),this.ws.onmessage=this.handleMessage.bind(this),this.ws.onerror=this.handleError.bind(this),this.ws.onclose=this.handleClose.bind(this),(r=this.onEvent)==null||r.call(this,"connecting"))}catch(h){(a=this.onEvent)==null||a.call(this,"error",h),this.scheduleReconnect()}}handleOpen(){var s,i,n,o;this.timeoutTimer&&clearTimeout(this.timeoutTimer),(s=this.onEvent)==null||s.call(this,"connected"),this.reconnectAttempts=0;const{message:t,isMessage:e}=this.config;if(t&&e)try{const r=l.DataSourceUtils.getVariableData(),a=typeof t=="string"?l.DataSourceUtils.replaceStringVariables(t,r):l.DataSourceUtils.replaceObjectVariables(t,r),h=typeof a=="string"?a:JSON.stringify(a);(i=this.ws)==null||i.send(h),(n=this.onEvent)==null||n.call(this,"messageSent",h)}catch{(o=this.onEvent)==null||o.call(this,"error","Failed to send initial message")}}async handleMessage(t){var e,s,i,n;try{let o=t.data;if(typeof o=="string")try{o=JSON.parse(o)}catch{}const r=await w.SHJDatasourceV2.processData(this.source.source.filter,o,this.source.source.mapping,this.id,this.source.source.dynamicMapping,this.noUseMapping,this.source.source.isUserdata);(e=this.onData)==null||e.call(this,r),(s=this.onEvent)==null||s.call(this,"data",r)}catch{(i=this.onEvent)==null||i.call(this,"error","Data processing failed"),(n=this.onData)==null||n.call(this,l.DataSourceUtils.noneData(this.id))}}handleError(t){var e,s,i;(e=this.onEvent)==null||e.call(this,"error",t),((s=this.ws)==null?void 0:s.readyState)!==WebSocket.OPEN&&((i=this.onData)==null||i.call(this,l.DataSourceUtils.noneData(this.id)))}handleClose(t){var e,s;this.timeoutTimer&&clearTimeout(this.timeoutTimer),(e=this.onEvent)==null||e.call(this,"disconnected",{code:t.code,reason:t.reason}),this.shouldReconnect(t)?this.scheduleReconnect():(s=this.onData)==null||s.call(this,l.DataSourceUtils.noneData(this.id))}shouldReconnect(t){const{reconnect:e=!1,maxReconnectAttempts:s=10}=this.config;return!this.isDisconnecting&&e&&this.reconnectAttempts<s&&t.code!==1e3&&t.code!==4001}scheduleReconnect(){var s;if(this.isDisconnecting)return;const{reconnectInterval:t=5e3}=this.config;this.reconnectAttempts++;const e=t*Math.pow(2,this.reconnectAttempts-1);(s=this.onEvent)==null||s.call(this,"reconnecting",{attempt:this.reconnectAttempts,delay:e}),setTimeout(()=>this.connect(),Math.min(e,3e4))}send(t){if(!this.ws||this.ws.readyState!==WebSocket.OPEN)throw new Error("WebSocket 未连接");const e=typeof t=="string"?t:JSON.stringify(t);this.ws.send(e)}disconnect(){this.isDisconnecting=!0,this.ws&&(this.ws.close(1e3,"Manual close"),this.timeoutTimer&&clearTimeout(this.timeoutTimer),u.wsManager.closeByComponentIds(this.tId))}getWebSocket(){return this.ws}}const S=(c,t,e,s,i,n)=>new m(c,t,e,s,i,n);exports.WebSocketInstance=m;exports.parseWebSocket=S;
