import '../../../../api/marianumconnect/queries/timetable_get_week/timetable_get_week_response.dart';

final RegExp _whitespaceRun = RegExp(r'\s+');

/// Collapses any line-break or whitespace run to a single space and trims.
/// Returns null when input is null or fully whitespace. Webuntis sometimes
/// returns multi-line values like "A30\n4" — this normalizes those so labels
/// render on a single line.
String? collapseWhitespace(String? s) {
  if (s == null) return null;
  final cleaned = s.replaceAll(_whitespaceRun, ' ').trim();
  return cleaned.isEmpty ? null : cleaned;
}

/// "7a, 7b" — shared by the calendar tile factory and the home-widget mapper
/// so both surfaces render identical class labels on teacher plans.
extension LessonClassLabel on McTimetableEntry {
  String? get classLabel => collapseWhitespace(classNames.join(', '));
}
