"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncIterUtilsExt = exports.AsyncIterUtils = void 0;
var AsyncIterUtils;
(function (AsyncIterUtils) {
    async function* map(iterable, mapItem) {
        for await (const item of iterable) {
            yield mapItem(item);
        }
    }
    AsyncIterUtils.map = map;
    async function* mapWithReturn(iterable, mapItem, mapReturn) {
        const iter = iterable[Symbol.asyncIterator]();
        let v;
        while (!((v = await iter.next()).done)) {
            yield mapItem(v.value);
        }
        return mapReturn(v.value);
    }
    AsyncIterUtils.mapWithReturn = mapWithReturn;
    async function* filter(iterable, filterItem) {
        for await (const item of iterable) {
            if (filterItem(item)) {
                yield item;
            }
        }
    }
    AsyncIterUtils.filter = filter;
    async function toArray(iterable) {
        const arr = [];
        for await (const item of iterable) {
            arr.push(item);
        }
        return arr;
    }
    AsyncIterUtils.toArray = toArray;
    async function* fromArray(arr) {
        for (const item of arr) {
            yield item;
        }
    }
    AsyncIterUtils.fromArray = fromArray;
    async function* fromArrayWithReturn(arr, returnValue) {
        for (const item of arr) {
            yield item;
        }
        return returnValue;
    }
    AsyncIterUtils.fromArrayWithReturn = fromArrayWithReturn;
    async function toArrayWithReturn(iterable) {
        const iter = iterable[Symbol.asyncIterator]();
        const arr = [];
        let v;
        while (!((v = await iter.next()).done)) {
            arr.push(v.value);
        }
        return [arr, v.value];
    }
    AsyncIterUtils.toArrayWithReturn = toArrayWithReturn;
    async function drainUntilReturn(iterable) {
        const iter = iterable[Symbol.asyncIterator]();
        let v;
        do {
            v = await iter.next();
        } while (!v.done);
        return v.value;
    }
    AsyncIterUtils.drainUntilReturn = drainUntilReturn;
})(AsyncIterUtils || (exports.AsyncIterUtils = AsyncIterUtils = {}));
/**
 * Namespace for extensions to AsyncIterUtils that are not generally useful enough to be in the main namespace, but are still worth keeping around.
 */
var AsyncIterUtilsExt;
(function (AsyncIterUtilsExt) {
    async function* splitLines(stream) {
        let buffer = null;
        for await (const chunk of stream) {
            buffer ??= '';
            buffer += chunk;
            const parts = buffer.split(/\r?\n/);
            buffer = parts.pop() ?? '';
            yield* parts;
        }
        if (buffer !== null) {
            yield buffer;
        }
    }
    AsyncIterUtilsExt.splitLines = splitLines;
})(AsyncIterUtilsExt || (exports.AsyncIterUtilsExt = AsyncIterUtilsExt = {}));
//# sourceMappingURL=asyncIterableUtils.js.map