'use strict';

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

var fromString = require('uint8arrays/from-string');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var nanoid = require('nanoid');
var all = require('it-all');
var last = require('it-last');
var drain = require('it-drain');
var cid = require('multiformats/cid');
var raw = require('multiformats/codecs/raw');
var testTimeout = require('../utils/test-timeout.js');

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 last__default = /*#__PURE__*/_interopDefaultLegacy(last);
var drain__default = /*#__PURE__*/_interopDefaultLegacy(drain);
var raw__namespace = /*#__PURE__*/_interopNamespace(raw);

function testRm(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.block.rm', () => {
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should respect timeout option when removing a block', () => {
      return testTimeout(() => drain__default["default"](ipfs.block.rm(cid.CID.parse('QmVwdDCY4SPGVFnNCiZnX5CtzwWDn6kAM98JXzKxE3kCmn'), { timeout: 1 })));
    });
    it('should remove by CID object', async () => {
      const cid$1 = await ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
        storeCodec: 'raw',
        hashAlg: 'sha2-256'
      });
      const localRefs = await all__default["default"](ipfs.refs.local());
      chai_js.expect(localRefs).to.have.property('length').that.is.greaterThan(0);
      chai_js.expect(localRefs.find(ref => ref.ref === cid.CID.createV1(raw__namespace.code, cid$1.multihash).toString())).to.be.ok();
      const result = await all__default["default"](ipfs.block.rm(cid$1));
      chai_js.expect(result).to.be.an('array').and.to.have.lengthOf(1);
      chai_js.expect(result[0].cid.toString()).equal(cid$1.toString());
      chai_js.expect(result[0]).to.not.have.property('error');
      const localRefsAfterRemove = await all__default["default"](ipfs.refs.local());
      chai_js.expect(localRefsAfterRemove.find(ref => ref.ref === cid.CID.createV1(raw__namespace.code, cid$1.multihash).toString())).to.not.be.ok();
    });
    it('should remove multiple CIDs', async () => {
      const cids = await Promise.all([
        ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
          storeCodec: 'raw',
          hashAlg: 'sha2-256'
        }),
        ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
          storeCodec: 'raw',
          hashAlg: 'sha2-256'
        }),
        ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
          storeCodec: 'raw',
          hashAlg: 'sha2-256'
        })
      ]);
      const result = await all__default["default"](ipfs.block.rm(cids));
      chai_js.expect(result).to.have.lengthOf(3);
      result.forEach(res => {
        chai_js.expect(cids.map(cid => cid.toString())).to.include(res.cid.toString());
        chai_js.expect(res).to.not.have.property('error');
      });
    });
    it('should error when removing non-existent blocks', async () => {
      const cid = await ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
        storeCodec: 'raw',
        hashAlg: 'sha2-256'
      });
      await all__default["default"](ipfs.block.rm(cid));
      const result = await all__default["default"](ipfs.block.rm(cid));
      chai_js.expect(result).to.be.an('array').and.to.have.lengthOf(1);
      chai_js.expect(result).to.have.nested.property('[0].error.message').that.includes('block not found');
    });
    it('should not error when force removing non-existent blocks', async () => {
      const cid = await ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
        storeCodec: 'raw',
        hashAlg: 'sha2-256'
      });
      await all__default["default"](ipfs.block.rm(cid));
      const result = await all__default["default"](ipfs.block.rm(cid, { force: true }));
      chai_js.expect(result).to.be.an('array').and.to.have.lengthOf(1);
      chai_js.expect(result[0].cid.toString()).to.equal(cid.toString());
      chai_js.expect(result[0]).to.not.have.property('error');
    });
    it('should return empty output when removing blocks quietly', async () => {
      const cid = await ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
        storeCodec: 'raw',
        hashAlg: 'sha2-256'
      });
      const result = await all__default["default"](ipfs.block.rm(cid, { quiet: true }));
      chai_js.expect(result).to.be.an('array').and.to.have.lengthOf(0);
    });
    it('should error when removing pinned blocks', async () => {
      const cid = await ipfs.dag.put(fromString.fromString(nanoid.nanoid()), {
        storeCodec: 'raw',
        hashAlg: 'sha2-256'
      });
      await ipfs.pin.add(cid);
      const result = await last__default["default"](ipfs.block.rm(cid));
      chai_js.expect(result).to.have.property('error').that.is.an('Error').with.property('message').that.includes('pinned');
    });
    it('should throw error for invalid CID input', () => {
      return chai_js.expect(all__default["default"](ipfs.block.rm('INVALID CID'))).to.eventually.be.rejected();
    });
  });
}

exports.testRm = testRm;
