var virtualDom = require("virtual-dom");
var nanoajax = require("nanoajax");

var config = window.config;

var h = virtualDom.h;
var diff = virtualDom.diff;
var createElement = virtualDom.create;
var patch = virtualDom.patch;

var requestBody = JSON.stringify({
    url: config.url,
    interests: config.interests
});
var interval = config.interval;

var buildPipelineTree = function(pipeline) {
    var stageNodes = pipeline.stages.map(function(stage) {
        return h("div", {
            className: "stage " + stage.status.toLowerCase()
        }, [stage.name]);
    });

    return h("div", {
        className: "pipeline"
    }, [
        h("div", {
            className: "stage-container"
        }, stageNodes),
        h("div", {
            className: "pipeline-name"
        }, [pipeline.name])
    ]);
};

var renderPipeline = function(pipelines) {
    return h("div", {
        id: "dashboard"
    }, pipelines.map(buildPipelineTree));
};

var renderError = function(message) {
    return h("div", {
        id: "dashboard",
        className: "error"
    }, message);
};


var isSuccess = function(code) {
    return code >= 200 && code <= 299;
};

var dash = function() {
        nanoajax.ajax({
            url: "/dashy",
            type: "POST",
            body: requestBody
        }, function(code, responseText, request) {
            if (isSuccess(code)) {
                newTree = renderPipeline(JSON.parse(responseText));
            } else {
                newTree = renderError(responseText || "Error!");
            }
            patches = diff(tree, newTree);
            rootNode = patch(rootNode, patches);
            tree = newTree;
            console.log("tick!");
        });
    },

    tree = h("div", {
        id: "dashboard"
    }),
    newTree,
    patches,
    rootNode = createElement(tree);

document.body.appendChild(rootNode);
dash();
setInterval(dash, interval);

@@ -97,16 +97,22 @@
 w.config
+ %7C%7C %7B%7D
 ;%0A%0Avar h
@@ -1088,17 +1088,16 @@
 e);%0A%7D;%0A%0A
-%0A
 var isSu
@@ -1172,19 +1172,21 @@
 var dash
+y
  = 
+(
 function
@@ -1198,39 +1198,38 @@
     
-    nanoajax.ajax(%7B%0A   
+var tree = h(%22div%22, %7B%0A
         
  url
@@ -1228,90 +1228,196 @@
     
- url
+id
 : %22
-/
 dash
-y%22,%0A            type: %22POST%22,%0A            body: requestBody%0A        %7D,
+board%22%0A    %7D);%0A    var newTree = tree;%0A    var patches = function() %7B%0A        return diff(tree, newTree);%0A    %7D;%0A    var rootNode = createElement(tree);%0A%0A    var responseHandler =
  fun
@@ -1461,20 +1461,16 @@
         
-    
 if (isSu
@@ -1488,36 +1488,32 @@
 ) %7B%0A            
-   
- 
 newTree = render
@@ -1556,20 +1556,16 @@
         
-    
 %7D else %7B
@@ -1565,20 +1565,16 @@
  else %7B%0A
-    
         
@@ -1638,61 +1638,10 @@
     
-    
 %7D%0A
-            patches = diff(tree, newTree);%0A    
     
@@ -1678,23 +1678,21 @@
  patches
+()
 );%0A
-    
         
@@ -1715,20 +1715,16 @@
         
-   
- 
 console.
@@ -1745,141 +1745,240 @@
     
+%7D;%0A
     
-%7D);%0A    %7D,%0A%0A    tree = h(%22div%22, %7B%0A        id: %22dashboard%22
+var ajaxOptions = %7B%0A        url: %22/dashy%22,%0A        type: %22POST%22,%0A        body: requestBody
 %0A    %7D
-),
+;%0A
 %0A    
-newTree,%0A    patches,%0A    rootNode = createElement(tree);%0A%0A
+var tick = function() %7B%0A        nanoajax.ajax(ajaxOptions, responseHandler);%0A    %7D;%0A%0A%0A    var init = function() %7B%0A        
 docu
@@ -2014,16 +2014,32 @@
 e);%0A
-dash();%0A
+        tick();%0A        
 setI
@@ -2046,20 +2046,20 @@
 nterval(
-dash
+tick
 , interv
@@ -2063,8 +2063,34 @@
 erval);%0A
+    %7D;%0A%0A    init();%0A%7D());%0A

move state inside a closure