import { UnitSystem as CoreUnitSystem } from '../../../shared-minimal/utils/units';

export type UnitSystem = CoreUnitSystem;

export class UnitConversionService {
  private static readonly KM_PER_MILE = 1.60934;
  private static readonly GALLONS_TO_LITERS = 3.78541;

  static getUnitLabels(unitSystem: UnitSystem) {
    return unitSystem === 'metric'
      ? { fuelUnits: 'liters', distanceUnits: 'kilometers', efficiencyUnits: 'km/L' }
      : { fuelUnits: 'gallons', distanceUnits: 'miles', efficiencyUnits: 'MPG' };
  }

  static calculateEfficiency(distance: number, fuelUnits: number): number {
    if (fuelUnits <= 0 || distance <= 0) return 0;
    return distance / fuelUnits; // Both km/L and MPG use distance/fuel formula
  }

  static convertEfficiency(efficiency: number, from: UnitSystem, to: UnitSystem): number {
    if (from === to) return efficiency;
    if (from === 'imperial' && to === 'metric') {
      // MPG to km/L: MPG * (gallons_to_liters / km_per_mile)
      return efficiency * (this.GALLONS_TO_LITERS / this.KM_PER_MILE);
    }
    if (from === 'metric' && to === 'imperial') {
      // km/L to MPG: km/L * (km_per_mile / gallons_to_liters)
      return efficiency * (this.KM_PER_MILE / this.GALLONS_TO_LITERS);
    }
    return efficiency;
  }
}

