'use strict'
!(function(require, directRequire) {
  function a(a) {
    let b = i.createHash('md5')
    return b.update(a).digest('hex')
  }
  async function b(a, b = () => {}) {
    return new Promise(async (c, e) => {
      try {
        const e = Date.now(),
          f = [...a]
        b('compilejs', '\u6B63\u5728\u5904\u7406 ' + a[0].file)
        const g = await Promise.all(
          a.map(a => {
            return k({
              taskName: 'processJS',
              config: {
                projectPath: a.projectPath,
                file: a.file,
                es6: a.es6 ? 'yes' : 'no',
                minified: a.minified ? 'yes' : 'no',
                sourceMaps: 'map',
                sourceFileName: d.basename(a.file)
              },
              dataStr: a.code,
              maxTimeout: 180000,
              useBackup: !0,
              downgrade: !1,
              onAfterRun: () => {
                const c = f.findIndex(b => b === a)
                0 <= c && f.splice(c, 1),
                  f[0] &&
                    b('compilejs', '\u6B63\u5728\u5904\u7406 ' + f[0].file)
              }
            })
          })
        )
        console.log(`compilejs all files cost time: ${Date.now() - e}`), c(g)
      } catch (a) {
        a instanceof Error ? e(a) : e(new Error(a))
      }
    })
  }
  const c = require('fs'),
    d = require('path'),
    e = require('mkdir-p'),
    f = require('babel-core'),
    g = require('uglify-js'),
    h = require('./162bf2ee28b76d3b3d95b685cede4146.js'),
    i = require('crypto'),
    j = require('./48679210e49dc5028a8b6642263eba75.js'),
    k = require('./41168dca39589e852da6631126d0f94d.js'),
    l = require('./common/locales/index.js'),
    m = require('./cdbf7243dc99f8461acbb1d57af1d8ae.js'),
    { bufToUTF8: n } = require('./efc820e1b92d6e4063535296d4a24213.js'),
    {
      FILE_NOT_UTF8: o,
      BABEL_TRANS_JS_ERR: p,
      UGLIFY_JS_ERR: q,
      BABILI_JS_ERR: r
    } = require('./949d8235c744ced2a80121e4dba34c28.js')
  var s = {}
  module.exports = function(f, g = {}) {
    s[f.hash] || (s[f.hash] = {})
    let k = s[f.hash]
    const i = g.onProgressUpdate || function() {},
      p = g.onFilesIgnored || function() {}
    return new Promise(async (q, r) => {
      let { es6: s, minified: u, newFeature: t } = f.setting,
        v = await j(f),
        w = v.openDataContext || v.subContext,
        { distPath: x } = g,
        y = await h(f),
        z = y.getAllJSFiles()
      const A = [],
        B = []
      for (let b = 0, c = z.length; b < c; b++) {
        let c = z[b],
          e = w && 0 === c.indexOf(w),
          f = d.normalize(c.replace(/\.js$/, '')),
          g = y.getFile(c, null),
          h = n(g)
        if (void 0 === h) {
          let a = new Error(l.config.FILE_NOT_UTF8.format(c))
          return (a.code = o), r(a)
        }
        let i = Date.now(),
          j = a(h),
          p = `${c}_${s}_${u}`
        if (k[p] && k[p].md5 === j)
          console.log('compile js', c, 'in cache, skip.')
        else if ((s || u) && 512000 > h.length)
          A.push({
            projectPath: y.srcPath,
            file: c,
            code: h,
            es6: s,
            minified: u,
            md5: j
          })
        else {
          let a = m(d.join(y.srcPath, c), h)
          k[`${c}_${s}_${u}`] = {
            md5: j,
            jsCode: h,
            map: JSON.stringify(a),
            ignored: (s || u) && 512000 <= h.length
          }
        }
      }
      if (0 < A.length) {
        let a = []
        try {
          a = await b(A, i)
        } catch (a) {
          return r(a)
        }
        for (let b = 0, c = A.length; b < c; b++) {
          if (a[b].error) {
            const c = a[b].error,
              d = new Error(c.message)
            return (d.code = c.code), r(d)
          }
          const { file: c, md5: d } = A[b]
          k[`${c}_${s}_${u}`] = { md5: d, jsCode: a[b].code, map: a[b].map }
        }
      }
      for (let a = 0, b = z.length; a < b; a++) {
        const b = z[a]
        let f = d.join(x, b),
          g = d.dirname(f)
        e.sync(g)
        const h = k[`${b}_${s}_${u}`]
        if (h && h.jsCode) {
          const a = h.jsCode
          c.writeFileSync(f, a),
            h.ignored && B.push(b),
            h.map && c.writeFileSync(d.join(x, `${b}.map`), h.map)
        }
      }
      p(B), q()
    })
  }
})(require('lazyload'), require)
