import { o as T } from "./rolldown-runtime.js";
import { i as _, t as R } from "./vendor/react.js";
import { t as g } from "./utils.js";
import { n as z } from "./interaction-context.js";
import { t as j } from "./checkbox2.js";
import { a as A, i as $, n as w, o as S, r as y, t as q } from "./table2.js";
var m = /* @__PURE__ */ T(_(), 1), t = R();
function k(i, n) {
  if (i.id !== void 0) return String(i.id);
  if (n.length > 0) {
    const h = n[0].key;
    return String(i[h] ?? "");
  }
  return "";
}
function H({ schema: i, className: n }) {
  const { onInteraction: h } = z(), [l, b] = m.useState(/* @__PURE__ */ new Set()), { id: p, title: u, columns: o, data: c, selectable: f = !1 } = i, s = m.useMemo(() => c.map((e) => k(e, o)), [c, o]), d = m.useCallback((e) => {
    h?.({
      componentId: p,
      componentType: "table",
      action: "select",
      value: { selectedRows: e },
      timestamp: Date.now()
    });
  }, [p, h]), v = m.useCallback((e, r) => {
    b((x) => {
      const a = new Set(x);
      return r ? a.add(e) : a.delete(e), d(Array.from(a)), a;
    });
  }, [d]), C = m.useCallback((e) => {
    e ? (b(new Set(s)), d(s)) : (b(/* @__PURE__ */ new Set()), d([]));
  }, [s, d]), I = s.length > 0 && l.size === s.length, N = l.size > 0 && l.size < s.length;
  return !c || c.length === 0 ? /* @__PURE__ */ (0, t.jsx)("div", {
    "data-slot": "ai-table",
    role: "status",
    className: g("flex items-center justify-center rounded-lg border border-border bg-muted p-8 text-muted-foreground", n),
    children: "No data available"
  }) : /* @__PURE__ */ (0, t.jsxs)("div", {
    "data-slot": "ai-table",
    className: g("flex flex-col gap-2", n),
    children: [
      u && /* @__PURE__ */ (0, t.jsx)("h3", {
        className: "text-sm font-medium text-foreground",
        children: u
      }),
      f && /* @__PURE__ */ (0, t.jsx)("div", {
        className: "sr-only",
        "aria-live": "polite",
        "aria-atomic": "true",
        children: l.size > 0 ? `${l.size} of ${s.length} rows selected` : "No rows selected"
      }),
      /* @__PURE__ */ (0, t.jsxs)(A, {
        "aria-label": u ?? "Interactive data table",
        children: [/* @__PURE__ */ (0, t.jsx)($, { children: /* @__PURE__ */ (0, t.jsxs)(S, { children: [f && /* @__PURE__ */ (0, t.jsx)(y, {
          className: "w-[40px]",
          children: /* @__PURE__ */ (0, t.jsx)(j, {
            checked: I,
            indeterminate: N,
            onCheckedChange: C,
            "aria-label": "Select all rows"
          })
        }), o.map((e) => /* @__PURE__ */ (0, t.jsx)(y, {
          style: { width: typeof e.width == "number" ? `${e.width}px` : e.width },
          children: e.label
        }, e.key))] }) }), /* @__PURE__ */ (0, t.jsx)(q, { children: c.map((e) => {
          const r = k(e, o), x = l.has(r);
          return /* @__PURE__ */ (0, t.jsxs)(S, {
            "data-state": x ? "selected" : void 0,
            className: "transition-colors",
            children: [f && /* @__PURE__ */ (0, t.jsx)(w, { children: /* @__PURE__ */ (0, t.jsx)(j, {
              checked: x,
              onCheckedChange: (a) => v(r, a === !0),
              "aria-label": `Select row ${r}`
            }) }), o.map((a) => /* @__PURE__ */ (0, t.jsx)(w, { children: String(e[a.key] ?? "") }, a.key))]
          }, r);
        }) })]
      })
    ]
  });
}
H.displayName = "AITable";
export {
  H as t
};

//# sourceMappingURL=AITable.js.map