"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.PickerSelectorListContent = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _designSystem = require("@wix/design-system");
var _react = _interopRequireWildcard(require("react"));
var _bexCore = require("@wix/bex-core");
var _mobxReactLite = require("mobx-react-lite");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/PickerSelectorListContent/PickerSelectorListContent.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 _PickerSelectorListContent(props) {
  const {
    state,
    renderItem,
    multiple,
    spacing = 30,
    hasAdditionalStep,
    ...rest
  } = props;
  const {
    picker
  } = state;
  const {
    collection
  } = picker;
  const {
    bulkSelect,
    result: {
      keyedItems
    },
    fetchedLastPage
  } = collection;
  const {
    checkedSet,
    indeterminateSet
  } = bulkSelect;
  (0, _react.useEffect)(() => {
    const disposers = [(0, _bexCore.addEventListener)(bulkSelect.select._map.events, 'isDisabled', selectionEntry => {
      const collectionItem = bulkSelect.getCollectionItem(selectionEntry.value.key);
      if (!collectionItem) {
        return false;
      }
      const {
        item,
        index
      } = collectionItem;
      return (renderItem == null ? void 0 : renderItem(item, index).disabled) || false;
    })];
    return () => {
      disposers.forEach(d => d());
    };
  }, [renderItem]);
  const items = (0, _react.useMemo)(() => keyedItems.map((keyedItem, index) => ({
    id: keyedItem.key,
    title: collection.itemName(keyedItem.item),
    ...(renderItem == null ? void 0 : renderItem(keyedItem.item, index)),
    keyedItem
  })), [keyedItems, renderItem]);
  return /*#__PURE__*/_react.default.createElement(_designSystem.SelectorListContent, (0, _extends2.default)({
    items: items,
    infiniteScrollRef: infiniteScroll => {
      var _infiniteScroll$scrol;
      collection.scrollableContent = infiniteScroll == null || (_infiniteScroll$scrol = infiniteScroll.scrollComponent) == null ? void 0 : _infiniteScroll$scrol.parentElement;
    },
    searchValue: "",
    loadMore: collection.fetchNextPageIfNeeded,
    checkIsSelected: item => {
      const {
        keyedItem
      } = item;
      return checkedSet.has(keyedItem.key);
    },
    checkIndeterminate: item => {
      const {
        keyedItem
      } = item;
      return indeterminateSet.has(keyedItem.key);
    },
    onToggle: item => {
      const {
        keyedItem
      } = item;
      picker.toggle(keyedItem, {
        clear: !multiple,
        hasAdditionalStep: !multiple && (hasAdditionalStep == null ? void 0 : hasAdditionalStep({
          item: keyedItem.item
        }))
      });
    },
    hasMore: !fetchedLastPage,
    multiple: multiple
  }, rest, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 82,
      columnNumber: 5
    }
  }));
}
const PickerSelectorListContent = exports.PickerSelectorListContent = (0, _mobxReactLite.observer)(_PickerSelectorListContent);
//# sourceMappingURL=PickerSelectorListContent.js.map