'use strict';

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

var nanoid = require('nanoid');
var fromString = require('uint8arrays/from-string');
var utils = require('./utils.js');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var last = require('it-last');
var PeerId = require('peer-id');

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

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

function testPublish(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.name.publish offline', () => {
    const keyName = nanoid.nanoid();
    let ipfs;
    let nodeId;
    before(async () => {
      ipfs = (await factory.spawn({ ipfsOptions: { config: { Routing: { Type: 'none' } } } })).api;
      const peerInfo = await ipfs.id();
      nodeId = peerInfo.id;
      await ipfs.add(utils.fixture.data, { pin: false });
    });
    after(() => factory.clean());
    it('should publish an IPNS record with the default params', async function () {
      this.timeout(50 * 1000);
      const value = utils.fixture.cid;
      const keys = await ipfs.key.list();
      const self = keys.find(key => key.name === 'self');
      if (!self) {
        throw new Error('No self key found');
      }
      const res = await ipfs.name.publish(value, { allowOffline: true });
      chai_js.expect(res).to.exist();
      chai_js.expect(PeerId__default["default"].parse(res.name).toString()).to.equal(PeerId__default["default"].parse(self.id).toString());
      chai_js.expect(res.value).to.equal(`/ipfs/${ value }`);
    });
    it('should publish correctly with the lifetime option and resolve', async () => {
      const {path} = await ipfs.add(fromString.fromString('should publish correctly with the lifetime option and resolve'));
      await ipfs.name.publish(path, {
        allowOffline: true,
        resolve: false,
        lifetime: '2h'
      });
      chai_js.expect(await last__default["default"](ipfs.name.resolve(`/ipns/${ nodeId }`))).to.eq(`/ipfs/${ path }`);
    });
    it('should publish correctly when the file was not added but resolve is disabled', async function () {
      this.timeout(50 * 1000);
      const value = 'QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU';
      const keys = await ipfs.key.list();
      const self = keys.find(key => key.name === 'self');
      if (!self) {
        throw new Error('No self key found');
      }
      const options = {
        resolve: false,
        lifetime: '1m',
        ttl: '10s',
        key: 'self',
        allowOffline: true
      };
      const res = await ipfs.name.publish(value, options);
      chai_js.expect(res).to.exist();
      chai_js.expect(PeerId__default["default"].parse(res.name).toString()).to.equal(PeerId__default["default"].parse(self.id).toString());
      chai_js.expect(res.value).to.equal(`/ipfs/${ value }`);
    });
    it('should publish with a key received as param, instead of using the key of the node', async function () {
      this.timeout(90 * 1000);
      const value = utils.fixture.cid;
      const options = {
        resolve: false,
        lifetime: '24h',
        ttl: '10s',
        key: keyName,
        allowOffline: true
      };
      const key = await ipfs.key.gen(keyName, {
        type: 'rsa',
        size: 2048
      });
      const res = await ipfs.name.publish(value, options);
      chai_js.expect(res).to.exist();
      chai_js.expect(PeerId__default["default"].parse(res.name).toString()).to.equal(PeerId__default["default"].parse(key.id).toString());
      chai_js.expect(res.value).to.equal(`/ipfs/${ value }`);
    });
  });
}

exports.testPublish = testPublish;
