/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { RevisionTagSchema, schemaFormatV1 } from "../../core/index.js";
import { JsonCompatibleReadOnlySchema, brandedNumberType, } from "../../util/index.js";
const noAdditionalProps = { additionalProperties: false };
export const ChangesetLocalIdSchema = brandedNumberType({
    multipleOf: 1,
});
export const EncodedChangeAtomId = Type.Union([
    Type.Tuple([ChangesetLocalIdSchema, RevisionTagSchema]),
    ChangesetLocalIdSchema,
]);
export const EncodedFieldChange = Type.Object({
    fieldKey: schemaFormatV1.FieldKeySchema,
    fieldKind: 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: JsonCompatibleReadOnlySchema,
}, noAdditionalProps);
export const EncodedFieldChangeMap = Type.Array(EncodedFieldChange);
const EncodedNodeExistsConstraint = Type.Object({
    violated: Type.Boolean(),
}, noAdditionalProps);
export const EncodedNodeChangeset = Type.Object({
    fieldChanges: Type.Optional(EncodedFieldChangeMap),
    nodeExistsConstraint: Type.Optional(EncodedNodeExistsConstraint),
}, noAdditionalProps);
export const EncodedRevisionInfo = Type.Object({
    revision: Type.Readonly(RevisionTagSchema),
    rollbackOf: Type.ReadonlyOptional(RevisionTagSchema),
}, noAdditionalProps);
/**
 * Index of field in an EncodedFieldBatch.
 */
export const EncodedTreeIndex = Type.Number({ multipleOf: 1, minimum: 0 });
export const CommitBuilds = Type.Array(Type.Tuple([
    // ID for first node
    ChangesetLocalIdSchema,
    // Index for a TreeChunk that represents all the nodes
    EncodedTreeIndex,
]));
export const EncodedBuildsArray = Type.Array(Type.Union([
    Type.Tuple([CommitBuilds, RevisionTagSchema]),
    // Revision is omitted when undefined
    Type.Tuple([CommitBuilds]),
]));
export const EncodedBuilds = Type.Object({
    builds: EncodedBuildsArray,
    /**
     * FieldBatch codec encoded data indexed by the EncodedTreeIndexes above.
     */
    trees: JsonCompatibleReadOnlySchema,
}, noAdditionalProps);
export const EncodedModularChangesetV1 = Type.Object({
    maxId: Type.Optional(ChangesetLocalIdSchema),
    changes: EncodedFieldChangeMap,
    revisions: Type.ReadonlyOptional(Type.Array(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(EncodedBuilds),
    refreshers: Type.Optional(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