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