import { Dimensions, DimensionUnit } from '../../types';

export function formatDimensions(dims: Dimensions): string {
  const parts: string[] = [];
  if (dims.height) parts.push(`H: ${dims.height}`);
  if (dims.width) parts.push(`W: ${dims.width}`);
  if (dims.diameter) parts.push(`D: ${dims.diameter}`);

  return parts.length > 0 ? `${parts.join(' × ')} ${dims.unit}` : '';
}

export function convertDimensionUnit(dims: Dimensions, toUnit: DimensionUnit): Dimensions {
  if (dims.unit === toUnit) return dims;

  const factor = toUnit === 'cm' ? 2.54 : 1 / 2.54;

  return {
    height: dims.height ? Math.round(dims.height * factor * 10) / 10 : undefined,
    width: dims.width ? Math.round(dims.width * factor * 10) / 10 : undefined,
    diameter: dims.diameter ? Math.round(dims.diameter * factor * 10) / 10 : undefined,
    unit: toUnit,
    notes: dims.notes,
  };
}
