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