'use strict'
!(function(require, directRequire) {
  const a = require('fs'),
    b = require('path'),
    c = require('http'),
    d = require('request'),
    e = require('./logger.js'),
    f = require('./74af8a23ddee45c6c1265c88746b66bc.js'),
    g = require('./c74067b2c1f476fa940f6315c56fb29e.js'),
    h = require('./tools.js'),
    i = require('./881e653f19d837f2408386047cb8c38c.js'),
    j = async a =>
      new Promise((b, d) => {
        const e = c.createServer(f.userRequestHandler)
        e.listen(a, '127.0.0.1', a => {
          return a ? d(a) : b()
        })
      })
  module.exports = {
    start: (c, f) =>
      new Promise(async (k, l) => {
        try {
          e.info(
            `start cli server, local port ${c}, cli client port ${f ||
              'undefined'}`
          ),
            await j(c)
          const l = nw.App.getDataPath()
          if (
            (global.isDevWindow
              ? (i.notifyCliPortUpdate(c),
                a.writeFileSync(
                  b.join(
                    l,
                    `.ide-${global.devInfo.appid}-${global.devInfo.projectname}`
                  ),
                  c,
                  'utf8'
                ))
              : a.writeFileSync(b.join(l, '.ide'), c, 'utf8'),
            !global.isDevWindow)
          )
            if (f)
              d({ url: `http://127.0.0.1:${f}/updatePort?port=${c}` }, a => {
                a && e.error('fail to notify remote cli program:', a)
              }),
                global.CLI.isTestMode &&
                  (d(
                    {
                      url: `http://127.0.0.1:${f}/wechatdevtools/${encodeURIComponent(
                        global.CLI.id
                      )}/pid/${process.pid}`
                    },
                    a => {
                      a &&
                        (e.error('fail to notify remote driver program:', a),
                        global.CLI.isTestMode &&
                          (e.error('quit ide...'), !global.online && h.quit()))
                    }
                  ),
                  d(
                    {
                      url: `http://127.0.0.1:${f}/wechatdevtools/${encodeURIComponent(
                        global.CLI.id
                      )}/updateport/${c}`
                    },
                    a => {
                      a &&
                        (e.error('fail to notify remote driver program:', a),
                        global.CLI.isTestMode &&
                          (e.error('quit ide...'), !global.online && h.quit()))
                    }
                  ))
            else
              try {
                ;(f = parseInt(a.readFileSync(b.join(l, '.cli'), 'utf8'))),
                  d(
                    {
                      url: `${'http://127.0.0.1'}:${f}/updatePort?port=${c}`,
                      timeout: 2e3
                    },
                    a => {
                      a
                        ? e.info(`no cli client started`)
                        : g.updateRemoteCLIPort(f)
                    }
                  )
              } catch (a) {
                e.info(`no cli client started`)
              }
          k()
        } catch (a) {
          l(a)
        }
      })
  }
})(require('lazyload'), require)
