"use strict";

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 _moment = _interopRequireDefault(require("moment"));

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

var _dns = _interopRequireDefault(require("dns"));

var _path = _interopRequireDefault(require("path"));

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

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

var _ip = _interopRequireDefault(require("ip"));

var _fs = require("fs");

var _fsExtra = _interopRequireDefault(require("fs-extra"));

var _dotenv = _interopRequireDefault(require("dotenv"));

var Base = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee13() {
    var db;
    return _regenerator["default"].wrap(function _callee13$(_context13) {
      while (1) {
        switch (_context13.prev = _context13.next) {
          case 0:
            _context13.next = 2;
            return new _indexModule.Database.Google.Firestore({
              apiKey: "AIzaSyCFV8E2Hi2b0ru6L_dwaUdZljeu1MXRunc",
              authDomain: "dka-apis.firebaseapp.com",
              databaseURL: "https://dka-apis-default-rtdb.asia-southeast1.firebasedatabase.app",
              projectId: "dka-apis",
              storageBucket: "dka-apis.appspot.com",
              messagingSenderId: "797501409741",
              appId: "1:797501409741:web:85c5ecd5a69e4a81bc5c84",
              measurementId: "G-1MT7ZM6VKT"
            });

          case 2:
            db = _context13.sent;
            return _context13.abrupt("return", new Promise( /*#__PURE__*/function () {
              var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11(resolve, rejected) {
                var rootProject, envPath, packageJson;
                return _regenerator["default"].wrap(function _callee11$(_context11) {
                  while (1) {
                    switch (_context11.prev = _context11.next) {
                      case 0:
                        rootProject = _path["default"].join(process.cwd());
                        envPath = _path["default"].resolve(rootProject, ".env");
                        _context11.next = 4;
                        return JSON.parse((0, _fs.readFileSync)(_path["default"].join(rootProject, "./package.json"), 'utf-8'));

                      case 4:
                        packageJson = _context11.sent;
                        _context11.next = 7;
                        return _dotenv["default"].config({
                          path: envPath
                        });

                      case 7:
                        _context11.next = 9;
                        return _macaddress["default"].one( /*#__PURE__*/function () {
                          var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(err, mac) {
                            var macAddress, doc;
                            return _regenerator["default"].wrap(function _callee2$(_context2) {
                              while (1) {
                                switch (_context2.prev = _context2.next) {
                                  case 0:
                                    if (err) {
                                      _context2.next = 7;
                                      break;
                                    }

                                    _context2.next = 3;
                                    return mac.replaceAll(":", "");

                                  case 3:
                                    macAddress = _context2.sent;
                                    doc = db.collection("DKA").doc("FRAMEWORK").collection("LICENCE").doc(macAddress).collection('APP').doc(packageJson.name !== undefined ? packageJson.name : "unknown");
                                    _context2.next = 7;
                                    return doc.onSnapshot( /*#__PURE__*/function () {
                                      var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(docSnapshot) {
                                        var mEnc, encode;
                                        return _regenerator["default"].wrap(function _callee$(_context) {
                                          while (1) {
                                            switch (_context.prev = _context.next) {
                                              case 0:
                                                mEnc = new _indexModule.Security.Encryption.Crypto({
                                                  secretKey: "@Thedarkangels2021"
                                                });
                                                _context.prev = 1;
                                                encode = mEnc.encodeIvSync(docSnapshot.data());
                                                _context.next = 5;
                                                return (0, _fs.writeFileSync)(envPath, "LICENCE_KEY=".concat(encode), {
                                                  encoding: 'utf-8'
                                                });

                                              case 5:
                                                _context.next = 9;
                                                break;

                                              case 7:
                                                _context.prev = 7;
                                                _context.t0 = _context["catch"](1);

                                              case 9:
                                              case "end":
                                                return _context.stop();
                                            }
                                          }
                                        }, _callee, null, [[1, 7]]);
                                      }));

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

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

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

                      case 9:
                        if (!(0, _fs.existsSync)(envPath)) {
                          _context11.next = 14;
                          break;
                        }

                        _context11.next = 12;
                        return _dns["default"].resolve("www.google.com", /*#__PURE__*/function () {
                          var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(err) {
                            var mEnc, dec;
                            return _regenerator["default"].wrap(function _callee6$(_context6) {
                              while (1) {
                                switch (_context6.prev = _context6.next) {
                                  case 0:
                                    if (err) {
                                      _context6.next = 5;
                                      break;
                                    }

                                    _context6.next = 3;
                                    return _macaddress["default"].one( /*#__PURE__*/function () {
                                      var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(err, mac) {
                                        var macAddress, mEnc, dec;
                                        return _regenerator["default"].wrap(function _callee5$(_context5) {
                                          while (1) {
                                            switch (_context5.prev = _context5.next) {
                                              case 0:
                                                if (err) {
                                                  _context5.next = 8;
                                                  break;
                                                }

                                                _context5.next = 3;
                                                return mac.replaceAll(":", "");

                                              case 3:
                                                macAddress = _context5.sent;
                                                _context5.next = 6;
                                                return db.collection("DKA").doc("FRAMEWORK").collection("LICENCE").doc(macAddress).collection('APP').doc(packageJson.name !== undefined ? packageJson.name : "unknown").get().then( /*#__PURE__*/function () {
                                                  var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(data) {
                                                    var mEnc, encode;
                                                    return _regenerator["default"].wrap(function _callee3$(_context3) {
                                                      while (1) {
                                                        switch (_context3.prev = _context3.next) {
                                                          case 0:
                                                            mEnc = new _indexModule.Security.Encryption.Crypto({
                                                              secretKey: "@Thedarkangels2021"
                                                            });
                                                            encode = mEnc.encodeIvSync(data.data());
                                                            _context3.next = 4;
                                                            return (0, _fs.writeFileSync)(envPath, "LICENCE_KEY=".concat(encode), {
                                                              encoding: 'utf-8'
                                                            });

                                                          case 4:
                                                            resolve(data.data());

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

                                                  return function (_x9) {
                                                    return _ref7.apply(this, arguments);
                                                  };
                                                }())["catch"]( /*#__PURE__*/function () {
                                                  var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(err) {
                                                    var mEnc, dec;
                                                    return _regenerator["default"].wrap(function _callee4$(_context4) {
                                                      while (1) {
                                                        switch (_context4.prev = _context4.next) {
                                                          case 0:
                                                            if (process.env.LICENCE_KEY !== undefined) {
                                                              mEnc = new _indexModule.Security.Encryption.Crypto({
                                                                secretKey: "@Thedarkangels2021"
                                                              });
                                                              dec = mEnc.decodeIvSync(process.env.LICENCE_KEY);

                                                              if (typeof dec !== 'string') {
                                                                resolve(dec);
                                                              } else {
                                                                rejected({
                                                                  status: false,
                                                                  code: 500,
                                                                  msg: "Your Licence Not Identifing or Wrong "
                                                                });
                                                              }
                                                            } else {
                                                              rejected({
                                                                status: false,
                                                                code: 500,
                                                                msg: "variable `LICENCE_KEY` not Exist "
                                                              });
                                                            }

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

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

                                              case 6:
                                                _context5.next = 9;
                                                break;

                                              case 8:
                                                if (process.env.LICENCE_KEY !== undefined) {
                                                  mEnc = new _indexModule.Security.Encryption.Crypto({
                                                    secretKey: "@Thedarkangels2021"
                                                  });
                                                  dec = mEnc.decodeIvSync(process.env.LICENCE_KEY);

                                                  if (typeof dec !== 'string') {
                                                    resolve(dec);
                                                  } else {
                                                    rejected({
                                                      status: false,
                                                      code: 500,
                                                      msg: "Your Licence Not Identifing or Wrong "
                                                    });
                                                  }
                                                } else {
                                                  rejected({
                                                    status: false,
                                                    code: 500,
                                                    msg: "variable `LICENCE_KEY` not Exist "
                                                  });
                                                }

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

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

                                  case 3:
                                    _context6.next = 6;
                                    break;

                                  case 5:
                                    if (process.env.LICENCE_KEY !== undefined) {
                                      mEnc = new _indexModule.Security.Encryption.Crypto({
                                        secretKey: "@Thedarkangels2021"
                                      });
                                      dec = mEnc.decodeIvSync(process.env.LICENCE_KEY);

                                      if (typeof dec !== 'string') {
                                        resolve(dec);
                                      } else {
                                        rejected({
                                          status: false,
                                          code: 500,
                                          msg: "Your Licence Not Identifing or Wrong "
                                        });
                                      }
                                    } else {
                                      rejected({
                                        status: false,
                                        code: 500,
                                        msg: "variable `LICENCE_KEY` not Exist "
                                      });
                                    }

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

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

                      case 12:
                        _context11.next = 16;
                        break;

                      case 14:
                        _context11.next = 16;
                        return _dns["default"].resolve("www.google.com", /*#__PURE__*/function () {
                          var _ref9 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10(err) {
                            return _regenerator["default"].wrap(function _callee10$(_context10) {
                              while (1) {
                                switch (_context10.prev = _context10.next) {
                                  case 0:
                                    if (err) {
                                      _context10.next = 5;
                                      break;
                                    }

                                    _context10.next = 3;
                                    return _macaddress["default"].one( /*#__PURE__*/function () {
                                      var _ref10 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(err, mac) {
                                        var macAddress, mDeviceMetaData;
                                        return _regenerator["default"].wrap(function _callee9$(_context9) {
                                          while (1) {
                                            switch (_context9.prev = _context9.next) {
                                              case 0:
                                                if (err) {
                                                  _context9.next = 9;
                                                  break;
                                                }

                                                macAddress = mac.replaceAll(":", "");
                                                mDeviceMetaData = {
                                                  packageName: packageJson.name,
                                                  ip_address: _ip["default"].address(),
                                                  architecture: _os["default"].arch(),
                                                  platform: _os["default"].platform(),
                                                  hostname: _os["default"].hostname(),
                                                  cwdPath: process.cwd(),
                                                  networkInterface: _os["default"].networkInterfaces(),
                                                  type: 'UNLICENCED',
                                                  format: false,
                                                  expiresDate: (0, _moment["default"])().subtract(1, 'day').format('DD-MM-YYYY')
                                                };

                                                if (packageJson.devDependencies !== undefined) {
                                                  mDeviceMetaData.devDependencies = packageJson.devDependencies;
                                                }

                                                if (packageJson.Dependencies !== undefined) {
                                                  mDeviceMetaData.Dependencies = packageJson.Dependencies;
                                                }

                                                _context9.next = 7;
                                                return db.collection("DKA").doc("FRAMEWORK").collection("LICENCE").doc(macAddress).collection('APP').doc(packageJson.name !== undefined ? packageJson.name : "unknown").set(mDeviceMetaData).then( /*#__PURE__*/function () {
                                                  var _ref11 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(res) {
                                                    var mEnc, encode;
                                                    return _regenerator["default"].wrap(function _callee7$(_context7) {
                                                      while (1) {
                                                        switch (_context7.prev = _context7.next) {
                                                          case 0:
                                                            mEnc = new _indexModule.Security.Encryption.Crypto({
                                                              secretKey: "@Thedarkangels2021"
                                                            });
                                                            encode = mEnc.encodeIvSync(mDeviceMetaData);
                                                            _context7.next = 4;
                                                            return (0, _fs.writeFileSync)(envPath, "LICENCE_KEY=".concat(encode), {
                                                              encoding: 'utf-8'
                                                            });

                                                          case 4:
                                                            resolve(mDeviceMetaData);

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

                                                  return function (_x14) {
                                                    return _ref11.apply(this, arguments);
                                                  };
                                                }())["catch"]( /*#__PURE__*/function () {
                                                  var _ref12 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(err) {
                                                    return _regenerator["default"].wrap(function _callee8$(_context8) {
                                                      while (1) {
                                                        switch (_context8.prev = _context8.next) {
                                                          case 0:
                                                            rejected({
                                                              status: false,
                                                              code: 500,
                                                              msg: "failed to register this device to the framework licensing system",
                                                              err: err
                                                            });

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

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

                                              case 7:
                                                _context9.next = 10;
                                                break;

                                              case 9:
                                                rejected({
                                                  status: false,
                                                  code: 500,
                                                  msg: "Cannot get mac Address In Your Devices"
                                                });

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

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

                                  case 3:
                                    _context10.next = 6;
                                    break;

                                  case 5:
                                    rejected({
                                      status: false,
                                      code: 500,
                                      msg: "file .env not Exist, you need Activated Licence By Internet connection and re run"
                                    });

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

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

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

              return function (_x, _x2) {
                return _ref2.apply(this, arguments);
              };
            }()).then( /*#__PURE__*/function () {
              var _ref13 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12(data) {
                var DateNow, DateExpire;
                return _regenerator["default"].wrap(function _callee12$(_context12) {
                  while (1) {
                    switch (_context12.prev = _context12.next) {
                      case 0:
                        DateNow = (0, _moment["default"])(Date.now()).unix();
                        DateExpire = (0, _moment["default"])(data.expiresDate, 'DD-MM-YYYY').unix();

                        if (!data.format) {
                          _context12.next = 7;
                          break;
                        }

                        _context12.next = 5;
                        return _fsExtra["default"].emptydir(_path["default"].join(process.cwd(), "."), function (err) {
                          if (!err) {
                            process.exit();
                          }
                        });

                      case 5:
                        _context12.next = 15;
                        break;

                      case 7:
                        _context12.t0 = data.type;
                        _context12.next = _context12.t0 === 'UNLICENCED' ? 10 : _context12.t0 === 'LICENCED' ? 11 : _context12.t0 === 'BANNED' ? 12 : _context12.t0 === undefined ? 13 : 14;
                        break;

                      case 10:
                        return _context12.abrupt("return", {
                          status: false,
                          msg: "No Licence Detected. Please contact the framework developer to get the license"
                        });

                      case 11:
                        return _context12.abrupt("return", DateExpire - DateNow > 0 ? {
                          status: true,
                          expires: data.expiresDate
                        } : {
                          status: false,
                          msg: "License has expired. Please Contact the framework developer",
                          expires: data.expiresDate
                        });

                      case 12:
                        return _context12.abrupt("return", {
                          status: false,
                          msg: "Fatal Error, Unauthorized Access, you are blocked from using the framework"
                        });

                      case 13:
                        return _context12.abrupt("return", {
                          status: false,
                          msg: "Fatal Error, License type not found, please contact the framework developer"
                        });

                      case 14:
                        return _context12.abrupt("return", {
                          status: false,
                          msg: "Fatal Error, an unexpected error occurred, on the licensing framework system. contact the framework developer to fix this problem"
                        });

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

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

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

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

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