import React__default, { useRef } from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';
import { getHeightAsNumber } from '../../../../utils/getHeightAsNumber.js';

function VirtualList(_ref) {
  var _virtualItems$0$start, _virtualItems$;
  var items = _ref.items,
    listMaxHeight = _ref.listMaxHeight,
    onScroll = _ref.onScroll,
    _ref$estimateSize = _ref.estimateSize,
    _estimateSize = _ref$estimateSize === void 0 ? 48 : _ref$estimateSize,
    renderItem = _ref.renderItem;
  var parentRef = useRef(null);
  var virtualizer = useVirtualizer({
    count: items.length,
    getScrollElement: function getScrollElement() {
      return parentRef.current;
    },
    estimateSize: function estimateSize() {
      return _estimateSize;
    }
  });
  var virtualItems = virtualizer.getVirtualItems();
  return /*#__PURE__*/React__default.createElement("div", {
    ref: parentRef,
    style: {
      height: 'auto',
      maxHeight: getHeightAsNumber(listMaxHeight),
      overflowY: 'auto'
    },
    onScroll: onScroll
  }, /*#__PURE__*/React__default.createElement("div", {
    style: {
      height: virtualizer.getTotalSize(),
      width: '100%',
      position: 'relative'
    }
  }, /*#__PURE__*/React__default.createElement("div", {
    style: {
      position: 'absolute',
      top: 0,
      left: 0,
      width: '100%',
      transform: "translateY(".concat((_virtualItems$0$start = (_virtualItems$ = virtualItems[0]) === null || _virtualItems$ === void 0 ? void 0 : _virtualItems$.start) !== null && _virtualItems$0$start !== void 0 ? _virtualItems$0$start : 0, "px)")
    }
  }, virtualItems.map(function (virtualRow) {
    return /*#__PURE__*/React__default.createElement("div", {
      key: virtualRow.key,
      "data-index": virtualRow.index,
      ref: virtualizer.measureElement
    }, renderItem(items[virtualRow.index], virtualRow.index));
  }))));
}

export { VirtualList };
