// Generated by Ignite ignite.com/cli
import { SigningStargateClient } from "@cosmjs/stargate";
import { Registry } from "@cosmjs/proto-signing";
import { msgTypes } from './registry';
import { Api } from "./rest";
import { MsgGrant } from "./types/cosmos/authz/v1beta1/tx";
import { MsgRevoke } from "./types/cosmos/authz/v1beta1/tx";
import { MsgExec } from "./types/cosmos/authz/v1beta1/tx";
export { MsgGrant, MsgRevoke, MsgExec };
export const registry = new Registry(msgTypes);
const defaultFee = {
    amount: [],
    gas: "200000",
};
export const txClient = ({ signer, prefix, addr } = { addr: "http://localhost:26657", prefix: "cosmos" }) => {
    return {
        async sendMsgGrant({ value, fee, memo }) {
            if (!signer) {
                throw new Error('TxClient:sendMsgGrant: Unable to sign Tx. Signer is not present.');
            }
            try {
                const { address } = (await signer.getAccounts())[0];
                const signingClient = await SigningStargateClient.connectWithSigner(addr, signer, { registry, prefix });
                let msg = this.msgGrant({ value: MsgGrant.fromPartial(value) });
                return await signingClient.signAndBroadcast(address, [msg], fee ? fee : defaultFee, memo);
            }
            catch (e) {
                throw new Error('TxClient:sendMsgGrant: Could not broadcast Tx: ' + e.message);
            }
        },
        async sendMsgRevoke({ value, fee, memo }) {
            if (!signer) {
                throw new Error('TxClient:sendMsgRevoke: Unable to sign Tx. Signer is not present.');
            }
            try {
                const { address } = (await signer.getAccounts())[0];
                const signingClient = await SigningStargateClient.connectWithSigner(addr, signer, { registry, prefix });
                let msg = this.msgRevoke({ value: MsgRevoke.fromPartial(value) });
                return await signingClient.signAndBroadcast(address, [msg], fee ? fee : defaultFee, memo);
            }
            catch (e) {
                throw new Error('TxClient:sendMsgRevoke: Could not broadcast Tx: ' + e.message);
            }
        },
        async sendMsgExec({ value, fee, memo }) {
            if (!signer) {
                throw new Error('TxClient:sendMsgExec: Unable to sign Tx. Signer is not present.');
            }
            try {
                const { address } = (await signer.getAccounts())[0];
                const signingClient = await SigningStargateClient.connectWithSigner(addr, signer, { registry, prefix });
                let msg = this.msgExec({ value: MsgExec.fromPartial(value) });
                return await signingClient.signAndBroadcast(address, [msg], fee ? fee : defaultFee, memo);
            }
            catch (e) {
                throw new Error('TxClient:sendMsgExec: Could not broadcast Tx: ' + e.message);
            }
        },
        msgGrant({ value }) {
            try {
                return { typeUrl: "/cosmos.authz.v1beta1.MsgGrant", value: MsgGrant.fromPartial(value) };
            }
            catch (e) {
                throw new Error('TxClient:MsgGrant: Could not create message: ' + e.message);
            }
        },
        msgRevoke({ value }) {
            try {
                return { typeUrl: "/cosmos.authz.v1beta1.MsgRevoke", value: MsgRevoke.fromPartial(value) };
            }
            catch (e) {
                throw new Error('TxClient:MsgRevoke: Could not create message: ' + e.message);
            }
        },
        msgExec({ value }) {
            try {
                return { typeUrl: "/cosmos.authz.v1beta1.MsgExec", value: MsgExec.fromPartial(value) };
            }
            catch (e) {
                throw new Error('TxClient:MsgExec: Could not create message: ' + e.message);
            }
        },
    };
};
export const queryClient = ({ addr: addr } = { addr: "http://localhost:1317" }) => {
    return new Api({ baseURL: addr });
};
class SDKModule {
    query;
    tx;
    registry = [];
    constructor(client) {
        this.query = queryClient({ addr: client.env.apiURL });
        this.updateTX(client);
        client.on('signer-changed', (signer) => {
            this.updateTX(client);
        });
    }
    updateTX(client) {
        const methods = txClient({
            signer: client.signer,
            addr: client.env.rpcURL,
            prefix: client.env.prefix ?? "cosmos",
        });
        this.tx = methods;
        for (let m in methods) {
            this.tx[m] = methods[m].bind(this.tx);
        }
    }
}
;
const Module = (test) => {
    return {
        module: {
            CosmosAuthzV1Beta1: new SDKModule(test)
        },
        registry: msgTypes
    };
};
export default Module;
//# sourceMappingURL=module.js.map