var t = require("../common/vendor.js");
exports.mtjInit = function (e) {
  var n,
    r,
    o = "1.10.23",
    a = "https://hmma.baidu.com/mini.gif",
    i = {
      app: ["onShow", "onHide", "onError"],
      page: ["onShow", "onReady", "onHide", "onPageScroll"],
      share: ["onShareAppMessage"],
      behavior: ["tap"],
    },
    c = "mtj_uuid",
    s = "mtj_user",
    u = "mtj_user_property",
    f = "mtj_track_status",
    l = "mtj_remote_config",
    h = "mtj_ab_experiment_list",
    p = "mtj_ab_active_experiment_ids",
    d = {},
    g = { type: 1 },
    m = { aso: {} },
    y = {},
    v = function (t) {
      if (!1 !== d.trackStatus) {
        var e = t.data.et
          ? { mtj_ii: t.data.uuid || "", mtj_et: t.data.et, mtj_en: t.data.en }
          : {};
        return n.request({
          url: t.url,
          data: t.data,
          header: Object.assign(
            { "content-type": "application/json" },
            e,
            t.header
          ),
          method: t.method || "POST",
          dataType: t.dataType || "json",
          success: function (e) {
            t.success && t.success(e);
          },
          fail: function (e) {
            t.fail && t.fail(e);
          },
        });
      }
    },
    b = function () {
      return "undefined" != typeof crypto && crypto.getRandomValues
        ? crypto.getRandomValues(new Uint32Array(1))[0]
        : Math.floor(4294967295 * Math.random());
    },
    j = function (t, e) {
      return "[object " + e + "]" === {}.toString.call(t);
    },
    S = function t(e) {
      return j(e, "Object") || j(e, "Array")
        ? (Object.keys(e).forEach(function (n) {
            var r = e[n];
            j(r, "Object") || j(r, "Array") ? (e[n] = t(r)) : (e[n] = "" + r);
          }),
          e)
        : e;
    },
    O = function (t) {
      return j(t, "String") && /^\d{11}$/.test(t);
    },
    w = function (t) {
      return j(t, "String") && 28 === t.length;
    },
    k = 0,
    x = function (t) {
      return new Promise(function (e, n) {
        return (
          (t.data = t.data || {}),
          d.blacklist &&
          ((d.blacklist.indexOf("all") > -1 && t.data.et) ||
            (d.blacklist.indexOf("behavior") > -1 && "behavior" === t.data.et))
            ? e()
            : ((t.data.v = o),
              (t.data.rqc = ++k),
              (r = t.data),
              JSON.stringify(r).length <= 204800
                ? ((t.success = function (t) {
                    return e(t);
                  }),
                  (t.fail = function (t) {
                    return n(t);
                  }),
                  void v(t))
                : (k--, n(new Error("invalid data"))))
        );
        var r;
      });
    },
    P = function (t, e) {
      var n = j(e, "Object") ? JSON.stringify(e) : "" + e;
      x({
        url: a,
        dataType: "string",
        data: Object.assign({}, g, {
          et: "error",
          en: t,
          ep: { ex: n },
          rid: b(),
        }),
      });
    },
    _ = function (t) {
      (t.rid = b()), (t.aso = t.aso || {});
      var e = { url: a, dataType: "string", data: Object.assign({}, g, t) };
      x(e),
        (y.circleToken || y.circleByThreeFingers) &&
          (("page" === t.et && "show" === t.en) ||
            ("behavior" === t.et && "tap" === t.en)) &&
          ((e.url = "https://hmma.baidu.com/mini.gif?circle=1"),
          (e.data.token = y.circleToken),
          x(e).catch(function (t) {
            return console.error(t);
          }));
    };
  function T(t, e) {
    return (
      (function (t) {
        if (Array.isArray(t)) return t;
      })(t) ||
      (function (t, e) {
        var n =
          t &&
          (("undefined" != typeof Symbol && t[Symbol.iterator]) ||
            t["@@iterator"]);
        if (null != n) {
          var r,
            o,
            a = [],
            i = !0,
            c = !1;
          try {
            for (
              n = n.call(t);
              !(i = (r = n.next()).done) &&
              (a.push(r.value), !e || a.length !== e);
              i = !0
            );
          } catch (t) {
            (c = !0), (o = t);
          } finally {
            try {
              i || null == n.return || n.return();
            } finally {
              if (c) throw o;
            }
          }
          return a;
        }
      })(t, e) ||
      (function (t, e) {
        if (t) {
          if ("string" == typeof t) return I(t, e);
          var n = Object.prototype.toString.call(t).slice(8, -1);
          return (
            "Object" === n && t.constructor && (n = t.constructor.name),
            "Map" === n || "Set" === n
              ? Array.from(t)
              : "Arguments" === n ||
                /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)
              ? I(t, e)
              : void 0
          );
        }
      })(t, e) ||
      (function () {
        throw new TypeError(
          "Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."
        );
      })()
    );
  }
  function I(t, e) {
    (null == e || e > t.length) && (e = t.length);
    for (var n = 0, r = new Array(e); n < e; n++) r[n] = t[n];
    return r;
  }
  var A = function (t) {
      try {
        return n.getStorageSync(t);
      } catch (t) {
        P("getStorageSync", t);
      }
    },
    E = function (t, e) {
      try {
        n.setStorageSync(t, e);
      } catch (t) {
        P("setStorageSync", t);
      }
    },
    N = function (t) {
      try {
        n.removeStorageSync(t);
      } catch (t) {
        P("removeStorageSync", t);
      }
    },
    C = function () {
      return Promise.resolve().then(function () {
        var t = A(c);
        return (
          (j(t, "String") && 32 === t.length) ||
            ((t = ([1e7] + 1e3 + 4e3 + 8e3 + 1e11).replace(
              /[018]/g,
              function (t) {
                return (
                  t ^
                  (("undefined" != typeof crypto && crypto.getRandomValues
                    ? crypto.getRandomValues(new Uint8Array(1))[0]
                    : Math.floor(255 * Math.random())) &
                    (15 >> (t / 4)))
                ).toString(16);
              }
            )),
            E(c, t),
            d.hasABTest && (N(h), N(p))),
          t
        );
      });
    },
    q = function () {
      return (
        r ||
        ((g.sid = b()),
        (g.rqc = 0),
        (r = Promise.all([
          C(),
          new Promise(function (t) {
            n.getSystemInfo({
              success: function (e) {
                delete e.errMsg, t(e);
              },
              fail: function () {
                t({});
              },
            });
          }),
          new Promise(function (t) {
            n.getNetworkType({
              success: function (e) {
                delete e.errMsg, t(e);
              },
              fail: function () {
                t({});
              },
            });
          }),
          Promise.resolve().then(function () {
            var t = A(s),
              e = j(t, "Object") ? t : {};
            return new Promise(function (t) {
              n.getSetting({
                success: function (r) {
                  r.authSetting && r.authSetting["scope.userInfo"]
                    ? n.getUserInfo({
                        success: function (n) {
                          delete n.userInfo.errMsg,
                            t(Object.assign(e, n.userInfo));
                        },
                        fail: function () {
                          t(e);
                        },
                      })
                    : t(e);
                },
                fail: function () {
                  t(e);
                },
              });
            });
          }),
          new Promise(function (t) {
            if (!d.getLocation) return t({});
            n.getLocation({
              type: "wgs84",
              success: function (e) {
                delete e.errMsg, t(e);
              },
              fail: function () {
                t({});
              },
            });
          }),
          Promise.resolve().then(function () {
            var t = A(u);
            return j(t, "Object") ? t : {};
          }),
        ]).then(function (t) {
          var e = T(t, 6),
            n = e[0],
            r = e[1],
            a = e[2],
            i = e[3],
            c = e[4],
            s = e[5];
          (g.uuid = n),
            (m.system = S(r)),
            (m.network = S(a)),
            Object.keys(i).length > 0 && (m.user = S(i)),
            Object.keys(c).length > 0 && (m.location = S(c)),
            Object.keys(s).length > 0 && (m.userProperty = JSON.stringify(s)),
            "devtools" === m.system.platform &&
              d.latestVersion &&
              (function (t, e) {
                for (
                  var n = t.split("."), r = e.split("."), o = 0;
                  o < 3;
                  o++
                ) {
                  var a = +n[o] || 0,
                    i = +r[o] || 0;
                  if (a > i) return 1;
                  if (i > a) return -1;
                }
                return 0;
              })(o, d.latestVersion) < 0 &&
              console.warn(
                "百度移动统计微信小程序SDK已更新，为不影响您的正常使用，请到SDK下载中心 https://mtj.baidu.com/web/sdk/index 下载最新版本"
              );
        })))
      );
    },
    U = {
      onShow: function () {
        var t =
            arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {},
          e = (m.aso.query || []).filter(function (t) {
            return 0 === t.key.indexOf("mtj_");
          });
        return (
          (m.aso.scene = "" + (t.scene || "")),
          t.referrerInfo && t.referrerInfo.appId
            ? (m.aso.referrerInfo = t.referrerInfo)
            : delete m.aso.referrerInfo,
          (m.aso.path = t.path || ""),
          (m.aso.query = Object.keys(t.query || {}).map(function (e) {
            return { key: e, value: t.query[e] };
          })),
          e.length > 0 &&
            !/(^|,)mtj_/.test(Object.keys(t.query || {}).join(",")) &&
            (m.aso.query = m.aso.query.concat(e)),
          (t.query || {}).mtj_ctoken &&
            !d.disableCircling &&
            (y.circleToken = t.query.mtj_ctoken),
          (t.query || {}).mtj_htoken &&
            d.hasHeatmap &&
            (y.heatmapToken = t.query.mtj_htoken),
          q()
            .then(function () {
              return (function (t) {
                return new Promise(function (e) {
                  if (!t) return e();
                  n.getShareInfo({
                    shareTicket: t,
                    success: function (t) {
                      delete t.errMsg, e(t);
                    },
                    fail: function () {
                      e({});
                    },
                  });
                });
              })(t.shareTicket);
            })
            .then(function (t) {
              t ? (m.aso.shareInfo = t) : delete m.aso.shareInfo,
                _(Object.assign({ et: "app", en: "show" }, m));
            })
            .catch(function (t) {
              P("app.onShow", t);
            })
        );
      },
      onHide: function () {
        _({ et: "app", en: "hide" });
      },
      onError: function (t) {
        var e = j(t, "Object") ? JSON.stringify(S(t)) : "" + t;
        _({ et: "app", en: "error", ep: { ex: e } });
      },
    };
  function B() {
    this.options = {
      opacity: 100,
      radius: 30,
      bshadow: 1.5,
      boundVal: 15e3,
      shadowBlur: 15,
      points: { max: 100, data: [] },
      gradient: {
        0.45: "rgb(0,0,255)",
        0.55: "rgb(0,255,255)",
        0.65: "rgb(0,255,0)",
        0.95: "yellow",
        1: "rgb(255,0,0)",
      },
    };
  }
  B.prototype = {
    getCanvas: function (t) {
      return new Promise(function (e, r) {
        n.createSelectorQuery()
          .select(t)
          .fields({ node: !0, size: !0 })
          .exec(function (t) {
            e(t[0].node);
          });
      });
    },
    getPageClientRect: function () {
      return new Promise(function (t, e) {
        n.createSelectorQuery()
          .select(".hm-container")
          .boundingClientRect(function (e) {
            t([e.width, e.height]);
          })
          .exec();
      });
    },
    init: function () {
      var t = this;
      return Promise.all([
        this.getPageClientRect(),
        this.getCanvas("#cvs"),
        this.getCanvas("#cvd"),
      ]).then(function (e) {
        var n = T(e, 3),
          r = n[0],
          o = n[1],
          a = n[2];
        (t.options.width = r[0]),
          (t.options.height = r[1]),
          (o.width = t.options.width),
          (o.height = t.options.height),
          t.options.context.setData({
            p_width: t.options.width,
            p_height: t.options.height,
          });
        var i = o.getContext("2d");
        (t.options.ctx = i), (a.width = 1), (a.height = 256);
        var c = a.getContext("2d");
        t.options.pctx = c;
      });
    },
    renderShadow: function (t, e, n, r) {
      var o = this.options.ctx,
        a = this.options.boundVal,
        i = parseFloat(n / this.options.points.max, 10);
      (o.shadowColor = "rgba(0, 0, 0, " + i + ")"),
        (o.shadowOffsetX = a),
        (o.shadowOffsetY = a),
        (o.shadowBlur = this.options.shadowBlur),
        o.beginPath(),
        o.arc(t - a, e - a, this.options.radius, 0, 2 * Math.PI, !0),
        o.closePath(),
        o.fill(),
        r || this.cachePoint(t, e, n);
    },
    colorize: function () {
      for (
        var t = this.options.width,
          e = this.options.height,
          n = this.options.ctx,
          r = n.getImageData(0, 0, t, e),
          o = r.data,
          a = o.length,
          i = this.getPalette(),
          c = this.options.opacity,
          s = 3;
        s < a;
        s += 4
      ) {
        var u = o[s],
          f = 4 * u;
        if (f) {
          var l = u < c ? u : c;
          (o[s - 3] = i[f]),
            (o[s - 2] = i[f + 1]),
            (o[s - 1] = i[f + 2]),
            (o[s] = l);
        }
      }
      n.putImageData(r, 0, 0);
    },
    getPalette: function () {
      var t = this.options.gradient,
        e = this.options.pctx,
        n = this.options.pctx.createLinearGradient(0, 0, 1, 256);
      for (var r in t) t.hasOwnProperty(r) && n.addColorStop(r, t[r]);
      return (
        (e.fillStyle = n),
        e.fillRect(0, 0, 1, 256),
        e.getImageData(0, 0, 1, 256).data
      );
    },
    cachePoint: function (t, e, n) {
      var r = this.options.points,
        o = r.data;
      n > r.max && (r.max = n), o.push([t, e, n]);
    },
    addPoint: function (t, e, n) {
      this.options.ctx.clearRect(0, 0, this.options.width, this.options.height),
        this.options.pctx.clearRect(0, 0, 1, 256);
      for (var r = this.options.points.data, o = r.length, a = 0; a < o; a++)
        this.renderShadow(r[a][0], r[a][1], r[a][2], !0);
      this.renderShadow(t, e, n), this.colorize();
    },
    start: function (t, e) {
      var n = this;
      (this.options.context = e),
        this.init().then(function () {
          for (var e = 0; e < t.length; e++)
            n.renderShadow(t[e][0], t[e][1], t[e][2]);
          n.colorize();
        });
    },
  };
  var D = -1,
    M = -1,
    R = 0,
    F = {
      onShow: function () {
        var t = getCurrentPages(),
          e = t[t.length - 1];
        return (
          (g.path = e.route),
          (g.query = Object.keys(e.options)
            .map(function (t) {
              return { key: t, value: e.options[t] };
            })
            .filter(function (t) {
              return (
                "mtj_qrid" !== t.key &&
                "mtj_lkid" !== t.key &&
                "mtj_shuuid" !== t.key
              );
            })),
          (g.path === y.lastPagePath &&
            JSON.stringify(g.query) === y.lastPageQuery) ||
            ((y.lastPagePath = g.path),
            (y.lastPageQuery = JSON.stringify(g.query)),
            (y.pageScrollTop = 0)),
          q()
            .then(function () {
              _(Object.assign({ et: "page", en: "show" }, m));
            })
            .catch(function (t) {
              P("page.onShow", t);
            })
        );
      },
      onReady: function () {
        var t = this;
        d.hasHeatmap &&
          y.heatmapToken &&
          q()
            .then(function () {
              var e,
                n = ""
                  .concat(
                    "https://hmma..com/analytics/ajax/cors",
                    "?corsToken="
                  )
                  .concat(y.heatmapToken),
                r = g.query
                  .filter(function (t) {
                    return "mtj_htoken" !== t.key;
                  })
                  .map(function (t) {
                    return "".concat(t.key, "=").concat(t.value);
                  })
                  .join("&"),
                o = g.path;
              v({
                url: n,
                data: {
                  method: "heatmap/heatmapData",
                  displayUrl: "".concat(o).concat(r ? "?" + r : ""),
                  screenWidth:
                    null === (e = m.system) || void 0 === e
                      ? void 0
                      : e.windowWidth,
                },
                method: "POST",
                success: function (e) {
                  var n,
                    r,
                    o =
                      (null == e ||
                      null === (n = e.data) ||
                      void 0 === n ||
                      null === (r = n.data) ||
                      void 0 === r
                        ? void 0
                        : r.points) || [];
                  o.length &&
                    setTimeout(function () {
                      new B().start(o, t);
                    }, 1e3);
                },
                fail: function () {
                  console.error("热力图数据请求失败");
                },
              });
            })
            .catch(function (t) {
              P("get heatmap", t);
            });
      },
      onHide: function () {
        var t,
          e,
          r,
          o,
          a,
          i = S({
            scrollTop: y.pageScrollTop,
            height:
              null === (t = m.system) ||
              void 0 === t ||
              null === (e = t.safeArea) ||
              void 0 === e
                ? void 0
                : e.height,
            width:
              null === (r = m.system) ||
              void 0 === r ||
              null === (o = r.safeArea) ||
              void 0 === o
                ? void 0
                : o.width,
          }),
          c = Object.keys(i).map(function (t) {
            return { key: t, value: i[t] };
          });
        if (
          (_({ et: "page", en: "hide", ep: { data: c } }), d.getComponentScroll)
        )
          try {
            (a = JSON.parse(JSON.stringify(g))),
              n
                .createSelectorQuery()
                .selectAll(".mtj-scroll")
                .fields({ id: !0, size: !0, scrollOffset: !0 })
                .exec(function (t) {
                  Object.keys(t[0]).length > 0 &&
                    _(
                      Object.assign(
                        { et: "page", en: "scroll", ep: S(t[0]) },
                        a
                      )
                    );
                });
          } catch (t) {
            P("page.trackComponentScrollEvent", t);
          }
      },
      onPageScroll: function (t) {
        (!y.pageScrollTop || t.scrollTop > y.pageScrollTop) &&
          (y.pageScrollTop = t.scrollTop);
      },
      onShareAppMessage: function (t) {
        var e =
            arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {},
          n = { from: t.from, path: e.path };
        if (!n.path) {
          var r = g.query
            .map(function (t) {
              return t.key + "=" + t.value;
            })
            .join("&");
          n.path = g.path + (r ? "?" + r : "");
        }
        e.title && (n.title = "" + e.title),
          t.target && (n.target = JSON.stringify(t.target)),
          _(Object.assign({ et: "share", en: "action", ep: n }, m));
        var o = m.aso.query.filter(function (t) {
            return "mtj_shuuid" === t.key;
          }),
          a = o[0] ? o[0].value.split("_") : [];
        g.uuid !== a[a.length - 1] && a.push(g.uuid);
        var i = a.slice(Math.max(0, a.length - 3)).join("_");
        return (
          (e.path = (function (t, e, n) {
            var r =
              (t = t
                .replace(new RegExp(e + "=[^&]*", "g"), "")
                .replace(/(\?|&)&/g, "$1")
                .replace(/(\?|&)$/g, "")).indexOf("?") > 0
                ? "&"
                : "?";
            return t + r + e + "=" + encodeURIComponent(n);
          })(n.path, "mtj_shuuid", i)),
          e
        );
      },
      onAction: function (t, e) {
        if (t && t.type && t.currentTarget) {
          var n = "#" + (t.currentTarget.id || e);
          if ("tap" === t.type) {
            var r = [{ key: "xpath", value: n }],
              o = t.detail,
              a = o.x,
              i = o.y,
              c = {};
            return (
              void 0 !== a &&
                void 0 !== i &&
                (c = { x: Math.floor(a), y: Math.floor(i) }),
              void _(
                Object.assign(
                  { et: "behavior", en: "tap", ep: { data: r }, posi: c },
                  m
                )
              )
            );
          }
          if (
            "touchmove" === t.type &&
            -1 === D &&
            t.touches instanceof Array &&
            3 === t.touches.length
          ) {
            if (((R += 1), clearTimeout(M), 3 === R))
              return (
                (y.circleByThreeFingers = !0),
                (y.circleToken = void 0),
                void _(Object.assign({ et: "page", en: "show" }, m))
              );
            D = setTimeout(function () {
              (D = -1),
                (M = setTimeout(function () {
                  R = 0;
                }, 500));
            }, 1e3);
          }
        }
      },
    },
    J = {
      trackEvent: function (t) {
        var e =
          arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
        if (
          !(function (t) {
            return j(t, "String") && /^[a-z][a-z0-9_]{0,31}$/.test(t);
          })(t)
        )
          return Promise.reject(new Error("事件名称不合法"));
        var n = Object.keys(e)
          .filter(function (t) {
            return (
              (function (t) {
                return j(t, "String") && /^[a-z0-9_]{1,32}$/.test(t);
              })(t) &&
              (function (t) {
                return j(t, "String") || j(t, "Number");
              })(e[t])
            );
          })
          .map(function (t) {
            return {
              key: "" + t,
              value: "" + e[t],
              type: j(e[t], "String") ? "string" : "number",
            };
          });
        return q()
          .then(function () {
            _(Object.assign({ et: "event", en: "" + t, ep: { data: n } }, m));
          })
          .catch(function (t) {
            P("trackEvent", t);
          });
      },
      setTrackStatus: function (t) {
        j(t, "Boolean") && ((d.trackStatus = t), E(f, t));
      },
      setUserInfo: function () {
        var t =
            arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {},
          e = t.tel,
          n = t.openId;
        return q()
          .then(function () {
            var t = A(s),
              r = j(t, "Object") ? t : {};
            O(e) && (r.tel = m.user.tel = e.substr(e.length - 11)),
              w(n) && (r.openId = m.user.openId = n),
              (r.tel || r.openId) && E(s, r),
              j(e, "Undefined") ||
                O(e) ||
                console.error("手机号 ".concat(e, " 不合法")),
              j(n, "Undefined") ||
                w(n) ||
                console.error("openid ".concat(n, " 不合法"));
          })
          .catch(function (t) {
            P("setUserInfo", t);
          });
      },
      setUserId: function (t) {
        return Promise.resolve().then(function () {
          if (!(j(t, "String") || (j(t, "Number") && Number.isFinite(t))))
            return Promise.reject(new Error("userId只能是字符串或数字"));
          var e = "" + t,
            n = A(u),
            r = j(n, "Object") ? n : {};
          if (!r.uid_ || r.uid_[0] !== e) {
            (r.uid_ = [e, "1"]), E(u, r), (m.userProperty = JSON.stringify(r));
            var o = [{ key: "uid", value: e }];
            return q()
              .then(function () {
                _(
                  Object.assign(
                    { et: "api", en: "setUserId", ep: { data: o } },
                    m
                  )
                );
              })
              .catch(function (t) {
                P("setUserId", t);
              });
          }
        });
      },
      setUserProperty: function (t) {
        return Promise.resolve().then(function () {
          var e = A(u),
            n = j(e, "Object") ? e : {};
          if (j(t, "Null"))
            Object.keys(n).forEach(function (t) {
              "_" !== t.charAt(0) &&
                "_" !== t.charAt(t.length - 1) &&
                delete n[t];
            });
          else if (!j(t, "Object"))
            return Promise.reject(new Error("userProperty必须是对象"));
          var r = Object.keys(n).filter(function (t) {
            return "_" !== t.charAt(0) && "_" !== t.charAt(t.length - 1);
          }).length;
          Object.keys(t || {}).forEach(function (e) {
            var o = t[e];
            "" === e ||
              "_" === e.charAt(0) ||
              ("_" === e.charAt(e.length - 1) &&
                "ab_" !== e &&
                "ab_filter_" !== e) ||
              (j(o, "Null")
                ? n[e] && (delete n[e], r--)
                : !(j(o, "String") || (j(o, "Number") && Number.isFinite(o))) ||
                  e.length > 256 ||
                  ("" + o).length > 256 ||
                  (!n[e] && r >= 100) ||
                  (n[e] || r++, (n[e] = [o, "1"])));
          }),
            E(u, n),
            (m.userProperty = JSON.stringify(n));
        });
      },
    },
    V = {
      data: [],
      init: function () {
        var t = this,
          e = g.uuid;
        e
          ? this.startFetch(e)
          : C().then(function (e) {
              (g.uuid = e), t.startFetch(e);
            });
      },
      startFetch: function (t) {
        console.log("---您的设备ID---", t), this.fetchTest();
      },
      getExpDataByParamName: function (t) {
        var e = A(h);
        if ((this.setLocalData(e), !e || !j(e, "Array"))) return {};
        var n = e.find(function (e) {
          return e && j(e.params, "Object") && !j(e.params[t], "Undefined");
        });
        return n && n.params ? { expValue: n.params[t], expId: n.e } : {};
      },
      fetchTest: function (t, e, n) {
        var r = this,
          o = g.uuid,
          a = g.key;
        v({
          url: "https://hm.baidu.com/v1/diversion/",
          data: { cid: o, tid: a },
          method: "GET",
          success: function (o) {
            j(o, "Object") &&
            200 === o.statusCode &&
            j(o.data, "Object") &&
            0 === o.data.status
              ? r.handleFetchResponse(o.data, t, e, n)
              : n && n(e);
          },
          fail: function () {
            console.error("请求分流实验失败"), n && n(e);
          },
        });
      },
      handleFetchResponse: function (t, e, n, r) {
        var o = [];
        if (
          (j(t, "Object") &&
            j(t.data, "Object") &&
            j(t.data.expr, "Array") &&
            (o = t.data.expr),
          this.updateStorage(o),
          this.setLocalData(o),
          r)
        ) {
          var a = this.getExpDataByParamName(e),
            i = a.expValue,
            c = a.expId;
          j(i, "Null") || j(i, "Undefined") ? r(n) : this.handleInTest(i, c, r);
        }
      },
      handleInTest: function (t, e, n) {
        n(t), this.updateUserProperty(e);
      },
      updateStorage: function (t) {
        E(h, t);
      },
      setLocalData: function (t) {
        this.data = t || [];
      },
      updateUserProperty: function (t) {
        var e = this,
          n = this.getActiveExpIds(t) || [],
          r = { ab_: null, ab_filter_: null },
          o = {},
          a = [];
        if (
          (n.forEach(function (t) {
            var n = e.getExpDataFromId(t);
            j(n, "Object") && ((o[n.e] = n.g), a.push(n.h || 0));
          }),
          a.length)
        ) {
          var i = 0;
          a.forEach(function (t) {
            i = (function (t, e) {
              var n = "00000000000000000000000000000000";
              function r(t) {
                var e = (n + n + Number(t).toString(2)).slice(-64);
                return [parseInt(e.slice(0, 32), 2), parseInt(e.slice(-32), 2)];
              }
              function o(t) {
                return (n + t.toString(2)).slice(-32);
              }
              var a = r(t),
                i = r(e);
              return parseInt(
                o((a[0] | i[0]) >>> 0) + o((a[1] | i[1]) >>> 0),
                2
              );
            })(i, t);
          }),
            (r = { ab_: JSON.stringify(o), ab_filter_: i });
        }
        J.setUserProperty(r);
      },
      getExpDataFromId: function (t) {
        return A(h).find(function (e) {
          return String(e.e) === String(t);
        });
      },
      setActiveExperimentIds: function (t) {
        E(p, t);
      },
      getActiveExpIds: function (t) {
        var e = this,
          n = A(p) || [];
        return (
          j(t, "Undefined") || -1 !== n.indexOf(t) || n.push(t),
          (n = (n = n.filter(function (t) {
            return !!e.getExpDataFromId(t);
          })).filter(function (t, e) {
            return n.indexOf(t) === e;
          })),
          this.setActiveExperimentIds(n),
          n
        );
      },
    },
    H = function (t) {
      if (d.hasABTest)
        if (j(t, "Object")) {
          var e = t.paramName;
          if (null != e) {
            var n = t.defaultValue;
            if (j(n, "Undefined")) console.error("请设置参数默认值");
            else {
              var r = t.callback;
              if (j(r, "Function")) {
                var o = V.getExpDataByParamName(e),
                  a = o.expValue,
                  i = o.expId;
                j(a, "Null") || j(a, "Undefined")
                  ? V.fetchTest(e, n, r)
                  : V.handleInTest(a, i, r);
              } else console.error("callback必须为函数");
            }
          } else console.error("请设置实验参数名称");
        } else console.error("传递参数请设置为对象");
    },
    L = function (t, e, n) {
      var r = e[t];
      e[t] = function (e) {
        if ((n.call(this, e, t), r)) return r.apply(this, arguments);
      };
    },
    z = App,
    $ = function (t) {
      i.app.forEach(function (e) {
        L(e, t, U[e]);
      }),
        (t.mtj = J),
        (t.mtj.pageEvent = F),
        (t.mtj.fetchABTest = H),
        z(t);
    },
    K = Page,
    Q = function (t) {
      i.page.forEach(function (e) {
        L(e, t, F[e]);
      }),
        i.share.forEach(function (e) {
          var n, r, o, a;
          (o = F[e]),
            (a = (r = t)[(n = e)]),
            (r[n] = function (t) {
              var e = a && a.apply(this, arguments);
              return o.call(this, t, e);
            });
        }),
        Object.keys(t).forEach(function (e) {
          "function" == typeof t[e] &&
            -1 === i.page.indexOf(e) &&
            -1 === i.share.indexOf(e) &&
            L(e, t, F.onAction);
        }),
        K(t);
    },
    G = Behavior,
    W = function (t) {
      return (
        i.page.forEach(function (e) {
          L(e, t.methods, F[e]);
        }),
        G(t)
      );
    };
  W.prototype.constructor = Behavior;
  var X = Component,
    Y = function (t) {
      return (
        i.page.forEach(function (e) {
          L(e, t.methods, F[e]);
        }),
        X(t)
      );
    },
    Z = function () {
      var e, r;
      (e = t.wx$1), (n = e);
      try {
        r = {
          appKey: "1b968b76bf",
          hasPlugin: !1,
          getLocation: !1,
          getComponentScroll: !1,
          hasABTest: !1,
          hasHeatmap: !1,
        };
      } catch (t) {
        return void console.error(
          "请把mtj-wx-sdk.config.js文件拷贝到utils目录中"
        );
      }
      r && r.appKey
        ? ((g.key = r.appKey),
          (d.getLocation = r.getLocation || !1),
          (d.getComponentScroll = r.getComponentScroll || !1),
          (d.disableCircling = r.disableCircling || !1),
          (d.trackStatus = !(!1 === A(f))),
          (d.hasABTest = r.hasABTest || !1),
          (d.hasHeatmap = r.hasHeatmap || !1),
          d.hasABTest && V.init(),
          (function () {
            var t = A(l);
            if (t) {
              Object.keys(t).forEach(function (e) {
                d[e] = t[e];
              });
              var e = t.updateTimestamp || 0;
              if (+new Date() - e < 864e5) return Promise.resolve();
            }
            x({
              url: "https://hmma.baidu.com/mini.conf",
              method: "POST",
              header: { "content-type": "application/x-www-form-urlencoded" },
              data: { type: "wx", key: g.key },
            })
              .then(function (t) {
                if (t && t.data) {
                  var e = t.data;
                  Object.keys(e).forEach(function (t) {
                    d[t] = e[t];
                  }),
                    (e.updateTimestamp = +new Date()),
                    E(l, e);
                } else P("remoteConfig", t);
              })
              .catch(function (t) {
                P("sendRequest", t);
              });
          })(),
          r.hasPlugin || ((App = $), (Page = Q)),
          (module.exports = { App: $, Page: Q, Behavior: W, Component: Y }))
        : console.error("请设置mtj-wx-sdk.config.js文件中的appKey字段");
    };
  return (
    Z(), (e.init = Z), Object.defineProperty(e, "__esModule", { value: !0 }), e
  );
};
