"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeV1CodecWithVersion = void 0;
const index_js_1 = require("../codec/index.js");
const index_js_2 = require("../util/index.js");
const editManagerFormatV1toV4_js_1 = require("./editManagerFormatV1toV4.js");
const editManagerCodecsCommons_js_1 = require("./editManagerCodecsCommons.js");
function makeV1CodecWithVersion(changeCodec, revisionTagCodec, options, version) {
    const format = (0, editManagerFormatV1toV4_js_1.EncodedEditManager)(changeCodec.json.encodedSchema ?? index_js_2.JsonCompatibleReadOnlySchema);
    const codec = (0, index_js_1.withSchemaValidation)(format, {
        encode: (data, context) => {
            const mainBranch = (0, editManagerCodecsCommons_js_1.encodeSharedBranch)(changeCodec, revisionTagCodec, data.main, context, data.originator);
            const json = {
                trunk: mainBranch.trunk,
                branches: mainBranch.peers,
                version,
            };
            return json;
        },
        decode: (json, context) => {
            return {
                main: (0, editManagerCodecsCommons_js_1.decodeSharedBranch)(changeCodec, revisionTagCodec, {
                    trunk: json.trunk,
                    peers: json.branches,
                }, context, 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;
}
exports.makeV1CodecWithVersion = makeV1CodecWithVersion;
//# sourceMappingURL=editManagerCodecsV1toV4.js.map