"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 index_js_1 = require("../util/index.js");
const editManagerCodecsCommons_js_1 = require("./editManagerCodecsCommons.js");
const editManagerFormatVSharedBranches_js_1 = require("./editManagerFormatVSharedBranches.js");
function makeSharedBranchesCodecWithVersion(changeCodec, revisionTagCodec, version) {
    const schema = (0, editManagerFormatVSharedBranches_js_1.EncodedEditManager)(changeCodec.encodedSchema ?? index_js_1.JsonCompatibleReadOnlySchema);
    const codec = {
        schema,
        encode: (data, context) => {
            const mainBranch = (0, editManagerCodecsCommons_js_1.encodeSharedBranch)(changeCodec, revisionTagCodec, data.main, context, data.originator);
            (0, internal_1.assert)(data.originator !== undefined, 0xca5 /* Cannot encode vSharedBranches summary without originator */);
            const json = {
                main: mainBranch,
                originator: data.originator,
                version,
            };
            if (data.branches !== undefined && data.branches.size > 0) {
                const branches = [];
                for (const [_, branch] of data.branches) {
                    branches.push((0, editManagerCodecsCommons_js_1.encodeSharedBranch)(changeCodec, revisionTagCodec, branch, context, data.originator));
                }
                json.branches = branches;
            }
            return json;
        },
        decode: (json, context) => {
            const mainBranch = (0, editManagerCodecsCommons_js_1.decodeSharedBranch)(changeCodec, revisionTagCodec, json.main, context, json.originator);
            const decoded = {
                main: mainBranch,
                originator: json.originator,
            };
            if (json.branches !== undefined) {
                const branches = new Map();
                for (const branch of json.branches) {
                    const decodedBranch = (0, editManagerCodecsCommons_js_1.decodeSharedBranch)(changeCodec, revisionTagCodec, branch, context, json.originator);
                    (0, internal_1.assert)(decodedBranch.id !== undefined, 0xc66 /* Shared branches must have an id */);
                    (0, internal_1.assert)(!branches.has(decodedBranch.id), 0xc67 /* Duplicate shared branch id */);
                    branches.set(decodedBranch.id, decodedBranch);
                }
                decoded.branches = branches;
            }
            return decoded;
        },
    };
    return codec;
}
exports.makeSharedBranchesCodecWithVersion = makeSharedBranchesCodecWithVersion;
//# sourceMappingURL=editManagerCodecsVSharedBranches.js.map