import React, { useMemo } from 'react'
import type { GridFullApi } from '../../api'
import type { Store } from '../../state'
import type { Emitter } from '../../events'
import { GridContext } from './context'

export type GridProviderProps = Store & {
    events: Emitter
    api: GridFullApi
    children: React.ReactNode
}

export const GridProvider = ({
    dispatch,
    subscribe,
    getState,
    selectors,
    events,
    api,
    children,
}: GridProviderProps) => {
    const state = useMemo(
        () => ({
            dispatch,
            subscribe,
            getState,
            selectors,
            events,
            api,
        }),
        [dispatch, subscribe, getState, selectors, events, api]
    )
    return <GridContext.Provider value={state}>{children}</GridContext.Provider>
}
