// SPDX-FileCopyrightText: 2026 FocusFlow contributors
// SPDX-License-Identifier: AGPL-3.0-only

part of '../../today_screen_models.dart';

/// Top-level helper that performs the `_dateLabel` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
String _dateLabel(CivilDate date) {
  const months = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December',
  ];
  return '${months[date.month - 1]} ${date.day}, ${date.year}';
}

/// Top-level helper that performs the `_shortDateTimeLabel` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
String _shortDateTimeLabel(
  DateTime value, {
  Duration timeZoneOffset = Duration.zero,
}) {
  final localValue = _displayDateTime(value, timeZoneOffset);
  const months = [
    'Jan',
    'Feb',
    'Mar',
    'Apr',
    'May',
    'Jun',
    'Jul',
    'Aug',
    'Sep',
    'Oct',
    'Nov',
    'Dec',
  ];
  return '${months[localValue.month - 1]} ${localValue.day}, '
      '${_formatTime(value, timeZoneOffset: timeZoneOffset)}';
}

/// Top-level helper that performs the `_civilDateForDateTime` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
CivilDate? _civilDateForDateTime(
  DateTime? value, {
  Duration timeZoneOffset = Duration.zero,
}) {
  if (value == null) {
    return null;
  }
  final localValue = _displayDateTime(value, timeZoneOffset);
  return CivilDate(localValue.year, localValue.month, localValue.day);
}

/// Top-level helper that performs the `_minutesSinceMidnight` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
int _minutesSinceMidnight(
  DateTime? value, {
  Duration timeZoneOffset = Duration.zero,
}) {
  if (value == null) {
    return 0;
  }
  final localValue = _displayDateTime(value, timeZoneOffset);
  return localValue.hour * 60 + localValue.minute;
}

/// Top-level helper that performs the `_formatTime` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
String _formatTime(DateTime? value, {Duration timeZoneOffset = Duration.zero}) {
  if (value == null) {
    return '';
  }
  final localValue = _displayDateTime(value, timeZoneOffset);
  final period = localValue.hour >= 12 ? 'PM' : 'AM';
  final rawHour = localValue.hour % 12;
  final hour = rawHour == 0 ? 12 : rawHour;
  final minute = localValue.minute.toString().padLeft(2, '0');
  return '$hour:$minute $period';
}

/// Top-level helper that performs the `_displayDateTime` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
DateTime _displayDateTime(DateTime value, Duration timeZoneOffset) {
  return value.toUtc().add(timeZoneOffset);
}
