/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { unreachableCase } from "@fluidframework/core-utils/internal";
import { LeafNodeStoredSchema, ObjectNodeStoredSchema, MapNodeStoredSchema, Multiplicity, } from "../../core/index.js";
import { allowsValue } from "../valueUtilities.js";
import { fail } from "../../util/index.js";
export var SchemaValidationErrors;
(function (SchemaValidationErrors) {
    SchemaValidationErrors[SchemaValidationErrors["NoError"] = 0] = "NoError";
    SchemaValidationErrors[SchemaValidationErrors["Field_KindNotInSchemaPolicy"] = 1] = "Field_KindNotInSchemaPolicy";
    SchemaValidationErrors[SchemaValidationErrors["Field_IncorrectMultiplicity"] = 2] = "Field_IncorrectMultiplicity";
    SchemaValidationErrors[SchemaValidationErrors["Field_NodeTypeNotAllowed"] = 3] = "Field_NodeTypeNotAllowed";
    SchemaValidationErrors[SchemaValidationErrors["LeafNode_InvalidValue"] = 4] = "LeafNode_InvalidValue";
    SchemaValidationErrors[SchemaValidationErrors["LeafNode_FieldsNotAllowed"] = 5] = "LeafNode_FieldsNotAllowed";
    SchemaValidationErrors[SchemaValidationErrors["ObjectNode_FieldNotInSchema"] = 6] = "ObjectNode_FieldNotInSchema";
    SchemaValidationErrors[SchemaValidationErrors["NonLeafNode_ValueNotAllowed"] = 7] = "NonLeafNode_ValueNotAllowed";
    SchemaValidationErrors[SchemaValidationErrors["Node_MissingSchema"] = 8] = "Node_MissingSchema";
    SchemaValidationErrors[SchemaValidationErrors["UnknownError"] = 9] = "UnknownError";
})(SchemaValidationErrors || (SchemaValidationErrors = {}));
export function isNodeInSchema(node, schemaAndPolicy) {
    // If the stored schema is completely empty it _probably_ (in almost all cases?) means the tree is brand new and we
    // shouldn't validate the data.
    // TODO: AB#8197
    // See https://github.com/microsoft/FluidFramework/pull/21305#discussion_r1626595991 for further discussion.
    if (schemaAndPolicy.schema.nodeSchema.size === 0) {
        return 0 /* SchemaValidationErrors.NoError */;
    }
    // Validate the schema declared by the node exists
    const schema = schemaAndPolicy.schema.nodeSchema.get(node.type);
    if (schema === undefined) {
        return 8 /* SchemaValidationErrors.Node_MissingSchema */;
    }
    // Validate the node is well formed according to its schema
    if (schema instanceof LeafNodeStoredSchema) {
        if (node.fields.size !== 0) {
            return 5 /* SchemaValidationErrors.LeafNode_FieldsNotAllowed */;
        }
        if (!allowsValue(schema.leafValue, node.value)) {
            return 4 /* SchemaValidationErrors.LeafNode_InvalidValue */;
        }
    }
    else if (schema instanceof ObjectNodeStoredSchema) {
        if (node.value !== undefined) {
            return 7 /* SchemaValidationErrors.NonLeafNode_ValueNotAllowed */;
        }
        const uncheckedFieldsFromNode = new Set(node.fields.keys());
        for (const [fieldKey, fieldSchema] of schema.objectNodeFields) {
            const nodeField = node.fields.get(fieldKey) ?? [];
            const fieldInSchemaResult = isFieldInSchema(nodeField, fieldSchema, schemaAndPolicy);
            if (fieldInSchemaResult !== 0 /* SchemaValidationErrors.NoError */) {
                return fieldInSchemaResult;
            }
            uncheckedFieldsFromNode.delete(fieldKey);
        }
        // The node has fields that we did not check as part of looking at every field defined in the node's schema
        if (uncheckedFieldsFromNode.size !== 0) {
            return 6 /* SchemaValidationErrors.ObjectNode_FieldNotInSchema */;
        }
    }
    else if (schema instanceof MapNodeStoredSchema) {
        if (node.value !== undefined) {
            return 7 /* SchemaValidationErrors.NonLeafNode_ValueNotAllowed */;
        }
        for (const field of node.fields.values()) {
            const fieldInSchemaResult = isFieldInSchema(field, schema.mapFields, schemaAndPolicy);
            if (fieldInSchemaResult !== 0 /* SchemaValidationErrors.NoError */) {
                return fieldInSchemaResult;
            }
        }
    }
    else {
        fail("Unknown TreeNodeStoredSchema type");
    }
    return 0 /* SchemaValidationErrors.NoError */;
}
export function isFieldInSchema(childNodes, schema, schemaAndPolicy) {
    // Validate that the field kind is handled by the schema policy
    const kind = schemaAndPolicy.policy.fieldKinds.get(schema.kind);
    if (kind === undefined) {
        return 1 /* SchemaValidationErrors.Field_KindNotInSchemaPolicy */;
    }
    // Validate that the field doesn't contain more nodes than its type supports
    if (!compliesWithMultiplicity(childNodes.length, kind.multiplicity)) {
        return 2 /* SchemaValidationErrors.Field_IncorrectMultiplicity */;
    }
    for (const node of childNodes) {
        // Validate the type declared by the node is allowed in this field
        if (schema.types !== undefined && !schema.types.has(node.type)) {
            return 3 /* SchemaValidationErrors.Field_NodeTypeNotAllowed */;
        }
        // Validate the node complies with the type it declares to be.
        const nodeInSchemaResult = isNodeInSchema(node, schemaAndPolicy);
        if (nodeInSchemaResult !== 0 /* SchemaValidationErrors.NoError */) {
            return nodeInSchemaResult;
        }
    }
    return 0 /* SchemaValidationErrors.NoError */;
}
/**
 * Validates that a given number of items complies with the specified {@link Multiplicity | multiplicity}.
 * @param numberOfItems - Number of items.
 * @param multiplicity - Kind of multiplicity to validate against.
 * @returns `true` if the specified number of items complies with the specified multiplicity; otherwise, `false`.
 */
export function compliesWithMultiplicity(numberOfItems, multiplicity) {
    switch (multiplicity) {
        case Multiplicity.Single:
            return numberOfItems === 1;
        case Multiplicity.Optional:
            return numberOfItems <= 1;
        case Multiplicity.Sequence:
            return true;
        case Multiplicity.Forbidden:
            return numberOfItems === 0;
        default:
            unreachableCase(multiplicity);
    }
}
//# sourceMappingURL=schemaChecker.js.map