var U = Object.defineProperty;
var A = (w, t, a) => t in w ? U(w, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : w[t] = a;
var I = (w, t, a) => A(w, typeof t != "symbol" ? t + "" : t, a);
import { isArray as F, isString as M, cloneDeep as P } from "lodash";
import { parseStaticData as b } from "./sources/static.mjs";
import { parseUrlData as j } from "./sources/url.mjs";
import { parseVariableData as E } from "./sources/variable.mjs";
import { parseAPIPort as W } from "./sources/api.mjs";
import { parseWebSocket as $ } from "./sources/ws.mjs";
import { DataSourceUtils as T } from "./utils/utils.mjs";
import { parseStorage as N } from "./sources/storage.mjs";
import { wsManager as Z } from "./utils/wsManager.mjs";
import "vue";
import "./utils/request.mjs";
const D = class D {
  static addParser(t, a) {
    this.parsers.add({ type: t, parser: a });
  }
  static getParser(t) {
    const a = Array.from(this.parsers).find((e) => e.type === t);
    return a == null ? void 0 : a.parser;
  }
  /**
   * 解析数据源
   * @param sources 数据源
   * @param callback 回调函数
   * @param tId 组件id
   * @param isStore 是否存储
   * @param noUseMapping 是否不使用映射
   * @param isInterval 是否开启定时任务
   * @returns
   */
  static parse({ sources: t, callback: a, tId: e, isStore: s = !1, noUseMapping: i = !1, isInterval: n = !0 }) {
    return a ?? (a = () => {
    }), t && F(t) && t.length > 0 && t[0] ? new Promise((c, o) => {
      T.cleanupPreviousWebSockets(this.wsInstances, t.map((r) => r.id), e);
      const h = /* @__PURE__ */ new Set(), l = this.wsInstances.get(e) || /* @__PURE__ */ new Set();
      t.forEach((r) => {
        var m;
        if (n) {
          const { source: f } = r;
          if (!f) {
            a([]), c([]);
            return;
          }
          const u = `${e}-${r.id}`, d = this.datasourceTimer.findIndex((S) => S.id === u);
          if (d !== -1 && (clearInterval(this.datasourceTimer[d].timer), this.datasourceTimer.splice(d, 1)), f.isAutoUpdate) {
            const S = setInterval(() => {
              var y;
              (y = this.task(r, s, i, e, (p) => {
                a(p);
              })) == null || y.then((p) => {
                c(p);
              });
            }, f.autoUpdateTime * 1e3);
            this.datasourceTimer.push({ id: u, timer: S });
          }
        }
        if (r.source.type !== "ws") {
          (m = this.task(r, s, i, e, (f) => a(f))) == null || m.then((f) => c(f));
          return;
        }
        if (T.hasValidWebSocket(this.wsInstances, e, r.id))
          l.forEach((f) => {
            f.getId() === r.id && h.add(f);
          });
        else {
          const f = $(r, r.id, i, e, (u) => {
            c(u), a({ data: [{ data: u.finalUserData.data }] }), this.taskStorage(t, u.finalUserData.data);
          });
          h.add(f);
        }
      }), h.size > 0 && this.wsInstances.set(e, h);
    }) : (a([]), Promise.resolve([]));
  }
  /**
   * 执行数据源任务
   * @param sources 数据源
   * @param isStore 是否存储
   * @param noUseMapping 是否不使用映射
   * @param tId 组件id
   * @param callback 回调函数
   * @returns
   */
  static task(t, a = !1, e = !1, s, i = () => {
  }) {
    return t ? new Promise((n, c) => {
      let o = null;
      const { source: h } = t, l = this.getParser(h.type);
      l && (o = l(t, t.id, e)), o.then((r) => {
        n(r), !e && t.source.mapping && t.source.mapping.length > 0 ? (this.taskStorage(t, r.finalKeyData), i(r.finalKeyData)) : (this.taskStorage(t, r.noMappingData), i(r.noMappingData));
      });
    }) : (i([]), Promise.resolve([]));
  }
  /**
   * 过滤数据
   * @param filter 过滤器
   * @param data 数据
   * @returns
   */
  static filterData(t, a) {
    return new Promise((e, s) => {
      if (M(t)) {
        try {
          const i = T.getVariableData();
          let n = `
                        ${t}
            
                        // 在这里调用 filter 函数，并传递 data 参数
                        return filter(data);
                    `;
          n = n.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""), n = T.replaceStringVariables(n, i);
          const c = new Function("data", n);
          e(c(a));
        } catch {
          e(a);
        }
        return;
      }
      e(a);
    });
  }
  /**
   * 处理数据
   * @param filter 过滤器
   * @param data 数据
   * @param mappings 映射
   * @param id 组件id
   * @param dynamicMapping 是否动态映射
   * @param noUseMapping 是否不使用映射
   * @returns
   */
  static processData(t, a, e, s, i, n, c) {
    return new Promise(function(o) {
      const h = P({ id: s, data: a });
      D.filterData(t, a).then((l) => {
        const r = P({ id: s, data: l });
        if (!n && e && e.length > 0) {
          let m = [];
          F(l) && (m = l.map((d, S) => {
            const y = {};
            if (i && i && S === 0) {
              const p = [];
              for (const g in d) {
                const k = e.find((K) => K.name === g);
                k ? p.push({ alias: k.alias, name: g, type: "any", label: g, mapping: k.mapping, status: !0 }) : p.push({ alias: g, name: g, type: "any", label: g, mapping: g, status: !0 });
              }
              e.splice(0, e.length), e.push(...p);
            }
            return e.forEach((p) => {
              try {
                p.alias ? y[p.alias] = d[p.mapping] : y[p.name] = d[p.mapping];
              } catch {
              }
            }), c && (y.userdata = d), y;
          }));
          const f = P({ id: s, data: m }), u = P({ id: s, data: D.parseMappedData(m, e, s) });
          o({
            id: s,
            finalKeyData: m.length === 0 ? { id: s, data: l } : u,
            finalUserData: m.length === 0 ? { id: s, data: l } : f,
            filteredData: r,
            rawData: h,
            noMappingData: []
          });
        } else
          o({
            id: s,
            finalKeyData: [],
            finalUserData: { id: s, data: l },
            filteredData: r,
            rawData: h,
            noMappingData: l
          });
      });
    });
  }
  /**
   * 解析数据为Key、Data格式
   * @param data 数据
   * @param mappings 映射
   * @param id 组件id
   * @returns
   */
  static parseMappedData(t, a, e) {
    return t.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 c = s.find((o) => o.key === n);
        return c ? c.data.push(i) : s.push({ key: n, data: [i] }), s;
      },
      []
    );
  }
  /**
   * 处理数据存储
   * @param sources 数据源
   * @returns
   */
  static taskStorage(t, a) {
    const { source: e } = t;
    e.storage && e.storage.setField && e.storage.setType !== 0 && (e.storage.setType === 1 && localStorage.setItem("Local_" + e.storage.setField, JSON.stringify(a)), e.storage.setType === 2 && sessionStorage.setItem("Session_" + e.storage.setField, JSON.stringify(a)));
  }
};
I(D, "wsInstances", /* @__PURE__ */ new Map()), I(D, "datasourceTimer", []), I(D, "parsers", /* @__PURE__ */ new Set([
  { type: "url", parser: j },
  { type: "static", parser: b },
  { type: "variable", parser: E },
  { type: "api", parser: W },
  { type: "storage", parser: N }
]));
let x = D;
export {
  T as DataSourceUtils,
  x as SHJDatasourceV2,
  Z as wsManager
};
