import { jsxs as m, jsx as e } from "react/jsx-runtime";
import { useState as N, useRef as w, useEffect as R, useCallback as L } from "react";
import { Document as H, Page as M } from "react-pdf";
import { u as P } from "./index-Bk6WqXgW.mjs";
const q = ({
  url: u,
  zoom: x,
  currentPage: s,
  onPageChange: o,
  onTotalPagesChange: E,
  onPageWidthChange: h
}) => {
  const k = P(), [c, D] = N(0), [l, b] = N(null), p = w(null), a = w(/* @__PURE__ */ new Map());
  R(() => {
    b(null);
  }, [u]);
  const S = ({ numPages: r }) => {
    D(r), E(r), o(1);
  }, j = (r) => {
    console.error("PDF 加载错误:", r), b(k("pdf.load_failed"));
  }, i = L(() => {
    if (!p.current) return;
    const r = p.current, t = r.scrollTop, f = r.clientHeight, n = t + f / 2;
    let d = 1, g = 1 / 0;
    a.current.forEach((A, _) => {
      const v = A.getBoundingClientRect(), B = r.getBoundingClientRect(), C = v.top - B.top + v.height / 2 + t, y = Math.abs(C - n);
      y < g && (g = y, d = _);
    }), d !== s && o(d);
  }, [s, o]);
  R(() => {
    const r = p.current;
    if (r)
      return r.addEventListener("scroll", i), () => r.removeEventListener("scroll", i);
  }, [i]);
  const T = L((r, t) => {
    t ? a.current.set(r, t) : a.current.delete(r);
  }, []);
  return /* @__PURE__ */ m(
    "div",
    {
      ref: p,
      className: "rfp-flex rfp-flex-col rfp-items-center rfp-w-full rfp-h-full rfp-overflow-auto rfp-py-4 md:rfp-py-8 rfp-px-2 md:rfp-px-4",
      children: [
        l && /* @__PURE__ */ e("div", { className: "rfp-text-fg-secondary rfp-text-center", children: /* @__PURE__ */ e("p", { className: "rfp-text-lg", children: l }) }),
        !l && /* @__PURE__ */ e(
          H,
          {
            file: u,
            onLoadSuccess: S,
            onLoadError: j,
            loading: /* @__PURE__ */ e("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-min-h-screen", children: /* @__PURE__ */ e("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" }) }),
            children: /* @__PURE__ */ e("div", { className: "rfp-flex rfp-flex-col rfp-gap-4", children: Array.from(new Array(c), (r, t) => {
              const f = t + 1;
              return /* @__PURE__ */ m(
                "div",
                {
                  ref: (n) => T(f, n),
                  className: "rfp-relative",
                  children: [
                    /* @__PURE__ */ e(
                      M,
                      {
                        pageNumber: f,
                        scale: x,
                        loading: /* @__PURE__ */ e("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-p-8 rfp-bg-surface-1 rfp-rounded-lg rfp-min-h-[600px]", children: /* @__PURE__ */ e("div", { className: "rfp-w-8 rfp-h-8 rfp-border-4 rfp-border-line-strong rfp-border-t-spinner-head rfp-rounded-full rfp-animate-spin" }) }),
                        renderTextLayer: !1,
                        renderAnnotationLayer: !1,
                        className: "rfp-shadow-2xl",
                        onRenderSuccess: (n) => {
                          f === 1 && h && h(n.originalWidth || n.width / x);
                        }
                      }
                    ),
                    /* @__PURE__ */ e("div", { className: "rfp-absolute rfp-top-2 rfp-right-2 rfp-bg-surface-nav-hover rfp-backdrop-blur-sm rfp-text-fg-primary rfp-text-xs rfp-px-3 rfp-py-1 rfp-rounded-full", children: f })
                  ]
                },
                `page_${f}`
              );
            }) })
          }
        ),
        c > 0 && /* @__PURE__ */ m("div", { className: "rfp-sticky rfp-bottom-2 md:rfp-bottom-4 rfp-mt-4 md:rfp-mt-8 rfp-bg-surface-nav-hover rfp-backdrop-blur-xl rfp-text-fg-primary rfp-px-4 rfp-py-2 md:rfp-px-6 md:rfp-py-3 rfp-rounded-full rfp-text-xs md:rfp-text-sm rfp-font-medium rfp-shadow-2xl rfp-border rfp-border-line-weak", children: [
          "第 ",
          s,
          " 页 / 共 ",
          c,
          " 页"
        ] })
      ]
    }
  );
};
export {
  q as PdfRenderer
};
//# sourceMappingURL=index-BtdmiRRJ.mjs.map
