| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import type { VehicleType } from '../types/user'; |
| import { VEHICLE_TYPES, FARE_ESTIMATE_BUFFER } from './constants'; |
|
|
| export interface FareEstimate { |
| min: number; |
| max: number; |
| baseFare: number; |
| perKmRate: number; |
| total: number; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function estimateFare( |
| distanceKm: number, |
| vehicleType: VehicleType, |
| ): FareEstimate { |
| const vehicle = VEHICLE_TYPES.find((v) => v.type === vehicleType); |
|
|
| if (!vehicle) { |
| throw new Error(`Unknown vehicle type: ${vehicleType}`); |
| } |
|
|
| if (distanceKm <= 0) { |
| return { |
| min: vehicle.baseFare, |
| max: vehicle.baseFare, |
| baseFare: vehicle.baseFare, |
| perKmRate: vehicle.perKmRate, |
| total: vehicle.baseFare, |
| }; |
| } |
|
|
| const total = vehicle.baseFare + vehicle.perKmRate * distanceKm; |
| const buffer = total * FARE_ESTIMATE_BUFFER; |
|
|
| return { |
| min: Math.round(total - buffer), |
| max: Math.round(total + buffer), |
| baseFare: vehicle.baseFare, |
| perKmRate: vehicle.perKmRate, |
| total: Math.round(total), |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function farePerPerson(totalFare: number, passengers: number): number { |
| const count = Math.max(1, passengers); |
| return Math.ceil(totalFare / count); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function formatFare(amount: number): string { |
| return `₹${Math.round(amount)}`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function formatFareRange(min: number, max: number): string { |
| return `${formatFare(min)} - ${formatFare(max)}`; |
| } |
|
|