import fetch from 'node-fetch';

let handler = async (m, { conn, text }) => {
  if (!text) throw 'Please provide a word to search for.';
  m.react("ğŸ”�")

  const url = `https://api.urbandictionary.com/v0/define?term=${encodeURIComponent(text)}`;

  const response = await fetch(url);
  const json = await response.json();

  if (!response.ok) {
    throw `An error occurred: ${json.message}`;
  m.react("â�Œ")
  }

  if (!json.list.length) {
    throw 'Word not found in the dictionary.';
  m.react("â˜¹ï¸�")
  }

  const firstEntry = json.list[0];
  const definition = firstEntry.definition;
  const example = firstEntry.example ? `*ExampleğŸ“–:* ${firstEntry.example}` : '';

  const message = `*WordğŸ“š:* ${text}\n
  
*DefinitionğŸ“š:* ${definition}\n
    
${example}`;
  conn.sendMessage(m.chat, { text: message }, 'extendedTextMessage', { quoted: m });
m.react("âœ…")
};

handler.help = ['define <word>'];
handler.tags = ['tools'];
handler.command = ['define', 'mean', 'df']

export default handler;
