"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeMessageCodec = void 0;
const typebox_1 = require("@sinclair/typebox");
const codec_1 = require("../codec");
const messageFormat_1 = require("./messageFormat");
function makeMessageCodec(changesetCodec) {
    return (0, codec_1.withSchemaValidation)((0, messageFormat_1.Message)(changesetCodec.encodedSchema ?? typebox_1.Type.Any()), {
        encode: ({ commit, sessionId }) => {
            const message = {
                revision: commit.revision,
                originatorId: sessionId,
                changeset: changesetCodec.encode(commit.change),
            };
            return message;
        },
        decode: (encoded) => {
            const { revision, originatorId, changeset } = encoded;
            return {
                commit: {
                    revision,
                    change: changesetCodec.decode(changeset),
                },
                sessionId: originatorId,
            };
        },
    });
}
exports.makeMessageCodec = makeMessageCodec;
//# sourceMappingURL=messageCodecs.js.map