/**
 *
 *Created By Yovangga Anandhika
 * DKA Framework Artificial Intelegencia Console
 * All Right Reserved
 *
 */
'use strict';
'use warning';
/**
 * @TODO Author : Yovangga Ananadhika
 *
 *
 * **/

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

var _fs = require("fs");

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

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

var _chalk = _interopRequireDefault(require("chalk"));

var Autoload = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
    return _regenerator["default"].wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            new Promise( /*#__PURE__*/function () {
              var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(resolve, rejected) {
                var mApp;
                return _regenerator["default"].wrap(function _callee4$(_context4) {
                  while (1) {
                    switch (_context4.prev = _context4.next) {
                      case 0:
                        mApp = (0, _path.join)(require.main.filename, "./../App");

                        if (!(0, _fs.existsSync)(mApp)) {
                          _context4.next = 7;
                          break;
                        }

                        new Promise( /*#__PURE__*/function () {
                          var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(resolve) {
                            return _regenerator["default"].wrap(function _callee2$(_context2) {
                              while (1) {
                                switch (_context2.prev = _context2.next) {
                                  case 0:
                                    _context2.next = 2;
                                    return (0, _fs.readdirSync)(mApp).forEach(function (file) {
                                      var AppProjectName = (0, _path.join)(mApp, file);
                                      (0, _fs.lstat)(AppProjectName, /*#__PURE__*/function () {
                                        var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(error, stats) {
                                          var AppFile, AppFileModule;
                                          return _regenerator["default"].wrap(function _callee$(_context) {
                                            while (1) {
                                              switch (_context.prev = _context.next) {
                                                case 0:
                                                  if (!stats.isDirectory()) {
                                                    _context.next = 17;
                                                    break;
                                                  }

                                                  /** Check Configuration **/
                                                  AppFile = (0, _path.join)(AppProjectName, "./index.js");

                                                  if (!(0, _fs.existsSync)(AppFile)) {
                                                    _context.next = 16;
                                                    break;
                                                  }

                                                  /** If AppFile Is Exists **/
                                                  AppFileModule = require(AppFile)["default"];
                                                  /** Check App File is Object Format {JSON}**/

                                                  if (!(AppFileModule instanceof Object)) {
                                                    _context.next = 13;
                                                    break;
                                                  }

                                                  if (!AppFileModule.serverEnabled) {
                                                    _context.next = 10;
                                                    break;
                                                  }

                                                  _context.next = 8;
                                                  return _indexModule["default"].Server(_lodash["default"].extend({
                                                    serverName: file,
                                                    serverEnabled: true,
                                                    options: {
                                                      /** Deklarasi Menetapkan Lokasi Assets CSS dan JS Dir Dan Gambar Untuk Aplikasi **/
                                                      layoutDir: (0, _path.join)(AppFile, "./../Layout"),
                                                      assetsDir: (0, _path.join)(AppFile, "./../Assets"),
                                                      appDir: (0, _path.join)(AppFile, "./../Controller"),

                                                      /** Fungsi Untuk Melakukan Folder Upload Di dalam aplikasi **/
                                                      uploadDir: (0, _path.join)(AppFile, "./../Upload"),
                                                      backupDir: (0, _path.join)(AppFile, "./../Backup")
                                                      /** **/

                                                    }
                                                  }, AppFileModule)).then(function (resolve) {
                                                    console.log(resolve);
                                                  })["catch"](function (error) {
                                                    console.log(error);
                                                  });

                                                case 8:
                                                  _context.next = 11;
                                                  break;

                                                case 10:
                                                  console.log(_chalk["default"].white("Project \"".concat(file, "\" Dinonaktifkan. Set \"serverEnabled\" ke \"true\" Untuk Mengaktifkan")));

                                                case 11:
                                                  _context.next = 14;
                                                  break;

                                                case 13:
                                                  console.log(_chalk["default"].redBright("File \"index.js\" Format Tidak Dikenali Di Folder \"".concat(file, "\". Harap Periksa File \"app.js\" Di Dalam Folder \"").concat(file, "\"")));

                                                case 14:
                                                  _context.next = 17;
                                                  break;

                                                case 16:
                                                  console.log(_chalk["default"].redBright("File \"index.js\" Tidak Ditemukan Di Folder \"".concat(file, "\" . Harap Buat File \"index.js\" Di Dalam Folder \"").concat(file, "\"")));

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

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

                                  case 2:
                                    _context2.next = 4;
                                    return resolve();

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

                          return function (_x3) {
                            return _ref3.apply(this, arguments);
                          };
                        }())["catch"]( /*#__PURE__*/function () {
                          var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(rejected) {
                            return _regenerator["default"].wrap(function _callee3$(_context3) {
                              while (1) {
                                switch (_context3.prev = _context3.next) {
                                  case 0:
                                    console.log(_chalk["default"].redBright("Terjadi Error Di Project \"".concat(file, "\". Kode Error : ") + rejected));

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

                          return function (_x6) {
                            return _ref5.apply(this, arguments);
                          };
                        }());
                        _context4.next = 5;
                        return resolve();

                      case 5:
                        _context4.next = 8;
                        break;

                      case 7:
                        rejected("Folder \"App\" Tidak Ditemukan Di tingkat root Anda. Harap Buat Folder \"App\" Di Dalam Project Root Anda");

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

              return function (_x, _x2) {
                return _ref2.apply(this, arguments);
              };
            }())["catch"](function (e) {
              console.error(e);
            });

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

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

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