'use strict'
!(function(require, directRequire) {
  function a(a) {
    return a
      .replace(F, '')
      .replace(G, '')
      .replace(H, '')
  }
  function b(a = 12) {
    const b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    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 c() {
    let a = u.getUserInfo(),
      b = a ? a.openid : 'unknow'
    return b || ''
  }
  function d(a, b) {
    let d = a.appid,
      e = c()
    if (b || !I[`${d}_${e}`]) {
      let a = n.join(E, e, d)
      ;(b || !o.existsSync(a)) &&
        (q.sync(n.join(a, A)), q.sync(n.join(a, B)), q.sync(n.join(a, C))),
        (I[`${d}_${e}`] = !0)
    }
  }
  function e(a) {
    return F.test(a) ? A : G.test(a) ? B : H.test(a) ? C : D
  }
  function f(a, d, e) {
    let f = a.appid,
      g = c(),
      h = p.createHash('md5')
    return (
      (d = d || `${Date.now()}`),
      h.update(d),
      `${f}.${g}.${b()}${h.digest('hex')}${e}`
    )
  }
  function g(a) {
    let b = a.appid,
      d = c()
    return n.join(E, d, b)
  }
  function h(b, d) {
    ;/^\/usr\/?$/i.test(b) || /^http:\/\/usr\/?$/i.test(b)
      ? (b = '/usr/./')
      : /^\/tmp\/?$/i.test(b) || /^http:\/\/tmp\/?$/i.test(b)
        ? (b = '/tmp/./')
        : (/^\/store\/?$/i.test(b) || /^http:\/\/store\/?$/i.test(b)) &&
          (b = '/store/./')
    let f = e(b)
    d = d || v.getCurrent()
    let g
    if (f !== D) {
      b = a(b)
      let e, h
      ;(e = d.appid || ''), (h = c())
      let i = n.join(E, h, e, f)
      if (
        ((i = /(\/|\\)$/.test(i) ? i : i + '/'),
        (i = n.normalize(i)),
        (g = n.join(E, h, e, f, b)),
        !g.startsWith(i))
      )
        return { error: !0 }
    } else {
      let a = d.miniprogramRoot,
        c = n.extname(b),
        e = { '.wxml': !0, '.js': !0, '.wxss': !0 }
      if (
        ('mingganci' !== v.getCurrent().compileType &&
          'game' !== v.getCurrent().compileType &&
          (e['.json'] = !0),
        !e[c])
      ) {
        let c
        if (
          (a
            ? ((c = n.join(d.projectpath, a)),
              (c = /(\/|\\)$/.test(c) ? c : c + '/'),
              (c = n.normalize(c)),
              (g = n.join(d.projectpath, a, b)))
            : ((c = n.join(d.projectpath)),
              (c = /(\/|\\)$/.test(c) ? c : c + '/'),
              (c = n.normalize(c)),
              (g = n.join(d.projectpath, b))),
          !g.startsWith(c))
        )
          return { error: !0 }
      }
    }
    return { error: !1, fileRealPath: g, type: f }
  }
  function i(a, b, d) {
    let e = f(a, b, d),
      g = c(),
      h = n.join(E, g, a.appid, B, e)
    return o.writeFileSync(h, b), `${y}${e}`
  }
  function j() {
    const a = v.getCurrentConfig(),
      b = a.setting.MaxFileStorageSize
    return 1024 * (1024 * (b || 10))
  }
  function k(a, b) {
    const d = c(),
      e = a.appid,
      g = n.join(E, d, e, b)
    let h = 0,
      i = {},
      f = r.sync('**/**', { statCache: i, nodir: !0, stat: !0, cwd: g }) || []
    return (
      f.forEach(a => {
        const b = i[n.join(g, a).replace(/\\/g, '/')]
        b && (h += b.size || 0)
      }),
      h
    )
  }
  function l(a, b, c) {
    const d = 1,
      e = 65536
    ;(function(a, b, c) {
      if ('string' != typeof b) throw new Error('_dest must be a string')
      if ('string' != typeof a) throw new Error('_src must be a string')
      if ('number' == typeof c && c && c !== d)
        throw Error(`EINVAL: invalid argument, copyfile -> '${b}'`)
    })(a, b, c)
    const f = c === d ? 'wx' : 'w',
      { size: g, mode: h } = o.statSync(a),
      j = o.openSync(a, 'r'),
      k = o.openSync(b, f, h),
      l = g < e ? g : e
    let m = 0
    const n = g < e ? 0 : g % e,
      p = 0
    let q = Buffer.allocUnsafe(l)
    for (let d = 0; l + m + n <= g; d++, m = l * d)
      o.readSync(j, q, p, l, m), o.writeSync(k, q, p, l, m)
    if (n) {
      const a = n
      ;(q = Buffer.allocUnsafe(a)),
        o.readSync(j, q, p, a, m),
        o.writeSync(k, q, p, a, m)
    }
    o.closeSync(j), o.closeSync(k)
  }
  function m(a, b) {
    let c = h(b)
    if (c.type !== A)
      return { error: !0, reason: `permission denied, open ${b}` }
    let d = c.fileRealPath,
      e = n.dirname(d),
      f = !1
    try {
      let a = o.lstatSync(e)
      f = a.isDirectory()
    } catch (a) {}
    return f
      ? { error: !1, realDirPath: e, fileRealPath: d }
      : { error: !0, reason: `no such file or directory ${b}` }
  }
  const n = require('path'),
    o = require('fs'),
    p = require('crypto'),
    q = require('mkdir-p'),
    r = require('glob'),
    s = require('rmdir'),
    t = require('adm-zip'),
    u = require('./89ba85d67a88f7636d657c22b5d3e038.js'),
    v = require('./3bfffbe88b3d923921f851c0697974fe.js'),
    {
      DATA_PATH: w,
      USER_DATA_PATH: x,
      TMP_DATA_PATH: y,
      STORE_DATA_PATH: z
    } = require('./37be435102276ea9cf47609ff6535cd4.js'),
    A = 'usr',
    B = 'tmp',
    C = 'store',
    D = 'package',
    { WeappFileSystem: E } = require('./92320c1386e6db6a6f2556736a9bc280.js'),
    F = /^(http:\/)?\/usr\//,
    G = /^(http:\/)?\/tmp\//,
    H = /^(http:\/)?\/store\//
  var I = {}
  const J = (a, b = !1) => {
    let c
    switch (a) {
      case A: {
        c = h('http://usr/./').fileRealPath
        break
      }
      case B: {
        c = h('http://tmp/./').fileRealPath
        break
      }
      case C: {
        c = h('http://store/./').fileRealPath
        break
      }
      case D: {
        c = h('./').fileRealPath
        break
      }
      default:
        return null
    }
    return (
      (c =
        b && c
          ? /(\/|\\)$/.test(c)
            ? c
            : c + '/'
          : /(\/|\\)$/.test(c)
            ? c.slice(0, -1)
            : c),
      n.normalize(c)
    )
  }
  var K = {
    saveStoreFile: function(a, b) {
      let c = a.appid,
        d = h(b)
      if (d.type === B) {
        let a = d.fileRealPath
        if (o.existsSync(a)) {
          const b = o.lstatSync(a)
          if (b.isSymbolicLink())
            return { error: !0, reason: 'tempFilePath is not a regular file' }
          let c = n.basename(a),
            d = o.readFileSync(a),
            e = n.join(a, '..', '..', C, c)
          o.writeFileSync(e, d)
          try {
            o.unlinkSync(a)
          } catch (b) {
            console.error('unlink file', a, 'failed')
          }
          return { error: !1, savedFilePath: `${z}${c}` }
        }
      }
      return { error: !0, reason: 'tempFilePath file not exist' }
    },
    saveUsrFile: function(a, b, d) {
      let e = a.appid,
        f = c(),
        g = h(d, a)
      if (g.type !== A)
        return { error: !0, reason: `permission denied, open "${d}"` }
      let i = g.fileRealPath,
        j = n.dirname(i),
        k = !1
      try {
        let a = o.lstatSync(j)
        k = a.isDirectory()
      } catch (a) {}
      if (!k)
        return {
          error: !0,
          reason: `no such file or directory "${n.dirname(d)}"`
        }
      let l = h(b)
      if (!o.existsSync(l.fileRealPath))
        return { error: !0, reason: `no such file or directory "${b}"` }
      const m = o.lstatSync(l.fileRealPath)
      if (m.isSymbolicLink())
        return { error: !0, reason: 'tempFilePath is not a regular file' }
      let p = o.readFileSync(l.fileRealPath)
      return o.writeFileSync(i, p), { error: !1, savedFilePath: d }
    },
    copyFileToTemp: function(a, b, c) {
      let d = o.readFileSync(b)
      return i(a, d, c)
    },
    copyFileDataToTemp: i,
    getUsrFileList: function(a) {
      let b = c(),
        d = a.appid,
        e = n.join(E, b, d, C),
        f = {},
        g = r.sync('**/**', { statCache: f, nodir: !0, stat: !0, cwd: e }),
        h = 0
      for (let b in f) h += f[b].size
      return { error: !1, fileList: g, totalSize: h }
    },
    getSavedFileList: function(a) {
      let b = c(),
        d = a.appid,
        e = n.join(E, b, d, C),
        f = 0,
        g = {},
        h = r.sync('**/**', { statCache: g, nodir: !0, stat: !0, cwd: e }),
        i = h.map(a => {
          let b = g[n.join(e, a).replace(/\\/g, '/')]
          return (
            (f += b.size),
            {
              filePath: `${z}${a}`,
              size: b.size,
              createTime: parseInt(b.ctime.getTime() / 1e3)
            }
          )
        })
      return { error: !1, fileList: i, totalSize: f }
    },
    readFile: function(a, b, c) {
      let d = h(b),
        e = d.fileRealPath
      if (!o.existsSync(e)) return { error: !0, reason: `${b} not found` }
      const f = o.lstatSync(e)
      if (f.isSymbolicLink())
        return { error: !0, reason: `${b} is not a regular file` }
      let g = o.readFileSync(e, c)
      return { error: !1, fileData: g }
    },
    writeFile: function(a, b, c, d = 'utf8') {
      let e = m(a, b)
      if (e.error) return e
      let f = e.fileRealPath
      if (o.existsSync(f)) {
        const a = o.lstatSync(f)
        if (a.isSymbolicLink())
          return { error: !0, reason: `${b} is not a regular file` }
        if (a.isDirectory())
          return {
            error: !0,
            reason: `illegal operation on a directory, open ${b}`
          }
      }
      try {
        const b = Buffer.from(c, d),
          e = b.byteLength || 0
        if (k(a, A) + k(a, C) + e > j(a))
          return {
            error: !0,
            reason: 'the maximum size of the file storage limit is exceeded'
          }
      } catch (a) {}
      return o.writeFileSync(f, c, d), { error: !1 }
    },
    mkdir: function(a, b) {
      let c = m(a, b)
      if (c.error) return c
      let d = c.fileRealPath
      return o.existsSync(d)
        ? { error: !0, reason: `file already exists ${b}` }
        : (q.sync(d), { error: !1 })
    },
    rmdir: function(a, b) {
      let d = m(a, b)
      if (d.error) return d
      let e = d.fileRealPath,
        f = d.realDirPath,
        g = c()
      if (f === n.join(E, g, a.appid))
        return { error: !0, reason: `permission denied, open ${b}` }
      if (!o.existsSync(e) || !o.lstatSync(e).isDirectory())
        return { error: !0, reason: `no such file or directory  ${b}` }
      let h = o.readdirSync(e).filter(() => {
        return !0
      })
      return 0 === h.length
        ? (o.rmdirSync(e), { error: !1 })
        : { error: !0, reason: `directory not empty` }
    },
    readdir: function(a, b) {
      let c = m(a, b + '/')
      if (c.error) return c
      let d = c.fileRealPath
      if (!o.existsSync(d))
        return { error: !0, reason: `no such file or directory  ${b}` }
      if (!o.lstatSync(d).isDirectory())
        return { error: !0, reason: `not a directory  ${b}` }
      let e = o.readdirSync(d).filter(() => {
        return !0
      })
      return { error: !1, files: e }
    },
    createTmpfileName: function(a, b) {
      return `${y}${f(a, '', b)}`
    },
    initTmpfileName: function(a, b, c) {
      return `${y}${f(a, b, c)}`
    },
    getFileSystemDir: g,
    unlink: function(a, b) {
      let c = h(b, a)
      if (c.type === D)
        return { error: !0, reason: `fail permission denied, open "${b}"` }
      let d = c.fileRealPath
      if (!o.existsSync(d))
        return { error: !0, reason: `fail no such file or directory "${b}"` }
      let e = o.lstatSync(d)
      if (e.isDirectory())
        return {
          error: !0,
          reason: `fail operation not permitted, unlink "${b}"`
        }
      try {
        o.unlinkSync(d)
      } catch (a) {
        return { error: !0, reason: a.toString() }
      }
      return { error: !1 }
    },
    stat: function(a, b) {
      let c = h(b, a)
      if (c.type === D && 0 === b.indexOf(w))
        return { error: !0, reason: `fail permission denied, open "${b}"` }
      let d = c.fileRealPath
      if (!o.existsSync(d))
        return { error: !0, reason: `fail no such file or directory "${b}"` }
      let e = o.lstatSync(d)
      return {
        error: !1,
        mode: e.mode,
        size: e.size,
        lastAccessedTime: parseInt(e.atimeMs / 1e3),
        lastModifiedTime: parseInt(e.mtimeMs / 1e3)
      }
    },
    saveBase64DataToFile: function(a, b, d) {
      let e = a.appid,
        g = c(),
        h = f(a, b, d)
      return (
        o.writeFileSync(n.join(E, g, e, B, h), b, { encoding: 'base64' }),
        { error: !1, tempFilePath: `${y}${h}` }
      )
    },
    clearFileSystem: function(a) {
      let b = g(a)
      s(b, () => {
        d(a, !0)
      })
    },
    unzip: function(a, b, c) {
      const d = h(b),
        e = d.fileRealPath
      if (!e || !o.existsSync(e))
        return { error: !0, reason: `no such file or directory, unzip "${b}"` }
      const f = c,
        g = h(c),
        i = g.fileRealPath
      if (!i || g.type !== A)
        return { error: !0, reason: `permission denied, unzip "${f}"` }
      const l = n.dirname(i)
      if (!o.existsSync(l) || o.lstatSync(l).isSymbolicLink())
        return { error: !0, reason: `no such file or directory, unzip "${f}"` }
      const m = o.lstatSync(e)
      if (m.isSymbolicLink())
        return { error: !0, reason: `${b} is not a regular file` }
      try {
        const b = new t(e),
          c = b.getEntries()
        let d = 0
        for (const a of c) {
          const c = a.entryName
          if (/^\.\.(\\|\/)+/.test(c)) throw new Error('illegal access')
          if (/(\\|\/)+\.\.(\\|\/)+/.test(c)) throw new Error('illegal access')
          if (/(\\|\/)+\.\.$/.test(c)) throw new Error('illegal access')
          const e = b.readFile(a)
          d += e.byteLength
        }
        if (k(a, A) + k(a, C) + d > j(a))
          return {
            error: !0,
            reason: 'the maximum size of the file storage limit is exceeded'
          }
        b.extractAllTo(i, !0)
      } catch (a) {
        return { error: !0, reason: 'unzip failed' }
      }
      return { error: !1 }
    },
    rename: function(a, b, c) {
      const d = h(b),
        e = d.fileRealPath
      if (!e || !o.existsSync(e))
        return { error: !0, reason: `no such file or directory, rename "${b}"` }
      if (d.type === D)
        return {
          error: !0,
          reason: `permission denied, rename "${b}" -> "${c}"`
        }
      const f = h(c),
        g = f.fileRealPath
      if (!g || f.type !== A)
        return {
          error: !0,
          reason: `permission denied, rename "${b}" -> "${c}"`
        }
      const i = J(A, !0),
        j = J(B, !0),
        k = J(C, !0),
        l = J(D, !0)
      if (
        !e.startsWith(i) &&
        !e.startsWith(j) &&
        !e.startsWith(k) &&
        !e.startsWith(l)
      )
        return {
          error: !0,
          reason: `permission denied, rename "${b}" -> "${c}"`
        }
      if (e === i || e === j || e === k || e === l)
        return {
          error: !0,
          reason: `permission denied, rename "${b}" -> "${c}"`
        }
      if (!g.startsWith(i) || i === g)
        return {
          error: !0,
          reason: `permission denied, rename "${b}" -> "${c}"`
        }
      if (o.lstatSync(e).isSymbolicLink())
        return { error: !0, reason: `${b} is not a regular file` }
      if (o.existsSync(g) && o.lstatSync(g).isSymbolicLink())
        return { error: !0, reason: `${c} is not a regular file` }
      const m = n.dirname(g)
      if (!o.existsSync(m) || o.lstatSync(m).isSymbolicLink())
        return { error: !0, reason: `no such file or directory, rename "${c}"` }
      try {
        o.renameSync(e, g)
      } catch (a) {
        return { error: !0, reason: 'rename failed' }
      }
      return { error: !1 }
    },
    copyFile: function(a, b, c) {
      const d = h(b),
        e = d.fileRealPath
      if (!e || !o.existsSync(e))
        return {
          error: !0,
          reason: `no such file or directory, copyFile "${b}"`
        }
      const f = h(c),
        g = f.fileRealPath
      if (!g || f.type !== A)
        return {
          error: !0,
          reason: `permission denied, copyFile "${b}" -> "${c}"`
        }
      const i = o.lstatSync(e)
      if (!i.isFile())
        return {
          error: !0,
          reason: `permission denied, copyFile "${b}" -> "${c}"`
        }
      if (i.isSymbolicLink())
        return { error: !0, reason: `${b} is not a regular file` }
      const m = k(a, A) + k(a, C)
      if (m + (i.size || 0) > j(a))
        return {
          error: !0,
          reason: 'the maximum size of the file storage limit is exceeded'
        }
      const p = J(A, !0)
      if (!g.startsWith(p) || p === g)
        return {
          error: !0,
          reason: `permission denied, copyFile "${b}" -> "${c}"`
        }
      if (o.existsSync(g) && o.lstatSync(g).isSymbolicLink())
        return { error: !0, reason: `${c} is not a regular file` }
      if (g === e) return { error: !1 }
      if (o.existsSync(g)) {
        try {
          o.unlinkSync(g)
        } catch (a) {}
        try {
          o.rmdirSync(g)
        } catch (a) {}
      }
      const q = n.dirname(g)
      if (!o.existsSync(q) || o.lstatSync(q).isSymbolicLink())
        return {
          error: !0,
          reason: `no such file or directory, copyFile "${c}"`
        }
      try {
        l(e, g)
      } catch (a) {
        return { error: !0, reason: 'copyFile failed' }
      }
      return { error: !1 }
    },
    appendFile: function(a, b, c, d) {
      const e = h(b)
      if (!e || e.type !== A)
        return { error: !0, reason: `permission denied, open "${b}"` }
      const f = e.fileRealPath
      if (!f || !o.existsSync(f))
        return { error: !0, reason: `no such file or directory, open "${b}"` }
      const g = o.lstatSync(f)
      if (!g.isFile())
        return {
          error: !0,
          reason: `illegal operation on a directory, open "${b}"`
        }
      if (g.isSymbolicLink())
        return { error: !0, reason: `${b} is not a regular file` }
      const i = Buffer.from(c, d || 'utf-8'),
        l = k(a, A) + k(a, C)
      if (l + (i.byteLength || 0) > j(a))
        return {
          error: !0,
          reason: 'the maximum size of the file storage limit is exceeded'
        }
      try {
        o.appendFileSync(f, c, d || 'utf-8')
      } catch (a) {
        return { error: !0, reason: (a.toString && a.toString()) || a }
      }
      return { error: !1 }
    }
  }
  Object.getOwnPropertyNames(K).forEach(function(a) {
    const b = K[a]
    K[a] = function() {
      return d(arguments[0]), b.apply(K, arguments)
    }
  }),
    (K.getFileRealPath = h),
    (K.removeSavedFile = function(a) {
      let b = h(a),
        c = b.fileRealPath
      if (b.type === C && o.existsSync(c)) {
        try {
          o.unlinkSync(c)
        } catch (a) {}
        return { error: !1 }
      }
      return { error: !0, reason: 'file not exist' }
    }),
    (K.getFileStat = function(a) {
      let b = h(a),
        c = b.fileRealPath
      try {
        let a = o.statSync(c)
        return {
          error: !1,
          size: a.size,
          createTime: parseInt(a.ctime.getTime() / 1e3)
        }
      } catch (a) {
        return { error: !0, reason: 'file not found' }
      }
    }),
    (K.getFileInfo = function(a, b, c) {
      let d = h(a)
      if (c && d.type !== c) return { error: !0, reason: 'file not find' }
      let e = d.fileRealPath
      if (o.existsSync(e)) {
        let c = o.lstatSync(e)
        if (c.isDirectory()) return { error: !0, reason: `${a} is directory` }
        if (c.isSymbolicLink())
          return { error: !0, reason: `${a} is not a regular file` }
        let d = o.readFileSync(e),
          f = ''
        if ('md5' === b) {
          let a = p.createHash('md5')
          a.update(d), (f = a.digest('hex'))
        } else if ('sha1' === b) {
          let a = p.createHash('sha1')
          a.update(d), (f = a.digest('hex'))
        }
        return { error: !1, size: c.size, digest: f }
      }
      return { error: !0, reason: 'file not exist' }
    }),
    (K.isLocalId = function(a) {
      return (
        0 === a.indexOf(x + '/') || 0 === a.indexOf(y) || 0 === a.indexOf(z)
      )
    }),
    (K.USR = 'usr'),
    (K.TMP = 'tmp'),
    (K.STORE = 'store'),
    (K.PACKAGE = 'package'),
    (K.STORE_DATA_PATH = z),
    (K.DATA_PATH = w),
    (K.USER_DATA_PATH = x),
    (K.TMP_DATA_PATH = y),
    (module.exports = K)
})(require('lazyload'), require)
