"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 _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

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

var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken"));

var _fs = _interopRequireDefault(require("fs"));

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

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

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

var mJwt = function mJwt(config) {
  var _this = this;

  (0, _classCallCheck2["default"])(this, mJwt);
  (0, _defineProperty2["default"])(this, "token", /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(payload) {
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              return _context3.abrupt("return", new Promise( /*#__PURE__*/function () {
                var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(resolve, rejected) {
                  var encr, err2;
                  return _regenerator["default"].wrap(function _callee2$(_context2) {
                    while (1) {
                      switch (_context2.prev = _context2.next) {
                        case 0:
                          encr = new _indexModule.Security.Encryption.Crypto();
                          err2 = encr.encode(payload);
                          console.log(err2);
                          _context2.next = 5;
                          return _jsonwebtoken["default"].sign(err2, _this.config.secretKey, _this.config.signOption, /*#__PURE__*/function () {
                            var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(error, token) {
                              return _regenerator["default"].wrap(function _callee$(_context) {
                                while (1) {
                                  switch (_context.prev = _context.next) {
                                    case 0:
                                      if (error) {
                                        _context.next = 5;
                                        break;
                                      }

                                      _context.next = 3;
                                      return resolve({
                                        status: true,
                                        msg: "Successfully to Create Token",
                                        data: token
                                      });

                                    case 3:
                                      _context.next = 7;
                                      break;

                                    case 5:
                                      _context.next = 7;
                                      return rejected({
                                        status: false,
                                        msg: "Error, Failed to Create Token",
                                        error: error
                                      });

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

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

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

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

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

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }());
  (0, _defineProperty2["default"])(this, "verify", /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(token) {
      return _regenerator["default"].wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              return _context6.abrupt("return", new Promise( /*#__PURE__*/function () {
                var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(resolve, rejected) {
                  return _regenerator["default"].wrap(function _callee5$(_context5) {
                    while (1) {
                      switch (_context5.prev = _context5.next) {
                        case 0:
                          _context5.next = 2;
                          return _jsonwebtoken["default"].verify(token, _this.config.secretKey, _this.config.signOption, /*#__PURE__*/function () {
                            var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(error, token) {
                              return _regenerator["default"].wrap(function _callee4$(_context4) {
                                while (1) {
                                  switch (_context4.prev = _context4.next) {
                                    case 0:
                                      if (error) {
                                        _context4.next = 5;
                                        break;
                                      }

                                      _context4.next = 3;
                                      return resolve({
                                        status: true,
                                        msg: "Successfully to Create Token",
                                        data: token
                                      });

                                    case 3:
                                      _context4.next = 7;
                                      break;

                                    case 5:
                                      _context4.next = 7;
                                      return rejected({
                                        status: false,
                                        msg: "Error, Failed to Create Token",
                                        error: error
                                      });

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

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

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

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

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

    return function (_x6) {
      return _ref4.apply(this, arguments);
    };
  }());
  this.config = _lodash["default"].extend({
    secretKey: _fs["default"].readFileSync(_path["default"].join(__dirname, "./key/private.key")),
    signOption: {
      algorithm: "HS256",
      expiresIn: 1288
    }
  });
};

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