/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { RevisionTagSchema } from "../../core";
import { ChangesetLocalIdSchema, EncodedChangeAtomId } from "../modular-schema";
import { unionOptions } from "../../codec";
const noAdditionalProps = { additionalProperties: false };
const CellCount = Type.Number();
const MoveId = ChangesetLocalIdSchema;
const HasMoveId = Type.Object({ id: MoveId });
const LineageEvent = Type.Tuple([
    RevisionTagSchema,
    ChangesetLocalIdSchema,
    /* count */ Type.Number(),
    /* offset */ Type.Number(),
]);
const HasLineage = Type.Object({ lineage: Type.Optional(Type.Array(LineageEvent)) });
const IdRange = Type.Tuple([ChangesetLocalIdSchema, CellCount]);
const CellId = Type.Composite([
    EncodedChangeAtomId,
    HasLineage,
    Type.Object({ adjacentCells: Type.Optional(Type.Array(IdRange)) }),
], noAdditionalProps);
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 InverseAttachFields = Type.Object({
    detachIdOverride: Type.Optional(EncodedChangeAtomId),
});
const Delete = Type.Composite([
    Type.Object({
        id: ChangesetLocalIdSchema,
    }),
    HasRevisionTag,
    InverseAttachFields,
], noAdditionalProps);
const MoveOut = Type.Composite([HasMoveFields, InverseAttachFields], noAdditionalProps);
const MovePlaceholder = Type.Composite([HasMoveId, HasRevisionTag], noAdditionalProps);
const Attach = Type.Object({
    insert: Type.Optional(Insert),
    moveIn: Type.Optional(MoveIn),
}, unionOptions);
const Detach = Type.Object({
    delete: Type.Optional(Delete),
    moveOut: Type.Optional(MoveOut),
}, unionOptions);
const AttachAndDetach = Type.Object({
    attach: Attach,
    detach: Detach,
});
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),
    delete: Type.Optional(Delete),
    moveOut: Type.Optional(MoveOut),
    placeholder: Type.Optional(MovePlaceholder),
    attachAndDetach: Type.Optional(AttachAndDetach),
}, unionOptions);
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);
const Mark = (tNodeChange) => CellMark(MarkEffect, tNodeChange);
export const Changeset = (tNodeChange) => Type.Array(Mark(tNodeChange));
//# sourceMappingURL=format.js.map