"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.type = exports.normalizeAllowedTypes = exports.normalizeFieldSchema = exports.FieldSchema = exports.createFieldSchema = exports.getDefaultProvider = exports.extractFieldProvider = exports.isConstant = exports.getExplicitStoredKey = exports.getStoredKey = exports.NodeKind = exports.FieldKind = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const index_js_1 = require("../feature-libraries/index.js");
const index_js_2 = require("../util/index.js");
/**
 * Kind of a field on a node.
 * @public
 */
var FieldKind;
(function (FieldKind) {
    /**
     * A field which can be empty or filled.
     * @remarks
     * Allows 0 or one child.
     */
    FieldKind[FieldKind["Optional"] = 0] = "Optional";
    /**
     * A field which must always be filled.
     * @remarks
     * Only allows exactly one child.
     */
    FieldKind[FieldKind["Required"] = 1] = "Required";
    /**
     * A special field used for node identifiers.
     * @remarks
     * Only allows exactly one child.
     */
    FieldKind[FieldKind["Identifier"] = 2] = "Identifier";
})(FieldKind || (exports.FieldKind = FieldKind = {}));
/**
 * Kind of tree node.
 * @public
 */
var NodeKind;
(function (NodeKind) {
    /**
     * A node which serves as a map, storing children under string keys.
     */
    NodeKind[NodeKind["Map"] = 0] = "Map";
    /**
     * A node which serves as an array, storing children in an ordered sequence.
     */
    NodeKind[NodeKind["Array"] = 1] = "Array";
    /**
     * A node which stores a heterogenous collection of children in named fields.
     * @remarks
     * Each field gets its own schema.
     */
    NodeKind[NodeKind["Object"] = 2] = "Object";
    /**
     * A node which stores a single leaf value.
     */
    NodeKind[NodeKind["Leaf"] = 3] = "Leaf";
})(NodeKind || (exports.NodeKind = NodeKind = {}));
/**
 * Maps from a view key to its corresponding {@link FieldProps.key | stored key} for the provided
 * {@link ImplicitFieldSchema | field schema}.
 *
 * @remarks
 * If an explicit stored key was specified in the schema, it will be used.
 * Otherwise, the stored key is the same as the view key.
 */
function getStoredKey(viewKey, fieldSchema) {
    return (0, index_js_2.brand)(getExplicitStoredKey(fieldSchema) ?? viewKey);
}
exports.getStoredKey = getStoredKey;
/**
 * Gets the {@link FieldProps.key | stored key} specified by the schema, if one was explicitly specified.
 * Otherwise, returns undefined.
 */
function getExplicitStoredKey(fieldSchema) {
    return fieldSchema instanceof FieldSchema ? fieldSchema.props?.key : undefined;
}
exports.getExplicitStoredKey = getExplicitStoredKey;
/**
 * Returns true if the given {@link FieldProvider} is a {@link ConstantFieldProvider}
 */
function isConstant(fieldProvider) {
    return fieldProvider.length === 0;
}
exports.isConstant = isConstant;
function extractFieldProvider(input) {
    return input;
}
exports.extractFieldProvider = extractFieldProvider;
function getDefaultProvider(input) {
    return input;
}
exports.getDefaultProvider = getDefaultProvider;
/**
 * All policy for a specific field,
 * including functionality that does not have to be kept consistent across versions or deterministic.
 *
 * This can include policy for how to use this schema for "view" purposes, and well as how to expose editing APIs.
 * Use {@link SchemaFactory} to create the FieldSchema instances, for example {@link SchemaFactory.optional}.
 * @privateRemarks
 * Public access to the constructor is removed to prevent creating expressible but unsupported (or not stable) configurations.
 * {@link createFieldSchema} can be used internally to create instances.
 * @sealed @public
 */
class FieldSchema {
    kind;
    allowedTypes;
    props;
    static {
        exports.createFieldSchema = (kind, allowedTypes, props) => new FieldSchema(kind, allowedTypes, props);
    }
    /**
     * This class is used with instanceof, and therefore should have nominal typing.
     * This field enforces that.
     */
    _typeCheck;
    lazyTypes;
    /**
     * What types of tree nodes are allowed in this field.
     * @remarks Counterpart to {@link FieldSchema.allowedTypes}, with any lazy definitions evaluated.
     */
    get allowedTypeSet() {
        return this.lazyTypes.value;
    }
    constructor(
    /**
     * The {@link https://en.wikipedia.org/wiki/Kind_(type_theory) | kind } of this field.
     * Determines the multiplicity, viewing and editing APIs as well as the merge resolution policy.
     */
    kind, 
    /**
     * What types of tree nodes are allowed in this field.
     */
    allowedTypes, 
    /**
     * Optional properties associated with the field.
     */
    props) {
        this.kind = kind;
        this.allowedTypes = allowedTypes;
        this.props = props;
        this.lazyTypes = new internal_1.Lazy(() => normalizeAllowedTypes(this.allowedTypes));
    }
}
exports.FieldSchema = FieldSchema;
/**
 * Normalizes a {@link ImplicitFieldSchema} to a {@link FieldSchema}.
 */
function normalizeFieldSchema(schema) {
    return schema instanceof FieldSchema ? schema : (0, exports.createFieldSchema)(FieldKind.Required, schema);
}
exports.normalizeFieldSchema = normalizeFieldSchema;
/**
 * Normalizes a {@link ImplicitAllowedTypes} to a set of {@link TreeNodeSchema}s, by eagerly evaluating any
 * lazy schema declarations.
 *
 * @remarks Note: this must only be called after all required schemas have been declared, otherwise evaluation of
 * recursive schemas may fail.
 */
function normalizeAllowedTypes(types) {
    const normalized = new Set();
    if ((0, index_js_2.isReadonlyArray)(types)) {
        for (const lazyType of types) {
            normalized.add(evaluateLazySchema(lazyType));
        }
    }
    else {
        normalized.add(evaluateLazySchema(types));
    }
    return normalized;
}
exports.normalizeAllowedTypes = normalizeAllowedTypes;
function evaluateLazySchema(value) {
    const evaluatedSchema = (0, index_js_1.isLazy)(value) ? value() : value;
    if (evaluatedSchema === undefined) {
        throw new internal_2.UsageError(`Encountered an undefined schema. This could indicate that some referenced schema has not yet been instantiated.`);
    }
    return evaluatedSchema;
}
/**
 * The type of a {@link TreeNode}.
 * For moore information about the type, use `Tree.schema(theNode)` instead.
 * @remarks
 * This symbol mainly exists on nodes to allow TypeScript to provide more accurate type checking.
 * `Tree.is` and `Tree.schema` provide a superset of this information in more friendly ways.
 *
 * This symbol should not manually be added to objects as doing so allows the object to be invalidly used where nodes are expected.
 * Instead construct a real node of the desired type using its constructor.
 * @privateRemarks
 * This prevents non-nodes from being accidentally used as nodes, as well as allows the type checker to distinguish different node types.
 * @public
 */
exports.type = Symbol("TreeNode Type");
//# sourceMappingURL=schemaTypes.js.map