import { Button as e } from "../../../components/ui/button.js";
import t from "../../../components/dynamic-form/Form.js";
import { useRunWorkflow as n } from "../../../hooks/useProcessor.js";
import { useEffect as r } from "react";
import { jsx as i, jsxs as a } from "react/jsx-runtime";
import { Loader2 as o } from "lucide-react";
import { useForm as s } from "react-hook-form";
import "zod";
//#region src/features/documents/renderers/DocumentFormRenderer.tsx
function c(e) {
	let t = e, n = (t?.widgets)?.find((e) => e.widget === "form");
	if (n?.options) {
		let e = n.options;
		return {
			formUi: { form: {
				properties: e.properties,
				order: e.order,
				title: e.title,
				description: e.description,
				disabled: e.disabled
			} },
			actions: e.actions ?? []
		};
	}
	return {
		formUi: e,
		actions: (t?.actions ?? []).map((e) => ({
			type: "button",
			transition: e.options?.transition,
			label: e.options?.label
		}))
	};
}
var l = ({ parentWorkflow: l, workflow: u, document: d, enabled: f, viewOnly: p }) => {
	let m = n(), h = s({
		defaultValues: d.schema.type === "object" ? d.content : { raw: d.content },
		mode: "onChange"
	});
	r(() => {
		d.validationError ? d.validationError.issues.forEach((e) => {
			let t = e.path.join(".");
			h.setError(t, {
				type: e.code,
				message: e.message
			});
		}) : h.clearErrors();
	}, [d.validationError, h]);
	let g = u.availableTransitions?.map((e) => e.id) ?? [], _ = (e, t) => {
		if (!g.includes(e)) {
			console.error(`Transition ${e} not available.`);
			return;
		}
		m.mutate({
			workflowId: l.id,
			runWorkflowPayloadDto: { transition: {
				id: e,
				workflowId: u.id,
				payload: t
			} }
		});
	}, v = (e) => (t) => {
		d.schema.type === "object" ? _(e, t) : _(e, t.raw);
	}, y = (e) => {
		e.transition && h.handleSubmit((t) => v(e.transition)(t), (e) => console.error("[DocumentFormRenderer] validation failed", e))();
	}, { formUi: b, actions: x } = c(d.ui), S = d.schema, C = !!b?.form?.disabled, w = !f || C || !1;
	return /* @__PURE__ */ i("div", {
		className: "flex",
		children: /* @__PURE__ */ i(t, {
			form: h,
			schema: S,
			ui: b ?? void 0,
			mimeType: d.meta?.mimeType,
			disabled: w,
			viewOnly: p,
			actions: !p && x.length > 0 ? /* @__PURE__ */ i("div", {
				className: "flex w-full flex-col items-end gap-4",
				children: x.map((t, n) => {
					let r = w || t.transition !== void 0 && !g.includes(t.transition);
					return /* @__PURE__ */ a(e, {
						type: "button",
						variant: t.variant ?? "default",
						disabled: r || m.isPending,
						onClick: () => y(t),
						className: t.type === "button-full-w" ? "w-full" : "w-48",
						...t.props ?? {},
						children: [m.isPending && /* @__PURE__ */ i(o, { className: "h-4 w-4 animate-spin" }), t.label ?? "Submit"]
					}, n);
				})
			}) : void 0
		})
	});
};
//#endregion
export { l as default };
