import { NominatimReverseGeocodingResponse } from '../../types/Types';

export const formatSelectedAddress = (selectedAddress: NominatimReverseGeocodingResponse | null): string => {
    if (!selectedAddress) {
        return "آدرس یافت نشد";
    }
    
    try {
        return [
            selectedAddress.address.state ?? selectedAddress.address.province,
            selectedAddress.address.city,
            selectedAddress.address.neighbourhood,
            !selectedAddress.address.road.startsWith('میدان') &&
                !selectedAddress.address.road.startsWith('بلوار') &&
                !selectedAddress.address.road.startsWith('خیابان') &&
                !selectedAddress.address.road.startsWith('کوچه') ?
                selectedAddress.type === 'secondary' ? `بلوار ${selectedAddress.address.road}` :
                    selectedAddress.type === 'tertiary' ? `خیابان ${selectedAddress.address.road}` :
                        selectedAddress.type === 'residential' ? `کوچه ${selectedAddress.address.road}` :
                            selectedAddress.address.road : selectedAddress.address.road
        ].filter(x => x).join('، ');
    } catch {
        if (selectedAddress.display_name) {
            return selectedAddress.display_name;
        } else {
            return "آدرس یافت نشد";
        }
    }
};

