"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RawFieldNode = exports.RawMapNode = exports.RawObjectNode = exports.RawTreeNode = exports.createRawNode = exports.extractRawNodeContent = void 0;
const feature_libraries_1 = require("../feature-libraries");
const util_1 = require("../util");
/** Stores the content of the raw node, i.e. the data that was passed to the factory */
const nodeContent = Symbol();
function extractRawNodeContent(node) {
    if (node instanceof RawTreeNode) {
        const content = node[nodeContent] ?? (0, util_1.fail)("Node content may only be extracted once");
        Reflect.deleteProperty(node, nodeContent);
        return content;
    }
    return undefined;
}
exports.extractRawNodeContent = extractRawNodeContent;
function createRawNode(schema, content) {
    if (schema instanceof feature_libraries_1.ObjectNodeSchema) {
        return new RawObjectNode(schema, content);
    }
    if (schema instanceof feature_libraries_1.MapNodeSchema) {
        return new RawMapNode(schema, content);
    }
    if (schema instanceof feature_libraries_1.FieldNodeSchema) {
        return new RawFieldNode(schema, content);
    }
    (0, util_1.fail)("Unrecognized schema");
}
exports.createRawNode = createRawNode;
/**
 * The base implementation of a node created by {@link createRawNode}.
 */
class RawTreeNode {
    constructor(schema, content) {
        this.schema = schema;
        this[_a] = feature_libraries_1.FlexTreeEntityKind.Node;
        this.type = schema.name;
        this[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");
    }
    [(_a = feature_libraries_1.flexTreeMarker, feature_libraries_1.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");
    }
    [feature_libraries_1.onNextChange](fn) {
        throw rawError("onNextChange event registration");
    }
}
exports.RawTreeNode = RawTreeNode;
/**
 * The implementation of an object node created by {@link createRawNode}.
 */
class RawObjectNode extends RawTreeNode {
    get localNodeKey() {
        throw rawError("Reading local node keys");
    }
}
exports.RawObjectNode = RawObjectNode;
/**
 * The implementation of a map node created by {@link createRawNode}.
 */
class RawMapNode extends RawTreeNode {
    get size() {
        return this[nodeContent].size;
    }
    has(key) {
        return this[nodeContent].has(key);
    }
    get(key) {
        return this[nodeContent].get(key);
    }
    getBoxed(key) {
        throw rawError("Reading boxed map values");
    }
    keys() {
        return this[nodeContent].keys();
    }
    values() {
        throw rawError("Iterating map values");
    }
    entries() {
        throw rawError("Iterating map entries");
    }
    forEach(callbackFn, thisArg) {
        throw rawError("Iterating maps with forEach");
    }
    set(key, value) {
        throw rawError("Setting a map entry");
    }
    delete(key) {
        throw rawError("Deleting a map entry");
    }
    get asObject() {
        throw rawError("Converting a map to an object");
    }
    [Symbol.iterator]() {
        return this.entries();
    }
    [feature_libraries_1.boxedIterator]() {
        throw rawError("Boxed iteration");
    }
}
exports.RawMapNode = RawMapNode;
/**
 * The implementation of a field node created by {@link createRawNode}.
 */
class RawFieldNode extends RawTreeNode {
    get content() {
        throw rawError("Reading content of a list");
    }
    get boxedContent() {
        throw rawError("Reading boxed content of a list");
    }
}
exports.RawFieldNode = RawFieldNode;
function rawError(message) {
    return new Error(`${message ?? "Operation"} is not supported on content which has not yet been inserted into the tree`);
}
//# sourceMappingURL=rawNode.js.map