/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { schemaFormat, RevisionTagSchema } from "../../core/index.js";
import { brandedNumberType, JsonCompatibleReadOnlySchema, } from "../../util/index.js";
import { EncodedFieldBatch } from "../chunked-forest/index.js";
const noAdditionalProps = { additionalProperties: false };
export const ChangesetLocalIdSchema = brandedNumberType({
    multipleOf: 1,
});
export const EncodedChangeAtomId = Type.Union([
    Type.Tuple([ChangesetLocalIdSchema, RevisionTagSchema]),
    ChangesetLocalIdSchema,
]);
const EncodedValueChange = Type.Object({
    revision: Type.Optional(RevisionTagSchema),
    value: Type.Optional(JsonCompatibleReadOnlySchema),
}, noAdditionalProps);
const EncodedValueConstraint = Type.Object({
    value: Type.Optional(JsonCompatibleReadOnlySchema),
    violated: Type.Boolean(),
}, noAdditionalProps);
const EncodedFieldChange = Type.Object({
    fieldKey: schemaFormat.FieldKeySchema,
    fieldKind: 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: JsonCompatibleReadOnlySchema,
}, noAdditionalProps);
const EncodedFieldChangeMap = Type.Array(EncodedFieldChange);
const EncodedNodeExistsConstraint = Type.Object({
    violated: Type.Boolean(),
}, noAdditionalProps);
export const EncodedNodeChangeset = Type.Object({
    valueChange: Type.Optional(EncodedValueChange),
    fieldChanges: Type.Optional(EncodedFieldChangeMap),
    valueConstraint: Type.Optional(EncodedValueConstraint),
    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,
    /**
     * Fields indexed by the EncodedTreeIndexes above.
     * TODO: Strongly typing this here may result in redundant schema validation of this data.
     */
    trees: EncodedFieldBatch,
}, noAdditionalProps);
export const EncodedModularChangeset = Type.Object({
    maxId: Type.Optional(ChangesetLocalIdSchema),
    changes: EncodedFieldChangeMap,
    revisions: Type.ReadonlyOptional(Type.Array(EncodedRevisionInfo)),
    builds: Type.Optional(EncodedBuilds),
    refreshers: Type.Optional(EncodedBuilds),
}, noAdditionalProps);
//# sourceMappingURL=modularChangeFormat.js.map