'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 _chalk = _interopRequireDefault(require("chalk"));

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 _path = _interopRequireDefault(require("path"));

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

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

var Server = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee16(config) {
    var mConfig, DKAServer, DKAPointing;
    return _regenerator["default"].wrap(function _callee16$(_context16) {
      while (1) {
        switch (_context16.prev = _context16.next) {
          case 0:
            _context16.next = 2;
            return _lodash["default"].extend({
              /** ServerName For Naming App Server **/
              serverName: "",

              /** Server Domain For Naming Service Domain Access **/
              serverDomain: "http://localhost",

              /** Server State Overide Enable and Disabled **/
              serverEnabled: true,

              /** Memilih Jenis Server Engine Yang Ingin Digunakan **/
              serverEngine: _Options["default"].FASTIFY_CORE_ENGINE,

              /** Untuk melakukan Set View Template View Di Dalam Framework **/
              serverView: _Options["default"].VIEW_POV_EJS,

              /** State Server {Server.SERVER_STATE_DEVELOPMENT | Server.SERVER_STATE_PRODUCTION } **/
              serverState: _Options["default"].SERVER_STATE_DEVELOPMENT,

              /** Server Domain Location **/
              serverHost: "localhost",

              /** Server Port **/
              serverPort: 80,

              /** Activated Security System **/
              secure: false,

              /** Memulai System App Controller **/
              app: false,

              /** Setting System Settings **/
              settings: {
                /** Ngrok Tunneling **/
                ngrok: {
                  enabled: false,
                  authToken: null
                },
                localtunnel: false
              },

              /** Fungsi Untuk Melakukan Asset Delarasion Untuk Menentukan Folder Asets Di Dalam System Data **/
              options: {
                /** Deklarasi Menetapkan Lokasi Assets CSS dan JS Dir Dan Gambar Untuk Aplikasi **/
                layoutDir: _path["default"].join(require.main.filename, "./../Layout"),
                assetsDir: _path["default"].join(require.main.filename, "./../Assets"),
                appDir: _path["default"].join(require.main.filename, "./../Controller"),

                /** Fungsi Untuk Melakukan Folder Upload Di dalam aplikasi **/
                uploadDir: _path["default"].join(require.main.filename, "./../Upload")
                /** **/

              }
            }, config);

          case 2:
            mConfig = _context16.sent;
            _context16.next = 5;
            return new Promise( /*#__PURE__*/function () {
              var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(resolve, rejected) {
                return _regenerator["default"].wrap(function _callee7$(_context7) {
                  while (1) {
                    switch (_context7.prev = _context7.next) {
                      case 0:
                        _context7.t0 = mConfig.serverEngine;
                        _context7.next = _context7.t0 === _Options["default"].HTTP2_CORE_ENGINE ? 3 : _context7.t0 === _Options["default"].EXPRESS_CORE_ENGINE ? 6 : _context7.t0 === _Options["default"].FASTIFY_CORE_ENGINE ? 9 : _context7.t0 === _Options["default"].RESTIFY_CORE_ENGINE ? 11 : _context7.t0 === _Options["default"].REACTJS_CORE_ENGINE ? 14 : 17;
                        break;

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

                      case 5:
                        return _context7.abrupt("break", 19);

                      case 6:
                        _context7.next = 8;
                        return (0, _EXPRESS["default"])(mConfig).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 _context7.abrupt("break", 19);

                      case 9:
                        //**************************************************/
                        (0, _FASTIFY["default"])(mConfig).then( /*#__PURE__*/function () {
                          var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(AppEngine) {
                            return _regenerator["default"].wrap(function _callee2$(_context2) {
                              while (1) {
                                switch (_context2.prev = _context2.next) {
                                  case 0:
                                    _context2.next = 2;
                                    return resolve(AppEngine);

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

                          return function (_x5) {
                            return _ref4.apply(this, arguments);
                          };
                        }())["catch"]( /*#__PURE__*/function () {
                          var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(error) {
                            return _regenerator["default"].wrap(function _callee3$(_context3) {
                              while (1) {
                                switch (_context3.prev = _context3.next) {
                                  case 0:
                                    _context3.next = 2;
                                    return rejected(error);

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

                          return function (_x6) {
                            return _ref5.apply(this, arguments);
                          };
                        }());
                        /***************************************************/

                        return _context7.abrupt("break", 19);

                      case 11:
                        _context7.next = 13;
                        return resolve();

                      case 13:
                        return _context7.abrupt("break", 19);

                      case 14:
                        _context7.next = 16;
                        return (0, _REACT["default"])(mConfig).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 (_x7) {
                            return _ref6.apply(this, arguments);
                          };
                        }());

                      case 16:
                        return _context7.abrupt("break", 19);

                      case 17:
                        (0, _FASTIFY["default"])(mConfig).then( /*#__PURE__*/function () {
                          var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(AppEngine) {
                            return _regenerator["default"].wrap(function _callee5$(_context5) {
                              while (1) {
                                switch (_context5.prev = _context5.next) {
                                  case 0:
                                    _context5.next = 2;
                                    return resolve(AppEngine);

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

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

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

                          return function (_x9) {
                            return _ref8.apply(this, arguments);
                          };
                        }());
                        return _context7.abrupt("break", 19);

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

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

          case 5:
            DKAServer = _context16.sent;
            _context16.next = 8;
            return new Promise( /*#__PURE__*/function () {
              var _ref9 = (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 _ref10 = (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:
                                    if (!(mConfig.app != null)) {
                                      _context10.next = 41;
                                      break;
                                    }

                                    _context10.t0 = mConfig.serverEngine;
                                    _context10.next = _context10.t0 === _Options["default"].EXPRESS_CORE_ENGINE ? 4 : _context10.t0 === _Options["default"].FASTIFY_CORE_ENGINE ? 9 : _context10.t0 === _Options["default"].REACTJS_CORE_ENGINE ? 28 : _context10.t0 === _Options["default"].RESTIFY_CORE_ENGINE ? 33 : 36;
                                    break;

                                  case 4:
                                    _context10.next = 6;
                                    return AppEngine.use(mConfig.app);

                                  case 6:
                                    _context10.next = 8;
                                    return resolve(AppEngine);

                                  case 8:
                                    return _context10.abrupt("break", 39);

                                  case 9:
                                    if (!mConfig.app) {
                                      _context10.next = 16;
                                      break;
                                    }

                                    _context10.next = 12;
                                    return AppEngine.register( /*#__PURE__*/function () {
                                      var _ref11 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(app, opts, next) {
                                        return _regenerator["default"].wrap(function _callee8$(_context8) {
                                          while (1) {
                                            switch (_context8.prev = _context8.next) {
                                              case 0:
                                                _context8.next = 2;
                                                return app.register(mConfig.app);

                                              case 2:
                                                _context8.next = 4;
                                                return next();

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

                                      return function (_x13, _x14, _x15) {
                                        return _ref11.apply(this, arguments);
                                      };
                                    }());

                                  case 12:
                                    _context10.next = 14;
                                    return resolve(AppEngine);

                                  case 14:
                                    _context10.next = 27;
                                    break;

                                  case 16:
                                    if (!(0, _fs.existsSync)(mConfig.options.appDir)) {
                                      _context10.next = 23;
                                      break;
                                    }

                                    _context10.next = 19;
                                    return AppEngine.register( /*#__PURE__*/function () {
                                      var _ref12 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(app, opts, next) {
                                        return _regenerator["default"].wrap(function _callee9$(_context9) {
                                          while (1) {
                                            switch (_context9.prev = _context9.next) {
                                              case 0:
                                                _context9.next = 2;
                                                return app.register(require(mConfig.options.appDir));

                                              case 2:
                                                _context9.next = 4;
                                                return next();

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

                                      return function (_x16, _x17, _x18) {
                                        return _ref12.apply(this, arguments);
                                      };
                                    }());

                                  case 19:
                                    _context10.next = 21;
                                    return resolve(AppEngine);

                                  case 21:
                                    _context10.next = 27;
                                    break;

                                  case 23:
                                    _context10.next = 25;
                                    return console.log(_chalk["default"].white("Directory `Controller` Not Exist. Please Make Dir in " + mConfig.options.appDir + " "));

                                  case 25:
                                    _context10.next = 27;
                                    return resolve(AppEngine);

                                  case 27:
                                    return _context10.abrupt("break", 39);

                                  case 28:
                                    _context10.next = 30;
                                    return (0, _REACT["default"])(mConfig, mConfig.app);

                                  case 30:
                                    _context10.next = 32;
                                    return resolve();

                                  case 32:
                                    return _context10.abrupt("break", 39);

                                  case 33:
                                    _context10.next = 35;
                                    return resolve();

                                  case 35:
                                    return _context10.abrupt("break", 39);

                                  case 36:
                                    _context10.next = 38;
                                    return resolve();

                                  case 38:
                                    return _context10.abrupt("break", 39);

                                  case 39:
                                    _context10.next = 42;
                                    break;

                                  case 41:
                                    resolve(AppEngine);

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

                          return function (_x12) {
                            return _ref10.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 (_x19) {
                            return _ref13.apply(this, arguments);
                          };
                        }());

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

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

          case 8:
            DKAPointing = _context16.sent;
            _context16.next = 11;
            return new Promise( /*#__PURE__*/function () {
              var _ref14 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee15(resolve, rejected) {
                return _regenerator["default"].wrap(function _callee15$(_context15) {
                  while (1) {
                    switch (_context15.prev = _context15.next) {
                      case 0:
                        DKAPointing.then( /*#__PURE__*/function () {
                          var _ref15 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee14(AppEngine) {
                            return _regenerator["default"].wrap(function _callee14$(_context14) {
                              while (1) {
                                switch (_context14.prev = _context14.next) {
                                  case 0:
                                    _context14.t0 = mConfig.serverEngine;
                                    _context14.next = _context14.t0 === _Options["default"].FASTIFY_CORE_ENGINE ? 3 : 6;
                                    break;

                                  case 3:
                                    _context14.next = 5;
                                    return AppEngine.listen(mConfig.serverPort, mConfig.serverHost, /*#__PURE__*/function () {
                                      var _ref16 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee13(err, address) {
                                        var tunnels, response, tunnel, _response, _response2;

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

                                                if (!(mConfig.settings.ngrok.enabled === true)) {
                                                  _context13.next = 12;
                                                  break;
                                                }

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

                                              case 4:
                                                _context13.next = 6;
                                                return _ngrok["default"].getApi().get('api/tunnels');

                                              case 6:
                                                tunnels = _context13.sent;
                                                response = {
                                                  status: true,
                                                  msg: "Berhasil",
                                                  text: "Aplikasi \"".concat(mConfig.serverName, "\" Server Dengan Alamat ").concat(address),
                                                  Ngrok: [JSON.parse(tunnels).tunnels[0].public_url, JSON.parse(tunnels).tunnels[1].public_url]
                                                };
                                                _context13.next = 10;
                                                return resolve(response);

                                              case 10:
                                                _context13.next = 25;
                                                break;

                                              case 12:
                                                if (!(mConfig.settings.localtunnel === true)) {
                                                  _context13.next = 22;
                                                  break;
                                                }

                                                _context13.next = 15;
                                                return (0, _localtunnel["default"])({
                                                  port: mConfig.serverPort
                                                });

                                              case 15:
                                                tunnel = _context13.sent;
                                                _response = {
                                                  status: true,
                                                  msg: "Berhasil",
                                                  text: "Aplikasi \"".concat(mConfig.serverName, "\" Server Dengan Alamat ").concat(address),
                                                  Localtunnel: tunnel.url
                                                };
                                                tunnel.on('close', function () {// tunnels are closed
                                                });
                                                _context13.next = 20;
                                                return resolve(_response);

                                              case 20:
                                                _context13.next = 25;
                                                break;

                                              case 22:
                                                _response2 = {
                                                  status: true,
                                                  msg: "Berhasil",
                                                  text: "Aplikasi \"".concat(mConfig.serverName, "\" Server Dengan Alamat ").concat(address)
                                                };
                                                _context13.next = 25;
                                                return resolve(_response2);

                                              case 25:
                                                _context13.next = 28;
                                                break;

                                              case 27:
                                                rejected(err);

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

                                      return function (_x23, _x24) {
                                        return _ref16.apply(this, arguments);
                                      };
                                    }());

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

                                  case 6:
                                    rejected("Not Engine Selected");

                                  case 7:
                                  case "end":
                                    return _context14.stop();
                                }
                              }
                            }, _callee14);
                          }));

                          return function (_x22) {
                            return _ref15.apply(this, arguments);
                          };
                        }());

                      case 1:
                      case "end":
                        return _context15.stop();
                    }
                  }
                }, _callee15);
              }));

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

          case 11:
            return _context16.abrupt("return", _context16.sent);

          case 12:
          case "end":
            return _context16.stop();
        }
      }
    }, _callee16);
  }));

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

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