import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback, useMemo, useState } from 'react';
import { DataGridContext } from '../DataGridContext';
const ROW_FLAGS_BY_DEFAULT = {
    isOpenedItems: true,
    iOpenedMoreItems: false,
};
export const DataGridContextProvider = ({ isLoading, children, }) => {
    const [openedItems, setOpenedItems] = useState({});
    const checkIsOpened = useCallback((key) => {
        if (openedItems[key]) {
            return true;
        }
        return false;
    }, [openedItems]);
    const checkIsMoreOpened = useCallback((key) => {
        if (openedItems[key]) {
            return openedItems[key].iOpenedMoreItems;
        }
        return false;
    }, [openedItems]);
    const toggleOpenItems = useCallback((key) => setOpenedItems((currentOpenedItems) => {
        if (checkIsOpened(key)) {
            const newOpenedItems = { ...currentOpenedItems };
            delete newOpenedItems[key];
            return newOpenedItems;
        }
        return { ...currentOpenedItems, [key]: ROW_FLAGS_BY_DEFAULT };
    }), [openedItems]);
    const toggleOpenMoreItems = useCallback((key) => setOpenedItems((currentOpenedItems) => {
        if (checkIsMoreOpened(key)) {
            const newOpenedItems = { ...currentOpenedItems };
            newOpenedItems[key] = ROW_FLAGS_BY_DEFAULT;
            return newOpenedItems;
        }
        return {
            ...currentOpenedItems,
            [key]: { ...ROW_FLAGS_BY_DEFAULT, iOpenedMoreItems: true },
        };
    }), [openedItems]);
    const contextValue = useMemo(() => ({
        checkIsOpened,
        checkIsMoreOpened,
        toggleOpenItems,
        toggleOpenMoreItems,
        isLoading,
    }), [
        checkIsOpened,
        checkIsMoreOpened,
        toggleOpenItems,
        toggleOpenMoreItems,
        isLoading,
        openedItems,
    ]);
    return (_jsx(DataGridContext.Provider, { value: contextValue, children: children }));
};
