/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var ReactDOM = require('react-dom');
var omit = require('lodash/omit');
var amisCore = require('amis-core');
var TooltipWrapper = require('./TooltipWrapper.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
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 = tslib.__read(React.useState(false), 2), ellipsisAvtived = _b[0], setEllipsisAvtived = _b[1];
    var _c = tslib.__read(React.useState(''), 2), innerText = _c[0], setInnerText = _c[1];
    var innerRef = React.useRef(null);
    var defaultTooltip = tooltip
        ? typeof tooltip === 'string'
            ? { content: tooltip }
            : amisCore.isObject(tooltip)
                ? tooltip
                : undefined
        : typeof children === 'string'
            ? { content: children }
            : undefined; /** 默认使用子节点文本 */
    var normalizedTooltip = innerText && (!defaultTooltip || (defaultTooltip === null || defaultTooltip === void 0 ? void 0 : defaultTooltip.content) == null)
        ? tslib.__assign(tslib.__assign({}, defaultTooltip), { content: innerText }) : defaultTooltip;
    var updateEllipsisActivation = React.useCallback(function (el) {
        setEllipsisAvtived(el
            ? el.scrollWidth > el.clientWidth || el.scrollHeight > el.scrollHeight
            : false);
    }, [innerRef.current]);
    var onMutation = React.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 = React.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变化 */
    React.useEffect(function () {
        var element = innerRef.current instanceof React__default["default"].Component
            ? ReactDOM.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]);
    /** 监听目标元素的尺寸变化 */
    React.useEffect(function () {
        var element = innerRef.current instanceof React__default["default"].Component
            ? ReactDOM.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["default"].isValidElement(children) ? (React__default["default"].cloneElement(children, { ref: innerRef })) : (_J$X_(WrapComponent, { ref: innerRef, className: cx('OverflowTpl', className, {
            'OverflowTpl--with-tooltip': showTooltip
        }) }, children));
    return showTooltip ? (_J$X_(TooltipWrapper["default"], tslib.__assign({}, omit__default["default"](props, ['className', 'inline', 'targetSelector', 'children']), { tooltip: normalizedTooltip }), Body)) : (Body);
};
OverflowTpl.defaultProps = {
    inline: true
};
var OverflowTpl$1 = amisCore.themeable(OverflowTpl);

exports["default"] = OverflowTpl$1;
