"use strict";

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

exports.__esModule = true;
exports.ListSorter = void 0;

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

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 _CommonListSorter = require("../common/CommonListSorter");

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

  function ListSorter() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleChangeSorting", function (property, direction) {
      _this.context.listStore.sortBy(property, direction);
    });
    return _this;
  }

  var _proto = ListSorter.prototype;

  _proto.getDirection = function getDirection() {
    var sorting = this.context.listStore.getSorting();

    if (sorting.fieldId !== this.props.property) {
      return null;
    }

    return sorting.direction;
  };

  _proto.render = function render() {
    return _react.default.createElement(_CommonListSorter.CommonListSorter, (0, _extends2.default)({}, this.props, {
      onChangeSorting: this.handleChangeSorting,
      direction: this.getDirection()
    }));
  };

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

exports.ListSorter = ListSorter;
(0, _defineProperty2.default)(ListSorter, "propTypes", {
  property: _propTypes.default.string,
  append: _propTypes.default.node,
  prepend: _propTypes.default.node
});
(0, _defineProperty2.default)(ListSorter, "defaultProps", {
  property: '',
  append: null,
  prepend: null
});
(0, _defineProperty2.default)(ListSorter, "contextTypes", {
  listStore: _propTypes.default.object
});