import monitor from "../../../core/utils/alipayLogger";
import { globalExt } from "../../../core/utils/constants";
import registerHooks from "../../../core/utils/registerHooks";

function wait(callback, time) {
  const {
    globalData
  } = getApp();

  if (time >= 5 || globalData.monitorPid) {
    callback(globalData);
  } else {
    setTimeout(() => {
      wait(callback, time + 1);
    }, 1000);
  }
}

registerHooks({
  async onAppLaunch(options) {
    wait(({
      monitorPid
    }) => {
      try {
        monitor.init({
          pid: monitorPid || globalExt.pid,
          // eslint-disable-next-line no-undef
          options,
          sample: 1,
          autoReportApi: false,
          autoReportPage: true,
          // Http请求返回数据中状态码字段名称
          code: ['code'],
          // Http返回数据中的error message字段名称
          msg: ['msg']
        });
        monitor.setCommonInfo({
          tag: this.globalData.appId
        });
      } catch (error) {
        console.error(error);
      }
    }, 0);
  }

});