import{A as e,I as t,N as n,j as r,k as i,t as a,z as o}from"./jsx-runtime-ktpX_HJe.js";import{r as s,t as c}from"./Image-iLcFsxYI.js";import{t as l}from"./createLucideIcon-x9JrQxsp.js";import{t as u}from"./utils-BRMsoFp5.js";var d=l(`chevron-left`,[[`path`,{d:`m15 18-6-6 6-6`,key:`1wnfg3`}]]),f=l(`chevron-right`,[[`path`,{d:`m9 18 6-6-6-6`,key:`mthhwq`}]]),p=a();function m(e){return(0,p.jsxs)(`div`,{className:`flex items-baseline gap-1.5`,children:[(0,p.jsx)(`div`,{className:`w-23 text-end shrink-0`,children:(0,p.jsxs)(`span`,{className:`
            relative inline-block
            text-2xl font-semibold
          `,children:[(0,p.jsx)(`span`,{className:`
              absolute inset-0
              -translate-x-2 translate-y-8
              bg-(--color-primary)
              z-0
              h-0.5
            `,"aria-hidden":!0}),(0,p.jsx)(`span`,{className:`relative z-1`,children:e.figure})]})}),(0,p.jsx)(`span`,{className:`text-sm`,children:e.text})]})}function h({title:e,imageSrc:t,items:n,getLabel:r,includeTime:i}){let[a,...o]=n;if(!a)return null;let s=r(a.item).length>28?`text-xl lg:text-2xl`:`text-2xl lg:text-4xl`;return(0,p.jsxs)(`div`,{className:`flex flex-col sm:flex-row gap-5`,children:[(0,p.jsx)(`div`,{className:`rewind-top-item-image min-w-[210px]`,children:(0,p.jsx)(c,{size:210,src:t,alt:e})}),(0,p.jsxs)(`div`,{className:`flex flex-col gap-1`,children:[(0,p.jsx)(`h5`,{className:`-mb-1 uppercase text-(--color-fg-secondary)`,children:e}),(0,p.jsx)(`div`,{className:`flex items-center gap-2`,children:(0,p.jsxs)(`div`,{className:`flex flex-col items-start mb-2`,children:[(0,p.jsx)(`h5`,{className:`${s} font-semibold mb-2 mt-2 max-w-100 wrap-normal`,children:r(a.item)}),(0,p.jsxs)(`span`,{className:`text-(--color-fg-secondary) -mt-2 text-sm`,children:[`${a.item.listen_count} plays`,i?` (${Math.floor(a.item.time_listened/60)} minutes)`:``]})]})}),o.map(e=>(0,p.jsxs)(`div`,{className:`text-sm max-w-90 wrap-normal`,children:[r(e.item),(0,p.jsxs)(`span`,{className:`text-(--color-fg-secondary)`,children:[` - ${e.item.listen_count} plays`,i?` (${Math.floor(e.item.time_listened/60)} minutes)`:``]})]},e.item.id))]})]})}function g(e){let t=e.stats.top_artists[0]?.item.image,n=e.stats.top_albums[0]?.item.image,r=e.stats.top_tracks[0]?.item.image;return!e.stats.top_artists[0]||!e.stats.top_albums[0]||!e.stats.top_tracks[0]?(0,p.jsx)(`p`,{children:`Not enough data exists to create a Rewind for this period :(`}):(0,p.jsxs)(`div`,{className:`flex flex-col gap-7 card pt-4 sm:pt-6 pb-8 sm:pb-12 px-4 sm:px-8 min-w-[350px]`,children:[(0,p.jsx)(`div`,{className:`w-full text-start shrink-0 sm:ml-6`,children:(0,p.jsxs)(`span`,{className:`
            relative inline-block
            text-2xl md:text-4xl font-semibold
          `,children:[(0,p.jsx)(`span`,{className:`
              absolute inset-0
              sm:-translate-x-6 translate-y-9 sm:translate-y-11
              bg-(--color-primary)
              z-0
              h-0.5
            `,"aria-hidden":!0}),(0,p.jsx)(`h5`,{className:`mb-2`,children:e.stats.title})]})}),(0,p.jsx)(h,{title:`Top Artist`,imageSrc:t?.medium,items:e.stats.top_artists,getLabel:e=>e.name,includeTime:e.includeTime}),(0,p.jsx)(h,{title:`Top Album`,imageSrc:n?.medium,items:e.stats.top_albums,getLabel:e=>e.title,includeTime:e.includeTime}),(0,p.jsx)(h,{title:`Top Track`,imageSrc:r?.medium,items:e.stats.top_tracks,getLabel:e=>e.title,includeTime:e.includeTime}),(0,p.jsxs)(`div`,{className:`grid grid-cols-1 sm:grid-cols-3 gap-y-5`,children:[(0,p.jsx)(m,{figure:`${e.stats.minutes_listened}`,text:`Minutes`}),(0,p.jsx)(m,{figure:`${e.stats.unique_tracks}`,text:`Tracks`}),(0,p.jsx)(m,{figure:`${e.stats.new_tracks}`,text:`New tracks`}),(0,p.jsx)(m,{figure:`${e.stats.plays}`,text:`Plays`}),(0,p.jsx)(m,{figure:`${e.stats.unique_albums}`,text:`Albums`}),(0,p.jsx)(m,{figure:`${e.stats.new_albums}`,text:`New albums`}),(0,p.jsx)(m,{figure:`${e.stats.avg_plays_per_day.toFixed(1)}`,text:`Plays / day`}),(0,p.jsx)(m,{figure:`${e.stats.unique_artists}`,text:`Artists`}),(0,p.jsx)(m,{figure:`${e.stats.new_artists}`,text:`New artists`})]})]})}var _=o(t(),1),v=[`January`,`February`,`March`,`April`,`May`,`June`,`July`,`August`,`September`,`October`,`November`,`December`],y=e=>{let t=e.get(`year`),n=e.get(`month`);if(!t&&!n)return u();if(t&&n)return{year:parseInt(t),month:parseInt(n)};if(t)return{year:parseInt(t)};let r=parseInt(n),i=new Date,a=i.getFullYear();return{year:r<=i.getMonth()+1?a:a-1,month:r}};async function b({request:e}){let{year:t,month:n}=y(new URL(e.url).searchParams),r=await fetch(`/apis/web/v1/summary?year=${t}&month=${n}`);if(!r.ok)throw new Response(`Failed to load summary`,{status:500});let i=await r.json();return i.title=`Your ${n&&n>0?v[n-1]:``} ${t} Rewind`,{stats:i}}var x=n(function(){let t=e(),n=r(),{stats:a}=i(),{firstActivity:o}=s(),{year:c,month:l}=y(new URLSearchParams(t.search)),[u,m]=(0,_.useState)(c),[h,b]=(0,_.useState)(l),[x,S]=(0,_.useState)(!l||l===0),[C,w]=(0,_.useState)(!1),T=e=>{let r=new URLSearchParams(t.search);for(let[t,n]of Object.entries(e))n===null?r.delete(t):r.set(t,n);n(`/rewind?${r.toString()}`,{replace:!1})},E=e=>{b(e),T({year:u.toString(),month:e.toString()})},D=e=>{m(e===`next`?u+1:u-1)};function O(e,t){return new Date(t,e+1)>new Date}function k(e,t){return!!o&&new Date(t,e+1)<o}function A(e,t){return O(e,t)||k(e,t)}let j=`${a.title} - Koito`;return(0,p.jsxs)(`div`,{className:`w-full min-h-screen`,children:[(0,p.jsx)(`title`,{children:j}),(0,p.jsx)(`meta`,{property:`og:title`,content:j}),(0,p.jsx)(`meta`,{name:`description`,content:j}),(0,p.jsxs)(`div`,{className:`flex flex-col lg:flex-row items-center lg:items-start lg:mt-15 mt-5 gap-10 w-19/20 mx-auto px-5 md:px-10`,children:[(0,p.jsx)(`div`,{className:`flex flex-col items-start gap-4`,children:(0,p.jsxs)(`div`,{className:`flex flex-col items-center gap-4 mt-4 sm:mt-14 w-[250px] mx-auto`,children:[!x&&(0,p.jsxs)(p.Fragment,{children:[(0,p.jsxs)(`div`,{className:`flex items-center gap-6 justify-around`,children:[(0,p.jsx)(`button`,{onClick:()=>D(`prev`),className:`p-2 disabled:opacity-0`,disabled:new Date(u-1,h||0)>new Date||o&&u-1<o.getFullYear(),children:(0,p.jsx)(d,{size:20})}),(0,p.jsx)(`p`,{className:`font-medium text-xl text-center w-30`,children:u}),(0,p.jsx)(`button`,{onClick:()=>D(`next`),className:`p-2 disabled:opacity-0`,disabled:O(0,u+1),children:(0,p.jsx)(f,{size:20})})]}),(0,p.jsx)(`div`,{className:`grid grid-cols-2 gap-2`,children:v.map((e,t)=>{let n=h!==void 0&&t===h-1&&c===u,r=A(t,u);return(0,p.jsx)(`button`,{className:`px-12 py-2 rounded-(--border-radius)
                          ${n?`card`:`border-1 border-(--color-bg)`}
                          ${r?`color-fg-tertiary`:`hover:bg-(--color-bg-secondary)`}
                        `,onClick:()=>E(t+1),disabled:r||n,children:e.substring(0,3)},e)})})]}),x&&o&&(0,p.jsx)(`div`,{className:`grid grid-cols-2 gap-2`,children:Array.from({length:new Date().getFullYear()-o.getFullYear()},(e,t)=>o.getFullYear()+t).map(e=>(0,p.jsx)(`button`,{className:`px-10 py-2 rounded-(--border-radius) hover:bg-(--color-bg-secondary)
                      ${e===u&&!h?`card`:`border-1 border-(--color-bg)`}
                    `,onClick:()=>{m(e),b(void 0),T({year:e.toString(),month:null})},disabled:(!h||h===0)&&e===u,children:e},e))}),(0,p.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,p.jsx)(`input`,{id:`show-time-checkbox`,type:`checkbox`,checked:C,onChange:e=>w(e.target.checked)}),(0,p.jsx)(`label`,{htmlFor:`show-time-checkbox`,children:`Show time listened?`})]})]})}),(0,p.jsxs)(`div`,{className:`flex flex-col items-center gap-4`,children:[(0,p.jsxs)(`div`,{className:`flex justify-around w-2/3 mb-4`,children:[(0,p.jsx)(`button`,{className:`period-selector`,onClick:()=>S(!1),disabled:!x,children:`MONTHLY`}),(0,p.jsx)(`button`,{className:`period-selector`,onClick:()=>S(!0),disabled:x,children:`YEARLY`})]}),a&&(0,p.jsx)(g,{stats:a,includeTime:C})]})]})]})});export{b as clientLoader,x as default};