'use strict'
var _extends =
  Object.assign ||
  function(a) {
    for (var b, c = 1; c < arguments.length; c++)
      for (var d in ((b = arguments[c]), b))
        Object.prototype.hasOwnProperty.call(b, d) && (a[d] = b[d])
    return a
  }
!(function(require, directRequire) {
  async function a(a, b, c) {
    return new Promise(d => {
      a({
        type: j.SIMULATOR_SET_AUTHORIZE_CONFIRM,
        data: {
          show: !0,
          scopeList: c.scopeList,
          imageUrl: c.imageUrl,
          appName: c.appName,
          callback: async (a, c) => {
            a = a && c[0].checked
            let { body: f } = await e({
              url: `${g.jsOperateWXDATAURL}`,
              method: 'post',
              body: JSON.stringify({
                data: JSON.stringify(b.args.data || {}),
                grant_scope: c[0].scope,
                opt: a ? 1 : 2
              }),
              needToken: 1,
              needAppID: 1
            })
            return a
              ? d({ errMsg: `${b.api}:ok`, data: JSON.parse(f.data) })
              : d({ errMsg: `${b.api}:fail auth deny` })
          }
        }
      })
    })
  }
  async function b(a) {
    if (!(Date.now() - n < m))
      return (
        (n = Date.now()),
        new Promise(b => {
          a({
            type: j.SIMULATOR_SET_CONFIRM,
            data: {
              show: !0,
              content:
                '\u83B7\u53D6\u7528\u6237\u4FE1\u606F\u63A5\u53E3\u540E\u7EED\u5C06\u4E0D\u518D\u51FA\u73B0\u6388\u6743\u5F39\u7A97\uFF0C\u8BF7\u6CE8\u610F\u5347\u7EA7',
              showCancel: !0,
              cancelText: '\u7EE7\u7EED\u4F7F\u7528',
              confirmText: '\u67E5\u770B\u8BE6\u60C5',
              callback: async a => {
                a &&
                  nw.Shell.openExternal(
                    'https://developers.weixin.qq.com/blogdetail?action=get_post_info&lang=zh_CN&token=1650183953&docid=0000a26e1aca6012e896a517556c01'
                  ),
                  b()
              }
            }
          })
        })
      )
  }
  async function c(a, b, c) {
    return new Promise(d => {
      a({
        type: j.SIMULATOR_SET_AUTHORIZE_CONFIRM,
        data: _extends({}, c, {
          show: !0,
          callback: async (a, c) => {
            let f = []
            c.forEach(a => {
              !a.checked || f.push(a.scope)
            })
            let { body: h } = await e({
              url: `${g.jsAuthorizeConfirmURL}`,
              method: 'post',
              body: JSON.stringify({ scope: f, opt: a ? 1 : 2 }),
              needToken: 1,
              needAppID: 1
            })
            return h.baseresponse && 0 === h.baseresponse.errcode
              ? void (a
                  ? d({ errMsg: `${b.api}:ok` })
                  : d({ errMsg: `${b.api}:fail auth deny` }))
              : void d({ errMsg: `${b.api}:fail` })
          }
        })
      })
    })
  }
  async function d() {
    for (; 0 < o.length; ) {
      const a = o[0]
      if (!a) break
      try {
        const b = await a.fn()
        a.resolve(b)
      } catch (b) {
        a.reject(b)
      }
      o.shift()
    }
  }
  const e = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    f = require('./3bfffbe88b3d923921f851c0697974fe.js'),
    g = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    h = require('./d3976cc01aeebc5b09e11c4135b6bd8d.js'),
    i = require('./df6d0ff021a69fb541c733de4dbba0fe.js'),
    j = require('./0634ee2ebd3e560d9d4804ecc960160f.js'),
    {
      enterBackground: k,
      enterForeground: l
    } = require('./a3959bb900db9f65ed2e9c5dfa6977b7.js'),
    m = 1800000
  let n = 0,
    o = []
  module.exports = {
    login: async function(a, b) {
      let { body: c } = await e({
        url: `${g.jsLoginURL}`,
        method: 'post',
        body: JSON.stringify({ scope: ['snsapi_base'] }),
        needToken: 1,
        needAppID: 1
      })
      return { errMsg: `${b.api}:ok`, code: c.code }
    },
    refreshSession: async function(a, b) {
      let { body: c } = await e({
        url: `${g.jsRefreshSessionURL}`,
        method: 'post',
        needToken: 1,
        needAppID: 1
      })
      return {
        errMsg: `${b.api}:ok`,
        expireIn: c.session_expire_in,
        err_code: c.baseresponse.errcode
      }
    },
    operateWXData: async function(c, d, k) {
      const l = f.getCurrent(),
        m = k()
      let { args: n, api: o } = d
      if (
        'webapi_getuserinfo' == n.data.api_name &&
        (1003000 > f.getLibVersionNumber() &&
          (delete n.data.lang, delete n.data.with_credentials),
        l.isTourist)
      ) {
        const a = m.user || {}
        return {
          errMsg: 'operateWXData:ok',
          data: {
            data: JSON.stringify({
              nickName: a.nickName,
              avatarUrl: a.headUrl,
              gender: 'male' === a.sex ? 1 : 2,
              province: a.province,
              city: a.city,
              country: a.country
            })
          }
        }
      }
      let { body: p } = await e({
          url: `${g.jsOperateWXDATAURL}`,
          method: 'post',
          body: JSON.stringify({ data: JSON.stringify(n.data || {}) }),
          needToken: 1,
          needAppID: 1,
          needCheckErrCode: -1
        }),
        q = p.baseresponse
      if (q) {
        let e = p.baseresponse.errcode
        if (0 == e) {
          let a = JSON.parse(p.data)
          try {
            let b =
              (n.data &&
                n.data.api_name &&
                n.data.api_name.replace('webapi_', '')) ||
              b
            c({
              type: j.SIMULATOR_UPDATE_DECRYPTED_INFO,
              api: b,
              data: {
                encryptedData: a.encryptedData,
                iv: a.iv,
                sessionKey: p.debug_info.session_key,
                decryptedData: p.debug_info.data
              }
            })
          } catch (a) {}
          return { errMsg: `${d.api}:ok`, data: a }
        }
        if (e == i.NEED_CONFORM)
          return await a(c, d, {
            imageUrl: p.appicon_url,
            appName: p.appname,
            scopeList: [p.scope]
          })
        let f = h.parseCgiErrorCode(e, q.errmsg)
        return (
          'scope unauthorized' != f ||
            'webapi_getuserinfo' != n.data.api_name ||
            n.data.from_component ||
            (await b(c)),
          { errMsg: `${d.api}:fail ${f}` }
        )
      }
    },
    authorize: async function(a, b) {
      let { body: f } = await e({
          url: `${g.jsAuthorizeURL}`,
          method: 'post',
          body: JSON.stringify({ scope: b.args.scope || [] }),
          needCheckErrCode: -1,
          needToken: 1,
          needAppID: 1
        }),
        j = f.baseresponse
      if (j) {
        let e = j.errcode
        if (0 == e) return { errMsg: `${b.api}:ok`, body: f }
        if (e == i.NEED_CONFORM)
          return await (async () =>
            new Promise((e, g) => {
              o.push({
                fn: async () => {
                  return await c(a, b, {
                    imageUrl: f.appicon_url,
                    appName: f.appname,
                    scopeList: f.scope_list
                  })
                },
                resolve: e,
                reject: g
              }),
                2 > o.length && d()
            }))()
        let g = h.parseCgiErrorCode(e, j.errmsg)
        return { errMsg: `${b.api}:fail ${g}` }
      }
      return { errMsg: `${b.api}:fail system error` }
    },
    openWeRunSetting: async function(a, b) {
      let { body: c } = await e({
          url: `${g.checkWeRunState}`,
          method: 'post',
          body: JSON.stringify({ appid: f.getProjectAppID() }),
          needToken: 1,
          needAppID: 1
        }),
        d = c.state
      if (1 == d) return { errMsg: `${b.api}:ok` }
      let h =
        c.wording ||
        '\u7528\u6237\u672A\u5F00\u542F\u5FAE\u4FE1\u8FD0\u52A8\uFF0C\u8BF7\u5728\u5FAE\u4FE1\u5BA2\u6237\u7AEF\u5F00\u542F'
      return (
        a({
          type: j.SIMULATOR_SET_CONFIRM,
          data: {
            show: !0,
            content: h,
            showCancel: !1,
            confirmText: '\u5173\u95ED'
          }
        }),
        { errMsg: `${b.api}:fail ${h}` }
      )
    },
    chooseInvoiceTitle: async function(a, b) {
      a({
        type: j.SIMULATOR_SET_CHOOSEINVOICETITLE,
        data: { show: !0, callbackID: b.callbackID, api: b.api }
      })
    },
    addPhoneContact: async function(a, b) {
      a({
        type: j.SIMULATOR_SET_ADDPHONECONTACT,
        data: {
          show: !0,
          callbackID: b.callbackID,
          api: b.api,
          contactData: _extends({}, b.args)
        }
      })
    },
    openAddress: async function(a, b) {
      a({
        type: j.SIMULATOR_SET_CHOOSEADDRESS,
        data: { show: !0, callbackID: b.callbackID, api: b.api }
      })
    },
    clearAuthorizeQueue() {
      o = []
    }
  }
})(require('lazyload'), require)
