"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Changeset = exports.DetachIdOverrideType = void 0;
const Type = __importStar(require("@sinclair/typebox"));
const index_js_1 = require("../../codec/index.js");
const index_js_2 = require("../../core/index.js");
const index_js_3 = require("../modular-schema/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 = Type.Number({ multipleOf: 1, minimum: 1 });
const MoveId = index_js_3.ChangesetLocalIdSchema;
const HasMoveId = Type.Object({ id: MoveId });
const LineageEvent = Type.Tuple([
    index_js_2.RevisionTagSchema,
    index_js_3.ChangesetLocalIdSchema,
    /** count */
    CellCount,
    /** offset */
    Type.Number({ multipleOf: 1, minimum: 0 }),
]);
const HasLineage = Type.Object({ lineage: Type.Optional(Type.Array(LineageEvent)) });
const IdRange = Type.Tuple([index_js_3.ChangesetLocalIdSchema, CellCount]);
const CellId = Type.Composite([
    HasLineage,
    Type.Object({
        atom: index_js_3.EncodedChangeAtomId,
        adjacentCells: Type.Optional(Type.Array(IdRange)),
    }),
], noAdditionalProps);
const HasRevisionTag = Type.Object({ revision: Type.Optional(index_js_2.RevisionTagSchema) });
const Insert = Type.Composite([HasMoveId, HasRevisionTag], noAdditionalProps);
const HasMoveFields = Type.Composite([
    HasMoveId,
    HasRevisionTag,
    Type.Object({ finalEndpoint: Type.Optional(index_js_3.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: index_js_3.ChangesetLocalIdSchema,
    }),
    HasRevisionTag,
    DetachFields,
], noAdditionalProps);
const MoveOut = Type.Composite([HasMoveFields, DetachFields], noAdditionalProps);
const Attach = Type.Object({
    insert: Type.Optional(Insert),
    moveIn: Type.Optional(MoveIn),
}, index_js_1.unionOptions);
const Detach = Type.Object({
    // TODO:AB6715 rename to `remove`
    delete: Type.Optional(Remove),
    moveOut: Type.Optional(MoveOut),
}, index_js_1.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),
}, index_js_1.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);
// 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
const Changeset = (tNodeChange) => Type.Array(Mark(tNodeChange));
exports.Changeset = Changeset;
//# sourceMappingURL=formatV1.js.map