import fetch from 'node-fetch';

let handler = async (m, { conn, text, usedPrefix, command }) => {
  if (!text && !(m.quoted && m.quoted.text)) {
    throw `ğŸ…¶ğŸ…³ğŸ†‚   ${usedPrefix}${command} ğ��“ğ��¡ğ��� ğ��›ğ���ğ��šğ��®ğ��­ğ��² ğ��¨ğ��Ÿ ğ��’ğ��¢ğ��§ğ���ğ��¡ `;

    
  }
  if (!text && m.quoted && m.quoted.text) {
    text = m.quoted.text;
  }

  const match = text.match(/(\d+)/);
  const numberOfImages = match ? parseInt(match[3]) : 3;

  try {
    m.react("â™»ï¸�");
    m.reply(imgs)

    const images = [];

    for (let i = 0; i < numberOfImages; i++) {
      const endpoint = `https://api.guruapi.tech/api/googleimage?text=${encodeURIComponent(text)}`;
      const response = await fetch(endpoint);


      if (response.ok) {
        const imageBuffer = await response.buffer();
        images.push(imageBuffer);
      } else {
        throw '*Image generation failed*';
      }
    }
    if (!text && m.quoted && m.quoted.text) text = m.quoted.text
    
    let cap = 'ğ�™·ğ�š�ğ�š›ğ�š� ğ�š’ğ�šœ ğ�š¢ğ�š˜ğ�š�ğ�š› ğ�š’ğ�š–ğ�šŠğ�š�ğ�š�';

    for (let i = 0; i < images.length; i++) {
      await conn.sendFile(m.chat, images[i], `image_${i + 1}.png`, cap, m);
      m.react("âœ…")


    }
  } catch {
    throw '*Oops! Something went wrong while generating images. Please try again later.*';
    m.react("â�Œ")
  }
};

handler.help = ['image'];
handler.tags = ['downloader'];
handler.command = ['img', 'gimage', 'image', 'pic', 'photo', 'picture'];

export default handler;
