'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var reactVirtual = require('@tanstack/react-virtual');
var SuggestionItem = require('../SuggestionItem/SuggestionItem.js');
var getHeightAsNumber = require('../../../../utils/getHeightAsNumber.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

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 = React.useRef(null);
  var virtualizer = reactVirtual.useVirtualizer({
    count: items.length,
    getScrollElement: function getScrollElement() {
      return parentRef.current;
    },
    estimateSize: function estimateSize() {
      return 48;
    }
  });
  var virtualItems = virtualizer.getVirtualItems();
  return /*#__PURE__*/React__default.default.createElement("div", {
    ref: parentRef,
    style: {
      height: 'auto',
      maxHeight: getHeightAsNumber.getHeightAsNumber(listMaxHeight),
      overflowY: 'auto'
    },
    onScroll: onScroll
  }, /*#__PURE__*/React__default.default.createElement("div", {
    style: {
      height: virtualizer.getTotalSize(),
      width: '100%',
      position: 'relative'
    }
  }, /*#__PURE__*/React__default.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.default.createElement("div", {
      key: virtualRow.key,
      "data-index": virtualRow.index,
      ref: virtualizer.measureElement
    }, /*#__PURE__*/React__default.default.createElement(SuggestionItem.SuggestionItem, {
      item: items[virtualRow.index],
      onClick: onClick,
      id: "".concat(listId, "/").concat(virtualRow.index),
      focused: false,
      renderItem: renderItem
    }));
  }))));
};

exports.VirtualList = VirtualList;
