"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeFieldBatchCodec = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const codec_1 = require("../../../codec");
const treeCompressionUtils_1 = require("../../treeCompressionUtils");
const format_1 = require("./format");
const chunkDecoding_1 = require("./chunkDecoding");
const schemaBasedEncoding_1 = require("./schemaBasedEncoding");
const uncompressedEncode_1 = require("./uncompressedEncode");
function makeFieldBatchCodec(options, context) {
    // 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)
    // Once the layering around schema/edit-manager/SharedTreeCore is sorted out, it would be preferable
    // for this codec to receive the current schema as part of its context rather than retain a reference
    // to the current schema (which can mutate between calls).
    return (0, codec_1.makeVersionedValidatedCodec)(options, format_1.validVersions, format_1.EncodedFieldBatch, {
        encode: (data) => {
            for (const cursor of data) {
                (0, core_utils_1.assert)(cursor.mode === 1 /* CursorLocationType.Fields */, 0x8a3 /* FieldBatch expects fields cursors */);
            }
            let encoded;
            switch (context.encodeType) {
                case treeCompressionUtils_1.TreeCompressionStrategy.Uncompressed:
                    encoded = (0, uncompressedEncode_1.uncompressedEncode)(data);
                    break;
                case treeCompressionUtils_1.TreeCompressionStrategy.Compressed:
                    // eslint-disable-next-line unicorn/prefer-ternary
                    if (context.schema !== undefined) {
                        encoded = (0, schemaBasedEncoding_1.schemaCompressedEncode)(context.schema.schema, context.schema.policy, data);
                    }
                    else {
                        // TODO: consider enabling a somewhat compressed but not schema accelerated encode.
                        encoded = (0, uncompressedEncode_1.uncompressedEncode)(data);
                    }
                    break;
                default:
                    (0, core_utils_1.unreachableCase)(context.encodeType);
            }
            // TODO: consider checking input data was in schema.
            return encoded;
        },
        decode: (data) => {
            // TODO: consider checking data is in schema.
            return (0, chunkDecoding_1.decode)(data).map((chunk) => chunk.cursor());
        },
    });
}
exports.makeFieldBatchCodec = makeFieldBatchCodec;
//# sourceMappingURL=codecs.js.map