import axios from 'axios';

export async function extractCoordsFromRedirectUrl(url: string): Promise<{ latitude: string; longitude: string } | null> {
  try {
    await axios.get(url, { maxRedirects: 0 });
    return null;
  } catch (error: any) {
    if (error.response?.status === 302 || error.response?.status === 301) {
      const redirectUrl = error.response.headers.location;
      const coordsMatch = redirectUrl.match(/!3d([-\d.]+)!4d([-\d.]+)/);
      if (coordsMatch) {
        return {
          latitude: coordsMatch[1],
          longitude: coordsMatch[2],
        };
      }
    }
    return null;
  }
}
