/**
 * Stream Utilities
 *
 * Small, cross-platform helpers built on top of the platform stream
 * implementation selected by `./streams` (Node.js or browser).
 */
import { consumers, createReadableFromArray, createTransform } from "./streams.browser.js";
import { stringToUint8Array as _stringToUint8Array } from "./shared.js";
// =============================================================================
// High-Level Stream Consumers
// =============================================================================
export async function collect(stream) {
    const result = [];
    for await (const chunk of stream) {
        result.push(chunk);
    }
    return result;
}
export async function text(stream) {
    return consumers.text(stream);
}
export async function json(stream) {
    return consumers.json(stream);
}
export async function bytes(stream) {
    return consumers.buffer(stream);
}
// =============================================================================
// Stream Factory Helpers
// =============================================================================
export function fromString(str) {
    return createReadableFromArray([_stringToUint8Array(str)], {
        objectMode: false
    });
}
export function fromJSON(data) {
    return fromString(JSON.stringify(data));
}
export function fromBytes(data) {
    return createReadableFromArray([data], { objectMode: false });
}
export function transform(fn) {
    return createTransform(fn);
}
export function filter(predicate) {
    return createTransform(async (chunk) => {
        if (await predicate(chunk)) {
            return chunk;
        }
        return undefined;
    }, { objectMode: true });
}
