import{u as B,e as R,b,j as s,I as p,R as z}from"./app.js";import{a as g}from"./table.js";import{u as M}from"./useInterval.js";import{S as N}from"./StableTable.js";import"./vendor.js";function y(n){if(n===0)return"0 B";const t=1024,i=["B","KB","MB","GB","TB"],a=Math.floor(Math.log(n)/Math.log(t));return`${(n/t**a).toFixed(2)} ${i[a]}`}function C(n){const t=Math.round(n);if(t===0)return"0s";const i=Math.floor(t/86400),a=Math.floor(t%86400/3600),r=Math.floor(t%3600/60),u=t%60,l=[];return i>0&&l.push(`${i}d`),a>0&&l.push(`${a}h`),r>0&&l.push(`${r}m`),(u>0||l.length===0)&&l.push(`${u}s`),l.join(" ")}function V(n){switch(n){case-1:return"Never";case 1:return"On Ratio";case 2:return"On Time";case 3:return"Ratio OR Time";case 4:return"Ratio AND Time";default:return"Unknown"}}function q(n,t){if(n===t)return!0;if(n.length!==t.length)return!1;for(let i=0;i<n.length;i+=1){const a=n[i],r=t[i];if(a.category!==r.category||a.instance!==r.instance||a.torrentCount!==r.torrentCount||a.seedingCount!==r.seedingCount||a.totalSize!==r.totalSize||a.avgRatio!==r.avgRatio||a.avgSeedingTime!==r.avgSeedingTime||a.managedBy!==r.managedBy)return!1}return!0}function I({active:n}){const[t,i]=g.useState([]),[a,r]=g.useState(!1),{push:u}=B(),{liveArr:l}=R(),x=g.useRef(!1),m=g.useCallback(async(e=!0)=>{if(!x.current){x.current=!0,e&&r(!0);try{const o=await b();i(f=>q(f,o.categories)?f:o.categories)}catch(o){u(o instanceof Error?o.message:"Failed to load qBit categories","error")}finally{x.current=!1,e&&r(!1)}}},[u]);g.useEffect(()=>{n&&m()},[n,m]),M(()=>{m(!1)},l?1e3:null);const v=g.useCallback(()=>{m()},[m]),d=g.useMemo(()=>{const e=t.reduce((c,h)=>c+h.torrentCount,0),o=t.reduce((c,h)=>c+h.seedingCount,0),f=t.reduce((c,h)=>c+h.totalSize,0),j=t.filter(c=>c.managedBy==="qbit").length,T=t.filter(c=>c.managedBy==="arr").length;return{totalTorrents:e,totalSeeding:o,totalSize:f,qbitCount:j,arrCount:T,categoryCount:t.length}},[t]),S=g.useMemo(()=>[{accessorKey:"category",header:"Category",cell:e=>e.getValue(),size:150},{accessorKey:"managedBy",header:"Managed By",cell:e=>e.getValue()==="qbit"?s.jsx("span",{className:"badge badge-qbit",children:"qBit"}):s.jsx("span",{className:"badge badge-arr",children:"Arr"}),size:120},{accessorKey:"instance",header:"Instance",cell:e=>e.getValue(),size:150},{accessorKey:"torrentCount",header:"Torrents",cell:e=>e.getValue().toLocaleString(),size:100},{accessorKey:"seedingCount",header:"Seeding",cell:e=>e.getValue().toLocaleString(),size:100},{accessorKey:"totalSize",header:"Total Size",cell:e=>y(e.getValue()),size:120},{accessorKey:"avgRatio",header:"Avg Ratio",cell:e=>e.getValue().toFixed(2),size:100},{accessorKey:"avgSeedingTime",header:"Avg Seed Time",cell:e=>C(Math.round(e.getValue())),size:140},{id:"maxRatio",header:"Max Ratio",accessorFn:e=>e.seedingConfig.maxRatio,cell:e=>{const o=e.getValue();return o===-1?"Disabled":o.toFixed(2)},size:100},{id:"maxTime",header:"Max Time",accessorFn:e=>e.seedingConfig.maxTime,cell:e=>{const o=e.getValue();return o===-1?"Disabled":C(o)},size:120},{id:"removeMode",header:"Remove Mode",accessorFn:e=>e.seedingConfig.removeMode,cell:e=>V(e.getValue()),size:150}],[]);return s.jsxs("div",{className:"stack animate-fade-in",children:[s.jsxs("div",{className:"row",style:{justifyContent:"space-between"},children:[s.jsxs("div",{className:"hint",children:["Category overview across all instances",s.jsx("br",{}),s.jsx("strong",{children:"Categories:"})," ",d.categoryCount," •"," ",s.jsx("strong",{children:"qBit-managed:"})," ",d.qbitCount," •"," ",s.jsx("strong",{children:"Arr-managed:"})," ",d.arrCount," •"," ",s.jsx("strong",{children:"Total Torrents:"})," ",d.totalTorrents.toLocaleString()," •"," ",s.jsx("strong",{children:"Seeding:"})," ",d.totalSeeding.toLocaleString()," •"," ",s.jsx("strong",{children:"Total Size:"})," ",y(d.totalSize)]}),s.jsxs("button",{className:"btn ghost",onClick:v,disabled:a,children:[a&&s.jsx("span",{className:"spinner"}),s.jsx(p,{src:z}),a?"Refreshing...":"Refresh"]})]}),a&&t.length===0?s.jsxs("div",{className:"loading",children:[s.jsx("span",{className:"spinner"})," Loading categories…"]}):t.length===0?s.jsx("div",{className:"hint",children:"No categories found. Configure ManagedCategories in your qBit config sections or add Arr instances to see categories."}):s.jsx(N,{data:t,columns:S,getRowKey:e=>`${e.instance}-${e.category}-${e.managedBy}`})]})}export{I as QbitCategoriesView};
//# sourceMappingURL=QbitCategoriesView.js.map
