/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils";
import { JsonCompatibleReadOnlySchema, mapIterable } from "../util";
import { EncodedEditManager } from "./editManagerFormat";
export function makeEditManagerCodec(changeCodec, revisionTagCodec, { jsonValidator: validator }) {
    const format = validator.compile(EncodedEditManager(changeCodec.json.encodedSchema ?? JsonCompatibleReadOnlySchema));
    const encodeCommit = (commit) => ({
        ...commit,
        revision: revisionTagCodec.encode(commit.revision),
        change: changeCodec.json.encode(commit.change),
    });
    const decodeCommit = (commit) => ({
        ...commit,
        revision: revisionTagCodec.decode(commit.revision),
        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) },
                ]),
            };
            assert(format.check(json), 0x6cc /* Encoded schema should validate */);
            return json;
        },
        decode: (json) => {
            assert(format.check(json), 0x6cd /* Encoded schema should validate */);
            return {
                trunk: json.trunk.map(decodeCommit),
                branches: new Map(mapIterable(json.branches, ([sessionId, branch]) => [
                    sessionId,
                    {
                        ...branch,
                        base: revisionTagCodec.decode(branch.base),
                        commits: branch.commits.map(decodeCommit),
                    },
                ])),
            };
        },
    };
}
//# sourceMappingURL=editManagerCodecs.js.map