'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var fromString = require('uint8arrays/from-string');
var base58 = require('multiformats/bases/base58');
var cid = require('multiformats/cid');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var all = require('it-all');
var raw = require('multiformats/codecs/raw');
var sha2 = require('multiformats/hashes/sha2');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n["default"] = e;
  return Object.freeze(n);
}

var all__default = /*#__PURE__*/_interopDefaultLegacy(all);
var raw__namespace = /*#__PURE__*/_interopNamespace(raw);

function testPut(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.block.put', () => {
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should put a buffer, using defaults', async () => {
      const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ';
      const blob = fromString.fromString('blorb');
      const cid = await ipfs.block.put(blob);
      chai_js.expect(cid.toString()).to.equal(expectedHash);
      chai_js.expect(cid.bytes).to.equalBytes(base58.base58btc.decode(`z${ expectedHash }`));
    });
    it('should put a buffer, using options', async () => {
      const blob = fromString.fromString(`TEST${ Math.random() }`);
      const cid = await ipfs.block.put(blob, {
        format: 'raw',
        mhtype: 'sha2-512',
        version: 1,
        pin: true
      });
      chai_js.expect(cid.version).to.equal(1);
      chai_js.expect(cid.code).to.equal(raw__namespace.code);
      chai_js.expect(cid.multihash.code).to.equal(sha2.sha512.code);
      chai_js.expect(await all__default["default"](ipfs.pin.ls({ paths: cid }))).to.have.lengthOf(1);
    });
    it('should put a Block instance', async () => {
      const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ';
      const expectedCID = cid.CID.parse(expectedHash);
      const b = fromString.fromString('blorb');
      const cid$1 = await ipfs.block.put(b);
      chai_js.expect(cid$1.multihash.bytes).to.equalBytes(expectedCID.multihash.bytes);
    });
  });
}

exports.testPut = testPut;
