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';
export var ASC = 'ASC';
export var DESC = 'DESC';
var sortingType = PropTypes.shape({
  field: PropTypes.string,
  direction: PropTypes.oneOf([ASC, DESC]).isRequired
});
export var passedWithListPropsTypes = {
  pageSize: PropTypes.number.isRequired,
  activePage: PropTypes.number.isRequired,
  onChangePage: PropTypes.func.isRequired,
  onChangePageSize: PropTypes.func.isRequired,
  fieldsParams: PropTypes.object.isRequired,
  onUpdateFieldParams: PropTypes.func.isRequired,
  onResetFieldParams: PropTypes.func.isRequired,
  filterText: PropTypes.string.isRequired,
  onChangeFilterText: PropTypes.func.isRequired,
  sorting: sortingType,
  onChangeSorting: PropTypes.func.isRequired
};
export var withListProps = function withListProps(Component) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_React$PureComponent) {
    _inheritsLoose(WithListProps, _React$PureComponent);

    function WithListProps() {
      var _this;

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

      _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;

      _defineProperty(_assertThisInitialized(_this), "state", {
        pageSize: _this.props.defaultPageSize,
        activePage: _this.props.defaultActivePage,
        filterText: _this.props.defaultFilterText,
        fieldsParams: _this.props.defaultFieldsParams,
        sorting: _this.props.defaultSorting
      });

      _defineProperty(_assertThisInitialized(_this), "handleUpdateFieldParams", function (_ref) {
        var field = _ref.field,
            params = _ref.params;
        var fieldsParams = Object.assign({}, _this.state.fieldsParams);
        fieldsParams[field] = params;

        _this.setState({
          fieldsParams: fieldsParams,
          activePage: 0
        });
      });

      _defineProperty(_assertThisInitialized(_this), "handleResetFieldParams", function (field) {
        var fieldsParams = Object.assign({}, _this.state.fieldsParams);
        delete fieldsParams[field];

        _this.setState({
          fieldsParams: fieldsParams,
          activePage: 0
        });
      });

      _defineProperty(_assertThisInitialized(_this), "handleChangeSorting", function (field, direction) {
        var sorting = {
          field: field,
          direction: direction
        };

        _this.setState({
          sorting: sorting
        });
      });

      _defineProperty(_assertThisInitialized(_this), "getListProps", function () {
        return Object.assign({}, _this.getFilterTextProps(), _this.getPaginationProps(), _this.getFieldsParamsProps(), _this.getSortingProps());
      });

      return _this;
    }

    var _proto = WithListProps.prototype;

    _proto.getPaginationProps = function getPaginationProps() {
      var _this2 = this;

      var _this$state = this.state,
          pageSize = _this$state.pageSize,
          activePage = _this$state.activePage;
      return {
        pageSize: pageSize,
        activePage: activePage,
        onChangePage: function onChangePage(activePage) {
          _this2.setState({
            activePage: activePage
          });
        },
        onChangePageSize: function onChangePageSize(pageSize) {
          _this2.setState({
            pageSize: pageSize,
            activePage: 0
          });
        }
      };
    };

    _proto.getFieldsParamsProps = function getFieldsParamsProps() {
      var fieldsParams = this.state.fieldsParams;
      return {
        fieldsParams: fieldsParams,
        onUpdateFieldParams: this.handleUpdateFieldParams,
        onResetFieldParams: this.handleResetFieldParams
      };
    };

    _proto.getFilterTextProps = function getFilterTextProps() {
      var _this3 = this;

      var filterText = this.state.filterText;
      return {
        filterText: filterText,
        onChangeFilterText: function onChangeFilterText(filterText) {
          _this3.setState({
            filterText: filterText,
            activePage: 0
          });
        }
      };
    };

    _proto.getSortingProps = function getSortingProps() {
      var sorting = this.state.sorting;
      return {
        sorting: sorting,
        onChangeSorting: this.handleChangeSorting
      };
    };

    _proto.render = function render() {
      return React.createElement(Component, _extends({}, this.getListProps(), this.props));
    };

    return WithListProps;
  }(React.PureComponent), _defineProperty(_class, "displayName", "WithListProps(" + (Component.displayName || Component.name || 'Component') + ")"), _defineProperty(_class, "propTypes", {
    defaultPageSize: PropTypes.number,
    defaultActivePage: PropTypes.number,
    defaultFilterText: PropTypes.string,
    defaultFieldsParams: PropTypes.object,
    defaultSorting: sortingType
  }), _defineProperty(_class, "defaultProps", {
    defaultPageSize: 10,
    defaultActivePage: 0,
    defaultFilterText: '',
    defaultFieldsParams: {},
    defaultSorting: {
      field: null,
      direction: ASC
    }
  }), _temp;
};