/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { JsonCompatibleReadOnlySchema, } from "../util/index.js";
import { Message } from "./messageFormatV1ToV4.js";
export function makeV1ToV4CodecWithVersion(changeCodec, revisionTagCodec, version) {
    const schema = Message(changeCodec.encodedSchema ?? JsonCompatibleReadOnlySchema);
    return {
        schema,
        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;
            return {
                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,
            };
        },
        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,
            };
        },
    };
}
//# sourceMappingURL=messageCodecV1ToV4.js.map