"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 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 schemaBasedEncoding_js_1 = require("./schemaBasedEncoding.js");
const uncompressedEncode_js_1 = require("./uncompressedEncode.js");
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), "Invalid write version for FieldBatch codec");
    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, schemaBasedEncoding_js_1.schemaCompressedEncode)(context.schema.schema, context.schema.policy, data);
                    }
                    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) => {
            // TODO: consider checking data is in schema.
            return (0, chunkDecoding_js_1.decode)(data).map((chunk) => chunk.cursor());
        },
    });
}
exports.makeFieldBatchCodec = makeFieldBatchCodec;
//# sourceMappingURL=codecs.js.map