import { useAuth } from "react-oidc-js";
import { useGetWalletV1PrivateUserassetSpotDefaultAll } from "../../services";
import { selectedSymbolStore } from "../../store";
import { useDisplayDecimalDigits } from "../useDisplayDecimalDigits";

const useUserSelectedUserAssets = () => {
  const { selectedSymbol } = selectedSymbolStore.useState();
  const { userData } = useAuth();
  const { truncFix, truncFixToCurrency } = useDisplayDecimalDigits();

  const { data: userAssets } = useGetWalletV1PrivateUserassetSpotDefaultAll({
    cacheTime: Infinity,
    staleTime: Infinity,
    enabled: !!userData?.access_token,
  });

  const userQuoteAsset = userAssets?.find(
    (asset) => asset.symbol === selectedSymbol?.quoteAsset,
  );

  const userBaseAsset = userAssets?.find(
    (asset) => asset.symbol === selectedSymbol?.baseAsset,
  );

  const quoteAvailableRemain = truncFix(
    userQuoteAsset?.availableRemain || 0,
    userQuoteAsset?.symbol,
  );
  const baseAvailableRemain = truncFix(
    userBaseAsset?.availableRemain || 0,
    userBaseAsset?.symbol,
  );

  const quoteDisplayAvailableRemain = truncFixToCurrency(
    userQuoteAsset?.availableRemain || 0,
    userQuoteAsset?.symbol,
  );
  const baseDisplayAvailableRemain = truncFixToCurrency(
    userBaseAsset?.availableRemain || 0,
    userBaseAsset?.symbol,
  );

  return {
    quoteAvailableRemain,
    baseAvailableRemain,
    quoteDisplayAvailableRemain,
    baseDisplayAvailableRemain,
  };
};

export { useUserSelectedUserAssets };
