'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('react'),
    { connect: b } = require('react-redux'),
    c = require('./bc78839ccca8df9e5ceeb7fae11b7be2.js'),
    d = require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),
    e = require('./9c906d27ca74e18d0deaaa231e71fdfa.js'),
    f = require('./e98c60a262d8d98e69e574a9d12a21df.js'),
    g = require('./cc2c2970ff81ae4a83123e81ee123da2.js'),
    h = require('./a8c87029da0fa06e986298d447ab0fe2.js'),
    i = require('./021e193565a294d7eb2d08daddd97cc4.js'),
    j = require('./11cb73adc87121c47550697aa2c8d2e4.js'),
    k = require('./0deb78a56cbcc9f9f3b0823d28892e90.js'),
    l = require('./299addce80fc9f50b337441dcd08bb70.js'),
    m = require('./bdbf06bc9f97badd3ab5ba497b62543a.js'),
    n = require('./c6e781935457a48a3f7675888ab50cfe.js'),
    o = require('./1d16b0395fef24439fc7941dbbfdf1fd.js'),
    p = require('./2c0e34a9e23574aea227f3448a526c52.js'),
    q = require('./dadaae924a58746545d3b2199ba0c470.js'),
    r = require('./eadce02c750c875a10680bcfedadec88.js'),
    s = require('./6620a0cf7dad1b400d60f0fdae40f524.js'),
    t = require('./eb1fe4da47d7ed10884f8b039b058c5b.js'),
    u = require('./7951ae92c68d4898c7ba427e64125f9e.js'),
    v = require('./d3ce001ab1e75959382f6a7e0156dd17.js'),
    w = require('./37d8b9297fb1bd87f9a3ac407b8006a0.js'),
    x = require('./84858de8a097c9cf84ff2c2e3d86e2a9.js'),
    y = require('./tools.js'),
    z = require('./437e6043fc662374e4f1c2330516ac40.js'),
    A = require('./c41853777103289d939a4b1503784d4f.js'),
    B = require('./dc59f57d54946e61d27c95ab24d8cb4f.js'),
    C = require('./b543ae2da406cea63b3ad8951f17b6c0.js'),
    D = require('./d7f7f9395e2df8d4130ef8626928c295.js'),
    E = require('./logger.js'),
    F = require('./84858de8a097c9cf84ff2c2e3d86e2a9.js'),
    G = require('./d28a711224425b00101635efe1034c99.js'),
    H = require('./2a36cc34e5f44e62f9188b9fc0871d70.js'),
    I = require('./3b4ae76898f5e38e0fa745be867d8de3.js'),
    J = 'darwin' === process.platform
  nw.App.on('reopen', () => {
    return global.isDevWindow
      ? void global.Win.show()
      : void (0 === C.clientWindows.size && global.Win.show(),
        global.CLI.startedByCLI &&
          (global.Win.showFunction &&
            ((global.Win.show = global.Win.showFunction),
            (global.CLI.showFunction = null)),
          0 === C.clientWindows.size && global.Win.show()))
  }),
    nw.App.on('open', a => {
      a
        ? J
          ? a.startsWith('wechatide')
            ? (E.info('open arg: ', a), D.handleOpenArgs(a))
            : global.Win.show()
          : a.includes('wechatide')
            ? (E.info('open arg: ', a), D.handleOpenArgs(a))
            : global.Win.show()
        : global.Win.show()
    })
  class K extends a.Component {
    constructor(a) {
      super(a),
        (this.state = {}),
        (this.updateProxyTimer = null),
        (this._onConfigUpdate = this.onConfigUpdate.bind(this))
    }
    componentDidMount() {
      this.checkWindowStatus(this.props),
        this.updateProxy(this.props.proxyType),
        B.on('CONFIG_UPDATE', this._onConfigUpdate)
      let a = !1
      try {
        if (nw.App.argv && 0 < nw.App.argv.length) {
          a = !0
          const b = nw.App.argv.join(' ')
          E.info('init open arg: ', b), D.handleOpenArgs(b)
        }
      } catch (a) {}
      if (!global.isDevWindow && !a) {
        let a = F.lastSelect
        a &&
          (F.projectList[a]
            ? this.props.openProject(a)
            : a === d.MAIN_WINDOW_TYPE.WEB_DEBUGGER && t.openWebDebugger())
      }
      try {
        let a = document.getElementById('fakeBackground')
        a && (a.style.opacity = 0),
          setTimeout(() => {
            ;(a = document.getElementById('fakeBackground')), a && a.remove()
          }, 2e3)
      } catch (a) {}
      H.startSync(), I.startSync()
    }
    componentWillUnmount() {
      B.off('CONFIG_UPDATE', this._onConfigUpdate), H.stopSync(), I.stopSync()
    }
    componentDidUpdate() {
      this.checkWindowStatus(this.props),
        this.props.mainWindow === d.MAIN_WINDOW_TYPE.LOGIN &&
          this.props.hasLogin &&
          this.props.setMainWindow(d.MAIN_WINDOW_TYPE.ENTRANCE)
    }
    onConfigUpdate(a) {
      G.updateVendorConfig(a)
    }
    componentWillReceiveProps(a) {
      this.props.mainWindow !== a.mainWindow &&
        (this.checkWindowStatus(a),
        !a.hasLogin &&
          (this.props.mainWindow !== d.MAIN_WINDOW_TYPE.LOGIN ||
            this.props.mainWindow !== d.MAIN_WINDOW_TYPE.DEV ||
            this.props.mainWindow !== d.MAIN_WINDOW_TYPE.WEB_DEBUGGER) &&
          this.props.setMainWindow(d.MAIN_WINDOW_TYPE.LOGIN)),
        a.proxyType != this.props.proxyType && this.updateProxy(a.proxyType)
    }
    checkWindowStatus(a) {
      const b = d.MAIN_WINDOW_TYPE
      switch (a.mainWindow) {
        case b.LOGIN:
        case b.ENTRANCE:
        case b.SELECT_PROJECT:
        case b.CREATE_PROJECT:
        case b.CREATE_MINICODE:
        case b.IMPORT_MINICODE: {
          0 === C.clientWindows.size &&
            'visible' !== global.Win.window.document.visibilityState &&
            global.Win.show()
          break
        }
        case b.DEV:
          break
        case b.WEB_DEBUGGER:
      }
    }
    updateProxy(a) {
      z.updateProxy(a)
        .then(() => {
          B.loop()
        })
        .catch(b => {
          E.error(
            `proxy updateProxy ${a} catch error ${b} try to set DIRECT instead`
          ),
            v({
              priority: 1,
              title: '\u8BBE\u7F6E\u4EE3\u7406\u5931\u8D25',
              message:
                '\u4EE3\u7406\u8BBE\u7F6E\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u91CD\u8BD5'
            }),
            this.props.setProxySetting({ proxyType: 'DIRECT' })
        })
    }
    onSettingsWindowClose() {
      this.props.closeIDESettings()
    }
    onWebDebuggerWindowClose() {
      this.props.setMainWindow(d.MAIN_WINDOW_TYPE.ENTRANCE)
    }
    onDevWindowClose() {
      J
        ? (this.props.closeProject(), global.Win.hide())
        : process.nextTick(() => {
            y.quit()
          })
    }
    getOptionsComponent() {
      const b = this.props,
        c = []
      if (b.confirmPopups)
        for (const d of b.confirmPopups)
          c.push(a.createElement(u, _extends({}, d, { key: d.id })))
      return c
    }
    render() {
      if (!this.props.rehydrated) return null
      if (!global.isDevWindow) {
        let b,
          c = d.MAIN_WINDOW_TYPE,
          { mainWindow: e } = this.props
        return (
          (b =
            e === c.LOGIN
              ? a.createElement(i, null)
              : e === c.ENTRANCE
                ? a.createElement(k, null)
                : e === c.SELECT_PROJECT
                  ? a.createElement(l, null)
                  : e === c.CREATE_PROJECT
                    ? a.createElement(m, null)
                    : e === c.CREATE_MINICODE || e === c.IMPORT_MINICODE
                      ? a.createElement(n, null)
                      : a.createElement(k, null)),
          a.createElement(
            'div',
            null,
            b,
            this.getOptionsComponent(),
            this.props.showSettings
              ? a.createElement(j, {
                  onSettingsWindowClose: this.onSettingsWindowClose.bind(this)
                })
              : null,
            this.props.showProjectManagement ? a.createElement(o, null) : null
          )
        )
      }
      let b
      return (
        (b =
          'miniprogram' === global.devType
            ? a.createElement(p, null)
            : a.createElement(q, null)),
        a.createElement(
          'div',
          null,
          b,
          this.getOptionsComponent(),
          this.props.showSettings
            ? a.createElement(j, {
                onSettingsWindowClose: this.onSettingsWindowClose.bind(this)
              })
            : null,
          this.props.showProjectManagement ? a.createElement(o, null) : null
        )
      )
    }
  }
  module.exports = b(
    a => {
      let b = !1
      if (a.user) {
        let c = a.user
        if (c) {
          let a = +new Date()
          a <= c.signatureExpiredTime && (b = !0)
        }
      }
      let c = a.window.mainWindow || d.MAIN_WINDOW_TYPE.LOGIN
      return {
        hasLogin: b,
        rehydrated: a.rehydrated,
        currentProject: a.project.current,
        showSettings: a.settings.show,
        showProjectManagement: a.window.projectManagement.show,
        moveToCenter: a.window.moveToCenter,
        proxyType: a.settings.proxy.proxyType || 'SYSTEM',
        confirmPopups: a.info.confirmPopups,
        mainWindow: c
      }
    },
    a => {
      return {
        removeUserInfo: b => {
          a(e.removeUserInfo(b))
        },
        closeIDESettings() {
          a(f.closeIDESettings())
        },
        setProxySetting(b) {
          a(f.setProxySetting(b))
        },
        closeProject() {
          a(g.closeProject())
        },
        setMainWindow(b) {
          a(h.setMainWindow(b))
        },
        openProject(b) {
          a(g.openProject(b)).catch(() => {
            a(h.setMainWindow(d.MAIN_WINDOW_TYPE.ENTRANCE))
          })
        }
      }
    }
  )(K)
})(require('lazyload'), require)
