"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 _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _map = _interopRequireDefault(require("lodash/map"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _Fetcher2 = _interopRequireDefault(require("./Fetcher"));

var _jsonify = _interopRequireDefault(require("./jsonify"));

var PaginatedFetcher =
/*#__PURE__*/
function (_Fetcher) {
  (0, _inherits2.default)(PaginatedFetcher, _Fetcher);

  function PaginatedFetcher() {
    var _this;

    var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};

    var getRecordIds = _ref.getRecordIds,
        args = (0, _objectWithoutProperties2.default)(_ref, ["getRecordIds"]);
    (0, _classCallCheck2.default)(this, PaginatedFetcher);
    _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(PaginatedFetcher).call(this, args));
    _this.asyncResponseHandler = _this.paginatedResponseHandler;

    _this.getRecordIds = getRecordIds || function (json) {
      return (0, _map.default)(json, 'id');
    };

    return _this;
  }

  (0, _createClass2.default)(PaginatedFetcher, [{
    key: "paginatedResponseHandler",
    value: function paginatedResponseHandler(response, request) {
      var _this2 = this;

      return (0, _jsonify.default)(response).then(function (json) {
        var total = parseInt(response.headers.get('total'), 10);
        var perPage = parseInt(response.headers.get('per-page'), 10);
        var paginationData = {
          totalPages: 1,
          records: _this2.getRecordIds(json)
        };
        if (perPage > 0) paginationData.totalPages = Math.ceil(total / perPage);
        return [json, paginationData];
      });
    }
  }, {
    key: "appendQueryParams",
    value: function appendQueryParams(url, params) {
      return (0, _get2.default)((0, _getPrototypeOf2.default)(PaginatedFetcher.prototype), "appendQueryParams", this).call(this, url, (0, _omit.default)(params, 'stateKey'));
    }
  }, {
    key: "fetch",
    value: function () {
      var _fetch = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee(url, params) {
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                return _context.abrupt("return", (0, _get2.default)((0, _getPrototypeOf2.default)(PaginatedFetcher.prototype), "fetch", this).call(this, url, (0, _omit.default)(params, 'stateKey')));

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

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

      return fetch;
    }()
  }]);
  return PaginatedFetcher;
}(_Fetcher2.default);

exports.default = PaginatedFetcher;