'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _extends = require('@babel/runtime/helpers/extends');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.object.values.js');
require('core-js/modules/web.dom-collections.for-each.js');
var classnames = require('classnames');
var PropTypes = require('prop-types');
var React = require('react');
var Settings = require('../../../constants/Settings.js');
var componentUtilityFunctions = require('../../../utils/componentUtilityFunctions.js');
var iconsReact = require('@carbon/icons-react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var React__default = /*#__PURE__*/_interopDefault(React);

var _excluded = ["className", "children", "isSortable", "isSortHeader", "onClick", "scope", "hasMultiSort", "hasOverflow", "hasTooltip", "sortDirection", "translateWithId", "thStyle", "initialWidth", "testId", "spanGroupRow", "rowSpan", "decorator"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var sortStates = {
  NONE: 'NONE',
  DESC: 'DESC',
  ASC: 'ASC'
};
var prefix = Settings.settings.prefix,
  iotPrefix = Settings.settings.iotPrefix;
var translationKeys = {
  iconDescription: 'carbon.table.header.icon.description'
};
var translateWithId = function translateWithId(key, _ref) {
  var sortDirection = _ref.sortDirection,
    isSortHeader = _ref.isSortHeader;
  if (key === translationKeys.iconDescription) {
    if (isSortHeader) {
      // When transitioning, we know that the sequence of states is as follows:
      // NONE -> ASC -> DESC -> NONE
      if (sortDirection === sortStates.NONE) {
        return 'Sort rows by this header in ascending order';
      }
      if (sortDirection === sortStates.ASC) {
        return 'Sort rows by this header in descending order';
      }
      return 'Un sort rows by this header';
    }
    return 'Sort rows by this header in ascending order';
  }
  return '';
};
var sortDirections = _defineProperty__default.default(_defineProperty__default.default(_defineProperty__default.default({}, sortStates.NONE, 'none'), sortStates.ASC, 'ascending'), sortStates.DESC, 'descending');
var TableHeader = /*#__PURE__*/React__default.default.forwardRef(function TableHeader(_ref2, ref) {
  var headerClassName = _ref2.className,
    children = _ref2.children,
    isSortable = _ref2.isSortable,
    isSortHeader = _ref2.isSortHeader,
    onClick = _ref2.onClick,
    scope = _ref2.scope,
    hasMultiSort = _ref2.hasMultiSort,
    hasOverflow = _ref2.hasOverflow,
    hasTooltip = _ref2.hasTooltip,
    sortDirection = _ref2.sortDirection,
    t = _ref2.translateWithId,
    thStyle = _ref2.thStyle,
    initialWidth = _ref2.initialWidth,
    testId = _ref2.testId,
    spanGroupRow = _ref2.spanGroupRow,
    rowSpanProp = _ref2.rowSpan,
    decorator = _ref2.decorator,
    rest = _objectWithoutProperties__default.default(_ref2, _excluded);
  var AILableRef = React.useRef();
  var colHasAILabel;
  var normalizedDecorator = /*#__PURE__*/React__default.default.isValidElement(decorator) ? decorator : null;
  if (normalizedDecorator) {
    colHasAILabel = true;
    normalizedDecorator = /*#__PURE__*/React__default.default.cloneElement(normalizedDecorator, {
      ref: AILableRef,
      className: '',
      onClick: function onClick(event) {
        event.preventDefault();
        event.stopPropagation();
      }
    });
  }
  var rowSpan = (rowSpanProp !== null && rowSpanProp !== void 0 ? rowSpanProp : spanGroupRow) ? '2' : undefined;
  var headerClassNames = classnames__default.default(headerClassName, _defineProperty__default.default(_defineProperty__default.default({}, "".concat(iotPrefix, "--table-header--span-group-row"), spanGroupRow), "".concat(prefix, "--table-sort__header--ai-label ").concat(prefix, "--table-sort__header--decorator"), colHasAILabel));
  if (!isSortable) {
    return (
      /*#__PURE__*/
      // eslint-disable-next-line react/jsx-filename-extension
      React__default.default.createElement("th", _extends__default.default({}, rest, {
        rowSpan: rowSpan,
        "data-testid": testId,
        width: initialWidth,
        className: headerClassNames,
        scope: scope,
        ref: ref,
        style: thStyle
      }), /*#__PURE__*/React__default.default.createElement("div", {
        className: "".concat(prefix, "--table-header-label-container")
      }, !hasTooltip ? /*#__PURE__*/React__default.default.createElement("span", {
        className: "".concat(prefix, "--table-header-label")
      }, children) : children, colHasAILabel ? normalizedDecorator : ''))
    );
  }
  var className = classnames__default.default(headerClassName, _defineProperty__default.default(_defineProperty__default.default(_defineProperty__default.default({}, "".concat(prefix, "--table-sort"), true), "".concat(prefix, "--table-sort--active"), isSortHeader && sortDirection !== sortStates.NONE), "".concat(prefix, "--table-sort--ascending"), isSortHeader && sortDirection === sortStates.DESC));
  var ariaSort = !isSortHeader ? 'none' : sortDirections[sortDirection];
  var ButtonTag = hasMultiSort || hasOverflow ? "a" : "button";
  var buttonProps = hasMultiSort || hasOverflow ? _objectSpread({
    role: 'button',
    tabIndex: 0,
    className: className,
    onClick: onClick,
    onKeyDown: componentUtilityFunctions.handleSpecificKeyDown(['Enter', 'Space'], onClick)
  }, rest) : _objectSpread({
    className: className,
    onClick: onClick
  }, rest);
  return /*#__PURE__*/React__default.default.createElement("th", {
    rowSpan: rowSpan,
    width: initialWidth,
    scope: scope,
    className: headerClassNames,
    "aria-sort": ariaSort,
    ref: ref,
    style: thStyle,
    "data-testid": testId
  }, /*#__PURE__*/React__default.default.createElement(ButtonTag, buttonProps, !hasTooltip ? /*#__PURE__*/React__default.default.createElement("span", {
    className: "".concat(prefix, "--table-header-label")
  }, children, " ") : children, sortDirection === sortStates.ASC ? /*#__PURE__*/React__default.default.createElement(iconsReact.ArrowUp, {
    className: "".concat(prefix, "--table-sort__icon"),
    "aria-label": t('carbon.table.header.icon.description', {
      header: children,
      sortDirection: sortDirection,
      isSortHeader: isSortHeader,
      sortStates: sortStates
    })
  }) : null, sortDirection === sortStates.DESC ? /*#__PURE__*/React__default.default.createElement(iconsReact.ArrowDown, {
    className: "".concat(prefix, "--table-sort__icon"),
    "aria-label": t('carbon.table.header.icon.description', {
      header: children,
      sortDirection: sortDirection,
      isSortHeader: isSortHeader,
      sortStates: sortStates
    })
  }) : null, /*#__PURE__*/React__default.default.createElement(iconsReact.ArrowsVertical, {
    className: "".concat(prefix, "--table-sort__icon-unsorted"),
    "aria-label": t('carbon.table.header.icon.description', {
      header: children,
      sortDirection: sortDirection,
      isSortHeader: isSortHeader,
      sortStates: sortStates
    })
  }), colHasAILabel ? normalizedDecorator : ''));
});
TableHeader.propTypes = {
  /**
   * Specify an optional className to be applied to the container node
   */
  className: PropTypes__default.default.string,
  /**
   * Pass in children that will be embedded in the table header label
   */
  children: PropTypes__default.default.node,
  /**
   * True if the header must span the column group row.
   */
  spanGroupRow: PropTypes__default.default.bool,
  hasOverflow: PropTypes__default.default.bool,
  hasMultiSort: PropTypes__default.default.bool,
  /** does the header have a tooltip, if so do not truncate */
  hasTooltip: PropTypes__default.default.bool,
  /**
   * The initial width of the column when hasResize:true. The fixed width
   * if hasResize:false. E.g. '200px'
   */
  initialWidth: PropTypes__default.default.string,
  /**
   * Specify whether this header is one through which a user can sort the table
   */
  isSortable: PropTypes__default.default.bool,
  /**
   * Specify whether this header is the header by which a table is being sorted
   * by
   */
  isSortHeader: PropTypes__default.default.bool,
  /**
   * Hook that is invoked when the header is clicked
   */
  onClick: PropTypes__default.default.func,
  /** Applies the HTML attribute rowspan with the number provided */
  rowSpan: PropTypes__default.default.number,
  /**
   * Specify the scope of this table header. You can find more info about this
   * attribute at the following URL:
   * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th#attr-scope
   */
  scope: PropTypes__default.default.string,
  /**
   * Specify which direction we are currently sorting by, should be one of DESC,
   * NONE, or ASC.
   */
  sortDirection: PropTypes__default.default.oneOf(Object.values(sortStates)),
  /**
   * Supply a method to translate internal strings with your i18n tool of
   * choice. Translation keys are available on the `translationKeys` field for
   * this component.
   */
  translateWithId: PropTypes__default.default.func,
  // eslint-disable-next-line react/forbid-prop-types
  thStyle: PropTypes__default.default.object,
  testId: PropTypes__default.default.string,
  decorator: PropTypes__default.default.node
};

/* istanbul ignore next: ignoring the default onClick */
TableHeader.defaultProps = {
  className: '',
  children: '',
  spanGroupRow: false,
  isSortHeader: false,
  hasTooltip: false,
  hasOverflow: false,
  hasMultiSort: false,
  isSortable: false,
  rowSpan: undefined,
  sortDirection: 'NONE',
  onClick: function onClick(_onClick) {
    return "".concat(_onClick);
  },
  scope: 'col',
  translateWithId: translateWithId,
  thStyle: {},
  initialWidth: undefined,
  testId: '',
  decorator: null
};
TableHeader.translationKeys = Object.values(translationKeys);
TableHeader.displayName = 'TableHeader';
TableHeader.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "TableHeader",
  "props": {
    "className": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "Specify an optional className to be applied to the container node",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "children": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "Pass in children that will be embedded in the table header label",
      "type": {
        "name": "node"
      },
      "required": false
    },
    "spanGroupRow": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "True if the header must span the column group row.",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "isSortHeader": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Specify whether this header is the header by which a table is being sorted\nby",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "hasTooltip": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "does the header have a tooltip, if so do not truncate",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "hasOverflow": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "hasMultiSort": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "isSortable": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Specify whether this header is one through which a user can sort the table",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "rowSpan": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Applies the HTML attribute rowspan with the number provided",
      "type": {
        "name": "number"
      },
      "required": false
    },
    "sortDirection": {
      "defaultValue": {
        "value": "'NONE'",
        "computed": false
      },
      "description": "Specify which direction we are currently sorting by, should be one of DESC,\nNONE, or ASC.",
      "type": {
        "name": "enum",
        "value": [{
          "value": "\"ASC\"",
          "computed": false
        }, {
          "value": "\"DESC\"",
          "computed": false
        }, {
          "value": "\"NONE\"",
          "computed": false
        }]
      },
      "required": false
    },
    "onClick": {
      "defaultValue": {
        "value": "(onClick) => `${onClick}`",
        "computed": false
      },
      "description": "Hook that is invoked when the header is clicked",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "scope": {
      "defaultValue": {
        "value": "'col'",
        "computed": false
      },
      "description": "Specify the scope of this table header. You can find more info about this\nattribute at the following URL:\nhttps://developer.mozilla.org/en-US/docs/Web/HTML/Element/th#attr-scope",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "translateWithId": {
      "defaultValue": {
        "value": "(key, { sortDirection, isSortHeader }) => {\n  if (key === translationKeys.iconDescription) {\n    if (isSortHeader) {\n      // When transitioning, we know that the sequence of states is as follows:\n      // NONE -> ASC -> DESC -> NONE\n      if (sortDirection === sortStates.NONE) {\n        return 'Sort rows by this header in ascending order';\n      }\n      if (sortDirection === sortStates.ASC) {\n        return 'Sort rows by this header in descending order';\n      }\n\n      return 'Un sort rows by this header';\n    }\n    return 'Sort rows by this header in ascending order';\n  }\n\n  return '';\n}",
        "computed": false
      },
      "description": "Supply a method to translate internal strings with your i18n tool of\nchoice. Translation keys are available on the `translationKeys` field for\nthis component.",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "thStyle": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "object"
      },
      "required": false
    },
    "initialWidth": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "The initial width of the column when hasResize:true. The fixed width\nif hasResize:false. E.g. '200px'",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "decorator": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "node"
      },
      "required": false
    }
  }
};
var TableHeader$1 = TableHeader;

exports.default = TableHeader$1;
exports.sortStates = sortStates;
exports.translateWithId = translateWithId;
exports.translationKeys = translationKeys;
