import { useStudio as e } from "../../../providers/StudioProvider.js";
import { useWorkflowConfig as t } from "../../../hooks/useConfig.js";
import { useCreateWorkflow as n } from "../../../hooks/useWorkflows.js";
import r from "../../../components/feedback/ErrorSnackbar.js";
import { useRunWorkflow as i } from "../../../hooks/useProcessor.js";
import a from "./workflow-form/ArgumentsView.js";
import o from "./workflow-form/SelectionView.js";
import { useEffect as s, useMemo as c, useState as l } from "react";
import { jsx as u, jsxs as d } from "react/jsx-runtime";
import { Loader2 as f } from "lucide-react";
//#region src/features/workspaces/components/WorkflowRunForm.tsx
var p = ({ title: p, workspace: m }) => {
	let { router: h } = e(), g = n(), _ = i(), v = t(m.className), [y, b] = l("selection"), [x, S] = l({
		name: "",
		alias: "",
		properties: {}
	}), [C, w] = l({
		name: "",
		alias: ""
	}), T = c(() => {
		if (!(!x.alias || !v.data)) return v.data.find((e) => e.alias === x.alias);
	}, [x.alias, v.data]), E = !!T?.schema, D = g.isPending || _.isPending;
	s(() => {
		!x.alias && v.data?.[0]?.alias && S((e) => ({
			...e,
			alias: v.data[0].alias
		}));
	}, [v.data, x.alias]);
	let O = () => x.alias ? !0 : (w({
		name: "",
		alias: "Please select an automation type"
	}), !1), k = (e) => {
		h.navigateToWorkflow(e);
	}, A = (e, t) => {
		g.mutate({ workflowCreateDto: {
			alias: x.alias,
			title: x.name || null,
			workspaceId: m.id,
			transition: e ?? null,
			args: t
		} }, { onSuccess: (e) => {
			_.mutate({
				workflowId: e.id,
				runWorkflowPayloadDto: {},
				force: !0
			}, { onSuccess: () => k(e.id) });
		} });
	};
	return v.isLoading ? /* @__PURE__ */ u("div", {
		className: "flex min-h-50 items-center justify-center",
		children: /* @__PURE__ */ u(f, { className: "text-primary h-8 w-8 animate-spin" })
	}) : v.data ? /* @__PURE__ */ d("div", {
		className: "relative",
		children: [
			/* @__PURE__ */ u(r, { error: g.error }),
			/* @__PURE__ */ u(r, { error: _.error }),
			/* @__PURE__ */ u(r, { error: v.error }),
			/* @__PURE__ */ u("div", {
				className: "relative overflow-hidden",
				children: /* @__PURE__ */ d("div", {
					className: "flex transition-transform duration-300 ease-in-out",
					style: { transform: `translateX(-${y === "arguments" ? 100 : 0}%)` },
					children: [/* @__PURE__ */ u("div", {
						className: "w-full shrink-0 px-1",
						children: /* @__PURE__ */ u(o, {
							title: p,
							workflowTypes: v.data,
							formData: x,
							errors: C,
							isLoading: D,
							onInputChange: (e, t) => {
								S((n) => ({
									...n,
									[e]: t
								})), C[e] && w((t) => ({
									...t,
									[e]: ""
								}));
							},
							onNext: () => {
								O() && (E ? b("arguments") : A());
							}
						})
					}), /* @__PURE__ */ u("div", {
						className: "w-full shrink-0 px-1",
						children: /* @__PURE__ */ u(a, {
							config: T,
							hasArguments: E,
							isLoading: D,
							onBack: () => {
								b("selection");
							},
							onSubmit: (e, t) => {
								A(e, t);
							}
						}, x.alias)
					})]
				})
			})
		]
	}) : null;
};
//#endregion
export { p as default };
