"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toUint8ArraySync = toUint8ArraySync;
exports.toUint8Array = toUint8Array;
exports.toAsyncIterable = toAsyncIterable;
const text_1 = require("../utils/text.js");
const type_guards_1 = require("../../stream/internal/type-guards.js");
function normalizeChunk(value) {
    if (!value) {
        return null;
    }
    if (value instanceof Uint8Array) {
        return value.length ? value : null;
    }
    if (typeof value === "string") {
        const bytes = (0, text_1.encodeUtf8)(value);
        return bytes.length ? bytes : null;
    }
    if (value instanceof ArrayBuffer) {
        return value.byteLength ? new Uint8Array(value) : null;
    }
    if (ArrayBuffer.isView(value)) {
        return value.byteLength
            ? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
            : null;
    }
    // Best-effort: treat unknown chunk as Uint8Array-like.
    if (typeof value.length === "number" && value.length) {
        return value;
    }
    return null;
}
function toUint8ArraySync(source) {
    if (source instanceof Uint8Array) {
        return source;
    }
    if (typeof source === "string") {
        return (0, text_1.encodeUtf8)(source);
    }
    return new Uint8Array(source);
}
async function toUint8Array(source) {
    if (source instanceof Uint8Array) {
        return source;
    }
    if (typeof source === "string") {
        return (0, text_1.encodeUtf8)(source);
    }
    if (source instanceof ArrayBuffer) {
        return new Uint8Array(source);
    }
    const buf = await source.arrayBuffer();
    return new Uint8Array(buf);
}
async function* toAsyncIterable(source) {
    if (source instanceof Uint8Array) {
        yield source;
        return;
    }
    if (typeof source === "string") {
        yield (0, text_1.encodeUtf8)(source);
        return;
    }
    if (source instanceof ArrayBuffer) {
        yield new Uint8Array(source);
        return;
    }
    if (typeof Blob !== "undefined" && source instanceof Blob) {
        yield await toUint8Array(source);
        return;
    }
    if ((0, type_guards_1.isReadableStream)(source)) {
        const reader = source.getReader();
        try {
            while (true) {
                const { done, value } = await reader.read();
                if (done) {
                    return;
                }
                const chunk = normalizeChunk(value);
                if (chunk) {
                    yield chunk;
                }
            }
        }
        finally {
            try {
                reader.releaseLock();
            }
            catch {
                // Ignore
            }
        }
    }
    if ((0, type_guards_1.isAsyncIterable)(source)) {
        for await (const value of source) {
            const chunk = normalizeChunk(value);
            if (chunk) {
                yield chunk;
            }
        }
        return;
    }
    throw new Error("Unsupported archive source");
}
