import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo, useReducer, useRef, } from "react";
import { TableApi } from "../../logic/table-api.js";
import { tableReducer } from "./table-reducer.js";
const TableContext = createContext(null);
const TableProvider = ({ children, config, tableRef, }) => {
    const [state, dispatch] = useReducer((tableReducer), {
        columns: config.columns,
        data: config.data,
        allowRowSelection: config.allowRowSelection ?? true,
        showRowNumbers: config.showRowNumbers ?? true,
        selectedRowIndexes: [],
        lastSelectedRowIndex: null,
        selectedCellIndex: null,
        isCellEditMode: false,
    });
    useEffect(() => {
        dispatch({ type: "SET_DISPATCH", payload: dispatch });
    }, [dispatch]);
    const stateRef = useRef(state);
    const configRef = useRef(config);
    useEffect(() => {
        stateRef.current = state;
        configRef.current = config;
    }, [state, config]);
    const api = useMemo(() => new TableApi(tableRef, configRef, stateRef), []);
    return (_jsx(TableContext.Provider, { value: {
            config,
            state,
            api,
        }, children: children }));
};
const useInternalTableState = () => {
    const context = useContext(TableContext);
    if (!context) {
        throw new Error("useTable must be used within a TableProvider");
    }
    return context;
};
export { TableProvider, useInternalTableState };
//# sourceMappingURL=table-provider.js.map