/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { DiscriminatedUnionDispatcher } from "../../codec/index.js";
import { Changeset as ChangesetSchema } from "./formatV3.js";
import { isNoopMark } from "./utils.js";
import { EncodedNodeChangeset } from "../modular-schema/index.js";
import { makeV2CodecHelpers } from "./sequenceFieldCodecV2.js";
export function makeV3Codec(revisionTagCodec) {
    const { changeAtomIdCodec: atomIdCodec, markEffectCodec: markEffectV2Codec, decoderLibrary: decoderLibraryV2, } = makeV2CodecHelpers(revisionTagCodec);
    const markEffectCodec = {
        encode(effect, context) {
            const type = effect.type;
            switch (type) {
                case "Rename":
                    return {
                        rename: {
                            idOverride: atomIdCodec.encode(effect.idOverride, context),
                        },
                    };
                default:
                    return markEffectV2Codec.encode(effect, context);
            }
        },
        decode(encoded, context) {
            return decoderLibrary.dispatch(encoded, context);
        },
    };
    const decoderLibrary = new DiscriminatedUnionDispatcher({
        ...decoderLibraryV2,
        rename(encoded, context) {
            return {
                type: "Rename",
                idOverride: atomIdCodec.decode(encoded.idOverride, context),
            };
        },
    });
    return {
        encode: (changeset, context) => {
            const jsonMarks = [];
            for (const mark of changeset) {
                const encodedMark = {
                    count: mark.count,
                };
                if (!isNoopMark(mark)) {
                    encodedMark.effect = markEffectCodec.encode(mark, context.baseContext);
                }
                if (mark.cellId !== undefined) {
                    encodedMark.cellId = atomIdCodec.encode(mark.cellId, context.baseContext);
                }
                if (mark.changes !== undefined) {
                    encodedMark.changes = context.encodeNode(mark.changes);
                }
                jsonMarks.push(encodedMark);
            }
            return jsonMarks;
        },
        decode: (changeset, context) => {
            const marks = [];
            for (const mark of changeset) {
                const decodedMark = {
                    count: mark.count,
                };
                if (mark.effect !== undefined) {
                    Object.assign(decodedMark, markEffectCodec.decode(mark.effect, context.baseContext));
                }
                if (mark.cellId !== undefined) {
                    decodedMark.cellId = atomIdCodec.decode(mark.cellId, context.baseContext);
                }
                if (mark.changes !== undefined) {
                    decodedMark.changes = context.decodeNode(mark.changes);
                }
                marks.push(decodedMark);
            }
            return marks;
        },
        encodedSchema: ChangesetSchema(EncodedNodeChangeset),
    };
}
//# sourceMappingURL=sequenceFieldCodecV3.js.map