var { ActionTypes, SLUG, DELIM } = require('../Constants');
var ApiUtil = require('../utils/ApiUtil');
var AppDispatcher = require('../AppDispatcher');

var ViewActionCreators = {
  loadBuild (url) {
    ApiUtil.loadBuild(url);
  },

  getBuildsFromUrl () {
    var slugSplit = location.href.split(SLUG);
    var host = slugSplit[0];
    var query = slugSplit[1];
    debugger;
    if (query) {
      var builds = query.split(DELIM);
      
      AppDispatcher.handleViewAction({
        type: ActionTypes.BUILDS_GRABBED,
        builds: builds
      });
    } else {
      AppDispatcher.handleViewAction({
        type: ActionTypes.NO_QUERY
      });
    }
  },

  addBuild (build) {
    var slugIndex = location.href.indexOf(SLUG);
    if (slugIndex > 0) {
      location.href += (DELIM + build);
    } else {
      location.href += (SLUG + build);
    }
    this.getBuildsFromUrl();
  },

  removeBuild (build) {
    var slugSplit = location.href.split(SLUG);
    var host = slugSplit[0];
    var query = slugSplit[1];
    var builds = query.split(DELIM);
    var filteredBuilds = builds.filter((curr) => {
      return curr !== build;
    });

    if(filteredBuilds.length > 0) {
      var filteredQuery = filteredBuilds.join(DELIM);
      location.href = (host + SLUG + filteredQuery);
    } else {
      location.href = host;
    }

    AppDispatcher.handleViewAction({
      type: ActionTypes.BUILD_REMOVED,
      build: build
    });
  }
};

module.exports = ViewActionCreators;


@@ -361,22 +361,8 @@
 1%5D;%0A
-    debugger;%0A
     
@@ -676,21 +676,21 @@
 var slug
-Index
+Split
  = locat
@@ -702,15 +702,13 @@
 ref.
-indexOf
+split
 (SLU
@@ -719,29 +719,257 @@
     
-if (slugIndex %3E 0) %7B%0A
+var host = slugSplit%5B0%5D;%0A    var query = slugSplit%5B1%5D;%0A    if (query) %7B%0A      var builds = query.split(DELIM);%0A      if (builds.indexOf(build) === -1) %7B%0A        var newBuilds = builds.concat(%5Bbuild%5D);%0A        var newQuery = newBuilds.join(DELIM);%0A  
     
@@ -988,27 +988,43 @@
 ref 
-+
 = (
-DELIM + build);
+host + SLUG + newQuery);%0A      %7D
 %0A   
@@ -1057,12 +1057,18 @@
 ref 
-+
 = (
+host + 
 SLUG

fix addBuild action to prevent duplicates in the query and be overall better