import { jsxs as U, jsx as e, Fragment as O } from "react/jsx-runtime";
import { useRef as H, useState as a, useEffect as L, useCallback as S, useMemo as G, Suspense as J, lazy as Q } from "react";
import { createPortal as V } from "react-dom";
import { ChevronRight as K, FolderOpen as ee, Folder as re, FileImage as te, FileText as ne, FileCode as fe, File as se } from "lucide-react";
import { u as le, c as q, _ as ie, F as pe, v as ae, w as oe, p as ce, d as de } from "./index-Bk6WqXgW.mjs";
const ue = ({
  left: n,
  right: l,
  initialLeftWidth: h = 280,
  minLeftWidth: x = 160,
  maxLeftWidth: d = 640,
  minRightWidth: y = 200,
  storageKey: i,
  desktopMedia: w = "(min-width: 768px)",
  className: g = ""
}) => {
  const v = H(null), [u, N] = a(() => {
    if (i && typeof window < "u") {
      const s = Number(window.localStorage.getItem(i));
      if (!isNaN(s) && s > 0) return s;
    }
    return h;
  }), [m, b] = a(!1), [r, j] = a(!1);
  L(() => {
    if (typeof window > "u") return;
    const s = window.matchMedia(w), o = () => j(s.matches);
    return o(), s.addEventListener("change", o), () => s.removeEventListener("change", o);
  }, [w]), L(() => {
    if (!m) return;
    const s = (M) => {
      if (!v.current) return;
      const c = v.current.getBoundingClientRect(), R = M.clientX - c.left, T = c.width - y - 6, $ = Math.min(d, T), P = Math.max(x, Math.min($, R));
      N(P);
    }, o = () => b(!1);
    window.addEventListener("mousemove", s), window.addEventListener("mouseup", o);
    const F = document.body.style.cursor, z = document.body.style.userSelect;
    return document.body.style.cursor = "col-resize", document.body.style.userSelect = "none", () => {
      window.removeEventListener("mousemove", s), window.removeEventListener("mouseup", o), document.body.style.cursor = F, document.body.style.userSelect = z;
    };
  }, [m, x, d, y]), L(() => {
    if (!(!i || m))
      try {
        window.localStorage.setItem(i, String(u));
      } catch {
      }
  }, [u, i, m]);
  const D = S((s) => {
    s.preventDefault(), b(!0);
  }, []);
  return /* @__PURE__ */ U(
    "div",
    {
      ref: v,
      className: `rfp-w-full rfp-h-full rfp-flex rfp-flex-col md:rfp-flex-row rfp-min-h-0 rfp-min-w-0 ${g}`,
      children: [
        /* @__PURE__ */ e(
          "div",
          {
            className: "rfp-min-h-0 rfp-min-w-0 rfp-flex-shrink-0 rfp-w-full rfp-max-h-60 md:rfp-h-full md:rfp-max-h-none",
            style: r ? { width: `${u}px` } : void 0,
            children: n
          }
        ),
        /* @__PURE__ */ e(
          "div",
          {
            role: "separator",
            "aria-orientation": "vertical",
            onMouseDown: D,
            className: `rfp-hidden md:rfp-block rfp-relative rfp-w-1.5 rfp-flex-shrink-0 rfp-cursor-col-resize rfp-transition-colors ${m ? "rfp-bg-surface-toolbar" : "rfp-bg-surface-2 hover:rfp-bg-surface-3"}`,
            children: /* @__PURE__ */ e("span", { className: "rfp-absolute rfp-inset-y-0 -rfp-left-1 -rfp-right-1" })
          }
        ),
        /* @__PURE__ */ e("div", { className: "rfp-flex-1 rfp-min-w-0 rfp-min-h-0 rfp-overflow-hidden", children: l })
      ]
    }
  );
}, me = Q(
  () => import("./index-Bk6WqXgW.mjs").then((n) => n.q).then((n) => ({ default: n.FilePreviewContent }))
), he = (n) => {
  const l = de({ name: n, type: "" });
  return l === "image" ? te : l === "text" || l === "markdown" || l === "json" || l === "csv" || l === "xml" || l === "subtitle" ? n.endsWith(".md") || n.endsWith(".markdown") ? ne : fe : se;
}, Z = ({
  node: n,
  depth: l,
  selectedPath: h,
  expanded: x,
  onToggle: d,
  onSelect: y,
  onHover: i,
  onLeave: w
}) => {
  var b;
  const g = x.has(n.path), v = h === n.path, u = { paddingLeft: `${l * 14 + 10}px` }, N = (r) => {
    const j = r.currentTarget.getBoundingClientRect();
    i(n.name || "/", j);
  };
  if (n.isDir)
    return /* @__PURE__ */ U(O, { children: [
      /* @__PURE__ */ U(
        "button",
        {
          type: "button",
          onClick: () => d(n.path),
          onMouseEnter: N,
          onMouseLeave: w,
          className: "rfp-w-full rfp-flex rfp-items-center rfp-gap-1.5 rfp-py-1.5 rfp-pr-2 rfp-text-left rfp-text-fg-secondary hover:rfp-bg-surface-1 rfp-text-sm",
          style: u,
          children: [
            /* @__PURE__ */ e(
              K,
              {
                className: `rfp-w-3.5 rfp-h-3.5 rfp-flex-shrink-0 rfp-transition-transform ${g ? "rfp-rotate-90" : ""}`
              }
            ),
            g ? /* @__PURE__ */ e(ee, { className: "rfp-w-4 rfp-h-4 rfp-flex-shrink-0 rfp-text-amber-300/80" }) : /* @__PURE__ */ e(re, { className: "rfp-w-4 rfp-h-4 rfp-flex-shrink-0 rfp-text-amber-300/80" }),
            /* @__PURE__ */ e("span", { className: "rfp-truncate rfp-flex-1 rfp-min-w-0", children: n.name || "/" })
          ]
        }
      ),
      g && ((b = n.children) == null ? void 0 : b.map((r) => /* @__PURE__ */ e(
        Z,
        {
          node: r,
          depth: l + 1,
          selectedPath: h,
          expanded: x,
          onToggle: d,
          onSelect: y,
          onHover: i,
          onLeave: w
        },
        r.path
      )))
    ] });
  const m = he(n.name);
  return /* @__PURE__ */ U(
    "button",
    {
      type: "button",
      onClick: () => y(n),
      onMouseEnter: N,
      onMouseLeave: w,
      className: `rfp-w-full rfp-flex rfp-items-center rfp-gap-1.5 rfp-py-1.5 rfp-pr-2 rfp-text-left rfp-text-sm ${v ? "rfp-bg-surface-2 rfp-text-fg-primary" : "rfp-text-fg-secondary hover:rfp-bg-surface-1"}`,
      style: u,
      children: [
        /* @__PURE__ */ e("span", { className: "rfp-w-3.5 rfp-h-3.5 rfp-flex-shrink-0" }),
        /* @__PURE__ */ e(m, { className: "rfp-w-4 rfp-h-4 rfp-flex-shrink-0 rfp-text-fg-tertiary" }),
        /* @__PURE__ */ e("span", { className: "rfp-flex-1 rfp-truncate rfp-min-w-0", children: n.name }),
        /* @__PURE__ */ e("span", { className: "rfp-text-xs rfp-text-fg-disabled rfp-flex-shrink-0 rfp-ml-2", children: ce(n.size) })
      ]
    }
  );
}, ge = ({ url: n, nestingDepth: l = 0, onStatsChange: h }) => {
  var W;
  const x = le(), [d, y] = a(null), [i, w] = a(null), [g, v] = a(!0), [u, N] = a(null), [m, b] = a(/* @__PURE__ */ new Set([""])), [r, j] = a(null), [D, s] = a(!1), [o, F] = a(null), [z, M] = a(null), c = H(h);
  L(() => {
    c.current = h;
  }, [h]), L(() => {
    let t = !1;
    return (async () => {
      try {
        v(!0), N(null);
        const p = await fetch(n);
        if (!p.ok) throw new Error("加载失败");
        const E = await p.arrayBuffer(), k = await pe(E);
        if (t) return;
        const I = ae(k), C = oe(I);
        y(k), w(C);
        const _ = /* @__PURE__ */ new Set([""]);
        if (C.children)
          for (const B of C.children) B.isDir && _.add(B.path);
        b(_);
      } catch (p) {
        console.error(p), t || N(x("zip.load_failed"));
      } finally {
        t || v(!1);
      }
    })(), () => {
      t = !0;
    };
  }, [n]), L(() => () => {
    r != null && r.blobUrl && URL.revokeObjectURL(r.blobUrl);
  }, [r]);
  const R = G(() => {
    if (!i) return null;
    let t = 0, f = 0, p = 0;
    const E = (k) => {
      var I;
      k.isDir ? (k.path && f++, (I = k.children) == null || I.forEach(E)) : (t++, p += k.size);
    };
    return E(i), { files: t, dirs: f, size: p };
  }, [i]);
  L(() => {
    var t;
    return (t = c.current) == null || t.call(c, R), () => {
      var f;
      (f = c.current) == null || f.call(c, null);
    };
  }, [R]);
  const T = S((t) => {
    b((f) => {
      const p = new Set(f);
      return p.has(t) ? p.delete(t) : p.add(t), p;
    });
  }, []), $ = S((t, f) => {
    M({
      text: t,
      x: f.right + 8,
      y: f.top + f.height / 2
    });
  }, []), P = S(() => {
    M(null);
  }, []), X = S(
    async (t) => {
      if (!(!d || t.isDir)) {
        r != null && r.blobUrl && URL.revokeObjectURL(r.blobUrl), s(!0), F(null);
        try {
          const f = q(t.name), p = await ie(d, t.path, f !== "application/octet-stream" ? f : void 0), E = URL.createObjectURL(p);
          j({ path: t.path, name: t.name, size: t.size, blobUrl: E });
        } catch (f) {
          console.error(f), F("条目读取失败");
        } finally {
          s(!1);
        }
      }
    },
    [d, r]
  );
  if (g)
    return /* @__PURE__ */ e("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-w-full rfp-h-full", 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" }) });
  if (u || !i)
    return /* @__PURE__ */ e("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-w-full rfp-h-full", children: /* @__PURE__ */ e("div", { className: "rfp-text-fg-secondary rfp-text-center", children: /* @__PURE__ */ e("p", { className: "rfp-text-lg", children: u || x("zip.parse_failed") }) }) });
  const Y = /* @__PURE__ */ e("div", { className: "rfp-w-full rfp-h-full rfp-overflow-auto", children: (W = i.children) == null ? void 0 : W.map((t) => /* @__PURE__ */ e(
    Z,
    {
      node: t,
      depth: 0,
      selectedPath: (r == null ? void 0 : r.path) ?? null,
      expanded: m,
      onToggle: T,
      onSelect: X,
      onHover: $,
      onLeave: P
    },
    t.path
  )) }), A = /* @__PURE__ */ U("div", { className: "rfp-w-full rfp-h-full rfp-flex rfp-flex-col", children: [
    !r && /* @__PURE__ */ e("div", { className: "rfp-flex-1 rfp-flex rfp-items-center rfp-justify-center rfp-text-fg-muted rfp-text-sm rfp-p-6", children: "从左侧选择一个文件以预览" }),
    r && D && /* @__PURE__ */ e("div", { className: "rfp-flex-1 rfp-flex rfp-items-center rfp-justify-center", 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" }) }),
    r && !D && o && /* @__PURE__ */ e("div", { className: "rfp-flex-1 rfp-flex rfp-items-center rfp-justify-center rfp-text-fg-secondary", children: o }),
    r && !D && !o && /* @__PURE__ */ e(O, { children: /* @__PURE__ */ e("div", { className: "rfp-flex-1 rfp-min-h-0 rfp-overflow-hidden rfp-flex rfp-relative rfp-z-0", children: /* @__PURE__ */ e(
      J,
      {
        fallback: /* @__PURE__ */ e("div", { className: "rfp-flex-1 rfp-flex rfp-items-center rfp-justify-center", 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" }) }),
        children: /* @__PURE__ */ e(
          me,
          {
            mode: "embed",
            files: [{ name: r.name, url: r.blobUrl, type: q(r.name) }],
            currentIndex: 0,
            zipNestingDepth: l + 1
          }
        )
      }
    ) }) })
  ] });
  return /* @__PURE__ */ U(O, { children: [
    /* @__PURE__ */ e(
      ue,
      {
        left: Y,
        right: A,
        initialLeftWidth: 280,
        minLeftWidth: 180,
        maxLeftWidth: 560,
        storageKey: "rfp-zip-split-left"
      }
    ),
    z && typeof document < "u" && V(
      /* @__PURE__ */ e(
        "div",
        {
          className: "rfp-fixed rfp-z-[9999] rfp-pointer-events-none rfp-px-2 rfp-py-1 rfp-bg-[rgba(0,0,0,0.85)] rfp-text-fg-primary rfp-text-xs rfp-rounded rfp-whitespace-nowrap rfp-shadow-lg",
          style: {
            left: `${z.x}px`,
            top: `${z.y}px`,
            transform: "translateY(-50%)"
          },
          children: z.text
        }
      ),
      document.body
    )
  ] });
};
export {
  ge as ZipRenderer
};
//# sourceMappingURL=index-P9qAkKfO.mjs.map
