"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncodedModularChangeset = exports.EncodedBuilds = exports.EncodedBuildsArray = exports.CommitBuilds = exports.EncodedTreeIndex = exports.EncodedRevisionInfo = exports.EncodedNodeChangeset = exports.EncodedChangeAtomId = exports.ChangesetLocalIdSchema = void 0;
const typebox_1 = require("@sinclair/typebox");
const index_js_1 = require("../../core/index.js");
const index_js_2 = require("../../util/index.js");
const index_js_3 = require("../chunked-forest/index.js");
const noAdditionalProps = { additionalProperties: false };
exports.ChangesetLocalIdSchema = (0, index_js_2.brandedNumberType)({
    multipleOf: 1,
});
exports.EncodedChangeAtomId = typebox_1.Type.Union([
    typebox_1.Type.Tuple([exports.ChangesetLocalIdSchema, index_js_1.RevisionTagSchema]),
    exports.ChangesetLocalIdSchema,
]);
const EncodedFieldChange = typebox_1.Type.Object({
    fieldKey: index_js_1.schemaFormat.FieldKeySchema,
    fieldKind: index_js_1.schemaFormat.FieldKindIdentifierSchema,
    // Implementation note: node and field change encoding is mutually recursive.
    // This field marks a boundary in that recursion to avoid constructing excessively complex
    // recursive types. Encoded changes are validated at this boundary at runtime--see modularChangeCodecs.ts.
    change: index_js_2.JsonCompatibleReadOnlySchema,
}, noAdditionalProps);
const EncodedFieldChangeMap = typebox_1.Type.Array(EncodedFieldChange);
const EncodedNodeExistsConstraint = typebox_1.Type.Object({
    violated: typebox_1.Type.Boolean(),
}, noAdditionalProps);
exports.EncodedNodeChangeset = typebox_1.Type.Object({
    fieldChanges: typebox_1.Type.Optional(EncodedFieldChangeMap),
    nodeExistsConstraint: typebox_1.Type.Optional(EncodedNodeExistsConstraint),
}, noAdditionalProps);
exports.EncodedRevisionInfo = typebox_1.Type.Object({
    revision: typebox_1.Type.Readonly(index_js_1.RevisionTagSchema),
    rollbackOf: typebox_1.Type.ReadonlyOptional(index_js_1.RevisionTagSchema),
}, noAdditionalProps);
/**
 * Index of field in an EncodedFieldBatch.
 */
exports.EncodedTreeIndex = typebox_1.Type.Number({ multipleOf: 1, minimum: 0 });
exports.CommitBuilds = typebox_1.Type.Array(typebox_1.Type.Tuple([
    // ID for first node
    exports.ChangesetLocalIdSchema,
    // Index for a TreeChunk that represents all the nodes
    exports.EncodedTreeIndex,
]));
exports.EncodedBuildsArray = typebox_1.Type.Array(typebox_1.Type.Union([
    typebox_1.Type.Tuple([exports.CommitBuilds, index_js_1.RevisionTagSchema]),
    // Revision is omitted when undefined
    typebox_1.Type.Tuple([exports.CommitBuilds]),
]));
exports.EncodedBuilds = typebox_1.Type.Object({
    builds: exports.EncodedBuildsArray,
    /**
     * Fields indexed by the EncodedTreeIndexes above.
     * TODO: Strongly typing this here may result in redundant schema validation of this data.
     */
    trees: index_js_3.EncodedFieldBatch,
}, noAdditionalProps);
exports.EncodedModularChangeset = typebox_1.Type.Object({
    maxId: typebox_1.Type.Optional(exports.ChangesetLocalIdSchema),
    changes: EncodedFieldChangeMap,
    revisions: typebox_1.Type.ReadonlyOptional(typebox_1.Type.Array(exports.EncodedRevisionInfo)),
    builds: typebox_1.Type.Optional(exports.EncodedBuilds),
    refreshers: typebox_1.Type.Optional(exports.EncodedBuilds),
}, noAdditionalProps);
//# sourceMappingURL=modularChangeFormat.js.map