import { r as e } from "./popover-IztNF45J.js";
import { t } from "./ui.module-DHVTOqZ0.js";
import { s as n } from "./input-CCdBtl-C.js";
import { n as r, o as i, s as a, t as o } from "./CalendarView-DGElHRTJ.js";
import { n as s } from "./radio-group-BAeJjyZ5.js";
import { useCallback as c, useEffect as l, useMemo as u, useRef as d, useState as f } from "react";
import { jsx as p, jsxs as m } from "react/jsx-runtime";
import { CalendarDaysIcon as ee } from "lucide-react";
import { DEFAULT_DATE_PRESET_NAMES as h, formatDate as g, formatDateRange as _, getCalendarPeriod as v, getDatePreset as y, isDateRangeTuple as b, parseDateByFormat as x, resolveDateFormatName as S } from "@ryuzaki13/react-foundation-lib/formatters";
//#region src/date-input/lib/dateInputValue.ts
var C = h.date, w = "dd.MM.yyyy", T = " - ";
function E(e) {
	return e.selectionMode === "day" || e.selectionMode === "week" ? "day" : e.selectionMode === "month" || e.selectionMode === "year" ? e.selectionMode : e.datePickerLevel ?? "day";
}
function D(e) {
	return {
		selectionMode: e?.selectionMode,
		weekEndDay: e?.weekEndDay
	};
}
function O(e, t) {
	return typeof e == "string" || e === void 0 ? {
		datePreset: e,
		datePickerLevel: t
	} : t && e.datePickerLevel === void 0 ? {
		...e,
		datePickerLevel: t
	} : e;
}
function k(e) {
	let t = S(e, C);
	return y(t) ? t : C;
}
function A(e, t) {
	return x(e, k(t.datePreset), {
		defaultFormat: C,
		precision: E(t),
		referenceDate: t.referenceDate
	})?.date ?? null;
}
function j(e, t) {
	return v(e, D(t))?.start ?? null;
}
function M(e, t) {
	if (!b(e)) return null;
	let [n, r] = e;
	return [j(n, t), N(r, t)];
}
function N(e, t) {
	return v(e, D(t))?.end ?? null;
}
function P(e) {
	return e?.getTime() ?? null;
}
function F(e, t, n) {
	return P(j(e, n)) === P(j(t, n));
}
function I(e, t, n) {
	let r = M(e, n), i = M(t, n);
	return P(r?.[0] ?? null) === P(i?.[0] ?? null) && P(r?.[1] ?? null) === P(i?.[1] ?? null);
}
function L(e, t, n) {
	if (!e) return "";
	let r = O(t, n), i = v(e, D(r));
	return r.selectionMode === "week" && i ? _(i.start, i.end, k(r.datePreset), { precision: E(r) }) : g(e, k(r.datePreset), { precision: E(r) });
}
function R(e, t, n) {
	if (!b(e)) return "";
	let r = O(t, n), [i, a] = e;
	return i && a ? _(i, a, k(r.datePreset), { precision: E(r) }) : i ? `${g(i, k(r.datePreset), { precision: E(r) })}${T}` : "";
}
function z(e, t, n) {
	return b(e) ? R(e, t, n) : L(e, t, n);
}
function B(e, t, n) {
	let r = O(t, n);
	return j(A(r.selectionMode === "week" ? e.split(T)[0] ?? "" : e, r), r);
}
function V(e, t, n) {
	let r = O(t, n), [i = "", a = ""] = e.split(T);
	return [B(i, r), H(a, r)];
}
function H(e, t, n) {
	let r = O(t, n);
	return N(A(e, r), r);
}
//#endregion
//#region src/date-input/model/useDateInput.ts
function U(e) {
	return b(e) ? e[0] ?? e[1] ?? void 0 : e ?? void 0;
}
var W = (e) => {
	let t = u(() => {
		let t = {
			selectionMode: e.selectionMode,
			weekEndDay: e.weekEndDay
		};
		return e.selectsRange ? M(e.value, t) : j(e.value, t);
	}, [
		e.selectsRange,
		e.selectionMode,
		e.value,
		e.weekEndDay
	]), n = u(() => ({
		datePreset: e.datePreset,
		datePickerLevel: e.datePickerLevel,
		selectionMode: e.selectionMode,
		weekEndDay: e.weekEndDay
	}), [
		e.datePickerLevel,
		e.datePreset,
		e.selectionMode,
		e.weekEndDay
	]), r = z(t, n), i = JSON.stringify([
		e.selectsRange,
		e.datePreset ?? null,
		e.datePickerLevel ?? null,
		e.selectionMode ?? null,
		e.weekEndDay ?? null,
		b(t) ? [t[0]?.getTime() ?? null, t[1]?.getTime() ?? null] : t?.getTime() ?? null
	]), [a, o] = f(() => ({
		sourceSignature: i,
		selectedDate: t,
		inputValue: r
	})), s = a.sourceSignature === i, c = s ? a.selectedDate : t, l = s ? a.inputValue : r, d = (e) => {
		o((n) => {
			let r = n.sourceSignature === i ? n.selectedDate : t;
			return {
				sourceSignature: i,
				selectedDate: r,
				inputValue: e
			};
		});
	}, p = (e) => {
		o((t) => {
			let n = t.sourceSignature === i ? t.inputValue : r;
			return {
				sourceSignature: i,
				selectedDate: e,
				inputValue: n
			};
		});
	}, m = (e) => z(e, n);
	return {
		inputValue: l,
		setInputValue: d,
		selectedDate: c,
		setSelectedDate: p,
		handleSelect: (t) => {
			let [r, a] = Array.isArray(t) ? t : [t, null];
			if (e.selectsRange) {
				let t = j(r, n), s = N(a, n);
				if (t && s) {
					if (s < t) {
						let t = [j(a, n), N(r, n)];
						o({
							sourceSignature: i,
							selectedDate: t,
							inputValue: m(t)
						}), e.onChange(t);
					} else {
						let n = [t, s];
						o({
							sourceSignature: i,
							selectedDate: n,
							inputValue: m(n)
						}), e.onChange(n);
					}
					return !0;
				}
				let c = [t, null];
				return o({
					sourceSignature: i,
					selectedDate: c,
					inputValue: m(c)
				}), !1;
			}
			let s = j(r, n);
			return o({
				sourceSignature: i,
				selectedDate: s,
				inputValue: m(s)
			}), e.onChange(s), !0;
		},
		formatDate: m,
		parseDate: (t) => {
			let r = {
				...n,
				referenceDate: U(c)
			};
			return e.selectsRange ? V(t, r) : B(t, r);
		}
	};
}, G = { selectionModeControl: "-tM-kj" }, K = {
	day: "День",
	week: "Неделя",
	month: "Месяц",
	year: "Год"
};
function q(e, t) {
	let n = new Set(e?.length ? e : r);
	return n.add(t), r.filter((e) => n.has(e));
}
function J({ placeholder: r, disabled: u, error: h, onClearError: g, minDate: _, maxDate: v, datePickerLevel: y, selectionMode: x, weekEndDay: S = "sunday", allowSelectionModeChange: C, selectionModeOptions: w, onSelectionModeChange: T, ...E }) {
	let [D, O] = f(!1), [k, A] = f(x ?? "day"), j = x ?? k, M = y ?? "day", N = a(j, M), [P, L] = f(() => ({
		view: N,
		selectionMode: j,
		minDatePickerLevel: M
	})), R = P.selectionMode === j && P.minDatePickerLevel === M && i(P.view, M) ? P.view : N, z = d(E.value), B = {
		selectionMode: j,
		weekEndDay: S
	}, { inputValue: V, setInputValue: H, selectedDate: U, setSelectedDate: J, handleSelect: Y, formatDate: X, parseDate: te } = W({
		...E,
		datePickerLevel: M,
		selectionMode: j,
		weekEndDay: S
	}), Z = E.selectsRange ? E.onChange : void 0, ne = c((e) => {
		i(e, M) && L({
			view: e,
			selectionMode: j,
			minDatePickerLevel: M
		});
	}, [M, j]);
	l(() => {
		z.current = E.value;
	}, [E.value]);
	let Q = () => {
		if (V) {
			let e = te(V);
			if (e) {
				if (H(X(e)), E.selectsRange && b(e)) {
					if (I(e, b(U) ? U : null, B)) return;
					J(e), E.onChange(e);
				} else if (!E.selectsRange && e instanceof Date) {
					if (F(e, U instanceof Date ? U : null, B)) return;
					J(e), E.onChange(e);
				}
			}
		}
	}, re = (e) => {
		e.key === "Enter" && (e.preventDefault(), Q());
	}, ie = c((e) => {
		Y(e) && O(!1);
	}, [Y]), ae = c((e) => {
		x === void 0 && A(e), T?.(e), L({
			view: a(e, M),
			selectionMode: e,
			minDatePickerLevel: M
		});
	}, [
		x,
		M,
		T
	]), oe = c(() => {
		if (Z) {
			let [, e] = b(U) ? U : [null, null];
			if (!e) {
				let e = z.current, t = Array.isArray(e) ? e : null, n = t?.[0] ?? null, r = t?.[1] ?? null;
				if (n && r) {
					let e = [n, r];
					J(e), H(X(e)), Z(e);
				} else J([null, null]), H(""), Z([null, null]);
			}
		}
	}, [
		Z,
		U,
		J,
		H,
		X
	]), $ = q(w, j), se = C && $.length > 1;
	return /* @__PURE__ */ m(e, {
		open: D,
		onOpenChange: (e) => {
			O(e), e || oe();
		},
		children: [/* @__PURE__ */ p(n, {
			label: E.label,
			description: E.description,
			size: E.size,
			value: V,
			onChange: H,
			onBlur: Q,
			onKeyUp: re,
			placeholder: r,
			disabled: u,
			error: h,
			onClearError: g,
			endAdornmentWidth: "var(--control-height)",
			endAdornment: /* @__PURE__ */ p(e.Trigger, { children: /* @__PURE__ */ p("div", {
				className: t.uiToggleButtonContainer,
				children: /* @__PURE__ */ p("button", {
					type: "button",
					disabled: u,
					"aria-label": "Открыть календарь",
					className: t.uiToggleButton,
					children: /* @__PURE__ */ p(ee, {})
				})
			}) })
		}), /* @__PURE__ */ m(e.Content, {
			background: "primary",
			children: [/* @__PURE__ */ p(o, {
				view: R,
				datePickerLevel: M,
				selectionMode: j,
				weekEndDay: S,
				onViewChange: ne,
				value: U ?? null,
				onChange: ie,
				minDate: _,
				maxDate: v,
				selectsRange: !!E.selectsRange
			}), se && /* @__PURE__ */ p("div", {
				className: G.selectionModeControl,
				children: /* @__PURE__ */ p(s, {
					value: j,
					onChange: ae,
					noWrap: !0,
					"aria-label": "Режим выбора даты",
					children: $.map((e) => /* @__PURE__ */ p(s.Option, {
						value: e,
						label: K[e]
					}, e))
				})
			})]
		})]
	});
}
function Y(e) {
	return /* @__PURE__ */ p(J, { ...e });
}
function X(e) {
	return /* @__PURE__ */ p(J, {
		...e,
		selectsRange: !0
	});
}
//#endregion
export { I as a, R as c, N as d, j as f, B as h, C as i, L as l, H as m, Y as n, F as o, V as p, w as r, z as s, X as t, M as u };

//# sourceMappingURL=date-input-C4GcKul6.js.map