import { useMutation } from "@tanstack/react-query";
import { Coordinate } from "@/types/global";
import { useSettings } from "@/hook/use-settings";
import { mapIrReverseGeocode } from "@/lib/map-ir";

/** reverse geocode با Map.ir — خروجی سازگار با شکل قبلی گوگل */
export const useSearchAddress = () => {
  const { settings } = useSettings();
  return useMutation({
    mutationFn: async (location?: Partial<Coordinate>) => {
      const key =
        settings?.google_map_key ||
        process.env.NEXT_PUBLIC_MAP_IR_KEY ||
        process.env.NEXT_PUBLIC_GOOGLE_MAPS_KEY ||
        "";
      const address = await mapIrReverseGeocode(
        { lat: Number(location?.lat), lng: Number(location?.lng) },
        key
      );
      return {
        results: [{ formatted_address: address }],
        plus_code: { compound_code: address },
      };
    },
  });
};
