import { Temperature, TemperatureUnit } from '../../types';

/**
 * Convert Fahrenheit to Celsius
 */
export function fahrenheitToCelsius(f: number): number {
  return (f - 32) * (5 / 9);
}

/**
 * Convert Celsius to Fahrenheit
 */
export function celsiusToFahrenheit(c: number): number {
  return (c * 9 / 5) + 32;
}

/**
 * Convert temperature between units
 */
export function convertTemperature(temp: Temperature, toUnit: TemperatureUnit): Temperature {
  if (temp.unit === toUnit) {
    return temp;
  }

  const value = temp.unit === 'F'
    ? fahrenheitToCelsius(temp.value)
    : celsiusToFahrenheit(temp.value);

  return { value: Math.round(value), unit: toUnit };
}

/**
 * Format temperature for display
 */
export function formatTemperature(temp: Temperature): string {
  return `${temp.value}°${temp.unit}`;
}

/**
 * Convert pounds to kilograms
 */
export function poundsToKilograms(lb: number): number {
  return lb * 0.453592;
}

/**
 * Convert kilograms to pounds
 */
export function kilogramsToPounds(kg: number): number {
  return kg / 0.453592;
}

/**
 * Convert inches to centimeters
 */
export function inchesToCentimeters(inches: number): number {
  return inches * 2.54;
}

/**
 * Convert centimeters to inches
 */
export function centimetersToInches(cm: number): number {
  return cm / 2.54;
}
