import { MarkerType as e, Position as t } from "../../../node_modules/@xyflow/system/dist/esm/index.js";
import { To as n } from "../../../node_modules/@dagrejs/dagre/dist/dagre.esm.js";
//#region src/features/debug/lib/flow-utils.ts
var r = 130, i = 70, a = {
	gt: ">",
	lt: "<",
	eq: "==",
	ne: "!=",
	ge: ">=",
	le: "<="
};
function o(e, a, o = "LR") {
	let s = new n.graphlib.Graph().setDefaultEdgeLabel(() => ({}));
	s.setGraph({
		rankdir: o,
		nodesep: 100,
		ranksep: 200
	});
	for (let t of e) s.setNode(t.id, {
		width: r,
		height: i
	});
	for (let e of a) s.setEdge(e.source, e.target);
	return n.layout(s), {
		nodes: e.map((e) => {
			let n = s.node(e.id);
			return {
				...e,
				targetPosition: o === "LR" ? t.Left : t.Top,
				sourcePosition: o === "LR" ? t.Right : t.Bottom,
				position: {
					x: n.x - r / 2,
					y: n.y - i / 2
				}
			};
		}),
		edges: a
	};
}
function s(e, t = /* @__PURE__ */ new Set()) {
	if (!e || typeof e != "object" || t.has(e)) return [];
	try {
		t.add(e);
	} catch {
		return [];
	}
	let n = e;
	if (Array.isArray(n.transitions)) return n.transitions;
	if (n.definition) {
		let e = s(n.definition, t);
		if (e.length > 0) return e;
	}
	if (n.specification) {
		let e = s(n.specification, t);
		if (e.length > 0) return e;
	}
	for (let e in n) {
		if (e === "history" || e === "data") continue;
		let r = n[e];
		if (r && typeof r == "object") {
			let e = s(r, t);
			if (e.length > 0) return e;
		} else if (typeof r == "string" && r.includes("\"transitions\":")) try {
			let e = s(JSON.parse(r), t);
			if (e.length > 0) return e;
		} catch {}
	}
	return [];
}
function c(e) {
	if (!e) return "";
	let t = e.replace(/\{\{|\}\}/g, "").trim(), n = t.split(/\s+/);
	if (n.length === 3) {
		let [e, t, r] = n, i = a[e];
		if (i) return `${t} ${i} ${r}`;
	}
	return t;
}
function l(e, t, n, r = [], i, a = !1, s = []) {
	let c = u(e, t, r), l = d(c, s), b = p(s), x = m(c, s), S = h(l, x), C = g(s), w = _(x), T = v(l, {
		workflowId: n,
		currentPlace: t?.place,
		endStates: S,
		visitedStates: C,
		visitCounts: f(s),
		direction: i,
		forceVisible: a
	}), E = y(x, {
		workflowId: n,
		executedMap: b,
		stateRanks: w,
		forceVisible: a
	});
	return T.length > 1 ? o(T, E, i) : {
		nodes: T,
		edges: E
	};
}
function u(e, t, n) {
	let r = [...n];
	e && r.push(...s(e)), t && r.push(...s(t));
	let i = /* @__PURE__ */ new Set();
	return r.filter((e) => {
		let t = Array.isArray(e.from) ? e.from.join(",") : e.from, n = `${e.id}-${t}-${e.to}`;
		return i.has(n) ? !1 : (i.add(n), !0);
	});
}
function d(e, t) {
	let n = new Set(["start"]);
	for (let t of e) {
		let e = Array.isArray(t.from) ? t.from : [t.from];
		for (let t of e) n.add(t ?? "start");
		n.add(t.to);
	}
	for (let e of t) e.place && n.add(e.place), e.transitionFrom && n.add(e.transitionFrom);
	return n;
}
function f(e) {
	let t = new Map([["start", 1]]);
	for (let n of e) n.place && t.set(n.place, (t.get(n.place) ?? 0) + 1);
	return t;
}
function p(e) {
	let t = /* @__PURE__ */ new Map();
	for (let n of e) {
		if (!n.transitionId) continue;
		let e = `${n.transitionFrom ?? "start"}->${n.place}:${n.transitionId}`;
		t.set(e, (t.get(e) ?? 0) + 1);
	}
	return t;
}
function m(e, t) {
	let n = [];
	for (let t of e) {
		let e = Array.isArray(t.from) ? t.from : [t.from], r = t;
		for (let i of e) n.push({
			id: t.id,
			from: i ?? "start",
			to: t.to,
			condition: r.if ?? r.condition,
			trigger: t.trigger,
			call: t.call
		});
	}
	for (let e of t) {
		if (!e.transitionId) continue;
		let t = e.transitionFrom ?? "start";
		n.some((n) => n.from === t && n.to === e.place && n.id === e.transitionId) || n.push({
			id: e.transitionId,
			from: t,
			to: e.place
		});
	}
	return n;
}
function h(e, t) {
	let n = new Set(t.map((e) => e.from)), r = /* @__PURE__ */ new Set();
	for (let t of e) !n.has(t) && t !== "start" && r.add(t);
	return r;
}
function g(e) {
	let t = new Set(["start"]);
	for (let n of e) n.place && t.add(n.place);
	return t;
}
function _(e) {
	let t = /* @__PURE__ */ new Map();
	for (let n of e) {
		if (n.from === n.to) continue;
		let e = t.get(n.from) ?? [];
		e.push(n.to), t.set(n.from, e);
	}
	let n = new Map([["start", 0]]), r = ["start"];
	for (; r.length > 0;) {
		let e = r.shift(), i = n.get(e);
		for (let a of t.get(e) ?? []) n.has(a) || (n.set(a, i + 1), r.push(a));
	}
	return n;
}
function v(e, t) {
	return Array.from(e).map((e) => ({
		id: `${t.workflowId}-${e}`,
		type: "stateNode",
		position: {
			x: 0,
			y: 0
		},
		data: {
			label: e,
			isStart: e === "start",
			isEnd: t.endStates.has(e),
			isCurrent: e === t.currentPlace,
			isVisited: t.visitedStates.has(e),
			visitCount: t.visitCounts.get(e) ?? 0,
			direction: t.direction,
			forceVisible: t.forceVisible
		}
	}));
}
function y(t, n) {
	let r = /* @__PURE__ */ new Set(), i = [], a = 0;
	for (let o of t) {
		let t = `${o.from}->${o.to}:${o.id}`;
		if (r.has(t)) continue;
		r.add(t);
		let s = n.executedMap.has(t), c = o.trigger === "onEntry", l = o.from === o.to, u = n.stateRanks.get(o.from) ?? 0, d = n.stateRanks.get(o.to) ?? 0, f = !l && d <= u, p = s ? "var(--primary)" : "var(--muted-foreground)", m = {
			...o,
			isExecuted: s,
			isSelfLoop: l,
			isBackEdge: f,
			forceVisible: n.forceVisible
		};
		i.push({
			id: `edge-${n.workflowId}-${a++}`,
			source: `${n.workflowId}-${o.from}`,
			target: `${n.workflowId}-${o.to}`,
			...f && {
				sourceHandle: "bottom-source",
				targetHandle: "bottom-target"
			},
			type: "workflowTransition",
			animated: !1,
			style: {
				strokeWidth: s ? 2.5 : 1.5,
				stroke: p,
				strokeDasharray: c ? "4,4" : s ? void 0 : "5,5",
				opacity: s || n.forceVisible ? 1 : .3
			},
			markerEnd: {
				type: e.ArrowClosed,
				width: 18,
				height: 18,
				color: p
			},
			data: m
		});
	}
	return i;
}
//#endregion
export { l as buildWorkflowGraph, c as formatCondition, s as getTransitions };
