var f = Object.defineProperty;
var b = (l, a, e) => a in l ? f(l, a, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[a] = e;
var d = (l, a, e) => b(l, typeof a != "symbol" ? a + "" : a, e);
import { cloneDeep as g } from "lodash";
import { SHJDatasourceV2 as u } from "../index.mjs";
import { storageData as V } from "./storageData.mjs";
import "../../event/actions/fullScreenAction.mjs";
import { EventUtils as h } from "../../event/utils/utils.mjs";
const i = class i {
  static executeAllVariable(a) {
    a.forEach((e) => {
      e.sources.isVariable = !0;
    }), i.setVariables(a), a.forEach((e) => {
      u.parse({
        sources: [e.sources],
        noUseMapping: !0,
        callback: (t) => {
          i.setVariableById(e, t, !1);
        }
      });
    });
  }
  static replaceObjectVariables(a) {
    const e = this.replaceStringVariables(JSON.stringify(a));
    return JSON.parse(e);
  }
  static replaceStringVariables(a) {
    return a.replace(/\$g\{([^}]+)\}/g, (e, t) => {
      const { varName: r, propPath: n } = this.parseVariablePath(t), c = this.getVariables().find((o) => o.name === r);
      if (!c)
        return e;
      let s = c._value;
      try {
        for (const o of n) {
          if (s == null) {
            s = void 0;
            break;
          }
          s = s[o];
        }
      } catch {
        return "undefined";
      }
      return typeof s == "object" ? JSON.stringify(s) : s === void 0 ? "undefined" : String(s);
    });
  }
  static getObjectVariableNames(a) {
    return this.getStringVariableNames(JSON.stringify(a));
  }
  static getStringVariableNames(a) {
    const e = /* @__PURE__ */ new Set(), t = /\$g\{([^}]+)\}/g;
    for (const r of a.matchAll(t)) {
      const n = r[1], { varName: c } = this.parseVariablePath(n);
      c && e.add(c);
    }
    return Array.from(e);
  }
  static parseVariablePath(a) {
    const t = a.replace(/\[/g, ".").replace(/\]/g, "").split("."), r = t[0], n = t.slice(1);
    return { varName: r, propPath: n };
  }
  static getEnvironments() {
    return window._apiEnvironment;
  }
  static setEnvironments(a) {
    const e = a.find((t) => t.selected);
    e && (window._apiEnvironment = {
      envName: e.envName,
      envBaseUrl: e.envBaseUrl
    });
  }
  static getVariables() {
    return window._variables || [];
  }
  static setVariables(a) {
    window._variables = g(a), window._variables.forEach((e) => {
      e._callbacks ?? (e._callbacks = /* @__PURE__ */ new Map());
    });
  }
  static setVariableById(a, e, t = !0) {
    const r = this.getVariableById(a.id);
    if (r) {
      r._value = e, r._initialState ?? (r._initialState = !0);
      try {
        V(a.sources.source.storage, e);
      } catch {
      }
      r._callbacks ?? (r._callbacks = /* @__PURE__ */ new Map()), r._callbacks.forEach(
        (n) => n(a._value)
      ), t && h.updateWidgetVariableData(r);
    }
  }
  static getVariableById(a) {
    return window._variables.find((e) => e.id === a);
  }
  static getVariableByName(a) {
    return window._variables.find((e) => e.name === a);
  }
  static getVariableValueById(a) {
    const e = window._variables.find((t) => t.id === a);
    return e ? e._value : null;
  }
  static noneData(a) {
    return {
      id: a,
      finalKeyData: { id: a, data: null },
      finalUserData: { id: a, data: null },
      filteredData: null,
      rawData: null,
      noMappingData: null
    };
  }
};
d(i, "addVariable", (a) => {
  u.parse({
    sources: [a.sources],
    noUseMapping: !0,
    callback: (e) => {
      i.getVariables().push(a), i.setVariableById(a, e, !1);
    }
  });
}), d(i, "updateVariable", (a) => {
  u.parse({
    sources: [a.sources],
    noUseMapping: !0,
    callback: (e) => {
      i.setVariableById(a, e, !0);
    }
  });
}), d(i, "deleteVariableById", (a) => {
  const e = window._variables.findIndex((t) => t.id === a);
  e > -1 && window._variables.splice(e, 1);
});
let p = i;
export {
  p as DataSourceUtils
};
