"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.factoryObjectErrorMessage = exports.addFactory = exports.getFactoryContent = void 0;
const contextuallyTyped_1 = require("../../contextuallyTyped");
const factoryContent = Symbol("Node content");
/**
 * Returns the content stored on an object created by a {@link SharedTreeObjectFactory}.
 */
function getFactoryContent(x) {
    return x[factoryContent];
}
exports.getFactoryContent = getFactoryContent;
/**
 * Adds a factory function (`create`) to the given schema so that it satisfies the {@link SharedTreeObjectFactory} interface.
 */
function addFactory(schema) {
    const create = (content) => {
        const node = Object.create(null);
        // Shallow copy the content and then add the type name symbol to it.
        // The copy is necessary so that the input `content` object can be re-used as the contents of a different typed/named node in another `create` call.
        const namedContent = { ...content, [contextuallyTyped_1.typeNameSymbol]: schema.name };
        Object.defineProperty(node, factoryContent, { value: namedContent });
        for (const [key] of schema.objectNodeFields) {
            Object.defineProperty(node, key, {
                // TODO: `node` could be made fully readable by recursively constructing/returning objects, maps and lists and values here.
                get: () => factoryObjectError(),
                set: () => factoryObjectError(),
                enumerable: true,
            });
        }
        return node;
    };
    return Object.defineProperty(schema, "create", {
        value: create,
        configurable: true,
        enumerable: true,
    });
}
exports.addFactory = addFactory;
function factoryObjectError() {
    throw new Error(exports.factoryObjectErrorMessage);
}
exports.factoryObjectErrorMessage = "Newly created node must be inserted into the tree before being queried";
//# sourceMappingURL=objectFactory.js.map