import{a as L,y as R,b as T,j as s,d as C,T as i,q as N,U as _,v as d,az as S,r as l}from"./main-D6SjkSWq.js";import{A as E}from"./album-image-CV6DMn1J.js";import{A as F}from"./album-link-C4KAB_gW.js";import{S as P}from"./small-artist-image-gQ68My-z.js";import{A as D}from"./artist-link-C48CzE2I.js";import{A as q}from"./artist-links-B9W9tkZ1.js";import{P as M}from"./play-arrow-filled-J8D1BDP8.js";import{T as $}from"./track-image-DHipIy5D.js";import{T as U}from"./track-link-BvulRuhc.js";import{a as z,b as G,L as K,P as k,G as O}from"./polar-area-chart-gX3MUCce.js";import{F as y}from"./formatted-number-BBL0Q5Lv.js";import{I as Q}from"./Info-DJrDtfBN.js";const v="reports/insights";function Z(e,t){return L({queryKey:[v,e],queryFn:()=>B(v,e),placeholderData:t.isEnabled?R:void 0,enabled:t.isEnabled})}function B(e,t){var a;const r={model:t.model,metrics:(a=t.metrics)==null?void 0:a.join(",")};return r.startDate=t.dateRange.start.toAbsoluteString(),r.endDate=t.dateRange.end.toAbsoluteString(),r.timezone=t.dateRange.start.timeZone,T.get(e,{params:r}).then(o=>o.data)}function u({data:e,isLoading:t,colSpan:r="col-span-6",...a}){const o=(e==null?void 0:e.datasets[0].data)||[];return s.jsxs(z,{...a,contentIsFlex:t,className:C("row-span-11 overflow-y-auto overflow-x-hidden",r),children:[t&&s.jsx(G,{}),o.map(n=>s.jsxs("div",{className:"mb-20 flex items-center justify-between gap-24 text-sm",children:[s.jsxs("div",{className:"flex items-center gap-8",children:[s.jsx(H,{model:n.model,size:"w-42 h-42",className:"flex-shrink-0 rounded"}),s.jsxs("div",{children:[s.jsx("div",{className:"text-sm",children:s.jsx(J,{model:n.model})}),s.jsx("div",{className:"text-xs text-muted",children:s.jsx(V,{model:n.model})})]})]}),s.jsxs("div",{className:"flex flex-shrink-0 items-center gap-4",children:[s.jsx(M,{className:"text-muted",size:"sm"}),s.jsx(i,{message:":count plays",values:{count:s.jsx(y,{value:n.value})}})]})]},n.model.id)),!t&&!o.length?s.jsxs("div",{className:"flex items-center gap-8 text-muted",children:[s.jsx(Q,{size:"sm"}),s.jsx(i,{message:"No plays in selected timeframe."})]}):null]})}function H({model:e,size:t,className:r}){const{pathname:a}=N(),n=a.includes("/admin")?`/admin/${e.model_type}s/${e.id}/insights`:`/backstage/${e.model_type}s/${e.id}/insights`;switch(e.model_type){case"artist":return s.jsx(d,{to:n,children:s.jsx(P,{artist:e,size:t,className:r})});case"album":return s.jsx(d,{to:n,children:s.jsx(E,{album:e,size:t,className:r})});case"track":return s.jsx(d,{to:n,children:s.jsx($,{track:e,size:t,className:r})});case"user":return s.jsx(_,{user:e,size:t,className:r})}}function J({model:e}){switch(e.model_type){case"artist":return s.jsx(D,{artist:e,target:"_blank"});case"album":return s.jsx(F,{album:e,target:"_blank"});case"track":return s.jsx(U,{track:e,target:"_blank"});case"user":return e.id?s.jsx(S,{user:e,target:"_blank"}):s.jsx(l.Fragment,{children:e.name})}}function V({model:e}){switch(e.model_type){case"artist":case"user":return null;case"album":case"track":return s.jsx(q,{artists:e.artists,target:"_blank"})}}function ls(e){const t=e.model,r=e.dateRange;return s.jsxs("div",{className:"chart-grid",children:[s.jsx(c,{metric:"plays",model:t,dateRange:r,children:({data:a})=>s.jsx(K,{colSpan:"col-span-8",title:s.jsx(i,{message:"Plays"}),hideLegend:!0,description:s.jsx(i,{message:":count total plays",values:{count:s.jsx(y,{value:(a==null?void 0:a.report.plays.total)||0})}})})}),s.jsx(c,{metric:"devices",model:t,dateRange:r,children:s.jsx(k,{colSpan:"col-span-4",title:s.jsx(i,{message:"Top devices"})})}),e.showTracks&&s.jsx(c,{metric:"tracks",model:t,dateRange:r,children:s.jsx(u,{title:s.jsx(i,{message:"Most played tracks"})})}),s.jsx(c,{metric:"users",model:t,dateRange:r,children:s.jsx(u,{title:s.jsx(i,{message:"Top listeners"}),colSpan:e.showTracks?"col-span-6":"col-span-12"})}),e.showArtistsAndAlbums&&s.jsxs(l.Fragment,{children:[s.jsx(c,{metric:"artists",model:t,dateRange:r,children:s.jsx(u,{title:s.jsx(i,{message:"Most played artists"})})}),s.jsx(c,{metric:"albums",model:t,dateRange:r,children:s.jsx(u,{title:s.jsx(i,{message:"Most played albums"})})})]}),s.jsx(c,{metric:"locations",model:t,dateRange:r,children:s.jsx(O,{})}),s.jsx(c,{metric:"platforms",model:t,dateRange:r,children:s.jsx(k,{colSpan:"col-span-5",title:s.jsx(i,{message:"Top platforms"})})})]})}function c({children:e,metric:t,model:r,dateRange:a}){var j,h;const[o,n]=l.useState(!1),x=Z({metrics:[t],model:r,dateRange:a},{isEnabled:o}),A=typeof e=="function"?e(x):e,m=l.useRef(null),I=l.useCallback(p=>{var f;if(p){const g=new IntersectionObserver(([w])=>{var b;w.isIntersecting&&(n(!0),(b=m.current)==null||b.disconnect(),m.current=null)},{threshold:.1});m.current=g,g.observe(p)}else m.current&&((f=m.current)==null||f.disconnect())},[]);return l.cloneElement(A,{data:(h=(j=x.data)==null?void 0:j.report)==null?void 0:h[t],isLoading:x.isLoading,contentRef:I})}export{ls as I};
