/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __read, __assign } from 'tslib';
import React__default, { useState, useRef, useCallback, useEffect } from 'react';
import { findDOMNode } from 'react-dom';
import omit from 'lodash/omit';
import { themeable, isObject } from 'amis-core';
import TooltipWrapper from './TooltipWrapper.js';

/**
 * @file OverflowTpl
 * @desc 文字提示组件，会基于内部文字是否触发ellipsis，动态展示tooltip提示, 默认使用子节点文本作为tooltip内容
 */
var OverflowTpl = function (props) {
    var cx = props.classnames, children = props.children, className = props.className, targetSelector = props.targetSelector, tooltip = props.tooltip, _a = props.inline, inline = _a === void 0 ? true : _a;
    var _b = __read(useState(false), 2), ellipsisAvtived = _b[0], setEllipsisAvtived = _b[1];
    var _c = __read(useState(''), 2), innerText = _c[0], setInnerText = _c[1];
    var innerRef = useRef(null);
    var defaultTooltip = tooltip
        ? typeof tooltip === 'string'
            ? { content: tooltip }
            : isObject(tooltip)
                ? tooltip
                : undefined
        : typeof children === 'string'
            ? { content: children }
            : undefined; /** 默认使用子节点文本 */
    var normalizedTooltip = innerText && (!defaultTooltip || (defaultTooltip === null || defaultTooltip === void 0 ? void 0 : defaultTooltip.content) == null)
        ? __assign(__assign({}, defaultTooltip), { content: innerText }) : defaultTooltip;
    var updateEllipsisActivation = useCallback(function (el) {
        setEllipsisAvtived(el
            ? el.scrollWidth > el.clientWidth || el.scrollHeight > el.scrollHeight
            : false);
    }, [innerRef.current]);
    var onMutation = useCallback(function (mutations) {
        var dom = (targetSelector
            ? document.querySelector(targetSelector)
            : mutations === null || mutations === void 0 ? void 0 : mutations[0].target);
        updateEllipsisActivation(dom);
        if ((dom === null || dom === void 0 ? void 0 : dom.textContent) &&
            typeof dom.textContent === 'string' &&
            (!defaultTooltip || (defaultTooltip === null || defaultTooltip === void 0 ? void 0 : defaultTooltip.content) == null)) {
            setInnerText(dom.textContent);
        }
    }, [targetSelector]);
    var onResize = useCallback(function (entries) {
        var dom = (targetSelector
            ? document.querySelector(targetSelector)
            : entries === null || entries === void 0 ? void 0 : entries[0].target);
        updateEllipsisActivation(dom);
        if ((dom === null || dom === void 0 ? void 0 : dom.textContent) &&
            typeof dom.textContent === 'string' &&
            (!defaultTooltip || !(defaultTooltip === null || defaultTooltip === void 0 ? void 0 : defaultTooltip.content) == null)) {
            setInnerText(dom.textContent);
        }
    }, [targetSelector]);
    /** 监听目标元素的DOM变化 */
    useEffect(function () {
        var element = innerRef.current instanceof React__default.Component
            ? findDOMNode(innerRef.current)
            : innerRef.current;
        if (element) {
            var observer_1 = new MutationObserver(onMutation);
            observer_1.observe(element, {
                childList: true,
                subtree: true,
                characterDataOldValue: true,
                characterData: true
            });
            return function () { return observer_1.disconnect(); };
        }
        return;
    }, [innerRef.current]);
    /** 监听目标元素的尺寸变化 */
    useEffect(function () {
        var element = innerRef.current instanceof React__default.Component
            ? findDOMNode(innerRef.current)
            : innerRef.current;
        if (element) {
            var observer_2 = new ResizeObserver(onResize);
            observer_2.observe(element);
            return function () { return observer_2.disconnect(); };
        }
        return;
    }, [innerRef.current]);
    var WrapComponent = inline !== false ? 'span' : 'div';
    var showTooltip = ellipsisAvtived && normalizedTooltip;
    var Body = React__default.isValidElement(children) ? (React__default.cloneElement(children, { ref: innerRef })) : (React__default.createElement(WrapComponent, { ref: innerRef, className: cx('OverflowTpl', className, {
            'OverflowTpl--with-tooltip': showTooltip
        }) }, children));
    return showTooltip ? (React__default.createElement(TooltipWrapper, __assign({}, omit(props, ['className', 'inline', 'targetSelector', 'children']), { tooltip: normalizedTooltip }), Body)) : (Body);
};
OverflowTpl.defaultProps = {
    inline: true
};
var OverflowTpl$1 = themeable(OverflowTpl);

export { OverflowTpl$1 as default };
