import type { InputNumberProps } from 'antd';

type Formatter = NonNullable<InputNumberProps['formatter']>;

export function createFormatter(options: Intl.NumberFormatOptions) {
  const format = Intl.NumberFormat('ru', options).format;
  const defaultFormat = Intl.NumberFormat('ru').format;

  return ((value, { userTyping }) => {
    if (userTyping) {
      if (options.minimumFractionDigits && options.minimumFractionDigits <= 2) {
        return defaultFormat((value || 0) as number);
      }

      return value || 0;
    }

    return format((value || 0) as number);
  }) as Formatter;
}
