/// Formatage affichage prénom / nom (capitalisation par mot et segments après `-` ou `'`).

String formatPersonNameCase(String raw) {
  final trimmed = raw.trim();
  if (trimmed.isEmpty) {
    return trimmed;
  }
  final words = trimmed.split(RegExp(r'\s+'));
  return words.map(_capitalizeComposedWord).join(' ');
}

String _capitalizeComposedWord(String word) {
  if (word.isEmpty) {
    return word;
  }
  final lower = word.toLowerCase();
  const separators = <String>{'-', "'", '’'};
  final buffer = StringBuffer();
  var capitalizeNext = true;

  for (var i = 0; i < lower.length; i++) {
    final char = lower[i];
    if (capitalizeNext && RegExp(r'[a-zà-öø-ÿ]').hasMatch(char)) {
      buffer.write(char.toUpperCase());
      capitalizeNext = false;
    } else {
      buffer.write(char);
      capitalizeNext = separators.contains(char);
    }
  }
  return buffer.toString();
}
