'use strict';

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

var React = require('react');
var reactVirtual = require('@tanstack/react-virtual');
var Select_styles = require('../../Select.styles.js');
var Select_tokens = require('../../Select.tokens.js');
var Item = require('./ui/Item/Item.js');
var TreeList_styles = require('./TreeList.styles.js');
var isEmpty = require('../../../../utils/isEmpty.js');
var getHeightAsNumber = require('../../../../utils/getHeightAsNumber.js');

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

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

var TreeList = function TreeList(_ref) {
  var items = _ref.items,
    listMaxHeight = _ref.listMaxHeight,
    onScroll = _ref.onScroll,
    virtual = _ref.virtual,
    beforeList = _ref.beforeList,
    afterList = _ref.afterList,
    emptyStateDescription = _ref.emptyStateDescription;
  if (virtual) {
    return /*#__PURE__*/React__default.default.createElement(VirtualTreeList, {
      items: items,
      listMaxHeight: listMaxHeight,
      onScroll: onScroll,
      beforeList: beforeList,
      afterList: afterList,
      emptyStateDescription: emptyStateDescription
    });
  }
  return /*#__PURE__*/React__default.default.createElement(TreeList_styles.ListWrapper, null, /*#__PURE__*/React__default.default.createElement(TreeList_styles.ScrollContainer, {
    listMaxHeight: listMaxHeight,
    onScroll: onScroll
  }, beforeList, isEmpty.isEmpty(items) ? /*#__PURE__*/React__default.default.createElement(Select_styles.StyledEmptyState, {
    className: Select_tokens.classes.emptyStateWrapper,
    description: emptyStateDescription || 'Ничего не найдено'
  }) : /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, items === null || items === void 0 ? void 0 : items.map(function (item, index) {
    return /*#__PURE__*/React__default.default.createElement(Item.Item, {
      key: index,
      item: item,
      pathToItem: [index]
    });
  })), afterList));
};
var VirtualTreeList = function VirtualTreeList(_ref2) {
  var _virtualItems$0$start, _virtualItems$;
  var _ref2$items = _ref2.items,
    items = _ref2$items === void 0 ? [] : _ref2$items,
    listMaxHeight = _ref2.listMaxHeight,
    onScroll = _ref2.onScroll,
    beforeList = _ref2.beforeList,
    afterList = _ref2.afterList,
    emptyStateDescription = _ref2.emptyStateDescription;
  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(TreeList_styles.ListWrapper, null, beforeList, isEmpty.isEmpty(items) ? /*#__PURE__*/React__default.default.createElement(Select_styles.StyledEmptyState, {
    className: Select_tokens.classes.emptyStateWrapper,
    description: emptyStateDescription || 'Ничего не найдено'
  }) : /*#__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(Item.Item, {
      item: items[virtualRow.index],
      pathToItem: [virtualRow.index]
    }));
  })))), afterList);
};

exports.TreeList = TreeList;
