var _excluded = ["value", "setSearchTerm", "setSearchView", "controlled"];
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, useCallback } from "react";
import useDebounce from "../../../hooks/useDebounce";
import { TextInput } from "../../../components/input";
import { Icon } from "../../../components/icon";
var SearchInput = function SearchInput(_ref) {
  var defaultValue = _ref.value,
    setSearchTerm = _ref.setSearchTerm,
    setSearchView = _ref.setSearchView,
    controlled = _ref.controlled,
    props = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = useState(defaultValue),
    value = _useState[0],
    setValue = _useState[1];
  useDebounce(function () {
    setSearchTerm(value);
    if (value.length < 3) return;
    setSearchView();
  }, 300, [value]);
  var onChange = useCallback(function (e) {
    return setValue(e.target.value);
  }, []);
  useEffect(function () {
    if (!controlled) return;
    setValue(defaultValue);
  }, [defaultValue]);
  return /*#__PURE__*/React.createElement(TextInput, _extends({
    value: value,
    onChange: onChange,
    placeholder: "Search Netdata\u2019s docs & community",
    autoFocus: true,
    iconLeft: /*#__PURE__*/React.createElement(Icon, {
      name: "search_s",
      size: "small",
      color: value ? "text" : "border"
    })
  }, props));
};
export default SearchInput;