import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025 GoodData Corporation
import { useCallback } from "react";
import cx from "classnames";
import { ShortenedText } from "../../../../ShortenedText/index.js";
import { typedUiMenuContextStore } from "../../context.js";
import { e } from "../../menuBem.js";
/**
 * @internal
 */
export function DefaultUiMenuInteractiveItemWrapper({ item, }) {
    const { useContextStore, createSelector } = typedUiMenuContextStore();
    const selector = createSelector((ctx) => ({
        onSelect: ctx.onSelect,
        controlType: ctx.controlType,
        scrollToView: ctx.scrollToView,
        setFocusedId: ctx.setFocusedId,
        makeItemId: ctx.makeItemId,
        itemClassName: ctx.itemClassName,
        itemDataTestId: ctx.itemDataTestId,
        InteractiveItemComponent: ctx.InteractiveItem,
        isFocused: ctx.focusedItem?.id === item.id,
    }));
    const { onSelect, scrollToView, controlType, setFocusedId, makeItemId, itemClassName, itemDataTestId, InteractiveItemComponent, isFocused, } = useContextStore(selector);
    const scrollToItem = (element) => {
        if (!element || !isFocused) {
            return;
        }
        scrollToView(element);
    };
    const handleMouseFocus = useCallback(() => {
        if (controlType !== "mouse") {
            return;
        }
        setFocusedId(item.id);
    }, [controlType, item.id, setFocusedId]);
    const handleSelect = useCallback(() => {
        if (item.isDisabled) {
            return;
        }
        onSelect(item);
    }, [item, onSelect]);
    const classNames = cx(e("item-wrapper", {
        isFocused,
        isDisabled: !!item.isDisabled,
    }), typeof itemClassName === "function" ? itemClassName(item) : itemClassName);
    const dataTestId = typeof itemDataTestId === "function" ? itemDataTestId(item) : itemDataTestId;
    return (_jsx("li", { ref: scrollToItem, role: "menuitem", "aria-haspopup": item.subItems ? "menu" : undefined, "aria-disabled": item.isDisabled, onMouseMove: handleMouseFocus, tabIndex: -1, id: makeItemId(item), className: classNames, "data-testid": dataTestId, children: _jsx(InteractiveItemComponent, { item: item, isFocused: isFocused, onSelect: handleSelect }) }));
}
/**
 * @internal
 */
export function DefaultUiMenuInteractiveItem({ item, isFocused, onSelect, size = "medium", }) {
    return (_jsxs("div", { className: e("item", {
            isFocused,
            isSelected: !!item.isSelected,
            isDisabled: !!item.isDisabled,
            size,
        }), onClick: onSelect, children: [_jsx(ShortenedText, { className: e("item-title"), ellipsisPosition: "end", children: item.stringTitle }), !!item.subItems && _jsx("i", { className: "gd-icon-navigateright" })] }));
}
//# sourceMappingURL=DefaultUiMenuInteractiveItem.js.map