"use strict";

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

exports.__esModule = true;
exports.withListProps = exports.passedWithListPropsTypes = exports.DESC = exports.ASC = 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 ASC = 'ASC';
exports.ASC = ASC;
var DESC = 'DESC';
exports.DESC = DESC;

var sortingType = _propTypes.default.shape({
  field: _propTypes.default.string,
  direction: _propTypes.default.oneOf([ASC, DESC]).isRequired
});

var passedWithListPropsTypes = {
  pageSize: _propTypes.default.number.isRequired,
  activePage: _propTypes.default.number.isRequired,
  onChangePage: _propTypes.default.func.isRequired,
  onChangePageSize: _propTypes.default.func.isRequired,
  fieldsParams: _propTypes.default.object.isRequired,
  onUpdateFieldParams: _propTypes.default.func.isRequired,
  onResetFieldParams: _propTypes.default.func.isRequired,
  filterText: _propTypes.default.string.isRequired,
  onChangeFilterText: _propTypes.default.func.isRequired,
  sorting: sortingType,
  onChangeSorting: _propTypes.default.func.isRequired
};
exports.passedWithListPropsTypes = passedWithListPropsTypes;

var withListProps = function withListProps(Component) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_React$PureComponent) {
    (0, _inheritsLoose2.default)(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;
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
        pageSize: _this.props.defaultPageSize,
        activePage: _this.props.defaultActivePage,
        filterText: _this.props.defaultFilterText,
        fieldsParams: _this.props.defaultFieldsParams,
        sorting: _this.props.defaultSorting
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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
        });
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleResetFieldParams", function (field) {
        var fieldsParams = Object.assign({}, _this.state.fieldsParams);
        delete fieldsParams[field];

        _this.setState({
          fieldsParams: fieldsParams,
          activePage: 0
        });
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleChangeSorting", function (field, direction) {
        var sorting = {
          field: field,
          direction: direction
        };

        _this.setState({
          sorting: sorting
        });
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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.default.createElement(Component, (0, _extends2.default)({}, this.getListProps(), this.props));
    };

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

exports.withListProps = withListProps;