import fetch from "node-fetch";
const handler = async (m, { conn, args, usedPrefix, command }) => {
  const text =
    args.length >= 1
      ? args.slice(0).join(" ")
      : (m.quoted && m.quoted?.text) ||
        m.quoted?.caption ||
        m.quoted?.description ||
        null;
  if (!text)
    return m.reply(
      `Masukkan teks atau reply pesan dengan teks yang ingin diolah.\nContoh penggunaan:\n*${usedPrefix}${command} Hai, apa kabar?*`,
    );
  m.react(wait);
  try {
    const aiArt = await aiArtGenerator(encodeURIComponent(text));
    if (aiArt) {
      const tag = `@${m.sender.split("@")[0]}`;
      await conn.sendMessage(
        m.chat,
        {
          image: Buffer.from(aiArt),
          caption: `*Prompt:*\n- ${text}\n\n*Request:* ${tag}`,
          mentions: [m.sender],
        },
        {
          quoted: m,
        },
      );
    } else console.log("Tidak ada respons dari OpenAI atau terjadi kesalahan.");
  } catch (error) {
    console.error("Terjadi kesalahan:", error), m.react(eror);
  }
};
(handler.help = ["aiartgen"]),
  (handler.tags = ["fun"]),
  (handler.command = /^(aiartgen)$/i);
export default handler;
async function aiArtGenerator(prompt) {
  try {
    const response = await fetch(
      "https://ai-api.magicstudio.com/api/ai-art-generator",
      {
        method: "POST",
        headers: {
          "User-Agent":
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
          Accept: "application/json, text/plain, */*",
          "Accept-Encoding": "gzip, deflate, br, zstd",
          "Accept-Language": "en-US,en;q=0.9",
          Origin: "https://magicstudio.com",
          Referer: "https://magicstudio.com/ai-art-generator/",
        },
        body: new URLSearchParams({
          prompt: prompt,
          output_format: "bytes",
          user_profile_id: "null",
          anonymous_user_id: "a584e30d-1996-4598-909f-70c7ac715dc1",
          request_timestamp: Date.now(),
          user_is_subscribed: "false",
          client_id: "pSgX7WgjukXCBoYwDM8G8GLnRRkvAoJlqa5eAVvj95o",
        }),
      },
    );
    if (!response.ok) {
      throw new Error("Network response was not ok");
    }
    const data = await response.arrayBuffer();
    return data;
  } catch (error) {
    console.error("Error fetching data:", error);
    throw error;
  }
}
