"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.walkAllowedTypes = exports.walkNodeSchema = void 0;
const treeNodeSchema_js_1 = require("./treeNodeSchema.js");
/**
 * Traverses all {@link TreeNodeSchema} schema reachable from `schema`, applying the visitor pattern.
 */
function walkNodeSchema(schema, visitor, visitedSet) {
    if (visitedSet.has(schema)) {
        return;
    }
    visitedSet.add(schema);
    const annotatedAllowedTypes = (0, treeNodeSchema_js_1.asTreeNodeSchemaCorePrivate)(schema).childAnnotatedAllowedTypes;
    for (const fieldAllowedTypes of annotatedAllowedTypes) {
        walkAllowedTypes(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);
}
exports.walkNodeSchema = walkNodeSchema;
/**
 * Traverses all {@link TreeNodeSchema} schema reachable from `allowedTypes`, applying the visitor pattern.
 */
function walkAllowedTypes(annotatedAllowedTypes, visitor, visitedSet = new Set()) {
    for (const { type } of annotatedAllowedTypes.types) {
        walkNodeSchema(type, visitor, visitedSet);
    }
    visitor.allowedTypes?.(annotatedAllowedTypes);
}
exports.walkAllowedTypes = walkAllowedTypes;
//# sourceMappingURL=walkSchema.js.map