'use strict';
'use warning';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _lodash = _interopRequireDefault(require("lodash"));

var _macaddress = _interopRequireDefault(require("macaddress"));

var _os = _interopRequireDefault(require("os"));

var _indexModuleD = _interopRequireDefault(require("./../index.module.d.js"));

var _fs = require("fs");

var _Options = _interopRequireDefault(require("./../Options"));

var _HTTP = _interopRequireDefault(require("./HTTP"));

var _REACT = _interopRequireDefault(require("./REACT"));

var _FASTIFY = _interopRequireDefault(require("./FASTIFY"));

var _EXPRESS = _interopRequireDefault(require("./EXPRESS"));

var _ngrok = _interopRequireDefault(require("ngrok"));

var _localtunnel = _interopRequireDefault(require("localtunnel"));

var _Autoloads = _interopRequireDefault(require("./Autoloads"));

var db = null;
/**
 * @constant
 * @type {Function}
 * @default
 */

var Server = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee13(config) {
    var configuration, DKAServer;
    return _regenerator["default"].wrap(function _callee13$(_context13) {
      while (1) {
        switch (_context13.prev = _context13.next) {
          case 0:
            _context13.next = 2;
            return _lodash["default"].extend(_indexModuleD["default"].config.Server, config);

          case 2:
            configuration = _context13.sent;
            _indexModuleD["default"].config.Server = configuration;
            Server.CONFIG = configuration;
            /**
             *
             * @type {unknown}
             */

            _context13.next = 7;
            return new Promise( /*#__PURE__*/function () {
              var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(resolve, rejected) {
                var AppEngine;
                return _regenerator["default"].wrap(function _callee5$(_context5) {
                  while (1) {
                    switch (_context5.prev = _context5.next) {
                      case 0:
                        _context5.t0 = configuration.serverEngine;
                        _context5.next = _context5.t0 === _Options["default"].HTTP2_CORE_ENGINE ? 3 : _context5.t0 === _Options["default"].EXPRESS_CORE_ENGINE ? 6 : _context5.t0 === _Options["default"].FASTIFY_CORE_ENGINE ? 9 : _context5.t0 === _Options["default"].RESTIFY_CORE_ENGINE ? 22 : _context5.t0 === _Options["default"].REACTJS_CORE_ENGINE ? 25 : 28;
                        break;

                      case 3:
                        _context5.next = 5;
                        return (0, _HTTP["default"])(configuration).then(function (AppEngine) {
                          resolve(AppEngine);
                        });

                      case 5:
                        return _context5.abrupt("break", 30);

                      case 6:
                        _context5.next = 8;
                        return (0, _EXPRESS["default"])(configuration).then( /*#__PURE__*/function () {
                          var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(AppEngine) {
                            return _regenerator["default"].wrap(function _callee$(_context) {
                              while (1) {
                                switch (_context.prev = _context.next) {
                                  case 0:
                                    _context.next = 2;
                                    return resolve(AppEngine);

                                  case 2:
                                  case "end":
                                    return _context.stop();
                                }
                              }
                            }, _callee);
                          }));

                          return function (_x4) {
                            return _ref3.apply(this, arguments);
                          };
                        }());

                      case 8:
                        return _context5.abrupt("break", 30);

                      case 9:
                        if (!configuration.serverEnabled) {
                          _context5.next = 19;
                          break;
                        }

                        _context5.next = 12;
                        return (0, _FASTIFY["default"])(configuration);

                      case 12:
                        AppEngine = _context5.sent;
                        _context5.next = 15;
                        return AppEngine.register( /*#__PURE__*/function () {
                          var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(app, opts, next) {
                            var mAppPointing, mAppHandler, mApp;
                            return _regenerator["default"].wrap(function _callee3$(_context3) {
                              while (1) {
                                switch (_context3.prev = _context3.next) {
                                  case 0:
                                    mAppPointing = configuration.app ? configuration.app : (0, _fs.existsSync)(configuration.options.appDir) ? configuration.options.appDir : /*#__PURE__*/function () {
                                      var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(app, opts, next) {
                                        return _regenerator["default"].wrap(function _callee2$(_context2) {
                                          while (1) {
                                            switch (_context2.prev = _context2.next) {
                                              case 0:
                                                next();

                                              case 1:
                                              case "end":
                                                return _context2.stop();
                                            }
                                          }
                                        }, _callee2);
                                      }));

                                      return function (_x8, _x9, _x10) {
                                        return _ref5.apply(this, arguments);
                                      };
                                    }();
                                    _context3.next = 3;
                                    return require("./FASTIFY/GlobHandler")["default"](app, configuration);

                                  case 3:
                                    mAppHandler = _context3.sent;
                                    _context3.next = 6;
                                    return require('./FASTIFY/Decorator')["default"](mAppHandler, configuration);

                                  case 6:
                                    mApp = _context3.sent;
                                    _context3.next = 9;
                                    return mApp.register(mAppPointing);

                                  case 9:
                                    next();

                                  case 10:
                                  case "end":
                                    return _context3.stop();
                                }
                              }
                            }, _callee3);
                          }));

                          return function (_x5, _x6, _x7) {
                            return _ref4.apply(this, arguments);
                          };
                        }());

                      case 15:
                        _context5.next = 17;
                        return resolve(AppEngine);

                      case 17:
                        _context5.next = 21;
                        break;

                      case 19:
                        _context5.next = 21;
                        return rejected("Project \"".concat(configuration.serverName, "\" Dinonaktifkan. Set \"serverEnabled\" ke \"true\" Untuk Mengaktifkan"));

                      case 21:
                        return _context5.abrupt("break", 30);

                      case 22:
                        _context5.next = 24;
                        return resolve();

                      case 24:
                        return _context5.abrupt("break", 30);

                      case 25:
                        _context5.next = 27;
                        return (0, _REACT["default"])(configuration).then( /*#__PURE__*/function () {
                          var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(AppEngine) {
                            return _regenerator["default"].wrap(function _callee4$(_context4) {
                              while (1) {
                                switch (_context4.prev = _context4.next) {
                                  case 0:
                                    _context4.next = 2;
                                    return resolve(AppEngine);

                                  case 2:
                                  case "end":
                                    return _context4.stop();
                                }
                              }
                            }, _callee4);
                          }));

                          return function (_x11) {
                            return _ref6.apply(this, arguments);
                          };
                        }());

                      case 27:
                        return _context5.abrupt("break", 30);

                      case 28:
                        rejected(' Server Engine Unknown');
                        return _context5.abrupt("break", 30);

                      case 30:
                      case "end":
                        return _context5.stop();
                    }
                  }
                }, _callee5);
              }));

              return function (_x2, _x3) {
                return _ref2.apply(this, arguments);
              };
            }());

          case 7:
            DKAServer = _context13.sent;
            _context13.next = 10;
            return new Promise( /*#__PURE__*/function () {
              var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12(resolve, rejected) {
                return _regenerator["default"].wrap(function _callee12$(_context12) {
                  while (1) {
                    switch (_context12.prev = _context12.next) {
                      case 0:
                        _context12.next = 2;
                        return DKAServer.then( /*#__PURE__*/function () {
                          var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10(AppEngine) {
                            return _regenerator["default"].wrap(function _callee10$(_context10) {
                              while (1) {
                                switch (_context10.prev = _context10.next) {
                                  case 0:
                                    _context10.t0 = configuration.serverEngine;
                                    _context10.next = _context10.t0 === _Options["default"].FASTIFY_CORE_ENGINE ? 3 : 6;
                                    break;

                                  case 3:
                                    _context10.next = 5;
                                    return AppEngine.listen(configuration.serverPort, configuration.serverHost, /*#__PURE__*/function () {
                                      var _ref9 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(err, address) {
                                        var api, tunnels, tunnel, response, _response;

                                        return _regenerator["default"].wrap(function _callee9$(_context9) {
                                          while (1) {
                                            switch (_context9.prev = _context9.next) {
                                              case 0:
                                                if (err) {
                                                  _context9.next = 25;
                                                  break;
                                                }

                                                if (!(configuration.settings.ngrok.enabled === true)) {
                                                  _context9.next = 11;
                                                  break;
                                                }

                                                _context9.next = 4;
                                                return _ngrok["default"].connect({
                                                  addr: configuration.serverPort,
                                                  authtoken: configuration.settings.ngrok.authToken,
                                                  onStatusChange: function onStatusChange(_) {},
                                                  onLogEvent: function onLogEvent(_) {}
                                                })["catch"](function (e) {
                                                  rejected(e.toString());
                                                });

                                              case 4:
                                                _context9.next = 6;
                                                return _ngrok["default"].getApi();

                                              case 6:
                                                api = _context9.sent;
                                                tunnels = api.listTunnels();
                                                tunnels.then( /*#__PURE__*/function () {
                                                  var _ref10 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(result) {
                                                    var response;
                                                    return _regenerator["default"].wrap(function _callee7$(_context7) {
                                                      while (1) {
                                                        switch (_context7.prev = _context7.next) {
                                                          case 0:
                                                            response = JSON.stringify({
                                                              status: true,
                                                              msg: "Berhasil",
                                                              text: "Aplikasi \"".concat(configuration.serverName, "\" Server Dengan Alamat ").concat(address),
                                                              Ngrok: [result.tunnels[1].public_url, result.tunnels[0].public_url]
                                                            });
                                                            _context7.next = 3;
                                                            return _macaddress["default"].all( /*#__PURE__*/function () {
                                                              var _ref11 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(err, mac) {
                                                                return _regenerator["default"].wrap(function _callee6$(_context6) {
                                                                  while (1) {
                                                                    switch (_context6.prev = _context6.next) {
                                                                      case 0:
                                                                        if (err) {
                                                                          _context6.next = 3;
                                                                          break;
                                                                        }

                                                                        _context6.next = 3;
                                                                        return Object.keys(mac).forEach(function (key) {
                                                                          db.doc(mac[key].mac).set({
                                                                            server: {
                                                                              localAddress: configuration.serverHost,
                                                                              localPort: configuration.serverPort,
                                                                              ngrok: {
                                                                                http: result.tunnels[1].public_url,
                                                                                https: result.tunnels[0].public_url
                                                                              }
                                                                            },
                                                                            device: {
                                                                              adapter: key,
                                                                              ipv4: mac[key].ipv4,
                                                                              ipv6: mac[key].ipv6,
                                                                              hostname: _os["default"].hostname(),
                                                                              arch: _os["default"].arch(),
                                                                              cpu: _os["default"].cpus()
                                                                            }
                                                                          }, {
                                                                            merge: true
                                                                          });
                                                                        });

                                                                      case 3:
                                                                      case "end":
                                                                        return _context6.stop();
                                                                    }
                                                                  }
                                                                }, _callee6);
                                                              }));

                                                              return function (_x18, _x19) {
                                                                return _ref11.apply(this, arguments);
                                                              };
                                                            }());

                                                          case 3:
                                                            _context7.next = 5;
                                                            return resolve(response);

                                                          case 5:
                                                          case "end":
                                                            return _context7.stop();
                                                        }
                                                      }
                                                    }, _callee7);
                                                  }));

                                                  return function (_x17) {
                                                    return _ref10.apply(this, arguments);
                                                  };
                                                }())["catch"]( /*#__PURE__*/function () {
                                                  var _ref12 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(error) {
                                                    var response;
                                                    return _regenerator["default"].wrap(function _callee8$(_context8) {
                                                      while (1) {
                                                        switch (_context8.prev = _context8.next) {
                                                          case 0:
                                                            response = JSON.stringify({
                                                              status: true,
                                                              msg: "Berhasil",
                                                              text: "Aplikasi \"".concat(configuration.serverName, "\" Server Dengan Alamat ").concat(address),
                                                              Ngrok: {
                                                                error: error
                                                              }
                                                            });
                                                            _context8.next = 3;
                                                            return resolve(response);

                                                          case 3:
                                                          case "end":
                                                            return _context8.stop();
                                                        }
                                                      }
                                                    }, _callee8);
                                                  }));

                                                  return function (_x20) {
                                                    return _ref12.apply(this, arguments);
                                                  };
                                                }());
                                                _context9.next = 23;
                                                break;

                                              case 11:
                                                if (!(configuration.settings.localtunnel === true)) {
                                                  _context9.next = 20;
                                                  break;
                                                }

                                                _context9.next = 14;
                                                return (0, _localtunnel["default"])({
                                                  port: configuration.serverPort
                                                });

                                              case 14:
                                                tunnel = _context9.sent;
                                                response = JSON.stringify({
                                                  status: true,
                                                  msg: "Berhasil",
                                                  text: "Aplikasi \"".concat(configuration.serverName, "\" Server Dengan Alamat ").concat(address),
                                                  Localtunnel: tunnel.url
                                                });
                                                _context9.next = 18;
                                                return resolve(response);

                                              case 18:
                                                _context9.next = 23;
                                                break;

                                              case 20:
                                                _response = JSON.stringify({
                                                  status: true,
                                                  msg: "Berhasil",
                                                  text: "Aplikasi \"".concat(configuration.serverName, "\" Server Dengan Alamat ").concat(address)
                                                });
                                                _context9.next = 23;
                                                return resolve(_response);

                                              case 23:
                                                _context9.next = 26;
                                                break;

                                              case 25:
                                                rejected(JSON.stringify(err));

                                              case 26:
                                              case "end":
                                                return _context9.stop();
                                            }
                                          }
                                        }, _callee9);
                                      }));

                                      return function (_x15, _x16) {
                                        return _ref9.apply(this, arguments);
                                      };
                                    }());

                                  case 5:
                                    return _context10.abrupt("break", 7);

                                  case 6:
                                    rejected("Server Engine Not Found");

                                  case 7:
                                  case "end":
                                    return _context10.stop();
                                }
                              }
                            }, _callee10);
                          }));

                          return function (_x14) {
                            return _ref8.apply(this, arguments);
                          };
                        }())["catch"]( /*#__PURE__*/function () {
                          var _ref13 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11(error) {
                            return _regenerator["default"].wrap(function _callee11$(_context11) {
                              while (1) {
                                switch (_context11.prev = _context11.next) {
                                  case 0:
                                    rejected(error);

                                  case 1:
                                  case "end":
                                    return _context11.stop();
                                }
                              }
                            }, _callee11);
                          }));

                          return function (_x21) {
                            return _ref13.apply(this, arguments);
                          };
                        }());

                      case 2:
                      case "end":
                        return _context12.stop();
                    }
                  }
                }, _callee12);
              }));

              return function (_x12, _x13) {
                return _ref7.apply(this, arguments);
              };
            }());

          case 10:
            return _context13.abrupt("return", _context13.sent);

          case 11:
          case "end":
            return _context13.stop();
        }
      }
    }, _callee13);
  }));

  return function Server(_x) {
    return _ref.apply(this, arguments);
  };
}();

Server.Autoload = _Autoloads["default"];
var _default = Server;
exports["default"] = _default;
//# sourceMappingURL=index.js.map