/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { DiscriminatedUnionDispatcher } from "../../codec/index.js";
import { EncodedNodeChangeset, } from "../modular-schema/index.js";
import { Changeset as ChangesetSchema } from "./formatV3.js";
import { decodeSequenceChangeset, encodeRevisionWithContext, encodeSequenceChangeset, makeV2CodecHelpers, tryGetEncodedCellRename, } from "./sequenceFieldCodecV2.js";
export function makeV3Codec(revisionTagCodec) {
    const { changeAtomIdCodec: atomIdCodec, encodeMarkEffect: encodeV2MarkEffect, decoderLibrary: decoderLibraryV2, } = makeV2CodecHelpers(revisionTagCodec);
    function encodeMarkEffect(mark, context) {
        const encoded = encodeV2MarkEffect(mark, context);
        const encodedRenameId = 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 DiscriminatedUnionDispatcher({
        ...decoderLibraryV2,
        rename(encoded, count, cellId, context) {
            return {
                type: "Rename",
                idOverride: atomIdCodec.decode(encoded.idOverride, context.baseContext),
            };
        },
    });
    return {
        encode: (changeset, context) => encodeSequenceChangeset(changeset, context, (revision) => encodeRevisionWithContext(revision, context.baseContext, revisionTagCodec), atomIdCodec, encodeMarkEffect),
        decode: (changeset, context) => decodeSequenceChangeset(changeset, context, atomIdCodec, decodeMarkEffect),
        encodedSchema: ChangesetSchema(EncodedNodeChangeset),
    };
}
//# sourceMappingURL=sequenceFieldCodecV3.js.map