"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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
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"));
var lodash_1 = __importDefault(require("lodash"));
var withListProps_1 = require("./HOCs/withListProps");
exports.passedListManagerPropsTypes = __assign({}, withListProps_1.passedWithListPropsTypes, { filteredRows: prop_types_1.default.array.isRequired, visibleRows: prop_types_1.default.array.isRequired });
// TODO add catch errors (fieldsRules do not have real ids)
var ListManagerInner = /** @class */ (function (_super) {
    __extends(ListManagerInner, _super);
    function ListManagerInner() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ListManagerInner.prototype.defaultFilterByText = function (value, filterText) {
        value = String(value).toLowerCase();
        return value.includes(filterText);
    };
    ListManagerInner.prototype.getFilterByText = function (field) {
        var customFilterByText = field.filterByText;
        if (lodash_1.default.isFunction(customFilterByText)) {
            return customFilterByText;
        }
        return this.defaultFilterByText;
    };
    ListManagerInner.prototype.defaultSort = function (prevValue, nextValue) {
        if (lodash_1.default.isString(prevValue)) {
            prevValue = prevValue.toLowerCase();
        }
        if (lodash_1.default.isString(nextValue)) {
            nextValue = nextValue.toLowerCase();
        }
        if (prevValue === nextValue) {
            return 0;
        }
        return prevValue >= nextValue ? 1 : -1;
    };
    ListManagerInner.prototype.getSort = function (field) {
        var customSort = field.sort;
        if (lodash_1.default.isFunction(customSort)) {
            return customSort;
        }
        return this.defaultSort;
    };
    ListManagerInner.prototype.getRowsByFilterText = function (rows, filterText, fieldsRules) {
        var _this = this;
        var fieldsRulesKeys = Object.keys(fieldsRules);
        if (fieldsRulesKeys.length === 0) {
            return rows;
        }
        filterText = filterText.toLowerCase();
        return rows.filter(function (row) {
            return fieldsRulesKeys
                .filter(function (fieldName) { return fieldsRules[fieldName].isFiltering === true; })
                .some(function (fieldName) {
                var filterByText = _this.getFilterByText(fieldsRules[fieldName]);
                return filterByText(row[fieldName], filterText);
            });
        });
    };
    ListManagerInner.prototype.getRowsByFieldParams = function (rows, fieldsParams, fieldsRules) {
        return rows.filter(function (row) {
            return Object.keys(fieldsParams).every(function (fieldName) {
                var filteredValue = row[fieldName];
                var fieldParams = fieldsParams[fieldName];
                var filterByParams = fieldsRules[fieldName].filterByParams;
                return filterByParams(filteredValue, fieldParams);
            });
        });
    };
    ListManagerInner.prototype.getRowsByPagination = function (rows, activePage, pageSize) {
        var beginIndex = activePage * pageSize;
        return rows.slice(beginIndex, beginIndex + pageSize);
    };
    ListManagerInner.prototype.getRowsBySorting = function (rows, sorting, fieldsRules) {
        var field = sorting.field, direction = sorting.direction;
        if (!lodash_1.default.isString(field)) {
            return rows;
        }
        var sort = this.getSort(fieldsRules[field]);
        return rows.sort(function (preRow, nextRow) {
            return sort(preRow[field], nextRow[field]) * (direction === withListProps_1.ASC ? 1 : -1);
        });
    };
    ListManagerInner.prototype.getProps = function () {
        var _a = this.props, rows = _a.rows, activePage = _a.activePage, pageSize = _a.pageSize, filterText = _a.filterText, fieldsRules = _a.fieldsRules, fieldsParams = _a.fieldsParams, sorting = _a.sorting;
        var filteredRows = this.getRowsByFieldParams(rows, fieldsParams, fieldsRules);
        filteredRows = this.getRowsByFilterText(filteredRows, filterText, fieldsRules);
        var sortedFilteredRows = this.getRowsBySorting(filteredRows, sorting, fieldsRules);
        var visibleRows = this.getRowsByPagination(sortedFilteredRows, activePage, pageSize);
        var _b = this.props, children = _b.children, _fieldsRules = _b.fieldsRules, _rows = _b.rows, props = __rest(_b, ["children", "fieldsRules", "rows"]);
        return __assign({ filteredRows: filteredRows,
            visibleRows: visibleRows }, props);
    };
    ListManagerInner.prototype.render = function () {
        return react_1.default.cloneElement(this.props.children, __assign({}, this.getProps()));
    };
    ListManagerInner.propTypes = __assign({}, withListProps_1.passedWithListPropsTypes, { rows: prop_types_1.default.array.isRequired, children: prop_types_1.default.element.isRequired, fieldsRules: prop_types_1.default.shape({
            isFiltering: prop_types_1.default.bool,
            filterByText: prop_types_1.default.func,
            filterByParams: prop_types_1.default.func,
            sort: prop_types_1.default.func
        }) });
    ListManagerInner.defaultProps = {
        fieldsRules: {}
    };
    return ListManagerInner;
}(react_1.default.PureComponent));
exports.ListManager = withListProps_1.withListProps(ListManagerInner);
//# sourceMappingURL=ListManager.js.map