import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useContext, useRef, } from 'react';
import { Virtuoso } from 'react-virtuoso';
import { ConfigContext } from '../ConfigProvider';
import { ContentState } from '../ContentState';
import { ScrollToTopButton } from '../ScrollToTopButton';
import { useToggle } from '../useToggle';
import { infiniteDataListClassnames, OVERSCAN_COUNT } from './constants';
import { EndData } from './EndData';
import { ErrorMessage } from './ErrorMessage';
import { Loader } from './Loader';
import { NoData } from './NoData';
import { Item } from './styles';
export const InfiniteDataList = ({ data, keyId, className, itemContent, noDataPlaceholder, endOfScrollMsg, errorMsg, isLoading, isError, isEndReached, onRetry, onEndReached, header, }) => {
    const virtuoso = useRef(null);
    const { imagesMap } = useContext(ConfigContext);
    const [isStickyButtonActive, showStickyButton, hideStickyButton] = useToggle();
    const handleRangeChanged = useCallback((range) => {
        if (range.startIndex > 2) {
            showStickyButton();
        }
        else {
            hideStickyButton();
        }
    }, [hideStickyButton, showStickyButton]);
    const handleScrollToTop = useCallback(() => {
        if (header) {
            virtuoso.current?.scrollTo({
                top: 0,
                behavior: 'smooth',
            });
        }
        else {
            virtuoso.current?.scrollToIndex({
                index: 0,
                align: 'start',
                behavior: 'smooth',
            });
        }
    }, [virtuoso]);
    const handleEndReach = useCallback(() => {
        if (!isEndReached && onEndReached) {
            onEndReached();
        }
    }, [isEndReached, onEndReached]);
    const isDataExist = Boolean(data?.length);
    if (!isDataExist && !isLoading && !isError) {
        // biome-ignore lint/complexity/noUselessFragments: Фрагмент необходим по типизации
        return noDataPlaceholder ? _jsx(_Fragment, { children: noDataPlaceholder }) : _jsx(NoData, {});
    }
    return (_jsxs(ContentState, { isLoading: isLoading && !isDataExist, isError: isError && !isDataExist, errorState: {
            imgAlt: 'Что-то пошло не так',
            errorList: [errorMsg || ''],
            imgSrc: imagesMap.defaultErrorImgSrc,
            onRetry,
        }, children: [_jsx(Virtuoso, { className: className, style: { height: '100%' }, data: data, ref: virtuoso, overscan: OVERSCAN_COUNT, endReached: handleEndReach, rangeChanged: handleRangeChanged, itemContent: (index, item) => (_jsx(Item, { children: itemContent?.(item, {
                        index,
                        className: infiniteDataListClassnames.item,
                    }) }, item[keyId])), components: {
                    Header: header ? () => _jsx(_Fragment, { children: header }) : undefined,
                    Footer: () => (_jsxs(_Fragment, { children: [isLoading && _jsx(Loader, {}), isError && _jsx(ErrorMessage, { onRetry: onRetry }), isEndReached && _jsx(EndData, { endOfScrollMsg: endOfScrollMsg })] })),
                } }), isStickyButtonActive && (_jsx(ScrollToTopButton, { onClick: handleScrollToTop }))] }));
};
