"use strict";
// Copyright 2017-2020 @polkadot/api 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 Abi_1 = __importDefault(require("../Abi"));
class Base {
    constructor(api, abi, decorateMethod) {
        this.abi = abi instanceof Abi_1.default
            ? abi
            : new Abi_1.default(api.registry, abi);
        this.registry = api.registry;
        this.api = api;
        this.decorateMethod = decorateMethod;
    }
    get messages() {
        return this.abi.abi.contract.messages
            .map((def, index) => ({
            index,
            def,
            fn: this.abi.messages[def.name] || this.abi.messages[util_1.stringCamelCase(name)]
        }));
    }
    getMessage(nameOrIndex) {
        const index = nameOrIndex
            ? typeof nameOrIndex === 'number'
                ? nameOrIndex
                : this.abi.abi.contract.messages.findIndex((message) => nameOrIndex === message.name || nameOrIndex === util_1.stringCamelCase(message.name))
            : 0;
        const def = this.abi.abi.contract.messages[index];
        util_1.assert(!!def, `Attempted to access a contract message that does not exist: ${typeof nameOrIndex === 'number' ? `index ${nameOrIndex}` : nameOrIndex}`);
        const fn = this.abi.messages[def.name] || this.abi.messages[util_1.stringCamelCase(def.name)];
        return { index, def, fn };
    }
}
exports.Base = Base;
class BaseWithTx extends Base {
    get apiContracts() {
        return this.api.rx.tx.contracts;
    }
    constructor(api, abi, decorateMethod) {
        super(api, abi, decorateMethod);
        util_1.assert(this.api.rx.tx.contracts && this.api.rx.tx.contracts.putCode, 'You need to connect to a node with the contracts module, the metadata does not enable api.tx.contracts on this instance');
    }
}
exports.BaseWithTx = BaseWithTx;
class BaseWithTxAndRpcCall extends BaseWithTx {
    get hasRpcContractsCall() {
        var _a;
        return util_1.isFunction((_a = this.api.rx.rpc.contracts) === null || _a === void 0 ? void 0 : _a.call);
    }
    get rpcContractsCall() {
        util_1.assert(this.hasRpcContractsCall, 'You need to connect to a node with the contracts.call RPC method.');
        return this.api.rx.rpc.contracts.call;
    }
}
exports.BaseWithTxAndRpcCall = BaseWithTxAndRpcCall;
