"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.CommonListSorter = exports.KEY_SPACE = exports.KEY_ENTER = exports.DESC = exports.ASC = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

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

var _Icon = require("../../Icon");

var _functions = require("../../functions");

var ASC = 'ASC';
exports.ASC = ASC;
var DESC = 'DESC';
exports.DESC = DESC;
var KEY_ENTER = 13;
exports.KEY_ENTER = KEY_ENTER;
var KEY_SPACE = 32;
exports.KEY_SPACE = KEY_SPACE;

var CommonListSorter =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(CommonListSorter, _React$Component);

  function CommonListSorter(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleKeyDown", function (e) {
      if (e.keyCode === KEY_ENTER || e.keyCode === KEY_SPACE) {
        _this.onClick();

        e.preventDefault();
      }
    });
    _this.onClick = _this.onClick.bind((0, _assertThisInitialized2.default)(_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);
    (0, _functions.playMessage)(shouldReverse ? descendingText.replace('{name}', displayName || children || property) : ascendingText.replace('{name}', displayName || children || property));
  };

  _proto.sorterClassName = function sorterClassName() {
    var direction = this.props.direction;
    return (0, _classnames.default)('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.default.createElement(_Icon.Icon, {
        "aria-hidden": "true",
        name: iconName,
        className: "sorter-icon"
      });
    } else {
      return _react.default.createElement(_Icon.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 = (0, _objectWithoutPropertiesLoose2.default)(_this$props2, ["children", "append", "prepend", "testAutomationId", "isGridCellSorter", "onChangeSorting", "direction", "displayName", "ascendingText", "descendingText", "sorterTitleText"]);
    props.className = (0, _classnames.default)(props.className, this.sorterClassName());
    return _react.default.createElement("th", (0, _extends2.default)({
      onClick: this.onClick,
      role: "columnheader",
      "aria-sort": this.getAriaSortName(),
      "data-test-automation-id": testAutomationId
    }, props), prepend, _react.default.createElement("div", this.getAccessibilityHeaderProps(), this.renderSorterIcon(), append && _react.default.createElement("span", {
      className: "column-header-append"
    }, append), _react.default.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 = (0, _objectWithoutPropertiesLoose2.default)(_this$props3, ["isGridCellSorter", "children", "append", "prepend"]);

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

    props.className = (0, _classnames.default)(props.className, this.sorterClassName());
    return _react.default.createElement("div", (0, _extends2.default)({
      onClick: this.onClick
    }, this.getAccessibilityHeaderProps(), props), prepend, this.renderSorterIcon(), append && _react.default.createElement("span", {
      className: "column-header-append"
    }, append), _react.default.createElement("div", null, children));
  };

  return CommonListSorter;
}(_react.default.Component);

exports.CommonListSorter = CommonListSorter;
(0, _defineProperty2.default)(CommonListSorter, "propTypes", {
  onChangeSorting: _propTypes.default.func.isRequired,
  direction: _propTypes.default.oneOf([null, ASC, DESC]).isRequired,
  children: _propTypes.default.node,
  append: _propTypes.default.node,
  prepend: _propTypes.default.node,
  property: _propTypes.default.string,
  displayName: _propTypes.default.string,
  ascendingText: _propTypes.default.string,
  descendingText: _propTypes.default.string,
  sorterTitleText: _propTypes.default.string,
  testAutomationId: _propTypes.default.string,
  isGridCellSorter: _propTypes.default.bool
});
(0, _defineProperty2.default)(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
});