"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function checkIt(resp) {
  if (parseInt(resp.status / 100, 10) === 2 || parseInt(resp.status / 100, 10) === 3) {
    return resp;
  } else {
    throw resp;
  }
}

function catchIt(err) {
  if (err.message === "Unexpected token O in JSON at position 0") {
    return "OK";
  }

  if (parseInt(err.status / 100, 10) === 4) {
    throw err;
  } else if (parseInt(err.status / 100, 10) === 5) {
    throw err;
  } else {
    return err;
  }
}

var API =
/*#__PURE__*/
function () {
  function API() {
    var baseURL = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var token = arguments.length > 1 ? arguments[1] : undefined;

    _classCallCheck(this, API);

    // if (process.env.NODE_ENV === 'production' && !baseURL.includes('http')) {
    baseURL = "/api/" + baseURL; // }

    this.baseURL = baseURL;
    this.token = token;
  }

  _createClass(API, [{
    key: "setToken",
    value: function setToken(t) {
      this.token = t;
    }
  }, {
    key: "get",
    value: function get(url) {
      var urb = this.baseURL + url;
      return window.fetch(urb, {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json',
          'Token': this.token
        }
      }).then(checkIt).then(function (r) {
        return r.json();
      }).catch(catchIt);
    }
  }, {
    key: "post",
    value: function post(url, body) {
      var urb = this.baseURL + url;
      return window.fetch(urb, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Token': this.token
        },
        body: JSON.stringify(body)
      }).then(checkIt).then(function (r) {
        return r.json();
      }).catch(catchIt);
    }
  }, {
    key: "put",
    value: function put(url, body) {
      var urb = this.baseURL + url;
      return window.fetch(urb, {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json',
          'Token': this.token
        },
        body: JSON.stringify(body)
      }).then(checkIt).then(function (r) {
        return r.json();
      }).catch(catchIt);
    }
  }, {
    key: "delete",
    value: function _delete(url) {
      var urb = this.baseURL + url;
      return window.fetch(urb, {
        method: 'DELETE',
        headers: {
          'Content-Type': 'application/json',
          'Token': this.token
        }
      }).then(checkIt).then(function (r) {
        return r.json();
      }).catch(catchIt);
    }
  }]);

  return API;
}();

var _default = API;
exports.default = _default;