'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('./ad554134baaa7dc37c79f2d0af026415.js'),
    c = require('querystring'),
    d = require('./c604817bbbe125c83759d41efefe4ee8.js'),
    e = {
      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(f = {}) {
    const g = _extends({}, e, f || {}),
      {
        taskName: h,
        config: i,
        dataStr: j,
        maxTimeout: k,
        useBackup: l,
        downgrade: m,
        onAskShouldRun: n,
        onBeforeRun: o,
        onAfterRun: p,
        onRunSuccess: q,
        onRunFail: r,
        forceBackup: s
      } = g
    return new Promise(async (e, f) => {
      try {
        if ('function' == typeof n && !n()) return f()
        const d = i
        let g = ''
        try {
          g = decodeURIComponent(c.stringify(d))
        } catch (a) {
          g = ''
        }
        const l = await (async () =>
          new Promise(async (c, f) => {
            'function' == typeof o && o.call(null)
            let e = !1
            !s &&
              k &&
              setTimeout(() => {
                e || (b.stop(!0), f(new Error('max timeout')))
              }, k),
              s
                ? f('force backup')
                : (async () => {
                    try {
                      const f = Date.now()
                      console.log('before run Task', `${h}?${g}`)
                      const i = await b.runTask({
                        path: a(h),
                        query: d,
                        headers: { downgrade: m ? 'yes' : 'no' },
                        body: j
                      })
                      console.log('done run Task', `${h}?${g}`, Date.now() - f),
                        (e = !0),
                        c(i)
                    } catch (a) {
                      f(a)
                    }
                  })()
          }))()
        'function' == typeof q && q.call(null),
          'function' == typeof p && p.call(null),
          e(l)
      } catch (b) {
        const c = a => {
          'function' == typeof r && r.call(null),
            'function' == typeof p && p.call(null),
            a instanceof Error ? f(a) : f(new Error(a))
        }
        if (s || l)
          try {
            const b = Date.now(),
              c = await d(a(h), i, j)
            console.log('task', h, 'backup method spent time', Date.now() - b),
              'function' == typeof q && q.call(null),
              'function' == typeof p && p.call(null),
              e(c)
          } catch (a) {
            c(a)
          }
        else c(b)
      }
    })
  }
})(require('lazyload'), require)
