import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Icon } from '../../Icon';
import { playMessage } from '../../functions';
export var ASC = 'ASC';
export var DESC = 'DESC';
export var KEY_ENTER = 13;
export var KEY_SPACE = 32;
export var CommonListSorter =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(CommonListSorter, _React$Component);

  function CommonListSorter(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;

    _defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (e) {
      if (e.keyCode === KEY_ENTER || e.keyCode === KEY_SPACE) {
        _this.onClick();

        e.preventDefault();
      }
    });

    _this.onClick = _this.onClick.bind(_assertThisInitialized(_this));
    return _this;
  }

  var _proto = CommonListSorter.prototype;

  _proto.onClick = function onClick() {
    var _this$props = this.props,
        _this$props$ascending = _this$props.ascendingText,
        ascendingText = _this$props$ascending === void 0 ? '' : _this$props$ascending,
        _this$props$descendin = _this$props.descendingText,
        descendingText = _this$props$descendin === void 0 ? '' : _this$props$descendin,
        displayName = _this$props.displayName,
        property = _this$props.property,
        children = _this$props.children,
        onChangeSorting = _this$props.onChangeSorting,
        direction = _this$props.direction;
    var shouldReverse = direction === ASC;
    onChangeSorting(property, shouldReverse ? DESC : ASC);
    playMessage(shouldReverse ? descendingText.replace('{name}', displayName || children || property) : ascendingText.replace('{name}', displayName || children || property));
  };

  _proto.sorterClassName = function sorterClassName() {
    var direction = this.props.direction;
    return classnames('sortable', {
      'order-asc': direction === ASC,
      'order-desc': direction === DESC
    });
  };

  _proto.getAccessibilityHeaderProps = function getAccessibilityHeaderProps() {
    return {
      role: 'button',
      onKeyDown: this.handleKeyDown,
      tabIndex: 0,
      title: this.props.sorterTitleText
    };
  };

  _proto.getAriaSortName = function getAriaSortName() {
    var direction = this.props.direction;
    var sortName = 'none';

    if (direction === ASC) {
      sortName = 'ascending';
    }

    if (direction === DESC) {
      sortName = 'descending';
    }

    return sortName;
  };

  _proto.renderSorterIcon = function renderSorterIcon() {
    var direction = this.props.direction;
    var iconName;

    if (direction === ASC) {
      iconName = 'chevron-down-small';
    }

    if (direction === DESC) {
      iconName = 'chevron-up-small';
    }

    if (iconName) {
      return React.createElement(Icon, {
        "aria-hidden": "true",
        name: iconName,
        className: "sorter-icon"
      });
    } else {
      return React.createElement(Icon, {
        "aria-hidden": "true",
        name: "chevron-down-small",
        className: "sorter-icon list-hover-arrow"
      });
    }
  };

  _proto.renderGridCellSorter = function renderGridCellSorter() {
    var _this$props2 = this.props,
        children = _this$props2.children,
        append = _this$props2.append,
        prepend = _this$props2.prepend,
        testAutomationId = _this$props2.testAutomationId,
        isGridCellSorter = _this$props2.isGridCellSorter,
        onChangeSorting = _this$props2.onChangeSorting,
        direction = _this$props2.direction,
        displayName = _this$props2.displayName,
        ascendingText = _this$props2.ascendingText,
        descendingText = _this$props2.descendingText,
        sorterTitleText = _this$props2.sorterTitleText,
        props = _objectWithoutPropertiesLoose(_this$props2, ["children", "append", "prepend", "testAutomationId", "isGridCellSorter", "onChangeSorting", "direction", "displayName", "ascendingText", "descendingText", "sorterTitleText"]);

    props.className = classnames(props.className, this.sorterClassName());
    return React.createElement("th", _extends({
      onClick: this.onClick,
      role: "columnheader",
      "aria-sort": this.getAriaSortName(),
      "data-test-automation-id": testAutomationId
    }, props), prepend, React.createElement("div", this.getAccessibilityHeaderProps(), this.renderSorterIcon(), append && React.createElement("span", {
      className: "column-header-append"
    }, append), React.createElement("div", {
      className: "text-overflow"
    }, children)));
  };

  _proto.render = function render() {
    var _this$props3 = this.props,
        isGridCellSorter = _this$props3.isGridCellSorter,
        children = _this$props3.children,
        append = _this$props3.append,
        prepend = _this$props3.prepend,
        props = _objectWithoutPropertiesLoose(_this$props3, ["isGridCellSorter", "children", "append", "prepend"]);

    if (isGridCellSorter) {
      return this.renderGridCellSorter();
    }

    props.className = classnames(props.className, this.sorterClassName());
    return React.createElement("div", _extends({
      onClick: this.onClick
    }, this.getAccessibilityHeaderProps(), props), prepend, this.renderSorterIcon(), append && React.createElement("span", {
      className: "column-header-append"
    }, append), React.createElement("div", null, children));
  };

  return CommonListSorter;
}(React.Component);

_defineProperty(CommonListSorter, "propTypes", {
  onChangeSorting: PropTypes.func.isRequired,
  direction: PropTypes.oneOf([null, ASC, DESC]).isRequired,
  children: PropTypes.node,
  append: PropTypes.node,
  prepend: PropTypes.node,
  property: PropTypes.string,
  displayName: PropTypes.string,
  ascendingText: PropTypes.string,
  descendingText: PropTypes.string,
  sorterTitleText: PropTypes.string,
  testAutomationId: PropTypes.string,
  isGridCellSorter: PropTypes.bool
});

_defineProperty(CommonListSorter, "defaultProps", {
  children: null,
  append: null,
  prepend: null,
  property: '',
  displayName: '',
  ascendingText: 'TRANSLATE:SORT_BY_ASCENDING',
  descendingText: 'TRANSLATE:SORT_BY_DESCENDING',
  sorterTitleText: 'TRANSLATE:CLICK_TO_SORT',
  testAutomationId: null,
  isGridCellSorter: false
});