'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 delay = require('delay');
var PeerId = require('peer-id');
var last = require('it-last');
var cid = require('multiformats/cid');
var Digest = require('multiformats/hashes/digest');

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 delay__default = /*#__PURE__*/_interopDefaultLegacy(delay);
var PeerId__default = /*#__PURE__*/_interopDefaultLegacy(PeerId);
var last__default = /*#__PURE__*/_interopDefaultLegacy(last);
var Digest__namespace = /*#__PURE__*/_interopNamespace(Digest);

function testResolve(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.name.resolve offline', function () {
    let ipfs;
    let nodeId;
    before(async () => {
      ipfs = (await factory.spawn({ ipfsOptions: { config: { Routing: { Type: 'none' } } } })).api;
      const peerInfo = await ipfs.id();
      nodeId = peerInfo.id;
    });
    after(() => factory.clean());
    it('should resolve a record default options', async function () {
      this.timeout(20 * 1000);
      const {path} = await ipfs.add(fromString.fromString('should resolve a record default options'));
      const {id: keyId} = await ipfs.key.gen('key-name-default', {
        type: 'rsa',
        size: 2048
      });
      await ipfs.name.publish(path, { allowOffline: true });
      await ipfs.name.publish(`/ipns/${ nodeId }`, {
        allowOffline: true,
        key: 'key-name-default'
      });
      chai_js.expect(await last__default["default"](ipfs.name.resolve(`/ipns/${ keyId }`))).to.eq(`/ipfs/${ path }`);
    });
    it('should resolve a record from peerid as cidv1 in base32', async function () {
      this.timeout(20 * 1000);
      const {cid: cid$1} = await ipfs.add(fromString.fromString('should resolve a record from cidv1b32'));
      const {id: peerId} = await ipfs.id();
      await ipfs.name.publish(cid$1, { allowOffline: true });
      const keyCid = cid.CID.createV1(114, Digest__namespace.decode(PeerId__default["default"].parse(peerId).toBytes()));
      const resolvedPath = await last__default["default"](ipfs.name.resolve(`/ipns/${ keyCid }`));
      chai_js.expect(resolvedPath).to.equal(`/ipfs/${ cid$1 }`);
    });
    it('should resolve a record recursive === false', async () => {
      const {path} = await ipfs.add(fromString.fromString('should resolve a record recursive === false'));
      await ipfs.name.publish(path, { allowOffline: true });
      chai_js.expect(await last__default["default"](ipfs.name.resolve(`/ipns/${ nodeId }`, { recursive: false }))).to.eq(`/ipfs/${ path }`);
    });
    it('should resolve a record recursive === true', async function () {
      this.timeout(20 * 1000);
      const {path} = await ipfs.add(fromString.fromString('should resolve a record recursive === true'));
      const {id: keyId} = await ipfs.key.gen('key-name', {
        type: 'rsa',
        size: 2048
      });
      await ipfs.name.publish(path, { allowOffline: true });
      await ipfs.name.publish(`/ipns/${ nodeId }`, {
        allowOffline: true,
        key: 'key-name'
      });
      chai_js.expect(await last__default["default"](ipfs.name.resolve(`/ipns/${ keyId }`, { recursive: true }))).to.eq(`/ipfs/${ path }`);
    });
    it('should resolve a record default options with remainder', async function () {
      this.timeout(20 * 1000);
      const {path} = await ipfs.add(fromString.fromString('should resolve a record default options with remainder'));
      const {id: keyId} = await ipfs.key.gen('key-name-remainder-default', {
        type: 'rsa',
        size: 2048
      });
      await ipfs.name.publish(path, { allowOffline: true });
      await ipfs.name.publish(`/ipns/${ nodeId }`, {
        allowOffline: true,
        key: 'key-name-remainder-default'
      });
      chai_js.expect(await last__default["default"](ipfs.name.resolve(`/ipns/${ keyId }/remainder/file.txt`))).to.eq(`/ipfs/${ path }/remainder/file.txt`);
    });
    it('should resolve a record recursive === false with remainder', async () => {
      const {path} = await ipfs.add(fromString.fromString('should resolve a record recursive = false with remainder'));
      await ipfs.name.publish(path, { allowOffline: true });
      chai_js.expect(await last__default["default"](ipfs.name.resolve(`/ipns/${ nodeId }/remainder/file.txt`, { recursive: false }))).to.eq(`/ipfs/${ path }/remainder/file.txt`);
    });
    it('should resolve a record recursive === true with remainder', async function () {
      this.timeout(20 * 1000);
      const {path} = await ipfs.add(fromString.fromString('should resolve a record recursive = true with remainder'));
      const {id: keyId} = await ipfs.key.gen('key-name-remainder', {
        type: 'rsa',
        size: 2048
      });
      await ipfs.name.publish(path, { allowOffline: true });
      await ipfs.name.publish(`/ipns/${ nodeId }`, {
        allowOffline: true,
        key: 'key-name-remainder'
      });
      chai_js.expect(await last__default["default"](ipfs.name.resolve(`/ipns/${ keyId }/remainder/file.txt`, { recursive: true }))).to.eq(`/ipfs/${ path }/remainder/file.txt`);
    });
    it('should not get the entry if its validity time expired', async () => {
      const publishOptions = {
        lifetime: '100ms',
        ttl: '10s',
        allowOffline: true
      };
      const {path} = await ipfs.add(fromString.fromString('should not get the entry if its validity time expired'));
      await ipfs.name.publish(path, publishOptions);
      await delay__default["default"](500);
      try {
        await last__default["default"](ipfs.name.resolve(nodeId));
      } catch (error) {
        chai_js.expect(error).to.exist();
      }
    });
  });
  describe('.name.resolve dns', function () {
    let ipfs;
    this.retries(5);
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should resolve /ipns/ipfs.io', async () => {
      chai_js.expect(await last__default["default"](ipfs.name.resolve('/ipns/ipfs.io'))).to.match(/\/ipfs\/.+$/);
    });
    it('should resolve /ipns/ipfs.io recursive === false', async () => {
      chai_js.expect(await last__default["default"](ipfs.name.resolve('/ipns/ipfs.io', { recursive: false }))).to.match(/\/ipns\/.+$/);
    });
    it('should resolve /ipns/ipfs.io recursive === true', async () => {
      chai_js.expect(await last__default["default"](ipfs.name.resolve('/ipns/ipfs.io', { recursive: true }))).to.match(/\/ipfs\/.+$/);
    });
    it('should resolve /ipns/ipfs.io with remainder', async () => {
      chai_js.expect(await last__default["default"](ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg'))).to.match(/\/ipfs\/.+\/images\/ipfs-logo.svg$/);
    });
    it('should resolve /ipns/ipfs.io with remainder recursive === false', async () => {
      chai_js.expect(await last__default["default"](ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: false }))).to.match(/\/ipns\/.+\/images\/ipfs-logo.svg$/);
    });
    it('should resolve /ipns/ipfs.io with remainder recursive === true', async () => {
      chai_js.expect(await last__default["default"](ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: true }))).to.match(/\/ipfs\/.+\/images\/ipfs-logo.svg$/);
    });
    it('should fail to resolve /ipns/ipfs.a', async () => {
      try {
        await last__default["default"](ipfs.name.resolve('ipfs.a'));
      } catch (error) {
        chai_js.expect(error).to.exist();
      }
    });
    it('should resolve ipns path with hamt-shard recursive === true', async () => {
      chai_js.expect(await last__default["default"](ipfs.name.resolve('/ipns/tr.wikipedia-on-ipfs.org/wiki/Anasayfa.html', { recursive: true }))).to.match(/\/ipfs\/.+$/);
    });
  });
}

exports.testResolve = testResolve;
