/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { makeVersionedValidatedCodec, } from "../codec";
import { JsonCompatibleReadOnlySchema, mapIterable } from "../util";
import { EncodedEditManager, version, } from "./editManagerFormat";
export function makeEditManagerCodec(changeCodec, revisionTagCodec, options) {
    const format = EncodedEditManager(changeCodec.json.encodedSchema ?? JsonCompatibleReadOnlySchema);
    const encodeCommit = (commit, context) => ({
        ...commit,
        revision: revisionTagCodec.encode(commit.revision, commit.sessionId),
        change: changeCodec.json.encode(commit.change, context),
    });
    const decodeCommit = (commit, context) => ({
        ...commit,
        revision: revisionTagCodec.decode(commit.revision, commit.sessionId),
        change: changeCodec.json.decode(commit.change, context),
    });
    const codec = makeVersionedValidatedCodec(options, new Set([version]), format, {
        encode: (data) => {
            const json = {
                trunk: data.trunk.map((commit) => encodeCommit(commit, { originatorId: commit.sessionId })),
                branches: Array.from(data.branches.entries(), ([sessionId, branch]) => [
                    sessionId,
                    {
                        base: revisionTagCodec.encode(branch.base, sessionId),
                        commits: branch.commits.map((commit) => encodeCommit(commit, { originatorId: commit.sessionId })),
                    },
                ]),
                version,
            };
            return json;
        },
        decode: (json) => {
            // TODO: sort out EncodedCommit vs Commit, and make this type check without `any`.
            const trunk = json.trunk;
            return {
                trunk: trunk.map((commit) => 
                // TODO: sort out EncodedCommit vs Commit, and make this type check without `as`.
                // eslint-disable-next-line @typescript-eslint/no-unsafe-return
                decodeCommit(commit, {
                    originatorId: commit.sessionId,
                })),
                branches: new Map(mapIterable(json.branches, ([sessionId, branch]) => [
                    sessionId,
                    {
                        base: revisionTagCodec.decode(branch.base, sessionId),
                        commits: branch.commits.map((commit) => 
                        // TODO: sort out EncodedCommit vs Commit, and make this type check without `as`.
                        decodeCommit(commit, {
                            originatorId: commit.sessionId,
                        })),
                    },
                ])),
            };
        },
    });
    // TODO: makeVersionedValidatedCodec and withSchemaValidation should allow the codec to decode JsonCompatibleReadOnly, or Versioned or something like that,
    // and not leak the internal encoded format in the API surface.
    // Fixing that would remove the need for this cast.
    return codec;
}
//# sourceMappingURL=editManagerCodecs.js.map