import { tools } from "./tools";

// geocoding.ts
export const getCityFromCoordinates = async (lat: number, lon: number): Promise<string | null> => {
    const url = `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`;

    try {
        const response = await fetch(url);
        if (!response.ok) {
            throw new Error('Errore nella richiesta di geocoding');
        }
        const data = await response.json();
        
        // Controlla se la città è presente nei risultati
        return data.address?.city || data.address?.town || data.address?.village || null;
    } catch (error) {
        console.error('Errore durante il geocoding:', error);
        return null;
    }
};

export const getMapBoundaries = (): any => {
    const ne = tools.getCookie(tools.COOK_MAPBOUNDS + 'ne', '{"lat": 20, "lng": 30}', false)
    const sw = tools.getCookie(tools.COOK_MAPBOUNDS + 'sw', '{"lat": 25, "lng": 35}', false)

    // console.log('getMapBoundaries', ne, sw)

    return { ne, sw }
};
    