'use strict'
!(function(require, directRequire) {
  function a(a) {
    let b = k.getCurrentConfig().gameApp,
      c = b ? r : q
    return 0 === a.indexOf(c)
  }
  function b(a) {
    return 0 === a.indexOf(p)
  }
  function c(b) {
    if (a(b)) {
      let a = k.getCurrentConfig().gameApp,
        c = a ? r : q,
        d = b.replace(c, '').replace(/\/$/, '')
      return g.join(s, d)
    }
    return b
  }
  function d(a, b) {
    let c = t()
    return h.writeFileSync(g.join(s, `${c}${b}`), a), `${q}${c}${b}`
  }
  const e = require('crypto'),
    f = require('os'),
    g = require('path'),
    h = require('fs'),
    i = require('./92320c1386e6db6a6f2556736a9bc280.js'),
    j = require('./89ba85d67a88f7636d657c22b5d3e038.js'),
    k = require('./3bfffbe88b3d923921f851c0697974fe.js'),
    l = require('mkdir-p'),
    m = require('./37be435102276ea9cf47609ff6535cd4.js'),
    n = 'tmp',
    o = 'store',
    p = `${m.LOCAL_PROTOCOL}${m.USER_DIR}`,
    q = 'wxfile://',
    r = 'http://',
    s = i.WeappFileCache
  var t = function() {
    let a,
      b = j.getUserInfo(),
      c = k.getCurrent(),
      d = c.hash,
      f = b ? b.openid : 'unknow'
    do {
      var i = e.createHash('md5')
      i.update(`${d}${f}${Date.now()}${Math.random()}`),
        (a = `${n}_${d}${f}${i.digest('hex')}`)
    } while (h.existsSync(g.join(s, a)))
    return a
  }
  module.exports = {
    isLocalId: a,
    isUserFile: b,
    getRealPath: c,
    copyFileDataToTemp: d,
    copyFileToTemp: function(a, b) {
      if (h.existsSync(a)) {
        let c = h.readFileSync(a)
        return d(c, b)
      }
      return !1
    },
    saveFileForever: function(a) {
      try {
        let b = c(a)
        if (h.existsSync(b)) {
          let d = k.getCurrentConfig().gameApp,
            e = d ? r : q,
            f = a.replace(`${e}${n}`, `${e}${o}`)
          return h.renameSync(b, c(f)), f
        }
      } catch (a) {}
      return !1
    },
    getSavedFileList: function() {
      let a = j.getUserInfo(),
        b = k.getCurrent(),
        c = b.hash,
        d = a ? a.openid : 'unknow',
        e = h.readdirSync(s).filter(a => {
          return !!~a.indexOf(`${o}_${c}${d}`)
        }),
        f = 0,
        i = e.reduce((a, b) => {
          let c = h.statSync(g.join(s, b))
          return (
            (f = c.size + f),
            a.push({
              filePath: `${q}${b}`,
              size: c.size,
              createTime: parseInt(c.ctime.getTime() / 1e3)
            }),
            a
          )
        }, [])
      return { fileList: i, totalSize: f }
    },
    getFileStat: function(a) {
      let b,
        d = c(a)
      return h.existsSync(d) && (b = h.statSync(d)), b
    },
    removeSavedFile: function(a) {
      let b = c(a),
        d = !1
      return h.existsSync(b) && (h.unlinkSync(b), (d = !0)), d
    },
    saveBase64DataToFile: function(a, b) {
      let c = new Buffer(a, 'base64').toString('binary'),
        d = t()
      return h.writeFileSync(g.join(s, `${d}${b}`), c, 'binary'), `${q}${d}${b}`
    },
    cleanProjectFile: function(a) {
      let b = k.getCurrent(),
        c = h.readdirSync(s).filter(c => {
          return !!~c.indexOf(a ? `_${b.hash}` : `${n}_${b.hash}`)
        })
      c.map(a => {
        h.unlinkSync(g.join(s, a))
      })
    },
    cleanTempFile: function() {
      try {
        h.readdirSync(s).map(a => {
          0 <= a.indexOf(n) && h.unlinkSync(g.join(s, item))
        })
      } catch (a) {}
    },
    createNewLocalId: function() {
      let a = k.getCurrentConfig().gameApp,
        b = a ? r : q
      return `${b}${t()}`
    },
    getWriteFilePath: function(a) {
      a = a || k.getCurrent()
      let b = a.appid,
        c = g.join(i.WeappFileCache, b)
      return l.sync(c), c
    },
    getUsrFileRealPath: function(a, c) {
      if (b(a)) {
        c = c || k.getCurrent()
        let b = g.join(s, c.appid)
        return l.sync(b), g.join(b, a.replace(p, ''))
      }
      return ''
    }
  }
})(require('lazyload'), require)
