import { usePipeline } from "../../../hooks/usePipelines.js";
import { Background, BackgroundVariant, Controls, index, useEdgesState, useNodesState, useReactFlow } from "../../../node_modules/@xyflow/react/dist/esm/index.js";
/* empty css                                             */
import StateNode_default from "./pipeline-flow/StateNode.js";
import WorkflowTransitionEdge_default from "./pipeline-flow/WorkflowTransitionEdge.js";
import WorkflowGraph_default from "./pipeline-flow/WorkflowGraph.js";
import { c } from "react/compiler-runtime";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Loader2 } from "lucide-react";
var nodeTypes = { stateNode: StateNode_default }, edgeTypes = { workflowTransition: WorkflowTransitionEdge_default }, PipelineFlowViewer_default = (b) => {
	let x = c(28), { pipelineId: S, workflows: C, pipelineConfig: w } = b, { data: T } = usePipeline(S), E;
	x[0] === Symbol.for("react.memo_cache_sentinel") ? (E = [], x[0] = E) : E = x[0];
	let [D, O, k] = useNodesState(E), A;
	x[1] === Symbol.for("react.memo_cache_sentinel") ? (A = [], x[1] = A) : A = x[1];
	let [j, M, N] = useEdgesState(A), { fitView: P } = useReactFlow(), F = useRef(!1), I;
	x[2] === Symbol.for("react.memo_cache_sentinel") ? (I = {}, x[2] = I) : I = x[2];
	let [L, R] = useState(I), z;
	x[3] === Symbol.for("react.memo_cache_sentinel") ? (z = /* @__PURE__ */ new Map(), x[3] = z) : z = x[3];
	let B = useRef(z), V = Object.values(L).some(_temp), H;
	x[4] === Symbol.for("react.memo_cache_sentinel") ? (H = (e, m) => {
		R((h) => h[e] === m ? h : {
			...h,
			[e]: m
		});
	}, x[4] = H) : H = x[4];
	let U = H, W;
	x[5] !== M || x[6] !== O || x[7] !== C ? (W = (e, m, h) => {
		let g = C.findIndex((m) => m.id === e) * 250, _ = m.map((e) => ({
			...e,
			position: {
				...e.position,
				y: e.position.y + g
			}
		}));
		B.current.set(e, {
			nodes: _,
			edges: h
		});
		let v = [], y = [];
		B.current.forEach((e) => {
			let { nodes: m, edges: h } = e;
			v.push(...m), y.push(...h);
		}), O(v), M(y);
	}, x[5] = M, x[6] = O, x[7] = C, x[8] = W) : W = x[8];
	let G = W, K, q;
	if (x[9] !== P || x[10] !== V || x[11] !== D.length ? (K = () => {
		if (!V && D.length > 0 && !F.current) {
			F.current = !0;
			let e = setTimeout(() => {
				P({ padding: .2 });
			}, 150);
			return () => clearTimeout(e);
		}
	}, q = [
		V,
		D.length,
		P
	], x[9] = P, x[10] = V, x[11] = D.length, x[12] = K, x[13] = q) : (K = x[12], q = x[13]), useEffect(K, q), V && D.length === 0) {
		let e;
		return x[14] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsx("div", {
			className: "flex h-full items-center justify-center",
			children: /* @__PURE__ */ jsx(Loader2, { className: "text-muted-foreground h-8 w-8 animate-spin" })
		}), x[14] = e) : e = x[14], e;
	}
	let J;
	x[15] !== G || x[16] !== T || x[17] !== w || x[18] !== C ? (J = T && C.map((e) => /* @__PURE__ */ jsx(WorkflowGraph_default, {
		pipeline: T,
		workflow: e,
		pipelineConfig: w,
		onGraphReady: G,
		onLoadingChange: U
	}, e.id)), x[15] = G, x[16] = T, x[17] = w, x[18] = C, x[19] = J) : J = x[19];
	let Y;
	x[20] !== j || x[21] !== D || x[22] !== N || x[23] !== k ? (Y = D.length === 0 ? /* @__PURE__ */ jsx("div", {
		className: "text-muted-foreground flex h-full items-center justify-center",
		children: /* @__PURE__ */ jsx("p", {
			className: "font-medium",
			children: "No workflow transitions available"
		})
	}) : /* @__PURE__ */ jsxs(index, {
		nodes: D,
		edges: j,
		onNodesChange: k,
		onEdgesChange: N,
		nodeTypes,
		edgeTypes,
		fitView: !0,
		minZoom: .1,
		maxZoom: 2,
		proOptions: { hideAttribution: !0 },
		className: "bg-background",
		children: [/* @__PURE__ */ jsx(Background, {
			variant: BackgroundVariant.Cross,
			gap: 24,
			size: 1,
			className: "opacity-[0.15]"
		}), /* @__PURE__ */ jsx(Controls, { className: "bg-card border-border rounded-lg border shadow-md" })]
	}), x[20] = j, x[21] = D, x[22] = N, x[23] = k, x[24] = Y) : Y = x[24];
	let X;
	return x[25] !== Y || x[26] !== J ? (X = /* @__PURE__ */ jsxs("div", {
		className: "h-full w-full",
		children: [J, Y]
	}), x[25] = Y, x[26] = J, x[27] = X) : X = x[27], X;
};
function _temp(e) {
	return e;
}
export { PipelineFlowViewer_default as default };
