import { n as e, t } from "../../utils-C_mWfcFX.js";
//#region src/visualization/rule-editor/index.ts
var n = {
	string: [
		"equals",
		"not equals",
		"contains",
		"starts with",
		"ends with",
		"is empty",
		"is not empty"
	],
	number: [
		"equals",
		"not equals",
		"greater than",
		"less than",
		"greater or equal",
		"less or equal",
		"between"
	],
	boolean: ["equals", "not equals"],
	date: [
		"equals",
		"before",
		"after",
		"between",
		"in last",
		"is empty"
	]
};
function r(e) {
	return "logic" in e && "conditions" in e;
}
function i() {
	return e("rule");
}
var a = class {
	container;
	config;
	instanceId;
	fields;
	maxDepth;
	rules;
	wrapperEl = null;
	dragState = null;
	constructor(n) {
		this.container = t(n.container), this.config = n, this.instanceId = e("cmm-rule-editor"), this.fields = n.fields, this.maxDepth = n.maxDepth ?? 3, this.rules = this.cloneGroup(n.rules), this.render();
	}
	getValue() {
		return this.cloneGroup(this.rules);
	}
	setValue(e) {
		this.rules = this.cloneGroup(e), this.render();
	}
	destroy() {
		this.container.innerHTML = "", this.wrapperEl = null, this.dragState = null;
	}
	cloneGroup(e) {
		return {
			id: e.id,
			logic: e.logic,
			conditions: e.conditions.map((e) => r(e) ? this.cloneGroup(e) : { ...e })
		};
	}
	emitChange() {
		this.config.onChange?.(this.cloneGroup(this.rules));
	}
	render() {
		this.container.innerHTML = "";
		let e = document.createElement("div");
		e.className = "cmm-rule-editor", e.id = this.instanceId, this.config.className !== void 0 && e.classList.add(this.config.className), e.appendChild(this.renderGroup(this.rules, 0)), this.wrapperEl = e, this.container.appendChild(e);
	}
	renderGroup(e, t) {
		let n = document.createElement("div");
		n.className = "cmm-rule-editor__group", n.dataset.groupId = e.id, n.dataset.depth = String(t), t > 0 && n.classList.add("cmm-rule-editor__group--nested");
		let a = document.createElement("div");
		a.className = "cmm-rule-editor__group-header";
		let o = document.createElement("button");
		o.className = "cmm-rule-editor__logic-toggle", o.classList.add(e.logic === "AND" ? "cmm-rule-editor__logic-toggle--and" : "cmm-rule-editor__logic-toggle--or"), o.textContent = e.logic, o.addEventListener("click", () => {
			e.logic = e.logic === "AND" ? "OR" : "AND", this.emitChange(), this.render();
		}), a.appendChild(o);
		let s = document.createElement("button");
		if (s.className = "cmm-rule-editor__btn cmm-rule-editor__btn--add-condition", s.textContent = "+ Condition", s.addEventListener("click", () => {
			let t = this.fields[0];
			if (t === void 0) return;
			let n = this.getOperatorsForField(t)[0], r = {
				id: i(),
				field: t.name,
				operator: n ?? "equals",
				value: this.getDefaultValue(t.type),
				type: t.type
			};
			e.conditions.push(r), this.emitChange(), this.render();
		}), a.appendChild(s), t < this.maxDepth - 1) {
			let t = document.createElement("button");
			t.className = "cmm-rule-editor__btn cmm-rule-editor__btn--add-group", t.textContent = "+ Group", t.addEventListener("click", () => {
				let t = {
					id: i(),
					logic: "AND",
					conditions: []
				};
				e.conditions.push(t), this.emitChange(), this.render();
			}), a.appendChild(t);
		}
		if (t > 0) {
			let t = document.createElement("button");
			t.className = "cmm-rule-editor__btn cmm-rule-editor__btn--remove", t.textContent = "×", t.title = "Remove group", t.addEventListener("click", () => {
				this.removeGroupById(this.rules, e.id), this.emitChange(), this.render();
			}), a.appendChild(t);
		}
		n.appendChild(a);
		let c = document.createElement("div");
		c.className = "cmm-rule-editor__conditions", c.dataset.groupId = e.id, this.setupGroupDropZone(c, e);
		for (let n of e.conditions) r(n) ? c.appendChild(this.renderGroup(n, t + 1)) : c.appendChild(this.renderCondition(n, e));
		return n.appendChild(c), n;
	}
	renderCondition(e, t) {
		let n = document.createElement("div");
		n.className = "cmm-rule-editor__condition", n.dataset.conditionId = e.id, n.draggable = !0, this.setupConditionDrag(n, e.id, t.id);
		let i = document.createElement("span");
		i.className = "cmm-rule-editor__drag-handle", i.textContent = "≡", i.title = "Drag to reorder", n.appendChild(i);
		let a = document.createElement("select");
		a.className = "cmm-rule-editor__select cmm-rule-editor__field-select";
		for (let t of this.fields) {
			let n = document.createElement("option");
			n.value = t.name, n.textContent = t.label, t.name === e.field && (n.selected = !0), a.appendChild(n);
		}
		a.addEventListener("change", () => {
			e.field = a.value;
			let t = this.fields.find((e) => e.name === a.value);
			t !== void 0 && (e.type = t.type, e.operator = this.getOperatorsForField(t)[0] ?? "equals", e.value = this.getDefaultValue(t.type)), this.emitChange(), this.render();
		}), n.appendChild(a);
		let o = this.fields.find((t) => t.name === e.field), s = o === void 0 ? ["equals"] : this.getOperatorsForField(o), c = document.createElement("select");
		c.className = "cmm-rule-editor__select cmm-rule-editor__operator-select";
		for (let t of s) {
			let n = document.createElement("option");
			n.value = t, n.textContent = t, t === e.operator && (n.selected = !0), c.appendChild(n);
		}
		c.addEventListener("change", () => {
			e.operator = c.value, this.emitChange();
		}), n.appendChild(c);
		let l = this.createValueInput(e, o);
		n.appendChild(l);
		let u = document.createElement("button");
		return u.className = "cmm-rule-editor__btn cmm-rule-editor__btn--remove", u.textContent = "×", u.title = "Remove condition", u.addEventListener("click", () => {
			let n = t.conditions.findIndex((t) => !r(t) && t.id === e.id);
			n >= 0 && (t.conditions.splice(n, 1), this.emitChange(), this.render());
		}), n.appendChild(u), n;
	}
	createValueInput(e, t) {
		switch (e.type ?? t?.type ?? "string") {
			case "boolean": {
				let t = document.createElement("select");
				t.className = "cmm-rule-editor__select cmm-rule-editor__value-select";
				let n = document.createElement("option");
				n.value = "true", n.textContent = "true", (e.value === !0 || e.value === "true") && (n.selected = !0), t.appendChild(n);
				let r = document.createElement("option");
				return r.value = "false", r.textContent = "false", (e.value === !1 || e.value === "false") && (r.selected = !0), t.appendChild(r), t.addEventListener("change", () => {
					e.value = t.value === "true", this.emitChange();
				}), t;
			}
			case "number": {
				let t = document.createElement("input");
				return t.className = "cmm-rule-editor__input", t.type = "number", t.value = String(e.value), t.addEventListener("change", () => {
					let n = Number(t.value);
					e.value = Number.isNaN(n) ? 0 : n, this.emitChange();
				}), t;
			}
			case "date": {
				let t = document.createElement("input");
				return t.className = "cmm-rule-editor__input", t.type = "date", t.value = String(e.value), t.addEventListener("change", () => {
					e.value = t.value, this.emitChange();
				}), t;
			}
			default: {
				let t = document.createElement("input");
				return t.className = "cmm-rule-editor__input", t.type = "text", t.value = String(e.value), t.placeholder = "Enter value...", t.addEventListener("input", () => {
					e.value = t.value, this.emitChange();
				}), t;
			}
		}
	}
	getOperatorsForField(e) {
		return e.operators !== void 0 && e.operators.length > 0 ? e.operators : n[e.type] ?? ["equals"];
	}
	getDefaultValue(e) {
		switch (e) {
			case "number": return 0;
			case "boolean": return !0;
			case "date": return (/* @__PURE__ */ new Date()).toISOString().split("T")[0] ?? "";
			default: return "";
		}
	}
	removeGroupById(e, t) {
		for (let n = 0; n < e.conditions.length; n++) {
			let i = e.conditions[n];
			if (i !== void 0 && r(i)) {
				if (i.id === t) return e.conditions.splice(n, 1), !0;
				if (this.removeGroupById(i, t)) return !0;
			}
		}
		return !1;
	}
	findGroupById(e, t) {
		if (e.id === t) return e;
		for (let n of e.conditions) if (r(n)) {
			let e = this.findGroupById(n, t);
			if (e !== void 0) return e;
		}
	}
	setupConditionDrag(e, t, n) {
		e.addEventListener("dragstart", (r) => {
			this.dragState = {
				conditionId: t,
				groupId: n
			}, e.classList.add("cmm-rule-editor__condition--dragging"), r.dataTransfer !== null && (r.dataTransfer.effectAllowed = "move", r.dataTransfer.setData("text/plain", t));
		}), e.addEventListener("dragend", () => {
			if (e.classList.remove("cmm-rule-editor__condition--dragging"), this.dragState = null, this.wrapperEl !== null) {
				let e = this.wrapperEl.querySelectorAll(".cmm-rule-editor__conditions--drag-over");
				for (let t of e) t.classList.remove("cmm-rule-editor__conditions--drag-over");
			}
		});
	}
	setupGroupDropZone(e, t) {
		e.addEventListener("dragover", (t) => {
			t.preventDefault(), t.dataTransfer !== null && (t.dataTransfer.dropEffect = "move"), e.classList.add("cmm-rule-editor__conditions--drag-over");
		}), e.addEventListener("dragleave", (t) => {
			let n = t.relatedTarget;
			n instanceof Node && e.contains(n) || e.classList.remove("cmm-rule-editor__conditions--drag-over");
		}), e.addEventListener("drop", (n) => {
			if (n.preventDefault(), n.stopPropagation(), e.classList.remove("cmm-rule-editor__conditions--drag-over"), this.dragState === null) return;
			let { conditionId: i, groupId: a } = this.dragState;
			this.dragState = null;
			let o = this.findGroupById(this.rules, a);
			if (o === void 0) return;
			let s = o.conditions.findIndex((e) => !r(e) && e.id === i);
			if (s < 0) return;
			let c = o.conditions.splice(s, 1)[0];
			if (c === void 0) return;
			let l = this.calculateDropIndex(e, n.clientY), u = Math.min(l, t.conditions.length);
			t.conditions.splice(u, 0, c), this.emitChange(), this.render();
		});
	}
	calculateDropIndex(e, t) {
		let n = e.querySelectorAll(":scope > .cmm-rule-editor__condition, :scope > .cmm-rule-editor__group"), r = n.length;
		for (let e = 0; e < n.length; e++) {
			let i = n[e];
			if (i === void 0) continue;
			let a = i.getBoundingClientRect();
			if (t < a.top + a.height / 2) {
				r = e;
				break;
			}
		}
		return r;
	}
};
//#endregion
export { a as RuleEditor };

//# sourceMappingURL=index.js.map