import { jsxs as y, jsx as s } from "react/jsx-runtime";
import { useState as R, useRef as i, useCallback as T, useEffect as M } from "react";
import { FileSpreadsheet as j } from "lucide-react";
import k from "x-data-spreadsheet";
/* empty css                       */
import { u as z, h as C, g as S, m as H, k as W } from "./index-Bk6WqXgW.mjs";
const A = ({ url: D, fileName: d }) => {
  const m = z(), [h, x] = R(!0), [w, N] = R(null), r = i(null), b = i(null), c = i(null), l = i(null), a = i(null), g = i({ width: 0, height: 0 }), o = T(() => {
    if (!r.current) return { width: 800, height: 600 };
    const e = r.current.clientWidth, f = r.current.clientHeight, t = e > 100 ? e : 800, n = f > 100 ? f : 600;
    return { width: t, height: n };
  }, []), p = T(() => {
    if (!r.current || !c.current) return;
    r.current.innerHTML = "", b.current = null;
    const { width: e, height: f } = o(), t = e < 640, n = new k(r.current, {
      mode: "read",
      showToolbar: !1,
      showContextmenu: !1,
      showGrid: !0,
      row: {
        len: 100,
        height: 25
      },
      col: {
        len: 26,
        width: t ? 80 : 100,
        indexWidth: t ? 40 : 60,
        minWidth: t ? 40 : 60
      },
      view: {
        height: () => f,
        width: () => e
      }
    });
    n.loadData(c.current), b.current = n;
  }, [o]);
  return M(() => {
    if (!r.current) return;
    let e = !0;
    const f = () => {
      if (e) {
        e = !1, g.current = o();
        return;
      }
      const t = o(), n = g.current, u = Math.abs(n.width - t.width), v = Math.abs(n.height - t.height);
      u < 10 && v < 10 || (g.current = t, a.current && clearTimeout(a.current), a.current = window.setTimeout(() => {
        c.current && p();
      }, 500));
    };
    return l.current = new ResizeObserver(() => {
      f();
    }), l.current.observe(r.current), () => {
      l.current && l.current.disconnect(), a.current && clearTimeout(a.current);
    };
  }, [o, p]), M(() => {
    let e = !0;
    const f = async () => {
      if (r.current) {
        x(!0), N(null);
        try {
          const n = await C(D), u = S(n, { delimiter: H(d) }), v = W(u.header, u.rows, d);
          if (!e) return;
          c.current = v, p(), x(!1);
        } catch (n) {
          e && (console.error("CSV 解析错误:", n), N(m("csv.load_failed")), x(!1));
        }
      }
    }, t = setTimeout(() => {
      requestAnimationFrame(() => {
        f();
      });
    }, 100);
    return () => {
      e = !1, clearTimeout(t), c.current = null, r.current && (r.current.innerHTML = ""), b.current = null;
    };
  }, [D, d, p]), /* @__PURE__ */ y("div", { className: "rfp-relative rfp-flex rfp-flex-col rfp-items-center rfp-w-full rfp-h-full", children: [
    h && /* @__PURE__ */ s("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__ */ y("div", { className: "rfp-text-center", children: [
      /* @__PURE__ */ s("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__ */ s("p", { className: "rfp-text-xs md:rfp-text-sm rfp-text-fg-secondary rfp-font-medium", children: m("csv.loading") })
    ] }) }),
    w && !h && /* @__PURE__ */ s("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__ */ y("div", { className: "rfp-text-center rfp-max-w-sm md:rfp-max-w-md rfp-px-4", children: [
      /* @__PURE__ */ s("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__ */ s(j, { className: "rfp-w-12 rfp-h-12 md:rfp-w-16 md:rfp-h-16 rfp-text-fg-primary" }) }),
      /* @__PURE__ */ s("p", { className: "rfp-text-lg md:rfp-text-xl rfp-text-fg-primary rfp-mb-2 md:rfp-mb-3 rfp-font-medium", children: m("csv.load_failed") }),
      /* @__PURE__ */ s("p", { className: "rfp-text-xs md:rfp-text-sm rfp-text-fg-tertiary rfp-mb-4 md:rfp-mb-6", children: w })
    ] }) }),
    !w && /* @__PURE__ */ s(
      "div",
      {
        ref: r,
        className: "xlsx-spreadsheet-container rfp-w-full rfp-h-full",
        style: { opacity: h ? 0 : 1 }
      }
    )
  ] });
};
export {
  A as CsvRenderer
};
//# sourceMappingURL=index-CuhaFmuJ.mjs.map
