"use strict";(()=>{function e(e){return function(e){return e.charAt(0).toUpperCase()+e.slice(1)}(e.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g,((e,t)=>t.toUpperCase())))}var t=(e,...t)=>{console[e]("[35m[better-spotify-genres][0m",...t)},r=e=>new Promise((t=>setTimeout(t,e)));!async function n(){if(!(Spicetify.Platform&&Spicetify.URI&&Spicetify.Player.data))return void setTimeout(n,300);function i(e,t){Spicetify.LocalStorage.set(e,t)}function s(){try{return JSON.parse((e="showGenre:settings",Spicetify.LocalStorage.get(e)??"{}"))}catch{return i("showGenre:settings","{}"),{}}var e}window._onGenreItemMouseOver=e=>{e.style.setProperty("color","var(--spice-text)")},window._onGenreItemMouseOut=e=>{e.style.setProperty("color","var(--spice-subtext)")};const a={cached:{pop:"spotify:playlist:6gS3HhOiI17QNojjPuPzqc"}};let o=s();function c(e,t){if(e){let r=s();return r[e]=t,void i("showGenre:settings",JSON.stringify(r))}i("showGenre:settings",JSON.stringify(o))}async function l(e,t=null,r=document.body){return new Promise((n=>{if(document.querySelector(e))return n(document.querySelector(e));const i=new MutationObserver((()=>{document.querySelector(e)?(n(document.querySelector(e)),i.disconnect()):"number"==typeof t&&setTimeout((()=>{i.disconnect(),n(null)}),t)}));i.observe(r,{childList:!0,subtree:!0})}))}async function u(){const{artist_name:r,title:n}=Spicetify.Player.data.item.metadata,i=await async function(e,t){let r=new URL("https://musicbrainz.org/ws/2/recording?fmt=json&limit=1");r.searchParams.set("query",`recording:"${t}" AND artist:"${e}"`);const n=await fetch(r).then((e=>e.json()));if(!n.recordings?.[0])return[];r=new URL(`https://musicbrainz.org/ws/2/recording/${n.recordings[0].id}?inc=tags+artists&fmt=json`);const i=await fetch(r).then((e=>e.json()));return 0===i.tags.length?i["artist-credit"][0].artist.tags.map((e=>e.name)):i.tags.map((e=>e.name))}(r,n);if(0===i.length)return t("warn","No genres found for the current track, removing genres from UI..."),void await y();const s=(await Promise.all(i.map((t=>async function(t){const r=o.cached[e(t)];if(null!=r)return{uri:r,genre:t};const n=new RegExp(`^the sound of ${t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}$`,"i"),i=await Spicetify.GraphQL.Request({name:"searchDesktop",operation:"query",sha256Hash:"fcad5a3e0d5af727fb76966f06971c19cfa2275e6ff7671196753e008611873c",value:null},{searchTerm:`The Sound of ${t}`,limit:1,offset:0,numberOfTopResults:1,includeArtistHasConcertsField:!1,includeAudiobooks:!1,includeAuthors:!1,includePreReleases:!1});for(const{data:r}of i.data.searchV2.playlists.items)return"thesoundsofspotify"===r.ownerV2.data.username&&n.test(r.name)?(o.cached[e(t)]=r.uri,c("cached",o.cached),{uri:r.uri,genre:t}):{uri:r.uri+"|||",genre:t};return{uri:null,genre:t}}(t))))).filter((e=>null!==e.uri)).map((e=>`${e.genre.replace(/(^\w{1})|([\s-]+\w{1})/g,(e=>e.toUpperCase()))}`)).join(", ");if(f||(f=document.createElement("div")),f.innerHTML=s,await p(),null!==d){f.style.fontSize="12px",f.style.color="var(--spice-misc)",f.style.setProperty("color","var(--spice-subtext)"),f.classList.add("ellipsis-one-line"),f.style.setProperty("grid-area","genres");{let[e,t]=window.getComputedStyle(d).getPropertyValue("grid-template").split("/").map((e=>e.trim()));const r='"genres genres"',n='"quality quality"';e.endsWith(r)||(e.includes(n)?e=e.replace(n,`${r} ${n}`):e+=` ${r}`);const i=`${e} / ${t}`;d.style.setProperty("grid-template",i)}d.appendChild(f)}}Object.keys(a).forEach((e=>{const t=e;void 0===o[t]&&(o={...o,[t]:a[t]})})),c();let d=null,f=null;const p=async()=>{const e=(await Promise.all([l("div.main-trackInfo-container",1e3),l("div.main-nowPlayingWidget-trackInfo",1e3)])).find((e=>null!==e));if(!e)return t("error","Couldn't find the info container, genres will not be displayed."),void(d=null);d=e};async function y(){await p();try{if(null===d||null===f)return;d.style.removeProperty("grid-template"),d.removeChild(f),f=null,d=null}catch{}}async function m(){if(Spicetify.Player.data.item.metadata.is_local||"track"!==Spicetify.URI.fromString(Spicetify.Player.data.item.uri).type)return t("warn","Current track is local, removing genres from UI..."),void await y();await u()}for(;!Spicetify.Player.data;)await r(1e3);m(),Spicetify.Player.addEventListener("songchange",m),t("info","Initialized")}()})();