import { j as jsxRuntimeExports } from '../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.js';
import { useState, useRef, useMemo, useEffect } from 'react';
import { TableContext } from './context.js';
import { defaultGroupOption } from './data-table.types.js';
import { useReactTable } from '../../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.js';
import { getDefaultTableQuery, getInitialColumnVisibility, queryToTableState, getColumnsWithFilterFn, groupData, hasQueryChanged, sanitizeTableQuery } from './utils/index.js';
import { Content } from './components/content.js';
import { Toolbar } from './components/toolbar.js';
import { TableSearch } from './components/search.js';
import { getCoreRowModel, getExpandedRowModel, getSortedRowModel, getFilteredRowModel } from '../../node_modules/.pnpm/@tanstack_table-core@8.9.2/node_modules/@tanstack/table-core/build/lib/index.js';

function DataTableRoot({ data = [], columns, query, mode = "client", isLoading = false, loadingRowCount = 3, defaultSort, children, onTableQueryChange, onLoadMore, onRowClick, }) {
    const defaultTableQuery = getDefaultTableQuery(defaultSort, query);
    const initialColumnVisibility = getInitialColumnVisibility(columns);
    const [columnVisibility, setColumnVisibility] = useState(initialColumnVisibility);
    const [tableQuery, setTableQuery] = useState(defaultTableQuery);
    const oldQueryRef = useRef(null);
    const reactTableState = useMemo(() => queryToTableState(tableQuery), [tableQuery]);
    function onDisplaySettingsReset() {
        setTableQuery((prev) => ({ ...prev, ...defaultTableQuery }));
        setColumnVisibility(initialColumnVisibility);
    }
    const group_by = tableQuery.group_by?.[0];
    const columnsWithFilters = useMemo(() => getColumnsWithFilterFn(columns, tableQuery.filters), [columns, tableQuery.filters]);
    const groupedData = useMemo(() => groupData(data, group_by, columns), [data, group_by, columns]);
    useEffect(() => {
        if (tableQuery &&
            onTableQueryChange &&
            hasQueryChanged(oldQueryRef.current, tableQuery) &&
            mode === "server") {
            onTableQueryChange(sanitizeTableQuery(tableQuery));
            oldQueryRef.current = tableQuery;
        }
    }, [tableQuery, onTableQueryChange]);
    const table = useReactTable({
        data: groupedData,
        columns: columnsWithFilters,
        getCoreRowModel: getCoreRowModel(),
        getExpandedRowModel: getExpandedRowModel(),
        getSubRows: (row) => row?.subRows || [],
        getSortedRowModel: mode === "server" ? undefined : getSortedRowModel(),
        getFilteredRowModel: mode === "server" ? undefined : 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 !== 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 (jsxRuntimeExports.jsx(TableContext.Provider, { value: contextValue, children: children }));
}
const DataTable = Object.assign(DataTableRoot, {
    Content: Content,
    Toolbar: Toolbar,
    Search: TableSearch,
});

export { DataTable };
//# sourceMappingURL=data-table.js.map
