"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, makeV1Codec(changeCodecs.resolve(0), revisionTagCodec, options)]]);
}
exports.makeEditManagerCodecs = makeEditManagerCodecs;
function makeV1Codec(changeCodec, revisionTagCodec, options) {
    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 }),
        change: changeCodec.json.encode(commit.change, context),
    });
    const decodeCommit = (commit, context) => ({
        ...commit,
        revision: revisionTagCodec.decode(commit.revision, { originatorId: commit.sessionId }),
        change: changeCodec.json.decode(commit.change, context),
    });
    const codec = (0, index_js_1.withSchemaValidation)(format, {
        encode: (data, context) => {
            const json = {
                trunk: data.trunk.map((commit) => encodeCommit(commit, {
                    originatorId: commit.sessionId,
                    schema: context.schema,
                })),
                branches: Array.from(data.peerLocalBranches.entries(), ([sessionId, branch]) => [
                    sessionId,
                    {
                        base: revisionTagCodec.encode(branch.base, {
                            originatorId: sessionId,
                        }),
                        commits: branch.commits.map((commit) => encodeCommit(commit, {
                            originatorId: commit.sessionId,
                            schema: context.schema,
                        })),
                    },
                ]),
                version: editManagerFormat_js_1.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,
                })),
                peerLocalBranches: new Map((0, index_js_3.mapIterable)(json.branches, ([sessionId, branch]) => [
                    sessionId,
                    {
                        base: revisionTagCodec.decode(branch.base, {
                            originatorId: sessionId,
                        }),
                        commits: branch.commits.map((commit) => 
                        // TODO: sort out EncodedCommit vs Commit, and make this type check without `as`.
                        decodeCommit(commit, {
                            originatorId: commit.sessionId,
                        })),
                    },
                ])),
            };
        },
    }, 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