'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 neetoCist = require('@bigbinary/neeto-cist');
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 Search$1 = require('@bigbinary/neeto-icons/Search');
var Input = require('@bigbinary/neetoui/Input');
var ramda = require('ramda');
var reactI18next = require('react-i18next');
var reactRouterDom = require('react-router-dom');
var injectCss = require('./inject-css-B6qYtOJe.js');
var utils = require('@bigbinary/neeto-commons-frontend/utils');
var jsxRuntime = require('react/jsx-runtime');

var css = ".neeto-molecules-search__wrapper input[type=search]::-webkit-search-cancel-button:hover,.neeto-molecules-search__wrapper input[type=search]::-webkit-search-decoration:hover{cursor:pointer}";
injectCss.n(css,{});

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"];
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,
    inputProps = _objectWithoutProperties(_ref, _excluded);
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var queryParams = useQueryParams({
    toCamelCase: false
  });
  var history = reactRouterDom.useHistory();
  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 () {
    if (!enableUrlSync || neetoCist.isPresent(queryParams === null || queryParams === void 0 ? void 0 : queryParams[searchParamName])) return;
    setInternalValue("");
  }, [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 searchParams = {};
    var pathname = window.location.pathname;
    if (trimmedValue) searchParams = _defineProperty({}, searchParamName, trimmedValue);else queryParams === null || queryParams === void 0 || delete queryParams[searchParamName];
    history.push(general.buildUrl(pathname, ramda.mergeLeft(searchParams, queryParams)));
  };
  var handleDebouncedSearchTermChange = useFuncDebounce(function (trimmedValue) {
    if (onSearch) onSearch(trimmedValue);else if (enableUrlSync) handleSearchQueryParams(trimmedValue);
  }, debounceTime);
  useUpdateEffect(function () {
    handleDebouncedSearchTermChange(trimmedValue);
  }, [trimmedValue]);
  if (value !== undefined && internalValue !== value) {
    setInternalValue(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(Input, _objectSpread({
    className: "neeto-molecules-search__wrapper",
    placeholder: t("neetoMolecules.common.actions.search"),
    prefix: /*#__PURE__*/jsxRuntime.jsx(Search$1, {}),
    type: "search",
    value: internalValue,
    onChange: handleOnChange
  }, inputProps));
};

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