"use strict";
const React = require("react");
const throttle = require("../../../../../../lodash@4.17.21/node_modules/lodash/throttle.js");
const pagination = require("../Pagination/pagination.js");
const index$1 = require("../Spin/index.js");
const item = require("./item.js");
const classNames = require("../_util/classNames.js");
const row = require("../Grid/row.js");
const col = require("../Grid/col.js");
const index = require("../ConfigProvider/index.js");
const omit = require("../_util/omit.js");
const index$2 = require("../_class/VirtualList/index.js");
const scrollIntoView = require("../_util/scrollIntoView.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const pick = require("../_util/pick.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__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 = globalThis && globalThis.__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 __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var DEFAULT_PAGE_SIZE = 10;
var DEFAULT_PAGE_CURRENT = 1;
var SizeList = ["small", "default", "large"];
var defaultProps = {
  split: true,
  bordered: true,
  defaultCurrent: 1,
  offsetBottom: 0,
  throttleDelay: 500
};
function List(baseProps, ref) {
  var _a = React.useContext(index.ConfigContext), getPrefixCls = _a.getPrefixCls, loadingElement = _a.loadingElement, ctxSize = _a.size, renderEmpty = _a.renderEmpty, componentConfig = _a.componentConfig, rtl = _a.rtl;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.List);
  var style = props.style, wrapperStyle = props.wrapperStyle, className = props.className, wrapperClassName = props.wrapperClassName, _b = props.children, children = _b === void 0 ? [] : _b, _c = props.dataSource, dataSource = _c === void 0 ? [] : _c, propSize = props.size, footer = props.footer, header = props.header, pagination$1 = props.pagination, bordered = props.bordered, split = props.split, render = props.render, grid = props.grid, loading = props.loading, hoverable = props.hoverable, scrollLoading = props.scrollLoading, paginationInFooter = props.paginationInFooter, offsetBottom = props.offsetBottom, throttleDelay = props.throttleDelay, defaultCurrent = props.defaultCurrent, noDataElement = props.noDataElement, listRef = props.listRef, onReachBottom = props.onReachBottom, onListScroll = props.onListScroll;
  var size = propSize || (SizeList.indexOf(ctxSize) > -1 ? ctxSize : "default");
  var prefixCls = getPrefixCls("list");
  var refDom = React.useRef(null);
  var refVirtualList = React.useRef(null);
  var refScrollElement = React.useRef(null);
  var refItemListWrapper = React.useRef(null);
  var refCanTriggerReachBottom = React.useRef(true);
  var _d = __read(React.useState(pagination$1 && typeof pagination$1 === "object" ? pagination$1.pageSize || pagination$1.defaultPageSize || DEFAULT_PAGE_SIZE : DEFAULT_PAGE_SIZE), 2), pageSize = _d[0], setPageSize = _d[1];
  var _e = __read(React.useState(pagination$1 && typeof pagination$1 === "object" ? pagination$1.current || pagination$1.defaultCurrent || DEFAULT_PAGE_CURRENT : DEFAULT_PAGE_CURRENT), 2), paginationCurrent = _e[0], setPaginationCurrent = _e[1];
  var _f = __read(React.useState(defaultCurrent), 2), pageCurrentForScroll = _f[0], setPageCurrentForScroll = _f[1];
  var childrenCount = React__default.default.Children.count(children);
  React.useImperativeHandle(listRef, function() {
    return {
      dom: refDom.current,
      scrollIntoView: function(index2, options) {
        if (refVirtualList.current) {
          refVirtualList.current.scrollTo({
            index: index2,
            options
          });
        } else if (refItemListWrapper.current) {
          var node = refItemListWrapper.current.children[index2];
          node && scrollIntoView(node, __assign({
            boundary: refScrollElement.current
          }, options));
        }
      }
    };
  });
  var virtualListProps = props.virtualListProps ? props.virtualListProps : props.height ? {
    height: props.height
  } : void 0;
  var paginationProps = __assign(__assign({
    pageSize,
    current: paginationCurrent,
    total: dataSource.length > 0 ? dataSource.length : childrenCount
  }, typeof pagination$1 === "object" ? pagination$1 : {}), {
    onPageSizeChange: function(size2, current) {
      setPageSize(size2);
      pagination$1 && typeof pagination$1 === "object" && pagination$1.onPageSizeChange && pagination$1.onPageSizeChange(size2, current);
    },
    onChange: function(pageNumber, pageSize2) {
      setPaginationCurrent(pageNumber);
      pagination$1 && typeof pagination$1 === "object" && pagination$1.onChange && pagination$1.onChange(pageNumber, pageSize2);
    }
  });
  paginationProps.current = Math.min(paginationProps.current, Math.ceil(paginationProps.total / paginationProps.pageSize));
  var needHandleScroll = !!(onListScroll || onReachBottom);
  var throttledScrollHandler = React.useCallback(throttle(function() {
    if (onListScroll) {
      onListScroll(refScrollElement.current);
      return;
    }
    var _a2 = refScrollElement.current, scrollTop = _a2.scrollTop, scrollHeight = _a2.scrollHeight, clientHeight = _a2.clientHeight;
    var scrollBottom = scrollHeight - (scrollTop + clientHeight);
    if (Math.abs(scrollBottom) < offsetBottom + 1) {
      if (refCanTriggerReachBottom.current) {
        setPageCurrentForScroll(pageCurrentForScroll + 1);
        onReachBottom && onReachBottom(pageCurrentForScroll + 1);
        refCanTriggerReachBottom.current = false;
      }
    } else {
      refCanTriggerReachBottom.current = true;
    }
  }, throttleDelay), [throttleDelay, pageCurrentForScroll, onListScroll, onReachBottom]);
  var renderListItems = function() {
    var getCurrentPageItems = function(items) {
      var current = paginationProps.current, pageSize2 = paginationProps.pageSize;
      var startIndex = (current - 1) * pageSize2;
      return pagination$1 && items.length > startIndex ? items.slice(startIndex, startIndex + pageSize2) : items;
    };
    var getItems = function(originItems, render2) {
      var currentPageItems = getCurrentPageItems(originItems);
      return render2 ? currentPageItems.map(render2) : currentPageItems;
    };
    var getGrid = function(originItems, render2) {
      var currentPageItems = getCurrentPageItems(originItems);
      if (grid.column || grid.span) {
        var items = [];
        var gutter = grid.gutter, justify = grid.justify, align = grid.align, gridRowSize = grid.column, colProps_1 = __rest(grid, ["gutter", "justify", "align", "column"]);
        var rowSize = gridRowSize || Math.floor(24 / grid.span);
        var span_1 = colProps_1.span || Math.floor(24 / rowSize);
        var startNum_1 = 0;
        var _loop_1 = function() {
          var nextStartNum = startNum_1 + rowSize;
          var currentRow = ~~(startNum_1 / rowSize);
          items.push(/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(row, {
            className: prefixCls + "-row",
            gutter,
            justify,
            align,
            children: currentPageItems.slice(startNum_1, nextStartNum).map(function(item2, index2) {
              return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(col, {
                ...__assign({
                  key: currentRow + "_" + index2,
                  className: prefixCls + "-row-col"
                }, colProps_1, {
                  span: span_1
                }),
                children: render2 ? render2(item2, startNum_1 + index2) : item2
              });
            })
          }, currentRow));
          startNum_1 = nextStartNum;
        };
        while (startNum_1 < currentPageItems.length) {
          _loop_1();
        }
        return items;
      }
      return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(row, {
        className: prefixCls + "-row",
        gutter: grid.gutter,
        children: currentPageItems.map(function(item2, index2) {
          return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(col, {
            ...__assign({
              className: prefixCls + "-row-col"
            }, omit(grid, ["gutter"]), {
              key: index2
            }),
            children: render2 ? render2(item2, index2) : item2
          });
        })
      });
    };
    if (dataSource.length > 0 && render) {
      return grid ? getGrid(dataSource, render) : getItems(dataSource, render);
    }
    if (childrenCount > 0) {
      return grid ? getGrid(children) : getItems(children);
    }
    if (!scrollLoading) {
      return noDataElement || renderEmpty("List");
    }
    return null;
  };
  var renderList = function() {
    var _a2, _b2;
    var listItems = renderListItems();
    var isVirtual = virtualListProps && Array.isArray(listItems);
    var paginationElement = pagination$1 ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pagination, {
      ...__assign({}, paginationProps, {
        className: classNames(prefixCls + "-pagination", paginationProps && paginationProps.className)
      })
    }) : null;
    var paginationElementInsideFooter = paginationInFooter ? paginationElement : null;
    var paginationElementOutsideFooter = paginationInFooter ? null : paginationElement;
    var scrollLoadingEle = scrollLoading !== void 0 && scrollLoading !== null ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-item " + prefixCls + "-scroll-loading",
      children: scrollLoading
    }) : null;
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
      ref: function(_ref) {
        ref = _ref;
        refDom.current = ref;
      },
      style: wrapperStyle,
      className: classNames(prefixCls + "-wrapper", (_a2 = {}, _a2[prefixCls + "-wrapper-rtl"] = rtl, _a2), wrapperClassName),
      children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
        ...__assign({}, pick.pickDataAttributes(props), {
          style,
          className: classNames(prefixCls, prefixCls + "-" + size, (_b2 = {}, _b2[prefixCls + "-no-border"] = !bordered, _b2[prefixCls + "-no-split"] = !split, _b2[prefixCls + "-hoverable"] = hoverable, _b2[prefixCls + "-rtl"] = rtl, _b2), className),
          ref: function(ref2) {
            if (!isVirtual) {
              refScrollElement.current = ref2;
            }
          },
          onScroll: !isVirtual && needHandleScroll ? throttledScrollHandler : void 0
        }),
        children: [header ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
          className: prefixCls + "-header",
          children: header
        }) : null, isVirtual ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(emotionReactJsxRuntime_browser_esm.Fragment, {
          children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2, {
            ...__assign({
              role: "list",
              ref: function(ref2) {
                if (ref2) {
                  refVirtualList.current = ref2;
                  refScrollElement.current = ref2.dom;
                }
              },
              className: prefixCls + "-content " + prefixCls + "-virtual",
              data: scrollLoadingEle ? listItems.concat(scrollLoadingEle) : listItems,
              isStaticItemHeight: false,
              onScroll: needHandleScroll ? throttledScrollHandler : void 0
            }, virtualListProps),
            children: function(child) {
              return child;
            }
          })
        }) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
          role: "list",
          className: prefixCls + "-content",
          ref: refItemListWrapper,
          children: [listItems, scrollLoadingEle]
        }), footer || paginationElementInsideFooter ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
          className: prefixCls + "-footer",
          children: [footer, paginationElementInsideFooter]
        }) : null]
      }), paginationElementOutsideFooter]
    });
  };
  return "loading" in props ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {
    style: {
      display: "block"
    },
    loading,
    element: loadingElement || /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {}),
    children: renderList()
  }) : renderList();
}
var ListComponent = React__default.default.forwardRef(List);
ListComponent.displayName = "List";
ListComponent.Item = item;
const List$1 = ListComponent;
module.exports = List$1;
//# sourceMappingURL=index.js.map
