export const formatPhoneNumber = (phone) => {
  if (!phone) return '';
  return phone.replace(/(\d{2})(\d{4})(\d+)/, '$1-$2-$3');
};

export const validateGPS = (lat, lng) => {
  const latNum = parseFloat(lat);
  const lngNum = parseFloat(lng);
  
  if (isNaN(latNum) || isNaN(lngNum)) {
    return { valid: false, message: 'Ungültige GPS-Koordinaten' };
  }
  
  if (latNum < -90 || latNum > 90) {
    return { valid: false, message: 'Latitude muss zwischen -90 und 90 liegen' };
  }
  
  if (lngNum < -180 || lngNum > 180) {
    return { valid: false, message: 'Longitude muss zwischen -180 und 180 liegen' };
  }
  
  return { valid: true };
};

export const calculateDistance = (lat1, lng1, lat2, lng2) => {
  const R = 6371; // Radius der Erde in km
  const dLat = (lat2 - lat1) * Math.PI / 180;
  const dLng = (lng2 - lng1) * Math.PI / 180;
  const a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
    Math.sin(dLng/2) * Math.sin(dLng/2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  return R * c;
};
