/**
 * Online-Board `Время рейса` default time range per TZ §4.1.1-R4 and R5.
 *
 * Desktop/tablet: always `00:00-24:00`.
 * Mobile: [-1h, +3h] window centered on the user's current local time,
 * clamped to [00:00, 24:00].
 *
 * Output uses `HHmm` strings (e.g. "0800" = 08:00). `2400` represents
 * end-of-day inclusive, matching existing URL encoding.
 */

export interface TimeRange {
  timeFrom: string;
  timeTo: string;
}

export function getOnlineBoardDefaultTimeRange(
  isMobile: boolean,
  now: Date = new Date(),
): TimeRange {
  if (!isMobile) {
    return { timeFrom: "0000", timeTo: "2400" };
  }
  const nowMinutes = now.getHours() * 60 + now.getMinutes();
  const fromMinutes = Math.max(0, nowMinutes - 60);
  const toMinutes = Math.min(24 * 60, nowMinutes + 3 * 60);
  return {
    timeFrom: minutesToHHmm(fromMinutes),
    timeTo: minutesToHHmm(toMinutes),
  };
}

function minutesToHHmm(total: number): string {
  if (total === 24 * 60) return "2400";
  const h = Math.floor(total / 60);
  const m = total % 60;
  return `${String(h).padStart(2, "0")}${String(m).padStart(2, "0")}`;
}
