import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2024-2026 GoodData Corporation
import { forwardRef, useRef } from "react";
import { simplifyText } from "@gooddata/util";
import { bem } from "../@utils/bem.js";
import { makeKeyboardNavigation } from "../@utils/keyboardNavigation.js";
import { UiIcon } from "../UiIcon/UiIcon.js";
import { UiTooltip } from "../UiTooltip/UiTooltip.js";
const { b, e } = bem("gd-ui-kit-breadcrumb");
const getGeneratedTestId = (label, ariaLabel) => {
    return ariaLabel ? `${simplifyText(ariaLabel)}` : `${simplifyText(label)}`;
};
/**
 * @internal
 */
export const UiBreadcrumb = forwardRef(({ id, label, tabIndex = 0, dataId, dataTestId, accessibilityConfig, maxWidth, items, onSelect }, ref) => {
    const refs = useRef([]);
    const testId = dataTestId || getGeneratedTestId(label, accessibilityConfig?.ariaLabel ?? "");
    const onKeyDown = makeKeyboardNavigation({
        onSelect: [{ code: ["Enter", "Space"] }],
    })({
        onSelect: (e) => {
            e.target.click();
        },
    });
    return (_jsx("nav", { id: id, ref: ref, className: b(), onKeyDown: onKeyDown, tabIndex: tabIndex, "data-id": dataId, "data-testid": testId, "aria-label": accessibilityConfig?.ariaLabel, "aria-labelledby": accessibilityConfig?.ariaLabelledBy, "aria-describedby": accessibilityConfig?.ariaDescribedBy, "aria-expanded": accessibilityConfig?.ariaExpanded, "aria-description": accessibilityConfig?.ariaDescription, "aria-controls": accessibilityConfig?.ariaControls, "aria-haspopup": accessibilityConfig?.ariaHaspopup, "aria-current": "true", style: { maxWidth }, children: _jsx("ol", { children: items.map((item, index) => (_jsxs("li", { children: [
                    _jsx("button", { ref: (item) => {
                            refs.current[index] = item;
                        }, className: e("item"), "aria-label": item.accessibilityConfig?.ariaLabel ?? item.label, "aria-labelledby": item.accessibilityConfig?.ariaLabelledBy, "aria-describedby": item.accessibilityConfig?.ariaDescribedBy, "aria-expanded": item.accessibilityConfig?.ariaExpanded, "aria-description": item.accessibilityConfig?.ariaDescription, "aria-controls": item.accessibilityConfig?.ariaControls, "aria-haspopup": item.accessibilityConfig?.ariaHaspopup, "aria-current": index === items.length - 1, onClick: () => {
                            onSelect?.(item);
                        }, children: item.tooltip ? (_jsx(UiTooltip, { anchor: item.label, content: item.tooltip, triggerBy: ["hover", "focus"] })) : (item.label) }), index < items.length - 1 ? (_jsx("div", { className: e("separator"), children: _jsx(UiIcon, { type: "navigateRight", size: 16, color: "complementary-5" }) })) : null] }, item.id))) }) }));
});
UiBreadcrumb.displayName = "UiBreadcrumb";
