import { escapeCData, escapeComment, escapeHTMLEntities } from "../utils/functions.js";
export var NodeType;
(function (NodeType) {
    NodeType[NodeType["ELEMENT_NODE"] = 1] = "ELEMENT_NODE";
    NodeType[NodeType["TEXT_NODE"] = 3] = "TEXT_NODE";
    NodeType[NodeType["CDATA_SECTION_NODE"] = 4] = "CDATA_SECTION_NODE";
    NodeType[NodeType["COMMENT_NODE"] = 8] = "COMMENT_NODE";
    NodeType[NodeType["DOCUMENT_NODE"] = 9] = "DOCUMENT_NODE";
    NodeType[NodeType["DOCUMENT_TYPE_NODE"] = 10] = "DOCUMENT_TYPE_NODE";
    NodeType[NodeType["DOCUMENT_FRAGMENT_NODE"] = 11] = "DOCUMENT_FRAGMENT_NODE";
})(NodeType || (NodeType = {}));
export class Node {
    constructor(nodeType) {
        this.childNodes = [];
        this._nodeValue = null;
        this._parentNode = null;
        this.nodeType = nodeType;
    }
    get firstChild() {
        var _a;
        return (_a = this.childNodes[0]) !== null && _a !== void 0 ? _a : null;
    }
    get lastChild() {
        var _a;
        return (_a = this.childNodes[this.childNodes.length - 1]) !== null && _a !== void 0 ? _a : null;
    }
    get nextSibling() {
        var _a;
        if (this.parentNode !== null) {
            const index = this.parentNode.childNodes.indexOf(this);
            if (index !== -1) {
                return (_a = this.parentNode.childNodes[index + 1]) !== null && _a !== void 0 ? _a : null;
            }
        }
        return null;
    }
    get previousSibling() {
        var _a;
        if (this.parentNode !== null) {
            const index = this.parentNode.childNodes.indexOf(this);
            if (index !== -1) {
                return (_a = this.parentNode.childNodes[index - 1]) !== null && _a !== void 0 ? _a : null;
            }
        }
        return null;
    }
    hasChildNodes() {
        return this.childNodes.length > 0;
    }
    appendChild(node) {
        this.childNodes.push(node);
        node.__parentNode = this;
        return node;
    }
    set __parentNode(parent) {
        this._parentNode = parent;
    }
    get parentNode() {
        return this._parentNode;
    }
    set textContent(text) {
        this._nodeValue = text;
    }
    get textContent() {
        return this._nodeValue;
    }
    set nodeValue(value) {
        this._nodeValue = value;
    }
    get nodeValue() {
        if (typeof this._nodeValue === "string") {
            if (this.nodeType === NodeType.TEXT_NODE) {
                return escapeHTMLEntities(this._nodeValue);
            }
            else if (this.nodeType === NodeType.COMMENT_NODE) {
                return escapeComment(this._nodeValue);
            }
            else if (this.nodeType === NodeType.CDATA_SECTION_NODE) {
                return escapeCData(this._nodeValue);
            }
            else {
                return "";
            }
        }
        return null;
    }
}
//# sourceMappingURL=Node.js.map