import { useStudio } from "../../../providers/StudioProvider.js";
import ErrorSnackbar_default from "../../../components/snackbars/ErrorSnackbar.js";
import { usePipelineConfig } from "../../../hooks/useConfig.js";
import { useCreatePipeline } from "../../../hooks/usePipelines.js";
import ArgumentsView_default from "./pipeline-form/ArgumentsView.js";
import SelectionView_default from "./pipeline-form/SelectionView.js";
import { useRunPipeline } from "../../../hooks/useProcessor.js";
import { c } from "react/compiler-runtime";
import { useEffect, useMemo, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Loader2 } from "lucide-react";
var PipelineForm_default = (l) => {
	let u = c(63), { title: d, workspace: f } = l, { router: p } = useStudio(), m = useCreatePipeline(), h = useRunPipeline(), g = usePipelineConfig(f.blockName), [_, v] = useState("selection"), y;
	u[0] === Symbol.for("react.memo_cache_sentinel") ? (y = {
		name: "",
		blockName: "",
		properties: {}
	}, u[0] = y) : y = u[0];
	let [b, x] = useState(y), S;
	u[1] === Symbol.for("react.memo_cache_sentinel") ? (S = {
		name: "",
		blockName: ""
	}, u[1] = S) : S = u[1];
	let [C, w] = useState(S), T;
	bb0: {
		if (!b.blockName || !g.data) {
			T = void 0;
			break bb0;
		}
		let e;
		if (u[2] !== g.data || u[3] !== b.blockName) {
			let o;
			u[5] === b.blockName ? o = u[6] : (o = (e) => e.blockName === b.blockName, u[5] = b.blockName, u[6] = o), e = g.data.find(o), u[2] = g.data, u[3] = b.blockName, u[4] = e;
		} else e = u[4];
		T = e;
	}
	let E = T, D = !!E?.schema, O = m.isPending || h.isPending, k, A;
	u[7] !== g.data || u[8] !== b.blockName ? (k = () => {
		!b.blockName && g.data?.[0]?.blockName && x((e) => ({
			...e,
			blockName: g.data[0].blockName
		}));
	}, A = [g.data, b.blockName], u[7] = g.data, u[8] = b.blockName, u[9] = k, u[10] = A) : (k = u[9], A = u[10]), useEffect(k, A);
	let j;
	u[11] === b.blockName ? j = u[12] : (j = () => b.blockName ? !0 : (w({
		name: "",
		blockName: "Please select an automation type"
	}), !1), u[11] = b.blockName, u[12] = j);
	let M = j, N;
	u[13] === p ? N = u[14] : (N = (e) => {
		p.navigateToPipeline(e);
	}, u[13] = p, u[14] = N);
	let P = N, F;
	u[15] !== m || u[16] !== b.blockName || u[17] !== b.name || u[18] !== P || u[19] !== h || u[20] !== f.id ? (F = (e, o) => {
		m.mutate({ pipelineCreateDto: {
			blockName: b.blockName,
			title: b.name || null,
			workspaceId: f.id,
			transition: e,
			args: o
		} }, { onSuccess: (e) => {
			h.mutate({
				pipelineId: e.data.id,
				runPipelinePayloadDto: {},
				force: !0
			}, { onSuccess: () => P(e.data.id) });
		} });
	}, u[15] = m, u[16] = b.blockName, u[17] = b.name, u[18] = P, u[19] = h, u[20] = f.id, u[21] = F) : F = u[21];
	let I = F, L;
	u[22] !== I || u[23] !== D || u[24] !== M ? (L = () => {
		M() && (D ? v("arguments") : I());
	}, u[22] = I, u[23] = D, u[24] = M, u[25] = L) : L = u[25];
	let R = L, z;
	u[26] === Symbol.for("react.memo_cache_sentinel") ? (z = () => {
		v("selection");
	}, u[26] = z) : z = u[26];
	let B = z, V;
	u[27] === I ? V = u[28] : (V = (e, o) => {
		I(e, o);
	}, u[27] = I, u[28] = V);
	let H = V, U;
	u[29] === C ? U = u[30] : (U = (e, o) => {
		x((s) => ({
			...s,
			[e]: o
		})), C[e] && w((o) => ({
			...o,
			[e]: ""
		}));
	}, u[29] = C, u[30] = U);
	let W = U;
	if (g.isLoading) {
		let e;
		return u[31] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsx("div", {
			className: "flex min-h-50 items-center justify-center",
			children: /* @__PURE__ */ jsx(Loader2, { className: "text-primary h-8 w-8 animate-spin" })
		}), u[31] = e) : e = u[31], e;
	}
	if (!g.data) return null;
	let G;
	u[32] === m.error ? G = u[33] : (G = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: m.error }), u[32] = m.error, u[33] = G);
	let K;
	u[34] === h.error ? K = u[35] : (K = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: h.error }), u[34] = h.error, u[35] = K);
	let q;
	u[36] === g.error ? q = u[37] : (q = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: g.error }), u[36] = g.error, u[37] = q);
	let J = `translateX(-${_ === "arguments" ? 100 : 0}%)`, Y;
	u[38] === J ? Y = u[39] : (Y = { transform: J }, u[38] = J, u[39] = Y);
	let X;
	u[40] !== C || u[41] !== g.data || u[42] !== b || u[43] !== W || u[44] !== R || u[45] !== O || u[46] !== d ? (X = /* @__PURE__ */ jsx("div", {
		className: "w-full shrink-0 px-1",
		children: /* @__PURE__ */ jsx(SelectionView_default, {
			title: d,
			pipelineTypes: g.data,
			formData: b,
			errors: C,
			isLoading: O,
			onInputChange: W,
			onNext: R
		})
	}), u[40] = C, u[41] = g.data, u[42] = b, u[43] = W, u[44] = R, u[45] = O, u[46] = d, u[47] = X) : X = u[47];
	let Z;
	u[48] !== b.blockName || u[49] !== H || u[50] !== D || u[51] !== O || u[52] !== E ? (Z = /* @__PURE__ */ jsx("div", {
		className: "w-full shrink-0 px-1",
		children: /* @__PURE__ */ jsx(ArgumentsView_default, {
			config: E,
			hasArguments: D,
			isLoading: O,
			onBack: B,
			onSubmit: H
		}, b.blockName)
	}), u[48] = b.blockName, u[49] = H, u[50] = D, u[51] = O, u[52] = E, u[53] = Z) : Z = u[53];
	let Q;
	u[54] !== Y || u[55] !== X || u[56] !== Z ? (Q = /* @__PURE__ */ jsx("div", {
		className: "relative overflow-hidden",
		children: /* @__PURE__ */ jsxs("div", {
			className: "flex transition-transform duration-300 ease-in-out",
			style: Y,
			children: [X, Z]
		})
	}), u[54] = Y, u[55] = X, u[56] = Z, u[57] = Q) : Q = u[57];
	let $;
	return u[58] !== G || u[59] !== K || u[60] !== q || u[61] !== Q ? ($ = /* @__PURE__ */ jsxs("div", {
		className: "relative",
		children: [
			G,
			K,
			q,
			Q
		]
	}), u[58] = G, u[59] = K, u[60] = q, u[61] = Q, u[62] = $) : $ = u[62], $;
};
export { PipelineForm_default as default };
