/*!
 * 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";
/**
 * 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`);
export function cachedFlexSchemaFromClassSchema(schema) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return schema[flexSchemaSymbol];
}
export function setFlexSchemaFromClassSchema(simple, flex) {
    assert(!(flexSchemaSymbol in simple), 0x91f /* simple schema already marked */);
    assert(!(simpleNodeSchemaSymbol in flex), 0x920 /* flex schema already marked */);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    simple[flexSchemaSymbol] = flex;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    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 TreeNodeSchema} for the {@link InnerNode}.
 */
export function getSimpleNodeSchemaFromNode(innerNode) {
    // TODO: to make this work without depending on flex tree schema, a new caching/lookup mechanism will be required, likely leveraging the FlexTreeContext:
    // A SimpleTreeContext could be defined and associated with the FlexTreeContext, and used to look up simple-tree schema by identifier.
    return getSimpleNodeSchema(innerNode.flexSchema);
}
//# sourceMappingURL=schemaCaching.js.map