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