function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React, { useMemo } from "react";
import { FixedSizeGrid as Grid } from "react-window";
import AutoSizer from "react-virtualized-auto-sizer";
import useGenGridProps from "./useGenGridProps";
import useGapSize from "./useGapSize";
import { Empty } from "antd";
import { isMobile } from "../../../../../utils/platform";
import "./index.less";
var Cell = function Cell(props) {
  var columnIndex = props.columnIndex,
    rowIndex = props.rowIndex,
    style = props.style,
    isScrolling = props.isScrolling,
    data = props.data;
  var renderItem = data.renderItem,
    gap = data.gap,
    columnCount = data.columnCount,
    dataSource = data.dataSource,
    paddingTop = data.paddingTop;
  var dataIndex = useMemo(function () {
    return columnCount * rowIndex + columnIndex;
  }, [columnCount, rowIndex, columnIndex]);
  var _useGapSize = useGapSize(gap),
    horizontal = _useGapSize.horizontal,
    vertical = _useGapSize.vertical;
  var content = useMemo(function () {
    if (dataIndex >= dataSource.length) {
      return null;
    }
    return renderItem(dataSource[dataIndex], dataIndex);
  }, [dataSource, dataIndex, renderItem]);
  return /*#__PURE__*/React.createElement("div", {
    style: _objectSpread(_objectSpread({}, style), {}, {
      left: ((style === null || style === void 0 ? void 0 : style.left) || 0) + columnIndex * horizontal,
      top: ((style === null || style === void 0 ? void 0 : style.top) || 0) + paddingTop,
      paddingBottom: vertical
    })
  }, content);
};
var GridComponent = function GridComponent(props) {
  var dataSource = props.dataSource,
    width = props.width,
    height = props.height,
    renderItem = props.renderItem,
    gap = props.gap,
    itemMaxWidth = props.itemMaxWidth,
    itemMinWidth = props.itemMinWidth,
    rowHeight = props.rowHeight,
    paddingTop = props.paddingTop;
  var _useGapSize2 = useGapSize(gap),
    horizontal = _useGapSize2.horizontal,
    vertical = _useGapSize2.vertical;
  var _useGenGridProps = useGenGridProps({
      itemMinWidth: itemMinWidth,
      itemMaxWidth: itemMaxWidth,
      width: width,
      dataSource: dataSource,
      horizontal: horizontal,
      vertical: vertical
    }),
    columnCount = _useGenGridProps.columnCount,
    columnWidth = _useGenGridProps.columnWidth,
    rowCount = _useGenGridProps.rowCount;
  var itemData = useMemo(function () {
    return {
      dataSource: dataSource,
      renderItem: renderItem,
      columnCount: columnCount,
      gap: gap,
      paddingTop: paddingTop
    };
  }, [dataSource, columnCount, gap, renderItem, paddingTop]);
  return /*#__PURE__*/React.createElement(Grid, {
    className: isMobile() ? "pisell-lowcode-mobile-gallery" : "",
    columnCount: columnCount,
    columnWidth: columnWidth,
    height: height,
    rowCount: rowCount,
    rowHeight: rowHeight + vertical,
    width: width,
    itemData: itemData
  }, Cell);
};
var VirtualGrid = function VirtualGrid(props) {
  var dataSource = props.dataSource,
    renderItem = props.renderItem,
    gap = props.gap,
    itemMinWidth = props.itemMinWidth,
    itemMaxWidth = props.itemMaxWidth,
    rowHeight = props.rowHeight,
    _props$paddingTop = props.paddingTop,
    paddingTop = _props$paddingTop === void 0 ? 0 : _props$paddingTop;
  if (dataSource.length === 0) {
    return /*#__PURE__*/React.createElement(Empty, {
      style: {
        marginTop: 32
      },
      image: Empty.PRESENTED_IMAGE_SIMPLE
    });
  }
  return /*#__PURE__*/React.createElement(AutoSizer, null, function (_ref) {
    var height = _ref.height,
      width = _ref.width;
    return /*#__PURE__*/React.createElement(GridComponent, {
      paddingTop: paddingTop,
      rowHeight: rowHeight,
      itemMinWidth: itemMinWidth,
      itemMaxWidth: itemMaxWidth,
      gap: gap,
      width: width,
      height: height,
      dataSource: dataSource,
      renderItem: renderItem
    });
  });
};
export default VirtualGrid;