"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeV2Codec = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../codec/index.js");
const index_js_2 = require("../../util/index.js");
const changeAtomIdCodec_js_1 = require("../changeAtomIdCodec.js");
const formatV2_js_1 = require("./formatV2.js");
const types_js_1 = require("./types.js");
const utils_js_1 = require("./utils.js");
const index_js_3 = require("../modular-schema/index.js");
function makeV2Codec(revisionTagCodec) {
    const changeAtomIdCodec = (0, changeAtomIdCodec_js_1.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 types_js_1.NoopMarkType:
                    (0, index_js_2.fail)(`Mark type: ${type} should not be encoded.`);
                default:
                    (0, internal_1.unreachableCase)(type);
            }
        },
        decode(encoded, context) {
            return decoderLibrary.dispatch(encoded, context);
        },
    };
    function decodeRevision(encodedRevision, context) {
        if (encodedRevision === undefined) {
            (0, internal_1.assert)(context.revision !== undefined, 0x996 /* Implicit revision should be provided */);
            return context.revision;
        }
        return revisionTagCodec.decode(encodedRevision, context);
    }
    const decoderLibrary = new index_js_1.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 (!(0, utils_js_1.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: (0, formatV2_js_1.Changeset)(index_js_3.EncodedNodeChangeset),
    };
}
exports.makeV2Codec = makeV2Codec;
//# sourceMappingURL=sequenceFieldCodecV2.js.map