"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSimpleFieldSchema = 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");
const schemaTypes_js_1 = require("./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`);
function cachedFlexSchemaFromClassSchema(schema) {
    return schema[flexSchemaSymbol];
}
exports.cachedFlexSchemaFromClassSchema = cachedFlexSchemaFromClassSchema;
function setFlexSchemaFromClassSchema(simple, flex) {
    (0, internal_1.assert)(!(flexSchemaSymbol in simple), "simple schema already marked");
    (0, internal_1.assert)(!(simpleNodeSchemaSymbol in flex), "flex schema already marked");
    simple[flexSchemaSymbol] = flex;
    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 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.
 */
function getSimpleFieldSchema(flexSchema, implicitSimpleSchema) {
    if (simpleFieldSchemaSymbol in flexSchema) {
        return flexSchema[simpleFieldSchemaSymbol];
    }
    const fieldSchema = (0, schemaTypes_js_1.normalizeFieldSchema)(implicitSimpleSchema);
    flexSchema[simpleFieldSchemaSymbol] = fieldSchema;
    return fieldSchema;
}
exports.getSimpleFieldSchema = getSimpleFieldSchema;
//# sourceMappingURL=schemaCaching.js.map