import { jsx } from "react/jsx-runtime";
import { forwardRef, memo, useCallback, useImperativeHandle, useState } from "react";
import { CanvasDropdownMenu } from "../shared/CanvasDropdownMenu.js";
const TaskMenuComponent = ({ task, getContextMenuItems, disabled }, ref)=>{
    const [isOpen, setIsOpen] = useState(false);
    const [menuItems, setMenuItems] = useState([]);
    const handleOpenChange = useCallback((open)=>{
        if (disabled && open) return;
        if (open) setMenuItems(getContextMenuItems(task));
        setIsOpen(open);
    }, [
        getContextMenuItems,
        task,
        disabled
    ]);
    const handleMenuItemClick = useCallback((item)=>{
        item.onClick();
        setIsOpen(false);
    }, []);
    const handleContextMenu = useCallback((e)=>{
        e.stopPropagation();
        e.preventDefault();
        if (disabled) return;
        handleOpenChange(true);
    }, [
        handleOpenChange,
        disabled
    ]);
    useImperativeHandle(ref, ()=>({
            handleContextMenu
        }));
    return /*#__PURE__*/ jsx(CanvasDropdownMenu, {
        open: isOpen,
        onOpenChange: handleOpenChange,
        menuItems: menuItems,
        onItemClick: handleMenuItemClick,
        triggerTestId: `stage-task-menu-${task.id}`,
        triggerAriaLabel: "Task actions",
        contentClassName: "w-[300px]",
        disabled: disabled
    });
};
const TaskMenu = /*#__PURE__*/ memo(/*#__PURE__*/ forwardRef(TaskMenuComponent));
export { TaskMenu };
