"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 typebox_1 = require("@sinclair/typebox");
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../codec/index.js");
const messageFormatV1ToV4_js_1 = require("./messageFormatV1ToV4.js");
function makeV1ToV4CodecWithVersion(changeCodec, revisionTagCodec, options, version) {
    return (0, index_js_1.withSchemaValidation)((0, messageFormatV1ToV4_js_1.Message)(changeCodec.encodedSchema ?? typebox_1.Type.Any()), {
        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;
            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);
}
exports.makeV1ToV4CodecWithVersion = makeV1ToV4CodecWithVersion;
//# sourceMappingURL=messageCodecV1ToV4.js.map