/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { fail } from "../util/index.js";
import { normalizeFieldSchema, } from "./schemaTypes.js";
/**
 * A symbol for storing FlexTreeSchema on TreeNodeSchema.
 * Eagerly set on leaves, and lazily set for other cases.
 */
const flexSchemaSymbol = Symbol(`flexSchema`);
/**
 * A symbol for storing TreeNodeSchema on FlexTreeNode's schema.
 */
const simpleNodeSchemaSymbol = Symbol(`simpleNodeSchema`);
/**
 * A symbol for storing {@link FieldSchema}s on a {@link FlexFieldSchema}.
 */
const simpleFieldSchemaSymbol = Symbol(`simpleFieldSchema`);
export function cachedFlexSchemaFromClassSchema(schema) {
    return schema[flexSchemaSymbol];
}
export function setFlexSchemaFromClassSchema(simple, flex) {
    assert(!(flexSchemaSymbol in simple), "simple schema already marked");
    assert(!(simpleNodeSchemaSymbol in flex), "flex schema already marked");
    simple[flexSchemaSymbol] = flex;
    flex[simpleNodeSchemaSymbol] = simple;
}
/**
 * Gets the {@link TreeNodeSchema} cached on the provided {@link FlexTreeNodeSchema | flexSchema}.
 * Returns `undefined` if no cached value is found.
 */
export function tryGetSimpleNodeSchema(flexSchema) {
    if (simpleNodeSchemaSymbol in flexSchema) {
        return flexSchema[simpleNodeSchemaSymbol];
    }
    return undefined;
}
/**
 * Gets the {@link TreeNodeSchema} cached on the provided {@link FlexTreeNodeSchema | flexSchema}.
 * Fails if no cached value is found.
 */
export function getSimpleNodeSchema(flexSchema) {
    return tryGetSimpleNodeSchema(flexSchema) ?? fail("missing simple schema");
}
/**
 * Gets the {@link FieldSchema} which corresponds with the provided {@link FlexFieldSchema | flexSchema}.
 * Caches the result on the provided `flexSchema` for future access.
 * @param flexSchema - The flex schema on which the result will be cached.
 * @param implicitSimpleSchema - The allowed types from which the `FieldSchema` will be derived.
 */
export function getSimpleFieldSchema(flexSchema, implicitSimpleSchema) {
    if (simpleFieldSchemaSymbol in flexSchema) {
        return flexSchema[simpleFieldSchemaSymbol];
    }
    const fieldSchema = normalizeFieldSchema(implicitSimpleSchema);
    flexSchema[simpleFieldSchemaSymbol] = fieldSchema;
    return fieldSchema;
}
//# sourceMappingURL=schemaCaching.js.map