"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.PickerContentItemsSelection = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _CollectionSearch = require("../CollectionSearch");
var _Picker = require("../Picker");
var _CollectionContext = require("../CollectionContext");
var _MaxSelection = require("../MaxSelection");
var _MaybeTooltip = require("../MaybeTooltip");
var _SelectAllCheckbox = require("./SelectAllCheckbox");
var _MultiCollectionSupportProvider = require("../MultiCollectionSupportProvider");
var _PickerContentBaseWrapped = require("./PickerContentBaseWrapped");
var _FilterRenderingContext = require("../FilterRenderingContext");
var _isValidElement = require("../isValidElement");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/PickerContent/PickerContentItemsSelection.tsx";
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 _PickerContentItemsSelection(props) {
  const {
    modalState,
    primaryButtonProps,
    renderEmptyState,
    renderError,
    state,
    multiple,
    customContentButtonText,
    customContentButtonPrefixIcon,
    customContentButtonOnClick,
    customContent,
    showSelectAllCheckbox,
    enablePrimaryButton,
    search,
    hideSearch,
    renderPickerContent,
    customMaxSelectionLabel,
    renderCustomNotification,
    filterComponent,
    showSelectedCount,
    ...rest
  } = props;
  const {
    _customContent
  } = modalState;
  const {
    picker
  } = state;
  const {
    collection
  } = picker;
  const {
    bulkSelect,
    translate: t,
    showInitialLoader
  } = collection;
  const {
    isEmpty: isSelectEmpty
  } = bulkSelect;
  (0, _react.useEffect)(() => {
    _customContent.value = () => customContent;
  }, []);
  const reachedMax = (0, _MaxSelection.reachedMaxSelection)({
    collection,
    multiple
  });
  const [filterRenderingContextValue] = (0, _react.useState)(() => ({
    location: 'modal'
  }));
  const onCustomContentButtonClick = (0, _react.useCallback)(() => {
    state.onCreateNewButtonClick(customContentButtonOnClick);
  }, [customContentButtonOnClick]);
  const renderItem = (item, index) => {
    const renderedItem = props.renderItem(item, index);
    const key = collection.itemKey(item);
    return {
      ...renderedItem,
      disabled: reachedMax && !collection.bulkSelect.selectedIds.includes(key) || renderedItem.disabled
    };
  };
  const searchElement = (0, _isValidElement.isValidElement)(search) ? (/*#__PURE__*/_react.default.cloneElement(search, {
    minWidthPixels: '100%'
  })) : search !== false ? /*#__PURE__*/_react.default.createElement(_CollectionSearch.CollectionSearch, {
    minWidthPixels: "100%",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 196,
      columnNumber: 5
    }
  }) : null;
  const shouldShowSearch = search !== undefined ? search !== false : !hideSearch;
  const resolvedSearchElement = shouldShowSearch ? searchElement : null;
  const renderSideActions = () => {
    if (showSelectAllCheckbox && !showInitialLoader && (multiple === true || multiple === Infinity)) {
      return /*#__PURE__*/_react.default.createElement(_SelectAllCheckbox.SelectAllCheckbox, {
        state: state,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 209,
          columnNumber: 14
        }
      });
    }
    const shouldShowCustomContentButton = customContent || customContentButtonOnClick;
    if (shouldShowCustomContentButton) {
      return /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 217,
          columnNumber: 9
        }
      }, /*#__PURE__*/_react.default.createElement(_MaybeTooltip.MaybeTooltip, {
        dataHook: "custom-content-button-tooltip",
        content: reachedMax && t('cairo.picker.createNew.button.tooltip'),
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 218,
          columnNumber: 11
        }
      }, /*#__PURE__*/_react.default.createElement("div", {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 222,
          columnNumber: 13
        }
      }, /*#__PURE__*/_react.default.createElement(_designSystem.TextButton, {
        dataHook: "custom-content-button",
        prefixIcon: customContentButtonPrefixIcon,
        disabled: reachedMax,
        onClick: onCustomContentButtonClick,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 223,
          columnNumber: 15
        }
      }, customContentButtonText ?? t('cairo.picker.createNew.button')))));
    }
    if (showSelectedCount) {
      return /*#__PURE__*/_react.default.createElement(_designSystem.Text, {
        dataHook: "cairo-selected-items-count",
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 238,
          columnNumber: 9
        }
      }, t('cairo.selector.modal.items.selected', {
        total: collection.bulkSelect.selectedCount
      }));
    }
    return null;
  };
  const renderFiltersComponent = () => {
    return /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
      gap: "12px",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 250,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
      width: "200px",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 251,
        columnNumber: 9
      }
    }, /*#__PURE__*/_react.default.createElement(_FilterRenderingContext.FilterRenderingContextProvider, {
      value: filterRenderingContextValue,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 252,
        columnNumber: 11
      }
    }, filterComponent)), resolvedSearchElement && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, resolvedSearchElement));
  };
  const showMaxSelection = (0, _MaxSelection.showMaxSelectionLabel)(multiple);
  return /*#__PURE__*/_react.default.createElement(_PickerContentBaseWrapped.PickerContentBaseWrapped, (0, _extends2.default)({
    secondaryButtonText: t('cairo.cancel.button'),
    primaryButtonProps: {
      disabled: isSelectEmpty && !enablePrimaryButton,
      ...primaryButtonProps,
      style: {
        ...(primaryButtonProps == null ? void 0 : primaryButtonProps.style)
      }
    },
    sideActions: renderSideActions()
  }, rest, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 263,
      columnNumber: 5
    }
  }), /*#__PURE__*/_react.default.createElement(_CollectionContext.CollectionProvider, {
    value: picker.collection,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 275,
      columnNumber: 7
    }
  }, (resolvedSearchElement || filterComponent || renderCustomNotification || showMaxSelection) && /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    display: "grid",
    paddingLeft: "SP5",
    paddingRight: "SP5",
    paddingBottom: "SP3",
    paddingTop: rest.showHeaderDivider ? 'SP3' : '3px',
    gap: "SP3",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 280,
      columnNumber: 11
    }
  }, filterComponent ? renderFiltersComponent() : resolvedSearchElement, renderCustomNotification ? /*#__PURE__*/_react.default.createElement("span", {
    "data-hook": "custom-notification",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 290,
      columnNumber: 15
    }
  }, renderCustomNotification({
    selected: collection.bulkSelect.selectedCount,
    total: collection.result.total
  })) : null, showMaxSelection && /*#__PURE__*/_react.default.createElement(_MaxSelection.BulkSelectionMaxSelectionLabel, {
    dataHook: "selection-max-label",
    multiple: multiple,
    customLabel: customMaxSelectionLabel,
    t: t,
    selectedCount: collection.bulkSelect.selectedCount,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 298,
      columnNumber: 15
    }
  })), /*#__PURE__*/_react.default.createElement(_MultiCollectionSupportProvider.MultiCollectionSupportProvider, {
    value: picker.multi,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 308,
      columnNumber: 9
    }
  }, /*#__PURE__*/_react.default.createElement(_Picker.Picker, {
    dataHook: "picker",
    state: picker,
    renderEmptyState: renderEmptyState,
    renderError: renderError,
    hasFilterDropdown: !!filterComponent,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 309,
      columnNumber: 11
    }
  }, renderPickerContent == null ? void 0 : renderPickerContent({
    state,
    renderItem
  })))));
}
const PickerContentItemsSelection = exports.PickerContentItemsSelection = (0, _mobxReactLite.observer)(_PickerContentItemsSelection);
//# sourceMappingURL=PickerContentItemsSelection.js.map