"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableBodyCell = TableBodyCell;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _tableBodyCellContent = require("./table-body-cell-content");
var _tableBodyCellLoader = require("./table-body-cell-loader");
var _useTableKeyboardNavigation = require("../../hooks/use-table-keyboard-navigation");
var _types = require("../types");
var _useTranslation = require("../../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function TableBodyCell({
  cell,
  tdClassName,
  onCellClick,
  isLeft,
  isLastPinned,
  shouldShowPinnedShadow,
  isRight,
  shouldShowRightPinnedShadow,
  isLoading,
  getRowActions,
  getRowFooterAction,
  rowClickDisabled,
  rowSelectionDisabled,
  rowIndex,
  columnIndex,
  rowClickHandler,
  localizationProps,
  tableBodyCellTestId
}) {
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const navigationContext = (0, _react.useContext)(_useTableKeyboardNavigation.TableKeyboardNavigationContext);
  const cellRef = (0, _react.useRef)(null);
  const onCellClickHandler = (0, _react.useCallback)(event => {
    if (onCellClick) {
      onCellClick(cell, event);
    }
  }, [cell, onCellClick]);
  const isRightAlignment = cell.column.columnDef?.meta?.align === _types.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 === (0, _useTableKeyboardNavigation.getCellId)({
    cellRowIndex: rowIndex,
    cellColumnIndex: columnIndex
  });
  const cellAriaLabel = (0, _react.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__*/(0, _jsxRuntime.jsx)("td", {
    className: (0, _classnames.default)('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__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)('react-table-cell', isRightAlignment && 'right'),
      children: isLoading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_tableBodyCellLoader.TableBodyCellLoader, {
        cell: cell
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_tableBodyCellContent.TableBodyCellContent, {
        cell: cell,
        getRowActions: getRowActions,
        getRowFooterAction: getRowFooterAction,
        rowClickDisabled: !!rowClickDisabled,
        rowSelectionDisabled: rowSelectionDisabled,
        keyboardNavCellId: `${rowIndex}-${columnIndex}`,
        localizationProps: localizationProps
      })
    })
  });
}