"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeEditManagerCodec = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const util_1 = require("../util");
const editManagerFormat_1 = require("./editManagerFormat");
function makeEditManagerCodec(changeCodec, { jsonValidator: validator }) {
    const format = validator.compile((0, editManagerFormat_1.EncodedEditManager)(changeCodec.json.encodedSchema ?? util_1.JsonCompatibleReadOnlySchema));
    const encodeCommit = (commit) => ({
        ...commit,
        change: changeCodec.json.encode(commit.change),
    });
    const decodeCommit = (commit) => ({
        ...commit,
        change: changeCodec.json.decode(commit.change),
    });
    return {
        encode: (data) => {
            const json = {
                trunk: data.trunk.map(encodeCommit),
                branches: Array.from(data.branches.entries(), ([sessionId, branch]) => [
                    sessionId,
                    { ...branch, commits: branch.commits.map(encodeCommit) },
                ]),
            };
            (0, common_utils_1.assert)(format.check(json), 0x6cc /* Encoded schema should validate */);
            return json;
        },
        decode: (json) => {
            (0, common_utils_1.assert)(format.check(json), 0x6cd /* Encoded schema should validate */);
            return {
                trunk: json.trunk.map(decodeCommit),
                branches: new Map((0, util_1.mapIterable)(json.branches, ([sessionId, branch]) => [
                    sessionId,
                    { ...branch, commits: branch.commits.map(decodeCommit) },
                ])),
            };
        },
    };
}
exports.makeEditManagerCodec = makeEditManagerCodec;
//# sourceMappingURL=editManagerCodecs.js.map