/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { assert } from "@fluidframework/core-utils/internal";
import { withSchemaValidation } from "../codec/index.js";
import { Message } from "./messageFormatV1ToV4.js";
export function makeV1ToV4CodecWithVersion(changeCodec, revisionTagCodec, options, version) {
    return withSchemaValidation(Message(changeCodec.encodedSchema ?? Type.Any()), {
        encode: (decoded, context) => {
            assert(decoded.type === "commit", 0xc68 /* Only commit messages are supported */);
            assert(decoded.branchId === "main", 0xc69 /* Only commit messages to main are supported */);
            const { commit, sessionId: originatorId } = decoded;
            const message = {
                revision: revisionTagCodec.encode(commit.revision, {
                    originatorId,
                    idCompressor: context.idCompressor,
                    revision: undefined,
                }),
                originatorId,
                changeset: changeCodec.encode(commit.change, {
                    originatorId,
                    schema: context.schema,
                    idCompressor: context.idCompressor,
                    revision: commit.revision,
                }),
                version,
            };
            return message;
        },
        decode: (encoded, context) => {
            const { revision: encodedRevision, originatorId, changeset, } = encoded;
            const revision = revisionTagCodec.decode(encodedRevision, {
                originatorId,
                revision: undefined,
                idCompressor: context.idCompressor,
            });
            return {
                branchId: "main",
                type: "commit",
                commit: {
                    revision,
                    change: changeCodec.decode(changeset, {
                        originatorId,
                        revision,
                        idCompressor: context.idCompressor,
                    }),
                },
                sessionId: originatorId,
            };
        },
    }, options.jsonValidator);
}
//# sourceMappingURL=messageCodecV1ToV4.js.map