import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { memo } from "react";
import cx from "classnames";
import { e } from "./comboboxBem.js";
import { useComboboxState } from "./UiComboboxContext.js";
/** @internal */
export function UiComboboxListItem(props) {
    const { option } = props;
    const { registerItemRef, setActiveIndex, selectOption, activeOption, selectedOption } = useComboboxState();
    const isDisabled = Boolean(option.disabled);
    const isActive = activeOption?.id === option.id;
    const isSelected = selectedOption?.id === option.id;
    return (_jsx(UiComboboxListItemImpl, { ...props, isActive: isActive, isSelected: isSelected, isDisabled: isDisabled, registerItemRef: registerItemRef, setActiveIndex: setActiveIndex, selectOption: selectOption }));
}
export const UiComboboxListItemImpl = memo(function UiComboboxListItemImpl(props) {
    const { option, index, className, children, onClick, onMouseMove, isActive, isSelected, isDisabled, registerItemRef, setActiveIndex, selectOption, ...htmlProps } = props;
    function handleClick(event) {
        if (isDisabled) {
            event.preventDefault();
            return;
        }
        selectOption(option, index);
        onClick?.(event);
    }
    function handleMouseMove(event) {
        if (!isActive) {
            setActiveIndex(index);
        }
        onMouseMove?.(event);
    }
    return (_jsx("li", { ...htmlProps, ref: (node) => {
            registerItemRef(node, index);
        }, id: option.id, role: "option", "aria-selected": isSelected, "aria-disabled": isDisabled, className: cx(e("item", { isActive, isSelected, isDisabled }), className), onClick: handleClick, onMouseMove: handleMouseMove, children: children ?? (_jsxs(_Fragment, { children: [
                _jsx(UiComboboxListItemLabel, { children: option.label }), option.creatable ? _jsx(UiComboboxListItemCreatableLabel, {}) : null] })) }));
});
/** @internal */
export function UiComboboxListItemLabel(props) {
    const { children, className, ...htmlProps } = props;
    return (_jsx("span", { ...htmlProps, className: cx(e("item-label"), className), children: children }));
}
/** @internal */
export function UiComboboxListItemCreatableLabel(props) {
    const { children = "(create new)", className, ...htmlProps } = props;
    return (_jsx("span", { ...htmlProps, className: cx(e("item-creatable"), className), children: children }));
}
