"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 _axios = _interopRequireDefault(require("axios"));

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

var SiCepat = /*#__PURE__*/function () {
  /**
   *
   * @param {{}} config Function For Configuration Api
   * @param {String} config.state Function For Configuration Mode and URL
   * @param {String} config.key Function For Configuration Key
   */
  function SiCepat(config) {
    var _this = this;

    (0, _classCallCheck2["default"])(this, SiCepat);
    (0, _defineProperty2["default"])(this, "LocationServices", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
        var mode,
            _args4 = arguments;
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                mode = _args4.length > 0 && _args4[0] !== undefined ? _args4[0] : SiCepat.SICEPAT_EXPEDITION_LOCATION_ORIGIN;
                return _context4.abrupt("return", new Promise( /*#__PURE__*/function () {
                  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(resolve, rejected) {
                    var url;
                    return _regenerator["default"].wrap(function _callee3$(_context3) {
                      while (1) {
                        switch (_context3.prev = _context3.next) {
                          case 0:
                            url = mode === SiCepat.SICEPAT_EXPEDITION_LOCATION_ORIGIN ? "".concat(_this.config.state, "customer/origin") : "".concat(_this.config.state, "customer/destination");

                            _axios["default"].get("".concat(url), {
                              headers: {
                                'api-key': _this.config.key
                              }
                            }).then( /*#__PURE__*/function () {
                              var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(res) {
                                return _regenerator["default"].wrap(function _callee$(_context) {
                                  while (1) {
                                    switch (_context.prev = _context.next) {
                                      case 0:
                                        resolve(res.data.sicepat);

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

                              return function (_x3) {
                                return _ref3.apply(this, arguments);
                              };
                            }())["catch"]( /*#__PURE__*/function () {
                              var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(error) {
                                return _regenerator["default"].wrap(function _callee2$(_context2) {
                                  while (1) {
                                    switch (_context2.prev = _context2.next) {
                                      case 0:
                                        rejected(error.response);

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

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

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

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

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

      return function () {
        return _ref.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "Price", /*#__PURE__*/function () {
      var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(origin, destination, weight) {
        return _regenerator["default"].wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                return _context8.abrupt("return", new Promise( /*#__PURE__*/function () {
                  var _ref6 = (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:
                            _axios["default"].get("".concat(_this.config.state, "customer/tariff"), {
                              headers: {
                                'api-key': _this.config.key
                              },
                              params: {
                                origin: origin,
                                destination: destination,
                                weight: weight
                              }
                            }).then( /*#__PURE__*/function () {
                              var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(res) {
                                return _regenerator["default"].wrap(function _callee5$(_context5) {
                                  while (1) {
                                    switch (_context5.prev = _context5.next) {
                                      case 0:
                                        resolve(res.data.sicepat);

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

                              return function (_x10) {
                                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:
                                        rejected(error.response.data.sicepat);

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

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

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

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

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

      return function (_x5, _x6, _x7) {
        return _ref5.apply(this, arguments);
      };
    }());
    this.config = _lodash["default"].extend({
      state: SiCepat.SICEPAT_EXPEDITION_DEVELOPMENT,
      key: null
    }, config);
  }

  (0, _createClass2["default"])(SiCepat, null, [{
    key: "SICEPAT_EXPEDITION_DEVELOPMENT",
    get: function get() {
      return 'http://apitrek.sicepat.com/';
    }
  }, {
    key: "SICEPAT_EXPEDITION_PRODUCTION",
    get: function get() {
      return 'http://apitrek.sicepat.com/';
    }
  }, {
    key: "SICEPAT_EXPEDITION_LOCATION_ORIGIN",
    get: function get() {
      return 'origin';
    }
  }, {
    key: "SICEPAT_EXPEDITION_LOCATION_DESTINATION",
    get: function get() {
      return 'destination';
    }
  }]);
  return SiCepat;
}();

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