var _excluded = ["value", "onChange", "reset", "onReset", "placeholder", "ref"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useEffect, useState } from "react";
import useDebounce from "../../hooks/useDebounce";
import usePrevious from "../../hooks/usePrevious";
import { Icon } from "../icon";
import { TextInput } from "../input";
import { IconButton } from "../button";
var Search = function Search(_ref) {
  var _ref$value = _ref.value,
    defaultValue = _ref$value === void 0 ? "" : _ref$value,
    onChange = _ref.onChange,
    reset = _ref.reset,
    onReset = _ref.onReset,
    placeholder = _ref.placeholder,
    ref = _ref.ref,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = useState(defaultValue),
    value = _useState[0],
    setValue = _useState[1];
  var prevValue = usePrevious(value);
  useDebounce(function () {
    if (value === defaultValue && typeof prevValue === "undefined") return;
    onChange == null || onChange(value);
  }, 300, [value]);
  useEffect(function () {
    if (reset) {
      setValue("");
    }
  }, [reset]);
  return /*#__PURE__*/React.createElement(TextInput, _extends({
    iconLeft: /*#__PURE__*/React.createElement(Icon, {
      name: "search",
      color: value ? "textFocus" : "textLite",
      width: "14px",
      height: "14px"
    }),
    iconRight: (!!value || !!defaultValue) && /*#__PURE__*/React.createElement(IconButton, {
      icon: "x",
      iconColor: value ? "textFocus" : "textLite",
      width: "14px",
      height: "14px",
      onClick: function onClick() {
        onReset == null || onReset();
        setValue("");
      },
      padding: [0],
      neutral: true
    }),
    inputRef: ref,
    value: value,
    onChange: function onChange(e) {
      return setValue(e.target.value);
    },
    placeholder: placeholder,
    size: "small",
    title: "search"
  }, rest));
};
export default Search;