"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

require("../injector");

var _Metadata = _interopRequireDefault(require("../Metadata"));

var _static = _interopRequireDefault(require("../Metadata/static"));

var _AccountId = _interopRequireDefault(require("../primitive/Generic/AccountId"));

var _Call = _interopRequireDefault(require("../primitive/Generic/Call"));

var _Text = _interopRequireDefault(require("../primitive/Text"));

var _create = require("./create");

var _Vec = _interopRequireDefault(require("./Vec"));

// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('Vec', () => {
  let vector;
  beforeEach(() => {
    vector = new _Vec.default(_Text.default, ['1', '23', '345', '4567', new _Text.default('56789')]);

    _Call.default.injectMetadata(new _Metadata.default(_static.default));
  });
  it('wraps a sequence of values', () => {
    expect(vector.length).toEqual(5); // eslint-disable-line
  });
  it('has a sane representation for toString', () => {
    expect(vector.toString()).toEqual('[1, 23, 345, 4567, 56789]');
  });
  it('encodes with length prefix', () => {
    expect(vector.toU8a()).toEqual(new Uint8Array([5 << 2, 1 << 2, 49, 2 << 2, 50, 51, 3 << 2, 51, 52, 53, 4 << 2, 52, 53, 54, 55, 5 << 2, 53, 54, 55, 56, 57]));
  });
  it('allows contruction via JSON', () => {
    expect(new _Vec.default(_Text.default, ['6', '7']).toJSON()).toEqual(['6', '7']);
  });
  it('allows contruction via JSON (string type)', () => {
    expect(new _Vec.default('u32', ['6', '7']).toJSON()).toEqual([6, 7]);
  });
  it('exposes the type', () => {
    expect(vector.Type).toEqual('Text');
  });
  it('decodes a complex type via construction', () => {
    const test = (0, _create.createTypeUnsafe)('Vec<(PropIndex, AccountId)>', [new Uint8Array([4, 10, 0, 0, 0, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79])]);
    const first = test[0];
    expect(first[0].toNumber()).toEqual(10);
    expect(first[1].toString()).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaQua');
  });
  describe('vector-like functions', () => {
    it('allows retrieval of a specific item', () => {
      expect(vector[2].toString()).toEqual('345');
    });
    it('exposes a working forEach', () => {
      const result = {};
      vector.forEach((e, i) => {
        result[i] = e.toString();
      });
      expect(result).toEqual({
        0: '1',
        1: '23',
        2: '345',
        3: '4567',
        4: '56789'
      });
    });
    it('exposes a working filter', () => {
      expect(vector.filter((e, i) => i >= 3).toString()).toEqual('4567,56789');
    });
    it('exposes a working map', () => {
      expect(vector.map(e => e.toString().substr(0, 1))).toEqual(['1', '2', '3', '4', '5']);
    });
    it('exposes a working reduce', () => {
      expect(vector.reduce((r, e) => "".concat(r).concat(e), '')).toEqual('123345456756789');
    });
    it('exposes a working indexOf', () => {
      expect(vector.indexOf('1')).toEqual(0);
      expect(vector.indexOf(new _Text.default('23'))).toEqual(1);
      expect(vector.indexOf('0')).toEqual(-1);
    });
  });
  describe('encode', () => {
    const testEncode = (to, expected) => it("can encode ".concat(to), () => {
      expect(vector[to]()).toEqual(expected);
    });

    testEncode('toHex', '0x1404310832330c3334351034353637143536373839');
    testEncode('toJSON', ['1', '23', '345', '4567', '56789']);
    testEncode('toString', '[1, 23, 345, 4567, 56789]');
    testEncode('toU8a', Uint8Array.from([20, 4, 49, 8, 50, 51, 12, 51, 52, 53, 16, 52, 53, 54, 55, 20, 53, 54, 55, 56, 57]));
  });
  describe('utils', () => {
    const vec = new _Vec.default(_Text.default, ['123', '456']);
    it('compares against codec types', () => {
      expect(vec.eq([new _Text.default('123'), new _Text.default('456')])).toBe(true);
    });
    it('compares against codec + primitive types', () => {
      expect(vec.eq(['123', new _Text.default('456')])).toBe(true);
    });
    it('finds the index of an value', () => {
      const myId = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
      const vec = new _Vec.default(_AccountId.default, ['5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw', '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty', '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY']);
      expect(vec.indexOf(myId)).toEqual(2);
    });
  });
});