'use strict';

var BlockService = require('ipfs-block-service');
var mDAG = require('ipfs-merkle-dag');
var DAGService = mDAG.DAGService;
var PeerBook = require('peer-book');

var defaultRepo = require('./default-repo');

var load = require('./ipfs/load');
var version = require('./ipfs/version');
var id = require('./ipfs/id');
var repo = require('./ipfs/repo');
var init = require('./ipfs/init');
var bootstrap = require('./ipfs/bootstrap');
var config = require('./ipfs/config');
var block = require('./ipfs/block');
var object = require('./ipfs/object');
var libp2p = require('./ipfs/libp2p');
var files = require('./ipfs/files');

exports = module.exports = IPFS;

function IPFS(repoInstance) {
  if (!(this instanceof IPFS)) {
    throw new Error('Must be instantiated with new');
  }

  if (typeof repoInstance === 'string' || repoInstance === undefined) {
    repoInstance = defaultRepo(repoInstance);
  }

  this._repo = repoInstance;
  this._blockS = new BlockService(this._repo);
  this._dagS = new DAGService(this._blockS);
  this._peerInfoBook = new PeerBook();
  this._peerInfo = null;
  this._libp2pNode = null;

  this.load = load(this);
  this.version = version(this);
  this.id = id(this);
  this.repo = repo(this);
  this.init = init(this);
  this.bootstrap = bootstrap(this);
  this.config = config(this);
  this.block = block(this);
  this.object = object(this);
  this.libp2p = libp2p(this);
  this.files = files(this);
}