"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTreeNodeSchemaInitializedData = exports.getUnhydratedContext = void 0;
const index_js_1 = require("../feature-libraries/index.js");
const index_js_2 = require("../util/index.js");
const index_js_3 = require("./core/index.js");
const fieldSchema_js_1 = require("./fieldSchema.js");
const toStoredSchema_js_1 = require("./toStoredSchema.js");
const contextCache = new WeakMap();
/**
 * Utility for creating {@link Context}s for unhydrated nodes.
 * @remarks
 * The resulting context will not allow any unknown optional fields.
 */
function getUnhydratedContext(schema) {
    return (0, index_js_2.getOrCreate)(contextCache, schema, (s) => {
        const normalized = (0, fieldSchema_js_1.normalizeFieldSchema)(schema);
        const flexContext = new index_js_3.UnhydratedContext(index_js_1.defaultSchemaPolicy, (0, toStoredSchema_js_1.toStoredSchema)(schema, toStoredSchema_js_1.toUnhydratedSchema));
        return new index_js_3.Context(flexContext, index_js_3.Context.schemaMapFromRootSchema(normalized.annotatedAllowedTypesNormalized));
    });
}
exports.getUnhydratedContext = getUnhydratedContext;
/**
 * Utility for creating {@link TreeNodeSchemaInitializedData}.
 */
function getTreeNodeSchemaInitializedData(schema, handler) {
    const data = (0, index_js_3.getTreeNodeSchemaPrivateData)(schema);
    return {
        ...handler,
        context: getUnhydratedContext(schema),
        childAnnotatedAllowedTypes: data.childAnnotatedAllowedTypes.map(index_js_3.normalizeAnnotatedAllowedTypes),
    };
}
exports.getTreeNodeSchemaInitializedData = getTreeNodeSchemaInitializedData;
//# sourceMappingURL=createContext.js.map