"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _TableProps = _interopRequireDefault(require("./TableProps"));
var _tableHelpers = _interopRequireDefault(require("./tableHelpers"));
var _useInMemoryPaging2 = _interopRequireDefault(require("../../../hooks/useInMemoryPaging"));
var _Placeholder = _interopRequireDefault(require("./PredefinedElements/Placeholder"));
var _comparisonHelper = require("../../../utils/comparisonHelper");
var _reactIntl = require("react-intl");
var _sharedMessages = _interopRequireDefault(require("../../../sharedMessages"));
var _SearchControl = _interopRequireDefault(require("../Inputs/PredefinedElements/SearchControl"));
var _Table = _interopRequireDefault(require("./Table"));
var _SectionToolbar = _interopRequireDefault(require("./PredefinedElements/SectionToolbar"));
var _TableInfoBar = _interopRequireDefault(require("./PredefinedElements/TableInfoBar"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
(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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 listFilter = function listFilter(list, searchTerm, searchProperties) {
  if (!searchTerm) {
    return list;
  }
  return list.filter(function (item) {
    return (0, _comparisonHelper.doesObjectContainsTextCaseInsensitive)(item, searchTerm, searchProperties);
  });
};
function getTableInfo(totalCountLabelId, noRowsTotalCountLabelId, formatMessage, totalCount) {
  if (totalCountLabelId && noRowsTotalCountLabelId) {
    if (totalCount) {
      return /*#__PURE__*/_react.default.createElement(_TableInfoBar.default, {
        tableLabel: formatMessage(totalCountLabelId, {
          quantity: totalCount
        })
      });
    }
    return /*#__PURE__*/_react.default.createElement(_TableInfoBar.default, {
      tableLabel: formatMessage(noRowsTotalCountLabelId)
    });
  }
  return null;
}
var TableWithInMemoryPaging = function TableWithInMemoryPaging(_ref) {
  var sortedRows = _ref.sortedRows,
    tableName = _ref.tableName,
    _ref$searchProperties = _ref.searchProperties,
    searchProperties = _ref$searchProperties === void 0 ? [] : _ref$searchProperties,
    columnDefs = _ref.columnDefs,
    toolbarRightContent = _ref.toolbarRightContent,
    _ref$pageSize = _ref.pageSize,
    pageSize = _ref$pageSize === void 0 ? 20 : _ref$pageSize,
    _ref$onItemClick = _ref.onItemClick,
    onItemClick = _ref$onItemClick === void 0 ? null : _ref$onItemClick,
    _ref$isReadMode = _ref.isReadMode,
    isReadMode = _ref$isReadMode === void 0 ? true : _ref$isReadMode,
    _ref$totalCountLabelI = _ref.totalCountLabelId,
    totalCountLabelId = _ref$totalCountLabelI === void 0 ? null : _ref$totalCountLabelI,
    _ref$noRowsTotalCount = _ref.noRowsTotalCountLabelId,
    noRowsTotalCountLabelId = _ref$noRowsTotalCount === void 0 ? null : _ref$noRowsTotalCount,
    _ref$placeholderIcon = _ref.placeholderIcon,
    placeholderIcon = _ref$placeholderIcon === void 0 ? null : _ref$placeholderIcon,
    _ref$placeholderTitle = _ref.placeholderTitle,
    placeholderTitle = _ref$placeholderTitle === void 0 ? null : _ref$placeholderTitle,
    _ref$placeholderSubti = _ref.placeholderSubtitle,
    placeholderSubtitle = _ref$placeholderSubti === void 0 ? null : _ref$placeholderSubti,
    _ref$rowKeyField = _ref.rowKeyField,
    rowKeyField = _ref$rowKeyField === void 0 ? "id" : _ref$rowKeyField;
  var _useIntl = (0, _reactIntl.useIntl)(),
    formatMessage = _useIntl.formatMessage;
  var tableRef = (0, _react.useRef)(null);
  var initialSearchProperties = (0, _react.useRef)(searchProperties);
  var searchPlaceholder = formatMessage(_sharedMessages.default.search);
  var sortAndFilter = (0, _react.useCallback)(function (_ref2) {
    var list = _ref2.list,
      filters = _ref2.filters;
    var searchTerm = filters.searchTerm;
    return listFilter(list, searchTerm, initialSearchProperties.current);
  }, [initialSearchProperties]);
  var _useInMemoryPaging = (0, _useInMemoryPaging2.default)({
      viewStateName: tableName,
      tableRef: tableRef,
      records: sortedRows,
      pageSize: pageSize,
      sortAndFilterFn: sortAndFilter
    }),
    rowsSlice = _useInMemoryPaging.rows,
    scrollLoader = _useInMemoryPaging.scrollLoader,
    currentPage = _useInMemoryPaging.currentPage,
    filters = _useInMemoryPaging.filters,
    setFilter = _useInMemoryPaging.setFilter,
    totalCount = _useInMemoryPaging.totalCount;
  var _buildHeaderAndRowFro = (0, _tableHelpers.default)(columnDefs, rowsSlice, isReadMode, rowKeyField),
    headers = _buildHeaderAndRowFro.headers,
    rows = _buildHeaderAndRowFro.rows;
  var placeholder = /*#__PURE__*/_react.default.createElement(_Placeholder.default, {
    icon: placeholderIcon,
    title: placeholderTitle,
    subtitle: placeholderSubtitle,
    cellList: columnDefs.map(function (col) {
      return col.placeholder;
    })
  });
  var onSearch = function onSearch(searchOption, searchText) {
    setFilter(_objectSpread(_objectSpread({}, filters), {}, {
      searchTerm: searchText
    }));
  };
  var tableProps = new _TableProps.default();
  tableProps.set(_TableProps.default.propNames.stickyHeader, true);
  tableProps.set(_TableProps.default.propNames.withoutTopBorder, true);
  tableProps.set(_TableProps.default.propNames.deepPropsComparation, true);
  tableProps.set(_TableProps.default.propNames.saveScrollbarPosition, true);
  tableProps.set(_TableProps.default.propNames.tableName, tableName);
  if (onItemClick) {
    tableProps.set(_TableProps.default.propNames.onRowClick, onItemClick);
  }
  var hasToolbar = toolbarRightContent || searchProperties.length > 0;
  var toolbar = hasToolbar ? /*#__PURE__*/_react.default.createElement(_SectionToolbar.default, {
    rightContent: toolbarRightContent
  }, searchProperties.length > 0 && /*#__PURE__*/_react.default.createElement(_SearchControl.default, {
    placeholder: searchPlaceholder,
    defaultValue: filters.searchTerm,
    onSearch: onSearch,
    focusAndSelectSearchFieldOnLoad: false
  })) : null;
  var tableInfo = getTableInfo(totalCountLabelId, noRowsTotalCountLabelId, formatMessage, totalCount);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, toolbar, /*#__PURE__*/_react.default.createElement(_Table.default, {
    ref: tableRef,
    headers: headers,
    tableInfo: tableInfo,
    rows: rows,
    tableProps: tableProps,
    placeholder: placeholder,
    scrollLoader: scrollLoader,
    latestPage: currentPage,
    pageLength: pageSize
  }));
};
__signature__(TableWithInMemoryPaging, "useIntl{{ formatMessage }}\nuseRef{tableRef}\nuseRef{initialSearchProperties}\nuseCallback{sortAndFilter}\nuseInMemoryPaging{{\n\t\trows: rowsSlice,\n\t\tscrollLoader,\n\t\tcurrentPage,\n\t\tfilters,\n\t\tsetFilter,\n\t\ttotalCount,\n\t}}", function () {
  return [_reactIntl.useIntl, _useInMemoryPaging2.default];
});
__signature__(TableWithInMemoryPaging, "useIntl{{ formatMessage }}\nuseRef{tableRef}\nuseRef{initialSearchProperties}\nuseCallback{sortAndFilter}\nuseInMemoryPaging{{\n\t\trows: rowsSlice,\n\t\tscrollLoader,\n\t\tcurrentPage,\n\t\tfilters,\n\t\tsetFilter,\n\t\ttotalCount,\n\t}}", function () {
  return [_reactIntl.useIntl, _useInMemoryPaging2.default];
});
var _default = TableWithInMemoryPaging;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(listFilter, "listFilter", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.js");
  reactHotLoader.register(getTableInfo, "getTableInfo", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.js");
  reactHotLoader.register(TableWithInMemoryPaging, "TableWithInMemoryPaging", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.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(listFilter, "listFilter", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.js");
  reactHotLoader.register(getTableInfo, "getTableInfo", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.js");
  reactHotLoader.register(TableWithInMemoryPaging, "TableWithInMemoryPaging", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/TableWithInMemoryPaging.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();