var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import { Clear, Search as SearchIcon } from '@procore/core-icons/dist';
import React from 'react';
import { mergeRefs } from '../../effects/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/latest/?path=/story/demos-search--demo)

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

 */
export var Search = React.forwardRef(function Search(_a, ref) {
    var _b = _a.onChange, onChange = _b === void 0 ? function () { } : _b, _c = _a.onClear, onClear = _c === void 0 ? function () { } : _c, _d = _a.onSubmit, onSubmit = _d === void 0 ? function () { } : _d, _e = _a.typeahead, typeahead = _e === void 0 ? false : _e, usingHook = _a.usingHook, _f = _a.value, value = _f === void 0 ? '' : _f, props = __rest(_a, ["onChange", "onClear", "onSubmit", "typeahead", "usingHook", "value"]);
    var search = usingHookOrDefault(usingHook, useSearch)({
        initialValue: value,
        onChange: onChange,
        onClear: onClear,
        onSubmit: onSubmit,
        typeahead: typeahead,
    });
    var inputRef = React.useRef(null);
    var setFocusToInput = function () {
        if (inputRef.current) {
            inputRef.current.focus();
        }
    };
    return (React.createElement(StyledSearch, { "$typeahead": typeahead },
        React.createElement(StyledInput, __assign({}, props, search.domHandlers, { "data-qa": "core-search-input", ref: mergeRefs(ref, inputRef), value: search.value })),
        !search.isEmpty && (React.createElement(StyledClearButton, { "data-qa": "core-search-clear-icon", icon: React.createElement(Clear, null), size: "sm", variant: "tertiary", disabled: props.disabled, onClick: function () {
                search.clear();
                setFocusToInput();
            }, tabIndex: -1 })),
        search.showSearch && (React.createElement(StyledSearchButton, { "data-qa": "core-search-search-icon", icon: React.createElement(SearchIcon, null), size: "sm", variant: "tertiary", disabled: props.disabled || typeahead, onClick: function () {
                search.submit();
                setFocusToInput();
            }, tabIndex: -1 }))));
});
Search.displayName = 'Search';
export default Search;
//# sourceMappingURL=Search.js.map