/* @vitest-environment node */

import { describe, it, expect } from "vitest";
import {
	normalizeDayClickArgs,
	buildCalendarState,
} from "./dateRangePickerUtils.js";

describe("lib/frontend/search/dateRangePickerUtils", () => {
	describe("normalizeDayClickArgs", () => {
		it("supports (day, modifiers) signature", () => {
			const day = new Date(2026, 0, 10);
			const modifiers = { disabled: false };

			const out = normalizeDayClickArgs([day, modifiers]);
			expect(out.day).toBe(day);
			expect(out.modifiers).toBe(modifiers);
		});

		it("supports (event, day, modifiers) signature", () => {
			const day = new Date(2026, 0, 10);
			const modifiers = { disabled: true };

			const out = normalizeDayClickArgs([{}, day, modifiers]);
			expect(out.day).toBe(day);
			expect(out.modifiers).toBe(modifiers);
		});

		it("returns nulls for unknown inputs", () => {
			const out = normalizeDayClickArgs([{}, {}, {}]);
			expect(out).toEqual({ day: null, modifiers: null });
		});
	});

	describe("buildCalendarState", () => {
		it("builds a valid selected range", () => {
			const from = new Date(2026, 0, 1);
			const to = new Date(2026, 0, 5);

			const out = buildCalendarState({
				fromDate: from,
				toDate: to,
				isRangeInvalid: false,
			});
			expect(out.calendarSelected).toEqual({ from, to });
			expect(out.calendarModifiers).toBe(undefined);
		});

		it("builds an invalid interval and modifiers when range is invalid", () => {
			const from = new Date(2026, 0, 10);
			const to = new Date(2026, 0, 5);

			const out = buildCalendarState({
				fromDate: from,
				toDate: to,
				isRangeInvalid: true,
			});
			expect(out.calendarSelected.from <= out.calendarSelected.to).toBe(true);
			expect(out.calendarModifiers).toBeTruthy();
			expect(out.calendarModifiersClassNames).toBeTruthy();
		});

		it("supports open range (from only)", () => {
			const from = new Date(2026, 0, 2);

			const out = buildCalendarState({
				fromDate: from,
				toDate: null,
				isRangeInvalid: false,
			});
			expect(out.calendarSelected).toEqual({ from, to: undefined });
		});

		it("supports to-only (single-day range)", () => {
			const to = new Date(2026, 0, 2);

			const out = buildCalendarState({
				fromDate: null,
				toDate: to,
				isRangeInvalid: false,
			});
			expect(out.calendarSelected).toEqual({ from: to, to });
		});
	});
});
