const openNavigationApp = (
	location: string,
	latitude?: number | null,
	longitude?: number | null,
) => {
	if (latitude && longitude) {
		const googleMapsUrl = `https://www.google.com/maps/dir/?api=1&destination=${latitude},${longitude}`;
		const appleMapsUrl = `maps://maps.apple.com/?daddr=${latitude},${longitude}`;
		const isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
		if (isIOS) {
			window.open(appleMapsUrl, "_blank");
		} else {
			window.open(googleMapsUrl, "_blank");
		}
	} else {
		const encodedAddress = encodeURIComponent(location);
		const googleMapsUrl = `https://www.google.com/maps/dir/?api=1&destination=${encodedAddress}`;
		const appleMapsUrl = `maps://maps.apple.com/?q=${encodedAddress}`;
		const isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
		if (isIOS) {
			window.open(appleMapsUrl, "_blank");
		} else {
			window.open(googleMapsUrl, "_blank");
		}
	}
};

export default openNavigationApp;
