import { createElement as _createElement } from "react";
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { forwardRef, useRef, useState } from 'react';
import { useConsolidatedRef, useI18n, useUID } from '../../hooks';
import Button from '../Button';
import Icon from '../Icon';
import MenuButton from '../MenuButton';
import { createStringMatcher, isInstance } from '../../utils';
export default forwardRef(function Actions({ 
// @ts-expect-error
'data-testid': dataTestId, items, menuAt = 2, filterAt, scrollAt, progress, iconOnly = true, menuButtonProps, contextualLabel, compact = false, disabled = false, tabIndex }, ref) {
    const t = useI18n();
    const menuRef = useRef(null);
    const menuButtonRef = useConsolidatedRef(ref);
    const filterElRef = useConsolidatedRef(null);
    const filterListId = useUID();
    const [filterValue, setFilterValue] = useState('');
    if (!items) {
        return null;
    }
    const renderFilter = typeof filterAt === 'number' && items.length >= filterAt;
    const filterRegex = createStringMatcher(filterValue, 'contains');
    const itemsToRender = renderFilter ? items.filter(item => filterRegex.test(item.text)) : items;
    const onBlur = (e) => {
        if (isInstance(e.relatedTarget, Node) && menuRef.current?.contains(e.relatedTarget)) {
            return;
        }
        setFilterValue('');
    };
    const filterInputProps = renderFilter
        ? {
            value: filterValue,
            ref: filterElRef,
            onChange: (e) => setFilterValue?.(e.target.value),
            onBlur
        }
        : undefined;
    return items.length >= menuAt ? (_jsx(MenuButton, { ...(dataTestId ? { 'data-testid': dataTestId } : {}), ref: menuButtonRef, text: t('actions'), "aria-label": contextualLabel && t('actions_for', [contextualLabel]), iconOnly: true, icon: 'more', variant: 'simple', compact: compact, onClick: menuButtonProps?.onClick, onKeyDown: menuButtonProps?.onKeyDown, disabled: disabled, tabIndex: tabIndex, menu: {
            scrollAt,
            filterInputProps: progress ? undefined : filterInputProps,
            listId: filterListId,
            items: itemsToRender.map(({ text, onClick, ...restProps }) => ({
                ...restProps,
                primary: text,
                onClick: onClick
                    ? (id, e) => {
                        onClick(id, e, menuButtonRef.current ?? undefined);
                        menuButtonRef.current?.focus();
                    }
                    : undefined
            })),
            loading: progress
        } })) : (_jsx(_Fragment, { children: items.map(({ id, icon, text, onClick, ...restProps }, index) => {
            return (_createElement(Button, { ...(dataTestId ? { 'data-testid': dataTestId } : {}), ref: index === 0 ? menuButtonRef : undefined, key: id, variant: icon && iconOnly ? 'simple' : undefined, onClick: (event) => onClick?.(id, event), label: icon ? text : undefined, icon: !!icon && iconOnly, compact: compact, "aria-label": contextualLabel ? `${text} - ${contextualLabel}` : text, disabled: disabled, ...restProps }, icon && iconOnly ? _jsx(Icon, { name: icon }) : text));
        }) }));
});
//# sourceMappingURL=Actions.js.map