import { cn } from "../../../lib/utils.js";
import { ScrollArea } from "../../../components/ui/scroll-area.js";
import MarkdownContent_default from "../../../components/dynamic-form/MarkdownContent.js";
import { ReactFlowProvider } from "../../../node_modules/@xyflow/react/dist/esm/index.js";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "../../../components/ui/tabs.js";
import ConfigFlowViewer_default from "../../debug/components/ConfigFlowViewer.js";
import { c } from "react/compiler-runtime";
import { jsx, jsxs } from "react/jsx-runtime";
import { Loader2 } from "lucide-react";
import { Prism } from "react-syntax-highlighter";
import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";
var BINARY_EXTENSIONS = new Set([
	".ico",
	".svg",
	".png",
	".jpg",
	".jpeg",
	".gif",
	".webp",
	".woff",
	".woff2",
	".ttf",
	".eot",
	".bin",
	".exe",
	".dll",
	".so",
	".dylib"
]);
function isBinaryFileName(e) {
	let h = e.includes(".") ? e.slice(e.lastIndexOf(".")).toLowerCase() : "";
	return BINARY_EXTENSIONS.has(h);
}
var EXTENSION_LANGUAGE = {
	".ts": "typescript",
	".tsx": "tsx",
	".js": "javascript",
	".jsx": "jsx",
	".mjs": "javascript",
	".cjs": "javascript",
	".json": "json",
	".jsonc": "json",
	".md": "markdown",
	".mdx": "markdown",
	".css": "css",
	".scss": "scss",
	".sass": "sass",
	".less": "less",
	".html": "html",
	".htm": "html",
	".xml": "xml",
	".yaml": "yaml",
	".yml": "yaml",
	".toml": "toml",
	".sh": "bash",
	".bash": "bash",
	".zsh": "bash",
	".fish": "bash",
	".txt": "plaintext",
	".log": "plaintext",
	".env": "plaintext",
	".gitignore": "plaintext",
	".dockerfile": "dockerfile",
	".dockerignore": "plaintext"
};
function getLanguageForFileName(e) {
	return EXTENSION_LANGUAGE[e.includes(".") ? e.slice(e.lastIndexOf(".")).toLowerCase() : ""] ?? "plaintext";
}
function FileContentViewer(v) {
	let y = c(34), { selectedFile: b, content: x, workflowConfig: S, isLoading: C, className: w } = v, T = S === void 0 ? null : S, E = C === void 0 ? !1 : C;
	if (!b) {
		let h;
		y[0] === w ? h = y[1] : (h = cn("flex flex-1 items-center justify-center bg-background", w), y[0] = w, y[1] = h);
		let g;
		y[2] === Symbol.for("react.memo_cache_sentinel") ? (g = /* @__PURE__ */ jsx("p", {
			className: "text-sm text-muted-foreground",
			children: "Select a tab to view its content"
		}), y[2] = g) : g = y[2];
		let _;
		return y[3] === h ? _ = y[4] : (_ = /* @__PURE__ */ jsx("div", {
			className: h,
			children: g
		}), y[3] = h, y[4] = _), _;
	}
	let D = isBinaryFileName(b.name), O = x ?? (D ? null : `// ${b.path ?? b.name}\n\n(No content available)`);
	if (D && x == null) {
		let h;
		y[5] === w ? h = y[6] : (h = cn("flex flex-1 flex-col rounded-lg border bg-background", w), y[5] = w, y[6] = h);
		let g;
		y[7] === Symbol.for("react.memo_cache_sentinel") ? (g = /* @__PURE__ */ jsx("div", {
			className: "flex flex-1 items-center justify-center p-8",
			children: /* @__PURE__ */ jsx("p", {
				className: "text-sm text-muted-foreground",
				children: "Binary file — preview not available"
			})
		}), y[7] = g) : g = y[7];
		let _;
		return y[8] === h ? _ = y[9] : (_ = /* @__PURE__ */ jsx("div", {
			className: h,
			children: g
		}), y[8] = h, y[9] = _), _;
	}
	if (E) {
		let h;
		y[10] === w ? h = y[11] : (h = cn("flex flex-1 flex-col rounded-lg border bg-background", w), y[10] = w, y[11] = h);
		let g;
		y[12] === Symbol.for("react.memo_cache_sentinel") ? (g = /* @__PURE__ */ jsx("div", {
			className: "flex justify-center p-8",
			children: /* @__PURE__ */ jsx(Loader2, { className: "h-6 w-6 animate-spin text-muted-foreground" })
		}), y[12] = g) : g = y[12];
		let _;
		return y[13] === h ? _ = y[14] : (_ = /* @__PURE__ */ jsx("div", {
			className: h,
			children: g
		}), y[13] = h, y[14] = _), _;
	}
	let k;
	y[15] === b.name ? k = y[16] : (k = getLanguageForFileName(b.name), y[15] = b.name, y[16] = k);
	let A = k, j;
	y[17] !== A || y[18] !== b.name ? (j = A === "markdown" || b.name.endsWith(".md") || b.name.endsWith(".mdx"), y[17] = A, y[18] = b.name, y[19] = j) : j = y[19];
	let M = j, N;
	y[20] !== A || y[21] !== b.name ? (N = A === "yaml" || b.name.endsWith(".yaml") || b.name.endsWith(".yml"), y[20] = A, y[21] = b.name, y[22] = N) : N = y[22];
	let P = N && T !== null, F;
	y[23] === w ? F = y[24] : (F = cn("flex h-full flex-col overflow-hidden rounded-lg border bg-background", w), y[23] = w, y[24] = F);
	let I;
	y[25] !== O || y[26] !== M || y[27] !== P || y[28] !== A || y[29] !== T ? (I = M && O ? /* @__PURE__ */ jsxs(Tabs, {
		defaultValue: "rendered",
		className: "flex h-full flex-col overflow-hidden",
		children: [
			/* @__PURE__ */ jsxs(TabsList, {
				className: "shrink-0 mx-2 mt-2",
				children: [/* @__PURE__ */ jsx(TabsTrigger, {
					value: "rendered",
					children: "Rendered"
				}), /* @__PURE__ */ jsx(TabsTrigger, {
					value: "unrendered",
					children: "Unrendered"
				})]
			}),
			/* @__PURE__ */ jsx(TabsContent, {
				value: "rendered",
				className: "flex-1 min-h-0 overflow-hidden mt-2",
				children: /* @__PURE__ */ jsx(ScrollArea, {
					className: "h-full",
					children: /* @__PURE__ */ jsx("div", {
						className: "p-4",
						children: /* @__PURE__ */ jsx(MarkdownContent_default, { content: O })
					})
				})
			}),
			/* @__PURE__ */ jsx(TabsContent, {
				value: "unrendered",
				className: "flex-1 min-h-0 overflow-hidden mt-0",
				children: /* @__PURE__ */ jsx(ScrollArea, {
					className: "flex-1 min-h-0",
					children: /* @__PURE__ */ jsx(Prism, {
						language: A,
						style: vscDarkPlus,
						customStyle: {
							margin: 0,
							padding: "1rem",
							fontSize: "13px"
						},
						showLineNumbers: !0,
						PreTag: "div",
						codeTagProps: { style: { fontFamily: "inherit" } },
						children: O ?? ""
					})
				})
			})
		]
	}) : P && T ? /* @__PURE__ */ jsxs(Tabs, {
		defaultValue: "code",
		className: "flex h-full flex-col overflow-hidden",
		children: [
			/* @__PURE__ */ jsxs(TabsList, {
				className: "shrink-0 mx-2 mt-2",
				children: [/* @__PURE__ */ jsx(TabsTrigger, {
					value: "code",
					children: "Code"
				}), /* @__PURE__ */ jsx(TabsTrigger, {
					value: "flow",
					children: "Flow Diagram"
				})]
			}),
			/* @__PURE__ */ jsx(TabsContent, {
				value: "code",
				className: "flex-1 min-h-0 overflow-hidden mt-0",
				children: /* @__PURE__ */ jsx(ScrollArea, {
					className: "flex-1 min-h-0",
					children: /* @__PURE__ */ jsx(Prism, {
						language: A,
						style: vscDarkPlus,
						customStyle: {
							margin: 0,
							padding: "1rem",
							fontSize: "13px"
						},
						showLineNumbers: !0,
						PreTag: "div",
						codeTagProps: { style: { fontFamily: "inherit" } },
						children: O ?? ""
					})
				})
			}),
			/* @__PURE__ */ jsx(TabsContent, {
				value: "flow",
				className: "flex-1 min-h-0 overflow-hidden mt-0",
				children: /* @__PURE__ */ jsx("div", {
					className: "h-full w-full",
					children: /* @__PURE__ */ jsx(ReactFlowProvider, { children: /* @__PURE__ */ jsx(ConfigFlowViewer_default, { config: T }) })
				})
			})
		]
	}) : /* @__PURE__ */ jsx(ScrollArea, {
		className: "flex-1 min-h-0",
		children: /* @__PURE__ */ jsx(Prism, {
			language: A,
			style: vscDarkPlus,
			customStyle: {
				margin: 0,
				padding: "1rem",
				fontSize: "13px"
			},
			showLineNumbers: !0,
			PreTag: "div",
			codeTagProps: { style: { fontFamily: "inherit" } },
			children: O ?? ""
		})
	}), y[25] = O, y[26] = M, y[27] = P, y[28] = A, y[29] = T, y[30] = I) : I = y[30];
	let L;
	return y[31] !== F || y[32] !== I ? (L = /* @__PURE__ */ jsx("div", {
		className: F,
		children: I
	}), y[31] = F, y[32] = I, y[33] = L) : L = y[33], L;
}
export { FileContentViewer };
