import { jsx as l, jsxs as j } from "react/jsx-runtime";
import { forwardRef as D, Children as L, useRef as h, useState as q, useCallback as z, useImperativeHandle as $ } from "react";
import G from "../smoothScrollbars/SmoothScrollbars.js";
import J from "../virtualList/useVirtualListResizeObserver.js";
import { toCssSize as K } from "./layout/columnSizing.js";
import Q from "./layout/useTableBodyScrollBottom.js";
import U from "./layout/useTableVirtualization.js";
import { shouldAnimateBodyRows as W } from "./runtime/shouldAnimateBodyRows.js";
import H from "./render/body/TableBodyContent.js";
import { useOptionalTableInteractionContext as X } from "./context/TableInteractionContext.js";
import { useOptionalTableLayoutContext as Y } from "./context/TableLayoutContext.js";
import { useOptionalTableRenderConfigContext as Z } from "./context/TableRenderConfigContext.js";
import { TableSectionContext as _, TableBodyContext as oo } from "./context/TableStructureContext.js";
import { useOptionalTableRenderContext as eo, TableRenderContext as to } from "./context/TableRenderContext.js";
const ro = (s) => {
  if (!s)
    return;
  const i = Number.parseFloat(s);
  return Number.isNaN(i) ? void 0 : i;
}, no = (s, i) => {
  const { children: b, maxHeight: O, onScrollBottom: d, scrollBottomOffset: P = 0, ...w } = s, t = Z(), o = eo(), R = X(), f = Y(), x = !!o && o.columns.length > 0, C = L.toArray(b).length > 0, c = h(0), m = h(null), y = h(null), [a, V] = q(null);
  c.current = 0;
  const r = K(O), k = ro(r), g = x && t?.virtualizedRows === !0 && !!r, E = (o?.bodyRows.length ?? 0) > 0, n = z(
    () => a?.view ?? y.current?.view ?? null,
    [a?.view]
  ), I = z((e) => {
    y.current = e, V(
      (A) => A?.view === e?.view ? A : e
    );
  }, []);
  $(
    i,
    () => ({
      scrollToTop: () => {
        n()?.scrollTo({ top: 0 }), requestAnimationFrame(() => {
          n()?.scrollTo({ top: 0 });
        });
      },
      scrollTo: (e) => {
        n()?.scrollTo(e);
      },
      getScrollElement: n
    }),
    [n]
  );
  const { isVirtualized: u, measureRow: N, paddingBottom: v, paddingTop: T, virtualRows: p } = U({
    enabled: g && E,
    overscan: t?.virtualizationOverscan ?? 0,
    rows: o?.bodyRows ?? [],
    scrollContainer: a,
    viewportHeightFallback: k
  });
  J(m, N, [u, p]);
  const { handleScrollBottom: F } = Q({
    enabled: !!r && !!d,
    itemCount: o?.bodyRows.length ?? 0,
    onScrollBottom: d,
    scrollBottomOffset: P,
    scrollElement: a?.view
  });
  if (!t || !o || !R || !f || !x)
    return null;
  const B = o.columns.length + (R.showSelectionColumn ? 1 : 0), S = W({
    rowAnimationProps: t.rowAnimationProps,
    expandedRowAnimation: t.expandedRowAnimation,
    hasExpandableRows: o.hasExpandableRows,
    hasExpandedRows: o.bodyRows.some(
      (e) => e.kind === "expanded" || e.kind === "expanded-full-width"
    )
  });
  o.renderDraft.bodyMaxHeight = r;
  const M = /* @__PURE__ */ l(to.Provider, { value: { ...o, isRegisteringBodyRows: !0 }, children: b });
  return /* @__PURE__ */ l(_.Provider, { value: "body", children: /* @__PURE__ */ j(
    oo.Provider,
    {
      value: {
        nextRowIndex: () => {
          const e = c.current;
          return c.current += 1, e;
        }
      },
      children: [
        M,
        r ? /* @__PURE__ */ l(
          G,
          {
            className: "table-body-scroll-wrapper",
            autoHeight: !0,
            autoHeightMax: r,
            onScroll: (e) => {
              f.handleBodyScroll?.(e), F(e?.currentTarget);
            },
            role: "presentation",
            ref: g || d ? I : void 0,
            children: /* @__PURE__ */ l(
              H,
              {
                columnCount: B,
                hasDeclaredChildren: C,
                htmlAttributes: w,
                shouldAnimateBodyRows: S,
                isVirtualized: u,
                itemsRef: m,
                paddingBottom: v,
                paddingTop: T,
                renderConfigContext: t,
                renderContext: o,
                virtualRows: p
              }
            )
          }
        ) : /* @__PURE__ */ l(
          H,
          {
            columnCount: B,
            hasDeclaredChildren: C,
            htmlAttributes: w,
            shouldAnimateBodyRows: S,
            isVirtualized: u,
            itemsRef: m,
            paddingBottom: v,
            paddingTop: T,
            renderConfigContext: t,
            renderContext: o,
            virtualRows: p
          }
        )
      ]
    }
  ) });
}, Co = D(no);
export {
  Co as default
};
//# sourceMappingURL=TableBody.js.map
