"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _useDebounce = _interopRequireDefault(require("../../hooks/useDebounce"));
var _usePrevious = _interopRequireDefault(require("../../hooks/usePrevious"));
var _icon = require("../icon");
var _input = require("../input");
var _button = require("../button");
var _excluded = ["value", "onChange", "reset", "onReset", "placeholder", "ref"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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; }
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 = (0, _react.useState)(defaultValue),
    value = _useState[0],
    setValue = _useState[1];
  var prevValue = (0, _usePrevious["default"])(value);
  (0, _useDebounce["default"])(function () {
    if (value === defaultValue && typeof prevValue === "undefined") return;
    onChange == null || onChange(value);
  }, 300, [value]);
  (0, _react.useEffect)(function () {
    if (reset) {
      setValue("");
    }
  }, [reset]);
  return /*#__PURE__*/_react["default"].createElement(_input.TextInput, _extends({
    iconLeft: /*#__PURE__*/_react["default"].createElement(_icon.Icon, {
      name: "search",
      color: value ? "textFocus" : "textLite",
      width: "14px",
      height: "14px"
    }),
    iconRight: (!!value || !!defaultValue) && /*#__PURE__*/_react["default"].createElement(_button.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));
};
var _default = exports["default"] = Search;