/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, unreachableCase } from "@fluidframework/core-utils/internal";
import { lowestMinVersionForCollab } from "@fluidframework/runtime-utils/internal";
import { ClientVersionDispatchingCodecBuilder, FluidClientVersion, } from "../../../codec/index.js";
import { brandedNumberType } from "../../../util/index.js";
import { TreeCompressionStrategy } from "../../treeCompressionUtils.js";
import { decode } from "./chunkDecoding.js";
import { EncodedFieldBatchV1, EncodedFieldBatchV2, FieldBatchFormatVersion, supportsIncrementalEncoding, } from "./format/index.js";
import { schemaCompressedEncodeV1, schemaCompressedEncodeV2 } from "./schemaBasedEncode.js";
import { uncompressedEncodeV1, uncompressedEncodeV2 } from "./uncompressedEncode.js";
const ChunkReferenceId = brandedNumberType({ multipleOf: 1, minimum: 0 });
/**
 * Creates the encode/decode functions for a specific FieldBatch format version.
 */
function makeFieldBatchCodecForVersion(version, uncompressedEncodeFn, schemaCompressedEncodeFn, encodedFieldBatchType) {
    return {
        encode: (data, context) => {
            for (const cursor of data) {
                assert(cursor.mode === 1 /* CursorLocationType.Fields */, 0x8a3 /* FieldBatch expects fields cursors */);
            }
            let encoded;
            let incrementalEncoder;
            switch (context.encodeType) {
                case TreeCompressionStrategy.Uncompressed: {
                    encoded = uncompressedEncodeFn(data);
                    break;
                }
                case TreeCompressionStrategy.CompressedIncremental: {
                    assert(supportsIncrementalEncoding(version), 0xca0 /* Unsupported FieldBatchFormatVersion for incremental encoding; must be v2 or higher */);
                    // Incremental encoding is only supported for CompressedIncremental.
                    incrementalEncoder = context.incrementalEncoderDecoder;
                }
                // fallthrough
                case TreeCompressionStrategy.Compressed: {
                    // eslint-disable-next-line unicorn/prefer-ternary
                    if (context.schema === undefined) {
                        // TODO: consider enabling a somewhat compressed but not schema accelerated encode.
                        encoded = uncompressedEncodeFn(data);
                    }
                    else {
                        encoded = schemaCompressedEncodeFn(context.schema.schema, context.schema.policy, data, context.idCompressor, incrementalEncoder);
                    }
                    break;
                }
                default: {
                    unreachableCase(context.encodeType);
                }
            }
            // TODO: consider checking input data was in schema.
            return encoded;
        },
        decode: (data, context) => {
            // TODO: consider checking data is in schema.
            return decode(data, {
                idCompressor: context.idCompressor,
                originatorId: context.originatorId,
            }, context.incrementalEncoderDecoder).map((chunk) => chunk.cursor());
        },
        schema: encodedFieldBatchType,
    };
}
/**
 * {@link ClientVersionDispatchingCodecBuilder} for field batch codecs.
 */
export const fieldBatchCodecBuilder = ClientVersionDispatchingCodecBuilder.build("FieldBatch", [
    {
        minVersionForCollab: lowestMinVersionForCollab,
        formatVersion: FieldBatchFormatVersion.v1,
        codec: makeFieldBatchCodecForVersion(FieldBatchFormatVersion.v1, uncompressedEncodeV1, schemaCompressedEncodeV1, EncodedFieldBatchV1),
    },
    {
        minVersionForCollab: FluidClientVersion.v2_73,
        formatVersion: FieldBatchFormatVersion.v2,
        codec: makeFieldBatchCodecForVersion(FieldBatchFormatVersion.v2, uncompressedEncodeV2, schemaCompressedEncodeV2, EncodedFieldBatchV2),
    },
]);
//# sourceMappingURL=codecs.js.map