import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { useAccount } from "wagmi";
import { setShortTokenList, setSupportedChains } from "../../store/globalSlice";
import { useChains } from "./useChains";
import { useFetchTokens } from "./useTokens";

export const useFetchData = () => {
	const dispatch = useDispatch();
	const { address } = useAccount();

	const supportedChains = useChains();
	const shortTokenList = useFetchTokens({ short: true, address: address });

	useEffect(() => {
		if (supportedChains?.length) {
			dispatch(setSupportedChains(supportedChains));
		}
	}, [supportedChains, dispatch]);

	useEffect(() => {
		if (shortTokenList) {
			dispatch(setShortTokenList(shortTokenList));
		}
	}, [shortTokenList, dispatch]);

	return { supportedChains, shortTokenList };
};
