/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, unreachableCase } from "@fluidframework/core-utils/internal";
import { makeVersionedValidatedCodec, } from "../../../codec/index.js";
import { TreeCompressionStrategy } from "../../treeCompressionUtils.js";
import { decode } from "./chunkDecoding.js";
import { EncodedFieldBatch, validVersions } from "./format.js";
import { schemaCompressedEncode } from "./schemaBasedEncode.js";
import { uncompressedEncode } from "./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.
 */
export function fluidVersionToFieldBatchCodecWriteVersion(oldestCompatibleClient) {
    // There is currently on only 1 version.
    return 1;
}
export 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).
    assert(validVersions.has(writeVersion), 0x935 /* Invalid write version for FieldBatch codec */);
    // TODO: use makeVersionDispatchingCodec to support adding more versions in the future.
    return makeVersionedValidatedCodec(options, validVersions, EncodedFieldBatch, {
        encode: (data, context) => {
            for (const cursor of data) {
                assert(cursor.mode === 1 /* CursorLocationType.Fields */, 0x8a3 /* FieldBatch expects fields cursors */);
            }
            let encoded;
            switch (context.encodeType) {
                case TreeCompressionStrategy.Uncompressed:
                    encoded = uncompressedEncode(data);
                    break;
                case TreeCompressionStrategy.Compressed:
                    // eslint-disable-next-line unicorn/prefer-ternary
                    if (context.schema !== undefined) {
                        encoded = schemaCompressedEncode(context.schema.schema, context.schema.policy, data, context.idCompressor);
                    }
                    else {
                        // TODO: consider enabling a somewhat compressed but not schema accelerated encode.
                        encoded = uncompressedEncode(data);
                    }
                    break;
                default:
                    unreachableCase(context.encodeType);
            }
            // TODO: consider checking input data was in schema.
            return encoded;
        },
        decode: (data, context) => {
            // TODO: consider checking data is in schema.
            return decode(data, {
                idCompressor: context.idCompressor,
                originatorId: context.originatorId,
            }).map((chunk) => chunk.cursor());
        },
    });
}
//# sourceMappingURL=codecs.js.map