'use strict';

var bs58 = require('bs58');
var isIPFS = require('is-ipfs');
var promisify = require('promisify-es6');

module.exports = function (send) {
  var cat = promisify(function (multihash, callback) {
    try {
      multihash = cleanMultihash(multihash);
    } catch (err) {
      return callback(err);
    }
    send('cat', multihash, null, null, callback);
  });
  return cat;
};

function cleanMultihash(multihash) {
  if (!isIPFS.multihash(multihash)) {
    throw new Error('not valid multihash');
  }
  if (Buffer.isBuffer(multihash)) {
    return bs58.encode(multihash);
  }
  return multihash;
}