import { useFetchWorkflowsByPipeline } from "../hooks/useWorkflows.js";
import ErrorSnackbar_default from "../components/snackbars/ErrorSnackbar.js";
import LoadingCentered_default from "../components/LoadingCentered.js";
import { usePipeline } from "../hooks/usePipelines.js";
import WorkflowItem_default from "../features/workbench/WorkflowItem.js";
import { requireParam } from "../lib/requireParam.js";
import { c } from "react/compiler-runtime";
import { useEffect, useRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { useParams } from "react-router-dom";
var EMBED_MESSAGE_TYPE = "loopstack:embed:workflow-completed", EMBED_RESIZE_MESSAGE_TYPE = "loopstack:embed:resize";
function EmbedWorkbenchPage() {
	let _ = c(24), v = useParams(), y;
	_[0] === v ? y = _[1] : (y = requireParam(v, "pipelineId"), _[0] = v, _[1] = y);
	let b = y, x = useRef(null), S = usePipeline(b), C = useFetchWorkflowsByPipeline(b), w = useRef(!1), T, E;
	_[2] !== C.data || _[3] !== b ? (T = () => {
		!C.data || w.current || C.data.length > 0 && C.data.every(_temp) && window.parent !== window && (w.current = !0, window.parent.postMessage({
			type: EMBED_MESSAGE_TYPE,
			pipelineId: b
		}, window.location.origin));
	}, E = [C.data, b], _[2] = C.data, _[3] = b, _[4] = T, _[5] = E) : (T = _[4], E = _[5]), useEffect(T, E);
	let D, O;
	_[6] === b ? (D = _[7], O = _[8]) : (D = () => {
		if (window.parent === window || !x.current) return;
		let t = new ResizeObserver(() => {
			if (!x.current) return;
			let t = Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);
			window.parent.postMessage({
				type: EMBED_RESIZE_MESSAGE_TYPE,
				pipelineId: b,
				height: t
			}, window.location.origin);
		});
		return t.observe(x.current), () => t.disconnect();
	}, O = [b], _[6] = b, _[7] = D, _[8] = O), useEffect(D, O);
	let k = _temp2, A;
	_[9] === Symbol.for("react.memo_cache_sentinel") ? (A = {
		enableDebugMode: !1,
		showFullMessageHistory: !1
	}, _[9] = A) : A = _[9];
	let j = A, M;
	_[10] === S.error ? M = _[11] : (M = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: S.error }), _[10] = S.error, _[11] = M);
	let N;
	_[12] === C.error ? N = _[13] : (N = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: C.error }), _[12] = C.error, _[13] = N);
	let P = S.isLoading || C.isLoading, F;
	_[14] !== S.data || _[15] !== C.data ? (F = S.data && C.data ? C.data.map((t) => /* @__PURE__ */ jsx(WorkflowItem_default, {
		pipeline: S.data,
		workflowId: t.id,
		scrollTo: k,
		settings: j,
		embed: !0
	}, t.id)) : null, _[14] = S.data, _[15] = C.data, _[16] = F) : F = _[16];
	let I;
	_[17] !== P || _[18] !== F ? (I = /* @__PURE__ */ jsx(LoadingCentered_default, {
		loading: P,
		children: F
	}), _[17] = P, _[18] = F, _[19] = I) : I = _[19];
	let L;
	return _[20] !== I || _[21] !== M || _[22] !== N ? (L = /* @__PURE__ */ jsxs("div", {
		ref: x,
		className: "overflow-hidden px-6 py-4",
		children: [
			M,
			N,
			I
		]
	}), _[20] = I, _[21] = M, _[22] = N, _[23] = L) : L = _[23], L;
}
function _temp2() {}
function _temp(t) {
	return t.status === "completed";
}
export { EmbedWorkbenchPage as default };
