/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils";
import { typeNameSymbol, valueSymbol, } from "../contextuallyTyped";
import { Multiplicity } from "../modular-schema";
import { contextSymbol, typeSymbol, } from "../untypedTree";
/**
 * Different schema aware APIs that can be generated.
 * @public
 */
export var ApiMode;
(function (ApiMode) {
    /**
     * Allow all forms accepted as ContextuallyTypedNodeData that align with the schema.
     * Types are optional.
     *
     * This also permits some cases which are ambiguous and thus would be rejected by `applyFieldTypesFromContext`.
     */
    ApiMode[ApiMode["Flexible"] = 0] = "Flexible";
    /**
     * Similar to what EditableTree uses.
     * No flexibility in representation.
     * Fields are unwrapped (see `EditableUnwrapped`).
     *
     * TODO: fix ways this differs from editable tree:
     * - Does not do primary field inlining.
     * - Primitive node handling might not match.
     */
    ApiMode[ApiMode["Editable"] = 1] = "Editable";
    /**
     * Editable, but primitive nodes are unwrapped to the primitive values.
     */
    ApiMode[ApiMode["EditableUnwrapped"] = 2] = "EditableUnwrapped";
    /**
     * Always use full node objects for everything.
     *
     * Fields are still shaped based on their multiplicity.
     *
     * TODO: test and fix
     */
    ApiMode[ApiMode["Wrapped"] = 3] = "Wrapped";
    /**
     * Simplified version of Flexible.
     *
     * Primitive values are always unwrapped.
     */
    ApiMode[ApiMode["Simple"] = 4] = "Simple";
})(ApiMode || (ApiMode = {}));
/**
 * Check if an `UntypedTreeCore` has a specific schema, and if it does, cast it to use `ApiMode.Editable` with that schema.
 * Provided schema must be included in the schema for the tree being viewed (getting this wrong will error).
 * @public
 */
// TODO: tests
export function downCast(schema, tree) {
    assert(typeof tree === "object", 0x72b /* downCast only valid on wrapped nodes */);
    const contextSchema = tree[contextSymbol].schema;
    const lookedUp = contextSchema.treeSchema.get(schema.name);
    // TODO: for this to pass, schematized view must have the view schema, not just stored schema.
    assert(lookedUp === schema, 0x68c /* cannot downcast to a schema the tree is not using */);
    // TODO: make this actually work
    const matches = tree[typeSymbol] === schema;
    assert(matches === (tree[typeSymbol].name === schema.name), 0x68d /* schema object identity comparison should match identifier comparison */);
    return matches;
}
//# sourceMappingURL=schemaAware.js.map