package com.vaadin.components.gwt.polymer.client;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;

public abstract class Polymer {

    private static Set<String> urlImported = new HashSet<>();

    /**
     * Ensures that the tagName has been registered, otherwise injects
     * the appropriate &lt;import&gt; tag in the document header.
     *
     * It loads the component from following the convention:
     * bower_components/tagName/tagName.html
     */
    public static void ensureTag(String tagName) {
        ensureHTMLImport(tagName + "/" + tagName + ".html");
    }

    /**
     * Ensures that the tagName has been registered, otherwise injects
     * the appropriate &lt;import&gt; tag in the document header.
     *
     * @param tagName
     * @param url component path relative to bower_components folder
     */
    public static void ensureTag(String tagName, String src) {
        ensureHTMLImport(src);
    }

    /**
     * Inserts the appropriate &lt;import&gt; of a component given by url.
     *
     * Note that bower_components will be prefixed to the url always.
     *
     * @param url component path relative to bower_components folder
     */
    public static void ensureHTMLImport(String url) {
        String href = "bower_components/" + url;
        if (!urlImported.contains(href)) {
            Element link = Document.get().createLinkElement();
            link.setAttribute("rel", "import");
            link.setAttribute("href", GWT.getModuleBaseForStaticFiles() + href);
            Document.get().getHead().appendChild(link);
            urlImported.add(href);
        }
    }

    /**
     * Returns a new instance of the Element. It loads the webcomponent
     * from the bower_components/src url if it was not loaded yet.
     */
    public static <T> T createElement(String tagName, String src) {
        ensureTag(tagName);
        return (T)Document.get().createElement(tagName);
    }

    /**
     * Returns a new instance of the Element. It loads the webcomponent
     * from the bower_components/tagName/tagName.html url if not loaded yet.
     */
    public static <T> T createElement(String tagName) {
        ensureTag(tagName);
        return (T)Document.get().createElement(tagName);
    }

    /**
     * Returns the JsInterop instance of Document
     */
    public static com.vaadin.components.gwt.polymer.client.webapi.Document getDocument() {
        return (com.vaadin.components.gwt.polymer.client.webapi.Document)Document.get();
    }
}

@@ -649,52 +649,25 @@
 sure
-HTMLImport(tagName + %22/%22 + tagName + %22.html%22
+Tag(tagName, null
 );%0A 
@@ -906,32 +906,104 @@
 omponents folder
+.%0A     *        if null it imports bower_components/tagName/tagName.html
 %0A     */%0A    pub
@@ -1086,16 +1086,66 @@
 port(src
+ == null ? tagName + %22/%22 + tagName + %22.html%22 : src
 );%0A    %7D
@@ -2076,32 +2076,37 @@
 nsureTag(tagName
+, src
 );%0A        retur
@@ -2389,61 +2389,15 @@
     
-ensureTag(tagName);%0A        return (T)Document.get().
+return 
 crea
@@ -2405,32 +2405,38 @@
 eElement(tagName
+, null
 );%0A    %7D%0A%0A    /*

use src based on tagname when src is null