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