import { TableActionCell } from '@wix/design-system';
import React, { useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { MessageActionModal } from '../ActionModal';
import { DisabledActionCellMenu } from '../ActionCell/DisabledActionCellMenu';
import { useToolbarCollectionContext } from '../ToolbarCollectionContext';
import { createPrimaryActionCellPropsWithBI, createSecondaryActionCellPropWithBi, reportOnPopoverMenuShow, } from './CollectionItemActionsHelper';
import { getDefaultActionCellProps } from './getDefaultActionCellProps';
import { useExtensionMenuItems } from '../../hooks/useExtensionMenuItems';
import { useIsMobile } from '../../hooks/useIsMobile';
function _CollectionItemActions({ state, renderActionCell, }) {
    const isMobile = useIsMobile();
    const { confirmModal } = state;
    const toolbarState = useToolbarCollectionContext();
    const { toolbarBIReporter } = toolbarState;
    const actionCellProps = renderActionCell?.(state) ?? {};
    const { secondaryMenuDisabled, secondaryMenuDisabledTooltip, ...tableActionCellProps } = actionCellProps;
    const keyedItem = state.actionCellAPI.keyedItem;
    const existingExtension = actionCellProps.containerId
        ? toolbarState.menuItemExtensions.get(actionCellProps.containerId)
        : undefined;
    const extensionMenuItems = useExtensionMenuItems({
        containerId: actionCellProps.containerId,
        extension: existingExtension,
    });
    useEffect(() => {
        if (extensionMenuItems && actionCellProps.containerId) {
            toolbarState.menuItemExtensions.set(actionCellProps.containerId, extensionMenuItems);
        }
    }, [extensionMenuItems, actionCellProps.containerId]);
    if (secondaryMenuDisabled) {
        return (React.createElement(DisabledActionCellMenu, { dataHook: "collection-action-cell-disabled", tooltipContent: secondaryMenuDisabledTooltip ??
                toolbarState.container.translate('cairo.disabled'), size: actionCellProps.size ?? 'medium', ariaLabel: toolbarState.container.translate('cairo.moreActions') }));
    }
    const secondaryActionCellPropsWithBI = createSecondaryActionCellPropWithBi({
        toolbarBIReporter,
        actionCellProps,
        keyedItem,
        extensionMenuItems,
    });
    let primaryActionCellPropsWithBI = !isMobile
        ? createPrimaryActionCellPropsWithBI(toolbarBIReporter, actionCellProps, keyedItem)
        : [];
    if (Array.isArray(primaryActionCellPropsWithBI) &&
        primaryActionCellPropsWithBI.length > 1) {
        primaryActionCellPropsWithBI = primaryActionCellPropsWithBI.map((action, index) => {
            return {
                ...action,
                priority: index ===
                    primaryActionCellPropsWithBI
                        .length -
                        1
                    ? 'primary'
                    : 'secondary',
            };
        });
    }
    return (React.createElement(React.Fragment, null,
        confirmModal.value && (React.createElement(MessageActionModal, { state: confirmModal, ...confirmModal.value })),
        React.createElement(TableActionCell, { dataHook: "collection-action-cell", ...tableActionCellProps, popoverMenuProps: {
                ...getDefaultActionCellProps(),
                onShow: () => reportOnPopoverMenuShow(toolbarBIReporter, actionCellProps, state.keyedItem),
                ...actionCellProps.popoverMenuProps,
                ...getDefaultActionCellProps(),
            }, primaryAction: primaryActionCellPropsWithBI, secondaryActions: secondaryActionCellPropsWithBI })));
}
export const CollectionItemActions = observer(_CollectionItemActions);
//# sourceMappingURL=CollectionItemActions.js.map