var m = Object.defineProperty;
var f = (a, e, t) => e in a ? m(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
var l = (a, e, t) => f(a, typeof e != "symbol" ? e + "" : e, t);
import { cloneDeep as y } from "lodash";
import { executeLinkAction as h } from "./actions/linkAction.mjs";
import { executeMoveAction as A } from "./actions/moveAction.mjs";
import { executeSendApiAction as x } from "./actions/sendApiAction.mjs";
import { executeScaleAction as g } from "./actions/scaleAction.mjs";
import { executeRotateAction as S } from "./actions/rotateAction.mjs";
import { executeUpdatePageAction as E } from "./actions/updatePageAction.mjs";
import { executeInvokeAction as b } from "./actions/invokeAction.mjs";
import { executeFullScreenAction as v } from "./actions/fullScreenAction.mjs";
import { executeUpdateVariableAction as V } from "./actions/updateVariableAction.mjs";
import { executeUpdateWidgetAction as w } from "./actions/updateWidgetAction.mjs";
import { executeUeCommonsWebscoketAction as M } from "./actions/ueCommonsWebscoketAction.mjs";
import { executeUeShjExecMethodAction as U } from "./actions/ueShjExecMethodAction.mjs";
import { executeUnityWebglExecMethodAction as k } from "./actions/unityWebglExecMethodAction.mjs";
import { executeUnityIframeExecMethodAction as C } from "./actions/unityIframeExecMethodAction.mjs";
import { executeVrSceneMethodAction as N } from "./actions/vrSceneMethodAction.mjs";
import { executeRefreshVariableAction as W } from "./actions/refreshVariableAction.mjs";
import { DataSourceUtils as d } from "../datasource/utils/utils.mjs";
import "../datasource/utils/request.mjs";
import "mqtt";
import "nanoid";
class F {
  /**
   * 添加事件解析器
   * @param type 事件类型
   * @param parser 事件解析器
   */
  static addAction(e, t) {
    const i = Array.from(this.actions).find(
      (o) => o.type === e
    );
    i && this.actions.delete(i), this.actions.add({ type: e, parser: t });
  }
  /**
   * 获取事件解析器
   * @param type 事件类型
   * @returns 事件解析器
   */
  static getAction(e) {
    const t = Array.from(this.actions).find((i) => i.type === e);
    return t == null ? void 0 : t.parser;
  }
  /**
   * 解析事件
   * @param events 事件
   * @param type 事件类型
   * @param params 参数
   */
  static parseEvents(e, t, i) {
    e != null && e.length && e.forEach((o, u) => {
      o.eventType === t && setTimeout(
        () => {
          const p = new CustomEvent("event-params-log", {
            detail: {
              eventId: o.id,
              params: i
            }
          });
          window.dispatchEvent(p);
          const s = this.filterEvent(o, i);
          this.conditionLogic(o, s) && o.actionsList.forEach((n) => {
            const r = this.getAction(n.type);
            r && r(n.action, s);
          });
        },
        o.delay + u * 10
      );
    });
  }
  /**
   * 过滤事件返回值
   * @param item 事件
   * @param params 参数
   * @returns 过滤后的参数
   */
  static filterEvent(e, t) {
    let i = y(t);
    if (e.filterCode)
      try {
        let o = `
                 ${e.filterCode}

                 // 在这里调用 filter 函数，并传递 data 参数
                 return filter(data);
             `;
        o = o.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""), o = d.replaceStringVariables(o), i = new Function("data", o)(t);
      } catch {
      }
    return i;
  }
  /**
   * 条件逻辑判断
   * @param item 事件
   * @param params 参数
   * @returns 是否通过
   */
  static conditionLogic(e, t) {
    var p, s;
    if (!((s = (p = e.condition) == null ? void 0 : p.list) != null && s.length))
      return !0;
    const i = (n) => {
      if (n.type === "field") {
        if (!n.conditionName) return !1;
        const r = t[n.conditionName];
        if (r === void 0) return !1;
        const c = n.conditionValue;
        switch (n.conditionExpression) {
          case "eq":
            return r.toString() === (c || "").toString();
          case "neq":
            return r !== c;
          case "lt":
            return r < Number(c);
          case "gt":
            return r > Number(c);
          case "lte":
            return r <= Number(c);
          case "gte":
            return r >= Number(c);
          case "includes":
            return r.includes(c);
          case "noincludes":
            return !r.includes(c);
          default:
            return !1;
        }
      }
      if (n.type === "custom")
        try {
          let r = `
                        ${n.customValue}
                        return condition(data);
                    `;
          return r = r.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""), r = d.replaceStringVariables(r), new Function("data", r)(t);
        } catch {
          return !1;
        }
      return !1;
    }, o = e.condition.list.map(i);
    return e.condition.type === "and" ? o.every(Boolean) : o.some(Boolean);
  }
}
/** 事件动作 */
l(F, "actions", /* @__PURE__ */ new Set([
  { type: "link", parser: h },
  { type: "sendApi", parser: x },
  { type: "move", parser: A },
  { type: "scale", parser: g },
  { type: "rotate", parser: S },
  { type: "updatePage", parser: E },
  { type: "invoke", parser: b },
  { type: "fullscreen", parser: v },
  { type: "updateVariable", parser: V },
  { type: "updateWidget", parser: w },
  { type: "ueCommonsWebscoket", parser: M },
  { type: "ueShjExecMethod", parser: U },
  { type: "unityWebglExecMethod", parser: k },
  {
    type: "unityIframeExecMethod",
    parser: C
  },
  { type: "vrSceneMethod", parser: N },
  { type: "refreshVariable", parser: W }
]));
export {
  F as SHJParseEvent
};
