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

var VirtualList = function VirtualList(_ref) {
  var _virtualItems$0$start, _virtualItems$;
  var items = _ref.items,
    onClick = _ref.onClick,
    listId = _ref.listId,
    listMaxHeight = _ref.listMaxHeight,
    onScroll = _ref.onScroll,
    renderItem = _ref.renderItem;
  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__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
    }, /*#__PURE__*/React__default.createElement(SuggestionItem, {
      item: items[virtualRow.index],
      onClick: onClick,
      id: "".concat(listId, "/").concat(virtualRow.index),
      focused: false,
      renderItem: renderItem
    }));
  }))));
};

export { VirtualList };
