'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, b) {
    if ('initsuccess' === b.type) {
      k = !0
      let a = n
      n = []
      for (const b of a) b[0]()
    } else if ('taskresult' === b.type) {
      const a = o[b.id]
      a
        ? (a.resolve && a.resolve(b.result), delete o[b.id])
        : console.warn('unrecognized task result of msg', b)
    } else if ('taskerror' === b.type) {
      console.warn('got task error', b.id)
      const a = o[b.id]
      a
        ? (a.reject && a.reject(b.error), delete o[b.id])
        : console.warn('unrecognized task error of msg', b)
    }
  }
  function b() {
    if (m) {
      try {
        m.removeAllListeners(), m.kill('SIGTERM')
      } catch (a) {
        console.warn('stopping child process error', a)
      }
      ;(m = null), (k = void 0), clearTimeout(l), (l = void 0)
      let a = n
      n = []
      for (const b of a) b[1]()
    }
  }
  function c() {
    let c = g.join(g.dirname(process.execPath), 'node')
    'darwin' !== process.platform && (c += '.exe'),
      (m = h.fork(i, ['--expose-gc'], {
        execPath: c,
        stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
        env: { START_SERVER: 'yes' }
      })),
      m.stdout.setEncoding('utf8'),
      m.stdout.on('data', a => {
        console.log('stdout: ' + a)
      }),
      m.stderr.on('data', a => {
        console.log('stderr: ' + a)
      })
    const d = m,
      e = (...a) => {
        if (m === d)
          try {
            b()
          } catch (a) {
            console.error('stop child process error', a)
          }
      }
    m.on('disconnect', e),
      m.on('close', e),
      m.on('exit', a => {
        e(a)
      }),
      m.on('error', (...a) => {
        e(...a), console.warn('child process encountered an error', ...a)
      }),
      m.on('message', b => {
        'object' == typeof b
          ? a(m, b)
          : console.warn('unrecognized msg from cp', b)
      })
  }
  async function d() {
    return m && !m.killed
      ? (clearTimeout(l), (l = void 0), Promise.resolve())
      : new Promise((a, b) => {
          if (!m || m.killed)
            try {
              c()
            } catch (a) {
              b(a)
            }
          n.push([a, b])
        })
  }
  function e(a = 12) {
    const b =
      '=-_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' + Date.now()
    let c = ''
    for (let d, e = 0; e < a; e++)
      (d = Math.floor(Math.random() * b.length)), (c += b.substring(d, d + 1))
    return c
  }
  function f(a, b) {
    if (!m || m.killed) return !1
    o[a.id] = a
    try {
      m.send(a, c => {
        c && (delete o[a.id], b && b(c))
      })
    } catch (a) {
      return console.error(a), !1
    }
    return !0
  }
  const g = require('path'),
    h = require('child_process'),
    {
      fileName: i,
      dirName: j
    } = require('./aab36a6b9ac5fdff1d9b42f1bccd6cb7.js')
  let k,
    l,
    m = null,
    n = [],
    o = {}
  module.exports = {
    start: d,
    stop: function(a = !1) {
      return !m || m.killed
        ? void 0
        : a
          ? void b()
          : void (
              void 0 != l ||
              (l = setTimeout(() => {
                b()
              }, 5e3))
            )
    },
    runTask: async function(a) {
      return new Promise(async (b, c) => {
        if (!a) return console.warn('invalid task param', a), c()
        await d()
        const g = a.id || `${e()}`,
          h = {
            type: 'task',
            id: g,
            body: a.body,
            path: a.path,
            headers: _extends({}, a.headers || {}),
            query: _extends({}, a.query || {})
          },
          i = f(h, c)
        return i
          ? void ((h.resolve = b), (h.reject = c), (o[h.id] = h))
          : c('send task failed')
      })
    },
    getCh() {
      return m
    },
    getTasksMap() {
      return o
    }
  }
})(require('lazyload'), require)
