import axios, { AxiosResponse } from 'axios';
import { getConfig } from './config';
import { NearbyOptions, NearbyResult } from './types';
export async function nearby(
  radius: number,
  limit: number,
  options: NearbyOptions
): Promise<any> {
  // Adjust the return type if needed
  const apiKey = getConfig().apiKey;
  const version = getConfig().version;
  let apiUrl = '';
  if (version === 'v2') {
    apiUrl = `https://barikoi.xyz/v2/api/search/nearby/${radius}/${limit}`;
  } else {
    apiUrl = `https://barikoi.xyz/v1/api/search/nearby/${apiKey}/${radius}/${limit}`;
  }
  const params =
    version === 'v1' ? { ...options } : { api_key: apiKey, ...options };

  try {
    const response: AxiosResponse<{
      places?: NearbyResult[];
      Place?: NearbyResult[];
      [key: string]: any; // To accommodate other possible fields in the response
    }> = await axios.get(apiUrl, {
      params,
    });
    return response.data; // Return the entire response data
  } catch (error) {
    console.error('Nearby places request failed:', error.message);
    throw error;
  }
}
