/* eslint-disable react/forbid-dom-props */
import React from 'react';
import classnames from 'classnames';
import { Tooltip, TOOLTIP_DELAY_TYPES, TOOLTIP_PLACEMENT_TYPES } from '../../tooltip';
import { PREVENT_PARENT_OVERFLOW, OFFSET_MODIFIER, FLIP_MODIFIER } from '../../react-popper';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export 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__*/_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__*/_jsxs("tbody", {
    className: "react-table-tbody",
    children: [paddingTop > 0 && /*#__PURE__*/_jsx("tr", {
      "aria-hidden": true,
      children: /*#__PURE__*/_jsx("td", {
        style: {
          height: `${paddingTop}px`
        }
      })
    }), virtualRows.map(virtualRow => {
      const row = rowsWithExpandedItems[virtualRow.index];
      if (row.empty) {
        expandedIndexOffset += 1;
        return /*#__PURE__*/_jsx("tr", {
          ref: measureElement,
          "data-index": virtualRow.index,
          "aria-hidden": true,
          children: /*#__PURE__*/_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__*/_jsx("tr", {
          className: "react-table-tr-expanded-content",
          "test-id": "react-table-tr-expanded-content",
          "data-index": virtualRow.index,
          ref: measureElement,
          children: /*#__PURE__*/_jsx("td", {
            className: "react-table-td-expanded-content",
            "test-id": "react-table-td-expanded-content",
            children: /*#__PURE__*/_jsx("div", {
              className: classnames('expanded-content-wrapper', expandedContentClassName),
              style: {
                width: tableContainerWidth
              },
              children: /*#__PURE__*/_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__*/_jsx(Tooltip, {
        content: typeof rowClickDisabled === 'string' ? rowClickDisabled : '',
        placement: TOOLTIP_PLACEMENT_TYPES.TOP,
        delayType: TOOLTIP_DELAY_TYPES.FAST,
        testId: "table-row-tooltip",
        popperModifiers: [OFFSET_MODIFIER, FLIP_MODIFIER, PREVENT_PARENT_OVERFLOW],
        children: /*#__PURE__*/_jsx("tr", {
          className: classnames('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__*/_jsx("tr", {
      "aria-hidden": true,
      children: /*#__PURE__*/_jsx("td", {
        style: {
          height: `${paddingBottom}px`
        }
      })
    })]
  });
}