const envCredential = require("../config/index");
const GOOGLE_MAP_API_KEY = envCredential.GOOGLE_MAP_API_KEY;
const axios = require("axios");

const getLatLongByAddress = async (address) => {
  try {
    console.log("=-=-=-=-=", address);
    const response = await axios.get(
      "https://maps.googleapis.com/maps/api/geocode/json",
      {
        params: {
          address: address,
          key: GOOGLE_MAP_API_KEY,
        },
      }
    );
    if (response.data.results.length > 0) {
      const location = response.data.results[0].geometry.location;
      return {
        latitude: location.lat,
        longitude: location.lng,
      };
    } else {
      return {
        latitude: 0,
        longitude: 0,
      };
    }
  } catch (error) {
    console.log(error);
    return {
      latitude: 0,
      longitude: 0,
    };
  }
};

module.exports = {
  getLatLongByAddress,
};
