"use strict";

exports.__esModule = true;
exports.useListState = exports.default = exports.Table = exports.ROW_HEIGHT = exports.PlaceholderCell = exports.PlaceholderBox = exports.Placeholder = exports.List = exports.HEADER_HEIGHT = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _recompose = require("recompose");
var _utils = require("../../utils");
var _withScrollBox = _interopRequireDefault(require("../../hocs/withScrollBox"));
var _withInfiniteScroll = _interopRequireDefault(require("../../hocs/withInfiniteScroll"));
var _Row = _interopRequireDefault(require("./Row"));
var _HeadRow = _interopRequireDefault(require("./HeadRow"));
var _useViewState3 = _interopRequireDefault(require("../../hooks/useViewState"));
var _enhanceColumnDefs = _interopRequireDefault(require("./enhanceColumnDefs"));
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 PlaceholderCell = exports.PlaceholderCell = _styledComponents.default.div.withConfig({
  displayName: "List__PlaceholderCell",
  componentId: "sc-dohdkx-0"
})(["height:", "px;display:flex;justify-content:center;"], /* istanbul ignore next*/function (props) {
  return props.cssHeight || 100;
});
var PlaceholderBox = exports.PlaceholderBox = _styledComponents.default.div.withConfig({
  displayName: "List__PlaceholderBox",
  componentId: "sc-dohdkx-1"
})(["padding-top:30px;width:100%;margin:auto;"]);
var Placeholder = exports.Placeholder = function Placeholder(_ref) {
  var width = _ref.width,
    height = _ref.height,
    children = _ref.children;
  return /*#__PURE__*/_react.default.createElement("tr", null, /*#__PURE__*/_react.default.createElement("td", {
    colSpan: width,
    style: {
      padding: 0
    }
  }, /*#__PURE__*/_react.default.createElement(PlaceholderCell, {
    cssHeight: height
  }, /*#__PURE__*/_react.default.createElement(PlaceholderBox, null, children))));
};
Placeholder.displayName = "Placeholder";
var Table = exports.Table = _styledComponents.default.table.withConfig({
  displayName: "List__Table",
  componentId: "sc-dohdkx-2"
})(["border-spacing:0;table-layout:fixed;width:100%;font-size:13px;"]);
var HEADER_HEIGHT = exports.HEADER_HEIGHT = 41;
var ROW_HEIGHT = exports.ROW_HEIGHT = 51;
var calculateVirtualization = function calculateVirtualization(virtual, scrollTop, scrollBuffer, height, rows) {
  var virtualFilter, heightAbove, heightBelow;
  if (virtual) {
    var correctedScrollTop = scrollTop - HEADER_HEIGHT; // Subtract header height
    var firstIn = Math.floor(correctedScrollTop / ROW_HEIGHT);
    var firstShow = Math.max(firstIn - scrollBuffer, 0);
    var lastIn = Math.ceil((correctedScrollTop + height) / ROW_HEIGHT);
    var lastShow = Math.min(lastIn + scrollBuffer, rows.length);
    virtualFilter = function virtualFilter(index) {
      return index >= firstShow && index < lastShow;
    };
    heightAbove = firstShow * ROW_HEIGHT;
    heightBelow = (rows.length - lastShow) * ROW_HEIGHT;
  } else {
    virtualFilter = function virtualFilter() {
      return true;
    };
    heightAbove = 0;
    heightBelow = 0;
  }
  return {
    virtualFilter: virtualFilter,
    heightAbove: heightAbove,
    heightBelow: heightBelow
  };
};
var useListState = exports.useListState = function useListState(name, columnDefs) {
  var _useViewState = (0, _useViewState3.default)(name),
    _useViewState2 = _slicedToArray(_useViewState, 2),
    viewState = _useViewState2[0],
    updateViewState = _useViewState2[1];
  var _viewState$selection = viewState.selection,
    selection = _viewState$selection === void 0 ? [] : _viewState$selection,
    _viewState$sorting = viewState.sorting,
    sorting = _viewState$sorting === void 0 ? {} : _viewState$sorting;
  var enhancedColumnDefs = (0, _enhanceColumnDefs.default)(sorting, selection, updateViewState, columnDefs);
  return [enhancedColumnDefs, selection];
};
__signature__(useListState, "useViewState{[viewState, updateViewState]}", function () {
  return [_useViewState3.default];
});
__signature__(useListState, "useViewState{[viewState, updateViewState]}", function () {
  return [_useViewState3.default];
});
var List = exports.List = function List(_ref2) {
  var name = _ref2.name,
    _ref2$columnDefs = _ref2.columnDefs,
    columnDefs = _ref2$columnDefs === void 0 ? [] : _ref2$columnDefs,
    _ref2$rows = _ref2.rows,
    rows = _ref2$rows === void 0 ? [] : _ref2$rows,
    rowOnClick = _ref2.rowOnClick,
    placeholder = _ref2.placeholder,
    _ref2$keyField = _ref2.keyField,
    keyField = _ref2$keyField === void 0 ? ["id"] : _ref2$keyField,
    _ref2$virtual = _ref2.virtual,
    virtual = _ref2$virtual === void 0 ? false : _ref2$virtual,
    _ref2$scrollTop = _ref2.scrollTop,
    scrollTop = _ref2$scrollTop === void 0 ? 0 : _ref2$scrollTop,
    _ref2$height = _ref2.height,
    height = _ref2$height === void 0 ? 300 : _ref2$height,
    _ref2$scrollBuffer = _ref2.scrollBuffer,
    scrollBuffer = _ref2$scrollBuffer === void 0 ? 10 : _ref2$scrollBuffer,
    _ref2$rowBackgroundGe = _ref2.rowBackgroundGetter,
    rowBackgroundGetter = _ref2$rowBackgroundGe === void 0 ? function () {} : _ref2$rowBackgroundGe;
  var _useListState = useListState(name, columnDefs),
    _useListState2 = _slicedToArray(_useListState, 2),
    enhancedColumnDefs = _useListState2[0],
    selection = _useListState2[1];
  if (columnDefs.length === 0) return null;
  var rowIds = [],
    rowElements = [];
  var _calculateVirtualizat = calculateVirtualization(virtual, scrollTop, scrollBuffer, height, rows),
    virtualFilter = _calculateVirtualizat.virtualFilter,
    heightAbove = _calculateVirtualizat.heightAbove,
    heightBelow = _calculateVirtualizat.heightBelow;
  rows.forEach(function (row, index) {
    var id = _utils.safeGet.apply(void 0, [row].concat(keyField)) + ""; // Ensure rowId is string
    rowIds.push(id);
    if (!virtualFilter(index)) return;
    rowElements.push(/*#__PURE__*/_react.default.createElement(_Row.default, {
      columnDefs: enhancedColumnDefs,
      key: id,
      rowId: id,
      row: row,
      rowOnClick: rowOnClick,
      selected: selection.indexOf(id) !== -1,
      bgColor: rowBackgroundGetter(row, index)
    }));
  });
  if (virtual && heightAbove) {
    rowElements.unshift(/*#__PURE__*/_react.default.createElement("tr", {
      key: "virtualAbove",
      style: {
        height: heightAbove
      }
    }));
  }
  if (virtual && heightBelow) {
    rowElements.push(/*#__PURE__*/_react.default.createElement("tr", {
      key: "virtualBelow",
      style: {
        height: heightBelow
      }
    }));
  }
  if (rowElements.length === 0 && placeholder) {
    rowElements.push(/*#__PURE__*/_react.default.createElement(Placeholder, {
      key: "placeholder",
      width: columnDefs.length,
      height: height - HEADER_HEIGHT
    }, placeholder));
  }
  return /*#__PURE__*/_react.default.createElement(Table, null, /*#__PURE__*/_react.default.createElement("thead", null, /*#__PURE__*/_react.default.createElement(_HeadRow.default, {
    columnDefs: columnDefs,
    rowIds: rowIds,
    allSelected: rows.length === selection.length && rows.length !== 0
  })), /*#__PURE__*/_react.default.createElement("tbody", null, rowElements));
};

/* istanbul ignore next */
__signature__(List, "useListState{[enhancedColumnDefs, selection]}", function () {
  return [useListState];
});
__signature__(List, "useListState{[enhancedColumnDefs, selection]}", function () {
  return [useListState];
});
var checkInfiniteScroll = (0, _recompose.branch)(function (_ref3) {
  var scrollLoader = _ref3.scrollLoader;
  return !!scrollLoader;
}, _withInfiniteScroll.default);
var StatefulList = (0, _recompose.compose)((0, _recompose.setDisplayName)("List"), checkInfiniteScroll, _withScrollBox.default)(List);
StatefulList.propTypes = {
  columnDefs: _propTypes.default.arrayOf(_propTypes.default.object),
  // Each object must be a valid column definition
  rows: _propTypes.default.arrayOf(_propTypes.default.object),
  rowOnClick: _propTypes.default.func,
  // Click handler for row.
  // Fires when row is clicked, excluding select or switch columns
  // Event target will have a 'rowId' data value which identifies the clicked row.
  placeholder: _propTypes.default.node,
  // A React element to render as placeholder.
  keyField: _propTypes.default.arrayOf(_propTypes.default.string),
  // Path to identifying data field on each row.
  // Infinite/virtual scroll
  // If scrollLoader is present, below props will control the scrolling
  scrollLoader: _propTypes.default.func,
  // Loader function. Called with page number to be loaded.
  onScroll: _propTypes.default.func,
  // Optional scroll event handler
  loadTrigger: _propTypes.default.number,
  // How many pixels from the bottom should the load be triggered. Default: 200
  length: _propTypes.default.number,
  // How many elements are loaded, should equal rows.length
  latestPage: _propTypes.default.number,
  // The latest page number loaded
  pageLength: _propTypes.default.number // The length of a page, in row items. Default: 20.
};
var _default = StatefulList;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(PlaceholderCell, "PlaceholderCell", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(PlaceholderBox, "PlaceholderBox", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(Placeholder, "Placeholder", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(Table, "Table", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(HEADER_HEIGHT, "HEADER_HEIGHT", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(ROW_HEIGHT, "ROW_HEIGHT", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(calculateVirtualization, "calculateVirtualization", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(useListState, "useListState", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(List, "List", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(checkInfiniteScroll, "checkInfiniteScroll", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(StatefulList, "StatefulList", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/List/List.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(PlaceholderCell, "PlaceholderCell", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(PlaceholderBox, "PlaceholderBox", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(Placeholder, "Placeholder", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(Table, "Table", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(HEADER_HEIGHT, "HEADER_HEIGHT", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(ROW_HEIGHT, "ROW_HEIGHT", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(calculateVirtualization, "calculateVirtualization", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(useListState, "useListState", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(List, "List", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(checkInfiniteScroll, "checkInfiniteScroll", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(StatefulList, "StatefulList", "/home/vsts/work/1/s/src/components/List/List.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/List/List.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();