/* eslint-disable no-undef */
import validate from './utils/validate'
import Log from './constants/log'
import globalConfig from './globalConfig'
function initEid(
  e = 'https://eid.faceid.qq.com',
  o = 'release',
  n = 'https://eid-enhance.faceid.qq.com'
) {
  ;(uni.eidBaseUrl = e),
    (uni.eidEnvVersion = o),
    (uni.eidBackUpUrl = n),
    uni.onAppShow((e) => {
      const { scene: o } = e
      if (1038 !== o) return
      const { referrerInfo: n } = e,
        { appId: i, extraData: t } = n
      if ('wx0e2cb0b052a91c92' !== i || !t) return
      const { verifyDone: r, token: a } = t
      r && uni.handleEidVerifyDone
        ? uni.eidTokenToCallback &&
          uni.eidTokenToCallback === a &&
          ((uni.eidTokenToCallback = ''),
          uni.reportLogToEid({
            token: a,
            event: Log.navigateBackFromEid,
            errMsg: `从EID核身完成返回，token:${a},verifyDone:${r}`
          }),
          uni.handleEidVerifyDone(t))
        : uni.reportLogToEid({
            token: a,
            event: Log.navigateBackFromEidFail,
            errMsg: `核验未完成或者没有处理核验完成的函数，token:${a},verifyDone:${r}`
          })
    })
  const i = uni.getSystemInfoSync(),
    { version: t } = i
  uni.reportLogToEid = function (e) {
    const { token: o = '', event: n = '', errCode: i = '', errMsg: r = '', data: a = {} } = e,
      s = new Date(),
      d = {
        Token: o,
        SourceType: Log.SourceType,
        SourceVersion: Log.version,
        EnvVersion: t,
        Timestamp: s.getTime(),
        Event: n,
        ErrorCode: 'number' == typeof i ? i.toString() : i,
        ErrorMsg: r,
        Data: JSON.stringify(a)
      }
    console.log('开始上报日志：', d),
      uni.request({
        url: `${uni.eidBaseUrl}/api/common/ReportEvent`,
        method: 'POST',
        data: d,
        success(e) {
          console.log('上报日志完成：', 'payload:', d, 'res:', e)
        }
      })
  }
}
function startEid(e) {
  const { data: o, verifyDoneCallback: n } = e
  if (!o || !n)
    return (
      uni.reportLogToEid({ token: i, event: Log.startEidFail, errMsg: '传入的参数有误' }),
      void uni.showModal({ title: '提示', content: '传入的参数有误', showCancel: !1 })
    )
  const { token: i } = o
  if (!validate.isValidateToken(i))
    return (
      uni.reportLogToEid({
        token: i,
        event: Log.startEidFail,
        errMsg: `传入的token有误，token:${i}`
      }),
      void uni.showModal({ title: '提示', content: '传入的token有误', showCancel: !1 })
    )
  ;(uni.handleEidVerifyDone = (e) => {
    const { token: o } = e
    uni.navigateBack({
      success() {
        uni.reportLogToEid({ token: o, event: Log.EidVerifyDone, errMsg: `验证完成，token：${o}` }),
          n({ token: o, verifyDone: !0 })
      }
    })
  }),
    uni.navigateTo({ url: `${globalConfig.normalPath}/mp_ecard_sdk/index/index?token=${i}` })
}
export { initEid, startEid }
