"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 formatV3_js_1 = require("./formatV3.js");
const utils_js_1 = require("./utils.js");
const index_js_2 = require("../modular-schema/index.js");
const sequenceFieldCodecV2_js_1 = require("./sequenceFieldCodecV2.js");
function makeV3Codec(revisionTagCodec) {
    const { changeAtomIdCodec: atomIdCodec, markEffectCodec: markEffectV2Codec, decoderLibrary: decoderLibraryV2, } = (0, sequenceFieldCodecV2_js_1.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 index_js_1.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 (!(0, utils_js_1.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: (0, formatV3_js_1.Changeset)(index_js_2.EncodedNodeChangeset),
    };
}
exports.makeV3Codec = makeV3Codec;
//# sourceMappingURL=sequenceFieldCodecV3.js.map