"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var array_1 = require("@dojo/framework/shim/array");
function sorter(data, _a) {
    var sort = _a.sort;
    var temp = tslib_1.__spread(data);
    if (sort) {
        temp.sort(function (a, b) {
            var left = sort.direction === 'asc' ? b : a;
            var right = sort.direction === 'asc' ? a : b;
            if (left[sort.columnId] < right[sort.columnId]) {
                return 1;
            }
            if (left[sort.columnId] > right[sort.columnId]) {
                return -1;
            }
            return 0;
        });
    }
    return temp;
}
exports.sorter = sorter;
function filterer(data, _a) {
    var filter = _a.filter;
    if (filter) {
        return data.filter(function (item) {
            var match = true;
            Object.keys(filter).forEach(function (columnId) {
                if (item[columnId].toLowerCase().indexOf(filter[columnId].toLowerCase()) === -1) {
                    match = false;
                }
            });
            return match;
        });
    }
    return tslib_1.__spread(data);
}
exports.filterer = filterer;
function createFetcherResult(data, page, pageSize) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var temp, block;
        return tslib_1.__generator(this, function (_a) {
            temp = tslib_1.__spread(data);
            block = tslib_1.__spread(temp).splice((page - 1) * pageSize, pageSize);
            return [2 /*return*/, { data: block, meta: { total: temp.length } }];
        });
    });
}
exports.createFetcherResult = createFetcherResult;
function createUpdater(data, idColumn) {
    if (idColumn === void 0) { idColumn = 'id'; }
    return function (updatedItem) {
        var index = array_1.findIndex(data, function (item) {
            return item[idColumn] === updatedItem[idColumn];
        });
        data[index] = updatedItem;
    };
}
exports.createUpdater = createUpdater;
function createFetcher(data) {
    var _this = this;
    return function (page, pageSize, options) {
        if (options === void 0) { options = {}; }
        return tslib_1.__awaiter(_this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                return [2 /*return*/, createFetcherResult(sorter(filterer(data, options), options), page, pageSize)];
            });
        });
    };
}
exports.createFetcher = createFetcher;

/*# sourceMappingURL=utils.js.map*/