import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
const getContentWidth = (element)=>{
    if (element) {
        const range = document.createRange();
        range.selectNodeContents(element);
        const contentWidth = range.getBoundingClientRect().width;
        return contentWidth;
    }
};
const ellipsisDefaultStyle = {
    overflow: 'hidden',
    overflowWrap: 'break-word',
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap',
    wordBreak: 'break-all'
};
const EllipsisWithTooltip = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.forwardRef)((props, ref)=>{
    const [ellipsis, setEllipsis] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const elRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useImperativeHandle)(ref, ()=>({
            setPopoverVisible: setVisible
        }));
    if (!props.ellipsis) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: props.children
    });
    const { popoverContent } = props;
    const isOverflowTooltip = ()=>{
        var _elRef_current;
        const contentWidth = getContentWidth(elRef.current);
        const offsetWidth = null === (_elRef_current = elRef.current) || void 0 === _elRef_current ? void 0 : _elRef_current.offsetWidth;
        return contentWidth > offsetWidth;
    };
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Popover, {
        open: ellipsis && visible,
        onOpenChange: (visible)=>{
            setVisible(ellipsis && visible);
        },
        content: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
            style: {
                width: 300,
                overflow: 'auto',
                maxHeight: 400
            },
            children: popoverContent || props.children
        }),
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
            ref: elRef,
            style: {
                ...ellipsisDefaultStyle
            },
            onMouseEnter: (e)=>{
                const el = e.target;
                const isShowTooltips = isOverflowTooltip();
                if (isShowTooltips) setEllipsis(el.scrollWidth >= el.clientWidth);
            },
            children: props.children
        })
    });
});
EllipsisWithTooltip.displayName = 'EllipsisWithTooltip';
export { EllipsisWithTooltip };
