/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-a795c5b | Cube Dev Team */
import { mergeProps as mergeProps$1 } from "../../../utils/react/mergeProps.js";
import { useProviderProps } from "../../../provider.js";
import { ItemAction } from "../../actions/ItemAction/ItemAction.js";
import { CloseIcon } from "../../../icons/CloseIcon.js";
import { SearchIcon } from "../../../icons/SearchIcon.js";
import { TextInputBase } from "../TextInput/TextInputBase.js";
import { castNullableStringValue } from "../../../utils/react/nullableValue.js";
import { ariaToCubeButtonProps } from "../../../utils/react/mapProps.js";
import { forwardRef, useRef } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { useSearchField } from "react-aria";
import { useSearchFieldState } from "react-stately";

//#region src/components/fields/SearchInput/SearchInput.tsx
const SearchInput = forwardRef(function SearchInput(props, ref) {
	props = castNullableStringValue(props);
	props = useProviderProps(props);
	let { isClearable, validationState, onClear, labelProps: userLabelProps, ...restProps } = props;
	let inputRef = useRef(null);
	let state = useSearchFieldState(restProps);
	let { labelProps, inputProps, clearButtonProps } = useSearchField(restProps, state, inputRef);
	let showClearButton = isClearable && state.value !== "" && !restProps.isReadOnly;
	return /* @__PURE__ */ jsx(TextInputBase, {
		ref,
		labelProps: mergeProps$1(labelProps, userLabelProps),
		inputProps: {
			...inputProps,
			"data-input-type": "searchinput"
		},
		inputRef,
		type: "search",
		icon: /* @__PURE__ */ jsx(SearchIcon, {}),
		suffixPosition: "after",
		validationState,
		...restProps,
		suffix: restProps.suffix || showClearButton ? /* @__PURE__ */ jsxs(Fragment$1, { children: [restProps.suffix, showClearButton && /* @__PURE__ */ jsx(ItemAction, {
			icon: /* @__PURE__ */ jsx(CloseIcon, {}),
			size: restProps.size,
			type: validationState === "invalid" ? "clear" : "neutral",
			theme: validationState === "invalid" ? "danger" : void 0,
			...ariaToCubeButtonProps(clearButtonProps),
			onPress: (e) => {
				clearButtonProps.onPress?.();
				onClear?.();
			}
		})] }) : void 0
	});
});

//#endregion
export { SearchInput, useSearchField, useSearchFieldState };
//# sourceMappingURL=SearchInput.js.map