"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isCancelError = exports["default"] = exports.Api = void 0;
var _axios = _interopRequireDefault(require("axios"));
var _qs = _interopRequireDefault(require("qs"));
var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var CancelToken = _axios["default"].CancelToken;
var source = CancelToken.source();
var ax = _axios["default"].create({
  cancelToken: source.token,
  paramsSerializer: function paramsSerializer(params) {
    return _qs["default"].stringify(params, {
      arrayFormat: 'brackets'
    });
  }
});
var Api = exports.Api = /*#__PURE__*/function () {
  function Api() {
    _classCallCheck(this, Api);
  }
  _createClass(Api, null, [{
    key: "request",
    value: function request(config, successCb, failCb) {
      var promise = ax.request(config);
      if ((0, _isFunction["default"])(successCb)) {
        promise.then(successCb);
      }
      if ((0, _isFunction["default"])(failCb)) {
        promise["catch"](failCb);
      }
      return promise;
    }
  }, {
    key: "cancel",
    value: function cancel() {
      source.cancel();
      source = CancelToken.source();
      ax.defaults.cancelToken = source.token;
    }
  }, {
    key: "setLanguage",
    value: function setLanguage(language) {
      var formattedLanguage = language.replace('-', '_') || 'de_DE';
      if (ax.defaults.params && ax.defaults.params._language === formattedLanguage) {
        // already set, nothing to do
        return 0;
      } else {
        ax.defaults.params = _objectSpread(_objectSpread({}, ax.defaults.params), {}, {
          _language: formattedLanguage
        });
        return 1;
      }
    }
  }, {
    key: "setChannel",
    value: function setChannel(channel) {
      if (ax.defaults.params && ax.defaults.params._channel === channel) {
        // already set, nothing to do
        return 0;
      } else {
        ax.defaults.params = _objectSpread(_objectSpread({}, ax.defaults.params), {}, {
          _channel: channel
        });
        return 1;
      }
    }

    /**
     * Sets a token in the request params
     * @param token
     */
  }, {
    key: "setToken",
    value: function setToken(token) {
      ax.defaults.params = _objectSpread(_objectSpread({}, ax.defaults.params), {}, {
        _token: token
      });
    }

    /**
     * Sets the Authorization header, or removes it if token is falsy
     * @param token
     */
  }, {
    key: "setTokenHeader",
    value: function setTokenHeader(token) {
      Api.setHeader('Authorization', token ? "Bearer ".concat(token) : false);
    }

    /**
     * Sets the Cache-Control header, or removes it if value is falsy
     * @param value
     */
  }, {
    key: "setCacheControlHeader",
    value: function setCacheControlHeader(value) {
      Api.setHeader('Cache-Control', value);
    }

    /**
     * Set a value to the specified header key, or remove it if value is falsy
     * @param key
     * @param value
     */
  }, {
    key: "setHeader",
    value: function setHeader(key, value) {
      if (value) {
        ax.defaults.headers = _objectSpread(_objectSpread({}, ax.defaults.headers), {}, _defineProperty({}, key, value));
      } else {
        delete ax.defaults.headers[key];
      }
    }
  }, {
    key: "setGETParameter",
    value: function setGETParameter(parameter, value) {
      ax.defaults.params = _objectSpread({}, ax.defaults.params);
      ax.defaults.params[parameter] = value;
    }
  }, {
    key: "getGETParameters",
    value: function getGETParameters() {
      return _objectSpread({}, ax.defaults.params);
    }
  }, {
    key: "setBaseURL",
    value: function setBaseURL(url) {
      ax.defaults.baseURL = url;
    }
  }, {
    key: "init",
    value: function init(config) {
      for (var key in config) {
        ax.defaults[key] = config[key];
      }
    }
  }]);
  return Api;
}();
var isCancelError = exports.isCancelError = function isCancelError(error) {
  return error instanceof _axios["default"].Cancel;
};
var _default = exports["default"] = Api;