import { useCallback } from 'react';
import { fetchGeoLocation, fetchGoogleGeoLocation } from '../commonFunctions';

/**
 * Custom hook to fetch location results based on a search term.
 * Handles dispatching the getLocationList action with predefined pagination.
 * 
 * @returns A function that takes a search term and dispatches the location list action.
 */
const useFetchLocationResults = () => {
    /**
     * Fetches location data based on a search term.
     * Uses useCallback to memoize the function and prevent unnecessary re-renders.
     * @param searchTerm - The keyword entered by the user to search location results.
     */
    const fetchResults = useCallback(async (searchTerm: string, region: string) => {
        try {
            const response = await fetch(`/api/searchLocation?query=${searchTerm}&region=${region}`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error('Error fetching location data:', error);
            return null;
        }
    }, []);

    return fetchResults;
};

export default useFetchLocationResults;
