'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) {
  function a() {
    let a = j.getState()
    return a.project.current
  }
  function b() {
    let b = a(),
      c = (b && b.attr) || {}
    return (
      c.setting || (c.setting = _extends({}, l.setting)),
      c.network ||
        (c.network = {
          RequestDomain: [],
          DownloadDomain: [],
          WsRequestDomain: [],
          UploadDomain: []
        }),
      c
    )
  }
  function c() {
    let a = j.getState()
    return a.project.list
  }
  function d() {
    a()
    return `http://127.0.0.1:${global.proxyPort}/__pageframe__/`
  }
  function e(b = null) {
    let c
    return (
      (c = b ? b : a()),
      c
        ? c &&
          c.attr &&
          c.attr.platform &&
          c.attr.extInfo &&
          c.attr.extInfo.appid
          ? c.attr.extInfo.appid
          : c.appid
        : void 0
    )
  }
  const f = require('./84858de8a097c9cf84ff2c2e3d86e2a9.js'),
    g = require('./92320c1386e6db6a6f2556736a9bc280.js'),
    h = require('querystring'),
    i = require('path'),
    j = require('./bc78839ccca8df9e5ceeb7fae11b7be2.js'),
    k = require('url'),
    l = require('./6242f55dbdfe53c2f07b7a51568311f2.js'),
    m = require('./162bf2ee28b76d3b3d95b685cede4146.js'),
    n = require('./1dea83a77e99a7c94f6b6f01f5c175b0.js'),
    o = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    p = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    q = require('./d28a711224425b00101635efe1034c99.js'),
    r = require('./logger.js'),
    {
      whiteFileExtName: s,
      gameWhiteFileExtName: t
    } = require('./6242f55dbdfe53c2f07b7a51568311f2.js'),
    {
      default_weapp_header: u
    } = require('./5498e660c05c574f739a28bd5d202cfa.js')
  module.exports = {
    getCurrent: a,
    getCurrentConfig: b,
    getProjectByHash: function(a) {
      let b = j.getState()
      if (b.project.current.hash == a) return b.project.current
      const c = b.project.list
      for (const b in c) if (c[b].hash === a) return c[b]
    },
    getCurrentShareInfo: function() {
      let b = a(),
        c = b ? b.attr : { share_info: [] }
      return b.attr.share_info || []
    },
    getProjectList: c,
    formatAppIDQuery: function() {
      let c = b() || {},
        d = a(),
        e = { appid: d.appid, platform: c.platform ? 1 : 0, ext_appid: '' }
      return (
        c.platform && c.extInfo && (e.ext_appid = c.extInfo.appid || ''),
        h.stringify(e)
      )
    },
    getBaseURL: d,
    getProjectAppID: e,
    getShowName: function() {
      let b = a()
      return (b.attr && b.attr.appName) || b.projectname
    },
    getResourceUrl: function(a = '') {
      let b = k.parse(a)
      return 'http:' == b.protocol || 'https:' == b.protocol
        ? a
        : 'wxfile:' == b.protocol
          ? a.replace('wxfile://', 'http://wxfile.open.weixin.qq.com/')
          : d() + a
    },
    getCompileCondiction: function() {
      let b,
        c = a()
      try {
        let a = c.condiction[c.compileType] || {}
        b = a.list[a.current]
      } catch (a) {
        b = void 0
      }
      return b
    },
    getWeappCodeSize: async function(b) {
      b = b || a()
      let c,
        d = 0
      try {
        const a = await n(b)
        a.subPackages &&
          (c = a.subPackages
            .map(a => {
              let b = a.root.startsWith('/') ? '' : '/',
                c = a.root.endsWith('/') ? '' : '/'
              return b + a.root + c
            })
            .reduce((a, b) => {
              return (a[b] = 0), a
            }, {}))
      } catch (a) {}
      let e = await m(b),
        f = e.getAllFileInfo(),
        g = 0,
        h = b.attr.gameApp ? t : s
      for (let a in f) {
        let b = i.extname(a)
        if (h[b]) {
          if (c) {
            const b = a.startsWith('/') ? a : '/' + a
            for (const e in c)
              if (b.startsWith(e)) {
                ;(c[e] += f[a].size), (d += f[a].size)
                break
              }
          }
          g += f[a].size
        }
      }
      if ((c && (c.__APP__ = g - d), (g = parseInt(g / 1024) + 1), c))
        for (const a in c) c[a] = (c[a] / 1024).toFixed(1)
      return { total: g, subPackages: c }
    },
    isQcloudProject: function(a) {
      let b = a.attr || {}
      return (
        b.qcloud &&
        b.qcloud.qcloud_status &&
        1 === b.qcloud.qcloud_status.open_qc_account
      )
    },
    getCurrentDomains: function() {
      let c = a(),
        d = b(),
        e = !1,
        f = d.qcloud || {},
        g = {}
      if (
        f.qcloud_status &&
        1 === f.qcloud_status.open_develop_env &&
        f.qcloud_dev_info &&
        f.qcloud_dev_info.networking
      )
        for (let a in ((e = !0), (g = f.qcloud_dev_info.networking), g))
          g[a] = g[a].map(b => {
            return (
              (b = b.toLowerCase()),
              'ws_request_domain' == a
                ? /wss?\:\/\//.test(b)
                  ? b
                  : `wss://${b}`
                : /https?\:\/\//.test(b)
                  ? b
                  : `https://${b}`
            )
          })
      let h = d.network || {},
        i = h.RequestDomain || [],
        j = h.WsRequestDomain || [],
        k = h.DownloadDomain || [],
        l = h.UploadDomain || []
      return {
        request: e ? i.concat(g.request_domain) : i,
        socket: e ? j.concat(g.ws_request_domain) : j,
        download: e ? k.concat(g.download_domain) : k,
        upload: e ? l.concat(g.upload_domain) : l
      }
    },
    getLibVersionNumber: function(b) {
      let d
      if (!b) d = a()
      else {
        const a = c()
        d = a[b]
      }
      try {
        let a = d.libVersion.split('.').reduce(function(a, b, c, d) {
          return a + b * Math.pow(1e3, d.length - c - 1)
        }, 0)
        return isNaN(a) && (a = 999999999), a
      } catch (a) {
        return 999999999
      }
    },
    getLatestProjectAttr: b =>
      new Promise((c, d) => {
        let f = b || a()
        if (f && f.isTourist) return
        let g, h
        b
          ? ((h = e(b)), (g = `${p.getWeappAttrURL}?appid=${h}`))
          : ((h = e()), (g = p.getWeappAttrURL)),
          o({
            url: g,
            body: JSON.stringify({ appid_list: [h] }),
            method: 'post',
            needToken: 1,
            needAppID: b ? 0 : 1
          })
            .then(({ resp: a, body: b }) => {
              let d = b.attr_list[0],
                e = d.jsapi_list,
                f = {}
              if (e && 0 < e.length)
                for (let a, b = 0; b < e.length; b++)
                  (a = e[b]), (f[a.name] = a)
              let g = !!d.is_platform,
                h = {
                  gameApp: !!d.game_app,
                  permission: f,
                  appName: d.appname,
                  appImageUrl: d.appicon_url ? `${d.appicon_url}/0` : u,
                  setting: _extends({}, l.setting, d.Setting),
                  network: d.Network,
                  appid: d.appid,
                  share_info: d.share_info,
                  released: d.released,
                  minPublicLibVersion: d.min_pub_version,
                  searchWidget: !!d.open_search_widget,
                  userbanded: !0,
                  permissionBitset: d.jsapi_permission_list
                }
              if (
                (g &&
                  ((h.platform = g),
                  (h.platformNickname = d.platform_nickname)),
                d.qcloud_attr_json)
              ) {
                let a = JSON.parse(d.qcloud_attr_json)
                h.qcloud = a
              }
              let i = {}
              if (b.GlobalConfigJson) {
                try {
                  i = JSON.parse(b.GlobalConfigJson)
                } catch (a) {
                  r.error(
                    `parse GlobalConfigJson "${
                      b.GlobalConfigJson
                    }" catch error: ${a}`
                  )
                }
                h.setting = _extends({}, h.setting, i)
              }
              c(h)
            })
            .catch(a => {
              d(a)
            })
      }),
    projectConfigDefault: a => {
      const b = q.getVendorConfig()
      return {
        projectid: `${a.appid}_${a.projectName}`,
        condiction: {
          weapp: { current: -1, list: [] },
          search: { current: -1, list: [] },
          conversation: { current: -1, list: [] },
          game: { currentL: -1, list: [] }
        },
        setting: {
          urlCheck: !0,
          es6: !0,
          postcss: !0,
          minified: !0,
          newFeature: !0
        },
        compileType: 'weapp',
        libVersion: b.currentLibVersion,
        createTime: +new Date(),
        accessTime: +new Date(),
        packOptions: { ignore: [] }
      }
    },
    getLatestPluginInfo: (b, c = {}) =>
      new Promise(async (d, e) => {
        function f(a) {
          return {
            version: a.custom_version,
            innerVersion: a.inner_version,
            updateTime: a.update_time,
            desc: a.version_desc,
            pkgSize: a.pkg_size
          }
        }
        try {
          ;(b = b || a()), b && b.isTourist && d()
          const { withHistory: g } = c,
            h = await n(b)
          if (h.plugins) {
            let a = []
            for (const b in h.plugins)
              h.plugins[b] &&
                h.plugins[b].provider &&
                'dev' != h.plugins[b].version &&
                a.push({
                  plugin_id: h.plugins[b].provider,
                  custom_version: h.plugins[b].version
                })
            if (0 === a.length) return d()
            try {
              const { resp: b, body: c } = await o({
                url: p.batchGetPluginInfo,
                method: 'POST',
                needToken: 1,
                needAppID: 1,
                body: JSON.stringify({
                  need_history: g ? 1 : 0,
                  plugin_list: a
                })
              })
              if (c && c.plugin_list) {
                const a = c.plugin_list.map(a => {
                  let b, c, d
                  return (
                    a.req_version && (b = f(a.req_version)),
                    a.latest_version && (c = f(a.latest_version)),
                    a.version_list &&
                      a.version_list.length &&
                      (d = a.version_list.map(f)),
                    {
                      appid: a.plugin_id,
                      name: a.nickname,
                      brandIconURL: a.brandiconurl,
                      headImgURL: a.headimgurl,
                      onlineVersion: a.online_custom_version,
                      current: b,
                      latest: c,
                      history: d
                    }
                  )
                })
                d(a)
              } else if (!c) {
                const a = `get plugin info response error: empty response body. status: ${
                  b.statusCode
                }`
                r.error(a), e(a)
              } else {
                const a = `get plugin info response error: empty plugin list. status: ${
                  b.statusCode
                }`
                r.error(a), e(a)
              }
            } catch (a) {
              e(a)
            }
          } else d()
        } catch (a) {
          e(a)
        }
      })
  }
})(require('lazyload'), require)
