import { formatEther, parseEther } from '@ethersproject/units'

export const formatUsdPrice = (price: number) => {
  if (price > 1000000) {
    return `$${(price / 1000000).toFixed(1)}M`
  } else if (price > 1000) {
    return `$${(price / 1000).toFixed(1)}K`
  } else {
    return `$${price.toFixed(2)}`
  }
}

export const formatUSDPriceWithCommas = (price: number) => {
  return `$${Math.round(price)
    .toString()
    .replace(/\B(?=(\d{3})+(?!\d))/g, ',')}`
}

export const formatEthPrice = (price: string) => {
  if (!price) return 0

  const formattedPrice = parseFloat(formatEther(String(price)))
  return (
    Math.round(formattedPrice * (formattedPrice >= 1 ? 100 : 1000) + Number.EPSILON) /
    (formattedPrice >= 1 ? 100 : 1000)
  )
}

// Stringify the `price` anyway because the `price` is being passed as any in some places
export const numberToWei = (amount: number) => {
  return parseEther(amount.toString())
}

export const ethNumberStandardFormatter = (amount: string | number | undefined, includeDollarSign = false): string => {
  if (!amount) return '-'

  const amountInDecimals = parseFloat(amount.toString())
  const conditionalDollarSign = includeDollarSign ? '$' : ''

  if (amountInDecimals < 0.0001) return `< ${conditionalDollarSign}0.00001`
  if (amountInDecimals < 1) return `${conditionalDollarSign}${amountInDecimals.toFixed(3)}`
  return (
    conditionalDollarSign +
    amountInDecimals
      .toFixed(2)
      .toString()
      .replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  )
}

export const formatWeiToDecimal = (amount: string) => {
  if (!amount) return '-'
  return ethNumberStandardFormatter(formatEther(amount))
}
