"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = require("react");
var _utils = require("../utils");
var _useViewState3 = _interopRequireDefault(require("./useViewState"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var scrollTableRef = function scrollTableRef(tableRef) {
  if (!(tableRef != null && tableRef.current)) {
    console.error("[useInMemoryPaging]: tableRef was not specified or tableRef.current is null.");
  } else {
    tableRef.current.scrollToTop();
  }
};
var useInMemoryPaging = function useInMemoryPaging(_ref) {
  var _viewState$filters, _viewState$sorting, _viewState$currentPag, _viewState$nextPageTo;
  var viewStateName = _ref.viewStateName,
    tableRef = _ref.tableRef,
    records = _ref.records,
    pageSize = _ref.pageSize,
    _ref$initialSort = _ref.initialSort,
    initialSort = _ref$initialSort === void 0 ? {} : _ref$initialSort,
    _ref$initialFilters = _ref.initialFilters,
    initialFilters = _ref$initialFilters === void 0 ? {} : _ref$initialFilters,
    sortAndFilterFn = _ref.sortAndFilterFn;
  var _useViewState = (0, _useViewState3.default)(viewStateName),
    _useViewState2 = _slicedToArray(_useViewState, 2),
    viewState = _useViewState2[0],
    updateViewState = _useViewState2[1];
  var internalInitialSort = (0, _react.useRef)(initialSort);
  var internalInitialFilters = (0, _react.useRef)(initialFilters);
  var filters = (_viewState$filters = viewState.filters) != null ? _viewState$filters : internalInitialFilters.current;
  var sorting = (_viewState$sorting = viewState.sorting) != null ? _viewState$sorting : internalInitialSort.current;
  var currentPage = (_viewState$currentPag = viewState.currentPage) != null ? _viewState$currentPag : 1;
  var nextPageToLoad = (_viewState$nextPageTo = viewState.nextPageToLoad) != null ? _viewState$nextPageTo : 1;
  var setFilter = function setFilter(filters) {
    scrollTableRef(tableRef);
    updateViewState("filters", filters);
    updateViewState("currentPage", 1);
    updateViewState("nextPageToLoad", 1);
  };
  var setSort = function setSort(sorting) {
    scrollTableRef(tableRef);
    updateViewState("sorting", sorting);
    updateViewState("currentPage", 1);
    updateViewState("nextPageToLoad", 1);
  };
  var scrollLoader = (0, _react.useCallback)(function (page) {
    if (page > nextPageToLoad) {
      updateViewState("currentPage", page);
      updateViewState("nextPageToLoad", page);
    }
  }, [nextPageToLoad, updateViewState]);
  var _useMemo = (0, _react.useMemo)(function () {
      var list = sortAndFilterFn({
        list: (0, _utils.unwrapImmutable)(records),
        filters: filters,
        sorting: sorting
      });
      return [(0, _utils.unwrapImmutable)(list.slice(0, currentPage * pageSize)), list.length];
    }, [currentPage, pageSize, records, filters, sorting, sortAndFilterFn]),
    _useMemo2 = _slicedToArray(_useMemo, 2),
    rows = _useMemo2[0],
    totalCount = _useMemo2[1];
  return {
    rows: rows,
    scrollLoader: scrollLoader,
    currentPage: currentPage,
    filters: filters,
    sorting: sorting,
    setFilter: setFilter,
    setSort: setSort,
    totalCount: totalCount
  };
};
__signature__(useInMemoryPaging, "useViewState{[viewState, updateViewState]}\nuseRef{internalInitialSort}\nuseRef{internalInitialFilters}\nuseCallback{scrollLoader}\nuseMemo{[rows, totalCount]}", function () {
  return [_useViewState3.default];
});
__signature__(useInMemoryPaging, "useViewState{[viewState, updateViewState]}\nuseRef{internalInitialSort}\nuseRef{internalInitialFilters}\nuseCallback{scrollLoader}\nuseMemo{[rows, totalCount]}", function () {
  return [_useViewState3.default];
});
var _default = useInMemoryPaging;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(scrollTableRef, "scrollTableRef", "/home/vsts/work/1/s/src/hooks/useInMemoryPaging.js");
  reactHotLoader.register(useInMemoryPaging, "useInMemoryPaging", "/home/vsts/work/1/s/src/hooks/useInMemoryPaging.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/hooks/useInMemoryPaging.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(scrollTableRef, "scrollTableRef", "/home/vsts/work/1/s/src/hooks/useInMemoryPaging.js");
  reactHotLoader.register(useInMemoryPaging, "useInMemoryPaging", "/home/vsts/work/1/s/src/hooks/useInMemoryPaging.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/hooks/useInMemoryPaging.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();