import 'package:reactive_forms/reactive_forms.dart';

class ErrorMessages {
  static const messages = {
    'required': '{_field_} harus diisi',
    'email': 'Format email harus benar',
  };

  static String getUiErrorMessage({
    required AbstractControl control,
    required String label,
    Map<String, String>? widgetCustomMessages,
  }) {
    Map<String, Object>? mergedErrorMessages = {
      ...?widgetCustomMessages,
      ...messages,
    };

    final message = mergedErrorMessages.entries.firstWhere(
      (element) => element.key == control.errors.entries.first.key,
      orElse: () => const MapEntry('', ''),
    );

    return message.value.toString().replaceFirst('{_field_}', label);
  }
}
