import { t as e } from "../../utils-B7XIe3qq.js";
import * as t from "react";
import { jsx as n, jsxs as r } from "react/jsx-runtime";
//#region src/react/primitives/cron-editor.tsx
var i = {
	minute: {
		label: "Minute",
		min: 0,
		max: 59
	},
	hour: {
		label: "Hour",
		min: 0,
		max: 23
	},
	day: {
		label: "Day of Month",
		min: 1,
		max: 31
	},
	month: {
		label: "Month",
		min: 1,
		max: 12,
		labels: [
			"Jan",
			"Feb",
			"Mar",
			"Apr",
			"May",
			"Jun",
			"Jul",
			"Aug",
			"Sep",
			"Oct",
			"Nov",
			"Dec"
		]
	},
	weekday: {
		label: "Weekday",
		min: 0,
		max: 6,
		labels: [
			"Sun",
			"Mon",
			"Tue",
			"Wed",
			"Thu",
			"Fri",
			"Sat"
		]
	}
}, a = [
	"minute",
	"hour",
	"day",
	"month",
	"weekday"
], o = [
	"every",
	"specific",
	"range",
	"interval"
], s = {
	every: "Every",
	specific: "Specific",
	range: "Range",
	interval: "Interval"
}, c = {
	minute: [0, 59],
	hour: [0, 23],
	day: [1, 31],
	month: [1, 12],
	weekday: [0, 6]
};
function l(e, t) {
	let [n, r] = c[t];
	if (e === "*") return { mode: "every" };
	if (e.includes("/")) {
		let t = e.split("/"), r = t[0] ?? "*";
		return {
			mode: "interval",
			interval: parseInt(t[1] ?? "1", 10),
			intervalStart: r === "*" ? n : parseInt(r, 10)
		};
	}
	if (e.includes("-") && !e.includes(",")) {
		let t = e.split("-");
		return {
			mode: "range",
			rangeStart: parseInt(t[0] ?? String(n), 10),
			rangeEnd: parseInt(t[1] ?? String(r), 10)
		};
	}
	let i = e.split(",").map((e) => parseInt(e.trim(), 10)).filter((e) => !isNaN(e));
	return i.length > 0 ? {
		mode: "specific",
		specific: i
	} : { mode: "every" };
}
function u(e, t) {
	let [n] = c[t];
	switch (e.mode) {
		case "every": return "*";
		case "specific": {
			let t = e.specific ?? [];
			return t.length === 0 ? "*" : t.sort((e, t) => e - t).join(",");
		}
		case "range": return `${e.rangeStart ?? n}-${e.rangeEnd ?? n}`;
		case "interval": {
			let t = e.interval ?? 1, r = e.intervalStart ?? n;
			return `${r === n ? "*" : String(r)}/${t}`;
		}
		default: return "*";
	}
}
function d(e) {
	let t = e.trim().split(/\s+/), n = a.map((e, n) => l(t[n] ?? "*", e));
	return {
		minute: n[0] ?? { mode: "every" },
		hour: n[1] ?? { mode: "every" },
		day: n[2] ?? { mode: "every" },
		month: n[3] ?? { mode: "every" },
		weekday: n[4] ?? { mode: "every" }
	};
}
function f(e) {
	return a.map((t) => u(e[t], t)).join(" ");
}
function p(e) {
	return e.trim().split(/\s+/).length === 5;
}
var m = [
	"",
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
], h = [
	"Sunday",
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday"
];
function g(e) {
	let t = e.trim().split(/\s+/);
	if (t.length < 5) return "Invalid cron expression";
	let [n, r, i, a, o] = t, s = [];
	if (n === "*" && r === "*" && i === "*" && a === "*" && o === "*") return "Every minute";
	if (n.includes("/") ? s.push(`Every ${n.split("/")[1] ?? "1"} minutes`) : n !== "*" && n !== "0" && s.push(`At minute ${n}`), r.includes("/")) s.push(`every ${r.split("/")[1] ?? "1"} hours`);
	else if (r !== "*") {
		let e = parseInt(r, 10), t = e >= 12 ? "PM" : "AM", i = e === 0 ? 12 : e > 12 ? e - 12 : e;
		n === "0" ? s.push(`At ${i}:00 ${t}`) : s.push(`at hour ${r}`);
	}
	if (i !== "*" && (i.includes("/") ? s.push(`every ${i.split("/")[1] ?? "1"} days`) : s.push(`on day ${i}`)), a !== "*") {
		let e = a.split(",").map((e) => m[parseInt(e, 10)] ?? e);
		s.push(`in ${e.join(", ")}`);
	}
	if (o !== "*") {
		let e = o.split(",").map((e) => h[parseInt(e, 10)] ?? e);
		s.push(`on ${e.join(", ")}`);
	}
	if (s.length === 0) return "Every minute";
	let c = s.join(", ");
	return c.charAt(0).toUpperCase() + c.slice(1);
}
function _(e, t, n) {
	let [r] = c[n];
	if (t === "*") return !0;
	let i = t.split(",");
	if (i.length > 1) return i.some((t) => _(e, t.trim(), n));
	if (t.includes("/")) {
		let n = t.split("/"), i = n[0] ?? "*", a = parseInt(n[1] ?? "1", 10), o = i === "*" ? r : parseInt(i, 10);
		return a <= 0 ? !1 : e >= o && (e - o) % a === 0;
	}
	if (t.includes("-")) {
		let n = t.split("-");
		return e >= parseInt(n[0] ?? "0", 10) && e <= parseInt(n[1] ?? "0", 10);
	}
	return e === parseInt(t, 10);
}
function v(e, t) {
	let n = e.trim().split(/\s+/);
	if (n.length < 5) return [];
	let [r, i, a, o, s] = n, c = [], l = /* @__PURE__ */ new Date();
	l.setSeconds(0, 0), l.setMinutes(l.getMinutes() + 1);
	for (let e = 0; e < 525600 && c.length < t; e++) _(l.getMinutes(), r, "minute") && _(l.getHours(), i, "hour") && _(l.getDate(), a, "day") && _(l.getMonth() + 1, o, "month") && _(l.getDay(), s, "weekday") && c.push(new Date(l.getTime())), l.setMinutes(l.getMinutes() + 1);
	return c;
}
function y(e) {
	let t = [
		"Sun",
		"Mon",
		"Tue",
		"Wed",
		"Thu",
		"Fri",
		"Sat"
	], n = [
		"Jan",
		"Feb",
		"Mar",
		"Apr",
		"May",
		"Jun",
		"Jul",
		"Aug",
		"Sep",
		"Oct",
		"Nov",
		"Dec"
	], r = e.getHours(), i = String(e.getMinutes()).padStart(2, "0"), a = r >= 12 ? "PM" : "AM", o = r === 0 ? 12 : r > 12 ? r - 12 : r;
	return `${t[e.getDay()]}, ${n[e.getMonth()]} ${e.getDate()}, ${e.getFullYear()} at ${o}:${i} ${a}`;
}
function b({ value: c = "* * * * *", showPreview: l = !0, showNextRuns: u = !0, nextRunCount: m = 5, className: h, onChange: _ }) {
	let [b, x] = t.useState(() => d(c)), [S, C] = t.useState("minute"), [w, T] = t.useState(c);
	t.useEffect(() => {
		let e = d(c);
		x(e), T(f(e));
	}, [c]);
	let E = t.useMemo(() => f(b), [b]), D = t.useMemo(() => g(E), [E]), O = t.useMemo(() => u ? v(E, m) : [], [
		E,
		u,
		m
	]);
	function k(e) {
		x(e);
		let t = f(e);
		T(t), _?.(t);
	}
	function A() {
		let e = w.trim();
		if (p(e)) {
			let t = d(e);
			x(t), _?.(f(t));
		}
	}
	function j(e) {
		let t = i[S], n = { mode: e };
		e === "specific" && (n.specific = []), e === "range" && (n.rangeStart = t.min, n.rangeEnd = t.max), e === "interval" && (n.interval = 1, n.intervalStart = t.min), k({
			...b,
			[S]: n
		});
	}
	function M(e) {
		let t = b[S];
		if (t.mode !== "specific") return;
		let n = [...t.specific ?? []], r = n.indexOf(e);
		r >= 0 ? n.splice(r, 1) : n.push(e), k({
			...b,
			[S]: {
				...t,
				specific: n
			}
		});
	}
	let N = b[S], P = i[S], F = (t) => e("px-3 py-2 text-[12px] font-medium rounded-lg transition-all duration-200", t === S ? "bg-primary text-white shadow-glow" : "text-foreground-secondary hover:bg-glass-hover hover:text-foreground"), I = (t) => e("px-3 py-1.5 text-[12px] rounded-lg transition-all duration-200", t === N.mode ? "bg-glass border border-primary text-primary" : "text-foreground-muted hover:bg-glass-hover hover:text-foreground"), L = e("border-border-glass bg-glass h-8 w-20 rounded-lg border px-2 text-[12px] text-foreground", "outline-none transition-all duration-200 focus:border-primary focus:shadow-glow backdrop-blur-[20px]");
	return /* @__PURE__ */ r("div", {
		"data-slot": "cron-editor",
		className: e("w-full rounded-xl border border-border-glass bg-glass p-4 backdrop-blur-[20px]", h),
		children: [
			/* @__PURE__ */ n("div", {
				role: "tablist",
				className: "mb-4 flex flex-wrap gap-1",
				children: a.map((e) => /* @__PURE__ */ n("button", {
					type: "button",
					role: "tab",
					"aria-selected": e === S,
					className: F(e),
					onClick: () => C(e),
					children: i[e].label
				}, e))
			}),
			/* @__PURE__ */ r("div", {
				role: "tabpanel",
				className: "mb-4",
				children: [/* @__PURE__ */ n("div", {
					className: "mb-3 flex flex-wrap gap-1",
					children: o.map((e) => /* @__PURE__ */ n("button", {
						type: "button",
						className: I(e),
						onClick: () => j(e),
						children: s[e]
					}, e))
				}), /* @__PURE__ */ r("div", {
					className: "min-h-[60px]",
					children: [
						N.mode === "every" && /* @__PURE__ */ r("p", {
							className: "text-[13px] text-foreground-muted",
							children: [
								"Every ",
								P.label.toLowerCase(),
								" (* wildcard)"
							]
						}),
						N.mode === "specific" && /* @__PURE__ */ n("div", {
							className: "flex flex-wrap gap-1",
							children: Array.from({ length: P.max - P.min + 1 }, (t, r) => {
								let i = P.min + r, a = (N.specific ?? []).includes(i), o = P.labels ? P.labels[r] ?? String(i) : String(i);
								return /* @__PURE__ */ n("button", {
									type: "button",
									className: e("inline-flex h-8 min-w-[2rem] items-center justify-center rounded-lg px-2 text-[12px] transition-all duration-200", a ? "bg-primary text-white shadow-glow" : "border border-border-glass text-foreground-secondary hover:bg-glass-hover"),
									onClick: () => M(i),
									children: o
								}, i);
							})
						}),
						N.mode === "range" && /* @__PURE__ */ r("div", {
							className: "flex items-center gap-2",
							children: [
								/* @__PURE__ */ n("label", {
									className: "text-[12px] text-foreground-muted",
									children: "From"
								}),
								/* @__PURE__ */ n("input", {
									type: "number",
									className: L,
									min: P.min,
									max: P.max,
									value: N.rangeStart ?? P.min,
									onChange: (e) => {
										let t = parseInt(e.target.value, 10);
										!isNaN(t) && t >= P.min && t <= P.max && k({
											...b,
											[S]: {
												...N,
												rangeStart: t
											}
										});
									}
								}),
								/* @__PURE__ */ n("label", {
									className: "text-[12px] text-foreground-muted",
									children: "To"
								}),
								/* @__PURE__ */ n("input", {
									type: "number",
									className: L,
									min: P.min,
									max: P.max,
									value: N.rangeEnd ?? P.max,
									onChange: (e) => {
										let t = parseInt(e.target.value, 10);
										!isNaN(t) && t >= P.min && t <= P.max && k({
											...b,
											[S]: {
												...N,
												rangeEnd: t
											}
										});
									}
								})
							]
						}),
						N.mode === "interval" && /* @__PURE__ */ r("div", {
							className: "flex items-center gap-2",
							children: [
								/* @__PURE__ */ n("label", {
									className: "text-[12px] text-foreground-muted",
									children: "Every"
								}),
								/* @__PURE__ */ n("input", {
									type: "number",
									className: L,
									min: 1,
									max: P.max - P.min + 1,
									value: N.interval ?? 1,
									onChange: (e) => {
										let t = parseInt(e.target.value, 10);
										!isNaN(t) && t > 0 && k({
											...b,
											[S]: {
												...N,
												interval: t
											}
										});
									}
								}),
								/* @__PURE__ */ n("label", {
									className: "text-[12px] text-foreground-muted",
									children: "Starting at"
								}),
								/* @__PURE__ */ n("input", {
									type: "number",
									className: L,
									min: P.min,
									max: P.max,
									value: N.intervalStart ?? P.min,
									onChange: (e) => {
										let t = parseInt(e.target.value, 10);
										!isNaN(t) && t >= P.min && t <= P.max && k({
											...b,
											[S]: {
												...N,
												intervalStart: t
											}
										});
									}
								})
							]
						})
					]
				})]
			}),
			/* @__PURE__ */ r("div", {
				className: "mb-3",
				children: [/* @__PURE__ */ n("label", {
					className: "mb-1 block text-[11px] font-medium text-foreground-muted",
					children: "Cron Expression"
				}), /* @__PURE__ */ n("input", {
					type: "text",
					"aria-label": "Cron expression",
					className: e("border-border-glass bg-glass h-10 w-full rounded-xl border px-4 py-2 font-mono text-[13px] text-foreground backdrop-blur-[20px]", "outline-none transition-all duration-200 focus:border-primary focus:shadow-glow"),
					value: w,
					onChange: (e) => T(e.target.value),
					onBlur: A,
					onKeyDown: (e) => e.key === "Enter" && A()
				})]
			}),
			l && /* @__PURE__ */ n("div", {
				className: "mb-3 text-[13px] text-foreground-secondary",
				children: D
			}),
			u && O.length > 0 && /* @__PURE__ */ r("div", { children: [/* @__PURE__ */ r("div", {
				className: "mb-1 text-[11px] font-medium text-foreground-muted",
				children: [
					"Next ",
					O.length,
					" runs"
				]
			}), /* @__PURE__ */ n("ul", {
				className: "space-y-0.5",
				children: O.map((e, t) => /* @__PURE__ */ n("li", {
					className: "text-[12px] text-foreground-secondary",
					children: y(e)
				}, t))
			})] })
		]
	});
}
//#endregion
export { b as CronEditor, g as cronToHuman, f as formatCron, v as getNextRuns, p as isValidCron, d as parseCron };

//# sourceMappingURL=cron-editor.js.map