/*!
 * 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, context, batch, rootDecoder) {
    const decoders = batch.shapes.map((shape) => decoderLibrary.dispatch(shape, context));
    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 {
    constructor(
    /**
     * Identifier substitution table (use to replace numeric identifier indexes with the actual identifiers from this table).
     */
    identifiers, shapes, idDecodingContext, 
    /**
     * To be used to decode incremental chunks, if any.
     * @remarks
     * See {@link IncrementalDecoder} for more information.
     */
    incrementalDecoder) {
        this.identifiers = identifiers;
        this.shapes = shapes;
        this.idDecodingContext = idDecodingContext;
        this.incrementalDecoder = incrementalDecoder;
    }
    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, context) {
    const content = readStream(stream);
    assert(typeof content === "number" || typeof content === "string", 0x73b /* content to be a number or string */);
    return context.identifier(content);
}
//# sourceMappingURL=chunkDecodingGeneric.js.map