'use strict'
!(function(require, directRequire) {
  function a(a) {
    let b = a.split('.'),
      c = parseInt(b[0]),
      d = parseInt(b[1]),
      e = parseInt(b[2])
    if (c > I) return 'app'
    return c == I && d > J
      ? 'big_code'
      : c == I && d == J && e > K && 'small_code'
  }
  function b({ except: a = [] } = {}) {
    return new Promise(b => {
      q('*', { cwd: y }, (c, d) => {
        d.forEach(b => {
          let c = o.join(y, b)
          try {
            let b = !1
            for (let d = 0, e = a.length; d < e; d++)
              if (c.endsWith(a[d])) {
                b = !0
                break
              }
            b || l.unlinkSync(c)
          } catch (a) {}
        }),
          b()
      })
    })
  }
  async function c(a, b) {
    M ||
      ((M = !0),
      (N = !0),
      v({
        title: `有新版本`,
        message: A
          ? `新版本 ${a} 已经准备好，点击更多进行安装`
          : `新版本 ${a} 已经准备好，立刻重启安装？`,
        buttons: [{ title: '\u5B89\u88C5\u65B0\u7248\u672C' }],
        callback: a => {
          if (0 === a) {
            let a = A ? 'open' : b,
              c = A ? [b] : []
            try {
              r(a, c, { detached: !0, cwd: y })
            } catch (a) {
              nw.Shell.openItem(o.join(y, b))
            }
            F.quit()
          }
        }
      }),
      await B.open(),
      await B.insert('noticecenter', [
        {
          type: C.DBType.tools,
          title: '\u7248\u672C\u66F4\u65B0',
          content: `最新的开发工具版本 ${a}`,
          link: b,
          timestamp: Date.now() / 1e3
        }
      ]),
      await B.close())
  }
  function d(a) {
    M ||
      ((M = !0),
      v({
        title: `有新版本`,
        message: `新版本 ${a} 已经准备好，立刻重启更新？`,
        buttons: [{ title: '\u91CD\u542F' }],
        callback: a => {
          0 === a && chrome.runtime.reload()
        }
      }))
  }
  function e(a, d) {
    if (L) return
    let e = A ? 'darwin' : 'x64' === process.arch ? 'x64' : 'ia32',
      f = { url: d ? d : `${E}&type=${e}` },
      g = `${e}_${a}.${A ? 'ndmg' : 'nexe'}`,
      h = o.join(y, g),
      i = `${e}_${a}.${A ? 'dmg' : 'exe'}`,
      j = o.join(y, i)
    if (l.existsSync(j)) c(a, i)
    else {
      ;(async () => {
        try {
          let c = a && a.replace ? a.replace(/\./g, '') : ''
          await b({ except: [`${e}_${c}.${A ? 'dmg' : 'exe'}`] })
        } catch (a) {}
        s(f, b => {
          b ||
            l.rename(h, j, b => {
              b || c(a, i)
            }),
            (L = !1)
        }).pipe(l.createWriteStream(h, { mode: 511 })),
          (L = !0)
      })()
    }
  }
  function f() {
    return new Promise((a, b) => {
      let c = { url: `${E}&type=small_code`, needToken: 0, encoding: null }
      s(c, (c, d, e) => {
        c ? b(c) : a(e)
      })
    })
  }
  function g(a) {
    return new Promise((b, c) => {
      m.gunzip(a, (a, d) => {
        a ? c(a) : b(d)
      })
    })
  }
  function h(a, b) {
    let c = o.join(y, b.toString())
    p.sync(c)
    let d = A ? o.join(c, 'app.nw') : o.join(c, 'package.nw')
    n.extractAll(a, d)
    let e = o.join(d, 'package.json'),
      f = JSON.parse(l.readFileSync(e, 'utf8'))
    ;(f['chromium-args'] = A
      ? '--disable-devtools --enable-experimental-web-platform-features -load-extension=/Applications/wechatwebdevtools2.app/Contents/Resources/app.nw/js/extensions/devtools/ -ignore-certificate-errors'
      : '--disable-devtools --enable-experimental-web-platform-features -load-extension=./package.nw/js/extensions/devtools/ -ignore-certificate-errors'),
      l.writeFileSync(e, JSON.stringify(f), 'utf8')
  }
  async function i(a, b) {
    return void e(a)
  }
  function j(a = '0.0.0.0') {
    if (x.getAPIVersion() !== a) x.update()
    else
      try {
        ;(l.existsSync(O) &&
          l.existsSync(Q) &&
          l.existsSync(P) &&
          l.existsSync(R)) ||
          x.update()
      } catch (a) {
        w.error(`update latest api failed with error ${a}`)
      }
  }
  function k(b) {
    return a(b.last_ide)
  }
  const l = require('fs'),
    m = require('zlib'),
    n = require('asar'),
    o = require('path'),
    p = require('mkdir-p'),
    q = require('glob'),
    r = require('child_process').spawn,
    s = require('./233d77ecf0781f44985f684f70e316d0.js'),
    t = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    u = require('./92320c1386e6db6a6f2556736a9bc280.js'),
    v = require('./d3ce001ab1e75959382f6a7e0156dd17.js'),
    w = require('./logger.js'),
    x = require('./4e88c741ca9fb65fab96c500b6d7f2fb.js'),
    y = u.WeappApplication,
    z = o.join(y, 'uplog.json'),
    A = 'darwin' === process.platform,
    B = require('./4102b43ad51182743111f5d5e1fc5b46.js'),
    C = require('./bcb48ae14d243711d3b31cb0f602d209.js'),
    D = require('./84858de8a097c9cf84ff2c2e3d86e2a9.js'),
    E = `${t.downloadRedirectURL}?os=${A ? 'darwin' : 'win'}`,
    F = require('./tools.js'),
    G = global.appVersion,
    H = G.split('.'),
    I = parseInt(H[0]),
    J = parseInt(H[1]),
    K = parseInt(H[2])
  var L = !1,
    M = !1
  var N = !1
  const O = x.JSAPIFilePath,
    P = x.JSAPIMonacoFilePath,
    Q = x.ComponentAPIFilePath,
    R = x.LibWaEs6FilePath
  module.exports = {
    onConfigUpdate: function(a) {
      if ((j(a.last_api), !global.appConfig.isBeta)) {
        let b = k(a)
        b && i(a.last_ide, b)
      }
    },
    cleanApplicationCache: b,
    checkNeedUpdate: k
  }
})(require('lazyload'), require)
