/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { makeCodecFamily, makeVersionDispatchingCodec, withSchemaValidation, } from "../codec/index.js";
import { Message } from "./messageFormat.js";
export function makeMessageCodec(changeCodecs, revisionTagCodec, options, writeVersion) {
    const family = makeMessageCodecs(changeCodecs, revisionTagCodec, options);
    return makeVersionDispatchingCodec(family, { ...options, writeVersion });
}
/**
 * @privateRemarks - Exported for testing.
 */
export function makeMessageCodecs(changeCodecs, revisionTagCodec, options) {
    const v1Codec = makeV1CodecWithVersion(changeCodecs.resolve(1).json, revisionTagCodec, options, 1);
    return makeCodecFamily([
        // Back-compat: messages weren't always written with an explicit version field.
        [undefined, v1Codec],
        [1, v1Codec],
        [2, makeV1CodecWithVersion(changeCodecs.resolve(2).json, revisionTagCodec, options, 2)],
    ]);
}
function makeV1CodecWithVersion(changeCodec, revisionTagCodec, options, version) {
    return withSchemaValidation(Message(changeCodec.encodedSchema ?? 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,
                }),
                version,
            };
            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