var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var g = generator.apply(thisArg, _arguments || []), i, q = [];
    return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
    function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
    function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
    function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
    function fulfill(value) { resume("next", value); }
    function reject(value) { resume("throw", value); }
    function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
function arrayToAsyncIterable(array) {
    return __asyncGenerator(this, arguments, function* arrayToAsyncIterable_1() {
        for (const item of array) {
            yield yield __await(item);
        }
    });
}
function asyncIterableBatchProcessor(items, batchProcess, batchSize = 10) {
    var _a, items_1, items_1_1;
    var _b, e_1, _c, _d;
    return __awaiter(this, void 0, void 0, function* () {
        let batch = [];
        try {
            for (_a = true, items_1 = __asyncValues(items); items_1_1 = yield items_1.next(), _b = items_1_1.done, !_b;) {
                _d = items_1_1.value;
                _a = false;
                try {
                    const item = _d;
                    batch.push(item);
                    if (batch.length === batchSize) {
                        yield batchProcess(batch);
                        batch = [];
                    }
                }
                finally {
                    _a = true;
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (!_a && !_b && (_c = items_1.return)) yield _c.call(items_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        if (batch.length > 0) {
            yield batchProcess(batch);
        }
    });
}
function asyncIterableToArray(asyncIterable) {
    var _a, asyncIterable_1, asyncIterable_1_1;
    var _b, e_2, _c, _d;
    return __awaiter(this, void 0, void 0, function* () {
        const resultArray = [];
        try {
            for (_a = true, asyncIterable_1 = __asyncValues(asyncIterable); asyncIterable_1_1 = yield asyncIterable_1.next(), _b = asyncIterable_1_1.done, !_b;) {
                _d = asyncIterable_1_1.value;
                _a = false;
                try {
                    const item = _d;
                    resultArray.push(item);
                }
                finally {
                    _a = true;
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (!_a && !_b && (_c = asyncIterable_1.return)) yield _c.call(asyncIterable_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
        return resultArray;
    });
}
function createAsyncIterable(data) {
    let index = 0;
    return {
        [Symbol.asyncIterator]() {
            return {
                next() {
                    if (index < data.length) {
                        return Promise.resolve({ done: false, value: data[index++] });
                    }
                    return Promise.resolve({ done: true, value: undefined });
                },
            };
        },
    };
}
export { arrayToAsyncIterable, asyncIterableBatchProcessor, asyncIterableToArray, createAsyncIterable, };
//# sourceMappingURL=iterable.js.map