import _extends from "@babel/runtime/helpers/extends";
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 { CommonListSorter } from '../common/CommonListSorter';
export var ListSorter =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_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.createElement(CommonListSorter, _extends({}, this.props, {
      onChangeSorting: this.handleChangeSorting,
      direction: this.getDirection()
    }));
  };

  return ListSorter;
}(React.Component);

_defineProperty(ListSorter, "propTypes", {
  property: PropTypes.string,
  append: PropTypes.node,
  prepend: PropTypes.node
});

_defineProperty(ListSorter, "defaultProps", {
  property: '',
  append: null,
  prepend: null
});

_defineProperty(ListSorter, "contextTypes", {
  listStore: PropTypes.object
});