"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 _qs = _interopRequireDefault(require("qs"));

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

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

    (0, _classCallCheck2["default"])(this, BNI);
    (0, _defineProperty2["default"])(this, "accessToken", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
        var clientId,
            clientSecret,
            _args = arguments;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                clientId = _args.length > 0 && _args[0] !== undefined ? _args[0] : _this.config.client_id;
                clientSecret = _args.length > 1 && _args[1] !== undefined ? _args[1] : _this.config.client_secret;
                return _context.abrupt("return", _axios["default"].post("".concat(_this.config.state, "/api/oauth/token"), _qs["default"].stringify({
                  client_id: clientId,
                  client_secret: clientSecret
                }), {
                  headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    Authorization: "Basic ".concat(Buffer.from("".concat(clientId, ":").concat(clientSecret)), ")")
                  },
                  params: {
                    grant_type: 'client_credentials'
                  }
                }));

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

      return function () {
        return _ref.apply(this, arguments);
      };
    }());
    this.config = _lodash["default"].extend({
      state: BNI.BNI_STATE_SANDBOX,
      cliend_id: null,
      client_secret: null
    }, config);
  }

  (0, _createClass2["default"])(BNI, null, [{
    key: "BNI_STATE_PRODUCTION",
    get: function get() {
      return "https://api.bni.co.id";
    }
  }, {
    key: "BNI_STATE_SANDBOX",
    get: function get() {
      return "https://apidev.bni.co.id";
    }
  }]);
  return BNI;
}();

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