"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.
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../../../types");
// migrate a storage hasher type
// see https://github.com/paritytech/substrate/pull/4462
/** @internal */
function createStorageHasher(registry, hasher) {
    // Blake2_128_Concat has been added at index 2, so we increment all the
    // indexes greater than 2
    if (hasher.toNumber() >= 2) {
        return types_1.createType(registry, 'StorageHasherV10', hasher.toNumber() + 1);
    }
    return types_1.createType(registry, 'StorageHasherV10', hasher);
}
/** @internal */
function createStorageType(registry, entryType) {
    if (entryType.isMap) {
        return [Object.assign(Object.assign({}, entryType.asMap), { hasher: createStorageHasher(registry, entryType.asMap.hasher) }), 1];
    }
    if (entryType.isDoubleMap) {
        return [Object.assign(Object.assign({}, entryType.asDoubleMap), { hasher: createStorageHasher(registry, entryType.asDoubleMap.hasher), key2Hasher: createStorageHasher(registry, entryType.asDoubleMap.key2Hasher) }), 2];
    }
    return [entryType.asPlain, 0];
}
/** @internal */
function convertModule(registry, mod) {
    const storage = mod.storage.unwrapOr(null);
    return types_1.createType(registry, 'ModuleMetadataV10', Object.assign(Object.assign({}, mod), { storage: storage
            ? Object.assign(Object.assign({}, storage), { items: storage.items.map((item) => (Object.assign(Object.assign({}, item), { type: types_1.createType(registry, 'StorageEntryTypeV10', ...createStorageType(registry, item.type)) }))) }) : null }));
}
/** @internal */
function toV10(registry, { modules }) {
    return types_1.createType(registry, 'MetadataV10', {
        modules: modules.map((mod) => convertModule(registry, mod))
    });
}
exports.default = toV10;
