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