'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

require('../../../../../../../_virtual/_commonjsHelpers.js');
var _default = require('../../../../../../../_virtual/default2.js');
require('../../../../../../../_virtual/html.js');
var html = require('../../../../../../../_virtual/html2.js');

exports.insertBefore = void 0;
exports.appendChild = void 0;
'use strict';

const { DOCUMENT_MODE } = html.__exports;

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

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

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

var createCommentNode = _default.__exports.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 = (exports.appendChild = _default.__exports.appendChild = function(parentNode, newNode) {
    parentNode.childNodes.push(newNode);
    newNode.parentNode = parentNode;
});

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

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

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

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

var setDocumentType = _default.__exports.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.__exports.setDocumentMode = function(document, mode) {
    document.mode = mode;
};

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

var detachNode = _default.__exports.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.__exports.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.__exports.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.__exports.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.__exports.getFirstChild = function(node) {
    return node.childNodes[0];
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

exports.default = _default.__exports;
exports.adoptAttributes = adoptAttributes;
exports.createCommentNode = createCommentNode;
exports.createDocument = createDocument;
exports.createDocumentFragment = createDocumentFragment;
exports.createElement = createElement;
exports.detachNode = detachNode;
exports.getAttrList = getAttrList;
exports.getChildNodes = getChildNodes;
exports.getCommentNodeContent = getCommentNodeContent;
exports.getDocumentMode = getDocumentMode;
exports.getDocumentTypeNodeName = getDocumentTypeNodeName;
exports.getDocumentTypeNodePublicId = getDocumentTypeNodePublicId;
exports.getDocumentTypeNodeSystemId = getDocumentTypeNodeSystemId;
exports.getFirstChild = getFirstChild;
exports.getNamespaceURI = getNamespaceURI;
exports.getNodeSourceCodeLocation = getNodeSourceCodeLocation;
exports.getParentNode = getParentNode;
exports.getTagName = getTagName;
exports.getTemplateContent = getTemplateContent;
exports.getTextNodeContent = getTextNodeContent;
exports.insertText = insertText;
exports.insertTextBefore = insertTextBefore;
exports.isCommentNode = isCommentNode;
exports.isDocumentTypeNode = isDocumentTypeNode;
exports.isElementNode = isElementNode;
exports.isTextNode = isTextNode;
exports.setDocumentMode = setDocumentMode;
exports.setDocumentType = setDocumentType;
exports.setNodeSourceCodeLocation = setNodeSourceCodeLocation;
exports.setTemplateContent = setTemplateContent;
exports.updateNodeSourceCodeLocation = updateNodeSourceCodeLocation;
//# sourceMappingURL=default.js.map
