"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.Mark = exports.Detach = exports.Attach = exports.Reattach = exports.NewAttach = exports.ReturnFrom = exports.ReturnTo = exports.Revive = exports.MoveOut = exports.Delete = exports.InverseAttachFields = exports.MoveIn = exports.Insert = exports.CanBeTransient = exports.Transient = exports.HasRevisionTag = exports.NoopMark = exports.HasReattachFields = exports.HasMarkFields = exports.CellId = exports.IdRange = exports.HasLineage = exports.LineageEvent = exports.HasMoveId = exports.MoveId = exports.NoopMarkType = exports.CellCount = exports.ProtoNode = void 0;
const typebox_1 = require("@sinclair/typebox");
const core_1 = require("../../core");
const modular_schema_1 = require("../modular-schema");
// TODO:AB#4259 Decouple types used for sequence-field's in-memory representation from their encoded variants.
// Currently, types in this file are largely used for both.
// See for example `Revive` whose type uses ITreeCursorSynchronous,
// but the schema for the serialized type uses ProtoNode (which is the result of serializing that cursor).
const noAdditionalProps = { additionalProperties: false };
exports.ProtoNode = typebox_1.Type.Any();
exports.CellCount = typebox_1.Type.Number();
/**
 * Left undefined for terseness.
 */
exports.NoopMarkType = undefined;
exports.MoveId = modular_schema_1.ChangesetLocalIdSchema;
exports.HasMoveId = typebox_1.Type.Object({ id: exports.MoveId });
// Boolean encodings can use this alternative to save space for frequently false values.
const OptionalTrue = typebox_1.Type.Optional(typebox_1.Type.Literal(true));
exports.LineageEvent = typebox_1.Type.Object({
    revision: typebox_1.Type.Readonly(core_1.RevisionTagSchema),
    id: typebox_1.Type.Readonly(modular_schema_1.ChangesetLocalIdSchema),
    count: typebox_1.Type.Readonly(typebox_1.Type.Number()),
    offset: typebox_1.Type.Readonly(typebox_1.Type.Number()),
}, noAdditionalProps);
exports.HasLineage = typebox_1.Type.Object({ lineage: typebox_1.Type.Optional(typebox_1.Type.Array(exports.LineageEvent)) });
exports.IdRange = typebox_1.Type.Object({
    id: modular_schema_1.ChangesetLocalIdSchema,
    count: exports.CellCount,
});
exports.CellId = typebox_1.Type.Composite([modular_schema_1.EncodedChangeAtomId, exports.HasLineage, typebox_1.Type.Object({ adjacentCells: typebox_1.Type.Optional(exports.IdRange) })], noAdditionalProps);
const HasMarkFields = (tNodeChange) => typebox_1.Type.Object({
    cellId: typebox_1.Type.Optional(exports.CellId),
    changes: typebox_1.Type.Optional(tNodeChange),
    count: exports.CellCount,
});
exports.HasMarkFields = HasMarkFields;
const HasReattachFields = (tNodeChange) => typebox_1.Type.Composite([
    typebox_1.Type.Object({
        inverseOf: typebox_1.Type.Optional(core_1.RevisionTagSchema),
    }),
    (0, exports.HasMarkFields)(tNodeChange),
]);
exports.HasReattachFields = HasReattachFields;
const NoopMark = (tNodeChange) => typebox_1.Type.Composite([(0, exports.HasMarkFields)(tNodeChange)], noAdditionalProps);
exports.NoopMark = NoopMark;
exports.HasRevisionTag = typebox_1.Type.Object({ revision: typebox_1.Type.Optional(core_1.RevisionTagSchema) });
exports.Transient = typebox_1.Type.Object({ detachedBy: modular_schema_1.EncodedChangeAtomId });
exports.CanBeTransient = typebox_1.Type.Partial(exports.Transient);
const Insert = (tNodeChange) => typebox_1.Type.Composite([
    (0, exports.HasMarkFields)(tNodeChange),
    exports.HasRevisionTag,
    exports.CanBeTransient,
    typebox_1.Type.Object({
        type: typebox_1.Type.Literal("Insert"),
        content: typebox_1.Type.Array(exports.ProtoNode),
    }),
], noAdditionalProps);
exports.Insert = Insert;
exports.MoveIn = typebox_1.Type.Composite([
    exports.HasMoveId,
    (0, exports.HasMarkFields)(typebox_1.Type.Undefined()),
    exports.HasRevisionTag,
    typebox_1.Type.Object({
        type: typebox_1.Type.Literal("MoveIn"),
        isSrcConflicted: OptionalTrue,
    }),
], noAdditionalProps);
exports.InverseAttachFields = typebox_1.Type.Object({
    detachIdOverride: typebox_1.Type.Optional(modular_schema_1.EncodedChangeAtomId),
});
const Delete = (tNodeChange) => typebox_1.Type.Composite([
    exports.HasRevisionTag,
    (0, exports.HasMarkFields)(tNodeChange),
    exports.InverseAttachFields,
    typebox_1.Type.Object({
        type: typebox_1.Type.Literal("Delete"),
        id: modular_schema_1.ChangesetLocalIdSchema,
    }),
], noAdditionalProps);
exports.Delete = Delete;
const MoveOut = (tNodeChange) => typebox_1.Type.Composite([
    exports.HasRevisionTag,
    exports.HasMoveId,
    (0, exports.HasMarkFields)(tNodeChange),
    typebox_1.Type.Object({
        type: typebox_1.Type.Literal("MoveOut"),
    }),
], noAdditionalProps);
exports.MoveOut = MoveOut;
const Revive = (tNodeChange) => typebox_1.Type.Composite([
    (0, exports.HasReattachFields)(tNodeChange),
    exports.HasRevisionTag,
    exports.CanBeTransient,
    typebox_1.Type.Object({
        type: typebox_1.Type.Literal("Revive"),
        content: typebox_1.Type.Array(exports.ProtoNode),
    }),
], noAdditionalProps);
exports.Revive = Revive;
exports.ReturnTo = typebox_1.Type.Composite([
    (0, exports.HasReattachFields)(typebox_1.Type.Undefined()),
    exports.HasRevisionTag,
    exports.HasMoveId,
    typebox_1.Type.Object({
        type: typebox_1.Type.Literal("ReturnTo"),
        isSrcConflicted: OptionalTrue,
    }),
], noAdditionalProps);
const ReturnFrom = (tNodeChange) => typebox_1.Type.Composite([
    exports.HasRevisionTag,
    exports.HasMoveId,
    (0, exports.HasMarkFields)(tNodeChange),
    exports.InverseAttachFields,
    typebox_1.Type.Object({
        type: typebox_1.Type.Literal("ReturnFrom"),
        isDstConflicted: OptionalTrue,
    }),
], noAdditionalProps);
exports.ReturnFrom = ReturnFrom;
const NewAttach = (tNodeChange) => typebox_1.Type.Union([(0, exports.Insert)(tNodeChange), exports.MoveIn]);
exports.NewAttach = NewAttach;
const Reattach = (tNodeChange) => typebox_1.Type.Union([(0, exports.Revive)(tNodeChange), exports.ReturnTo]);
exports.Reattach = Reattach;
const Attach = (tNodeChange) => typebox_1.Type.Union([(0, exports.NewAttach)(tNodeChange), (0, exports.Reattach)(tNodeChange)]);
exports.Attach = Attach;
const Detach = (tNodeChange) => typebox_1.Type.Union([(0, exports.Delete)(tNodeChange), (0, exports.MoveOut)(tNodeChange), (0, exports.ReturnFrom)(tNodeChange)]);
exports.Detach = Detach;
const Mark = (tNodeChange) => typebox_1.Type.Union([(0, exports.NoopMark)(tNodeChange), (0, exports.Attach)(tNodeChange), (0, exports.Detach)(tNodeChange)]);
exports.Mark = Mark;
const Changeset = (tNodeChange) => typebox_1.Type.Array((0, exports.Mark)(tNodeChange));
exports.Changeset = Changeset;
//# sourceMappingURL=format.js.map