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;
};

var __spreadArray = this && this.__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

import { createElement as _createElement } from "react";
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import { Virtuoso } from 'react-virtuoso';
import createScrollbar from "../scrollbar/createScrollbar";
import useMeasure from 'react-use/lib/useMeasure';
import { randomUuid } from "../_utils/stringUtil";
import "./style/group-virtualized-list.css";
var prefixCls = 'dmc-group-list';

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

  var data = props.data,
      scrollToIndex = props.scrollToIndex,
      GroupView = props.GroupView,
      ItemView = props.ItemView,
      defaultHeight = props.defaultHeight,
      type = props.type,
      className = props.className,
      style = props.style,
      width = props.width,
      height = props.height,
      noBorder = props.noBorder,
      noRowRender = props.noRowRender,
      overscan = props.overscan,
      onLoadMore = props.onLoadMore,
      others = __rest(props, ["data", "scrollToIndex", "GroupView", "ItemView", "defaultHeight", "type", "className", "style", "width", "height", "noBorder", "noRowRender", "overscan", "onLoadMore"]);

  var wrapperCls = classNames(prefixCls + "-wrapper", (_a = {}, _a[prefixCls + "-" + type] = type, _a[prefixCls + "-no-border"] = noBorder, _a), className);
  var count = React.useMemo(function () {
    var c = data.length;
    data.forEach(function (d) {
      c += d.items.length;
    });
    return c;
  }, [data]);
  var dataList = React.useMemo(function () {
    var list = [];
    data.forEach(function (groupItem) {
      list.push(__assign({
        type: 'group'
      }, groupItem));

      if (groupItem.items) {
        list = list.concat(__spreadArray([], groupItem.items));
      }
    });
    return list;
  }, [data]);
  var itemRender = React.useCallback(function (index) {
    var item = dataList[index];

    if (item.type === 'group') {
      return _jsx(GroupView, {
        item: item,
        className: prefixCls + "-group"
      }, void 0);
    } else {
      return _jsx(ItemView, {
        index: index,
        item: item,
        className: prefixCls + "-group-item"
      }, void 0);
    }
  }, [GroupView, ItemView, dataList]);

  var _b = useMeasure(),
      containerRef = _b[0],
      _c = _b[1],
      boxWidth = _c.width,
      boxHeight = _c.height;

  var psRef = React.useRef();
  var uuid = React.useMemo(function () {
    return randomUuid();
  }, []);
  React.useEffect(function () {
    var selector = "." + prefixCls + "-list." + prefixCls + "-list-" + uuid;
    var dom = document.querySelector(selector);

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

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

    (_a = psRef.current) === null || _a === void 0 ? void 0 : _a.update();
  });
  var wrapperStyle = React.useMemo(function () {
    return __assign(__assign({}, style), {
      width: width,
      height: height
    });
  }, [height, style, width]);
  var vStyle = React.useMemo(function () {
    return {
      width: boxWidth,
      height: boxHeight
    };
  }, [boxHeight, boxWidth]);
  return _jsx("div", __assign({
    ref: containerRef,
    style: wrapperStyle,
    className: wrapperCls
  }, {
    children: data.length > 0 ? /*#__PURE__*/_createElement(Virtuoso, __assign({}, others, {
      key: uuid,
      className: prefixCls + "-list " + prefixCls + "-list-" + uuid,
      style: vStyle,
      totalCount: count,
      item: itemRender,
      initialTopMostItemIndex: scrollToIndex,
      overscan: overscan || 200,
      defaultItemHeight: defaultHeight,
      endReached: onLoadMore
    })) : _jsx(_Fragment, {
      children: noRowRender
    }, void 0)
  }), void 0);
};

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

