'use strict';

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

module.exports = Command.extend({
  desc: 'Remove a raw IPFS block',

  options: {},

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

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

      if (utils.isDaemonOn()) {
        // TODO implement this once `js-ipfs-api` supports it
        throw new Error('rm block with daemon running is not yet implemented');
      }

      var mh = new Buffer(bs58.decode(key));

      ipfs.block.del(mh, function (err) {
        if (err) {
          log.error(err);
          throw err;
        }

        console.log('removed', key);
      });
    });
  }
});