"use strict";

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

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

var _API = _interopRequireDefault(require("./API"));

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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; }

var Repository =
/*#__PURE__*/
function () {
  function Repository(tablename, token) {
    var filters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

    _classCallCheck(this, Repository);

    this.tablename = tablename;
    this.token = token;
    this.filters = filters.map(function (item) {
      return _objectSpread({}, item, {
        options: item.values
      });
    });
    this.API = new _API.default('/' + this.tablename, token);
    this.renderFilters();
  }

  _createClass(Repository, [{
    key: "setToken",
    value: function setToken(token) {
      this.API.token = token;
      this.token = token;
    }
  }, {
    key: "setFilters",
    value: function setFilters(fil) {
      if (!fil) return;
      this.filters = fil;
      this.renderFilters();
    }
  }, {
    key: "renderFilters",
    value: function renderFilters() {
      var fils = [];
      this.filters.forEach(function (item) {
        if (item.values && item.values.length || ["ascending", "descending"].includes(item.type)) {
          fils.push(item);
        }
      });
      this.renderedFilters = JSON.stringify(fils);
    }
  }, {
    key: "count",
    value: function count() {
      return this.API.get("/count?filters=".concat(this.renderedFilters));
    }
  }, {
    key: "getPage",
    value: function getPage(page) {
      var perPage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
      return this.API.get("?page=".concat(page, "&perPage=").concat(perPage, "&filters=").concat(this.renderedFilters));
    }
  }, {
    key: "get",
    value: function get(id) {
      return this.API.get('/' + id);
    }
  }, {
    key: "update",
    value: function update(id, obj) {
      return this.API.put('/' + id, obj);
    }
  }, {
    key: "add",
    value: function add(obj) {
      return this.API.post('', obj);
    }
  }, {
    key: "delete",
    value: function _delete(id) {
      return this.API.delete('/' + id);
    }
  }, {
    key: "import",
    value: function _import(data) {
      return this.API.post('/import', data);
    }
  }, {
    key: "model",
    value: function model() {
      return this.API.get('/Model');
    }
  }]);

  return Repository;
}();

var _default = Repository;
exports.default = _default;