import { useWorkflowConfig as e, useWorkspaceConfig as t } from "../../../hooks/useConfig.js";
import { useCreateWorkflow as n } from "../../../hooks/useWorkflows.js";
import { useFilterWorkspaces as r } from "../../../hooks/useWorkspaces.js";
import { useComponentOverrides as i } from "../../../providers/ComponentOverridesProvider.js";
import { Button as a } from "../../../components/ui/button.js";
import { Collapsible as o, CollapsibleContent as s, CollapsibleTrigger as c } from "../../../components/ui/collapsible.js";
import { Dialog as l, DialogContent as u, DialogTitle as d } from "../../../components/ui/dialog.js";
import { Select as f, SelectContent as p, SelectItem as m, SelectTrigger as h, SelectValue as g } from "../../../components/ui/select.js";
import _ from "../../../components/feedback/ErrorSnackbar.js";
import v from "../../workspaces/components/CreateWorkspace.js";
import y from "../../../components/dynamic-form/Form.js";
import { useRunWorkflow as b } from "../../../hooks/useProcessor.js";
import "../../workspaces/index.js";
import { useCallback as x, useEffect as S, useMemo as ee, useState as C } from "react";
import { jsx as w, jsxs as T } from "react/jsx-runtime";
import { Check as E, CheckIcon as D, ChevronDown as O, Loader2 as k, Plus as te } from "lucide-react";
import * as A from "@radix-ui/react-select";
import { useForm as ne } from "react-hook-form";
//#region src/features/workbench/components/NewRunDialog.tsx
function j({ open: e, onOpenChange: t, onSuccess: n }) {
	return /* @__PURE__ */ w(l, {
		open: e,
		onOpenChange: t,
		children: /* @__PURE__ */ T(u, {
			className: "max-h-[80vh] min-h-[300px] !max-w-2xl",
			children: [/* @__PURE__ */ w(d, { children: "New Run" }), /* @__PURE__ */ w(M, {
				open: e,
				onSuccess: n
			})]
		})
	});
}
function M({ open: o, onSuccess: s }) {
	let [c, l] = C("workspace"), [u, d] = C(!1), [E, O] = C(""), [j, M] = C(""), { CreateWorkspace: P } = i(), F = P ?? v, I = t(), L = r(void 0, {}, "title", "ASC", 0, 100), R = L.data?.data ?? [], z = R.find((e) => e.id === E), B = e(z?.className), V = B.data ?? [], H = n(), U = b(), W = H.isPending || U.isPending, G = ne({
		defaultValues: {},
		mode: "onChange"
	}), K = ee(() => {
		if (!(!j || !V.length)) return V.find((e) => e.alias === j);
	}, [j, V]), q = !!K?.schema;
	S(() => {
		!E && R.length > 0 && O(R[0].id);
	}, [R, E]), S(() => {
		V.length > 0 && !V.find((e) => e.alias === j) && M(V[0].alias);
	}, [V, j]), S(() => {
		o || (l("workspace"), d(!1), O(""), M(""), G.reset({}), H.reset(), U.reset());
	}, [o]);
	let re = (e) => {
		O(e), l("automation");
	}, ie = (e) => {
		M(e), G.reset({});
	};
	S(() => {
		j && K && q && l("config");
	}, [
		K,
		j,
		q
	]);
	let J = x((e, t) => {
		!E || !j || H.mutate({ workflowCreateDto: {
			alias: j,
			title: null,
			workspaceId: E,
			transition: e ?? null,
			args: t ?? {}
		} }, { onSuccess: (e) => {
			U.mutate({
				workflowId: e.id,
				runWorkflowPayloadDto: {},
				force: !0
			}, { onSuccess: () => s(e.id) });
		} });
	}, [
		E,
		j,
		H,
		U,
		s
	]), ae = () => {
		q ? G.handleSubmit((e) => J("", e))() : J();
	}, oe = !!E && !!j, Y = (e) => {
		l((t) => t === e ? null : e);
	}, X = !!E, Z = !!j, Q = X, $ = Z && q;
	return /* @__PURE__ */ T("div", {
		className: "mt-2 overflow-y-auto",
		children: [
			/* @__PURE__ */ w(_, { error: H.error }),
			/* @__PURE__ */ w(_, { error: U.error }),
			/* @__PURE__ */ T("div", {
				className: "space-y-2",
				children: [
					/* @__PURE__ */ w(N, {
						step: 1,
						title: "Select Workspace",
						summary: z?.title,
						isActive: c === "workspace",
						isComplete: X && c !== "workspace",
						isEnabled: !0,
						isLoading: L.isLoading,
						onToggle: () => Y("workspace"),
						children: u ? /* @__PURE__ */ w("div", {
							className: "pt-2",
							children: /* @__PURE__ */ w(F, {
								types: I.data ?? [],
								onSuccess: () => {
									d(!1);
								}
							})
						}) : /* @__PURE__ */ T("div", {
							className: "flex gap-2 pt-2",
							children: [/* @__PURE__ */ T(f, {
								value: E,
								onValueChange: re,
								children: [/* @__PURE__ */ w(h, {
									id: "workspace",
									className: "flex-1",
									children: /* @__PURE__ */ w(g, { placeholder: "Select a workspace..." })
								}), /* @__PURE__ */ w(p, { children: R.map((e) => /* @__PURE__ */ w(m, {
									value: e.id,
									children: e.title
								}, e.id)) })]
							}), /* @__PURE__ */ w(a, {
								variant: "outline",
								onClick: () => d(!0),
								className: "px-3",
								title: "Create new workspace",
								children: /* @__PURE__ */ w(te, { className: "h-4 w-4" })
							})]
						})
					}),
					/* @__PURE__ */ w(N, {
						step: 2,
						title: "Select Workflow",
						summary: Z ? V.find((e) => e.alias === j)?.title ?? j : void 0,
						isActive: c === "automation",
						isComplete: Z && c !== "automation",
						isEnabled: Q,
						isLoading: B.isLoading,
						onToggle: () => Q && Y("automation"),
						children: /* @__PURE__ */ w("div", {
							className: "pt-2",
							children: /* @__PURE__ */ T(f, {
								value: j,
								onValueChange: ie,
								disabled: W,
								children: [/* @__PURE__ */ w(h, {
									id: "automation",
									className: "w-full",
									children: /* @__PURE__ */ w(g, { placeholder: "Select a workflow..." })
								}), /* @__PURE__ */ w(p, { children: V.map((e) => /* @__PURE__ */ T(A.Item, {
									value: e.alias,
									className: "focus:bg-accent focus:text-accent-foreground relative flex w-full cursor-pointer flex-col gap-0.5 rounded-sm py-2 pr-8 pl-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
									children: [
										/* @__PURE__ */ w("span", {
											className: "absolute right-2 top-2.5 flex size-3.5 items-center justify-center",
											children: /* @__PURE__ */ w(A.ItemIndicator, { children: /* @__PURE__ */ w(D, { className: "size-4" }) })
										}),
										/* @__PURE__ */ w(A.ItemText, { children: e.title ?? e.alias }),
										e.description && /* @__PURE__ */ w("span", {
											className: "text-muted-foreground text-xs leading-snug",
											children: e.description
										})
									]
								}, e.alias)) })]
							})
						})
					}),
					q && /* @__PURE__ */ w(N, {
						step: 3,
						title: "Configuration",
						isActive: c === "config",
						isComplete: !1,
						isEnabled: $,
						onToggle: () => $ && Y("config"),
						children: /* @__PURE__ */ w("div", {
							className: "max-h-72 overflow-y-auto pt-2",
							children: K?.schema && /* @__PURE__ */ w(y, {
								form: G,
								schema: K.schema,
								ui: K.ui,
								disabled: !1,
								viewOnly: !1
							})
						})
					})
				]
			}),
			/* @__PURE__ */ w("div", {
				className: "mt-4 flex justify-end border-t pt-4",
				children: /* @__PURE__ */ T(a, {
					variant: "default",
					disabled: !oe || W,
					onClick: ae,
					size: "lg",
					className: "font-medium",
					children: [W && /* @__PURE__ */ w(k, { className: "h-4 w-4 animate-spin" }), "Run Now"]
				})
			})
		]
	});
}
function N({ step: e, title: t, summary: n, isActive: r, isComplete: i, isEnabled: a, isLoading: l, onToggle: u, children: d }) {
	return /* @__PURE__ */ T(o, {
		open: r,
		onOpenChange: () => u(),
		children: [/* @__PURE__ */ T(c, {
			className: `flex w-full items-center gap-3 rounded-lg px-3 py-2.5 text-left transition-colors ${a ? "hover:bg-muted/50 cursor-pointer" : "cursor-not-allowed opacity-50"}`,
			disabled: !a,
			children: [
				/* @__PURE__ */ w("div", {
					className: `flex h-7 w-7 flex-shrink-0 items-center justify-center rounded-full text-xs font-semibold ${i ? "bg-primary text-primary-foreground" : r ? "border-primary text-primary border-2" : "border-border text-muted-foreground border"}`,
					children: i ? /* @__PURE__ */ w(E, { className: "h-3.5 w-3.5" }) : e
				}),
				/* @__PURE__ */ w("span", {
					className: `flex-1 text-sm font-medium ${a ? "text-foreground" : "text-muted-foreground"}`,
					children: t
				}),
				!r && i && n && /* @__PURE__ */ w("span", {
					className: "text-muted-foreground mr-1 max-w-48 truncate text-sm",
					children: n
				}),
				l && r && /* @__PURE__ */ w(k, { className: "text-muted-foreground h-4 w-4 animate-spin" }),
				/* @__PURE__ */ w(O, { className: `text-muted-foreground h-4 w-4 transition-transform ${r ? "rotate-180" : ""}` })
			]
		}), /* @__PURE__ */ w(s, {
			className: "px-3 pb-3 pl-13",
			children: d
		})]
	});
}
//#endregion
export { j as NewRunDialog };
