var d = Object.defineProperty;
var l = (a, e, t) => e in a ? d(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
var m = (a, e, t) => l(a, typeof e != "symbol" ? e + "" : e, t);
import { cloneDeep as f } from "lodash";
import { executePageAction as y } from "./actions/pageAction.mjs";
import { executeLinkAction as x } from "./actions/linkAction.mjs";
import { executeMoveAction as A } from "./actions/moveAction.mjs";
import { executeSendApiAction as h } from "./actions/sendApiAction.mjs";
import { executeScaleAction as g } from "./actions/scaleAction.mjs";
import { executeRotateAction as E } from "./actions/rotateAction.mjs";
import { executeUpdatePageAction as S } from "./actions/updatePageAction.mjs";
import { executeInvokeAction as b } from "./actions/invokeAction.mjs";
import { executeFullScreenAction as w } from "./actions/fullScreenAction.mjs";
import { executeUpdateVariableAction as M } from "./actions/updateVariableAction.mjs";
import { executeUpdateWidgetAction as U } from "./actions/updateWidgetAction.mjs";
import { executeUeCommonsWebscoketAction as k } from "./actions/ueCommonsWebscoketAction.mjs";
import { executeUeShjExecMethodAction as C } from "./actions/ueShjExecMethodAction.mjs";
import { executeUnityWebglExecMethodAction as N } from "./actions/unityWebglExecMethodAction.mjs";
import { executeUnityIframeExecMethodAction as P } from "./actions/unityIframeExecMethodAction.mjs";
import { executeVrSceneMethodAction as V } from "./actions/vrSceneMethodAction.mjs";
class W {
  /**
   * 添加事件解析器
   * @param type 事件类型
   * @param parser 事件解析器
   */
  static addAction(e, t) {
    this.actions.add({ type: e, parser: t });
  }
  /**
   * 获取事件解析器
   * @param type 事件类型
   * @returns 事件解析器
   */
  static getAction(e) {
    const t = Array.from(this.actions).find((n) => n.type === e);
    return t == null ? void 0 : t.parser;
  }
  /**
   * 解析事件
   * @param events 事件
   * @param type 事件类型
   * @param params 参数
   */
  static parseEvents(e, t, n) {
    e != null && e.length && e.forEach((r, p) => {
      r.eventType === t && setTimeout(() => {
        const u = new CustomEvent("event-params-log", {
          detail: {
            eventId: r.id,
            params: n
          }
        });
        window.dispatchEvent(u);
        const s = this.filterEvent(r, n);
        if (!this.conditionLogic(r, s) || !r.actions)
          return;
        const i = this.getAction(r.actions);
        i && i(r, s);
      }, r.delay + p * 10);
    });
  }
  /**
   * 过滤事件返回值
   * @param event 事件
   * @param params 参数
   * @returns 过滤后的参数
   */
  static filterEvent(e, t) {
    let n = f(t);
    if (e.filterCode)
      try {
        const r = `
                 ${e.filterCode}
     
                 // 在这里调用 filter 函数，并传递 data 参数
                 return filter(data);
             `;
        n = new Function("data", r)(t);
      } catch {
      }
    return n;
  }
  /**
   * 条件逻辑判断
   * @param event 事件
   * @param params 参数
   * @returns 是否通过
   */
  static conditionLogic(e, t) {
    var u, s;
    if (!((s = (u = e.condition) == null ? void 0 : u.list) != null && s.length))
      return !0;
    const n = (i) => {
      if (i.type === "field") {
        if (!i.conditionName) return !1;
        const o = t[i.conditionName];
        if (o === void 0) return !1;
        const c = i.conditionValue;
        switch (i.conditionExpression) {
          case "eq":
            return o.toString() === (c || "").toString();
          case "neq":
            return o !== c;
          case "lt":
            return o < Number(c);
          case "gt":
            return o > Number(c);
          case "lte":
            return o <= Number(c);
          case "gte":
            return o >= Number(c);
          case "includes":
            return o.includes(c);
          case "noincludes":
            return !o.includes(c);
          default:
            return !1;
        }
      }
      if (i.type === "custom")
        try {
          const o = `
                        ${i.customValue}
                        return condition(data);
                    `;
          return new Function("data", o)(t);
        } catch {
          return !1;
        }
      return !1;
    }, r = e.condition.list.map(n);
    return e.condition.type === "and" ? r.every(Boolean) : r.some(Boolean);
  }
}
/** 事件动作 */
m(W, "actions", /* @__PURE__ */ new Set([
  { type: "page", parser: y },
  { type: "link", parser: x },
  { type: "sendApi", parser: h },
  { type: "move", parser: A },
  { type: "scale", parser: g },
  { type: "rotate", parser: E },
  { type: "updatePage", parser: S },
  { type: "invoke", parser: b },
  { type: "fullscreen", parser: w },
  { type: "updateVariable", parser: M },
  { type: "updateWidget", parser: U },
  { type: "ueCommonsWebscoket", parser: k },
  { type: "ueShjExecMethod", parser: C },
  { type: "unityWebglExecMethod", parser: N },
  { type: "unityIframeExecMethod", parser: P },
  { type: "vrSceneMethod", parser: V }
]));
export {
  W as SHJParseEvent
};
