import { jsxs as T, jsx as s } from "react/jsx-runtime";
import { forwardRef as ur, useRef as c, useState as h, useCallback as d, useImperativeHandle as dr, useEffect as X } from "react";
import mr from "@likecoin/epub-ts";
import { X as br } from "lucide-react";
import { u as hr } from "./index-Bk6WqXgW.mjs";
if (typeof document < "u" && !document.getElementById("rfp-epub-styles")) {
  const p = document.createElement("style");
  p.id = "rfp-epub-styles", p.textContent = `
    .epub-container { overflow-y: auto !important; scrollbar-width: thin; }
    .epub-container::-webkit-scrollbar { width: 8px; }
    .epub-container::-webkit-scrollbar-track { background: transparent; }
    .epub-container::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 4px; }
    .epub-container::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,0.3); }
    .epub-view > iframe { background: white; }
  `, document.head.appendChild(p);
}
const wr = 794, Nr = ur(
  ({ url: p, onChapterChange: _, onFullWidthChange: D }, sr) => {
    const G = hr(), w = c(null), F = c(null), a = c(null), f = c(_), E = c(D);
    f.current = _, E.current = D;
    const y = c(0), H = c(null), M = c(!1), [cr, I] = h(!0), [L, U] = h(null), [V, ar] = h(!1), [B, J] = h([]), [A, x] = h(!1), [K, O] = h(""), Q = c([]);
    Q.current = B, M.current = V;
    const Y = d(() => {
      var r;
      (r = a.current) == null || r.prev();
    }, []), Z = d(() => {
      var r;
      (r = a.current) == null || r.next();
    }, []), m = c(null), R = c(0), P = c((r) => {
      var i, l;
      const t = m.current;
      if (!t) return;
      const e = t;
      if (e.scrollTop + e.clientHeight >= e.scrollHeight - 200)
        try {
          const n = (i = a.current) == null ? void 0 : i.manager;
          (l = n == null ? void 0 : n.check) == null || l.call(n, 500, 500);
        } catch {
        }
    }), S = d(() => {
      m.current && (m.current.removeEventListener("scroll", P.current), m.current = null), cancelAnimationFrame(R.current);
      const r = () => {
        var e;
        const t = ((e = w.current) == null ? void 0 : e.querySelector(".epub-container")) ?? null;
        if (!t) {
          R.current = requestAnimationFrame(r);
          return;
        }
        m.current = t, t.addEventListener("scroll", P.current, { passive: !0 });
      };
      R.current = requestAnimationFrame(r);
    }, []), C = d(() => {
      var t;
      const r = !M.current;
      ar(r), (t = E.current) == null || t.call(E, r), setTimeout(() => {
        const e = w.current, u = a.current;
        !e || !u || (u.resize(e.offsetWidth, e.offsetHeight), H.current && u.display(H.current), S());
      }, 350);
    }, [S]), rr = d(() => {
      x((r) => !r);
    }, []), fr = d((r) => {
      var t;
      O(r), (t = a.current) == null || t.display(r), x(!1);
    }, []);
    dr(sr, () => ({
      prevPage: Y,
      nextPage: Z,
      toggleFullWidth: C,
      toggleToc: rr
    }), [Y, Z, C, rr]), X(() => {
      const r = w.current;
      if (!r) return;
      I(!0), U(null), J([]), x(!1), r.innerHTML = "", H.current = null, y.current = 0;
      let t = !1;
      const e = window.setTimeout(() => {
        t || u();
      }, 0), u = async () => {
        var i;
        try {
          let l = p;
          p.startsWith("blob:") && (l = await (await fetch(p)).arrayBuffer());
          const n = mr(l);
          F.current = n;
          const g = n.renderTo(r, {
            manager: "continuous",
            flow: "scrolled",
            width: "100%",
            height: "100%"
          });
          a.current = g, g.themes.register("default", {
            body: {
              background: "#ffffff !important",
              color: "#1a1a1a !important",
              "font-family": '"Noto Serif SC", "Source Han Serif SC", Georgia, "Times New Roman", serif !important',
              "font-size": "16px !important",
              "line-height": "2 !important",
              padding: "40px 60px !important",
              "max-width": "100% !important",
              "box-sizing": "border-box !important",
              "word-break": "break-word !important",
              "overflow-wrap": "break-word !important"
            },
            p: { "text-indent": "2em !important", margin: "0.8em 0 !important" },
            h1: { "text-align": "center !important", margin: "1.5em 0 1em !important" },
            h2: { margin: "1.2em 0 0.8em !important" },
            h3: { margin: "1em 0 0.6em !important" },
            img: { "max-width": "100% !important", height: "auto !important" },
            a: { color: "#2563eb !important", "text-decoration": "none !important" }
          }), g.themes.select("default"), await n.ready, n.locations.generate(1024).then(() => {
            var v, k, N;
            if (t) return;
            y.current = n.locations.length();
            const b = (v = a.current) == null ? void 0 : v.currentLocation(), o = ((k = b == null ? void 0 : b.start) == null ? void 0 : k.location) ?? 0;
            (N = f.current) == null || N.call(f, o + 1, y.current);
          }).catch(() => {
          });
          const z = await n.loaded.navigation;
          if (!t && Array.isArray(z == null ? void 0 : z.toc) && J(z.toc), await g.display(), t) return;
          I(!1), (i = f.current) == null || i.call(f, 1, y.current || 1), g.on("relocated", (b) => {
            var N, er, nr, or;
            const o = b;
            if ((N = o == null ? void 0 : o.start) != null && N.cfi && (H.current = o.start.cfi), (er = o == null ? void 0 : o.start) != null && er.href) {
              const $ = o.start.href, q = [], ir = (pr) => {
                for (const W of pr) {
                  const j = W.href.split("#")[0];
                  j && ($ === j || $.endsWith("/" + j) || $.endsWith(j)) && q.push(W.href), W.subitems && ir(W.subitems);
                }
              };
              ir(Q.current), q.length === 1 && O(q[0]);
            }
            const v = (nr = o == null ? void 0 : o.start) == null ? void 0 : nr.location, k = y.current;
            typeof v == "number" && k > 0 && ((or = f.current) == null || or.call(f, v + 1, k));
          });
        } catch (l) {
          console.error("EPUB 加载错误:", l), t || (U(G("epub.load_failed")), I(!1));
        }
      };
      return () => {
        var i, l, n;
        t = !0, window.clearTimeout(e);
        try {
          (l = (i = a.current) == null ? void 0 : i.destroy) == null || l.call(i);
        } catch {
        }
        try {
          (n = F.current) == null || n.destroy();
        } catch {
        }
        a.current = null, F.current = null;
      };
    }, [p]), X(() => {
      const r = () => {
        const t = w.current;
        !t || !a.current || a.current.resize(t.offsetWidth, t.offsetHeight);
      };
      return window.addEventListener("resize", r), () => window.removeEventListener("resize", r);
    }, []), X(() => (S(), () => {
      var r;
      cancelAnimationFrame(R.current), (r = m.current) == null || r.removeEventListener("scroll", P.current);
    }), [p, S]);
    const lr = d((r) => r === K, [K]), tr = (r, t = 0) => /* @__PURE__ */ s("ul", { style: { marginLeft: t > 0 ? 16 : 0 }, children: r.map((e, u) => {
      const i = lr(e.href);
      return /* @__PURE__ */ T("li", { children: [
        /* @__PURE__ */ s(
          "button",
          {
            onClick: () => fr(e.href),
            className: `rfp-w-full rfp-text-left rfp-py-2 rfp-px-3 rfp-text-sm rfp-rounded rfp-transition-all rfp-truncate ${i ? "rfp-text-fg-primary rfp-bg-surface-3 rfp-font-medium" : "rfp-text-fg-secondary hover:rfp-text-fg-primary hover:rfp-bg-surface-2"}`,
            title: e.label,
            children: e.label.trim()
          }
        ),
        e.subitems && e.subitems.length > 0 && tr(e.subitems, t + 1)
      ] }, `${e.href}-${u}`);
    }) });
    return /* @__PURE__ */ T("div", { className: "rfp-relative rfp-w-full rfp-h-full rfp-flex rfp-justify-center rfp-bg-[#f5f5f0] rfp-overflow-hidden", children: [
      L && /* @__PURE__ */ s("div", { className: "rfp-absolute rfp-inset-0 rfp-flex rfp-items-center rfp-justify-center rfp-text-fg-secondary rfp-text-center", children: /* @__PURE__ */ s("p", { className: "rfp-text-lg", children: L }) }),
      cr && !L && /* @__PURE__ */ s("div", { className: "rfp-absolute rfp-inset-0 rfp-flex rfp-items-center rfp-justify-center rfp-z-10", children: /* @__PURE__ */ s("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" }) }),
      B.length > 0 && /* @__PURE__ */ T(
        "div",
        {
          className: "rfp-absolute rfp-inset-0 rfp-z-20 rfp-flex rfp-transition-opacity rfp-duration-300",
          style: {
            opacity: A ? 1 : 0,
            pointerEvents: A ? "auto" : "none"
          },
          children: [
            /* @__PURE__ */ T(
              "div",
              {
                className: "rfp-w-72 rfp-max-w-[80%] rfp-h-full rfp-bg-surface-overlay rfp-backdrop-blur-xl rfp-border-r rfp-border-line-weak rfp-flex rfp-flex-col rfp-shadow-2xl rfp-transition-transform rfp-duration-300",
                style: { transform: A ? "translateX(0)" : "translateX(-100%)" },
                children: [
                  /* @__PURE__ */ T("div", { className: "rfp-flex rfp-items-center rfp-justify-between rfp-px-4 rfp-py-3 rfp-border-b rfp-border-line-weak rfp-flex-shrink-0", children: [
                    /* @__PURE__ */ s("span", { className: "rfp-text-fg-primary rfp-font-medium rfp-text-sm", children: G("toolbar.toc") }),
                    /* @__PURE__ */ s(
                      "button",
                      {
                        onClick: () => x(!1),
                        className: "rfp-text-fg-tertiary hover:rfp-text-fg-primary rfp-transition-colors",
                        children: /* @__PURE__ */ s(br, { className: "rfp-w-4 rfp-h-4" })
                      }
                    )
                  ] }),
                  /* @__PURE__ */ s("div", { className: "rfp-flex-1 rfp-overflow-y-auto rfp-py-4 rfp-px-1", children: tr(B) })
                ]
              }
            ),
            /* @__PURE__ */ s(
              "div",
              {
                className: "rfp-flex-1 rfp-transition-opacity rfp-duration-300",
                style: { background: A ? "rgba(0,0,0,0.3)" : "transparent" },
                onClick: () => x(!1)
              }
            )
          ]
        }
      ),
      !L && /* @__PURE__ */ s(
        "div",
        {
          ref: w,
          className: "rfp-h-full rfp-bg-surface-toolbar rfp-shadow-lg",
          style: {
            width: V ? "100%" : `${wr}px`,
            maxWidth: "100%",
            transition: "width 0.3s ease",
            overflow: "hidden"
          }
        }
      )
    ] });
  }
);
export {
  Nr as EpubRenderer
};
//# sourceMappingURL=index-BPuxADap.mjs.map
