import { r as e } from "../chunks/popover-IztNF45J.js";
import { t } from "../chunks/ui.module-DHVTOqZ0.js";
import { c as n, d as r, i } from "../chunks/input-CCdBtl-C.js";
import { t as a } from "../chunks/CalendarView-DGElHRTJ.js";
import { t as o } from "../chunks/wheel-picker-FS58cRsQ.js";
import { useCallback as s, useMemo as c, useRef as l, useState as u } from "react";
import { cn as d } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as f, jsxs as p } from "react/jsx-runtime";
import { CalendarDaysIcon as m, Clock3Icon as h } from "lucide-react";
import { createCalendarDate as g, getStartOfDay as _, isDateRangeTuple as v, parseDate as y } from "@ryuzaki13/react-foundation-lib/formatters";
import { areAllDateSegmentsEmpty as b, dateToIndexedSegmentValues as x, indexedSegmentsToDate as S, parseDateSegmentMask as C } from "@ryuzaki13/react-foundation-lib/date-segments";
//#region src/date-time-input/lib/dateTimeValue.ts
var w = "dd.MM.yyyy HH:mm", T = "HH:mm", E = 1970, D = 0, O = 1;
C(T);
function k(e = 0, t = 0) {
	return g(E, D, O, e, t, 0, 0);
}
function A(e = "date-time") {
	return e === "time" ? T : w;
}
function j(e, t = "date-time") {
	if (e == null) return null;
	let n = y(e);
	return n ? t === "time" ? k(n.getHours(), n.getMinutes()) : n : null;
}
function M(e, t, n, r = "date-time") {
	let i = j(e, r);
	if (!i) return !1;
	if (r === "time") {
		let e = i.getHours() * 60 + i.getMinutes(), r = t ? j(t, "time") : null, a = n ? j(n, "time") : null;
		return !(r && e < r.getHours() * 60 + r.getMinutes() || a && e > a.getHours() * 60 + a.getMinutes());
	}
	return !(t && i < t || n && i > n);
}
function N(e, t) {
	let n = j(t), r = n?.getHours() ?? 0, i = n?.getMinutes() ?? 0;
	return g(e.getFullYear(), e.getMonth(), e.getDate(), r, i, 0, 0);
}
function P(e, t, n, r = "date-time") {
	return r === "time" ? k(t, n) : g(e.getFullYear(), e.getMonth(), e.getDate(), t, n, 0, 0);
}
function F(e, t, n = "date-time") {
	return n === "time" ? j(e, "time") ?? k() : j(e, n) ?? t;
}
function I(e, t, n, r = "date-time") {
	let i = j(e, r) ?? e, a = t ? j(t, r) : null, o = n ? j(n, r) : null, s = !!a && i.getFullYear() === a.getFullYear() && i.getMonth() === a.getMonth() && i.getDate() === a.getDate(), c = !!o && i.getFullYear() === o.getFullYear() && i.getMonth() === o.getMonth() && i.getDate() === o.getDate(), l = s ? a.getHours() : 0, u = c ? o.getHours() : 23, d = s && l === u ? a.getMinutes() : 0, f = c && l === u ? o.getMinutes() : 59, p = l > u || l === u && d > f;
	return {
		disabled: p,
		minHour: l,
		maxHour: u,
		minMinuteByHour: (e) => p ? 0 : s && e === l ? a.getMinutes() : 0,
		maxMinuteByHour: (e) => p ? 59 : c && e === u ? o.getMinutes() : 59
	};
}
function L(e, t, n, r, i, a = "date-time") {
	let o = I(e, r, i, a);
	if (o.disabled) return {
		hours: 0,
		minutes: 0
	};
	let s = Math.min(Math.max(t, o.minHour), o.maxHour), c = o.minMinuteByHour(s), l = o.maxMinuteByHour(s);
	return {
		hours: s,
		minutes: Math.min(Math.max(n, c), l)
	};
}
//#endregion
//#region src/date-time-input/lib/timePanelState.ts
function R(e, t, n, r = "date-time") {
	let i = I(e, t, n, r), a = e.getHours(), o = e.getMinutes(), s = i.minMinuteByHour(a), c = i.maxMinuteByHour(a);
	return i.disabled ? {
		disabled: !0,
		selectedHour: a,
		selectedMinute: o,
		hours: [],
		minutes: []
	} : {
		disabled: !1,
		selectedHour: a,
		selectedMinute: o,
		hours: Array.from({ length: i.maxHour - i.minHour + 1 }, (e, t) => i.minHour + t),
		minutes: Array.from({ length: c - s + 1 }, (e, t) => s + t)
	};
}
//#endregion
//#region src/date-time-input/model/useDateTimeSegments.ts
function z(e) {
	let t = [];
	return e.forEach((e, n) => {
		e.kind === "editable" && t.push(n);
	}), t;
}
function B({ value: e, onChange: t, onClearError: n, disabled: r, minDate: i, maxDate: a, mode: o = "date-time" }) {
	let d = c(() => A(o), [o]), f = c(() => C(d), [d]), p = c(() => z(f), [f]), m = c(() => x(e ?? null, f), [f, e]), h = e?.getTime() ?? null, [g, _] = u(null), v = (g?.sourceValueTime === h && g.sourceMode === o ? g.values : null) ?? m, y = l(/* @__PURE__ */ new Map()), w = l(0), T = s((e) => {
		_(e === null ? null : {
			sourceValueTime: h,
			sourceMode: o,
			values: e
		});
	}, [h, o]), E = s((n) => {
		if (b(f, n)) return t(null), null;
		let r = S(f, n, { defaultDate: o === "time" ? j(e, "time") ?? k() : void 0 });
		return !r || !M(r, i, a, o) ? null : (t(r), r);
	}, [
		a,
		i,
		o,
		t,
		f,
		e
	]), D = s((e, t = !0) => {
		let n = y.current.get(e);
		n && (n.focus(), t && requestAnimationFrame(() => n.select()));
	}, []), O = s((e) => {
		let t = p.indexOf(e);
		return t < p.length - 1 ? p[t + 1] : null;
	}, [p]), N = s((e) => {
		let t = p.indexOf(e);
		return t > 0 ? p[t - 1] : null;
	}, [p]), P = s((e, t) => e.padStart(t.length, "0"), []);
	return {
		segments: f,
		segmentValues: v,
		registerRef: s((e) => (t) => {
			if (t) {
				y.current.set(e, t);
				return;
			}
			y.current.delete(e);
		}, []),
		handleInput: s((e) => (t) => {
			if (r) return;
			let i = f[e];
			if (i.kind !== "editable") return;
			let a = t.target.value.replace(/\D/g, "").slice(0, i.length), o = new Map(v);
			if (o.set(e, a), T(o), n?.(), a.length === i.length) {
				let t = O(e);
				t !== null && requestAnimationFrame(() => D(t));
			}
			E(o);
		}, [
			r,
			D,
			O,
			n,
			v,
			f,
			T,
			E
		]),
		handleKeyDown: s((e) => (t) => {
			if (r) return;
			let n = f[e];
			if (n.kind !== "editable") return;
			let i = v.get(e) ?? "", a = t.currentTarget;
			switch (t.key) {
				case "ArrowRight":
					if (a.selectionStart === i.length || i === "") {
						t.preventDefault();
						let n = O(e);
						n !== null && D(n);
					}
					break;
				case "ArrowLeft":
					if (a.selectionStart === 0 || i === "") {
						t.preventDefault();
						let n = N(e);
						n !== null && D(n);
					}
					break;
				case "ArrowUp": {
					t.preventDefault();
					let r = i === "" ? n.min : Number.parseInt(i, 10) + 1;
					r > n.max && (r = n.min);
					let o = new Map(v);
					o.set(e, P(String(r), n)), T(o), E(o), requestAnimationFrame(() => a.select());
					break;
				}
				case "ArrowDown": {
					t.preventDefault();
					let r = i === "" ? n.max : Number.parseInt(i, 10) - 1;
					r < n.min && (r = n.max);
					let o = new Map(v);
					o.set(e, P(String(r), n)), T(o), E(o), requestAnimationFrame(() => a.select());
					break;
				}
				case "Backspace":
					if (i === "") {
						t.preventDefault();
						let n = N(e);
						n !== null && D(n);
					}
					break;
				case "Delete": {
					t.preventDefault();
					let n = new Map(v);
					n.set(e, ""), T(n), E(n);
					break;
				}
			}
		}, [
			r,
			D,
			O,
			N,
			P,
			v,
			f,
			T,
			E
		]),
		handleFocus: s(() => (e) => {
			e.target.select();
		}, []),
		handleContainerFocus: s(() => {
			clearTimeout(w.current);
		}, []),
		handleContainerBlur: s(() => {
			let n = new Map(v);
			w.current = window.setTimeout(() => {
				if (b(f, n)) {
					t(null), T(n);
					return;
				}
				let r = S(f, n, { defaultDate: o === "time" ? j(e, "time") ?? k() : void 0 });
				if (!r || !M(r, i, a, o)) {
					T(null);
					return;
				}
				T(n);
			}, 0);
		}, [
			a,
			i,
			o,
			t,
			v,
			f,
			T,
			e
		]),
		focusFirstEmpty: s(() => {
			let e = p.find((e) => !(v.get(e) ?? "")) ?? p[p.length - 1];
			e !== void 0 && D(e);
		}, [
			p,
			D,
			v
		])
	};
}
var V = {
	segmentInput: "AvG-mf",
	separator: "ig7KtT",
	popoverContent: "YQHPZa",
	bottom: "-RZe-i",
	right: "Ox0SRE",
	calendarPane: "gfaRup",
	timePanelPane: "_59kEea",
	timeOnlyPopoverContent: "Jjdu1Q",
	timePanelPaneBottom: "Q8Y2xS",
	timePanelPaneRight: "XxZYTW"
}, H = {
	timePanel: "_5-SSnO",
	hint: "s1NHua",
	fields: "KQC9-8",
	field: "WDu02W",
	label: "_9mEhfD",
	wheel: "N-Fzjk"
};
//#endregion
//#region src/date-time-input/ui/TimePanel.tsx
function U({ value: e, disabled: t, minDate: n, maxDate: r, mode: i = "date-time", onChange: a }) {
	let s = R(e, n, r, i), c = (e) => String(e).padStart(2, "0");
	return s.disabled ? /* @__PURE__ */ f("div", {
		className: H.timePanel,
		children: /* @__PURE__ */ f("p", {
			className: H.hint,
			children: "Для выбранного дня нет доступного времени в заданном диапазоне."
		})
	}) : /* @__PURE__ */ f("div", {
		className: H.timePanel,
		children: /* @__PURE__ */ p("div", {
			className: H.fields,
			children: [/* @__PURE__ */ p("label", {
				className: H.field,
				children: [/* @__PURE__ */ f("span", {
					className: H.label,
					children: "Часы"
				}), /* @__PURE__ */ f(o, {
					className: H.wheel,
					items: s.hours,
					value: s.selectedHour,
					onChange: (e) => a(e, s.selectedMinute),
					ariaLabel: "Выбор часов",
					disabled: t,
					formatItem: c
				})]
			}), /* @__PURE__ */ p("label", {
				className: H.field,
				children: [/* @__PURE__ */ f("span", {
					className: H.label,
					children: "Минуты"
				}), /* @__PURE__ */ f(o, {
					className: H.wheel,
					items: s.minutes,
					value: s.selectedMinute,
					onChange: (e) => a(s.selectedHour, e),
					ariaLabel: "Выбор минут",
					disabled: t,
					formatItem: c
				})]
			})]
		})
	});
}
//#endregion
//#region src/date-time-input/ui/SingleDateTimeInput.tsx
function W({ value: o, onChange: s, label: l, description: g, placeholder: y, disabled: b, size: x, error: S, onClearError: C, minDate: w, maxDate: T, timePanelPlacement: E = "right", mode: D = "date-time" }) {
	let O = c(() => j(o, D), [D, o]), k = c(() => _(/* @__PURE__ */ new Date()), []), [A, I] = u(!1), [R, z] = u(O ?? null), [H, W] = u("day"), [G, K] = u(() => F(O, k, D)), { controlId: q, labelId: J, descriptionId: Y, errorId: X, describedBy: Z } = r({
		hasLabel: l != null,
		hasDescription: !!g,
		hasError: !!S
	}), { segments: ee, segmentValues: te, registerRef: ne, handleInput: re, handleKeyDown: ie, handleFocus: ae, handleContainerFocus: oe, handleContainerBlur: se, focusFirstEmpty: ce } = B({
		value: O,
		onChange: s,
		onClearError: C,
		disabled: b,
		minDate: w,
		maxDate: T,
		mode: D
	}), le = (e) => {
		if (D === "time" || v(e) || b) return;
		let t = N(e, R ?? O ?? G), { hours: n, minutes: r } = L(e, t.getHours(), t.getMinutes(), w, T, D), i = P(e, n, r);
		M(i, w, T, D) && (C?.(), z(i), K(i));
	}, Q = (e, t) => {
		if (b) return;
		let n = R ?? O ?? G, r = L(n, e, t, w, T, D), i = P(n, r.hours, r.minutes, D);
		M(i, w, T, D) && (C?.(), z(i), K(i));
	}, ue = (e) => {
		e && (z(O ?? null), K(F(O, k, D))), I(e);
	}, de = () => {
		(O?.getTime() ?? null) !== (R?.getTime() ?? null) && s(R);
	}, fe = E === "right" ? V.timePanelPaneRight : V.timePanelPaneBottom, pe = D === "time" ? "Открыть выбор времени" : "Открыть календарь даты и времени", me = y ?? (D === "time" ? "чч:мм" : "дд.мм.гггг чч:мм"), $ = R ?? O ?? G;
	return /* @__PURE__ */ p(e, {
		open: A,
		onOpenChange: ue,
		children: [/* @__PURE__ */ f(n, {
			label: l,
			description: g,
			disabled: b,
			size: x,
			error: S,
			controlId: q,
			labelId: J,
			descriptionId: Y,
			errorId: X,
			children: /* @__PURE__ */ f(i, {
				endAdornmentWidth: "var(--control-height)",
				endAdornment: /* @__PURE__ */ f(e.Trigger, { children: /* @__PURE__ */ f("div", {
					className: t.uiToggleButtonContainer,
					children: /* @__PURE__ */ f("button", {
						type: "button",
						disabled: b,
						"aria-label": pe,
						className: t.uiToggleButton,
						children: f(D === "time" ? h : m, {})
					})
				}) }),
				children: ({ controlClassName: e }) => /* @__PURE__ */ f("div", {
					id: q,
					className: d(t.uiInputControlFake, e),
					"data-disabled": b || void 0,
					"data-invalid": S ? "" : void 0,
					role: "group",
					"aria-invalid": !!S || void 0,
					"aria-placeholder": me,
					"aria-labelledby": J,
					"aria-describedby": Z,
					onFocus: oe,
					onBlur: se,
					onClick: (e) => {
						e.target === e.currentTarget && ce();
					},
					children: ee.map((e, t) => e.kind === "literal" ? /* @__PURE__ */ f("span", {
						className: V.separator,
						"data-disabled": b || void 0,
						children: e.text
					}, t) : /* @__PURE__ */ f("input", {
						ref: ne(t),
						type: "text",
						inputMode: "numeric",
						className: V.segmentInput,
						style: { width: `${e.length + .5}ch` },
						value: te.get(t) ?? "",
						placeholder: e.placeholder,
						maxLength: e.length,
						disabled: b,
						"aria-label": e.ariaLabel,
						autoComplete: "off",
						onChange: re(t),
						onKeyDown: ie(t),
						onFocus: ae()
					}, t))
				})
			})
		}), /* @__PURE__ */ f(e.Content, {
			background: "primary",
			onClose: de,
			children: D === "time" ? /* @__PURE__ */ f("div", {
				className: V.timeOnlyPopoverContent,
				children: /* @__PURE__ */ f(U, {
					value: $,
					disabled: b,
					minDate: w,
					maxDate: T,
					mode: D,
					onChange: Q
				})
			}) : /* @__PURE__ */ p("div", {
				className: d(V.popoverContent, V[E]),
				children: [/* @__PURE__ */ f("div", {
					className: V.calendarPane,
					children: /* @__PURE__ */ f(a, {
						view: H,
						datePickerLevel: "day",
						selectionMode: "day",
						onViewChange: W,
						value: $,
						onChange: le,
						minDate: w,
						maxDate: T,
						currentDate: G,
						onCurrentDateChange: K
					})
				}), /* @__PURE__ */ f("div", {
					className: d(V.timePanelPane, fe),
					children: /* @__PURE__ */ f(U, {
						value: $,
						disabled: b,
						minDate: w,
						maxDate: T,
						mode: D,
						onChange: Q
					})
				})]
			})
		})]
	});
}
//#endregion
export { W as SingleDateTimeInput };

//# sourceMappingURL=index.js.map