'use strict';

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

var chai_js = require('aegir/utils/chai.js');
var loadFixture = require('aegir/utils/fixtures.js');
var cid = require('multiformats/cid');
var drain = require('it-drain');
var fromString = require('uint8arrays/from-string');
var first = require('it-first');

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

var loadFixture__default = /*#__PURE__*/_interopDefaultLegacy(loadFixture);
var drain__default = /*#__PURE__*/_interopDefaultLegacy(drain);
var first__default = /*#__PURE__*/_interopDefaultLegacy(first);

const pinTypes = {
  direct: 'direct',
  recursive: 'recursive',
  indirect: 'indirect',
  all: 'all'
};
const fixtures = Object.freeze({
  directory: Object.freeze({
    cid: cid.CID.parse('QmY8KdYQSYKFU5hM7F5ioZ5yYSgV5VZ1kDEdqfRL3rFgcd'),
    files: Object.freeze([
      Object.freeze({
        path: 'test-folder/ipfs-add.js',
        data: loadFixture__default["default"]('test/fixtures/test-folder/ipfs-add.js', 'interface-ipfs-core'),
        cid: cid.CID.parse('QmbKtKBrmeRHjNCwR4zAfCJdMVu6dgmwk9M9AE9pUM9RgG')
      }),
      Object.freeze({
        path: 'test-folder/files/ipfs.txt',
        data: loadFixture__default["default"]('test/fixtures/test-folder/files/ipfs.txt', 'interface-ipfs-core'),
        cid: cid.CID.parse('QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w')
      })
    ])
  }),
  files: Object.freeze([
    Object.freeze({
      data: fromString.fromString('Plz add me!\n'),
      cid: cid.CID.parse('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
    }),
    Object.freeze({
      data: loadFixture__default["default"]('test/fixtures/test-folder/files/hello.txt', 'interface-ipfs-core'),
      cid: cid.CID.parse('QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu')
    })
  ])
});
const clearPins = async ipfs => {
  await drain__default["default"](ipfs.pin.rmAll(ipfs.pin.ls({ type: pinTypes.recursive })));
  await drain__default["default"](ipfs.pin.rmAll(ipfs.pin.ls({ type: pinTypes.direct })));
};
const clearRemotePins = async ipfs => {
  for (const {service} of await ipfs.pin.remote.service.ls()) {
    const cids = [];
    const status = [
      'queued',
      'pinning',
      'pinned',
      'failed'
    ];
    for await (const pin of ipfs.pin.remote.ls({
        status,
        service
      })) {
      cids.push(pin.cid);
    }
    if (cids.length > 0) {
      await ipfs.pin.remote.rmAll({
        cid: cids,
        status,
        service
      });
    }
  }
};
const addRemotePins = async (ipfs, service, pins) => {
  const requests = [];
  for (const [name, cid] of Object.entries(pins)) {
    requests.push(ipfs.pin.remote.add(cid, {
      name,
      service,
      background: true
    }));
  }
  await Promise.all(requests);
};
const clearServices = async ipfs => {
  const services = await ipfs.pin.remote.service.ls();
  await Promise.all(services.map(({service}) => ipfs.pin.remote.service.rm(service)));
};
const expectPinned = async (ipfs, cid, type = pinTypes.all, pinned = true) => {
  if (typeof type === 'boolean') {
    pinned = type;
    type = pinTypes.all;
  }
  const result = await isPinnedWithType(ipfs, cid, type);
  chai_js.expect(result).to.eql(pinned);
};
const expectNotPinned = (ipfs, cid, type = pinTypes.all) => {
  return expectPinned(ipfs, cid, type, false);
};
async function isPinnedWithType(ipfs, cid, type) {
  try {
    const res = await first__default["default"](ipfs.pin.ls({
      paths: cid,
      type
    }));
    return Boolean(res);
  } catch (err) {
    return false;
  }
}

exports.addRemotePins = addRemotePins;
exports.clearPins = clearPins;
exports.clearRemotePins = clearRemotePins;
exports.clearServices = clearServices;
exports.expectNotPinned = expectNotPinned;
exports.expectPinned = expectPinned;
exports.fixtures = fixtures;
exports.isPinnedWithType = isPinnedWithType;
exports.pinTypes = pinTypes;
