import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useKey_mjs__ from "../../hooks/useKey.mjs";
import * as __WEBPACK_EXTERNAL_MODULE_react_icons_md__ from "react-icons/md";
import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__ from "@chakra-ui/react";
const SearchField = ({ setSearchText, searchText, placeholderText = 'Search...', icon = 'search', isDisabled = false, ...chakraProps })=>{
    // State
    const isEmpty = !searchText || '' === searchText;
    const inputRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    // Actions
    const isAlreadyFocused = ()=>document.activeElement && inputRef?.current && document.activeElement !== inputRef?.current && 'INPUT' === document.activeElement.tagName;
    const reset = ()=>setSearchText('');
    const focus = ()=>!isDisabled && !isAlreadyFocused() && inputRef?.current && inputRef.current.focus();
    const blur = ()=>inputRef?.current && inputRef.current.blur();
    // Keyboard shortcuts
    (0, __WEBPACK_EXTERNAL_MODULE__hooks_useKey_mjs__["default"])('Escape', isEmpty ? blur : reset);
    (0, __WEBPACK_EXTERNAL_MODULE__hooks_useKey_mjs__["default"])('/', focus);
    const tagColorScheme = (0, __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.useColorModeValue)('gray', 'light');
    return /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.InputGroup, chakraProps, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.InputLeftElement, {
        width: "2rem"
    }), /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Tooltip, {
        isDisabled: isDisabled,
        placement: "bottom",
        hasArrow: true,
        py: 1,
        label: isEmpty ? /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement("span", null, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Tag, {
            colorScheme: tagColorScheme,
            size: "sm"
        }, "/"), ' ', "to focus") : /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement("span", null, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Tag, {
            colorScheme: tagColorScheme,
            size: "sm"
        }, "Esc"), ' ', "to clear")
    }, isEmpty ? /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.InputLeftElement, {
        onClick: focus
    }, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Icon, {
        "data-testid": "search-field-search-icon",
        as: 'filter' === icon ? __WEBPACK_EXTERNAL_MODULE_react_icons_md__.MdOutlineFilterList : __WEBPACK_EXTERNAL_MODULE_react_icons_md__.MdSearch,
        color: "gray.300",
        "aria-label": "search-icon-focus"
    })) : /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.InputLeftElement, {
        onClick: reset
    }, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Icon, {
        "data-testid": "search-field-reset-icon",
        as: __WEBPACK_EXTERNAL_MODULE_react_icons_md__.MdClose,
        color: "gray.300",
        "aria-label": "search-icon-reset"
    }))), /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Input, {
        "data-testid": "search-field-input",
        ref: inputRef,
        type: "text",
        className: "search-control",
        placeholder: placeholderText,
        onChange: (event)=>setSearchText(event.target.value),
        value: searchText,
        isDisabled: isDisabled
    }));
};
/* ESM default export */ const SearchField_rslib_entry_ = SearchField;
export { SearchField_rslib_entry_ as default };
