import { useFilterDocuments } from "../../hooks/useDocuments.js";
import { useWorkflow } from "../../hooks/useWorkflows.js";
import { cn } from "../../lib/utils.js";
import ErrorSnackbar_default from "../../components/snackbars/ErrorSnackbar.js";
import LoadingCentered_default from "../../components/LoadingCentered.js";
import { useRunPipeline } from "../../hooks/useProcessor.js";
import DocumentList_default from "./components/DocumentList.js";
import WorkflowForms_default from "./components/WorkflowForms.js";
import ErrorAlert_default from "../../components/content/ErrorAlert.js";
import WorkflowButtons_default from "./components/buttons/WorkflowButtons.js";
import React, { useCallback, useEffect, useMemo } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { useParams } from "react-router-dom";
var WorkflowItem_default = ({ pipeline: g, workflowId: _, scrollTo: v, settings: y, embed: b }) => {
	let { workflowId: x, clickId: S } = useParams(), C = useWorkflow(_), w = useFilterDocuments(_);
	useEffect(() => {
		x === _ && C.isSuccess && w.isSuccess && v(_);
	}, [
		C.isSuccess,
		w.isSuccess,
		_,
		x,
		S,
		v
	]);
	let T = useCallback((e) => {
		let p = e.meta, m = e.ui, h = p?.hidden || m?.hidden || !!p?.hideAtPlaces?.includes(C.data?.place ?? "");
		return !y.showFullMessageHistory && e.tags?.includes("internal") && (h = !0), !h;
	}, [C.data, y.showFullMessageHistory]), E = useMemo(() => w.data ? w.data.filter(T) : [], [w.data, T]), D = useRunPipeline(), O = (e, p) => {
		D.mutate({
			pipelineId: C.data.pipelineId,
			runPipelinePayloadDto: { transition: {
				id: e,
				workflowId: _,
				payload: p
			} }
		});
	}, k = D.isPending || C.data?.status === "running";
	return /* @__PURE__ */ jsxs("div", {
		className: cn("flex flex-col", b ? "p-0" : "min-h-[calc(100vh-16rem)] p-4"),
		children: [
			/* @__PURE__ */ jsx(LoadingCentered_default, { loading: C.isLoading || w.isLoading }),
			/* @__PURE__ */ jsx(ErrorSnackbar_default, { error: w.error }),
			/* @__PURE__ */ jsx(ErrorAlert_default, { error: C.data?.errorMessage }),
			C.isSuccess && /* @__PURE__ */ jsx(DocumentList_default, {
				pipeline: g,
				workflow: C.data,
				documents: E,
				scrollTo: v,
				settings: y,
				isLoading: k
			}),
			/* @__PURE__ */ jsx(LoadingCentered_default, { loading: k }),
			!!C.data && !b && /* @__PURE__ */ jsxs("div", {
				className: "mt-auto flex flex-col gap-6 pt-12",
				children: [/* @__PURE__ */ jsx(WorkflowForms_default, {
					workflow: C.data,
					onSubmit: O
				}), /* @__PURE__ */ jsx(WorkflowButtons_default, {
					pipeline: g,
					workflow: C.data
				})]
			})
		]
	});
};
export { WorkflowItem_default as default };
