import { jsx as a, jsxs as b } from "react/jsx-runtime";
import { useTableState as x, TableHeader as D, Column as A, TableBody as O, Row as R, Cell as E } from "@react-stately/table";
import { useState as H, useRef as z } from "react";
import { TableCellType as m, TableCell as F } from "./TableCell.es.js";
import { TableColumnHeaderType as g, TableColumnHeader as $ } from "./TableColumnHeader.es.js";
import { TableHeaderRow as j } from "./TableHeaderRow.es.js";
import { TableRow as K } from "./TableRow.es.js";
var L = /* @__PURE__ */ ((e) => (e.NoSelect = "none", e.SingleSelect = "single", e.MultiSelect = "multiple", e))(L || {}), _ = /* @__PURE__ */ ((e) => (e.Ascending = "ascending", e.Descending = "descending", e))(_ || {});
const B = "descending", I = (e, n, d = !1) => ({
  children: [
    /* @__PURE__ */ a(D, { columns: e, children: (t) => {
      const s = !!(t.sortable && d);
      return /* @__PURE__ */ a(A, { allowsSorting: s, align: t.align, children: t.titleNode ?? t.name });
    } }, "table-header"),
    /* @__PURE__ */ a(O, { items: n, children: (t) => /* @__PURE__ */ a(R, { children: (s) => /* @__PURE__ */ a(
      E,
      {
        "aria-label": t.cells[s].ariaLabel,
        children: t.cells[s].value
      },
      `${t.key}-${s}`
    ) }) }, "table-body")
  ]
}), P = (e, n) => e.find(({ key: d }) => d === n) || null, U = (e) => e.map(({ key: n }) => n), q = ({
  columns: e,
  rows: n,
  onSelectionChange: d,
  selectionMode: t = "none",
  onSortChange: s,
  selectedRowIds: c = [],
  ariaLabel: w = "Table"
}) => {
  const y = t === "single" || t === "multiple", [p, k] = H({
    sortedColumnKey: void 0,
    sortOrder: void 0
  }), u = (l, r) => {
    const i = r === "ascending" ? "descending" : "ascending";
    k({
      sortedColumnKey: l,
      sortOrder: p.sortedColumnKey !== l ? B : i
    }), s == null || s(l, r);
  }, T = U(n), f = z(null), C = I(e, n, !!s), S = x({
    ...C,
    selectionMode: t,
    showSelectionCheckboxes: y
  }), { collection: h } = S;
  return /* @__PURE__ */ a("div", { className: "tw-w-full tw-max-h-96 sm:tw-max-h-full", children: /* @__PURE__ */ b(
    "table",
    {
      "aria-label": w,
      ref: f,
      className: "tw-border-collapse tw-table-auto tw-w-full tw-min-w-max tw-text-left dark:tw-bg-black-100 dark:tw-text-black-20",
      children: [
        /* @__PURE__ */ a("thead", { children: h.headerRows.map((l) => /* @__PURE__ */ b(j, { children: [
          [...l.childNodes].map((r) => {
            var o;
            const i = r.props.isSelectionCell ? g.SelectAll : g.Default;
            return /* @__PURE__ */ a(
              $,
              {
                column: r,
                type: i,
                rowIds: T,
                sortDirection: p.sortOrder,
                selectionMode: t,
                isColumnSorted: p.sortedColumnKey === r.key,
                handleSortChange: u,
                setSelectedRows: d,
                align: (o = r.props) == null ? void 0 : o.align
              },
              r.key
            );
          }),
          /* @__PURE__ */ a("td", {})
        ] }, l.key)) }),
        /* @__PURE__ */ a("tbody", { className: "tw-relative tw-z-[-1] sm:tw-z-auto", children: [...h.body.childNodes].map((l) => {
          const r = P(n, l.key);
          return /* @__PURE__ */ b(K, { isSelected: c.includes(l.key), children: [
            [...l.childNodes].map((i) => {
              const o = e.find(
                ({ key: N }) => N === String(i.key).split(`${l.key}-`)[1]
              ), v = i.props.isSelectionCell ? m.Checkbox : m.Default;
              return /* @__PURE__ */ a(
                F,
                {
                  cell: i,
                  selectionMode: t,
                  type: v,
                  isChecked: c.includes(l.key),
                  selectedRows: c,
                  setSelectedRows: d,
                  align: o == null ? void 0 : o.align
                },
                i.key
              );
            }),
            r != null && r.actionElements ? /* @__PURE__ */ a("td", { className: "tw-sticky tw-right-0 tw-z-10", "data-test-id": "table-actions", children: /* @__PURE__ */ a("div", { className: "tw-float-right hover:tw-bg-gradient-to-r hover:tw-from-transparent hover:tw-to-black-0 dark:hover:tw-to-black-95 tw-py-4 tw-pr-8 tw-pl-4", children: r.actionElements }) }) : null
          ] }, l.key);
        }) })
      ]
    }
  ) });
};
q.displayName = "FondueTable";
export {
  L as SelectionMode,
  _ as SortDirection,
  q as Table
};
//# sourceMappingURL=Table.es.js.map
