"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 __1 = __importDefault(require("../"));
const createUnchecked_1 = __importDefault(require("./createUnchecked"));
/** @internal */
function fromMetadata(registry, metadata) {
    return metadata.asLatest.modules
        .filter(({ calls }) => calls.isSome)
        .reduce((result, { calls, name }, sectionIndex) => {
        const section = util_1.stringCamelCase(name.toString());
        result[section] = calls.unwrap().reduce((newModule, callMetadata, methodIndex) => {
            const method = util_1.stringCamelCase(callMetadata.name.toString());
            newModule[method] = createUnchecked_1.default(registry, section, sectionIndex, methodIndex, callMetadata);
            return newModule;
        }, {});
        return result;
    }, Object.assign({}, __1.default));
}
exports.default = fromMetadata;
