"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildUseVueMutation = exports.buildUseVueQuery = exports.useTendermintClient = exports.useRpcClient = exports.useRpcEndpoint = exports.isCacheResolver = exports.DEFAULT_SIGNING_CLIENT_QUERY_KEY = exports.DEFAULT_RPC_ENDPOINT_QUERY_KEY = exports.DEFAULT_RPC_CLIENT_QUERY_KEY = void 0;
const extern_1 = require("./extern");
const helpers_1 = require("./helpers");
const helper_func_types_1 = require("./helper-func-types");
const vue_query_1 = require("@tanstack/vue-query");
const tendermint_rpc_1 = require("@cosmjs/tendermint-rpc");
exports.DEFAULT_RPC_CLIENT_QUERY_KEY = 'rpcClient';
exports.DEFAULT_RPC_ENDPOINT_QUERY_KEY = 'rpcEndPoint';
exports.DEFAULT_SIGNING_CLIENT_QUERY_KEY = 'signingClient';
function isCacheResolver(resolver) {
    return (resolver !== null &&
        resolver !== undefined &&
        resolver.rpcEndpoint !== undefined &&
        resolver.clientQueryKey !== undefined);
}
exports.isCacheResolver = isCacheResolver;
function useRpcEndpoint({ getter, options, rpcEndPointKey, extraKey, }) {
    const key = rpcEndPointKey || exports.DEFAULT_RPC_ENDPOINT_QUERY_KEY;
    return (0, vue_query_1.useQuery)({
        queryKey: [key, extraKey],
        queryFn: async () => {
            return await getter();
        }, ...options
    });
}
exports.useRpcEndpoint = useRpcEndpoint;
;
function useRpcClient({ options, clientResolver, }) {
    const queryClient = (0, vue_query_1.useQueryClient)();
    const key = clientResolver?.clientQueryKey || exports.DEFAULT_RPC_CLIENT_QUERY_KEY;
    return (0, vue_query_1.useQuery)({
        queryKey: [key, clientResolver?.rpcEndpoint],
        queryFn: async () => {
            if (!clientResolver?.rpcEndpoint) {
                throw new Error('rpcEndpoint is required');
            }
            const client = await (0, extern_1.getRpcClient)(clientResolver.rpcEndpoint);
            if (!client) {
                throw new Error('Failed to connect to rpc client');
            }
            queryClient.setQueryData([key], client);
            return client;
        },
        ...options
    });
}
exports.useRpcClient = useRpcClient;
/**
 * Function that uses vue-query to cache a connected tendermint client.
 */
const useTendermintClient = ({ rpcEndpoint, options, }) => {
    const { data: client } = (0, vue_query_1.useQuery)({
        queryKey: ['client', 'tendermint', rpcEndpoint],
        queryFn: () => tendermint_rpc_1.Tendermint34Client.connect(rpcEndpoint),
        ...{
            // allow overriding
            onError: (e) => {
                throw new Error(`Failed to connect to ${rpcEndpoint}` + '\n' + e);
            },
            ...options,
        }
    });
    return { client };
};
exports.useTendermintClient = useTendermintClient;
function buildUseVueQuery(opts) {
    return function useBuiltQuery({ request, options, clientResolver, customizedQueryKey, }) {
        const queryClient = (0, vue_query_1.useQueryClient)();
        let rpcResolver;
        if ((0, helpers_1.isRpc)(clientResolver)) {
            rpcResolver = clientResolver;
        }
        else if (isCacheResolver(clientResolver)) {
            const key = clientResolver.clientQueryKey || exports.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 (0, vue_query_1.useQuery)({
            queryKey: customizedQueryKey || [opts.queryKeyPrefix, request],
            queryFn: () => queryFn(request.value),
            ...options
        });
    };
}
exports.buildUseVueQuery = buildUseVueQuery;
function buildUseVueMutation(opts) {
    return function useBuiltMutation({ options, clientResolver, }) {
        const queryClient = (0, vue_query_1.useQueryClient)();
        let signingClientResolver;
        if ((0, helper_func_types_1.isISigningClient)(clientResolver)) {
            signingClientResolver = clientResolver;
        }
        else if (isCacheResolver(clientResolver)) {
            const key = clientResolver.clientQueryKey || exports.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 (0, vue_query_1.useMutation)({
            mutationFn: (reqData) => mutationFn(reqData.signerAddress, reqData.message, reqData.fee, reqData.memo),
            ...options
        });
    };
}
exports.buildUseVueMutation = buildUseVueMutation;
