var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
const TextEllipsis = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("text-ellipsis", prefixCls);
  const [expanded, setExpanded] = (0, import_react.useState)(false);
  const [hasAction, setHasAction] = (0, import_react.useState)(false);
  const root = (0, import_react.useRef)(null);
  const [text, setText] = (0, import_react.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__ */ import_react.default.createElement("span", {
    className: (0, import_classnames.default)(bem("action")),
    onClick: onClickAction
  }, expanded ? props.collapseText : props.expandText);
  (0, import_react.useEffect)(() => {
    calcEllipsis();
  }, [props.content, props.rows]);
  (0, import_use_event_listener.default)("resize", calcEllipsis);
  return /* @__PURE__ */ import_react.default.createElement("div", {
    ref: root,
    className: (0, import_classnames.default)(props.className, bem())
  }, expanded ? props.content : text, hasAction ? renderAction() : null);
};
TextEllipsis.defaultProps = {
  rows: 1,
  expandText: "",
  collapseText: ""
};
var stdin_default = TextEllipsis;
