'use client'
export const getMobileOS = () => {
	if (typeof window === "undefined") return "Other"
	const ua = window.navigator.userAgent
	if (/android/i.test(ua)) {
		return "Android"
	}
	else if (/iPad|iPhone|iPod/.test(ua)) {
	  return "iOS"
	}
  
	return "Other"
}

export const getBrowser = (): string => {
	if (typeof window === 'undefined') return 'Other'

	// 1) iOS WebView判定: window.webkit.messageHandlers.getDevicePushToken があれば true
	// 2) Android WebView判定: window.AndroidNative.getDevicePushToken があれば true
	if (
		(
			(window as any).webkit
			&& (window as any).webkit.messageHandlers
			&& (window as any).webkit.messageHandlers.getDevicePushToken
		)
		|| (
			(window as any).AndroidNative
			&& (window as any).AndroidNative.getDevicePushToken
		)
	) {
		return 'WebView'
	}

	const agent = window.navigator.userAgent.toLowerCase()

	if (agent.indexOf('msie') !== -1 || agent.indexOf('trident') !== -1) {
		return 'Internet Explorer'
	} else if (agent.indexOf('edg') !== -1 || agent.indexOf('edge') !== -1) {
		return 'Edge'
	} else if (agent.indexOf('opr') !== -1 || agent.indexOf('opera') !== -1) {
		return 'Opera'
	} else if (agent.indexOf('chrome') !== -1 || agent.indexOf('crios') !== -1) {
		// iOS版Chromeは 'crios' を含む
		return 'Chrome'
	} else if (agent.indexOf('safari') !== -1) {
		return 'Safari'
	} else if (agent.indexOf('firefox') !== -1) {
		return 'FireFox'
	} else {
		return 'Other'
	}
}

