import{n as e}from"./rolldown-runtime-DAXXjFlN.js";import{Gi as t,Zi as n,Zn as r,an as i,da as a,ia as o,on as s,ra as c,sn as l}from"./ws-Dl8XkIy4.js";import{i as u,r as d}from"./lyrics-preferences-store-CGvOZDy3.js";import{a as f,c as p,n as m,o as h,s as g,t as _}from"./LyricsComposable-Csyn82Qp.js";function v(){return M||=m({props:{offsetY:128,noSingLyrics:!1,simpleLyricStyle:!1,isImmersive:!1,zoomScale:1,showNoLyricsFoundText:!0,type:`lyrics`}}),M}function y(e){let t=Promise.resolve(),n=e=>{t=e.then(()=>void 0,()=>void 0)},r=N.then(()=>e(n),()=>e(n));return N=r.then(()=>t,()=>t),r}function b(e){return{id:e,type:e.startsWith(`i.`)?`library-songs`:`songs`}}async function x(e){if(e.attributes)return e;let t=(await a(e.id.startsWith(`i.`)?`/v1/me/library/songs/${e.id}`:`/v1/catalog/$STOREFRONT/songs/${e.id}`,{},{suppressServiceIssue:!0}))?.data?.data?.[0];return t?.attributes?t:e}function S(e,t){let n=[];for(let r of e){if(r.isCredit)continue;let e={start:r.start,end:r.end,text:r.text,empty:r.empty};r.songPart&&(e.songPart=r.songPart),r.isDuet!==void 0&&(e.isDuet=r.isDuet),r.isBackground!==void 0&&(e.isBackground=r.isBackground),t&&r.words?.length&&(e.words=r.words.map(e=>{let t={start:e.start,end:e.end,text:e.text};return e.isBackground!==void 0&&(t.isBackground=e.isBackground),t})),n.push(e)}return n}async function C(e,t,n){let r=v();r.lyricsList.value=[];let i=t.provider?A[t.provider]:void 0,a=e,o=(async()=>{a=await x(e),await r.getLyrics(a,i)})();n(o);let s=await Promise.race([o.then(()=>`done`,()=>`error`),new Promise(e=>setTimeout(()=>e(`timeout`),j))]);if(s===`timeout`)return{ok:!1,reason:`TIMEOUT`};if(s===`error`||r.lyricsList.value.length===0||r.currentTrackId.value!==a.id)return{ok:!1,reason:`NOT_FOUND`};let c=!!r.rawTtml.value&&(r.sourceInfo.provider===`Apple Music`||r.sourceInfo.provider===`User`),l=r.lyricsList.value,u=r.sourceInfo.timingType;if(t.words&&c&&!l.some(e=>e.words?.length))try{let e=p(r.rawTtml.value);e.lyrics.length>0&&(l=e.lyrics,u=e.type)}catch(e){console.debug(`[LyricsRPC] TTML re-parse failed, returning line-level lyrics`,e)}let d={ok:!0,id:r.currentTrackId.value??a.id,source:{provider:r.sourceInfo.provider,timingType:u,language:r.sourceInfo.language,contributorName:r.sourceInfo.contributorName},writers:[...r.songWriters.value],lines:S(l,!!t.words)};return t.ttml&&c&&(d.ttml=r.rawTtml.value),d}function w(e,t={}){return y(n=>C(b(e),t,n))}function T(e={}){let t=o().nowPlayingItemMediaItem;if(!t?.id)return Promise.resolve({ok:!1,reason:`NO_TRACK`});let n;try{n=JSON.parse(JSON.stringify(t))}catch{n=b(t.id)}return y(t=>C(n,e,t))}async function E(e){let n=await t(b(e)),r=await h.getAllUserLyrics(n);return r?.lyrics?.length?{ok:!0,trackId:n,variations:r.lyrics.map(e=>({variationId:e.id,contributorName:e.contributorName??null,language:e.language,syncType:e.syncType,accessCount:e.accessCount,trackName:e.trackName,artistName:e.artistName}))}:{ok:!1,reason:`NOT_FOUND`}}async function D(e,n,r={}){let i=await t(b(e)),a=(await h.getAllUserLyrics(i))?.lyrics?.find(e=>e.id===n);if(!a)return{ok:!1,reason:`NOT_FOUND`};let o;try{o=p(a.ttml)}catch{return{ok:!1,reason:`NOT_FOUND`}}if(!o.lyrics.length)return{ok:!1,reason:`NOT_FOUND`};let s={ok:!0,variationId:a.id,source:{provider:`User`,timingType:o.type,language:a.language||o.language,contributorName:a.contributorName},writers:(a.writers??[]).length>0?a.writers:o.writers.map(e=>e.name),lines:S(o.lyrics,!!r.words)};return r.ttml&&(s.ttml=a.ttml),s}async function O(e,n){let r=l(n);if(!r)return{ok:!1,reason:`NOT_FOUND`};let i=await t(b(e)),a=u(),o=a.getTranslations(i,r);if(o)return{ok:!0,language:r,credit:o.credit,translations:o.translations.map(e=>({line:e.matched_line,translation:e.translation}))};try{let e=await h.getTranslations(i,r);return e.translations.length?(a.addTranslations(e.translations,i,r,e.credit),{ok:!0,language:r,credit:e.credit,translations:e.translations.map(e=>({line:e.matched_line,translation:e.translation}))}):{ok:!1,reason:`NOT_FOUND`}}catch{return{ok:!1,reason:`NOT_FOUND`}}}function k(){return{languages:i}}var A,j,M,N,P=e((()=>{_(),d(),c(),r(),n(),g(),f(),s(),A={"apple-music":`Apple Music`,musixmatch:`musixmatch`,netease:`NetEase`,user:`User`},j=55e3,M=null,N=Promise.resolve()}));export{D as a,O as i,w as n,E as o,k as r,P as s,T as t};