import React, { useCallback, useContext, useRef, useMemo } from 'react';
import classnames from 'classnames';
import { TableBodyCellContent } from './table-body-cell-content';
import { TableBodyCellLoader } from './table-body-cell-loader';
import { getCellId, TableKeyboardNavigationContext } from '../../hooks/use-table-keyboard-navigation';
import { ALIGNMENT } from '../types';
import { useTranslation } from '../../hooks/use-translation';
import { jsx as _jsx } from "react/jsx-runtime";
export function TableBodyCell({
  cell,
  tdClassName,
  onCellClick,
  isLeft,
  isLastPinned,
  shouldShowPinnedShadow,
  isRight,
  shouldShowRightPinnedShadow,
  isLoading,
  getRowActions,
  getRowFooterAction,
  rowClickDisabled,
  rowSelectionDisabled,
  rowIndex,
  columnIndex,
  rowClickHandler,
  localizationProps,
  tableBodyCellTestId
}) {
  const {
    t
  } = useTranslation();
  const navigationContext = useContext(TableKeyboardNavigationContext);
  const cellRef = useRef(null);
  const onCellClickHandler = useCallback(event => {
    if (onCellClick) {
      onCellClick(cell, event);
    }
  }, [cell, onCellClick]);
  const isRightAlignment = cell.column.columnDef?.meta?.align === ALIGNMENT.RIGHT;
  navigationContext?.registerCell({
    cellRowIndex: rowIndex,
    cellColumnIndex: columnIndex,
    cellRef,
    cellAction: cell.column.columnDef.meta?.hasCustomCellAction ? undefined : e => {
      onCellClickHandler(e);
      rowClickHandler(e);
    }
  });
  const isCurrentCellFocused = navigationContext?.focusedCell === getCellId({
    cellRowIndex: rowIndex,
    cellColumnIndex: columnIndex
  });
  const cellAriaLabel = useMemo(() => {
    if (rowClickDisabled) {
      const label = localizationProps?.disabledRowDefaultLabel || t('Disabled row');
      if (typeof rowClickDisabled === 'string') {
        return `${label}: ${rowClickDisabled}`;
      }
      return label;
    }
    if (cell.column.columnDef.meta?.cellAriaLabel) {
      return cell.column.columnDef.meta?.cellAriaLabel;
    }
    if (localizationProps?.cellAriaLabel) {
      return localizationProps?.cellAriaLabel;
    }
    return cell.row.getCanSelect() ? t('Click to select the row') : undefined;
  }, [cell.column.columnDef.meta?.cellAriaLabel, localizationProps?.cellAriaLabel, cell.row.getCanSelect()]);
  return /*#__PURE__*/_jsx("td", {
    className: classnames('react-table-td', tdClassName, cell.column.columnDef.meta?.className, isLeft && 'left-pinned', isLeft && isLastPinned && 'last-pinned', shouldShowPinnedShadow && 'left-shadow', isRight && 'right-pinned', shouldShowRightPinnedShadow && 'right-shadow'),
    "test-id": tableBodyCellTestId,
    onClick: onCellClickHandler
    // eslint-disable-next-line react/forbid-dom-props
    ,
    style: {
      width: cell.column.getSize(),
      left: isLeft ? cell.column.getStart('left') : undefined
    },
    ref: cellRef,
    role: "gridcell",
    tabIndex: isCurrentCellFocused ? 0 : -1,
    headers: cell.column.id,
    "aria-label": cellAriaLabel,
    children: /*#__PURE__*/_jsx("div", {
      className: classnames('react-table-cell', isRightAlignment && 'right'),
      children: isLoading ? /*#__PURE__*/_jsx(TableBodyCellLoader, {
        cell: cell
      }) : /*#__PURE__*/_jsx(TableBodyCellContent, {
        cell: cell,
        getRowActions: getRowActions,
        getRowFooterAction: getRowFooterAction,
        rowClickDisabled: !!rowClickDisabled,
        rowSelectionDisabled: rowSelectionDisabled,
        keyboardNavCellId: `${rowIndex}-${columnIndex}`,
        localizationProps: localizationProps
      })
    })
  });
}