'use strict'
!(function(require, directRequire) {
  const a = require('./233d77ecf0781f44985f684f70e316d0.js'),
    b = require('./logger.js'),
    c = require('./df16f80636465ea18fc1a346ae18a580.js'),
    d = require('./df6d0ff021a69fb541c733de4dbba0fe.js'),
    e = require('./84858de8a097c9cf84ff2c2e3d86e2a9.js'),
    f = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    g = 'darwin' === process.platform ? 'darwin' : 'win'
  let h,
    i = []
  const j = new BroadcastChannel('login')
  j.onmessage = a => {
    let { type: b, data: c } = a.data
    switch (b) {
      case 'cleanRequestQueue': {
        ;(c = c || {}), k(c.err, c.data, 'other')
        break
      }
    }
  }
  const k = (a, b, c = 'self') => {
      i.forEach(c => {
        a ? c.reject(a) : c.resolve(b)
      }),
        (i = []),
        delete localStorage.lock_refreshing,
        clearTimeout(h),
        (h = null),
        'self' === c &&
          j.postMessage({
            type: 'cleanRequestQueue',
            err: a && a.toString(),
            data: b
          })
    },
    l = () => {
      b.info(`refresh wait timeout`), k()
    },
    m = function() {
      return new Promise((c, g) => {
        if (
          (i.push({ resolve: c, reject: g }),
          h || (h = setTimeout(l, 10000)),
          !localStorage.lock_refreshing)
        ) {
          let c = e.userInfo
          if (!c || !c.openid || !c.signature) return k(d.NOT_LOGIN)
          const g = '' + (global.isDevWindow ? global.devInfo.id : 'server')
          if (
            ((localStorage.lock_refreshing = g),
            localStorage.lock_refreshing === g)
          ) {
            let g = {
              url: f.refreshTicketURL,
              form: JSON.stringify({
                openid: c.openid,
                signature: c.signature
              }),
              method: 'post'
            }
            a(g, (a, c, f) => {
              if (a) return b.error(`requestRefreshTicket ${a}`), k(a)
              let g
              try {
                g = JSON.parse(f)
              } catch (a) {
                return (
                  b.error(`requestRefreshTicket JSON.parse catch error ${a}`),
                  k(a)
                )
              }
              let h = g.baseresponse,
                i = parseInt(h.errcode)
              if (0 != i)
                return (
                  b.error(`requestRefreshTicket error ${i}`), k(d.NOT_LOGIN)
                )
              let j = +new Date() + 1e3 * g.ticket_expired_time,
                l = c.headers['debugger-newticket'],
                m = e.userInfo
              ;(m.newticket = l),
                (m.ticketExpiredTime = j),
                (e.userInfo = m),
                k(null, JSON.parse(JSON.stringify(m)))
            })
          }
        }
      })
    }
  module.exports = {
    getUserInfo: () => {
      let a = e.userInfo
      return (
        (a.signature && a.openid && !(a.signatureExpiredTime < Date.now())) ||
          (e.userInfo = {}),
        e.userInfo
      )
    },
    requestRefreshTicket: m,
    requestLogin: function(b) {
      return new Promise((c, d) => {
        a({ url: b }, (a, b, f) => {
          if (!a) {
            let a = JSON.parse(f),
              g = a.baseresponse,
              h = g.errcode ? parseInt(g.errcode) : 0
            if (0 !== h) return void d(g.errmsg)
            let i = b.headers,
              j = i['debugger-signature'],
              k = i['debugger-newticket'],
              l = +new Date(),
              m = {
                signature: j,
                newticket: k,
                openid: a.openid,
                nickName: a.nickname,
                headUrl:
                  a.headurl ||
                  'https://res.wx.qq.com/zh_CN/htmledition/v2/images/web_wechat_no_contect.png',
                ticketExpiredTime: 1e3 * a.ticket_expired_time + l,
                signatureExpiredTime: 1e3 * a.signature_expired_time + l,
                sex: 1 === a.sex ? 'male' : 'female',
                province: a.province,
                city: a.city,
                contry: a.contry
              }
            ;(e.userInfo = m), c(m)
          } else d(`${a.toString()}`)
        })
      })
    },
    makeRequestOptionsWithLoginState: async function(a) {
      let b = Date.now(),
        c = e.userInfo,
        d = ''
      c.newticket && c.ticketExpiredTime && c.ticketExpiredTime > b
        ? (d = c.newticket)
        : (await m(), (d = e.userInfo.newticket))
      let f = -1 === a.url.indexOf('?') ? `?newticket=${d}` : `&newticket=${d}`
      return (a.url += f), a
    },
    getNewTicket: async () => {
      let a = Date.now(),
        b = e.userInfo,
        c = ''
      return (
        b.newticket && b.ticketExpiredTime && b.ticketExpiredTime > a
          ? (c = b.newticket)
          : (await m(), (c = e.userInfo.newticket)),
        c
      )
    }
  }
})(require('lazyload'), require)
