'use strict'
!(function(require, directRequire) {
  const a = require('adbkit'),
    b = a.createClient(),
    c = require('stream'),
    d = require('url'),
    e = require('querystring'),
    f = require('./b385551f3631ca17292e974c0c8b3b1a.js'),
    g = require('./92320c1386e6db6a6f2556736a9bc280.js'),
    h = require('fs'),
    i = require('path'),
    j = g.WeappTraceFiles,
    k = '/tencent/MicroMsg/appbrand/trace/'
  let l, m, n
  const o = a => {
      b.listDevices()
        .then(b => {
          a(null, b)
        })
        .catch(b => {
          a(b)
        })
    },
    p = c => {
      return new Promise(function(d, e) {
        b.shell(c, 'echo $EXTERNAL_STORAGE')
          .then(a.util.readAll)
          .then(a => {
            const b = a.toString('utf-8').replace(/\r?\n|\r/g, '')
            if (0 < b.trim().length) (n = b), d(n)
            else {
              throw new Error('trace.js could not found SDCard path.')
            }
          })
          .catch(a => {
            e(a)
          })
      })
    },
    q = a => {
      let b
      return (
        a.split(/\r?\n/).forEach(a => {
          0 <= a.toLowerCase().indexOf('memtotal') &&
            (b = parseInt(a.split(':')[1]))
        }),
        b
      )
    },
    r = c => {
      return new Promise(function(d, e) {
        b.shell(c, 'cat /proc/meminfo')
          .then(a.util.readAll)
          .then(a => {
            const b = a.toString('utf-8')
            if (((m = q(b)), null == m)) {
              throw new Error('trace.js could not found totalmem.')
            }
            d()
          })
          .catch(a => {
            e(a)
          })
      })
    },
    s = () => {
      const a = i.join(j, l)
      try {
        h.mkdirSync(a)
      } catch (a) {
        if ('EEXIST' !== a.code) throw a
      }
    },
    t = (a, b) => {
      p(a)
        .then(() => {
          return r(a)
        })
        .then(() => {
          ;(l = a), s(), b(null, { deviceId: l, SDCardPath: n, totalMem: m })
        })
        .catch(a => {
          b(a)
        })
    },
    u = (a, b) => {
      a = `${a}_${Date.now()}`
      const c = JSON.stringify(b.result()),
        d = i.join(j, l, a)
      h.writeFileSync(d, c, 'utf8')
    },
    v = c => {
      let d = c.length
      return new Promise(function(e, g) {
        return 0 === c.length
          ? void e()
          : void c.forEach(c => {
              const h = n + k + c.name
              b.pull(l, h)
                .then(a.util.readAll)
                .then(a => {
                  const d = a.toString('utf-8'),
                    e = new f()
                  return (
                    m && (e.totalMem = m / 1024),
                    e.parse(d),
                    u(c.name, e),
                    b.shell(l, `rm ${h}`)
                  )
                })
                .then(() => {
                  --d, 0 === d && e()
                })
                .catch(a => {
                  g(a)
                })
            })
      })
    },
    w = a => {
      b.readdir(l, n + k)
        .then(a => {
          return v(a)
        })
        .then(() => {
          const b = i.join(j, l),
            c = h.readdirSync(b)
          a(
            null,
            c.filter(a => {
              return 4 === a.split('_').length
            })
          )
        })
        .catch(b => {
          a(b)
        })
    },
    x = (a, b) => {
      const c = i.join(j, l, a)
      try {
        const a = h.readFileSync(c, 'utf8')
        b(null, JSON.parse(a))
      } catch (a) {
        b({ message: a.message })
      }
    },
    y = (a, b) => {
      const c = i.join(j, l, a)
      try {
        h.unlinkSync(c)
        b(null, { message: 'delete ok' })
      } catch (a) {
        b({ message: a.message })
      }
    }
  module.exports = {
    getTraceRoute: (a, b) => {
      const c = e.parse(a.query)
      if (0 < a.pathname.indexOf('listDevices')) o(b)
      else if (0 < a.pathname.indexOf('chooseDevice')) {
        const a = c.deviceId
        if (null == a) return void b({ message: 'deviceId is null.' })
        t(a, b)
      } else if (0 < a.pathname.indexOf('listFiles')) {
        if (null == l) return void b({ message: 'must choose device first.' })
        w(b)
      } else if (0 < a.pathname.indexOf('getTraceFile')) {
        if (null == l) return void b({ message: 'must choose device first.' })
        const a = c.fileName
        if (null == a) return void b({ message: 'fileName is null.' })
        x(a, b)
      } else if (0 < a.pathname.indexOf('deleteTraceFile')) {
        if (null == l) return void b({ message: 'must choose device first.' })
        const a = c.fileName
        if (null == a) return void b({ message: 'fileName is null.' })
        y(a, b)
      }
    }
  }
})(require('lazyload'), require)
