'use strict';

var bitcore = require('bitcore-lib');
var Inventory = require('../inventory');

function builder(options) {
  /* jshint maxstatements: 20 */
  /* jshint maxcomplexity: 10 */

  if (!options) {
    options = {};
  }

  if (!options.network) {
    options.network = bitcore.Networks.defaultNetwork;
  }

  options.Block = options.Block || bitcore.Block;
  options.BlockHeader = options.BlockHeader || bitcore.BlockHeader;
  options.Transaction = options.Transaction || bitcore.Transaction;
  options.MerkleBlock = options.MerkleBlock || bitcore.MerkleBlock;
  options.protocolVersion = options.protocolVersion || 70000;

  var exported = {
    constructors: {
      Block: options.Block,
      BlockHeader: options.BlockHeader,
      Transaction: options.Transaction,
      MerkleBlock: options.MerkleBlock
    },
    defaults: {
      protocolVersion: options.protocolVersion,
      network: options.network
    },
    inventoryCommands: [
      'getdata',
      'inv',
      'notfound'
    ],
    commandsMap: {
      version: 'Version',
      verack: 'VerAck',
      ping: 'Ping',
      pong: 'Pong',
      block: 'Block',
      tx: 'Transaction',
      getdata: 'GetData',
      headers: 'Headers',
      notfound: 'NotFound',
      inv: 'Inventory',
      addr: 'Addresses',
      alert: 'Alert',
      reject: 'Reject',
      merkleblock: 'MerkleBlock',
      filterload: 'FilterLoad',
      filteradd: 'FilterAdd',
      filterclear: 'FilterClear',
      getblocks: 'GetBlocks',
      getheaders: 'GetHeaders',
      mempool: 'MemPool',
      getaddr: 'GetAddr'
    },
    commands: {}
  };

  exported.add = function(key, Command) {
    exported.commands[key] = function(obj) {
      return new Command(obj, options);
    };

    exported.commands[key]._constructor = Command;

    exported.commands[key].fromBuffer = function(buffer) {
      var message = exported.commands[key]();
      message.setPayload(buffer);
      return message;
    };
  };

  Object.keys(exported.commandsMap).forEach(function(key) {
    exported.add(key, require('./commands/' + key));
  });

  exported.inventoryCommands.forEach(function(command) {

    // add forTransaction methods
    exported.commands[command].forTransaction = function forTransaction(hash) {
      return new exported.commands[command]([Inventory.forTransaction(hash)]);
    };

    // add forBlock methods
    exported.commands[command].forBlock = function forBlock(hash) {
      return new exported.commands[command]([Inventory.forBlock(hash)]);
    };

    // add forFilteredBlock methods
    exported.commands[command].forFilteredBlock = function forFilteredBlock(hash) {
      return new exported.commands[command]([Inventory.forFilteredBlock(hash)]);
    };

  });

  return exported;

}

module.exports = builder;

@@ -1578,24 +1578,1079 @@
 ddr'%0A    %7D,%0A
+    commandsMapBrowserify: %7B%0A      version: require('./commands/version.js'),%0A      verack: require('./commands/verack.js'),%0A      ping: require('./commands/ping.js'),%0A      pong: require('./commands/pong.js'),%0A      block: require('./commands/block.js'),%0A      tx: require('./commands/tx.js'),%0A      getdata: require('./commands/getdata.js'),%0A      headers: require('./commands/headers.js'),%0A      notfound: require('./commands/notfound.js'),%0A      inv: require('./commands/inv.js'),%0A      addr: require('./commands/addr.js'),%0A      alert: require('./commands/alert.js'),%0A      reject: require('./commands/reject.js'),%0A      merkleblock: require('./commands/merkleblock.js'),%0A      filterload: require('./commands/filterload.js'),%0A      filteradd: require('./commands/filteradd.js'),%0A      filterclear: require('./commands/filterclear.js'),%0A      getblocks: require('./commands/getblocks.js'),%0A      getheaders: require('./commands/getheaders.js'),%0A      mempool: require('./commands/mempool.js'),%0A      getaddr: require('./commands/getaddr.js')%0A    %7D,%0A%0A
     commands
@@ -2654,16 +2654,16 @@
 nds: %7B%7D%0A
-
   %7D;%0A%0A  
@@ -3023,16 +3023,18 @@
   %7D;%0A%0A  
+//
 Object.k
@@ -3081,24 +3081,26 @@
 on(key) %7B%0A  
+//
   exported.a
@@ -3138,16 +3138,154 @@
  key));%0A
+  //%7D);%0A  Object.keys(exported.commandsMapBrowserify).forEach(function(key) %7B%0A    exported.add(key, exported.commandsMapBrowserify%5Bkey%5D);%0A
   %7D);%0A%0A 

Update builder.js