export default class GitCasGraphReaderAdapter {
    _persistence;
    _assertEmptyBlobExists;
    _treeOidReader;
    constructor(options) {
        this._persistence = options.persistence;
        this._assertEmptyBlobExists = options.assertEmptyBlobExists;
        this._treeOidReader = options.treeOidReader;
    }
    async readBlob(oid) {
        const stream = await this._persistence.readBlobStream(oid);
        const bytes = await collectUnboundedGraphBlobStream(stream);
        if (bytes.byteLength === 0) {
            await this._assertEmptyBlobExists(oid);
        }
        return bytes;
    }
    async readTreeOids(treeOid) {
        return await this._treeOidReader.readTreeOids(treeOid);
    }
}
async function collectUnboundedGraphBlobStream(source) {
    const chunks = [];
    let byteLength = 0;
    for await (const chunk of source) {
        const bytes = normalizeBytes(chunk);
        chunks.push(bytes);
        byteLength += bytes.byteLength;
    }
    const output = new Uint8Array(byteLength);
    let offset = 0;
    for (const chunk of chunks) {
        output.set(chunk, offset);
        offset += chunk.byteLength;
    }
    return output;
}
function normalizeBytes(chunk) {
    if (chunk.constructor === Uint8Array) {
        return chunk;
    }
    return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
}
