"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Changeset = exports.DetachIdOverrideType = void 0;
const typebox_1 = require("@sinclair/typebox");
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../modular-schema/index.js");
const index_js_3 = require("../../codec/index.js");
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 || (exports.DetachIdOverrideType = DetachIdOverrideType = {}));
const noAdditionalProps = { additionalProperties: false };
const CellCount = typebox_1.Type.Number({ multipleOf: 1, minimum: 1 });
const MoveId = index_js_2.ChangesetLocalIdSchema;
const HasMoveId = typebox_1.Type.Object({ id: MoveId });
const LineageEvent = typebox_1.Type.Tuple([
    index_js_1.RevisionTagSchema,
    index_js_2.ChangesetLocalIdSchema,
    /** count */
    CellCount,
    /** offset */
    typebox_1.Type.Number({ multipleOf: 1, minimum: 0 }),
]);
const HasLineage = typebox_1.Type.Object({ lineage: typebox_1.Type.Optional(typebox_1.Type.Array(LineageEvent)) });
const IdRange = typebox_1.Type.Tuple([index_js_2.ChangesetLocalIdSchema, CellCount]);
const CellId = typebox_1.Type.Composite([
    HasLineage,
    typebox_1.Type.Object({
        atom: index_js_2.EncodedChangeAtomId,
        adjacentCells: typebox_1.Type.Optional(typebox_1.Type.Array(IdRange)),
    }),
], noAdditionalProps);
const HasRevisionTag = typebox_1.Type.Object({ revision: typebox_1.Type.Optional(index_js_1.RevisionTagSchema) });
const Insert = typebox_1.Type.Composite([HasMoveId, HasRevisionTag], noAdditionalProps);
const HasMoveFields = typebox_1.Type.Composite([
    HasMoveId,
    HasRevisionTag,
    typebox_1.Type.Object({ finalEndpoint: typebox_1.Type.Optional(index_js_2.EncodedChangeAtomId) }),
]);
const MoveIn = typebox_1.Type.Composite([HasMoveFields], noAdditionalProps);
const DetachIdOverride = typebox_1.Type.Object({
    type: typebox_1.Type.Enum(DetachIdOverrideType),
    id: CellId,
}, noAdditionalProps);
const DetachFields = typebox_1.Type.Object({
    idOverride: typebox_1.Type.Optional(DetachIdOverride),
});
const Remove = typebox_1.Type.Composite([
    typebox_1.Type.Object({
        id: index_js_2.ChangesetLocalIdSchema,
    }),
    HasRevisionTag,
    DetachFields,
], noAdditionalProps);
const MoveOut = typebox_1.Type.Composite([HasMoveFields, DetachFields], noAdditionalProps);
const Attach = typebox_1.Type.Object({
    insert: typebox_1.Type.Optional(Insert),
    moveIn: typebox_1.Type.Optional(MoveIn),
}, index_js_3.unionOptions);
const Detach = typebox_1.Type.Object({
    // TODO:AB6715 rename to `remove`
    delete: typebox_1.Type.Optional(Remove),
    moveOut: typebox_1.Type.Optional(MoveOut),
}, index_js_3.unionOptions);
const AttachAndDetach = typebox_1.Type.Object({
    attach: Attach,
    detach: Detach,
});
const MarkEffect = typebox_1.Type.Object({
    // Note: `noop` is encoded by omitting `effect` from the encoded cell mark, so is not included here.
    insert: typebox_1.Type.Optional(Insert),
    moveIn: typebox_1.Type.Optional(MoveIn),
    // TODO:AB6715 rename to `remove`
    delete: typebox_1.Type.Optional(Remove),
    moveOut: typebox_1.Type.Optional(MoveOut),
    attachAndDetach: typebox_1.Type.Optional(AttachAndDetach),
}, index_js_3.unionOptions);
const CellMark = (tMark, tNodeChange) => typebox_1.Type.Object({
    // If undefined, indicates a Noop mark.
    effect: typebox_1.Type.Optional(tMark),
    cellId: typebox_1.Type.Optional(CellId),
    changes: typebox_1.Type.Optional(tNodeChange),
    count: CellCount,
}, noAdditionalProps);
const Mark = (tNodeChange) => CellMark(MarkEffect, tNodeChange);
const Changeset = (tNodeChange) => typebox_1.Type.Array(Mark(tNodeChange));
exports.Changeset = Changeset;
//# sourceMappingURL=format.js.map