export interface GeoLocation {
  lat: number;
  lng: number;
  placeName: string;
  adminName1: string;
  adminName3: string;
  countryCode: string;
  postalCode: string;
  adminCode1?: string;
  "ISO3166-2"?: string;

}

export const fetchZipSuggestions = async (
  postalCode: string
): Promise<GeoLocation[] | null> => {
  const username = process.env.NEXT_PUBLIC_GEONAMES_USERNAME;
  if (!username || postalCode.trim() === "") return null;

  const url = `https://secure.geonames.org/postalCodeSearchJSON?postalcode=${postalCode}&maxRows=10&username=${username}`;
  const res = await fetch(url);
  if (!res.ok) return null;

  const data = await res.json();
  return data.postalCodes || null;
};
