import React, { useRef } from "react";
import { useVirtualizer } from "@tanstack/react-virtual";
import { getHeightAsNumber, isEmpty } from "../../../../utils";
import { StyledEmptyState } from "../../Select.styles";
import { classes } from "../../Select.tokens";
import { Item } from "./ui/Item/Item";
import { ListWrapper, ScrollContainer } from "./TreeList.styles";
export const TreeList = ({ items, listMaxHeight, onScroll, virtual, beforeList, afterList, emptyStateDescription })=>{
    if (virtual) {
        return /*#__PURE__*/ React.createElement(VirtualTreeList, {
            items: items,
            listMaxHeight: listMaxHeight,
            onScroll: onScroll,
            beforeList: beforeList,
            afterList: afterList,
            emptyStateDescription: emptyStateDescription
        });
    }
    return /*#__PURE__*/ React.createElement(ListWrapper, null, /*#__PURE__*/ React.createElement(ScrollContainer, {
        listMaxHeight: listMaxHeight,
        onScroll: onScroll
    }, beforeList, isEmpty(items) ? /*#__PURE__*/ React.createElement(StyledEmptyState, {
        className: classes.emptyStateWrapper,
        description: emptyStateDescription || 'Ничего не найдено'
    }) : /*#__PURE__*/ React.createElement(React.Fragment, null, items?.map((item, index)=>{
        return /*#__PURE__*/ React.createElement(Item, {
            key: index,
            item: item,
            pathToItem: [
                index
            ]
        });
    })), afterList));
};
const VirtualTreeList = ({ items = [], listMaxHeight, onScroll, beforeList, afterList, emptyStateDescription })=>{
    const parentRef = useRef(null);
    const virtualizer = useVirtualizer({
        count: items.length,
        getScrollElement: ()=>parentRef.current,
        estimateSize: ()=>48
    });
    const virtualItems = virtualizer.getVirtualItems();
    return /*#__PURE__*/ React.createElement(ListWrapper, null, beforeList, isEmpty(items) ? /*#__PURE__*/ React.createElement(StyledEmptyState, {
        className: classes.emptyStateWrapper,
        description: emptyStateDescription || 'Ничего не найдено'
    }) : /*#__PURE__*/ React.createElement("div", {
        ref: parentRef,
        style: {
            height: 'auto',
            maxHeight: getHeightAsNumber(listMaxHeight),
            overflowY: 'auto'
        },
        onScroll: onScroll
    }, /*#__PURE__*/ React.createElement("div", {
        style: {
            height: virtualizer.getTotalSize(),
            width: '100%',
            position: 'relative'
        }
    }, /*#__PURE__*/ React.createElement("div", {
        style: {
            position: 'absolute',
            top: 0,
            left: 0,
            width: '100%',
            transform: `translateY(${virtualItems[0]?.start ?? 0}px)`
        }
    }, virtualItems.map((virtualRow)=>/*#__PURE__*/ React.createElement("div", {
            key: virtualRow.key,
            "data-index": virtualRow.index,
            ref: virtualizer.measureElement
        }, /*#__PURE__*/ React.createElement(Item, {
            item: items[virtualRow.index],
            pathToItem: [
                virtualRow.index
            ]
        })))))), afterList);
};
