'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var neetoAtoms = require('@bigbinary/neeto-atoms');
var useFuncDebounce = require('@bigbinary/neeto-commons-frontend/react-utils/useFuncDebounce');
var useQueryParams = require('@bigbinary/neeto-commons-frontend/react-utils/useQueryParams');
var useUpdateEffect = require('@bigbinary/neeto-commons-frontend/react-utils/useUpdateEffect');
var general = require('@bigbinary/neeto-commons-frontend/utils/general');
var ramda = require('ramda');
var reactI18next = require('react-i18next');
var reactRouterDom = require('react-router-dom');
var utils = require('@bigbinary/neeto-commons-frontend/utils');
var jsxRuntime = require('react/jsx-runtime');
var search = require('../search-BBuFYdmG.js');
require('../createLucideIcon-DBDhE7kE.js');

var getInitialSearchValue = function getInitialSearchValue(_ref) {
  var _ref$initialValue = _ref.initialValue,
    initialValue = _ref$initialValue === void 0 ? "" : _ref$initialValue,
    enableUrlSync = _ref.enableUrlSync,
    searchParamName = _ref.searchParamName;
  if (enableUrlSync) {
    var _queryParams$searchPa;
    var queryParams = utils.getQueryParams({
      toCamelCase: false
    });
    return (_queryParams$searchPa = queryParams[searchParamName]) !== null && _queryParams$searchPa !== void 0 ? _queryParams$searchPa : "";
  }
  return initialValue;
};

var _excluded = ["value", "debounceTime", "onSearch", "onChange", "enableUrlSync", "searchParamName", "className", "label"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var Search = function Search(_ref) {
  var value = _ref.value,
    _ref$debounceTime = _ref.debounceTime,
    debounceTime = _ref$debounceTime === void 0 ? 300 : _ref$debounceTime,
    onSearch = _ref.onSearch,
    onChange = _ref.onChange,
    _ref$enableUrlSync = _ref.enableUrlSync,
    enableUrlSync = _ref$enableUrlSync === void 0 ? false : _ref$enableUrlSync,
    _ref$searchParamName = _ref.searchParamName,
    searchParamName = _ref$searchParamName === void 0 ? "search_term" : _ref$searchParamName,
    className = _ref.className,
    label = _ref.label,
    inputProps = _objectWithoutProperties(_ref, _excluded);
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var queryParams = useQueryParams({
    toCamelCase: false
  });
  var history = reactRouterDom.useHistory();
  var inputId = React.useId();
  var _useState = React.useState(getInitialSearchValue({
      initialValue: value,
      enableUrlSync: enableUrlSync,
      searchParamName: searchParamName
    })),
    _useState2 = _slicedToArray(_useState, 2),
    internalValue = _useState2[0],
    setInternalValue = _useState2[1];
  var trimmedValue = value === null || value === void 0 ? void 0 : value.trim();
  useUpdateEffect(function () {
    var _queryParams$searchPa;
    if (!enableUrlSync) return;
    var searchParamValue = (_queryParams$searchPa = queryParams === null || queryParams === void 0 ? void 0 : queryParams[searchParamName]) !== null && _queryParams$searchPa !== void 0 ? _queryParams$searchPa : "";
    setInternalValue(searchParamValue);
  }, [queryParams === null || queryParams === void 0 ? void 0 : queryParams[searchParamName]]);
  var handleSearchQueryParams = function handleSearchQueryParams(trimmedValue) {
    if (!trimmedValue && !(queryParams !== null && queryParams !== void 0 && queryParams[searchParamName])) return;
    if (trimmedValue === (queryParams === null || queryParams === void 0 ? void 0 : queryParams[searchParamName])) return;
    var pathname = history.location.pathname;
    if (trimmedValue) {
      history.push(general.buildUrl(pathname, ramda.mergeLeft(_defineProperty({}, searchParamName, trimmedValue), queryParams)));
    } else {
      history.push(general.buildUrl(pathname, ramda.omit([searchParamName], queryParams)));
    }
  };
  var handleDebouncedSearchTermChange = useFuncDebounce(function (trimmedValue) {
    if (onSearch) onSearch(trimmedValue);else if (enableUrlSync) handleSearchQueryParams(trimmedValue);
  }, debounceTime);
  useUpdateEffect(function () {
    handleDebouncedSearchTermChange(trimmedValue);
  }, [trimmedValue]);
  var prevValueRef = React.useRef(value);
  if (value !== undefined && value !== prevValueRef.current) {
    setInternalValue(value);
    prevValueRef.current = value;
  }
  var handleOnChange = function handleOnChange(e) {
    var _e$target$value;
    var trimmedValue = (_e$target$value = e.target.value) === null || _e$target$value === void 0 ? void 0 : _e$target$value.trim();
    onChange === null || onChange === void 0 || onChange(e);
    setInternalValue(e.target.value);
    if ((internalValue === null || internalValue === void 0 ? void 0 : internalValue.trim()) !== trimmedValue) {
      handleDebouncedSearchTermChange(trimmedValue);
    }
  };
  React.useEffect(function () {
    return handleDebouncedSearchTermChange.cancel;
  }, []);
  return /*#__PURE__*/jsxRuntime.jsx(neetoAtoms.Input, _objectSpread({
    className: className,
    label: label,
    id: inputId,
    placeholder: t("neetoMolecules.common.actions.search"),
    prefix: /*#__PURE__*/jsxRuntime.jsx(search.Search, {}),
    type: "search",
    value: internalValue,
    onChange: handleOnChange
  }, inputProps));
};

module.exports = Search;
//# sourceMappingURL=Search.js.map
