import React, { useEffect, useMemo, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { VariableSizeGrid, } from 'react-window';
import { useScrollableContentContext } from '../../providers';
import { VirtualGridRepeaterState, } from './VirtualGridRepeaterState';
import { VirtualGridRepeaterContextProvider } from './VirtualGridRepeaterContext';
function _VirtualGridRepeaterBase(props) {
    const { gridState, renderItem, innerElementType, outerElementType, children, } = props;
    const { scrollableContentRef } = useScrollableContentContext();
    const [state] = useState(() => new VirtualGridRepeaterState({ gridState }));
    const { calcGridProps, rowCount, height, width, itemKeyRecycler: itemKey, } = state;
    const [onFullRenderEnd] = useState(() => state.gridState.toolbar.fedopsReporter.onFullRenderStart?.());
    useEffect(() => {
        if (height && width) {
            onFullRenderEnd?.();
        }
    }, [height && width]);
    const { columnCount, columnWidthIncludingGap, rowHeightIncludingGap, overscanCount, estimatedRowHeight, } = calcGridProps;
    useEffect(() => {
        const container = scrollableContentRef?.current;
        return state.init({
            container,
        });
    }, []);
    const style = useMemo(() => ({
        overflow: 'visible',
        height: '100%',
    }), []);
    const context = useMemo(() => ({ state, renderItem, itemKey }), [renderItem, itemKey]);
    useEffect(() => {
        state.gridRef?.resetAfterIndices({
            columnIndex: 0,
            rowIndex: 0,
            shouldForceUpdate: true,
        });
    }, [height, width, calcGridProps]);
    return (React.createElement(VirtualGridRepeaterContextProvider, { value: context },
        React.createElement(VariableSizeGrid, { ref: (el) => {
                state.gridRef = el;
                state.gridState.gridRef = el;
            }, height: height, width: width, columnCount: columnCount, outerElementType: outerElementType, innerElementType: innerElementType, columnWidth: () => columnWidthIncludingGap(0), rowHeight: (index) => rowHeightIncludingGap({
                index,
                key: itemKey({ rowIndex: index, columnIndex: 0 }),
            }) ?? estimatedRowHeight, estimatedRowHeight: estimatedRowHeight, rowCount: height && width ? rowCount : 0, overscanRowCount: overscanCount, itemKey: itemKey, style: style }, children)));
}
export const VirtualGridRepeaterBase = observer(_VirtualGridRepeaterBase);
//# sourceMappingURL=VirtualGridRepeaterBase.js.map