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, getItemProps, 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, getItemProps: getItemProps, registerItemRef: registerItemRef, selectOption: selectOption }));
}
export const UiComboboxListItemImpl = memo(function UiComboboxListItemImpl(props) {
    const { option, index, className, children, onClick, isActive, isSelected, isDisabled, getItemProps, registerItemRef, selectOption, ...htmlProps } = props;
    // https://floating-ui.com/docs/useRole#component-roles
    const itemProps = getItemProps({
        ...htmlProps,
        active: isActive,
        selected: isSelected,
    });
    function handleClick(event) {
        if (isDisabled) {
            event.preventDefault();
            return;
        }
        selectOption(option, index);
        onClick?.(event);
    }
    return (_jsx("li", { ...itemProps, 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, children: children ?? (_jsxs(_Fragment, { children: [
                _jsx(UiComboboxListItemLabel, { children: option.label }), option.creatable ? _jsx(UiComboboxListItemCreatableLabel, {}) : null] })) }));
});
/**
 * Renders the primary label content within a combobox list item.
 * Use this component for composable customization of list item content.
 *
 * @internal
 */
export function UiComboboxListItemLabel(props) {
    const { children, className, ...htmlProps } = props;
    return (_jsx("span", { ...htmlProps, className: cx(e("item-label"), className), children: children }));
}
/**
 * Renders the "creatable" label suffix within a combobox list item.
 * Use this component for composable customization of list item content.
 *
 * @internal
 */
export function UiComboboxListItemCreatableLabel(props) {
    const { children = "(create new)", className, ...htmlProps } = props;
    return (_jsx("span", { ...htmlProps, className: cx(e("item-creatable"), className), children: children }));
}
//# sourceMappingURL=UiComboboxListItem.js.map