import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useContext } from 'react';
import { Virtuoso } from 'react-virtuoso';
import { ConfigContext } from '../ConfigProvider';
import { ContentState } from '../ContentState';
import { MobileSkeleton } from '../DataGrid';
import { dataGridClassnames, EXPANDED_LEVEL_BY_DEFAULT, INITIAL_OPENED_NESTED_CHILDREN_COUNT_BY_DEFAULT, } from '../DataGrid/constants';
import { DataGridContextProvider } from '../DataGrid/DataGridContext';
import { Variant } from '../DataGrid/enums';
import { Head } from '../DataGrid/Head';
import { Row, RowContextProvider } from '../DataGrid/Row';
import { Container } from '../DataGrid/styles';
import { Loader } from '../Loader';
import { classNames } from '../utils/classNames';
import { cva } from '../utils/cva';
import { getInertProps } from '../utils/getInertProps';
import { dataGridInfiniteClassnames, OVERSCAN_COUNT } from './constants';
import { EndData } from './EndData';
import { ErrorMessage } from './ErrorMessage';
import { List } from './List';
import { NoData } from './NoData';
import { Backdrop, DataGridWrapper, DisabledDataGridWrapper, FooterRow, StyledScrollToTopButton, } from './styles';
import { useLogic } from './useLogic';
const INITIAL_LEVEL = 0;
const dataGridInfiniteCva = cva(dataGridClassnames.root, {
    variants: {
        hasGroupedColumns: {
            true: dataGridClassnames.hasGroupedColumns,
        },
        gridViewMode: {
            full: dataGridClassnames.gridViewFull,
            standard: dataGridClassnames.gridViewStandard,
        },
    },
});
export const DataGridInfinite = (props) => {
    const { imagesMap, emptySymbol } = useContext(ConfigContext);
    const { isNoData, isDataGridDisabled, treeRenderConfig, virtuosoProps, headProps, rowProps, scrollToTopButtonProps, isEndReached, isHideHead, containerCssVars, isMobile, mobileSkeletonProps, flattenColumns, hasGroupedColumns, gridViewMode, } = useLogic(props);
    const { columns, rows = [], selectedRows = [], sorting, isLoading, isDisabled, keyId, emptyCellValue = emptySymbol, variant = Variant.Tree, subrows, className, isError, endOfScrollMsg, errorMsg, noDataPlaceholder, onRowClick, onSort, onRetry, headPreAddon, } = props;
    const { moreButtonColumnPosition = 1, isVisibleCollapseButton = true } = subrows || {};
    const { isInitialExpanded = false, expandedLevel = EXPANDED_LEVEL_BY_DEFAULT, initialVisibleChildrenCount = INITIAL_OPENED_NESTED_CHILDREN_COUNT_BY_DEFAULT, } = treeRenderConfig || {};
    const TableContainer = isDisabled ? DisabledDataGridWrapper : DataGridWrapper;
    return (_jsx(DataGridContextProvider, { children: _jsxs(Container, { style: containerCssVars, className: classNames(dataGridInfiniteCva({ hasGroupedColumns, gridViewMode }), className), children: [headPreAddon, _jsxs(TableContainer, { ...getInertProps(isDataGridDisabled), children: [!isHideHead && (_jsx(Head, { ...headProps, onSort: onSort, sorting: sorting, columns: columns })), _jsx(ContentState, { isLoading: isLoading && !isNoData, loadingTitle: "\u0417\u0430\u0433\u0440\u0443\u0437\u043A\u0430 \u0434\u0430\u043D\u043D\u044B\u0445", isError: isError && !isNoData, loadingContent: isMobile && _jsx(MobileSkeleton, { ...mobileSkeletonProps }), errorState: {
                                imgAlt: 'Что-то пошло не так',
                                errorList: [errorMsg || ''],
                                imgSrc: imagesMap.defaultErrorImgSrc,
                                onRetry,
                            }, children: _jsx(Virtuoso, { ...virtuosoProps, style: { height: '100%' }, overscan: OVERSCAN_COUNT, data: rows, components: {
                                    // biome-ignore lint/suspicious/noTsIgnore: Требует HTMLDivElement, а для элемента списка используем HTMLUListElement (https://github.com/petyosi/react-virtuoso/issues/864)
                                    // @ts-ignore
                                    List,
                                    Item: ({ children, item, ...itemProps }) => {
                                        const { children: nestedChildren, options, ...row } = item;
                                        return (_jsx(RowContextProvider, { children: _jsx(Row, { ...rowProps, ...itemProps, row: row, columns: flattenColumns, selectedRows: selectedRows, options: options, keyId: keyId, variant: variant, level: INITIAL_LEVEL, nestedChildren: nestedChildren, isInitialExpanded: isInitialExpanded, expandedLevel: expandedLevel, initialVisibleChildrenCount: initialVisibleChildrenCount, moreButtonColumnPosition: moreButtonColumnPosition, isVisibleCollapseButton: isVisibleCollapseButton, emptyCellValue: emptyCellValue, onRowClick: onRowClick }) }));
                                    },
                                    EmptyPlaceholder: () => _jsx(_Fragment, { children: noDataPlaceholder || _jsx(NoData, {}) }),
                                    Footer: () => {
                                        if (!isLoading && !isError && !isEndReached) {
                                            return null;
                                        }
                                        return (_jsxs(FooterRow, { children: [isLoading && _jsx(Loader, {}), isError && _jsx(ErrorMessage, { onRetry: onRetry }), isEndReached && (_jsx(EndData, { endOfScrollMsg: endOfScrollMsg }))] }));
                                    },
                                } }) }), _jsx(StyledScrollToTopButton, { ...scrollToTopButtonProps, className: dataGridInfiniteClassnames.scrollToTopButton }), isDisabled && _jsx(Backdrop, {})] })] }) }));
};
