import { useEffect, useMemo, useState } from "react";
import { useTypedSelector } from "redux/rootReducer";
import {
  selectExchangeRates,
  selectExchangeRatesUSD,
} from "redux/User/selector";
import { formatterCurrencyEXRate } from "utils/currency";

const MINIMUM_PRICE_USD = 1; // minium is 1USD

const useMiniumPrice = () => {
  const exchangeRate = useTypedSelector(selectExchangeRates);
  const exchangeRateUSD = useTypedSelector(selectExchangeRatesUSD);
  const [minimumPrice, setMinimumPrice] = useState(0);

  useEffect(() => {
    if (!exchangeRate || !exchangeRateUSD) return;
    setMinimumPrice(
      Number(MINIMUM_PRICE_USD) *
        (Number(exchangeRate?.GBPExchangeRate) /
          Number(exchangeRateUSD?.GBPExchangeRate))
    );
  }, [exchangeRate, exchangeRateUSD]);

  const minimumPriceFormat = useMemo(
    () => formatterCurrencyEXRate(exchangeRate).format(minimumPrice),
    [minimumPrice, exchangeRate]
  );

  return [minimumPrice, minimumPriceFormat];
};

export default useMiniumPrice;
