import { canLevelUp, xpRange } from "../../lib/levelling.js";
import { levelup } from "../../lib/canvas.js";
export async function before(m) {
  if (m.isBaileys || !m.sender) return !1;
  let user = db.data.users[m.sender];
  if (!db.data.chats[m.chat].autolevelup) return !1;
  let beforeLevel = 1 * user.level;
  for (; canLevelUp(user.level, user.exp, multiplier); ) user.level++;
  if (beforeLevel !== user.level) {
    let ppuser,
      teks = `Selamat ${await this.getName(m.sender)} naik 🧬level\n.             ${user.role}`,
      str =
        `Selamat ${await this.getName(m.sender)} naik 🧬level\n.             ${user.role}\n\n*🎉 C O N G R A T S 🎉*\n*${beforeLevel}* ➔ *${user.level}* [ *${user.role}* ]\n\n• 🧬Level Sebelumnya : ${beforeLevel}\n• 🧬Level Baru : ${user.level}\n• Pada Jam : ${new Date().toLocaleString("id-ID")}\n\n*Note:* _Semakin sering berinteraksi dengan bot Semakin Tinggi level kamu_\n`.trim(),
      { min, xp, max } = xpRange(user.level, multiplier);
    await this.profilePictureUrl(m.sender).catch(
      (_) => "./src/avatar_contact.png",
    ),
      user.exp,
      user.role,
      user.level,
      m.sender.substring(3, 7),
      Object.entries(db.data.users)
        .sort((a, b) => b[1].level - a[1].level)
        .map((v) => v[0])
        .indexOf(m.sender);
    try {
      ppuser = await this.profilePictureUrl(m.sender, "image");
    } catch {
      ppuser =
        "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTMxMUXFtd5GrFkxyrU-f5zA2IH8MZ-U-cFKg&usqp=CAU";
    }
    try {
      let datac = await levelup(teks, user.level);
      await this.sendFile(m.chat, datac, "", str, m);
    } catch (e) {
      try {
        await this.sendFile(m.chat, flaaa.getRandom() + "levelup", "", str, m);
      } catch (e) {
        m.reply(str || eror);
      }
    }
  }
}
