/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { lowestMinVersionForCollab } from "@fluidframework/runtime-utils/internal";
import { ClientVersionDispatchingCodecBuilder, FluidClientVersion, makeDiscontinuedCodecAndSchema, } from "../codec/index.js";
import { makeV1ToV4CodecWithVersion } from "./messageCodecV1ToV4.js";
import { makeSharedBranchesCodecWithVersion } from "./messageCodecVSharedBranches.js";
import { MessageFormatVersion } from "./messageFormat.js";
/**
 * Codec name used to identify the message codec, see {@link makeMessageCodecBuilder}.
 */
export const messageCodecName = "Message";
/**
 * Creates a {@link ClientVersionDispatchingCodecBuilder} for encoding/decoding messages.
 */
export function makeMessageCodecBuilder() {
    // See MessageFormatVersion and its members for documentation on what changed in each version.
    const versions = [
        // The "undefined" wire format (no version field) is discontinued.
        makeDiscontinuedCodecAndSchema(undefined, "2.73.0"),
        makeDiscontinuedCodecAndSchema(MessageFormatVersion.v1, "2.73.0"),
        makeDiscontinuedCodecAndSchema(MessageFormatVersion.v2, "2.73.0"),
        {
            minVersionForCollab: lowestMinVersionForCollab,
            formatVersion: MessageFormatVersion.v3,
            codec: (options) => makeV1ToV4CodecWithVersion(options.changeCodecs.resolve(options.dependentChangeFormatVersion.lookup(MessageFormatVersion.v3)), options.revisionTagCodec, MessageFormatVersion.v3),
        },
        {
            minVersionForCollab: FluidClientVersion.v2_43,
            formatVersion: MessageFormatVersion.v4,
            codec: (options) => makeV1ToV4CodecWithVersion(options.changeCodecs.resolve(options.dependentChangeFormatVersion.lookup(MessageFormatVersion.v4)), options.revisionTagCodec, MessageFormatVersion.v4),
        },
        makeDiscontinuedCodecAndSchema(MessageFormatVersion.v5, "2.74.0"),
        {
            minVersionForCollab: FluidClientVersion.v2_80,
            formatVersion: MessageFormatVersion.v6,
            codec: (options) => makeV1ToV4CodecWithVersion(options.changeCodecs.resolve(options.dependentChangeFormatVersion.lookup(MessageFormatVersion.v6)), options.revisionTagCodec, MessageFormatVersion.v6),
        },
        {
            minVersionForCollab: undefined,
            formatVersion: MessageFormatVersion.vSharedBranches,
            codec: (options) => makeSharedBranchesCodecWithVersion(options.changeCodecs.resolve(options.dependentChangeFormatVersion.lookup(MessageFormatVersion.vSharedBranches)), options.revisionTagCodec, MessageFormatVersion.vSharedBranches),
        },
    ];
    return ClientVersionDispatchingCodecBuilder.build(messageCodecName, versions);
}
export function getCodecTreeForMessageFormatWithChange(clientVersion, changeFormat) {
    const builder = makeMessageCodecBuilder();
    return {
        ...builder.getCodecTree(clientVersion),
        children: [changeFormat],
    };
}
//# sourceMappingURL=messageCodecs.js.map