"use client";
import { jsx as s, jsxs as C } from "react/jsx-runtime";
import { flexRender as oe } from "../../../../../node_modules/@tanstack/react-table/build/lib/index.js";
import { useRef as ne, useState as G, useMemo as b, useCallback as F, useLayoutEffect as re, useEffect as B } from "react";
import { cn as L } from "../../../../../lib/utils.js";
import { ScrollArea as se, ScrollBar as P } from "../../../scroll-area.js";
import { Table as ie, TableBody as A, TableRow as I, TableCell as M } from "../../../table.js";
import { getPinnedStyle as ce } from "../data-table.service.js";
import { DataTableHeaderComponent as le } from "./data-table-header.js";
import { SkeletonRows as ue } from "./data-table-skeleton-rows.js";
function Re({
  table: u,
  tableContainerRef: i,
  loading: d,
  data: W,
  emptyComponent: j,
  notFoundMessage: V,
  onAddFilter: $,
  maxSortedColumns: U = 3,
  i18n: J,
  isServerSide: p = !1,
  filterBadges: K = [],
  advancedFilterBadge: R,
  searchbarGlobalFilter: Q,
  serverDebounceTime: D = 750,
  rowClassName: x
}) {
  const X = (e) => {
    if (K.some((r) => r.columnId === e)) return !0;
    const o = (r) => r.some((c) => c.columnId === e), n = (r) => r ? r.some((c) => o(c.conditions) ? !0 : c.groups ? n(c.groups) : !1) : !1;
    if (R?.rootGroup) {
      if (o(R.rootGroup.conditions))
        return !0;
      if (R.rootGroup.groups)
        return n(R.rootGroup.groups);
    }
    return !1;
  }, z = ne({}), [k, Y] = G({}), [Z, N] = G(8), [v, _] = G(!1), y = b(() => {
    let e;
    const t = (o) => {
      clearTimeout(e), e = setTimeout(
        () => _(o),
        D
      );
    };
    return t.cancel = () => {
      clearTimeout(e);
    }, t;
  }, [D]), q = b(() => {
    const e = u.getHeaderGroups()[0];
    return e ? e.headers : [];
  }, [u.getHeaderGroups]), g = b(
    () => q.filter((e) => e.column.getIsVisible()),
    [q]
  ), H = b(
    () => g.filter((e) => e.column.columnDef.meta?.pinned === "left"),
    [g]
  ), S = b(
    () => g.filter((e) => e.column.columnDef.meta?.pinned === "right"),
    [g]
  ), ee = b(
    () => g.map((e) => e.id).join("|"),
    [g]
  ), te = u.getState().pagination, T = F(() => {
    const e = i.current;
    if (!e) return 8;
    const t = e.querySelector("thead tr"), o = e.querySelector("tbody tr"), n = t?.getBoundingClientRect().height ?? 40, r = o?.getBoundingClientRect().height ?? 32.5, O = e.offsetHeight - n, w = Math.floor(O / r);
    return w > 1 ? w : 8;
  }, [i]), a = F(
    (e = 0) => {
      const t = {};
      let o = !1;
      if (!i.current) {
        e < 5 && setTimeout(() => a(e + 1), 100);
        return;
      }
      const n = i.current.querySelector(
        "tbody tr:first-child"
      );
      let r = 0;
      for (const w of H) {
        const f = w.column.id;
        t[f] = { side: "left", offset: r };
        let l = 0;
        const m = n?.querySelector(
          `[data-column-id="${f}"]`
        ), h = z.current[f];
        m && m.offsetWidth > 0 ? l = m.getBoundingClientRect().width : h && h.offsetWidth > 0 ? l = h.getBoundingClientRect().width : (o = !0, l = 150), r += l;
      }
      let c = 0;
      for (const w of [...S].reverse()) {
        const f = w.column.id;
        t[f] = { side: "right", offset: c };
        let l = 0;
        const m = n?.querySelector(
          `[data-column-id="${f}"]`
        ), h = z.current[f];
        m && m.offsetWidth > 0 ? l = m.getBoundingClientRect().width : h && h.offsetWidth > 0 ? l = h.getBoundingClientRect().width : (o = !0, l = 150), c += l;
      }
      Y(t);
      const O = T();
      N(O), o && e < 5 && setTimeout(
        () => a(e + 1),
        100 * (e + 1)
      );
    },
    [T, H, S, i]
  );
  return re(() => {
    (() => {
      requestAnimationFrame(() => {
        requestAnimationFrame(() => {
          setTimeout(() => {
            a();
          }, 50);
        });
      });
    })();
  }, [
    H,
    S,
    ee,
    d,
    W,
    te,
    a
  ]), B(() => {
    const e = () => {
      clearTimeout(e.timeout), e.timeout = setTimeout(() => a(), 100);
    };
    window.addEventListener("resize", e);
    const t = i.current;
    let o = null;
    return t && typeof ResizeObserver < "u" && (o = new ResizeObserver(() => {
      a();
    }), o.observe(t)), () => {
      window.removeEventListener("resize", e), clearTimeout(e.timeout), o && o.disconnect();
    };
  }, [a, i]), B(() => {
    if (d) {
      const e = () => {
        const n = T();
        N(n);
      };
      e();
      const t = i.current;
      let o = null;
      return t && typeof ResizeObserver < "u" && (o = new ResizeObserver(() => {
        e();
      }), o.observe(t)), () => {
        o && o.disconnect();
      };
    }
  }, [T, d, i]), B(() => (d ? (_(!0), y.cancel()) : y(!1), () => {
    y.cancel();
  }), [d, y]), /* @__PURE__ */ s(
    "div",
    {
      className: "flex max-h-full min-h-0 max-w-full min-w-0 flex-1",
      ref: i,
      children: /* @__PURE__ */ C(se, { className: "border-d-border h-full min-h-0 w-full rounded-md border", children: [
        /* @__PURE__ */ s(P, { orientation: "horizontal", className: "z-[100]" }),
        /* @__PURE__ */ s(P, { orientation: "vertical" }),
        /* @__PURE__ */ C(ie, { className: "w-full min-w-max", children: [
          /* @__PURE__ */ s(
            le,
            {
              table: u,
              headerRefs: z,
              pinnedOffsets: k,
              maxSortedColumns: U,
              i18n: J,
              searchbarGlobalFilter: Q,
              isColumnFiltered: X,
              onAddFilter: $
            }
          ),
          /* @__PURE__ */ s(
            A,
            {
              "data-table-body": "skeleton",
              className: p && v || !p && d ? "" : "absolute -z-100 h-0 min-h-0 overflow-hidden opacity-0",
              children: (p && v || !p && d) && /* @__PURE__ */ s(
                ue,
                {
                  skeletonRowCount: Z,
                  pinnedOffsets: k,
                  table: u
                }
              )
            }
          ),
          /* @__PURE__ */ s(
            A,
            {
              "data-table-body": "data",
              className: p && v || !p && d ? "absolute -z-100 h-0 min-h-0 overflow-hidden opacity-0" : "",
              children: u.getRowModel().rows?.length ? u.getRowModel().rows.map((e) => /* @__PURE__ */ s(
                I,
                {
                  "data-state": e.getIsSelected() && "selected",
                  className: L(
                    "border-0 shadow-[inset_0_-1px_0_var(--d-border)]",
                    typeof x == "function" ? x(e) : x
                  ),
                  children: e.getVisibleCells().map((t) => {
                    const o = t.column.columnDef.meta?.cellClassName, n = typeof o == "function" ? o(t.getValue(), e.original) : o;
                    return /* @__PURE__ */ s(
                      M,
                      {
                        "data-column-id": t.column.id,
                        className: L(
                          "border-0",
                          t.column.columnDef.meta?.pinned ? "bg-d-background z-[50]" : "shadow-[inset_-1px_0_0_var(--d-border)]",
                          n
                        ),
                        style: ce(
                          t.column.id,
                          k,
                          t.column.columnDef.meta?.pinned,
                          !1
                        ),
                        children: oe(
                          t.column.columnDef.cell,
                          t.getContext()
                        )
                      },
                      t.id
                    );
                  })
                },
                e.id
              )) : /* @__PURE__ */ s(I, { children: /* @__PURE__ */ s(
                M,
                {
                  colSpan: u?.getHeaderGroups()?.[0]?.headers?.length ?? 1,
                  className: "pt-4 text-center",
                  children: j || V
                }
              ) })
            }
          )
        ] })
      ] })
    }
  );
}
export {
  Re as DataTableBody
};
