/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, unreachableCase } from "@fluidframework/core-utils/internal";
import { DiscriminatedUnionDispatcher } from "../../../codec/index.js";
import { assertValidIndex } from "../../../util/index.js";
import { BasicChunk } from "../basicChunk.js";
import { emptyChunk } from "../emptyChunk.js";
import { SequenceChunk } from "../sequenceChunk.js";
import { getChecked, readStream, readStreamBoolean, readStreamNumber, readStreamStream, readStreamValue, } from "./chunkCodecUtilities.js";
import { DecoderContext, decode as genericDecode, readStreamIdentifier, } from "./chunkDecodingGeneric.js";
import { SpecialField, } from "./format.js";
/**
 * Decode `chunk` into a TreeChunk.
 */
export function decode(chunk, idDecodingContext) {
    return genericDecode(decoderLibrary, new DecoderContext(chunk.identifiers, chunk.shapes, idDecodingContext), chunk, anyDecoder);
}
const decoderLibrary = new DiscriminatedUnionDispatcher({
    a(shape, cache) {
        return new NestedArrayDecoder(shape);
    },
    b(shape, cache) {
        return new InlineArrayDecoder(shape);
    },
    c(shape, cache) {
        return new TreeDecoder(shape, cache);
    },
    d(shape) {
        return anyDecoder;
    },
});
/**
 * Decode a node's value from `stream` using its shape.
 */
export function readValue(stream, shape, idDecodingContext) {
    if (shape === undefined) {
        return readStreamBoolean(stream) ? readStreamValue(stream) : undefined;
    }
    else {
        if (shape === true) {
            return readStreamValue(stream);
        }
        else if (shape === false) {
            return undefined;
        }
        else if (Array.isArray(shape)) {
            assert(shape.length === 1, 0x734 /* expected a single constant for value */);
            return shape[0];
        }
        else if (shape === SpecialField.Identifier) {
            // This case is a special case handling the decoding of identifier fields.
            const streamValue = readStream(stream);
            assert(typeof streamValue === "number" || typeof streamValue === "string", 0x997 /* identifier must be string or number. */);
            const idCompressor = idDecodingContext.idCompressor;
            return typeof streamValue === "number"
                ? idCompressor.decompress(idCompressor.normalizeToSessionSpace(streamValue, idDecodingContext.originatorId))
                : streamValue;
        }
        else {
            // EncodedCounter case:
            unreachableCase(shape, "decoding values as deltas is not yet supported");
        }
    }
}
/**
 * Normalize a {@link TreeChunk} into an array.
 *
 * Unwraps {@link SequenceChunk}s, and wraps other chunks.
 */
export function deaggregateChunks(chunk) {
    if (chunk === emptyChunk) {
        return [];
    }
    // TODO: when handling of SequenceChunks has better performance (for example in cursors),
    // consider keeping SequenceChunks here if they are longer than some threshold.
    if (chunk instanceof SequenceChunk) {
        // Could return [] here, however the logic in this file is designed to never produce an empty SequenceChunk, so its better to throw an error here to detect bugs.
        assert(chunk.subChunks.length > 0, 0x735 /* Unexpected empty sequence */);
        // Logic in this file is designed to never produce an unneeded (single item) SequenceChunks, so its better to throw an error here to detect bugs.
        assert(chunk.subChunks.length > 1, 0x736 /* Unexpected single item sequence */);
        for (const sub of chunk.subChunks) {
            // The logic in this file is designed to never produce an nested SequenceChunks or emptyChunk, so its better to throw an error here to detect bugs.
            assert(!(sub instanceof SequenceChunk), 0x737 /* unexpected nested sequence */);
            assert(sub !== emptyChunk, 0x738 /* unexpected empty chunk */);
            sub.referenceAdded();
        }
        chunk.referenceRemoved();
        return chunk.subChunks;
    }
    else {
        return [chunk];
    }
}
/**
 * Normalize a {@link TreeChunk}[] into a single TreeChunk.
 *
 * Avoids creating nested or less than 2 child {@link SequenceChunk}s.
 */
export function aggregateChunks(input) {
    const chunks = input.flatMap(deaggregateChunks);
    switch (chunks.length) {
        case 0:
            return emptyChunk;
        case 1:
            return chunks[0];
        default:
            return new SequenceChunk(chunks);
    }
}
/**
 * Decoder for {@link EncodedNestedArray}s.
 */
export class NestedArrayDecoder {
    constructor(shape) {
        this.shape = shape;
    }
    decode(decoders, stream) {
        const decoder = decoders[this.shape];
        // TODO: uniform chunk fast path
        const chunks = [];
        const data = readStream(stream);
        if (typeof data === "number") {
            // This case means that the array contained only 0-sized items, and was thus encoded as the length of the array.
            const inner = { data: [], offset: 0 };
            for (let index = 0; index < data; index++) {
                chunks.push(decoder.decode(decoders, inner));
            }
        }
        else {
            assert(Array.isArray(data), 0x739 /* expected number of array for encoding of nested array */);
            const inner = { data, offset: 0 };
            while (inner.offset !== inner.data.length) {
                chunks.push(decoder.decode(decoders, inner));
            }
        }
        return aggregateChunks(chunks);
    }
}
/**
 * Decoder for {@link EncodedInlineArray}s.
 */
export class InlineArrayDecoder {
    constructor(shape) {
        this.shape = shape;
    }
    decode(decoders, stream) {
        const length = this.shape.length;
        const decoder = decoders[this.shape.shape];
        const chunks = [];
        for (let index = 0; index < length; index++) {
            chunks.push(decoder.decode(decoders, stream));
        }
        return aggregateChunks(chunks);
    }
}
/**
 * Decoder for {@link EncodedAnyShape}s.
 */
export const anyDecoder = {
    decode(decoders, stream) {
        const shapeIndex = readStreamNumber(stream);
        const decoder = getChecked(decoders, shapeIndex);
        return decoder.decode(decoders, stream);
    },
};
/**
 * Get a decoder for fields of a provided (via `shape` and `cache`) {@link EncodedChunkShape}.
 */
function fieldDecoder(cache, key, shape) {
    assertValidIndex(shape, cache.shapes);
    return (decoders, stream) => [key, decoders[shape].decode(decoders, stream)];
}
/**
 * Decoder for {@link EncodedTreeShape}s.
 */
export class TreeDecoder {
    constructor(shape, cache) {
        this.shape = shape;
        this.cache = cache;
        this.type = shape.type === undefined ? undefined : cache.identifier(shape.type);
        const fieldDecoders = [];
        for (const [fieldKey, fieldShape] of shape.fields ?? []) {
            const key = cache.identifier(fieldKey);
            fieldDecoders.push(fieldDecoder(cache, key, fieldShape));
        }
        this.fieldDecoders = fieldDecoders;
    }
    decode(decoders, stream) {
        const type = this.type ?? readStreamIdentifier(stream, this.cache);
        // TODO: Consider typechecking against stored schema in here somewhere.
        const value = readValue(stream, this.shape.value, this.cache.idDecodingContext);
        const fields = new Map();
        // Helper to add fields, but with unneeded array chunks removed.
        function addField(key, data) {
            // TODO: when handling of ArrayChunks has better performance (for example in cursors),
            // consider keeping array chunks here if they are longer than some threshold.
            const chunks = deaggregateChunks(data);
            if (chunks.length !== 0) {
                fields.set(key, chunks);
            }
        }
        for (const field of this.fieldDecoders) {
            const [key, content] = field(decoders, stream);
            addField(key, content);
        }
        if (this.shape.extraFields !== undefined) {
            const decoder = decoders[this.shape.extraFields];
            const inner = readStreamStream(stream);
            while (inner.offset !== inner.data.length) {
                const key = readStreamIdentifier(inner, this.cache);
                addField(key, decoder.decode(decoders, inner));
            }
        }
        return new BasicChunk(type, fields, value);
    }
}
//# sourceMappingURL=chunkDecoding.js.map