"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 util_1 = require("@polkadot/util");
const createFunction_1 = __importDefault(require("./createFunction"));
const storage_1 = __importDefault(require("./storage"));
/** @internal */
function fromMetadata(registry, metadata) {
    return metadata.asLatest.modules.reduce((result, moduleMetadata) => {
        if (moduleMetadata.storage.isNone) {
            return result;
        }
        const { name } = moduleMetadata;
        const section = util_1.stringCamelCase(name.toString());
        const unwrapped = moduleMetadata.storage.unwrap();
        const prefix = unwrapped.prefix.toString();
        // For access, we change the index names, i.e. Balances.FreeBalance -> balances.freeBalance
        result[section] = unwrapped.items.reduce((newModule, meta) => {
            const method = meta.name.toString();
            newModule[util_1.stringLowerFirst(method)] = createFunction_1.default(registry, {
                meta,
                method,
                prefix,
                section
            }, { metaVersion: metadata.version });
            return newModule;
        }, {});
        return result;
    }, Object.assign({}, storage_1.default(registry, metadata.version)));
}
exports.default = fromMetadata;
