'use strict';

var jsxRuntime = require('../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.cjs');
var React = require('react');
var context = require('./context.cjs');
var dataTable_types = require('./data-table.types.cjs');
var index$1 = require('../../node_modules/.pnpm/@tanstack_react-table@8.9.2_react-dom@18.3.1_react@18.2.0__react@18.2.0/node_modules/@tanstack/react-table/build/lib/index.cjs');
var index = require('./utils/index.cjs');
var content = require('./components/content.cjs');
var toolbar = require('./components/toolbar.cjs');
var search = require('./components/search.cjs');
var index$2 = require('../../node_modules/.pnpm/@tanstack_table-core@8.9.2/node_modules/@tanstack/table-core/build/lib/index.cjs');

function DataTableRoot({ data = [], columns, query, mode = "client", isLoading = false, loadingRowCount = 3, defaultSort, children, onTableQueryChange, onLoadMore, onRowClick, }) {
    const defaultTableQuery = index.getDefaultTableQuery(defaultSort, query);
    const initialColumnVisibility = index.getInitialColumnVisibility(columns);
    const [columnVisibility, setColumnVisibility] = React.useState(initialColumnVisibility);
    const [tableQuery, setTableQuery] = React.useState(defaultTableQuery);
    const oldQueryRef = React.useRef(null);
    const reactTableState = React.useMemo(() => index.queryToTableState(tableQuery), [tableQuery]);
    function onDisplaySettingsReset() {
        setTableQuery((prev) => ({ ...prev, ...defaultTableQuery }));
        setColumnVisibility(initialColumnVisibility);
    }
    const group_by = tableQuery.group_by?.[0];
    const columnsWithFilters = React.useMemo(() => index.getColumnsWithFilterFn(columns, tableQuery.filters), [columns, tableQuery.filters]);
    const groupedData = React.useMemo(() => index.groupData(data, group_by, columns), [data, group_by, columns]);
    React.useEffect(() => {
        if (tableQuery &&
            onTableQueryChange &&
            index.hasQueryChanged(oldQueryRef.current, tableQuery) &&
            mode === "server") {
            onTableQueryChange(index.sanitizeTableQuery(tableQuery));
            oldQueryRef.current = tableQuery;
        }
    }, [tableQuery, onTableQueryChange]);
    const table = index$1.useReactTable({
        data: groupedData,
        columns: columnsWithFilters,
        getCoreRowModel: index$2.getCoreRowModel(),
        getExpandedRowModel: index$2.getExpandedRowModel(),
        getSubRows: (row) => row?.subRows || [],
        getSortedRowModel: mode === "server" ? undefined : index$2.getSortedRowModel(),
        getFilteredRowModel: mode === "server" ? undefined : index$2.getFilteredRowModel(),
        manualSorting: mode === "server",
        manualFiltering: mode === "server",
        onColumnVisibilityChange: setColumnVisibility,
        globalFilterFn: mode === "server" ? undefined : "auto",
        initialState: {
            columnVisibility: initialColumnVisibility,
        },
        filterFromLeafRows: true,
        state: {
            ...reactTableState,
            columnVisibility: columnVisibility,
            expanded: group_by && group_by !== dataTable_types.defaultGroupOption.id ? true : undefined,
        },
    });
    function updateTableQuery(fn) {
        setTableQuery((prev) => fn(prev));
    }
    function loadMoreData() {
        if (mode === "server" && onLoadMore) {
            onLoadMore();
        }
    }
    const contextValue = {
        table,
        columns,
        mode,
        isLoading,
        loadMoreData,
        tableQuery,
        updateTableQuery,
        onDisplaySettingsReset,
        defaultSort,
        loadingRowCount,
        onRowClick,
    };
    return (jsxRuntime.jsxRuntimeExports.jsx(context.TableContext.Provider, { value: contextValue, children: children }));
}
const DataTable = Object.assign(DataTableRoot, {
    Content: content.Content,
    Toolbar: toolbar.Toolbar,
    Search: search.TableSearch,
});

exports.DataTable = DataTable;
//# sourceMappingURL=data-table.cjs.map
