/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { Type } from "@sinclair/typebox";
import { FieldKindIdentifierSchema, FieldKeySchema, RevisionTagSchema, } from "../../core";
import { brandedNumberType, JsonCompatibleReadOnlySchema, } from "../../util";
const noAdditionalProps = { additionalProperties: false };
export const ChangesetLocalIdSchema = brandedNumberType();
export const EncodedChangeAtomId = Type.Object({
    /**
     * Uniquely identifies the changeset within which the change was made.
     */
    revision: Type.Optional(RevisionTagSchema),
    /**
     * Uniquely identifies, in the scope of the changeset, the change made to the field.
     */
    localId: ChangesetLocalIdSchema,
}, noAdditionalProps);
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: FieldKeySchema,
    fieldKind: 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(),
});
export const EncodedNodeChangeset = Type.Object({
    valueChange: Type.Optional(EncodedValueChange),
    fieldChanges: Type.Optional(EncodedFieldChangeMap),
    valueConstraint: Type.Optional(EncodedValueConstraint),
    nodeExistsConstraint: Type.Optional(EncodedNodeExistsConstraint),
}, noAdditionalProps);
const EncodedRevisionInfo = Type.Object({
    revision: Type.Readonly(RevisionTagSchema),
    rollbackOf: Type.ReadonlyOptional(RevisionTagSchema),
}, noAdditionalProps);
// TODO:YA6307 adopt more efficient encoding, likely based on contiguous runs of IDs
export const EncodedBuilds = Type.Array(Type.Union([
    Type.Tuple([ChangesetLocalIdSchema, Type.Any()]),
    Type.Tuple([RevisionTagSchema, ChangesetLocalIdSchema, Type.Any()]),
]));
export const EncodedModularChangeset = Type.Object({
    maxId: Type.Optional(ChangesetLocalIdSchema),
    changes: EncodedFieldChangeMap,
    revisions: Type.ReadonlyOptional(Type.Array(EncodedRevisionInfo)),
    builds: Type.Optional(EncodedBuilds),
}, noAdditionalProps);
//# sourceMappingURL=modularChangeFormat.js.map