import { useFetchWorkflowsByPipeline } from "../../hooks/useWorkflows.js";
import { cn } from "../../lib/utils.js";
import { Button } from "../../components/ui/button.js";
import ErrorSnackbar_default from "../../components/snackbars/ErrorSnackbar.js";
import LoadingCentered_default from "../../components/LoadingCentered.js";
import WorkflowItem_default from "./WorkflowItem.js";
import { useScrollToBottom } from "./hooks/useAutoScrollBottom.js";
import { useIntersectionObserver } from "./hooks/useIntersectionObserver.js";
import { useScrollToListItem } from "./hooks/useScrollToListItem.js";
import { WorkbenchContextProvider } from "./providers/WorkbenchContextProvider.js";
import WorkbenchSettingsModal_default from "./components/WorkbenchSettingsModal.js";
import { c } from "react/compiler-runtime";
import React, { useContext, useEffect, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { ArrowDownIcon, ChevronDownIcon, ChevronRightIcon, LayersIcon } from "lucide-react";
var WorkflowList_default = (S) => {
	let C = c(33), { pipeline: w } = S, T = useFetchWorkflowsByPipeline(w.id), [E, D] = useState(!1), O;
	C[0] === Symbol.for("react.memo_cache_sentinel") ? (O = {}, C[0] = O) : O = C[0];
	let [k, A] = useState(O), j;
	C[1] === Symbol.for("react.memo_cache_sentinel") ? (j = {
		enableDebugMode: !1,
		showFullMessageHistory: !1
	}, C[1] = j) : j = C[1];
	let [M, N] = useState(j), { activeId: P, observe: F } = useIntersectionObserver("0px 0px 0px 0px"), { listRef: I, scrollTo: L } = useScrollToListItem(), { canScrollDown: R, scrollToBottom: z } = useScrollToBottom(), B = useContext(WorkbenchContextProvider), V, H;
	C[2] !== P || C[3] !== B ? (V = () => {
		B && B.setActiveSectionId && B.state.activeSectionId !== P && (B.setActiveSectionId(P), P && A((t) => ({
			...t,
			[P]: !0
		})));
	}, H = [P, B], C[2] = P, C[3] = B, C[4] = V, C[5] = H) : (V = C[4], H = C[5]), useEffect(V, H);
	let U, W;
	C[6] !== P || C[7] !== T.data ? (U = () => {
		if (T.data && T.data.length > 0) {
			let t = T.data.length - 1, y = T.data[t], b = `section-${y.index}-${y.id}`;
			A({
				[b]: !0,
				...P && P !== b ? { [P]: !0 } : {}
			});
		}
	}, W = [T.data, P], C[6] = P, C[7] = T.data, C[8] = U, C[9] = W) : (U = C[8], W = C[9]), useEffect(U, W);
	let G;
	C[10] === Symbol.for("react.memo_cache_sentinel") ? (G = (t) => {
		A((y) => ({
			...y,
			[t]: !y[t]
		}));
	}, C[10] = G) : G = C[10];
	let K = G, q;
	C[11] !== R || C[12] !== z ? (q = R && /* @__PURE__ */ jsx(Button, {
		variant: "outline",
		size: "icon",
		onClick: z,
		className: "bg-background/80 fixed right-[calc(var(--sidebar-width)+1.5rem)] bottom-6 z-50 rounded-full shadow-md backdrop-blur-sm",
		children: /* @__PURE__ */ jsx(ArrowDownIcon, { className: "size-4" })
	}), C[11] = R, C[12] = z, C[13] = q) : q = C[13];
	let J;
	C[14] === T.isLoading ? J = C[15] : (J = /* @__PURE__ */ jsx(LoadingCentered_default, { loading: T.isLoading }), C[14] = T.isLoading, C[15] = J);
	let Y;
	C[16] === T.error ? Y = C[17] : (Y = /* @__PURE__ */ jsx(ErrorSnackbar_default, { error: T.error }), C[16] = T.error, C[17] = Y);
	let X;
	C[18] !== P || C[19] !== k || C[20] !== T.data || C[21] !== I || C[22] !== F || C[23] !== E || C[24] !== w || C[25] !== L || C[26] !== M ? (X = T.data ? /* @__PURE__ */ jsx("div", {
		className: "mb-10",
		ref: I,
		children: /* @__PURE__ */ jsx("div", { children: T.data.map((t) => {
			let b = `section-${t.index}-${t.id}`, x = P === b;
			return /* @__PURE__ */ jsxs("div", {
				ref: (t) => F(t),
				"data-id": b,
				className: "space-y-0",
				children: [/* @__PURE__ */ jsx("div", {
					className: "bg-background/95 supports-[backdrop-filter]:bg-background/60 sticky top-0 z-10 backdrop-blur",
					children: /* @__PURE__ */ jsxs("div", {
						className: "mb-4 flex items-center gap-2 py-2 px-3 cursor-pointer border-b border-border bg-muted/50 rounded-t-sm",
						onClick: () => K(b),
						children: [
							k[b] ? /* @__PURE__ */ jsx(ChevronDownIcon, { className: cn("size-4", x ? "text-primary" : "text-muted-foreground") }) : /* @__PURE__ */ jsx(ChevronRightIcon, { className: cn("size-4", x ? "text-primary" : "text-muted-foreground") }),
							/* @__PURE__ */ jsx(LayersIcon, { className: cn("size-4", x ? "text-primary" : "text-muted-foreground") }),
							/* @__PURE__ */ jsx("span", {
								className: cn("text-base font-medium transition-colors flex-1", x ? "text-primary" : "text-muted-foreground text-gray-400"),
								children: t.title ?? t.blockName
							}),
							x && /* @__PURE__ */ jsx(WorkbenchSettingsModal_default, {
								settings: M,
								onSettingsChange: N,
								open: E,
								onOpenChange: D
							})
						]
					})
				}), k[b] && /* @__PURE__ */ jsx("div", {
					className: "max-w-4xl pl-9",
					children: /* @__PURE__ */ jsx(WorkflowItem_default, {
						pipeline: w,
						workflowId: t.id,
						scrollTo: L,
						settings: M
					})
				})]
			}, t.id);
		}) })
	}) : null, C[18] = P, C[19] = k, C[20] = T.data, C[21] = I, C[22] = F, C[23] = E, C[24] = w, C[25] = L, C[26] = M, C[27] = X) : X = C[27];
	let Z;
	return C[28] !== Y || C[29] !== X || C[30] !== q || C[31] !== J ? (Z = /* @__PURE__ */ jsxs("div", { children: [
		q,
		J,
		Y,
		X
	] }), C[28] = Y, C[29] = X, C[30] = q, C[31] = J, C[32] = Z) : Z = C[32], Z;
};
export { WorkflowList_default as default };
