"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeFieldBatchCodec = exports.fluidVersionToFieldBatchCodecWriteVersion = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../../codec/index.js");
const treeCompressionUtils_js_1 = require("../../treeCompressionUtils.js");
const chunkDecoding_js_1 = require("./chunkDecoding.js");
const format_js_1 = require("./format.js");
const schemaBasedEncode_js_1 = require("./schemaBasedEncode.js");
const uncompressedEncode_js_1 = require("./uncompressedEncode.js");
/**
 * Get the write version for {@link makeFieldBatchCodec} based on the `oldestCompatibleClient` version.
 * @privateRemarks
 * TODO: makeFieldBatchCodec (and makeVersionDispatchingCodec transitively) should bake in this versionToFormat logic and the resulting codec can then support use with FluidClientVersion directly.
 */
function fluidVersionToFieldBatchCodecWriteVersion(oldestCompatibleClient) {
    // There is currently on only 1 version.
    return 1;
}
exports.fluidVersionToFieldBatchCodecWriteVersion = fluidVersionToFieldBatchCodecWriteVersion;
function makeFieldBatchCodec(options, writeVersion) {
    // 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 */);
    // TODO: use makeVersionDispatchingCodec to support adding more versions in the future.
    return (0, index_js_1.makeVersionedValidatedCodec)(options, format_js_1.validVersions, format_js_1.EncodedFieldBatch, {
        encode: (data, context) => {
            for (const cursor of data) {
                (0, internal_1.assert)(cursor.mode === 1 /* CursorLocationType.Fields */, 0x8a3 /* FieldBatch expects fields cursors */);
            }
            let encoded;
            switch (context.encodeType) {
                case treeCompressionUtils_js_1.TreeCompressionStrategy.Uncompressed:
                    encoded = (0, uncompressedEncode_js_1.uncompressedEncode)(data);
                    break;
                case treeCompressionUtils_js_1.TreeCompressionStrategy.Compressed:
                    // eslint-disable-next-line unicorn/prefer-ternary
                    if (context.schema !== undefined) {
                        encoded = (0, schemaBasedEncode_js_1.schemaCompressedEncode)(context.schema.schema, context.schema.policy, data, context.idCompressor);
                    }
                    else {
                        // TODO: consider enabling a somewhat compressed but not schema accelerated encode.
                        encoded = (0, uncompressedEncode_js_1.uncompressedEncode)(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,
            }).map((chunk) => chunk.cursor());
        },
    });
}
exports.makeFieldBatchCodec = makeFieldBatchCodec;
//# sourceMappingURL=codecs.js.map