import { jsxs as b, jsx as i } from "react/jsx-runtime";
import { useState as M, useRef as u, useCallback as z, useEffect as L } from "react";
import { Presentation as _ } from "lucide-react";
import { init as C } from "pptx-preview";
import { u as B } from "./index-Bk6WqXgW.mjs";
const I = ({ url: y, tiled: a = !0 }) => {
  const s = B(), [v, w] = M(!0), [N, T] = M(null), [E, P] = M(0), n = u(null), o = u(null), x = u(null), d = u(null), m = u(null), k = u({ width: 0, height: 0 }), c = z(() => {
    var t;
    if (!n.current) return { width: 960, height: 540 };
    const e = n.current.clientWidth, r = ((t = n.current.parentElement) == null ? void 0 : t.clientWidth) || 0, p = e > 100 ? e : r > 100 ? r : 300, l = Math.floor(p * 9 / 16);
    return { width: p, height: l };
  }, []), W = z(async () => {
    if (!(!n.current || !d.current || E === 0))
      try {
        if (o.current)
          try {
            o.current.destroy();
          } catch {
          }
        n.current.innerHTML = "";
        const e = c(), r = C(n.current, {
          width: e.width,
          height: a ? e.height * E : e.height,
          mode: a ? "list" : "slide"
        });
        o.current = r, await r.preview(d.current);
      } catch {
      }
  }, [c, a, E]);
  return L(() => {
    if (!n.current) return;
    let e = !0;
    const r = () => {
      if (e) {
        e = !1, k.current = c();
        return;
      }
      const p = c(), l = k.current, t = Math.abs(l.width - p.width), f = Math.abs(l.height - p.height);
      t < 10 && f < 10 || (k.current = p, m.current && clearTimeout(m.current), m.current = window.setTimeout(() => {
        o.current && d.current && W();
      }, 800));
    };
    return x.current = new ResizeObserver(() => {
      r();
    }), x.current.observe(n.current), () => {
      x.current && x.current.disconnect(), m.current && clearTimeout(m.current);
    };
  }, [c, W]), L(() => {
    let e = !0, r = null;
    const p = async () => {
      if (n.current) {
        w(!0), T(null), r = setTimeout(() => {
          e && (T(s("pptx.timeout")), w(!1));
        }, 3e4);
        try {
          const t = await fetch(y, {
            mode: "cors",
            credentials: "omit",
            redirect: "follow"
          });
          if (!t.ok)
            throw t.status === 404 ? new Error(s("pptx.not_found")) : t.status === 403 ? new Error("无权限访问此文件") : t.status >= 500 ? new Error("服务器错误，请稍后重试") : new Error(`文件加载失败 (${t.status})`);
          const f = await t.arrayBuffer();
          if (f.byteLength === 0)
            throw new Error("文件为空");
          if (d.current = f, !e) return;
          const h = document.createElement("div");
          h.style.cssText = "position:absolute;left:-9999px;top:-9999px;visibility:hidden", document.body.appendChild(h);
          try {
            const R = C(h, {
              width: 100,
              height: 100,
              mode: "slide"
            });
            try {
              await R.preview(f);
            } catch {
              throw new Error(s("pptx.invalid_format"));
            }
            const g = R.slideCount;
            if (!g || g === 0)
              throw new Error(s("pptx.no_pages"));
            if (R.destroy(), !e) return;
            P(g), n.current && (n.current.innerHTML = "");
            const D = c(), j = C(n.current, {
              width: D.width,
              height: a ? D.height * g : D.height,
              mode: a ? "list" : "slide"
            });
            o.current = j, await j.preview(f), r && (clearTimeout(r), r = null), e && w(!1);
          } finally {
            document.body.contains(h) && document.body.removeChild(h);
          }
        } catch (t) {
          if (r && (clearTimeout(r), r = null), e) {
            let f = s("pptx.parse_failed");
            t instanceof Error ? f = t.message : typeof t == "string" && (f = t), T(f), w(!1);
          }
        }
      }
    }, l = setTimeout(() => {
      requestAnimationFrame(() => {
        requestAnimationFrame(() => {
          p();
        });
      });
    }, 150);
    return () => {
      if (e = !1, clearTimeout(l), r && clearTimeout(r), d.current = null, P(0), o.current)
        try {
          o.current.destroy();
        } catch {
        }
      o.current = null;
    };
  }, [y, c, a]), /* @__PURE__ */ b("div", { className: "rfp-relative rfp-flex rfp-flex-col rfp-items-center rfp-w-full rfp-h-full", children: [
    v && /* @__PURE__ */ i("div", { className: "rfp-absolute rfp-inset-0 rfp-flex rfp-items-center rfp-justify-center rfp-bg-surface-toolbar rfp-backdrop-blur-sm rfp-z-10", children: /* @__PURE__ */ b("div", { className: "rfp-text-center", children: [
      /* @__PURE__ */ i("div", { className: "rfp-w-10 rfp-h-10 md:rfp-w-12 md:rfp-h-12 rfp-mx-auto rfp-mb-3 rfp-border-4 rfp-border-line-strong rfp-border-t-spinner-head rfp-rounded-full rfp-animate-spin" }),
      /* @__PURE__ */ i("p", { className: "rfp-text-xs md:rfp-text-sm rfp-text-fg-secondary rfp-font-medium", children: s("pptx.loading") })
    ] }) }),
    N && !v && /* @__PURE__ */ i("div", { className: "rfp-absolute rfp-inset-0 rfp-flex rfp-items-center rfp-justify-center rfp-bg-surface-toolbar rfp-backdrop-blur-sm rfp-z-10", children: /* @__PURE__ */ b("div", { className: "rfp-text-center rfp-max-w-sm md:rfp-max-w-md rfp-px-4", children: [
      /* @__PURE__ */ i("div", { className: "rfp-w-24 rfp-h-24 md:rfp-w-32 md:rfp-h-32 rfp-mx-auto rfp-mb-4 md:rfp-mb-6 rfp-rounded-2xl md:rfp-rounded-3xl rfp-bg-gradient-to-br rfp-from-orange-500 rfp-via-red-500 rfp-to-pink-500 rfp-flex rfp-items-center rfp-justify-center rfp-shadow-2xl", children: /* @__PURE__ */ i(_, { className: "rfp-w-12 rfp-h-12 md:rfp-w-16 md:rfp-h-16 rfp-text-fg-primary" }) }),
      /* @__PURE__ */ i("p", { className: "rfp-text-lg md:rfp-text-xl rfp-text-fg-primary rfp-mb-2 md:rfp-mb-3 rfp-font-medium", children: s("pptx.load_failed") }),
      /* @__PURE__ */ i("p", { className: "rfp-text-xs md:rfp-text-sm rfp-text-fg-tertiary rfp-mb-4 md:rfp-mb-6", children: N }),
      /* @__PURE__ */ b(
        "a",
        {
          href: y,
          download: !0,
          className: "rfp-inline-flex rfp-items-center rfp-gap-2 rfp-px-4 rfp-py-2 md:rfp-px-6 md:rfp-py-3 rfp-bg-gradient-to-r rfp-from-purple-500 rfp-to-pink-500 rfp-text-fg-primary rfp-text-sm md:rfp-text-base rfp-rounded-lg md:rfp-rounded-xl hover:rfp-scale-105 rfp-transition-all rfp-shadow-lg",
          children: [
            /* @__PURE__ */ i("svg", { className: "rfp-w-4 rfp-h-4 md:rfp-w-5 md:rfp-h-5", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ i("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" }) }),
            s("common.download")
          ]
        }
      ),
      /* @__PURE__ */ i("p", { className: "rfp-text-xs rfp-text-fg-muted rfp-mt-3 md:rfp-mt-4", children: "提示：可以使用 Microsoft PowerPoint 或 WPS 打开" })
    ] }) }),
    !N && /* @__PURE__ */ i(
      "div",
      {
        ref: n,
        className: "pptx-wrapper rfp-w-full rfp-max-w-full md:rfp-max-w-6xl",
        style: { opacity: v ? 0 : 1 }
      }
    )
  ] });
};
export {
  I as PptxRenderer
};
//# sourceMappingURL=index-ClCtUSEI.mjs.map
