import e from "../../components/messages/CompletionMessagePaper.js";
import { useRunWorkflow as t } from "../../hooks/useProcessor.js";
import { useOAuthPopup as n } from "./useOAuthPopup.js";
import { useCallback as r, useEffect as i, useRef as a } from "react";
import { Fragment as o, jsx as s, jsxs as c } from "react/jsx-runtime";
//#region src/features/oauth/OAuthPromptRenderer.tsx
var l = ({ parentWorkflow: o, workflow: l, document: u, isActive: m }) => {
	let h = u.content, g = u.ui, _ = (g?.widgets)?.[0]?.options?.transition ?? (g?.actions)?.map((e) => e.options?.transition).find((e) => !!e), v = t(), { result: y, open: b, reset: x } = n(), S = a(!1), C = l.availableTransitions?.map((e) => e.id) ?? [], w = r((e, t) => {
		!_ || !C.includes(_) || v.mutate({
			workflowId: o.id,
			runWorkflowPayloadDto: { transition: {
				id: _,
				workflowId: l.id,
				payload: {
					code: e,
					state: t
				}
			} }
		});
	}, [
		_,
		C,
		v,
		o.id,
		l.id
	]);
	i(() => {
		y.status === "success" && !S.current && (S.current = !0, w(y.code, y.state));
	}, [y, w]);
	let T = () => {
		S.current = !1, b({
			authUrl: h.authUrl,
			state: h.state
		});
	}, E = () => {
		x(), S.current = !1, b({
			authUrl: h.authUrl,
			state: h.state
		});
	};
	return h.status === "success" ? /* @__PURE__ */ s(e, { children: /* @__PURE__ */ s(d, {
		status: "success",
		provider: h.provider,
		message: h.message
	}) }) : h.status === "error" ? /* @__PURE__ */ c(e, { children: [/* @__PURE__ */ s(d, {
		status: "error",
		provider: h.provider,
		message: h.message
	}), m && /* @__PURE__ */ s(f, { onClick: E })] }) : /* @__PURE__ */ s(e, { children: /* @__PURE__ */ s(p, {
		result: y,
		provider: h.provider,
		isActive: m,
		isSubmitting: v.isPending,
		onSignIn: T,
		onRetry: E
	}) });
}, u = ({ provider: e }) => /* @__PURE__ */ s(o, { children: e.charAt(0).toUpperCase() + e.slice(1) }), d = ({ status: e, provider: t, message: n }) => /* @__PURE__ */ s("div", {
	className: "flex items-center gap-3 py-1",
	children: e === "success" ? /* @__PURE__ */ c(o, { children: [/* @__PURE__ */ s("span", {
		className: "flex h-6 w-6 items-center justify-center rounded-full bg-green-100 text-green-600 text-sm",
		children: "✓"
	}), /* @__PURE__ */ c("div", { children: [/* @__PURE__ */ c("div", {
		className: "text-sm font-medium",
		children: ["Connected to ", /* @__PURE__ */ s(u, { provider: t })]
	}), n && /* @__PURE__ */ s("div", {
		className: "text-xs text-gray-500 mt-0.5",
		children: n
	})] })] }) : /* @__PURE__ */ c(o, { children: [/* @__PURE__ */ s("span", {
		className: "flex h-6 w-6 items-center justify-center rounded-full bg-red-100 text-red-600 text-sm",
		children: "✗"
	}), /* @__PURE__ */ c("div", { children: [/* @__PURE__ */ s("div", {
		className: "text-sm font-medium",
		children: "Authentication failed"
	}), n && /* @__PURE__ */ s("div", {
		className: "text-xs text-red-500 mt-0.5",
		children: n
	})] })] })
}), f = ({ onClick: e }) => /* @__PURE__ */ s("button", {
	onClick: e,
	className: "mt-3 rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50",
	children: "Try again"
}), p = ({ result: e, provider: t, isActive: n, isSubmitting: r, onSignIn: i, onRetry: a }) => {
	switch (e.status) {
		case "idle": return /* @__PURE__ */ c("div", {
			className: "py-1",
			children: [
				/* @__PURE__ */ c("div", {
					className: "text-sm font-medium mb-1",
					children: [
						"Connect your ",
						/* @__PURE__ */ s(u, { provider: t }),
						" account"
					]
				}),
				/* @__PURE__ */ c("div", {
					className: "text-xs text-gray-500 mb-3",
					children: [
						"This workflow needs access to your ",
						/* @__PURE__ */ s(u, { provider: t }),
						" resources."
					]
				}),
				n && /* @__PURE__ */ c("button", {
					onClick: i,
					className: "rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50",
					children: ["Sign in with ", /* @__PURE__ */ s(u, { provider: t })]
				})
			]
		});
		case "pending": return /* @__PURE__ */ c("div", {
			className: "py-1",
			children: [
				/* @__PURE__ */ c("div", {
					className: "flex items-center gap-3",
					children: [/* @__PURE__ */ s("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-gray-600" }), /* @__PURE__ */ s("div", {
						className: "text-sm font-medium",
						children: "Waiting for authentication..."
					})]
				}),
				/* @__PURE__ */ s("div", {
					className: "text-xs text-gray-500 mt-2",
					children: "Complete the sign-in in the popup window."
				}),
				n && /* @__PURE__ */ s("div", {
					className: "mt-3 flex gap-2",
					children: /* @__PURE__ */ s("button", {
						onClick: a,
						className: "rounded-md border border-gray-300 bg-white px-3 py-1.5 text-xs font-medium text-gray-600 hover:bg-gray-50",
						children: "Retry"
					})
				})
			]
		});
		case "success": return /* @__PURE__ */ s("div", {
			className: "py-1",
			children: r ? /* @__PURE__ */ c("div", {
				className: "flex items-center gap-3",
				children: [/* @__PURE__ */ s("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-gray-600" }), /* @__PURE__ */ s("div", {
					className: "text-sm font-medium",
					children: "Completing authentication..."
				})]
			}) : /* @__PURE__ */ s(d, {
				status: "success",
				provider: t
			})
		});
		case "error": return /* @__PURE__ */ c("div", {
			className: "py-1",
			children: [/* @__PURE__ */ s(d, {
				status: "error",
				provider: t,
				message: e.error
			}), n && /* @__PURE__ */ s(f, { onClick: a })]
		});
		case "timeout": return /* @__PURE__ */ c("div", {
			className: "py-1",
			children: [/* @__PURE__ */ s(d, {
				status: "error",
				provider: t,
				message: "Authentication timed out."
			}), n && /* @__PURE__ */ s(f, { onClick: a })]
		});
		case "blocked": return /* @__PURE__ */ c("div", {
			className: "py-1",
			children: [/* @__PURE__ */ s(d, {
				status: "error",
				provider: t,
				message: "Popup was blocked by the browser. Please allow popups for this site and try again."
			}), n && /* @__PURE__ */ s(f, { onClick: a })]
		});
	}
};
//#endregion
export { l as default };
