'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) {
  function a(a) {
    return '/' + a
  }
  const b = require('./df19554acb2f1b6c76cb83ec2b7e8877.js'),
    c = require('querystring'),
    d = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    e = require('./3dc69cc05e660d78ed40092685e884d4.js'),
    f = a => d(_extends({ needRandom: -1 }, a)),
    g = {
      taskName: '',
      config: {},
      dataStr: '',
      maxTimeout: 0,
      useBackup: !0,
      downgrade: !1,
      onBeforeRun: null,
      forceBackup: !1,
      onAfterRun: null,
      onRunSuccess: null,
      onRunFail: null,
      onAskShouldRun: null
    }
  module.exports = async function(d = {}) {
    const h = _extends({}, g, d || {}),
      {
        taskName: i,
        config: j,
        dataStr: k,
        maxTimeout: l,
        useBackup: m,
        downgrade: n,
        onAskShouldRun: o,
        onBeforeRun: p,
        onAfterRun: q,
        onRunSuccess: r,
        onRunFail: s,
        forceBackup: t
      } = h
    return new Promise(async (d, g) => {
      try {
        if ('function' == typeof o && !o()) return g()
        const a = {}
        for (const b in j) a[b] = `${encodeURIComponent(j[b])}`
        const e = c.stringify(a),
          h = await (async () =>
            new Promise(async (a, c) => {
              let d = 10,
                g = !1
              'function' == typeof p && p.call(null)
              const h = async () => {
                let j
                try {
                  j = await b.start()
                } catch (a) {
                  return c(a)
                }
                const l = `http://127.0.0.1:${j}/${i}?${e}`
                try {
                  console.log('before send request', l, 1 * new Date())
                  const b = await f({
                    url: l,
                    body: k,
                    method: 'post',
                    headers: { downgrade: n ? 'yes' : 'no' }
                  })
                  ;(g = !0),
                    console.log('done request', l, 1 * new Date()),
                    a(b)
                } catch (a) {
                  --d
                  const b = 400 + 100 * (10 - d)
                  0 <= d
                    ? (console.warn(
                        '[runTask] request error, retry in ' + b + 'ms',
                        a && a.message ? a.message : a
                      ),
                      setTimeout(() => {
                        h()
                      }, b))
                    : (console.warn('[runTask] tried a lot of times. give up.'),
                      c(a))
                }
              }
              !t &&
                l &&
                setTimeout(() => {
                  g || (b.stop(!0), c(new Error('max timeout')))
                }, l),
                t ? c('force backup') : h()
            }))()
        'function' == typeof r && r.call(null),
          'function' == typeof q && q.call(null),
          d(h.body)
      } catch (b) {
        const c = a => {
          'function' == typeof s && s.call(null),
            'function' == typeof q && q.call(null),
            a instanceof Error ? g(a) : g(new Error(a))
        }
        if (t || m)
          try {
            const b = Date.now(),
              c = await e(a(i), j, k)
            console.log('task', i, 'backup method spent time', Date.now() - b),
              'function' == typeof r && r.call(null),
              'function' == typeof q && q.call(null),
              d(c)
          } catch (a) {
            c(a)
          }
        else c(b)
      }
    })
  }
})(require('lazyload'), require)
