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

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