import { ElMessage } from "element-plus";

interface BaseReturn {
	status: boolean;
	message: string;
}
export async function copyToClipboard(txet: string): Promise<BaseReturn> {
	try {
		// 使用现代 Clipboard API
		await navigator.clipboard.writeText(txet);
		console.log("已复制:", txet);
		return { status: true, message: "已复制:" + txet };
	} catch (err) {
		console.error("复制失败:", err);

		// 降级方案：使用document.execCommand
		const textArea = document.createElement("textarea");
		textArea.value = txet;
		document.body.appendChild(textArea);
		textArea.select();
		try {
			const successful = document.execCommand("copy");
			if (successful) {
				return { status: true, message: "已复制:" + txet };
			}
		} catch (err) {}
		document.body.removeChild(textArea);
		return { status: false, message: "复制失败，请手动复制" };
	}
}

// 复制类型名称到剪贴板
export async function copyToClipboardThenTip(text: string): Promise<void> {
	const { status, message } = await copyToClipboard(text);
	if (status) {
		ElMessage.success(message);
	} else {
		ElMessage.error(message);
	}
}
