'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var react = require('@chakra-ui/react');
var icons = require('@chakra-ui/icons');
var reactTable = require('@tanstack/react-table');
var fa = require('react-icons/fa');
var Empty = require('../Empty/Empty.cjs');
var Pagination = require('../Pagination/Pagination.cjs');
var Spinner = require('../Spinner/Spinner.cjs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
const dataTableMediaCSS = {
  "@media (hover: hover)": {
    ".resizer": {
      opacity: "0"
    },
    "*:hover > .resizer": {
      opacity: "1"
    }
  }
};
const dataTableResizerCSS = __spreadValues({
  content: "''",
  position: "absolute",
  right: "0",
  top: "0",
  height: "100%",
  width: "5px",
  background: "rgba(0, 0, 0, 0.5)",
  cursor: "col-resize",
  "user-select": "none",
  "touch-action": "none"
}, dataTableMediaCSS);
const isResizingCSS = {
  background: "blue",
  opacity: "1"
};
function DataTable({
  columns = [],
  data = [],
  size = "md",
  showColumnSelector = false,
  enableColumnFilters = false,
  enableColumnResizing = false,
  subComponent: SubComponent,
  expandRowByClick = false,
  selectable = false,
  rowSelection = {},
  onRowSelectionChange,
  defaultPageSize = 50,
  sorting = true,
  onSortingChange,
  pagination = true,
  onPaginationChange,
  emptyText = void 0,
  emptyAction = void 0,
  isLoading = false,
  loadingText = "Loading ...",
  onRow = void 0,
  rowClassName = void 0,
  getRowCanExpand
}) {
  const enableSorting = sorting !== void 0 && sorting !== false;
  const expanderColumns = Boolean(SubComponent) ? [
    {
      id: "expander",
      enableSorting: false,
      cell: ({ row }) => {
        return row.getCanExpand() ? /* @__PURE__ */ jsxRuntime.jsx(
          "button",
          __spreadProps(__spreadValues({}, {
            onClick: row.getToggleExpandedHandler(),
            style: { cursor: "pointer" }
          }), {
            children: row.getIsExpanded() ? /* @__PURE__ */ jsxRuntime.jsx(fa.FaRegMinusSquare, { size: 16 }) : /* @__PURE__ */ jsxRuntime.jsx(fa.FaRegPlusSquare, { size: 16 })
          })
        ) : null;
      }
    }
  ] : [];
  const selectableColumns = selectable ? [
    {
      id: "select",
      header: ({ table: table2 }) => /* @__PURE__ */ jsxRuntime.jsx(
        react.Checkbox,
        __spreadValues({}, {
          isChecked: table2.getIsAllRowsSelected(),
          isIndeterminate: table2.getIsSomeRowsSelected(),
          onChange: table2.getToggleAllRowsSelectedHandler()
        })
      ),
      cell: ({ row }) => /* @__PURE__ */ jsxRuntime.jsx(
        react.Checkbox,
        __spreadValues({}, {
          isChecked: row.getIsSelected(),
          isDisabled: !row.getCanSelect(),
          isIndeterminate: row.getIsSomeSelected(),
          onChange: row.getToggleSelectedHandler()
        })
      )
    }
  ] : [];
  const [columnVisibility, setColumnVisibility] = React.useState({});
  const initialState = {
    pagination: {
      pageSize: -1
    }
  };
  if (pagination === true) {
    initialState.pagination = {
      pageSize: defaultPageSize,
      pageIndex: 0
    };
  } else if (pagination !== void 0 && pagination !== false) {
    initialState.pagination = pagination;
  }
  if (sorting === true) {
    initialState.sorting = [];
  } else if (sorting !== void 0 && sorting !== false) {
    initialState.sorting = sorting;
  }
  const tableOptions = {
    // Data
    data,
    columns: [...expanderColumns, ...selectableColumns, ...columns],
    initialState,
    state: {
      columnVisibility,
      rowSelection
    },
    enableSorting,
    enableColumnFilters,
    enableColumnResizing,
    onColumnVisibilityChange: setColumnVisibility,
    onRowSelectionChange,
    autoResetPageIndex: false,
    // Pipeline
    getCoreRowModel: reactTable.getCoreRowModel(),
    getFilteredRowModel: reactTable.getFilteredRowModel(),
    getSortedRowModel: reactTable.getSortedRowModel(),
    getPaginationRowModel: reactTable.getPaginationRowModel(),
    getFacetedRowModel: reactTable.getFacetedRowModel(),
    getFacetedUniqueValues: reactTable.getFacetedUniqueValues(),
    getFacetedMinMaxValues: reactTable.getFacetedMinMaxValues(),
    //
    debugTable: true,
    getRowCanExpand: getRowCanExpand ? getRowCanExpand : () => Boolean(SubComponent)
  };
  if (pagination !== true && pagination !== void 0 && pagination !== false && onPaginationChange) {
    if (tableOptions.state) {
      tableOptions.state.pagination = pagination;
    }
    tableOptions.onPaginationChange = onPaginationChange;
  }
  if (sorting !== true && sorting !== false && sorting !== void 0 && onSortingChange) {
    if (tableOptions.state) {
      tableOptions.state.sorting = sorting;
    }
    tableOptions.onSortingChange = onSortingChange;
  }
  const table = reactTable.useReactTable(tableOptions);
  const isDataEmpty = table.getRowModel().rows.length === 0;
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: /* @__PURE__ */ jsxRuntime.jsxs(react.TableContainer, { children: [
    showColumnSelector && /* @__PURE__ */ jsxRuntime.jsx(ColumnSelector, { table }),
    /* @__PURE__ */ jsxRuntime.jsxs(react.Table, { size, variant: "simple", children: [
      /* @__PURE__ */ jsxRuntime.jsx(react.Thead, { children: table.getHeaderGroups().map((headerGroup) => /* @__PURE__ */ jsxRuntime.jsx(react.Tr, { children: headerGroup.headers.map((header) => {
        var _a;
        return /* @__PURE__ */ jsxRuntime.jsxs(
          react.Th,
          {
            colSpan: header.colSpan,
            style: {
              position: "relative",
              width: header.getSize()
            },
            children: [
              header.isPlaceholder ? null : /* @__PURE__ */ jsxRuntime.jsxs(
                "div",
                __spreadProps(__spreadValues({}, {
                  className: header.column.getCanSort() ? "cursor-pointer select-none" : "",
                  onClick: header.column.getToggleSortingHandler()
                }), {
                  children: [
                    reactTable.flexRender(header.column.columnDef.header, header.getContext()),
                    enableSorting && header.column.getCanSort() && /* @__PURE__ */ jsxRuntime.jsx(react.chakra.span, { pl: "4", children: (_a = {
                      asc: /* @__PURE__ */ jsxRuntime.jsx(icons.TriangleUpIcon, { "aria-label": "sorted ascending" }),
                      desc: /* @__PURE__ */ jsxRuntime.jsx(icons.TriangleDownIcon, { "aria-label": "sorted descending" })
                    }[header.column.getIsSorted()]) != null ? _a : /* @__PURE__ */ jsxRuntime.jsx(icons.TriangleUpIcon, { "aria-label": "unsorted", color: "blackAlpha.300" }) })
                  ]
                })
              ),
              header.column.getCanFilter() ? /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ColumnFilter, { column: header.column, table }) }) : null,
              enableColumnResizing && /* @__PURE__ */ jsxRuntime.jsx(
                react.Box,
                __spreadProps(__spreadValues({}, {
                  onMouseDown: header.getResizeHandler(),
                  onTouchStart: header.getResizeHandler(),
                  className: `resizer ${header.column.getIsResizing() ? "isResizing" : ""}`,
                  style: {
                    transform: header.column.getIsResizing() ? `translateX(${table.getState().columnSizingInfo.deltaOffset}px)` : ""
                  }
                }), {
                  __css: header.column.getIsResizing() ? __spreadValues(__spreadValues({}, dataTableResizerCSS), isResizingCSS) : dataTableResizerCSS
                })
              )
            ]
          },
          header.id
        );
      }) }, headerGroup.id)) }),
      /* @__PURE__ */ jsxRuntime.jsx(react.Tbody, { children: table.getRowModel().rows.map((row) => {
        var _a;
        const subEl = (_a = SubComponent && /* @__PURE__ */ jsxRuntime.jsx(SubComponent, { row })) != null ? _a : null;
        return /* @__PURE__ */ jsxRuntime.jsxs(React__default["default"].Fragment, { children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            react.Tr,
            {
              className: rowClassName == null ? void 0 : rowClassName(row),
              onClick: expandRowByClick ? () => {
                if (row.getCanExpand()) {
                  row.toggleExpanded();
                }
              } : () => {
                onRow == null ? void 0 : onRow(row);
              },
              cursor: expandRowByClick || onRow ? "pointer" : void 0,
              children: row.getVisibleCells().map((cell) => {
                return /* @__PURE__ */ jsxRuntime.jsx(
                  react.Td,
                  {
                    style: {
                      width: cell.column.getSize()
                    },
                    "data-testid": "data-table-cell",
                    children: reactTable.flexRender(cell.column.columnDef.cell, cell.getContext())
                  },
                  cell.id
                );
              })
            }
          ),
          row.getIsExpanded() && subEl && /* @__PURE__ */ jsxRuntime.jsx(react.Tr, { children: /* @__PURE__ */ jsxRuntime.jsx(react.Td, { colSpan: row.getVisibleCells().length, children: subEl }) })
        ] }, row.id);
      }) })
    ] }),
    isLoading && isDataEmpty && /* @__PURE__ */ jsxRuntime.jsxs(react.VStack, { my: 10, gap: 2, children: [
      /* @__PURE__ */ jsxRuntime.jsx(Spinner, { emptyColor: "gray.200", color: "blue.500", size: "xl" }),
      /* @__PURE__ */ jsxRuntime.jsx(react.Text, { children: loadingText })
    ] }),
    !isLoading && data.length === 0 && /* @__PURE__ */ jsxRuntime.jsxs(react.VStack, { my: 10, children: [
      /* @__PURE__ */ jsxRuntime.jsx(Empty, { description: emptyText }),
      /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: emptyAction })
    ] }),
    data.length > 0 && (typeof pagination === "object" || pagination && data.length > defaultPageSize) && /* @__PURE__ */ jsxRuntime.jsx(TablePagination, { table, size })
  ] }) });
}
function TablePagination({
  table,
  size,
  showGoToPage = false
}) {
  return /* @__PURE__ */ jsxRuntime.jsxs(react.Flex, { my: 4, justifyContent: "space-between", children: [
    /* @__PURE__ */ jsxRuntime.jsxs(react.Flex, { gap: 2, children: [
      /* @__PURE__ */ jsxRuntime.jsx(
        react.Select,
        {
          value: table.getState().pagination.pageSize,
          onChange: (e) => {
            table.setPageSize(Number(e.target.value));
          },
          w: "auto",
          size,
          children: [5, 10, 20, 30, 40, 50, 100].map((pageSize) => /* @__PURE__ */ jsxRuntime.jsxs("option", { value: pageSize, children: [
            pageSize,
            " / page"
          ] }, pageSize))
        }
      ),
      showGoToPage && /* @__PURE__ */ jsxRuntime.jsxs(react.Flex, { gap: 1, alignItems: "center", children: [
        /* @__PURE__ */ jsxRuntime.jsx(react.Text, { children: "Go to page:" }),
        /* @__PURE__ */ jsxRuntime.jsx(
          react.Input,
          {
            type: "number",
            htmlSize: 2,
            w: 100,
            onChange: (e) => {
              if (e.target.valueAsNumber) {
                table.setPageIndex(
                  Math.min(Math.max(0, e.target.valueAsNumber - 1), table.getPageCount() - 1)
                );
              }
            }
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx(
      Pagination.Pagination,
      {
        currentPage: table.getState().pagination.pageIndex + 1,
        totalCount: table.getFilteredRowModel().rows.length,
        pageSize: table.getState().pagination.pageSize,
        onPageChange: (page) => {
          table.setPageIndex(page - 1);
        }
      }
    )
  ] });
}
function ColumnSelector({ table }) {
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "inline-block border border-black shadow rounded", children: [
    /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-1 border-b border-black", children: /* @__PURE__ */ jsxRuntime.jsxs("label", { children: [
      /* @__PURE__ */ jsxRuntime.jsx(
        "input",
        __spreadValues({}, {
          type: "checkbox",
          checked: table.getIsAllColumnsVisible(),
          onChange: table.getToggleAllColumnsVisibilityHandler()
        })
      ),
      " ",
      "Toggle All"
    ] }) }),
    table.getAllLeafColumns().map((column) => {
      return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-1", children: /* @__PURE__ */ jsxRuntime.jsxs("label", { children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          "input",
          __spreadValues({}, {
            type: "checkbox",
            checked: column.getIsVisible(),
            onChange: column.getToggleVisibilityHandler()
          })
        ),
        " ",
        column.id
      ] }) }, column.id);
    })
  ] });
}
function ColumnFilter({ column, table }) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
  const firstValue = (_a = table.getPreFilteredRowModel().flatRows[0]) == null ? void 0 : _a.getValue(column.id);
  const columnFilterValue = column.getFilterValue();
  const sortedUniqueValues = React.useMemo(
    () => typeof firstValue === "number" ? [] : Array.from(column.getFacetedUniqueValues().keys()).sort(),
    [column.getFacetedUniqueValues()]
  );
  return typeof firstValue === "number" ? /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex space-x-2", children: [
      /* @__PURE__ */ jsxRuntime.jsx(
        DebouncedInput,
        {
          type: "number",
          min: Number((_c = (_b = column.getFacetedMinMaxValues()) == null ? void 0 : _b[0]) != null ? _c : ""),
          max: Number((_e = (_d = column.getFacetedMinMaxValues()) == null ? void 0 : _d[1]) != null ? _e : ""),
          value: (_f = columnFilterValue == null ? void 0 : columnFilterValue[0]) != null ? _f : "",
          onChange: (value) => column.setFilterValue((old) => [value, old == null ? void 0 : old[1]]),
          placeholder: `Min ${((_g = column.getFacetedMinMaxValues()) == null ? void 0 : _g[0]) ? `(${(_h = column.getFacetedMinMaxValues()) == null ? void 0 : _h[0]})` : ""}`,
          className: "w-24 border shadow rounded",
          w: 70
        }
      ),
      /* @__PURE__ */ jsxRuntime.jsx(
        DebouncedInput,
        {
          type: "number",
          min: Number((_j = (_i = column.getFacetedMinMaxValues()) == null ? void 0 : _i[0]) != null ? _j : ""),
          max: Number((_l = (_k = column.getFacetedMinMaxValues()) == null ? void 0 : _k[1]) != null ? _l : ""),
          value: (_m = columnFilterValue == null ? void 0 : columnFilterValue[1]) != null ? _m : "",
          onChange: (value) => column.setFilterValue((old) => [old == null ? void 0 : old[0], value]),
          placeholder: `Max ${((_n = column.getFacetedMinMaxValues()) == null ? void 0 : _n[1]) ? `(${(_o = column.getFacetedMinMaxValues()) == null ? void 0 : _o[1]})` : ""}`,
          className: "w-24 border shadow rounded",
          w: 70
        }
      )
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx("div", { className: "h-1" })
  ] }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsx("datalist", { id: column.id + "list", children: sortedUniqueValues.slice(0, 5e3).map((value) => /* @__PURE__ */ jsxRuntime.jsx("option", { value }, value)) }),
    /* @__PURE__ */ jsxRuntime.jsx(
      DebouncedInput,
      {
        type: "text",
        value: columnFilterValue != null ? columnFilterValue : "",
        onChange: (value) => column.setFilterValue(value),
        placeholder: `Search... (${column.getFacetedUniqueValues().size})`,
        className: "w-36 border shadow rounded",
        list: column.id + "list"
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx("div", { className: "h-1" })
  ] });
}
function DebouncedInput(_a) {
  var _b = _a, {
    value: initialValue,
    onChange,
    debounce = 500
  } = _b, props = __objRest(_b, [
    "value",
    "onChange",
    "debounce"
  ]);
  const [value, setValue] = React.useState(initialValue);
  React.useEffect(() => {
    setValue(initialValue);
  }, [initialValue]);
  React.useEffect(() => {
    const timeout = setTimeout(() => {
      onChange(value);
    }, debounce);
    return () => clearTimeout(timeout);
  }, [value]);
  return /* @__PURE__ */ jsxRuntime.jsx(
    react.Input,
    __spreadProps(__spreadValues({}, props), {
      size: "xs",
      width: "auto",
      value,
      onChange: (e) => setValue(e.target.value)
    })
  );
}

module.exports = DataTable;
//# sourceMappingURL=DataTable.cjs.map
