/**
 * 精确检测设备类型，区分 PC、Mac、平板和移动设备
 * @private
 * @returns {Object} 设备信息对象
 */
export const detectDeviceType = () => {
	const userAgent = navigator.userAgent.toLowerCase();
	const platform = navigator.platform.toLowerCase();
	const hasTouchSupport =
		"ontouchstart" in window || navigator.maxTouchPoints > 0;

	// 检测操作系统
	const isMac = /mac|darwin/.test(platform) || /macintosh/.test(userAgent);
	const isWindows = /win/.test(platform);
	const isLinux = /linux/.test(platform) && !/android/.test(userAgent);

	// 检测设备类型 - 修复iPad检测逻辑
	const isMobile = /mobile|phone|android.*mobile|iphone/.test(userAgent);
	// 修复iPad检测：包括iOS iPad和Android平板
	const isTablet =
		/tablet|ipad|android(?!.*mobile)/.test(userAgent) ||
		/ipad/.test(userAgent) ||
		(navigator.maxTouchPoints &&
			navigator.maxTouchPoints > 1 &&
			/mac/.test(userAgent));
	const isDesktop = !isMobile && !isTablet;

	// 检测浏览器类型（用于特定优化）
	const isSafari = /safari/.test(userAgent) && !/chrome/.test(userAgent);
	const isChrome = /chrome/.test(userAgent);
	const isFirefox = /firefox/.test(userAgent);

	// 调试日志 - 仅在开发环境输出
	if (process.env.NODE_ENV === "development") {
		//   console.log("设备检测结果:", {
		//     userAgent,
		//     platform,
		//     isMobile,
		//     isTablet,
		//     isDesktop,
		//     hasTouchSupport,
		//     maxTouchPoints: navigator.maxTouchPoints,
		//   });
	}
	return {
		isMac,
		isWindows,
		isLinux,
		isMobile,
		isTablet,
		isDesktop,
		isSafari,
		isChrome,
		isFirefox,
		hasTouchSupport,
		// 判断是否应该使用触摸事件作为主要交互方式
		preferTouchEvents: (isMobile || isTablet) && !isDesktop,
		// 判断是否需要特殊的 Mac 触控板处理
		needsMacTrackpadOptimization: isMac && isDesktop && hasTouchSupport,
	};
}