"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var _ = require("lodash");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __object = typescript_angular_utilities_1.services.object;
var sorter_service_1 = require("../../sorts/sorter/sorter.service");
var DataSourceProcessorOld = (function () {
    function DataSourceProcessorOld(object, sorter) {
        this.object = object;
        this.sorter = sorter;
    }
    DataSourceProcessorOld.prototype.process = function (sorts, filters, pager, data) {
        var processedData = data;
        processedData = this.sort(processedData, sorts);
        if (this.object.isNullOrEmpty(filters) === false) {
            processedData = _.reduce(filters, function (filteredData, filter) {
                // Filter the data set using the filter function on the filter
                return _.filter(filteredData, filter.filter.bind(filter));
            }, processedData);
        }
        var result = {
            count: (processedData != null ? processedData.length : 0),
            filteredDataSet: processedData,
            dataSet: processedData,
        };
        result.dataSet = this.page(processedData, pager);
        return result;
    };
    DataSourceProcessorOld.prototype.processAndCount = function (sorts, filters, pager, data) {
        var _this = this;
        // If there are no filters that need to updated option counts, use the normal processor
        if (this.object.isNullOrEmpty(filters)
            || _.some(filters, function (filter) { return _.isFunction(filter.updateOptionCounts); }) === false) {
            return this.process(sorts, filters, pager, data);
        }
        var processedData = data;
        processedData = this.sort(processedData, sorts);
        var wrappedData = this.wrapData(processedData);
        // Run filtration logic and compute visible items
        _.each(filters, function (filter /* filters.IFilterWithCounts */) {
            _.each(wrappedData, function (item) {
                item.filterData[filter.type] = filter.filter(item.data);
            });
        });
        // Give each filter a chance to update option counts
        _.each(filters, function (filter /* filters.IFilterWithCounts */) {
            if (_.isFunction(filter.updateOptionCounts)) {
                var otherFiltersApplied = _.filter(wrappedData, function (item) {
                    // Omit the true or false of the current filter an
                    //  only filter out items removed by other filters
                    var filterData = _.omit(item.filterData, filter.type); //*filterData
                    return _.every(_.values(filterData));
                });
                filter.updateOptionCounts(_this.unwrapData(otherFiltersApplied));
            }
        });
        // Filter down to final data set by removing items that don't match all filters
        wrappedData = _.filter(wrappedData, function (item) {
            return _.every(_.values(item.filterData));
        });
        processedData = this.unwrapData(wrappedData);
        var result = {
            count: processedData.length,
            filteredDataSet: processedData,
            dataSet: processedData,
        };
        result.dataSet = this.page(processedData, pager);
        return result;
    };
    DataSourceProcessorOld.prototype.sort = function (data, sorts) {
        if (this.object.isNullOrEmpty(sorts) === false) {
            return this.sorter.sort(data, sorts);
        }
        return data;
    };
    DataSourceProcessorOld.prototype.page = function (data, pager) {
        if (pager != null) {
            return pager.filter(data);
        }
        return data;
    };
    DataSourceProcessorOld.prototype.wrapData = function (data) {
        return _.map(data, function (item) {
            return {
                data: item,
                filterData: {},
            };
        });
    };
    DataSourceProcessorOld.prototype.unwrapData = function (data) {
        return _.map(data, function (item) {
            return item.data;
        });
    };
    return DataSourceProcessorOld;
}());
DataSourceProcessorOld = __decorate([
    core_1.Injectable(),
    __metadata("design:paramtypes", [__object.ObjectUtility, sorter_service_1.Sorter])
], DataSourceProcessorOld);
exports.DataSourceProcessorOld = DataSourceProcessorOld;
//# sourceMappingURL=data:application/json;base64, 