import { useEffect, useRef, useState } from 'react';
import { CollectionReachBottom } from '../state/CollectionReachBottom';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import { addEventListener } from '@wix/bex-core/util';
export function useCollectionReachBottom({ collection, containerRef, scrollMoreAlways, }) {
    const targetRef = useRef(null);
    const { lodash } = useWixPatternsContainer();
    const { result: { keyedItems }, } = collection;
    const [collectionReachBottom] = useState(() => new CollectionReachBottom({
        collection,
        lodash,
    }));
    useEffect(() => {
        if (containerRef?.current == null) {
            return;
        }
        collectionReachBottom.reachBottom.container = containerRef?.current;
        collectionReachBottom.reachBottom.target = targetRef?.current;
        const disposers = [
            addEventListener(collection.emitter, 'triggerReachBottomListener', () => {
                collectionReachBottom.triggerListenerIfReached();
            }),
        ];
        collectionReachBottom.subscribe();
        return () => {
            collectionReachBottom.reachBottom.target = null;
            disposers.forEach((d) => d());
            collectionReachBottom.unsubscribe();
        };
    }, []);
    // Check if should load more items (scrollable container is not filled yet) after new data has been rendered
    useEffect(() => {
        // In jsdom element dimensions are always 0, so calculations inside `CollectionReachBottom` are not relevant.
        if (process.env.NODE_ENV === 'test') {
            return;
        }
        if (scrollMoreAlways || containerRef?.current?.scrollHeight) {
            collectionReachBottom.triggerListenerIfReached();
        }
    }, [keyedItems.length]);
    return { containerRef, targetRef };
}
//# sourceMappingURL=useCollectionReachBottom.js.map