import { jsxs as h, jsx as n } from "react/jsx-runtime";
import { useState as k, useRef as i, useCallback as E, useEffect as D } from "react";
import { FileSpreadsheet as T } from "lucide-react";
import M from "exceljs";
import W from "x-data-spreadsheet";
/* empty css                       */
import { u as j, C as L } from "./index-Bk6WqXgW.mjs";
const O = ({ url: x }) => {
  const l = j(), [w, g] = k(!0), [b, N] = k(null), e = i(null), v = i(null), a = i(null), d = i(null), p = i(null), y = i({ width: 0, height: 0 }), c = E(() => {
    if (!e.current) return { width: 800, height: 600 };
    const t = e.current.clientWidth, s = e.current.clientHeight, f = t > 100 ? t : 800, r = s > 100 ? s : 600;
    return { width: f, height: r };
  }, []), m = E(() => {
    if (!e.current || !a.current) return;
    e.current.innerHTML = "", v.current = null;
    const { width: t, height: s } = c(), f = t < 640, r = new W(e.current, {
      mode: "read",
      showToolbar: !1,
      showContextmenu: !1,
      showGrid: !0,
      row: {
        len: 100,
        height: 25
      },
      col: {
        len: 26,
        width: f ? 80 : 100,
        indexWidth: f ? 40 : 60,
        minWidth: f ? 40 : 60
      },
      view: {
        height: () => s,
        width: () => t
      }
    });
    r.loadData(a.current), v.current = r;
  }, [c]);
  return D(() => {
    if (!e.current) return;
    let t = !0;
    const s = () => {
      if (t) {
        t = !1, y.current = c();
        return;
      }
      const f = c(), r = y.current, o = Math.abs(r.width - f.width), u = Math.abs(r.height - f.height);
      o < 10 && u < 10 || (y.current = f, p.current && clearTimeout(p.current), p.current = window.setTimeout(() => {
        a.current && m();
      }, 500));
    };
    return d.current = new ResizeObserver(() => {
      s();
    }), d.current.observe(e.current), () => {
      d.current && d.current.disconnect(), p.current && clearTimeout(p.current);
    };
  }, [c, m]), D(() => {
    let t = !0;
    const s = async () => {
      if (e.current) {
        g(!0), N(null);
        try {
          const r = await fetch(x, {
            mode: "cors",
            credentials: "omit",
            redirect: "follow"
          });
          if (!r.ok)
            throw r.status === 404 ? new Error(l("xlsx.not_found")) : r.status === 403 ? new Error("无权限访问此文件") : new Error(`文件加载失败 (${r.status})`);
          const o = await r.arrayBuffer();
          if (o.byteLength === 0)
            throw new Error("文件为空");
          const u = new M.Workbook();
          await u.xlsx.load(o);
          const R = L(u);
          if (!t) return;
          a.current = R, m(), g(!1);
        } catch (r) {
          if (t) {
            console.error("Excel 解析错误:", r);
            let o = l("xlsx.parse_failed");
            r instanceof Error && (o = r.message), N(o), g(!1);
          }
        }
      }
    }, f = setTimeout(() => {
      requestAnimationFrame(() => {
        s();
      });
    }, 100);
    return () => {
      t = !1, clearTimeout(f), a.current = null, e.current && (e.current.innerHTML = ""), v.current = null;
    };
  }, [x, m]), /* @__PURE__ */ h("div", { className: "rfp-relative rfp-flex rfp-flex-col rfp-items-center rfp-w-full rfp-h-full", children: [
    w && /* @__PURE__ */ n("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__ */ h("div", { className: "rfp-text-center", children: [
      /* @__PURE__ */ n("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__ */ n("p", { className: "rfp-text-xs md:rfp-text-sm rfp-text-fg-secondary rfp-font-medium", children: l("xlsx.loading") })
    ] }) }),
    b && !w && /* @__PURE__ */ n("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__ */ h("div", { className: "rfp-text-center rfp-max-w-sm md:rfp-max-w-md rfp-px-4", children: [
      /* @__PURE__ */ n("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-green-500 rfp-via-emerald-500 rfp-to-teal-500 rfp-flex rfp-items-center rfp-justify-center rfp-shadow-2xl", children: /* @__PURE__ */ n(T, { className: "rfp-w-12 rfp-h-12 md:rfp-w-16 md:rfp-h-16 rfp-text-fg-primary" }) }),
      /* @__PURE__ */ n("p", { className: "rfp-text-lg md:rfp-text-xl rfp-text-fg-primary rfp-mb-2 md:rfp-mb-3 rfp-font-medium", children: l("xlsx.load_failed") }),
      /* @__PURE__ */ n("p", { className: "rfp-text-xs md:rfp-text-sm rfp-text-fg-tertiary rfp-mb-4 md:rfp-mb-6", children: b }),
      /* @__PURE__ */ h(
        "a",
        {
          href: x,
          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__ */ n("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__ */ n("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" }) }),
            l("common.download")
          ]
        }
      ),
      /* @__PURE__ */ n("p", { className: "rfp-text-xs rfp-text-fg-muted rfp-mt-3 md:rfp-mt-4", children: "提示：可以使用 Microsoft Excel 或 WPS 打开" })
    ] }) }),
    !b && /* @__PURE__ */ n(
      "div",
      {
        ref: e,
        className: "xlsx-spreadsheet-container rfp-w-full rfp-h-full",
        style: { opacity: w ? 0 : 1 }
      }
    )
  ] });
};
export {
  O as XlsxRenderer
};
//# sourceMappingURL=index-BJoYaujz.mjs.map
