'use strict';

// Requirements
const JspRenderer = require('entoj-export-jsp').export.JspRenderer;


/**
 * @memberOf export
 * @extends export.Renderer
 */
class CmRenderer extends JspRenderer
{
    /**
     * @inheritDocs
     */
    static get className()
    {
        return 'export/CmRenderer';
    }


    /**
     * @inheritDocs
     */
    renderPreface(configuration)
    {
        const promise = super.renderPreface(configuration)
            .then((source) =>
            {
                return configuration.getMacroConfiguration(configuration.macro.name).then((macroConfiguration) =>
                {
                    source+= '<%@ taglib prefix="cm" uri="http://www.coremedia.com/2004/objectserver-1.0-2.0"%>';
                    source+= '<%@ taglib prefix="bp" uri="http://www.coremedia.com/2012/blueprint"%>';
                    source+= '<!-- <%--@elvariable id="settingsService" type="com.coremedia.blueprint.base.settings.SettingsService"--%> -->';
                    if (macroConfiguration && macroConfiguration.namespace && macroConfiguration.type)
                    {
                        source+='<!-- <%--@elvariable id="self" type="' + macroConfiguration.namespace + '.' + macroConfiguration.type + '"--%> -->';
                        source+='<!-- <%--@elvariable id="model" type="' + macroConfiguration.namespace + '.' + macroConfiguration.type + '"--%> -->';
                    }
                    return source;
                });
            });
        return promise;
    }


    /**
     * @inheritDocs
     */
    static get injections()
    {
        return { 'parameters': ['export/CmRenderer.nodeRenderers', 'export/CmRenderer.options'] };
    }
}


// Exports
module.exports.CmRenderer = CmRenderer;

@@ -92,16 +92,42 @@
 nderer;%0A
+const co = require('co');%0A
 %0A%0A/**%0A *
@@ -233,33 +233,32 @@
    * @inheritDoc
-s
 %0A     */%0A    sta
@@ -345,33 +345,32 @@
    * @inheritDoc
-s
 %0A     */%0A    ren
@@ -415,17 +415,22 @@
   const 
-p
+superP
 romise =
@@ -460,24 +460,25 @@
 nfiguration)
+;
 %0A           
@@ -478,202 +478,112 @@
     
-    .then((source) =%3E%0A            %7B%0A                return configuration.getMacroConfiguration(configuration.macro.name).then((macroConfiguration) =%3E%0A                %7B%0A                    source
+const promise = co(function*()%0A        %7B%0A            let result = yield superPromise;%0A            result
 += '
@@ -674,38 +674,30 @@
             
-        source
+result
 += '%3C%25@ tagl
@@ -773,30 +773,22 @@
         
-        source
+result
 += '%3C!--
@@ -892,32 +892,191 @@
 %22--%25%3E --%3E';%0A    
+        if (configuration.macro)%0A            %7B%0A                const macroConfiguration = yield configuration.getMacroConfiguration(configuration.macro.name);%0A
                 
@@ -1166,36 +1166,32 @@
                 
-    
 %7B%0A              
@@ -1192,34 +1192,30 @@
             
-    source
+result
 +='%3C!-- %3C%25--
@@ -1346,18 +1346,14 @@
     
-    source
+result
 +='%3C
@@ -1485,20 +1485,16 @@
         
-    
 %7D%0A      
@@ -1487,32 +1487,38 @@
       %7D%0A        
+    %7D%0A
             retu
@@ -1524,40 +1524,16 @@
 urn 
-source;%0A                %7D);%0A    
+result;%0A
     

use getMacroConfiguration only when a macro was configured