var _excluded = ["onChange", "onClear", "onSubmit", "usingHook", "value"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { Clear, Search as SearchIcon } from '@procore/core-icons/dist';
import { mergeProps } from '@react-aria/utils';
import React from 'react';
import { useI18nContext } from '../_hooks/I18n';
import { mergeRefs } from '../_utils/mergeRefs';
import { usingHookOrDefault } from '../_utils/usingHookOrDefault';
import { useSearch } from './Search.hooks';
import { StyledClearButton, StyledInput, StyledSearch, StyledSearchButton } from './Search.styles';
/**

 Our users use search to find items on a tool’s landing page or, in forms, users
 use a search typeahead to find information to associate with the form.

 @since 10.19.0

 @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-search--demo)

 @see [Design Guidelines](https://design.procore.com/search-component)

 */
export var Search = /*#__PURE__*/React.forwardRef(function Search(_ref, ref) {
  var _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
    _ref$onClear = _ref.onClear,
    onClear = _ref$onClear === void 0 ? function () {} : _ref$onClear,
    _ref$onSubmit = _ref.onSubmit,
    onSubmit = _ref$onSubmit === void 0 ? function () {} : _ref$onSubmit,
    usingHook = _ref.usingHook,
    _ref$value = _ref.value,
    value = _ref$value === void 0 ? '' : _ref$value,
    props = _objectWithoutProperties(_ref, _excluded);
  var i18n = useI18nContext();
  var search = usingHookOrDefault(usingHook, useSearch)({
    initialValue: value,
    onChange: onChange,
    onClear: onClear,
    onSubmit: onSubmit
  });
  var inputRef = React.useRef(null);
  var setFocusToInput = function setFocusToInput() {
    if (inputRef.current) {
      inputRef.current.focus();
    }
  };
  return /*#__PURE__*/React.createElement(StyledSearch, null, /*#__PURE__*/React.createElement(StyledInput, _extends({}, mergeProps(props, search.domHandlers), {
    "data-qa": "core-search-input",
    ref: mergeRefs(ref, inputRef),
    value: search.value
  })), !search.isEmpty && /*#__PURE__*/React.createElement(StyledClearButton, {
    "aria-label": i18n.t('core.search.clear'),
    "data-qa": "core-search-clear-icon",
    icon: /*#__PURE__*/React.createElement(Clear, null),
    size: "sm",
    variant: "tertiary",
    disabled: props.disabled,
    onClick: function onClick() {
      search.clear();
      setFocusToInput();
    },
    tabIndex: -1 // TODO revisit this accessibility
  }), /*#__PURE__*/React.createElement(StyledSearchButton, {
    "aria-label": i18n.t('core.search.goSearch'),
    "data-qa": "core-search-search-icon",
    icon: /*#__PURE__*/React.createElement(SearchIcon, null),
    size: "sm",
    variant: "tertiary",
    disabled: props.disabled,
    onClick: function onClick() {
      search.submit();
      setFocusToInput();
    },
    tabIndex: -1 // TODO revisit this accessibility
  }));
});

Search.displayName = 'Search';
//# sourceMappingURL=Search.js.map