"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableToolbarActions = TableToolbarActions;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faFilter = require("@fortawesome/free-solid-svg-icons/faFilter");
var _button = require("../../button");
var _actionBarSearch = require("../../action-bar-search");
var _useDebounce = require("../../hooks/use-debounce");
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 TableToolbarActions({
  isFilterActive,
  onFilterClick,
  children,
  onSearchChange,
  searchQuery,
  filters,
  itemsFoundCount,
  isExceededTotalLimit,
  localizationText: localizationTextParams,
  filterButtonProps = {},
  searchProps = {}
}) {
  const itemsFoundRef = (0, _react.useRef)(null);
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const localizationText = localizationTextParams ?? {
    filter: t('Filters'),
    itemsFoundText: (count, isExceeded) => isExceeded ? t('{{count}}+ item found', {
      count,
      plural: '{{count}}+ items found'
    }) : t('{{count}} item found', {
      count,
      plural: '{{count}} items found'
    }),
    searchText: t('Search'),
    searchPlaceholder: t('Search')
  };
  const shouldShowItemsFoundCount = !!((searchQuery || filters?.length) && itemsFoundCount);
  const updateItemsFound = (0, _react.useCallback)(itemCount => {
    if (itemsFoundRef.current) {
      itemsFoundRef.current.innerHTML = `${localizationText.itemsFoundText(itemCount, isExceededTotalLimit)}`;
    }
  }, [localizationText.itemsFoundText, isExceededTotalLimit]);
  const debouncedFunc = (0, _useDebounce.useDebounce)(updateItemsFound, 1200);
  (0, _react.useEffect)(() => {
    if (shouldShowItemsFoundCount && itemsFoundCount > 0) {
      debouncedFunc(itemsFoundCount);
    }
  }, [itemsFoundCount, shouldShowItemsFoundCount]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "table-toolbar-actions",
    children: [children, onFilterClick && /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
      accentType: _button.ACCENT_TYPES.GHOST,
      onClick: onFilterClick,
      leftIcon: _faFilter.faFilter,
      ...filterButtonProps,
      className: (0, _classnames.default)(isFilterActive && 'active', filterButtonProps.className),
      "aria-pressed": isFilterActive,
      children: localizationText.filter
    }), onSearchChange && /*#__PURE__*/(0, _jsxRuntime.jsx)(_actionBarSearch.ActionBarSearch, {
      searchQuery: searchQuery,
      onChange: onSearchChange,
      searchText: localizationText.searchText,
      focusPlaceholder: localizationText.searchPlaceholder,
      ...searchProps
    }), shouldShowItemsFoundCount && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "table-toolbar-actions-items-found",
      "test-id": "table-toolbar-actions-items-found",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        children: localizationText.itemsFoundText(itemsFoundCount, isExceededTotalLimit)
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "sr-only",
        "aria-live": "polite",
        "test-id": "table-toolbar-actions-items-found-hidden",
        ref: itemsFoundRef
      })]
    })]
  });
}