!async function(){for(;!Spicetify.React||!Spicetify.ReactDOM;)await new Promise(t=>setTimeout(t,10));var I,o,M,A,$,z,t,e,n,r,F,q,U,W,J,K,H,Q,G,Y,Z,V,X,h,s,tt,i,a,et,c,nt,u,rt,it,st,l,at,f,d,p,ot,ct,ut,b,lt,ht,g,ft,dt,pt,bt,gt,yt,vt,y,v,mt,wt,St,xt,Bt,Pt,Et,_t,kt,Ct,Dt,m,w,Rt,jt,S,Nt,Ot,Tt;I=Object.create,o=Object.defineProperty,M=Object.getOwnPropertyDescriptor,A=Object.getOwnPropertyNames,$=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty,e=(t,e,n)=>{n=null!=t?I($(t)):{};var r=!e&&t&&t.__esModule?n:o(n,"default",{value:t,enumerable:!0}),i=t,s=void 0,a=void 0;if(i&&"object"==typeof i||"function"==typeof i)for(let t of A(i))z.call(r,t)||t===s||o(r,t,{get:()=>i[t],enumerable:!(a=M(i,t))||a.enumerable});return r},n=(t,e,n)=>{var r;return r=n,(e="symbol"!=typeof e?e+"":e)in(t=t)?o(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,n},r=(t=(t,e)=>function(){return e||(0,t[A(t)[0]])((e={exports:{}}).exports,e),e.exports})({"external-global-plugin:react"(t,e){e.exports=Spicetify.React}}),F=e(t({"external-global-plugin:react-dom"(t,e){e.exports=Spicetify.ReactDOM}})()),q=/^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i,U=t=>{if("string"!=typeof t)throw new TypeError("Invalid argument expected string");var e=t.match(q);if(e)return e.shift(),e;throw new Error(`Invalid argument not valid semver ('${t}' received)`)},W=t=>"*"===t||"x"===t||"X"===t,J=t=>{var e=parseInt(t,10);return isNaN(e)?t:e},K=(t,e)=>{return W(t)||W(e)?0:([t,e]=(t=J(t),e=J(e),typeof t!=typeof e?[String(t),String(e)]:[t,e]),e{for(let t=0;t{var r=n;if("string"!=typeof r)throw new TypeError("Invalid operator type, expected string but got "+typeof r);if(-1===Y.indexOf(r))throw new Error("Invalid operator, expected one of "+Y.join("|"));r=e,e=U(e=t),r=U(r),t=e.pop(),i=r.pop();var i,r=0!==(e=H(e,r))?e:t&&i?H(t.split("."),i.split(".")):t||i?t?-1:1:0;return G[n].includes(r)},G={">":[1],">=":[0,1],"=":[0],"<=":[-1,0],"<":[-1],"!=":[-1,1]},Y=Object.keys(G),Z=class{analysis;constructor(t){t={bars:t.bars.map(t=>this.toRemixed(t)),beats:t.beats.map(t=>this.toRemixed(t)),sections:t.sections.map(t=>this.toRemixedSection(t)),segments:t.segments.map(t=>this.toRemixedSegment(t)),tatums:t.tatums.map(t=>this.toRemixed(t))};this.analysis=t}toRemixed(t){return{...t,firstOverlappingSegment:void 0,overlappingSegments:[],children:[],parent:void 0,indexInParent:0,prev:void 0,next:void 0,index:0}}toRemixedSection(t){return{...t,children:[],prev:void 0,next:void 0,index:0}}toRemixedSegment(t){return{...t,parent:void 0,indexInParent:0,prev:void 0,next:void 0,index:0}}remixTrack(){return this.preprocessTrack()}preprocessTrack(){for(const r of["sections","bars","beats","tatums","segments"]){var t,e,n=this.analysis[r];for([t,e]of n.entries())0<(e.index=t)?e.prev=n[t-1]:e.prev=null,t=i.start&&r.starti.start)break}}}connectFirstOverlappingSegment(t,e){let n=0;var r=t.segments;for(const s of e)for(let t=n;t=s.start){s.firstOverlappingSegment=i,n=t;break}}}connectAllOverlappingSegments(t,e){let n=0;var r=t.segments;for(const s of e){s.overlappingSegments=[];for(let t=n;ts.start+s.duration)break;n=t,s.overlappingSegments.push(i)}}}}},V=class{beatsPlayed=0;startTime=0;track;analysis;graph;currentRandomBranchChance=0;constructor(t,e,n){this.track=t,this.analysis=e,this.graph=n,this.currentRandomBranchChance=window.jukebox.settings.minRandomBranchChance,this.startTime=(new Date).getTime()}},X=function(t,e){return(X=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(t,e){t.__proto__=e}:function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}))(t,e)},h=Ut(function(e){return function(t){e(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(t,e){return e+1+") "+t.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t}}),C.prototype.unsubscribe=function(){var e,t,n;if(!this.closed){this.closed=!0;var r=this._parentage;if(r)if(this._parentage=null,Array.isArray(r))try{for(var i=B(r),s=i.next();!s.done;s=i.next())s.value.remove(this)}catch(t){o={error:t}}finally{try{s&&!s.done&&(a=i.return)&&a.call(i)}finally{if(o)throw o.error}}else r.remove(this);var a=this.initialTeardown;if(k(a))try{a()}catch(t){n=t instanceof h?t.errors:[t]}var o=this._finalizers;if(o){this._finalizers=null;try{for(var c=B(o),u=c.next();!u.done;u=c.next()){var l=u.value;try{Kt(l)}catch(t){n=null!=n?n:[],t instanceof h?n=E(E([],P(n)),P(t.errors)):n.push(t)}}}catch(t){e={error:t}}finally{try{u&&!u.done&&(t=c.return)&&t.call(c)}finally{if(e)throw e.error}}}if(n)throw new h(n)}},C.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)Kt(t);else{if(t instanceof C){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=null!=(e=this._finalizers)?e:[]).push(t)}},C.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},C.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},C.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&Wt(e,t)},C.prototype.remove=function(t){var e=this._finalizers;e&&Wt(e,t),t instanceof C&&t._removeParent(this)},C.EMPTY=((t=new C).closed=!0,t),tt=(s=C).EMPTY,i={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},a={setTimeout:function(t,e){for(var n=[],r=2;r=this.start&&t<=this.end}},xt=class{constructor(t,e){this.settings=t,this.beats=e,this.graph=new mt,this.allNeighbours=e.map(()=>[]),this.minLongBranch=e.length/5}graph;allEdges=[];allNeighbours=[];minLongBranch=0;computedMaxBranchDistance=0;maxBranches=4;generateGraph(){var t,e,n=this.beats.map(t=>new St(t.index,1e3*t.start,1e3*t.duration));for([t,e]of n.entries())tt.distance>e.distance?1:e.distance>t.distance?-1:0);for(let t=0;t!t.deleted&&!(this.settings.justBackwards&&t.destination.index>e||this.settings.justLongBranches&&Math.abs(t.destination.index-e)t&&(t=e)}return 100*t/this.beats.length}insertBestBackwardBranch(t,e){var n,r,i,s,a=[];for([n,r]of this.graph.beats.entries())for(const l of this.allNeighbours[n]){var o=l.destination.index,c=l.distance,u=n-o;0t.percentDistance-e.percentDistance),a.reverse(),s=(i=a[0]).currentBeat,t<(t=i.edge).distance)&&s.neighbours.push(t)}calculateReachability(){const n=this.graph.beats.map(t=>0);this.graph.beats.forEach((t,e)=>{n[e]=this.graph.beats.length-e});for(let t=0;t<1e3;t++){let e=0;for(var[r,i]of this.graph.beats.entries()){let t=!1;for(const a of i.neighbours){var s=n[a.destination.index];s>n[r]&&(n[r]=s,t=!0)}if(rn[r]&&(n[r]=i,t=!0),t){e++;for(let t=0;tr&&0t.destination.index!this.hasSequentialBranch(e,t))}}hasSequentialBranch(t,e){if(t.index!==this.graph.lastBranchPoint){var n=t.previous;if(null!==n){var r=t.index-e.destination.index;for(const i of n.neighbours)if(r==n.index-i.destination.index)return!0}}return!1}},Bt=class{constructor(t,e){this.songState=t,this.settings=e}playerSubscription=new s;currentBeat=null;bouncing=!1;bounceSeed=null;bounceCount=0;lastBranch=null;isSeekingResolver=null;beatsSinceLastBranch=0;onProgressSubject=new b;onProgress$=this.onProgressSubject.asObservable();start(){this.logDebug("Driver started."),document.addEventListener("keydown",this.onBounceKeyDown),document.addEventListener("keyup",this.onBounceKeyUp);var t=le(Spicetify.Player,"onprogress").pipe(oe(t=>t.data),fe()).subscribe(t=>{this.process(t)});this.playerSubscription.add(t)}onBounceKeyDown=t=>{"Shift"===t.key&&(this.bouncing=!0)};onBounceKeyUp=t=>{"Shift"===t.key&&(this.bouncing=!1)};async process(t){if(null!==this.isSeekingResolver){if(this.logDebug(`Is seeking... ${t} -> `+this.currentBeat?.start),!this.isSeekingResolver(t))return;this.isSeekingResolver=null}if(this.logDebug(`Processing with current beat: ${this.currentBeat?.toString()}, player time: `+Spicetify.Player.getProgress()),null!==this.lastBranch&&this.setLastBranchPlaying(!1),null!==this.currentBeat){if(this.currentBeat.isInBeat(t))return;this.currentBeat.isPlaying=!1}this.beatsSinceLastBranch++;var e=this.currentBeat?.next?.end,n=this.currentBeat?.previous?.start,e=null!=e&&eSpicetify.Player.getProgress();this.isSeekingResolver=t?t=>t>=i:t=>t<=i+1e3,this.logDebug(`Time to get there: ${Math.abs(Spicetify.Player.getProgress()-n)}ms`),await Mt("PlayerAPI").seekTo(i)}}getNextBeat(t,e){if(null===this.currentBeat||e){for(const n of this.songState.graph.beats)if(t>=n.start&&t<=n.end)return n;return this.songState.graph.beats[0]}return this.bouncing?(null===this.bounceSeed&&(this.bounceSeed=this.currentBeat,this.bounceCount=0),this.bounceCount++%2==1?this.selectNextNeighbor(this.bounceSeed):this.bounceSeed):null!=this.bounceSeed?(e=this.bounceSeed,this.bounceSeed=null,e):(e=this.currentBeat.index+1)>=this.songState.graph.beats.length?(this.stop(),null):this.selectRandomNextBeat(this.songState.graph.beats[e])}selectRandomNextBeat(t){var e;return 0!==t.neighbours.length&&this.shouldRandomBranch(t)?(e=t.neighbours.shift(),t.neighbours.push(e),this.beatsSinceLastBranch=0,this.lastBranch=e,this.setLastBranchPlaying(!0),e.destination):t}setLastBranchPlaying(t){null!==this.lastBranch&&(this.lastBranch.isPlaying=t,this.lastBranch.source.isPlaying=t,this.lastBranch.destination.isPlaying=t)}selectNextNeighbor(t){var e;return 0===t.neighbours.length?t:(e=t.neighbours.shift(),t.neighbours.push(e),this.lastBranch=e,this.setLastBranchPlaying(!0),e.destination)}shouldRandomBranch(t){if(t.index===this.songState.graph.lastBranchPoint&&this.settings.alwaysFollowLastBranch)return!0;if(this.beatsSinceLastBranch<=v.minBeatsBeforeBranching)return!1;t=(new Date).getTime()-this.songState.startTime;if(0this.settings.maxJukeboxPlayTime)return this.songState.currentRandomBranchChance=0,!1;this.songState.currentRandomBranchChance=Math.min(this.songState.currentRandomBranchChance+this.settings.randomBranchChanceDelta,this.settings.maxRandomBranchChance);t=Math.random(){this.stop(),this.start()});this.songChangedSubscription.add(t),await this.start()}disable(){this.stop(),this.songChangedSubscription.unsubscribe(),this.songChangedSubscription=new s,this.stateChangedSubject.next(!1)}stop(){this.driver?.stop(),this.driver=null,this.driverProcessSubscription.unsubscribe(),this.driverProcessSubscription=new s,this.songState=null}async start(){var e=Spicetify.Player.data.item;if(void 0!==e){Spicetify.showNotification("Fetching analysis for song...");var n=Spicetify.URI.fromString(e.uri);if(Spicetify.URI.isLocalTrack(n))this.disableWithError("No analysis available for local tracks.");else if(Spicetify.URI.isEpisode(n))this.disableWithError("No analysis available for shows.");else{var r,n=pe(n);if(null===n)this.disableWithError("Couldn't get track id.");else{let t=null;try{t=await be(n)}catch{}null===t||0===t.beats.length?this.disableWithError("No analysis available for this track."):(n=new Z(t).remixTrack(),r=new xt(this.settings,n.beats).generateGraph(),this.songState=new V(e,n,r),this.driver=new Bt(this.songState,this.settings),this.driverProcessSubscription.add(this.driver.onProgress$.subscribe(()=>{this.statsChangedSubject.next({beatsPlayed:this.songState?.beatsPlayed??0,currentRandomBranchChance:this.songState?.currentRandomBranchChance??0,listenTime:null!==this.songState?(new Date).getTime()-this.songState.startTime:0})})),this.driver.start())}}}}disableWithError(t){Spicetify.showNotification(t,!0),this.disable()}},m=e(r()),w=e(r()),Rt={xmlns:"http://www.w3.org/2000/svg",width:24,height:24,viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"},kt="Infinity",Ct=[["path",{d:"M12 12c-2-2.67-4-4-6-4a4 4 0 1 0 0 8c2 0 4-1.33 6-4Zm0 0c2 2.67 4 4 6 4a4 4 0 0 0 0-8c-2 0-4 1.33-6 4Z",key:"1z0uae"}]],(t=(0,w.forwardRef)(({color:t="currentColor",size:e=24,strokeWidth:n=2,absoluteStrokeWidth:r,className:i="",children:s,...a},o)=>(0,w.createElement)("svg",{ref:o,...Rt,width:e,height:e,stroke:t,strokeWidth:r?24*Number(n)/Number(e):n,className:["lucide","lucide-"+kt.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase().trim(),i].join(" "),...a},[...Ct.map(([t,e])=>(0,w.createElement)(t,e)),...Array.isArray(s)?s:[s]]))).displayName=""+kt,jt=t,S=e(r(),1),Nt=(()=>"undefined"==typeof window?S.useEffect:S.useLayoutEffect)(),Ot=t=>t[1]?.next?t[1]:{next:t[1],error:t[2],complete:t[3]},Tt=e(r()),(async()=>{for(window.jukebox=new Dt;!Spicetify?.Platform;)await new Promise(t=>setTimeout(t,100));await 0;try{i=".player-controls__right",a=null,o=!(s=5e3);var t=await new Promise((t,e)=>{let n=document.querySelector(i);if(null!==n)o&&console.log("found element in querySelector"),t(n);else{const r=new MutationObserver(()=>{null!==(n=document.querySelector(i))&&(o&&console.log("found element in observer"),t(n),r.disconnect())});null===a&&(a=document.body),r.observe(a,{childList:!0,subtree:!0}),setTimeout(()=>{null===n&&(o&&console.log("trying to find element from querySelector again"),r.disconnect(),null!==(n=document.querySelector(i))?t(n):e(new Error(`Couldn't find the element "${i}".`)))},s)}});e=Tt.default.createElement(ye,null),n=t,r=F.default.createRoot(document.createElement("div")),e=F.default.createPortal(e,n),r.render(e),async function(t,n){var r=await It("History");const i=async()=>{await At(t,n)||Spicetify.showNotification("📢 A new version of the custom app is available.",!1,5e3)};if(r.location.pathname==="/"+n)await i();else{let e=null;e=r.listen(async t=>{t.pathname==="/"+n&&(await i(),e?.())})}}("1.0.0-beta.3","eternal-jukebox")}catch(t){console.error(t),Spicetify.showNotification("Failed to register the eternal jukebox playbar button",!0)}var e,n,r,i,s,a,o})();function Lt(t){return Spicetify.Platform?.[t]}async function It(t){return async function(t){let e=t();for(;void 0===e;)await new Promise(t=>setTimeout(t,100)),e=t();return e}(()=>Lt(t))}function Mt(t){var e=Lt(t);if(void 0===e)throw new Error(`Platform API "${t}" is not available`);return e}async function At(t,e){e=await(await(await fetch(`https://raw.githubusercontent.com/Pithaya/spicetify-apps/main/custom-apps/${e}/package.json`)).json()).version;return Q(t,e,">=")}function x(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}X(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function $t(t,a,o,c){return new(o=o||Promise)(function(n,e){function r(t){try{s(c.next(t))}catch(t){e(t)}}function i(t){try{s(c.throw(t))}catch(t){e(t)}}function s(t){var e;t.done?n(t.value):((e=t.value)instanceof o?e:new o(function(t){t(e)})).then(r,i)}s((c=c.apply(t,a||[])).next())})}function zt(r,i){var s,a,o,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function t(n){return function(t){var e=[n,t];if(s)throw new TypeError("Generator is already executing.");for(;c=u&&e[u=0]?0:c;)try{if(s=1,a&&(o=2&e[0]?a.return:e[0]?a.throw||((o=a.return)&&o.call(a),0):a.next)&&!(o=o.call(a,e[1])).done)return o;switch(a=0,(e=o?[2&e[0],o.value]:e)[0]){case 0:case 1:o=e;break;case 4:return c.label++,{value:e[1],done:!1};case 5:c.label++,a=e[1],e=[0];continue;case 7:e=c.ops.pop(),c.trys.pop();continue;default:if(!(o=0<(o=c.trys).length&&o[o.length-1])&&(6===e[0]||2===e[0])){c=0;continue}if(3===e[0]&&(!o||e[1]>o[0]&&e[1]=t.length?void 0:t)&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function P(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,s=n.call(t),a=[];try{for(;(void 0===e||0{a.current=s,o.current=Ot(s)}),i(()=>{const e=a.current[0],t=(o.current||(o.current=Ot(a.current)),e.subscribe({next:t=>{e===a.current[0]&&o.current.next?.(t)},error:t=>{e===a.current[0]&&(o.current.error?o.current:console).error(t)},complete:()=>{e===a.current[0]&&o.current.complete?.()}}));return c.current=t,()=>{t.unsubscribe()}},[s[0]]),c}}function ye(){var[t,e]=(0,m.useState)(!1);return ge(window.jukebox.stateChanged$,e),m.default.createElement(Spicetify.ReactComponent.TooltipWrapper,{label:"Enable jukebox",showDelay:100,renderInline:!1},m.default.createElement("button",{className:"main-repeatButton-button "+(t?"main-repeatButton-active":""),onClick:async function(){await window.jukebox.setEnabled(!window.jukebox.isEnabled)}},m.default.createElement(jt,{size:24})))}}();