import React__default, { Children, isValidElement } from 'react';
import { T as Tooltip } from './Tooltip-es.js';
import { B as Button } from './Button-es.js';
import 'classnames';
import { b as getExposedActions } from './DataList.utils-es.js';
import { u as useDataListLayoutActionsContext } from './DataListLayoutContext-es2.js';
import { D as DataListItemActionsOverflow } from './DataListItemActionsOverflow-es.js';

function DataListActions({ children, itemsToExpose = 2, }) {
    const { activeItem } = useDataListLayoutActionsContext();
    const childrenArray = Children.toArray(children).filter(isValidElement);
    const exposedActions = getExposedActions(childrenArray, itemsToExpose);
    childrenArray.splice(0, exposedActions.length);
    return (React__default.createElement(React__default.Fragment, null,
        exposedActions.map(({ props }) => {
            const isVisible = props.visible ? props.visible(activeItem) : true;
            const hasIconOrAlwaysVisible = props.icon || props.alwaysVisible;
            if (!isVisible || !hasIconOrAlwaysVisible) {
                return null;
            }
            function getActionLabel() {
                if (typeof props.label === "string") {
                    return props.label;
                }
                if (activeItem) {
                    return props.label(activeItem);
                }
                return "";
            }
            const actionLabel = getActionLabel();
            // If the action is always visible, we don't want a tooltip.
            if (props.alwaysVisible) {
                return (React__default.createElement(Button, { ariaLabel: actionLabel, key: actionLabel, icon: props.icon, label: actionLabel, onClick: () => {
                        var _a;
                        if (activeItem)
                            (_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, activeItem);
                    }, type: "secondary", variation: "subtle" }));
            }
            return (React__default.createElement(Tooltip, { key: actionLabel, message: actionLabel },
                React__default.createElement(Button, { icon: props.icon, ariaLabel: actionLabel, onClick: () => {
                        var _a, _b;
                        if (activeItem) {
                            (_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, activeItem);
                        }
                        else {
                            (_b = props.onClick) === null || _b === void 0 ? void 0 : _b.call(props);
                        }
                    }, type: "tertiary", variation: "subtle" })));
        }),
        React__default.createElement(DataListItemActionsOverflow, { actions: childrenArray })));
}

export { DataListActions as D };
