"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 index_js_1 = require("../codec/index.js");
const messageFormat_js_1 = require("./messageFormat.js");
function makeMessageCodec(changeCodec, revisionTagCodec, options) {
    // TODO: consider adding version and using makeVersionedValidatedCodec
    return (0, index_js_1.withSchemaValidation)((0, messageFormat_js_1.Message)(changeCodec.encodedSchema ?? typebox_1.Type.Any()), {
        encode: ({ commit, sessionId: originatorId }, context) => {
            const message = {
                revision: revisionTagCodec.encode(commit.revision, { originatorId }),
                originatorId,
                changeset: changeCodec.encode(commit.change, {
                    originatorId,
                    schema: context.schema,
                }),
            };
            return message;
        },
        decode: (encoded) => {
            const { revision, originatorId, changeset } = encoded;
            return {
                commit: {
                    revision: revisionTagCodec.decode(revision, { originatorId }),
                    change: changeCodec.decode(changeset, { originatorId }),
                },
                sessionId: originatorId,
            };
        },
    }, options.jsonValidator);
}
exports.makeMessageCodec = makeMessageCodec;
//# sourceMappingURL=messageCodecs.js.map