import React__default, { useCallback } from 'react';
import { cn } from '../../../utils/cn.js';
import { useContextMenuContext } from './ContextMenuContext.js';

/**
 * ContextMenuTrigger - Area that triggers the context menu on right-click
 */
const ContextMenuTrigger = React__default.forwardRef(({ children, className, disabled = false }, ref) => {
    const { onOpenChange, triggerRef } = useContextMenuContext();
    // Handle right-click event
    const handleContextMenu = useCallback((e) => {
        if (disabled)
            return;
        // Prevent default browser context menu
        e.preventDefault();
        e.stopPropagation();
        // Store cursor position on trigger element via data attributes
        const target = e.currentTarget;
        target.dataset.cursorX = String(e.clientX);
        target.dataset.cursorY = String(e.clientY);
        // Now open the menu with position available
        onOpenChange(true);
    }, [disabled, onOpenChange]);
    // Clone child element and add context menu handler
    const child = React__default.Children.only(children);
    const handleRef = useCallback((el) => {
        // Assign to context ref
        if (triggerRef && typeof triggerRef === "object" && "current" in triggerRef) {
            triggerRef.current = el;
        }
        // Forward ref
        if (typeof ref === "function") {
            ref(el);
        }
        else if (ref && typeof ref === "object" && "current" in ref) {
            ref.current = el;
        }
    }, [ref, triggerRef]);
    return React__default.cloneElement(child, {
        ref: handleRef,
        onContextMenu: handleContextMenu,
        className: cn(child.props.className, className),
        "data-disabled": disabled,
    });
});
ContextMenuTrigger.displayName = "ContextMenu.Trigger";

export { ContextMenuTrigger };
