"use client";
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/molecules/data/CodeSnippet/CodeSnippet.tsx
var codeSnippetVariants = cva("overflow-hidden rounded-xl bg-background", {
	variants: { size: {
		sm: "text-xs",
		md: "text-base",
		lg: "text-lg"
	} },
	defaultVariants: { size: "md" }
});
var lineNumberVariants = cva("select-none font-mono font-bold leading-normal text-quaternary-foreground", {
	variants: { size: {
		sm: "text-xs",
		md: "text-base",
		lg: "text-lg"
	} },
	defaultVariants: { size: "md" }
});
function LineNumbers({ count, startLine, size }) {
	return /* @__PURE__ */ jsx("div", {
		"aria-hidden": "true",
		className: cn(lineNumberVariants({ size })),
		children: Array.from({ length: count }, (_, i) => /* @__PURE__ */ jsx("span", {
			className: "block mb-px last:mb-0",
			children: startLine + i
		}, i))
	});
}
var CodeSnippet = React.forwardRef(({ code, language, showLineNumbers = true, startLineNumber = 1, showCopyButton = false, size, className, breakpoint = "desktop" }, ref) => {
	const highlightedHtml = React.useMemo(() => highlight(code), [code]);
	const lineCount = React.useMemo(() => code.split("\n").length, [code]);
	const [copied, setCopied] = React.useState(false);
	const rootRef = React.useRef(null);
	React.useImperativeHandle(ref, () => rootRef.current);
	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]);
	const containerPadding = breakpoint === "mobile" ? "p-4" : "p-6";
	const contentPadding = breakpoint === "mobile" ? "py-4 px-4 pb-6" : "py-6 px-6 pb-8";
	return /* @__PURE__ */ jsxs("div", {
		ref: rootRef,
		"data-slot": "code-snippet",
		role: "region",
		"aria-label": "Code snippet",
		className: cn(codeSnippetVariants({ size }), "relative flex items-start justify-center", className),
		children: [
			(language || showCopyButton) && /* @__PURE__ */ jsxs("div", {
				className: "absolute top-2 end-2 flex items-center gap-2 z-10",
				children: [language && /* @__PURE__ */ jsx("span", {
					className: "text-xs font-medium text-quaternary-foreground uppercase select-none",
					children: language
				}), showCopyButton && /* @__PURE__ */ jsx(Button, {
					appearance: "ghost",
					variant: "secondary",
					size: "sm",
					onClick: handleCopy,
					"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-3.5 text-success" }) : /* @__PURE__ */ jsx(Copy, { className: "size-3.5" })
				})]
			}),
			showLineNumbers && /* @__PURE__ */ jsx("div", {
				className: cn("shrink-0 self-stretch bg-muted border-e border-border", containerPadding),
				children: /* @__PURE__ */ jsx(LineNumbers, {
					count: lineCount,
					startLine: startLineNumber,
					size
				})
			}),
			/* @__PURE__ */ jsx("div", {
				className: cn("grow min-w-0 overflow-x-auto", contentPadding),
				tabIndex: 0,
				children: /* @__PURE__ */ jsx("pre", {
					className: cn("font-mono font-medium leading-normal !bg-transparent !p-0 !m-0 !border-0 !rounded-none", "[&_.sh__line]:block [&_.sh__line]:leading-normal [&_.sh__line]:mb-px [&_.sh__line:last-child]:mb-0"),
					dangerouslySetInnerHTML: { __html: highlightedHtml }
				})
			})
		]
	});
});
CodeSnippet.displayName = "CodeSnippet";
//#endregion
export { CodeSnippet, codeSnippetVariants, lineNumberVariants };

//# sourceMappingURL=index78.js.map