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

//# sourceMappingURL=index.js.map