'use client';


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

//#region src/components/CopyToClipboard/index.tsx
const useCopyToClipboard = (text, feedbackDuration = 2e3, onCopySuccess, onCopyError) => {
	const [isCopied, setIsCopied] = useState(false);
	const copy = async () => {
		if (!text) return;
		try {
			if (navigator.clipboard?.writeText) await navigator.clipboard.writeText(text);
			else {
				onCopyError?.(/* @__PURE__ */ new Error("Clipboard API not supported in this browser"));
				return;
			}
			setIsCopied(true);
			setTimeout(() => setIsCopied(false), feedbackDuration);
			onCopySuccess?.();
		} catch (error) {
			const errorMessage = error instanceof Error ? error.message : "Failed to copy to clipboard";
			onCopyError?.(error instanceof Error ? error : new Error(errorMessage));
		}
	};
	return {
		isCopied,
		copy
	};
};
/**
* 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, copy } = useCopyToClipboard(text, feedbackDuration, onCopySuccess, onCopyError);
	if (disable) return /* @__PURE__ */ jsx("span", {
		className,
		children
	});
	const handleKeyDown = (event) => {
		if (event.key === "Enter" || event.key === " ") handleCopy(event);
	};
	const handleCopy = (event) => {
		if (preventDefault) event.preventDefault();
		copy();
	};
	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, useCopyToClipboard };
//# sourceMappingURL=index.mjs.map