import { n as e, t } from "../../utils-C_mWfcFX.js";
//#region src/security/detection-status/index.ts
var n = {
	active: "Active",
	disabled: "Disabled",
	testing: "Testing",
	error: "Error"
}, r = {
	critical: 0,
	high: 1,
	medium: 2,
	low: 3,
	info: 4
}, i = {
	error: 0,
	active: 1,
	testing: 2,
	disabled: 3
};
function a(e) {
	return (e instanceof Date ? e : new Date(e)).toLocaleDateString(void 0, {
		year: "numeric",
		month: "short",
		day: "numeric",
		hour: "2-digit",
		minute: "2-digit"
	});
}
var o = class {
	el;
	statusId;
	showFilters;
	showSearch;
	ruleClickHandler;
	currentRules;
	filteredRules;
	sortKey = "name";
	sortDir = "asc";
	filterStatus = "all";
	filterSeverity = "all";
	searchQuery = "";
	boundRowHandlers = [];
	constructor(n) {
		this.statusId = e("cmm-detection-status"), this.showFilters = n.showFilters === !0, this.showSearch = n.showSearch === !0, this.ruleClickHandler = n.onRuleClick, this.currentRules = n.rules, this.filteredRules = [...n.rules], this.el = document.createElement("div"), this.el.id = this.statusId, this.el.className = this.buildRootClassName(n.className), this.renderAll(), t(n.container).appendChild(this.el);
	}
	update(e) {
		this.currentRules = e, this.applyFilters(), this.renderAll();
	}
	destroy() {
		this.cleanupHandlers(), this.el.remove();
	}
	renderAll() {
		if (this.cleanupHandlers(), this.el.innerHTML = "", this.showSearch || this.showFilters) {
			let e = document.createElement("div");
			if (e.className = "cmm-detection-status__controls", this.showSearch) {
				let t = document.createElement("input");
				t.className = "cmm-detection-status__search", t.type = "text", t.placeholder = "Search rules...", t.value = this.searchQuery, t.setAttribute("aria-label", "Search detection rules"), t.addEventListener("input", () => {
					this.searchQuery = t.value, this.applyFilters(), this.renderAll();
				}), e.appendChild(t);
			}
			if (this.showFilters) {
				let t = document.createElement("select");
				t.className = "cmm-detection-status__filter", t.setAttribute("aria-label", "Filter by status");
				for (let [e, n] of [
					["all", "All Statuses"],
					["active", "Active"],
					["disabled", "Disabled"],
					["testing", "Testing"],
					["error", "Error"]
				]) {
					let r = document.createElement("option");
					r.value = e, r.textContent = n, e === this.filterStatus && (r.selected = !0), t.appendChild(r);
				}
				t.addEventListener("change", () => {
					this.filterStatus = t.value, this.applyFilters(), this.renderAll();
				}), e.appendChild(t);
				let n = document.createElement("select");
				n.className = "cmm-detection-status__filter", n.setAttribute("aria-label", "Filter by severity");
				for (let [e, t] of [
					["all", "All Severities"],
					["critical", "Critical"],
					["high", "High"],
					["medium", "Medium"],
					["low", "Low"],
					["info", "Info"]
				]) {
					let r = document.createElement("option");
					r.value = e, r.textContent = t, e === this.filterSeverity && (r.selected = !0), n.appendChild(r);
				}
				n.addEventListener("change", () => {
					this.filterSeverity = n.value, this.applyFilters(), this.renderAll();
				}), e.appendChild(n);
			}
			this.el.appendChild(e);
		}
		let e = document.createElement("table");
		e.className = "cmm-detection-status__table";
		let t = document.createElement("thead"), r = document.createElement("tr"), i = [
			{
				key: "name",
				label: "Name"
			},
			{
				key: "status",
				label: "Status"
			},
			{
				key: "severity",
				label: "Severity"
			},
			{
				key: "lastTriggered",
				label: "Last Triggered"
			},
			{
				key: "triggerCount",
				label: "Count"
			}
		];
		for (let e of i) {
			let t = document.createElement("th");
			if (t.className = "cmm-detection-status__header", t.textContent = e.label, t.style.cursor = "pointer", this.sortKey === e.key) {
				let n = this.sortDir === "asc" ? " ↑" : " ↓";
				t.textContent = e.label + n, t.classList.add("cmm-detection-status__header--sorted");
			}
			t.addEventListener("click", () => {
				this.sortKey === e.key ? this.sortDir = this.sortDir === "asc" ? "desc" : "asc" : (this.sortKey = e.key, this.sortDir = "asc"), this.applyFilters(), this.renderAll();
			}), r.appendChild(t);
		}
		t.appendChild(r), e.appendChild(t);
		let o = document.createElement("tbody");
		if (this.filteredRules.length === 0) {
			let e = document.createElement("tr"), t = document.createElement("td");
			t.className = "cmm-detection-status__empty", t.colSpan = i.length, t.textContent = "No detection rules found", e.appendChild(t), o.appendChild(e);
		} else for (let e of this.filteredRules) {
			let t = document.createElement("tr");
			t.className = "cmm-detection-status__row";
			let r = document.createElement("td");
			r.className = "cmm-detection-status__cell";
			let i = document.createElement("span");
			i.className = "cmm-detection-status__name", i.textContent = e.name, r.appendChild(i);
			let s = document.createElement("span");
			if (s.className = "cmm-detection-status__source", s.textContent = e.source, r.appendChild(s), e.tags !== void 0 && e.tags.length > 0) {
				let t = document.createElement("div");
				t.className = "cmm-detection-status__tags";
				for (let n of e.tags) {
					let e = document.createElement("span");
					e.className = "cmm-detection-status__tag", e.textContent = n, t.appendChild(e);
				}
				r.appendChild(t);
			}
			t.appendChild(r);
			let c = document.createElement("td");
			c.className = "cmm-detection-status__cell";
			let l = document.createElement("span");
			l.className = `cmm-detection-status__dot cmm-detection-status__dot--${e.status}`, l.setAttribute("aria-hidden", "true"), c.appendChild(l);
			let u = document.createElement("span");
			u.className = "cmm-detection-status__status-label", u.textContent = n[e.status], c.appendChild(u), t.appendChild(c);
			let d = document.createElement("td");
			if (d.className = "cmm-detection-status__cell", e.severity !== void 0) {
				let t = document.createElement("span");
				t.className = `cmm-detection-status__severity cmm-detection-status__severity--${e.severity}`, t.textContent = e.severity.charAt(0).toUpperCase() + e.severity.slice(1), d.appendChild(t);
			} else d.textContent = "—";
			t.appendChild(d);
			let f = document.createElement("td");
			f.className = "cmm-detection-status__cell", e.lastTriggered === void 0 ? f.textContent = "Never" : f.textContent = a(e.lastTriggered), t.appendChild(f);
			let p = document.createElement("td");
			if (p.className = "cmm-detection-status__cell", p.textContent = e.triggerCount === void 0 ? "0" : String(e.triggerCount), t.appendChild(p), this.ruleClickHandler !== void 0) {
				t.style.cursor = "pointer";
				let n = (t) => {
					t.stopPropagation(), this.ruleClickHandler !== void 0 && this.ruleClickHandler(e);
				};
				t.addEventListener("click", n), this.boundRowHandlers.push({
					el: t,
					handler: n
				});
			}
			o.appendChild(t);
		}
		e.appendChild(o), this.el.appendChild(e);
	}
	applyFilters() {
		let e = [...this.currentRules];
		if (this.filterStatus !== "all") {
			let t = this.filterStatus;
			e = e.filter((e) => e.status === t);
		}
		if (this.filterSeverity !== "all") {
			let t = this.filterSeverity;
			e = e.filter((e) => e.severity === t);
		}
		if (this.searchQuery.length > 0) {
			let t = this.searchQuery.toLowerCase();
			e = e.filter((e) => e.name.toLowerCase().includes(t) || e.source.toLowerCase().includes(t));
		}
		e.sort((e, t) => {
			let n = 0;
			switch (this.sortKey) {
				case "name":
					n = e.name.localeCompare(t.name);
					break;
				case "status":
					n = (i[e.status] ?? 99) - (i[t.status] ?? 99);
					break;
				case "severity":
					n = (e.severity === void 0 ? 99 : r[e.severity] ?? 99) - (t.severity === void 0 ? 99 : r[t.severity] ?? 99);
					break;
				case "lastTriggered":
					n = (e.lastTriggered === void 0 ? 0 : e.lastTriggered instanceof Date ? e.lastTriggered.getTime() : new Date(e.lastTriggered).getTime()) - (t.lastTriggered === void 0 ? 0 : t.lastTriggered instanceof Date ? t.lastTriggered.getTime() : new Date(t.lastTriggered).getTime());
					break;
				case "triggerCount":
					n = (e.triggerCount ?? 0) - (t.triggerCount ?? 0);
					break;
			}
			return this.sortDir === "desc" ? -n : n;
		}), this.filteredRules = e;
	}
	cleanupHandlers() {
		for (let e of this.boundRowHandlers) e.el.removeEventListener("click", e.handler);
		this.boundRowHandlers.length = 0;
	}
	buildRootClassName(e) {
		let t = ["cmm-detection-status"];
		return e && t.push(e), t.join(" ");
	}
};
//#endregion
export { o as DetectionStatus };

//# sourceMappingURL=index.js.map