import { jsx as _jsx } from "react/jsx-runtime";
import * as React from "react";
import * as PopoverPrimitive from "@radix-ui/react-popover";
import { cn } from "../libs/utils";
import { useIsInModal } from "./dialog";
const PopoverContext = React.createContext(null);
const Popover = ({ hover = false, click = false, children, _open, onOpenChange }) => {
    const [open, setOpen] = React.useState(_open || false);
    const insideModal = useIsInModal();
    const handleOpenChange = (open) => {
        setOpen(open);
        if (onOpenChange) {
            onOpenChange(open);
        }
    };
    return (_jsx(PopoverContext.Provider, { value: { open, setOpen, hover, click }, children: _jsx(PopoverPrimitive.Root, { open: open, onOpenChange: handleOpenChange, modal: insideModal, children: children }) }));
};
function handleHover(hover = false, setOpen = () => { }, type) {
    if (hover) {
        setOpen(type === "enter");
    }
}
const PopoverTrigger = React.forwardRef(({ children, ...props }, ref) => {
    const context = React.useContext(PopoverContext);
    if (!context) {
        throw new Error("PopoverTrigger must be used within a Popover");
    }
    const { setOpen, hover, click } = context;
    return (_jsx(PopoverPrimitive.Trigger, { ref: ref, asChild: true, onMouseEnter: () => handleHover(hover, setOpen, "enter"), onMouseLeave: () => handleHover(hover, setOpen, "leave"), onClick: () => {
            if (click)
                setOpen((prev) => !prev);
        }, ...props, children: children }));
});
PopoverTrigger.displayName = PopoverPrimitive.Trigger.displayName;
const PopoverAnchor = PopoverPrimitive.Anchor;
const PopoverContent = React.forwardRef(({ className, align = "center", side = "bottom", ...props }, ref) => {
    const context = React.useContext(PopoverContext);
    if (!context) {
        throw new Error("PopoverContent must be used within a Popover");
    }
    const { setOpen, hover } = context;
    return (_jsx(PopoverPrimitive.Portal, { children: _jsx(PopoverPrimitive.Content, { ref: ref, align: align, side: side, onMouseEnter: () => handleHover(hover, setOpen, "enter"), onMouseLeave: () => handleHover(hover, setOpen, "leave"), 
            // onClick={() => {setOpen(false)}}
            className: cn("z-50 w-72 rounded-md border-popover bg-popover text-popover-foreground ring-1 ring-gray-200 dark:ring-slate-700 shadow-md focus:outline-none animate-in", "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2", className), ...props }) }));
});
PopoverContent.displayName = PopoverPrimitive.Content.displayName;
const PopoverClose = PopoverPrimitive.Close;
PopoverClose.displayName = PopoverPrimitive.Close.displayName;
export { Popover, PopoverTrigger, PopoverContent, PopoverAnchor, PopoverClose };
//# sourceMappingURL=popover.js.map