import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { formatScheduleDateRangeWithCurrentWeek } from "./dateLabels.js";

describe("4.1.9-R: formatScheduleDateRangeWithCurrentWeek", () => {
  beforeEach(() => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date(2026, 4, 15, 12, 0, 0)); // Fri 2026-05-15
  });
  afterEach(() => { vi.useRealTimers(); });

  it("returns 'Текущая неделя' when range = Mon-Sun of current week", () => {
    const t = (k: string) => (k === "SCHEDULE.CURRENT-WEEK" ? "Текущая неделя" : k);
    expect(
      formatScheduleDateRangeWithCurrentWeek(new Date(2026, 4, 11), new Date(2026, 4, 17), t),
    ).toBe("Текущая неделя");
  });

  it("returns dd.MM.yyyy-dd.MM.yyyy for ranges that don't contain today", () => {
    const t = (k: string) => k;
    expect(
      formatScheduleDateRangeWithCurrentWeek(new Date(2026, 4, 18), new Date(2026, 4, 24), t),
    ).toBe("18.05.2026-24.05.2026");
  });

  it("returns 'Текущая неделя' for partial current week containing today (matches Angular)", () => {
    // today = 2026-05-15 (Fri); range 2026-05-13 .. 2026-05-17 contains today.
    // Angular's CalendarInputWeekComponent uses `from <= today <= to`.
    const t = (k: string) => (k === "SCHEDULE.CURRENT-WEEK" ? "Текущая неделя" : k);
    expect(
      formatScheduleDateRangeWithCurrentWeek(new Date(2026, 4, 13), new Date(2026, 4, 17), t),
    ).toBe("Текущая неделя");
  });

  it("returns 'Текущая неделя' for clamped popular-click range (today-1 .. Sun)", () => {
    // Popular-click on Schedule clamps `from` to today−1 = 2026-05-14.
    const t = (k: string) => (k === "SCHEDULE.CURRENT-WEEK" ? "Текущая неделя" : k);
    expect(
      formatScheduleDateRangeWithCurrentWeek(new Date(2026, 4, 14), new Date(2026, 4, 17), t),
    ).toBe("Текущая неделя");
  });

  it("returns empty string for null inputs", () => {
    expect(formatScheduleDateRangeWithCurrentWeek(null, null, () => "")).toBe("");
  });
});
