import React, { forwardRef, useState, useEffect, useRef } from 'react';
import classnames from 'classnames';
import { faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons/faMagnifyingGlass';
import { faXmark } from '@fortawesome/free-solid-svg-icons/faXmark';
import { FontAwesomeIcon } from '../font-awesome-icon';
import { Input, INPUT_SIZES } from '../input';
import { Button, ACCENT_TYPES } from '../button';
import { useTranslation } from '../hooks/use-translation';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export const SEARCH_MODE_TYPES = {
  DEFAULT: 'default',
  LARGE: 'large'
};
/**
 * Search Field with input clearing support
 * @midnight-gray-supported
 * @alpine-light-supported
 */
export const Search = /*#__PURE__*/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
  } = useTranslation();
  const [searchQuery, setSearchQuery] = useState(value);
  const lastOnChane = useRef(value);
  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__*/_jsxs("div", {
    className: classnames('search-wrapper', className),
    "test-id": "search-component-wrapper",
    children: [/*#__PURE__*/_jsx(Input, {
      value: searchQuery || '',
      className: classnames('search-control', inputClassName),
      inputSize: mode === SEARCH_MODE_TYPES.LARGE ? INPUT_SIZES.XL : 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,
      autoComplete: autoComplete,
      isDisabled: isDisabled,
      ref: ref,
      "aria-label": inputAriaLabel ?? t('Search'),
      onKeyDown: onKeyDown,
      role: role
    }), searchQuery?.length ? /*#__PURE__*/_jsx(Button, {
      className: "over-input-icon clear-icon",
      testId: "clear-search",
      onClick: clearSearch,
      accentType: ACCENT_TYPES.COMMAND,
      isDisabled: isDisabled,
      "aria-label": ariaLabelClearInput ?? t('Clear Input'),
      children: /*#__PURE__*/_jsx(FontAwesomeIcon, {
        icon: faXmark
      })
    }) : null]
  });
});
Search.displayName = 'Search';