"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _index = _interopRequireDefault(require("../../Button/index"));

var _index2 = _interopRequireDefault(require("../../ArrowsInline/index"));

var _SORT_ORDER_DIRECTION;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var SORT_ORDERS = {
  ASC: 'asc',
  DESC: 'desc'
};
var SORT_ORDER_DIRECTION = (_SORT_ORDER_DIRECTION = {}, _defineProperty(_SORT_ORDER_DIRECTION, SORT_ORDERS.ASC, 'up'), _defineProperty(_SORT_ORDER_DIRECTION, SORT_ORDERS.DESC, 'down'), _SORT_ORDER_DIRECTION);

var TH = function TH(_ref) {
  var children = _ref.children,
      colSpan = _ref.colSpan,
      isCollapsed = _ref.isCollapsed,
      isNumerical = _ref.isNumerical,
      sorting = _ref.sorting,
      testSection = _ref.testSection,
      textAlign = _ref.textAlign,
      width = _ref.width;
  var classes = (0, _classnames["default"])({
    'oui-numerical': isNumerical,
    'oui-cell-collapse': isCollapsed
  });
  var styles = {
    width: width,
    textAlign: textAlign
  };
  var tableHeaderContent = sorting.canSort ? _react["default"].createElement(_index["default"], {
    onClick: sorting.handleSort,
    style: "unstyled",
    testSection: "table-header-sort-button"
  }, children, _react["default"].createElement("span", {
    className: "push-half--left"
  }, _react["default"].createElement(_index2["default"], {
    direction: SORT_ORDER_DIRECTION[sorting.order],
    testSection: "table-header-sort-indicator"
  }))) : children;
  return _react["default"].createElement("th", {
    className: classes,
    "data-test-section": testSection,
    style: styles,
    colSpan: colSpan
  }, tableHeaderContent);
};

TH.propTypes = {
  /** Content within the `Table.TH` component */
  children: _propTypes["default"].node,

  /** Number of columns that the cell should span */
  colSpan: _propTypes["default"].number,

  /**
    Tell the cell to take up the least amount of width possible. This only
    works well if the table layout is `auto`, not `fixed`.
  */
  isCollapsed: _propTypes["default"].bool,

  /** Right-align the cell if the contents are numerical */
  isNumerical: _propTypes["default"].bool,

  /** Sorting options */
  sorting: _propTypes["default"].shape({
    /** Indicate if this column should be sortable */
    canSort: _propTypes["default"]["boolean"],

    /** Function that handles sorting */
    handleSort: _propTypes["default"].func,

    /** Current order. One of 'asc' or 'desc' */
    order: _propTypes["default"].oneOf([SORT_ORDERS.ASC, SORT_ORDERS.DESC])
  }),

  /** Hook for automated JavaScript tests */
  testSection: _propTypes["default"].string,

  /** Text alignment */
  textAlign: _propTypes["default"].oneOf(['center', 'right', 'left']),

  /** A number with a unit that becomes the width of the `Table` cell */
  width: _propTypes["default"].string
};
TH.defaultProps = {
  isCollapsed: false,
  isNumerical: false,
  sorting: {
    canSort: false
  }
};
TH.propTypes = {
  /** Checks that all the info for sorting is present.
   *  @param {Object} props Object of props
   *  @returns {Error} Error or null
   */
  sorting: function verifyHandleSortExists(props) {
    if (props.sorting.canSort && !props.sorting.handleSort) {
      return new Error('Must pass a sorting function.');
    }

    if (props.sorting.canSort && !props.sorting.order) {
      return new Error('Must pass a sorting direction.');
    }

    return null;
  }
};
TH.displayName = 'Table.TH';
var _default = TH;
exports["default"] = _default;