"use strict";var b=Object.defineProperty;var U=(S,e,a)=>e in S?b(S,e,{enumerable:!0,configurable:!0,writable:!0,value:a}):S[e]=a;var P=(S,e,a)=>U(S,typeof e!="symbol"?e+"":e,a);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("lodash"),F=require("./sources/static.js"),A=require("./sources/url.js"),j=require("./sources/variable.js"),v=require("./sources/api.js"),K=require("./sources/ws.js"),I=require("./utils/utils.js"),x=require("./sources/storage.js"),g=class g{static addParser(e,a){this.parsers.add({type:e,parser:a})}static getParser(e){const a=Array.from(this.parsers).find(t=>t.type===e);return a==null?void 0:a.parser}static parse({sources:e,callback:a,tId:t,isStore:s=!1,noUseMapping:i=!1,isInterval:n=!0}){return a??(a=()=>{}),e&&y.isArray(e)&&e.length>0?new Promise((u,o)=>{I.DataSourceUtils.cleanupPreviousWebSockets(this.wsInstances,e.map(r=>r.id),t);const c=new Set,h=this.wsInstances.get(t)||new Set;e.forEach(r=>{var m;if(n){const{source:l}=r,d=`${t}-${r.id}`,w=this.datasourceTimer.findIndex(D=>D.id===d);if(w!==-1&&(clearInterval(this.datasourceTimer[w].timer),this.datasourceTimer.splice(w,1)),l.isAutoUpdate){const D=setInterval(()=>{var f;(f=this.task(r,s,i,t,p=>{a(p)}))==null||f.then(p=>{u(p)})},l.autoUpdateTime*1e3);this.datasourceTimer.push({id:d,timer:D})}}if(r.source.type!=="ws"){(m=this.task(r,s,i,t,l=>a(l)))==null||m.then(l=>u(l));return}if(I.DataSourceUtils.hasValidWebSocket(this.wsInstances,t,r.id))h.forEach(l=>{l.getId()===r.id&&c.add(l)});else{const l=K.parseWebSocket(r,r.id,i,t,d=>{u(d),a({data:[{data:d.finalUserData.data}]}),this.taskStorage(e,d.rawData.data)});c.add(l)}}),c.size>0&&this.wsInstances.set(t,c)}):(a([]),Promise.resolve([]))}static task(e,a=!1,t=!1,s,i=()=>{}){return e?new Promise((n,u)=>{let o=null;const{source:c}=e,h=this.getParser(c.type);h&&(o=h(e,e.id,t)),o.then(r=>{i(t?r.noMappingData:r.finalKeyData),this.taskStorage(e,r.rawData.data),n(r)})}):(i([]),Promise.resolve([]))}static filterData(e,a){return new Promise((t,s)=>{if(y.isString(e)){try{const i=I.DataSourceUtils.getVariableData();let n=`
                        ${e}
            
                        // 在这里调用 filter 函数，并传递 data 参数
                        return filter(data);
                    `;n=n.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g,""),n=I.DataSourceUtils.replaceStringVariables(n,i);const u=new Function("data",n);t(u(a))}catch{t(a)}return}t(a)})}static processData(e,a,t,s,i,n){return new Promise(function(u){const o=y.cloneDeep({id:s,data:a});g.filterData(e,a).then(c=>{const h=y.cloneDeep({id:s,data:c});if(n)u({id:s,finalKeyData:[],finalUserData:{id:s,data:c},filteredData:h,rawData:o,noMappingData:c});else{let r=[];y.isArray(c)&&(r=c.map((d,w)=>{const D={};if(i&&i&&w===0){const f=[];for(const p in d){const T=t.find(q=>q.name===p);T?f.push({alias:T.alias,name:p,type:"any",label:p,mapping:T.mapping,status:!0}):f.push({alias:p,name:p,type:"any",label:p,mapping:p,status:!0})}t.splice(0,t.length),t.push(...f)}return t.forEach(f=>{f.alias?D[f.alias]=d[f.mapping]:D[f.name]=d[f.mapping]}),D}));const m=y.cloneDeep({id:s,data:r}),l=y.cloneDeep({id:s,data:g.parseMappedData(r,t,s)});u({id:s,finalKeyData:r.length===0?{id:s,data:c}:l,finalUserData:r.length===0?{id:s,data:c}:m,filteredData:h,rawData:o,noMappingData:[]})}})})}static parseMappedData(e,a,t){return e.reduce((s,i)=>{let n="系列一";a&&a.find(o=>o.name==="series")&&(n=i[a.find(o=>o.name==="series").name],delete i[a.find(o=>o.name==="series").name]);const u=s.find(o=>o.key===n);return u?u.data.push(i):s.push({key:n,data:[i]}),s},[])}static taskStorage(e,a){const{source:t}=e;t.storage&&t.storage.setField&&t.storage.setType!==0&&(t.storage.setType===1&&localStorage.setItem("Local_"+t.storage.setField,JSON.stringify(a)),t.storage.setType===2&&sessionStorage.setItem("Session_"+t.storage.setField,JSON.stringify(a)))}};P(g,"wsInstances",new Map),P(g,"datasourceTimer",[]),P(g,"parsers",new Set([{type:"url",parser:A.parseUrlData},{type:"static",parser:F.parseStaticData},{type:"variable",parser:j.parseVariableData},{type:"api",parser:v.parseAPIPort},{type:"storage",parser:x.parseStorage}]));let k=g;exports.SHJDatasourceV2=k;
