/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { RevisionTagSchema } from "../../core/index.js";
import { ChangesetLocalIdSchema, EncodedChangeAtomId } from "../modular-schema/index.js";
import { unionOptions } from "../../codec/index.js";
export var DetachIdOverrideType;
(function (DetachIdOverrideType) {
    /**
     * The detach effect is the inverse of the prior attach characterized by the accompanying `CellId`'s revision and
     * local ID.
     *
     * An override is needed in such a case to ensure that rollbacks and undos return tree content to the appropriate
     * detached root. It is also needed to ensure that cell comparisons work properly for undos.
     */
    DetachIdOverrideType[DetachIdOverrideType["Unattach"] = 0] = "Unattach";
    /**
     * The detach effect is reapplying a prior detach.
     *
     * The accompanying cell ID is used in two ways:
     * - It indicates the location of the cell (including adjacent cell information) so that rebasing over this detach
     * can contribute the correct lineage information to the rebased mark.
     * - It specifies the revision and local ID that should be used to characterize the cell in the output context of
     * detach.
     */
    DetachIdOverrideType[DetachIdOverrideType["Redetach"] = 1] = "Redetach";
})(DetachIdOverrideType || (DetachIdOverrideType = {}));
const noAdditionalProps = { additionalProperties: false };
const CellCount = Type.Number({ multipleOf: 1, minimum: 1 });
const MoveId = ChangesetLocalIdSchema;
const HasMoveId = Type.Object({ id: MoveId });
const LineageEvent = Type.Tuple([
    RevisionTagSchema,
    ChangesetLocalIdSchema,
    /** count */
    CellCount,
    /** offset */
    Type.Number({ multipleOf: 1, minimum: 0 }),
]);
const HasLineage = Type.Object({ lineage: Type.Optional(Type.Array(LineageEvent)) });
const IdRange = Type.Tuple([ChangesetLocalIdSchema, CellCount]);
const CellId = Type.Composite([
    HasLineage,
    Type.Object({
        atom: EncodedChangeAtomId,
        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 DetachIdOverride = Type.Object({
    type: Type.Enum(DetachIdOverrideType),
    id: CellId,
}, noAdditionalProps);
const DetachFields = Type.Object({
    idOverride: Type.Optional(DetachIdOverride),
});
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({
    // TODO:AB6715 rename to `remove`
    delete: Type.Optional(Remove),
    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),
    // TODO:AB6715 rename to `remove`
    delete: Type.Optional(Remove),
    moveOut: Type.Optional(MoveOut),
    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