'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) {
  const a = require('fs'),
    b = require('path'),
    c = require('qrcode-terminal'),
    d = require('./84858de8a097c9cf84ff2c2e3d86e2a9.js'),
    e = require('./42191d95974f14b18961c9f2c730464e.js'),
    f = require('./cc2c2970ff81ae4a83123e81ee123da2.js'),
    g = require('./d3976cc01aeebc5b09e11c4135b6bd8d.js'),
    h = require('./d28a711224425b00101635efe1034c99.js'),
    i = require('./6242f55dbdfe53c2f07b7a51568311f2.js'),
    j = require('./f0466135fc8b3a662084784e5f4ac792.js'),
    k = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    l = require('./9fdd4ac31a05c27355910f0d74accd4c.js'),
    m = require('./5eb00762b104199e0cf0af7efbe591da.js'),
    n = require('./6b0bd6bb0fa981d25d4c0fb4b85edbab.js'),
    o = require('./36244f5aa8a25574083d64e28953b466.js'),
    p = require('./78294f0b5b50fc38258d7028553744ef.js'),
    q = require('./5719b6ded53098ffd9e848abcac30153.js'),
    r = require('./da7c31daaf542cf1796023d8e344b98a.js'),
    s = {
      network: {
        RequestDomain: [],
        WsRequestDomain: [],
        UploadDomain: [],
        DownloadDomain: []
      },
      setting: _extends({}, i.setting)
    },
    t = ({
      test: h = !0,
      projectpath: b,
      version: d,
      desc: e,
      appid: f,
      format: i = 'image',
      qroutput: g
    } = {}) =>
      new Promise(async (f, k) => {
        let r
        if (((r = p.checkProjectPath(b)), r.error)) return k(r.error)
        if (h)
          if (!i) i = 'image'
          else if (((r = p.checkFormat(i)), r.error)) return k(r.error)
        if (h && g && ((r = p.checkQROutput(g)), r.error)) return k(r.error)
        if (!h && ((r = p.checkVersion(d)), r.error)) return k(r.error)
        let s
        if (((r = p.checkProjectConfigJSON(b)), r.error)) return k(r.error)
        if (((s = r.config), !s.appid))
          return k(q.ERROR.NOT_FOUND_IN_PROJECT_CONFIG_JSON('appid'))
        let t
        try {
          t = await m.getAppInfo(s.appid)
        } catch (a) {
          k(a)
        }
        try {
          let m = await o.getProject(t, s, b),
            p = { test: h, tempProject: m, onFilesIgnored: function() {} }
          if (!h) (p.version = d), (p.desc = e)
          else if (s.compileType && s.condiction) {
            let a = s.compileType,
              b = s.condiction[a] || {},
              c = b.list && b.list[b.current]
            a == l.search
              ? c &&
                ((p.searchQuery = c.customQuery || c.query),
                (p.boxQI = c.customQuery ? '' : c.boxQI))
              : a == l.weapp &&
                ((p.page = c ? c.pathName : void 0),
                (p.query = c ? c.query : ''))
          } else (p.page = void 0), (p.query = '')
          j(p)
            .then(async b => {
              if (h) {
                const d = b.qrcode_img
                if ('terminal' == i)
                  try {
                    const b = await n.decodeQRCode(
                      new Buffer(d, 'base64'),
                      s.appid
                    )
                    c.generate(b, b => {
                      g && a.writeFileSync(g, b),
                        f({ qrcode: b, encoding: 'utf8' })
                    })
                  } catch (a) {
                    k(q.ERROR.GENERIC_ERROR(a.toString()))
                  }
                else
                  'image' == i
                    ? (g && a.writeFileSync(g, new Buffer(d, 'base64')),
                      f({ qrcode: new Buffer(d, 'base64'), encoding: null }))
                    : 'base64' == i &&
                      (g && a.writeFileSync(g, d, 'utf8'),
                      f({ qrcode: d, encoding: 'utf8' }))
              } else {
                const a = b.baseresponse
                if (a) {
                  const b = a.errcode
                  0 === b
                    ? f()
                    : k(
                        q.ERROR.GENERIC_ERROR(
                          '\u672A\u77E5\u4E0A\u4F20\u7ED3\u679C'
                        )
                      )
                } else
                  k(
                    q.ERROR.GENERIC_ERROR(
                      '\u672A\u77E5\u4E0A\u4F20\u7ED3\u679C'
                    )
                  )
              }
              global.CLI.project = null
            })
            .catch(a => {
              ;(global.CLI.project = null),
                k(q.ERROR.GENERIC_ERROR(a.toString()))
            })
        } catch (a) {
          ;(global.CLI.project = null), k(q.ERROR.GENERIC_ERROR(a.toString()))
        }
      })
  module.exports = {
    handleUpload: t,
    registerHandlers: a => {
      a.get('/preview', async (a, b) => {
        try {
          const c = decodeURIComponent(a.mQuery.projectpath || ''),
            d = decodeURIComponent(a.mQuery.format || ''),
            e = decodeURIComponent(a.mQuery.qroutput || ''),
            f = decodeURIComponent(a.mQuery.cli || ''),
            g = decodeURIComponent(a.mQuery.from || '')
          f
            ? r('cli_preview', null, null, g ? { engine_from: g } : null)
            : r('http_preview', null, null, g ? { engine_from: g } : null)
          const h = await t({
            projectpath: c,
            format: d,
            qroutput: e,
            test: !0
          })
          ;(b.statusCode = 200), b.end(h.qrcode, h.encoding)
        } catch (a) {
          a ? ((b.statusCode = 400), b.end(a)) : ((b.statusCode = 500), b.end())
        }
      }),
        a.get('/upload', async (a, b) => {
          try {
            const c = decodeURIComponent(a.mQuery.projectpath || ''),
              d = decodeURIComponent(a.mQuery.version || ''),
              e = decodeURIComponent(a.mQuery.desc || ''),
              f = decodeURIComponent(a.mQuery.format || ''),
              g = decodeURIComponent(a.mQuery.qroutput || ''),
              h = decodeURIComponent(a.mQuery.cli || ''),
              i = decodeURIComponent(a.mQuery.from || '')
            h
              ? r('cli_upload', null, null, i ? { engine_from: i } : null)
              : r('http_upload', null, null, i ? { engine_from: i } : null)
            await t({
              projectpath: c,
              version: d,
              desc: e,
              format: f,
              qroutput: g,
              test: !1
            })
            ;(b.statusCode = 200), b.end()
          } catch (a) {
            a
              ? ((b.statusCode = a.statusCode || 400), b.end(a.toString()))
              : ((b.statusCode = 500), b.end(q.ERROR.GENERIC_ERROR()))
          }
        })
    }
  }
})(require('lazyload'), require)
