import { _ as _slicedToArray } from '../_verture/slicedToArray-76060636.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useRef, useState, useEffect, useMemo } from 'react';
import { Tooltip } from '../Tooltip/index.js';
import { $ as $prefixCls } from '../_verture/constant-66aa48a1.js';
import { isIE } from '@iam-com/lib';
import { s as styleInject } from '../_verture/style-inject.es-300983ab.js';
import '../_verture/unsupportedIterableToArray-cb478f24.js';
import '../_verture/tslib.es6-55ed4bd2.js';
import 'rc-tooltip';
import 'rc-tooltip/lib/placements';
import 'clsx';

var css_248z$1 = "@charset \"UTF-8\";\n/**\n* @author linhd\n* @date 2021/11/1 20:28\n* @description 文字隐藏...\n*/\n/**\n* @author Hanz\n* @date 2021/10/20 下午2:08\n* @description color\n*/\n.paraui-v3-auto-tips-multiline {\n  max-width: 100%;\n  display: block;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n}\n.paraui-v3-auto-tips-multiline > .auto-tips-multiline-content-box {\n  position: relative;\n}\n.paraui-v3-auto-tips-multiline > .auto-tips-multiline-content-box > .auto-tips-multiline-content > .last-line {\n  overflow: hidden;\n  white-space: nowrap;\n  text-overflow: ellipsis;\n}\n.paraui-v3-auto-tips-multiline > .auto-tips-multiline-content-box > .hidden {\n  opacity: 0;\n  visibility: hidden;\n  position: absolute;\n  bottom: 0;\n  left: 0;\n  z-index: -100000;\n}\n.paraui-v3-auto-tips-multiline.paraui-v3-auto-tips-multiline-safari > .auto-tips-multiline-content-box > .auto-tips-multiline-content > .last-line::before {\n  content: \"\";\n  display: block;\n}\n\n.paraui-v3-auto-tips-multiline-tooltip .component-tooltip-inner {\n  max-height: calc(100vh - 25px);\n  overflow: auto;\n}";
styleInject(css_248z$1);

var AutoTipsMultiline = function AutoTipsMultiline(props) {
  var children = props.children,
    _props$arrow = props.arrow,
    arrow = _props$arrow === void 0 ? false : _props$arrow,
    _props$placement = props.placement,
    placement = _props$placement === void 0 ? "bottom-start" : _props$placement,
    _props$tips = props.tips,
    tips = _props$tips === void 0 ? '' : _props$tips,
    _props$className = props.className,
    className = _props$className === void 0 ? '' : _props$className,
    _props$interactive = props.interactive,
    interactive = _props$interactive === void 0 ? true : _props$interactive,
    tipMaxWidth = props.tipMaxWidth,
    _props$line = props.line,
    line = _props$line === void 0 ? 2 : _props$line,
    refresh = props.refresh,
    style = props.style;
  var boxRef = useRef(null);
  var singleWordRefs = useRef([]);
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    lineClamp = _useState2[0],
    setLineClamp = _useState2[1];
  var constData = useRef({
    line: line,
    tips: tips
  });
  constData.current.line = line;
  constData.current.tips = tips;
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    tTips = _useState4[0],
    setTTips = _useState4[1];
  useEffect(function () {
    window.addEventListener('resize', calcLine);
    return function () {
      window.removeEventListener('resize', calcLine);
    };
  }, []);
  useEffect(function () {
    calcLine();
  }, [children, refresh]);
  // 计算行数
  var calcLine = function calcLine() {
    var _a;
    if (!children) return;
    var lineArr = [];
    var showTooltip = false;
    var boxRefWidth = ((_a = boxRef === null || boxRef === void 0 ? void 0 : boxRef.current) === null || _a === void 0 ? void 0 : _a.clientWidth) || 0;
    var addWidth = 0;
    var linePos = 0;
    for (var i = 0; i < singleWordRefs.current.length; i++) {
      var dom = singleWordRefs.current[i];
      var offsetWidth = dom.offsetWidth;
      addWidth += offsetWidth;
      // 累计宽度超出了一行宽度
      if (addWidth > boxRefWidth) {
        if (lineArr.length < constData.current.line - 1) {
          lineArr.push(children.slice(linePos, i));
          linePos = i;
          addWidth = offsetWidth; // 重置宽度
          // 最后一个字
          if (i === singleWordRefs.current.length - 1) {
            if (lineArr.length < constData.current.line) {
              // 没超出行数
              lineArr.push(children.slice(linePos, children.length));
            } else {
              showTooltip = true;
              setTTips(constData.current.tips || children);
              lineArr.push(children.slice(linePos, children.length));
            }
          }
        } else {
          // 超出
          showTooltip = true;
          setTTips(constData.current.tips || children);
          lineArr.push(children.slice(linePos, children.length));
          break;
        }
      } else {
        // 未超过且是最后一个字符
        if (i === singleWordRefs.current.length - 1) {
          lineArr.push(children.slice(linePos, children.length));
        }
      }
    }
    setLineClamp({
      lineArr: lineArr,
      showTooltip: showTooltip
    });
  };
  /**
   * 描述文字span包裹
   */
  var descriptionRender = useMemo(function () {
    singleWordRefs.current = [];
    return jsx("div", Object.assign({
      className: 'hidden'
    }, {
      children: children === null || children === void 0 ? void 0 : children.split('').map(function (word, wIdx) {
        return jsx("span", Object.assign({
          ref: function ref(dom) {
            return dom && singleWordRefs.current.push(dom);
          }
        }, {
          children: word
        }), wIdx);
      })
    }));
  }, [children]);
  // 判断浏览器是否是safari
  var isSafari = function isSafari() {
    var userAgent = navigator.userAgent;
    if (userAgent.indexOf("Safari") > -1) {
      return true;
    }
  };
  // 处理最大宽度
  var handMaxWidth = function handMaxWidth() {
    if (tipMaxWidth) return tipMaxWidth;
    return boxRef && boxRef.current ? boxRef.current.offsetWidth : 0;
  };
  // 处理class
  var handClass = function handClass() {
    var str = $prefixCls + '-auto-tips-multiline';
    if (isSafari()) str += ' ' + $prefixCls + '-auto-tips-multiline-safari';
    if (className) str += ' ' + className;
    return str;
  };
  return jsx("div", Object.assign({
    ref: boxRef,
    className: handClass(),
    style: style
  }, {
    children: jsx(Tooltip, Object.assign({
      arrow: arrow,
      title: tTips,
      placement: placement,
      interactive: interactive,
      boundariesElement: 'viewport',
      overlayClassName: $prefixCls + '-auto-tips-multiline-tooltip',
      maxWidth: handMaxWidth()
    }, {
      children: jsxs("div", Object.assign({
        className: "auto-tips-multiline-content-box"
      }, {
        children: [jsx("div", Object.assign({
          className: 'auto-tips-multiline-content'
        }, {
          children: lineClamp === null || lineClamp === void 0 ? void 0 : lineClamp.lineArr.map(function (line, lIdx) {
            var str = 'line-text';
            if (lIdx === lineClamp.lineArr.length - 1) str += ' last-line';
            return jsx("div", Object.assign({
              className: str
            }, {
              children: line
            }), lIdx);
          })
        })), descriptionRender]
      }))
    }))
  }));
};

var css_248z = "@charset \"UTF-8\";\n/**\n* @author linhd\n* @date 2021/11/1 20:28\n* @description 文字隐藏...\n*/\n/**\n* @author Hanz\n* @date 2021/10/20 下午2:08\n* @description color\n*/\n.paraui-v3-auto-tips {\n  max-width: 100%;\n  display: block;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n}\n.paraui-v3-auto-tips > .auto-tips-content {\n  display: inline;\n  max-width: 100%;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n}\n.paraui-v3-auto-tips.paraui-v3-auto-tips-safari > .auto-tips-content::before {\n  content: \"\";\n  display: block;\n}\n\n.paraui-v3-auto-tips-tooltip .component-tooltip-inner {\n  max-height: calc(100vh - 25px);\n  overflow: auto;\n}";
styleInject(css_248z);

var AutoTips = function AutoTips(props) {
  var children = props.children,
    _props$arrow = props.arrow,
    arrow = _props$arrow === void 0 ? false : _props$arrow,
    _props$placement = props.placement,
    placement = _props$placement === void 0 ? "bottom-start" : _props$placement,
    _props$tips = props.tips,
    tips = _props$tips === void 0 ? '' : _props$tips,
    _props$className = props.className,
    className = _props$className === void 0 ? '' : _props$className,
    _props$beyondText = props.beyondText,
    beyondText = _props$beyondText === void 0 ? true : _props$beyondText,
    _props$interactive = props.interactive,
    interactive = _props$interactive === void 0 ? true : _props$interactive,
    tipMaxWidth = props.tipMaxWidth,
    style = props.style;
  var tipsElm = useRef();
  var textElm = useRef();
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    tTips = _useState2[0],
    setTTips = _useState2[1];
  useEffect(function () {
    setTTips(tips);
  }, [tips]);
  // 鼠标移入事件
  var mouseHover = function mouseHover() {
    var val = '';
    var tipsEle = tipsElm.current;
    var tipsEleRect = tipsEle.getBoundingClientRect();
    var textEleRect = textElm.current.getBoundingClientRect();
    if (tipsElm.current) {
      if (isIE()) {
        if (tipsEleRect.width > textEleRect.width) {
          if (tips) {
            val = tips;
          } else {
            val = beyondText ? tipsEle.innerText : children;
          }
        }
      } else {
        // mac：safari,firefox,chrome
        // windows: firefox,chrome
        if (textEleRect.width > tipsEleRect.width) {
          if (tips) {
            val = tips;
          } else {
            val = beyondText ? tipsEle.innerText : children;
          }
        }
      }
    }
    setTTips(val);
  };
  // 处理最大宽度
  var handMaxWidth = function handMaxWidth() {
    if (tipMaxWidth) return tipMaxWidth;
    return 500;
    //return tipsElm && tipsElm.current ? tipsElm.current.offsetWidth : 0;
  };
  // 判断浏览器是否是safari
  var isSafari = function isSafari() {
    var userAgent = navigator.userAgent;
    if (userAgent.indexOf("Safari") > -1) {
      return true;
    }
  };
  // 处理class
  var handClass = function handClass() {
    var str = $prefixCls + '-auto-tips';
    if (isSafari()) str += ' ' + $prefixCls + '-auto-tips-safari';
    if (className) str += ' ' + className;
    return str;
  };
  return jsx(Tooltip, Object.assign({
    arrow: arrow,
    title: tTips,
    placement: placement,
    interactive: interactive,
    boundariesElement: 'viewport',
    overlayClassName: $prefixCls + '-auto-tips-tooltip',
    maxWidth: handMaxWidth()
  }, {
    children: jsx("div", Object.assign({
      ref: tipsElm,
      className: handClass(),
      onMouseOver: mouseHover,
      style: style
    }, {
      children: jsx("div", Object.assign({
        className: "auto-tips-content",
        ref: textElm
      }, {
        children: children
      }))
    }))
  }));
};

export { AutoTipsMultiline, AutoTips as default };
