import { t as e } from "../../utils-B7XIe3qq.js";
import { Calendar as t } from "./calendar.js";
import * as n from "react";
import { jsx as r, jsxs as i } from "react/jsx-runtime";
//#region src/react/primitives/date-picker.tsx
function a(e, t, n) {
	if (t === "YYYY-MM-DD") return e.toISOString().split("T")[0] ?? "";
	let r = n ?? (typeof navigator < "u" ? navigator.language : "en-US");
	return new Intl.DateTimeFormat(r, {
		year: "numeric",
		month: "2-digit",
		day: "2-digit"
	}).format(e);
}
function o(e) {
	let t = e.trim();
	if (t === "") return null;
	let n = t.replace(/\D/g, "");
	if (n.length >= 8) {
		let e = parseInt(n.substring(0, 4), 10), t = parseInt(n.substring(4, 6), 10), r = parseInt(n.substring(6, 8), 10);
		if (!isNaN(e) && t >= 1 && t <= 12 && r >= 1 && r <= 31) {
			let n = new Date(e, t - 1, r);
			if (!isNaN(n.getTime())) return n;
		}
	}
	let r = new Date(t);
	return isNaN(r.getTime()) ? null : r;
}
var s = () => /* @__PURE__ */ i("svg", {
	width: "16",
	height: "16",
	viewBox: "0 0 16 16",
	fill: "none",
	children: [/* @__PURE__ */ r("rect", {
		x: "2",
		y: "3",
		width: "12",
		height: "11",
		rx: "1.5",
		stroke: "currentColor",
		strokeWidth: "1.2"
	}), /* @__PURE__ */ r("path", {
		d: "M2 6.5h12M5.5 2v2M10.5 2v2",
		stroke: "currentColor",
		strokeWidth: "1.2",
		strokeLinecap: "round"
	})]
}), c = () => /* @__PURE__ */ r("svg", {
	width: "14",
	height: "14",
	viewBox: "0 0 14 14",
	fill: "none",
	children: /* @__PURE__ */ r("path", {
		d: "M10.5 3.5L3.5 10.5M3.5 3.5L10.5 10.5",
		stroke: "currentColor",
		strokeWidth: "1.5",
		strokeLinecap: "round"
	})
});
function l({ value: l, format: u = "YYYY-MM-DD", placeholder: d, minDate: f, maxDate: p, disabledDates: m, weekStart: h, locale: g, className: _, onChange: v }) {
	let [y, b] = n.useState(!1), [x, S] = n.useState(l ? a(l, u, g) : ""), C = n.useRef(null);
	n.useEffect(() => {
		S(l ? a(l, u, g) : "");
	}, [
		l,
		u,
		g
	]), n.useEffect(() => {
		if (!y) return;
		function e(e) {
			C.current && !C.current.contains(e.target) && b(!1);
		}
		return document.addEventListener("mousedown", e), () => document.removeEventListener("mousedown", e);
	}, [y]);
	function w() {
		let e = o(x);
		e && (S(a(e, u, g)), v?.(e));
	}
	function T(e) {
		S(a(e, u, g)), v?.(e), b(!1);
	}
	function E(e) {
		e.stopPropagation(), S(""), v?.(null);
	}
	return /* @__PURE__ */ i("div", {
		ref: C,
		"data-slot": "date-picker",
		className: e("relative inline-block w-full", _),
		children: [/* @__PURE__ */ i("div", {
			className: "relative flex items-center",
			children: [/* @__PURE__ */ r("input", {
				type: "text",
				role: "combobox",
				"aria-haspopup": "dialog",
				"aria-expanded": y,
				"aria-label": "Date",
				autoComplete: "off",
				placeholder: d ?? u.toLowerCase(),
				className: e("border-border-glass bg-glass text-foreground h-10 w-full rounded-xl border px-4 pr-20 py-2 text-[13px] backdrop-blur-[20px]", "placeholder:text-foreground-muted", "transition-all duration-[250ms] outline-none", "focus:border-primary focus:shadow-glow"),
				value: x,
				onChange: (e) => S(e.target.value),
				onBlur: w,
				onKeyDown: (e) => {
					e.key === "Enter" && w(), e.key === "Escape" && y && (e.preventDefault(), b(!1)), e.key === "ArrowDown" && !y && (e.preventDefault(), b(!0));
				},
				onFocus: () => b(!0)
			}), /* @__PURE__ */ i("div", {
				className: "absolute right-1 flex items-center gap-0.5",
				children: [x && /* @__PURE__ */ r("button", {
					type: "button",
					"aria-label": "Clear date",
					onClick: E,
					className: "inline-flex h-8 w-8 items-center justify-center rounded-lg text-foreground-muted transition-colors hover:text-foreground",
					children: /* @__PURE__ */ r(c, {})
				}), /* @__PURE__ */ r("button", {
					type: "button",
					"aria-label": "Open calendar",
					onClick: () => b((e) => !e),
					className: "inline-flex h-8 w-8 items-center justify-center rounded-lg text-foreground-muted transition-colors hover:text-foreground",
					children: /* @__PURE__ */ r(s, {})
				})]
			})]
		}), y && /* @__PURE__ */ r("div", {
			role: "dialog",
			"aria-modal": "false",
			"aria-label": "Choose date",
			className: e("absolute left-0 z-50 mt-2 rounded-xl border border-border-glass bg-glass p-3 shadow-lg backdrop-blur-[20px]", "animate-in fade-in-0 zoom-in-95 slide-in-from-top-2"),
			children: /* @__PURE__ */ r(t, {
				value: l,
				mode: "single",
				...f === void 0 ? {} : { minDate: f },
				...p === void 0 ? {} : { maxDate: p },
				...m === void 0 ? {} : { disabledDates: m },
				...h === void 0 ? {} : { weekStart: h },
				onChange: T
			})
		})]
	});
}
function u() {
	return [
		{
			label: "Last 7 days",
			getRange: () => {
				let e = /* @__PURE__ */ new Date(), t = /* @__PURE__ */ new Date();
				return t.setDate(t.getDate() - 6), [t, e];
			}
		},
		{
			label: "Last 30 days",
			getRange: () => {
				let e = /* @__PURE__ */ new Date(), t = /* @__PURE__ */ new Date();
				return t.setDate(t.getDate() - 29), [t, e];
			}
		},
		{
			label: "Last 90 days",
			getRange: () => {
				let e = /* @__PURE__ */ new Date(), t = /* @__PURE__ */ new Date();
				return t.setDate(t.getDate() - 89), [t, e];
			}
		},
		{
			label: "Year to date",
			getRange: () => {
				let e = /* @__PURE__ */ new Date();
				return [new Date(e.getFullYear(), 0, 1), e];
			}
		}
	];
}
function d({ startDate: o, endDate: c, format: l = "YYYY-MM-DD", presets: d, minDate: f, maxDate: p, locale: m, className: h, onChange: g }) {
	let [_, v] = n.useState(!1), [y, b] = n.useState(o ?? null), [x, S] = n.useState(c ?? null), C = n.useRef(null);
	n.useEffect(() => {
		b(o ?? null), S(c ?? null);
	}, [o, c]), n.useEffect(() => {
		if (!_) return;
		function e(e) {
			C.current && !C.current.contains(e.target) && v(!1);
		}
		return document.addEventListener("mousedown", e), () => document.removeEventListener("mousedown", e);
	}, [_]);
	let w = n.useMemo(() => o && c ? `${a(o, l, m)} \u2014 ${a(c, l, m)}` : o ? `${a(o, l, m)} \u2014 ...` : "", [
		o,
		c,
		l,
		m
	]), T = d ?? u();
	function E() {
		b(o ?? null), S(c ?? null), v(!0);
	}
	function D() {
		g?.(y, x), v(!1);
	}
	function O(e) {
		let [t, n] = e.getRange();
		b(t), S(n);
	}
	let k = {
		mode: "range",
		rangeStart: y,
		rangeEnd: x,
		...f === void 0 ? {} : { minDate: f },
		...p === void 0 ? {} : { maxDate: p },
		onRangeChange: (e, t) => {
			b(e), S(t);
		}
	};
	return /* @__PURE__ */ i("div", {
		ref: C,
		"data-slot": "date-range-picker",
		className: e("relative inline-block w-full", h),
		children: [/* @__PURE__ */ i("div", {
			className: "relative flex items-center",
			children: [/* @__PURE__ */ r("input", {
				type: "text",
				readOnly: !0,
				role: "combobox",
				"aria-haspopup": "dialog",
				"aria-expanded": _,
				"aria-label": "Date range",
				placeholder: "Select date range",
				className: e("border-border-glass bg-glass text-foreground h-10 w-full cursor-pointer rounded-xl border px-4 pr-12 py-2 text-[13px] backdrop-blur-[20px]", "placeholder:text-foreground-muted", "transition-all duration-[250ms] outline-none", "focus:border-primary focus:shadow-glow"),
				value: w,
				onClick: () => _ ? v(!1) : E(),
				onKeyDown: (e) => {
					e.key === "Escape" && _ && (e.preventDefault(), v(!1));
				}
			}), /* @__PURE__ */ r("button", {
				type: "button",
				"aria-label": "Open date range picker",
				onClick: (e) => {
					e.stopPropagation(), _ ? v(!1) : E();
				},
				className: "absolute right-2 inline-flex h-8 w-8 items-center justify-center rounded-lg text-foreground-muted transition-colors hover:text-foreground",
				children: /* @__PURE__ */ r(s, {})
			})]
		}), _ && /* @__PURE__ */ i("div", {
			role: "dialog",
			"aria-modal": "false",
			"aria-label": "Choose date range",
			className: e("absolute left-0 z-50 mt-2 rounded-xl border border-border-glass bg-glass p-4 shadow-lg backdrop-blur-[20px]", "animate-in fade-in-0 zoom-in-95 slide-in-from-top-2"),
			children: [/* @__PURE__ */ i("div", {
				className: "flex gap-4",
				children: [/* @__PURE__ */ r("div", {
					className: "flex min-w-[140px] flex-col gap-1",
					children: T.map((t) => /* @__PURE__ */ r("button", {
						type: "button",
						className: e("rounded-lg px-3 py-2 text-left text-[12px] transition-all duration-200", "text-foreground-secondary hover:bg-glass-hover hover:text-foreground"),
						onClick: () => O(t),
						children: t.label
					}, t.label))
				}), /* @__PURE__ */ i("div", {
					className: "flex gap-4",
					children: [/* @__PURE__ */ r(t, { ...k }), /* @__PURE__ */ r(t, { ...k })]
				})]
			}), /* @__PURE__ */ i("div", {
				className: "mt-3 flex justify-end gap-2 border-t border-border-glass pt-3",
				children: [/* @__PURE__ */ r("button", {
					type: "button",
					className: e("rounded-xl px-4 py-2 text-[13px] transition-all duration-200", "text-foreground-secondary hover:bg-glass-hover hover:text-foreground"),
					onClick: () => v(!1),
					children: "Cancel"
				}), /* @__PURE__ */ r("button", {
					type: "button",
					className: e("rounded-xl bg-primary px-4 py-2 text-[13px] font-medium text-white shadow-glow transition-all duration-200", "hover:brightness-110"),
					onClick: D,
					children: "Apply"
				})]
			})]
		})]
	});
}
function f(e) {
	return String(e).padStart(2, "0");
}
function p({ value: o, format: c = "YYYY-MM-DD", placeholder: l, minDate: u, maxDate: d, disabledDates: p, weekStart: m, locale: h, use24Hour: g = !1, className: _, onChange: v }) {
	let [y, b] = n.useState(!1), [x, S] = n.useState(o ?? null), [C, w] = n.useState(o ? o.getHours() : 0), [T, E] = n.useState(o ? o.getMinutes() : 0), D = n.useRef(null);
	n.useEffect(() => {
		S(o ?? null), o && (w(o.getHours()), E(o.getMinutes()));
	}, [o]), n.useEffect(() => {
		if (!y) return;
		function e(e) {
			D.current && !D.current.contains(e.target) && b(!1);
		}
		return document.addEventListener("mousedown", e), () => document.removeEventListener("mousedown", e);
	}, [y]);
	function O(e, t, n) {
		if (!e) {
			v?.(null);
			return;
		}
		let r = new Date(e.getFullYear(), e.getMonth(), e.getDate(), t, n, 0);
		v?.(r);
	}
	function k(e) {
		S(e), O(e, C, T);
	}
	function A(e) {
		let t = parseInt(e.target.value, 10);
		!isNaN(t) && t >= (g ? 0 : 1) && t <= (g ? 23 : 12) && (w(t), O(x, t, T));
	}
	function j(e) {
		let t = parseInt(e.target.value, 10);
		!isNaN(t) && t >= 0 && t <= 59 && (E(t), O(x, C, t));
	}
	let M = n.useMemo(() => x ? `${a(x, c, h)} ${`${f(C)}:${f(T)}`}` : "", [
		x,
		C,
		T,
		c,
		h
	]), N = e("border-border-glass bg-glass h-8 w-14 rounded-lg border px-2 text-center text-[13px] text-foreground", "outline-none transition-all duration-200 focus:border-primary focus:shadow-glow backdrop-blur-[20px]");
	return /* @__PURE__ */ i("div", {
		ref: D,
		"data-slot": "date-time-picker",
		className: e("relative inline-block w-full", _),
		children: [/* @__PURE__ */ i("div", {
			className: "relative flex items-center",
			children: [/* @__PURE__ */ r("input", {
				type: "text",
				readOnly: !0,
				role: "combobox",
				"aria-haspopup": "dialog",
				"aria-expanded": y,
				"aria-label": "Date and time",
				placeholder: l ?? `${c.toLowerCase()} hh:mm`,
				className: e("border-border-glass bg-glass text-foreground h-10 w-full cursor-pointer rounded-xl border px-4 pr-12 py-2 text-[13px] backdrop-blur-[20px]", "placeholder:text-foreground-muted", "transition-all duration-[250ms] outline-none", "focus:border-primary focus:shadow-glow"),
				value: M,
				onClick: () => b((e) => !e),
				onKeyDown: (e) => {
					e.key === "Escape" && y && (e.preventDefault(), b(!1));
				}
			}), /* @__PURE__ */ r("button", {
				type: "button",
				"aria-label": "Open date time picker",
				onClick: (e) => {
					e.stopPropagation(), b((e) => !e);
				},
				className: "absolute right-2 inline-flex h-8 w-8 items-center justify-center rounded-lg text-foreground-muted transition-colors hover:text-foreground",
				children: /* @__PURE__ */ r(s, {})
			})]
		}), y && /* @__PURE__ */ i("div", {
			role: "dialog",
			"aria-modal": "false",
			"aria-label": "Choose date and time",
			className: e("absolute left-0 z-50 mt-2 rounded-xl border border-border-glass bg-glass p-3 shadow-lg backdrop-blur-[20px]", "animate-in fade-in-0 zoom-in-95 slide-in-from-top-2"),
			children: [/* @__PURE__ */ r(t, {
				value: x,
				mode: "single",
				minDate: u,
				maxDate: d,
				disabledDates: p,
				weekStart: m,
				onChange: k
			}), /* @__PURE__ */ i("div", {
				className: "mt-3 flex items-center justify-center gap-1 border-t border-border-glass pt-3",
				children: [
					/* @__PURE__ */ r("span", {
						className: "mr-2 text-[12px] text-foreground-muted",
						children: "Time"
					}),
					/* @__PURE__ */ r("input", {
						type: "number",
						"aria-label": "Hours",
						className: N,
						min: g ? 0 : 1,
						max: g ? 23 : 12,
						value: f(C),
						onChange: A
					}),
					/* @__PURE__ */ r("span", {
						className: "text-foreground-muted",
						"aria-hidden": "true",
						children: ":"
					}),
					/* @__PURE__ */ r("input", {
						type: "number",
						"aria-label": "Minutes",
						className: N,
						min: 0,
						max: 59,
						value: f(T),
						onChange: j
					})
				]
			})]
		})]
	});
}
//#endregion
export { l as DatePicker, d as DateRangePicker, p as DateTimePicker };

//# sourceMappingURL=date-picker.js.map