/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { normalizeAnnotatedAllowedTypes, } from "./allowedTypes.js";
import { getTreeNodeSchemaPrivateData } from "./treeNodeSchema.js";
/**
 * Traverses all {@link TreeNodeSchema} schema reachable from `schema`, applying the visitor pattern.
 * @internal
 */
export function walkNodeSchema(schema, visitor, visitedSet) {
    if (visitedSet.has(schema)) {
        return;
    }
    visitedSet.add(schema);
    // Since walkNodeSchema is used in the implementation of TreeNodeSchemaPrivateData.idempotentInitialize,
    // Avoid depending on it here to avoid circular dependencies for recursive schema.
    // Instead normalize/evaluate the allowed types as needed.
    const annotatedAllowedTypes = getTreeNodeSchemaPrivateData(schema).childAnnotatedAllowedTypes;
    for (const fieldAllowedTypes of annotatedAllowedTypes) {
        walkAllowedTypes(normalizeAnnotatedAllowedTypes(fieldAllowedTypes), visitor, visitedSet);
    }
    // This visit is done at the end so the traversal order is most inner types first.
    // This was picked since when fixing errors,
    // working from the inner types out to the types that use them will probably go better than the reverse.
    // This does not however ensure all types referenced by a type are visited before it, since in recursive cases thats impossible.
    visitor.node?.(schema);
}
/**
 * Traverses all {@link TreeNodeSchema} schema reachable from `allowedTypes`, applying the visitor pattern.
 * @internal
 */
export function walkAllowedTypes(annotatedAllowedTypes, visitor, visitedSet = new Set()) {
    for (const allowedType of annotatedAllowedTypes.types) {
        if ((visitor.allowedTypeFilter ?? (() => true))(allowedType)) {
            walkNodeSchema(allowedType.type, visitor, visitedSet);
        }
    }
    visitor.allowedTypes?.(annotatedAllowedTypes);
}
//# sourceMappingURL=walkSchema.js.map