'use strict'
var _extends =
  Object.assign ||
  function(a) {
    for (var b, c = 1; c < arguments.length; c++)
      for (var d in ((b = arguments[c]), b))
        Object.prototype.hasOwnProperty.call(b, d) && (a[d] = b[d])
    return a
  }
!(function(require, directRequire) {
  const a = require('fs'),
    b = require('path'),
    c = require('adm-zip'),
    d = require('jszip'),
    e = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    f = require('./92320c1386e6db6a6f2556736a9bc280.js'),
    g = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    h = require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),
    i = h.CONTENTTYPEINFO,
    j = {
      '.png': { type: 'img', name: '\u56FE\u7247' },
      '.jpg': { type: 'img', name: '\u56FE\u7247' },
      '.jpeg': { type: 'img', name: '\u56FE\u7247' },
      '.gif': { type: 'img', name: '\u56FE\u7247' },
      '.bmp': { type: 'img', name: '\u56FE\u7247' },
      '.ogg': { type: 'video', name: '\u591A\u5A92\u4F53' },
      '.mp3': { type: 'audio', name: '\u97F3\u9891' },
      '.mp4': { type: 'audio', name: '\u89C6\u9891' },
      '.m4a': { type: 'audio', name: '\u89C6\u9891' },
      '.wav': { type: 'audio', name: '\u97F3\u9891' },
      '.aac': { type: 'audio', name: '\u97F3\u9891' },
      '.silk': { type: 'audio', name: '\u97F3\u9891' }
    },
    k = 2097152,
    l = 8388608,
    m = (a, b) => {
      if (0 == a.returnCode) return a.data
      if (1003 == a.returnCode) {
        var c = new Error(
          '\u672A\u5F00\u901A\u817E\u8BAF\u4E91 CDN \u670D\u52A1\uFF0C\u8BF7\u524D\u5F80\u5F00\u901A'
        )
        throw ((c.code = 1003), c)
      } else throw new Error(`${b} ${a.returnCode}:${a.returnMessage}`)
    },
    n = async () => {
      let { body: a } = await e({
        url: g.getCosTicket,
        needToken: 1,
        needAppID: 1
      })
      return { CosTicket: a.cos_ticket, username: a.username }
    },
    o = async a => {
      const b = global.windowMap.get(h.WINDOW_REGISTRY.MATERIAL).window
      let c = document.createElement('input')
      return (
        (c.style.display = 'none'),
        c.setAttribute('type', 'file'),
        a.accept && c.setAttribute('accept', a.accept),
        a.multiple && c.setAttribute('multiple', 'multiple'),
        b.document.body.appendChild(c),
        c.click(),
        new Promise(a => {
          c.addEventListener('change', d => {
            b.document.body.removeChild(c), a({ success: !0, event: d })
          }),
            c.addEventListener('cancel', d => {
              b.document.body.removeChild(c), a({ success: !1, event: d })
            })
        })
      )
    }
  module.exports = {
    whiteFileExtName: j,
    getUploadFiles: async (c = () => {}) => {
      let e = await o({
        accept: 'video/*,audio/*,image/*,application/zip',
        multiple: !0
      })
      if (e.success) {
        let g = new d(),
          h = e.event,
          i = h.target.value.split(';')
        if (1 == i.length && /\.zip$/.test(i[0])) {
          var f = await d.loadAsync(a.readFileSync(i[0]))
          for (let a in f.files) {
            let d = f.files[a],
              e = b.extname(a) || ''
            if (((e = e.toLowerCase()), j[e])) {
              let b = await d.async('nodebuffer'),
                e = b.length
              e > k
                ? c('ignore', {
                    file: a,
                    reason: `文件大小 ${e}B 大于最大限制 ${k / 1048576}MB`
                  })
                : (g.file(a, b, { binary: !0 }),
                  c('zip', { file: a, length: e }))
            } else
              d.dir
                ? g.folder(a)
                : c('ignore', {
                    file: a,
                    reason: '\u975E\u767D\u540D\u5355\u6587\u4EF6'
                  })
          }
          let e = await g.generateAsync({
            type: 'nodebuffer',
            addUnicodeExtraField: !1
          })
          if (e.length > l)
            throw new Error(`数据包 ${e.length}B 大于最大限制 ${l / 1048576}MB`)
          return e
        }
        i.forEach(d => {
          let e = b.extname(d),
            f = b.basename(d)
          if (j[e]) {
            let b = a.readFileSync(d),
              e = b.length
            e > k
              ? c('ignore', {
                  file: f,
                  reason: `文件大小 ${e}B 大于最大限制 ${k / 1048576}MB`
                })
              : (g.file(f, b, { binary: !0 }), c('zip', { file: f, length: e }))
          } else
            c('ignore', {
              file: f,
              reason: '\u975E\u767D\u540D\u5355\u6587\u4EF6'
            })
        })
        let m = await g.generateAsync({
          type: 'nodebuffer',
          addUnicodeExtraField: !1
        })
        if (m.length > l)
          throw new Error(`数据包 ${m.length}B 大于最大限制 ${l / 1048576}MB`)
        return m
      }
    },
    getCosTicket: n,
    getTaskId: async () => {
      let a = await n(),
        { body: b } = await e({
          url: g.cosAction,
          method: 'post',
          body: JSON.stringify(_extends({ action: 'GetTaskId' }, a)),
          needAppID: -1,
          needCheckErrCode: -1
        })
      return m(b, 'GetTaskId')
    },
    getTaskResult: async a => {
      let b = await n(),
        { body: c } = await e({
          url: g.cosAction,
          method: 'post',
          body: JSON.stringify(
            _extends({ action: 'TaskResult', taskId: a }, b)
          ),
          needAppID: -1,
          needCheckErrCode: -1
        })
      return m(c, 'TaskResult')
    },
    getResources: async a => {
      let c = await n(),
        { body: d } = await e({
          url: g.cosAction,
          method: 'post',
          body: JSON.stringify(
            _extends(
              {
                action: 'GetResources',
                directory: a || '/',
                page_no: 0,
                page_size: 1e3
              },
              c
            )
          ),
          needAppID: -1,
          needCheckErrCode: -1
        }),
        f = m(d, 'GetResources'),
        h = f.file_list.map(c => {
          const d = /\/$/.test(c.relative_path)
          let e = {}
          if (!d) {
            let a = b.extname(c.relative_path)
            ;(a = (a || '').toLowerCase()), (e = j[a] || {})
          }
          return {
            isDir: d,
            fileInfo: e,
            isOpen: !1,
            path: a + c.relative_path,
            name: b.basename(c.relative_path),
            url: c.url,
            md5: c.md5,
            size: c.size,
            lastModified: c.last_modified,
            children: []
          }
        })
      return h
    },
    upload: async (c, d) => {
      let h = await n(),
        i = b.join(f.Weappdest, `${Date.now()}${Math.random()}.zip`)
      a.writeFileSync(i, c)
      let { body: j } = await e({
        url: g.cosAction,
        method: 'post',
        formData: _extends(
          {
            action: 'UploadFiles',
            directory: d || '/',
            file: a.createReadStream(i)
          },
          h
        ),
        needAppID: -1,
        needCheckErrCode: -1
      })
      return m(j, 'UploadFiles')
    },
    RemoveResources: async a => {
      let b = await n(),
        { body: c } = await e({
          url: g.cosAction,
          method: 'post',
          body: JSON.stringify(
            _extends({ action: 'RemoveResource', relative_path_list: a }, b)
          ),
          needAppID: -1,
          needCheckErrCode: -1
        })
      return m(c, 'RemoveResources')
    },
    getResourceDetail: async (a, b) => {
      let { resp: c } = await e({
        url: b,
        headers: { Range: 'bytes=0-10' },
        needToken: -1,
        needAppID: -1,
        needParse: -1
      })
      const d = c.headers['content-type'].replace(/;.*/g, ''),
        f = i[d.toLowerCase()],
        g = j[`.${f}`]
      return {
        currentPath: a,
        ext: (f || '').toUpperCase(),
        type: g ? g.type : 'unknown',
        name: g ? g.name : '\u6587\u4EF6'
      }
    }
  }
})(require('lazyload'), require)
