import axios from "axios";
import endPointQuery from "../../helpers/EndPointQuery";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import { getWbot } from "../../libs/wbot";

const GetProfilePicUrl = async (number: string): Promise<any> => {
  const defaultWhatsapp = await GetDefaultWhatsApp({
    ignoreNoWhatsappFound: true
  });
 
  let profilePicUrl;

  if (defaultWhatsapp) {
    const wbot_url = await getWbot(defaultWhatsapp.id);

    const {data} = await endPointQuery(`${wbot_url}/api/GetProfilePicUrl`, {
      number: `${number}`
    });

     if (data?.data) {
       profilePicUrl = data.data;
     }
  }
 
  try {
    if (!profilePicUrl) {
      const { data } = await axios.post(
        `${process.env.WHATS_NUMBER_VALIDATOR_URL}/api/GetProfilePicUrl`,
        { number },
        {
          headers: {
            "Content-Type": "application/json"
          }
        }
      );
      profilePicUrl = data?.data;
    }
     
    if (profilePicUrl) {
      return profilePicUrl;
    }
  } catch (error) {}

  return null;
};

export default GetProfilePicUrl;
