import{a as v,o as t,L as k}from"./chunk-NL6KNZEE-DfxmOBHd.js";import{k as _,u as P,P as E}from"./useTheme-2QbCXKY0.js";import{A as q,w as F,B as M,C as R}from"./api-BuTqLiNI.js";import{c as A}from"./createLucideIcon-Bp-vR5YK.js";import{a as K}from"./utils-BqohOHgI.js";import{A as $}from"./ArtistLinks-B4A9lGBW.js";import{u as z}from"./AppProvider-z3c8LSyC.js";/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const O=[["path",{d:"m6 9 6 6 6-6",key:"qrunsl"}]],T=A("chevron-down",O);/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const U=[["path",{d:"m18 15-6-6-6 6",key:"153udz"}]],B=A("chevron-up",U);function G({stepSetter:a,currentStep:d,rangeSetter:m,currentRange:h,disableCache:n=!1}){const p=["day","week","month"],o=[105,182,364],[x,g]=v.useState(!0),u=e=>{g(e),n||localStorage.setItem("activity_configuring_"+window.location.pathname.split("/")[1],String(!e))},f=e=>{a(e),n||localStorage.setItem("activity_step_"+window.location.pathname.split("/")[1],e)},y=e=>{m(e),n||localStorage.setItem("activity_range_"+window.location.pathname.split("/")[1],String(e))};return v.useEffect(()=>{if(!n){const e=parseInt(localStorage.getItem("activity_range_"+window.location.pathname.split("/")[1])??"182");e&&m(e);const r=localStorage.getItem("activity_step_"+window.location.pathname.split("/")[1]);r&&a(r);const l=localStorage.getItem("activity_configuring_"+window.location.pathname.split("/")[1]);r&&u(l!=="true")}},[]),t.jsxs("div",{className:"relative w-full",children:[t.jsx("button",{onClick:()=>u(!x),className:"absolute left-[75px] -top-9 text-muted hover:color-fg transition",title:"Toggle options",children:x?t.jsx(T,{size:18}):t.jsx(B,{size:18})}),t.jsx("div",{className:`overflow-hidden transition-[max-height,opacity] duration-250 ease ${x?"max-h-0 opacity-0":"max-h-[100px] opacity-100"}`,children:t.jsxs("div",{className:"flex flex-wrap gap-4 mt-1 text-sm",children:[t.jsxs("div",{className:"flex items-center gap-1",children:[t.jsx("span",{className:"text-muted",children:"Step:"}),p.map(e=>t.jsx("button",{className:`px-1 rounded transition ${e===d?"color-fg font-medium":"color-fg-secondary hover:color-fg"}`,onClick:()=>f(e),disabled:e===d,children:e},e))]}),t.jsxs("div",{className:"flex items-center gap-1",children:[t.jsx("span",{className:"text-muted",children:"Range:"}),o.map(e=>t.jsx("button",{className:`px-1 rounded transition ${e===h?"color-fg font-medium":"color-fg-secondary hover:color-fg"}`,onClick:()=>y(e),disabled:e===h,children:e},e))]})]})})]})}function H(a){const d=a.primary,m=d.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/);if(m){const[,h,n,p]=m.map(Number);return"#"+[h,n,p].map(o=>o.toString(16).padStart(2,"0")).join("")}return d}function tt({step:a="day",range:d=182,month:m=0,year:h=0,artistId:n=0,albumId:p=0,trackId:o=0,configurable:x=!1}){const[g,u]=v.useState(a),[f,y]=v.useState(d),{isPending:e,isError:r,data:l,error:N}=_({queryKey:["listen-activity",{step:g,range:f,month:m,year:h,artist_id:n,album_id:p,track_id:o}],queryFn:({queryKey:s})=>q(s[1])}),{theme:b}=P(),L=H(b);if(e)return t.jsxs("div",{className:"w-[350px]",children:[t.jsx("h3",{children:"Activity"}),t.jsx("p",{children:"Loading..."})]});if(r)return t.jsxs("div",{className:"w-[350px]",children:[t.jsx("h3",{children:"Activity"}),t.jsxs("p",{className:"error",children:["Error: ",N.message]})]});function C(s,c){s=String(s).replace(/[^0-9a-f]/gi,""),s.length<6&&(s=s[0]+s[0]+s[1]+s[1]+s[2]+s[2]),c=c||0;var i="#",w,j;for(j=0;j<3;j++)w=parseInt(s.substring(j*2,j*2+2),16),w=Math.round(Math.min(Math.max(0,w+w*c),255)).toString(16),i+=("00"+w).substring(w.length);return i}const D=(s,c)=>{const i=n==p&&p==o&&o==0?10:1;switch(g){case"day":c=10*i;break;case"week":c=20*i;break;case"month":c=50*i;break;case"year":c=100*i;break}return s=Math.min(s,c),(s-c)/c*.8},S=26*7,I=[];for(let s=0;s<l.length;s+=S)I.push(l.slice(s,s+S));return t.jsxs("div",{className:"flex flex-col items-start",children:[t.jsx("h3",{children:"Activity"}),x?t.jsx(G,{rangeSetter:y,currentRange:f,stepSetter:u,currentStep:g}):null,I.map((s,c)=>t.jsx("div",{className:"w-auto grid grid-flow-col grid-rows-7 gap-[3px] md:gap-[5px] mb-4",children:s.map(i=>t.jsx("div",{className:"w-[10px] sm:w-[12px] h-[10px] sm:h-[12px]",children:t.jsx(E,{position:"top",space:12,extraClasses:"left-2",inner:`${new Date(i.start_time).toLocaleDateString()} ${i.listens} plays`,children:t.jsx("div",{style:{display:"inline-block",background:i.listens>0?C(L,D(i.listens,100)):"var(--color-bg-secondary)"},className:`w-[10px] sm:w-[12px] h-[10px] sm:h-[12px] rounded-[2px] md:rounded-[3px] ${i.listens>0?"":"border-[0.5px] border-(--color-bg-tertiary)"}`})})},new Date(i.start_time).toString()))},c))]})}function et(a){const{user:d}=z(),{isPending:m,isError:h,data:n,error:p}=_({queryKey:["last-listens",{limit:a.limit,period:"all_time",artist_id:a.artistId,album_id:a.albumId,track_id:a.trackId}],queryFn:({queryKey:r})=>M(r[1])}),{data:o}=_({queryKey:["now-playing"],queryFn:()=>R()}),x="Last played",[g,u]=v.useState(null),f=async r=>{if(n)try{const l=await F(r);l.ok||l.status>=200&&l.status<300?u(N=>(N??n.items).filter(b=>b.time!==r.time)):console.error("Failed to delete listen:",l.status)}catch(l){console.error("Error deleting listen:",l)}};if(m)return t.jsxs("div",{className:"w-[300px] sm:w-[500px]",children:[t.jsx("h3",{children:x}),t.jsx("p",{children:"Loading..."})]});if(h)return t.jsxs("div",{className:"w-[300px] sm:w-[500px]",children:[t.jsx("h3",{children:x}),t.jsxs("p",{className:"error",children:["Error: ",p.message]})]});const y=g??n.items;let e="";return e+=a.artistId?`&artist_id=${a.artistId}`:"",e+=a.albumId?`&album_id=${a.albumId}`:"",e+=a.trackId?`&track_id=${a.trackId}`:"",t.jsxs("div",{className:"text-sm sm:text-[16px]",children:[t.jsx("h3",{className:"hover:underline",children:t.jsx(k,{to:`/listens?period=all_time${e}`,children:x})}),y.length<1&&"Nothing to show",t.jsx("table",{className:"-ml-4",children:t.jsxs("tbody",{children:[a.showNowPlaying&&o&&o.currently_playing&&t.jsxs("tr",{className:"group hover:bg-[--color-bg-secondary]",children:[t.jsx("td",{className:"w-[18px] pr-2 align-middle"}),t.jsx("td",{className:"color-fg-tertiary pr-2 sm:pr-4 text-sm whitespace-nowrap w-0",children:"Now Playing"}),t.jsxs("td",{className:"text-ellipsis overflow-hidden max-w-[400px] sm:max-w-[600px]",children:[a.hideArtists?null:t.jsxs(t.Fragment,{children:[t.jsx($,{artists:o.track.artists})," –"," "]}),t.jsx(k,{className:"hover:text-[--color-fg-secondary]",to:`/track/${o.track.id}`,children:o.track.title})]})]}),y.map(r=>t.jsxs("tr",{className:"group hover:bg-[--color-bg-secondary]",children:[t.jsx("td",{className:"w-[18px] pr-2 align-middle",children:t.jsx("button",{onClick:()=>f(r),className:"opacity-0 group-hover:opacity-100 transition-opacity text-(--color-fg-tertiary) hover:text-(--color-error)","aria-label":"Delete",hidden:d==null,children:"×"})}),t.jsx("td",{className:"color-fg-tertiary pr-2 sm:pr-4 text-sm whitespace-nowrap w-0",title:new Date(r.time).toString(),children:K(new Date(r.time))}),t.jsxs("td",{className:"text-ellipsis overflow-hidden max-w-[400px] sm:max-w-[600px]",children:[a.hideArtists?null:t.jsxs(t.Fragment,{children:[t.jsx($,{artists:r.track.artists})," –"," "]}),t.jsx(k,{className:"hover:text-[--color-fg-secondary]",to:`/track/${r.track.id}`,children:r.track.title})]})]},`last_listen_${r.time}`))]})})]})}export{tt as A,et as L};
