"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 });
// we do a direct import here to remove all circular dependencies (importing from
// /types, /types/codec or /types/codec/create makes all hell break loose)
const createType_1 = require("../../../../types/codec/create/createType");
const util_1 = require("@polkadot/util");
/** @internal */
function createDescriptor(registry, section, sectionIndex, methodIndex, callMetadata) {
    const callIndex = new Uint8Array([sectionIndex, methodIndex]);
    const expectedArgs = callMetadata.args;
    const funcName = util_1.stringCamelCase(callMetadata.name.toString());
    const extrinsicFn = (...args) => {
        util_1.assert(expectedArgs.length.valueOf() === args.length, `Extrinsic ${section}.${funcName} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`);
        return createType_1.createType(registry, 'Call', {
            args,
            callIndex
        }, callMetadata);
    };
    extrinsicFn.callIndex = callIndex;
    extrinsicFn.meta = callMetadata;
    extrinsicFn.method = funcName;
    extrinsicFn.section = section;
    extrinsicFn.toJSON = () => callMetadata.toJSON();
    return extrinsicFn;
}
exports.default = createDescriptor;
