import{w as p,A as u,a as h,o as t,L as f}from"./chunk-NL6KNZEE-DfxmOBHd.js";import{C as g}from"./ChartLayout-C3A1r0s5.js";import{w as j}from"./api-BuTqLiNI.js";import{a as w}from"./utils-BqohOHgI.js";import{A as b}from"./ArtistLinks-B4A9lGBW.js";import{u as N}from"./AppProvider-DDBhlShu.js";import"./color.esm-brSKLyvp.js";import"./PeriodSelector-pCJGRgEO.js";async function A({request:d}){const s=new URL(d.url),n=s.searchParams.get("page")||"0";s.searchParams.set("page",n);const i=await fetch(`/apis/web/v1/listens?${s.searchParams.toString()}`);if(!i.ok)throw new Response("Failed to load top tracks",{status:500});return{listens:await i.json()}}const S=p(function(){const{listens:s}=u(),[n,i]=h.useState(null),{user:c}=N(),m=async r=>{if(s)try{const e=await j(r);e.ok||e.status>=200&&e.status<300?i(o=>(o??s.items).filter(l=>l.time!==r.time)):console.error("Failed to delete listen:",e.status)}catch(e){console.error("Error deleting listen:",e)}},x=n??s.items;return t.jsx(g,{title:"Last Played",initialData:s,endpoint:"listens",render:({data:r,page:e,onNext:o,onPrev:l})=>t.jsxs("div",{className:"flex flex-col gap-5 text-sm md:text-[16px]",children:[t.jsxs("div",{className:"flex gap-15 mx-auto",children:[t.jsx("button",{className:"default",onClick:l,disabled:e<=1,children:"Prev"}),t.jsx("button",{className:"default",onClick:o,disabled:!r.has_next_page,children:"Next"})]}),t.jsx("table",{className:"-ml-4",children:t.jsx("tbody",{children:x.map(a=>t.jsxs("tr",{className:"group hover:bg-[--color-bg-secondary]",children:[t.jsx("td",{className:"w-[17px] pr-2 align-middle",children:t.jsx("button",{onClick:()=>m(a),className:"opacity-0 group-hover:opacity-100 transition-opacity text-(--color-fg-tertiary) hover:text-(--color-error)","aria-label":"Delete",hidden:c==null,children:"×"})}),t.jsx("td",{className:"color-fg-tertiary pr-2 sm:pr-4 text-sm whitespace-nowrap w-0",title:new Date(a.time).toString(),children:w(new Date(a.time))}),t.jsxs("td",{className:"text-ellipsis overflow-hidden max-w-[400px] sm:max-w-[600px]",children:[t.jsx(b,{artists:a.track.artists})," –"," ",t.jsx(f,{className:"hover:text-[--color-fg-secondary]",to:`/track/${a.track.id}`,children:a.track.title})]})]},`last_listen_${a.time}`))})}),t.jsxs("div",{className:"flex gap-15 mx-auto",children:[t.jsx("button",{className:"default",onClick:l,disabled:e===0,children:"Prev"}),t.jsx("button",{className:"default",onClick:o,disabled:!r.has_next_page,children:"Next"})]})]})})});export{A as clientLoader,S as default};
