var _excluded = ["field", "isSortable", "onSortClick", "sortOrder", "children"];
var _SORT_ORDER_ICONS_MAP, _SORT_ORDER_ARIA_SORT;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React from 'react';
import { IconSortAscending, IconSortDescending, IconSortUnsorted } from '@defencedigital/icon-library';
import { TABLE_SORT_ORDER } from './constants';
import { StyledTableColumn } from './partials/StyledTableColumn';
var SORT_ORDER_ICONS_MAP = (_SORT_ORDER_ICONS_MAP = {}, _defineProperty(_SORT_ORDER_ICONS_MAP, TABLE_SORT_ORDER.ASCENDING, /*#__PURE__*/React.createElement(IconSortAscending, {
  "aria-hidden": true
})), _defineProperty(_SORT_ORDER_ICONS_MAP, TABLE_SORT_ORDER.DESCENDING, /*#__PURE__*/React.createElement(IconSortDescending, {
  "aria-hidden": true
})), _SORT_ORDER_ICONS_MAP);
var SORT_ORDER_ARIA_SORT_MAP = (_SORT_ORDER_ARIA_SORT = {}, _defineProperty(_SORT_ORDER_ARIA_SORT, TABLE_SORT_ORDER.ASCENDING, 'ascending'), _defineProperty(_SORT_ORDER_ARIA_SORT, TABLE_SORT_ORDER.DESCENDING, 'descending'), _SORT_ORDER_ARIA_SORT);
function getIcon(sortable, sortOrder) {
  if (!sortable) {
    return null;
  }
  if (!sortOrder) {
    return /*#__PURE__*/React.createElement(IconSortUnsorted, {
      "aria-hidden": true
    });
  }
  return SORT_ORDER_ICONS_MAP[sortOrder];
}
function getAriaSort(isSortable, sortOrder) {
  if (!isSortable) {
    return undefined;
  }
  if (!sortOrder) {
    return 'none';
  }
  return SORT_ORDER_ARIA_SORT_MAP[sortOrder];
}
export var TableColumn = function TableColumn(_ref) {
  var field = _ref.field,
    _ref$isSortable = _ref.isSortable,
    isSortable = _ref$isSortable === void 0 ? false : _ref$isSortable,
    onSortClick = _ref.onSortClick,
    _ref$sortOrder = _ref.sortOrder,
    sortOrder = _ref$sortOrder === void 0 ? null : _ref$sortOrder,
    children = _ref.children,
    rest = _objectWithoutProperties(_ref, _excluded);
  var icon = getIcon(isSortable, sortOrder);
  var onClick = function onClick() {
    if (isSortable) {
      onSortClick === null || onSortClick === void 0 ? void 0 : onSortClick(field);
    }
  };
  return /*#__PURE__*/React.createElement(StyledTableColumn, _extends({
    "aria-sort": getAriaSort(isSortable, sortOrder),
    $isSortable: isSortable,
    onClick: onClick
  }, rest), children, icon);
};