import { useAccount } from "wagmi";
import type { FormattedQuoteResponse } from "./apis/common/quote/types";
import { isAutoQuote } from "./apis/common/quote/useQuote";

export const useIsChainSwitchRequired = ({
	activeRoute,
}: { activeRoute?: FormattedQuoteResponse }) => {
	const { chainId, connector } = useAccount();
	if (!activeRoute) return false;
	/**
	 * @todo need to write tests
	 * Switch network for all txs
	 * - wrap/unwrap
	 * - native txs
	 * - manual txs
	 * - signing when wallet ain't supported
	 *
	 * Do not switch network when
	 * - sign & wallet supports (only for auto)
	 * */
	const supportedWallets = ["com.coinbase.wallet", "sh.frame"];
	const walletSupportsUniversalSigning = connector?.id
		? supportedWallets.includes(connector?.id)
		: false;
	const differentChainConnected =
		chainId !== activeRoute.primaryInput.token.chainId;

	if (isAutoQuote(activeRoute) && walletSupportsUniversalSigning) return false;
	return differentChainConnected;
};
