import { jsxs as x, jsx as n } from "react/jsx-runtime";
import { forwardRef as ir, useRef as c, useState as p, useCallback as l, useImperativeHandle as lr, useEffect as cr } from "react";
import { X as pr } from "lucide-react";
import "foliate-js/view.js";
import { u as dr } from "./index-Bk6WqXgW.mjs";
const ur = `
  @namespace epub "http://www.idpf.org/2007/ops";
  html { color-scheme: light; }
  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;
    max-width: 100% !important;
    box-sizing: border-box !important;
    word-break: break-word !important;
    overflow-wrap: break-word !important;
  }
  p, li, blockquote, dd { line-height: 2; text-align: justify; }
  p { text-indent: 2em; margin: 0.8em 0; }
  h1 { text-align: center; margin: 1.5em 0 1em; }
  h2 { margin: 1.2em 0 0.8em; }
  h3 { margin: 1em 0 0.6em; }
  img { max-width: 100% !important; height: auto !important; }
  a { color: #2563eb; text-decoration: none; }
  pre { white-space: pre-wrap !important; }
`, mr = 794, hr = ir(
  ({ url: T, onChapterChange: E, onFullWidthChange: j }, K) => {
    const z = dr(), C = c(null), f = c(null), w = c(E), g = c(j);
    w.current = E, g.current = j;
    const I = c(1), [Q, R] = p(!0), [y, M] = p(null), [H, W] = p([]), [v, d] = p(!1), [$, S] = p(""), [L, Y] = p(!1), P = c(!1);
    P.current = L;
    const k = l((e, t) => {
      var r;
      t > 0 && (I.current = t), (r = w.current) == null || r.call(w, Math.max(1, e + 1), I.current);
    }, []), F = l(() => {
      const e = f.current;
      e && e.prev().catch(() => {
      });
    }, []), X = l(() => {
      const e = f.current;
      e && e.next().catch(() => {
      });
    }, []), _ = l(() => d((e) => !e), []), D = l(() => {
      var u;
      const e = !P.current;
      Y(e), (u = g.current) == null || u.call(g, e);
      const t = f.current;
      if (!t) return;
      const r = t.renderer;
      r && r.setAttribute("max-inline-size", e ? "9999" : "720");
    }, []), rr = l((e) => {
      var t;
      S(e), d(!1), (t = f.current) == null || t.goTo(e).catch(() => {
      });
    }, []);
    lr(K, () => ({
      prevPage: F,
      nextPage: X,
      toggleFullWidth: D,
      toggleToc: _
    }), [F, X, D, _]), cr(() => {
      const e = C.current;
      if (!e) return;
      R(!0), M(null), W([]), d(!1), S(""), e.replaceChildren();
      let t = !1, r = null;
      return (async () => {
        var a, s, m, q, B, G;
        try {
          if (t) return;
          r = document.createElement("foliate-view"), e.appendChild(r), f.current = r, r.addEventListener("relocate", (V) => {
            var Z, J;
            const i = V.detail;
            if (!i) return;
            const b = i.location;
            if (b && typeof b.current == "number" && typeof b.total == "number")
              k(b.current, b.total);
            else {
              const or = ((J = (Z = f.current) == null ? void 0 : Z.book) == null ? void 0 : J.sections) ?? [], ar = i.index ?? 0, sr = i.fraction ?? 0, A = Math.max(or.length, 1), fr = Math.round((ar + sr) / A * A);
              k(fr, A);
            }
            const N = i.tocItem;
            N != null && N.href && S(N.href);
          });
          const h = await fetch(T);
          if (!h.ok) throw new Error(`请求失败: ${h.status}`);
          const tr = await h.blob();
          let O = "book.mobi";
          try {
            const i = new URL(T, window.location.href).pathname.split("/").pop();
            i && (O = decodeURIComponent(i));
          } catch {
          }
          const nr = new File([tr], O);
          if (await r.open(nr), t) {
            (s = (a = r.book) == null ? void 0 : a.destroy) == null || s.call(a);
            return;
          }
          const o = r.renderer;
          o && (o.setAttribute("animated", ""), o.setAttribute("max-inline-size", "720"), o.setAttribute("margin", "48"), o.setAttribute("gap", "5%"), (m = o.setStyles) == null || m.call(o, ur), await ((q = o.next) == null ? void 0 : q.call(o))), W(((B = r.book) == null ? void 0 : B.toc) ?? []), R(!1), k(0, ((G = r.book) == null ? void 0 : G.sections.length) ?? 1);
        } catch (h) {
          console.error("MOBI/AZW3 加载错误:", h), t || (M(z("mobi.load_failed")), R(!1));
        }
      })(), () => {
        var a, s, m;
        t = !0;
        try {
          (m = (s = (a = f.current) == null ? void 0 : a.book) == null ? void 0 : s.destroy) == null || m.call(s);
        } catch {
        }
        f.current = null, e.replaceChildren();
      };
    }, [T, k]);
    const er = l(
      (e) => !!e && e === $,
      [$]
    ), U = (e, t = 0) => /* @__PURE__ */ n("ul", { style: { listStyle: "none", padding: 0, margin: t > 0 ? "0 0 0 16px" : 0 }, children: e.map((r, u) => {
      var a, s;
      return /* @__PURE__ */ x("li", { children: [
        r.href ? /* @__PURE__ */ n(
          "button",
          {
            onClick: () => rr(r.href),
            className: `rfp-w-full rfp-text-left rfp-py-2 rfp-px-3 rfp-text-sm rfp-rounded rfp-transition-all rfp-truncate ${er(r.href) ? "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: r.label,
            children: (a = r.label) == null ? void 0 : a.trim()
          }
        ) : /* @__PURE__ */ n("div", { className: "rfp-w-full rfp-py-2 rfp-px-3 rfp-text-sm rfp-text-fg-tertiary rfp-truncate", children: (s = r.label) == null ? void 0 : s.trim() }),
        r.subitems && r.subitems.length > 0 && U(r.subitems, t + 1)
      ] }, `${r.href ?? r.label}-${u}`);
    }) });
    return /* @__PURE__ */ x("div", { className: "rfp-relative rfp-w-full rfp-h-full rfp-flex rfp-justify-center rfp-bg-[#f5f5f0] rfp-overflow-hidden", children: [
      y && /* @__PURE__ */ n("div", { className: "rfp-absolute rfp-inset-0 rfp-flex rfp-items-center rfp-justify-center rfp-text-fg-secondary rfp-text-center rfp-p-6", children: /* @__PURE__ */ n("p", { className: "rfp-text-lg", children: y }) }),
      Q && !y && /* @__PURE__ */ n("div", { className: "rfp-absolute rfp-inset-0 rfp-flex rfp-items-center rfp-justify-center rfp-z-10", children: /* @__PURE__ */ n("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" }) }),
      H.length > 0 && /* @__PURE__ */ x(
        "div",
        {
          className: "rfp-absolute rfp-inset-0 rfp-z-20 rfp-flex rfp-transition-opacity rfp-duration-300",
          style: { opacity: v ? 1 : 0, pointerEvents: v ? "auto" : "none" },
          children: [
            /* @__PURE__ */ x(
              "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: v ? "translateX(0)" : "translateX(-100%)" },
                children: [
                  /* @__PURE__ */ x("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__ */ n("span", { className: "rfp-text-fg-primary rfp-font-medium rfp-text-sm", children: z("toolbar.toc") }),
                    /* @__PURE__ */ n(
                      "button",
                      {
                        onClick: () => d(!1),
                        className: "rfp-text-fg-tertiary hover:rfp-text-fg-primary rfp-transition-colors",
                        children: /* @__PURE__ */ n(pr, { className: "rfp-w-4 rfp-h-4" })
                      }
                    )
                  ] }),
                  /* @__PURE__ */ n("div", { className: "rfp-flex-1 rfp-overflow-y-auto rfp-py-4 rfp-px-1", children: U(H) })
                ]
              }
            ),
            /* @__PURE__ */ n(
              "div",
              {
                className: "rfp-flex-1 rfp-transition-opacity rfp-duration-300",
                style: { background: v ? "rgba(0,0,0,0.3)" : "transparent" },
                onClick: () => d(!1)
              }
            )
          ]
        }
      ),
      !y && /* @__PURE__ */ n(
        "div",
        {
          ref: C,
          className: "rfp-h-full rfp-bg-surface-toolbar rfp-shadow-lg",
          style: {
            width: L ? "100%" : `${mr}px`,
            maxWidth: "100%",
            transition: "width 0.3s ease"
          }
        }
      )
    ] });
  }
);
hr.displayName = "MobiRenderer";
export {
  hr as MobiRenderer
};
//# sourceMappingURL=index-tH_f5uEK.mjs.map
