"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.VirtualizedListMemo = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var react_1 = require("react");

var React = __importStar(require("react"));

var ReactDOM = __importStar(require("react-dom"));

var classnames_1 = __importDefault(require("classnames"));

var List_1 = require("react-virtualized/dist/commonjs/List");

var useMeasure_1 = __importDefault(require("react-use/lib/useMeasure"));

var createScrollbar_1 = __importDefault(require("../scrollbar/createScrollbar"));

require("./style/virtualized-list.css");

var react_virtualized_1 = require("react-virtualized");

var react_2 = require("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_1["default"](prefixCls, (_a = {}, _a[prefixCls + "-" + type] = type, _a[prefixCls + "-no-border"] = noBorder, _a[prefixCls + "-no-scrollbar"] = !useScrollbar, _a), className);

  var _f = useMeasure_1["default"](),
      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 = react_2.useState(new react_virtualized_1.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_1["default"](itemViewProps.className, itemCls);
    return dynamicRowHeight ? jsx_runtime_1.jsx(react_virtualized_1.CellMeasurer, __assign({
      cache: cache,
      parent: parent,
      rowIndex: index,
      columnIndex: 0
    }, {
      children: jsx_runtime_1.jsx("div", __assign({
        style: style
      }, {
        children: react_1.createElement(ItemView, __assign({}, itemViewProps, {
          key: key,
          index: index,
          item: item,
          style: {},
          className: cls
        }))
      }), void 0)
    }), key) : react_1.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_1["default"](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 = React.useMemo(function () {
    var style = {
      width: width,
      height: height
    };

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

    return style;
  }, [width, height]);
  return jsx_runtime_1.jsx("div", __assign({
    className: wrapperCls,
    style: style,
    ref: ref
  }, {
    children: jsx_runtime_1.jsx(List_1.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';
exports["default"] = VirtualizedList;
var VirtualizedListMemo = React.memo(VirtualizedList);
exports.VirtualizedListMemo = VirtualizedListMemo;
VirtualizedListMemo.displayName = 'VirtualizedListMemo';

