import * as tslib_1 from "tslib";
import { findIndex } from '@dojo/framework/shim/array';
export function sorter(data, { sort }) {
    const temp = [...data];
    if (sort) {
        temp.sort((a, b) => {
            const left = sort.direction === 'asc' ? b : a;
            const 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;
}
export function filterer(data, { filter }) {
    if (filter) {
        return data.filter((item) => {
            let match = true;
            Object.keys(filter).forEach((columnId) => {
                if (item[columnId].toLowerCase().indexOf(filter[columnId].toLowerCase()) === -1) {
                    match = false;
                }
            });
            return match;
        });
    }
    return [...data];
}
export function createFetcherResult(data, page, pageSize) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const temp = [...data];
        const block = [...temp].splice((page - 1) * pageSize, pageSize);
        return { data: block, meta: { total: temp.length } };
    });
}
export function createUpdater(data, idColumn = 'id') {
    return (updatedItem) => {
        const index = findIndex(data, (item) => {
            return item[idColumn] === updatedItem[idColumn];
        });
        data[index] = updatedItem;
    };
}
export function createFetcher(data) {
    return (page, pageSize, options = {}) => tslib_1.__awaiter(this, void 0, void 0, function* () {
        return createFetcherResult(sorter(filterer(data, options), options), page, pageSize);
    });
}

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