var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
  if (from && typeof from == "object" || typeof from == "function")
    for (let key of __getOwnPropNames(from))
      !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var Table_exports = {};
__export(Table_exports, {
  DataTable: () => DataTable,
  Table: () => Table,
  usePagination: () => usePagination
});
module.exports = __toCommonJS(Table_exports);
var import_react_table = require("@tanstack/react-table"), import_icons_ui = require("@truststack/icons-ui"), import_react = require("react"), import_tamagui = require("tamagui"), import_Chip = require("./Chip"), import_IconButton = require("./IconButton"), import_Spinner = require("./Spinner"), import_typography = require("./typography"), import_jsx_runtime = require("react/jsx-runtime");
const TableContext = (0, import_tamagui.createStyledContext)({
  density: "0",
  transparent: !1
}), Row = (0, import_tamagui.styled)(import_tamagui.ThemeableStack, {
  context: TableContext,
  flexDirection: "row",
  gap: 12,
  padding: 8,
  variants: {
    density: {
      0: {},
      "-1": {
        padding: 6,
        gap: 8
      },
      "-2": {
        padding: 4,
        gap: 6
      },
      "-3": {
        padding: 2,
        gap: 2
      }
    },
    outline: {
      true: {
        borderBottomWidth: "$border.outline",
        borderBottomColor: "$outlineVariant"
      },
      false: {}
    }
  },
  defaultVariants: {
    outline: !0
  }
}), Cell = (0, import_tamagui.styled)(import_tamagui.ThemeableStack, {
  flexDirection: "row",
  alignItems: "center",
  context: TableContext,
  flexBasis: 1,
  flexGrow: 1,
  minHeight: 40,
  marginLeft: 16,
  marginRight: 16,
  variants: {
    align: {
      right: {
        justifyContent: "flex-end"
      },
      center: {
        justifyContent: "center"
      }
    },
    density: {
      0: {},
      "-1": {
        minHeight: 36,
        marginLeft: 12,
        marginRight: 12
      },
      "-2": {
        minHeight: 32,
        marginLeft: 8,
        marginRight: 8
      },
      "-3": {
        minHeight: 28,
        marginLeft: 4,
        marginRight: 4
      }
    },
    shrink: {
      true: {
        flexGrow: 0,
        flexShrink: 1
      },
      false: {}
    }
  }
}), Text = (0, import_tamagui.styled)(import_typography.Body, {
  name: "TableText",
  numberOfLines: 1,
  adjustsFontSizeToFit: !0
}), HeaderCell = (0, import_tamagui.styled)(import_tamagui.ThemeableStack, {
  flexDirection: "row",
  alignItems: "center",
  context: TableContext,
  flexBasis: 1,
  flexGrow: 1,
  minHeight: 36,
  marginLeft: 16,
  marginRight: 16,
  variants: {
    align: {
      right: {
        justifyContent: "flex-end"
      },
      center: {
        justifyContent: "center"
      }
    },
    density: {
      0: {},
      "-1": {
        minHeight: 32,
        marginLeft: 12,
        marginRight: 12
      },
      "-2": {
        minHeight: 28,
        marginLeft: 8,
        marginRight: 8
      },
      "-3": {
        minHeight: 24,
        marginLeft: 4,
        marginRight: 4
      }
    },
    shrink: {
      true: {
        flexGrow: 0,
        flexShrink: 1
      },
      false: {}
    }
  }
}), HeaderLabel = (0, import_tamagui.styled)(import_typography.Label, {
  name: "HeaderLabel",
  size: "large",
  numberOfLines: 1,
  adjustsFontSizeToFit: !0
}), TableBody = (0, import_tamagui.styled)(import_tamagui.ThemeableStack, {
  flexDirection: "column",
  context: TableContext,
  flexShrink: 1
}), TableHead = (0, import_tamagui.styled)(import_tamagui.ThemeableStack, {
  flexDirection: "column",
  context: TableContext,
  flexShrink: 1,
  variants: {
    transparent: {
      true: {
        backgroundColor: "transparent"
      },
      false: {
        backgroundColor: "$surface"
      }
    }
  }
}), TableComp = (0, import_tamagui.styled)(import_tamagui.ThemeableStack, {
  context: TableContext,
  borderRadius: "$shape.corner_medium",
  overflow: "hidden",
  variants: {
    density: {
      0: {},
      "-1": {},
      "-2": {},
      "-3": {}
    },
    transparent: {
      true: {
        backgroundColor: "transparent"
      },
      false: {
        backgroundColor: "$surface"
      }
    },
    rightFlat: {
      true: {
        borderTopRightRadius: 0,
        borderBottomRightRadius: 0
      },
      false: {}
    },
    leftFlat: {
      true: {
        borderTopLeftRadius: 0,
        borderBottomLeftRadius: 0
      },
      false: {}
    }
  },
  defaultVariants: {
    transparent: !1
  }
}), TableTitle = (0, import_tamagui.styled)(import_typography.Title, {
  name: "TableTitle",
  textAlign: "center"
});
function TableSpinner() {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    Row,
    {
      display: "flex",
      flexDirection: "row",
      justifyContent: "center",
      alignItems: "center",
      p: 12,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Spinner.Spinner, {})
    }
  );
}
function NoResults({
  message = "No results found.",
  ...props
}) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    Row,
    {
      display: "flex",
      flexDirection: "row",
      justifyContent: "center",
      alignItems: "center",
      p: 12,
      ...props,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Text, { children: message })
    }
  );
}
function Pagination({
  onRowsPerPageChange,
  rowsPerPage,
  page,
  onPageChange
}) {
  const { density } = (0, import_react.useContext)(TableContext);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    import_tamagui.XStack,
    {
      gap: 20,
      alignItems: "center",
      justifyContent: "flex-end",
      width: "100%",
      p: density === "0" ? 16 : density === "-1" ? 12 : density === "-2" ? 8 : 4,
      children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Text, { children: [
          "Showing ",
          page * rowsPerPage + 1,
          " - ",
          page * rowsPerPage + rowsPerPage
        ] }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_tamagui.XStack, { gap: 12, children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_Chip.Chip,
            {
              variant: rowsPerPage == 5 ? "filter" : "assist",
              onPress: () => onRowsPerPageChange(5),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Chip.Chip.Text, { children: "5" })
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_Chip.Chip,
            {
              variant: rowsPerPage == 10 ? "filter" : "assist",
              onPress: () => onRowsPerPageChange(10),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Chip.Chip.Text, { children: "10" })
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_Chip.Chip,
            {
              variant: rowsPerPage == 20 ? "filter" : "assist",
              onPress: () => onRowsPerPageChange(20),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Chip.Chip.Text, { children: "20" })
            }
          )
        ] }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_tamagui.XStack, { gap: 12, children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_IconButton.IconButton,
            {
              variant: page == 0 ? "standard" : "filled-tertiary",
              onPress: () => onPageChange(0),
              disabled: page == 0,
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_IconButton.IconButton.Icon, { Icon: import_icons_ui.ChevronFirst })
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_IconButton.IconButton,
            {
              variant: "standard",
              disabled: page == 0,
              onPress: () => onPageChange(page - 1),
              children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_IconButton.IconButton.Icon, { Icon: import_icons_ui.ChevronLeft })
            }
          ),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_IconButton.IconButton, { variant: "standard", onPress: () => onPageChange(page + 1), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_IconButton.IconButton.Icon, { Icon: import_icons_ui.ChevronRight }) }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_IconButton.IconButton, { variant: "standard", disabled: !0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_IconButton.IconButton.Icon, { Icon: import_icons_ui.ChevronLast }) })
        ] })
      ]
    }
  );
}
const TextCell = Cell.styleable(({ children, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Cell, { ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Text, { children }) })), HeaderTextCell = HeaderCell.styleable(({ children, ...props }, ref) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(HeaderCell, { ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(HeaderLabel, { children }) })), Table = (0, import_tamagui.withStaticProperties)(TableComp, {
  props: TableContext.Provider,
  Body: TableBody,
  Cell,
  Head: TableHead,
  HeaderCell,
  HeaderLabel,
  HeaderTextCell,
  NoResults,
  Pagination,
  Row,
  Spinner: TableSpinner,
  Text,
  TextCell,
  Title: TableTitle
});
function DataTable({
  table,
  loading,
  pagination,
  TableHeadProps = {},
  noResultsMessage,
  expandedRow,
  scrollViewHeight,
  ...props
}) {
  const noResults = !table.getRowCount() && !loading, headerGroups = table.getHeaderGroups(), tableRows = table.getRowModel().rows;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Table, { backgroundColor: "$surfaceContainerHigh", ...props, children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.Head, { ...TableHeadProps, children: headerGroups.map((headerGroup) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.Row, { children: headerGroup.headers.map((header, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.Fragment, { children: (0, import_react_table.flexRender)(
      typeof header.column.columnDef.header == "string" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.HeaderTextCell, { children: header.column.columnDef.header }) : header.column.columnDef.header,
      header.getContext()
    ) }, `header-${index}`)) }, headerGroup.id)) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.Body, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      import_tamagui.ScrollView,
      {
        maxHeight: scrollViewHeight,
        showsVerticalScrollIndicator: !0,
        children: [
          loading && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.Spinner, {}),
          noResults && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.NoResults, { message: noResultsMessage }),
          tableRows.map((row) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react.Fragment, { children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.Row, { children: row.getVisibleCells().map((cell, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.Fragment, { children: (0, import_react_table.flexRender)(cell.column.columnDef.cell, cell.getContext()) }, `cell-${index}`)) }, row.id),
            row.getIsExpanded() && expandedRow && expandedRow(row)
          ] }, row.id))
        ]
      }
    ) }),
    pagination && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Table.Pagination, { ...pagination })
  ] });
}
const usePagination = ({ rowsPerPage: defaultRowsPerPage } = {
  rowsPerPage: 10
}) => {
  const [page, setPage] = (0, import_react.useState)(0), [rowsPerPage, setRowsPerPage] = (0, import_react.useState)(defaultRowsPerPage);
  return {
    page,
    setPage,
    rowsPerPage,
    setRowsPerPage,
    offset: page * rowsPerPage,
    limit: rowsPerPage
  };
};
//# sourceMappingURL=Table.js.map
