import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { useId, useState, useRef, useEffect } from 'react';
import { Input } from '@bigbinary/neeto-atoms';
import useFuncDebounce from '@bigbinary/neeto-commons-frontend/v2/react-utils/useFuncDebounce';
import useQueryParams from '@bigbinary/neeto-commons-frontend/v2/react-utils/useQueryParams';
import useUpdateEffect from '@bigbinary/neeto-commons-frontend/v2/react-utils/useUpdateEffect';
import { buildUrl } from '@bigbinary/neeto-commons-frontend/v2/utils/general';
import { mergeLeft, omit } from 'ramda';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router-dom';
import { getQueryParams } from '@bigbinary/neeto-commons-frontend/v2/utils';
import { jsx } from 'react/jsx-runtime';
import { S as Search$1 } from '../search-DCgpNjip.js';
import '../createLucideIcon-mNMhCCpf.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 = 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 = useTranslation(),
    t = _useTranslation.t;
  var queryParams = useQueryParams({
    toCamelCase: false
  });
  var history = useHistory();
  var inputId = useId();
  var _useState = 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(buildUrl(pathname, mergeLeft(_defineProperty({}, searchParamName, trimmedValue), queryParams)));
    } else {
      history.push(buildUrl(pathname, 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 = 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);
    }
  };
  useEffect(function () {
    return handleDebouncedSearchTermChange.cancel;
  }, []);
  return /*#__PURE__*/jsx(Input, _objectSpread({
    className: className,
    label: label,
    id: inputId,
    placeholder: t("neetoMolecules.common.actions.search"),
    prefix: /*#__PURE__*/jsx(Search$1, {}),
    type: "search",
    value: internalValue,
    onChange: handleOnChange
  }, inputProps));
};

export { Search as default };
//# sourceMappingURL=Search.js.map
