import { jsx } from "react/jsx-runtime";
import { useCallback, useRef } from "react";
import { Popover } from "antd";
const StablePopover = (props)=>{
    const target = useRef(null);
    const avoidClose = useCallback((e)=>{
        target.current = e.target;
    }, []);
    const onOpenChange = useCallback((open)=>{
        setTimeout(()=>{
            var _props_onOpenChange;
            if (!open && null !== target.current && !isTargetInPopover(target.current)) {
                target.current = null;
                return;
            }
            null == (_props_onOpenChange = props.onOpenChange) || _props_onOpenChange.call(props, open);
        });
    }, [
        props
    ]);
    return /*#__PURE__*/ jsx("div", {
        className: "popover-with-stop-propagation",
        onClick: avoidClose,
        children: /*#__PURE__*/ jsx(Popover, {
            ...props,
            onOpenChange: onOpenChange
        })
    });
};
function isTargetInPopover(target) {
    if (!target) return false;
    while(target){
        var _target_classList;
        if (null == (_target_classList = target.classList) ? void 0 : _target_classList.contains('popover-with-stop-propagation')) return true;
        target = target.parentNode;
    }
    return false;
}
export { StablePopover };
