'use strict';

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

var chai_js = require('aegir/utils/chai.js');
var isShardAtPath = require('./is-shard-at-path.js');
var last = require('it-last');

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

var last__default = /*#__PURE__*/_interopDefaultLegacy(last);

async function createShardedDirectory(ipfs, files = 1001) {
  const dirPath = `/sharded-dir-${ Math.random() }`;
  const result = await last__default["default"](ipfs.addAll(function* () {
    for (let i = 0; i < files; i++) {
      yield {
        path: `${ dirPath }/file-${ i }`,
        content: Uint8Array.from([
          0,
          1,
          2,
          3,
          4,
          5,
          i
        ])
      };
    }
  }(), {
    preload: false,
    pin: false
  }));
  if (!result) {
    throw new Error('No result received from ipfs.addAll');
  }
  await ipfs.files.cp(`/ipfs/${ result.cid }`, dirPath);
  await chai_js.expect(isShardAtPath(dirPath, ipfs)).to.eventually.be.true('Tried to create a shared directory but the result was not a shard');
  return dirPath;
}

exports.createShardedDirectory = createShardedDirectory;
