export type AfterSalesRecoveryAllocation = {
  pendingDepositDeductionAmount: number
  availableDeductionAmount: number
  debtAmount: number
}

export function resolveAfterSalesRecoveryAllocation(input: {
  recoveryAmount: number
  pendingDepositAmount: number
  availableAmount: number
}): AfterSalesRecoveryAllocation {
  let remaining = Math.max(0, Number(input.recoveryAmount) || 0)
  const pendingDepositDeductionAmount = Math.min(
    remaining,
    Math.max(0, Number(input.pendingDepositAmount) || 0),
  )
  remaining -= pendingDepositDeductionAmount
  const availableDeductionAmount = Math.min(
    remaining,
    Math.max(0, Number(input.availableAmount) || 0),
  )
  remaining -= availableDeductionAmount
  return {
    pendingDepositDeductionAmount,
    availableDeductionAmount,
    debtAmount: remaining,
  }
}
