import EventEmitter from"./event-emitter";import WebsocketHeartbeatJs from"websocket-heartbeat-js";import{isDebugger,randomString}from"./util";import deepClone from"lodash/cloneDeep";var RuntimeLogActionType=function(a){return a.START="start",a.STOP="stop",a.PROPERTY="property",a.RULE="rule",a.EVENT="event",a.TASK="task",a.PATTERN="pattern",a.COMMAND="command",a}(RuntimeLogActionType||{});export let SocketResponseType=function(a){return a.heartbeat="SPACE_SUBSCRIBE_HEARTBEAT",a.spaceResourceDetail="SPACE_RESOURCE_DETAIL",a.spaceResourceChange="SPACE_RESOURCE_CHANGE",a.spaceInfo="SPACE_INFO",a.spaceDeviceBind="SPACE_DEVICE_BIND",a.spaceDeviceOnoffline="SPACE_DEVICE_ONOFFLINE",a.deviceProperty="DEVICE_PROPERTY",a.deviceInfoChange="DEVICE_INFO_CHANGE",a.runtimeLog="RUNTIME_LOG",a.spaceEvent="SPACE_EVENT",a.spaceProperty="SPACE_PROPERTY",a.spaceInstanceInfo="SPACE_INSTANCE_INFO",a.spaceNodeInfo="SPACE_NODE_INFO",a.spaceMessagePlugin="SPACE_MESSAGE_PLUGIN",a.spaceInstanceInit="SPACE_INSTANCE_INIT",a}({});export class SocketInterceptor{constructor(a){this.requestList=[],this.responseList=[],null!==a&&void 0!==a&&a.interceptor&&(a.interceptor.request&&this.requestList.push(a.interceptor.request),(null===a||void 0===a?void 0:a.interceptor.response)&&this.responseList.push(a.interceptor.response))}request(a){this.requestList.push(a)}response(a){this.responseList.push(a)}merge(a){return a.requestList.forEach(a=>{this.request(a)}),a.responseList.forEach(a=>{this.response(a)}),this}}class Socket extends EventEmitter{constructor(a,b,c){super(),this.ws=void 0,this.config=void 0,this.interceptor=void 0,this.channelId=void 0,this.appCode=void 0,this.AllowEmpty=!1,this.appCode=a,this.config=b,this.interceptor=c}removeListener(a,b){super.removeEventListener(a,b),0!==this.count("message")||this.AllowEmpty||this.close()}removeAllListener(a){super.removeAllListener(a),0!==this.count("message")||this.AllowEmpty||this.close()}addListener(a,b){return super.addEventListener(a,b),this.ws||this.connect(),this}emit(a,...b){let c=this.$listeners[a]||[];const d=c.length;for(let e=0;e<d;e++)c[e].apply(this,deepClone(b))}connect(){this.ws||(this.interceptor&&(this.config=this.interceptor.requestList.reduce((a,b)=>b(this.appCode,a),this.config)),this.ws=new WebsocketHeartbeatJs({get pingMsg(){return JSON.stringify({id:randomString(12),time:new Date().getTime(),type:SocketResponseType.heartbeat,data:SocketResponseType.heartbeat})},...this.config,protocols:this.appCode,url:this.config.url}),this.ws.onmessage=a=>{try{let b=JSON.parse(a.data);if(isDebugger()&&void 0,b.type!==SocketResponseType.heartbeat){this.interceptor&&(b=this.interceptor.responseList.reduce((a,b)=>b(a),b));let a;try{a=JSON.parse(b.payload)}catch(c){a=b.payload}b.spaceId&&Array.isArray(a)&&a.forEach(a=>{a.spaceId=b.spaceId,a.spaceName=b.spaceName,a.traceId=b.traceId}),this.emit("message",{messageId:b.id,messageType:b.type,event:"message",data:a})}else b.type===SocketResponseType.heartbeat&&("connected"===b.payload?this.emit("socket",{event:"socket",type:"connected"}):"heartbeat"===b.payload&&this.emit("socket",{event:"socket",type:"heartbeat"}))}catch(b){throw new Error(JSON.stringify(a))}},this.ws.onerror=()=>{this.emit("socket",{event:"socket",type:"error"})},this.ws.onclose=()=>{this.emit("socket",{event:"socket",type:"close"})},this.ws.onopen=()=>{this.ws.send("connected"),this.emit("socket",{event:"socket",type:"open"})},this.ws.onreconnect=()=>{this.ws.send("connected"),this.emit("socket",{event:"socket",type:"reconnect"})})}send(a){if(!this.ws)throw Error("ws is undefined");let b=a;this.interceptor&&(b=this.interceptor.requestList.reduce((a,b)=>b(this.appCode,a),a)),this.ws.send(b)}close(){this.ws&&this.ws.close(),this.ws=void 0}}export default Socket;export function socketSignInterceptor(){return(a,b)=>{var c;return null!==(c=b.url)&&void 0!==c&&c.includes("/ws/node/spaceInstanceStatus")&&(b.protocols=a),b}}