import CompletionMessagePaper_default from "../../components/messages/CompletionMessagePaper.js";
import { useRunPipeline } from "../../hooks/useProcessor.js";
import { useOAuthPopup } from "./useOAuthPopup.js";
import { c } from "react/compiler-runtime";
import React, { useCallback, useEffect, useRef } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
var OAuthPromptRenderer = ({ pipeline: l, workflow: u, document: m, isActive: h }) => {
	let g = m.content, _ = (m.ui?.actions ?? []).find((e) => e.transition)?.transition, v = useRunPipeline(), { result: y, open: b, reset: x } = useOAuthPopup(), S = useRef(!1), C = u.availableTransitions?.map((e) => e.id) ?? [], w = useCallback((e, o) => {
		!_ || !C.includes(_) || v.mutate({
			pipelineId: l.id,
			runPipelinePayloadDto: { transition: {
				id: _,
				workflowId: u.id,
				payload: {
					code: e,
					state: o
				}
			} }
		});
	}, [
		_,
		C,
		v,
		l.id,
		u.id
	]);
	useEffect(() => {
		y.status === "success" && !S.current && (S.current = !0, w(y.code, y.state));
	}, [y, w]);
	let T = () => {
		S.current = !1, b({
			authUrl: g.authUrl,
			state: g.state
		});
	}, E = () => {
		x(), S.current = !1, b({
			authUrl: g.authUrl,
			state: g.state
		});
	};
	return g.status === "success" ? /* @__PURE__ */ jsx(CompletionMessagePaper_default, { children: /* @__PURE__ */ jsx(StatusDisplay, {
		status: "success",
		provider: g.provider,
		message: g.message
	}) }) : g.status === "error" ? /* @__PURE__ */ jsxs(CompletionMessagePaper_default, { children: [/* @__PURE__ */ jsx(StatusDisplay, {
		status: "error",
		provider: g.provider,
		message: g.message
	}), h && /* @__PURE__ */ jsx(RetryButton, { onClick: E })] }) : /* @__PURE__ */ jsx(CompletionMessagePaper_default, { children: /* @__PURE__ */ jsx(PopupResultView, {
		result: y,
		provider: g.provider,
		isActive: h,
		isSubmitting: v.isPending,
		onSignIn: T,
		onRetry: E
	}) });
}, ProviderLabel = (e) => {
	let o = c(2), { provider: s } = e, u = s.charAt(0).toUpperCase() + s.slice(1), d;
	return o[0] === u ? d = o[1] : (d = /* @__PURE__ */ jsx(Fragment$1, { children: u }), o[0] = u, o[1] = d), d;
}, StatusDisplay = (e) => {
	let o = c(4), { status: s, provider: u, message: d } = e, f;
	return o[0] !== d || o[1] !== u || o[2] !== s ? (f = /* @__PURE__ */ jsx("div", {
		className: "flex items-center gap-3 py-1",
		children: s === "success" ? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("span", {
			className: "flex h-6 w-6 items-center justify-center rounded-full bg-green-100 text-green-600 text-sm",
			children: "✓"
		}), /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsxs("div", {
			className: "text-sm font-medium",
			children: ["Connected to ", /* @__PURE__ */ jsx(ProviderLabel, { provider: u })]
		}), d && /* @__PURE__ */ jsx("div", {
			className: "text-xs text-gray-500 mt-0.5",
			children: d
		})] })] }) : /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("span", {
			className: "flex h-6 w-6 items-center justify-center rounded-full bg-red-100 text-red-600 text-sm",
			children: "✗"
		}), /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("div", {
			className: "text-sm font-medium",
			children: "Authentication failed"
		}), d && /* @__PURE__ */ jsx("div", {
			className: "text-xs text-red-500 mt-0.5",
			children: d
		})] })] })
	}), o[0] = d, o[1] = u, o[2] = s, o[3] = f) : f = o[3], f;
}, RetryButton = (e) => {
	let o = c(2), { onClick: s } = e, u;
	return o[0] === s ? u = o[1] : (u = /* @__PURE__ */ jsx("button", {
		onClick: s,
		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"
	}), o[0] = s, o[1] = u), u;
}, PopupResultView = (e) => {
	let o = c(47), { result: s, provider: u, isActive: d, isSubmitting: f, onSignIn: p, onRetry: m } = e;
	switch (s.status) {
		case "idle": {
			let e;
			o[0] === u ? e = o[1] : (e = /* @__PURE__ */ jsxs("div", {
				className: "text-sm font-medium mb-1",
				children: [
					"Connect your ",
					/* @__PURE__ */ jsx(ProviderLabel, { provider: u }),
					" account"
				]
			}), o[0] = u, o[1] = e);
			let s;
			o[2] === u ? s = o[3] : (s = /* @__PURE__ */ jsxs("div", {
				className: "text-xs text-gray-500 mb-3",
				children: [
					"This workflow needs access to your ",
					/* @__PURE__ */ jsx(ProviderLabel, { provider: u }),
					" resources."
				]
			}), o[2] = u, o[3] = s);
			let l;
			o[4] !== d || o[5] !== p || o[6] !== u ? (l = d && /* @__PURE__ */ jsxs("button", {
				onClick: p,
				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__ */ jsx(ProviderLabel, { provider: u })]
			}), o[4] = d, o[5] = p, o[6] = u, o[7] = l) : l = o[7];
			let f;
			return o[8] !== e || o[9] !== s || o[10] !== l ? (f = /* @__PURE__ */ jsxs("div", {
				className: "py-1",
				children: [
					e,
					s,
					l
				]
			}), o[8] = e, o[9] = s, o[10] = l, o[11] = f) : f = o[11], f;
		}
		case "pending": {
			let e, s;
			o[12] === Symbol.for("react.memo_cache_sentinel") ? (e = /* @__PURE__ */ jsxs("div", {
				className: "flex items-center gap-3",
				children: [/* @__PURE__ */ jsx("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-gray-600" }), /* @__PURE__ */ jsx("div", {
					className: "text-sm font-medium",
					children: "Waiting for authentication..."
				})]
			}), s = /* @__PURE__ */ jsx("div", {
				className: "text-xs text-gray-500 mt-2",
				children: "Complete the sign-in in the popup window."
			}), o[12] = e, o[13] = s) : (e = o[12], s = o[13]);
			let l;
			o[14] !== d || o[15] !== m ? (l = d && /* @__PURE__ */ jsx("div", {
				className: "mt-3 flex gap-2",
				children: /* @__PURE__ */ jsx("button", {
					onClick: m,
					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"
				})
			}), o[14] = d, o[15] = m, o[16] = l) : l = o[16];
			let u;
			return o[17] === l ? u = o[18] : (u = /* @__PURE__ */ jsxs("div", {
				className: "py-1",
				children: [
					e,
					s,
					l
				]
			}), o[17] = l, o[18] = u), u;
		}
		case "success": {
			let e;
			return o[19] !== f || o[20] !== u ? (e = /* @__PURE__ */ jsx("div", {
				className: "py-1",
				children: f ? /* @__PURE__ */ jsxs("div", {
					className: "flex items-center gap-3",
					children: [/* @__PURE__ */ jsx("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-gray-600" }), /* @__PURE__ */ jsx("div", {
						className: "text-sm font-medium",
						children: "Completing authentication..."
					})]
				}) : /* @__PURE__ */ jsx(StatusDisplay, {
					status: "success",
					provider: u
				})
			}), o[19] = f, o[20] = u, o[21] = e) : e = o[21], e;
		}
		case "error": {
			let e;
			o[22] !== u || o[23] !== s.error ? (e = /* @__PURE__ */ jsx(StatusDisplay, {
				status: "error",
				provider: u,
				message: s.error
			}), o[22] = u, o[23] = s.error, o[24] = e) : e = o[24];
			let l;
			o[25] !== d || o[26] !== m ? (l = d && /* @__PURE__ */ jsx(RetryButton, { onClick: m }), o[25] = d, o[26] = m, o[27] = l) : l = o[27];
			let f;
			return o[28] !== e || o[29] !== l ? (f = /* @__PURE__ */ jsxs("div", {
				className: "py-1",
				children: [e, l]
			}), o[28] = e, o[29] = l, o[30] = f) : f = o[30], f;
		}
		case "timeout": {
			let e;
			o[31] === u ? e = o[32] : (e = /* @__PURE__ */ jsx(StatusDisplay, {
				status: "error",
				provider: u,
				message: "Authentication timed out."
			}), o[31] = u, o[32] = e);
			let s;
			o[33] !== d || o[34] !== m ? (s = d && /* @__PURE__ */ jsx(RetryButton, { onClick: m }), o[33] = d, o[34] = m, o[35] = s) : s = o[35];
			let l;
			return o[36] !== e || o[37] !== s ? (l = /* @__PURE__ */ jsxs("div", {
				className: "py-1",
				children: [e, s]
			}), o[36] = e, o[37] = s, o[38] = l) : l = o[38], l;
		}
		case "blocked": {
			let e;
			o[39] === u ? e = o[40] : (e = /* @__PURE__ */ jsx(StatusDisplay, {
				status: "error",
				provider: u,
				message: "Popup was blocked by the browser. Please allow popups for this site and try again."
			}), o[39] = u, o[40] = e);
			let s;
			o[41] !== d || o[42] !== m ? (s = d && /* @__PURE__ */ jsx(RetryButton, { onClick: m }), o[41] = d, o[42] = m, o[43] = s) : s = o[43];
			let l;
			return o[44] !== e || o[45] !== s ? (l = /* @__PURE__ */ jsxs("div", {
				className: "py-1",
				children: [e, s]
			}), o[44] = e, o[45] = s, o[46] = l) : l = o[46], l;
		}
	}
}, OAuthPromptRenderer_default = OAuthPromptRenderer;
export { OAuthPromptRenderer_default as default };
