'use strict';

var React = require('react');
var Tooltip = require('./Tooltip-cjs.js');
var Button = require('./Button-cjs.js');
require('classnames');
var DataList_utils = require('./DataList.utils-cjs.js');
var DataListLayoutContext = require('./DataListLayoutContext-cjs2.js');
var DataListItemActionsOverflow = require('./DataListItemActionsOverflow-cjs.js');

function DataListActions({ children, itemsToExpose = 2, }) {
    const { activeItem } = DataListLayoutContext.useDataListLayoutActionsContext();
    const childrenArray = React.Children.toArray(children).filter(React.isValidElement);
    const exposedActions = DataList_utils.getExposedActions(childrenArray, itemsToExpose);
    childrenArray.splice(0, exposedActions.length);
    return (React.createElement(React.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.createElement(Button.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.createElement(Tooltip.Tooltip, { key: actionLabel, message: actionLabel },
                React.createElement(Button.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.createElement(DataListItemActionsOverflow.DataListItemActionsOverflow, { actions: childrenArray })));
}

exports.DataListActions = DataListActions;
