/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { unreachableCase } from "@fluidframework/common-utils";
import { Type } from "@sinclair/typebox";
import { fail } from "../../util";
import { makeCodecFamily } from "../../codec";
import { jsonableTreeFromCursor, singleTextCursor } from "../treeTextCursor";
import { Changeset, NoopMarkType } from "./format";
export const sequenceFieldChangeCodecFactory = (childCodec) => makeCodecFamily([[0, makeV0Codec(childCodec)]]);
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 NoopMarkType:
                    case "MoveIn":
                    case "ReturnTo":
                    case "Insert":
                    case "Delete":
                    case "MoveOut":
                    case "ReturnFrom":
                        break;
                    case "Revive": {
                        encodedMark.content =
                            mark.content.map(jsonableTreeFromCursor);
                        break;
                    }
                    case "Placeholder":
                        fail("Should not have placeholders in serialized changeset");
                    default:
                        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 NoopMarkType:
                    case "MoveIn":
                    case "ReturnTo":
                    case "Insert":
                    case "Delete":
                    case "MoveOut":
                    case "ReturnFrom":
                        break;
                    case "Revive": {
                        decodedMark.content =
                            mark.content.map(singleTextCursor);
                        break;
                    }
                    case "Placeholder":
                        fail("Should not have placeholders in serialized changeset");
                    default:
                        unreachableCase(type);
                }
                marks.push(decodedMark);
            }
            return marks;
        },
        encodedSchema: Changeset(childCodec.encodedSchema ?? Type.Any()),
    };
}
//# sourceMappingURL=sequenceFieldChangeEncoder.js.map