/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { withSchemaValidation } from "../codec";
import { Message } from "./messageFormat";
export function makeMessageCodec(changeCodec, revisionTagCodec, options) {
    // TODO: consider adding version and using makeVersionedValidatedCodec
    return withSchemaValidation(Message(changeCodec.encodedSchema ?? Type.Any()), {
        encode: ({ commit, sessionId: originatorId }) => {
            const message = {
                revision: revisionTagCodec.encode(commit.revision, originatorId),
                originatorId,
                changeset: changeCodec.encode(commit.change, { originatorId }),
            };
            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);
}
//# sourceMappingURL=messageCodecs.js.map