"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.MultiSelectCollectionFilter = MultiSelectCollectionFilter;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _state = require("../../state");
var _mobxReactLite = require("mobx-react-lite");
var _MultiSelectFilterBase = require("../MultiSelectFilterBase");
var _hooks = require("../../hooks");
var _react2 = require("@wix/bex-core/react");
var _providers = require("../../providers");
var _useFilterCollectionStates = require("../../hooks/useFilterCollectionStates");
var _useCollectionFilterInitialFetchTiming = require("../../hooks/useCollectionFilterInitialFetchTiming");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/MultiSelectCollectionFilter/MultiSelectCollectionFilter.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 _MultiSelectCollectionFilter(props) {
  const {
    filter,
    collection,
    renderItem,
    initialFetchTiming,
    ...rest
  } = props;
  const container = (0, _react2.useWixPatternsContainer)();
  const toolbar = (0, _providers.useFilterToolbarState)();
  const buildOption = (0, _hooks.useBuildOption)({
    filter,
    renderItem
  });
  const [collectionFilter] = (0, _react.useState)(() => new _state.CollectionFilterState({
    collection,
    filter
  }));
  const [{
    dropdownSelect,
    init
  }] = (0, _react.useState)(() => new _state.DropdownCollectionFilterState({
    collectionFilter,
    resetOnClose: true,
    container,
    toolbar
  }));
  const {
    query: {
      search
    }
  } = collection;
  const {
    skipInitialFetch,
    onFocus
  } = (0, _useCollectionFilterInitialFetchTiming.useCollectionFilterInitialFetchTiming)(props);
  (0, _react.useEffect)(() => init({
    skipInitialFetch
  }), []);
  const {
    result: {
      keyedItems
    },
    fetchedLastPage
  } = collection;
  const options = keyedItems.map(buildOption);
  const {
    fixedFooter,
    showInitialLoader
  } = (0, _useFilterCollectionStates.useFilterCollectionStates)({
    collection,
    fixedFooter: rest.fixedFooter
  });
  return /*#__PURE__*/_react.default.createElement(_MultiSelectFilterBase.MultiSelectFilterBase, (0, _extends2.default)({}, rest, {
    fixedFooter: fixedFooter,
    infiniteScroll: true,
    value: search.value,
    onChange: e => {
      collection.search(e.target.value);
    },
    onFocus: onFocus,
    filter: filter,
    dropdownSelect: dropdownSelect,
    options: options,
    hasMore: !showInitialLoader && !fetchedLastPage,
    loadMore: collection.fetchNextPageIfNeeded,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 92,
      columnNumber: 5
    }
  }));
}
const MultiSelectCollectionFilterInternal = (0, _mobxReactLite.observer)(_MultiSelectCollectionFilter);
function MultiSelectCollectionFilter(props) {
  return /*#__PURE__*/_react.default.createElement(_providers.FilterTranslationProvider, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 118,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(MultiSelectCollectionFilterInternal, (0, _extends2.default)({}, props, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 119,
      columnNumber: 7
    }
  })));
}
MultiSelectCollectionFilter.displayName = 'MultiSelectCollectionFilter';
//# sourceMappingURL=MultiSelectCollectionFilter.js.map