/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-7eefd09
 * Released under the MIT license.
 */

import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { forwardRef, useRef } from 'react';
import { useSearchField } from 'react-aria';
import { useSearchFieldState } from 'react-stately';
import { CloseIcon, SearchIcon } from '../../../icons/index.js';
import { useProviderProps } from '../../../provider.js';
import { mergeProps } from '../../../utils/react/index.js';
import { ariaToCubeButtonProps } from '../../../utils/react/mapProps.js';
import { castNullableStringValue, } from '../../../utils/react/nullableValue.js';
import { ItemAction } from '../../actions/index.js';
import { TextInputBase } from '../TextInput/index.js';
export { useSearchFieldState, useSearchField };
export 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;
    // Merge user-provided labelProps with aria labelProps
    const mergedLabelProps = mergeProps(labelProps, userLabelProps);
    return (_jsx(TextInputBase, { ref: ref, labelProps: mergedLabelProps, inputProps: { ...inputProps, 'data-input-type': 'searchinput' }, inputRef: inputRef, type: "search", icon: _jsx(SearchIcon, {}), suffixPosition: "after", validationState: validationState, ...restProps, suffix: restProps.suffix || showClearButton ? (_jsxs(_Fragment, { children: [restProps.suffix, showClearButton && (_jsx(ItemAction, { icon: _jsx(CloseIcon, {}), size: restProps.size, type: validationState === 'invalid' ? 'clear' : 'neutral', theme: validationState === 'invalid' ? 'danger' : undefined, ...ariaToCubeButtonProps(clearButtonProps), onPress: (e) => {
                        // Call the original clear functionality
                        clearButtonProps.onPress?.();
                        // Call the onClear callback
                        onClear?.();
                    } }))] })) : undefined }));
});

        