/**
 * Schedule range-calendar label substitution per TZ §4.1.9 Table 14.
 * Any range containing today → "Текущая неделя", otherwise dd.MM.yyyy-dd.MM.yyyy.
 *
 * Matches Angular `CalendarInputWeekComponent.getDateString()`: substitutes
 * the label whenever `from <= today <= to`, not only on an exact Mon-Sun
 * match. This covers the popular-click case where the start page clamps
 * the outbound `from` to today−1 to stay inside Schedule's [-1, +330] window.
 */

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type TFunction = (key: string, opts?: any) => string;

function toYmd(d: Date): string {
  const day = String(d.getDate()).padStart(2, "0");
  const month = String(d.getMonth() + 1).padStart(2, "0");
  return `${day}.${month}.${d.getFullYear()}`;
}

export function formatScheduleDateRangeWithCurrentWeek(
  dateFrom: Date | null | undefined,
  dateTo: Date | null | undefined,
  t: TFunction,
): string {
  if (!dateFrom || !dateTo) return "";
  const today = new Date();
  today.setHours(0, 0, 0, 0);
  const from = new Date(dateFrom);
  from.setHours(0, 0, 0, 0);
  const to = new Date(dateTo);
  to.setHours(0, 0, 0, 0);
  if (from.getTime() <= today.getTime() && today.getTime() <= to.getTime()) {
    return t("SCHEDULE.CURRENT-WEEK");
  }
  return `${toYmd(from)}-${toYmd(to)}`;
}
