import * as n from "react";
import B from "../../../../rc-util/es/Children/toArray.mjs";
import y from "../../../../rc-util/es/hooks/useLayoutEffect.mjs";
function W(o) {
  const e = typeof o;
  return e === "string" || e === "number";
}
function G(o) {
  let e = 0;
  return o.forEach((t) => {
    W(t) ? e += String(t).length : e += 1;
  }), e;
}
function I(o, e) {
  let t = 0;
  const l = [];
  for (let f = 0; f < o.length; f += 1) {
    if (t === e)
      return l;
    const c = o[f], p = W(c) ? String(c).length : 1, a = t + p;
    if (a > e) {
      const r = e - t;
      return l.push(String(c).slice(0, r)), l;
    }
    l.push(c), t = a;
  }
  return o;
}
const K = 0, w = 1, P = 2, _ = 3, b = 4, z = (o) => {
  let {
    enabledMeasure: e,
    children: t,
    text: l,
    width: f,
    fontSize: c,
    rows: m,
    onEllipsis: p
  } = o;
  const [[a, r, L], O] = n.useState([0, 0, 0]), [H, A] = n.useState(0), [s, S] = n.useState(K), [E, D] = n.useState(0), k = n.useRef(null), N = n.useRef(null), u = n.useMemo(() => B(l), [l]), d = n.useMemo(() => G(u), [u]), T = n.useMemo(() => !e || s !== _ ? H && s !== b && e ? t(I(u, H), H < d) : t(u, !1) : t(I(u, r), r < d), [e, s, t, u, r, d]);
  y(() => {
    e && f && c && d && (S(w), O([0, Math.ceil(d / 2), d]));
  }, [e, f, c, l, d, m]), y(() => {
    var i;
    s === w && D(((i = k.current) === null || i === void 0 ? void 0 : i.offsetHeight) || 0);
  }, [s]), y(() => {
    var i, g;
    if (E) {
      if (s === w) {
        const h = ((i = N.current) === null || i === void 0 ? void 0 : i.offsetHeight) || 0, x = m * E;
        h <= x ? (S(b), p(!1)) : S(P);
      } else if (s === P)
        if (a !== L) {
          const h = ((g = N.current) === null || g === void 0 ? void 0 : g.offsetHeight) || 0, x = m * E;
          let M = a, v = L;
          a === L - 1 ? v = a : h <= x ? M = r : v = r;
          const j = Math.ceil((M + v) / 2);
          O([M, j, v]);
        } else
          S(_), A(r), p(!0);
    }
  }, [s, a, L, m, E]);
  const C = {
    width: f,
    whiteSpace: "normal",
    margin: 0,
    padding: 0
  }, R = (i, g, h) => /* @__PURE__ */ n.createElement("span", {
    "aria-hidden": !0,
    ref: g,
    style: Object.assign({
      position: "fixed",
      display: "block",
      left: 0,
      top: 0,
      zIndex: -9999,
      visibility: "hidden",
      pointerEvents: "none",
      fontSize: Math.ceil(c / 2) * 2
    }, h)
  }, i), F = (i, g) => {
    const h = I(u, i);
    return R(t(h, !0), g, C);
  };
  return /* @__PURE__ */ n.createElement(n.Fragment, null, T, e && s !== _ && s !== b && /* @__PURE__ */ n.createElement(n.Fragment, null, R("lg", k, {
    wordBreak: "keep-all",
    whiteSpace: "nowrap"
  }), s === w ? R(t(u, !1), N, C) : F(r, N)));
};
process.env.NODE_ENV !== "production" && (z.displayName = "Ellipsis");
const $ = z;
export {
  $ as default
};
