import { Mixin, hasPrototype } from '../af/util/make.js';

/**
 * selects a dom node by the given query.
 *
 * @function
 * @deprecated don't use this anymore, polyMask is deprecated.
 *
 * @param {string} query the query selector to search for on the DOM
 * @param {Node} [context] the node to start the searching on
 *
 * @return {Node} the first node that matches the selector
 */
export let selectElement = function(query, context){
    let node = null;

    if (context) {
        node = context.querySelector(query);
    } else {
        node = document.querySelector(query);
    }

    node = polyMask(node);

    return node;
};

/**
 * @function
 * @deprecated don't use anymore. Use {@link document.querySelectorAll}
 *
 * @param {string} query the query to look for
 * @param {Node} context the node to start the searching on
 *
 * @return {NodeList} the node list with all matching nodes
 */
export let selectAllElements = function(query, context) {
    let nodeList = null;

    if (context) {
        nodeList = context.querySelectorAll(query);
    } else {
        nodeList = document.querySelectorAll(query);
    }

    if (window.Polymer) {
        nodeList = [].map.apply(nodeList, [polyMask]);
    }

    return nodeList;
};

/**
 * attempts to extract the original node from an polymer node
 *
 * @function
 * @deprecated there is no need to use this function anymore
 *
 * @param {Node} node the node to unwrap
 *
 * @return {Node} a mixin exposing the original node
 */
export let unwrapPolymerNode = function(node) {
    if (!hasPrototype(node, Node)) {
        return Mixin(node, node.node);
    }

    return node;
};

/**
 * creates a mixin of the node and a wrapped version from Polymer
 *
 * @function
 * @deprecated this method shouldn't be used anymore. Use polyInvoke
 *
 * @param {Node} node the dom node to mask
 *
 * @return {Node} returns the masked node
 */
export let polyMask = function(node){
    let polyNode = {};

    let additions = {
        get bare (){
            return node;
        }
    };

    if (window.Polymer) {
        polyNode = window.Polymer.dom(node);
    }

    return Mixin(polyNode, node, additions);
};

/**
 * Tries to call Polymers dom() function if available, to keep them in the loop.
 *
 * @param {Node} node the node we want to take care of.
 * @return {Node} the dom node, maybe wrapped.
 */
export let polyInvoke = function(node) {

    if (window.Polymer) {
        node = window.Polymer.dom(node);
    }

    return node;
};

/**
 * attempts to find a parent node with a particular node name
 *
 * @function
 *
 * @param {Node} node the base node
 * @param {string} parentName the node name to search for
 *
 * @return {Node} the node we where searching for
 */
export let getPolyParent = function(node, parentName){
    while (node && node.localName !== parentName) {
        node = node.parentNode;
    }

    return node;
};

export let sanitizeText = function(rawText) {
    let text = rawText.replace(/&nbsp;/g, '\u00a0').replace(/<br>/, '\n');

    // html escape
    text = document.createTextNode(text).textContent;

    //fix legal HTML
    text = text.replace(/\n/g, '<br>').replace(/ {2}/g, ' &nbsp;');

    return text;
};

/**
 * checks if a node is currenty visible on the viewport
 *
 * @param  {Node} node - the node to check
 * @return {boolean} - the visibility status of the node
 */
export let nodeIsVisible = function(node) {
    return node.offsetHeight === 0 && node.offsetWidth === 0;
};

@@ -1,63 +1,4 @@
-import %7B Mixin, hasPrototype %7D from '../af/util/make.js';%0A%0A
 /**%0A
@@ -527,36 +527,8 @@
  %7D%0A%0A
-    node = polyMask(node);%0A%0A
     
@@ -1046,1035 +1046,23 @@
     
-if (window.Polymer) %7B%0A        nodeList = %5B%5D.map.apply(nodeList, %5BpolyMask%5D);%0A    %7D%0A%0A    return nodeList;%0A%7D;%0A%0A/**%0A * attempts to extract the original node from an polymer node%0A *%0A * @function%0A * @deprecated there is no need to use this function anymore%0A *%0A * @param %7BNode%7D node the node to unwrap%0A *%0A * @return %7BNode%7D a mixin exposing the original node%0A */%0Aexport let unwrapPolymerNode = function(node) %7B%0A    if (!hasPrototype(node, Node)) %7B%0A        return Mixin(node, node.node);%0A    %7D%0A%0A    return node;%0A%7D;%0A%0A/**%0A * creates a mixin of the node and a wrapped version from Polymer%0A *%0A * @function%0A * @deprecated this method shouldn't be used anymore. Use polyInvoke%0A *%0A * @param %7BNode%7D node the dom node to mask%0A *%0A * @return %7BNode%7D returns the masked node%0A */%0Aexport let polyMask = function(node)%7B%0A    let polyNode = %7B%7D;%0A%0A    let additions = %7B%0A        get bare ()%7B%0A            return node;%0A        %7D%0A    %7D;%0A%0A    if (window.Polymer) %7B%0A        polyNode = window.Polymer.dom(node);%0A    %7D%0A%0A    return Mixin(polyNode, node, additions)
+return nodeList
 ;%0A%7D;

remove deprecated, dead and obsolete code