"use strict";
var rxjs_1 = require("rxjs");
var lodash_1 = require("lodash");
var dataSourceProcessor_1 = require("./processor/dataSourceProcessor");
var DataSourceBase = (function () {
    function DataSourceBase() {
        this.countFilterGroups = false;
        this._dataSet = new rxjs_1.BehaviorSubject(null);
        this._filteredDataSet = new rxjs_1.BehaviorSubject(null);
        this._rawDataSet = new rxjs_1.BehaviorSubject(null);
        this._count = new rxjs_1.BehaviorSubject(0);
        this._isEmpty = new rxjs_1.BehaviorSubject(null);
        this._loadingDataSet = new rxjs_1.BehaviorSubject(false);
    }
    Object.defineProperty(DataSourceBase.prototype, "dataSet$", {
        get: function () {
            return this._dataSet.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceBase.prototype, "filteredDataSet$", {
        get: function () {
            return this._filteredDataSet.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceBase.prototype, "rawDataSet$", {
        get: function () {
            return this._rawDataSet.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceBase.prototype, "count$", {
        get: function () {
            return this._count.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceBase.prototype, "loadingDataSet$", {
        get: function () {
            return this._loadingDataSet.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceBase.prototype, "needsRefinedSearch$", {
        get: function () {
            return this.dataSet$.combineLatest(this.rawDataSet$, this.count$, this._isEmpty)
                .map(function (_a) {
                var dataSet = _a[0], rawDataSet = _a[1], count = _a[2], isEmpty = _a[3];
                var noItemsDisplayed = !(dataSet && dataSet.length);
                var moreItemsOnServer = isEmpty === false || (rawDataSet != null && rawDataSet.length < count);
                return noItemsDisplayed && moreItemsOnServer;
            });
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceBase.prototype, "isEmpty$", {
        get: function () {
            return this.rawDataSet$.combineLatest(this._isEmpty)
                .map(function (_a) {
                var rawDataSet = _a[0], isEmpty = _a[1];
                return !(rawDataSet && rawDataSet.length)
                    && (isEmpty != null ? isEmpty : true);
            });
        },
        enumerable: true,
        configurable: true
    });
    DataSourceBase.prototype.init = function () {
        // override with any init logic in children
    };
    DataSourceBase.prototype.clear = function () {
        this._rawDataSet.next([]);
        this._dataSet.next([]);
        this._filteredDataSet.next([]);
        this._count.next(0);
        this._isEmpty.next(true);
    };
    DataSourceBase.prototype.processData = function () {
        var processedData;
        // if (this.countFilterGroups) {
        // processedData = this.dataSourceProcessor.processAndCount<TDataType>(this.sorts
        // 																, <filters.IFilterWithCounts[]>this.filters
        // 																, this.pager
        // 																, this.rawDataSet);
        // } else {
        processedData = dataSourceProcessor_1.process(this.sorter, this.filters, this.pager, this.rawDataSet$);
        // }
        this.setProcessedData(processedData);
    };
    //used when we need to process data but without client filters.
    DataSourceBase.prototype.processDataNoClientFilters = function () {
        var processedData;
        if (this.countFilterGroups) {
        }
        else {
            processedData = dataSourceProcessor_1.process(this.sorter, null, this.pager, this.rawDataSet$);
        }
        this.setProcessedData(processedData);
    };
    DataSourceBase.prototype.setProcessedData = function (processedData) {
        var _this = this;
        var countSubscription = processedData.count$.subscribe(function (count) { return _this._count.next(count); });
        var dataSetSubscription = processedData.dataSet$.subscribe(function (dataSet) { return _this._dataSet.next(dataSet); });
        var filteredDataSetSubscription = processedData.filteredDataSet$.subscribe(function (filteredDataSet) { return _this._filteredDataSet.next(filteredDataSet); });
        this.subscription = countSubscription.add(dataSetSubscription)
            .add(filteredDataSetSubscription);
    };
    DataSourceBase.prototype.add = function (data) {
        var updatedDataSet = this._rawDataSet.getValue().concat([data]);
        this._rawDataSet.next(updatedDataSet);
    };
    DataSourceBase.prototype.remove = function (data) {
        var updatedDataSet = lodash_1.without(this._rawDataSet.getValue(), data);
        this._rawDataSet.next(updatedDataSet);
    };
    DataSourceBase.prototype.replace = function (oldData, newData) {
        var updatedDataSet = lodash_1.map(this._rawDataSet.getValue(), function (item) {
            return item === oldData
                ? newData
                : item;
        });
        this._rawDataSet.next(updatedDataSet);
    };
    return DataSourceBase;
}());
exports.DataSourceBase = DataSourceBase;
//# sourceMappingURL=data:application/json;base64, 