export type NumberInput = number | string | null | undefined;

export function formatNumber(
  value: NumberInput,
  options?: Intl.NumberFormatOptions
): string {
  if (value === '' || value === null || value === undefined) {
    return '';
  }

  const parsed =
    typeof value === 'number' ? value : Number(String(value).replaceAll(',', ''));

  if (Number.isNaN(parsed)) {
    return '';
  }

  return new Intl.NumberFormat('en-US', options).format(parsed);
}
