"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCodecTreeForFieldBatchFormat = exports.makeFieldBatchCodec = 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 format_js_1 = require("./format.js");
const uncompressedEncode_js_1 = require("./uncompressedEncode.js");
const schemaBasedEncode_js_1 = require("./schemaBasedEncode.js");
const ChunkReferenceId = (0, index_js_2.brandedNumberType)({ multipleOf: 1, minimum: 0 });
/**
 * Convert a MinimumVersionForCollab to write version for {@link FieldBatchCodec}.
 * @param clientVersion - The MinimumVersionForCollab to convert.
 */
function clientVersionToFieldBatchVersion(clientVersion) {
    return (0, index_js_2.brand)((0, internal_2.getConfigForMinVersionForCollab)(clientVersion, {
        [internal_2.lowestMinVersionForCollab]: format_js_1.FieldBatchFormatVersion.v1,
        [index_js_1.FluidClientVersion.v2_73]: format_js_1.FieldBatchFormatVersion.v2,
    }));
}
function makeFieldBatchCodec(options) {
    const writeVersion = clientVersionToFieldBatchVersion(options.minVersionForCollab);
    // Note: it's important that the decode function is schema-agnostic for this strategy/layering to work, since
    // the schema that an op was encoded in doesn't necessarily match the current schema for the document (e.g. if
    // decode is being run on a client that just submitted a schema change, but the op is from another client who has
    // yet to receive that change).
    (0, internal_1.assert)(format_js_1.validVersions.has(writeVersion), 0x935 /* Invalid write version for FieldBatch codec */);
    let uncompressedEncodeFn;
    let schemaCompressedEncodeFn;
    let encodedFieldBatchType;
    switch (writeVersion) {
        case format_js_1.FieldBatchFormatVersion.v1:
            uncompressedEncodeFn = uncompressedEncode_js_1.uncompressedEncodeV1;
            schemaCompressedEncodeFn = schemaBasedEncode_js_1.schemaCompressedEncodeV1;
            encodedFieldBatchType = format_js_1.EncodedFieldBatchV1;
            break;
        case format_js_1.FieldBatchFormatVersion.v2:
            uncompressedEncodeFn = uncompressedEncode_js_1.uncompressedEncodeV2;
            schemaCompressedEncodeFn = schemaBasedEncode_js_1.schemaCompressedEncodeV2;
            encodedFieldBatchType = format_js_1.EncodedFieldBatchV2;
            break;
        default:
            (0, internal_1.unreachableCase)(writeVersion);
    }
    return (0, index_js_1.makeVersionedValidatedCodec)(options, format_js_1.validVersions, encodedFieldBatchType, {
        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)(writeVersion >= format_js_1.FieldBatchFormatVersion.v2, 0xca0 /* Unsupported FieldBatchFormatVersion for incremental encoding; must be v2 or higher */);
                    // Incremental encoding is only supported for CompressedIncremental.
                    incrementalEncoder = context.incrementalEncoderDecoder;
                case treeCompressionUtils_js_1.TreeCompressionStrategy.Compressed:
                    // eslint-disable-next-line unicorn/prefer-ternary
                    if (context.schema !== undefined) {
                        encoded = schemaCompressedEncodeFn(context.schema.schema, context.schema.policy, data, context.idCompressor, incrementalEncoder);
                    }
                    else {
                        // TODO: consider enabling a somewhat compressed but not schema accelerated encode.
                        encoded = uncompressedEncodeFn(data);
                    }
                    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,
            }, context.incrementalEncoderDecoder).map((chunk) => chunk.cursor());
        },
    });
}
exports.makeFieldBatchCodec = makeFieldBatchCodec;
function getCodecTreeForFieldBatchFormat(clientVersion) {
    return { name: "FieldBatch", version: clientVersionToFieldBatchVersion(clientVersion) };
}
exports.getCodecTreeForFieldBatchFormat = getCodecTreeForFieldBatchFormat;
//# sourceMappingURL=codecs.js.map