"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaFactoryRecursive = void 0;
const feature_libraries_1 = require("../feature-libraries");
const schemaFactory_1 = require("./schemaFactory");
/**
 * Extends SchemaFactory with utilities for recursive types.
 *
 * @remarks This is separated from SchemaFactory as these APIs are more experimental, and may be stabilized independently.
 *
 * @sealed @internal
 */
class SchemaFactoryRecursive extends schemaFactory_1.SchemaFactory {
    /**
     * {@link SchemaFactory.object} except tweaked to work better for recursive types.
     * @remarks
     * For unknown reasons, recursive objects work better (compile in more cases)
     * if they their insertable types and node types are not required to be an object.
     * This reduces type safety a bit, but is worth it to make the recursive types actually work at all.
     */
    objectRecursive(name, t) {
        return this.object(name, t);
    }
    /**
     * For unknown reasons, recursive arrays work better (compile in more cases)
     * if their constructor takes in an object with a member containing the iterable,
     * rather than taking the iterable as a parameter directly.
     *
     * This version of `array` leverages this fact, and has a constructor that requires its data be passed in like:
     * ```typescript
     * new MyRecursiveArray({x: theData});
     * ```
     */
    arrayRecursive(name, allowedTypes) {
        class RecursiveArray extends this.namedArray_internal(name, allowedTypes, true, false) {
            constructor(data) {
                if ((0, feature_libraries_1.isFlexTreeNode)(data)) {
                    super(data);
                }
                else {
                    super(data.x);
                }
            }
        }
        return RecursiveArray;
    }
    /**
     * For unknown reasons, recursive maps work better (compile in more cases)
     * if their constructor does not take in the desired type.
     *
     * This version of `map` leverages this fact and takes in undefined instead.
     * Unfortunately this means all maps created this way must be created empty then filled later.
     * @privateRemarks
     * TODO:
     * Figure out a way to make recursive prefilled maps work.
     */
    mapRecursive(name, allowedTypes) {
        class MapSchema extends this.namedMap_internal(name, allowedTypes, true, false) {
            constructor(data) {
                if ((0, feature_libraries_1.isFlexTreeNode)(data)) {
                    super(data);
                }
                else {
                    super(new Map());
                }
            }
        }
        return MapSchema;
    }
}
exports.SchemaFactoryRecursive = SchemaFactoryRecursive;
//# sourceMappingURL=schemaFactoryRecursive.js.map