/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { defaultSchemaPolicy } from "../feature-libraries/index.js";
import { getOrCreate } from "../util/index.js";
import { Context, getTreeNodeSchemaPrivateData, normalizeAnnotatedAllowedTypes, UnhydratedContext, } from "./core/index.js";
import { normalizeFieldSchema } from "./fieldSchema.js";
import { toStoredSchema, toUnhydratedSchema } from "./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.
 */
export function getUnhydratedContext(schema) {
    return getOrCreate(contextCache, schema, (s) => {
        const normalized = normalizeFieldSchema(schema);
        const flexContext = new UnhydratedContext(defaultSchemaPolicy, toStoredSchema(schema, toUnhydratedSchema));
        return new Context(flexContext, Context.schemaMapFromRootSchema(normalized.annotatedAllowedTypesNormalized));
    });
}
/**
 * Utility for creating {@link TreeNodeSchemaInitializedData}.
 */
export function getTreeNodeSchemaInitializedData(schema, handler) {
    const data = getTreeNodeSchemaPrivateData(schema);
    return {
        ...handler,
        context: getUnhydratedContext(schema),
        childAnnotatedAllowedTypes: data.childAnnotatedAllowedTypes.map(normalizeAnnotatedAllowedTypes),
    };
}
//# sourceMappingURL=createContext.js.map