"use strict";
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const create_1 = require("../../../types/codec/create");
const StorageKey_1 = require("../../../types/primitive/StorageKey");
const getUniqTypes_1 = __importDefault(require("./getUniqTypes"));
const Metadata_1 = __importDefault(require("../Metadata"));
/** @internal */
function decodeLatestSubstrate(registry, version, rpcData, staticSubstrate) {
    it('decodes latest substrate properly', () => {
        const metadata = new Metadata_1.default(registry, rpcData);
        try {
            expect(metadata.version).toBe(version);
            expect(metadata[`asV${version}`].modules.length).not.toBe(0);
            expect(metadata.toJSON()).toEqual(staticSubstrate);
        }
        catch (error) {
            console.error(JSON.stringify(metadata.toJSON()));
            throw error;
        }
    });
}
exports.decodeLatestSubstrate = decodeLatestSubstrate;
/** @internal */
function toLatest(registry, version, rpcData) {
    it(`converts v${version} to latest`, () => {
        const metadata = new Metadata_1.default(registry, rpcData)[`asV${version}`];
        const metadataLatest = new Metadata_1.default(registry, rpcData).asLatest;
        expect(getUniqTypes_1.default(registry, metadata, true)).toEqual(getUniqTypes_1.default(registry, metadataLatest, true));
    });
}
exports.toLatest = toLatest;
/** @internal */
function defaultValues(registry, rpcData) {
    describe('storage with default values', () => {
        const metadata = new Metadata_1.default(registry, rpcData);
        metadata.asLatest.modules
            .filter(({ storage }) => storage.isSome)
            .forEach((mod) => {
            mod.storage.unwrap().items.forEach(({ fallback, name, type }) => {
                const inner = StorageKey_1.unwrapStorageType(type);
                const location = `${mod.name}.${name}: ${inner}`;
                it(`creates default types for ${location}`, () => {
                    expect(() => {
                        try {
                            return create_1.createTypeUnsafe(registry, inner, [fallback]);
                        }
                        catch (error) {
                            throw new Error(`${location}:: ${error.message}`);
                        }
                    }).not.toThrow();
                });
            });
        });
    });
}
exports.defaultValues = defaultValues;
