import similarity from "similarity";
export async function before(m, { match }) {
  try {
    if (m.isBaileys && !(m.text || m.caption) && !m.prefix) {
      return;
    }
    if (!m.isBaileys && (m.text || m.caption) && m.prefix) {
      const usedPrefix = match?.[0]?.[0] || "",
        words = (m?.text || m?.caption)
          ?.replace(usedPrefix, "")
          ?.trim()
          ?.split(" "),
        noPrefix = words[0]?.toLowerCase(),
        help =
          (words.slice(1)?.join(" ")?.toLowerCase(),
          Object.values(plugins)
            ?.flatMap((v) => v.help ?? [])
            ?.map((entry) => entry.trim()?.split(" ")[0]?.toLowerCase())
            ?.filter(Boolean)),
        filteredMatches = help
          .map((target) => ({
            target: target,
            rating: similarity(noPrefix, target),
          }))
          ?.filter(({ rating }) => rating > 0.7);
      if (0 === filteredMatches.length) return;
      const sortedMatches = filteredMatches.sort((a, b) => b.rating - a.rating),
        bestMatch = sortedMatches[0],
        otherMatches = sortedMatches.length > 1 ? sortedMatches.slice(1) : [],
        bestMatchEntry = {
          header: `Similarity: ${(100 * bestMatch.rating)?.toFixed(0)}%`,
          id: `${usedPrefix}${bestMatch.target} ${(m.text || "").split(" ").slice(1).join(" ")}`,
          title: bestMatch.target,
          description: "",
        },
        otherMatchEntries = otherMatches.map((item) => ({
          header: `Similarity: ${(100 * item.rating)?.toFixed(0)}%`,
          id: `${usedPrefix}${item.target} ${(m.text || "").split(" ").slice(1).join(" ")}`,
          title: item.target,
          description: "",
        })),
        rows = [
          {
            title: "Best Match",
            highlight_label: "Best match",
            rows: [bestMatchEntry],
          },
          ...(otherMatchEntries.length > 0
            ? [
                {
                  title: "Other Matches",
                  highlight_label: "Other matches",
                  rows: otherMatchEntries,
                },
              ]
            : []),
        ],
        mentionedJid =
          m?.mentionedJid?.[0] ||
          (m?.fromMe ? this.user.jid : m?.sender || m?.key.remoteJid),
        senderName = (m?.name || m?.pushName || m?.sender || "")?.split(
          "\n",
        )[0];
      if (
        !!(
          filteredMatches.some(({ target }) => target === noPrefix) ||
          m?.isCommand ||
          help.includes(noPrefix) ||
          ["=>", ">", "$"].some((char) =>
            (m?.text || m?.caption)?.startsWith(char),
          )
        )
      )
        return;
      const caption = `\`\`\`👋 Hai, ${senderName} @${mentionedJid.split("@")[0]}!\`\`\`\n`;
      await this.sendButtonCta(
        m.chat,
        [
          [
            caption,
            "Mungkin yang kamu maksud.",
            null,
            [],
            null,
            null,
            [["Lihat Disini", rows]],
          ],
        ],
        m,
      );
    }
  } catch (error) {
    console.error("An error occurred:", error);
  }
}
export const disabled = !1;
