import { useStudio } from "../../../providers/StudioProvider.js";
import { Badge } from "../../../components/ui/badge.js";
import CustomListView_default from "../../../components/lists/CustomListView.js";
import ErrorSnackbar_default from "../../../components/snackbars/ErrorSnackbar.js";
import { useBatchDeletePipeline, useChildPipelines, useFilterPipelines } from "../../../hooks/usePipelines.js";
import NewPipelineRunDialog_default from "./NewPipelineRunDialog.js";
import { c } from "react/compiler-runtime";
import React, { useEffect, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { useQueryClient } from "@tanstack/react-query";
import { ChevronDown, ChevronRight, Loader2 } from "lucide-react";
import { format, formatDistanceToNow, isToday, isYesterday, parseISO } from "date-fns";
var ChildPipelineList = (e) => {
	let n = c(15), { parentId: r, formatUpdatedTime: i, getPipelineStatusColor: o, onChildClick: s } = e, { data: l, isPending: u } = useChildPipelines(r, !0);
	if (u) {
		let e;
		return n[0] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsx("div", {
			className: "flex items-center py-2 pl-8",
			children: /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin" })
		}), n[0] = e) : e = n[0], e;
	}
	let d;
	n[1] === l?.data ? d = n[2] : (d = l?.data ?? [], n[1] = l?.data, n[2] = d);
	let f = d;
	if (f.length === 0) {
		let e;
		return n[3] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsx("p", {
			className: "py-1 pl-8 text-xs text-gray-400",
			children: "No child workflows"
		}), n[3] = e) : e = n[3], e;
	}
	let p;
	if (n[4] !== f || n[5] !== i || n[6] !== o || n[7] !== s) {
		let e;
		n[9] !== i || n[10] !== o || n[11] !== s ? (e = (e) => /* @__PURE__ */ jsxs("div", {
			className: "hover:bg-muted/50 flex cursor-pointer items-center justify-between rounded px-2 py-1.5",
			onClick: (t) => {
				t.stopPropagation(), s(e.id);
			},
			children: [/* @__PURE__ */ jsxs("div", { children: [
				/* @__PURE__ */ jsxs("p", {
					className: "text-sm font-medium",
					children: [
						"Run #",
						e.run,
						" ",
						e.title ? `(${e.title})` : ""
					]
				}),
				/* @__PURE__ */ jsx("p", {
					className: "text-xs text-gray-500",
					children: e.blockName
				}),
				/* @__PURE__ */ jsx("p", {
					className: "text-xs text-gray-400",
					children: i(e.updatedAt)
				})
			] }), /* @__PURE__ */ jsx(Badge, {
				variant: "default",
				className: o(e.status),
				children: e.status
			})]
		}, e.id), n[9] = i, n[10] = o, n[11] = s, n[12] = e) : e = n[12], p = f.map(e), n[4] = f, n[5] = i, n[6] = o, n[7] = s, n[8] = p;
	} else p = n[8];
	let m;
	return n[13] === p ? m = n[14] : (m = /* @__PURE__ */ jsx("div", {
		className: "mt-4 border-t pt-3 pl-8",
		children: p
	}), n[13] = p, n[14] = m), m;
}, ExecutionTimeline_default = (a) => {
	let l = c(40), { workspace: h } = a, { router: g } = useStudio(), _ = useQueryClient(), [v, y] = useState(0), [b, x] = useState(25), [S, C] = useState(!1), w;
	l[0] === Symbol.for("react.memo_cache_sentinel") ? (w = /* @__PURE__ */ new Set(), l[0] = w) : w = l[0];
	let [T, E] = useState(w), D;
	l[1] === Symbol.for("react.memo_cache_sentinel") ? (D = (e, t) => {
		t.stopPropagation(), E((t) => {
			let n = new Set(t);
			return n.has(e) ? n.delete(e) : n.add(e), n;
		});
	}, l[1] = D) : D = l[1];
	let se = D, O, k;
	l[2] === _ ? (O = l[3], k = l[4]) : (O = () => {
		_.invalidateQueries({ queryKey: ["pipelines"] });
	}, k = [_], l[2] = _, l[3] = O, l[4] = k), useEffect(O, k);
	let A = _temp, j;
	l[5] === h.id ? j = l[6] : (j = {
		workspaceId: h.id,
		parentId: null
	}, l[5] = h.id, l[6] = j);
	let M = useFilterPipelines(void 0, j, "createdAt", "DESC", v, b), N = useBatchDeletePipeline(), P;
	l[7] === N ? P = l[8] : (P = (e) => {
		N.mutate(e);
	}, l[7] = N, l[8] = P);
	let F = P, I;
	l[9] === Symbol.for("react.memo_cache_sentinel") ? (I = () => {
		C(!0);
	}, l[9] = I) : I = l[9];
	let L = I, R;
	l[10] === Symbol.for("react.memo_cache_sentinel") ? (R = () => {
		C(!1);
	}, l[10] = R) : R = l[10];
	let z = R, B;
	l[11] === g ? B = l[12] : (B = (e) => {
		g.navigateToPipeline(e);
	}, l[11] = g, l[12] = B);
	let V = B, H = _temp2, U;
	l[13] === M.data?.data ? U = l[14] : (U = M.data?.data ?? [], l[13] = M.data?.data, l[14] = U);
	let W = U, G = M.data?.total ?? 0, K;
	l[15] !== T || l[16] !== V ? (K = (e) => /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsxs("div", {
		className: "flex items-center justify-between space-x-3",
		children: [/* @__PURE__ */ jsxs("div", { children: [
			/* @__PURE__ */ jsxs("h3", {
				className: "hover:text-primary font-medium transition-colors",
				children: [
					"Run #",
					e.run,
					" ",
					e.title ? `(${e.title})` : ""
				]
			}),
			/* @__PURE__ */ jsx("p", {
				className: "mt-1 text-sm text-gray-500",
				children: e.blockName
			}),
			e.hasChildren > 0 && /* @__PURE__ */ jsxs("button", {
				onClick: (t) => se(e.id, t),
				className: "hover:bg-muted mt-2 flex items-center gap-1 rounded px-1.5 py-0.5 text-xs text-gray-400",
				children: [
					T.has(e.id) ? /* @__PURE__ */ jsx(ChevronDown, { className: "h-3 w-3" }) : /* @__PURE__ */ jsx(ChevronRight, { className: "h-3 w-3" }),
					e.hasChildren,
					" sub ",
					e.hasChildren === 1 ? "execution" : "executions"
				]
			})
		] }), /* @__PURE__ */ jsxs("div", {
			className: "flex flex-col items-end gap-2",
			children: [/* @__PURE__ */ jsx(Badge, {
				variant: "default",
				className: H(e.status),
				children: e.status
			}), /* @__PURE__ */ jsx("span", {
				className: "text-xs text-gray-400",
				children: A(e.updatedAt)
			})]
		})]
	}), e.hasChildren > 0 && T.has(e.id) && /* @__PURE__ */ jsx(ChildPipelineList, {
		parentId: e.id,
		formatUpdatedTime: A,
		getPipelineStatusColor: H,
		onChildClick: V
	})] }), l[15] = T, l[16] = V, l[17] = K) : K = l[17];
	let q = K, J;
	l[18] === M.error ? J = l[19] : (J = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: M.error }), l[18] = M.error, l[19] = J);
	let Y;
	l[20] === M.isPending ? Y = l[21] : (Y = M.isPending ? /* @__PURE__ */ jsx("div", {
		className: "flex items-center justify-center py-8",
		children: /* @__PURE__ */ jsx(Loader2, { className: "h-8 w-8 animate-spin" })
	}) : "", l[20] = M.isPending, l[21] = Y);
	let X = M.error ?? null, Z;
	l[22] !== M.isPending || l[23] !== F || l[24] !== V || l[25] !== v || l[26] !== W || l[27] !== q || l[28] !== b || l[29] !== X || l[30] !== G ? (Z = /* @__PURE__ */ jsx(CustomListView_default, {
		loading: M.isPending,
		error: X,
		items: W,
		totalItems: G,
		onClick: V,
		handleNew: L,
		setPage: y,
		setRowsPerPage: x,
		page: v,
		rowsPerPage: b,
		enableBatchActions: !0,
		batchDelete: F,
		itemRenderer: q,
		newButtonLabel: "Run"
	}), l[22] = M.isPending, l[23] = F, l[24] = V, l[25] = v, l[26] = W, l[27] = q, l[28] = b, l[29] = X, l[30] = G, l[31] = Z) : Z = l[31];
	let Q;
	l[32] !== S || l[33] !== h ? (Q = /* @__PURE__ */ jsx(NewPipelineRunDialog_default, {
		isOpen: S,
		onOpenChange: C,
		workspace: h,
		onSuccess: z
	}), l[32] = S, l[33] = h, l[34] = Q) : Q = l[34];
	let $;
	return l[35] !== J || l[36] !== Y || l[37] !== Z || l[38] !== Q ? ($ = /* @__PURE__ */ jsxs("div", { children: [
		J,
		Y,
		Z,
		Q
	] }), l[35] = J, l[36] = Y, l[37] = Z, l[38] = Q, l[39] = $) : $ = l[39], $;
};
function _temp(e) {
	let t = parseISO(e);
	return isToday(t) ? formatDistanceToNow(t, { addSuffix: !0 }) : isYesterday(t) ? `Yesterday at ${format(t, "h:mm a")}` : format(t, "MMM d, yyyy h:mm a");
}
function _temp2(e) {
	switch (e) {
		case "completed": return "bg-green-600";
		case "paused": return "bg-yellow-600";
		case "failed": return "bg-red-600";
		case "canceled":
		case "pending":
		case "running": return "bg-black";
	}
}
export { ExecutionTimeline_default as default };
