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

export const times = (
  x: BigDecimal,
  y: BigDecimal,
  fixDecimals?: number,
  rounding: 'round' | 'floor' | 'ceil' = 'round',
): BigDecimal => {
  // adjust the decimals and multiply the values.
  const decimals = x.decimals + y.decimals
  const value = x.value * y.value

  if (fixDecimals === undefined) {
    // remove trailing zeros.
    return removeTrailingZeros({ value, decimals })
  }

  return fix({ value, decimals }, fixDecimals, rounding)
}
