import { removeTrailingZeros } from './removeTrailingZeros'
import type { BigDecimal } from './type'

export const plus = (x: BigDecimal, y: BigDecimal): BigDecimal => {
  // adjust the decimals and add the values.
  const decimals = Math.max(x.decimals, y.decimals)
  const valueX = x.value * 10n ** BigInt(decimals - x.decimals)
  const valueY = y.value * 10n ** BigInt(decimals - y.decimals)
  const value = valueX + valueY

  return removeTrailingZeros({ value, decimals })
}
