import { n as e } from "../sse-D0HLjufB.js";
import { t } from "../utils-B7XIe3qq.js";
import { PhaseProgress as n } from "../primitives/phase-progress.js";
import { SSELiveBadge as r } from "../primitives/sse-live-badge.js";
import { LiveLogTail as i } from "../primitives/live-log-tail.js";
import { MetricsCard as a } from "./metrics-card.js";
import { PerCaseStatsTable as o } from "./per-case-stats-table.js";
import { Button as s } from "../react/primitives/button.js";
import * as c from "react";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
//#region src/composites/run-monitor.tsx
function d(e) {
	if (typeof e == "string") return e;
	if (e && typeof e == "object") {
		let t = e;
		if (typeof t.line == "string") return t.line;
		if (typeof t.message == "string") return t.message;
		if (typeof t.text == "string") return t.text;
	}
	return null;
}
function f({ url: t, onLine: n, onConnected: r }) {
	let i = c.useRef(n), a = c.useRef(r);
	c.useEffect(() => {
		i.current = n;
	}, [n]), c.useEffect(() => {
		a.current = r;
	}, [r]);
	let { connected: o } = e({
		url: t,
		withCredentials: !0,
		events: c.useMemo(() => ({ log: (e) => {
			let t = d(e);
			t !== null && i.current(t);
		} }), [])
	});
	return c.useEffect(() => {
		a.current(o);
	}, [o]), null;
}
function p({ runId: e, streamUrl: d, phases: p, currentPhase: m, status: h, metrics: g, perCaseStats: _, perCaseColumns: v, sseToken: y, header: b, actions: x, summary: S, error: C, lineParser: w, logLineParser: T, onLogLine: E, streamSource: D = "auto", logRef: O, onCancel: k, onResume: A, className: j, ...M }) {
	let N = c.useRef(null), P = c.useCallback((e) => {
		N.current = e, typeof O == "function" ? O(e) : O && typeof O == "object" && (O.current = e);
	}, [O]), [F, I] = c.useState(!1), L = D === "auto", R = c.useCallback((e) => {
		N.current?.append(e), E?.(e);
	}, [E]), z = h === "running", B = h === "failed" || h === "cancelled", V = /* @__PURE__ */ u("div", {
		className: "flex items-center justify-between gap-3",
		children: [/* @__PURE__ */ l("div", {
			className: "flex flex-1 items-center gap-3 min-w-0",
			children: b
		}), /* @__PURE__ */ u("div", {
			className: "flex shrink-0 items-center gap-2",
			children: [
				L && /* @__PURE__ */ l(r, { connected: F }),
				x,
				!x && k && z && /* @__PURE__ */ l(s, {
					tone: "shadcn",
					variant: "outline",
					size: "sm",
					onClick: k,
					children: "Cancel"
				}),
				!x && A && B && /* @__PURE__ */ l(s, {
					tone: "shadcn",
					variant: "default",
					size: "sm",
					onClick: A,
					children: "Resume"
				})
			]
		})]
	}), H = w ?? T;
	return /* @__PURE__ */ u("div", {
		"data-slot": "run-monitor",
		"data-run-id": String(e),
		"data-stream-source": D,
		className: t("flex w-full flex-col gap-4", j),
		...M,
		children: [
			L && /* @__PURE__ */ l(f, {
				url: d,
				onLine: R,
				onConnected: I
			}),
			V,
			/* @__PURE__ */ l(n, {
				phases: p,
				currentPhase: m,
				status: h
			}),
			g && g.length > 0 && /* @__PURE__ */ l(a, { items: g }),
			_ && v && /* @__PURE__ */ l(o, {
				rows: _,
				columnDefs: v,
				...m ? { currentId: m } : {}
			}),
			/* @__PURE__ */ l(i, {
				ref: P,
				follow: !0,
				className: "h-[320px]",
				...H ? { lineParser: H } : {}
			}),
			S && /* @__PURE__ */ l("div", {
				"data-slot": "run-monitor-summary",
				children: S
			}),
			B && C && /* @__PURE__ */ l("div", {
				"data-slot": "run-monitor-error",
				className: "rounded-xl border border-accent-rose/40 bg-accent-rose/5 p-4 text-sm text-accent-rose",
				role: "alert",
				children: C
			})
		]
	});
}
//#endregion
export { p as RunMonitor };

//# sourceMappingURL=run-monitor.js.map