'use strict';

var peerId = require('peer-id');
var PeerInfo = require('peer-info');
var multiaddr = require('multiaddr');

var utils = require('../utils');

module.exports = function load(self) {
  return function (callback) {
    utils.ifRepoExists(self._repo, function (err) {
      if (err) {
        throw err;
      }

      self._repo.config.get(function (err, config) {
        if (err) {
          throw err;
        }
        var pid = peerId.createFromPrivKey(config.Identity.PrivKey);
        self._peerInfo = new PeerInfo(pid);
        config.Addresses.Swarm.forEach(function (addr) {
          self._peerInfo.multiaddr.add(multiaddr(addr));
        });
        callback();
      });
    });
  };
};