'use strict'
!(function(require, directRequire) {
  const a = require('glob'),
    b = require('fs'),
    c = require('path'),
    d = require('./logger.js'),
    e = require('crypto')
  module.exports = async (f, g, h = {}) => {
    var i = [
      new Buffer(1),
      new Buffer(4),
      new Buffer(4),
      new Buffer(4),
      new Buffer(1)
    ]
    i[0].writeIntLE(190), i[1].writeInt32BE(1), i[4].writeIntLE(237)
    let j = 0,
      k = [],
      l = [],
      m = [],
      n = [],
      o = []
    return new Promise((p, q) => {
      let r = Object.assign({ nodir: !0 }, h),
        s = {},
        t = 0
      a(`${f}/**`, r, (a, r) => {
        if (!a) {
          r.forEach(a => {
            let d = b.readFileSync(a),
              g = c.relative(f, a)
            if (h.needMd5) {
              let a = e.createHash('md5')
              a.update(d)
              let b = a.digest('hex')
              if (((s[g] = b), h.ignoreFileMd5 && h.ignoreFileMd5[g] == b))
                return
            }
            let i = new Buffer(`/${g.replace(/\\/g, '/')}`)
            j++,
              m.push(i),
              n.push(d),
              /\.js\.map$/.test(a) ||
                ((t += d.length), (t += i.length), (t += 12))
          })
          let a = 18 + 12 * j + Buffer.concat(m).length
          l = m.map((b, c) => {
            let d = new Buffer(4)
            d.writeInt32BE(b.length)
            let e = new Buffer(4),
              f = n[c].length,
              g = a
            e.writeInt32BE(g), (a += f)
            let h = new Buffer(4)
            return h.writeInt32BE(f), Buffer.concat([d, b, e, h])
          })
          let q = new Buffer(4)
          q.writeInt32BE(j),
            l.unshift(q),
            (k = Buffer.concat(l)),
            (o = Buffer.concat(n)),
            i[2].writeInt32BE(k.length),
            i[3].writeInt32BE(o.length),
            (i = Buffer.concat(i))
          let u = Buffer.concat([i, k, o])
          b.writeFileSync(g, u),
            d.info(`pack.js create ${g} success!`),
            (t += 18),
            p({ destPath: g, data: u, totalSize: t, fileMd5Info: s })
        } else q(a)
      })
    })
  }
})(require('lazyload'), require)
