"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isUint8Array = exports.isArrayOf = exports.includesAsync = exports.everyAsync = exports.someAsync = exports.forEachAsync = exports.reduceAsync = exports.mapAsync = exports.filterAsync = exports.findLast = exports.findLastIndex = exports.chunk = exports.dedupArray = void 0;
var tslib_1 = require("tslib");
function dedupArray(array) {
    return Array.from(new Set(array));
}
exports.dedupArray = dedupArray;
function chunk(array, chunkSize) {
    var chunks = [];
    for (var i = 0; i < array.length; i += chunkSize) {
        chunks.push(array.slice(i, i + chunkSize));
    }
    return chunks;
}
exports.chunk = chunk;
function findLastIndex(array, predicate) {
    for (var i = array.length - 1; i >= 0; i--) {
        if (predicate(array[i], i, array)) {
            return i;
        }
    }
    return -1;
}
exports.findLastIndex = findLastIndex;
function findLast(array, predicate) {
    var index = findLastIndex(array, predicate);
    return index >= 0 ? array[index] : undefined;
}
exports.findLast = findLast;
function filterAsync(array, predicate) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var results;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, Promise.all(array.map(predicate))];
                case 1:
                    results = _a.sent();
                    return [2, array.filter(function (_, index) { return results[index]; })];
            }
        });
    });
}
exports.filterAsync = filterAsync;
function mapAsync(array, mapper) {
    return Promise.all(array.map(mapper));
}
exports.mapAsync = mapAsync;
function reduceAsync(array, reducer, initialValue) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var accumulator, i;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    accumulator = initialValue;
                    i = 0;
                    _a.label = 1;
                case 1:
                    if (!(i < array.length)) return [3, 4];
                    return [4, reducer(accumulator, array[i], i, array)];
                case 2:
                    accumulator = _a.sent();
                    _a.label = 3;
                case 3:
                    i++;
                    return [3, 1];
                case 4: return [2, accumulator];
            }
        });
    });
}
exports.reduceAsync = reduceAsync;
function forEachAsync(array, callback) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, mapAsync(array, callback)];
                case 1:
                    _a.sent();
                    return [2];
            }
        });
    });
}
exports.forEachAsync = forEachAsync;
function someAsync(array, predicate) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var results;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, mapAsync(array, predicate)];
                case 1:
                    results = _a.sent();
                    return [2, results.some(function (value) { return value; })];
            }
        });
    });
}
exports.someAsync = someAsync;
function everyAsync(array, predicate) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var _this = this;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, someAsync(array, function (value, index, obj) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
                        switch (_a.label) {
                            case 0: return [4, predicate(value, index, obj)];
                            case 1: return [2, !(_a.sent())];
                        }
                    }); }); })];
                case 1: return [2, !(_a.sent())];
            }
        });
    });
}
exports.everyAsync = everyAsync;
function includesAsync(array, predicate) {
    return someAsync(array, predicate);
}
exports.includesAsync = includesAsync;
function isArrayOf(array, predicate) {
    return Array.isArray(array) && array.every(predicate);
}
exports.isArrayOf = isArrayOf;
function isUint8Array(value) {
    return (Array.isArray(value) &&
        value.length > 0 &&
        value.every(function (item) { return typeof item === "number" && Number.isInteger(item) && item >= 0 && item < 256; }));
}
exports.isUint8Array = isUint8Array;
//# sourceMappingURL=ArrayUtils.js.map