import React, { useEffect, useRef } from 'react';
import { useExtensionMenuItems } from '../../hooks/useExtensionMenuItems';
import { IconButton, PopoverMenu, } from '@wix/design-system';
import { getDefaultActionCellProps } from '../CollectionItemActions/getDefaultActionCellProps';
import { MoreSmall } from '@wix/wix-ui-icons-common';
import { DisabledActionCellMenu } from '../ActionCell/DisabledActionCellMenu';
import { createKanbanPrimaryActionCellPropsWithBI, createKanbanSecondaryActionCellPropsWithBI, reportOnKanbanPopoverMenuShow, } from './KanbanCardActionsHelper';
export function evaluateActionCell({ actionCell, originalItem, itemIndex, itemActionsState, }) {
    if (!actionCell) {
        return {};
    }
    if (typeof actionCell === 'function') {
        return actionCell(originalItem, itemIndex, itemActionsState.actionCellAPI);
    }
    return actionCell;
}
export function useCardActions({ actionCell, originalItem, itemIndex, itemActionsState, stageKey, cardKey, state, }) {
    const actionCellConfig = evaluateActionCell({
        actionCell,
        originalItem,
        itemIndex,
        itemActionsState,
    });
    const extensionMenuItems = useExtensionMenuItems({
        containerId: actionCellConfig.containerId,
    });
    const secondaryActionCellPropsWithBI = createKanbanSecondaryActionCellPropsWithBI({
        state,
        actionCellProps: actionCellConfig,
        keyedItem: itemActionsState.keyedItem,
        extensionMenuItems,
        stageId: stageKey,
    });
    const primaryActionCellPropsWithBI = createKanbanPrimaryActionCellPropsWithBI({
        state,
        actionCellProps: actionCellConfig,
        keyedItem: itemActionsState.keyedItem,
        stageId: stageKey,
    });
    const popoverCloseRef = useRef(null);
    useEffect(() => {
        const closePopoverOnScroll = () => {
            if (popoverCloseRef.current) {
                const scrollEvent = new Event('scroll');
                popoverCloseRef.current(scrollEvent);
            }
        };
        const stageCardsContainer = document.querySelector(`[data-hook="kanban-stage-cards-container-${stageKey}"]`);
        if (stageCardsContainer &&
            typeof stageCardsContainer.addEventListener === 'function') {
            stageCardsContainer.addEventListener('scroll', closePopoverOnScroll, {
                passive: true,
            });
            return () => {
                if (typeof stageCardsContainer.removeEventListener === 'function') {
                    stageCardsContainer.removeEventListener('scroll', closePopoverOnScroll);
                }
            };
        }
        return () => { };
    }, [stageKey]);
    let secondaryActions = null;
    if (actionCellConfig.secondaryMenuDisabled) {
        secondaryActions = (React.createElement(DisabledActionCellMenu, { dataHook: `kanban-card-more-actions-trigger-disabled-${stageKey}-${cardKey}`, tooltipContent: actionCellConfig.secondaryMenuDisabledTooltip ??
                state.container.translate('cairo.disabled'), size: "tiny", icon: React.createElement(MoreSmall, null), ariaLabel: state.container.translate('cairo.moreActions') }));
    }
    else if (secondaryActionCellPropsWithBI &&
        secondaryActionCellPropsWithBI.length > 0) {
        secondaryActions = (React.createElement(PopoverMenu, { ...actionCellConfig.popoverMenuProps, ...getDefaultActionCellProps(), textSize: "small", minWidth: "223px", dataHook: `kanban-card-more-actions-${stageKey}-${cardKey}`, triggerElement: ({ toggle, close }) => {
                popoverCloseRef.current = close;
                return (React.createElement(IconButton, { dataHook: `kanban-card-more-actions-trigger-${stageKey}-${cardKey}`, onClick: (e) => {
                        e.stopPropagation();
                        toggle();
                        reportOnKanbanPopoverMenuShow({
                            state,
                            keyedItem: itemActionsState.keyedItem,
                            stageId: stageKey,
                        });
                    }, priority: "secondary", size: "tiny" },
                    React.createElement(MoreSmall, null)));
            } }, 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, subtitle: action.disabledDescription, prefixIcon: prefixIcon, dataHook: action.dataHook ||
                    `kanban-card-more-actions-item-${stageKey}-${cardKey}-${index}`, skin: skin, ...rest }));
        })));
    }
    return {
        secondaryActions,
        primaryActionProps: primaryActionCellPropsWithBI,
    };
}
//# sourceMappingURL=useCardActions.js.map