var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { jsx as _jsx } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import classNames from 'classnames';
import { List } from 'react-virtualized/dist/commonjs/List';
import useMeasure from 'react-use/lib/useMeasure';
import createScrollbar from "../scrollbar/createScrollbar";
import "./style/virtualized-list.css";
import { CellMeasurer, CellMeasurerCache } from 'react-virtualized';
import { useState } from 'react';
var prefixCls = 'dmc-list';
var defaultRowHeight = 36;

var VirtualizedList = function VirtualizedList(props) {
  var _a;

  var _b = props.data,
      data = _b === void 0 ? [] : _b,
      _c = props.rowHeight,
      rowHeight = _c === void 0 ? props.defaultRowHeight || defaultRowHeight : _c,
      rowWidth = props.rowWidth,
      _d = props.itemViewProps,
      itemViewProps = _d === void 0 ? {} : _d,
      type = props.type,
      width = props.width,
      height = props.height,
      maxHeight = props.maxHeight,
      ItemView = props.ItemView,
      className = props.className,
      noBorder = props.noBorder,
      dynamicRowHeight = props.dynamicRowHeight,
      minRowHeight = props.minRowHeight,
      _e = props.useScrollbar,
      useScrollbar = _e === void 0 ? true : _e,
      restProps = __rest(props, ["data", "rowHeight", "rowWidth", "itemViewProps", "type", "width", "height", "maxHeight", "ItemView", "className", "noBorder", "dynamicRowHeight", "minRowHeight", "useScrollbar"]);

  var wrapperCls = classNames(prefixCls, (_a = {}, _a[prefixCls + "-" + type] = type, _a[prefixCls + "-no-border"] = noBorder, _a[prefixCls + "-no-scrollbar"] = !useScrollbar, _a), className);

  var _f = useMeasure(),
      ref = _f[0],
      _g = _f[1],
      eleWidth = _g.width,
      eleHeight = _g.height;

  var itemCls = prefixCls + "-item";
  var psRef = React.useRef();
  var listRef = React.useRef(null);
  var cache = useState(new CellMeasurerCache({
    fixedWidth: true,
    minHeight: minRowHeight
  }))[0];
  var rowRender = React.useCallback(function (_a) {
    var key = _a.key,
        index = _a.index,
        parent = _a.parent,
        style = _a.style;
    var item = data[index];
    var cls = classNames(itemViewProps.className, itemCls);
    return dynamicRowHeight ? _jsx(CellMeasurer, __assign({
      cache: cache,
      parent: parent,
      rowIndex: index,
      columnIndex: 0
    }, {
      children: _jsx("div", __assign({
        style: style
      }, {
        children: /*#__PURE__*/_createElement(ItemView, __assign({}, itemViewProps, {
          key: key,
          index: index,
          item: item,
          style: {},
          className: cls
        }))
      }), void 0)
    }), key) : /*#__PURE__*/_createElement(ItemView, __assign({}, itemViewProps, {
      key: key,
      index: index,
      item: item,
      style: style,
      className: cls
    }));
  }, // eslint-disable-next-line
  [data, ItemView, itemViewProps]);
  React.useEffect(function () {
    if (useScrollbar) {
      var dom = ReactDOM.findDOMNode(listRef.current);

      if (dom) {
        var _a = createScrollbar(dom, type),
            ps = _a[0],
            destroy_1 = _a[1];

        psRef.current = ps;
        return function () {
          destroy_1();
          psRef.current = undefined;
        };
      }
    }
  }, [type, useScrollbar]);
  React.useEffect(function () {
    var _a;

    (_a = psRef.current) === null || _a === void 0 ? void 0 : _a.update();
  });
  React.useEffect(function () {
    var _a;

    if (dynamicRowHeight) {
      cache.clearAll();
      (_a = listRef.current) === null || _a === void 0 ? void 0 : _a.forceUpdateGrid();
    }
  }, [cache, data, dynamicRowHeight]);
  var allHeight = data.length * rowHeight || rowHeight;
  var calcHeight = maxHeight ? Math.min(allHeight, maxHeight) : eleHeight || allHeight;
  var style = {
    width: width,
    height: height
  };

  if (height) {
    style.flex = 'none';
  }

  return _jsx("div", __assign({
    className: wrapperCls,
    style: style,
    ref: ref
  }, {
    children: _jsx(List, __assign({}, restProps, {
      ref: listRef,
      height: calcHeight,
      rowHeight: dynamicRowHeight ? cache.rowHeight : rowHeight,
      rowCount: data.length,
      rowRenderer: rowRender,
      width: rowWidth !== null && rowWidth !== void 0 ? rowWidth : eleWidth
    }), void 0)
  }), void 0);
};

VirtualizedList.defaultProps = {
  data: []
};
VirtualizedList.displayName = 'VirtualizedList';
export default VirtualizedList;
var VirtualizedListMemo = /*#__PURE__*/React.memo(VirtualizedList);
VirtualizedListMemo.displayName = 'VirtualizedListMemo';
export { VirtualizedListMemo };

