/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { unionOptions } from "../../codec/index.js";
import { RevisionTagSchema } from "../../core/index.js";
import { ChangesetLocalIdSchema, EncodedChangeAtomId } from "../modular-schema/index.js";
const noAdditionalProps = { additionalProperties: false };
const CellCount = Type.Number({ multipleOf: 1, minimum: 1 });
const MoveId = ChangesetLocalIdSchema;
const HasMoveId = Type.Object({ id: MoveId });
const IdRange = Type.Tuple([ChangesetLocalIdSchema, CellCount]);
export const CellId = EncodedChangeAtomId;
const HasRevisionTag = Type.Object({ revision: Type.Optional(RevisionTagSchema) });
const Insert = Type.Composite([HasMoveId, HasRevisionTag], noAdditionalProps);
const HasMoveFields = Type.Composite([
    HasMoveId,
    HasRevisionTag,
    Type.Object({ finalEndpoint: Type.Optional(EncodedChangeAtomId) }),
]);
const MoveIn = Type.Composite([HasMoveFields], noAdditionalProps);
const DetachFields = Type.Object({
    idOverride: Type.Optional(CellId),
});
const Remove = Type.Composite([
    Type.Object({
        id: ChangesetLocalIdSchema,
    }),
    HasRevisionTag,
    DetachFields,
], noAdditionalProps);
const MoveOut = Type.Composite([HasMoveFields, DetachFields], noAdditionalProps);
const Attach = Type.Object({
    insert: Type.Optional(Insert),
    moveIn: Type.Optional(MoveIn),
}, unionOptions);
const Detach = Type.Object({
    remove: Type.Optional(Remove),
    moveOut: Type.Optional(MoveOut),
}, unionOptions);
const AttachAndDetach = Type.Object({
    attach: Attach,
    detach: Detach,
});
export const MarkEffect = Type.Object({
    // Note: `noop` is encoded by omitting `effect` from the encoded cell mark, so is not included here.
    insert: Type.Optional(Insert),
    moveIn: Type.Optional(MoveIn),
    remove: Type.Optional(Remove),
    moveOut: Type.Optional(MoveOut),
    attachAndDetach: Type.Optional(AttachAndDetach),
}, unionOptions);
export const CellMark = (tMark, tNodeChange) => Type.Object({
    // If undefined, indicates a Noop mark.
    effect: Type.Optional(tMark),
    cellId: Type.Optional(CellId),
    changes: Type.Optional(tNodeChange),
    count: CellCount,
}, noAdditionalProps);
// Return type is intentionally derived.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const Mark = (tNodeChange) => CellMark(MarkEffect, tNodeChange);
// Return type is intentionally derived.
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const Changeset = (tNodeChange) => Type.Array(Mark(tNodeChange));
//# sourceMappingURL=formatV2.js.map