import { useCallback, useMemo, useState } from "react";
//#region src/hooks/useClipboard.tsx
function useClipboard(clipboardArgs) {
	const { onWriteEmptyText, onWriteError, onWrite, onReadError } = useMemo(() => ({
		onWriteEmptyText: clipboardArgs?.onWriteEmptyText,
		onWriteError: clipboardArgs?.onWriteError,
		onWrite: clipboardArgs?.onWrite,
		onReadError: clipboardArgs?.onReadError
	}), [clipboardArgs]);
	const [canReadFromClipboard, setCanReadFromClipboard] = useState(typeof navigator.clipboard?.readText !== "undefined");
	const [canWriteToClipboard, setCanWriteToClipboard] = useState(typeof navigator.clipboard?.writeText !== "undefined");
	const writeTextToClipboard = useCallback(async (text) => {
		try {
			if (!text.trim()) {
				if (onWriteEmptyText) onWriteEmptyText();
			} else {
				await navigator.clipboard.writeText(text);
				if (onWrite) onWrite();
			}
		} catch {
			if (onWriteError) onWriteError();
			setCanWriteToClipboard(false);
		}
	}, [
		onWrite,
		onWriteEmptyText,
		onWriteError
	]);
	const readTextFromClipboard = useCallback(async () => {
		try {
			return await navigator.clipboard.readText();
		} catch {
			if (onReadError) onReadError();
			setCanReadFromClipboard(false);
			return null;
		}
	}, [onReadError]);
	return useMemo(() => ({
		canReadFromClipboard,
		canWriteToClipboard,
		readTextFromClipboard,
		writeTextToClipboard
	}), [
		canReadFromClipboard,
		canWriteToClipboard,
		readTextFromClipboard,
		writeTextToClipboard
	]);
}
//#endregion
export { useClipboard };
