function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["className", "paginationType", "pagination", "footer", "header", "listHeight", "loadMoreData", "arrow"];
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useMemo, useRef } from "react";
import classNames from "classnames";
import { List as AntList, Skeleton } from "antd";
import { getUniqueId } from "@pisell/utils";
import InfiniteScroll from "react-infinite-scroll-component";
import { DoubleRightOutlined } from "@ant-design/icons";
import { useScroll } from "ahooks";
import Pagination from "../Pagination";
import "./index.less";
var prefix = "pisell-lowcode-";
var downArrow = /*#__PURE__*/React.createElement("div", {
  className: "".concat(prefix, "list-arrow-wrap ").concat(prefix, "list-arrow-down")
}, /*#__PURE__*/React.createElement(DoubleRightOutlined, {
  style: {
    transform: "rotate(90deg)"
  }
}));
var upArrow = /*#__PURE__*/React.createElement("div", {
  className: "".concat(prefix, "list-arrow-wrap ").concat(prefix, "list-arrow-up")
}, /*#__PURE__*/React.createElement(DoubleRightOutlined, {
  style: {
    transform: "rotate(-90deg)"
  }
}));
var List = function List(props) {
  var className = props.className,
    paginationType = props.paginationType,
    pagination = props.pagination,
    footer = props.footer,
    header = props.header,
    listHeight = props.listHeight,
    loadMoreData = props.loadMoreData,
    arrow = props.arrow,
    others = _objectWithoutProperties(props, _excluded);
  var listRef = useRef(null);
  var scrollListId = useMemo(function () {
    return "scrollableDiv-".concat(getUniqueId());
  }, []);
  var position = useScroll(listRef);
  var isShowDownArrow = useMemo(function () {
    var _listRef$current, _listRef$current2;
    return arrow && (listRef === null || listRef === void 0 ? void 0 : listRef.current) && ((position === null || position === void 0 ? void 0 : position.top) || 0) + (listRef === null || listRef === void 0 || (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.clientHeight) < ((_listRef$current2 = listRef.current) === null || _listRef$current2 === void 0 ? void 0 : _listRef$current2.scrollHeight);
  }, [position, arrow]);
  var isShowUpArrow = useMemo(function () {
    return arrow && ((position === null || position === void 0 ? void 0 : position.top) || 0) > 0;
  }, [position, arrow]);
  var _footer = useMemo(function () {
    if (!(pagination && paginationType === "default") && !footer) {
      return null;
    }
    return /*#__PURE__*/React.createElement(React.Fragment, null, pagination && paginationType === "default" && /*#__PURE__*/React.createElement(Pagination, _extends({
      showLessItems: true
    }, pagination)), footer);
  }, [pagination, footer]);
  var list = useMemo(function () {
    var _props$grid2;
    if (paginationType === "scroll") {
      var _props$grid;
      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefix, "scroll-list-wrap")
      }, header && /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefix, "scroll-header-wrap")
      }, header), /*#__PURE__*/React.createElement("div", {
        id: scrollListId,
        ref: listRef,
        style: {
          height: listHeight || 400,
          overflow: "auto"
        }
      }, isShowUpArrow && upArrow, /*#__PURE__*/React.createElement(InfiniteScroll, {
        key: "".concat(document.getElementById(scrollListId)),
        dataLength: props.dataSource.length,
        next: loadMoreData,
        hasMore: props.dataSource.length < ((pagination === null || pagination === void 0 ? void 0 : pagination.total) || 999999),
        loader: props.dataSource.length > 0 && /*#__PURE__*/React.createElement(Skeleton, {
          avatar: true,
          paragraph: {
            rows: 1
          },
          active: true,
          className: "".concat(prefix, "scroll-skeleton")
        }),
        scrollableTarget: scrollListId
      }, /*#__PURE__*/React.createElement(AntList, _extends({}, others, {
        className: classNames(className, _defineProperty({}, "".concat(prefix, "list-one-line-wrap"), props === null || props === void 0 || (_props$grid = props.grid) === null || _props$grid === void 0 ? void 0 : _props$grid.oneLine))
      }))), isShowDownArrow && downArrow), _footer && /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefix, "scroll-footer-wrap")
      }, _footer));
    }
    return /*#__PURE__*/React.createElement(AntList, _extends({}, props, {
      className: classNames(className, _defineProperty({}, "".concat(prefix, "list-one-line-wrap"), props === null || props === void 0 || (_props$grid2 = props.grid) === null || _props$grid2 === void 0 ? void 0 : _props$grid2.oneLine))
    }));
  }, [props, _footer, isShowDownArrow, isShowUpArrow, scrollListId]);
  return list;
};
List.Item = AntList.Item;
export default List;