"use strict";
// The require scope
var __webpack_require__ = {};
/************************************************************************/ // webpack/runtime/compat_get_default_export
(()=>{
    // getDefaultExport function for compatibility with non-ESM modules
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
// webpack/runtime/define_property_getters
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
// webpack/runtime/has_own_property
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
// webpack/runtime/make_namespace_object
(()=>{
    // define __esModule on exports
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
/************************************************************************/ var __webpack_exports__ = {};
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
    default: ()=>/* binding */ DataTable_rslib_entry_
});
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const react_namespaceObject = require("@chakra-ui/react");
const icons_namespaceObject = require("@chakra-ui/icons");
const react_table_namespaceObject = require("@tanstack/react-table");
const Empty_js_namespaceObject = require("../Empty/Empty.js");
var Empty_js_default = /*#__PURE__*/ __webpack_require__.n(Empty_js_namespaceObject);
const Pagination_js_namespaceObject = require("../Pagination/Pagination.js");
const Spinner_js_namespaceObject = require("../Spinner/Spinner.js");
var Spinner_js_default = /*#__PURE__*/ __webpack_require__.n(Spinner_js_namespaceObject);
const md_namespaceObject = require("react-icons/md");
const dataTableMediaCSS = {
    '@media (hover: hover)': {
        '.resizer': {
            opacity: '0'
        },
        '*:hover > .resizer': {
            opacity: '1'
        }
    }
};
const dataTableResizerCSS = {
    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({ variant = 'default', 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, emptyAction, isLoading = false, loadingText = 'Loading ...', onRow, rowClassName, getRowCanExpand }) {
    const enableSorting = void 0 !== sorting && false !== sorting;
    const expanderColumns = Boolean(SubComponent) ? [
        {
            id: 'expander',
            enableSorting: false,
            cell: ({ row })=>row.getCanExpand() ? row.getIsExpanded() ? /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.IconButton, {
                    minWidth: 0,
                    onClick: row.getToggleExpandedHandler(),
                    height: "auto",
                    "aria-label": "Expand row",
                    icon: /*#__PURE__*/ external_react_default().createElement(md_namespaceObject.MdKeyboardArrowDown, {
                        size: 20
                    }),
                    variant: "unstyled"
                }) : /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.IconButton, {
                    minWidth: 0,
                    onClick: row.getToggleExpandedHandler(),
                    height: "auto",
                    "aria-label": "Collapse row",
                    icon: /*#__PURE__*/ external_react_default().createElement(md_namespaceObject.MdKeyboardArrowRight, {
                        size: 20
                    }),
                    variant: "unstyled"
                }) : null
        }
    ] : [];
    const selectableColumns = selectable ? [
        {
            id: 'select',
            header: ({ table })=>/*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Checkbox, {
                    isChecked: table.getIsAllRowsSelected(),
                    isIndeterminate: table.getIsSomeRowsSelected(),
                    onChange: table.getToggleAllRowsSelectedHandler()
                }),
            cell: ({ row })=>/*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Checkbox, {
                    isChecked: row.getIsSelected(),
                    isDisabled: !row.getCanSelect(),
                    isIndeterminate: row.getIsSomeSelected(),
                    onChange: row.getToggleSelectedHandler()
                })
        }
    ] : [];
    const [columnVisibility, setColumnVisibility] = (0, external_react_namespaceObject.useState)({});
    const initialState = {
        pagination: {
            pageSize: -1
        }
    };
    if (true === pagination) initialState.pagination = {
        pageSize: defaultPageSize,
        pageIndex: 0
    };
    else if (void 0 !== pagination && false !== pagination) initialState.pagination = pagination;
    if (true === sorting) initialState.sorting = [];
    else if (void 0 !== sorting && false !== sorting) 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: (0, react_table_namespaceObject.getCoreRowModel)(),
        getFilteredRowModel: (0, react_table_namespaceObject.getFilteredRowModel)(),
        getSortedRowModel: (0, react_table_namespaceObject.getSortedRowModel)(),
        getPaginationRowModel: (0, react_table_namespaceObject.getPaginationRowModel)(),
        getFacetedRowModel: (0, react_table_namespaceObject.getFacetedRowModel)(),
        getFacetedUniqueValues: (0, react_table_namespaceObject.getFacetedUniqueValues)(),
        getFacetedMinMaxValues: (0, react_table_namespaceObject.getFacetedMinMaxValues)(),
        //
        debugTable: true,
        getRowCanExpand: getRowCanExpand ? getRowCanExpand : ()=>Boolean(SubComponent)
    };
    if (true !== pagination && void 0 !== pagination && false !== pagination && onPaginationChange) {
        if (tableOptions.state) tableOptions.state.pagination = pagination;
        tableOptions.onPaginationChange = onPaginationChange;
    }
    if (true !== sorting && false !== sorting && void 0 !== sorting && onSortingChange) {
        if (tableOptions.state) tableOptions.state.sorting = sorting;
        tableOptions.onSortingChange = onSortingChange;
    }
    const table = (0, react_table_namespaceObject.useReactTable)(tableOptions);
    const isDataEmpty = 0 === table.getRowModel().rows.length;
    // @ts-ignore
    return /*#__PURE__*/ external_react_default().createElement(external_react_default().Fragment, null, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.TableContainer, null, showColumnSelector && /*#__PURE__*/ external_react_default().createElement(ColumnSelector, {
        table: table
    }), /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Table, {
        size: size,
        variant: variant
    }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Thead, null, table.getHeaderGroups().map((headerGroup)=>/*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tr, {
            key: headerGroup.id
        }, headerGroup.headers.map((header)=>/*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Th, {
                key: header.id,
                colSpan: header.colSpan,
                style: {
                    position: 'relative',
                    width: header.getSize()
                }
            }, header.isPlaceholder ? null : /*#__PURE__*/ external_react_default().createElement("div", {
                className: header.column.getCanSort() ? 'cursor-pointer select-none' : '',
                onClick: header.column.getToggleSortingHandler()
            }, (0, react_table_namespaceObject.flexRender)(header.column.columnDef.header, header.getContext()), enableSorting && header.column.getCanSort() && /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.chakra.span, {
                pl: "4"
            }, {
                asc: /*#__PURE__*/ external_react_default().createElement(icons_namespaceObject.TriangleUpIcon, {
                    "aria-label": "sorted ascending"
                }),
                desc: /*#__PURE__*/ external_react_default().createElement(icons_namespaceObject.TriangleDownIcon, {
                    "aria-label": "sorted descending"
                })
            }[header.column.getIsSorted()] ?? /*#__PURE__*/ external_react_default().createElement(icons_namespaceObject.TriangleUpIcon, {
                "aria-label": "unsorted",
                color: "blackAlpha.300"
            }))), header.column.getCanFilter() ? /*#__PURE__*/ external_react_default().createElement("div", null, /*#__PURE__*/ external_react_default().createElement(ColumnFilter, {
                column: header.column,
                table: table
            })) : null, enableColumnResizing && /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Box, {
                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() ? {
                    ...dataTableResizerCSS,
                    ...isResizingCSS
                } : dataTableResizerCSS
            })))))), /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tbody, null, table.getRowModel().rows.map((row)=>{
        const subEl = (SubComponent && /*#__PURE__*/ external_react_default().createElement(SubComponent, {
            row: row
        })) ?? null;
        return /*#__PURE__*/ external_react_default().createElement(external_react_default().Fragment, {
            key: row.id
        }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tr, {
            className: rowClassName?.(row),
            onClick: expandRowByClick ? ()=>{
                if (row.getCanExpand()) row.toggleExpanded();
            } : ()=>{
                onRow?.(row);
            },
            cursor: expandRowByClick || onRow ? 'pointer' : void 0
        }, row.getVisibleCells().map((cell)=>/*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Td, {
                key: cell.id,
                style: {
                    width: cell.column.getSize()
                },
                "data-testid": "data-table-cell"
            }, (0, react_table_namespaceObject.flexRender)(cell.column.columnDef.cell, cell.getContext())))), row.getIsExpanded() && subEl && /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tr, {
            p: 0
        }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Td, {
            p: 0,
            colSpan: row.getVisibleCells().length
        }, subEl)));
    }), isLoading && isDataEmpty && /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tr, null, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Td, {
        colSpan: table.getVisibleFlatColumns().length
    }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.VStack, {
        my: 10,
        gap: 2
    }, /*#__PURE__*/ external_react_default().createElement(Spinner_js_default(), {
        emptyColor: "gray.200",
        color: "blue.500",
        size: "xl"
    }), /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Text, null, loadingText)))), !isLoading && 0 === data.length && /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tr, null, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Td, {
        colSpan: table.getVisibleFlatColumns().length
    }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.VStack, {
        my: 10
    }, /*#__PURE__*/ external_react_default().createElement(Empty_js_default(), {
        description: emptyText
    }), emptyAction)))), data.length > 0 && ('object' == typeof pagination || pagination && data.length > defaultPageSize) && /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tfoot, null, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Tr, null, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Td, {
        colSpan: table.getVisibleFlatColumns().length
    }, /*#__PURE__*/ external_react_default().createElement(TablePagination, {
        table: table,
        size: size
    })))))));
}
function TablePagination({ table, size, showGoToPage = false }) {
    return /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Flex, {
        justifyContent: "space-between"
    }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Flex, {
        gap: 2
    }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Select, {
        value: table.getState().pagination.pageSize,
        onChange: (e)=>{
            table.setPageSize(Number(e.target.value));
        },
        w: "auto",
        size: size
    }, [
        5,
        10,
        20,
        30,
        40,
        50,
        100
    ].map((pageSize)=>/*#__PURE__*/ external_react_default().createElement("option", {
            key: pageSize,
            value: pageSize
        }, pageSize, " / page"))), showGoToPage && /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Flex, {
        gap: 1,
        alignItems: "center"
    }, /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Text, null, "Go to page:"), /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.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__*/ external_react_default().createElement(Pagination_js_namespaceObject.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__*/ external_react_default().createElement("div", {
        className: "inline-block border border-black shadow rounded"
    }, /*#__PURE__*/ external_react_default().createElement("div", {
        className: "px-1 border-b border-black"
    }, /*#__PURE__*/ external_react_default().createElement("label", null, /*#__PURE__*/ external_react_default().createElement("input", {
        type: 'checkbox',
        checked: table.getIsAllColumnsVisible(),
        onChange: table.getToggleAllColumnsVisibilityHandler()
    }), ' ', "Toggle All")), table.getAllLeafColumns().map((column)=>/*#__PURE__*/ external_react_default().createElement("div", {
            key: column.id,
            className: "px-1"
        }, /*#__PURE__*/ external_react_default().createElement("label", null, /*#__PURE__*/ external_react_default().createElement("input", {
            type: 'checkbox',
            checked: column.getIsVisible(),
            onChange: column.getToggleVisibilityHandler()
        }), ' ', column.id))));
}
function ColumnFilter({ column, table }) {
    const firstValue = table.getPreFilteredRowModel().flatRows[0]?.getValue(column.id);
    const columnFilterValue = column.getFilterValue();
    const sortedUniqueValues = (0, external_react_namespaceObject.useMemo)(()=>'number' == typeof firstValue ? [] : Array.from(column.getFacetedUniqueValues().keys()).sort(), [
        column.getFacetedUniqueValues()
    ]);
    return 'number' == typeof firstValue ? /*#__PURE__*/ external_react_default().createElement("div", null, /*#__PURE__*/ external_react_default().createElement("div", {
        className: "flex space-x-2"
    }, /*#__PURE__*/ external_react_default().createElement(DebouncedInput, {
        type: "number",
        min: Number(column.getFacetedMinMaxValues()?.[0] ?? ''),
        max: Number(column.getFacetedMinMaxValues()?.[1] ?? ''),
        value: columnFilterValue?.[0] ?? '',
        onChange: (value)=>column.setFilterValue((old)=>[
                    value,
                    old?.[1]
                ]),
        placeholder: `Min ${column.getFacetedMinMaxValues()?.[0] ? `(${column.getFacetedMinMaxValues()?.[0]})` : ''}`,
        className: "w-24 border shadow rounded",
        w: 70
    }), /*#__PURE__*/ external_react_default().createElement(DebouncedInput, {
        type: "number",
        min: Number(column.getFacetedMinMaxValues()?.[0] ?? ''),
        max: Number(column.getFacetedMinMaxValues()?.[1] ?? ''),
        value: columnFilterValue?.[1] ?? '',
        onChange: (value)=>column.setFilterValue((old)=>[
                    old?.[0],
                    value
                ]),
        placeholder: `Max ${column.getFacetedMinMaxValues()?.[1] ? `(${column.getFacetedMinMaxValues()?.[1]})` : ''}`,
        className: "w-24 border shadow rounded",
        w: 70
    })), /*#__PURE__*/ external_react_default().createElement("div", {
        className: "h-1"
    })) : /*#__PURE__*/ external_react_default().createElement(external_react_default().Fragment, null, /*#__PURE__*/ external_react_default().createElement("datalist", {
        id: column.id + 'list'
    }, sortedUniqueValues.slice(0, 5000).map((value)=>/*#__PURE__*/ external_react_default().createElement("option", {
            value: value,
            key: value
        }))), /*#__PURE__*/ external_react_default().createElement(DebouncedInput, {
        type: "text",
        value: columnFilterValue ?? '',
        onChange: (value)=>column.setFilterValue(value),
        placeholder: `Search... (${column.getFacetedUniqueValues().size})`,
        className: "w-36 border shadow rounded",
        list: column.id + 'list'
    }), /*#__PURE__*/ external_react_default().createElement("div", {
        className: "h-1"
    }));
}
function DebouncedInput({ value: initialValue, onChange, debounce = 500, ...props }) {
    const [value, setValue] = (0, external_react_namespaceObject.useState)(initialValue);
    (0, external_react_namespaceObject.useEffect)(()=>{
        setValue(initialValue);
    }, [
        initialValue
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const timeout = setTimeout(()=>{
            onChange(value);
        }, debounce);
        return ()=>clearTimeout(timeout);
    }, [
        value
    ]);
    return /*#__PURE__*/ external_react_default().createElement(react_namespaceObject.Input, {
        ...props,
        size: "xs",
        width: "auto",
        value: value,
        onChange: (e)=>setValue(e.target.value)
    });
}
/* ESM default export */ const DataTable_rslib_entry_ = DataTable;
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
