"use strict";

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

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

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

function withDynamoPagination(Fetcher) {
  return (
    /*#__PURE__*/
    function (_Fetcher) {
      (0, _inherits2.default)(_class, _Fetcher);

      function _class() {
        var _getPrototypeOf2;

        var _this;

        (0, _classCallCheck2.default)(this, _class);

        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(_class)).call.apply(_getPrototypeOf2, [this].concat(args))); // just pass thru the entire fetch Response object

        _this.asyncResponseHandler = function (response) {
          return response;
        };

        return _this;
      }

      (0, _createClass2.default)(_class, [{
        key: "paginatedGet",
        value: function () {
          var _paginatedGet = (0, _asyncToGenerator2.default)(
          /*#__PURE__*/
          _regenerator.default.mark(function _callee(url) {
            var _this2 = this;

            var _ref,
                actionCreator,
                lastEvaluatedHashKey,
                lastEvaluatedRangeKey,
                responses,
                ancillary,
                response,
                resolvedResponses,
                _args = arguments;

            return _regenerator.default.wrap(function _callee$(_context) {
              while (1) {
                switch (_context.prev = _context.next) {
                  case 0:
                    _ref = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}, actionCreator = _ref.actionCreator;
                    responses = [];
                    ancillary = [];

                  case 3:
                    _context.next = 5;
                    return this.get(url, {
                      lastEvaluatedHashKey: lastEvaluatedHashKey,
                      lastEvaluatedRangeKey: lastEvaluatedRangeKey
                    });

                  case 5:
                    response = _context.sent;
                    responses.push(response); // we handle the json parsing and dispatching asynchronously so that
                    // we can keep making network requests until pagination is exhausted

                    ancillary.push(actionCreator ? response.json().then(function (json) {
                      return _this2.dispatch(actionCreator(json));
                    }) : response.json());
                    lastEvaluatedHashKey = response.headers.get('X-Last-Evaluated-Hash-Key');
                    lastEvaluatedRangeKey = response.headers.get('X-Last-Evaluated-Range-Key');

                  case 10:
                    if (lastEvaluatedHashKey && lastEvaluatedRangeKey) {
                      _context.next = 3;
                      break;
                    }

                  case 11:
                    _context.next = 13;
                    return Promise.all(ancillary);

                  case 13:
                    resolvedResponses = _context.sent;

                    if (!actionCreator) {
                      _context.next = 18;
                      break;
                    }

                    return _context.abrupt("return", responses);

                  case 18:
                    return _context.abrupt("return", resolvedResponses.reduce(function (res, acc) {
                      return [].concat((0, _toConsumableArray2.default)(res), (0, _toConsumableArray2.default)(acc));
                    }, []));

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

          function paginatedGet(_x) {
            return _paginatedGet.apply(this, arguments);
          }

          return paginatedGet;
        }()
      }]);
      return _class;
    }(Fetcher)
  );
}