"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeEditManagerCodecs = exports.makeEditManagerCodec = void 0;
const index_js_1 = require("../codec/index.js");
const index_js_2 = require("../codec/index.js");
const index_js_3 = require("../util/index.js");
const editManagerFormat_js_1 = require("./editManagerFormat.js");
function makeEditManagerCodec(changeCodecs, revisionTagCodec, options, writeVersion) {
    const family = makeEditManagerCodecs(changeCodecs, revisionTagCodec, options);
    return (0, index_js_2.makeVersionDispatchingCodec)(family, { ...options, writeVersion });
}
exports.makeEditManagerCodec = makeEditManagerCodec;
function makeEditManagerCodecs(changeCodecs, revisionTagCodec, options) {
    return (0, index_js_1.makeCodecFamily)([
        [1, makeV1CodecWithVersion(changeCodecs.resolve(1), revisionTagCodec, options, 1)],
        [2, makeV1CodecWithVersion(changeCodecs.resolve(2), revisionTagCodec, options, 2)],
        [3, makeV1CodecWithVersion(changeCodecs.resolve(3), revisionTagCodec, options, 3)],
    ]);
}
exports.makeEditManagerCodecs = makeEditManagerCodecs;
function makeV1CodecWithVersion(changeCodec, revisionTagCodec, options, version) {
    const format = (0, editManagerFormat_js_1.EncodedEditManager)(changeCodec.json.encodedSchema ?? index_js_3.JsonCompatibleReadOnlySchema);
    const encodeCommit = (commit, context) => ({
        ...commit,
        revision: revisionTagCodec.encode(commit.revision, {
            originatorId: commit.sessionId,
            idCompressor: context.idCompressor,
            revision: undefined,
        }),
        change: changeCodec.json.encode(commit.change, { ...context, revision: commit.revision }),
    });
    const decodeCommit = (commit, context) => {
        const revision = revisionTagCodec.decode(commit.revision, {
            originatorId: commit.sessionId,
            idCompressor: context.idCompressor,
            revision: undefined,
        });
        return {
            ...commit,
            revision,
            change: changeCodec.json.decode(commit.change, { ...context, revision }),
        };
    };
    const codec = (0, index_js_1.withSchemaValidation)(format, {
        encode: (data, context) => {
            const json = {
                trunk: data.trunk.map((commit) => encodeCommit(commit, {
                    originatorId: commit.sessionId,
                    idCompressor: context.idCompressor,
                    schema: context.schema,
                    revision: undefined,
                })),
                branches: Array.from(data.peerLocalBranches.entries(), ([sessionId, branch]) => [
                    sessionId,
                    {
                        base: revisionTagCodec.encode(branch.base, {
                            originatorId: sessionId,
                            idCompressor: context.idCompressor,
                            revision: undefined,
                        }),
                        commits: branch.commits.map((commit) => encodeCommit(commit, {
                            originatorId: commit.sessionId,
                            idCompressor: context.idCompressor,
                            schema: context.schema,
                            revision: undefined,
                        })),
                    },
                ]),
                version,
            };
            return json;
        },
        decode: (json, context) => {
            // TODO: sort out EncodedCommit vs Commit, and make this type check without `any`.
            // eslint-disable-next-line @typescript-eslint/no-explicit-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,
                    idCompressor: context.idCompressor,
                    revision: undefined,
                })),
                peerLocalBranches: new Map((0, index_js_3.mapIterable)(json.branches, ([sessionId, branch]) => [
                    sessionId,
                    {
                        base: revisionTagCodec.decode(branch.base, {
                            originatorId: sessionId,
                            idCompressor: context.idCompressor,
                            revision: undefined,
                        }),
                        commits: branch.commits.map((commit) => 
                        // TODO: sort out EncodedCommit vs Commit, and make this type check without `as`.
                        decodeCommit(commit, {
                            originatorId: commit.sessionId,
                            idCompressor: context.idCompressor,
                            revision: undefined,
                        })),
                    },
                ])),
            };
        },
    }, options.jsonValidator);
    // 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