import type { CalculatorDef } from '$lib/data/calculators';
import { formatConversionValue } from '$lib/utils/formatConversionValue';

type RateConfig = Pick<CalculatorDef, 'type' | 'factor' | 'offset' | 'labels'>;

export const getConversionRateText = (config: RateConfig): string | null => {
  if (config.type !== 'standard' || !config.factor) {
    return null;
  }

  const { in1, in2 } = config.labels;
  if (!in1 || !in2) {
    return null;
  }

  const formattedFactor = formatConversionValue(config.factor);
  const hasOffset = Boolean(config.offset);
  const formattedOffset = formatConversionValue(config.offset ?? 0);

  return `1 ${in1} = ${formattedFactor}${hasOffset ? ` + ${formattedOffset}` : ''} ${in2}`;
};
