import _extends from '@babel/runtime/helpers/extends';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.object.get-own-property-descriptor.js';
import 'core-js/modules/es.object.get-own-property-descriptors.js';
import 'core-js/modules/es.object.keys.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.object.values.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import React__default, { useRef } from 'react';
import { settings } from '../../../constants/Settings.js';
import { handleSpecificKeyDown } from '../../../utils/componentUtilityFunctions.js';
import { ArrowUp, ArrowDown, ArrowsVertical } from '@carbon/icons-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(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.prefix,
  iotPrefix = 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(_defineProperty(_defineProperty({}, sortStates.NONE, 'none'), sortStates.ASC, 'ascending'), sortStates.DESC, 'descending');
var TableHeader = /*#__PURE__*/React__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(_ref2, _excluded);
  var AILableRef = useRef();
  var colHasAILabel;
  var normalizedDecorator = /*#__PURE__*/React__default.isValidElement(decorator) ? decorator : null;
  if (normalizedDecorator) {
    colHasAILabel = true;
    normalizedDecorator = /*#__PURE__*/React__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(headerClassName, _defineProperty(_defineProperty({}, "".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.createElement("th", _extends({}, rest, {
        rowSpan: rowSpan,
        "data-testid": testId,
        width: initialWidth,
        className: headerClassNames,
        scope: scope,
        ref: ref,
        style: thStyle
      }), /*#__PURE__*/React__default.createElement("div", {
        className: "".concat(prefix, "--table-header-label-container")
      }, !hasTooltip ? /*#__PURE__*/React__default.createElement("span", {
        className: "".concat(prefix, "--table-header-label")
      }, children) : children, colHasAILabel ? normalizedDecorator : ''))
    );
  }
  var className = classnames(headerClassName, _defineProperty(_defineProperty(_defineProperty({}, "".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: handleSpecificKeyDown(['Enter', 'Space'], onClick)
  }, rest) : _objectSpread({
    className: className,
    onClick: onClick
  }, rest);
  return /*#__PURE__*/React__default.createElement("th", {
    rowSpan: rowSpan,
    width: initialWidth,
    scope: scope,
    className: headerClassNames,
    "aria-sort": ariaSort,
    ref: ref,
    style: thStyle,
    "data-testid": testId
  }, /*#__PURE__*/React__default.createElement(ButtonTag, buttonProps, !hasTooltip ? /*#__PURE__*/React__default.createElement("span", {
    className: "".concat(prefix, "--table-header-label")
  }, children, " ") : children, sortDirection === sortStates.ASC ? /*#__PURE__*/React__default.createElement(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.createElement(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.createElement(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.string,
  /**
   * Pass in children that will be embedded in the table header label
   */
  children: PropTypes.node,
  /**
   * True if the header must span the column group row.
   */
  spanGroupRow: PropTypes.bool,
  hasOverflow: PropTypes.bool,
  hasMultiSort: PropTypes.bool,
  /** does the header have a tooltip, if so do not truncate */
  hasTooltip: PropTypes.bool,
  /**
   * The initial width of the column when hasResize:true. The fixed width
   * if hasResize:false. E.g. '200px'
   */
  initialWidth: PropTypes.string,
  /**
   * Specify whether this header is one through which a user can sort the table
   */
  isSortable: PropTypes.bool,
  /**
   * Specify whether this header is the header by which a table is being sorted
   * by
   */
  isSortHeader: PropTypes.bool,
  /**
   * Hook that is invoked when the header is clicked
   */
  onClick: PropTypes.func,
  /** Applies the HTML attribute rowspan with the number provided */
  rowSpan: PropTypes.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.string,
  /**
   * Specify which direction we are currently sorting by, should be one of DESC,
   * NONE, or ASC.
   */
  sortDirection: PropTypes.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.func,
  // eslint-disable-next-line react/forbid-prop-types
  thStyle: PropTypes.object,
  testId: PropTypes.string,
  decorator: PropTypes.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;

export { TableHeader$1 as default, sortStates, translateWithId, translationKeys };
