import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useCallback, useMemo, useRef } from "react";
import { useIntl } from "react-intl";
import { ItemsWrapper, SingleSelectListItem, UiFocusManager, UiTooltip, makeMenuKeyboardNavigation, useToastMessage, } from "@gooddata/sdk-ui-kit";
import { bem } from "../../notificationsPanel/bem.js";
import { DELETE_CONFIRM_DIALOG_ID, PAUSE_CONFIRM_DIALOG_ID, RESUME_CONFIRM_DIALOG_ID, UNSUBSCRIBE_CONFIRM_DIALOG_ID, } from "../constants.js";
import { messages } from "../messages.js";
const { b } = bem("gd-ui-ext-automation-menu-item");
export function AutomationMenu({ item, workspace, canManage, isSubscribed, automationsType, canPause, canResume, canTrigger, editAutomation, deleteAutomation, unsubscribeFromAutomation, pauseAutomation, resumeAutomation, triggerAutomation, closeDropdown, setPendingAction, }) {
    const intl = useIntl();
    const { addSuccess } = useToastMessage();
    const menuWrapperRef = useRef(null);
    const menuItemRefs = useRef(new Map());
    const editActionUnavailable = useMemo(() => {
        return !item.dashboard?.id;
    }, [item.dashboard?.id]);
    const setMenuItemRef = useCallback((itemId) => (element) => {
        if (element) {
            menuItemRefs.current.set(itemId, element);
        }
        else {
            menuItemRefs.current.delete(itemId);
        }
    }, []);
    const onEdit = useCallback(() => {
        if (editActionUnavailable) {
            return;
        }
        closeDropdown();
        editAutomation(item, workspace, item.dashboard?.id);
    }, [editAutomation, workspace, item, closeDropdown, editActionUnavailable]);
    const onDelete = useCallback(() => {
        closeDropdown();
        setPendingAction({
            type: "delete",
            automationsType,
            automationTitle: item.title,
            onConfirm: () => deleteAutomation(item),
        });
    }, [deleteAutomation, closeDropdown, setPendingAction, automationsType, item]);
    const onUnsubscribe = useCallback(() => {
        closeDropdown();
        setPendingAction({
            type: "unsubscribe",
            automationsType,
            automationTitle: item.title,
            onConfirm: () => unsubscribeFromAutomation(item),
        });
    }, [unsubscribeFromAutomation, closeDropdown, setPendingAction, automationsType, item]);
    const onPause = useCallback(() => {
        closeDropdown();
        setPendingAction({
            type: "pause",
            automationsType,
            automationTitle: item.title,
            onConfirm: () => pauseAutomation(item),
        });
    }, [pauseAutomation, closeDropdown, setPendingAction, automationsType, item]);
    const onResume = useCallback(() => {
        closeDropdown();
        setPendingAction({
            type: "resume",
            automationsType,
            automationTitle: item.title,
            onConfirm: () => resumeAutomation(item),
        });
    }, [resumeAutomation, closeDropdown, setPendingAction, automationsType, item]);
    const onTrigger = useCallback(() => {
        closeDropdown();
        triggerAutomation(item);
    }, [triggerAutomation, closeDropdown, item]);
    const onCopyId = useCallback(() => {
        closeDropdown();
        void navigator.clipboard.writeText(item.id);
        addSuccess(messages.messageCopyIdSuccess);
    }, [item.id, addSuccess, closeDropdown]);
    // Build menu items array
    const menuItems = useMemo(() => {
        const items = [];
        if (canManage) {
            items.push({
                id: "edit",
                label: intl.formatMessage(messages.menuEdit),
                onClick: onEdit,
                disabled: editActionUnavailable,
                tooltip: editActionUnavailable ? intl.formatMessage(messages.menuEditUnavailable) : undefined,
            });
        }
        if (isSubscribed) {
            items.push({
                id: "unsubscribe",
                label: intl.formatMessage(messages.menuUnsubscribe),
                onClick: onUnsubscribe,
                ariaControls: UNSUBSCRIBE_CONFIRM_DIALOG_ID,
            });
        }
        if (canResume) {
            items.push({
                id: "resume",
                label: intl.formatMessage(messages.menuResume),
                onClick: onResume,
                ariaControls: RESUME_CONFIRM_DIALOG_ID,
            });
        }
        if (canPause) {
            items.push({
                id: "pause",
                label: intl.formatMessage(messages.menuPause),
                onClick: onPause,
                ariaControls: PAUSE_CONFIRM_DIALOG_ID,
            });
        }
        if (canTrigger && automationsType === "schedule") {
            items.push({
                id: "trigger",
                label: intl.formatMessage(messages.menuTrigger),
                onClick: onTrigger,
            });
        }
        items.push({
            id: "copyId",
            label: intl.formatMessage(messages.menuCopyId),
            onClick: onCopyId,
        });
        if (canManage) {
            items.push({
                id: "delete",
                label: intl.formatMessage(messages.menuDelete),
                onClick: onDelete,
                withSeparator: true,
                ariaControls: DELETE_CONFIRM_DIALOG_ID,
            });
        }
        return items;
    }, [
        canManage,
        isSubscribed,
        canResume,
        canPause,
        canTrigger,
        automationsType,
        editActionUnavailable,
        intl,
        onEdit,
        onUnsubscribe,
        onResume,
        onPause,
        onTrigger,
        onCopyId,
        onDelete,
    ]);
    // Keyboard navigation handler
    const menuKeyboardNavigationHandler = useMemo(() => makeMenuKeyboardNavigation({
        onFocusFirst: () => {
            const elements = Array.from(menuItemRefs.current.values());
            if (elements.length > 0) {
                elements[0]?.focus();
            }
        },
        onFocusNext: () => {
            const elements = Array.from(menuItemRefs.current.values());
            const currentIndex = elements.indexOf(document.activeElement);
            const nextElement = currentIndex === elements.length - 1 ? elements[0] : elements[currentIndex + 1];
            nextElement?.focus();
        },
        onFocusPrevious: () => {
            const elements = Array.from(menuItemRefs.current.values());
            const currentIndex = elements.indexOf(document.activeElement);
            const previousElement = currentIndex <= 0 ? elements[elements.length - 1] : elements[currentIndex - 1];
            previousElement?.focus();
        },
        onClose: closeDropdown,
        onSelect: () => {
            // Trigger click on the currently focused element
            const focusedElement = document.activeElement;
            focusedElement?.click();
        },
    }), [closeDropdown]);
    return (_jsx(UiFocusManager, { enableAutofocus: true, enableFocusTrap: true, enableReturnFocusOnUnmount: true, children: _jsx("div", { onKeyDown: menuKeyboardNavigationHandler, children: _jsx(ItemsWrapper, { smallItemsSpacing: true, wrapperRef: menuWrapperRef, children: _jsx("div", { role: "menu", children: menuItems.map((menuItem) => (_jsx(AutomationMenuItem, { menuItem: menuItem, setMenuItemRef: setMenuItemRef }, menuItem.id))) }) }) }) }));
}
function AutomationMenuItem({ menuItem, setMenuItemRef }) {
    return (_jsxs(_Fragment, { children: [menuItem.withSeparator ? (_jsx(SingleSelectListItem, { type: "separator", accessibilityConfig: {
                    role: "separator",
                } })) : null, _jsx(UiTooltip, { content: menuItem.tooltip, triggerBy: ["hover", "focus"], arrowPlacement: "right", disabled: !menuItem.tooltip, anchor: _jsx(SingleSelectListItem, { ref: setMenuItemRef(menuItem.id), className: b({
                        disabled: !!menuItem.disabled,
                    }), title: menuItem.label, onClick: menuItem.onClick, elementType: "button", accessibilityConfig: {
                        role: "menuitem",
                        ariaDisabled: menuItem.disabled,
                        ariaHasPopup: menuItem.ariaControls ? "dialog" : undefined,
                        ariaControls: menuItem.ariaControls,
                    } }) })
        ] }));
}
//# sourceMappingURL=AutomationMenu.js.map