import { n as e } from "./button-BsvHGWPc.js";
import { useState as t } from "react";
import { cn as n } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as r, jsxs as i } from "react/jsx-runtime";
import { ChevronLeft as a, ChevronRight as o } from "lucide-react";
import { addCalendarMonths as s, addCalendarYears as c, createCalendarDate as l, getCalendarPeriod as u, getStartOfDay as d, isCalendarPeriodWithinDateBounds as f, isDateInsideCalendarPeriod as p, isDateRangeTuple as m, isSameCalendarDay as h } from "@ryuzaki13/react-foundation-lib/formatters";
//#region src/date-input/lib/dateInputCalendar.ts
var g = [
	"day",
	"month",
	"year"
], _ = [
	"day",
	"week",
	"month",
	"year"
], v = [
	"friday",
	"saturday",
	"sunday"
];
function y(e) {
	return e === "month" ? "month" : e === "year" ? "year" : "day";
}
function b(e, t) {
	return g.indexOf(e) >= g.indexOf(t);
}
function x(e, t) {
	let n = y(e);
	return b(n, t) ? n : t;
}
function S(e, t) {
	return !e || !t ? !1 : e.start.getTime() === t.start.getTime() && e.end.getTime() === t.end.getTime();
}
function C(e, t, n) {
	if (!e || !t || !n) return !1;
	let r = Math.min(t.start.getTime(), n.start.getTime()), i = Math.max(t.end.getTime(), n.end.getTime());
	return e.start.getTime() >= r && e.end.getTime() <= i;
}
function w({ date: e, value: t, hoveredDate: n, selectsRange: r, selectionOptions: i }) {
	let a = u(e, i), o = !r && t instanceof Date ? t : null, s = u(o, i);
	if (!r || !Array.isArray(t)) return {
		selected: p(e, s),
		range: !1,
		rangeStart: !1,
		rangeEnd: !1
	};
	let [c, l] = t, d = u(c, i), f = u(l ?? (c ? n : null), i), m = S(a, d), h = S(a, f);
	return {
		selected: !1,
		range: C(a, d, f) && !m && !h,
		rangeStart: m,
		rangeEnd: h
	};
}
var T = {
	dayView: "Dshr-j",
	weekdays: "esetVt",
	weekday: "i-LJYe",
	daysGrid: "XZsnCH",
	week: "_2KDjTS",
	day: "o-freG",
	disabled: "GcV08-",
	outsideMonth: "UCrE-I",
	periodSelection: "L2V3uq",
	periodPreview: "Te9oCq",
	selected: "cUF2pQ",
	range: "d1L5uw",
	rangeStart: "LU0M9Y",
	rangeEnd: "MR3y5p",
	weekend: "VDqDpm",
	now: "CLR-7m"
}, E = [
	"Пн",
	"Вт",
	"Ср",
	"Чт",
	"Пт",
	"Сб",
	"Вс"
], D = new Intl.DateTimeFormat("ru-RU", {
	weekday: "long",
	year: "numeric",
	month: "long",
	day: "numeric"
}), O = ({ now: e, currentDate: a, value: o, onChange: s, minDate: c, maxDate: m, selectsRange: g, selectionMode: _ = "day", weekEndDay: v = "sunday" }) => {
	let [y, b] = t(null), x = {
		selectionMode: _,
		weekEndDay: v
	}, S = (() => {
		let e = a.getFullYear(), t = a.getMonth(), n = l(e, t, 1), r = l(e, t + 1, 0), i = n.getDay() === 0 ? 6 : n.getDay() - 1, o = 42 - (i + r.getDate()), s = [], c = l(e, t, 0).getDate();
		for (let n = i; n > 0; --n) s.push(l(e, t - 1, c - n + 1));
		for (let n = 1; n <= r.getDate(); n += 1) s.push(l(e, t, n));
		for (let n = 1; n <= o; n += 1) s.push(l(e, t + 1, n));
		let u = [];
		for (let e = 0; e < s.length; e += 7) u.push(s.slice(e, e + 7));
		return u;
	})(), C = g && Array.isArray(o) ? o[0] : null, w = g && Array.isArray(o) ? o[1] : null, O = u(g ? null : o, x), k = u(y, x), A = w ?? (C ? k?.end ?? null : null), j = _ === "week", M = j && (!g || !C), N = (e) => {
		if (!g || !C || !A) return !1;
		let t = d(e).getTime(), n = C.getTime(), r = A.getTime();
		return t >= Math.min(n, r) && t <= Math.max(n, r);
	}, P = (e) => !f(u(e, x), {
		minDate: c,
		maxDate: m
	}), F = (e, t) => {
		t || (j && b(e), !(!g || !C || w) && (j || b(e)));
	};
	return /* @__PURE__ */ i("div", {
		className: T.dayView,
		children: [/* @__PURE__ */ r("div", {
			className: T.weekdays,
			children: E.map((e) => /* @__PURE__ */ r("div", {
				className: T.weekday,
				children: e
			}, e))
		}), /* @__PURE__ */ r("div", {
			className: T.daysGrid,
			onMouseLeave: () => {
				(j || !w) && b(null);
			},
			children: S.map((t, i) => /* @__PURE__ */ r("div", {
				className: T.week,
				children: t.map((t, o) => {
					let c = t.getMonth() === a.getMonth(), l = p(t, O), u = M && p(t, k), d = N(t), f = h(t, C), m = h(t, A), g = h(t, e), v = P(t);
					return /* @__PURE__ */ r("button", {
						type: "button",
						onClick: () => !v && s(t),
						onMouseEnter: () => F(t, v),
						disabled: v,
						"data-ui": "calendar-day-button",
						"data-action": "select-calendar-day",
						className: n(T.day, {
							[T.outsideMonth]: !c,
							[T.periodSelection]: _ === "week",
							[T.periodPreview]: u && !l,
							[T.selected]: l,
							[T.range]: d && !f && !m,
							[T.rangeStart]: f,
							[T.rangeEnd]: m,
							[T.disabled]: v,
							[T.weekend]: o > 4,
							[T.now]: g
						}),
						"aria-label": D.format(t),
						"aria-pressed": l || d || f || m,
						children: t.getDate()
					}, `${i}-${o}`);
				})
			}, i))
		})]
	});
}, k = {
	monthView: "p5pRGW",
	monthsGrid: "pUHpxD",
	month: "lwWv-z",
	disabled: "_8gn-3z",
	range: "b57d-c",
	selected: "nGc5FO",
	rangeStart: "JLqjri",
	rangeEnd: "UQMYwg"
}, A = new Intl.DateTimeFormat("ru-RU", { month: "short" }), j = new Intl.DateTimeFormat("ru-RU", {
	month: "long",
	year: "numeric"
}), M = ({ currentDate: e, value: i, onChange: a, minDate: o, maxDate: s, selectsRange: c, selectsPeriod: d, selectionMode: p = "day", weekEndDay: m = "sunday" }) => {
	let [h, g] = t(null), _ = Array.from({ length: 12 }, (e, t) => t), v = e.getFullYear(), y = {
		selectionMode: p,
		weekEndDay: m
	}, b = c && Array.isArray(i) ? i[0] : null, x = c && Array.isArray(i) ? i[1] : null, S = (e) => A.format(l(v, e, 1)), C = (e) => {
		if (!o && !s) return !1;
		let t = l(v, e, 1), n = l(v, e + 1, 0, 23, 59, 59);
		return d ? !f(u(t, y), {
			minDate: o,
			maxDate: s
		}) : !!(o && n < o || s && t > s);
	}, T = (e, t) => {
		!c || !b || x || t || g(e);
	};
	return /* @__PURE__ */ r("div", {
		className: k.monthView,
		children: /* @__PURE__ */ r("div", {
			className: k.monthsGrid,
			onMouseLeave: () => {
				x || g(null);
			},
			children: _.map((e) => {
				let t = C(e), o = l(v, e, 1), s = w({
					date: o,
					value: i,
					hoveredDate: h,
					selectsRange: c,
					selectionOptions: y
				});
				return /* @__PURE__ */ r("button", {
					type: "button",
					onClick: () => !t && a(o),
					onMouseEnter: () => T(o, t),
					disabled: t,
					"data-ui": "calendar-month-button",
					"data-action": "select-calendar-month",
					className: n(k.month, {
						[k.selected]: s.selected,
						[k.range]: s.range,
						[k.rangeStart]: s.rangeStart,
						[k.rangeEnd]: s.rangeEnd,
						[k.disabled]: t
					}),
					"aria-label": j.format(o),
					"aria-pressed": s.selected || s.range || s.rangeStart || s.rangeEnd,
					children: S(e)
				}, e);
			})
		})
	});
}, N = {
	calendarView: "E35KE6",
	calendarHeader: "L14Qef"
}, P = {
	yearView: "_--xW7O",
	yearsGrid: "-N-5Jk",
	year: "hhY92S",
	disabled: "IhX7jj",
	range: "NWNpWj",
	selected: "d1SfcV",
	rangeStart: "Gkui4r",
	rangeEnd: "_0dVZLW"
}, F = ({ currentDate: e, value: i, onChange: a, minDate: o, maxDate: s, selectsRange: c, selectsPeriod: d, selectionMode: p = "day", weekEndDay: m = "sunday" }) => {
	let [h, g] = t(null), _ = Math.floor(e.getFullYear() / 12) * 12, v = Array.from({ length: 12 }, (e, t) => _ + t), y = {
		selectionMode: p,
		weekEndDay: m
	}, b = c && Array.isArray(i) ? i[0] : null, x = c && Array.isArray(i) ? i[1] : null, S = (e) => {
		if (!o && !s) return !1;
		let t = l(e, 0, 1), n = l(e, 11, 31, 23, 59, 59);
		return d ? !f(u(t, y), {
			minDate: o,
			maxDate: s
		}) : !!(o && n < o || s && t > s);
	}, C = (e, t) => {
		!c || !b || x || t || g(e);
	};
	return /* @__PURE__ */ r("div", {
		className: P.yearView,
		children: /* @__PURE__ */ r("div", {
			className: P.yearsGrid,
			onMouseLeave: () => {
				x || g(null);
			},
			children: v.map((e) => {
				let t = S(e), o = l(e, 0, 1), s = w({
					date: o,
					value: i,
					hoveredDate: h,
					selectsRange: c,
					selectionOptions: y
				});
				return /* @__PURE__ */ r("button", {
					type: "button",
					onClick: () => !t && a(o),
					onMouseEnter: () => C(o, t),
					disabled: t,
					"data-ui": "calendar-year-button",
					"data-action": "select-calendar-year",
					className: n(P.year, {
						[P.selected]: s.selected,
						[P.range]: s.range,
						[P.rangeStart]: s.rangeStart,
						[P.rangeEnd]: s.rangeEnd,
						[P.disabled]: t
					}),
					"aria-label": e.toString(),
					"aria-pressed": s.selected || s.range || s.rangeStart || s.rangeEnd,
					children: e
				}, e);
			})
		})
	});
};
//#endregion
//#region src/date-input/ui/CalendarView.tsx
function I(e, t) {
	if (t && m(e) && e[0] instanceof Date) return e[0];
	if (!t && e instanceof Date) return e;
}
function L({ view: n, datePickerLevel: l, selectionMode: p = "day", weekEndDay: m = "sunday", onViewChange: h, ...g }) {
	let [_] = t(() => d(/* @__PURE__ */ new Date())), v = I(g.value, g.selectsRange), y = v?.getTime() ?? null, [x, S] = t(() => ({
		sourceValueTime: y,
		currentDate: v ?? d(/* @__PURE__ */ new Date())
	})), C = v && x.sourceValueTime !== y ? v : x.currentDate, w = g.currentDate ?? C, T = {
		selectionMode: p,
		weekEndDay: m
	}, E = (e) => {
		let t = typeof e == "function" ? e(w) : e;
		g.currentDate === void 0 && S({
			sourceValueTime: y,
			currentDate: t
		}), g.onCurrentDateChange?.(t);
	}, D = () => {
		if (n === "day") {
			E((e) => s(e, -1));
			return;
		}
		if (n === "month") {
			E((e) => c(e, -1));
			return;
		}
		E((e) => c(e, -12));
	}, k = () => {
		if (n === "day") {
			E((e) => s(e, 1));
			return;
		}
		if (n === "month") {
			E((e) => c(e, 1));
			return;
		}
		E((e) => c(e, 12));
	};
	return /* @__PURE__ */ i("div", {
		className: N.calendarView,
		children: [(() => {
			let t = "";
			if (n === "day") t = w.toLocaleDateString("ru-RU", {
				month: "long",
				year: "numeric"
			});
			else if (n === "month") t = w.getFullYear().toString();
			else {
				let e = Math.floor(w.getFullYear() / 12) * 12;
				t = `${e} - ${e + 11}`;
			}
			return /* @__PURE__ */ i("div", {
				className: N.calendarHeader,
				children: [
					/* @__PURE__ */ r(e, {
						icon: /* @__PURE__ */ r(a, {}),
						onClick: D,
						variant: "ghost",
						"aria-label": "Предыдущий",
						"data-action": "calendar-previous"
					}),
					/* @__PURE__ */ r(e, {
						onClick: () => {
							n === "day" ? h("month") : n === "month" && h("year");
						},
						variant: "ghost",
						className: "flex1",
						"data-ui": "calendar-view-switch-button",
						"data-action": "calendar-switch-view",
						children: t
					}),
					/* @__PURE__ */ r(e, {
						icon: /* @__PURE__ */ r(o, {}),
						onClick: k,
						variant: "ghost",
						"aria-label": "Следующий",
						"data-ui": "calendar-next-button",
						"data-action": "calendar-next"
					})
				]
			});
		})(), (() => {
			let e = (e) => {
				let t = u(e, T);
				if (!(!t || !f(t, {
					minDate: g.minDate,
					maxDate: g.maxDate
				}))) {
					if (g.selectsRange) {
						let e = null, n = null;
						if (Array.isArray(g.value) && ([e, n] = g.value), !e || n) {
							g.onChange([t.start, null]);
							return;
						}
						let r = u(e, T);
						if (!r) return;
						if (t.start < r.start) {
							g.onChange([t.start, r.end]);
							return;
						}
						g.onChange([r.start, t.end]);
						return;
					}
					g.onChange(t.start);
				}
			}, t = (t) => {
				if (E(t), p === "month" || !b("day", l)) {
					e(t);
					return;
				}
				h("day");
			}, i = (t) => {
				if (E(t), p === "year" || !b("month", l)) {
					e(t);
					return;
				}
				h("month");
			};
			switch (n) {
				case "day": return /* @__PURE__ */ r(O, {
					now: _,
					currentDate: w,
					value: g.value,
					onChange: e,
					minDate: g.minDate,
					maxDate: g.maxDate,
					selectsRange: g.selectsRange,
					selectionMode: p,
					weekEndDay: m
				});
				case "month": return /* @__PURE__ */ r(M, {
					currentDate: w,
					value: g.value,
					onChange: t,
					minDate: g.minDate,
					maxDate: g.maxDate,
					selectsRange: g.selectsRange,
					selectsPeriod: p === "month" || !b("day", l),
					selectionMode: p,
					weekEndDay: m
				});
				case "year": return /* @__PURE__ */ r(F, {
					currentDate: w,
					value: g.value,
					onChange: i,
					minDate: g.minDate,
					maxDate: g.maxDate,
					selectsRange: g.selectsRange,
					selectsPeriod: p === "year" || !b("month", l),
					selectionMode: p,
					weekEndDay: m
				});
				default: return null;
			}
		})()]
	});
}
//#endregion
export { y as a, w as c, g as i, _ as n, b as o, v as r, x as s, L as t };

//# sourceMappingURL=CalendarView-DGElHRTJ.js.map