"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeV1ToV4CodecWithVersion = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../util/index.js");
const messageFormatV1ToV4_js_1 = require("./messageFormatV1ToV4.js");
function makeV1ToV4CodecWithVersion(changeCodec, revisionTagCodec, version) {
    const schema = (0, messageFormatV1ToV4_js_1.Message)(changeCodec.encodedSchema ?? index_js_1.JsonCompatibleReadOnlySchema);
    return {
        schema,
        encode: (decoded, context) => {
            (0, internal_1.assert)(decoded.type === "commit", 0xc68 /* Only commit messages are supported */);
            (0, internal_1.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,
                    isSummary: false,
                }),
                originatorId,
                changeset: changeCodec.encode(commit.change, {
                    originatorId,
                    schema: context.schema,
                    idCompressor: context.idCompressor,
                    revision: commit.revision,
                    isSummary: false,
                }),
                version,
            };
        },
        decode: (encoded, context) => {
            const { revision: encodedRevision, originatorId, changeset } = encoded;
            const revision = revisionTagCodec.decode(encodedRevision, {
                originatorId,
                revision: undefined,
                idCompressor: context.idCompressor,
                isSummary: false,
            });
            return {
                branchId: "main",
                type: "commit",
                commit: {
                    revision,
                    change: changeCodec.decode(changeset, {
                        originatorId,
                        revision,
                        idCompressor: context.idCompressor,
                        isSummary: false,
                    }),
                },
                sessionId: originatorId,
            };
        },
    };
}
exports.makeV1ToV4CodecWithVersion = makeV1ToV4CodecWithVersion;
//# sourceMappingURL=messageCodecV1ToV4.js.map