"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeSharedBranchesCodecWithVersion = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const typebox_1 = require("@sinclair/typebox");
const branchIdCodec_js_1 = require("./branchIdCodec.js");
const messageFormatVSharedBranches_js_1 = require("./messageFormatVSharedBranches.js");
function makeSharedBranchesCodecWithVersion(changeCodec, revisionTagCodec, version) {
    const schema = (0, messageFormatVSharedBranches_js_1.Message)(changeCodec.encodedSchema ?? typebox_1.Type.Any());
    return {
        schema,
        encode: (message, context) => {
            const type = message.type;
            switch (type) {
                case "commit": {
                    const changeContext = {
                        originatorId: message.sessionId,
                        schema: context.schema,
                        idCompressor: context.idCompressor,
                        revision: message.commit.revision,
                        isSummary: false,
                    };
                    return {
                        revision: revisionTagCodec.encode(message.commit.revision, {
                            originatorId: message.sessionId,
                            idCompressor: context.idCompressor,
                            revision: undefined,
                            isSummary: false,
                        }),
                        originatorId: message.sessionId,
                        changeset: changeCodec.encode(message.commit.change, changeContext),
                        branchId: (0, branchIdCodec_js_1.encodeBranchId)(context.idCompressor, message.branchId),
                        version,
                    };
                }
                case "branch": {
                    return {
                        originatorId: message.sessionId,
                        branchId: (0, branchIdCodec_js_1.encodeBranchId)(context.idCompressor, message.branchId),
                        version,
                    };
                }
                default: {
                    (0, internal_1.unreachableCase)(type);
                }
            }
        },
        decode: (encoded, context) => {
            const { revision: encodedRevision, originatorId, changeset, branchId: encodedBranchId, } = encoded;
            const changeContext = {
                originatorId,
                revision: undefined,
                idCompressor: context.idCompressor,
                isSummary: false,
            };
            const branchId = (0, branchIdCodec_js_1.decodeBranchId)(context.idCompressor, encodedBranchId, changeContext);
            if (changeset === undefined) {
                return { type: "branch", sessionId: originatorId, branchId };
            }
            (0, internal_1.assert)(encodedRevision !== undefined, 0xc6a /* Commit messages must have a revision */);
            const revision = revisionTagCodec.decode(encodedRevision, changeContext);
            return {
                type: "commit",
                commit: {
                    revision,
                    change: changeCodec.decode(changeset, {
                        originatorId,
                        revision,
                        idCompressor: context.idCompressor,
                        isSummary: false,
                    }),
                },
                branchId,
                sessionId: originatorId,
            };
        },
    };
}
exports.makeSharedBranchesCodecWithVersion = makeSharedBranchesCodecWithVersion;
//# sourceMappingURL=messageCodecVSharedBranches.js.map