import React, { useCallback } from 'react';
import { CnTooltip } from '@cainiaofe/cn-ui';
/**
 * 将按钮用 CnTooltip 包裹，同时通过 forwardRef 把 ref 转发到内部按钮，
 * 使 CnPopup 能正确绑定到按钮 DOM 上，点击有效，悬停仍显示 CnTooltip。
 */
export const TriggerWithTooltip = React.forwardRef(function TriggerWithTooltip({ children, tooltipContent, disableTooltip = false }, ref) {
    const mergedRef = useCallback((node) => {
        const childRef = children.ref;
        if (typeof childRef === 'function')
            childRef(node);
        else if (childRef)
            childRef.current = node;
        if (typeof ref === 'function')
            ref(node);
        else if (ref) {
            const r = ref;
            r.current = node;
        }
    }, [children, ref]);
    const triggerProps = { ref: mergedRef };
    const triggerElement = React.cloneElement(children, triggerProps);
    // 始终用 CnTooltip 包裹，避免 disableTooltip 切换时从「Tooltip(button)」变为「button」导致按钮被卸载重挂、图标抖动
    return (React.createElement(CnTooltip, { trigger: triggerElement, triggerType: "hover", align: "b", popupProps: disableTooltip ? { visible: false } : undefined }, tooltipContent));
});
