import React, { useRef } from "react";
import { useVirtualizer } from "@tanstack/react-virtual";
import { getHeightAsNumber, isEmpty } from "../../../../../utils";
import { StyledEmptyState } from "../../Combobox.styles";
import { classes } from "../../Combobox.tokens";
import { Item } from "./ui/Item/Item";
import { ListWrapper, ScrollContainer } from "./TreeList.styles";
export var TreeList = function TreeList(param) {
    var items = param.items, listMaxHeight = param.listMaxHeight, onScroll = param.onScroll, virtual = param.virtual, beforeList = param.beforeList, afterList = param.afterList, emptyStateDescription = param.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 === null || items === void 0 ? void 0 : items.map(function(item, index) {
        return /*#__PURE__*/ React.createElement(Item, {
            key: index,
            item: item,
            pathToItem: [
                index
            ]
        });
    })), afterList));
};
var VirtualTreeList = function VirtualTreeList(param) {
    var _param_items = param.items, items = _param_items === void 0 ? [] : _param_items, listMaxHeight = param.listMaxHeight, onScroll = param.onScroll, beforeList = param.beforeList, afterList = param.afterList, emptyStateDescription = param.emptyStateDescription;
    var _ref;
    var _virtualItems_;
    var parentRef = useRef(null);
    var virtualizer = useVirtualizer({
        count: items.length,
        getScrollElement: function getScrollElement() {
            return parentRef.current;
        },
        estimateSize: function estimateSize() {
            return 48;
        }
    });
    var 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(".concat((_ref = (_virtualItems_ = virtualItems[0]) === null || _virtualItems_ === void 0 ? void 0 : _virtualItems_.start) !== null && _ref !== void 0 ? _ref : 0, "px)")
        }
    }, virtualItems.map(function(virtualRow) {
        return /*#__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);
};
