"use client";
'use strict';

var jsxRuntime = require('react/jsx-runtime');
var index$1 = require('../../node_modules/.pnpm/@tanstack_react-table@8.9.2_react-dom@19.1.1_react@19.1.1__react@19.1.1/node_modules/@tanstack/react-table/build/lib/index.cjs');
var React = require('react');
var content = require('./components/content.cjs');
var search = require('./components/search.cjs');
var toolbar = require('./components/toolbar.cjs');
var context = require('./context.cjs');
var dataTable_types = require('./data-table.types.cjs');
var index = require('./utils/index.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]);
    const onDisplaySettingsReset = React.useCallback(() => {
        setTableQuery(prev => ({ ...prev, ...defaultTableQuery }));
        setColumnVisibility(initialColumnVisibility);
    }, [defaultTableQuery, 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.transformToDataTableQuery(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));
    }
    const loadMoreData = React.useCallback(() => {
        if (mode === 'server' && onLoadMore) {
            onLoadMore();
        }
    }, [mode, onLoadMore]);
    const searchQuery = query?.search;
    React.useEffect(() => {
        if (searchQuery) {
            updateTableQuery(prev => ({
                ...prev,
                search: searchQuery
            }));
        }
    }, [searchQuery]);
    const contextValue = React.useMemo(() => {
        return {
            table,
            columns,
            mode,
            isLoading,
            loadMoreData,
            tableQuery,
            updateTableQuery,
            onDisplaySettingsReset,
            defaultSort,
            loadingRowCount,
            onRowClick
        };
    }, [
        table,
        columns,
        mode,
        isLoading,
        loadMoreData,
        tableQuery,
        updateTableQuery,
        onDisplaySettingsReset,
        defaultSort,
        loadingRowCount,
        onRowClick
    ]);
    return (jsxRuntime.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
