import 'package:flutter/services.dart';
import 'package:intl/intl.dart';

class IndianNumberInputFormatter extends TextInputFormatter {
  final NumberFormat _formatter = NumberFormat('##,##,###');

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    if (newValue.text.isEmpty) {
      return newValue.copyWith(text: '');
    }

    final cleanText = newValue.text.replaceAll(',', ''); // Remove existing commas
    final formattedText = _formatter.format(int.tryParse(cleanText) ?? 0);

    return newValue.copyWith(
      text: formattedText,
      selection: TextSelection.collapsed(offset: formattedText.length),
    );
  }
}
