/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, unreachableCase } from "@fluidframework/core-utils/internal";
import { Type } from "@sinclair/typebox";
import { decodeBranchId, encodeBranchId } from "./branchIdCodec.js";
import { Message } from "./messageFormatVSharedBranches.js";
export function makeSharedBranchesCodecWithVersion(changeCodec, revisionTagCodec, version) {
    const schema = Message(changeCodec.encodedSchema ?? 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: encodeBranchId(context.idCompressor, message.branchId),
                        version,
                    };
                }
                case "branch": {
                    return {
                        originatorId: message.sessionId,
                        branchId: encodeBranchId(context.idCompressor, message.branchId),
                        version,
                    };
                }
                default: {
                    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 = decodeBranchId(context.idCompressor, encodedBranchId, changeContext);
            if (changeset === undefined) {
                return { type: "branch", sessionId: originatorId, branchId };
            }
            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,
            };
        },
    };
}
//# sourceMappingURL=messageCodecVSharedBranches.js.map