export type MeasurementSystem = 'metric' | 'imperial'; export interface UnitPreferences { measurement_system: MeasurementSystem; temperature: 'celsius' | 'fahrenheit'; auto_detect: boolean; } // Get unit labels (helper functions - use getDisplayUnit for conversion logic) export function getSpeedUnit(system: MeasurementSystem): string { return system === 'imperial' ? 'mph' : 'km/h'; } export function getDistanceUnit(system: MeasurementSystem): string { return system === 'imperial' ? 'ft' : 'm'; } export function getHeightUnit(system: MeasurementSystem): string { return system === 'imperial' ? 'in' : 'cm'; } export function getShortDistanceUnit(system: MeasurementSystem): string { return system === 'imperial' ? 'ft' : 'm'; } // Countries that primarily use imperial system export const IMPERIAL_COUNTRIES = ['US', 'LR', 'MM']; // Detect measurement system from country code export function getMeasurementSystemFromCountry(countryCode: string): MeasurementSystem { return IMPERIAL_COUNTRIES.includes(countryCode.toUpperCase()) ? 'imperial' : 'metric'; } // Unit type detection export type UnitType = 'speed' | 'distance' | 'height' | 'weight' | 'unknown'; export function detectUnitType(unit: string): UnitType { const normalized = unit.toLowerCase().trim(); // Speed units if (['km/h', 'kmh', 'kph', 'mph', 'm/s', 'ms'].includes(normalized)) { return 'speed'; } // Distance units (meters/feet) if (['m', 'meter', 'meters', 'metre', 'metres', 'ft', 'feet', 'foot'].includes(normalized)) { return 'distance'; } // Height units (cm/inches) if (['cm', 'centimeter', 'centimeters', 'in', 'inch', 'inches'].includes(normalized)) { return 'height'; } // Weight units if (['kg', 'kilogram', 'kilograms', 'lb', 'lbs', 'pound', 'pounds'].includes(normalized)) { return 'weight'; } return 'unknown'; } // Convert any value to metric based on its unit export function convertValueToMetric(value: number, unit: string): number { const normalized = unit.toLowerCase().trim(); // Speed conversions to km/h if (normalized === 'mph') { return Math.round(value / 0.621371); } if (['m/s', 'ms'].includes(normalized)) { return Math.round(value * 3.6); } if (['km/h', 'kmh', 'kph'].includes(normalized)) { return Math.round(value); } // Distance conversions to meters if (['ft', 'feet', 'foot'].includes(normalized)) { return Math.round(value / 3.28084); } if (['m', 'meter', 'meters', 'metre', 'metres'].includes(normalized)) { return Math.round(value); } // Height conversions to cm if (['in', 'inch', 'inches'].includes(normalized)) { return Math.round(value / 0.393701); } if (['cm', 'centimeter', 'centimeters'].includes(normalized)) { return Math.round(value); } // Weight conversions to kg if (['lb', 'lbs', 'pound', 'pounds'].includes(normalized)) { return Math.round(value / 2.20462); } if (['kg', 'kilogram', 'kilograms'].includes(normalized)) { return Math.round(value); } // Unknown unit, return as-is return value; } // Convert metric value to target unit export function convertValueFromMetric(value: number, targetUnit: string, metricUnit: string): number { const normalized = targetUnit.toLowerCase().trim(); const metricNormalized = metricUnit.toLowerCase().trim(); // Speed conversions from km/h if (metricNormalized === 'km/h' || metricNormalized === 'kmh' || metricNormalized === 'kph') { if (normalized === 'mph') { return Math.round(value * 0.621371); } if (normalized === 'm/s' || normalized === 'ms') { return Math.round(value / 3.6); } } // Distance conversions from meters if (metricNormalized === 'm' || metricNormalized === 'meter' || metricNormalized === 'meters') { if (['ft', 'feet', 'foot'].includes(normalized)) { return Math.round(value * 3.28084); } } // Height conversions from cm if (metricNormalized === 'cm' || metricNormalized === 'centimeter' || metricNormalized === 'centimeters') { if (['in', 'inch', 'inches'].includes(normalized)) { return Math.round(value * 0.393701); } } // Weight conversions from kg if (metricNormalized === 'kg' || metricNormalized === 'kilogram' || metricNormalized === 'kilograms') { if (['lb', 'lbs', 'pound', 'pounds'].includes(normalized)) { return Math.round(value * 2.20462); } } return value; } // Get metric unit for a given unit type export function getMetricUnit(unit: string): string { const unitType = detectUnitType(unit); switch (unitType) { case 'speed': return 'km/h'; case 'distance': return 'm'; case 'height': return 'cm'; case 'weight': return 'kg'; default: return unit; } } // Get display unit based on unit type and measurement system export function getDisplayUnit(metricUnit: string, system: MeasurementSystem): string { const unitType = detectUnitType(metricUnit); switch (unitType) { case 'speed': return system === 'imperial' ? 'mph' : 'km/h'; case 'distance': return system === 'imperial' ? 'ft' : 'm'; case 'height': return system === 'imperial' ? 'in' : 'cm'; case 'weight': return system === 'imperial' ? 'lbs' : 'kg'; default: return metricUnit; } }