// BRICO


var through = require('through')
var proc = process.title // node or browser

module.exports = function bricoleur (data) {
  var conf
  var _ = {}

  // get config!
  data.get('config', function handleConfig (e, d) {
    e ? interface.emit('error', e) : conf = JSON.parse(d) 
  })

  // DATA 
  var dataFilter = through(function write(d) {
    if (d.type == 'del') fil.rm(d)

    if (d.type === 'put') {
      var path = d.key.split(':')
      var action = path[0]
      var loc = path[1]
      var id = path[2]
      if (typeof d.value === 'string' && d.value[0] === '{') d.value = JSON.parse(d.value)

      switch (action) {
        case config.spaces.active : fil.put(d); break;
        default : interface.emit('error', new Error('unable to handle action: '+action))
      }
    }
  }, function end () {
    this.emit('end')
  })

  var fil = {
    put: function (mod) {
      // a way to insert options?
      var m = mod.key.split(':')[1]
      var mPath = config.dir_wilds+m
      _[m] = require(mPath)(m.value)
      console.log(_)
    },
    rm: function (mod) {
      // unpipe? close stream!?
      _[mod.key].emit('close')
      delete _[mod.key]
      console.log(_)
    },
    conn: function (mods) {
      var mods = con.split('-')
      _[mods[0]].pipe(_[mods[1]])
    },
    disconn: function (mods) {
      var mods = con.split('-')
      _[mods[0]].unpipe(_[mods[1]])
    }
  }

  var liveStream = data.liveStream({old:false}) 
  liveStream.pipe(dataFilter)


  // WILDS / RUNNING MODULES
  var api = { // use prefixes from config
    put: function (args) {
      var match = false
      var opts = {}

      // check module exists
      var ks = data.createKeyStream()
      ks.on('data', function (d) {
        var path = d.split(':')
        if (args[1]===path[1]) {
          match = true
          for (var i=2; i<args.length;i++) {
            var pair = args[i].split('=')
            var key = pair[0]
            var val = pair[1]
            opts[key] = val
          }
          if (opts === {}) opts = null
          data.put(config.spaces.active+':'+args[1],JSON.stringify(opts))
        }
        // what prefix?
      })
      ks.on('end', function () {
        if (match!==true) interface.emit('error', new Error('could not find module'))
      })
    },
    rm: function (mod) {
      data.del(mod[1], function (e) {
        if (e) interface.emit('error',e)
      })
    }
  }

  // METHODS / API
  var interface = through(function (input) {
    var args = input.split(' ')
    var cmd = args[0]
    api[cmd] ? api[cmd](args) : this.emit('error', new Error('no such command'))
  }, function end () {
    this.emit('end')
  })

  return interface
}

@@ -1596,74 +1596,326 @@
 var 
-match = false%0A      var opts = %7B%7D%0A%0A      // check module exists%0A  
+opts = %7B%7D%0A      search(args, function () %7B%0A        // data.put(%0A      %7D)%0A    %7D,%0A    rm: function (mod) %7B%0A      data.del(mod%5B1%5D, function (e) %7B%0A        if (e) interface.emit('error',e)%0A      %7D)%0A    %7D%0A  %7D%0A%0A  function search (args, cb) %7B%0A    var match = false%0A    // check module exists // move this to a search function%0A
     
@@ -1946,18 +1946,16 @@
 tream()%0A
-  
     ks.o
@@ -1981,26 +1981,24 @@
 (d) %7B%0A      
-  
 var path = d
@@ -2011,26 +2011,24 @@
 (':')%0A      
- 
- 
 if (args%5B1%5D=
@@ -2048,18 +2048,16 @@
         
-  
 match = 
@@ -2069,18 +2069,16 @@
         
- 
- 
 for (var
@@ -2100,26 +2100,24 @@
 ngth;i++) %7B%0A
-  
           va
@@ -2150,26 +2150,24 @@
 )%0A          
-  
 var key = pa
@@ -2168,26 +2168,24 @@
 y = pair%5B0%5D%0A
-  
           va
@@ -2210,18 +2210,16 @@
         
-  
 opts%5Bkey
@@ -2230,30 +2230,26 @@
 val%0A        
- 
- 
 %7D%0A
-  
         if (
@@ -2273,18 +2273,16 @@
  = null%0A
-  
         
@@ -2351,27 +2351,24 @@
 )%0A      
-  
 %7D%0A    
-   
+%7D)
  // what
@@ -2376,27 +2376,16 @@
 prefix?%0A
-      %7D)%0A  
     ks.o
@@ -2403,26 +2403,24 @@
 nction () %7B%0A
-  
       if (ma
@@ -2499,138 +2499,10 @@
     
-  %7D)%0A    %7D,%0A    rm: function (mod) %7B%0A      data.del(mod%5B1%5D, function (e) %7B%0A        if (e) interface.emit('error',e)%0A      %7D)%0A    
 %7D
+)
 %0A  %7D

move streaming search into its own function