"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.fieldBatchCodecBuilder = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/runtime-utils/internal");
const index_js_1 = require("../../../codec/index.js");
const index_js_2 = require("../../../util/index.js");
const treeCompressionUtils_js_1 = require("../../treeCompressionUtils.js");
const chunkDecoding_js_1 = require("./chunkDecoding.js");
const index_js_3 = require("./format/index.js");
const schemaBasedEncode_js_1 = require("./schemaBasedEncode.js");
const uncompressedEncode_js_1 = require("./uncompressedEncode.js");
const ChunkReferenceId = (0, index_js_2.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) {
                (0, internal_1.assert)(cursor.mode === 1 /* CursorLocationType.Fields */, 0x8a3 /* FieldBatch expects fields cursors */);
            }
            let encoded;
            let incrementalEncoder;
            switch (context.encodeType) {
                case treeCompressionUtils_js_1.TreeCompressionStrategy.Uncompressed: {
                    encoded = uncompressedEncodeFn(data);
                    break;
                }
                case treeCompressionUtils_js_1.TreeCompressionStrategy.CompressedIncremental: {
                    (0, internal_1.assert)((0, index_js_3.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 treeCompressionUtils_js_1.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: {
                    (0, internal_1.unreachableCase)(context.encodeType);
                }
            }
            // TODO: consider checking input data was in schema.
            return encoded;
        },
        decode: (data, context) => {
            // TODO: consider checking data is in schema.
            return (0, chunkDecoding_js_1.decode)(data, {
                idCompressor: context.idCompressor,
                originatorId: context.originatorId,
                isSummary: context.isSummary,
                healUnresolvableIdentifiersOnDecode: context.healUnresolvableIdentifiersOnDecode,
                sharedObjectId: context.sharedObjectId,
            }, context.incrementalEncoderDecoder).map((chunk) => chunk.cursor());
        },
        schema: encodedFieldBatchType,
    };
}
/**
 * {@link ClientVersionDispatchingCodecBuilder} for field batch codecs.
 */
exports.fieldBatchCodecBuilder = index_js_1.ClientVersionDispatchingCodecBuilder.build("FieldBatch", [
    {
        minVersionForCollab: internal_2.lowestMinVersionForCollab,
        formatVersion: index_js_3.FieldBatchFormatVersion.v1,
        codec: makeFieldBatchCodecForVersion(index_js_3.FieldBatchFormatVersion.v1, uncompressedEncode_js_1.uncompressedEncodeV1, schemaBasedEncode_js_1.schemaCompressedEncodeV1, index_js_3.EncodedFieldBatchV1),
    },
    {
        minVersionForCollab: index_js_1.FluidClientVersion.v2_73,
        formatVersion: index_js_3.FieldBatchFormatVersion.v2,
        codec: makeFieldBatchCodecForVersion(index_js_3.FieldBatchFormatVersion.v2, uncompressedEncode_js_1.uncompressedEncodeV2, schemaBasedEncode_js_1.schemaCompressedEncodeV2, index_js_3.EncodedFieldBatchV2),
    },
]);
//# sourceMappingURL=codecs.js.map