"use strict";
// Copyright 2017-2020 @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.
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("@polkadot/util");
const overrides_1 = require("./overrides");
// flatten a VersionedType[] into a Record<string, string>
/** @internal */
function filterVersions(versions = [], version) {
    return versions
        .filter(({ minmax: [min, max] }) => (util_1.isUndefined(min) || version >= min) &&
        (util_1.isUndefined(max) || version <= max))
        .reduce((result, { types }) => (Object.assign(Object.assign({}, result), types)), {});
}
// based on the metadata version, return the registry types
/** @internal */
function getMetadataTypes(version) {
    return filterVersions(overrides_1.TYPES_META, version);
}
exports.getMetadataTypes = getMetadataTypes;
// based on the chain and runtimeVersion, get the applicable types (ready for registration)
/** @internal */
function getChainTypes(chainName, { specName, specVersion }, typesChain = {}, typesSpec = {}) {
    const _chainName = chainName.toString();
    const _specName = specName.toString();
    const _specVersion = specVersion.toNumber();
    return Object.assign(Object.assign(Object.assign(Object.assign({}, filterVersions(overrides_1.TYPES_SPEC[_specName], _specVersion)), filterVersions(overrides_1.TYPES_CHAIN[_chainName], _specVersion)), (typesSpec[_specName] || {})), (typesChain[_chainName] || {}));
}
exports.getChainTypes = getChainTypes;
// get types for specific modules (metadata override)
/** @internal */
function getModuleTypes(section) {
    return overrides_1.TYPES_MODULES[section] || {};
}
exports.getModuleTypes = getModuleTypes;
