'use strict'
!(function(require, directRequire) {
  function a() {
    let a = g.getCurrent(),
      b = 'tourist' == a.appid,
      c = a.setting.urlCheck,
      d = !0
    return b ? (d = !1) : !c && (d = !1), d
  }
  const b = require('fs'),
    c = require('path'),
    d = require('./bc78839ccca8df9e5ceeb7fae11b7be2.js'),
    e = require('./0634ee2ebd3e560d9d4804ecc960160f.js'),
    f = require('./233d77ecf0781f44985f684f70e316d0.js'),
    g = require('./3bfffbe88b3d923921f851c0697974fe.js'),
    h = require('./f6cbcecf6ed9f533f6a506310d8f07b6.js'),
    i = require('./a1dd553cc059d528bb0ef56afed53968.js')
  module.exports = {
    downloadImage: async function(e, i) {
      let j = i.args
      const k = d.getState(),
        l = g.getCurrentConfig(),
        m = 1024 * (1024 * l.setting.DownloadFileSizeLimit)
      return new Promise(d => {
        let e = !1
        const k = a => {
          e || (d(a), (e = !0))
        }
        try {
          let d = 0,
            e = 200,
            l = c.extname(j.url.split('?')[0]),
            n = g.getCurrent(),
            o = h.createTmpfileName(n, l),
            p = h.getFileRealPath(o),
            q = p.fileRealPath,
            r = [],
            s = j.header || {}
          s.Referer = `https://servicewechat.com/${g.getProjectAppID()}/devtools/page-frame.html`
          let t = {
            method: 'get',
            url: j.url,
            encoding: null,
            headers: s,
            followRedirect: function(a) {
              let b = !1
              ;(n.urlCheck || n.isTourist) && (b = !0)
              let c = a.statusCode
              if (300 <= c && 400 > c && (302 == c || 301 == c)) {
                let c = a.caseless.get('location'),
                  d = g.getCurrentDomains().download
                for (let a = 0; a < d.length; a++)
                  if (c && 0 === c.indexOf(d[a])) {
                    b = !0
                    break
                  }
              }
              return b
            }
          }
          a()
            ? (t.agentOptions = { secureProtocol: 'TLSv1_2_method' })
            : (t.tunnel = !1)
          let u = f(t)
          u.on('response', a => {
            if (((e = a.statusCode), 200 != e && 206 != e))
              k({ errMsg: `${i.api}:ok`, statusCode: e })
            else {
              let b = parseInt(a.headers['content-length'])
              if (
                (b > m &&
                  (u.abort(),
                  k({ errMsg: `${i.api}:fail exceed max file size` })),
                a.headers['content-type'])
              ) {
                let b = a.headers['content-type'].split('/')
                b && b[1] && (l = `.${b[1]}`)
              }
            }
          })
            .on('error', function(a) {
              a && 'EPROTO' === a.code
                ? k({
                    errMsg: `${
                      i.api
                    }:fail 小程序要求的 TLS 版本必须大于等于 1.2`
                  })
                : a && 'ETIMEDOUT' === a.code
                  ? k({ errMsg: `${i.api}:fail timeout` })
                  : k({ errMsg: `${i.api}:fail ${a}` })
            })
            .on('data', a => {
              ;(d += a.length),
                r.push(i),
                d > m &&
                  (u.abort(),
                  k({ errMsg: `${i.api}:fail exceed max file size` }))
            })
            .on('end', () => {
              let a = Buffer.concat(r),
                c = h.initTmpfileName(n, a, l),
                d = h.getFileRealPath(c),
                f = d.fileRealPath
              b.writeFileSync(f, a),
                b.unlink(q, () => {}),
                k({ errMsg: `${i.api}:ok`, tempFilePath: c, statusCode: e })
            })
            .pipe(b.createWriteStream(q))
        } catch (a) {
          k({ errMsg: `${i.api}:fail ${a}` })
        }
      })
    }
  }
})(require('lazyload'), require)
