"use client";
import { cn } from "./index3.js";
import { AICodeBlock } from "./index414.js";
import * as React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/organisms/ai/_shared/code-handler.tsx
var LazyAIMermaidDiagram = React.lazy(() => import("./index430.js"));
var LazyAIAsciiDiagram = React.lazy(() => import("./index411.js"));
var LazyAIChart = React.lazy(() => import("./index413.js"));
var LazyAITable = React.lazy(() => import("./index442.js"));
var LazyAIForm = React.lazy(() => import("./index427.js"));
var LazyAISelect = React.lazy(() => import("./index435.js"));
function LazyLoadingSkeleton() {
	return /* @__PURE__ */ jsx("div", {
		className: "my-4 flex items-center justify-center p-8 rounded-xl border border-border bg-muted animate-pulse",
		children: /* @__PURE__ */ jsx("div", { className: "h-4 w-32 bg-accent rounded" })
	});
}
var INTERACTIVE_LANGUAGES = [
	"chart",
	"select",
	"table",
	"form"
];
function isInteractiveLanguage(language) {
	return INTERACTIVE_LANGUAGES.includes(language);
}
var RENDER_ONLY_LANGUAGES = ["mermaid", "ascii"];
function isRenderOnlyLanguage(language) {
	return RENDER_ONLY_LANGUAGES.includes(language);
}
var INTERACTIVE_COMPONENTS = {
	chart: LazyAIChart,
	select: LazyAISelect,
	table: LazyAITable,
	form: LazyAIForm
};
var RENDER_ONLY_COMPONENTS = {
	mermaid: {
		Component: LazyAIMermaidDiagram,
		prop: "code"
	},
	ascii: {
		Component: LazyAIAsciiDiagram,
		prop: "content"
	}
};
var LOADING_FALLBACK = /* @__PURE__ */ jsx(LazyLoadingSkeleton, {});
var INVERTED_SH_STYLE = {
	"--sh-class": "#84caff",
	"--sh-identifier": "#fafaf9",
	"--sh-sign": "#d7d3d0",
	"--sh-property": "#84caff",
	"--sh-entity": "#84caff",
	"--sh-jsxliterals": "#84caff",
	"--sh-string": "#e7e5e4",
	"--sh-keyword": "#f670c7",
	"--sh-comment": "#75e0a7"
};
function renderInteractiveComponent(language, schema) {
	const Component = INTERACTIVE_COMPONENTS[language];
	return /* @__PURE__ */ jsx(React.Suspense, {
		fallback: LOADING_FALLBACK,
		children: /* @__PURE__ */ jsx(Component, { schema })
	});
}
function renderRenderOnlyComponent(language, content) {
	const { Component, prop } = RENDER_ONLY_COMPONENTS[language];
	return /* @__PURE__ */ jsx(React.Suspense, {
		fallback: LOADING_FALLBACK,
		children: /* @__PURE__ */ jsx(Component, { [prop]: content })
	});
}
function createCodeComponent(variant = "default") {
	const inv = variant === "inverted";
	return function UnifiedCodeBlock({ className, children }) {
		if (!className?.includes("language-")) return /* @__PURE__ */ jsx("code", {
			className: cn("px-1.5 py-0.5 rounded text-sm font-mono transition-colors", inv ? "bg-primary-foreground/15 text-primary-foreground" : "bg-accent text-foreground", className),
			children
		});
		const match = /language-(\w+)/.exec(className || "");
		const language = match ? match[1] : "plaintext";
		const content = String(children).replace(/\n$/, "");
		if (isRenderOnlyLanguage(language)) return renderRenderOnlyComponent(language, content);
		if (isInteractiveLanguage(language)) try {
			return renderInteractiveComponent(language, JSON.parse(content));
		} catch {
			return /* @__PURE__ */ jsxs("div", {
				className: cn("my-4 p-4 rounded-lg", inv ? "border border-primary-foreground/30 bg-primary-foreground/10 text-primary-foreground text-sm" : "border border-destructive bg-destructive/20 text-destructive text-sm"),
				role: "alert",
				"data-slot": "ai-interactive-error",
				children: [
					/* @__PURE__ */ jsxs("span", {
						className: "font-semibold",
						children: [
							"Invalid ",
							language,
							" JSON:"
						]
					}),
					" ",
					"Unable to parse the configuration. Please check the JSON syntax."
				]
			});
		}
		if (inv) return /* @__PURE__ */ jsx("div", {
			className: "my-4 [&_[data-slot=ai-code-block]]:bg-primary-foreground/10 [&_[data-slot=ai-code-block]]:border-primary-foreground/20 [&_[data-slot=ai-code-block-header]]:bg-primary-foreground/5 [&_[data-slot=ai-code-block-header]]:border-primary-foreground/20 [&_[data-slot=ai-code-block-header]_span]:!text-primary-foreground/60 [&_[data-slot=ai-code-block-header]_button]:!text-primary-foreground/60",
			style: INVERTED_SH_STYLE,
			children: /* @__PURE__ */ jsx(AICodeBlock, {
				code: content,
				language,
				showHeader: true,
				showCopyButton: true
			})
		});
		return /* @__PURE__ */ jsx(AICodeBlock, {
			code: content,
			language,
			showHeader: true,
			showCopyButton: true,
			className: "my-4"
		});
	};
}
//#endregion
export { createCodeComponent };

//# sourceMappingURL=index416.js.map