/**
* This file and any referenced files were automatically generated by @hyperweb/telescope@1.17.4
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
import { getRpcClient } from "./extern";
import { isRpc } from "./helpers";
import { toConverters, toEncoders } from "@interchainjs/cosmos";
export function buildQuery(opts) {
    registerDependencies(opts.deps ?? []);
    return async (client, request) => {
        let rpc;
        if (isRpc(client)) {
            rpc = client;
        }
        else {
            rpc = client ? await getRpcClient(client) : undefined;
        }
        if (!rpc)
            throw new Error("Query Rpc is not initialized");
        const data = opts.encode(request).finish();
        const response = await rpc.request(opts.service, opts.method, data);
        return opts.decode(response);
    };
}
export function buildTx(opts) {
    if (opts.msg) {
        registerDependencies([opts.msg]);
    }
    return async (client, signerAddress, message, fee, memo) => {
        if (!client)
            throw new Error("SigningClient is not initialized");
        //register all related encoders and converters
        client.addEncoders?.(toEncoders(opts.msg));
        client.addConverters?.(toConverters(opts.msg));
        const data = Array.isArray(message)
            ? message.map(msg => ({
                typeUrl: opts.msg.typeUrl,
                value: msg,
            }))
            : [{
                    typeUrl: opts.msg.typeUrl,
                    value: message,
                }];
        return client.signAndBroadcast(signerAddress, data, fee, memo);
    };
}
function registerDependencies(deps) {
    for (const dep of deps) {
        dep.registerTypeUrl?.();
    }
}
