"use strict";

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