import { useStudio as e } from "../../../providers/StudioProvider.js";
import { useBatchDeleteWorkflows as t, useChildWorkflows as n, useFilterWorkflows as r } from "../../../hooks/useWorkflows.js";
import { Badge as i } from "../../../components/ui/badge.js";
import a from "../../../components/lists/CustomListView.js";
import o from "../../../components/feedback/ErrorSnackbar.js";
import { require_enums as s } from "../../../packages/contracts/dist/enums/index.js";
import c from "./NewWorkflowRunDialog.js";
import { useEffect as l, useState as u } from "react";
import { jsx as d, jsxs as f } from "react/jsx-runtime";
import { useQueryClient as p } from "@tanstack/react-query";
import { ChevronDown as m, ChevronRight as h, Loader2 as g } from "lucide-react";
import { format as _, formatDistanceToNow as v, isToday as y, isYesterday as b, parseISO as x } from "date-fns";
//#region src/features/workspaces/components/ExecutionTimeline.tsx
var S = s(), C = ({ parentId: e, formatUpdatedTime: t, getWorkflowStateColor: r, onChildClick: a }) => {
	let { data: o, isPending: s } = n(e, !0);
	if (s) return /* @__PURE__ */ d("div", {
		className: "flex items-center py-2 pl-8",
		children: /* @__PURE__ */ d(g, { className: "h-4 w-4 animate-spin" })
	});
	let c = o ?? [];
	return c.length === 0 ? /* @__PURE__ */ d("p", {
		className: "py-1 pl-8 text-xs text-gray-400",
		children: "No child workflows"
	}) : /* @__PURE__ */ d("div", {
		className: "mt-4 border-t pt-3 pl-8",
		children: c.map((e) => /* @__PURE__ */ f("div", {
			className: "hover:bg-muted/50 flex cursor-pointer items-center justify-between rounded px-2 py-1.5",
			onClick: (t) => {
				t.stopPropagation(), a(e.id);
			},
			children: [/* @__PURE__ */ f("div", { children: [
				/* @__PURE__ */ f("p", {
					className: "text-sm font-medium",
					children: [
						"Run #",
						e.run,
						" ",
						e.title ? `(${e.title})` : ""
					]
				}),
				/* @__PURE__ */ d("p", {
					className: "text-xs text-gray-500",
					children: e.alias
				}),
				/* @__PURE__ */ d("p", {
					className: "text-xs text-gray-400",
					children: t(e.updatedAt)
				})
			] }), /* @__PURE__ */ d(i, {
				variant: "default",
				className: r(e.status),
				children: e.status
			})]
		}, e.id))
	});
}, w = ({ workspace: n }) => {
	let { router: s } = e(), w = p(), [T, E] = u(0), [D, O] = u(25), [k, A] = u(!1), [j, M] = u(/* @__PURE__ */ new Set()), N = (e, t) => {
		t.stopPropagation(), M((t) => {
			let n = new Set(t);
			return n.has(e) ? n.delete(e) : n.add(e), n;
		});
	};
	l(() => {
		w.invalidateQueries({ queryKey: ["workflows"] });
	}, [w]);
	let P = (e) => {
		let t = x(e);
		return y(t) ? v(t, { addSuffix: !0 }) : b(t) ? `Yesterday at ${_(t, "h:mm a")}` : _(t, "MMM d, yyyy h:mm a");
	}, F = r(void 0, {
		workspaceId: n.id,
		parentId: null
	}, "createdAt", "DESC", T, D), I = t(), L = (e) => {
		I.mutate(e);
	}, R = () => {
		A(!0);
	}, z = () => {
		A(!1);
	}, B = (e) => {
		s.navigateToWorkflow(e);
	}, V = (e) => {
		switch (e) {
			case S.WorkflowState.Completed: return "bg-green-600";
			case S.WorkflowState.Paused: return "bg-yellow-600";
			case S.WorkflowState.Failed: return "bg-red-600";
			case S.WorkflowState.Canceled:
			case S.WorkflowState.Pending:
			case S.WorkflowState.Running:
			default: return "bg-black";
		}
	}, H = F.data?.data ?? [], U = F.data?.total ?? 0;
	return /* @__PURE__ */ f("div", { children: [
		/* @__PURE__ */ d(o, { error: F.error }),
		F.isPending ? /* @__PURE__ */ d("div", {
			className: "flex items-center justify-center py-8",
			children: /* @__PURE__ */ d(g, { className: "h-8 w-8 animate-spin" })
		}) : "",
		/* @__PURE__ */ d(a, {
			loading: F.isPending,
			error: F.error ?? null,
			items: H,
			totalItems: U,
			onClick: B,
			handleNew: R,
			setPage: E,
			setRowsPerPage: O,
			page: T,
			rowsPerPage: D,
			enableBatchActions: !0,
			batchDelete: L,
			itemRenderer: (e) => /* @__PURE__ */ f("div", { children: [/* @__PURE__ */ f("div", {
				className: "flex items-center justify-between space-x-3",
				children: [/* @__PURE__ */ f("div", { children: [
					/* @__PURE__ */ f("h3", {
						className: "hover:text-primary font-medium transition-colors",
						children: [
							"Run #",
							e.run,
							" ",
							e.title ? `(${e.title})` : ""
						]
					}),
					/* @__PURE__ */ d("p", {
						className: "mt-1 text-sm text-gray-500",
						children: e.alias
					}),
					e.hasChildren > 0 && /* @__PURE__ */ f("button", {
						onClick: (t) => N(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: [
							j.has(e.id) ? /* @__PURE__ */ d(m, { className: "h-3 w-3" }) : /* @__PURE__ */ d(h, { className: "h-3 w-3" }),
							e.hasChildren,
							" sub ",
							e.hasChildren === 1 ? "execution" : "executions"
						]
					})
				] }), /* @__PURE__ */ f("div", {
					className: "flex flex-col items-end gap-2",
					children: [/* @__PURE__ */ d(i, {
						variant: "default",
						className: V(e.status),
						children: e.status
					}), /* @__PURE__ */ d("span", {
						className: "text-xs text-gray-400",
						children: P(e.updatedAt)
					})]
				})]
			}), e.hasChildren > 0 && j.has(e.id) && /* @__PURE__ */ d(C, {
				parentId: e.id,
				formatUpdatedTime: P,
				getWorkflowStateColor: V,
				onChildClick: B
			})] }),
			newButtonLabel: "Run"
		}),
		/* @__PURE__ */ d(c, {
			isOpen: k,
			onOpenChange: A,
			workspace: n,
			onSuccess: z
		})
	] });
};
//#endregion
export { w as default };
