"use strict";

require("core-js/modules/es.array.is-array.js");

require("core-js/modules/es.symbol.js");

require("core-js/modules/es.symbol.description.js");

require("core-js/modules/es.symbol.iterator.js");

require("core-js/modules/es.array.from.js");

require("core-js/modules/es.array.slice.js");

require("core-js/modules/es.function.name.js");

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.object.define-property.js");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.batchedGet = batchedGet;
exports.chunk = chunk;
exports.cleanUpQueryParams = cleanUpQueryParams;
exports.slice = slice;

require("regenerator-runtime/runtime.js");

require("core-js/modules/es.array.map.js");

require("core-js/modules/es.array.for-each.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/web.dom-collections.for-each.js");

require("core-js/modules/es.object.keys.js");

require("core-js/modules/es.array.join.js");

require("core-js/modules/es.array.iterator.js");

require("core-js/modules/es.promise.js");

require("core-js/modules/es.string.iterator.js");

require("core-js/modules/web.dom-collections.iterator.js");

require("core-js/modules/es.array.flat.js");

require("core-js/modules/es.array.unscopables.flat.js");

var _errors = require("./errors");

var _server = require("./server");

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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function slice(array) {
  var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : array.length;
  var length = array.length;

  if (!length) {
    return [];
  }

  if (start < 0) {
    start = -start > length ? 0 : length + start;
  }

  end = end > length ? length : end;

  if (end < 0) {
    end += length;
  }

  length = start > end ? 0 : end - start >>> 0;
  start >>>= 0;
  return _toConsumableArray(new Array(length)).map(function (_, idx) {
    return array[idx + start];
  });
}
/**
 * Creates an array of elements split into groups the length of `size`.
 * If `array` can't be split evenly, the final chunk will be the remaining
 * elements.
 *
 * @category Array
 * @param {Array} array The array to process.
 * @param {number} [size=1] The length of each chunk
 * @returns {Array} Returns the new array of chunks.
 * @example
 *
 * chunk(['a', 'b', 'c', 'd'], 2)
 * // => [['a', 'b'], ['c', 'd']]
 *
 * chunk(['a', 'b', 'c', 'd'], 3)
 * // => [['a', 'b', 'c'], ['d']]
 */


function chunk(array) {
  var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  size = Math.max(size, 0);
  var length = array.length;

  if (!length || size < 1) {
    return [];
  }

  return _toConsumableArray(new Array(Math.ceil(length / size))).map(function (_, idx) {
    return slice(array, idx * size, (idx + 1) * size);
  });
}
/**
 * Removes empty values from query params.
 * @param queryParams Query parameters object.
 */


function cleanUpQueryParams(queryParams) {
  Object.keys(queryParams).forEach(function (key) {
    if (queryParams[key] === undefined) delete queryParams[key];
  });
}

function batchedGet(_x, _x2, _x3) {
  return _batchedGet.apply(this, arguments);
}

function _batchedGet() {
  _batchedGet = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(toBatch, batchKey, path) {
    var batchSize,
        server,
        batches,
        fetchAndError,
        processedBatches,
        _args2 = arguments;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            batchSize = _args2.length > 3 && _args2[3] !== undefined ? _args2[3] : 60;
            server = new _server.APIServer();
            batches = chunk(toBatch, batchSize);

            fetchAndError = /*#__PURE__*/function () {
              var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(batch) {
                var response;
                return regeneratorRuntime.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        _context.next = 2;
                        return server.get(path, undefined, _defineProperty({}, batchKey, batch.join()));

                      case 2:
                        response = _context.sent;

                        if (!response.error) {
                          _context.next = 5;
                          break;
                        }

                        throw new _errors.APIResponseError("Error while making batched get ".concat(batch), response);

                      case 5:
                        return _context.abrupt("return", response.data);

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

              return function fetchAndError(_x4) {
                return _ref.apply(this, arguments);
              };
            }();

            _context2.next = 6;
            return Promise.all(batches.map(function (batch) {
              return fetchAndError(batch);
            }));

          case 6:
            processedBatches = _context2.sent;
            return _context2.abrupt("return", processedBatches.flat());

          case 8:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _batchedGet.apply(this, arguments);
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 