/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { ValueSchema } from "../core/index.js";
import { isFlexTreeNode, valueSchemaAllows, } from "../feature-libraries/index.js";
import { NodeKind, } from "./core/index.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.
 */
export class LeafNodeSchema {
    create(data) {
        if (isFlexTreeNode(data)) {
            const value = data.value;
            assert(valueSchemaAllows(this.info, value), 0x916 /* invalid value */);
            return value;
        }
        return data;
    }
    createFromInsertable(data) {
        return data;
    }
    constructor(name, t) {
        this.kind = NodeKind.Leaf;
        this.implicitlyConstructable = true;
        this.childTypes = new Set();
        this.childAnnotatedAllowedTypes = [];
        this.metadata = {};
        this.identifier = name;
        this.info = t;
        this.leafKind = t;
    }
}
/**
 * Wrapper around LeafNodeSchema's constructor that provides the return type that is desired in the package public API.
 */
function makeLeaf(name, t) {
    // Names in this domain follow https://en.wikipedia.org/wiki/Reverse_domain_name_notation
    return new LeafNodeSchema(`com.fluidframework.leaf.${name}`, t);
}
// Leaf schema shared between all SchemaFactory instances.
export const stringSchema = makeLeaf("string", ValueSchema.String);
export const numberSchema = makeLeaf("number", ValueSchema.Number);
export const booleanSchema = makeLeaf("boolean", ValueSchema.Boolean);
export const nullSchema = makeLeaf("null", ValueSchema.Null);
export const handleSchema = makeLeaf("handle", ValueSchema.FluidHandle);
//# sourceMappingURL=leafNodeSchema.js.map