'use strict'
!(function(require, directRequire) {
  const a = require('fs'),
    b = require('path'),
    c = require('request'),
    { URL: d } = require('url'),
    e = require('querystring'),
    f = require('qrcode-terminal'),
    g = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    h = require('./6b0bd6bb0fa981d25d4c0fb4b85edbab.js'),
    i = require('./78294f0b5b50fc38258d7028553744ef.js'),
    j = require('./bc78839ccca8df9e5ceeb7fae11b7be2.js'),
    k = require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),
    l = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    m = require('./9c906d27ca74e18d0deaaa231e71fdfa.js'),
    n = require('./5bdce4f0657e887a1fda83e134c0b823.js'),
    o = require('./5719b6ded53098ffd9e848abcac30153.js'),
    p = require('./da7c31daaf542cf1796023d8e344b98a.js'),
    q = {
      SUCCESS: 405,
      SCANNED: 404,
      CANCELLED: 403,
      TIMEOUT: 402,
      ERROR: 500,
      KEEP_ALIVE: 408
    },
    r = {
      REQ_ERR_RETRY_INTERVAL: 500,
      SCANNED_TIMEOUT: 500,
      CANCELLED_TIMEOUT: 2e3,
      KEEP_ALIVE_TIMEOUT: 2e3,
      RELOAD_TO_LONGPOLL_TIMEOUT: 100,
      LONGPOLL_TIMEOUT: 6e4
    },
    s = 'darwin' === process.platform ? 'darwin' : 'win',
    t = (b = 'image', c) =>
      new Promise(async (d, e) => {
        try {
          let j
          if (((j = i.checkFormat(b)), j.error)) return e(j.error)
          if (((j = i.checkQROutput(c)), j.error)) return e(j.error)
          const k = await n.getLoginInfo(n.QRCODE_FORMAT.IMAGE),
            { qrcode: m, longPollURL: p } = k
          if (c)
            try {
              if ('base64' === b)
                a.writeFileSync(c, h.toBase64(m, 'jpeg'), 'utf8'),
                  d({ longPollURL: p, qrcode: c })
              else if ('image' === b)
                a.writeFileSync(c, m, null), d({ longPollURL: p, qrcode: c })
              else
                try {
                  const { body: b } = await g({
                    url: `${l.jsDecodeLoginQRCodeURL}`,
                    method: 'post',
                    body: m
                  })
                  if (b.result) {
                    let g = decodeURIComponent(b.result)
                    try {
                      f.generate(g, b => {
                        a.writeFileSync(c, b, null),
                          d({ longPollURL: p, qrcode: b })
                      })
                    } catch (a) {
                      e(o.ERROR.GENERIC_ERROR(a.toString()))
                    }
                  }
                } catch (a) {
                  e(o.ERROR.GENERIC_ERROR(a.toString()))
                }
            } catch (a) {
              e(o.ERROR.GENERIC_ERROR(a.toString()))
            }
          else
            try {
              if ('base64' === b) {
                const a = h.toBase64(m, 'jpeg')
                d({ longPollURL: p, qrcode: a })
              } else if ('image' === b) d({ longPollURL: p, qrcode: m })
              else
                try {
                  const { body: a } = await g({
                    url: `${l.jsDecodeLoginQRCodeURL}`,
                    method: 'post',
                    body: m
                  })
                  if (a.result) {
                    let b = decodeURIComponent(a.result)
                    try {
                      f.generate(b, a => {
                        d({ longPollURL: p, qrcode: a })
                      })
                    } catch (a) {
                      e(o.ERROR.GENERIC_ERROR(a.toString()))
                    }
                  }
                } catch (a) {
                  e(o.ERROR.GENERIC_ERROR(a.toString()))
                }
            } catch (a) {
              e(a)
            }
        } catch (a) {
          e(o.ERROR.GENERIC_ERROR(a.toString()))
        }
      }),
    u = a =>
      new Promise((b, c) => {
        v(b, c, a)
      }),
    v = async (a, b, d, e) => {
      c(
        {
          url: `${d}${e ? '&last=' + e : ''}&_=${+new Date()}`,
          headers: { 'Content-Type': 'application/javascript' },
          timeout: r.LONGPOLL_TIMEOUT,
          resolveWithFullResponse: !0
        },
        (c, e, f) => {
          if (!c) {
            eval(f)
            const c = window.wx_errcode,
              e = window.wx_code
            switch (c) {
              case q.SUCCESS: {
                let b = l.LOGIN_REDIRECT_URL
                ;(b = b.replace(/&amp;/g, '&')),
                  (b +=
                    (-1 < b.indexOf('?') ? '&' : '?') + `code=${e}&state=${s}`),
                  a(b)
                break
              }
              case q.SCANNED: {
                setTimeout(v.bind(this), r.SCANNED_TIMEOUT, a, b, d, c)
                break
              }
              case q.CANCELLED: {
                b()
                break
              }
              case q.TIMEOUT: {
                b(o.ERROR.QR_OUTDATED())
                break
              }
              case q.ERROR: {
                b(o.ERROR.LOGIN_RETRY())
                break
              }
              case q.KEEP_ALIVE:
                setTimeout(v.bind(this), r.KEEP_ALIVE_TIMEOUT, a, b, d)
              default:
            }
          } else if ('ECONNRESET' === c.code)
            return void b(o.ERROR.BAD_NETWORK())
        }
      )
    },
    w = a => {
      let b
      a.get('/login', async (a, c) => {
        try {
          ;(global.CLI.pendingLogin = !0), j.dispatch(m.loginExpired())
          const d = decodeURIComponent(a.mQuery.format || '') || 'image',
            e = decodeURIComponent(a.mQuery.qroutput || ''),
            f = decodeURIComponent(a.mQuery.cli || ''),
            g = decodeURIComponent(a.mQuery.from || '')
          f
            ? p('cli_login', null, null, g ? { engine_from: g } : null)
            : p('http_login', null, null, g ? { engine_from: g } : null)
          let h
          if (((h = i.checkFormat(d)), h.error))
            return (c.statusCode = 400), void c.end(h.error)
          if (((h = i.checkQROutput(e)), h.error))
            return (c.statusCode = 400), void c.end(h.error)
          const { qrcode: l, longPollURL: n } = await t(d, e)
          ;(c.statusCode = 200),
            c.end(l, 'image' === d ? null : 'utf8'),
            (b = k.LOGIN_STATUS.PENDING),
            u(n)
              .then(async a => {
                try {
                  await j.dispatch(m.login(a))
                  b = k.LOGIN_STATUS.SUCCESS
                } catch (a) {
                  b = k.LOGIN_STATUS.FAIL
                }
                global.CLI.pendingLogin = !1
              })
              .catch(() => {
                ;(b = k.LOGIN_QR_STATUS.FAIL), (global.CLI.pendingLogin = !1)
              })
        } catch (a) {
          ;(global.CLI.pendingLogin = !1),
            (c.statusCode = 400),
            c.end(a.toString())
        }
      }),
        a.get('/loginresult', async (a, c) => {
          ;(c.statusCode = 200), c.end(JSON.stringify({ loginStatus: b }))
        })
    }
  module.exports = { registerHandlers: w }
})(require('lazyload'), require)
