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/rule-editor.tsx
var i = {
	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 a(e) {
	return "logic" in e && "conditions" in e;
}
var o = 0;
function s() {
	return o++, `rule-${Date.now()}-${o}`;
}
function c(e) {
	return e.operators && e.operators.length > 0 ? e.operators : i[e.type] ?? ["equals"];
}
function l(e) {
	return e === "number" ? 0 : e === "boolean" ? !0 : e === "date" ? (/* @__PURE__ */ new Date()).toISOString().split("T")[0] ?? "" : "";
}
function u(e) {
	return {
		id: e.id,
		logic: e.logic,
		conditions: e.conditions.map((e) => a(e) ? u(e) : { ...e })
	};
}
function d({ condition: e, fields: t, onChange: i, onRemove: a }) {
	let o = t.find((t) => t.name === e.field), s = o ? c(o) : ["equals"], u = (n) => {
		let r = t.find((e) => e.name === n);
		if (!r) return;
		let a = c(r);
		i({
			...e,
			field: n,
			type: r.type,
			operator: a[0] ?? "equals",
			value: l(r.type)
		});
	};
	return /* @__PURE__ */ r("div", {
		className: "bg-glass border-border-glass flex items-center gap-2 rounded-md border p-2 backdrop-blur-[20px]",
		children: [
			/* @__PURE__ */ n("span", {
				className: "text-foreground-secondary cursor-grab text-sm",
				title: "Drag to reorder",
				children: "≡"
			}),
			/* @__PURE__ */ n("select", {
				className: "bg-surface-secondary text-foreground rounded px-2 py-1 text-sm outline-none",
				value: e.field,
				onChange: (e) => u(e.target.value),
				children: t.map((e) => /* @__PURE__ */ n("option", {
					value: e.name,
					children: e.label
				}, e.name))
			}),
			/* @__PURE__ */ n("select", {
				className: "bg-surface-secondary text-foreground rounded px-2 py-1 text-sm outline-none",
				value: e.operator,
				onChange: (t) => i({
					...e,
					operator: t.target.value
				}),
				children: s.map((e) => /* @__PURE__ */ n("option", {
					value: e,
					children: e
				}, e))
			}),
			e.type === "boolean" ? /* @__PURE__ */ r("select", {
				className: "bg-surface-secondary text-foreground rounded px-2 py-1 text-sm outline-none",
				value: String(e.value),
				onChange: (t) => i({
					...e,
					value: t.target.value === "true"
				}),
				children: [/* @__PURE__ */ n("option", {
					value: "true",
					children: "true"
				}), /* @__PURE__ */ n("option", {
					value: "false",
					children: "false"
				})]
			}) : e.type === "number" ? /* @__PURE__ */ n("input", {
				type: "number",
				className: "bg-surface-secondary text-foreground w-24 rounded px-2 py-1 text-sm outline-none",
				value: String(e.value),
				onChange: (t) => {
					let n = Number(t.target.value);
					i({
						...e,
						value: Number.isNaN(n) ? 0 : n
					});
				}
			}) : e.type === "date" ? /* @__PURE__ */ n("input", {
				type: "date",
				className: "bg-surface-secondary text-foreground rounded px-2 py-1 text-sm outline-none",
				value: String(e.value),
				onChange: (t) => i({
					...e,
					value: t.target.value
				})
			}) : /* @__PURE__ */ n("input", {
				type: "text",
				className: "bg-surface-secondary text-foreground placeholder:text-foreground-secondary min-w-[120px] rounded px-2 py-1 text-sm outline-none",
				placeholder: "Enter value...",
				value: String(e.value),
				onChange: (t) => i({
					...e,
					value: t.target.value
				})
			}),
			/* @__PURE__ */ n("button", {
				onClick: a,
				className: "text-foreground-secondary hover:text-accent-rose ml-auto text-sm transition-colors",
				title: "Remove condition",
				children: "×"
			})
		]
	});
}
function f({ group: t, fields: i, depth: o, maxDepth: u, onChange: p, onRemove: m }) {
	let h = () => {
		p({
			...t,
			logic: t.logic === "AND" ? "OR" : "AND"
		});
	}, g = () => {
		let e = i[0];
		if (!e) return;
		let n = c(e), r = {
			id: s(),
			field: e.name,
			operator: n[0] ?? "equals",
			value: l(e.type),
			type: e.type
		};
		p({
			...t,
			conditions: [...t.conditions, r]
		});
	}, _ = () => {
		let e = {
			id: s(),
			logic: "AND",
			conditions: []
		};
		p({
			...t,
			conditions: [...t.conditions, e]
		});
	}, v = (e, n) => {
		let r = [...t.conditions];
		r[e] = n, p({
			...t,
			conditions: r
		});
	}, y = (e) => {
		let n = t.conditions.filter((t, n) => n !== e);
		p({
			...t,
			conditions: n
		});
	};
	return /* @__PURE__ */ r("div", {
		className: e("rounded-lg border p-3", o === 0 ? "border-border-glass bg-glass backdrop-blur-[20px]" : "border-border-glass/50 bg-surface-secondary/30"),
		children: [/* @__PURE__ */ r("div", {
			className: "mb-2 flex items-center gap-2",
			children: [
				/* @__PURE__ */ n("button", {
					onClick: h,
					className: e("rounded px-2.5 py-1 text-xs font-bold transition-colors", t.logic === "AND" ? "bg-accent-cyan/20 text-accent-cyan" : "bg-accent-violet/20 text-accent-violet"),
					children: t.logic
				}),
				/* @__PURE__ */ n("button", {
					onClick: g,
					className: "bg-surface-secondary text-foreground-secondary hover:text-foreground rounded px-2 py-1 text-xs transition-colors",
					children: "+ Condition"
				}),
				o < u - 1 && /* @__PURE__ */ n("button", {
					onClick: _,
					className: "bg-surface-secondary text-foreground-secondary hover:text-foreground rounded px-2 py-1 text-xs transition-colors",
					children: "+ Group"
				}),
				m && /* @__PURE__ */ n("button", {
					onClick: m,
					className: "text-foreground-secondary hover:text-accent-rose ml-auto text-sm transition-colors",
					title: "Remove group",
					children: "×"
				})
			]
		}), /* @__PURE__ */ n("div", {
			className: "flex flex-col gap-2",
			children: t.conditions.map((e, t) => a(e) ? /* @__PURE__ */ n(f, {
				group: e,
				fields: i,
				depth: o + 1,
				maxDepth: u,
				onChange: (e) => v(t, e),
				onRemove: () => y(t)
			}, e.id) : /* @__PURE__ */ n(d, {
				condition: e,
				fields: i,
				onChange: (e) => v(t, e),
				onRemove: () => y(t)
			}, e.id))
		})]
	});
}
function p({ rules: r, fields: i, onChange: a, maxDepth: o = 3, className: s, ...c }) {
	let [l, d] = t.useState(() => u(r));
	t.useEffect(() => {
		d(u(r));
	}, [r]);
	let p = t.useCallback((e) => {
		d(e), a?.(u(e));
	}, [a]);
	return /* @__PURE__ */ n("div", {
		"data-slot": "rule-editor",
		className: e(s),
		...c,
		children: /* @__PURE__ */ n(f, {
			group: l,
			fields: i,
			depth: 0,
			maxDepth: o,
			onChange: p
		})
	});
}
p.displayName = "RuleEditor";
//#endregion
export { p as RuleEditor };

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