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

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