var f = Object.defineProperty;
var m = (s, e, t) => e in s ? f(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var d = (s, e, t) => m(s, 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 b } from "./actions/rotateAction.mjs";
import { executeUpdatePageAction as S } from "./actions/updatePageAction.mjs";
import { executeInvokeAction as v } from "./actions/invokeAction.mjs";
import { executeFullScreenAction as E } 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 D } from "./actions/vrSceneMethodAction.mjs";
import { executeRefreshVariableAction as N } from "./actions/refreshVariableAction.mjs";
import { DataSourceUtils as l } from "../datasource/utils/utils.mjs";
class W {
  /**
   * 添加事件解析器
   * @param type 事件类型
   * @param parser 事件解析器
   */
  static addAction(e, t) {
    const n = Array.from(this.actions).find((r) => r.type === e);
    n && this.actions.delete(n), 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, c) => {
      r.eventType === t && setTimeout(() => {
        const u = new CustomEvent("event-params-log", {
          detail: {
            eventId: r.id,
            params: n
          }
        });
        window.dispatchEvent(u);
        const p = this.filterEvent(r, n);
        if (!this.conditionLogic(r, p) || !r.actions)
          return;
        const a = this.getAction(r.actions);
        a && a(r, p);
      }, r.delay + c * 10);
    });
  }
  /**
   * 过滤事件返回值
   * @param item 事件
   * @param params 参数
   * @returns 过滤后的参数
   */
  static filterEvent(e, t) {
    let n = y(t);
    if (e.filterCode)
      try {
        const r = l.getVariableData();
        let c = `
                 ${e.filterCode}
     
                 // 在这里调用 filter 函数，并传递 data 参数
                 return filter(data);
             `;
        c = c.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""), c = l.replaceStringVariables(c, r), n = new Function("data", c)(t);
      } catch {
      }
    return n;
  }
  /**
   * 条件逻辑判断
   * @param item 事件
   * @param params 参数
   * @returns 是否通过
   */
  static conditionLogic(e, t) {
    var u, p;
    if (!((p = (u = e.condition) == null ? void 0 : u.list) != null && p.length))
      return !0;
    const n = (a) => {
      if (a.type === "field") {
        if (!a.conditionName) return !1;
        const i = t[a.conditionName];
        if (i === void 0) return !1;
        const o = a.conditionValue;
        switch (a.conditionExpression) {
          case "eq":
            return i.toString() === (o || "").toString();
          case "neq":
            return i !== o;
          case "lt":
            return i < Number(o);
          case "gt":
            return i > Number(o);
          case "lte":
            return i <= Number(o);
          case "gte":
            return i >= Number(o);
          case "includes":
            return i.includes(o);
          case "noincludes":
            return !i.includes(o);
          default:
            return !1;
        }
      }
      if (a.type === "custom")
        try {
          const i = l.getVariableData();
          let o = `
                        ${a.customValue}
                        return condition(data);
                    `;
          return o = o.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""), o = l.replaceStringVariables(o, i), 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);
  }
}
/** 事件动作 */
d(W, "actions", /* @__PURE__ */ new Set([
  { type: "link", parser: h },
  { type: "sendApi", parser: x },
  { type: "move", parser: A },
  { type: "scale", parser: g },
  { type: "rotate", parser: b },
  { type: "updatePage", parser: S },
  { type: "invoke", parser: v },
  { type: "fullscreen", parser: E },
  { 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: D },
  { type: "refreshVariable", parser: N }
]));
export {
  W as SHJParseEvent
};
