import { useMemo, useState } from "react";

export function useDateWindow(initialStart = new Date()) {
	const [startDate, setStartDate] = useState(initialStart);
	const [endDayCount, setEndDayCount] = useState<string>(
		localStorage.getItem("endDayCount") || "5",
	);
	const [startDayCount, startEndDayCount] = useState<string>(
		localStorage.getItem("startDayCount") || "0",
	);

	const days = useMemo(() => {
		const startOffset = parseInt(startDayCount, 10);
		const endOffset = parseInt(endDayCount, 10);

		const start = new Date(startDate);
		start.setDate(start.getDate() - startOffset);

		return Array.from({ length: endOffset + startOffset + 1 }, (_, i) => {
			const d = new Date(start);
			d.setDate(start.getDate() + i);
			return d;
		});
	}, [startDate, startDayCount, endDayCount]);

	return {
		days,
		startDate,
		setStartDate,
		endDayCount,
		setEndDayCount,
		startDayCount,
		startEndDayCount,
	};
}
