'use strict';

var Command = require('ronin').Command;
var utils = require('../../../utils');
var bs58 = require('bs58');
var debug = require('debug');
var log = debug('cli:object');
log.error = debug('cli:object:error');

module.exports = Command.extend({
  desc: 'Remove a link from an object',

  options: {},

  run: function run(root, link) {
    if (!root) {
      throw new Error("Argument 'root' is required");
    }
    if (!link) {
      throw new Error("Argument 'link' is required");
    }

    utils.getIPFS(function (err, ipfs) {
      if (err) {
        throw err;
      }

      if (utils.isDaemonOn()) {
        return ipfs.object.patch.rmLink(root, link, function (err, obj) {
          if (err) {
            log.error(err);
            throw err;
          }

          console.log(obj.Hash);
        });
      }

      var mh = new Buffer(bs58.decode(root));
      ipfs.object.patch.rmLink(mh, link, function (err, obj) {
        if (err) {
          log.error(err);
          throw err;
        }

        console.log(bs58.encode(obj.multihash()).toString());
      });
    });
  }
});