'use strict';

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

var fromString = require('uint8arrays/from-string');
var dagPB = require('@ipld/dag-pb');
var nanoid = require('nanoid');
var cid = require('multiformats/cid');
var sha2 = require('multiformats/hashes/sha2');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');

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 dagPB__namespace = /*#__PURE__*/_interopNamespace(dagPB);

function testLinks(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.object.links', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should get empty links by multihash', async () => {
      const testObj = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const cid = await ipfs.object.put(testObj);
      const node = await ipfs.object.get(cid);
      const links = await ipfs.object.links(cid);
      chai_js.expect(node.Links).to.eql(links);
    });
    it('should get links by multihash', async () => {
      const node1a = {
        Data: fromString.fromString('Some data 1'),
        Links: []
      };
      const node2 = {
        Data: fromString.fromString('Some data 2'),
        Links: []
      };
      const node2Buf = dagPB__namespace.encode(node2);
      const link = {
        Name: 'some-link',
        Tsize: node2Buf.length,
        Hash: cid.CID.createV0(await sha2.sha256.digest(node2Buf))
      };
      const node1b = {
        Data: node1a.Data,
        Links: [link]
      };
      const node1bCid = await ipfs.object.put(node1b);
      const links = await ipfs.object.links(node1bCid);
      chai_js.expect(links).to.have.lengthOf(1);
      chai_js.expect(node1b.Links).to.deep.equal(links);
    });
    it('should get links from CBOR object', async () => {
      const hashes = [];
      const res1 = await ipfs.add(fromString.fromString('test data'));
      hashes.push(res1.cid);
      const res2 = await ipfs.add(fromString.fromString('more test data'));
      hashes.push(res2.cid);
      const obj = {
        some: 'data',
        mylink: hashes[0],
        myobj: { anotherLink: hashes[1] }
      };
      const cid = await ipfs.dag.put(obj);
      const links = await ipfs.object.links(cid);
      chai_js.expect(links.length).to.eql(2);
      const cids = [
        links[0].Hash.toString(),
        links[1].Hash.toString()
      ];
      chai_js.expect(cids).includes(hashes[0].toString());
      chai_js.expect(cids).includes(hashes[1].toString());
    });
    it('returns error for request without argument', () => {
      return chai_js.expect(ipfs.object.links(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
    it('returns error for request with invalid argument', () => {
      return chai_js.expect(ipfs.object.links('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
  });
}

exports.testLinks = testLinks;
