"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionSearch = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@wix/bex-core/react");
var _useIsMobile = require("../hooks/useIsMobile");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/CollectionSearch.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); }
const CollectionSearch = exports.CollectionSearch = (0, _mobxReactLite.observer)(function CollectionSearch({
  onSearchChange,
  onSearchClick,
  onSearchBlur,
  visibility,
  expandable,
  ...rest
}) {
  const collection = (0, _react2.useCollectionContext)();
  const {
    query: {
      search
    },
    translate: t
  } = collection;
  const isMobile = (0, _useIsMobile.useIsMobile)();
  const [isSearchExpanded, setIsSearchExpanded] = (0, _react.useState)(!!search.value);
  const searchInputRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    collection.searchInputRef = {
      focus: () => {
        var _searchInputRef$curre;
        (_searchInputRef$curre = searchInputRef.current) == null || _searchInputRef$curre.focus();
      }
    };
    return () => {
      collection.searchInputRef = null;
    };
  }, [collection]);
  const handleCancel = (0, _react.useCallback)(() => {
    collection.clearSearch();
    onSearchChange == null || onSearchChange('');
    if (searchInputRef.current) {
      searchInputRef.current.blur();
      setIsSearchExpanded(false);
    }
  }, [collection, onSearchChange]);
  const showCancelButton = isMobile && isSearchExpanded;
  const searchElement = /*#__PURE__*/_react.default.createElement(_designSystem.Search, (0, _extends2.default)({
    dataHook: "collection-search",
    onChange: e => {
      const value = e.target.value;
      collection.search(e.target.value, {
        clearResult: true
      });
      onSearchChange == null || onSearchChange(value);
    },
    ref: searchInputRef,
    value: search.value,
    onClear: () => {
      const value = '';
      collection.clearSearch();
      onSearchChange == null || onSearchChange(value);
    },
    expandable: expandable,
    expandDirection: isMobile ? 'left' : undefined,
    placeholder: t('cairo.searchPlaceholder', {
      defaultValue: ''
    }),
    textOverflow: "ellipsis",
    onFocus: () => {
      setIsSearchExpanded(true);
    },
    onBlur: e => {
      const value = e.target.value;
      if (!value) {
        setIsSearchExpanded(false);
      }
      onSearchBlur == null || onSearchBlur(value);
    }
  }, rest, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 67,
      columnNumber: 5
    }
  }));
  if (isMobile && expandable) {
    return /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
      width: "100%",
      gap: "SP2",
      verticalAlign: "middle",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 101,
        columnNumber: 7
      }
    }, searchElement, showCancelButton && /*#__PURE__*/_react.default.createElement(_designSystem.TextButton, {
      dataHook: "collection-search-cancel",
      onClick: handleCancel,
      size: "small",
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 104,
        columnNumber: 11
      }
    }, t('cairo.cancel.button')));
  }
  return searchElement;
});
//# sourceMappingURL=CollectionSearch.js.map