"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncodedModularChangesetV1 = exports.EncodedBuilds = exports.EncodedBuildsArray = exports.CommitBuilds = exports.EncodedTreeIndex = exports.EncodedRevisionInfo = exports.EncodedNodeChangeset = exports.EncodedFieldChangeMap = exports.EncodedFieldChange = 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 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,
]);
exports.EncodedFieldChange = typebox_1.Type.Object({
    fieldKey: index_js_1.schemaFormatV1.FieldKeySchema,
    fieldKind: index_js_1.schemaFormatV1.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);
exports.EncodedFieldChangeMap = typebox_1.Type.Array(exports.EncodedFieldChange);
const EncodedNodeExistsConstraint = typebox_1.Type.Object({
    violated: typebox_1.Type.Boolean(),
}, noAdditionalProps);
exports.EncodedNodeChangeset = typebox_1.Type.Object({
    fieldChanges: typebox_1.Type.Optional(exports.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,
    /**
     * FieldBatch codec encoded data indexed by the EncodedTreeIndexes above.
     */
    trees: index_js_2.JsonCompatibleReadOnlySchema,
}, noAdditionalProps);
exports.EncodedModularChangesetV1 = typebox_1.Type.Object({
    maxId: typebox_1.Type.Optional(exports.ChangesetLocalIdSchema),
    changes: exports.EncodedFieldChangeMap,
    revisions: typebox_1.Type.ReadonlyOptional(typebox_1.Type.Array(exports.EncodedRevisionInfo)),
    // TODO#8574: separating `builds` and `refreshers` here means that we encode their `EncodedBuilds.trees` separately.
    // This can lead to a less efficient wire representation because of duplicated schema/shape information.
    builds: typebox_1.Type.Optional(exports.EncodedBuilds),
    refreshers: typebox_1.Type.Optional(exports.EncodedBuilds),
    /**
     * The number of constraints within this changeset that are violated.
     */
    violations: typebox_1.Type.Optional(typebox_1.Type.Number({ minimum: 0, multipleOf: 1 })),
}, noAdditionalProps);
//# sourceMappingURL=modularChangeFormatV1.js.map