/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert, unreachableCase } from "@fluidframework/core-utils/internal";
import { DiscriminatedUnionDispatcher } from "../../codec/index.js";
import { fail } from "../../util/index.js";
import { makeChangeAtomIdCodec } from "../changeAtomIdCodec.js";
import { Changeset as ChangesetSchema } from "./formatV2.js";
import { NoopMarkType, } from "./types.js";
import { isNoopMark } from "./utils.js";
import { EncodedNodeChangeset } from "../modular-schema/index.js";
export function makeV2Codec(revisionTagCodec) {
    const changeAtomIdCodec = makeChangeAtomIdCodec(revisionTagCodec);
    const markEffectCodec = {
        encode(effect, context) {
            function encodeRevision(revision) {
                if (revision === undefined || revision === context.revision) {
                    return undefined;
                }
                return revisionTagCodec.encode(revision, context);
            }
            const type = effect.type;
            switch (type) {
                case "MoveIn":
                    return {
                        moveIn: {
                            revision: encodeRevision(effect.revision),
                            finalEndpoint: effect.finalEndpoint === undefined
                                ? undefined
                                : changeAtomIdCodec.encode(effect.finalEndpoint, context),
                            id: effect.id,
                        },
                    };
                case "Insert":
                    return {
                        insert: {
                            revision: encodeRevision(effect.revision),
                            id: effect.id,
                        },
                    };
                case "Remove":
                    return {
                        remove: {
                            revision: encodeRevision(effect.revision),
                            idOverride: effect.idOverride === undefined
                                ? undefined
                                : changeAtomIdCodec.encode(effect.idOverride, context),
                            id: effect.id,
                        },
                    };
                case "MoveOut":
                    return {
                        moveOut: {
                            revision: encodeRevision(effect.revision),
                            finalEndpoint: effect.finalEndpoint === undefined
                                ? undefined
                                : changeAtomIdCodec.encode(effect.finalEndpoint, context),
                            idOverride: effect.idOverride === undefined
                                ? undefined
                                : changeAtomIdCodec.encode(effect.idOverride, context),
                            id: effect.id,
                        },
                    };
                case "AttachAndDetach":
                    return {
                        attachAndDetach: {
                            attach: markEffectCodec.encode(effect.attach, context),
                            detach: markEffectCodec.encode(effect.detach, context),
                        },
                    };
                case NoopMarkType:
                    fail(`Mark type: ${type} should not be encoded.`);
                default:
                    unreachableCase(type);
            }
        },
        decode(encoded, context) {
            return decoderLibrary.dispatch(encoded, context);
        },
    };
    function decodeRevision(encodedRevision, context) {
        if (encodedRevision === undefined) {
            assert(context.revision !== undefined, 0x996 /* Implicit revision should be provided */);
            return context.revision;
        }
        return revisionTagCodec.decode(encodedRevision, context);
    }
    const decoderLibrary = new DiscriminatedUnionDispatcher({
        moveIn(encoded, context) {
            const { id, finalEndpoint, revision } = encoded;
            const mark = {
                type: "MoveIn",
                id,
            };
            mark.revision = decodeRevision(revision, context);
            if (finalEndpoint !== undefined) {
                mark.finalEndpoint = changeAtomIdCodec.decode(finalEndpoint, context);
            }
            return mark;
        },
        insert(encoded, context) {
            const { id, revision } = encoded;
            const mark = {
                type: "Insert",
                id,
            };
            mark.revision = decodeRevision(revision, context);
            return mark;
        },
        remove(encoded, context) {
            const { id, revision, idOverride } = encoded;
            const mark = {
                type: "Remove",
                id,
            };
            mark.revision = decodeRevision(revision, context);
            if (idOverride !== undefined) {
                mark.idOverride = changeAtomIdCodec.decode(idOverride, context);
            }
            return mark;
        },
        moveOut(encoded, context) {
            const { id, finalEndpoint, idOverride, revision } = encoded;
            const mark = {
                type: "MoveOut",
                id,
            };
            mark.revision = decodeRevision(revision, context);
            if (finalEndpoint !== undefined) {
                mark.finalEndpoint = changeAtomIdCodec.decode(finalEndpoint, context);
            }
            if (idOverride !== undefined) {
                mark.idOverride = changeAtomIdCodec.decode(idOverride, context);
            }
            return mark;
        },
        attachAndDetach(encoded, context) {
            return {
                type: "AttachAndDetach",
                attach: decoderLibrary.dispatch(encoded.attach, context),
                detach: decoderLibrary.dispatch(encoded.detach, 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 = changeAtomIdCodec.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 = changeAtomIdCodec.decode(mark.cellId, context.baseContext);
                }
                if (mark.changes !== undefined) {
                    decodedMark.changes = context.decodeNode(mark.changes);
                }
                marks.push(decodedMark);
            }
            return marks;
        },
        encodedSchema: ChangesetSchema(EncodedNodeChangeset),
    };
}
//# sourceMappingURL=sequenceFieldCodecV2.js.map