'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('./bc78839ccca8df9e5ceeb7fae11b7be2.js'),
    b = require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),
    c = require('./6242f55dbdfe53c2f07b7a51568311f2.js'),
    d = require('./cc2c2970ff81ae4a83123e81ee123da2.js'),
    e = require('./78294f0b5b50fc38258d7028553744ef.js'),
    f = require('./36244f5aa8a25574083d64e28953b466.js'),
    g = require('./5eb00762b104199e0cf0af7efbe591da.js'),
    h = require('./5719b6ded53098ffd9e848abcac30153.js'),
    i = require('./da7c31daaf542cf1796023d8e344b98a.js'),
    j = require('./tools.js'),
    k = 'darwin' === process.platform,
    l = (a, b = {}) =>
      new Promise(async (d, i) => {
        try {
          if (b.isTemp) {
            let c
            try {
              c = await g.getAppInfo(b.appid, b)
            } catch (a) {
              i(a)
            }
            const e = _extends({}, b, {
                projectpath: a,
                projectname: `tmp_${b.appid}_${+new Date()}`
              }),
              h = `${b.appid}_${e.projectname}`
            ;(e.compileType = c.game_app ? 'game' : 'weapp'),
              await f.openProject(c, e, a, b),
              d(h)
          } else {
            let j
            if (((j = e.checkProjectPath(a)), j.error)) return i(j.error)
            let l
            if (((j = e.checkProjectConfigJSON(a)), j.error)) return i(j.error)
            if (((l = j.config), !l.appid || !l.projectname))
              return i(
                h.ERROR.NOT_FOUND_IN_PROJECT_CONFIG_JSON(
                  l.appid ? 'projectname' : 'appid'
                )
              )
            ;(l.projectname = encodeURIComponent(
              decodeURIComponent(l.projectname || '')
            )),
              l.isGameTourist && (l.appid = c.GAME_TOURIST_APPID)
            const m = `${l.appid}_${l.projectname}`
            if (f.isOpened(m, a)) {
              process.stderr.write('isopened\n')
              const a = global.windowMap.get('MAIN')
              if (a)
                return (
                  a.show(),
                  k ||
                    (a.setAlwaysOnTop(!0),
                    setTimeout(() => {
                      a.setAlwaysOnTop(!1)
                    }, 20)),
                  setTimeout(() => {
                    f.openSimulatorAndDebugger(), f.compile()
                  }, 20),
                  void d(m)
                )
            }
            let n
            try {
              n = await g.getAppInfo(l.appid, b)
            } catch (a) {
              i(a)
            }
            await f.openProject(n, l, a, b), d(m)
          }
        } catch (a) {
          i(h.ERROR.GENERIC_ERROR(a.toString()))
        }
      })
  module.exports = {
    registerHandlers: c => {
      c.get('/open', async (c, d) => {
        try {
          global.Win.showFunction && (global.Win.show = global.Win.showFunction)
          const e = a.getState()
          if (
            e.window.mainWindow !== b.MAIN_WINDOW_TYPE.DEV &&
            e.window.mainWindow !== b.MAIN_WINDOW_TYPE.WEB_DEBUGGER
          )
            k ? global.Win.show() : global.Win.focus()
          else {
            const a = global.windowMap.get('MAIN')
            a && (k ? a.show() : a.focus())
          }
          let f = c.mQuery.projectpath
          const g = decodeURIComponent(c.mQuery.cli || ''),
            h = decodeURIComponent(c.mQuery.from || '')
          g
            ? f
              ? i('cli_open_project', null, null, h ? { engine_from: h } : null)
              : i('cli_open_ide', null, null, h ? { engine_from: h } : null)
            : f
              ? i(
                  'http_open_project',
                  null,
                  null,
                  h ? { engine_from: h } : null
                )
              : i('http_open_ide', null, null, h ? { engine_from: h } : null),
            f && ((f = decodeURIComponent(f)), await l(f)),
            (d.statusCode = 200),
            d.end()
        } catch (a) {
          ;(d.statusCode = 400), d.end(a.toString(), 'utf8')
        }
      }),
        c.get('/quit', async (a, b) => {
          ;(b.statusCode = 200), b.end(), j.quit()
        })
    },
    openProject: l
  }
})(require('lazyload'), require)
