/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { getChecked, readStream } from "./chunkCodecUtilities.js";
/**
 * General purpose shape based tree decoder which gets its support for specific shapes from the caller.
 */
export function decode(decoderLibrary, cache, batch, rootDecoder) {
    const decoders = batch.shapes.map((shape) => decoderLibrary.dispatch(shape, cache));
    const chunks = [];
    for (const field of batch.data) {
        const stream = { data: field, offset: 0 };
        const result = rootDecoder.decode(decoders, stream);
        assert(stream.offset === stream.data.length, 0x73a /* expected decode to consume full stream */);
        chunks.push(result);
    }
    return chunks;
}
/**
 * Shared data for use in constructing decoders.
 */
export class DecoderContext {
    /**
     * @param identifiers - identifier substitution table (use to replace numeric identifier indexes with the actual identifiers from this table).
     */
    constructor(identifiers, shapes) {
        this.identifiers = identifiers;
        this.shapes = shapes;
    }
    identifier(encoded) {
        if (typeof encoded === "string") {
            return encoded;
        }
        return getChecked(this.identifiers, encoded);
    }
}
/**
 * Read one identifier from the stream, advancing the stream offset.
 */
export function readStreamIdentifier(stream, cache) {
    const content = readStream(stream);
    assert(typeof content === "number" || typeof content === "string", 0x73b /* content to be a number or string */);
    return cache.identifier(content);
}
//# sourceMappingURL=chunkDecodingGeneric.js.map