import { jsxs as g, jsx as n } from "react/jsx-runtime";
import { Box as x, Typography as d, Skeleton as y } from "@mui/material";
import { useReactive as h, useSize as z, useCreation as v } from "ahooks";
import C from "lodash/noop";
import { useRef as c, useEffect as b } from "react";
let u = -1;
function w() {
  const i = document.createElement("div");
  Object.assign(i.style, {
    fontSize: "1px",
    visibility: "hidden",
    position: "absolute"
  }), i.textContent = "Test", document.body.appendChild(i);
  const r = parseFloat(getComputedStyle(i).fontSize);
  return document.body.removeChild(i), r;
}
function E({
  children: i = null,
  minFontSize: r = 12,
  /* 设置为 auto 时字体会自动收缩，直到达到浏览器最小字体限制 */
  fontSize: t = void 0,
  sx: a,
  onShrink: p = C,
  ...l
}) {
  const f = c(null), S = c(null), e = h({
    // @ts-expect-error
    fontSize: t,
    loading: !0
  }), o = z(f.current), s = z(S.current);
  return b(() => {
    if (e.loading)
      if (t === "auto") {
        if (o && !e.initialSize) {
          const m = getComputedStyle(f.current).fontSize;
          e.initialSize = Number(m.replace("px", "")), e.fontSize = e.initialSize;
        }
        u < 0 && (u = w()), e.fontSize <= u ? (e.loading = !1, p?.({ fontSize: e.fontSize, initialSize: e.initialSize })) : s && o && (s.width < o.width && e.fontSize > r ? e.fontSize-- : (e.loading = !1, p?.({ fontSize: e.fontSize, initialSize: e.initialSize })));
      } else
        e.loading = !1;
  }, [t, o?.width, s?.width]), v(() => t === "auto" ? e.loading : !1, [t, e.loading]) ? /* @__PURE__ */ g(
    x,
    {
      ref: S,
      sx: {
        flex: 1
      },
      children: [
        /* @__PURE__ */ n(d, { ...l, sx: a, noWrap: !0, children: /* @__PURE__ */ n(y, { variant: "text", sx: { fontSize: "1rem" } }) }),
        /* @__PURE__ */ n(
          d,
          {
            ref: f,
            ...l,
            sx: {
              ...a,
              ...e.fontSize ? { fontSize: `${e.fontSize}px !important` } : {},
              position: "fixed",
              top: -1e3,
              left: -1e3
            },
            noWrap: !0,
            children: i
          }
        )
      ]
    }
  ) : /* @__PURE__ */ n(
    d,
    {
      ...l,
      sx: {
        ...a,
        ...e.fontSize ? { fontSize: `${e.fontSize}px !important` } : {}
      },
      children: i
    }
  );
}
export {
  E as default
};
