"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sequenceFieldChangeCodecFactory = void 0;
const common_utils_1 = require("@fluidframework/common-utils");
const typebox_1 = require("@sinclair/typebox");
const util_1 = require("../../util");
const codec_1 = require("../../codec");
const treeTextCursor_1 = require("../treeTextCursor");
const format_1 = require("./format");
const sequenceFieldChangeCodecFactory = (childCodec) => (0, codec_1.makeCodecFamily)([[0, makeV0Codec(childCodec)]]);
exports.sequenceFieldChangeCodecFactory = sequenceFieldChangeCodecFactory;
function makeV0Codec(childCodec) {
    return {
        encode: (changeset) => {
            const jsonMarks = [];
            for (const mark of changeset) {
                // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
                const encodedMark = { ...mark };
                if (mark.changes !== undefined) {
                    encodedMark.changes = childCodec.encode(mark.changes);
                }
                const type = mark.type;
                switch (type) {
                    case format_1.NoopMarkType:
                    case "MoveIn":
                    case "ReturnTo":
                    case "Insert":
                    case "Delete":
                    case "MoveOut":
                    case "ReturnFrom":
                        break;
                    case "Revive": {
                        encodedMark.content =
                            mark.content.map(treeTextCursor_1.jsonableTreeFromCursor);
                        break;
                    }
                    case "Placeholder":
                        (0, util_1.fail)("Should not have placeholders in serialized changeset");
                    default:
                        (0, common_utils_1.unreachableCase)(type);
                }
                jsonMarks.push(encodedMark);
            }
            return jsonMarks;
        },
        decode: (changeset) => {
            const marks = [];
            const array = changeset;
            for (const mark of array) {
                // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
                const decodedMark = { ...mark };
                if (mark.changes !== undefined) {
                    decodedMark.changes = childCodec.decode(mark.changes);
                }
                const type = mark.type;
                switch (type) {
                    case format_1.NoopMarkType:
                    case "MoveIn":
                    case "ReturnTo":
                    case "Insert":
                    case "Delete":
                    case "MoveOut":
                    case "ReturnFrom":
                        break;
                    case "Revive": {
                        decodedMark.content =
                            mark.content.map(treeTextCursor_1.singleTextCursor);
                        break;
                    }
                    case "Placeholder":
                        (0, util_1.fail)("Should not have placeholders in serialized changeset");
                    default:
                        (0, common_utils_1.unreachableCase)(type);
                }
                marks.push(decodedMark);
            }
            return marks;
        },
        encodedSchema: (0, format_1.Changeset)(childCodec.encodedSchema ?? typebox_1.Type.Any()),
    };
}
//# sourceMappingURL=sequenceFieldChangeEncoder.js.map