"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.EncodedModularChangesetV1 = exports.EncodedBuilds = exports.EncodedBuildsArray = exports.CommitBuilds = exports.EncodedTreeIndex = exports.EncodedRevisionInfo = exports.EncodedNodeChangeset = exports.EncodedFieldChangeMap = exports.EncodedFieldChange = exports.EncodedChangeAtomId = exports.ChangesetLocalIdSchema = void 0;
const Type = __importStar(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 = Type.Union([
    Type.Tuple([exports.ChangesetLocalIdSchema, index_js_1.RevisionTagSchema]),
    exports.ChangesetLocalIdSchema,
]);
exports.EncodedFieldChange = 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 = Type.Array(exports.EncodedFieldChange);
const EncodedNodeExistsConstraint = Type.Object({
    violated: Type.Boolean(),
}, noAdditionalProps);
exports.EncodedNodeChangeset = Type.Object({
    fieldChanges: Type.Optional(exports.EncodedFieldChangeMap),
    nodeExistsConstraint: Type.Optional(EncodedNodeExistsConstraint),
}, noAdditionalProps);
exports.EncodedRevisionInfo = Type.Object({
    revision: Type.Readonly(index_js_1.RevisionTagSchema),
    rollbackOf: Type.ReadonlyOptional(index_js_1.RevisionTagSchema),
}, noAdditionalProps);
/**
 * Index of field in an EncodedFieldBatch.
 */
exports.EncodedTreeIndex = Type.Number({ multipleOf: 1, minimum: 0 });
exports.CommitBuilds = Type.Array(Type.Tuple([
    // ID for first node
    exports.ChangesetLocalIdSchema,
    // Index for a TreeChunk that represents all the nodes
    exports.EncodedTreeIndex,
]));
exports.EncodedBuildsArray = Type.Array(Type.Union([
    Type.Tuple([exports.CommitBuilds, index_js_1.RevisionTagSchema]),
    // Revision is omitted when undefined
    Type.Tuple([exports.CommitBuilds]),
]));
exports.EncodedBuilds = Type.Object({
    builds: exports.EncodedBuildsArray,
    /**
     * FieldBatch codec encoded data indexed by the EncodedTreeIndexes above.
     */
    trees: index_js_2.JsonCompatibleReadOnlySchema,
}, noAdditionalProps);
exports.EncodedModularChangesetV1 = Type.Object({
    maxId: Type.Optional(exports.ChangesetLocalIdSchema),
    changes: exports.EncodedFieldChangeMap,
    revisions: Type.ReadonlyOptional(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: Type.Optional(exports.EncodedBuilds),
    refreshers: Type.Optional(exports.EncodedBuilds),
    /**
     * The number of constraints within this changeset that are violated.
     */
    violations: Type.Optional(Type.Number({ minimum: 0, multipleOf: 1 })),
}, noAdditionalProps);
//# sourceMappingURL=modularChangeFormatV1.js.map