'use strict';

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

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _get2 = require('babel-runtime/helpers/get');

var _get3 = _interopRequireDefault(_get2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _map = require('lodash/map');

var _map2 = _interopRequireDefault(_map);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _Fetcher2 = require('./Fetcher');

var _Fetcher3 = _interopRequireDefault(_Fetcher2);

var _jsonify = require('./jsonify');

var _jsonify2 = _interopRequireDefault(_jsonify);

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

var PaginatedFetcher = function (_Fetcher) {
  (0, _inherits3.default)(PaginatedFetcher, _Fetcher);

  function PaginatedFetcher() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var getRecordIds = _ref.getRecordIds,
        args = (0, _objectWithoutProperties3.default)(_ref, ['getRecordIds']);
    (0, _classCallCheck3.default)(this, PaginatedFetcher);

    var _this = (0, _possibleConstructorReturn3.default)(this, (PaginatedFetcher.__proto__ || Object.getPrototypeOf(PaginatedFetcher)).call(this, args));

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

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

      return (0, _jsonify2.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, _get3.default)(PaginatedFetcher.prototype.__proto__ || Object.getPrototypeOf(PaginatedFetcher.prototype), 'appendQueryParams', this).call(this, url, (0, _omit2.default)(params, 'stateKey'));
    }
  }, {
    key: 'fetch',
    value: function () {
      var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(url, params) {
        return _regenerator2.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                return _context.abrupt('return', (0, _get3.default)(PaginatedFetcher.prototype.__proto__ || Object.getPrototypeOf(PaginatedFetcher.prototype), 'fetch', this).call(this, url, (0, _omit2.default)(params, 'stateKey')));

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

      function fetch(_x2, _x3) {
        return _ref2.apply(this, arguments);
      }

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

exports.default = PaginatedFetcher;