"use strict";
/**
* This file and any referenced files were automatically generated by @hyperweb/telescope@1.17.0
* 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.DEFAULT_SIGNING_CLIENT_QUERY_KEY = exports.DEFAULT_RPC_ENDPOINT_QUERY_KEY = exports.DEFAULT_RPC_CLIENT_QUERY_KEY = void 0;
exports.isCacheResolver = isCacheResolver;
exports.useRpcEndpoint = useRpcEndpoint;
exports.useRpcClient = useRpcClient;
exports.buildUseVueQuery = buildUseVueQuery;
exports.buildUseVueMutation = buildUseVueMutation;
const extern_1 = require("./extern");
const helpers_1 = require("./helpers");
const signing_client_1 = require("@interchainjs/cosmos/types/signing-client");
const vue_query_1 = require("@tanstack/vue-query");
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);
}
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
    });
}
;
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
    });
}
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;
        }
        return (0, vue_query_1.useQuery)({
            queryKey: customizedQueryKey || [opts.queryKeyPrefix, request],
            queryFn: () => opts.builderQueryFn(rpcResolver, request.value),
            ...options
        });
    };
}
function buildUseVueMutation(opts) {
    return function useBuiltMutation({ options, clientResolver, }) {
        const queryClient = (0, vue_query_1.useQueryClient)();
        let signingClientResolver;
        if ((0, signing_client_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;
        }
        return (0, vue_query_1.useMutation)({
            mutationFn: (reqData) => opts.builderMutationFn(signingClientResolver, reqData.signerAddress, reqData.message, reqData.fee, reqData.memo),
            ...options
        });
    };
}
