"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

require("./injector");

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

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

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

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

var exported = _interopRequireWildcard(require("./index.types"));

var definitions = _interopRequireWildcard(require("./interfaces/definitions"));

// 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.
// NOTE This is not a shortcut to implementing types incorrectly. This is here
// specifically for the types that _should_ throw in the constrtuctor, i.e
// `usize` is not allowed (runtime incompat) and `origin` is not passed through
// to any calls. All other types _must_ pass and allow for empty defaults
const UNCONSTRUCTABLE = ['ExtrinsicPayloadUnknown', 'GenericExtrinsicPayloadUnknown', 'ExtrinsicUnknown', 'GenericExtrinsicUnknown', 'GenericOrigin', 'Origin', 'usize'].map(v => v.toLowerCase());

function testTypes(type, typeNames) {
  describe(type, () => {
    describe("".concat(type, ":: default creation"), () => {
      typeNames.forEach(name => {
        it("creates an empty ".concat(name), () => {
          const constructFn = () => (0, _create.createTypeUnsafe)(name);

          if (UNCONSTRUCTABLE.includes(name.toLowerCase())) {
            expect(constructFn).toThrow();
          } else {
            expect(constructFn).not.toThrow();
          }
        });
      });
    });
    describe("".concat(type, ":: default creation (empty bytes)"), () => {
      _Call.default.injectMetadata(new _Metadata.default(_static.default));

      typeNames.forEach(name => {
        it("creates an empty ".concat(name, " (from empty bytes)"), () => {
          const constructFn = () => (0, _create.createTypeUnsafe)(name, [(0, _create.createTypeUnsafe)('Bytes')]);

          if (UNCONSTRUCTABLE.includes(name.toLowerCase())) {
            expect(constructFn).toThrow();
          } else {
            expect(constructFn).not.toThrow();
          }
        });
      });
    });
  });
}

describe('type creation', () => {
  testTypes('exported', Object.keys(exported));
  Object.entries(definitions).forEach((_ref) => {
    let [name, {
      types
    }] = _ref;
    return testTypes("".concat(name, " (injected)"), Object.keys(types));
  });
});