import { cn } from "./index3.js";
import { Button } from "./index6.js";
import { highlight } from "./index77.js";
import { cva } from "class-variance-authority";
import { Check, Copy } from "lucide-react";
import * as React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/organisms/ai/AICodeBlock/AICodeBlock.tsx
var codeBlockVariants = cva("relative overflow-hidden rounded-xl border border-border bg-muted", {
	variants: { size: {
		sm: "text-xs",
		md: "text-sm",
		lg: "text-base"
	} },
	defaultVariants: { size: "md" }
});
function AICodeBlockHeader({ language, fileName, showCopyButton = true, onCopy, copied = false, className, actions }) {
	return /* @__PURE__ */ jsxs("div", {
		"data-slot": "ai-code-block-header",
		className: cn("flex items-center justify-between px-4 py-2 border-b border-border bg-accent", className),
		children: [/* @__PURE__ */ jsxs("div", {
			className: "flex items-center gap-2",
			children: [fileName && /* @__PURE__ */ jsx("span", {
				className: "text-sm font-medium text-secondary-foreground",
				children: fileName
			}), language && !fileName && /* @__PURE__ */ jsx("span", {
				className: "text-xs font-medium text-quaternary-foreground uppercase",
				children: language
			})]
		}), /* @__PURE__ */ jsxs("div", {
			className: "flex items-center gap-2",
			children: [actions, showCopyButton && /* @__PURE__ */ jsxs(Button, {
				appearance: "ghost",
				variant: "secondary",
				size: "sm",
				onClick: onCopy,
				"aria-label": copied ? "Copied to clipboard" : "Copy code to clipboard",
				className: "h-7 px-2 text-quaternary-foreground hover:text-secondary-foreground transition-colors duration-150 ease-out",
				children: [copied ? /* @__PURE__ */ jsx(Check, { className: "size-4 text-success delight-bounce-in motion-reduce:animate-none" }) : /* @__PURE__ */ jsx(Copy, { className: "size-4" }), /* @__PURE__ */ jsx("span", {
					className: "ms-1.5 text-xs",
					children: copied ? "Copied!" : "Copy"
				})]
			})]
		})]
	});
}
AICodeBlockHeader.displayName = "AICodeBlockHeader";
function AICodeBlockContent({ children, showLineNumbers = false, startLineNumber = 1, className }) {
	const renderContent = () => {
		if (showLineNumbers && typeof children === "string") return /* @__PURE__ */ jsxs("div", {
			className: "flex",
			children: [/* @__PURE__ */ jsx("div", {
				className: "flex flex-col items-end pe-4 select-none border-e border-border me-4",
				children: children.split("\n").map((_, index) => /* @__PURE__ */ jsx("span", {
					className: "text-muted-foreground leading-relaxed",
					children: startLineNumber + index
				}, index))
			}), /* @__PURE__ */ jsx("pre", {
				className: "flex-1 overflow-x-auto",
				children: /* @__PURE__ */ jsx("code", { children })
			})]
		});
		return /* @__PURE__ */ jsx("pre", {
			className: "overflow-x-auto",
			children: /* @__PURE__ */ jsx("code", { children })
		});
	};
	return /* @__PURE__ */ jsx("div", {
		"data-slot": "ai-code-block-content",
		className: cn("p-4 font-mono leading-relaxed", className),
		children: renderContent()
	});
}
AICodeBlockContent.displayName = "AICodeBlockContent";
function AICodeBlock({ code, language = "plaintext", fileName, showHeader = true, showCopyButton = true, showLineNumbers = false, startLineNumber = 1, highlightedCode, size, className, headerActions }) {
	const syntaxHtml = React.useMemo(() => highlightedCode ?? highlight(code), [code, highlightedCode]);
	const [copied, setCopied] = React.useState(false);
	const rootRef = React.useRef(null);
	const handleCopy = React.useCallback(async () => {
		try {
			const win = rootRef.current?.ownerDocument.defaultView;
			if (!win) return;
			await win.navigator.clipboard.writeText(code);
			setCopied(true);
			setTimeout(() => setCopied(false), 2e3);
		} catch (err) {
			console.error("Failed to copy code:", err);
		}
	}, [code]);
	return /* @__PURE__ */ jsxs("div", {
		ref: rootRef,
		"data-slot": "ai-code-block",
		className: cn(codeBlockVariants({ size }), className),
		children: [showHeader && /* @__PURE__ */ jsx(AICodeBlockHeader, {
			language,
			fileName,
			showCopyButton,
			onCopy: handleCopy,
			copied,
			actions: headerActions
		}), /* @__PURE__ */ jsx("pre", {
			tabIndex: 0,
			className: cn("p-4 overflow-x-auto font-mono leading-relaxed !bg-transparent !m-0 !border-0 !rounded-none", "[&_.sh__line]:block [&_.sh__line]:leading-relaxed", showLineNumbers && ["[&_.sh__line]:[counter-increment:sh-line]", "[&_.sh__line]:before:[content:counter(sh-line)] [&_.sh__line]:before:inline-block [&_.sh__line]:before:w-8 [&_.sh__line]:before:me-4 [&_.sh__line]:before:text-end [&_.sh__line]:before:text-muted-foreground [&_.sh__line]:before:select-none"]),
			style: showLineNumbers ? { counterReset: `sh-line ${startLineNumber - 1}` } : void 0,
			dangerouslySetInnerHTML: { __html: syntaxHtml }
		})]
	});
}
AICodeBlock.displayName = "AICodeBlock";
//#endregion
export { AICodeBlock, AICodeBlockContent, AICodeBlockHeader, codeBlockVariants };

//# sourceMappingURL=index414.js.map