/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.11.19
* 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 { isISigningClient } from './helper-func-types';
import { useQuery, useQueryClient, useMutation, } from '@tanstack/vue-query';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
export const DEFAULT_RPC_CLIENT_QUERY_KEY = 'rpcClient';
export const DEFAULT_RPC_ENDPOINT_QUERY_KEY = 'rpcEndPoint';
export const DEFAULT_SIGNING_CLIENT_QUERY_KEY = 'signingClient';
export function isCacheResolver(resolver) {
    return (resolver !== null &&
        resolver !== undefined &&
        resolver.rpcEndpoint !== undefined &&
        resolver.clientQueryKey !== undefined);
}
export function useRpcEndpoint({ getter, options, rpcEndPointKey, extraKey, }) {
    const key = rpcEndPointKey || DEFAULT_RPC_ENDPOINT_QUERY_KEY;
    return useQuery({
        queryKey: [key, extraKey],
        queryFn: async () => {
            return await getter();
        }, ...options
    });
}
;
export function useRpcClient({ options, clientResolver, }) {
    const queryClient = useQueryClient();
    const key = clientResolver?.clientQueryKey || DEFAULT_RPC_CLIENT_QUERY_KEY;
    return useQuery({
        queryKey: [key, clientResolver?.rpcEndpoint],
        queryFn: async () => {
            if (!clientResolver?.rpcEndpoint) {
                throw new Error('rpcEndpoint is required');
            }
            const client = await getRpcClient(clientResolver.rpcEndpoint);
            if (!client) {
                throw new Error('Failed to connect to rpc client');
            }
            queryClient.setQueryData([key], client);
            return client;
        },
        ...options
    });
}
/**
 * Function that uses vue-query to cache a connected tendermint client.
 */
export const useTendermintClient = ({ rpcEndpoint, options, }) => {
    const { data: client } = useQuery({
        queryKey: ['client', 'tendermint', rpcEndpoint],
        queryFn: () => Tendermint34Client.connect(rpcEndpoint),
        ...{
            // allow overriding
            onError: (e) => {
                throw new Error(`Failed to connect to ${rpcEndpoint}` + '\n' + e);
            },
            ...options,
        }
    });
    return { client };
};
export function buildUseVueQuery(opts) {
    return function useBuiltQuery({ request, options, clientResolver, customizedQueryKey, }) {
        const queryClient = useQueryClient();
        let rpcResolver;
        if (isRpc(clientResolver)) {
            rpcResolver = clientResolver;
        }
        else if (isCacheResolver(clientResolver)) {
            const key = clientResolver.clientQueryKey || DEFAULT_RPC_CLIENT_QUERY_KEY;
            const queryKey = clientResolver.rpcEndpoint
                ? [key, clientResolver.rpcEndpoint]
                : [key];
            rpcResolver = queryClient.getQueryData(queryKey);
            if (!rpcResolver && clientResolver.rpcEndpoint) {
                rpcResolver = clientResolver.rpcEndpoint;
            }
        }
        else {
            rpcResolver = clientResolver;
        }
        const queryFn = opts.builderQueryFn(rpcResolver);
        return useQuery({
            queryKey: customizedQueryKey || [opts.queryKeyPrefix, request],
            queryFn: () => queryFn(request.value),
            ...options
        });
    };
}
export function buildUseVueMutation(opts) {
    return function useBuiltMutation({ options, clientResolver, }) {
        const queryClient = useQueryClient();
        let signingClientResolver;
        if (isISigningClient(clientResolver)) {
            signingClientResolver = clientResolver;
        }
        else if (isCacheResolver(clientResolver)) {
            const key = clientResolver.clientQueryKey || DEFAULT_SIGNING_CLIENT_QUERY_KEY;
            const queryKey = clientResolver.rpcEndpoint
                ? [key, clientResolver.rpcEndpoint]
                : [key];
            signingClientResolver = queryClient.getQueryData(queryKey);
        }
        else {
            clientResolver = clientResolver;
        }
        const mutationFn = opts.builderMutationFn(signingClientResolver);
        return useMutation({
            mutationFn: (reqData) => mutationFn(reqData.signerAddress, reqData.message, reqData.fee, reqData.memo),
            ...options
        });
    };
}
