"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.walkFieldSchema = exports.walkNodeSchema = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("./core/index.js");
const leafNodeSchema_js_1 = require("./leafNodeSchema.js");
const objectNodeTypes_js_1 = require("./objectNodeTypes.js");
const schemaTypes_js_1 = require("./schemaTypes.js");
function walkNodeSchema(schema, visitor, visitedSet) {
    if (visitedSet.has(schema)) {
        return;
    }
    visitedSet.add(schema);
    if (schema instanceof leafNodeSchema_js_1.LeafNodeSchema) {
        // nothing to do
    }
    else if ((0, objectNodeTypes_js_1.isObjectNodeSchema)(schema)) {
        for (const field of schema.fields.values()) {
            walkFieldSchema(field, visitor, visitedSet);
        }
    }
    else {
        (0, internal_1.assert)(schema.kind === index_js_1.NodeKind.Array || schema.kind === index_js_1.NodeKind.Map, 0x9b3 /* invalid schema */);
        const childTypes = schema.info;
        walkAllowedTypes((0, schemaTypes_js_1.normalizeAllowedTypes)(childTypes), 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;
function walkFieldSchema(schema, visitor, visitedSet = new Set()) {
    walkAllowedTypes((0, schemaTypes_js_1.normalizeFieldSchema)(schema).allowedTypeSet, visitor, visitedSet);
}
exports.walkFieldSchema = walkFieldSchema;
function walkAllowedTypes(allowedTypes, visitor, visitedSet) {
    for (const childType of allowedTypes) {
        walkNodeSchema(childType, visitor, visitedSet);
    }
    visitor.allowedTypes?.(allowedTypes);
}
exports.walkAllowedTypes = walkAllowedTypes;
//# sourceMappingURL=walkSchema.js.map