import { generateDateByUTCValue, getDateUTCValue, sameDate, sameYear, sameYearSameMonth } from "../../utils/dates.js";
import { viewYears } from "../consts.js";
import { getNextDate, isDay, isExact, isFrom, isMonth, isRange, isTo, isYear } from "../utils.js";
import { useBoundaries } from "./useBoundaries.js";
import { useSelectedDate } from "./useSelectedDate.js";
import { useMemo } from "react";

//#region src/calendar/hooks/useCalendar.ts
/**
* Main hook for doing calendar calculations
* Calculates for all time unit modes: day, month and year
* Depends on useBoundaries and useSelectedDate to do initial setup
*/
const useCalendar = ({ boundaries, selectedDates, mode = "exact", timeUnitMode, viewDate }) => {
	const boundariesProcessed = useBoundaries({ boundaries });
	const selectedProcessed = useSelectedDate({
		selectedDates,
		mode
	});
	return useMemo(() => {
		const { maxDateTime, minDateTime } = boundariesProcessed;
		const { currentDateTime, maxSelectedDateTime, minSelectedDateTime } = selectedProcessed;
		const { utcYear: viewDateYear, utcMonth: viewDateMonth } = getDateUTCValue(viewDate);
		let calendarFirstDay;
		let calendarLastDay;
		let nextFirstDay;
		let prevLastDay;
		let firstDay;
		let lastDay;
		if (isDay(timeUnitMode)) {
			firstDay = generateDateByUTCValue(viewDateYear, viewDateMonth, 1);
			lastDay = generateDateByUTCValue(viewDateYear, viewDateMonth + 1, 0);
			nextFirstDay = generateDateByUTCValue(viewDateYear, viewDateMonth + 1, 1);
			prevLastDay = generateDateByUTCValue(viewDateYear, viewDateMonth, 0);
			const weekdayFirst = firstDay.getUTCDay();
			const weekdayLast = lastDay.getUTCDay();
			calendarFirstDay = new Date(Date.UTC(viewDateYear, viewDateMonth, 2 - weekdayFirst));
			if (weekdayFirst === 0) calendarFirstDay = new Date(Date.UTC(viewDateYear, viewDateMonth, -5));
			calendarLastDay = new Date(Date.UTC(viewDateYear, viewDateMonth + 1, 8 - weekdayLast));
			if (weekdayLast === 0) calendarLastDay = new Date(Date.UTC(viewDateYear, viewDateMonth + 1, 1));
			else if (weekdayLast === 1) calendarLastDay = new Date(Date.UTC(viewDateYear, viewDateMonth + 1, 0));
		} else if (isMonth(timeUnitMode)) {
			calendarFirstDay = firstDay = generateDateByUTCValue(viewDateYear, 0, 1);
			calendarLastDay = lastDay = generateDateByUTCValue(viewDateYear, 11, 31);
			nextFirstDay = generateDateByUTCValue(viewDateYear + 1, 0, 1);
			prevLastDay = generateDateByUTCValue(viewDateYear - 1, 11, 31);
		} else {
			const calendarStartYear = (/* @__PURE__ */ new Date()).getUTCFullYear() - viewYears / 2;
			const diffYears = calendarStartYear - viewDateYear;
			const startYear = calendarStartYear - Math.ceil(diffYears / viewYears) * viewYears;
			const endYear = startYear + viewYears - 1;
			if (startYear === 1753) calendarFirstDay = firstDay = generateDateByUTCValue(startYear, 0, 2);
			else calendarFirstDay = firstDay = generateDateByUTCValue(startYear, 0, 1);
			calendarLastDay = lastDay = generateDateByUTCValue(endYear, 11, 31);
			nextFirstDay = generateDateByUTCValue(endYear + 1, 0, 1);
			prevLastDay = generateDateByUTCValue(startYear - 1, 11, 31);
		}
		const items = [];
		let dateItem = calendarFirstDay;
		do {
			const isHidden = dateItem < firstDay || dateItem >= lastDay;
			const isToday = sameDate(dateItem, /* @__PURE__ */ new Date());
			const { utcYear: minDateYear, utcMonth: minDateMonth } = getDateUTCValue(minDateTime);
			let minUnitModeDate = minDateTime;
			if (isMonth(timeUnitMode)) minUnitModeDate = generateDateByUTCValue(minDateYear, minDateMonth, 1);
			else if (isYear(timeUnitMode)) minUnitModeDate = generateDateByUTCValue(minDateYear, 0, 1);
			const isDisabled = dateItem < minUnitModeDate || dateItem > maxDateTime ? true : false;
			const isExactSelected = isExact(mode) && minSelectedDateTime && +dateItem === +minSelectedDateTime;
			const inRangeLow = minSelectedDateTime && dateItem >= minSelectedDateTime;
			const inRangeHigh = maxSelectedDateTime && dateItem <= maxSelectedDateTime;
			const inRange = !isExact(mode) && (!isDisabled && inRangeLow && (!maxSelectedDateTime || inRangeHigh) || inRangeHigh && (!minSelectedDateTime || inRangeLow));
			const isRangeSelected = isRange(mode) && inRange;
			const isFromSelected = isFrom(mode) && inRangeLow;
			const isToSelected = isTo(mode) && inRangeHigh;
			const isSelected = !!(isExactSelected || isFromSelected || isToSelected || isRangeSelected);
			let maxSelectedCheck = maxSelectedDateTime;
			let minSelectedCheck = minSelectedDateTime;
			if (isYear(timeUnitMode)) {
				if (maxSelectedCheck) maxSelectedCheck = generateDateByUTCValue(maxSelectedCheck.getUTCFullYear(), 0, 1);
				if (minSelectedCheck) minSelectedCheck = generateDateByUTCValue(minSelectedCheck.getUTCFullYear(), 0, 1);
			}
			if (isMonth(timeUnitMode)) {
				if (maxSelectedCheck) maxSelectedCheck = generateDateByUTCValue(maxSelectedCheck.getUTCFullYear(), maxSelectedCheck.getUTCMonth(), 1);
				if (minSelectedCheck) minSelectedCheck = generateDateByUTCValue(minSelectedCheck.getUTCFullYear(), minSelectedCheck.getUTCMonth(), 1);
			}
			const boundary = !isTo(mode) && sameDate(dateItem, minSelectedCheck);
			let current;
			if (isDay(timeUnitMode)) current = sameDate(dateItem, currentDateTime);
			else if (isMonth(timeUnitMode)) current = sameYearSameMonth(dateItem, currentDateTime);
			else current = sameYear(dateItem, currentDateTime);
			items.push({
				date: dateItem,
				isHidden,
				isToday,
				isDisabled: boundary ? false : isDisabled,
				isSelected,
				boundary,
				current
			});
			dateItem = getNextDate(dateItem, timeUnitMode);
		} while (dateItem <= calendarLastDay);
		return {
			items,
			prevDisabled: minDateTime > prevLastDay,
			nextDisabled: maxDateTime < nextFirstDay
		};
	}, [
		boundaries,
		selectedDates,
		mode,
		timeUnitMode,
		viewDate
	]);
};

//#endregion
export { useCalendar };
globalThis.__vuiVersion__ = "4.0.0-beta.1"
//# sourceMappingURL=useCalendar.js.map