"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var _RawTreeNode_anchor, _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rawError = exports.RawTreeNode = exports.extractRawNodeContent = exports.nodeContent = void 0;
const index_js_1 = require("../feature-libraries/index.js");
const index_js_2 = require("../util/index.js");
/** Stores the content of the raw node, i.e. the data that was passed to the factory */
exports.nodeContent = Symbol();
/**
 * Retrieve the content of a raw object node created via {@link createRawNode}.
 * @remarks
 * Raw nodes should only ever be processed as input once.
 * Therefore, this extraction removes the content from the node and will error if called twice on the same node.
 */
function extractRawNodeContent(node) {
    if (node instanceof RawTreeNode) {
        const content = node[exports.nodeContent] ?? (0, index_js_2.fail)("Node content may only be extracted once");
        Reflect.deleteProperty(node, exports.nodeContent);
        return content;
    }
    return undefined;
}
exports.extractRawNodeContent = extractRawNodeContent;
/**
 * Node that pretends to satisfy the given schema while wrapping the given node content.
 * Retrieve the node content via {@link extractRawNodeContent}.
 *
 * @remarks This is useful for creating "raw" nodes: nodes which capture data about a pending insertion but are not yet inserted.
 * These raw nodes can be then used on the right-hand side of an assignment (via `=`) to the tree.
 * However, many of their properties and methods are currently unimplemented and will error if accessed.
 */
class RawTreeNode {
    constructor(schema, content) {
        this.schema = schema;
        this[_a] = index_js_1.FlexTreeEntityKind.Node;
        _RawTreeNode_anchor.set(this, void 0);
        this.type = schema.name;
        this[exports.nodeContent] = content;
    }
    get context() {
        throw rawError("Getting context");
    }
    get parentField() {
        throw rawError("Accessing parentage");
    }
    is(schema) {
        return schema === this.schema;
    }
    tryGetField(key) {
        throw rawError("Reading fields");
    }
    getBoxed(key) {
        throw rawError("Reading boxed fields");
    }
    boxedIterator() {
        throw rawError("Boxed iteration");
    }
    treeStatus() {
        // TODO: We could add a new TreeStatus for "raw" nodes.
        throw rawError("Status querying");
    }
    on(eventName, listener) {
        throw rawError("Event registration");
    }
    get anchorNode() {
        throw rawError("Reading anchor node");
    }
}
exports.RawTreeNode = RawTreeNode;
_RawTreeNode_anchor = new WeakMap(), _a = index_js_1.flexTreeMarker;
function rawError(message) {
    return new Error(`${message ?? "Operation"} is not supported on content which has not yet been inserted into the tree`);
}
exports.rawError = rawError;
//# sourceMappingURL=rawNode.js.map