'use strict';

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

var fromString = require('uint8arrays/from-string');
var dagCBOR = require('@ipld/dag-cbor');
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 dagCBOR__namespace = /*#__PURE__*/_interopNamespace(dagCBOR);

function testPut(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.dag.put', () => {
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    const pbNode = {
      Data: fromString.fromString('some data'),
      Links: []
    };
    const cborNode = { data: fromString.fromString('some other data') };
    const joseNode = 'eyJhbGciOiJFUzI1NksifQ.AXESICjDGMg3fEBSX7_fpbBUYF4E61TXLysmLJgfGEpFG8Pu.z7a2MvPWLsd7leOeHyfeA1OcAFC9yy5rn1HD8xCeHz3nFrwyn_Su5xXUoaIxAre3fXhGjPkVSNiCE36AKiaMng';
    it('should put dag-pb with default hash func (sha2-256)', () => {
      return ipfs.dag.put(pbNode, {
        storeCodec: 'dag-pb',
        hashAlg: 'sha2-256'
      });
    });
    it('should put dag-pb with non-default hash func (sha2-512)', () => {
      return ipfs.dag.put(pbNode, {
        storeCodec: 'dag-pb',
        hashAlg: 'sha2-512'
      });
    });
    it('should put dag-cbor with default hash func (sha2-256)', () => {
      return ipfs.dag.put(cborNode, {
        storeCodec: 'dag-cbor',
        hashAlg: 'sha2-256'
      });
    });
    it('should put dag-cbor with non-default hash func (sha2-512)', () => {
      return ipfs.dag.put(cborNode, {
        storeCodec: 'dag-cbor',
        hashAlg: 'sha2-512'
      });
    });
    it('should put dag-jose with default hash func (sha2-256)', () => {
      return ipfs.dag.put(joseNode, {
        storeCodec: 'dag-jose',
        hashAlg: 'sha2-256'
      });
    });
    it('should put dag-jose with non-default hash func (sha2-512)', () => {
      return ipfs.dag.put(joseNode, {
        storeCodec: 'dag-jose',
        hashAlg: 'sha2-512'
      });
    });
    it('should return the cid', async () => {
      const cid$1 = await ipfs.dag.put(cborNode, {
        storeCodec: 'dag-cbor',
        hashAlg: 'sha2-256'
      });
      chai_js.expect(cid$1).to.exist();
      chai_js.expect(cid$1).to.be.an.instanceOf(cid.CID);
      const bytes = dagCBOR__namespace.encode(cborNode);
      const hash = await sha2.sha256.digest(bytes);
      const _cid = cid.CID.createV1(dagCBOR__namespace.code, hash);
      chai_js.expect(cid$1.bytes).to.eql(_cid.bytes);
    });
    it('should not fail when calling put without options', () => {
      return ipfs.dag.put(cborNode);
    });
    it('should set defaults when calling put without options', async () => {
      const cid = await ipfs.dag.put(cborNode);
      chai_js.expect(cid.code).to.equal(dagCBOR__namespace.code);
      chai_js.expect(cid.multihash.code).to.equal(sha2.sha256.code);
    });
    it('should override hash algorithm default and resolve with it', async () => {
      const cid = await ipfs.dag.put(cborNode, {
        storeCodec: 'dag-cbor',
        hashAlg: 'sha2-512'
      });
      chai_js.expect(cid.code).to.equal(dagCBOR__namespace.code);
      chai_js.expect(cid.multihash.code).to.equal(sha2.sha512.code);
    });
    it.skip('should put by passing the cid instead of format and hashAlg', done => {
    });
  });
}

exports.testPut = testPut;
