import React from 'react';
import { IconButton, PopoverMenu, } from '@wix/design-system';
import { More } from '@wix/wix-ui-icons-common';
import { DisabledActionCellMenu } from '../ActionCell/DisabledActionCellMenu';
import { createPrimaryActionCellPropsWithBI, createSecondaryActionCellPropWithBi, reportOnPopoverMenuShow, } from '../CollectionItemActions/CollectionItemActionsHelper';
import { getDefaultActionCellProps } from '../CollectionItemActions/getDefaultActionCellProps';
import { useExtensionMenuItems } from '../../hooks/useExtensionMenuItems';
export function evaluateActionCell({ actionCell, item, index, gridState, keyedItem, itemActionsState, }) {
    if (!actionCell) {
        return {};
    }
    if (typeof actionCell === 'function') {
        return actionCell(item, index, {
            openConfirmDeleteModal: itemActionsState.openConfirmDeleteModal,
            openConfirmModal: itemActionsState.openConfirmModal,
            collection: gridState.collection,
            keyedItem,
        });
    }
    return actionCell;
}
export function useGridItemActions(actionCell) {
    const actions = evaluateActionCell(actionCell);
    let settingsMenu = null;
    let primaryAction = null;
    const extensionMenuItems = useExtensionMenuItems({
        containerId: actions.containerId,
    });
    const secondaryActionCellPropsWithBI = createSecondaryActionCellPropWithBi({
        toolbarBIReporter: actionCell.gridState.toolbar.toolbarBIReporter,
        actionCellProps: actions,
        keyedItem: actionCell.keyedItem,
        extensionMenuItems,
    });
    const primaryActionCellPropsWithBI = createPrimaryActionCellPropsWithBI(actionCell.gridState.toolbar.toolbarBIReporter, actions, actionCell.keyedItem);
    if (primaryActionCellPropsWithBI) {
        primaryAction = {
            label: primaryActionCellPropsWithBI.text,
            onClick: primaryActionCellPropsWithBI.onClick,
            disabled: primaryActionCellPropsWithBI.disabled,
            disabledMessage: primaryActionCellPropsWithBI.disabledMessage,
        };
    }
    if (actions.secondaryMenuDisabled) {
        settingsMenu = (React.createElement(DisabledActionCellMenu, { dataHook: "popover-icon-button-trigger-disabled", tooltipContent: actions.secondaryMenuDisabledTooltip ??
                actionCell.gridState.container.translate('cairo.disabled'), size: "small", skin: actionCell.imagePlacement === 'top' ? 'light' : 'standard', ariaLabel: actionCell.gridState.container.translate('cairo.moreActions') }));
    }
    else if (secondaryActionCellPropsWithBI &&
        secondaryActionCellPropsWithBI.length > 0) {
        settingsMenu = (React.createElement(PopoverMenu, { ...actions.popoverMenuProps, ...getDefaultActionCellProps(), dataHook: "popover", triggerElement: ({ toggle, close }) => (React.createElement(IconButton, { dataHook: "popover-icon-button-trigger", onClick: (e) => {
                    e.stopPropagation();
                    toggle();
                    reportOnPopoverMenuShow(actionCell.gridState.toolbar.toolbarBIReporter, actions, actionCell.keyedItem);
                }, onPointerDown: actionCell.gridState._dnd ? (e) => e.stopPropagation() : undefined, onMouseLeave: close, skin: actionCell.imagePlacement === 'top' ? 'light' : 'standard', size: "small", priority: "secondary" },
                React.createElement(More, null))), children: secondaryActionCellPropsWithBI.map((action, index) => {
                if (action.divider) {
                    return React.createElement(PopoverMenu.Divider, { key: index });
                }
                const { icon: prefixIcon, skin, ...rest } = action;
                return (React.createElement(PopoverMenu.MenuItem, { key: index, prefixIcon: prefixIcon, skin: skin, ...rest }));
            }) }));
    }
    return {
        primaryAction,
        settingsMenu,
    };
}
//# sourceMappingURL=useGridItemActions.js.map