import { n as e } from "./button-CC0Jdx1j.js";
import { useEffect as t, useEffectEvent as n, useState as r } from "react";
import { cn as i } from "@ryuzaki13/react-foundation-lib/utils";
import { addCalendarMonths as a, addCalendarYears as o, createCalendarDate as s, getCalendarPeriod as c, getStartOfDay as l, isCalendarPeriodWithinDateBounds as u, isDateInsideCalendarPeriod as d, isDateRangeTuple as f, isSameCalendarDay as p } from "@ryuzaki13/react-foundation-lib/formatters";
import { ChevronLeft as m, ChevronRight as h } from "lucide-react";
import { jsx as g, jsxs as _ } from "react/jsx-runtime";
//#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 = c(e, i), o = c(!r && t instanceof Date ? t : null, i);
	if (!r || !Array.isArray(t)) return {
		selected: d(e, o),
		range: !1,
		rangeStart: !1,
		rangeEnd: !1
	};
	let [s, l] = t, u = c(s, i), f = c(l ?? (s ? n : null), i), p = w(a, u), m = w(a, f);
	return {
		selected: !1,
		range: T(a, u, f) && !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: f, selectsRange: m, selectionMode: h = "day", weekEndDay: v = "sunday" }) => {
	let [y, b] = r(null), x = {
		selectionMode: h,
		weekEndDay: v
	}, S = (() => {
		let e = t.getFullYear(), n = t.getMonth(), r = s(e, n, 1), i = s(e, n + 1, 0), a = r.getDay() === 0 ? 6 : r.getDay() - 1, o = 42 - (a + i.getDate()), c = [], l = s(e, n, 0).getDate();
		for (let t = a; t > 0; --t) c.push(s(e, n - 1, l - t + 1));
		for (let t = 1; t <= i.getDate(); t += 1) c.push(s(e, n, t));
		for (let t = 1; t <= o; t += 1) c.push(s(e, n + 1, t));
		let u = [];
		for (let e = 0; e < c.length; e += 7) u.push(c.slice(e, e + 7));
		return u;
	})(), C = m && Array.isArray(n) ? n[0] : null, w = m && Array.isArray(n) ? n[1] : null, T = c(m ? null : n, x), E = c(y, x), A = w ?? (C ? E?.end ?? null : null), j = h === "week", M = j && (!m || !C), N = (e) => {
		if (!m || !C || !A) return !1;
		let t = l(e).getTime(), n = C.getTime(), r = A.getTime();
		return t >= Math.min(n, r) && t <= Math.max(n, r);
	}, P = (e) => !u(c(e, x), {
		minDate: o,
		maxDate: f
	}), F = (e, t) => {
		t || (j && b(e), !(!m || !C || w) && (j || b(e)));
	};
	return /* @__PURE__ */ _("div", {
		className: D.dayView,
		children: [/* @__PURE__ */ g("div", {
			className: D.weekdays,
			children: O.map((e) => /* @__PURE__ */ g("div", {
				className: D.weekday,
				children: e
			}, e))
		}), /* @__PURE__ */ g("div", {
			className: D.daysGrid,
			onMouseLeave: () => {
				(j || !w) && b(null);
			},
			children: S.map((n, r) => /* @__PURE__ */ g("div", {
				className: D.week,
				children: n.map((n, o) => {
					let s = n.getMonth() === t.getMonth(), c = d(n, T), l = M && d(n, E), u = N(n), f = p(n, C), m = p(n, A), _ = p(n, e), v = P(n);
					return /* @__PURE__ */ g("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]: !s,
							[D.periodSelection]: h === "week",
							[D.periodPreview]: l && !c,
							[D.selected]: c,
							[D.range]: u && !f && !m,
							[D.rangeStart]: f,
							[D.rangeEnd]: m,
							[D.disabled]: v,
							[D.weekend]: o > 4,
							[D.now]: _
						}),
						"aria-label": k.format(n),
						"aria-pressed": c || u || f || m,
						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: l, selectsPeriod: d, selectionMode: f = "day", weekEndDay: p = "sunday" }) => {
	let [m, h] = r(null), _ = Array.from({ length: 12 }, (e, t) => t), v = e.getFullYear(), y = {
		selectionMode: f,
		weekEndDay: p
	}, b = l && Array.isArray(t) ? t[0] : null, x = l && Array.isArray(t) ? t[1] : null, S = (e) => M.format(s(v, e, 1)), C = (e) => {
		if (!a && !o) return !1;
		let t = s(v, e, 1), n = s(v, e + 1, 0, 23, 59, 59);
		return d ? !u(c(t, y), {
			minDate: a,
			maxDate: o
		}) : !!(a && n < a || o && t > o);
	}, w = (e, t) => {
		!l || !b || x || t || h(e);
	};
	return /* @__PURE__ */ g("div", {
		className: j.monthView,
		children: /* @__PURE__ */ g("div", {
			className: j.monthsGrid,
			onMouseLeave: () => {
				x || h(null);
			},
			children: _.map((e) => {
				let r = C(e), a = s(v, e, 1), o = E({
					date: a,
					value: t,
					hoveredDate: m,
					selectsRange: l,
					selectionOptions: y
				});
				return /* @__PURE__ */ g("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: l, selectsPeriod: d, selectionMode: f = "day", weekEndDay: p = "sunday" }) => {
	let [m, h] = r(null), _ = Math.floor(e.getFullYear() / 12) * 12, v = Array.from({ length: 12 }, (e, t) => _ + t), y = {
		selectionMode: f,
		weekEndDay: p
	}, b = l && Array.isArray(t) ? t[0] : null, x = l && Array.isArray(t) ? t[1] : null, S = (e) => {
		if (!a && !o) return !1;
		let t = s(e, 0, 1), n = s(e, 11, 31, 23, 59, 59);
		return d ? !u(c(t, y), {
			minDate: a,
			maxDate: o
		}) : !!(a && n < a || o && t > o);
	}, C = (e, t) => {
		!l || !b || x || t || h(e);
	};
	return /* @__PURE__ */ g("div", {
		className: I.yearView,
		children: /* @__PURE__ */ g("div", {
			className: I.yearsGrid,
			onMouseLeave: () => {
				x || h(null);
			},
			children: v.map((e) => {
				let r = S(e), a = s(e, 0, 1), o = E({
					date: a,
					value: t,
					hoveredDate: m,
					selectsRange: l,
					selectionOptions: y
				});
				return /* @__PURE__ */ g("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: s, selectionMode: d = "day", weekEndDay: p = "sunday", onViewChange: v, ...y }) {
	let [b] = r(() => l(/* @__PURE__ */ new Date())), [x, C] = r(() => y.selectsRange && f(y.value) && y.value[0] instanceof Date ? y.value[0] : !y.selectsRange && y.value instanceof Date ? y.value : l(/* @__PURE__ */ new Date())), w = y.currentDate ?? x, T = {
		selectionMode: d,
		weekEndDay: p
	}, 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) => a(e, -1));
			return;
		}
		if (i === "month") {
			E((e) => o(e, -1));
			return;
		}
		E((e) => o(e, -12));
	}, k = () => {
		if (i === "day") {
			E((e) => a(e, 1));
			return;
		}
		if (i === "month") {
			E((e) => o(e, 1));
			return;
		}
		E((e) => o(e, 12));
	};
	return /* @__PURE__ */ _("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__ */ _("div", {
				className: F.calendarHeader,
				children: [
					/* @__PURE__ */ g(e, {
						icon: /* @__PURE__ */ g(m, {}),
						onClick: O,
						variant: "ghost",
						"aria-label": "Предыдущий",
						"data-action": "calendar-previous"
					}),
					/* @__PURE__ */ g(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__ */ g(e, {
						icon: /* @__PURE__ */ g(h, {}),
						onClick: k,
						variant: "ghost",
						"aria-label": "Следующий",
						"data-ui": "calendar-next-button",
						"data-action": "calendar-next"
					})
				]
			});
		})(), (() => {
			let e = (e) => {
				let t = c(e, T);
				if (!(!t || !u(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 = c(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), d === "month" || !S("day", s)) {
					e(t);
					return;
				}
				v("day");
			}, n = (t) => {
				if (E(t), d === "year" || !S("month", s)) {
					e(t);
					return;
				}
				v("month");
			};
			switch (i) {
				case "day": return /* @__PURE__ */ g(A, {
					now: b,
					currentDate: w,
					value: y.value,
					onChange: e,
					minDate: y.minDate,
					maxDate: y.maxDate,
					selectsRange: y.selectsRange,
					selectionMode: d,
					weekEndDay: p
				});
				case "month": return /* @__PURE__ */ g(P, {
					currentDate: w,
					value: y.value,
					onChange: t,
					minDate: y.minDate,
					maxDate: y.maxDate,
					selectsRange: y.selectsRange,
					selectsPeriod: d === "month" || !S("day", s),
					selectionMode: d,
					weekEndDay: p
				});
				case "year": return /* @__PURE__ */ g(L, {
					currentDate: w,
					value: y.value,
					onChange: n,
					minDate: y.minDate,
					maxDate: y.maxDate,
					selectsRange: y.selectsRange,
					selectsPeriod: d === "year" || !S("month", s),
					selectionMode: d,
					weekEndDay: p
				});
				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-C0gJvre1.js.map