/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { leaf } from "../domains/index.js";
import { isFlexTreeNode, valueSchemaAllows, } from "../feature-libraries/index.js";
import { setFlexSchemaFromClassSchema } from "./schemaCaching.js";
import { NodeKind } from "./schemaTypes.js";
/**
 * Instances of this class are schema for leaf nodes.
 * @remarks
 * Unlike other schema, leaf schema are class instances instead of classes themselves.
 * This is because the instance type (the tree node type) for leaves are not objects,
 * so those instances can't be instances of a schema based class.
 * @privateRemarks
 * This class refers to the underlying flex tree schema in its constructor, so this class can't be included in the package API.
 */
class LeafNodeSchema {
    create(data) {
        if (isFlexTreeNode(data)) {
            const value = data.value;
            assert(valueSchemaAllows(this.info, value), 0x916 /* invalid value */);
            return value;
        }
        return data;
    }
    constructor(schema) {
        this.kind = NodeKind.Leaf;
        this.implicitlyConstructable = true;
        setFlexSchemaFromClassSchema(this, schema);
        this.identifier = schema.name;
        this.info = schema.info;
    }
}
/**
 * Wrapper around LeafNodeSchema's constructor that provides the return type that is desired in the package public API.
 */
function makeLeaf(schema) {
    return new LeafNodeSchema(schema);
}
// Leaf schema shared between all SchemaFactory instances.
export const stringSchema = makeLeaf(leaf.string);
export const numberSchema = makeLeaf(leaf.number);
export const booleanSchema = makeLeaf(leaf.boolean);
export const nullSchema = makeLeaf(leaf.null);
export const handleSchema = makeLeaf(leaf.handle);
//# sourceMappingURL=leafNodeSchema.js.map