// // utils/hooks/useLocationSearch.ts
// import { useApiInterceptor } from "./useApiInterceptor";

// export const useLocationSearch = async (query: string) => {
//   try {
//     const response = await useApiInterceptor().get(
//       `location/search?query=${encodeURIComponent(query)}`
//     );
//     return { resp: response.data, status: response.status };
//   } catch (error) {
//     console.error("Error fetching location search:", error);
//     throw error;
//   }
// };

import { useApiInterceptor } from "./useApiInterceptor";

// Primary search
export const useLocationSearch = async (query: string) => {
  try {
    const response = await useApiInterceptor().get(
      `/location?query=${encodeURIComponent(query)}`
    );
    return { resp: response.data, status: response.status };
  } catch (error) {
    console.error("Error fetching location search:", error);
    throw error;
  }
};

// Sub-location fetcher
export const useFetchSubLocations = async (item: {
  type: "country" | "state" | "city";
  countryId?: number;
  stateId?: number;
  cityId?: number;
}) => {
  if (item.type === "city") {
    return { resp: { results: [] }, status: 200 }; // No sub-locations for city
  }

  try {
    const params = new URLSearchParams();

    params.set("type", item.type);
    if (item.countryId) params.set("countryId", String(item.countryId));
    if (item.stateId) params.set("stateId", String(item.stateId));

    const response = await useApiInterceptor().get(
      `/location/sublocations?${params.toString()}`
    );
    return { resp: response.data, status: response.status };
  } catch (error) {
    console.error("Error fetching sub-locations:", error);
    throw error;
  }
};
