import { queryClient } from '../commands/_app.js';
import { queryMappingChainById } from './chainById.js';
import { queryChainConfig } from './chainConfig.js';
import { CHAIN_LIST_URL } from '../superchain-registry/fetchChainList.js';
import { useQuery } from '@tanstack/react-query';
const getQueryParams = (chainListURL) => {
    return {
        queryKey: ['chainByIdentifier', chainListURL],
        queryFn: async () => {
            const chainList = await queryChainConfig(chainListURL);
            const chainById = await queryMappingChainById(chainListURL);
            return chainList.reduce((acc, config) => {
                acc[config.identifier] = chainById[config.chainId];
                return acc;
            }, {});
        },
    };
};
export const queryMappingChainByIdentifier = async (chainListURL = CHAIN_LIST_URL) => {
    return queryClient.fetchQuery(getQueryParams(chainListURL));
};
export const useMappingChainByIdentifier = (chainListURL = CHAIN_LIST_URL) => {
    return useQuery({
        ...getQueryParams(chainListURL),
        staleTime: Infinity, // For the duration of the CLI session, this is cached
    });
};
