import fetch from "node-fetch";
const handler = async (m, { conn, text }) => {
  if (!text)
    throw "❌ *Spotify Search* ❌\n\nSilakan masukkan kata kunci untuk mencari lagu di Spotify.";
  try {
    let json = await searchSpotifyTracks(text);
    if (json.length < 1)
      throw "❌ *Spotify Search* ❌\n\nTidak ada hasil ditemukan.";
    let ini_txt = "✨ *Spotify Search* ✨";
    for (const x of json)
      ini_txt += `\n      \n🎵 *Judul:* ${x.name}\n👥 *Artis:* ${x.artists.map((v) => v.name).join(", ")}\n👥 *Artis Album:* ${x.album.artists.map((v) => v.name).join(", ")}\n🆔 *ID:* ${x.id}\n📅 *Tanggal Rilis Album:* ${x.album.release_date}\n🆔 *ID Album:* ${x.album.id}\n🎵 *Jumlah Trek Album:* ${x.album.total_tracks}\n🔢 *Nomor Trek:* ${x.album.track_number}\n⏳ *Durasi:* ${x.duration_ms} ms\n🔗 *Uri:* ${x.uri}\n🎵 *URL Album*: ${x.album.external_urls.spotify}\n🔗 *URL:* ${x.external_urls.spotify}\n${x.preview_url ? `🎧 *Direct URL:* ${x.preview_url}` : ""}\n───────────────────`;
    m.reply(ini_txt);
  } catch (e) {
    throw "❌ *Spotify Search* ❌\n\nTerjadi Kesalahan, Coba Lagi Nanti.";
  }
};
(handler.help = ["spotifysearch"]),
  (handler.tags = ["downloader"]),
  (handler.command = /^spotifysearch$/i),
  (handler.limit = !0);
export default handler;
async function searchSpotifyTracks(query) {
  const auth = Buffer.from(
      "acc6302297e040aeb6e4ac1fbdfd62c3:0e8439a1280a43aba9a5bc0a16f3f009",
    ).toString("base64"),
    accessToken = await (async () => {
      const response = await fetch("https://accounts.spotify.com/api/token", {
        method: "POST",
        timeout: 6e4,
        body: new URLSearchParams({
          grant_type: "client_credentials",
        }),
        headers: {
          Authorization: `Basic ${auth}`,
        },
      });
      return (await response.json()).access_token;
    })(),
    searchUrl = `https://api.spotify.com/v1/search?q=${encodeURIComponent(query)}&type=track&offset=10`,
    response = await fetch(searchUrl, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
  return (await response.json()).tracks.items;
}
