'use client';


import { cn } from "../../utils/cn.mjs";
import { CopyCheck, CopyIcon } from "lucide-react";
import { useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/components/CopyToClipboard/index.tsx
/**
* CopyToClipboard Component
*
* A versatile component that allows users to copy text to their clipboard with visual feedback.
* Provides accessibility features, customizable feedback duration, and error handling.
*
* @example
* ```tsx
* // Basic usage
* <CopyToClipboard text="Hello World">
*   <span>Click to copy</span>
* </CopyToClipboard>
*
* // With custom styling and callbacks
* <CopyToClipboard
*   text="npm install @intlayer/design-system"
*   className="bg-gray-100 p-2"
*   feedbackDuration={3000}
*   onCopySuccess={() => console.log('Copied!')}
* >
*   <code>npm install @intlayer/design-system</code>
* </CopyToClipboard>
* ```
*
* @component
* @accessibility
* - Uses proper ARIA labels for copy and copied states
* - Supports keyboard navigation (Enter and Space keys)
* - Announces state changes to screen readers
* - Provides visual focus indicators
*/
const CopyToClipboard = ({ text, children, className, "aria-label": ariaLabel = "Copy to clipboard", "aria-copied-label": ariaCopiedLabel = "Copied to clipboard", feedbackDuration = 2e3, onCopySuccess, onCopyError, disable, preventDefault = true }) => {
	const [isCopied, setIsCopied] = useState(false);
	if (disable) return /* @__PURE__ */ jsx("span", {
		className,
		children
	});
	const handleCopy = async (e) => {
		e.preventDefault();
		if (!text) return;
		try {
			if (preventDefault) e.preventDefault();
			if (navigator.clipboard?.writeText) await navigator.clipboard.writeText(text);
			else {
				const textArea = document.createElement("textarea");
				textArea.value = text;
				textArea.style.position = "fixed";
				textArea.style.left = "-999999px";
				textArea.style.top = "-999999px";
				document.body.appendChild(textArea);
				textArea.focus();
				textArea.select();
				document.body.removeChild(textArea);
			}
			setIsCopied(true);
			setTimeout(() => setIsCopied(false), feedbackDuration);
			onCopySuccess?.(e);
		} catch (error) {
			const errorMessage = error instanceof Error ? error.message : "Failed to copy to clipboard";
			onCopyError?.(error instanceof Error ? error : new Error(errorMessage));
		}
	};
	const handleKeyDown = (event) => {
		if (event.key === "Enter" || event.key === " ") handleCopy(event);
	};
	const currentAriaLabel = isCopied ? ariaCopiedLabel : ariaLabel;
	const IconComponent = isCopied ? CopyCheck : CopyIcon;
	return /* @__PURE__ */ jsxs("span", {
		className: cn("inline-flex max-w-full cursor-pointer items-center gap-2 rounded-md p-0.5 hover:bg-neutral/10", className),
		onClick: handleCopy,
		onKeyDown: handleKeyDown,
		role: "button",
		tabIndex: 0,
		"aria-label": currentAriaLabel,
		"aria-pressed": isCopied,
		"data-testid": "copy-to-clipboard",
		children: [/* @__PURE__ */ jsx("span", {
			className: "min-w-0 break-all",
			children
		}), text && /* @__PURE__ */ jsx(IconComponent, {
			className: "ml-1 ml-auto size-4 min-w-4 shrink-0",
			"aria-hidden": "true"
		})]
	});
};

//#endregion
export { CopyToClipboard };
//# sourceMappingURL=index.mjs.map