"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Changeset = void 0;
const typebox_1 = require("@sinclair/typebox");
const core_1 = require("../../core");
const modular_schema_1 = require("../modular-schema");
const codec_1 = require("../../codec");
const noAdditionalProps = { additionalProperties: false };
const CellCount = typebox_1.Type.Number();
const MoveId = modular_schema_1.ChangesetLocalIdSchema;
const HasMoveId = typebox_1.Type.Object({ id: MoveId });
const LineageEvent = typebox_1.Type.Tuple([
    core_1.RevisionTagSchema,
    modular_schema_1.ChangesetLocalIdSchema,
    /* count */ typebox_1.Type.Number(),
    /* offset */ typebox_1.Type.Number(),
]);
const HasLineage = typebox_1.Type.Object({ lineage: typebox_1.Type.Optional(typebox_1.Type.Array(LineageEvent)) });
const IdRange = typebox_1.Type.Tuple([modular_schema_1.ChangesetLocalIdSchema, CellCount]);
const CellId = typebox_1.Type.Composite([
    modular_schema_1.EncodedChangeAtomId,
    HasLineage,
    typebox_1.Type.Object({ adjacentCells: typebox_1.Type.Optional(typebox_1.Type.Array(IdRange)) }),
], noAdditionalProps);
const HasRevisionTag = typebox_1.Type.Object({ revision: typebox_1.Type.Optional(core_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(modular_schema_1.EncodedChangeAtomId) }),
]);
const MoveIn = typebox_1.Type.Composite([HasMoveFields], noAdditionalProps);
const InverseAttachFields = typebox_1.Type.Object({
    detachIdOverride: typebox_1.Type.Optional(modular_schema_1.EncodedChangeAtomId),
});
const Delete = typebox_1.Type.Composite([
    typebox_1.Type.Object({
        id: modular_schema_1.ChangesetLocalIdSchema,
    }),
    HasRevisionTag,
    InverseAttachFields,
], noAdditionalProps);
const MoveOut = typebox_1.Type.Composite([HasMoveFields, InverseAttachFields], noAdditionalProps);
const MovePlaceholder = typebox_1.Type.Composite([HasMoveId, HasRevisionTag], noAdditionalProps);
const Attach = typebox_1.Type.Object({
    insert: typebox_1.Type.Optional(Insert),
    moveIn: typebox_1.Type.Optional(MoveIn),
}, codec_1.unionOptions);
const Detach = typebox_1.Type.Object({
    delete: typebox_1.Type.Optional(Delete),
    moveOut: typebox_1.Type.Optional(MoveOut),
}, codec_1.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),
    delete: typebox_1.Type.Optional(Delete),
    moveOut: typebox_1.Type.Optional(MoveOut),
    placeholder: typebox_1.Type.Optional(MovePlaceholder),
    attachAndDetach: typebox_1.Type.Optional(AttachAndDetach),
}, codec_1.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