import { j as i } from "../../_virtual/jsx-runtime.js";
import { useState as I, useEffect as v } from "react";
import { DataTableWrapper as y, ActionBar as C, SelectedItemsInfo as A, ActionButtons as F, ResponsiveTableWrapper as S, Table as D, Footer as E } from "./styles.js";
import { Button as H } from "../Button/index.js";
import { getId as k } from "../../utils/index.js";
import W from "./components/MainHeader.js";
import _ from "./components/TableHeader.js";
import z from "./components/TableBody.js";
import M from "./components/PaginatedFooter.js";
import O from "./components/TableFoot.js";
function U({
  variant: f = "wide",
  header: p,
  actionsBar: m,
  columns: j,
  rows: o,
  footer: a,
  selectableRows: t,
  additionalRows: x,
  "data-testid": u,
  "data-trackid": P
}) {
  const d = k(u), n = k(P), r = a && a.variant === "pagination" && a.pagination, [e, h] = I(
    r && r.currentPage || 1
  );
  v(() => {
    r && r.controlledPagination && h(r.currentPage || 1);
  }, [o]);
  const c = r && r.controlledPagination ? o.slice(
    e * r.rowsPerPage - r.rowsPerPage,
    e * r.rowsPerPage
  ) : o;
  return /* @__PURE__ */ i.jsxs(
    y,
    {
      "data-testid": d(),
      "data-trackid": n(),
      children: [
        p && /* @__PURE__ */ i.jsx(
          W,
          {
            variant: f,
            headerVariant: p.variant || "subtle",
            title: p.title,
            secondaryText: p.secondaryText,
            "data-testid": d("main-header"),
            "data-trackid": n("main-header")
          }
        ),
        m && /* @__PURE__ */ i.jsxs(
          C,
          {
            "data-testid": d("action-bar"),
            "data-trackid": n("action-bar"),
            children: [
              m.text && t && /* @__PURE__ */ i.jsxs(A, { children: [
                t.selectedRows.length,
                " ",
                m.text
              ] }),
              /* @__PURE__ */ i.jsx(F, { children: m.actionButtons.map((g, T) => /* @__PURE__ */ i.jsx(
                H,
                {
                  variant: "inverse-ghost",
                  size: "small-expressive",
                  iconPosition: "trailing",
                  icon: g.icon,
                  disabled: !!g.disabled,
                  onClick: () => g.onClick(),
                  role: "button",
                  children: g.text
                },
                `action_button_${T}`
              )) })
            ]
          }
        ),
        /* @__PURE__ */ i.jsx(S, { children: /* @__PURE__ */ i.jsxs(D, { role: "table", children: [
          /* @__PURE__ */ i.jsx(
            _,
            {
              columns: j,
              rows: o,
              selectAllRows: t == null ? void 0 : t.onSelectAll,
              selectedRows: t == null ? void 0 : t.selectedRows,
              rowIdentifier: t == null ? void 0 : t.rowId,
              "data-testid": d("table-header"),
              "data-trackid": n("table-header")
            }
          ),
          /* @__PURE__ */ i.jsx(
            z,
            {
              filteredRows: c,
              onRowClick: t == null ? void 0 : t.onRowClick,
              selectedRows: t == null ? void 0 : t.selectedRows,
              disabledRows: t == null ? void 0 : t.disabledRows,
              columns: j,
              rowIdentifier: t == null ? void 0 : t.rowId,
              "data-testid": d("table-body"),
              "data-trackid": n("table-body")
            }
          ),
          x && x.length > 0 && /* @__PURE__ */ i.jsx(
            O,
            {
              "data-testid": d("table-foot"),
              "data-trackid": n("table-foot"),
              additionalRows: x
            }
          )
        ] }) }),
        a && /* @__PURE__ */ i.jsxs(
          E,
          {
            "data-testid": d("footer"),
            "data-trackid": n("footer"),
            children: [
              a.variant === "pagination" && /* @__PURE__ */ i.jsx(
                M,
                {
                  footer: a,
                  currentPage: e,
                  setCurrentPage: h,
                  numberOfRows: o.length,
                  variant: f,
                  "data-testid": d("pagination"),
                  "data-trackid": n("pagination")
                }
              ),
              a.variant === "custom" && a.content
            ]
          }
        )
      ]
    }
  );
}
export {
  U as DataTable
};
