"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSimpleNodeSchemaFromNode = exports.getSimpleNodeSchema = exports.tryGetSimpleNodeSchema = exports.setFlexSchemaFromClassSchema = exports.cachedFlexSchemaFromClassSchema = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const index_js_1 = require("../../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`);
function cachedFlexSchemaFromClassSchema(schema) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return schema[flexSchemaSymbol];
}
exports.cachedFlexSchemaFromClassSchema = cachedFlexSchemaFromClassSchema;
function setFlexSchemaFromClassSchema(simple, flex) {
    (0, internal_1.assert)(!(flexSchemaSymbol in simple), 0x91f /* simple schema already marked */);
    (0, internal_1.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;
}
exports.setFlexSchemaFromClassSchema = setFlexSchemaFromClassSchema;
/**
 * Gets the {@link TreeNodeSchema} cached on the provided {@link FlexTreeNodeSchema | flexSchema}.
 * Returns `undefined` if no cached value is found.
 */
function tryGetSimpleNodeSchema(flexSchema) {
    if (simpleNodeSchemaSymbol in flexSchema) {
        return flexSchema[simpleNodeSchemaSymbol];
    }
    return undefined;
}
exports.tryGetSimpleNodeSchema = tryGetSimpleNodeSchema;
/**
 * Gets the {@link TreeNodeSchema} cached on the provided {@link FlexTreeNodeSchema | flexSchema}.
 * Fails if no cached value is found.
 */
function getSimpleNodeSchema(flexSchema) {
    return tryGetSimpleNodeSchema(flexSchema) ?? (0, index_js_1.fail)("missing simple schema");
}
exports.getSimpleNodeSchema = getSimpleNodeSchema;
/**
 * Gets the {@link TreeNodeSchema} for the {@link InnerNode}.
 */
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);
}
exports.getSimpleNodeSchemaFromNode = getSimpleNodeSchemaFromNode;
//# sourceMappingURL=schemaCaching.js.map