"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionAutoComplete = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _state = require("../../state");
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _hooks = require("../../hooks");
var _react2 = require("@wix/bex-core/react");
var _providers = require("../../providers");
var _useFilterCollectionStates = require("../../hooks/useFilterCollectionStates");
var _AutoCompleteFilterState = require("../AutoCompleteFilter/AutoCompleteFilterState");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/CollectionAutoComplete/CollectionAutoComplete.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 _CollectionAutoComplete(props) {
  const {
    filter,
    collection,
    renderItem,
    renderSection,
    onSelect,
    onClear,
    readOnly,
    placeholder,
    ...rest
  } = props;
  const container = (0, _react2.useWixPatternsContainer)();
  const toolbar = (0, _providers.useFilterToolbarState)();
  const [collectionFilter] = (0, _react.useState)(() => new _state.CollectionFilterState({
    collection,
    filter
  }));
  const [state] = (0, _react.useState)(() => new _AutoCompleteFilterState.AutoCompleteFilterState({
    collectionFilter,
    resetOnClose: false,
    container,
    toolbar
  }));
  const {
    translate
  } = (0, _providers.useDropdownTranslationContext)();
  (0, _react.useEffect)(() => {
    return state.init();
  }, []);
  const {
    result: {
      keyedItems
    },
    fetchedLastPage
  } = collection;
  const buildOption = (0, _hooks.useBuildOption)({
    filter,
    renderItem
  });
  const builtOptions = keyedItems.map(buildOption);
  const options = (0, _hooks.useBuildGroupedOptions)({
    items: builtOptions,
    renderSection
  });
  const {
    fixedFooter,
    showInitialLoader
  } = (0, _useFilterCollectionStates.useFilterCollectionStates)({
    collection,
    fixedFooter: rest.fixedFooter
  });
  return /*#__PURE__*/_react.default.createElement(_designSystem.AutoComplete, (0, _extends2.default)({
    dataHook: `collection-filter-${filter.name}`,
    closeOnSelect: true,
    border: "round",
    options: options,
    onClose: () => {
      state.dropdown.dropdownSelect.close();
      state.onInputBlur();
    },
    onBlur: state.onInputBlur,
    readOnly: readOnly,
    clearButton: !readOnly,
    onClear: !readOnly ? e => {
      onClear == null || onClear(e);
      state.clearSelectionAndSearch();
    } : undefined,
    placeholder: placeholder ?? translate('placeholder'),
    onSelect: (option, sameOptionWasPicked) => {
      const {
        keyedItem
      } = option;
      state.dropdown.dropdownSelect.setSingle(keyedItem);
      onSelect == null || onSelect(option, sameOptionWasPicked);
      state.searchInputValue = state.collection.itemName(keyedItem.item);
    },
    infiniteScroll: true,
    value: state.searchInputValue,
    onChange: e => {
      state.searchInputValue = e.target.value;
      collection.search(e.target.value);
    },
    hasMore: !showInitialLoader && !fetchedLastPage,
    loadMore: collection.fetchNextPageIfNeeded
  }, rest, {
    fixedFooter: fixedFooter,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 114,
      columnNumber: 5
    }
  }));
}
const CollectionAutoComplete = exports.CollectionAutoComplete = (0, _mobxReactLite.observer)(_CollectionAutoComplete);
//# sourceMappingURL=CollectionAutoComplete.js.map