"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableBody = TableBody;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _tooltip = require("../../tooltip");
var _reactPopper = require("../../react-popper");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/* eslint-disable react/forbid-dom-props */

function TableBody({
  tableModel,
  selectable,
  centerScrolled,
  scrollableRight,
  trClassName,
  tdClassName,
  expandedContentClassName,
  TdElement,
  onCellClick,
  paddingTop,
  paddingBottom,
  virtualRows,
  onRowClick,
  isRowClickDisabled = () => false,
  isRowSelectionDisabled = () => false,
  ExpandedRowComponent,
  measureElement,
  getCustomRowContent,
  getRowActions,
  getRowFooterAction,
  size,
  tableContainerWidth,
  localizationProps,
  tableRowTestId,
  tableBodyCellTestId
}) {
  const {
    rows
  } = tableModel.getRowModel();
  // we need this offset so that rowIndex passed to cell renderer would not take
  // into account expanded rows otherwise we have issues with keyboard navigation hook
  // (it uses rowIndex in its' state, and those row indexes do not include expanded rows)
  let expandedIndexOffset = 0;

  // This adds another "artificial" row right after expanded one with isMockedExpandedRow set to true
  // this is needed to make things consistent across react-virtualize elements counts and real rows rendered
  // (expanded row is treated as a usual row, but with specific renderer)
  const rowsWithExpandedItems = rows.reduce((acc, row) => {
    acc.push(row);
    if (row.getIsExpanded()) {
      acc.push({
        ...row,
        isMockedExpandedRow: true
      });
      acc.push({
        ...row,
        empty: true
      });
    }
    return acc;
  }, []);
  const getRowContent = ({
    row,
    isLoading,
    rowClickDisabled,
    rowIndex,
    rowSelectionDisabled,
    rowClickHandler
  }) => {
    const customRowContent = getCustomRowContent?.(row, tableContainerWidth, centerScrolled, scrollableRight);
    return customRowContent || row.getVisibleCells().map((cell, index) => {
      const isLastPinned = index === tableModel.getLeftVisibleLeafColumns().length - 1;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(TdElement, {
        isLeft: cell.column.getIsPinned() === 'left',
        isLastPinned: isLastPinned,
        shouldShowPinnedShadow: isLastPinned && centerScrolled,
        isRight: cell.column.getIsPinned() === 'right',
        shouldShowRightPinnedShadow: scrollableRight,
        cell: cell,
        isLoading: isLoading,
        onCellClick: onCellClick,
        tdClassName: tdClassName,
        getRowActions: getRowActions,
        getRowFooterAction: getRowFooterAction,
        rowClickDisabled: rowClickDisabled,
        rowSelectionDisabled: rowSelectionDisabled,
        rowIndex: rowIndex,
        columnIndex: index,
        rowClickHandler: rowClickHandler,
        localizationProps: localizationProps,
        tableBodyCellTestId: tableBodyCellTestId
      }, cell.id);
    });
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("tbody", {
    className: "react-table-tbody",
    children: [paddingTop > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
      "aria-hidden": true,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("td", {
        style: {
          height: `${paddingTop}px`
        }
      })
    }), virtualRows.map(virtualRow => {
      const row = rowsWithExpandedItems[virtualRow.index];
      if (row.empty) {
        expandedIndexOffset += 1;
        return /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
          ref: measureElement,
          "data-index": virtualRow.index,
          "aria-hidden": true,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)("td", {
            style: {
              height: `12px`
            }
          })
        }, `${row.id}-expanded-empty-row`);
      }
      // if this is a mocked row for expanded content - render expanded row instead of usual
      if (row.isMockedExpandedRow && ExpandedRowComponent) {
        expandedIndexOffset += 1;
        return /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
          className: "react-table-tr-expanded-content",
          "test-id": "react-table-tr-expanded-content",
          "data-index": virtualRow.index,
          ref: measureElement,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)("td", {
            className: "react-table-td-expanded-content",
            "test-id": "react-table-td-expanded-content",
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: (0, _classnames.default)('expanded-content-wrapper', expandedContentClassName),
              style: {
                width: tableContainerWidth
              },
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ExpandedRowComponent, {
                rowData: row.original
              })
            })
          })
        }, `${row.id}-expanded-content`);
      }
      const rowClickDisabled = isRowClickDisabled(row.original);
      const rowSelectionDisabled = isRowSelectionDisabled(row.original);
      const isLoading = !!row.original?.reservedMeta?.isLoading;
      const rowClickHandler = e => {
        if (onRowClick && !rowClickDisabled && !isLoading) {
          onRowClick(e, row.original);
        }
        if (selectable && !rowSelectionDisabled) {
          row.toggleSelected();
        }
      };
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
        content: typeof rowClickDisabled === 'string' ? rowClickDisabled : '',
        placement: _tooltip.TOOLTIP_PLACEMENT_TYPES.TOP,
        delayType: _tooltip.TOOLTIP_DELAY_TYPES.FAST,
        testId: "table-row-tooltip",
        popperModifiers: [_reactPopper.OFFSET_MODIFIER, _reactPopper.FLIP_MODIFIER, _reactPopper.PREVENT_PARENT_OVERFLOW],
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
          className: (0, _classnames.default)('react-table-tr', size, typeof trClassName === 'function' ? trClassName(row) : trClassName || '', (selectable && !rowSelectionDisabled || onRowClick) && !isLoading && 'clickable', row.getIsSelected() && !isLoading && 'react-table-tr-selected', row.getIsExpanded() && !isLoading && 'react-table-tr-expanded', !isLoading && rowClickDisabled && 'disabled'),
          "test-id": typeof tableRowTestId === 'function' ? tableRowTestId(row) : tableRowTestId,
          onClick: rowClickHandler,
          "data-index": virtualRow.index,
          ref: measureElement,
          role: "row",
          "aria-selected": row.getIsSelected(),
          "aria-expanded": tableModel.options.enableExpanding ? row.getIsExpanded() : undefined,
          children: getRowContent({
            row,
            isLoading,
            rowClickDisabled,
            rowIndex: virtualRow.index - expandedIndexOffset,
            rowSelectionDisabled,
            rowClickHandler
          })
        })
      }, row.id);
    }), paddingBottom > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
      "aria-hidden": true,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("td", {
        style: {
          height: `${paddingBottom}px`
        }
      })
    })]
  });
}