"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionMultiSelectCheckbox = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _state = require("../../state");
var _bexCore = require("@wix/bex-core");
var _react2 = require("@wix/bex-core/react");
var _mobxReactLite = require("mobx-react-lite");
var _MultiSelectCheckboxFilterBase = require("../MultiSelectCheckboxFilterBase");
var _InputOverflow = require("../InputOverflow");
var _providers = require("../../providers");
var _useCollectionFilterInitialFetchTiming = require("../../hooks/useCollectionFilterInitialFetchTiming");
var _useFilterCollectionStates = require("../../hooks/useFilterCollectionStates");
var _useCollectionOptions = require("../../hooks/useCollectionOptions");
var _useDropdownPlaceholder = require("../../hooks/useDropdownPlaceholder");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/CollectionMultiSelectCheckbox/CollectionMultiSelectCheckbox.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 _CollectionMultiSelectCheckbox(props) {
  const {
    filter,
    collection,
    renderItem,
    onSelect,
    onClose,
    onOptionsShow,
    onDeselect,
    placeholder: placeholderProp,
    ...rest
  } = props;
  const container = (0, _react2.useWixPatternsContainer)();
  const toolbar = (0, _providers.useFilterToolbarState)();
  const {
    translate
  } = (0, _providers.useDropdownTranslationContext)();
  const [collectionFilter] = (0, _react.useState)(() => new _state.CollectionFilterState({
    collection,
    filter
  }));
  const [dropdownCollectionFilterState] = (0, _react.useState)(() => new _state.DropdownCollectionFilterState({
    resetOnClose: false,
    collectionFilter,
    container,
    toolbar
  }));
  const {
    dropdownSelect,
    init,
    inputState,
    shouldShowPlaceholder
  } = dropdownCollectionFilterState;
  const {
    disableEditing
  } = inputState;
  const {
    select: {
      selectedKeys,
      selectedValues
    }
  } = dropdownSelect;
  const [textOverflowState] = (0, _react.useState)(() => new _bexCore.TextOverflowState({
    observable: () => dropdownSelect.select.selectedValues
  }));
  const {
    isOverflow
  } = textOverflowState;
  const {
    fetchedLastPage
  } = collection;
  const {
    fixedFooter,
    showInitialLoader
  } = (0, _useFilterCollectionStates.useFilterCollectionStates)({
    collection,
    fixedFooter: rest.fixedFooter
  });
  const {
    skipInitialFetch,
    onFocus
  } = (0, _useCollectionFilterInitialFetchTiming.useCollectionFilterInitialFetchTiming)(props);
  (0, _react.useEffect)(() => init({
    skipInitialFetch
  }), []);
  const {
    selectedValuesText,
    placeholder
  } = (0, _useDropdownPlaceholder.useDropdownPlaceholder)({
    dropdownSelect,
    filter,
    shouldShowPlaceholder,
    isOverflow,
    placeholderProp
  });
  const {
    options
  } = (0, _useCollectionOptions.useCollectionOptions)({
    collection,
    dropdownCollectionFilterState,
    renderItem,
    filter
  });
  return /*#__PURE__*/_react.default.createElement(_MultiSelectCheckboxFilterBase.MultiSelectCheckboxFilterBase, (0, _extends2.default)({}, rest, {
    fixedFooter: fixedFooter,
    infiniteScroll: true,
    filter: filter,
    dropdownSelect: dropdownSelect,
    options: options,
    selectedOptions: selectedKeys,
    hasMore: !showInitialLoader && !fetchedLastPage,
    loadMore: collection.fetchNextPageIfNeeded,
    onClose: () => {
      if (!dropdownSelect.hasPendingFilter) {
        dropdownSelect.closeAndChangeSelectedItems();
      } else {
        dropdownSelect.close();
      }
      inputState.onBlur();
      onClose == null || onClose();
    },
    onSelect: (id, option) => {
      const {
        keyedItem
      } = option;
      dropdownSelect.set(keyedItem);
      onSelect == null || onSelect(id, option);
    },
    onDeselect: (id, option) => {
      const {
        keyedItem
      } = option;
      dropdownSelect.delete(keyedItem);
      onDeselect == null || onDeselect(id, option);
    },
    value: collection.query.search.value,
    onFocus: () => {
      inputState.onFocus();
      onFocus();
    },
    onBlur: () => {
      inputState.onBlur();
    },
    onChange: e => {
      collection.search(e.target.value, {
        clearResult: true
      });
    },
    onOptionsShow: () => {
      dropdownCollectionFilterState.onOptionsShow();
      inputState.onOptionsShow();
      onOptionsShow == null || onOptionsShow();
    },
    onKeyDown: e => {
      if (e.nativeEvent.code === 'Tab') {
        inputState.onTabKeyDown();
      }
    },
    onClickOutside: e => {
      props.onClickOutside == null || props.onClickOutside(e);
      inputState.onClickOutside();
    },
    onClear: () => {
      inputState.onBlur();
      collection.clearSearch();
    },
    disableEditing: disableEditing,
    placeholder: placeholder,
    clearButton: !disableEditing,
    clearButtonTooltipContent: translate('clearButtonTooltip'),
    focusOnClearClick: true,
    inputElement: /*#__PURE__*/_react.default.createElement(_InputOverflow.InputOverflow, {
      selected: selectedValues.length > 0,
      focused: inputState.isFocused,
      text: selectedValuesText,
      state: textOverflowState,
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 191,
        columnNumber: 9
      }
    }),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 127,
      columnNumber: 5
    }
  }));
}
const CollectionMultiSelectCheckbox = exports.CollectionMultiSelectCheckbox = (0, _mobxReactLite.observer)(_CollectionMultiSelectCheckbox);
//# sourceMappingURL=CollectionMultiSelectCheckbox.js.map