"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeMessageCodecs = 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(changeCodecs, revisionTagCodec, options, writeVersion) {
    const family = makeMessageCodecs(changeCodecs, revisionTagCodec, options);
    return (0, index_js_1.makeVersionDispatchingCodec)(family, { ...options, writeVersion });
}
exports.makeMessageCodec = makeMessageCodec;
/**
 * @privateRemarks Exported for testing.
 */
function makeMessageCodecs(changeCodecs, revisionTagCodec, options) {
    const v1Codec = makeV1CodecWithVersion(changeCodecs.resolve(1).json, revisionTagCodec, options, 1);
    return (0, index_js_1.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)],
        [3, makeV1CodecWithVersion(changeCodecs.resolve(3).json, revisionTagCodec, options, 3)],
        [4, makeV1CodecWithVersion(changeCodecs.resolve(4).json, revisionTagCodec, options, 4)],
    ]);
}
exports.makeMessageCodecs = makeMessageCodecs;
function makeV1CodecWithVersion(changeCodec, revisionTagCodec, options, version) {
    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,
                    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 {
                commit: {
                    revision,
                    change: changeCodec.decode(changeset, {
                        originatorId,
                        revision,
                        idCompressor: context.idCompressor,
                    }),
                },
                sessionId: originatorId,
            };
        },
    }, options.jsonValidator);
}
//# sourceMappingURL=messageCodecs.js.map