var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 (Component) {
    var _a;
    return _a = /** @class */ (function (_super) {
            __extends(WithListProps, _super);
            function WithListProps() {
                var _this = _super !== null && _super.apply(this, arguments) || this;
                _this.state = {
                    pageSize: _this.props.defaultPageSize,
                    activePage: _this.props.defaultActivePage,
                    filterText: _this.props.defaultFilterText,
                    fieldsParams: _this.props.defaultFieldsParams,
                    sorting: _this.props.defaultSorting
                };
                _this.handleUpdateFieldParams = function (_a) {
                    var field = _a.field, params = _a.params;
                    var fieldsParams = __assign({}, _this.state.fieldsParams);
                    fieldsParams[field] = params;
                    _this.setState({ fieldsParams: fieldsParams, activePage: 0 });
                };
                _this.handleResetFieldParams = function (field) {
                    var fieldsParams = __assign({}, _this.state.fieldsParams);
                    delete fieldsParams[field];
                    _this.setState({ fieldsParams: fieldsParams, activePage: 0 });
                };
                _this.handleChangeSorting = function (field, direction) {
                    var sorting = { field: field, direction: direction };
                    _this.setState({ sorting: sorting });
                };
                _this.getListProps = function () {
                    return __assign({}, _this.getFilterTextProps(), _this.getPaginationProps(), _this.getFieldsParamsProps(), _this.getSortingProps());
                };
                return _this;
            }
            WithListProps.prototype.getPaginationProps = function () {
                var _this = this;
                var _a = this.state, pageSize = _a.pageSize, activePage = _a.activePage;
                return {
                    pageSize: pageSize,
                    activePage: activePage,
                    onChangePage: function (activePage) {
                        _this.setState({ activePage: activePage });
                    },
                    onChangePageSize: function (pageSize) {
                        _this.setState({ pageSize: pageSize, activePage: 0 });
                    }
                };
            };
            WithListProps.prototype.getFieldsParamsProps = function () {
                var fieldsParams = this.state.fieldsParams;
                return {
                    fieldsParams: fieldsParams,
                    onUpdateFieldParams: this.handleUpdateFieldParams,
                    onResetFieldParams: this.handleResetFieldParams
                };
            };
            WithListProps.prototype.getFilterTextProps = function () {
                var _this = this;
                var filterText = this.state.filterText;
                return {
                    filterText: filterText,
                    onChangeFilterText: function (filterText) {
                        _this.setState({ filterText: filterText, activePage: 0 });
                    }
                };
            };
            WithListProps.prototype.getSortingProps = function () {
                var sorting = this.state.sorting;
                return {
                    sorting: sorting,
                    onChangeSorting: this.handleChangeSorting
                };
            };
            WithListProps.prototype.render = function () {
                return React.createElement(Component, __assign({}, this.getListProps(), this.props));
            };
            return WithListProps;
        }(React.PureComponent)),
        _a.displayName = "WithListProps(" + (Component.displayName ||
            Component.name ||
            'Component') + ")",
        _a.propTypes = {
            defaultPageSize: PropTypes.number,
            defaultActivePage: PropTypes.number,
            defaultFilterText: PropTypes.string,
            defaultFieldsParams: PropTypes.object,
            defaultSorting: sortingType
        },
        _a.defaultProps = {
            defaultPageSize: 10,
            defaultActivePage: 0,
            defaultFilterText: '',
            defaultFieldsParams: {},
            defaultSorting: {
                field: null,
                direction: ASC
            }
        },
        _a;
};
//# sourceMappingURL=withListProps.js.map