"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Search = exports.SEARCH_MODE_TYPES = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faMagnifyingGlass = require("@fortawesome/free-solid-svg-icons/faMagnifyingGlass");
var _faXmark = require("@fortawesome/free-solid-svg-icons/faXmark");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _input = require("../input");
var _button = require("../button");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const SEARCH_MODE_TYPES = exports.SEARCH_MODE_TYPES = {
  DEFAULT: 'default',
  LARGE: 'large'
};
/**
 * Search Field with input clearing support
 * @midnight-gray-supported
 * @alpine-light-supported
 */
const Search = exports.Search = /*#__PURE__*/(0, _react.forwardRef)(({
  onBlur,
  onClear,
  onChange,
  onKeyDown,
  onFocus,
  className,
  inputClassName,
  value = '',
  placeholder,
  ariaLabelClearInput,
  isDisabled = false,
  testId = 'search-input',
  mode = SEARCH_MODE_TYPES.DEFAULT,
  autoFocus = true,
  autoComplete,
  id = 'search-control',
  inputAriaLabel,
  role
}, ref) => {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const [searchQuery, setSearchQuery] = (0, _react.useState)(value);
  const lastOnChane = (0, _react.useRef)(value);
  (0, _react.useEffect)(() => {
    updateState(value);
  }, [value]);
  function updateState(updatedValue) {
    setSearchQuery(updatedValue);
    if (lastOnChane.current !== updatedValue) {
      lastOnChane.current = updatedValue;
      onChange(updatedValue);
    }
  }
  function clearSearch() {
    updateState('');
    if (onClear) {
      return onClear();
    }
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)('search-wrapper', className),
    "test-id": "search-component-wrapper",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_input.Input, {
      value: searchQuery || '',
      className: (0, _classnames.default)('search-control', inputClassName),
      inputSize: mode === SEARCH_MODE_TYPES.LARGE ? _input.INPUT_SIZES.XL : _input.INPUT_SIZES.MD,
      onChange: ({
        target
      }) => updateState(target.value),
      onFocus: onFocus,
      onBlur: ({
        target
      }) => {
        let onBlurValue = target.value;
        if (target.value !== target.value.trim()) {
          onBlurValue = target.value.trim();
          updateState(onBlurValue);
        }
        return onBlur && onBlur(onBlurValue);
      },
      autoFocus: autoFocus,
      placeholder: placeholder ?? t('Search'),
      "test-id": testId,
      id: id,
      leftIcon: _faMagnifyingGlass.faMagnifyingGlass,
      autoComplete: autoComplete,
      isDisabled: isDisabled,
      ref: ref,
      "aria-label": inputAriaLabel ?? t('Search'),
      onKeyDown: onKeyDown,
      role: role
    }), searchQuery?.length ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
      className: "over-input-icon clear-icon",
      testId: "clear-search",
      onClick: clearSearch,
      accentType: _button.ACCENT_TYPES.COMMAND,
      isDisabled: isDisabled,
      "aria-label": ariaLabelClearInput ?? t('Clear Input'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
        icon: _faXmark.faXmark
      })
    }) : null]
  });
});
Search.displayName = 'Search';