"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeV3Codec = void 0;
const index_js_1 = require("../../codec/index.js");
const index_js_2 = require("../modular-schema/index.js");
const formatV3_js_1 = require("./formatV3.js");
const sequenceFieldCodecV2_js_1 = require("./sequenceFieldCodecV2.js");
function makeV3Codec(revisionTagCodec) {
    const { changeAtomIdCodec: atomIdCodec, encodeMarkEffect: encodeV2MarkEffect, decoderLibrary: decoderLibraryV2, } = (0, sequenceFieldCodecV2_js_1.makeV2CodecHelpers)(revisionTagCodec);
    function encodeMarkEffect(mark, context) {
        const encoded = encodeV2MarkEffect(mark, context);
        const encodedRenameId = (0, sequenceFieldCodecV2_js_1.tryGetEncodedCellRename)(encoded);
        if (encodedRenameId !== undefined) {
            return {
                rename: {
                    idOverride: encodedRenameId,
                },
            };
        }
        return encoded;
    }
    function decodeMarkEffect(encoded, count, cellId, context) {
        return decoderLibrary.dispatch(encoded, count, cellId, context);
    }
    const decoderLibrary = new index_js_1.DiscriminatedUnionDispatcher({
        ...decoderLibraryV2,
        rename(encoded, count, cellId, context) {
            return {
                type: "Rename",
                idOverride: atomIdCodec.decode(encoded.idOverride, context.baseContext),
            };
        },
    });
    return {
        encode: (changeset, context) => (0, sequenceFieldCodecV2_js_1.encodeSequenceChangeset)(changeset, context, (revision) => (0, sequenceFieldCodecV2_js_1.encodeRevisionWithContext)(revision, context.baseContext, revisionTagCodec), atomIdCodec, encodeMarkEffect),
        decode: (changeset, context) => (0, sequenceFieldCodecV2_js_1.decodeSequenceChangeset)(changeset, context, atomIdCodec, decodeMarkEffect),
        encodedSchema: (0, formatV3_js_1.Changeset)(index_js_2.EncodedNodeChangeset),
    };
}
exports.makeV3Codec = makeV3Codec;
//# sourceMappingURL=sequenceFieldCodecV3.js.map