import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { forwardRef, useCallback, } from "react";
import { UiTextInput } from "../UiTextInput/UiTextInput.js";
import { useComboboxState } from "./UiComboboxContext.js";
/**
 * Binds `UiTextInput` to the surrounding `UiCombobox` state: value, keyboard
 * handler, open/close, ARIA combobox+listbox attributes.
 *
 * @internal
 */
export const UiComboboxInput = forwardRef(function UiComboboxInput(props, forwardedRef) {
    const { accessibilityConfig, placeholder, name, autoFocus, onKeyDown: callerOnKeyDown, onFocus: callerOnFocus, onBlur: callerOnBlur, onClick: callerOnClick, dataTestId, } = props;
    const { inputValue, onInputChange, onInputKeyDown, onInputBlur, isOpen, setIsOpen, anchorRef, activeOption, listboxId, } = useComboboxState();
    const handleKeyDown = useCallback((event) => {
        onInputKeyDown(event);
        if (!event.isDefaultPrevented()) {
            callerOnKeyDown?.(event);
        }
    }, [onInputKeyDown, callerOnKeyDown]);
    const handleBlur = useCallback((event) => {
        onInputBlur();
        callerOnBlur?.(event);
    }, [onInputBlur, callerOnBlur]);
    // Click toggles open state (matches floating-ui's default `useClick`),
    // so the user can also click to close. Focus alone doesn't open —
    // Tab / programmatic focus shouldn't expand the listbox.
    const handleClick = useCallback((event) => {
        setIsOpen(!isOpen);
        callerOnClick?.(event);
    }, [isOpen, setIsOpen, callerOnClick]);
    return (_jsx(UiTextInput, { inputRef: forwardedRef, wrapperRef: anchorRef, value: inputValue, onChange: onInputChange, name: name, placeholder: placeholder, autoFocus: autoFocus, 
        // Browser autofill would overlap the listbox; the combobox
        // owns its own typeahead so we suppress all native suggestions.
        autoComplete: "off", autoCapitalize: "none", autoCorrect: "off", dataTestId: dataTestId, onKeyDown: handleKeyDown, onFocus: callerOnFocus, onBlur: handleBlur, onClick: handleClick, accessibilityConfig: {
            ...accessibilityConfig,
            role: "combobox",
            ariaAutocomplete: "list",
            ariaExpanded: isOpen,
            ariaActiveDescendant: activeOption?.id,
            ariaHaspopup: "listbox",
            ariaControls: listboxId,
        } }));
});
