'use strict';

var _keys = require('babel-runtime/core-js/object/keys');

var _keys2 = _interopRequireDefault(_keys);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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: 'Print information of 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;
      }

      var mh = utils.isDaemonOn() ? key : new Buffer(bs58.decode(key));

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

        if (typeof block.Key !== 'string') {
          block.Key = bs58.encode(block.Key).toString();
        }

        (0, _keys2.default)(block).forEach(function (key) {
          console.log(key + ': ' + block[key]);
        });
      });
    });
  }
});