"use strict";
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
exports.ASC = 'ASC';
exports.DESC = 'DESC';
var sortingType = prop_types_1.default.shape({
    field: prop_types_1.default.string,
    direction: prop_types_1.default.oneOf([exports.ASC, exports.DESC]).isRequired
});
exports.passedWithListPropsTypes = {
    pageSize: prop_types_1.default.number.isRequired,
    activePage: prop_types_1.default.number.isRequired,
    onChangePage: prop_types_1.default.func.isRequired,
    onChangePageSize: prop_types_1.default.func.isRequired,
    fieldsParams: prop_types_1.default.object.isRequired,
    onUpdateFieldParams: prop_types_1.default.func.isRequired,
    onResetFieldParams: prop_types_1.default.func.isRequired,
    filterText: prop_types_1.default.string.isRequired,
    onChangeFilterText: prop_types_1.default.func.isRequired,
    sorting: sortingType,
    onChangeSorting: prop_types_1.default.func.isRequired
};
exports.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_1.default.createElement(Component, __assign({}, this.getListProps(), this.props));
            };
            return WithListProps;
        }(react_1.default.PureComponent)),
        _a.displayName = "WithListProps(" + (Component.displayName ||
            Component.name ||
            'Component') + ")",
        _a.propTypes = {
            defaultPageSize: prop_types_1.default.number,
            defaultActivePage: prop_types_1.default.number,
            defaultFilterText: prop_types_1.default.string,
            defaultFieldsParams: prop_types_1.default.object,
            defaultSorting: sortingType
        },
        _a.defaultProps = {
            defaultPageSize: 10,
            defaultActivePage: 0,
            defaultFilterText: '',
            defaultFieldsParams: {},
            defaultSorting: {
                field: null,
                direction: exports.ASC
            }
        },
        _a;
};
//# sourceMappingURL=withListProps.js.map