import { n as e, t } from "../../utils-C_mWfcFX.js";
import { t as n } from "../../a11y-CuHKgVSx.js";
import { cronToHuman as r, formatCron as i, getNextRuns as a, isValidCron as o, parseCron as s } from "./parser.js";
//#region src/form/cron-editor/index.ts
var c = {
	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"
		]
	}
}, l = [
	"minute",
	"hour",
	"day",
	"month",
	"weekday"
], u = {
	every: "Every",
	specific: "Specific",
	range: "Range",
	interval: "Interval"
}, d = [
	"every",
	"specific",
	"range",
	"interval"
], f = class {
	container;
	config;
	id;
	tabPanelId;
	parsed;
	activeField = "minute";
	tabBarEl = null;
	fieldBodyEl = null;
	expressionInput = null;
	previewEl = null;
	nextRunsEl = null;
	constructor(n) {
		this.container = t(n.container), this.config = n, this.id = e("cmm-cron-editor"), this.tabPanelId = e("cmm-cron-tabpanel"), this.parsed = s(n.value ?? "* * * * *"), this.render();
	}
	getValue() {
		return i(this.parsed);
	}
	setValue(e) {
		o(e) && (this.parsed = s(e), this.updateAllUI());
	}
	destroy() {
		this.container.innerHTML = "", this.tabBarEl = null, this.fieldBodyEl = null, this.expressionInput = null, this.previewEl = null, this.nextRunsEl = null;
	}
	render() {
		this.container.innerHTML = "";
		let e = document.createElement("div");
		e.className = "cmm-cron-editor", this.config.className && e.classList.add(this.config.className), e.id = this.id;
		let t = document.createElement("div");
		t.className = "cmm-cron-editor__tabs", t.setAttribute("role", "tablist"), this.tabBarEl = t;
		for (let e of l) {
			let n = c[e], r = `${this.id}-tab-${e}`, i = document.createElement("button");
			i.className = "cmm-cron-editor__tab", i.type = "button", i.id = r, i.setAttribute("role", "tab"), i.setAttribute("aria-selected", e === this.activeField ? "true" : "false"), i.setAttribute("aria-controls", this.tabPanelId), i.dataset.field = e, i.textContent = n.label, e === this.activeField && i.classList.add("cmm-cron-editor__tab--active"), i.addEventListener("click", () => {
				this.setActiveField(e);
			}), t.appendChild(i);
		}
		e.appendChild(t);
		let n = document.createElement("div");
		n.id = this.tabPanelId, n.className = "cmm-cron-editor__body", n.setAttribute("role", "tabpanel"), n.setAttribute("aria-labelledby", `${this.id}-tab-${this.activeField}`), this.fieldBodyEl = n, e.appendChild(n);
		let r = document.createElement("div");
		r.className = "cmm-cron-editor__expression";
		let a = document.createElement("label");
		a.className = "cmm-cron-editor__expression-label", a.textContent = "Cron Expression";
		let u = document.createElement("input");
		if (u.className = "cmm-cron-editor__expression-input", u.type = "text", u.value = i(this.parsed), u.setAttribute("aria-label", "Cron expression"), u.addEventListener("change", () => {
			let e = u.value.trim();
			o(e) && (this.parsed = s(e), this.updateFieldBody(), this.updatePreview(), this.updateNextRuns(), this.updateTabHighlights(), this.emitChange());
		}), this.expressionInput = u, r.appendChild(a), r.appendChild(u), e.appendChild(r), this.config.showPreview !== !1) {
			let t = document.createElement("div");
			t.className = "cmm-cron-editor__preview", this.previewEl = t, e.appendChild(t);
		}
		if (this.config.showNextRuns !== !1) {
			let t = document.createElement("div");
			t.className = "cmm-cron-editor__next-runs", this.nextRunsEl = t, e.appendChild(t);
		}
		this.container.appendChild(e), this.updateFieldBody(), this.updatePreview(), this.updateNextRuns();
	}
	setActiveField(e) {
		this.activeField = e, this.updateTabHighlights(), this.updateFieldBody(), this.fieldBodyEl !== null && this.fieldBodyEl.setAttribute("aria-labelledby", `${this.id}-tab-${e}`);
		let t = c[e];
		n(`${t.label} field selected`, "polite");
	}
	updateTabHighlights() {
		this.tabBarEl !== null && this.tabBarEl.querySelectorAll(".cmm-cron-editor__tab").forEach((e) => {
			e.dataset.field === this.activeField ? (e.classList.add("cmm-cron-editor__tab--active"), e.setAttribute("aria-selected", "true")) : (e.classList.remove("cmm-cron-editor__tab--active"), e.setAttribute("aria-selected", "false"));
		});
	}
	updateFieldBody() {
		let e = this.fieldBodyEl;
		if (e === null) return;
		e.innerHTML = "";
		let t = this.parsed[this.activeField], n = c[this.activeField], r = document.createElement("div");
		r.className = "cmm-cron-editor__modes";
		for (let e of d) {
			let n = document.createElement("button");
			n.className = "cmm-cron-editor__mode", n.type = "button", n.textContent = u[e], e === t.mode && n.classList.add("cmm-cron-editor__mode--active"), n.addEventListener("click", () => {
				this.setFieldMode(e);
			}), r.appendChild(n);
		}
		e.appendChild(r);
		let i = document.createElement("div");
		switch (i.className = "cmm-cron-editor__editor", t.mode) {
			case "every":
				this.renderEveryEditor(i, n);
				break;
			case "specific":
				this.renderSpecificEditor(i, n, t);
				break;
			case "range":
				this.renderRangeEditor(i, n, t);
				break;
			case "interval":
				this.renderIntervalEditor(i, n, t);
				break;
		}
		e.appendChild(i);
	}
	setFieldMode(e) {
		let t = c[this.activeField], n = { mode: e };
		switch (e) {
			case "specific":
				n.specific = [];
				break;
			case "range":
				n.rangeStart = t.min, n.rangeEnd = t.max;
				break;
			case "interval":
				n.interval = 1, n.intervalStart = t.min;
				break;
		}
		this.parsed[this.activeField] = n, this.onFieldChanged();
	}
	renderEveryEditor(e, t) {
		let n = document.createElement("p");
		n.className = "cmm-cron-editor__hint", n.textContent = `Every ${t.label.toLowerCase()} (* wildcard)`, e.appendChild(n);
	}
	renderSpecificEditor(e, t, n) {
		let r = document.createElement("div");
		r.className = "cmm-cron-editor__grid";
		let i = new Set(n.specific ?? []);
		for (let e = t.min; e <= t.max; e++) {
			let n = document.createElement("button");
			if (n.className = "cmm-cron-editor__grid-item", n.type = "button", t.labels !== void 0) {
				let r = e - t.min;
				n.textContent = t.labels[r] ?? String(e);
			} else n.textContent = String(e);
			i.has(e) && n.classList.add("cmm-cron-editor__grid-item--active");
			let a = e;
			n.addEventListener("click", () => {
				this.toggleSpecificValue(a);
			}), r.appendChild(n);
		}
		e.appendChild(r);
	}
	toggleSpecificValue(e) {
		let t = this.parsed[this.activeField];
		if (t.mode !== "specific") return;
		let n = t.specific ?? [], r = n.indexOf(e);
		r >= 0 ? n.splice(r, 1) : n.push(e), t.specific = n, this.onFieldChanged();
	}
	renderRangeEditor(e, t, n) {
		let r = document.createElement("div");
		r.className = "cmm-cron-editor__range-row";
		let i = document.createElement("label");
		i.className = "cmm-cron-editor__field-label", i.textContent = "From";
		let a = document.createElement("input");
		a.className = "cmm-cron-editor__field-input", a.type = "number", a.min = String(t.min), a.max = String(t.max), a.value = String(n.rangeStart ?? t.min), a.addEventListener("change", () => {
			let e = parseInt(a.value, 10);
			!isNaN(e) && e >= t.min && e <= t.max && (this.parsed[this.activeField].rangeStart = e, this.onFieldChanged());
		});
		let o = document.createElement("label");
		o.className = "cmm-cron-editor__field-label", o.textContent = "To";
		let s = document.createElement("input");
		s.className = "cmm-cron-editor__field-input", s.type = "number", s.min = String(t.min), s.max = String(t.max), s.value = String(n.rangeEnd ?? t.max), s.addEventListener("change", () => {
			let e = parseInt(s.value, 10);
			!isNaN(e) && e >= t.min && e <= t.max && (this.parsed[this.activeField].rangeEnd = e, this.onFieldChanged());
		}), r.appendChild(i), r.appendChild(a), r.appendChild(o), r.appendChild(s), e.appendChild(r);
	}
	renderIntervalEditor(e, t, n) {
		let r = document.createElement("div");
		r.className = "cmm-cron-editor__interval-row";
		let i = document.createElement("label");
		i.className = "cmm-cron-editor__field-label", i.textContent = "Every";
		let a = document.createElement("input");
		a.className = "cmm-cron-editor__field-input", a.type = "number", a.min = "1", a.max = String(t.max - t.min + 1), a.value = String(n.interval ?? 1), a.addEventListener("change", () => {
			let e = parseInt(a.value, 10);
			!isNaN(e) && e > 0 && (this.parsed[this.activeField].interval = e, this.onFieldChanged());
		});
		let o = document.createElement("label");
		o.className = "cmm-cron-editor__field-label", o.textContent = "Starting at";
		let s = document.createElement("input");
		s.className = "cmm-cron-editor__field-input", s.type = "number", s.min = String(t.min), s.max = String(t.max), s.value = String(n.intervalStart ?? t.min), s.addEventListener("change", () => {
			let e = parseInt(s.value, 10);
			!isNaN(e) && e >= t.min && e <= t.max && (this.parsed[this.activeField].intervalStart = e, this.onFieldChanged());
		}), r.appendChild(i), r.appendChild(a), r.appendChild(o), r.appendChild(s), e.appendChild(r);
	}
	onFieldChanged() {
		this.updateFieldBody(), this.updateExpressionInput(), this.updatePreview(), this.updateNextRuns(), this.emitChange();
	}
	updateExpressionInput() {
		this.expressionInput !== null && (this.expressionInput.value = i(this.parsed));
	}
	updatePreview() {
		if (this.previewEl === null) return;
		let e = i(this.parsed);
		this.previewEl.textContent = r(e);
	}
	updateNextRuns() {
		if (this.nextRunsEl === null) return;
		this.nextRunsEl.innerHTML = "";
		let e = this.config.nextRunCount ?? 5, t = a(i(this.parsed), e);
		if (t.length === 0) {
			let e = document.createElement("p");
			e.className = "cmm-cron-editor__no-runs", e.textContent = "No upcoming runs found", this.nextRunsEl.appendChild(e);
			return;
		}
		let n = document.createElement("div");
		n.className = "cmm-cron-editor__next-runs-heading", n.textContent = `Next ${String(t.length)} runs`, this.nextRunsEl.appendChild(n);
		let r = document.createElement("ul");
		r.className = "cmm-cron-editor__runs-list";
		for (let e of t) {
			let t = document.createElement("li");
			t.className = "cmm-cron-editor__run", t.textContent = p(e), r.appendChild(t);
		}
		this.nextRunsEl.appendChild(r);
	}
	updateAllUI() {
		this.updateTabHighlights(), this.updateFieldBody(), this.updateExpressionInput(), this.updatePreview(), this.updateNextRuns();
	}
	emitChange() {
		let e = i(this.parsed);
		this.config.onChange?.(e), n(`Schedule: ${r(e)}`, "polite");
	}
};
function p(e) {
	let t = [
		"Sun",
		"Mon",
		"Tue",
		"Wed",
		"Thu",
		"Fri",
		"Sat"
	], n = [
		"Jan",
		"Feb",
		"Mar",
		"Apr",
		"May",
		"Jun",
		"Jul",
		"Aug",
		"Sep",
		"Oct",
		"Nov",
		"Dec"
	], r = t[e.getDay()] ?? "", i = n[e.getMonth()] ?? "", a = e.getDate(), o = e.getFullYear(), s = e.getHours(), c = String(e.getMinutes()).padStart(2, "0"), l = s >= 12 ? "PM" : "AM", u = s === 0 ? 12 : s > 12 ? s - 12 : s;
	return `${r}, ${i} ${String(a)}, ${String(o)} at ${String(u)}:${c} ${l}`;
}
//#endregion
export { f as CronEditor, r as cronToHuman, i as formatCron, a as getNextRuns, o as isValidCron, s as parseCron };

//# sourceMappingURL=index.js.map