"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = SearchableList;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _listCard = require("../list-card");
var _message = require("../message");
var _loadingIcon = require("../loading-icon");
var _searchableListView = _interopRequireDefault(require("./searchable-list-view"));
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function SearchableList({
  className,
  items,
  errorMessage,
  isLoading,
  pagination,
  availableSizes,
  ItemComponent = _listCard.ListCard,
  searchValue = '',
  onSearch = () => {},
  header = null,
  total = 0,
  noItemsText,
  updatingResultsText,
  loadingText
}) {
  const [isInitialLoad, setIsInitialLoad] = (0, _react.useState)(true);
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  (0, _react.useEffect)(() => {
    if (!isLoading) {
      // set a flag, so we could show a correct loading message
      setIsInitialLoad(false);
    }
  }, [isLoading]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_searchableListView.default, {
    className: (0, _classnames.default)('searchable-list', className, {
      loading: isLoading
    }),
    searchValue: searchValue,
    pagination: pagination,
    onSearch: onSearch,
    header: header,
    total: total,
    availableSizes: availableSizes,
    noDataElement: /*#__PURE__*/(0, _jsxRuntime.jsx)(_message.Message, {
      type: errorMessage ? _message.MESSAGE_TYPES.ERROR : _message.MESSAGE_TYPES.INFO,
      testId: "searchable-list-no-data-message",
      isCentered: true,
      children: errorMessage || (noItemsText ?? t('No items to list'))
    }),
    fillContainer: isLoading && /*#__PURE__*/(0, _jsxRuntime.jsx)(_loadingIcon.LoadingIcon, {
      testId: "searchable-list-loading-icon",
      message: isInitialLoad ? loadingText ?? t('Loading...') : updatingResultsText ?? t('Updating list results...')
    }),
    children: items.map(item => /*#__PURE__*/_react.default.createElement(ItemComponent, {
      ...item,
      key: item.id
    }))
  });
}