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

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

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

var _midtransClient = _interopRequireDefault(require("midtrans-client"));

var MidTrans = /*#__PURE__*/function () {
  function MidTrans(config) {
    var _this = this;

    (0, _classCallCheck2["default"])(this, MidTrans);
    (0, _defineProperty2["default"])(this, "charge", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(data) {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                return _context2.abrupt("return", new Promise( /*#__PURE__*/function () {
                  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(resolve, rejected) {
                    return _regenerator["default"].wrap(function _callee$(_context) {
                      while (1) {
                        switch (_context.prev = _context.next) {
                          case 0:
                            _context.t0 = _this.config.engine;
                            _context.next = _context.t0 === MidTrans.ENGINE_MIDTRANS_CORE_API ? 3 : _context.t0 === MidTrans.ENGINE_MIDTRANS_CORE_SNAP ? 5 : 7;
                            break;

                          case 3:
                            _this.INSTANCE.charge(data).then(resolve)["catch"](rejected);

                            return _context.abrupt("break", 8);

                          case 5:
                            _this.INSTANCE.createTransaction(data).then(resolve)["catch"](rejected);

                            return _context.abrupt("break", 8);

                          case 7:
                            _this.INSTANCE.charge(data).then(resolve)["catch"](rejected);

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

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

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

      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }());
    this.config = _lodash["default"].extend({
      version: 2,
      engine: MidTrans.ENGINE_MIDTRANS_CORE_API,
      isProduction: false,
      serverKey: null,
      clientKey: null
    }, config);

    switch (this.config.engine) {
      case MidTrans.ENGINE_MIDTRANS_CORE_API:
        this.INSTANCE = new _midtransClient["default"].CoreApi(this.config);
        break;

      case MidTrans.ENGINE_MIDTRANS_CORE_SNAP:
        this.INSTANCE = new _midtransClient["default"].Snap(this.config);
        break;

      default:
        this.INSTANCE = new _midtransClient["default"].CoreApi(this.config);
    }
  }

  (0, _createClass2["default"])(MidTrans, null, [{
    key: "ENGINE_MIDTRANS_CORE_API",
    get: function get() {
      return 1;
    }
  }, {
    key: "ENGINE_MIDTRANS_CORE_SNAP",
    get: function get() {
      return 2;
    }
  }, {
    key: "STATE_MIDTRANS_SANDBOX",
    get: function get() {
      return "https://api.sandbox.midtrans.com/";
    }
  }, {
    key: "STATE_MIDTRANS_PRODUCTION",
    get: function get() {
      return "https://api.midtrans.com/";
    }
  }]);
  return MidTrans;
}();

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