"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.TableCell = void 0;
var _react = require("react");
var _TableContext = _interopRequireDefault(require("./TableContext"));
var _classnames = _interopRequireDefault(require("classnames"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
 * `TableCell` dynamically renders a `<th>` or `<td>` element based on the
 * parent component or user specified `component` prop. By default `TableCell`
 * will automatically render a `<th>` element if the parent component is
 * `TableHead`, otherwise it will render a `<td>` element.
 */
const TableCell = _ref => {
  let {
    align,
    children,
    className,
    component,
    headers,
    id,
    scope,
    stackedTitle,
    stackedClassName,
    _isTableHeadChild,
    ...tableCellProps
  } = _ref;
  const {
    stackable,
    warningDisabled
  } = (0, _react.useContext)(_TableContext.default);
  let Component;
  if (component) {
    Component = component;
  } else {
    Component = _isTableHeadChild ? 'th' : 'td';
  }
  if (process.env.NODE_ENV !== 'production' && stackable && !warningDisabled) {
    // Provide warning message for `id` prop for cells with parent component of `TableHead`
    if (_isTableHeadChild) {
      if (!id && children) {
        console.warn('The id prop in `TableCell` is required for stackable tables. This prop is needed to assign an id to a heading in the responsive stacked view.');
      }
    } else if (Component === 'td') {
      // Provide warning message for stacktable `headers` and `stackedTitle` props
      if (!headers) {
        console.warn('The headers prop in `TableCell` is required for stackable tables. This prop is needed to associate the headings with data cells in the responsive stacked view.');
      }
      if (!stackedTitle) {
        console.warn('The stackedTitle prop in `TableCell` is required for stackable tables. This prop is displayed for the data cell in the responsive stacked view.');
      }
    }
  }
  let role = 'cell';
  if (_isTableHeadChild) {
    role = 'columnheader';
  } else if (component === 'th') {
    role = 'rowheader';
  }
  let defaultScope = scope;
  if (!defaultScope && _isTableHeadChild) {
    defaultScope = 'col';
  }
  const alignClassName = align ? `ds-u-text-align--${align}` : null;
  const classes = (0, _classnames.default)(alignClassName, className);

  // The data attributes `data-title` is access by CSS to generates row header content for stacked table
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
    className: classes,
    role: role,
    scope: defaultScope,
    headers: headers,
    id: id,
    "data-title": stackedTitle,
    ...tableCellProps,
    children: children
  });
};
exports.TableCell = TableCell;
TableCell.defaultProps = {
  align: 'left'
};
var _default = exports.default = TableCell;