import '../../../../../../../_virtual/_commonjsHelpers.mjs';
import { __exports as _default } from '../../../../../../../_virtual/default2.mjs';
import '../../../../../../../_virtual/html.mjs';
import { __exports as html } from '../../../../../../../_virtual/html2.mjs';

var insertBefore_1;
var appendChild_1;
'use strict';

const { DOCUMENT_MODE } = html;

//Node construction
var createDocument = _default.createDocument = function() {
    return {
        nodeName: '#document',
        mode: DOCUMENT_MODE.NO_QUIRKS,
        childNodes: []
    };
};

var createDocumentFragment = _default.createDocumentFragment = function() {
    return {
        nodeName: '#document-fragment',
        childNodes: []
    };
};

var createElement = _default.createElement = function(tagName, namespaceURI, attrs) {
    return {
        nodeName: tagName,
        tagName: tagName,
        attrs: attrs,
        namespaceURI: namespaceURI,
        childNodes: [],
        parentNode: null
    };
};

var createCommentNode = _default.createCommentNode = function(data) {
    return {
        nodeName: '#comment',
        data: data,
        parentNode: null
    };
};

const createTextNode = function(value) {
    return {
        nodeName: '#text',
        value: value,
        parentNode: null
    };
};

//Tree mutation
const appendChild = (appendChild_1 = _default.appendChild = function(parentNode, newNode) {
    parentNode.childNodes.push(newNode);
    newNode.parentNode = parentNode;
});

const insertBefore = (insertBefore_1 = _default.insertBefore = function(parentNode, newNode, referenceNode) {
    const insertionIdx = parentNode.childNodes.indexOf(referenceNode);

    parentNode.childNodes.splice(insertionIdx, 0, newNode);
    newNode.parentNode = parentNode;
});

var setTemplateContent = _default.setTemplateContent = function(templateElement, contentElement) {
    templateElement.content = contentElement;
};

var getTemplateContent = _default.getTemplateContent = function(templateElement) {
    return templateElement.content;
};

var setDocumentType = _default.setDocumentType = function(document, name, publicId, systemId) {
    let doctypeNode = null;

    for (let i = 0; i < document.childNodes.length; i++) {
        if (document.childNodes[i].nodeName === '#documentType') {
            doctypeNode = document.childNodes[i];
            break;
        }
    }

    if (doctypeNode) {
        doctypeNode.name = name;
        doctypeNode.publicId = publicId;
        doctypeNode.systemId = systemId;
    } else {
        appendChild(document, {
            nodeName: '#documentType',
            name: name,
            publicId: publicId,
            systemId: systemId
        });
    }
};

var setDocumentMode = _default.setDocumentMode = function(document, mode) {
    document.mode = mode;
};

var getDocumentMode = _default.getDocumentMode = function(document) {
    return document.mode;
};

var detachNode = _default.detachNode = function(node) {
    if (node.parentNode) {
        const idx = node.parentNode.childNodes.indexOf(node);

        node.parentNode.childNodes.splice(idx, 1);
        node.parentNode = null;
    }
};

var insertText = _default.insertText = function(parentNode, text) {
    if (parentNode.childNodes.length) {
        const prevNode = parentNode.childNodes[parentNode.childNodes.length - 1];

        if (prevNode.nodeName === '#text') {
            prevNode.value += text;
            return;
        }
    }

    appendChild(parentNode, createTextNode(text));
};

var insertTextBefore = _default.insertTextBefore = function(parentNode, text, referenceNode) {
    const prevNode = parentNode.childNodes[parentNode.childNodes.indexOf(referenceNode) - 1];

    if (prevNode && prevNode.nodeName === '#text') {
        prevNode.value += text;
    } else {
        insertBefore(parentNode, createTextNode(text), referenceNode);
    }
};

var adoptAttributes = _default.adoptAttributes = function(recipient, attrs) {
    const recipientAttrsMap = [];

    for (let i = 0; i < recipient.attrs.length; i++) {
        recipientAttrsMap.push(recipient.attrs[i].name);
    }

    for (let j = 0; j < attrs.length; j++) {
        if (recipientAttrsMap.indexOf(attrs[j].name) === -1) {
            recipient.attrs.push(attrs[j]);
        }
    }
};

//Tree traversing
var getFirstChild = _default.getFirstChild = function(node) {
    return node.childNodes[0];
};

var getChildNodes = _default.getChildNodes = function(node) {
    return node.childNodes;
};

var getParentNode = _default.getParentNode = function(node) {
    return node.parentNode;
};

var getAttrList = _default.getAttrList = function(element) {
    return element.attrs;
};

//Node data
var getTagName = _default.getTagName = function(element) {
    return element.tagName;
};

var getNamespaceURI = _default.getNamespaceURI = function(element) {
    return element.namespaceURI;
};

var getTextNodeContent = _default.getTextNodeContent = function(textNode) {
    return textNode.value;
};

var getCommentNodeContent = _default.getCommentNodeContent = function(commentNode) {
    return commentNode.data;
};

var getDocumentTypeNodeName = _default.getDocumentTypeNodeName = function(doctypeNode) {
    return doctypeNode.name;
};

var getDocumentTypeNodePublicId = _default.getDocumentTypeNodePublicId = function(doctypeNode) {
    return doctypeNode.publicId;
};

var getDocumentTypeNodeSystemId = _default.getDocumentTypeNodeSystemId = function(doctypeNode) {
    return doctypeNode.systemId;
};

//Node types
var isTextNode = _default.isTextNode = function(node) {
    return node.nodeName === '#text';
};

var isCommentNode = _default.isCommentNode = function(node) {
    return node.nodeName === '#comment';
};

var isDocumentTypeNode = _default.isDocumentTypeNode = function(node) {
    return node.nodeName === '#documentType';
};

var isElementNode = _default.isElementNode = function(node) {
    return !!node.tagName;
};

// Source code location
var setNodeSourceCodeLocation = _default.setNodeSourceCodeLocation = function(node, location) {
    node.sourceCodeLocation = location;
};

var getNodeSourceCodeLocation = _default.getNodeSourceCodeLocation = function(node) {
    return node.sourceCodeLocation;
};

var updateNodeSourceCodeLocation = _default.updateNodeSourceCodeLocation = function(node, endLocation) {
    node.sourceCodeLocation = Object.assign(node.sourceCodeLocation, endLocation);
};

export { adoptAttributes, appendChild_1 as appendChild, createCommentNode, createDocument, createDocumentFragment, createElement, _default as default, detachNode, getAttrList, getChildNodes, getCommentNodeContent, getDocumentMode, getDocumentTypeNodeName, getDocumentTypeNodePublicId, getDocumentTypeNodeSystemId, getFirstChild, getNamespaceURI, getNodeSourceCodeLocation, getParentNode, getTagName, getTemplateContent, getTextNodeContent, insertBefore_1 as insertBefore, insertText, insertTextBefore, isCommentNode, isDocumentTypeNode, isElementNode, isTextNode, setDocumentMode, setDocumentType, setNodeSourceCodeLocation, setTemplateContent, updateNodeSourceCodeLocation };
//# sourceMappingURL=default.mjs.map
