import React, { useState, useRef, useContext, useEffect } from "react";
import classnames from "classnames";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import useEventListener from "../hooks/use-event-listener";
const TextEllipsis = (props) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("text-ellipsis", prefixCls);
  const [expanded, setExpanded] = useState(false);
  const [hasAction, setHasAction] = useState(false);
  const root = useRef(null);
  const [text, setText] = useState("");
  const pxToNum = (value) => {
    if (!value)
      return 0;
    const match = value.match(/^\d*(\.\d*)?/);
    return match ? Number(match[0]) : 0;
  };
  const calcEllipsis = () => {
    const cloneContainer = () => {
      if (!root.current)
        return;
      const originStyle = window.getComputedStyle(root.current);
      const container2 = document.createElement("div");
      const styleNames = Array.prototype.slice.apply(originStyle);
      styleNames.forEach((name) => {
        container2.style.setProperty(name, originStyle.getPropertyValue(name));
      });
      container2.style.position = "fixed";
      container2.style.zIndex = "-9999";
      container2.style.top = "-9999px";
      container2.style.height = "auto";
      container2.style.minHeight = "auto";
      container2.style.maxHeight = "auto";
      container2.innerText = props.content;
      document.body.appendChild(container2);
      return container2;
    };
    const calcEllipsisText = (container2, maxHeight2) => {
      const {
        content,
        expandText
      } = props;
      const dot = "...";
      let left = 0;
      let right = content.length;
      let res = -1;
      while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        container2.innerText = content.slice(0, mid) + dot + expandText;
        if (container2.offsetHeight <= maxHeight2) {
          left = mid + 1;
          res = mid;
        } else {
          right = mid - 1;
        }
      }
      return content.slice(0, res) + dot;
    };
    const container = cloneContainer();
    if (!container)
      return;
    const {
      paddingBottom,
      paddingTop,
      lineHeight
    } = container.style;
    const maxHeight = (Number(props.rows) + 0.5) * pxToNum(lineHeight) + pxToNum(paddingTop) + pxToNum(paddingBottom);
    if (maxHeight < container.offsetHeight) {
      setHasAction(true);
      setText(calcEllipsisText(container, maxHeight));
    } else {
      setHasAction(false);
      setText(props.content);
    }
    document.body.removeChild(container);
  };
  const onClickAction = (event) => {
    var _a;
    setExpanded((e) => !e);
    (_a = props.onClick) == null ? void 0 : _a.call(props, event);
  };
  const renderAction = () => /* @__PURE__ */ React.createElement("span", {
    className: classnames(bem("action")),
    onClick: onClickAction
  }, expanded ? props.collapseText : props.expandText);
  useEffect(() => {
    calcEllipsis();
  }, [props.content, props.rows]);
  useEventListener("resize", calcEllipsis);
  return /* @__PURE__ */ React.createElement("div", {
    ref: root,
    className: classnames(props.className, bem())
  }, expanded ? props.content : text, hasAction ? renderAction() : null);
};
TextEllipsis.defaultProps = {
  rows: 1,
  expandText: "",
  collapseText: ""
};
var stdin_default = TextEllipsis;
export {
  stdin_default as default
};
