import React__default from 'react';
import { cn } from '../../../utils/cn.js';
import { usePopoverContext } from './PopoverContext.js';

const PopoverTrigger = React__default.forwardRef(({ children, className, ...props }, ref) => {
    var _a;
    const context = usePopoverContext();
    const childElement = children;
    // Create trigger events based on triggerType
    const getTriggerEvents = () => {
        if (context.triggerType === "click") {
            return {
                onClick: (e) => {
                    var _a, _b;
                    e.preventDefault();
                    context.onOpenChange(!context.isOpen);
                    // Call the original onClick if it exists
                    (_b = (_a = childElement.props) === null || _a === void 0 ? void 0 : _a.onClick) === null || _b === void 0 ? void 0 : _b.call(_a, e);
                },
            };
        }
        if (context.triggerType === "hover") {
            return {
                onMouseEnter: (e) => {
                    var _a, _b;
                    context.onOpenChange(true);
                    // Call the original onMouseEnter if it exists
                    (_b = (_a = childElement.props) === null || _a === void 0 ? void 0 : _a.onMouseEnter) === null || _b === void 0 ? void 0 : _b.call(_a, e);
                },
                onMouseLeave: (e) => {
                    var _a, _b;
                    if (!context.preventClose) {
                        context.onOpenChange(false);
                    }
                    // Call the original onMouseLeave if it exists
                    (_b = (_a = childElement.props) === null || _a === void 0 ? void 0 : _a.onMouseLeave) === null || _b === void 0 ? void 0 : _b.call(_a, e);
                },
            };
        }
        return {};
    };
    // Merge refs for the trigger element
    function mergeRefs(node) {
        context.triggerRef.current = node;
        if (ref) {
            if (typeof ref === "function")
                ref(node);
            else if (typeof ref === "object" && ref)
                ref.current = node;
        }
        const childRef = childElement.ref;
        if (childRef) {
            if (typeof childRef === "function")
                childRef(node);
            else if (typeof childRef === "object" && childRef)
                childRef.current = node;
        }
    }
    const triggerEvents = getTriggerEvents();
    return React__default.cloneElement(childElement, {
        ref: mergeRefs,
        "aria-expanded": context.isOpen,
        "aria-haspopup": true,
        "aria-controls": context.contentId,
        className: cn("popover-trigger", (_a = childElement.props) === null || _a === void 0 ? void 0 : _a.className, className),
        "data-state": context.isOpen ? "open" : "closed",
        "data-trigger-type": context.triggerType,
        ...triggerEvents,
        ...props,
    });
});
PopoverTrigger.displayName = "Popover.Trigger";

export { PopoverTrigger };
