import { jsx as r, jsxs as N } from "react/jsx-runtime";
import { useState as f, useRef as A, useEffect as y, useCallback as G } from "react";
import P from "mammoth";
import { u as T } from "./index-Bk6WqXgW.mjs";
const _ = 1123, E = 60, v = 50, D = _ - E * 2, I = 24, w = {
  fontFamily: "system-ui, -apple-system, sans-serif",
  lineHeight: "1.8",
  color: "#333"
}, L = ({ url: a }) => {
  const H = T(), [n, c] = f(""), [b, p] = f(!0), [d, u] = f(null), [h, m] = f([]), i = A(null);
  y(() => {
    (async () => {
      p(!0), u(null), c("");
      try {
        const e = await fetch(a);
        if (!e.ok)
          throw new Error("文件加载失败");
        const t = await e.arrayBuffer(), s = await P.convertToHtml({ arrayBuffer: t });
        c(s.value);
      } catch (e) {
        console.error("Docx 解析错误:", e), u(H("docx.parse_failed"));
      } finally {
        p(!1);
      }
    })();
  }, [a]);
  const x = G(() => {
    const o = i.current;
    if (!o || !n) return;
    const e = Array.from(o.children);
    if (e.length === 0) {
      m([n]);
      return;
    }
    const t = [[]];
    let s = 0;
    for (const l of e) {
      const g = l.offsetHeight;
      s > 0 && s + g > D && (t.push([]), s = 0), t[t.length - 1].push(l.outerHTML), s += g;
    }
    t.length === 0 && t.push([]), m(t.map((l) => l.join("")));
  }, [n]);
  return y(() => {
    !n || !i.current || requestAnimationFrame(() => {
      x();
    });
  }, [n, x]), b ? /* @__PURE__ */ r("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-w-full rfp-h-full", children: /* @__PURE__ */ r("div", { className: "rfp-w-12 rfp-h-12 rfp-border-4 rfp-border-line-strong rfp-border-t-spinner-head rfp-rounded-full rfp-animate-spin" }) }) : d ? /* @__PURE__ */ r("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-w-full rfp-h-full", children: /* @__PURE__ */ r("div", { className: "rfp-text-fg-secondary rfp-text-center", children: /* @__PURE__ */ r("p", { className: "rfp-text-lg", children: d }) }) }) : /* @__PURE__ */ N(
    "div",
    {
      className: "rfp-w-full rfp-h-full rfp-overflow-auto",
      style: { background: "rgba(0, 0, 0, 0.15)" },
      children: [
        /* @__PURE__ */ r(
          "div",
          {
            ref: i,
            dangerouslySetInnerHTML: { __html: n },
            style: {
              ...w,
              position: "absolute",
              visibility: "hidden",
              width: `${794 - v * 2}px`,
              pointerEvents: "none"
            }
          }
        ),
        /* @__PURE__ */ r(
          "div",
          {
            className: "rfp-py-6 md:rfp-py-10 rfp-flex rfp-flex-col rfp-items-center",
            style: { gap: `${I}px` },
            children: (h.length > 0 ? h : [""]).map((o, e) => /* @__PURE__ */ r(
              "div",
              {
                style: {
                  width: "100%",
                  maxWidth: "794px",
                  minHeight: `${_}px`,
                  background: "white",
                  boxShadow: "0 2px 12px rgba(0, 0, 0, 0.15)",
                  flexShrink: 0,
                  padding: `${E}px ${v}px`
                },
                children: /* @__PURE__ */ r(
                  "div",
                  {
                    dangerouslySetInnerHTML: { __html: o },
                    style: w
                  }
                )
              },
              e
            ))
          }
        )
      ]
    }
  );
};
export {
  L as DocxRenderer
};
//# sourceMappingURL=index-DmL2F9gH.mjs.map
