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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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); }
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;