| 12 |
- import{i as e}from"./chunk-aKtaBQYM.js";import{C as t,O as n,T as r,a as i,n as a,r as o,t as s,v as c,w as l}from"./client-D5sDHQfN.js";import{n as u}from"./QueryClientProvider-RTEea2Mk.js";import{t as d}from"./useQuery-CGtzNsic.js";import{t as f}from"./useMutation-Kl2cmi_r.js";import{t as p}from"./useQueries-Ryq2rPkY.js";import{i as m,n as h,r as g}from"./haSensorDisplay-B1mKzoOE.js";import{t as _}from"./arrow-down-Co6Nksgq.js";import{t as v}from"./arrow-up-down-9uZQu-2C.js";import{t as y}from"./arrow-up-C73fgG6c.js";import{i as b,n as x,r as S,t as C}from"./ReferenceLine-CevEzIBV.js";import{t as w}from"./check-CKa8_QSu.js";import{a as T,i as E,n as D,t as O}from"./info-hj_2aNUa.js";import{n as k,t as A}from"./chevrons-right-_Nd5b9N0.js";import{n as ee,r as j}from"./ToastContext-DKTQgyG5.js";import{t as M}from"./columns-2-DvVrpCGJ.js";import{t as N}from"./copy-Blc8TIO6.js";import{t as te}from"./layout-grid-B8dnvfdS.js";import{t as P}from"./link-2-Z92U1tkD.js";import{t as F}from"./loader-circle-D_tGybHN.js";import{t as ne}from"./lock-BRN_ISqr.js";import{_ as I,c as L,g as re,h as R,s as ie,v as ae}from"./locationSensorDefaults-BdJNdFf-.js";import{t as oe}from"./package-BinzsIMO.js";import{t as se}from"./search-CGgI8arA.js";import{t as z}from"./sparkles-rrcdCPcd.js";import{L as B,Rn as ce,Xt as le,c as ue,p as de,s as fe}from"./CartesianChart-DXORvQRl.js";import{t as V}from"./triangle-alert-B24VGTa4.js";import{t as pe}from"./unlink-Cr0KGseH.js";import{t as me}from"./x-CUdw628F.js";import{n as he}from"./AuthContext-F8QHEqr7.js";import{t as H}from"./Button-D71ID444.js";import{r as ge,v as U,y as _e}from"./date-G9KUauqz.js";import{n as ve}from"./filamentSwatchHelpers-D3oXQSGk.js";import{$n as ye,An as be,Bn as xe,Dt as Se,G as Ce,Jt as W,Kn as we,Kt as Te,Ln as Ee,Mn as De,Nn as Oe,Pt as ke,Qt as Ae,Rn as je,Rt as Me,St as Ne,T as Pe,Ut as Fe,Vn as Ie,X as Le,_n as Re,_t as ze,a as Be,cn as Ve,en as He,gt as Ue,ht as We,i as G,kt as Ge,ln as Ke,o as qe,on as Je,qt as Ye,r as K,rn as Xe,s as Ze,tn as Qe,yt as $e,zt as et}from"./index-nsyp3RII.js";import{i as tt,r as nt}from"./slicerPrinterMatch-CUdoNag2.js";import{t as rt}from"./ColumnConfigModal-rg7yoh0g.js";import{n as it,t as at}from"./AreaChart-DGOYHKEZ.js";import{a as ot,i as st,n as ct,o as lt,r as q,s as ut,t as dt}from"./AdditionalSection-CmDzqEJx.js";import{_ as ft,a as J,b as pt,c as mt,d as ht,f as gt,g as _t,i as vt,l as yt,m as bt,n as xt,o as St,r as Ct,s as wt,t as Tt,u as Et,v as Dt}from"./utils-BIttisLy.js";import{n as Ot,r as Y,t as kt}from"./nozzleFlow-vrXwck9b.js";import{t as At}from"./AssignToAmsModal-CYPg8ccC.js";import{t as jt}from"./spoolCost-CrGi2ZL6.js";import{t as Mt}from"./inventorySearch-BsJCOUn0.js";var Nt=t(`beaker`,[[`path`,{d:`M4.5 3h15`,key:`c7n0jr`}],[`path`,{d:`M6 3v16a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V3`,key:`m1uhx7`}],[`path`,{d:`M6 14h12`,key:`4cwo0f`}]]),Pt=t(`chart-no-axes-column`,[[`path`,{d:`M5 21v-6`,key:`1hz6c0`}],[`path`,{d:`M12 21V3`,key:`1lcnhd`}],[`path`,{d:`M19 21V9`,key:`unv183`}]]),Ft=t(`credit-card`,[[`rect`,{width:`20`,height:`14`,x:`2`,y:`5`,rx:`2`,key:`ynyp8z`}],[`line`,{x1:`2`,x2:`22`,y1:`10`,y2:`10`,key:`1b3vmo`}]]),It=t(`group`,[[`path`,{d:`M3 7V5c0-1.1.9-2 2-2h2`,key:`adw53z`}],[`path`,{d:`M17 3h2c1.1 0 2 .9 2 2v2`,key:`an4l38`}],[`path`,{d:`M21 17v2c0 1.1-.9 2-2 2h-2`,key:`144t0e`}],[`path`,{d:`M7 21H5c-1.1 0-2-.9-2-2v-2`,key:`rtnfgi`}],[`rect`,{width:`7`,height:`5`,x:`7`,y:`7`,rx:`1`,key:`1eyiv7`}],[`rect`,{width:`7`,height:`5`,x:`10`,y:`12`,rx:`1`,key:`1qlmkx`}]]),Lt=t(`package-check`,[[`path`,{d:`m16 16 2 2 4-4`,key:`gfu2re`}],[`path`,{d:`M21 10V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l2-1.14`,key:`e7tb2h`}],[`path`,{d:`m7.5 4.27 9 5.15`,key:`1c824w`}],[`polyline`,{points:`3.29 7 12 12 20.71 7`,key:`ousv84`}],[`line`,{x1:`12`,x2:`12`,y1:`22`,y2:`12`,key:`a4e8g8`}]]),Rt=t(`store`,[[`path`,{d:`M15 21v-5a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v5`,key:`slp6dd`}],[`path`,{d:`M17.774 10.31a1.12 1.12 0 0 0-1.549 0 2.5 2.5 0 0 1-3.451 0 1.12 1.12 0 0 0-1.548 0 2.5 2.5 0 0 1-3.452 0 1.12 1.12 0 0 0-1.549 0 2.5 2.5 0 0 1-3.77-3.248l2.889-4.184A2 2 0 0 1 7 2h10a2 2 0 0 1 1.653.873l2.895 4.192a2.5 2.5 0 0 1-3.774 3.244`,key:`o0xfot`}],[`path`,{d:`M4 10.95V19a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8.05`,key:`wn3emo`}]]),zt=t(`table-properties`,[[`path`,{d:`M15 3v18`,key:`14nvp0`}],[`rect`,{width:`18`,height:`18`,x:`3`,y:`3`,rx:`2`,key:`afitv7`}],[`path`,{d:`M21 9H3`,key:`1338ky`}],[`path`,{d:`M21 15H3`,key:`9uk58r`}]]),X=e(n(),1),Z=l(),Bt=1.65,Vt=[`#1DB954`,`#3B82F6`,`#F59E0B`,`#EF4444`,`#8B5CF6`];function Ht(e,t,n,r){return`${e}||${t??``}||${n??``}||${r??``}`}function Ut(e,t){let n=new Date(e);return n.setDate(n.getDate()+Math.round(t)),n}function Wt(e){return e.toLocaleDateString(void 0,{month:`short`,day:`numeric`,year:`numeric`})}function Gt(e){return e.toLocaleDateString(void 0,{month:`short`,day:`numeric`})}function Kt(e){if(e.length<2)return null;let t=new Map;for(let n of e){let e=n.created_at.slice(0,10);t.set(e,(t.get(e)??0)+n.weight_used)}if(t.size<2)return null;let n=[...t.entries()].map(([e,t])=>({ms:new Date(e).getTime(),totalG:t})).sort((e,t)=>e.ms-t.ms),r=Date.now(),i=Math.LN2/30,a=[];for(let e=1;e<n.length;e++){let t=Math.max((n[e].ms-n[e-1].ms)/864e5,1),o=(r-n[e].ms)/864e5,s=n[e].totalG/t,c=Math.exp(-i*o);a.push({rate:s,weight:c})}let o=a.reduce((e,t)=>e+t.weight,0);if(o===0)return null;let s=a.reduce((e,t)=>e+t.rate*t.weight,0)/o,c=a.reduce((e,t)=>e+t.weight*(t.rate-s)**2,0)/o;return{rate:s,stdDev:Math.sqrt(c)}}function qt(e){let t=e.reduce((e,t)=>e+Math.max(0,t.weight_used-(t.weight_used_baseline??0)),0);if(t===0)return null;let n=Date.now(),r=(n-e.reduce((e,t)=>{let n=new Date(t.created_at).getTime();return n<e?n:e},n))/864e5;return r<1?null:t/r}function Jt(e,t=60){if(e.dailyRateG===null)return[];let n=e.dailyRateG,r=[];for(let i=0;i<=t;i++){let t=Math.max(0,e.totalRemainingG-n*i);if(r.push({day:i,label:Gt(Ut(new Date,i)),stock:Math.round(t),rop:Math.round(e.reorderPointG)}),t===0)break}return r}function Yt({spools:e}){let t=u(),{showToast:n}=ee(),{t:a}=r(),{hasPermission:o,hasAnyPermission:s}=he(),c=o(`inventory:forecast_read`),l=s(`inventory:forecast_write`,`inventory:update`),[f,p]=(0,X.useState)(!1),[m,h]=(0,X.useState)(`material`),[g,_]=(0,X.useState)(`asc`),[v,y]=(0,X.useState)(``),[b,x]=(0,X.useState)(``),[S,C]=(0,X.useState)(null),[w,D]=(0,X.useState)(!1),[O,k]=(0,X.useState)(!1),[A,j]=(0,X.useState)(30),{data:M}=Ce({enabled:c}),{data:N=[]}=d({queryKey:[`sku-settings`],queryFn:i.getSkuSettings,staleTime:6e4,enabled:c}),{data:te=[]}=d({queryKey:[`all-usage-history-forecast`],queryFn:()=>i.getAllUsageHistory(5e3),staleTime:6e4,enabled:c}),{data:P=[]}=d({queryKey:[`shopping-list`],queryFn:i.getShoppingList,staleTime:3e4,enabled:c}),F=M?.forecast_global_lead_time_days??0,I=(0,X.useMemo)(()=>{let e=new Map;for(let t of N)e.set(Ht(t.material,t.subtype,t.brand,t.color_name),t);return e},[N]),L=(0,X.useMemo)(()=>{let e=new Map;for(let t of te){if(t.spool_id===null)continue;let n=e.get(t.spool_id)??[];n.push(t),e.set(t.spool_id,n)}return e},[te]),re=(0,X.useMemo)(()=>{let t=new Map;for(let n of e){if(n.archived_at)continue;let e=Ht(n.material,n.subtype,n.brand,n.color_name),r=t.get(e)??{key:e,material:n.material,subtype:n.subtype,brand:n.brand,colorName:n.color_name,spools:[]};r.spools.push(n),t.set(e,r)}return[...t.values()]},[e]),R=(0,X.useMemo)(()=>{let e=new Date;return e.setHours(0,0,0,0),re.map(t=>{let n=I.get(t.key)??(t.colorName===null?null:I.get(Ht(t.material,t.subtype,t.brand,null))??null),r=n?.lead_time_days??0,i=Math.max(F,r),a=n?.safety_margin_value??14,o=n?.safety_margin_unit??`days`,s=t.spools.reduce((e,t)=>e+Math.max(0,t.label_weight-t.weight_used),0),c=t.spools.reduce((e,t)=>e+t.label_weight,0),l=t.spools.reduce((e,t)=>e+Math.max(0,t.weight_used-(t.weight_used_baseline??0)),0),u=[];for(let e of t.spools)(e.weight_used_baseline??0)===0&&u.push(...L.get(e.id)??[]);let d=null,f=null,p=`none`,m=Kt(u);if(m!==null)d=m.rate,f=m.stdDev,p=`history`;else{let e=qt(t.spools);e!==null&&(d=e,p=`delta`)}let h=Bt*(f??(d===null?0:d*.2))*Math.sqrt(i)+(o===`g`?a:d===null?a*5:d*a),g=d===null?0:d*i+h,_=d&&d>0?Math.floor(s/d):null,v=_===null?null:Ut(e,_),y=d&&d>0?Math.floor((s-g)/d):null,b=y===null?null:Ut(e,Math.max(0,y)),x=_!==null&&i>0&&_<=i,S=!x&&y!==null&&y<=0;return{group:t,settings:n,totalRemainingG:s,totalLabelG:c,totalSpools:t.spools.length,totalUsedG:l,dailyRateG:d,dailyRateStdDev:f,rateTier:p,effectiveLeadTimeDays:i,safetyStockG:h,reorderPointG:g,daysRemaining:_,daysUntilROP:y,projectedEmptyDate:v,reorderTriggerDate:b,reorderAlert:S,stockBreakAlert:x}})},[re,I,L,F]),ie=(0,X.useMemo)(()=>[...new Set(re.map(e=>e.material))].sort(),[re]),ae=(0,X.useMemo)(()=>[...new Set(re.map(e=>e.brand).filter(Boolean))].sort(),[re]),oe=(0,X.useMemo)(()=>{let e=[...R];return v&&(e=e.filter(e=>e.group.material===v)),b&&(e=e.filter(e=>e.group.brand===b)),e.sort((e,t)=>{let n=0,r=0;switch(m){case`material`:n=[e.group.material,e.group.subtype??``,e.group.brand??``].join(` `).toLowerCase(),r=[t.group.material,t.group.subtype??``,t.group.brand??``].join(` `).toLowerCase();break;case`used`:n=e.totalUsedG,r=t.totalUsedG;break;case`days_left`:n=e.daysRemaining??999999,r=t.daysRemaining??999999;break;case`stock`:n=e.totalRemainingG,r=t.totalRemainingG;break}let i=n<r?-1:+(n>r);return g===`asc`?i:-i}),e},[R,m,g,v,b]),se=(0,X.useMemo)(()=>R.filter(e=>!e.settings?.alerts_snoozed&&(e.stockBreakAlert||e.reorderAlert)),[R]),z=(0,X.useMemo)(()=>[...R].filter(e=>e.dailyRateG!==null).sort((e,t)=>t.totalUsedG-e.totalUsedG).slice(0,5),[R]);if(!c)return(0,Z.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 text-bambu-gray gap-3`,children:[(0,Z.jsx)(ne,{className:`w-8 h-8 opacity-40`}),(0,Z.jsx)(`p`,{className:`text-sm`,children:a(`forecast.noReadAccess`)})]});function B(e){m===e?_(e=>e===`asc`?`desc`:`asc`):(h(e),_(e===`days_left`?`asc`:`desc`))}let le=P.length>0?P.length:null;return(0,Z.jsxs)(`div`,{className:`space-y-5`,children:[(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-3`,children:[se.length>0&&(0,Z.jsxs)(`button`,{onClick:()=>p(e=>!e),className:`flex items-center gap-2 px-3 py-1.5 rounded-lg border text-sm font-medium transition-colors ${se.some(e=>e.stockBreakAlert)?`bg-red-100 dark:bg-red-500/15 border-red-300 dark:border-red-500/30 text-red-700 dark:text-red-300 hover:bg-red-200 dark:hover:bg-red-500/25`:`bg-yellow-100 dark:bg-yellow-500/15 border-yellow-300 dark:border-yellow-500/30 text-yellow-700 dark:text-yellow-300 hover:bg-yellow-200 dark:hover:bg-yellow-500/25`}`,children:[(0,Z.jsx)(V,{className:`w-4 h-4`}),a(`forecast.alertCount`,{count:se.length}),f?(0,Z.jsx)(E,{className:`w-3.5 h-3.5`}):(0,Z.jsx)(T,{className:`w-3.5 h-3.5`})]}),l&&(0,Z.jsx)(en,{value:F,onSave:e=>{i.updateSettings({forecast_global_lead_time_days:e}).then(()=>{t.invalidateQueries({queryKey:[`settings`]}),n(a(`forecast.globalLeadTimeSaved`),`success`)})}}),(0,Z.jsxs)(`select`,{value:v,onChange:e=>y(e.target.value),className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${v?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:a(`inventory.material`)}),ie.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(0,Z.jsxs)(`select`,{value:b,onChange:e=>x(e.target.value),className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${b?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:a(`inventory.brand`)}),ae.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(0,Z.jsxs)(`button`,{onClick:()=>D(e=>!e),className:`relative flex items-center gap-2 px-3 py-1.5 rounded-lg border border-bambu-dark-tertiary text-bambu-gray hover:bg-bambu-dark-tertiary text-sm transition-colors ml-auto`,children:[(0,Z.jsx)(W,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:a(`forecast.shoppingList`)}),le&&(0,Z.jsx)(`span`,{className:`absolute -top-1.5 -right-1.5 w-4 h-4 rounded-full bg-bambu-green text-white text-[10px] font-bold flex items-center justify-center`,children:le})]})]}),f&&se.length>0&&(0,Z.jsx)(`div`,{className:`space-y-2`,children:se.map(e=>(0,Z.jsx)(Zt,{forecast:e,onCart:()=>C(e)},e.group.key))}),w&&(0,Z.jsx)(on,{items:P,forecasts:R,globalLeadTime:F,canWrite:l,onClose:()=>D(!1),onRemove:e=>{i.removeFromShoppingList(e).then(()=>t.invalidateQueries({queryKey:[`shopping-list`]})).catch(()=>n(a(`forecast.failedSaveSettings`),`error`))},onClear:()=>k(!0)}),O&&(0,Z.jsx)(Se,{title:a(`forecast.clearAllConfirmTitle`),message:a(`forecast.clearAllConfirmMessage`,{count:P.length}),confirmText:a(`forecast.clearAll`),variant:`danger`,onConfirm:()=>{k(!1),i.clearShoppingList().then(()=>t.invalidateQueries({queryKey:[`shopping-list`]})).catch(()=>n(a(`forecast.failedSaveSettings`),`error`))},onCancel:()=>k(!1)}),z.length>0&&(0,Z.jsx)($t,{forecasts:z,days:A,onDaysChange:j}),R.length===0?(0,Z.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 text-bambu-gray`,children:[(0,Z.jsx)(ce,{className:`w-10 h-10 mb-3 opacity-40`}),(0,Z.jsx)(`p`,{className:`text-sm`,children:a(`forecast.noSpools`)})]}):(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsx)(`th`,{className:`w-8 px-4 py-3`}),(0,Z.jsx)(Xt,{col:`material`,active:m,dir:g,onSort:B,children:a(`forecast.sku`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:a(`locations.spools`)}),(0,Z.jsx)(Xt,{col:`stock`,active:m,dir:g,onSort:B,children:a(`forecast.stock`)}),(0,Z.jsx)(Xt,{col:`used`,active:m,dir:g,onSort:B,children:a(`forecast.dailyRate`)}),(0,Z.jsx)(Xt,{col:`days_left`,active:m,dir:g,onSort:B,children:a(`forecast.daysLeft`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:a(`forecast.emptyBy`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:a(`forecast.reorderBy`)}),(0,Z.jsx)(`th`,{className:`w-24 px-4 py-3`})]})}),(0,Z.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:oe.map(e=>(0,Z.jsx)(tn,{forecast:e,globalLeadTime:F,canWrite:l,onSaved:()=>t.invalidateQueries({queryKey:[`sku-settings`]}),onCart:()=>C(e),showToast:n},e.group.key))})]})}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-4 px-4 py-3 text-xs text-bambu-gray border-t border-bambu-dark-tertiary bg-bambu-dark-tertiary/20`,children:[(0,Z.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-bambu-green inline-block`}),a(`forecast.trendLegend`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-blue-400 inline-block`}),a(`forecast.estimatedLegend`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-bambu-gray/40 inline-block`}),a(`forecast.noDataLegend`)]})]})]}),S&&(0,Z.jsx)(cn,{forecast:S,onClose:()=>C(null),onAdd:e=>{i.addToShoppingList(e).then(()=>{t.invalidateQueries({queryKey:[`shopping-list`]}),n(a(`forecast.addedToCart`),`success`),C(null),D(!0)}).catch(()=>n(a(`forecast.failedAddItem`),`error`))}})]})}function Xt({col:e,active:t,dir:n,onSort:r,children:i}){return(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide cursor-pointer select-none hover:text-white transition-colors`,onClick:()=>r(e),children:(0,Z.jsxs)(`span`,{className:`inline-flex items-center`,children:[i,t===e?n===`asc`?(0,Z.jsx)(y,{className:`w-3 h-3 ml-1 text-bambu-green`}):(0,Z.jsx)(_,{className:`w-3 h-3 ml-1 text-bambu-green`}):(0,Z.jsx)(v,{className:`w-3 h-3 ml-1 opacity-40`})]})})}function Zt({forecast:e,onCart:t}){let{t:n}=r(),i=[e.group.brand,e.group.material,e.group.subtype,e.group.colorName].filter(Boolean).join(` `),a=e.stockBreakAlert;return(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 px-4 py-3 rounded-lg border text-sm ${a?`bg-red-50 dark:bg-red-500/10 border-red-300 dark:border-red-500/30 text-red-700 dark:text-red-300`:`bg-yellow-50 dark:bg-yellow-500/10 border-yellow-300 dark:border-yellow-500/30 text-yellow-700 dark:text-yellow-300`}`,children:[(0,Z.jsx)(V,{className:`w-4 h-4 flex-shrink-0`}),(0,Z.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Z.jsx)(`span`,{className:`font-medium`,children:i}),a?(0,Z.jsxs)(`span`,{className:`ml-2 text-xs opacity-80`,children:[n(`forecast.stockBreakRisk`),` — `,n(`forecast.stockBreakDetail`,{days:e.daysRemaining,lt:e.effectiveLeadTimeDays})]}):(0,Z.jsxs)(`span`,{className:`ml-2 text-xs opacity-80`,children:[n(`forecast.reorderNow`),` — `,n(`forecast.reorderTriggerPassed`,{date:e.reorderTriggerDate?Wt(e.reorderTriggerDate):`—`})]})]}),(0,Z.jsxs)(`button`,{onClick:t,className:`flex items-center gap-1.5 px-2.5 py-1 rounded border border-current text-xs opacity-70 hover:opacity-100 transition-opacity`,children:[(0,Z.jsx)(W,{className:`w-3 h-3`}),` `,n(`forecast.order`)]})]})}var Qt=[{label:`1W`,value:7},{label:`1M`,value:30},{label:`6M`,value:180}];function $t({forecasts:e,days:t,onDaysChange:n}){let{t:i}=r(),a=Array.from({length:t+1},(e,t)=>t),o=e.map((e,n)=>({key:e.group.key,label:[e.group.brand,e.group.material,e.group.subtype,e.group.colorName].filter(Boolean).join(` `),color:Vt[n%Vt.length],rop:e.reorderPointG,points:Jt(e,t)})),s=a.map(e=>{let t={day:e,label:Gt(Ut(new Date,e))};for(let n of o){let r=n.points.find(t=>t.day===e);t[n.key]=r?.stock??0}return t}),c=(()=>{for(let e=t;e>=0;e--)if(o.some(t=>s[e]?.[t.key]>0))return e;return t})(),l=s.slice(0,c+1),u=o.filter(e=>e.rop>0);return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-4`,children:[(0,Z.jsx)(ce,{className:`w-4 h-4 text-bambu-green`}),(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-white`,children:i(`forecast.chartTitle`)}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray ml-1 hidden sm:inline`,children:i(`forecast.dashedLinesROP`)}),(0,Z.jsx)(`div`,{className:`ml-auto flex items-center bg-bambu-dark-tertiary rounded-lg p-0.5`,children:Qt.map(e=>(0,Z.jsx)(`button`,{onClick:()=>n(e.value),className:`px-2.5 py-1 text-xs font-medium rounded-md transition-colors ${t===e.value?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:e.label},e.value))})]}),(0,Z.jsx)(le,{width:`100%`,height:220,children:(0,Z.jsxs)(at,{data:l,margin:{top:4,right:8,bottom:0,left:0},children:[(0,Z.jsx)(`defs`,{children:o.map(e=>(0,Z.jsxs)(`linearGradient`,{id:`grad-${e.key}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Z.jsx)(`stop`,{offset:`5%`,stopColor:e.color,stopOpacity:.25}),(0,Z.jsx)(`stop`,{offset:`95%`,stopColor:e.color,stopOpacity:.02})]},e.key))}),(0,Z.jsx)(de,{strokeDasharray:`3 3`,stroke:`#374151`,strokeOpacity:.5}),(0,Z.jsx)(ue,{dataKey:`label`,tick:{fill:`#6B7280`,fontSize:10},interval:Math.max(0,Math.ceil(c/8)-1),axisLine:!1,tickLine:!1}),(0,Z.jsx)(fe,{tick:{fill:`#6B7280`,fontSize:10},axisLine:!1,tickLine:!1,tickFormatter:e=>e>=1e3?`${(e/1e3).toFixed(1)}kg`:`${e}g`,width:48}),(0,Z.jsx)(B,{content:({label:e,payload:t})=>t?.length?(0,Z.jsxs)(`div`,{style:{background:`#1a1a2e`,border:`1px solid #374151`,borderRadius:8,fontSize:12,padding:`8px 12px`},children:[(0,Z.jsx)(`div`,{style:{color:`#9CA3AF`,marginBottom:6},children:e}),t.map(e=>{let t=o.find(t=>t.key===String(e.dataKey));return typeof e.value==`number`?(0,Z.jsxs)(`div`,{style:{display:`flex`,alignItems:`center`,gap:6,color:`#E5E7EB`,marginBottom:2},children:[(0,Z.jsx)(`span`,{style:{color:t?.color??`#9CA3AF`,fontSize:10},children:`●`}),(0,Z.jsx)(`span`,{children:t?.label??String(e.dataKey)}),(0,Z.jsxs)(`span`,{style:{color:`#9CA3AF`,marginLeft:4},children:[e.value,`g`]})]},String(e.dataKey)):null})]}):null}),(0,Z.jsx)(x,{formatter:e=>(0,Z.jsx)(`span`,{style:{color:`#9CA3AF`,fontSize:11},children:o.find(t=>t.key===e)?.label??e})}),o.map(e=>(0,Z.jsx)(it,{type:`monotone`,dataKey:e.key,stroke:e.color,strokeWidth:2,fill:`url(#grad-${e.key})`,dot:!1,activeDot:{r:3}},e.key)),u.map(e=>(0,Z.jsx)(C,{y:e.rop,stroke:e.color,strokeDasharray:`4 3`,strokeOpacity:.6},`rop-${e.key}`))]})})]})}function en({value:e,onSave:t}){let{t:n}=r(),[i,a]=(0,X.useState)(!1),[o,s]=(0,X.useState)(String(e));function c(){let e=parseInt(o,10);isNaN(e)||e<0||(t(e),a(!1))}return(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 px-3 py-1.5 bg-bambu-dark-tertiary/40 rounded-lg border border-bambu-dark-tertiary text-xs text-bambu-gray`,children:[(0,Z.jsx)(O,{className:`w-3.5 h-3.5 flex-shrink-0`,"aria-label":n(`forecast.globalLeadTimeHint`)}),(0,Z.jsxs)(`span`,{className:`hidden sm:inline`,children:[n(`forecast.globalLeadTime`),`:`]}),i?(0,Z.jsxs)(`form`,{className:`flex items-center gap-1.5`,onSubmit:e=>{e.preventDefault(),c()},children:[(0,Z.jsx)(`input`,{type:`number`,min:0,max:365,value:o,onChange:e=>s(e.target.value),className:`w-14 px-1.5 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`,autoFocus:!0}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:`d`}),(0,Z.jsx)(`button`,{type:`submit`,className:`px-2 py-0.5 bg-bambu-green text-white text-xs rounded hover:bg-bambu-green/80`,children:n(`forecast.save`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>a(!1),className:`text-xs text-bambu-gray hover:text-white`,children:`✕`})]}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsxs)(`span`,{className:`font-semibold text-white`,children:[e,`d`]}),(0,Z.jsx)(`button`,{onClick:()=>{s(String(e)),a(!0)},className:`p-0.5 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(I,{className:`w-3 h-3`})})]})]})}function tn({forecast:e,globalLeadTime:t,canWrite:n,onSaved:a,onCart:o,showToast:s}){let{t:c}=r(),[l,u]=(0,X.useState)(!1),[d,p]=(0,X.useState)(!1),[m,h]=(0,X.useState)(!1),[g,_]=(0,X.useState)(String(e.settings?.lead_time_days??0)),[v,y]=(0,X.useState)(String(e.settings?.safety_margin_value??14)),[x,S]=(0,X.useState)(e.settings?.safety_margin_unit??`days`);(0,X.useEffect)(()=>{d||_(String(e.settings?.lead_time_days??0))},[e.settings?.lead_time_days,d]),(0,X.useEffect)(()=>{m||(y(String(e.settings?.safety_margin_value??14)),S(e.settings?.safety_margin_unit??`days`))},[e.settings?.safety_margin_value,e.settings?.safety_margin_unit,m]);let C=f({mutationFn:i.upsertSkuSettings,onSuccess:()=>{a(),s(c(`forecast.settingsSaved`),`success`)},onError:()=>s(c(`forecast.failedSaveSettings`),`error`)}),D=e.settings?.alerts_snoozed??!1,O=[e.group.brand,e.group.material,e.group.subtype,e.group.colorName].filter(Boolean).join(` `),k=e.group.spools[0]?.rgba?$e(e.group.spools[0].rgba):{backgroundColor:`#4B5563`},A=e.totalLabelG>0?Math.round(e.totalRemainingG/e.totalLabelG*100):0,ee=D||e.daysRemaining===null?`text-bambu-gray`:e.stockBreakAlert?`text-red-700 dark:text-red-400`:e.reorderAlert||e.daysRemaining<30?`text-yellow-700 dark:text-yellow-400`:`text-green-700 dark:text-green-400`;function j(t,n,r,i=D){C.mutate({material:e.group.material,subtype:e.group.subtype,brand:e.group.brand,color_name:e.group.colorName,lead_time_days:t,safety_margin_value:n,safety_margin_unit:r,alerts_snoozed:i})}function M(t){t.stopPropagation(),j(e.settings?.lead_time_days??0,e.settings?.safety_margin_value??14,e.settings?.safety_margin_unit??`days`,!D)}let N=e.rateTier===`history`?(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-bambu-green/15 text-bambu-green`,children:[(0,Z.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-bambu-green`}),c(`forecast.trend`)]}):e.rateTier===`delta`?(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-blue-100 dark:bg-blue-400/15 text-blue-700 dark:text-blue-400`,children:[(0,Z.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-blue-400`}),c(`forecast.estimated`)]}):(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-bambu-dark-tertiary text-bambu-gray/60`,children:[(0,Z.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-bambu-gray/40`}),c(`forecast.noData`)]});return(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/50 cursor-pointer hover:bg-bambu-dark-tertiary/30 transition-colors ${D?``:e.stockBreakAlert?`bg-red-500/5`:e.reorderAlert?`bg-yellow-500/5`:``} ${D?`opacity-50`:``}`,onClick:()=>u(e=>!e),children:[(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`block w-5 h-5 rounded-full border border-black/20`,style:k})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:O})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.totalSpools})}),(0,Z.jsx)(`td`,{className:`px-4 py-3 min-w-[140px]`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${A>50?`bg-bambu-green`:A>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(A,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(e.totalRemainingG),`g`]})]})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5 flex-wrap`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:e.dailyRateG===null?`—`:`${e.dailyRateG.toFixed(1)}g/d`}),N]})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm font-semibold ${ee}`,children:e.daysRemaining===null?(0,Z.jsx)(`span`,{className:`text-bambu-gray font-normal`,children:`—`}):`${e.daysRemaining}d`})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.projectedEmptyDate?Wt(e.projectedEmptyDate):`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm ${!D&&e.reorderAlert?`text-yellow-700 dark:text-yellow-400`:`text-bambu-gray`}`,children:e.reorderTriggerDate?Wt(e.reorderTriggerDate):`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,onClick:e=>e.stopPropagation(),children:(0,Z.jsxs)(`div`,{className:`flex items-center justify-end gap-1`,children:[n&&(0,Z.jsx)(`button`,{onClick:o,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:c(`forecast.addToCart`),children:(0,Z.jsx)(W,{className:`w-4 h-4`})}),!D&&(e.stockBreakAlert?(0,Z.jsx)(V,{className:`w-4 h-4 text-red-600 dark:text-red-400`,"aria-label":c(`forecast.stockBreakRisk`)}):e.reorderAlert?(0,Z.jsx)(V,{className:`w-4 h-4 text-yellow-600 dark:text-yellow-400`,"aria-label":c(`forecast.reorderNow`)}):e.daysRemaining===null?null:(0,Z.jsx)(w,{className:`w-4 h-4 text-bambu-green/50`})),n&&(0,Z.jsx)(`button`,{onClick:M,className:`p-1 rounded transition-colors ${D?`text-amber-600/80 dark:text-amber-400/80 hover:text-amber-700 dark:hover:text-amber-300`:`text-slate-400 hover:text-white`}`,title:c(D?`forecast.alertsEnabled`:`forecast.alertsSnoozed`),children:(0,Z.jsx)(b,{className:`w-3.5 h-3.5`})}),(0,Z.jsx)(`button`,{onClick:e=>{e.stopPropagation(),u(e=>!e)},className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,children:l?(0,Z.jsx)(E,{className:`w-4 h-4`}):(0,Z.jsx)(T,{className:`w-4 h-4`})})]})})]}),l&&(0,Z.jsx)(`tr`,{className:`bg-bambu-dark-tertiary/10`,children:(0,Z.jsx)(`td`,{colSpan:9,className:`px-6 py-4`,children:(0,Z.jsxs)(`div`,{className:`space-y-3`,children:[(0,Z.jsxs)(`div`,{className:`grid gap-2 ${n?`grid-cols-2 sm:grid-cols-4`:`grid-cols-2`}`,children:[(0,Z.jsx)(nn,{label:c(`forecast.effectiveLeadTime`),value:`${e.effectiveLeadTimeDays}d`,hint:c(`forecast.effectiveLeadTimeHint`,{global:t,sku:e.settings?.lead_time_days??0})}),(0,Z.jsx)(nn,{label:c(`forecast.reorderPoint`),value:`${Math.round(e.reorderPointG)}g`,hint:c(`forecast.reorderPointHint`)}),n&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(rn,{label:c(`forecast.skuLeadTimeOverride`),hint:c(`forecast.skuLeadTimeHint`),unit:c(`forecast.leadTime`),editing:d,value:e.settings?.lead_time_days??0,inputValue:g,onInputChange:_,onEdit:()=>{_(String(e.settings?.lead_time_days??0)),p(!0)},onSave:()=>{let t=parseInt(g,10);!isNaN(t)&&t>=0&&(j(t,e.settings?.safety_margin_value??14,x),p(!1))},onCancel:()=>p(!1),isPending:C.isPending,saveLabel:c(`forecast.save`),cancelLabel:c(`forecast.cancel`)}),(0,Z.jsx)(an,{value:e.settings?.safety_margin_value??14,unit:x,editing:m,inputValue:v,dailyRateG:e.dailyRateG,onInputChange:y,onUnitChange:e=>S(e),onEdit:()=>{y(String(e.settings?.safety_margin_value??14)),S(e.settings?.safety_margin_unit??`days`),h(!0)},onSave:()=>{let t=parseInt(v,10);!isNaN(t)&&t>=0&&(j(e.settings?.lead_time_days??0,t,x),h(!1))},onCancel:()=>h(!1),isPending:C.isPending,saveLabel:c(`forecast.save`),cancelLabel:c(`forecast.cancel`),safetyMarginLabel:c(`forecast.safetyMarginLabel`)})]})]}),e.group.spools.length>1&&(0,Z.jsxs)(`div`,{className:`border-t border-bambu-dark-tertiary pt-3`,children:[(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray mb-2`,children:c(`forecast.individualSpools`)}),(0,Z.jsx)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:`#`}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.remaining`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.used`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`forecast.labelWeight`)})]})}),(0,Z.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:e.group.spools.map(e=>{let t=Math.max(0,e.label_weight-e.weight_used),n=e.label_weight>0?t/e.label_weight*100:0;return(0,Z.jsxs)(`tr`,{className:`hover:bg-bambu-dark-tertiary/30 transition-colors`,children:[(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray/70`,children:[`#`,e.id]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsx)(`div`,{className:`w-24 h-1.5 bg-bambu-dark-tertiary rounded-full overflow-hidden flex-shrink-0`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${n>50?`bg-bambu-green`:n>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(n,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-sm text-white`,children:[Math.round(t),`g`]})]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[Math.round(Math.max(0,e.weight_used-(e.weight_used_baseline??0))),`g`]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[e.label_weight,`g`]})})]},e.id)})})]})})]})]})})})]})}function nn({label:e,value:t,hint:n}){return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3`,title:n,children:[(0,Z.jsx)(`div`,{className:`text-xs font-medium text-white mb-1`,children:e}),(0,Z.jsx)(`div`,{className:`text-lg font-semibold text-white`,children:t})]})}function rn({label:e,hint:t,unit:n,editing:r,value:i,inputValue:a,onInputChange:o,onEdit:s,onSave:c,onCancel:l,isPending:u,saveLabel:d=`Save`,cancelLabel:f=`Cancel`}){return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3 space-y-1`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs font-medium text-white`,children:e}),(0,Z.jsx)(`span`,{title:t,children:(0,Z.jsx)(O,{className:`w-3 h-3 text-bambu-gray/50`})})]}),r?(0,Z.jsxs)(`form`,{className:`flex items-center gap-2`,onSubmit:e=>{e.preventDefault(),c()},children:[(0,Z.jsx)(`input`,{type:`number`,min:0,max:365,value:a,onChange:e=>o(e.target.value),className:`w-20 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`,autoFocus:!0,disabled:u}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:n}),(0,Z.jsx)(`button`,{type:`submit`,disabled:u,className:`px-2 py-1 bg-bambu-green text-white text-xs rounded hover:bg-bambu-green/80 disabled:opacity-50`,children:d}),(0,Z.jsx)(`button`,{type:`button`,onClick:l,disabled:u,className:`px-2 py-1 text-xs text-bambu-gray hover:text-white`,children:f})]}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-lg font-semibold text-white`,children:i}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:n}),(0,Z.jsx)(`button`,{onClick:s,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(I,{className:`w-3 h-3`})})]})]})}function an({value:e,unit:t,editing:n,inputValue:i,dailyRateG:a,onInputChange:o,onUnitChange:s,onEdit:c,onSave:l,onCancel:u,isPending:d,saveLabel:f=`Save`,cancelLabel:p=`Cancel`,safetyMarginLabel:m=`Safety Margin`}){let{t:h}=r(),g=t===`g`?e:a===null?null:Math.round(a*e),_=t===`days`?h(`forecast.safetyMarginHintDays`,{approx:g===null?``:h(`forecast.safetyMarginHintDaysApprox`,{g})}):h(`forecast.safetyMarginHintG`,{approx:a===null?``:h(`forecast.safetyMarginHintGApprox`,{days:Math.round(e/a)})});return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3 space-y-1`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs font-medium text-white`,children:m}),(0,Z.jsx)(`span`,{title:_,children:(0,Z.jsx)(O,{className:`w-3 h-3 text-bambu-gray/50`})})]}),n?(0,Z.jsxs)(`form`,{className:`flex items-center gap-2 flex-wrap`,onSubmit:e=>{e.preventDefault(),l()},children:[(0,Z.jsx)(`input`,{type:`number`,min:0,max:t===`g`?1e4:365,value:i,onChange:e=>o(e.target.value),className:`w-20 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`,autoFocus:!0,disabled:d}),(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded overflow-hidden text-xs`,children:[(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`days`),className:`px-2 py-1 transition-colors ${t===`days`?`bg-bambu-green text-white`:`text-bambu-gray hover:text-white`}`,children:`days`}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`g`),className:`px-2 py-1 transition-colors ${t===`g`?`bg-bambu-green text-white`:`text-bambu-gray hover:text-white`}`,children:`g`})]}),(0,Z.jsx)(`button`,{type:`submit`,disabled:d,className:`px-2 py-1 bg-bambu-green text-white text-xs rounded hover:bg-bambu-green/80 disabled:opacity-50`,children:f}),(0,Z.jsx)(`button`,{type:`button`,onClick:u,disabled:d,className:`px-2 py-1 text-xs text-bambu-gray hover:text-white`,children:p})]}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-lg font-semibold text-white`,children:e}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:t}),g!==null&&t===`days`&&(0,Z.jsxs)(`span`,{className:`text-lg font-semibold text-white`,children:[`≈ `,g,`g`]}),t===`g`&&a!==null&&(0,Z.jsxs)(`span`,{className:`text-lg font-semibold text-white`,children:[`≈ `,Math.round(e/a),`d`]}),(0,Z.jsx)(`button`,{onClick:c,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(I,{className:`w-3 h-3`})})]})]})}function on({items:e,forecasts:t,globalLeadTime:n,canWrite:a,onClose:o,onRemove:s,onClear:c}){let{t:l}=r(),d=u(),[p,m]=(0,X.useState)(`list`),h=f({mutationFn:async({id:e,status:t,item:n,avgSpoolG:r})=>{if(await i.updateShoppingListStatus(e,t),t===`received`&&n){let t=r??1e3,a={material:n.material,subtype:n.subtype,brand:n.brand,label_weight:t,core_weight:0,core_weight_catalog_id:null,color_name:n.color_name,rgba:null,extra_colors:null,effect_type:null,nozzle_temp_min:null,nozzle_temp_max:null,note:n.note??null,tag_uid:null,tray_uuid:null,data_origin:`manual`,tag_type:null,cost_per_kg:null,last_scale_weight:null,last_weighed_at:null,weight_used:0,slicer_filament:null,slicer_filament_name:null,added_full:null,last_used:null,encode_time:null,category:`Stock`,low_stock_threshold_pct:null,material_number:null};await i.bulkCreateSpools(a,n.quantity_spools),await i.removeFromShoppingList(e)}},onSuccess:()=>{d.invalidateQueries({queryKey:[`shopping-list`]}),d.invalidateQueries({queryKey:[`spools`]})}}),g=(0,X.useMemo)(()=>{let e=new Map;for(let n of t)e.set(n.group.key,n);return e},[t]),_=(0,X.useMemo)(()=>e.map(e=>({item:e,forecast:g.get(Ht(e.material,e.subtype,e.brand,e.color_name))??null})),[e,g]),v=(0,X.useMemo)(()=>_.filter(({forecast:e})=>!e||e.dailyRateG===null?!1:e.stockBreakAlert||e.daysRemaining!==null&&e.daysRemaining<=e.effectiveLeadTimeDays),[_]);function y(){let t=[l(`forecast.qty`),l(`forecast.material`),l(`inventory.brand`),l(`inventory.subtype`),l(`inventory.color`),`${l(`forecast.weight`)} (g)`,`${l(`forecast.leadTime`)} (d)`,l(`forecast.expectedRestock`),l(`forecast.status`),l(`forecast.note`)],r=e.map(e=>{let t=g.get(Ht(e.material,e.subtype,e.brand,e.color_name))??null,r=t&&t.totalSpools>0?t.totalLabelG/t.totalSpools:1e3,i=Math.round(e.quantity_spools*r),a=t?.effectiveLeadTimeDays??n??0,o=a>0?Wt(Ut(new Date,a)):``;return[e.quantity_spools,e.material,e.brand??``,e.subtype??``,e.color_name??``,i,a||``,o,e.status,e.note??``].map(e=>`"${String(e).replace(/"/g,`""`)}"`).join(`,`)}),i=[t.join(`,`),...r].join(`
- `),a=new Blob([i],{type:`text/csv`}),o=URL.createObjectURL(a),s=document.createElement(`a`);s.href=o,s.download=`shopping-list-${new Date().toISOString().slice(0,10)}.csv`,s.click(),setTimeout(()=>URL.revokeObjectURL(o),100)}return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsx)(W,{className:`w-4 h-4 text-bambu-green`}),(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-white`,children:l(`forecast.shoppingList`)}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:l(`forecast.shoppingListItems`,{count:e.length})}),e.length>0&&(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-tertiary rounded-md p-0.5 ml-1`,children:[(0,Z.jsxs)(`button`,{onClick:()=>m(`list`),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${p===`list`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:[(0,Z.jsx)(oe,{className:`w-3 h-3`}),l(`forecast.listView`)]}),(0,Z.jsxs)(`button`,{onClick:()=>m(`logistics`),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${p===`logistics`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:[(0,Z.jsx)(Pt,{className:`w-3 h-3`}),l(`forecast.logisticsView`),v.length>0&&(0,Z.jsx)(`span`,{className:`w-3.5 h-3.5 rounded-full bg-red-500 text-white text-[9px] font-bold flex items-center justify-center`,children:v.length})]})]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[e.length>0&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`button`,{onClick:y,className:`flex items-center gap-1.5 text-xs text-bambu-gray hover:text-white transition-colors px-2 py-1 rounded border border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`,children:[(0,Z.jsx)(Oe,{className:`w-3 h-3`}),l(`forecast.downloadCsv`)]}),a&&(0,Z.jsx)(`button`,{onClick:c,className:`text-xs text-red-700 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 transition-colors px-2 py-1 rounded border border-red-200 dark:border-red-500/20 hover:bg-red-50 dark:hover:bg-red-500/10`,children:l(`forecast.clearAll`)})]}),(0,Z.jsx)(`button`,{onClick:o,className:`p-1 text-bambu-gray hover:text-white transition-colors`,children:(0,Z.jsx)(me,{className:`w-4 h-4`})})]})]}),e.length===0?(0,Z.jsxs)(`div`,{className:`flex flex-col items-center py-8 text-bambu-gray`,children:[(0,Z.jsx)(oe,{className:`w-8 h-8 mb-2 opacity-30`}),(0,Z.jsx)(`p`,{className:`text-sm`,children:l(`forecast.shoppingListEmpty`)})]}):p===`list`?(0,Z.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/20`,children:[(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.qty`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.material`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.weight`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.leadTime`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.expectedRestock`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.status`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.note`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 text-right text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.actions`)})]})}),(0,Z.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:e.map(e=>{let t=[e.brand,e.material,e.subtype,e.color_name].filter(Boolean).join(` `),r=v.some(t=>t.item.id===e.id),i=g.get(Ht(e.material,e.subtype,e.brand,e.color_name))??null,o=i&&i.totalSpools>0?i.totalLabelG/i.totalSpools:1e3,c=Math.round(e.quantity_spools*o),u=i?.effectiveLeadTimeDays??n??0,d=u>0?Ut(new Date,u):null,f=e.status===`purchased`||e.status===`received`,p=e.status===`received`,m=h.isPending;return(0,Z.jsxs)(`tr`,{className:`hover:bg-bambu-dark-tertiary/30 transition-colors ${r&&!f?`bg-red-500/5`:``}`,children:[(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsxs)(`span`,{className:`text-sm font-semibold text-bambu-green`,children:[e.quantity_spools,`×`]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:t}),r&&!f&&(0,Z.jsx)(V,{className:`w-3.5 h-3.5 text-red-600 dark:text-red-400 flex-shrink-0`,"aria-label":l(`forecast.stockBreakBefore`)})]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:c>=1e3?`${(c/1e3).toFixed(1)}kg`:`${c}g`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:u>0?`${u}d`:`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:d?Wt(d):`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`inline-flex items-center px-2 py-0.5 rounded text-xs font-medium ${p?`bg-bambu-green/20 text-bambu-green`:f?`bg-blue-100 dark:bg-blue-400/20 text-blue-700 dark:text-blue-400`:`bg-bambu-dark-tertiary text-bambu-gray`}`,children:l(p?`forecast.received`:f?`forecast.purchased`:`forecast.pending`)})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:e.note||`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`div`,{className:`flex items-center justify-end gap-1`,children:a&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`button`,{onClick:()=>h.mutate({id:e.id,status:f?`pending`:`purchased`}),disabled:m||p,title:l(f?`forecast.resetToPending`:`forecast.markPurchased`),className:`p-1.5 rounded transition-colors disabled:opacity-30 ${f?`text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300`:`text-blue-600/50 dark:text-blue-400/50 hover:text-blue-600 dark:hover:text-blue-400`}`,children:f?(0,Z.jsx)(He,{className:`w-4 h-4`}):(0,Z.jsx)(Ft,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>h.mutate({id:e.id,status:`received`,item:e,avgSpoolG:o}),disabled:m||!f||p,title:l(`forecast.markReceived`),className:`p-1.5 rounded transition-colors text-bambu-green/50 hover:text-bambu-green disabled:opacity-30`,children:(0,Z.jsx)(Lt,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>s(e.id),className:`p-1 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 transition-colors`,title:l(`forecast.remove`),children:(0,Z.jsx)(et,{className:`w-3.5 h-3.5`})})]})})})]},e.id)})})]})}):(0,Z.jsx)(`div`,{className:`divide-y divide-bambu-dark-tertiary`,children:_.filter(({item:e})=>e.status!==`received`).map(({item:e,forecast:t})=>(0,Z.jsx)(sn,{item:e,forecast:t,globalLeadTime:n,canWrite:a,onRemove:()=>s(e.id)},e.id))})]})}function sn({item:e,forecast:t,globalLeadTime:n,canWrite:i,onRemove:a}){let{t:o}=r(),s=[e.brand,e.material,e.subtype,e.color_name].filter(Boolean).join(` `),c=(0,X.useMemo)(()=>{if(!t||t.dailyRateG===null||t.dailyRateG<=0)return null;let n=t.dailyRateG,r=t.effectiveLeadTimeDays,i=t.totalSpools>0?t.totalLabelG/t.totalSpools:1e3,a=e.quantity_spools*i,o=Math.max(0,t.totalRemainingG-n*r),s=o+a,c=Math.ceil(s/n),l=Math.min(r+c+5,365),u=[];for(let e=0;e<=l;e++){let i=Gt(Ut(new Date,e));if(e===r)u.push({day:e,label:i,stock:Math.round(o),rop:Math.round(t.reorderPointG),safetyStock:Math.round(t.safetyStockG)}),u.push({day:e,label:i,stock:Math.round(s),rop:Math.round(t.reorderPointG),safetyStock:Math.round(t.safetyStockG),arrival:!0});else{let a=e<r?Math.max(0,t.totalRemainingG-n*e):Math.max(0,s-n*(e-r));u.push({day:e,label:i,stock:Math.round(a),rop:Math.round(t.reorderPointG),safetyStock:Math.round(t.safetyStockG)})}}return{points:u,lt:r,maxDays:l,arrivalG:a,peakG:s,stockAtArrival:o}},[t,e.quantity_spools]),l=(0,X.useMemo)(()=>{if(!t||t.dailyRateG===null||t.dailyRateG<=0)return null;let e=Math.floor(t.totalRemainingG/t.dailyRateG);return e<t.effectiveLeadTimeDays?e:null},[t]),u=l!==null;return(0,Z.jsxs)(`div`,{className:`px-4 py-4 ${u?`bg-red-500/5`:``}`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between mb-3`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 min-w-0`,children:[u?(0,Z.jsx)(V,{className:`w-4 h-4 text-red-600 dark:text-red-400 flex-shrink-0`}):(0,Z.jsx)(w,{className:`w-4 h-4 text-bambu-green/60 flex-shrink-0`}),(0,Z.jsx)(`span`,{className:`text-sm font-medium text-white truncate`,children:s}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray flex-shrink-0`,children:[o(`forecast.spoolCount`,{count:e.quantity_spools}),` ordered`]})]}),i&&(0,Z.jsx)(`button`,{onClick:a,className:`p-1 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 transition-colors flex-shrink-0`,children:(0,Z.jsx)(et,{className:`w-3.5 h-3.5`})})]}),u&&(0,Z.jsxs)(`div`,{className:`mb-3 px-3 py-2 rounded-lg bg-red-50 dark:bg-red-500/10 border border-red-200 dark:border-red-500/20 text-xs text-red-700 dark:text-red-300`,children:[(0,Z.jsx)(`span`,{className:`font-medium`,children:o(`forecast.stockBreakIn`,{days:l})}),` `,o(`forecast.stockRunsOutBefore`,{lt:t.effectiveLeadTimeDays}),t.dailyRateG!==null&&(0,Z.jsxs)(`span`,{children:[` `,o(`forecast.atRate`,{rate:t.dailyRateG.toFixed(1)}),` `,(0,Z.jsx)(`span`,{className:`font-semibold`,children:o(`forecast.moreSpools`,{count:Math.ceil((t.dailyRateG*t.effectiveLeadTimeDays-t.totalRemainingG)/(t.totalLabelG/(t.totalSpools||1)||1e3))})}),` `,o(`forecast.bridgeGap`)]})]}),!t||t.dailyRateG===null?(0,Z.jsx)(`div`,{className:`py-4 text-center text-xs text-bambu-gray`,children:o(`forecast.noUsageData`)}):c?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`div`,{className:`grid grid-cols-5 gap-2 mb-3`,children:[(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.stock`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[Math.round(t.totalRemainingG),`g`]})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.leadTime`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[t.effectiveLeadTimeDays,`d`]}),(0,Z.jsxs)(`div`,{className:`text-[10px] text-bambu-gray/60`,children:[`max(g:`,n,`, sku:`,t.settings?.lead_time_days??0,`)`]})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.safetyMarginLabel`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[Math.round(t.safetyStockG),`g`]})]}),(0,Z.jsxs)(`div`,{className:`rounded-lg px-2.5 py-2 text-center ${u?`bg-red-100 dark:bg-red-500/15`:`bg-bambu-dark-tertiary/40`}`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.daysLeft`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold ${u?`text-red-700 dark:text-red-400`:`text-green-700 dark:text-green-400`}`,children:[t.daysRemaining??`—`,`d`]})]}),c&&(0,Z.jsxs)(`div`,{className:`bg-bambu-green/15 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.onArrival`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-bambu-green`,children:[Math.round(c.arrivalG),`g`]}),(0,Z.jsxs)(`div`,{className:`text-[10px] text-bambu-gray/60`,children:[`+`,o(`forecast.spoolCount`,{count:e.quantity_spools})]})]})]}),(0,Z.jsx)(le,{width:`100%`,height:180,children:(0,Z.jsxs)(at,{data:c.points,margin:{top:8,right:8,bottom:0,left:0},children:[(0,Z.jsxs)(`defs`,{children:[(0,Z.jsxs)(`linearGradient`,{id:`cart-pre-${e.id}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Z.jsx)(`stop`,{offset:`5%`,stopColor:u?`#EF4444`:`#1DB954`,stopOpacity:.25}),(0,Z.jsx)(`stop`,{offset:`95%`,stopColor:u?`#EF4444`:`#1DB954`,stopOpacity:.02})]}),(0,Z.jsxs)(`linearGradient`,{id:`cart-post-${e.id}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Z.jsx)(`stop`,{offset:`5%`,stopColor:`#1DB954`,stopOpacity:.3}),(0,Z.jsx)(`stop`,{offset:`95%`,stopColor:`#1DB954`,stopOpacity:.03})]})]}),(0,Z.jsx)(de,{strokeDasharray:`3 3`,stroke:`#374151`,strokeOpacity:.4}),(0,Z.jsx)(ue,{dataKey:`label`,tick:{fill:`#6B7280`,fontSize:9},interval:Math.max(0,Math.ceil(c.maxDays/6)-1),axisLine:!1,tickLine:!1}),(0,Z.jsx)(fe,{tick:{fill:`#6B7280`,fontSize:9},axisLine:!1,tickLine:!1,tickFormatter:e=>e>=1e3?`${(e/1e3).toFixed(1)}kg`:`${e}g`,width:44}),(0,Z.jsx)(B,{contentStyle:{background:`#1a1a2e`,border:`1px solid #374151`,borderRadius:8,fontSize:11},labelStyle:{color:`#9CA3AF`},formatter:(e,t)=>typeof e==`number`?t===`stock`?`${e}g — ${o(`forecast.stock`)}`:t===`rop`?`${e}g — ${o(`forecast.reorderPoint`)}`:t===`safetyStock`?`${e}g — ${o(`forecast.safetyMarginLabel`)}`:`${e}`:``}),(0,Z.jsx)(it,{type:`linear`,dataKey:`stock`,stroke:`#1DB954`,strokeWidth:2,fill:`url(#cart-post-${e.id})`,dot:!1,activeDot:{r:3}}),t.reorderPointG>0&&(0,Z.jsx)(C,{y:t.reorderPointG,stroke:`#F59E0B`,strokeDasharray:`5 3`,strokeOpacity:.8,label:{value:`ROP`,position:`insideTopRight`,fill:`#F59E0B`,fontSize:9}}),t.safetyStockG>0&&(0,Z.jsx)(C,{y:t.safetyStockG,stroke:`#6B7280`,strokeDasharray:`3 3`,strokeOpacity:.6,label:{value:`SS`,position:`insideTopRight`,fill:`#6B7280`,fontSize:9}}),(0,Z.jsx)(C,{x:Gt(Ut(new Date,c.lt)),stroke:`#3B82F6`,strokeWidth:1.5,strokeDasharray:`4 3`,strokeOpacity:.9,label:{value:`+${c.arrivalG>=1e3?`${(c.arrivalG/1e3).toFixed(1)}kg`:`${Math.round(c.arrivalG)}g`} arrives (d${c.lt})`,position:`insideTopLeft`,fill:`#3B82F6`,fontSize:9}}),l!==null&&(0,Z.jsx)(C,{x:Gt(Ut(new Date,l)),stroke:`#EF4444`,strokeWidth:1.5,strokeOpacity:.9,label:{value:`OUT`,position:`insideTopLeft`,fill:`#EF4444`,fontSize:9}})]})}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-3 mt-2 text-[10px] text-bambu-gray`,children:[(0,Z.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-yellow-400 border-dashed`}),` `,o(`forecast.ropLabel`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t border-bambu-gray border-dashed`}),` `,o(`forecast.safetyStockLegend`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-blue-400 border-dashed`}),` `,o(`forecast.stockArrivalLegend`)]}),u&&(0,Z.jsxs)(`span`,{className:`flex items-center gap-1 text-red-700 dark:text-red-400`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-red-400`}),` `,o(`forecast.stockoutLegend`)]})]})]}):null]})}function cn({forecast:e,onClose:t,onAdd:n}){let{t:i}=r(),a=[e.group.brand,e.group.material,e.group.subtype,e.group.colorName].filter(Boolean).join(` `),[o,s]=(0,X.useState)(`qty`),[c,l]=(0,X.useState)(`1`),[u,d]=(0,X.useState)(`30`),[f,p]=(0,X.useState)(``),m=(0,X.useMemo)(()=>{if(!e.dailyRateG||e.dailyRateG<=0)return null;let t=e.dailyRateG*Number(u),n=e.group.spools.length>0?e.group.spools.reduce((e,t)=>e+t.label_weight,0)/e.group.spools.length:1e3;return Math.ceil(t/n)},[e,u]),h=o===`qty`?parseInt(c,10)||1:m??1;function g(t){t.preventDefault(),n({material:e.group.material,subtype:e.group.subtype,brand:e.group.brand,color_name:e.group.colorName,quantity_spools:h,note:f||null})}return(0,Z.jsx)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4`,children:(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-2xl border border-bambu-dark-tertiary w-full max-w-sm shadow-2xl`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-5 pt-5 pb-4 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(W,{className:`w-5 h-5 text-bambu-green`}),(0,Z.jsx)(`h2`,{className:`text-base font-semibold text-white`,children:i(`forecast.addToCartTitle`)})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white transition-colors`,children:(0,Z.jsx)(me,{className:`w-5 h-5`})})]}),(0,Z.jsxs)(`form`,{onSubmit:g,className:`p-5 space-y-4`,children:[(0,Z.jsx)(`div`,{className:`text-sm text-bambu-gray`,children:a}),(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-tertiary rounded-lg p-0.5`,children:[(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`qty`),className:`flex-1 py-1.5 text-xs font-medium rounded-md transition-colors ${o===`qty`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:i(`forecast.byQuantity`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`duration`),className:`flex-1 py-1.5 text-xs font-medium rounded-md transition-colors ${o===`duration`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:i(`forecast.byDuration`)})]}),o===`qty`?(0,Z.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Z.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.numberOfSpools`)}),(0,Z.jsx)(`input`,{type:`number`,min:1,max:99,value:c,onChange:e=>l(e.target.value),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,autoFocus:!0})]}):(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Z.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.lastHowManyDays`)}),(0,Z.jsx)(`input`,{type:`number`,min:1,max:365,value:u,onChange:e=>d(e.target.value),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,autoFocus:!0})]}),e.dailyRateG===null?(0,Z.jsx)(`div`,{className:`text-xs text-yellow-700 dark:text-yellow-400 px-1`,children:i(`forecast.noUsageQty`)}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 px-3 py-2 bg-bambu-dark-tertiary/50 rounded-lg`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`≈`}),(0,Z.jsx)(`span`,{className:`text-sm font-semibold text-bambu-green`,children:i(`forecast.spoolCount`,{count:m??0})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray`,children:[`at `,e.dailyRateG.toFixed(1),`g/day`]})]})]}),(0,Z.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Z.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.noteOptional`)}),(0,Z.jsx)(`input`,{type:`text`,maxLength:200,value:f,onChange:e=>p(e.target.value),placeholder:i(`forecast.notePlaceholder`),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/40 focus:outline-none focus:border-bambu-green`})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 pt-1`,children:[(0,Z.jsx)(`button`,{type:`submit`,className:`flex-1 py-2 bg-bambu-green text-white text-sm font-medium rounded-lg hover:bg-bambu-green/80 transition-colors`,children:i(`forecast.addNSpools`,{count:h})}),(0,Z.jsx)(`button`,{type:`button`,onClick:t,className:`px-4 py-2 text-sm text-bambu-gray hover:text-white border border-bambu-dark-tertiary rounded-lg transition-colors`,children:i(`forecast.cancel`)})]})]})]})})}function ln({links:e,onChange:t,currencySymbol:n}){let{t:a}=r(),{showToast:o}=ee(),c=u(),l=(0,X.useRef)(null),[f,p]=(0,X.useState)(!1),[m,h]=(0,X.useState)(``),[g,_]=(0,X.useState)(!1),{data:v=[],isLoading:y,isError:b}=d({queryKey:Ze,queryFn:i.getSuppliers});(0,X.useEffect)(()=>{if(!f)return;let e=e=>{l.current&&!l.current.contains(e.target)&&(p(!1),h(``))};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[f]);let x=(0,X.useMemo)(()=>new Set(e.map(e=>e.supplier_id)),[e]),S=(0,X.useMemo)(()=>v.filter(e=>!x.has(e.id)).filter(e=>e.name.toLowerCase().includes(m.trim().toLowerCase())),[v,x,m]),C=v.some(e=>e.name.toLowerCase()===m.trim().toLowerCase()),w=n=>{t([...e,{supplier_id:n.id,supplier_name:n.name,supplier_article_number:``,quoted_price_per_kg:null,is_purchase_source:!1}]),p(!1),h(``)},E=async()=>{let e=m.trim();if(e){_(!0);try{let t=await i.createSupplier({name:e});await G(c),w(t)}catch(e){console.error(`SupplierSection.createAndAdd failed:`,e),o(a(e instanceof s&&e.status===409?`inventory.suppliers.duplicateName`:`inventory.suppliers.createFailed`),`error`)}finally{_(!1)}}},D=(n,r)=>{t(e.map(e=>e.supplier_id===n?{...e,...r}:r.is_purchase_source?{...e,is_purchase_source:!1}:e))},O=n=>{t(e.filter(e=>e.supplier_id!==n))};return(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`label`,{className:`block text-sm font-medium text-bambu-gray mb-1`,children:a(`inventory.suppliers.label`)}),e.length>0&&(0,Z.jsx)(`div`,{className:`space-y-2 mb-2`,children:e.map(e=>(0,Z.jsxs)(`div`,{className:`p-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs bg-bambu-green/10 text-bambu-green`,children:[(0,Z.jsx)(Rt,{className:`w-3 h-3`}),e.supplier_name]}),(0,Z.jsxs)(`label`,{className:`ml-auto flex items-center gap-1.5 text-xs text-bambu-gray cursor-pointer`,title:a(`inventory.suppliers.purchaseSourceHelp`),children:[(0,Z.jsx)(`input`,{type:`checkbox`,className:`w-3.5 h-3.5 accent-bambu-green`,checked:e.is_purchase_source,onChange:t=>D(e.supplier_id,{is_purchase_source:t.target.checked})}),a(`inventory.suppliers.purchaseSource`)]}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>O(e.supplier_id),className:`p-1 rounded text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary`,"aria-label":a(`common.remove`),children:(0,Z.jsx)(me,{className:`w-3.5 h-3.5`})})]}),(0,Z.jsxs)(`div`,{className:`flex gap-2 mt-2`,children:[(0,Z.jsx)(`input`,{type:`text`,className:`flex-1 min-w-0 px-2 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`,placeholder:a(`inventory.suppliers.articleNumberPlaceholder`),value:e.supplier_article_number,maxLength:100,onChange:t=>D(e.supplier_id,{supplier_article_number:t.target.value})}),(0,Z.jsxs)(`div`,{className:`relative w-28`,children:[(0,Z.jsx)(`span`,{className:`absolute left-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray pointer-events-none`,children:n}),(0,Z.jsx)(`input`,{type:`number`,className:`w-full pl-6 pr-2 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs text-right placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`,placeholder:a(`inventory.suppliers.pricePlaceholder`),title:a(`inventory.suppliers.priceHelp`),min:0,step:.01,value:e.quoted_price_per_kg??``,onChange:t=>D(e.supplier_id,{quoted_price_per_kg:t.target.value===``?null:parseFloat(t.target.value)})})]})]})]},e.supplier_id))}),(0,Z.jsxs)(`div`,{className:`relative`,ref:l,children:[(0,Z.jsxs)(`button`,{type:`button`,onClick:()=>p(e=>!e),className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-sm text-bambu-gray hover:text-white flex items-center justify-between transition-colors focus:outline-none focus:border-bambu-green`,children:[(0,Z.jsx)(`span`,{children:a(`inventory.suppliers.addSupplier`)}),(0,Z.jsx)(T,{className:`w-4 h-4 transition-transform ${f?`rotate-180`:``}`})]}),f&&(0,Z.jsxs)(`div`,{className:`absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg`,children:[(0,Z.jsx)(`div`,{className:`p-2 border-b border-bambu-dark-tertiary`,children:(0,Z.jsx)(`input`,{type:`text`,autoFocus:!0,className:`w-full px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`,placeholder:a(`inventory.suppliers.searchPlaceholder`),value:m,onChange:e=>h(e.target.value)})}),(0,Z.jsx)(`div`,{className:`max-h-48 overflow-y-auto py-1`,children:y?(0,Z.jsxs)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray flex items-center gap-2`,children:[(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}),a(`common.loading`)]}):b?(0,Z.jsxs)(`div`,{className:`px-3 py-2 text-sm text-red-400 flex items-center gap-2`,children:[(0,Z.jsx)(V,{className:`w-4 h-4`}),a(`inventory.suppliers.loadFailed`)]}):(0,Z.jsxs)(Z.Fragment,{children:[S.map(e=>(0,Z.jsx)(`button`,{type:`button`,onClick:()=>w(e),className:`w-full px-3 py-2 text-left text-sm text-white hover:bg-bambu-dark-tertiary`,children:e.name},e.id)),S.length===0&&!m.trim()&&(0,Z.jsx)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray`,children:a(`inventory.suppliers.noneLeft`)}),m.trim()&&!C&&(0,Z.jsxs)(`button`,{type:`button`,onClick:E,disabled:g,className:`w-full px-3 py-2 text-left text-sm text-bambu-green hover:bg-bambu-dark-tertiary flex items-center gap-1.5`,children:[g?(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(Je,{className:`w-4 h-4`}),a(`inventory.suppliers.createNew`,{name:m.trim()})]})]})})]})]}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray mt-1`,children:a(`inventory.suppliers.help`)})]})}function un({filaments:e,isLoading:t,selectedId:n,onSelect:i}){let{t:a}=r(),[o,s]=(0,X.useState)(!1),[c,l]=(0,X.useState)(``),u=(0,X.useRef)(null),d=(0,X.useRef)(null),f=(0,X.useMemo)(()=>e.find(e=>e.id===n)??null,[e,n]);(0,X.useEffect)(()=>{let e=e=>{u.current&&!u.current.contains(e.target)&&(s(!1),l(``))};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[]);let p=(0,X.useMemo)(()=>{let t=c.toLowerCase().trim();return t?e.filter(e=>e.name.toLowerCase().includes(t)||(e.material?.toLowerCase().includes(t)??!1)||(e.vendor?.name.toLowerCase().includes(t)??!1)||(e.color_name?.toLowerCase().includes(t)??!1)):e},[e,c]);(0,X.useEffect)(()=>{o&&d.current?.focus()},[o]);let m=()=>{s(!0),l(``)},h=e=>{i(e),s(!1),l(``)};return(0,Z.jsxs)(`div`,{ref:u,className:`relative`,children:[(0,Z.jsx)(`label`,{className:`block text-sm font-medium text-bambu-gray mb-1`,children:a(`inventory.pickFromSpoolmanCatalog`)}),(0,Z.jsxs)(`button`,{type:`button`,onClick:m,className:`w-full flex items-center gap-2 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-left focus:outline-none focus:border-bambu-green hover:border-bambu-gray transition-colors`,children:[t?(0,Z.jsx)(F,{className:`w-4 h-4 text-bambu-gray animate-spin shrink-0`}):f?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`span`,{className:`w-4 h-4 rounded-full shrink-0 border border-white/20`,style:$e(f.color_hex),"aria-label":a(`inventory.spoolmanFilamentColorSwatch`)}),(0,Z.jsxs)(`span`,{className:`text-white text-sm truncate flex-1`,children:[f.vendor?.name?`${f.vendor.name} — `:``,f.name]})]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(oe,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`span`,{className:`text-bambu-gray text-sm flex-1`,children:a(`inventory.spoolmanFilamentCatalog`)})]}),(0,Z.jsx)(T,{className:`w-4 h-4 text-bambu-gray shrink-0 ml-auto`})]}),o&&(0,Z.jsxs)(`div`,{className:`absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl`,children:[(0,Z.jsx)(`div`,{className:`p-2 border-b border-bambu-dark-tertiary`,children:(0,Z.jsx)(`input`,{ref:d,type:`text`,value:c,onChange:e=>l(e.target.value),placeholder:a(`inventory.pickFromSpoolmanCatalog`),className:`w-full px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm placeholder-bambu-gray focus:outline-none focus:border-bambu-green`})}),(0,Z.jsx)(`ul`,{className:`max-h-56 overflow-y-auto py-1`,children:t?(0,Z.jsxs)(`li`,{className:`flex items-center justify-center gap-2 py-4 text-bambu-gray text-sm`,children:[(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}),(0,Z.jsx)(`span`,{children:a(`common.loading`,`Loading…`)})]}):p.length===0?(0,Z.jsx)(`li`,{className:`py-4 text-center text-bambu-gray text-sm`,children:a(`inventory.noSpoolmanFilaments`)}):p.map(e=>(0,Z.jsx)(`li`,{children:(0,Z.jsxs)(`button`,{type:`button`,onClick:()=>h(e),className:`w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-bambu-dark-tertiary transition-colors ${e.id===n?`bg-bambu-dark-tertiary`:``}`,children:[(0,Z.jsx)(`span`,{className:`w-4 h-4 rounded-full shrink-0 border border-white/20`,style:$e(e.color_hex),"aria-label":a(`inventory.spoolmanFilamentColorSwatch`)}),(0,Z.jsxs)(`span`,{className:`flex-1 min-w-0`,children:[(0,Z.jsxs)(`span`,{className:`block text-white text-sm truncate`,children:[e.vendor?.name?`${e.vendor.name} — `:``,e.name]}),(0,Z.jsxs)(`span`,{className:`block text-bambu-gray text-xs truncate`,children:[[e.material,e.color_name].filter(Boolean).join(` · `),e.weight?` · ${e.weight}g`:``]})]})]})},e.id))})]})]})}function dn(e,t){let n=e.nozzles??[];return Y(n[(e.printer.nozzle_count??1)>1&&t>0?t:0]?.nozzle_type)??Y(n[0]?.nozzle_type)}function fn(e){let t=new Set;for(let n of e.nozzles??[]){let e=(n?.nozzle_diameter??``).trim();e&&parseFloat(e)>0&&t.add(e)}for(let n of e.calibrations){let e=(n.nozzle_diameter??``).trim();e&&parseFloat(e)>0&&t.add(e)}return Array.from(t).sort((e,t)=>parseFloat(e)-parseFloat(t))}function pn(e,t){return(e.printer.nozzle_count??1)>1?[{extruder:1,label:t.left},{extruder:0,label:t.right}]:[{extruder:0,label:t.single}]}function mn({formData:e,printersWithCalibrations:t,filamentOptions:n,modelPresets:i,setModelPresets:a,selectedProfiles:o,setSelectedProfiles:s,selectedGroupId:c,setSelectedGroupId:l,printerModels:u,isLoading:d=!1}){let{t:f}=r(),p=(0,X.useMemo)(()=>{let e=new Map,n=[];for(let r of t){let t=(r.printer.model||``).trim();if(!t){n.push(r);continue}let i=e.get(t);i?i.push(r):e.set(t,[r])}return[...Array.from(e.entries()).map(([e,t])=>({id:`m:${e}`,model:e,printers:t,diameters:Array.from(new Set([...pt,...t.flatMap(fn)])).sort((e,t)=>parseFloat(e)-parseFloat(t))})).sort((e,t)=>e.model.localeCompare(t.model)),...n.map(e=>({id:`p:${e.printer.id}`,model:``,printers:[e],diameters:Array.from(new Set([...pt,...fn(e)])).sort((e,t)=>parseFloat(e)-parseFloat(t))}))]},[t]),m=p.find(e=>e.id===c)??p[0],h=(0,X.useMemo)(()=>{let e=new Map;return(t,r)=>{if(!t)return n;let i=e.get(t);if(i||(i=n.filter(e=>{let n=nt(e.name,u??{});return!n||tt(n,t)}),e.set(t,i)),r&&!i.some(e=>e.code===r)){let e=n.find(e=>e.code===r);if(e)return[e,...i]}return i}},[n,u]),g=[e.brand,e.material,e.subtype].map(e=>e.trim()).filter(Boolean).join(` `),_=/^[0-9A-Fa-f]{6,8}$/.test(e.rgba)?`#${e.rgba.slice(0,6)}`:`var(--bambu-gray, #808080)`,v=e=>pn(e,{left:f(`inventory.leftNozzle`),right:f(`inventory.rightNozzle`),single:f(`inventory.nozzle`)}),y=t=>t.printer.connected?t.calibrations.filter(t=>St(t,e)):[],b=e=>{let t=0;for(let n of e.printers){let r=y(n);for(let i of v(n))for(let a of e.diameters)r.some(e=>(e.extruder_id??0)===i.extruder&&((e.nozzle_diameter??``).trim()||`0.4`)===a)&&(o.get(J(n.printer.id,i.extruder,a))||t++)}return t},x=(e,t,n)=>{a(r=>{let i=new Map(r),a=ht(e,t);return n?i.set(a,{code:n.code,name:n.name}):i.delete(a),i})},S=(e,t,n,r)=>{s(i=>{let a=new Map(i),o=J(e,t,n);return r?a.set(o,r):a.delete(o),a})},C=()=>{let t=n.find(t=>t.code===e.slicer_filament);if(!t)return;let r=t.name.split(`@`)[0].trim().toLowerCase();r&&a(e=>{let t=new Map(e);for(let e of p){if(!e.model)continue;let n=h(e.model,void 0).filter(e=>e.name.toLowerCase().startsWith(r)&&nt(e.name,u??{})!==null);if(n.length!==0)for(let r of e.diameters){let i=n.find(e=>RegExp(`\\b${r.replace(`.`,`\\.`)}\\s*nozzle\\b`,`i`).test(e.name)),a=n.find(e=>!/\b[\d.]+\s*nozzle\b/i.test(e.name)),o=i??a;o&&t.set(ht(e.model,r),{code:o.code,name:o.name})}}return t})};if(t.length===0)return(0,Z.jsx)(`div`,{className:`p-6 bg-bambu-dark rounded-lg text-center`,children:(0,Z.jsxs)(`p`,{className:`text-bambu-gray flex items-center justify-center gap-2`,children:[d&&(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}),f(d?`common.loading`:`inventory.noPrintersConfigured`)]})});let T=(e,t,n)=>{let r=ht(e,t),a=i.get(r),o=h(e,a?.code);return(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 min-w-0`,children:(0,Z.jsx)(st,{ariaLabel:`${e} ${t}mm ${f(`inventory.filamentPreset`)}`,value:a?.code??``,options:o,inheritLabel:n,disabled:!e,onChange:n=>x(e,t,n)})}),(0,Z.jsx)(`span`,{className:`text-[10px] font-semibold uppercase tracking-wide px-2 py-1 rounded-full shrink-0 ${a?`bg-bambu-green/20 text-bambu-green`:`bg-bambu-dark-tertiary text-bambu-gray`}`,children:f(a?`inventory.presetOverride`:`inventory.presetInherited`)})]})};return(0,Z.jsxs)(`div`,{className:`space-y-3`,children:[(g||e.color_name)&&(0,Z.jsxs)(`div`,{className:`flex items-center gap-2.5 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Z.jsx)(`span`,{className:`w-5 h-5 rounded-full border border-white/15 shrink-0`,style:{background:_},"aria-hidden":`true`}),g&&(0,Z.jsx)(`span`,{className:`text-sm text-white truncate`,children:g}),e.color_name&&(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray truncate`,children:e.color_name})]}),(0,Z.jsxs)(`div`,{className:`flex flex-col md:flex-row gap-4`,children:[(0,Z.jsx)(`div`,{role:`tablist`,"aria-label":f(`inventory.printersTab`),"aria-orientation":`vertical`,className:`md:w-52 md:shrink-0 md:self-start md:sticky md:top-0 flex md:flex-col gap-1.5 overflow-x-auto md:overflow-x-visible`,children:p.map(e=>{let t=e===m,n=b(e);return(0,Z.jsxs)(`button`,{type:`button`,role:`tab`,onClick:()=>l(e.id),"aria-selected":t,className:`flex items-center gap-2 px-3 py-2 rounded-lg border text-left transition-colors shrink-0 md:shrink ${t?`bg-bambu-green/10 border-bambu-green/40 text-white`:`bg-transparent border-transparent text-bambu-gray hover:bg-bambu-dark hover:text-white`}`,children:[(0,Z.jsxs)(`div`,{className:`min-w-0 flex-1`,children:[(0,Z.jsx)(`div`,{className:`text-sm font-semibold truncate`,children:e.model||f(`inventory.unknownModel`)}),(0,Z.jsx)(`div`,{className:`text-[11px] text-bambu-gray`,children:e.printers.length===1?f(`inventory.onePrinter`):f(`inventory.nPrinters`,{n:e.printers.length})})]}),n>0?(0,Z.jsx)(`span`,{className:`text-[10px] font-mono px-1.5 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray shrink-0`,children:n}):(0,Z.jsx)(w,{className:`w-3.5 h-3.5 text-bambu-green shrink-0`})]},e.id)})}),(0,Z.jsx)(`div`,{className:`flex-1 min-w-0`,children:m&&(0,Z.jsxs)(`div`,{className:`space-y-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-start justify-between gap-3`,children:[(0,Z.jsxs)(`div`,{className:`min-w-0`,children:[(0,Z.jsx)(`h4`,{className:`text-base font-semibold text-white truncate`,children:m.model||f(`inventory.unknownModel`)}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:m.printers.map(e=>e.printer.name).join(`, `)})]}),e.slicer_filament&&(0,Z.jsxs)(`button`,{type:`button`,onClick:C,title:f(`inventory.autoMatchPresetsHint`),className:`flex items-center gap-1.5 px-2 py-1 text-xs bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white hover:border-bambu-green transition-colors shrink-0`,children:[(0,Z.jsx)(z,{className:`w-3.5 h-3.5`}),f(`inventory.autoMatchPresets`)]})]}),(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsx)(`p`,{className:`text-xs font-semibold text-bambu-gray uppercase tracking-wide`,children:f(`inventory.filamentPreset`)}),m.model?(0,Z.jsx)(`div`,{className:`space-y-2`,children:m.diameters.map(e=>(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray w-14 shrink-0`,children:[e,`mm`]}),(0,Z.jsx)(`div`,{className:`flex-1 min-w-0`,children:T(m.model,e,f(`inventory.presetUseSpoolDefault`))})]},e))}):(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic`,children:f(`inventory.presetNeedsModel`)})]}),(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsx)(`p`,{className:`text-xs font-semibold text-bambu-gray uppercase tracking-wide`,children:f(`inventory.kProfilesPerPrinter`)}),e.material?m.printers.map(e=>{let t=y(e);return(0,Z.jsxs)(`div`,{className:`p-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(Xe,{className:`w-3.5 h-3.5 text-bambu-gray shrink-0`}),(0,Z.jsx)(`span`,{className:`text-sm font-semibold text-white truncate`,children:e.printer.name}),(0,Z.jsx)(`span`,{className:`text-[10px] font-semibold uppercase tracking-wide px-2 py-0.5 rounded-full shrink-0 ${e.printer.connected?`bg-green-500/20 text-green-500`:`bg-bambu-dark-tertiary text-bambu-gray`}`,children:e.printer.connected?f(`inventory.connected`):f(`inventory.offline`)})]}),e.printer.connected?t.length===0?(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-1`,children:f(`inventory.noKProfilesMatch`)}):(0,Z.jsxs)(`div`,{className:`grid gap-x-3 gap-y-1.5 items-center`,style:{gridTemplateColumns:`3.5rem repeat(${v(e).length}, minmax(0, 1fr))`},children:[(0,Z.jsx)(`span`,{}),v(e).map(e=>(0,Z.jsx)(`span`,{className:`text-[11px] font-semibold uppercase tracking-wide text-bambu-gray`,children:e.label},e.extruder)),m.diameters.map(n=>(0,Z.jsxs)(X.Fragment,{children:[(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray`,children:[n,`mm`]}),v(e).map(r=>{let i=t.filter(e=>(e.extruder_id??0)===r.extruder&&((e.nozzle_diameter??``).trim()||`0.4`)===n),a=J(e.printer.id,r.extruder,n),s=o.get(a);if(i.length===0)return(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray/50 px-2 py-1.5`,title:f(`inventory.noKProfilesMatch`),children:`—`},a);let c=dn(e,r.extruder),l=Y(s?.nozzle_id),u=!!s&&!kt(s.nozzle_id,c);return(0,Z.jsxs)(`select`,{title:u?f(`inventory.kProfileFlowMismatch`,{profile:Ot(l),fitted:Ot(c)}):void 0,"aria-label":`${e.printer.name} ${r.label} ${n}mm`,value:s?String(s.cali_idx):``,onChange:t=>{let a=i.find(e=>String(e.cali_idx)===t.target.value)??null;S(e.printer.id,r.extruder,n,a)},className:`min-w-0 px-2 py-1.5 bg-bambu-dark-secondary border rounded-lg text-sm text-white focus:outline-none focus:border-bambu-green ${u?`border-amber-500/60`:`border-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:f(`inventory.kProfileNotSet`)}),i.map(e=>{let t=Ot(Y(e.nozzle_id));return(0,Z.jsx)(`option`,{value:e.cali_idx,children:`${t?`[${t}] `:``}${e.name||e.filament_id} K=${e.k_value.toFixed(3)}`},e.cali_idx)})]},a)})]},n))]}):(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-1`,children:f(`inventory.printerOffline`)})]},e.printer.id)}):(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic`,children:f(`inventory.selectMaterialFirst`)})]})]})})]})]})}function hn(e){let t=U(e);return t?t.toLocaleDateString(`en-GB`,{day:`2-digit`,month:`2-digit`,year:`2-digit`})+` `+t.toLocaleTimeString(`en-GB`,{hour:`2-digit`,minute:`2-digit`}):``}var gn={completed:`text-bambu-green`,failed:`text-red-700 dark:text-red-400`,aborted:`text-yellow-700 dark:text-yellow-400`};function _n({spoolId:e}){let{t}=r(),n=u(),{showToast:a}=ee(),{data:o,isLoading:s}=d({queryKey:[`spool-usage`,e],queryFn:()=>i.getSpoolUsageHistory(e)}),c=f({mutationFn:()=>i.clearSpoolUsageHistory(e),onSuccess:()=>{n.invalidateQueries({queryKey:[`spool-usage`,e]}),a(t(`inventory.historyCleared`),`success`)}});return s?(0,Z.jsx)(`div`,{className:`flex justify-center py-4`,children:(0,Z.jsx)(F,{className:`w-5 h-5 animate-spin text-bambu-green`})}):!o||o.length===0?(0,Z.jsxs)(`div`,{className:`text-center py-4 text-bambu-gray text-sm`,children:[(0,Z.jsx)(Ee,{className:`w-5 h-5 mx-auto mb-2 opacity-50`}),t(`inventory.noUsageHistory`)]}):(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,Z.jsx)(`h4`,{className:`text-sm font-medium text-white`,children:t(`inventory.usageHistory`)}),(0,Z.jsxs)(H,{variant:`ghost`,size:`sm`,onClick:()=>c.mutate(),disabled:c.isPending,className:`text-xs text-bambu-gray hover:text-red-700 dark:hover:text-red-400`,children:[(0,Z.jsx)(et,{className:`w-3 h-3 mr-1`}),t(`inventory.clearHistory`)]})]}),(0,Z.jsx)(`div`,{className:`max-h-48 overflow-y-auto space-y-1`,children:o.map(e=>(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-2 rounded bg-bambu-dark/50 text-xs`,children:[(0,Z.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:hn(e.created_at)}),e.print_name&&(0,Z.jsx)(`span`,{className:`text-white ml-2 truncate`,title:e.print_name,children:e.print_name})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 flex-shrink-0 ml-2`,children:[(0,Z.jsxs)(`span`,{className:`text-white font-medium`,children:[e.weight_used.toFixed(1),`g`]}),(0,Z.jsxs)(`span`,{className:`text-bambu-gray`,children:[`(`,e.percent_used,`%)`]}),(0,Z.jsx)(`span`,{className:gn[e.status]||`text-bambu-gray`,children:e.status})]})]},e.id))})]})}var vn={tag_uid:null,tray_uuid:null,tag_type:null,data_origin:null};function yn({isOpen:e,onClose:t,spool:n,mode:a,printersWithCalibrations:o=[],currencySymbol:c,onSpoolsCreated:l,spoolmanMode:p=!1,spoolsQueryKey:m=[`inventory-spools`]}){let{t:h}=r(),g=u(),{showToast:_}=ee(),v=()=>Be(g,m),y=a===`edit`,b=a===`copy`,[x,S]=(0,X.useState)(lt),[C,w]=(0,X.useState)({}),[T,E]=(0,X.useState)(`filament`),[D,O]=(0,X.useState)(!1),[k,A]=(0,X.useState)(!1),[j,M]=(0,X.useState)(!1),[N,te]=(0,X.useState)(!1),[ne,I]=(0,X.useState)([]),[L,re]=(0,X.useState)(!1),[R,ie]=(0,X.useState)(!1),[ae,oe]=(0,X.useState)(1),[se,z]=(0,X.useState)(!1);(0,X.useEffect)(()=>{z(!1)},[e,n?.id]);let[B,ce]=(0,X.useState)(!1),[le,ue]=(0,X.useState)(!1),[de,fe]=(0,X.useState)([]),[V,he]=(0,X.useState)(new Set),[ge,ve]=(0,X.useState)(``),[ye,be]=(0,X.useState)([]),[xe,Se]=(0,X.useState)([]),[W,we]=(0,X.useState)([]),[Te,Ee]=(0,X.useState)([]),[De,Oe]=(0,X.useState)([]),[ke,je]=(0,X.useState)([]),[Me,Ne]=(0,X.useState)([]),[Pe,Ie]=(0,X.useState)(!1),[Le,Re]=(0,X.useState)(new Map),[ze,Ve]=(0,X.useState)(new Map),[He,Ue]=(0,X.useState)(``),We=o.length>0?o:Me,G=Le.size+ze.size,{data:qe=[],isLoading:Je,error:Ye}=d({queryKey:[`spoolman-inventory-filaments`],queryFn:()=>i.getSpoolmanInventoryFilaments(),enabled:p&&e,staleTime:6e4,retry:!1});(0,X.useEffect)(()=>{je(wt())},[]),(0,X.useEffect)(()=>{let t=!1;return e&&((async()=>{ue(!0);try{let[e,n]=await Promise.allSettled([(async()=>(await i.getCloudStatus()).is_authenticated?{connected:!0,presets:await i.getFilamentPresets()}:{connected:!1,presets:[]})(),(async()=>(await i.orcaCloudStatus()).connected?{connected:!0,presets:(await i.orcaCloudListProfiles()).filament}:{connected:!1,presets:[]})()]);if(t)return;let r=e.status===`fulfilled`&&e.value.connected,a=n.status===`fulfilled`&&n.value.connected,o=e.status===`fulfilled`?e.value.presets:[],s=n.status===`fulfilled`?n.value.presets:[];ce(r||a),fe([...o,...s]),he(new Set(s.map(e=>e.setting_id)))}catch(e){if(t)return;console.error(`Failed to fetch cloud presets:`,e),ce(!1)}finally{t||ue(!1)}})(),i.getSpoolCatalog().then(be).catch(console.error),i.getColorCatalog().then(Oe).catch(console.error),i.getLocalPresets().then(e=>we(e.filament)).catch(console.error),i.getBuiltinFilaments().then(Ee).catch(console.error),i.getLocations().then(e=>Se(e.map(e=>({id:e.id,name:e.name})))).catch(console.error),o.length===0&&(async()=>{Ie(!0);try{let e=await i.getPrinters(),n=await Promise.all(e.map(e=>i.getPrinterStatus(e.id).catch(()=>null))),r=await Promise.all(e.map(async(e,t)=>{let r=n[t],i=r?.connected??!1,a=[];return i&&(a=await Ct(e.id,r)),{printer:{...e,connected:i},calibrations:a,nozzles:r?.nozzles}}));t||Ne(r)}catch(e){console.error(`Failed to fetch printer calibrations:`,e)}finally{t||Ie(!1)}})()),()=>{t=!0}},[e,o.length]);let Xe=(0,X.useMemo)(()=>Tt(de,new Set,W,Te,V),[de,W,Te,V]),Ze=(0,X.useMemo)(()=>{let e=xt(de,W),t=De.map(e=>e.manufacturer?.trim()).filter(e=>!!e),n=new Set([...e,...t]);return Array.from(n).sort((e,t)=>e.localeCompare(t))},[de,W,De]),Qe=(0,X.useMemo)(()=>{let e=De.map(e=>e.material?.trim()).filter(e=>!!e),t=new Set([...Dt,...e]);return Array.from(t).sort((e,t)=>e.localeCompare(t))},[De]),$e=(0,X.useMemo)(()=>{let e=[];for(let t of De){let n=t.manufacturer?.trim(),r=t.material?.trim();n&&r&&e.push({brand:n,material:r})}for(let t of de){let n=Et(t.name);n.brand&&n.material&&e.push({brand:n.brand,material:n.material})}for(let t of W){let n=Et(t.name),r=t.filament_vendor?.trim()||n.brand,i=n.material;r&&i&&e.push({brand:r,material:i})}return e},[de,De,W]),et=(0,X.useMemo)(()=>{let e=new Map;for(let t of $e){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(n)||e.set(n,new Set),e.get(n).add(r)}return e},[$e]),tt=(0,X.useMemo)(()=>{let e=new Map;for(let t of $e){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(r)||e.set(r,new Set),e.get(r).add(n)}return e},[$e]),nt=(0,X.useMemo)(()=>bt(Ze,x.brand),[Ze,x.brand]),rt=(0,X.useMemo)(()=>bt(Qe,x.material),[Qe,x.material]),it=(0,X.useMemo)(()=>mt(nt,x.material,tt),[nt,x.material,tt]),at=(0,X.useMemo)(()=>mt(rt,x.brand,et),[rt,x.brand,et]),st=(0,X.useMemo)(()=>vt(x.slicer_filament,Xe),[x.slicer_filament,Xe]);(0,X.useEffect)(()=>{if(e){if(n){let e=n.rgba&&/^[0-9A-Fa-f]{8}$/.test(n.rgba)?n.rgba:`808080FF`;if(S({material:n.material||``,subtype:n.subtype||``,brand:n.brand||``,color_name:n.color_name_is_synthesized?``:n.color_name||``,rgba:e,extra_colors:n.extra_colors||``,effect_type:n.effect_type||``,label_weight:n.label_weight||1e3,core_weight:n.core_weight??250,core_weight_catalog_id:n.core_weight_catalog_id??null,weight_used:b?0:n.weight_used||0,slicer_filament:n.slicer_filament||``,note:n.note||``,cost_per_kg:n.cost_per_kg??null,category:n.category||``,low_stock_threshold_pct:n.low_stock_threshold_pct??null,material_number:n.material_number||``,location_id:n.location_id??null,last_dried_at:(()=>{let e=b?null:U(n.last_dried_at);return e?_e(e):``})(),spoolman_filament_id:null}),ve(n.slicer_filament_name||n.slicer_filament||``),n.k_profiles&&n.k_profiles.length>0){let e=new Map;for(let t of n.k_profiles){if(t.cali_idx===null||t.cali_idx===void 0)continue;let n=(t.nozzle_diameter||``).trim()||`0.4`,r=t.extruder??0;e.set(J(t.printer_id,r,n),{cali_idx:t.cali_idx,filament_id:``,setting_id:t.setting_id||``,name:t.name||``,k_value:t.k_value,n_coef:0,extruder_id:r,nozzle_diameter:n,nozzle_id:t.nozzle_type||``})}Re(e)}else Re(new Map);I((n.suppliers??[]).map(e=>({supplier_id:e.supplier_id,supplier_name:e.supplier_name,supplier_article_number:e.supplier_article_number??``,quoted_price_per_kg:e.quoted_price_per_kg,is_purchase_source:b?!1:e.is_purchase_source})))}else S(lt),ve(``),Re(new Map),I([]);ie(!1),oe(1),w({}),E(`filament`),Ue(``),Ve(new Map),O(!1),te(!1),A(b&&n?.core_weight_is_inherited===!1),M(!1),re(!1)}},[e,n,a,b]),(0,X.useEffect)(()=>{if(!e||!n)return;let t=!1;return(p?i.getSpoolmanFilamentPresets:i.getSpoolFilamentPresets)(n.id).then(e=>{if(t)return;let n=new Map;for(let t of e)n.set(ht(t.printer_model,t.nozzle_diameter||``),{code:t.slicer_filament||``,name:t.slicer_filament_name||``});Ve(n)}).catch(e=>{t||(console.error(`Failed to load filament preset overrides:`,e),_(h(`inventory.filamentPresetsLoadFailed`),`warning`))}),()=>{t=!0}},[e,n?.id,p]),(0,X.useEffect)(()=>{if(!e||!n||j||x.location_id!=null)return;let t=n.storage_location?.trim();if(!t||xe.length===0)return;let r=xe.find(e=>e.name.toLowerCase()===t.toLowerCase());r&&S(e=>e.location_id===r.id?e:{...e,location_id:r.id})},[e,n,xe,x.location_id,j]);let ft=(e,t)=>{let n=ot.has(e);p&&n&&x.spoolman_filament_id!==null&&_(h(`inventory.spoolmanFilamentUnlinked`),`info`),S(r=>({...r,[e]:t,...p&&n&&r.spoolman_filament_id!==null?{spoolman_filament_id:null}:{}})),e===`weight_used`&&O(!0),e===`core_weight`&&A(!0),e===`location_id`&&M(!0),e===`last_dried_at`&&te(!0),C[e]&&w(t=>({...t,[e]:void 0}))},pt=e=>{let t=e.material||``,n=e.name||``,r=t&&n.startsWith(t)?n.slice(t.length).trim():n,i=(e.color_hex??``).replace(`#`,``).toUpperCase(),a=/^[0-9A-F]{6}(?:[0-9A-F]{2})?$/.test(i)?i:`808080`,o=a.length===8?a:`${a}FF`;S(n=>({...n,spoolman_filament_id:e.id,material:t,subtype:r,brand:e.vendor?.name||``,rgba:o,color_name:e.color_name||``,label_weight:e.weight??n.label_weight})),_(h(`inventory.spoolmanFilamentSelected`),`success`)},_t=e=>{je(t=>gt(e,t))},St=f({mutationFn:e=>p?i.createSpoolmanInventorySpool(e):i.createSpool(e),onSuccess:async e=>{e?.id&&(!await Jt(e.id)||!await qt(e.id))||(await v(),l&&l([e]),_(h(`inventory.spoolCreated`),`success`),t())},onError:e=>{e instanceof s&&e.status===503?_(h(`inventory.spoolmanUnreachable`),`error`):_(h(`inventory.saveFailed`),`error`)}}),Ot=f({mutationFn:({data:e,qty:t})=>p?i.bulkCreateSpoolmanInventorySpools(e,t):i.bulkCreateSpools(e,t),onSuccess:async e=>{let n=p&&`created`in e?e:null,r=n?n.created:e;if(Le.size>0||ze.size>0)for(let e of r)await Jt(e.id);if(Kt)for(let e of r)await qt(e.id);await v(),l&&l(r),n&&n.failed_count>0?_(h(`inventory.spoolsPartiallyCreated`,{created:r.length,total:n.requested_count}),`warning`):_(h(`inventory.spoolsCreated`,{count:r.length}),`success`),t()},onError:e=>{e instanceof s&&e.status===503?_(h(`inventory.spoolmanUnreachable`),`error`):_(h(`inventory.saveFailed`),`error`)}}),kt=f({mutationFn:e=>p?i.updateSpoolmanInventorySpool(n.id,e):i.updateSpool(n.id,e),onSuccess:async()=>{n?.id&&(!await Jt(n.id)||!await qt(n.id))||(await v(),_(h(`inventory.spoolUpdated`),`success`),t())},onError:e=>{e instanceof s&&e.status===503?_(h(`inventory.spoolmanUnreachable`),`error`):_(h(`inventory.saveFailed`),`error`)}}),jt=f({mutationFn:()=>p?i.updateSpoolmanInventorySpool(n.id,vn):i.updateSpool(n.id,vn),onSuccess:async()=>{await v(),_(h(`inventory.rfidCleared`,`RFID tag cleared`),`success`),t()},onError:e=>{e instanceof s&&e.status===503?_(h(`inventory.spoolmanUnreachable`),`error`):_(h(`inventory.tagClearFailed`),`error`)}}),{data:Mt,isLoading:Pt}=d({queryKey:[`spool-assignments`],queryFn:()=>i.getAssignments(),enabled:e&&y&&!p}),{data:Ft,isLoading:It}=d({queryKey:[`spoolman-slot-assignments`,`all`],queryFn:()=>i.getSpoolmanSlotAssignments(),enabled:e&&y&&p}),Lt=(()=>{if(n)return p?Ft?.find(e=>e.spoolman_spool_id===n.id):Mt?.find(e=>e.spool_id===n.id)})(),Rt=p?It:Pt,{data:zt}=d({queryKey:[`inventory-spools`],queryFn:()=>i.getSpools(!0),enabled:e}),{data:Bt}=Ce({enabled:e}),{data:Vt}=d({queryKey:[`slicerPrinterModels`],queryFn:i.getSlicerPrinterModels,enabled:e,staleTime:1/0}),Ht=(()=>{let e=new Set;for(let t of zt??[]){let n=t.category?.trim();n&&e.add(n)}return Array.from(e).sort((e,t)=>e.localeCompare(t))})(),Ut=(()=>{let e=new Set;for(let t of zt??[]){let n=t.material_number?.trim();n&&e.add(n)}return Array.from(e).sort((e,t)=>e.localeCompare(t,void 0,{numeric:!0}))})(),Wt=Bt?.low_stock_threshold??20,Gt=f({mutationFn:async()=>{if(!Lt)throw Error(`No assignment`);if(p){if(!n)throw Error(`No spool`);await i.unassignSpoolmanSlot(n.id);return}await i.unassignSpool(Lt.printer_id,Lt.ams_id,Lt.tray_id)},onSuccess:async()=>{p?(await g.invalidateQueries({queryKey:[`spoolman-slot-assignments`,`all`]}),await g.invalidateQueries({queryKey:[`spoolman-slot-assignments`]})):await g.invalidateQueries({queryKey:[`spool-assignments`]}),_(h(`inventory.unassignSuccess`,`Spool unassigned`),`success`),t()},onError:e=>{_(e.message,`error`)}}),Kt=L||b,qt=async e=>{if(!Kt)return!0;let t=p?i.setSpoolmanSpoolSuppliers:i.setSpoolSuppliers;try{return await t(e,ne.map(e=>({supplier_id:e.supplier_id,supplier_article_number:e.supplier_article_number.trim()||null,quoted_price_per_kg:e.quoted_price_per_kg,is_purchase_source:e.is_purchase_source}))),!0}catch(e){return console.error(`SpoolFormModal.saveSupplierLinks failed:`,e),_(h(`inventory.suppliers.saveFailed`),`error`),!1}},Jt=async e=>{let t=p?i.saveSpoolmanKProfiles:i.saveSpoolKProfiles,n=p?i.saveSpoolmanFilamentPresets:i.saveSpoolFilamentPresets,r=[];for(let[e,t]of Le){let[n,i,a]=e.split(`:`);r.push({printer_id:parseInt(n),extruder:parseInt(i),nozzle_diameter:a||`0.4`,nozzle_type:Y(t.nozzle_id),k_value:t.k_value,name:t.name||null,cali_idx:t.cali_idx,setting_id:t.setting_id||null})}let a=[];for(let[e,t]of ze){let{model:n,diameter:r}=yt(e);n&&a.push({printer_model:n,nozzle_diameter:r,slicer_filament:t.code||null,slicer_filament_name:t.name||null})}try{await t(e,r)}catch(e){return console.error(`Failed to save K-profiles:`,e),_(h(`inventory.kProfileSaveFailed`),`warning`),!1}try{await n(e,a)}catch(e){return console.error(`Failed to save filament preset overrides:`,e),_(h(`inventory.filamentPresetSaveFailed`),`warning`),!1}return!0};if((0,X.useEffect)(()=>{if(!e)return;let n=e=>{e.key===`Escape`&&!se&&t()};return document.addEventListener(`keydown`,n),()=>document.removeEventListener(`keydown`,n)},[e,t,se]),!e)return null;let Yt=()=>{let e=ut(x,R,p,a);if(!e.isValid){w(e.errors),(e.errors.slicer_filament||e.errors.material||e.errors.brand||e.errors.subtype)&&E(`filament`);return}let t=st?.displayName||ge||null,n={material:x.material||null,subtype:x.subtype||null,brand:x.brand||null,color_name:x.color_name||null,rgba:x.rgba||null,extra_colors:x.extra_colors||null,effect_type:x.effect_type||null,label_weight:x.label_weight,...p?k?{core_weight:x.core_weight}:{}:{core_weight:x.core_weight,core_weight_catalog_id:x.core_weight_catalog_id},slicer_filament:x.slicer_filament||null,slicer_filament_name:t,nozzle_temp_min:null,nozzle_temp_max:null,note:x.note||null,cost_per_kg:x.cost_per_kg,category:x.category.trim()||null,low_stock_threshold_pct:x.low_stock_threshold_pct,material_number:x.material_number.trim()||null,...p?{spoolman_filament_id:x.spoolman_filament_id}:{}};(!y||D)&&(n.weight_used=x.weight_used),(!y||j)&&(n.location_id=x.location_id),y&&N&&(n.last_dried_at=x.last_dried_at?new Date(x.last_dried_at).toISOString():null),y?kt.mutate(n):ae>1?Ot.mutate({data:n,qty:ae}):St.mutate(n)},Xt=St.isPending||Ot.isPending||kt.isPending||jt.isPending||Gt.isPending;return(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:t}),(0,Z.jsxs)(`div`,{className:`relative w-full max-w-5xl mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Z.jsxs)(`h2`,{className:`text-lg font-semibold text-white flex items-baseline gap-2`,children:[h(y?`inventory.editSpool`:b?`inventory.copySpool`:`inventory.addSpool`),y&&n&&(0,Z.jsxs)(`span`,{className:`text-sm font-mono text-bambu-gray`,children:[`#`,n.id]})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(me,{className:`w-5 h-5`})})]}),a===`create`&&(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-2 border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(Ge,{className:`w-4 h-4 text-amber-600 dark:text-amber-400`}),(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:h(`inventory.quickAdd`)})]}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>{ie(!R),R||E(`filament`)},className:`relative inline-flex h-5 w-9 items-center rounded-full transition-colors ${R?`bg-bambu-green`:`bg-bambu-dark-tertiary`}`,children:(0,Z.jsx)(`span`,{className:`inline-block h-3.5 w-3.5 rounded-full bg-white transition-transform ${R?`translate-x-4`:`translate-x-0.5`}`})})]}),(0,Z.jsxs)(`div`,{className:`flex border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Z.jsxs)(`button`,{onClick:()=>E(`filament`),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${T===`filament`?`text-bambu-green border-b-2 border-bambu-green`:`text-bambu-gray hover:text-white`}`,children:[(0,Z.jsx)(Nt,{className:`w-4 h-4`}),h(`inventory.filamentInfoTab`)]}),!R&&(0,Z.jsxs)(`button`,{onClick:()=>E(`appearance`),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${T===`appearance`?`text-bambu-green border-b-2 border-bambu-green`:`text-bambu-gray hover:text-white`}`,children:[(0,Z.jsx)(Ke,{className:`w-4 h-4`}),h(`inventory.colorAndCostTab`)]}),!R&&(0,Z.jsxs)(`button`,{onClick:()=>E(`printers`),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${T===`printers`?`text-bambu-green border-b-2 border-bambu-green`:`text-bambu-gray hover:text-white`}`,children:[(0,Z.jsx)(Ge,{className:`w-4 h-4`}),h(`inventory.printersTab`),G>0&&(0,Z.jsx)(`span`,{className:`text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green`,children:G})]})]}),(0,Z.jsx)(`div`,{className:`p-4 overflow-y-auto flex-1`,style:{scrollbarGutter:`stable`},children:T===`filament`?(0,Z.jsxs)(`div`,{className:`space-y-6`,children:[p&&!y&&(0,Z.jsx)(`div`,{children:Ye?(0,Z.jsx)(`p`,{className:`text-sm text-red-700 dark:text-red-400 px-1`,children:h(`inventory.spoolmanCatalogLoadFailed`)}):(0,Z.jsx)(un,{filaments:qe,isLoading:Je,selectedId:x.spoolman_filament_id,onSelect:pt})}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.filamentInfo`)}),(0,Z.jsx)(q,{formData:x,updateField:ft,cloudAuthenticated:B,loadingCloudPresets:le,presetInputValue:ge,setPresetInputValue:ve,selectedPresetOption:st,filamentOptions:Xe,availableBrands:nt,availableMaterials:rt,suggestedBrands:it,suggestedMaterials:at,quickAdd:R,detailsRequired:!R&&!p&&a===`create`,quantity:ae,onQuantityChange:oe,errors:C})]})]}):T===`appearance`?(0,Z.jsxs)(`div`,{className:`space-y-6`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.color`)}),(0,Z.jsx)(ct,{formData:x,updateField:ft,recentColors:ke,onColorUsed:_t,catalogColors:De})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.additional`)}),(0,Z.jsx)(dt,{formData:x,updateField:ft,spoolCatalog:ye,currencySymbol:c,availableCategories:Ht,availableMaterialNumbers:Ut,availableLocations:xe,showLastDried:y,onCreateLocation:async e=>{try{let t=await i.createLocation({name:e});return Se(e=>[...e,{id:t.id,name:t.name}].sort((e,t)=>e.name.localeCompare(t.name))),await K(g),{id:t.id,name:t.name}}catch(e){return console.error(e),_((e instanceof Error?e.message:h(`locations.saveFailed`))||h(`locations.saveFailed`),`error`),null}},globalLowStockThreshold:Wt,spoolmanMode:p})]}),(0,Z.jsx)(`div`,{children:(0,Z.jsx)(ln,{links:ne,onChange:e=>{I(e),re(!0)},currencySymbol:c})}),y&&n&&!p&&(0,Z.jsx)(`div`,{children:(0,Z.jsx)(_n,{spoolId:n.id})})]}):(0,Z.jsx)(mn,{formData:x,printersWithCalibrations:We,filamentOptions:Xe,modelPresets:ze,setModelPresets:Ve,selectedProfiles:Le,setSelectedProfiles:Re,selectedGroupId:He,setSelectedGroupId:Ue,printerModels:Vt,isLoading:Pe})}),(0,Z.jsxs)(`div`,{className:`flex gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0`,children:[y&&(0,Z.jsxs)(`div`,{className:`flex gap-2 mr-auto`,children:[(0,Z.jsxs)(H,{variant:`secondary`,onClick:()=>jt.mutate(),disabled:Xt||!(n?.tag_uid||n?.tray_uuid),children:[(0,Z.jsx)(Fe,{className:`w-4 h-4`}),h(`inventory.clearRfid`,`Clear RFID Tag`)]}),Lt?(0,Z.jsxs)(H,{variant:`secondary`,onClick:()=>Gt.mutate(),disabled:Xt||Rt,children:[(0,Z.jsx)(pe,{className:`w-4 h-4`}),h(`inventory.unassignSpool`,`Unassign`)]}):(0,Z.jsxs)(H,{variant:`secondary`,onClick:()=>z(!0),disabled:Xt||Rt,children:[(0,Z.jsx)(P,{className:`w-4 h-4`}),h(`inventory.assignSpool`,`Assign Spool`)]})]}),(0,Z.jsxs)(`div`,{className:`flex gap-2 ml-auto`,children:[(0,Z.jsx)(H,{variant:`secondary`,onClick:t,children:h(`common.cancel`)}),(0,Z.jsx)(H,{onClick:Yt,disabled:Xt,children:Xt?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}),h(`common.saving`)]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(Ae,{className:`w-4 h-4`}),h(y?`common.save`:b?`inventory.copySpool`:`inventory.addSpool`)]})})]})]})]})]}),y&&n&&(0,Z.jsx)(At,{isOpen:se,onClose:()=>z(!1),spool:n,printerId:null,variant:`dialog`,spoolmanMode:p})]})}var bn=[{value:`ams_holder_74x33`,i18nKey:`amsHolderSmall`,fallbackLabel:`AMS holder — small (74 × 33 mm)`,fallbackHint:`Single label per page; matches the printable label from MakerWorld model 752566 (AMS Filament Label Holder).`},{value:`ams_holder_75x55`,i18nKey:`amsHolderLarge`,fallbackLabel:`AMS holder — large (75 × 55 mm)`,fallbackHint:`Single label per page; fits the cardstock-insert variant of the AMS Filament Label Holder. Roomy enough for swatch, brand, material, ID, and QR code.`},{value:`box_40x30`,i18nKey:`box40x30`,fallbackLabel:`Box label (40 × 30 mm)`,fallbackHint:`Single label per page; common DK/Brother roll size, good for filament-bag and storage-bin labels.`},{value:`box_62x29`,i18nKey:`box`,fallbackLabel:`Box label (62 × 29 mm)`,fallbackHint:`Single label per page; sized for Brother PT/QL and Dymo small labels.`},{value:`avery_l7160`,i18nKey:`averyL7160`,fallbackLabel:`Avery L7160 — A4 sheet (38.1 × 63.5 mm × 21)`,fallbackHint:`EU sheet stock; 21 labels per A4 page.`},{value:`avery_5160`,i18nKey:`avery5160`,fallbackLabel:`Avery 5160 — US Letter sheet (25.4 × 66.7 mm × 30)`,fallbackHint:`US sheet stock; 30 labels per Letter page.`}],xn={avery_l7160:21,avery_5160:30},Sn=[203,300,600],Cn={brand:{i18nKey:`brand`,fallback:`Brand`},material:{i18nKey:`material`,fallback:`Material and subtype`},hex:{i18nKey:`hex`,fallback:`Colour code`},name:{i18nKey:`name`,fallback:`Colour or filament name`},location:{i18nKey:`location`,fallback:`Storage location`},material_number:{i18nKey:`materialNumber`,fallback:`Material number`},temps:{i18nKey:`temps`,fallback:`Nozzle temperature`},weight:{i18nKey:`weight`,fallback:`Net weight`},note:{i18nKey:`note`,fallback:`Note`},added:{i18nKey:`added`,fallback:`Date added`},qr:{i18nKey:`qr`,fallback:`QR code`},spool_id:{i18nKey:`spoolId`,fallback:`Spool ID`}},wn=`bambuddy-label-print-options`,Tn={template:bn[0].value,fields:{},monochrome:!1,format:`pdf`,dpi:300};function En(){let e;try{e=JSON.parse(localStorage.getItem(wn)??`null`)}catch{return Tn}if(!e||typeof e!=`object`)return Tn;let t=e,n=bn.map(e=>e.value),r={};if(t.fields&&typeof t.fields==`object`)for(let[e,i]of Object.entries(t.fields))!n.includes(e)||!Array.isArray(i)||(r[e]=i.filter(e=>o.includes(e)));return{template:n.includes(t.template)?t.template:Tn.template,fields:r,monochrome:t.monochrome===!0,format:t.format===`png`?`png`:`pdf`,dpi:Sn.includes(t.dpi)?t.dpi:Tn.dpi}}function Dn(e){try{localStorage.setItem(wn,JSON.stringify(e))}catch{}}function On(e,t){return e.fields[t]??a}function kn(e){let t=window.URL.createObjectURL(e);if(!window.open(t,`_blank`)){let e=document.createElement(`a`);e.href=t,e.download=`bambuddy-labels.pdf`,document.body.appendChild(e),e.click(),document.body.removeChild(e)}setTimeout(()=>window.URL.revokeObjectURL(t),6e4)}function An(e,t){let n=window.URL.createObjectURL(e),r=document.createElement(`a`);r.href=n,r.download=`bambuddy-labels-${t}.${e.type===`application/zip`?`zip`:`png`}`,document.body.appendChild(r),r.click(),document.body.removeChild(r),setTimeout(()=>window.URL.revokeObjectURL(n),6e4)}function Q(e){return $e(e)}function jn(e){return Ne(e.color_name,e.rgba,e.color_name_is_synthesized)}function Mn(e){return`${jn(e)??`${e.material}${e.subtype?` ${e.subtype}`:``}`}${e.brand?` · ${e.brand}`:``}`}function Nn(e){return[jn(e),e.color_name,e.material,e.subtype,e.brand,`#${e.id}`].filter(Boolean).join(` `).toLowerCase()}function Pn(e){if(!e)return[1,0];let t=e.replace(/^#/,``).slice(0,6);if(t.length!==6)return[1,0];let n=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);if([n,r,i].some(Number.isNaN))return[1,0];let a=n/255,o=r/255,s=i/255,c=Math.max(a,o,s),l=Math.min(a,o,s),u=(c+l)/2,d=c-l;if((d===0?0:d/(1-Math.abs(2*u-1)))<.1)return[1,u];let f=0;return f=c===a?(o-s)/d%6:c===o?(s-a)/d+2:(a-o)/d+4,f*=60,f<0&&(f+=360),[0,f]}function Fn({isOpen:e,onClose:t,availableSpools:n,initialSelectedIds:s,spoolmanMode:c}){let{t:l}=r(),u=Ue(),{showToast:d}=ee(),[f,p]=(0,X.useState)(!1),[m,h]=(0,X.useState)(new Set),[g,_]=(0,X.useState)(``),[v,y]=(0,X.useState)(``),[b,x]=(0,X.useState)(`id`),[S,C]=(0,X.useState)(Tn),[w,T]=(0,X.useState)(`1`),[E,D]=(0,X.useState)(null),[O,k]=(0,X.useState)(!1),[A,j]=(0,X.useState)(null),M=(0,X.useRef)(null);(0,X.useEffect)(()=>{if(e){let e=new Set(n.map(e=>e.id));h(new Set(s.filter(t=>e.has(t)))),_(``),y(``),x(`id`),C(En()),T(`1`),p(!1)}},[e]);let N=(0,X.useMemo)(()=>{let e=[...n];return b===`color`?(e.sort((e,t)=>{let n=Pn(e.rgba),r=Pn(t.rgba);return n[0]===r[0]?n[1]===r[1]?e.id-t.id:n[1]-r[1]:n[0]-r[0]}),e):(e.sort((e,t)=>e.id-t.id),e)},[n,b]),te=(0,X.useMemo)(()=>{let e=new Set;for(let t of N)t.material&&e.add(t.material.toUpperCase());return[...e].sort()},[N]),P=(0,X.useMemo)(()=>{let e=g.trim().toLowerCase();return N.filter(t=>!(v&&(t.material||``).toUpperCase()!==v||e&&!Nn(t).includes(e)))},[N,g,v,u]),ne=P.length>0&&P.every(e=>m.has(e.id)),I=S.template,L=On(S,I),re=L.join(`,`),R=N.find(e=>m.has(e.id))?.id??null;function ie(e){M.current&&window.URL.revokeObjectURL(M.current),M.current=e,D(e)}if((0,X.useEffect)(()=>{if(!e||R===null){ie(null),j(null),k(!1);return}let t=new AbortController;k(!0);let n=window.setTimeout(async()=>{let e={spool_id:R,template:I,monochrome:S.monochrome,fields:re?re.split(`,`):[]};try{let n=c?await i.previewSpoolmanSpoolLabel(e,t.signal):await i.previewSpoolLabel(e,t.signal);if(t.signal.aborted)return;ie(window.URL.createObjectURL(n)),j(null)}catch(e){if(t.signal.aborted)return;ie(null),j(e instanceof Error?e.message:String(e))}finally{t.signal.aborted||k(!1)}},250);return()=>{window.clearTimeout(n),t.abort()}},[e,R,I,re,S.monochrome,c]),(0,X.useEffect)(()=>()=>{M.current&&window.URL.revokeObjectURL(M.current)},[]),!e)return null;let ae=m.size,oe=ae===0,z=xn[I],B=Number(w),ce=z===void 0||Number.isInteger(B)&&B>=1&&B<=z,le=bn.find(e=>e.value===I)??bn[0];function ue(e){C(t=>{let n={...t,...e};return Dn(n),n})}function de(e){let t=o.filter(t=>e.includes(t));ue({fields:{...S.fields,[I]:t}})}function fe(e){de(L.includes(e)?L.filter(t=>t!==e):[...L,e])}function V(e){h(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})}function pe(){h(e=>{let t=new Set(e);for(let e of P)t.add(e.id);return t})}function he(){h(e=>{let t=new Set(e);for(let e of P)t.delete(e.id);return t})}function ge(){h(new Set)}async function U(){if(oe||f||!ce)return;let e={spool_ids:N.filter(e=>m.has(e.id)).map(e=>e.id),template:I,monochrome:S.monochrome,starting_position:z===void 0?1:B,fields:L,format:S.format,dpi:S.dpi};p(!0);try{let n=c?await i.printSpoolmanSpoolLabels(e):await i.printSpoolLabels(e);S.format===`pdf`?kn(n):An(n,I),t()}catch(e){d(l(`inventory.labels.error`,`Could not generate labels: {{msg}}`,{msg:e instanceof Error?e.message:String(e)}),`error`)}finally{p(!1)}}let _e=e=>`px-2 py-0.5 text-xs rounded-full border transition ${e?`bg-bambu-green text-bambu-dark border-bambu-green`:`bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray`}`;return(0,Z.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-start sm:items-center justify-center p-4 overflow-y-auto`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:t}),(0,Z.jsxs)(`div`,{"data-testid":`label-template-picker-panel`,className:`relative w-full max-w-5xl bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] overflow-clip flex flex-col my-auto`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(Xe,{className:`w-5 h-5 text-bambu-green`}),(0,Z.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:l(`inventory.labels.title`,`Print spool labels`)}),ae>0&&(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[`(`,l(`inventory.labels.selectedCount`,`{{count}} selected`,{count:ae}),`)`]})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,"aria-label":l(`common.close`,`Close`),children:(0,Z.jsx)(me,{className:`w-5 h-5`})})]}),(0,Z.jsxs)(`div`,{className:`flex-1 min-h-0 overflow-y-auto md:overflow-hidden grid grid-cols-1 md:grid-cols-2`,children:[(0,Z.jsxs)(`div`,{className:`flex flex-col min-h-0 md:border-r border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`p-4 space-y-2 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`relative`,children:[(0,Z.jsx)(se,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none`}),(0,Z.jsx)(`input`,{type:`search`,value:g,onChange:e=>_(e.target.value),placeholder:l(`inventory.labels.searchPlaceholder`,`Search name, brand, or #ID`),className:`w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray focus:outline-none focus:border-bambu-green`})]}),te.length>1&&(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray mr-1`,children:l(`inventory.labels.filterByMaterial`,`Material:`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>y(``),className:`px-2 py-0.5 text-xs rounded-full border transition ${v===``?`bg-bambu-green text-bambu-dark border-bambu-green`:`bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray`}`,children:l(`inventory.labels.allMaterials`,`All`)}),te.map(e=>(0,Z.jsx)(`button`,{type:`button`,onClick:()=>y(e),className:`px-2 py-0.5 text-xs rounded-full border transition ${v===e?`bg-bambu-green text-bambu-dark border-bambu-green`:`bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray`}`,children:e},e))]}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray mr-1`,children:l(`inventory.labels.sortBy.label`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>x(`id`),className:`px-2 py-0.5 text-xs rounded-full border transition ${b===`id`?`bg-bambu-green text-bambu-dark border-bambu-green`:`bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray`}`,children:l(`inventory.labels.sortBy.id`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>x(`color`),className:`px-2 py-0.5 text-xs rounded-full border transition ${b===`color`?`bg-bambu-green text-bambu-dark border-bambu-green`:`bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray`}`,children:l(`inventory.labels.sortBy.color`)})]})]}),(0,Z.jsxs)(`div`,{className:`px-4 pt-3 pb-2 flex items-center justify-between gap-3 flex-wrap`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:l(`inventory.labels.pickSpools`,`Pick which spools to print labels for:`)}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 text-xs`,children:[(0,Z.jsx)(`button`,{type:`button`,onClick:ne?he:pe,disabled:P.length===0,className:`text-bambu-green hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed`,children:ne?l(`inventory.labels.deselectVisible`,`Deselect visible`):l(`inventory.labels.selectVisible`,`Select all visible ({{count}})`,{count:P.length})}),(0,Z.jsx)(`button`,{type:`button`,onClick:ge,disabled:ae===0,className:`text-bambu-gray hover:text-white hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed`,children:l(`inventory.labels.clearAll`,`Clear all`)})]})]}),(0,Z.jsx)(`div`,{className:`flex-1 overflow-y-auto px-2 pb-2 min-h-0 max-h-72 md:max-h-none`,children:P.length===0?(0,Z.jsx)(`div`,{className:`text-center text-sm text-bambu-gray py-6`,children:N.length===0?l(`inventory.labels.noSpoolsToShow`,`No spools to show. Adjust your filter and try again.`):l(`inventory.labels.noMatches`,`No spools match the current search or filter.`)}):(0,Z.jsx)(`ul`,{className:`space-y-0.5`,children:P.map(e=>{let t=m.has(e.id);return(0,Z.jsx)(`li`,{children:(0,Z.jsxs)(`label`,{className:`flex items-center gap-3 px-2 py-1.5 rounded hover:bg-bambu-dark-tertiary/50 cursor-pointer`,children:[t?(0,Z.jsx)(Ye,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Z.jsx)(Te,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`input`,{type:`checkbox`,checked:t,onChange:()=>V(e.id),className:`sr-only`}),(0,Z.jsx)(`span`,{className:`w-4 h-4 rounded border border-black/20 shrink-0`,style:Q(e.rgba)}),(0,Z.jsx)(`span`,{className:`flex-1 min-w-0 truncate text-sm text-white`,children:Mn(e)}),(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray shrink-0`,children:[`#`,e.id]})]})},e.id)})})})]}),(0,Z.jsxs)(`div`,{className:`md:overflow-y-auto p-4 space-y-4 border-t md:border-t-0 border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`space-y-1`,children:[(0,Z.jsx)(`label`,{htmlFor:`label-template`,className:`text-sm font-medium text-white`,children:l(`inventory.labels.template`,`Label size`)}),(0,Z.jsx)(`select`,{id:`label-template`,"data-testid":`label-template-select`,value:I,onChange:e=>ue({template:e.target.value}),className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,children:bn.map(e=>(0,Z.jsx)(`option`,{value:e.value,children:l(`inventory.labels.templates.${e.i18nKey}.label`,e.fallbackLabel)},e.value))}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:l(`inventory.labels.templates.${le.i18nKey}.hint`,le.fallbackHint)})]}),(0,Z.jsxs)(`div`,{className:`space-y-1`,children:[(0,Z.jsx)(`div`,{className:`text-sm font-medium text-white`,children:R===null?l(`inventory.labels.preview`,`Preview`):l(`inventory.labels.previewOf`,`Preview of #{{id}}`,{id:R})}),(0,Z.jsxs)(`div`,{"data-testid":`label-preview`,className:`relative flex items-center justify-center min-h-40 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary`,children:[R===null?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray text-center`,children:l(`inventory.labels.previewNoSelection`,`Select a spool to see its label.`)}):A?(0,Z.jsx)(`span`,{className:`text-sm text-red-400 text-center`,children:l(`inventory.labels.previewError`,`Could not render the preview: {{msg}}`,{msg:A})}):E?(0,Z.jsx)(`img`,{src:E,alt:l(`inventory.labels.previewAlt`,`Label preview`),className:`max-h-56 max-w-full object-contain bg-white shadow`}):null,O&&(0,Z.jsx)(F,{className:`absolute top-2 right-2 w-4 h-4 animate-spin text-bambu-green`})]}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:l(`inventory.labels.previewHint`,`Lines that don't fit on this label size are left out.`)})]}),(0,Z.jsxs)(`fieldset`,{className:`space-y-2`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between gap-2`,children:[(0,Z.jsx)(`legend`,{className:`text-sm font-medium text-white`,children:l(`inventory.labels.content`,`Print on the label`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>de(a),className:`text-xs text-bambu-green hover:underline`,children:l(`inventory.labels.resetFields`,`Reset to default`)})]}),(0,Z.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 gap-x-3 gap-y-1`,children:o.map(e=>{let t=L.includes(e),{i18nKey:n,fallback:r}=Cn[e];return(0,Z.jsxs)(`label`,{className:`flex items-center gap-2 py-0.5 cursor-pointer select-none`,children:[t?(0,Z.jsx)(Ye,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Z.jsx)(Te,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`input`,{type:`checkbox`,"data-testid":`label-field-${e}`,checked:t,onChange:()=>fe(e),className:`sr-only`}),(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:l(`inventory.labels.fields.${n}`,r)})]},e)})})]}),(0,Z.jsxs)(`div`,{className:`space-y-3`,children:[(0,Z.jsxs)(`label`,{className:`inline-flex items-center gap-2 cursor-pointer select-none`,children:[S.monochrome?(0,Z.jsx)(Ye,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Z.jsx)(Te,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`input`,{type:`checkbox`,checked:S.monochrome,onChange:e=>ue({monochrome:e.target.checked}),className:`sr-only`}),(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:l(`inventory.labels.monochrome`,`Monochrome (black & white printer)`)}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:l(`inventory.labels.monochromeHint`,`Drops the colour swatch and widens the text`)})]}),z!==void 0&&(0,Z.jsxs)(`div`,{className:`flex items-start gap-3`,children:[(0,Z.jsx)(`label`,{htmlFor:`label-starting-position`,className:`text-sm text-white whitespace-nowrap pt-1.5`,children:l(`inventory.labels.startingPosition`,`Starting label position`)}),(0,Z.jsx)(`input`,{id:`label-starting-position`,"data-testid":`label-starting-position`,type:`number`,min:1,max:z,step:1,value:w,onChange:e=>T(e.target.value),"aria-describedby":`label-starting-position-help`,className:`w-20 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green`}),(0,Z.jsx)(`div`,{id:`label-starting-position-help`,"data-testid":`label-starting-position-status`,className:`text-xs pt-1.5 ${ce?`text-bambu-gray`:`text-red-400`}`,children:ce?B===1?l(`inventory.labels.startingPositionFirst`,`Printing starts at position 1.`):l(`inventory.labels.startingPositionSkipped`,`Positions 1 through {{lastPosition}} will be left blank on the first sheet.`,{lastPosition:B-1}):l(`inventory.labels.startingPositionInvalid`,`Enter a whole number from 1 to {{capacity}}.`,{capacity:z})})]}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-white mr-1`,children:l(`inventory.labels.format`,`Output:`)}),(0,Z.jsx)(`button`,{type:`button`,"data-testid":`label-format-pdf`,"aria-pressed":S.format===`pdf`,onClick:()=>ue({format:`pdf`}),className:_e(S.format===`pdf`),children:`PDF`}),(0,Z.jsx)(`button`,{type:`button`,"data-testid":`label-format-png`,"aria-pressed":S.format===`png`,onClick:()=>ue({format:`png`}),className:_e(S.format===`png`),children:`PNG`}),S.format===`png`&&(0,Z.jsx)(`select`,{"data-testid":`label-dpi-select`,"aria-label":l(`inventory.labels.dpi`,`Resolution`),value:S.dpi,onChange:e=>ue({dpi:Number(e.target.value)}),className:`ml-2 px-2 py-0.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-xs focus:outline-none focus:border-bambu-green`,children:Sn.map(e=>(0,Z.jsxs)(`option`,{value:e,children:[e,` dpi`]},e))})]}),S.format===`png`&&(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:l(`inventory.labels.pngHint`,`For label printer software that takes images. Match your printer's resolution; several labels come as a ZIP.`)})]})]})]}),(0,Z.jsxs)(`div`,{className:`flex justify-end gap-2 px-5 py-3 border-t border-bambu-dark-tertiary`,children:[(0,Z.jsx)(H,{variant:`secondary`,onClick:t,disabled:f,children:l(`common.cancel`,`Cancel`)}),(0,Z.jsxs)(H,{"data-testid":`label-print-button`,onClick:U,disabled:oe||f||!ce,children:[f?(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}):S.format===`pdf`?(0,Z.jsx)(Xe,{className:`w-4 h-4`}):(0,Z.jsx)(Oe,{className:`w-4 h-4`}),S.format===`pdf`?l(`inventory.labels.createPdf`,`Create PDF`):l(`inventory.labels.downloadPng`,`Download PNG`)]})]})]})]})}function In({onClose:e,onImported:t}){let{t:n}=r(),[a,o]=(0,X.useState)(null),[s,c]=(0,X.useState)(!1),[l,u]=(0,X.useState)(null),[d,f]=(0,X.useState)(!1),[p,m]=(0,X.useState)(!1),[h,g]=(0,X.useState)(null),_=(0,X.useRef)(null),v=async e=>{o(e),u(null),g(null),f(!0);try{u(await i.importSpoolsCsvPreview(e))}catch(e){g(e instanceof Error?e.message:n(`inventory.csv.previewError`,`Could not read the CSV file`))}finally{f(!1)}},y=e=>{let t=e.target.files?.[0];t&&v(t)},b=e=>{e.preventDefault(),c(!1);let t=e.dataTransfer.files?.[0];t&&v(t)},x=async()=>{if(a){m(!0),g(null);try{t((await i.importSpoolsCsv(a)).created)}catch(e){g(e instanceof Error?e.message:n(`inventory.csv.importError`,`Import failed`)),m(!1)}}},S=e=>e===`valid`?(0,Z.jsx)(j,{className:`w-4 h-4 text-green-500 flex-shrink-0`}):e===`error`?(0,Z.jsx)(D,{className:`w-4 h-4 text-red-500 flex-shrink-0`}):(0,Z.jsx)(je,{className:`w-4 h-4 text-bambu-gray flex-shrink-0`}),C=l?.valid_count??0;return(0,Z.jsx)(`div`,{className:`fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4`,children:(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg w-full max-w-3xl border border-bambu-dark-tertiary flex flex-col max-h-[90vh]`,children:[(0,Z.jsxs)(`div`,{className:`p-4 border-b border-bambu-dark-tertiary flex items-center justify-between`,children:[(0,Z.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:n(`inventory.csv.modalTitle`,`Import spools from CSV`)}),(0,Z.jsx)(`button`,{onClick:e,className:`p-1 hover:bg-bambu-dark rounded`,children:(0,Z.jsx)(me,{className:`w-5 h-5 text-bambu-gray`})})]}),(0,Z.jsxs)(`div`,{className:`p-4 space-y-4 overflow-y-auto flex-1`,children:[(0,Z.jsxs)(`div`,{onDragOver:e=>{e.preventDefault(),c(!0)},onDragLeave:e=>{e.preventDefault(),c(!1)},onDrop:b,onClick:()=>_.current?.click(),className:`border-2 border-dashed rounded-lg p-6 text-center cursor-pointer transition-colors ${s?`border-bambu-green bg-bambu-green/10`:`border-bambu-dark-tertiary hover:border-bambu-green/50`}`,children:[(0,Z.jsx)(Me,{className:`w-9 h-9 mx-auto mb-2 ${s?`text-bambu-green`:`text-bambu-gray`}`}),a?(0,Z.jsxs)(`p`,{className:`text-white font-medium flex items-center justify-center gap-2`,children:[(0,Z.jsx)(be,{className:`w-4 h-4`}),` `,a.name]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`p`,{className:`text-white font-medium`,children:n(`inventory.csv.selectFile`,`Choose a CSV file or drag it here`)}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray/70 mt-1`,children:n(`inventory.csv.dragHint`,`Header: material (required), brand, subtype, color_name, rgba, …`)})]})]}),(0,Z.jsx)(`input`,{ref:_,type:`file`,accept:`.csv,text/csv`,className:`hidden`,onChange:y}),d&&(0,Z.jsxs)(`div`,{className:`flex items-center justify-center gap-2 text-bambu-gray py-4`,children:[(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}),n(`inventory.csv.parsing`,`Reading file…`)]}),h&&(0,Z.jsxs)(`div`,{className:`p-3 bg-red-50 dark:bg-red-500/10 border border-red-300 dark:border-red-500/30 rounded-lg flex items-start gap-3`,children:[(0,Z.jsx)(D,{className:`w-5 h-5 text-red-600 dark:text-red-400 mt-0.5 flex-shrink-0`}),(0,Z.jsx)(`p`,{className:`text-sm text-red-700 dark:text-red-300 break-words`,children:h})]}),l&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`div`,{className:`flex flex-wrap gap-3 text-sm`,children:[(0,Z.jsx)(`span`,{className:`px-2 py-1 rounded bg-green-50 dark:bg-green-500/10 text-green-700 dark:text-green-400`,children:n(`inventory.csv.validCount`,`{{count}} valid`,{count:l.valid_count})}),(0,Z.jsx)(`span`,{className:`px-2 py-1 rounded bg-red-50 dark:bg-red-500/10 text-red-700 dark:text-red-400`,children:n(`inventory.csv.errorCount`,`{{count}} error`,{count:l.error_count})}),(0,Z.jsx)(`span`,{className:`px-2 py-1 rounded bg-bambu-dark text-bambu-gray`,children:n(`inventory.csv.skippedCount`,`{{count}} skipped`,{count:l.skipped_count})})]}),l.warnings.length>0&&(0,Z.jsx)(`div`,{className:`p-3 bg-yellow-50 dark:bg-yellow-500/10 border border-yellow-300 dark:border-yellow-500/30 rounded-lg space-y-1`,children:l.warnings.map((e,t)=>(0,Z.jsx)(`p`,{className:`text-xs text-yellow-700 dark:text-yellow-300`,children:e},t))}),l.rows.length>0&&(0,Z.jsx)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`max-h-72 overflow-y-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full text-sm`,children:[(0,Z.jsx)(`thead`,{className:`bg-bambu-dark sticky top-0`,children:(0,Z.jsxs)(`tr`,{className:`text-left text-bambu-gray`,children:[(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:n(`inventory.csv.colRow`,`Row`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:n(`inventory.csv.colStatus`,`Status`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:n(`inventory.material`,`Material`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:n(`inventory.brand`,`Brand`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:n(`inventory.csv.colColor`,`Color`)})]})}),(0,Z.jsx)(`tbody`,{children:l.rows.map(e=>(0,Z.jsxs)(`tr`,{className:`border-t border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`td`,{className:`px-3 py-2 text-bambu-gray`,children:e.row_number}),(0,Z.jsx)(`td`,{className:`px-3 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[S(e.status),e.status===`error`&&e.reason&&(0,Z.jsx)(`span`,{className:`text-xs text-red-700 dark:text-red-400 break-words`,children:e.reason})]})}),(0,Z.jsx)(`td`,{className:`px-3 py-2 text-white`,children:e.material||`—`}),(0,Z.jsx)(`td`,{className:`px-3 py-2 text-white`,children:e.brand||`—`}),(0,Z.jsx)(`td`,{className:`px-3 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[e.rgba&&(0,Z.jsx)(`span`,{className:`inline-block w-4 h-4 rounded-full border border-bambu-dark-tertiary flex-shrink-0`,style:$e(e.rgba)}),(0,Z.jsx)(`span`,{className:`text-white`,children:e.color_name||`—`}),e.resolved_color&&!e.cross_material_color&&(0,Z.jsx)(`span`,{title:n(`inventory.csv.colorResolved`,`Color filled from catalog`),children:(0,Z.jsx)(ke,{className:`w-3.5 h-3.5 text-bambu-green flex-shrink-0`})}),e.cross_material_color&&(0,Z.jsx)(`span`,{title:n(`inventory.csv.colorCrossMaterial`,`Color taken from a different material — no exact match in catalog`),children:(0,Z.jsx)(V,{className:`w-3.5 h-3.5 text-yellow-500 flex-shrink-0`})}),e.duplicate_of_existing&&(0,Z.jsx)(`span`,{title:n(`inventory.csv.duplicateExisting`,`A spool with this material, brand and color already exists — it will still be imported as a new spool`),children:(0,Z.jsx)(N,{className:`w-3.5 h-3.5 text-amber-600 dark:text-amber-400 flex-shrink-0`})})]})})]},e.row_number))})]})})})]})]}),(0,Z.jsxs)(`div`,{className:`p-4 border-t border-bambu-dark-tertiary flex justify-end gap-2`,children:[(0,Z.jsx)(H,{variant:`secondary`,onClick:e,disabled:p,children:n(`common.cancel`)}),(0,Z.jsx)(H,{onClick:x,disabled:!l||C===0||p,children:p?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(F,{className:`w-4 h-4 mr-2 animate-spin`}),n(`inventory.csv.importing`,`Importing…`)]}):C>0?n(`inventory.csv.importValidRows`,`Import {{count}} valid rows`,{count:C}):n(`inventory.csv.noValidRows`,`No valid rows`)})]})]})})}function Ln({open:e,onClose:t}){let{t:n}=r(),{showToast:a}=ee(),o=u(),{data:c=[],isLoading:l,isError:f}=d({queryKey:Ze,queryFn:i.getSuppliers,enabled:e}),[p,m]=(0,X.useState)(``),[h,g]=(0,X.useState)(!1),[_,v]=(0,X.useState)(null),[y,b]=(0,X.useState)(``),[x,S]=(0,X.useState)(``),[C,T]=(0,X.useState)(``),[E,D]=(0,X.useState)(``),[O,k]=(0,X.useState)(!1),[A,j]=(0,X.useState)(null);if((0,X.useEffect)(()=>{if(!e)return;let n=e=>{e.key===`Escape`&&t()};return window.addEventListener(`keydown`,n),()=>window.removeEventListener(`keydown`,n)},[e,t]),!e)return null;let M=c.filter(e=>e.name.toLowerCase().includes(p.toLowerCase())),N=()=>{b(``),S(``),T(``),D(``)},te=()=>({name:y.trim(),website:x.trim()||null,customer_number:C.trim()||null,note:E.trim()||null}),P=async()=>{if(!y.trim()){a(n(`inventory.suppliers.nameRequired`),`error`);return}k(!0);try{await i.createSupplier(te()),await G(o),g(!1),N(),a(n(`inventory.suppliers.added`),`success`)}catch(e){console.error(`SuppliersModal.handleAdd failed:`,e),a(n(e instanceof s&&e.status===409?`inventory.suppliers.duplicateName`:`inventory.suppliers.addFailed`),`error`)}finally{k(!1)}},ne=e=>{v(e.id),b(e.name),S(e.website??``),T(e.customer_number??``),D(e.note??``)},I=()=>{v(null),N()},L=async e=>{if(!y.trim()){a(n(`inventory.suppliers.nameRequired`),`error`);return}k(!0);try{await i.updateSupplier(e,te()),await G(o),I(),a(n(`inventory.suppliers.updated`),`success`)}catch(e){console.error(`SuppliersModal.handleUpdate failed:`,e),a(n(e instanceof s&&e.status===409?`inventory.suppliers.duplicateName`:`inventory.suppliers.updateFailed`),`error`)}finally{k(!1)}},re=async()=>{if(A)try{await i.deleteSupplier(A.id),await G(o),a(n(`inventory.suppliers.deleted`),`success`)}catch(e){console.error(`SuppliersModal.handleDelete failed:`,e),e instanceof s&&e.status===409?a(n(`inventory.suppliers.deleteBlocked`,{count:A.spool_count}),`error`):a(n(`inventory.suppliers.deleteFailed`),`error`)}finally{j(null)}},R=(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsx)(`input`,{type:`text`,className:`w-full px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:n(`inventory.suppliers.namePlaceholder`),value:y,maxLength:200,onChange:e=>b(e.target.value)}),(0,Z.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Z.jsx)(`input`,{type:`text`,className:`flex-1 min-w-0 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:n(`inventory.suppliers.websitePlaceholder`),value:x,maxLength:500,onChange:e=>S(e.target.value)}),(0,Z.jsx)(`input`,{type:`text`,className:`w-40 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:n(`inventory.suppliers.customerNumberPlaceholder`),value:C,maxLength:100,onChange:e=>T(e.target.value)})]}),(0,Z.jsx)(`input`,{type:`text`,className:`w-full px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:n(`inventory.suppliers.notePlaceholder`),value:E,maxLength:500,onChange:e=>D(e.target.value)})]});return(0,Z.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 bg-black/60`,onClick:t}),(0,Z.jsxs)(`div`,{className:`relative w-full max-w-3xl mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col`,role:`dialog`,"aria-modal":`true`,"aria-labelledby":`suppliers-modal-title`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between gap-4 px-6 py-4 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(Rt,{className:`w-5 h-5 text-bambu-gray`}),(0,Z.jsx)(`h2`,{id:`suppliers-modal-title`,className:`text-lg font-semibold text-white`,children:n(`inventory.suppliers.title`)}),(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[`(`,c.length,`)`]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsxs)(`button`,{onClick:()=>{g(!0),v(null),N()},className:`px-3 py-1.5 text-sm bg-bambu-green text-white rounded-lg hover:bg-bambu-green/80 transition-colors flex items-center gap-1.5`,children:[(0,Z.jsx)(Je,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:n(`common.add`)})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors`,"aria-label":n(`common.close`),children:(0,Z.jsx)(me,{className:`w-5 h-5`})})]})]}),(0,Z.jsxs)(`div`,{className:`flex-1 min-h-0 overflow-y-auto px-6 py-4 space-y-4`,children:[(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:n(`inventory.suppliers.description`)}),(0,Z.jsxs)(`div`,{className:`relative`,children:[(0,Z.jsx)(se,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray`}),(0,Z.jsx)(`input`,{type:`text`,className:`w-full pl-10 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:n(`inventory.suppliers.search`),value:p,onChange:e=>m(e.target.value)})]}),h&&(0,Z.jsxs)(`div`,{className:`p-4 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary space-y-3`,children:[(0,Z.jsx)(`h3`,{className:`text-sm font-medium text-white`,children:n(`inventory.suppliers.addNew`)}),R,(0,Z.jsxs)(`div`,{className:`flex gap-2 justify-end`,children:[(0,Z.jsx)(`button`,{onClick:()=>{g(!1),N()},className:`px-3 py-2 rounded-lg text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary`,children:n(`common.cancel`)}),(0,Z.jsxs)(`button`,{onClick:P,disabled:O,className:`px-3 py-2 bg-bambu-green text-white rounded-lg hover:bg-bambu-green/80 flex items-center gap-1`,children:[O?(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(w,{className:`w-4 h-4`}),n(`common.add`)]})]})]}),l?(0,Z.jsxs)(`div`,{className:`flex items-center justify-center py-8 text-bambu-gray`,children:[(0,Z.jsx)(F,{className:`w-5 h-5 animate-spin mr-2`}),n(`common.loading`)]}):f?(0,Z.jsxs)(`div`,{className:`flex items-center justify-center gap-2 py-8 text-sm text-red-400`,children:[(0,Z.jsx)(V,{className:`w-4 h-4`}),n(`inventory.suppliers.loadFailed`)]}):(0,Z.jsx)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg`,children:(0,Z.jsxs)(`table`,{className:`w-full text-sm`,children:[(0,Z.jsx)(`thead`,{className:`bg-bambu-dark sticky top-0`,children:(0,Z.jsxs)(`tr`,{children:[(0,Z.jsx)(`th`,{className:`px-4 py-2 text-left text-bambu-gray font-medium`,children:n(`common.name`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 text-left text-bambu-gray font-medium hidden sm:table-cell`,children:n(`inventory.suppliers.customerNumber`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 text-right text-bambu-gray font-medium w-20`,children:n(`inventory.suppliers.spools`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 w-24`})]})}),(0,Z.jsx)(`tbody`,{children:M.length===0?(0,Z.jsx)(`tr`,{children:(0,Z.jsx)(`td`,{colSpan:4,className:`px-4 py-8 text-center text-bambu-gray`,children:n(p?`inventory.suppliers.noMatch`:`inventory.suppliers.empty`)})}):M.map(e=>(0,Z.jsx)(`tr`,{className:`border-t border-bambu-dark-tertiary hover:bg-bambu-dark align-top`,children:_===e.id?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`td`,{className:`px-4 py-2`,colSpan:3,children:R}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex justify-end gap-1`,children:[(0,Z.jsx)(`button`,{onClick:()=>L(e.id),disabled:O,className:`p-1.5 rounded hover:bg-green-500/20 text-green-500`,children:O?(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(w,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:I,className:`p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray`,children:(0,Z.jsx)(me,{className:`w-4 h-4`})})]})})]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`td`,{className:`px-4 py-2 text-white`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{children:e.name}),e.website&&(0,Z.jsx)(`a`,{href:e.website,target:`_blank`,rel:`noopener noreferrer`,className:`text-bambu-gray hover:text-bambu-green`,title:e.website,children:(0,Z.jsx)(De,{className:`w-3.5 h-3.5`})})]}),e.note&&(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-0.5`,children:e.note})]}),(0,Z.jsx)(`td`,{className:`px-4 py-2 text-bambu-gray hidden sm:table-cell`,children:e.customer_number||`-`}),(0,Z.jsx)(`td`,{className:`px-4 py-2 text-right font-mono text-bambu-gray`,children:e.spool_count}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex justify-end gap-1`,children:[(0,Z.jsx)(`button`,{onClick:()=>{ne(e),g(!1)},className:`p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white`,children:(0,Z.jsx)(Ve,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>j(e),className:`p-1.5 rounded bg-red-500/10 hover:bg-red-500/20 text-red-500`,children:(0,Z.jsx)(et,{className:`w-4 h-4`})})]})})]})},e.id))})]})})]})]}),A&&(0,Z.jsx)(Se,{title:n(`inventory.suppliers.deleteTitle`),message:A.spool_count>0?n(`inventory.suppliers.deleteConfirmReferenced`,{name:A.name,count:A.spool_count}):n(`inventory.suppliers.deleteConfirm`,{name:A.name}),confirmText:n(`common.delete`),variant:`danger`,onConfirm:re,onCancel:()=>j(null)})]})}var Rn=[{id:`material`,type:`searchable`,labelKey:`inventory.material`},{id:`subtype`,type:`searchable`,labelKey:`inventory.subtype`},{id:`brand`,type:`searchable`,labelKey:`inventory.brand`},{id:`color_name`,type:`text`,labelKey:`inventory.colorName`},{id:`rgba`,type:`color`,labelKey:`inventory.color`,pattern:`^[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$`},{id:`location_id`,type:`searchableClosed`,labelKey:`inventory.storageLocation`},{id:`slicer_filament_name`,type:`searchable`,labelKey:`inventory.slicerFilamentName`},{id:`slicer_filament`,type:`searchable`,labelKey:`inventory.slicerFilament`},{id:`cost_per_kg`,type:`number`,labelKey:`inventory.costPerKg`,min:0,step:.01},{id:`note`,type:`textarea`,labelKey:`inventory.note`},{id:`label_weight`,type:`number`,labelKey:`inventory.labelWeight`,min:1,step:1},{id:`core_weight`,type:`number`,labelKey:`inventory.coreWeight`,min:0,step:1},{id:`category`,type:`searchable`,labelKey:`inventory.category`,internalOnly:!0},{id:`low_stock_threshold_pct`,type:`number`,labelKey:`inventory.lowStockThresholdOverride`,min:1,max:99,step:1,internalOnly:!0},{id:`material_number`,type:`searchable`,labelKey:`inventory.materialNumber`,internalOnly:!0}];function zn({value:e,onChange:t,options:n,allowCustom:i,placeholderKey:a,disabled:o}){let{t:s}=r(),c=(0,X.useRef)(null),[l,u]=(0,X.useState)(!1),[d,f]=(0,X.useState)(``);(0,X.useEffect)(()=>{if(!l)return;let e=e=>{c.current&&!c.current.contains(e.target)&&(u(!1),f(``))},t=e=>{e.key===`Escape`&&(u(!1),f(``))};return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t)}},[l]);let p=l?d:n.find(t=>t.value===e)?.label??e,m=(0,X.useMemo)(()=>{if(!l)return n;let e=d.trim().toLowerCase();return e?n.filter(t=>t.label.toLowerCase().includes(e)||t.value.toLowerCase().includes(e)):n},[l,d,n]),h=l&&d.trim()&&!n.some(e=>e.value.toLowerCase()===d.trim().toLowerCase());return(0,Z.jsxs)(`div`,{className:`relative`,ref:c,children:[(0,Z.jsx)(`input`,{type:`text`,disabled:o,value:p,onChange:e=>{f(e.target.value),u(!0),i&&t(e.target.value)},onFocus:()=>{u(!0),f(``)},placeholder:a?s(a):void 0,className:`w-full px-3 py-2 pr-9 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none`}),(0,Z.jsx)(T,{className:`absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none`}),l&&(0,Z.jsxs)(`div`,{className:`absolute z-50 left-0 right-0 mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto`,children:[m.length===0&&!i&&(0,Z.jsx)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray`,children:s(`inventory.noResults`)}),m.map(n=>(0,Z.jsx)(`button`,{type:`button`,className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${e===n.value?`bg-bambu-green/10 text-bambu-green`:`text-white`}`,onClick:()=>{t(n.value),u(!1),f(``)},children:n.label},n.value)),i&&h&&(0,Z.jsx)(`button`,{type:`button`,className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary`,onClick:()=>{t(d.trim()),u(!1),f(``)},children:s(`inventory.bulk.useCustom`,{value:d.trim()})})]})]})}function Bn(...e){let t=new Set;for(let n of e)for(let e of n){let n=e?.trim();n&&t.add(n)}return Array.from(t).sort((e,t)=>e.localeCompare(t))}function Vn({isOpen:e,selectedCount:t,isPending:n,availableLocations:a,availableMaterials:o,availableSubtypes:s,availableBrands:c,availableCategories:l,availableMaterialNumbers:u,availableSlicerFilaments:f,availableSlicerFilamentNames:p,spoolmanMode:m=!1,onClose:h,onApply:g}){let{t:_}=r(),v=(0,X.useMemo)(()=>Rn.filter(e=>!(m&&e.internalOnly)),[m]),{data:y=[]}=d({queryKey:[`bulk-edit-cloud-presets`],enabled:e,staleTime:300*1e3,queryFn:async()=>{let e=[];try{if((await i.getCloudStatus()).is_authenticated){let t=await i.getFilamentPresets();e.push(...t)}}catch{}try{if((await i.orcaCloudStatus()).connected){let t=await i.orcaCloudListProfiles();e.push(...t.filament)}}catch{}return e}}),{data:b}=d({queryKey:[`bulk-edit-local-presets`],enabled:e,staleTime:300*1e3,queryFn:i.getLocalPresets}),{data:x=[]}=d({queryKey:[`builtin-filaments`],enabled:e,staleTime:300*1e3,queryFn:i.getBuiltinFilaments}),S=(0,X.useMemo)(()=>Tt(y,new Set,b?.filament??[],x),[y,b,x]),[C,w]=(0,X.useState)({}),T=(0,X.useMemo)(()=>Bn(Dt,o).map(e=>({value:e,label:e})),[o]),E=(0,X.useMemo)(()=>Bn(ft,s).map(e=>({value:e,label:e})),[s]),D=(0,X.useMemo)(()=>Bn(_t,c).map(e=>({value:e,label:e})),[c]),O=(0,X.useMemo)(()=>Bn(l).map(e=>({value:e,label:e})),[l]),k=(0,X.useMemo)(()=>Bn(u).map(e=>({value:e,label:e})),[u]),A=(0,X.useMemo)(()=>{let e=S.map(e=>({value:e.code,label:e.displayName})),t=f.filter(t=>!e.some(e=>e.value===t)).map(e=>({value:e,label:e}));return[...e,...t].sort((e,t)=>e.label.localeCompare(t.label))},[S,f]),ee=(0,X.useMemo)(()=>{let e=S.map(e=>({value:e.displayName,label:e.displayName})),t=p.filter(t=>!e.some(e=>e.value===t)).map(e=>({value:e,label:e}));return[...e,...t].sort((e,t)=>e.label.localeCompare(t.label))},[S,p]),j=(0,X.useMemo)(()=>a.map(e=>({value:String(e.id),label:e.name})),[a]);if(!e)return null;let M=(e,t)=>{w(n=>({...n,[e]:t}))},N=e=>{w(t=>{let n={...t};return delete n[e],n})},te=(()=>{let e={};for(let t of v){let n=C[t.id];if(n===void 0)continue;let r=typeof n==`string`?n.trim():n;if(!(r===``||r===null))if(t.type===`number`){let n=Number(r);Number.isFinite(n)&&(e[t.id]=n)}else if(t.id===`location_id`){let n=Number(r);Number.isFinite(n)&&n>0&&(e[t.id]=n)}else if(t.id===`rgba`){let n=String(r).replace(/^#/,``),i=n.length===6?`${n}FF`:n;/^[0-9A-Fa-f]{8}$/.test(i)&&(e[t.id]=i.toUpperCase())}else e[t.id]=String(r)}return e})(),P=Object.keys(te).length>0,ne=v.some(e=>{let t=C[e.id];return t===void 0||t.trim()===``?!1:te[e.id]===void 0}),I=e=>e===`material`?T:e===`subtype`?E:e===`brand`?D:e===`category`?O:e===`material_number`?k:e===`slicer_filament`?A:e===`slicer_filament_name`?ee:e===`location_id`?j:[],L=e=>{let t=C[e.id]??``;if(e.type===`searchable`||e.type===`searchableClosed`)return(0,Z.jsx)(zn,{value:t,onChange:t=>{t===``?N(e.id):M(e.id,t)},options:I(e.id),allowCustom:e.type===`searchable`,disabled:n});if(e.type===`textarea`)return(0,Z.jsx)(`textarea`,{disabled:n,value:t,onChange:t=>M(e.id,t.target.value),className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none resize-none min-h-[60px]`});if(e.type===`color`){let r=t.trim().replace(/^#/,``),i=r.length===6?`${r}FF`:r,a=t.trim()!==``&&!/^[0-9A-Fa-f]{8}$/.test(i);return(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`input`,{type:`color`,disabled:n,value:`#${(t||`808080`).replace(/^#/,``).slice(0,6)}`,onChange:t=>M(e.id,t.target.value.replace(/^#/,``).toUpperCase()),className:`h-9 w-12 rounded cursor-pointer`}),(0,Z.jsx)(`input`,{type:`text`,disabled:n,value:t,onChange:t=>M(e.id,t.target.value.replace(/^#/,``).toUpperCase()),placeholder:`RRGGBB or RRGGBBAA`,className:`flex-1 px-3 py-2 bg-bambu-dark border rounded-lg text-white placeholder-bambu-gray/50 focus:outline-none ${a?`border-red-500 focus:border-red-500`:`border-bambu-dark-tertiary focus:border-bambu-green`}`,pattern:e.pattern})]}),a&&(0,Z.jsx)(`p`,{className:`mt-1 text-xs text-red-600 dark:text-red-400`,children:_(`inventory.bulk.invalidHex`)})]})}return(0,Z.jsx)(`input`,{type:e.type===`number`?`number`:`text`,disabled:n,value:t,onChange:t=>M(e.id,t.target.value),min:e.min,max:e.max,step:e.step,className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none`})};return(0,Z.jsx)(`div`,{className:`fixed inset-0 bg-black/50 flex items-center justify-center p-4 z-50`,onClick:n?void 0:h,children:(0,Z.jsxs)(`div`,{className:`w-full max-w-3xl bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg max-h-[90vh] flex flex-col`,onClick:e=>e.stopPropagation(),children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-5 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:_(`inventory.bulk.editTitle`)}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray mt-0.5`,children:_(`inventory.bulk.editSubtitle`,{count:t})})]}),(0,Z.jsx)(`button`,{onClick:h,disabled:n,className:`p-1 text-bambu-gray hover:text-white transition-colors`,"aria-label":_(`common.close`),children:(0,Z.jsx)(me,{className:`w-5 h-5`})})]}),(0,Z.jsx)(`p`,{className:`px-5 pt-4 text-xs text-bambu-gray`,children:_(`inventory.bulk.editHint`)}),(0,Z.jsx)(`div`,{className:`flex-1 overflow-y-auto p-5 space-y-3`,children:v.map(e=>{let t=C[e.id]!==void 0;return(0,Z.jsxs)(`div`,{className:`flex items-start gap-3 rounded-md p-2 transition-colors ${t?`bg-bambu-green/5 border border-bambu-green/30`:`border border-transparent`}`,children:[(0,Z.jsx)(`div`,{className:`pt-2`,children:(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,checked:t,onChange:t=>{t.target.checked?M(e.id,``):N(e.id)},"aria-label":_(`inventory.bulk.toggleField`)})}),(0,Z.jsxs)(`div`,{className:`flex-1`,children:[(0,Z.jsx)(`label`,{className:`block text-sm text-bambu-gray mb-1`,children:_(e.labelKey)}),L(e)]})]},e.id)})}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 p-5 border-t border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:_(`inventory.bulk.changeCount`,{count:Object.keys(te).length})}),(0,Z.jsxs)(`div`,{className:`ml-auto flex gap-2`,children:[(0,Z.jsx)(H,{variant:`secondary`,onClick:h,disabled:n,children:_(`common.cancel`)}),(0,Z.jsx)(H,{onClick:()=>g(te),disabled:!P||n||ne,children:n?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(F,{className:`w-4 h-4 mr-2 animate-spin`}),_(`inventory.bulk.applyPending`)]}):_(`inventory.bulk.applyButton`,{count:t})})]})]})]})})}function Hn(e){let t=e[0],n=0,r=0,i=0;for(let t of e)n+=t.label_weight,r+=t.weight_used,i+=t.core_weight;return{...t,label_weight:n,weight_used:r,core_weight:i}}function Un(e){let t=new Set;for(let n of e)if(typeof n==`string`){let e=n.trim();e&&t.add(e)}return Array.from(t).sort((e,t)=>e.localeCompare(t))}function Wn(e){return`${e.material}|${e.subtype||``}|${e.brand||``}|${e.color_name||``}|${e.rgba||``}|${e.extra_colors||``}|${e.effect_type||``}|${e.label_weight}|${e.last_dried_at||``}`}var Gn=`bambuddy-inventory-columns`,Kn=`none`.padStart(65,`_`);function qn(e){return(e||``).toLowerCase().replace(/\d+/g,e=>e.padStart(64,`0`))}var Jn=[{id:`id`,label:`#`,visible:!0},{id:`added_time`,label:`Added`,visible:!0},{id:`encode_time`,label:`Encoded`,visible:!1},{id:`last_used_time`,label:`Last Used`,visible:!1},{id:`rgba`,label:`Color`,visible:!0},{id:`material`,label:`Material`,visible:!0},{id:`subtype`,label:`Subtype`,visible:!0},{id:`color_name`,label:`Color Name`,visible:!1},{id:`brand`,label:`Brand`,visible:!0},{id:`slicer_filament`,label:`Slicer Filament`,visible:!1},{id:`location`,label:`Location`,visible:!0},{id:`storage_location`,label:`Storage Location`,visible:!1},{id:`temperature`,label:`Temperature`,visible:!1},{id:`humidity`,label:`Humidity`,visible:!1},{id:`last_dried`,label:`Last Dried`,visible:!0},{id:`battery`,label:`Battery`,visible:!1},{id:`label_weight`,label:`Label`,visible:!0},{id:`net`,label:`Net`,visible:!0},{id:`gross`,label:`Gross`,visible:!1},{id:`used`,label:`Used`,visible:!1},{id:`printed_total`,label:`Printed Total`,visible:!1},{id:`printed_since_weight`,label:`Printed Since Weight`,visible:!1},{id:`note`,label:`Note`,visible:!1},{id:`material_number`,label:`Material No.`,visible:!1},{id:`suppliers`,label:`Suppliers`,visible:!1},{id:`pa_k`,label:`PA(K)`,visible:!0},{id:`tag_id`,label:`Tag ID`,visible:!1},{id:`data_origin`,label:`Data Origin`,visible:!1},{id:`tag_type`,label:`Linked Tag Type`,visible:!1},{id:`stock`,label:`Stock`,visible:!1},{id:`remaining`,label:`Remaining`,visible:!0},{id:`spool_name`,label:`Spool`,visible:!1},{id:`cost_per_kg`,label:`Cost/kg`,visible:!1},{id:`weight_check`,label:`Weight Check`,visible:!1}];function Yn(){try{let e=localStorage.getItem(Gn);if(e){let t=JSON.parse(e),n=new Set(Jn.map(e=>e.id)),r=new Set(t.map(e=>e.id)),i=[...t.filter(e=>n.has(e.id))];for(let e of Jn){if(r.has(e.id))continue;let t=Jn.indexOf(e),n=i.length;for(let e=t-1;e>=0;e--){let t=i.findIndex(t=>t.id===Jn[e].id);if(t!==-1){n=t+1;break}}i.splice(n,0,e)}return i}}catch{}return Jn.map(e=>({...e}))}function Xn(e){try{localStorage.setItem(Gn,JSON.stringify(e))}catch{}}function Zn(e,t=!1){return t&&e>=1e3?`${(e/1e3).toFixed(1)}kg`:`${Math.round(e)}g`}var Qn={PLA:`bg-green-100 dark:bg-green-500/20 text-green-700 dark:text-green-400`,ABS:`bg-red-100 dark:bg-red-500/20 text-red-700 dark:text-red-400`,PETG:`bg-blue-100 dark:bg-blue-500/20 text-blue-700 dark:text-blue-400`,TPU:`bg-purple-100 dark:bg-purple-500/20 text-purple-700 dark:text-purple-400`,ASA:`bg-orange-100 dark:bg-orange-500/20 text-orange-700 dark:text-orange-400`,PA:`bg-yellow-100 dark:bg-yellow-500/20 text-yellow-700 dark:text-yellow-400`,PC:`bg-cyan-100 dark:bg-cyan-500/20 text-cyan-700 dark:text-cyan-400`,PET:`bg-sky-100 dark:bg-sky-500/20 text-sky-700 dark:text-sky-400`};function $n(e){let t=[];return e.last_dried_temp!=null&&t.push(`${e.last_dried_temp}°C`),e.last_dried_hours!=null&&t.push(`${Number(e.last_dried_hours.toFixed(1))}h`),t.join(` · `)}function er(e,t=`system`){if(!e)return`-`;let n=U(e);return n?ge(n,t):`-`}var tr={id:()=>`#`,added_time:()=>`Added`,encode_time:()=>`Encoded`,last_used_time:()=>`Last Used`,rgba:e=>e(`inventory.color`),material:e=>e(`inventory.material`),subtype:e=>e(`inventory.subtype`),color_name:e=>e(`inventory.colorName`),brand:e=>e(`inventory.brand`),slicer_filament:e=>e(`inventory.slicerFilament`),location:()=>`Location`,storage_location:e=>e(`inventory.storageLocation`),temperature:e=>e(`inventory.temperature`),humidity:e=>e(`inventory.humidity`),last_dried:e=>e(`inventory.lastDried`),battery:e=>e(`inventory.battery`),label_weight:e=>e(`inventory.labelWeight`),net:e=>e(`inventory.net`),gross:()=>`Gross`,used:e=>e(`inventory.weightUsed`),printed_total:()=>`Printed Total`,printed_since_weight:()=>`Printed Since Weight`,note:e=>e(`inventory.note`),material_number:e=>e(`inventory.materialNumber`),suppliers:e=>e(`inventory.suppliers.label`),pa_k:()=>`PA(K)`,tag_id:()=>`Tag ID`,data_origin:()=>`Data Origin`,tag_type:()=>`Linked Tag Type`,stock:e=>e(`inventory.stock`),remaining:e=>e(`inventory.remaining`),spool_name:e=>e(`inventory.spoolName`),cost_per_kg:e=>e(`inventory.costPerKg`),weight_check:e=>e(`inventory.weightCheck`)},nr={id:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm font-medium text-white`,children:e.id}),added_time:({spool:e,dateFormat:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:er(e.created_at,t)}),encode_time:({spool:e,dateFormat:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:er(e.encode_time,t)}),last_used_time:({spool:e,dateFormat:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.last_used?er(e.last_used,t):`Never`}),last_dried:({spool:e,dateFormat:t})=>{if(!e.last_dried_at)return(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`});let n=$n(e);return(0,Z.jsxs)(`div`,{className:`text-sm text-bambu-gray`,children:[(0,Z.jsx)(`div`,{children:er(e.last_dried_at,t)}),n&&(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray/60`,children:n})]})},rgba:({spool:e})=>(0,Z.jsx)(`div`,{className:`flex items-center justify-center`,children:(0,Z.jsx)(Pe,{rgba:e.rgba,extraColors:e.extra_colors,effectType:e.effect_type,effectSize:`table`,subtype:e.subtype})}),material:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:e.material}),subtype:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.subtype||`-`}),color_name:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Ne(e.color_name,e.rgba,e.color_name_is_synthesized)||`-`}),brand:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.brand||`-`}),slicer_filament:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,title:e.slicer_filament||void 0,children:e.slicer_filament_name||e.slicer_filament||`-`}),location:({spool:e,assignmentMap:t})=>{let n=t[e.id];if(!n)return(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`});let r=n.printer_name||`Printer ${n.printer_id}`,i=n.ams_id===254||n.ams_id===255,a=!i&&n.ams_id>=128;return(0,Z.jsxs)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-purple-100 dark:bg-purple-500/20 text-purple-700 dark:text-purple-400`,children:[r,` `,Le(n.ams_id,n.tray_id,a,i),n.ams_label?` (${n.ams_label})`:``]})},storage_location:({spool:e})=>e.storage_location?(0,Z.jsx)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-blue-100 dark:bg-blue-500/20 text-blue-700 dark:text-blue-400`,children:e.storage_location}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}),temperature:({spool:e,locationReadingsMap:t,t:n,colorizeLocationSensors:r,locationSensorAboveColor:i,locationSensorBelowColor:a,locationSensorOptimalColor:o})=>{let s=e.location_id?t[e.location_id]?.find(e=>e.device_class===`temperature`):void 0;return s?(0,Z.jsx)(`span`,{title:s.name,className:`text-sm ${$(s,r,i,a,o)}`,children:pr(s,n)}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},humidity:({spool:e,locationReadingsMap:t,t:n,colorizeLocationSensors:r,locationSensorAboveColor:i,locationSensorBelowColor:a,locationSensorOptimalColor:o})=>{let s=e.location_id?t[e.location_id]?.find(e=>e.device_class===`humidity`):void 0;return s?(0,Z.jsx)(`span`,{title:s.name,className:`text-sm ${$(s,r,i,a,o)}`,children:pr(s,n)}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},battery:({spool:e,locationReadingsMap:t,t:n,colorizeLocationSensors:r,locationSensorAboveColor:i,locationSensorBelowColor:a,locationSensorOptimalColor:o})=>{let s=e.location_id?t[e.location_id]?.find(e=>e.device_class===`battery`):void 0;return s?(0,Z.jsx)(`span`,{title:s.name,className:`text-sm ${$(s,r,i,a,o)}`,children:pr(s,n)}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},label_weight:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:Zn(e.label_weight)}),net:({remaining:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:Zn(e)}),gross:({spool:e,remaining:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Zn(t+e.core_weight)}),used:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.weight_used>0?Zn(e.weight_used):`-`}),printed_total:()=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`}),printed_since_weight:()=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`}),note:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray max-w-[150px] truncate block`,title:e.note||void 0,children:e.note||`-`}),material_number:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.material_number||`-`}),suppliers:({spool:e})=>{let t=e.suppliers??[];return t.length===0?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Z.jsx)(`div`,{className:`flex flex-wrap gap-1 max-w-[220px]`,children:[...t].sort((e,t)=>Number(t.is_purchase_source)-Number(e.is_purchase_source)).map(e=>(0,Z.jsx)(`span`,{title:e.supplier_article_number||void 0,className:`inline-flex items-center px-1.5 py-0.5 rounded-full text-[10px] font-medium ${e.is_purchase_source?`bg-bambu-green/20 text-bambu-green`:`bg-bambu-gray/10 text-bambu-gray`}`,children:e.supplier_name},e.id))})},pa_k:({spool:e})=>(e.k_profiles?.length??0)===0?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Z.jsx)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-bambu-green/20 text-bambu-green`,children:`K`}),tag_id:({spool:e})=>{let t=e.tag_uid||e.tray_uuid;return t?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray font-mono`,title:t,children:t.length>12?`${t.slice(0,6)}...${t.slice(-4)}`:t}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},data_origin:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.data_origin||`-`}),tag_type:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.tag_type||`-`}),stock:({spool:e,t})=>e.slicer_filament?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Z.jsx)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-amber-100 dark:bg-amber-500/20 text-amber-700 dark:text-amber-400`,children:t(`inventory.stock`)}),remaining:({remaining:e,pct:t})=>(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${t>50?`bg-bambu-green`:t>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(t,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(e),`g`]})]}),spool_name:({spool:e,catalogMap:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:(e.core_weight_catalog_id==null?void 0:t[e.core_weight_catalog_id])?.name||`-`}),cost_per_kg:({spool:e,currencySymbol:t,t:n})=>{let{rate:r,fromFilament:i}=jt(e);return(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[r==null?`-`:`${t}${r.toFixed(2)}`,i&&(0,Z.jsx)(`span`,{className:`ml-1 text-xs opacity-70`,children:n(`inventory.costFromFilament`)})]})},weight_check:({spool:e,onSyncWeight:t})=>{let n=e.last_scale_weight;if(n==null)return(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,title:`No scale measurement`,children:`-`});let r=e.core_weight||0,i=Math.max(0,e.label_weight-e.weight_used)+r,a,o;n<r?(a=n-r,o=!0):(a=n-i,o=Math.abs(a)<=50);let s=a>0?`+${Math.round(a)}`:`${Math.round(a)}`,c=o?`Scale: ${Math.round(n)}g\nCalculated: ${Math.round(i)}g\nDifference: ${s}g (within tolerance)`:`Scale: ${Math.round(n)}g\nCalculated: ${Math.round(i)}g\nDifference: ${s}g (mismatch!)`;return(0,Z.jsxs)(`div`,{className:`flex items-center gap-1 text-sm font-medium ${o?`text-green-700 dark:text-green-400`:`text-yellow-700 dark:text-yellow-400`}`,title:c,children:[(0,Z.jsxs)(`span`,{children:[Math.round(n),`g`]}),o?(0,Z.jsx)(w,{className:`w-3 h-3`}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(V,{className:`w-3 h-3`}),t&&(0,Z.jsx)(`button`,{type:`button`,onClick:n=>{n.stopPropagation(),n.preventDefault(),t(e)},className:`p-1 hover:bg-bambu-green/20 rounded transition-colors text-bambu-green`,title:`Sync: trust scale weight and reset tracking`,children:(0,Z.jsx)(Qe,{className:`w-3.5 h-3.5`})})]})]})}},rr={id:e=>e.id,added_time:e=>e.created_at||``,encode_time:e=>e.encode_time||``,last_used_time:e=>e.last_used||``,last_dried:e=>e.last_dried_at||``,material:e=>(e.material||``).toLowerCase(),subtype:e=>(e.subtype||``).toLowerCase(),color_name:e=>(e.color_name||``).toLowerCase(),rgba:e=>ze(e.rgba),brand:e=>(e.brand||``).toLowerCase(),slicer_filament:e=>(e.slicer_filament_name||e.slicer_filament||``).toLowerCase(),location:(e,t)=>{let n=t[e.id];if(!n)return``;let r=n.ams_id===254||n.ams_id===255,i=!r&&n.ams_id>=128,a=n.ams_label?` (${n.ams_label})`:``;return`${n.printer_name||``} ${Le(n.ams_id,n.tray_id,i,r)}${a}`},storage_location:e=>(e.storage_location||``).toLowerCase(),label_weight:e=>e.label_weight,net:e=>Math.max(0,e.label_weight-e.weight_used),gross:e=>Math.max(0,e.label_weight-e.weight_used)+e.core_weight,used:e=>e.weight_used,remaining:e=>e.label_weight>0?Math.max(0,e.label_weight-e.weight_used)/e.label_weight:0,note:e=>(e.note||``).toLowerCase(),material_number:e=>qn(e.material_number),suppliers:e=>{let t=e.suppliers??[];return((t.find(e=>e.is_purchase_source)??t[0])?.supplier_name||``).toLowerCase()},data_origin:e=>(e.data_origin||``).toLowerCase(),tag_type:e=>(e.tag_type||``).toLowerCase(),stock:e=>+!!e.slicer_filament,spool_name:e=>e.core_weight_catalog_id??0,cost_per_kg:e=>jt(e).rate??0,weight_check:e=>{if(e.last_scale_weight==null)return-1;let t=Math.max(0,e.label_weight-e.weight_used)+e.core_weight;return Math.abs(e.last_scale_weight-t)},temperature:(e,t,n)=>(e.location_id?n[e.location_id]:void 0)?.find(e=>e.device_class===`temperature`)?.value??-1/0,humidity:(e,t,n)=>(e.location_id?n[e.location_id]:void 0)?.find(e=>e.device_class===`humidity`)?.value??-1/0,battery:(e,t,n)=>(e.location_id?n[e.location_id]:void 0)?.find(e=>e.device_class===`battery`)?.value??-1/0},ir=`bambuddy-inventory-sort`;function ar(){try{let e=localStorage.getItem(ir);if(e)return JSON.parse(e)}catch{}return null}function or(e){try{e?localStorage.setItem(ir,JSON.stringify(e)):localStorage.removeItem(ir)}catch{}}function sr(){let{data:e}=d({queryKey:[`spoolman-settings`],queryFn:i.getSpoolmanSettings,staleTime:300*1e3}),t=e!==void 0;return(0,Z.jsx)(cr,{spoolmanMode:e?.spoolman_enabled===`true`&&!!e?.spoolman_url,spoolmanModeReady:t})}function cr({spoolmanMode:e=!1,spoolmanModeReady:t=!0}){let{t:n}=r(),a=Ue(),o=u(),{showToast:l}=ee(),{hasPermission:h,loading:g}=he(),b=!g&&h(`inventory:forecast_read`),[x,C]=ye(),[w,E]=(0,X.useState)(null),D=(0,X.useRef)(!1),[O,j]=(0,X.useState)(null),[N,P]=(0,X.useState)(null),[L,ie]=(0,X.useState)(!1),[z,B]=(0,X.useState)(!1),[le,ue]=(0,X.useState)(!1),[de,fe]=(0,X.useState)(!1),[pe,ge]=(0,X.useState)(`active`),[U,_e]=(0,X.useState)(`all`),[be,W]=(0,X.useState)(``),[Te,De]=(0,X.useState)(``),[ke,Ae]=(0,X.useState)(``),[je,Pe]=(0,X.useState)(``),[Fe,Le]=(0,X.useState)(``),[ze,Ve]=(0,X.useState)(``),[G,Ge]=(0,X.useState)(`all`),[Ke,Ye]=(0,X.useState)(``),[K,Ze]=(0,X.useState)(`table`),[Qe,$e]=(0,X.useState)(ar),[tt,nt]=(0,X.useState)(Yn),[it,at]=(0,X.useState)(!1),[ot,st]=(0,X.useState)(()=>{try{return localStorage.getItem(`bambuddy-inventory-group`)===`true`}catch{return!1}}),[ct,lt]=(0,X.useState)(new Set),[q,ut]=(0,X.useState)(new Set),[dt,ft]=(0,X.useState)(!1),[J,pt]=(0,X.useState)(null),mt=(0,X.useCallback)(e=>{ut(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[]),ht=(0,X.useCallback)(()=>ut(new Set),[]);(0,X.useEffect)(()=>{ut(new Set)},[pe,U,be,Te,ke,je,Fe,ze,G,Ke]);let[gt,_t]=(0,X.useState)(0),[vt,yt]=(0,X.useState)(()=>{try{let e=localStorage.getItem(`bambuddy-inventory-pageSize`);if(e){let t=Number(e);if([15,30,50,100,-1].includes(t))return t}}catch{}return 15}),{data:bt}=Ce(),xt=bt?.date_format||`system`,St=(bt?.location_sensor_poll_interval||120)*1e3,{colorize:Ct,aboveColor:wt,belowColor:Tt,optimalColor:Et}=R(),Dt=e?[`spoolman-inventory-spools`]:[`inventory-spools`],Ot=()=>Be(o,Dt),{data:Y,isLoading:kt}=d({queryKey:Dt,queryFn:()=>e?i.getSpoolmanInventorySpools(!0):i.getSpools(!0),refetchInterval:3e4}),At=(0,X.useCallback)(async()=>{B(!0);try{await i.exportSpoolsCsv()}catch(e){l(e instanceof Error?e.message:n(`inventory.csv.exportError`,`Export failed`),`error`)}finally{B(!1)}},[l,n]),jt=e?n(`inventory.csv.spoolmanHint`,`In Spoolman mode, use Spoolman's built-in CSV import/export.`):void 0,{data:Nt=[]}=d({queryKey:qe,queryFn:i.getLocations}),Pt=x.get(`location_id`),Ft=Pt===`__none__`?`__none__`:Pt&&/^\d+$/.test(Pt)&&Number(Pt)>0?Pt:``,Lt=x.get(`spool`),Bt=Lt&&/^\d+$/.test(Lt)&&Number(Lt)>0?Number(Lt):null,Vt=Y?.find(e=>e.id===Bt)??null,Ht=(0,X.useCallback)(()=>{D.current=!0,C(e=>(e.delete(`spool`),e),{replace:!0})},[C]),{data:Ut,isError:Wt,error:Gt}=d({queryKey:e?[`spoolman-inventory-spool`,Bt]:[`inventory-spool`,Bt],queryFn:()=>e?i.getSpoolmanInventorySpool(Bt):i.getSpool(Bt),enabled:t&&Bt!==null&&Vt===null,staleTime:1/0,retry:(e,t)=>e<2&&!(t instanceof s&&t.status===404)});(0,X.useEffect)(()=>{if(!D.current){if(t&&Bt&&Vt){Ht(),E({spool:Vt,mode:`edit`});return}if(Ut){Ht(),E({spool:Ut,mode:`edit`});return}Wt&&(Ht(),l(n(Gt instanceof s&&Gt.status===404?`inventory.deepLinkSpoolNotFound`:`inventory.deepLinkFetchFailed`),`error`))}},[t,Bt,Vt,Ut,Wt,Gt,Ht,l,n]);let{data:Kt}=d({queryKey:[`spool-assignments`],queryFn:()=>i.getAssignments(),refetchInterval:3e4}),{data:qt=[],isError:Jt}=d({queryKey:[`spoolman-slot-assignments`,`all`],queryFn:()=>i.getSpoolmanSlotAssignments(),enabled:e,refetchInterval:3e4,staleTime:1e4,retry:1}),Xt=(0,X.useRef)(!1);(0,X.useEffect)(()=>{Jt&&!Xt.current?(Xt.current=!0,l(n(`inventory.spoolmanUnreachable`),`error`)):Jt||(Xt.current=!1)},[Jt,l,n]);let{data:Zt}=d({queryKey:[`spool-catalog`],queryFn:()=>i.getSpoolCatalog()}),Qt=f({mutationFn:t=>e?i.deleteSpoolmanInventorySpool(t):i.deleteSpool(t),onSuccess:()=>{Ot(),l(n(`inventory.spoolDeleted`),`success`)},onError:e=>{e instanceof s&&e.status===404?l(n(`inventory.deleteSpoolNotFound`),`error`):e instanceof s&&e.status===503?l(n(`inventory.spoolmanUnreachable`),`error`):l(n(`inventory.deleteFailed`),`error`)}}),$t=f({mutationFn:t=>e?i.archiveSpoolmanInventorySpool(t):i.archiveSpool(t),onSuccess:()=>{Ot(),l(n(`inventory.spoolArchived`),`success`)},onError:e=>{e instanceof s&&e.status===404?l(n(`inventory.archiveSpoolNotFound`),`error`):e instanceof s&&e.status===503?l(n(`inventory.spoolmanUnreachable`),`error`):l(n(`inventory.archiveFailed`),`error`)}}),en=f({mutationFn:t=>e?i.restoreSpoolmanInventorySpool(t):i.restoreSpool(t),onSuccess:()=>{Ot(),l(n(`inventory.spoolRestored`),`success`)},onError:e=>{e instanceof s&&e.status===404?l(n(`inventory.restoreSpoolNotFound`),`error`):e instanceof s&&e.status===503?l(n(`inventory.spoolmanUnreachable`),`error`):l(n(`inventory.restoreFailed`),`error`)}}),tn=f({mutationFn:t=>e?i.resetSpoolmanInventorySpoolConsumedCounter(t):i.resetSpoolConsumedCounter(t),onSuccess:()=>{o.invalidateQueries({queryKey:Dt}),l(n(`inventory.consumedCounterReset`),`success`)},onError:()=>{l(n(`inventory.resetConsumedCounterFailed`),`error`)}}),nn=e=>(e.not_found?.length??0)+(e.errors?.length??0),rn=f({mutationFn:t=>e?i.bulkResetSpoolmanInventorySpoolConsumedCounter(t):i.bulkResetSpoolConsumedCounter(t),onSuccess:e=>{o.invalidateQueries({queryKey:Dt});let t=nn(e);if(e.reset===0&&t>0){l(n(`inventory.bulk.resetAllFailed`,{count:t}),`error`);return}t>0?l(n(`inventory.bulk.resetPartial`,{ok:e.reset,failed:t}),`warning`):l(n(`inventory.allConsumedCountersReset`,{count:e.reset}),`success`),pt(null),ht()},onError:()=>{l(n(`inventory.resetConsumedCounterFailed`),`error`)}}),an=f({mutationFn:async({ids:t,update:n})=>e?i.bulkUpdateSpoolmanInventorySpools(t,n):i.bulkUpdateSpools(t,n),onSuccess:e=>{Ot();let t=nn(e);if(e.updated===0){l(n(`inventory.bulk.updateAllFailed`,{count:t}),`error`);return}t>0?l(n(`inventory.bulk.updatePartial`,{ok:e.updated,failed:t}),`warning`):l(n(`inventory.bulk.updateSuccess`,{count:e.updated}),`success`),ft(!1),ht()},onError:e=>{l(e.message||n(`inventory.bulk.updateFailed`),`error`)}}),on=f({mutationFn:async t=>e?i.bulkDeleteSpoolmanInventorySpools(t):i.bulkDeleteSpools(t),onSuccess:e=>{Ot();let t=nn(e);if(e.deleted===0){l(n(`inventory.bulk.deleteAllFailed`,{count:t}),`error`);return}t>0?l(n(`inventory.bulk.deletePartial`,{ok:e.deleted,failed:t}),`warning`):l(n(`inventory.bulk.deleteSuccess`,{count:e.deleted}),`success`),pt(null),ht()},onError:e=>{l(e.message||n(`inventory.bulk.deleteFailed`),`error`)}}),sn=f({mutationFn:async t=>e?i.bulkArchiveSpoolmanInventorySpools(t):i.bulkArchiveSpools(t),onSuccess:e=>{Ot();let t=nn(e);if(e.archived===0&&t>0){l(n(`inventory.bulk.archiveAllFailed`,{count:t}),`error`);return}t>0?l(n(`inventory.bulk.archivePartial`,{ok:e.archived,failed:t}),`warning`):l(n(`inventory.bulk.archiveSuccess`,{count:e.archived}),`success`),pt(null),ht()},onError:e=>{l(e.message||n(`inventory.bulk.archiveFailed`),`error`)}}),cn=f({mutationFn:async t=>e?i.bulkRestoreSpoolmanInventorySpools(t):i.bulkRestoreSpools(t),onSuccess:e=>{Ot();let t=nn(e);if(e.restored===0&&t>0){l(n(`inventory.bulk.restoreAllFailed`,{count:t}),`error`);return}t>0?l(n(`inventory.bulk.restorePartial`,{ok:e.restored,failed:t}),`warning`):l(n(`inventory.bulk.restoreSuccess`,{count:e.restored}),`success`),pt(null),ht()},onError:e=>{l(e.message||n(`inventory.bulk.restoreFailed`),`error`)}}),ln=(0,X.useMemo)(()=>(Y??[]).map(e=>e.id),[Y]),un=async t=>{if(t.last_scale_weight!=null)try{e?await i.syncSpoolmanSpoolWeight(t.id,t.last_scale_weight):await c.updateSpoolWeight(t.id,t.last_scale_weight),o.invalidateQueries({queryKey:Dt}),l(`Synced "${[t.brand,t.material,Ne(t.color_name,t.rgba,t.color_name_is_synthesized)].filter(Boolean).join(` `)}" to scale weight`,`success`)}catch(e){let t=e instanceof s&&e.status===404,r=e instanceof s&&e.status===503;l(n(t?`inventory.syncWeightSpoolNotFound`:r?`inventory.syncWeightSpoolmanUnreachable`:`inventory.syncWeightFailed`),`error`)}},dn=bt?.low_stock_threshold??20,[fn,pn]=(0,X.useState)(!1),[mn,hn]=(0,X.useState)(dn.toString());(0,X.useEffect)(()=>{fn||hn(dn.toString())},[dn,fn]);let gn=f({mutationFn:e=>i.updateSettings({low_stock_threshold:e}),onSuccess:()=>{o.invalidateQueries({queryKey:[`settings`]}),l(n(`common.saved`),`success`),pn(!1)},onError:()=>{l(n(`inventory.lowStockThresholdError`),`error`)}}),_n=(0,X.useMemo)(()=>{if(!Y)return null;let e=0,t=0,n=0,r=0,i={};for(let a of Y){if(t+=Math.max(0,a.weight_used-(a.weight_used_baseline??0)),a.archived_at)continue;r++;let o=Math.max(0,a.label_weight-a.weight_used);e+=o,(a.label_weight>0?o/a.label_weight*100:0)<(a.low_stock_threshold_pct??dn)&&n++;let s=a.material||`Unknown`;i[s]||(i[s]={count:0,weight:0}),i[s].count++,i[s].weight+=o}return{totalWeight:e,totalConsumed:t,lowStock:n,byMaterial:i,totalSpools:r}},[Y,dn]),vn=(Kt?.length??0)+(e?qt.length:0),bn=We(bt?.currency||`USD`),xn=(0,X.useMemo)(()=>{let e={};for(let t of Kt||[])e[t.spool_id]={printer_id:t.printer_id,printer_name:t.printer_name,ams_id:t.ams_id,tray_id:t.tray_id,ams_label:t.ams_label??null};for(let t of qt)typeof t?.spoolman_spool_id!=`number`||t.spoolman_spool_id<=0||typeof t.printer_id!=`number`||typeof t.ams_id!=`number`||typeof t.tray_id!=`number`||e[t.spoolman_spool_id]||(e[t.spoolman_spool_id]={printer_id:t.printer_id,printer_name:t.printer_name??null,ams_id:t.ams_id,tray_id:t.tray_id,ams_label:t.ams_label??null});return e},[Kt,qt]),Sn=(0,X.useMemo)(()=>{let e={};for(let t of Zt||[])e[t.id]=t;return e},[Zt]),{data:Cn}=d({queryKey:[`locationHaSensors`],queryFn:()=>i.getLocationHASensors()}),wn=(0,X.useMemo)(()=>new Set((Cn??[]).map(e=>e.location_id)),[Cn]),Tn=(0,X.useMemo)(()=>{let e=new Set;for(let t of Y||[])t.location_id&&wn.has(t.location_id)&&e.add(t.location_id);return Array.from(e)},[Y,wn]),En=K===`cards`||K===`table`&&tt.some(e=>e.visible&&(e.id===`temperature`||e.id===`humidity`||e.id===`battery`)),Dn=p({queries:Tn.map(e=>({queryKey:[`locationHaSensorReadings`,e],queryFn:()=>i.getLocationHASensorReadings(e,!1),refetchInterval:St,enabled:En}))}),On=(0,X.useMemo)(()=>{let e={};return Tn.forEach((t,n)=>{let r=Dn[n]?.data;r&&(e[t]=r)}),e},[Tn,Dn]),kn=(0,X.useMemo)(()=>_n?Object.entries(_n.byMaterial).sort((e,t)=>t[1].weight-e[1].weight).slice(0,4):[],[_n]),An=(0,X.useMemo)(()=>{let e=Y||[];if(e=pe===`active`?e.filter(e=>!e.archived_at):e.filter(e=>!!e.archived_at),U===`used`?e=e.filter(e=>e.weight_used>0):U===`new`?e=e.filter(e=>e.weight_used===0):U===`lowstock`&&(e=e.filter(e=>{let t=Math.max(0,e.label_weight-e.weight_used);return(e.label_weight>0?t/e.label_weight*100:0)<(e.low_stock_threshold_pct??dn)})),be&&(e=e.filter(e=>e.material===be)),Te&&(e=e.filter(e=>e.brand===Te)),ke&&(e=ke===`__none__`?e.filter(e=>!e.category):e.filter(e=>e.category===ke)),je&&(e=je===Kn?e.filter(e=>!e.material_number?.trim()):e.filter(e=>e.material_number===je)),Fe)if(Fe===`__none__`)e=e.filter(e=>(e.suppliers??[]).length===0);else{let t=Number(Fe);e=e.filter(e=>(e.suppliers??[]).some(e=>e.supplier_id===t))}if(ze){let t=Number(ze);e=e.filter(e=>e.core_weight_catalog_id===t)}if(Ft)if(Ft===`__none__`)e=e.filter(e=>!e.location_id&&!e.storage_location?.trim());else{let t=Number(Ft),n=Nt.find(e=>e.id===t)?.name?.trim().toLowerCase();e=e.filter(e=>e.location_id==null?n?(e.storage_location||``).trim().toLowerCase()===n:!1:e.location_id===t)}return G===`stock`?e=e.filter(e=>!e.slicer_filament):G===`configured`&&(e=e.filter(e=>!!e.slicer_filament)),Ke&&(e=Mt(e,Ke)),e},[Y,pe,U,be,Te,ke,je,Fe,ze,G,Ft,Ke,dn,Nt,a]),Q=()=>_t(0),jn=(0,X.useCallback)(e=>{C(t=>(t.delete(`location_id`),e&&t.set(`location_id`,e),t),{replace:!0}),Q()},[C]),Mn=[...new Set(Y?.map(e=>e.material)||[])].sort(),Nn=[...new Set(Y?.map(e=>e.brand).filter(Boolean)||[])].sort(),Pn=[...new Set(Y?.map(e=>e.category?.trim()).filter(Boolean)||[])].sort(),Rn=(Y??[]).some(e=>!e.category),zn=[...new Set(Y?.map(e=>e.material_number?.trim()).filter(Boolean)||[])].sort((e,t)=>e.localeCompare(t,void 0,{numeric:!0})),Bn=(Y??[]).some(e=>!e.material_number?.trim()),Gn=(0,X.useMemo)(()=>{let e=new Map;for(let t of Y??[])for(let n of t.suppliers??[])e.set(n.supplier_id,n.supplier_name);return[...e.entries()].map(([e,t])=>({id:e,name:t})).sort((e,t)=>e.name.localeCompare(t.name))},[Y]),qn=(Y??[]).some(e=>(e.suppliers??[]).length===0),$n=[...new Set(Y?.map(e=>e.core_weight_catalog_id).filter(e=>e!=null)||[])].sort((e,t)=>{let n=(Sn[e]?.name||``).toLowerCase(),r=(Sn[t]?.name||``).toLowerCase();return n.localeCompare(r)}),er=(Y??[]).some(e=>!e.location_id&&!e.storage_location?.trim()),nr=pe!==`active`||U!==`all`||!!be||!!Te||!!ke||!!je||!!Fe||!!ze||!!Ft||G!==`all`||!!Ke,ir=e=>{nt(e),Xn(e)},sr=(0,X.useMemo)(()=>tt.filter(e=>e.visible).map(e=>e.id),[tt]),cr=e=>{rr[e]&&($e(t=>{let n;return n=t?.column===e?t.direction===`asc`?{column:e,direction:`desc`}:null:{column:e,direction:`asc`},or(n),n}),Q())},dr=(0,X.useMemo)(()=>{if(!Qe)return An;let e=rr[Qe.column];return e?[...An].sort((t,n)=>{let r=e(t,xn,On),i=e(n,xn,On);return r<i?Qe.direction===`asc`?-1:1:r>i?Qe.direction===`asc`?1:-1:0}):An},[An,Qe,xn,On]),fr=(0,X.useMemo)(()=>{if(!ot)return dr.map(e=>({type:`single`,spool:e}));let e=new Map;for(let t of dr)if(!(t.weight_used>0||xn[t.id])){let n=Wn(t),r=e.get(n);r?r.push(t):e.set(n,[t])}let t=[],n=new Set;for(let r of dr){if(r.weight_used>0||xn[r.id]){t.push({type:`single`,spool:r});continue}let i=Wn(r);if(n.has(i))continue;n.add(i);let a=e.get(i);a.length===1?t.push({type:`single`,spool:a[0]}):t.push({type:`group`,key:i,spools:a,representative:a[0]})}return t},[dr,ot,xn]),pr=vt===-1,$=fr.length,mr=pr?$||1:vt,vr=Math.max(1,Math.ceil($/mr)),yr=pr?0:Math.min(gt,vr-1),br=pr?fr:fr.slice(yr*mr,(yr+1)*mr),xr=()=>{let e=!ot;st(e),lt(new Set),Q();try{localStorage.setItem(`bambuddy-inventory-group`,String(e))}catch{}},Sr=e=>{lt(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},Cr=e=>{yt(e),_t(0);try{localStorage.setItem(`bambuddy-inventory-pageSize`,String(e))}catch{}};return(0,Z.jsxs)(`div`,{className:`p-4 md:p-8 space-y-6`,children:[(0,Z.jsxs)(`div`,{className:`flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`h1`,{className:`text-2xl font-bold text-white flex items-center gap-3`,children:[(0,Z.jsx)(oe,{className:`w-7 h-7 text-bambu-green`}),n(`inventory.title`)]}),(0,Z.jsx)(`p`,{className:`text-bambu-gray mt-1`,children:n(`inventory.subtitle`)})]}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2`,children:[(0,Z.jsxs)(H,{variant:`secondary`,disabled:e,onClick:()=>ie(!0),title:jt,children:[(0,Z.jsx)(Me,{className:`w-4 h-4`}),n(`inventory.csv.importButton`,`Import CSV`)]}),(0,Z.jsxs)(H,{variant:`secondary`,disabled:e||z,onClick:At,title:jt,children:[z?(0,Z.jsx)(F,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(Oe,{className:`w-4 h-4`}),n(`inventory.csv.exportButton`,`Export CSV`)]}),(0,Z.jsxs)(H,{variant:`secondary`,onClick:()=>ue(!0),children:[(0,Z.jsx)(ae,{className:`w-4 h-4`}),n(`locations.manage`)]}),(0,Z.jsxs)(H,{variant:`secondary`,onClick:()=>fe(!0),children:[(0,Z.jsx)(Rt,{className:`w-4 h-4`}),n(`inventory.suppliers.title`)]}),(0,Z.jsxs)(H,{variant:`secondary`,disabled:An.length===0,onClick:()=>P(q.size>0?[...q]:An.map(e=>e.id)),title:An.length===0?n(`inventory.labels.noSpoolsTitle`,`No spools to label`):q.size>0?n(`inventory.labels.selectedTitle`,`Print labels for the {{count}} selected spools`,{count:q.size}):n(`inventory.labels.bulkTitle`,`Pick spools to print labels for from the {{count}} currently shown`,{count:An.length}),children:[(0,Z.jsx)(Xe,{className:`w-4 h-4`}),n(`inventory.labels.printLabels`,`Print labels…`)]}),(0,Z.jsxs)(H,{onClick:()=>E({spool:null,mode:`create`}),children:[(0,Z.jsx)(Je,{className:`w-4 h-4`}),n(`inventory.addSpool`)]})]})]}),_n&&!kt&&(0,Z.jsxs)(`div`,{className:`grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3`,children:[(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(oe,{className:`w-4 h-4 text-bambu-green`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:n(`inventory.totalInventory`)})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Zn(_n.totalWeight,!0)}),(0,Z.jsxs)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:[_n.totalSpools,` `,_n.totalSpools===1?n(`inventory.spool`):n(`inventory.spools`)]})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between gap-2 mb-1`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(ce,{className:`w-4 h-4 text-blue-600 dark:text-blue-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:n(`inventory.totalConsumed`)})]}),_n.totalConsumed>0&&ln.length>0&&(0,Z.jsx)(`button`,{onClick:()=>j({type:`reset-all-consumed-counters`}),className:`p-1 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 rounded transition-colors`,title:n(`inventory.resetAllConsumedCountersTooltip`),"aria-label":n(`inventory.resetAllConsumedCounters`),children:(0,Z.jsx)(S,{className:`w-3.5 h-3.5`})})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Zn(_n.totalConsumed,!0)}),(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:n(`inventory.sinceTracking`)})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(Re,{className:`w-4 h-4 text-green-600 dark:text-green-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:n(`inventory.byMaterial`)})]}),(0,Z.jsx)(`div`,{className:`flex flex-wrap gap-1.5 mt-1`,children:kn.map(([e,t])=>(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${Qn[e]||`bg-bambu-dark-tertiary text-bambu-gray`}`,children:[e,` `,(0,Z.jsx)(`span`,{className:`opacity-70`,children:Zn(t.weight,!0)})]},e))})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(Xe,{className:`w-4 h-4 text-purple-600 dark:text-purple-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:n(`inventory.inPrinter`)})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold text-white`,children:vn}),(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:n(`inventory.loadedInAms`)})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(V,{className:`w-4 h-4 text-yellow-600 dark:text-yellow-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:n(`inventory.lowStock`)})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold ${_n.lowStock>0?`text-yellow-700 dark:text-yellow-400`:`text-white`}`,children:_n.lowStock}),(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1 flex items-center gap-2`,children:fn?(0,Z.jsxs)(`form`,{onSubmit:e=>{e.preventDefault();let t=parseFloat(mn);!isNaN(t)&&t>=.1&&t<=99.9?gn.mutate(t):l(n(`inventory.lowStockThresholdError`),`error`)},className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`<`}),(0,Z.jsx)(`input`,{type:`text`,inputMode:`decimal`,pattern:`^\\d{0,2}(\\.\\d?)?$`,maxLength:4,value:mn,onChange:e=>{let t=e.target.value.replace(/[^\d.]/g,``);/^\d{0,2}(\.\d?)?$/.test(t)&&hn(t)},className:`px-1.5 py-1 rounded border border-bambu-dark-tertiary text-xs text-white bg-bambu-dark-secondary focus:outline-none focus:border-bambu-green w-14 text-center`,onWheel:e=>e.currentTarget.blur(),disabled:gn.isPending}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`%`}),(0,Z.jsx)(H,{type:`submit`,size:`sm`,disabled:gn.isPending,children:n(`common.save`)}),(0,Z.jsx)(H,{type:`button`,size:`sm`,variant:`ghost`,onClick:()=>pn(!1),disabled:gn.isPending,children:n(`common.cancel`)})]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray`,children:[`< `,dn,`%`]}),(0,Z.jsx)(`button`,{className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:n(`common.edit`),onClick:()=>{hn(dn.toString()),pn(!0)},children:(0,Z.jsx)(I,{className:`w-4 h-4`})})]})})]})]}),(0,Z.jsxs)(`div`,{className:`flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between`,children:[(0,Z.jsxs)(`div`,{className:`relative flex-1 max-w-md ${K===`forecast`?`invisible pointer-events-none`:``}`,children:[(0,Z.jsx)(se,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50`}),(0,Z.jsx)(`input`,{type:`text`,value:Ke,onChange:e=>{Ye(e.target.value),Q()},placeholder:n(`inventory.search`),className:`w-full pl-10 pr-8 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`}),Ke&&(0,Z.jsx)(`button`,{onClick:()=>{Ye(``),Q()},className:`absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white`,children:(0,Z.jsx)(me,{className:`w-4 h-4`})})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[K===`table`&&(0,Z.jsxs)(`button`,{onClick:()=>at(!0),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-bambu-gray border border-bambu-dark-tertiary rounded-lg hover:bg-bambu-dark-tertiary transition-colors`,title:n(`inventory.configureColumns`),children:[(0,Z.jsx)(M,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:n(`inventory.columns`)})]}),K!==`forecast`&&(0,Z.jsxs)(`button`,{onClick:xr,className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium border rounded-lg transition-colors ${ot?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,title:n(`inventory.groupSimilar`),children:[(0,Z.jsx)(It,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:n(`inventory.groupSimilar`)})]}),(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:[(0,Z.jsxs)(`button`,{onClick:()=>Ze(`table`),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${K===`table`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(zt,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:n(`inventory.table`)})]}),(0,Z.jsxs)(`button`,{onClick:()=>Ze(`cards`),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${K===`cards`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(te,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:n(`inventory.cards`)})]}),(0,Z.jsxs)(`button`,{onClick:()=>b&&Ze(`forecast`),disabled:!b,title:b?void 0:n(`forecast.noReadAccess`),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors disabled:opacity-40 disabled:cursor-not-allowed ${K===`forecast`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[b?(0,Z.jsx)(m,{className:`w-4 h-4`}):(0,Z.jsx)(ne,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:n(`forecast.title`)})]})]})]})]}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2 ${K===`forecast`?`hidden`:``}`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Z.jsxs)(`button`,{onClick:()=>{ge(`active`),Q()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${pe===`active`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(oe,{className:`w-3.5 h-3.5`}),n(`inventory.active`)]}),(0,Z.jsxs)(`button`,{onClick:()=>{ge(`archived`),Q()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${pe===`archived`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(we,{className:`w-3.5 h-3.5`}),n(`inventory.archived`)]})]}),(0,Z.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Z.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Z.jsx)(`button`,{onClick:()=>{_e(`all`),Q()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${U===`all`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:n(`inventory.all`)}),(0,Z.jsxs)(`button`,{onClick:()=>{_e(`used`),Q()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${U===`used`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(Ee,{className:`w-3.5 h-3.5`}),n(`inventory.used`)]}),(0,Z.jsx)(`button`,{onClick:()=>{_e(`new`),Q()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${U===`new`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:n(`inventory.new`)}),(0,Z.jsxs)(`button`,{onClick:()=>{_e(`lowstock`),Q()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${U===`lowstock`?`bg-yellow-100 dark:bg-yellow-500/20 text-yellow-700 dark:text-yellow-400`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(V,{className:`w-3.5 h-3.5`}),n(`inventory.lowStock`)]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Z.jsx)(`button`,{onClick:()=>{Ge(`all`),Q()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${G===`all`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:n(`inventory.all`)}),(0,Z.jsx)(`button`,{onClick:()=>{Ge(`stock`),Q()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${G===`stock`?`bg-amber-100 dark:bg-amber-500/20 text-amber-700 dark:text-amber-400`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:n(`inventory.stock`)}),(0,Z.jsx)(`button`,{onClick:()=>{Ge(`configured`),Q()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${G===`configured`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:n(`inventory.configured`)})]}),(0,Z.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Z.jsxs)(`select`,{value:be,onChange:e=>{W(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${be?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:n(`inventory.material`)}),Mn.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(0,Z.jsxs)(`select`,{value:Te,onChange:e=>{De(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${Te?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:n(`inventory.brand`)}),Nn.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(Pn.length>0||ke)&&(0,Z.jsxs)(`select`,{value:ke,onChange:e=>{Ae(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${ke?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:n(`inventory.category`)}),Pn.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e)),Rn&&(0,Z.jsx)(`option`,{value:`__none__`,children:n(`inventory.categoryNone`)})]}),(zn.length>0||je)&&(0,Z.jsxs)(`select`,{value:je,onChange:e=>{Pe(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${je?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:n(`inventory.materialNumber`)}),zn.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e)),Bn&&(0,Z.jsx)(`option`,{value:Kn,children:n(`inventory.materialNumberNone`)})]}),(Gn.length>0||Fe)&&(0,Z.jsxs)(`select`,{value:Fe,onChange:e=>{Le(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${Fe?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:n(`inventory.suppliers.label`)}),Gn.map(e=>(0,Z.jsx)(`option`,{value:String(e.id),children:e.name},e.id)),qn&&(0,Z.jsx)(`option`,{value:`__none__`,children:n(`inventory.suppliers.none`)})]}),$n.length>0&&(0,Z.jsxs)(`select`,{value:ze,onChange:e=>{Ve(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${ze?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:n(`inventory.spoolName`)}),$n.map(e=>(0,Z.jsx)(`option`,{value:e,children:Sn[e]?.name||`#${e}`},e))]}),(Nt.length>0||Ft)&&(0,Z.jsxs)(`select`,{value:Ft,onChange:e=>{jn(e.target.value)},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${Ft?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:n(`inventory.storageLocation`)}),Nt.map(e=>(0,Z.jsx)(`option`,{value:String(e.id),children:e.name},e.id)),er&&(0,Z.jsx)(`option`,{value:`__none__`,children:n(`inventory.storageLocationNone`)})]}),nr&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Z.jsxs)(`button`,{onClick:()=>{ge(`active`),_e(`all`),W(``),De(``),Ae(``),Pe(``),Le(``),Ve(``),Ge(`all`),Ye(``),C(e=>(e.delete(`location_id`),e),{replace:!0}),Q()},className:`flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors`,children:[(0,Z.jsx)(me,{className:`w-3.5 h-3.5`}),n(`inventory.clearFilters`)]})]}),K!==`forecast`&&(0,Z.jsxs)(`span`,{className:`ml-auto text-xs text-bambu-gray`,children:[dr.length,` `,dr.length===1?n(`inventory.spool`):n(`inventory.spools`),ot&&$<dr.length&&` (${$} ${n(`inventory.groupedRows`)})`]})]}),q.size>0&&K!==`forecast`&&(0,Z.jsxs)(`div`,{className:`sticky top-2 z-10 mb-4 flex items-center gap-2 px-3 py-2 bg-bambu-green/10 border border-bambu-green/30 rounded-lg backdrop-blur-sm`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-bambu-green font-medium`,children:n(`inventory.bulk.selectionCount`,{count:q.size})}),(0,Z.jsxs)(`div`,{className:`ml-auto flex flex-wrap items-center gap-2`,children:[(0,Z.jsxs)(H,{size:`sm`,variant:`secondary`,onClick:()=>ft(!0),children:[(0,Z.jsx)(I,{className:`w-3.5 h-3.5 mr-1.5`}),n(`inventory.bulk.edit`)]}),(0,Z.jsxs)(H,{size:`sm`,variant:`secondary`,onClick:()=>P([...q]),children:[(0,Z.jsx)(Xe,{className:`w-3.5 h-3.5 mr-1.5`}),n(`inventory.bulk.printLabels`)]}),(0,Z.jsxs)(H,{size:`sm`,variant:`secondary`,onClick:()=>pt(`reset-consumed-counter`),children:[(0,Z.jsx)(S,{className:`w-3.5 h-3.5 mr-1.5`}),n(`inventory.bulk.resetUsage`)]}),pe===`archived`?(0,Z.jsxs)(H,{size:`sm`,variant:`secondary`,onClick:()=>pt(`restore`),children:[(0,Z.jsx)(He,{className:`w-3.5 h-3.5 mr-1.5`}),n(`inventory.bulk.restore`)]}):(0,Z.jsxs)(H,{size:`sm`,variant:`secondary`,onClick:()=>pt(`archive`),children:[(0,Z.jsx)(we,{className:`w-3.5 h-3.5 mr-1.5`}),n(`inventory.bulk.archive`)]}),(0,Z.jsxs)(H,{size:`sm`,variant:`danger`,onClick:()=>pt(`delete`),children:[(0,Z.jsx)(et,{className:`w-3.5 h-3.5 mr-1.5`}),n(`inventory.bulk.delete`)]}),(0,Z.jsx)(`button`,{className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,onClick:ht,title:n(`inventory.bulk.clearSelection`),"aria-label":n(`inventory.bulk.clearSelection`),children:(0,Z.jsx)(me,{className:`w-4 h-4`})})]})]}),kt?(0,Z.jsx)(`div`,{className:`flex justify-center py-16`,children:(0,Z.jsx)(F,{className:`w-8 h-8 text-bambu-green animate-spin`})}):K===`forecast`?(0,Z.jsx)(Yt,{spools:Y||[]}):K===`cards`?br.length>0?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4`,children:br.map(e=>{if(e.type===`group`){let{key:t,spools:r,representative:i}=e,a=r.reduce((e,t)=>e+Math.max(0,t.label_weight-t.weight_used),0),o=ve({rgba:i.rgba,extraColors:i.extra_colors,effectType:i.effect_type,subtype:i.subtype,effectSize:`groupheader`}),s=ct.has(t);return(0,Z.jsxs)(`div`,{className:`col-span-full`,children:[(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-green/30 hover:border-bambu-green transition-colors cursor-pointer`,onClick:()=>Sr(t),children:[(0,Z.jsx)(`div`,{className:`h-10 flex items-center px-4 gap-3`,style:o,children:(0,Z.jsx)(`span`,{className:`bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium`,children:Ne(i.color_name,i.rgba,i.color_name_is_synthesized)||`-`})}),(0,Z.jsxs)(`div`,{className:`px-4 py-3 flex items-center justify-between`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsx)(T,{className:`w-4 h-4 text-bambu-gray transition-transform ${s?``:`-rotate-90`}`}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`h3`,{className:`font-semibold text-white`,children:[i.material,i.subtype?` ${i.subtype}`:``]}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:i.brand||`-`})]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,title:n(`inventory.remaining`),children:Zn(a)}),(0,Z.jsx)(`span`,{className:`text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full`,children:n(`inventory.groupedSpools`,{count:r.length})})]})]})]}),s&&(0,Z.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4 mt-2 ml-4`,children:r.map(e=>{let t=Math.max(0,e.label_weight-e.weight_used);return(0,Z.jsx)(ur,{spool:e,remaining:t,pct:e.label_weight>0?t/e.label_weight*100:0,onClick:()=>E({spool:e,mode:`edit`}),onPrintLabel:()=>P([e.id]),onCopy:()=>E({spool:e,mode:`copy`}),t:n,colorizeLocationSensors:Ct,locationSensorAboveColor:wt,locationSensorBelowColor:Tt,locationSensorOptimalColor:Et,dateFormat:xt},e.id)})})]},`group-${t}`)}let t=e.spool,r=Math.max(0,t.label_weight-t.weight_used);return(0,Z.jsx)(ur,{spool:t,remaining:r,pct:t.label_weight>0?r/t.label_weight*100:0,onClick:()=>E({spool:t,mode:`edit`}),onPrintLabel:()=>P([t.id]),onCopy:()=>E({spool:t,mode:`copy`}),t:n,colorizeLocationSensors:Ct,locationSensorAboveColor:wt,locationSensorBelowColor:Tt,locationSensorOptimalColor:Et,dateFormat:xt},t.id)})}),(0,Z.jsx)(lr,{pageIndex:yr,pageSize:vt,totalRows:$,totalPages:vr,onPageChange:_t,onPageSizeChange:Cr,t:n})]}):(0,Z.jsx)(_r,{hasFilters:nr,onAddSpool:()=>E({spool:null,mode:`create`}),t:n}):br.length>0?(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsx)(`th`,{className:`w-10 px-3 py-3`,children:(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":n(`inventory.bulk.selectAllVisible`),checked:br.length>0&&br.every(e=>(e.type===`group`?e.spools.map(e=>e.id):[e.spool.id]).every(e=>q.has(e))),onChange:e=>{if(e.target.checked){let e=new Set(q);for(let t of br){let n=t.type===`group`?t.spools.map(e=>e.id):[t.spool.id];for(let t of n)e.add(t)}ut(e)}else{let e=new Set(q);for(let t of br){let n=t.type===`group`?t.spools.map(e=>e.id):[t.spool.id];for(let t of n)e.delete(t)}ut(e)}}})}),sr.map(e=>{let t=!!rr[e],r=Qe?.column===e;return(0,Z.jsx)(`th`,{className:`text-left py-3 px-4 text-xs font-medium uppercase tracking-wide select-none ${e===`remaining`?`min-w-[150px]`:``} ${t?`cursor-pointer hover:text-bambu-green transition-colors`:``} ${r?`text-bambu-green`:`text-bambu-gray`}`,onClick:t?()=>cr(e):void 0,children:(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1`,children:[tr[e]?.(n)??e,t&&(r?Qe.direction===`asc`?(0,Z.jsx)(y,{className:`w-3 h-3`}):(0,Z.jsx)(_,{className:`w-3 h-3`}):(0,Z.jsx)(v,{className:`w-3 h-3 opacity-30`}))]})},e)}),(0,Z.jsx)(`th`,{className:`text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:n(`common.actions`)})]})}),(0,Z.jsx)(`tbody`,{children:br.map(e=>{if(e.type===`group`){let{key:t,spools:r}=e,i=ct.has(t),a=Hn(r),o=Math.max(0,a.label_weight-a.weight_used);return(0,Z.jsx)(gr,{spools:r,headerSpool:a,remaining:o,pct:a.label_weight>0?o/a.label_weight*100:0,isExpanded:i,onToggle:()=>Sr(t),selectedIds:q,onToggleSelected:mt,onToggleGroupSelected:(e,t)=>{ut(n=>{let r=new Set(n);for(let n of e)t?r.add(n):r.delete(n);return r})},onEdit:e=>E({spool:e,mode:`edit`}),onCopy:e=>E({spool:e,mode:`copy`}),onArchive:e=>j({type:`archive`,spoolId:e}),onRestore:e=>en.mutate(e),onDelete:e=>j({type:`delete`,spoolId:e}),onPrintLabel:e=>P([e]),onResetConsumedCounter:e=>j({type:`reset-consumed-counter`,spoolId:e}),visibleColumns:sr,assignmentMap:xn,catalogMap:Sn,locationReadingsMap:On,currencySymbol:bn,dateFormat:xt,t:n,onSyncWeight:un,colorizeLocationSensors:Ct,locationSensorAboveColor:wt,locationSensorBelowColor:Tt,locationSensorOptimalColor:Et},`group-${t}`)}let t=e.spool,r=Math.max(0,t.label_weight-t.weight_used);return(0,Z.jsx)(hr,{spool:t,remaining:r,pct:t.label_weight>0?r/t.label_weight*100:0,isSelected:q.has(t.id),onToggleSelected:()=>mt(t.id),onEdit:()=>E({spool:t,mode:`edit`}),onCopy:()=>E({spool:t,mode:`copy`}),onRestore:()=>en.mutate(t.id),onArchive:()=>j({type:`archive`,spoolId:t.id}),onDelete:()=>j({type:`delete`,spoolId:t.id}),onPrintLabel:()=>P([t.id]),onResetConsumedCounter:()=>j({type:`reset-consumed-counter`,spoolId:t.id}),visibleColumns:sr,assignmentMap:xn,catalogMap:Sn,locationReadingsMap:On,currencySymbol:bn,dateFormat:xt,t:n,onSyncWeight:un,colorizeLocationSensors:Ct,locationSensorAboveColor:wt,locationSensorBelowColor:Tt,locationSensorOptimalColor:Et},t.id)})})]})}),(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-3 bg-bambu-dark-tertiary/50 border-t border-bambu-dark-tertiary text-sm`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:pr?`${$} ${n($===1?`inventory.spool`:`inventory.spools`)}`:(0,Z.jsxs)(Z.Fragment,{children:[n(`inventory.showing`),` `,yr*mr+1,` `,n(`inventory.to`),` `,Math.min((yr+1)*mr,$),` `,n(`inventory.of`),` `,$,` `,n(`inventory.spools`)]})}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:n(`inventory.show`)}),(0,Z.jsxs)(`select`,{value:vt,onChange:e=>Cr(Number(e.target.value)),className:`px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green`,children:[[15,30,50,100].map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e)),(0,Z.jsx)(`option`,{value:-1,children:n(`inventory.all`)})]}),!pr&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`button`,{onClick:()=>_t(0),disabled:yr===0,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,title:`First page`,children:(0,Z.jsx)(k,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>_t(e=>Math.max(0,e-1)),disabled:yr===0,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(xe,{className:`w-4 h-4`})}),(0,Z.jsxs)(`span`,{className:`text-bambu-gray px-2 whitespace-nowrap`,children:[n(`inventory.page`),` `,yr+1,` `,n(`inventory.of`),` `,vr]}),(0,Z.jsx)(`button`,{onClick:()=>_t(e=>Math.min(vr-1,e+1)),disabled:yr>=vr-1,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(Ie,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>_t(vr-1),disabled:yr>=vr-1,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,title:`Last page`,children:(0,Z.jsx)(A,{className:`w-4 h-4`})})]})]})]})]}):(0,Z.jsx)(_r,{hasFilters:nr,onAddSpool:()=>E({spool:null,mode:`create`}),t:n}),w!==null&&(0,Z.jsx)(yn,{isOpen:!0,onClose:()=>E(null),spool:w.spool,mode:w.mode,currencySymbol:bn,spoolmanMode:e,spoolsQueryKey:Dt}),O&&(0,Z.jsx)(Se,{title:O.type===`delete`?n(`common.delete`):O.type===`archive`?n(`inventory.archive`):O.type===`reset-consumed-counter`?n(`inventory.resetConsumedCounter`):n(`inventory.resetAllConsumedCounters`),message:O.type===`delete`?n(`inventory.deleteConfirm`):O.type===`archive`?n(`inventory.archiveConfirm`):O.type===`reset-consumed-counter`?n(`inventory.resetConsumedCounterConfirm`):n(`inventory.resetAllConsumedCountersConfirm`,{count:ln.length}),confirmText:O.type===`delete`?n(`common.delete`):O.type===`archive`?n(`inventory.archive`):n(`inventory.resetConsumedCounter`),variant:O.type===`archive`?`warning`:`danger`,onConfirm:()=>{O.type===`delete`?Qt.mutate(O.spoolId):O.type===`archive`?$t.mutate(O.spoolId):O.type===`reset-consumed-counter`?tn.mutate(O.spoolId):rn.mutate(ln),j(null)},onCancel:()=>j(null)}),(0,Z.jsx)(rt,{isOpen:it,onClose:()=>at(!1),columns:tt,defaultColumns:Jn,onSave:ir}),(0,Z.jsx)(Fn,{isOpen:N!==null,onClose:()=>P(null),availableSpools:An,initialSelectedIds:N??[],spoolmanMode:e}),(0,Z.jsx)(Vn,{isOpen:dt,selectedCount:q.size,isPending:an.isPending,availableLocations:Nt.map(e=>({id:e.id,name:e.name})),availableMaterials:Un((Y??[]).map(e=>e.material)),availableSubtypes:Un((Y??[]).map(e=>e.subtype)),availableBrands:Un((Y??[]).map(e=>e.brand)),availableCategories:Un((Y??[]).map(e=>e.category)),availableMaterialNumbers:Un((Y??[]).map(e=>e.material_number)),availableSlicerFilaments:Un((Y??[]).map(e=>e.slicer_filament)),availableSlicerFilamentNames:Un((Y??[]).map(e=>e.slicer_filament_name)),spoolmanMode:e,onClose:()=>ft(!1),onApply:e=>an.mutate({ids:[...q],update:e})}),J&&(0,Z.jsx)(Se,{title:n(J===`delete`?`inventory.bulk.deleteTitle`:J===`archive`?`inventory.bulk.archiveTitle`:J===`restore`?`inventory.bulk.restoreTitle`:`inventory.bulk.resetUsageTitle`),message:n(J===`delete`?`inventory.bulk.deleteMessage`:J===`archive`?`inventory.bulk.archiveMessage`:J===`restore`?`inventory.bulk.restoreMessage`:`inventory.bulk.resetUsageMessage`,{count:q.size}),confirmText:n(J===`delete`?`common.delete`:J===`archive`?`inventory.archive`:J===`restore`?`inventory.restore`:`inventory.resetConsumedCounter`),variant:J===`delete`?`danger`:`warning`,isLoading:on.isPending||sn.isPending||cn.isPending||rn.isPending,onConfirm:()=>{let e=[...q];J===`delete`?on.mutate(e):J===`archive`?sn.mutate(e):J===`restore`?cn.mutate(e):rn.mutate(e)},onCancel:()=>pt(null)}),L&&(0,Z.jsx)(In,{onClose:()=>ie(!1),onImported:e=>{ie(!1),o.invalidateQueries({queryKey:Dt}),l(n(`inventory.csv.importSuccess`,`{{count}} spools imported`,{count:e}),`success`)}}),(0,Z.jsx)(re,{open:le,onClose:()=>ue(!1),onPickLocation:e=>jn(String(e))}),(0,Z.jsx)(Ln,{open:de,onClose:()=>fe(!1)})]})}function lr({pageIndex:e,pageSize:t,totalRows:n,totalPages:r,onPageChange:i,onPageSizeChange:a,t:o}){let s=t===-1;if(r<=1&&!s)return null;let c=s?n||1:t;return(0,Z.jsxs)(`div`,{className:`flex items-center justify-between pt-2 text-sm`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:s?`${n} ${o(n===1?`inventory.spool`:`inventory.spools`)}`:(0,Z.jsxs)(Z.Fragment,{children:[o(`inventory.showing`),` `,e*c+1,` `,o(`inventory.to`),` `,Math.min((e+1)*c,n),` `,o(`inventory.of`),` `,n,` `,o(`inventory.spools`)]})}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:o(`inventory.show`)}),(0,Z.jsxs)(`select`,{value:t,onChange:e=>a(Number(e.target.value)),className:`px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green`,children:[[15,30,50,100].map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e)),(0,Z.jsx)(`option`,{value:-1,children:o(`inventory.all`)})]}),!s&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`button`,{onClick:()=>i(0),disabled:e===0,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(k,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>i(Math.max(0,e-1)),disabled:e===0,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(xe,{className:`w-4 h-4`})}),(0,Z.jsxs)(`span`,{className:`text-bambu-gray px-2 whitespace-nowrap`,children:[o(`inventory.page`),` `,e+1,` `,o(`inventory.of`),` `,r]}),(0,Z.jsx)(`button`,{onClick:()=>i(Math.min(r-1,e+1)),disabled:e>=r-1,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(Ie,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>i(r-1),disabled:e>=r-1,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(A,{className:`w-4 h-4`})})]})]})]})}function ur({spool:e,remaining:t,pct:n,onClick:r,onPrintLabel:i,onCopy:a,t:o,colorizeLocationSensors:s,locationSensorAboveColor:c,locationSensorBelowColor:l,locationSensorOptimalColor:u,dateFormat:d}){let f=ve({rgba:e.rgba,extraColors:e.extra_colors,effectType:e.effect_type,subtype:e.subtype,effectSize:`card`});return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary hover:border-bambu-green transition-colors cursor-pointer ${e.archived_at?`opacity-50`:``}`,onClick:r,children:[(0,Z.jsxs)(`div`,{className:`h-14 flex items-center justify-center`,style:f,children:[(0,Z.jsx)(`span`,{className:`bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium`,children:Ne(e.color_name,e.rgba,e.color_name_is_synthesized)||`-`}),a&&(0,Z.jsx)(`button`,{type:`button`,onClick:e=>{e.stopPropagation(),a()},className:`p-1.5 bg-black/20 hover:bg-black/40 text-white rounded-full transition-colors`,title:o(`inventory.copySpool`),children:(0,Z.jsx)(N,{className:`w-3.5 h-3.5`})})]}),(0,Z.jsxs)(`div`,{className:`p-4 space-y-3`,children:[(0,Z.jsxs)(`div`,{className:`flex items-start justify-between gap-2`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`h3`,{className:`font-semibold text-white`,children:[e.material,e.subtype?` ${e.subtype}`:``]}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:e.brand||`-`})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-1`,children:[i&&(0,Z.jsx)(`button`,{onClick:e=>{e.stopPropagation(),i()},className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,title:o(`inventory.labels.printOne`),"aria-label":o(`inventory.labels.printOne`),children:(0,Z.jsx)(Xe,{className:`w-4 h-4`})}),(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded`,children:[`#`,e.id]})]})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`div`,{className:`flex justify-between text-xs text-bambu-gray mb-1`,children:[(0,Z.jsx)(`span`,{children:o(`inventory.remaining`)}),(0,Z.jsxs)(`span`,{children:[Math.round(n),`%`]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${n>50?`bg-bambu-green`:n>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(n,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(t),`g`]})]})]}),(0,Z.jsxs)(`div`,{className:`grid grid-cols-2 gap-2 text-xs`,children:[e.last_dried_at&&(0,Z.jsxs)(`div`,{className:`col-span-2`,children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.lastDried`),`: `]}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:[er(e.last_dried_at,d),$n(e)].filter(Boolean).join(` · `)})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.labelWeight`),`: `]}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:Zn(e.label_weight)})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.weightUsed`),`: `]}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:e.weight_used>0?Zn(e.weight_used):`-`})]})]}),e.location_id&&(0,Z.jsx)(mr,{locationId:e.location_id,locationName:e.storage_location??null,isLast:!e.note,colorize:s,aboveColor:c,belowColor:l,optimalColor:u}),e.note&&(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray/60 pt-3 border-t border-bambu-dark-tertiary truncate`,title:e.note,children:e.note})]})]})}var dr={temperature:0,humidity:1,battery:2};function fr(e){return e===`humidity`?`mr-[2px]`:e===`battery`?`mr-[3px]`:``}function pr(e,t){return h(e,t,{decimals:2})}function $(e,t,n,r,i){return t&&L(ie(e),n,r,i)||`text-bambu-gray`}function mr({locationId:e,locationName:t,isLast:n,colorize:a,aboveColor:o,belowColor:s,optimalColor:c}){let{t:l}=r(),{data:u}=Ce(),f=(u?.location_sensor_poll_interval||120)*1e3,{data:p}=d({queryKey:[`locationHaSensors`],queryFn:()=>i.getLocationHASensors()}),m=(p??[]).some(t=>t.location_id===e),{data:h}=d({queryKey:[`locationHaSensorReadings`,e],queryFn:()=>i.getLocationHASensorReadings(e,!1),refetchInterval:f,enabled:m}),_=h?.filter(e=>e.show_on_card);if(!_?.length)return null;let v=_.find(e=>e.device_class===`battery`),y=_.filter(e=>e.device_class!==`battery`).sort((e,t)=>(dr[e.device_class??``]??99)-(dr[t.device_class??``]??99));return(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 pt-3 border-t border-bambu-dark-tertiary text-xs text-bambu-gray ${n?`-mb-1`:``}`,children:[t&&(0,Z.jsx)(`span`,{className:`truncate`,children:t}),(0,Z.jsx)(`span`,{className:`text-bambu-gray/40`,children:`|`}),(0,Z.jsx)(`div`,{className:`flex items-center gap-3`,children:y.map((e,t)=>{let n=g(e),r=t===0?`ml-[-3.6px]`:``;return(0,Z.jsxs)(`span`,{title:e.name,className:`flex items-center gap-[3px]`,children:[(0,Z.jsx)(n,{className:`w-3 h-3 ${fr(e.device_class)} ${r}`}),(0,Z.jsx)(`span`,{className:$(e,a,o,s,c),children:pr(e,l)})]},e.id)})}),v&&(()=>{let e=g(v);return(0,Z.jsxs)(`span`,{title:v.name,className:`flex items-center gap-[3px] ml-auto`,children:[(0,Z.jsx)(e,{className:`w-3 h-3 ${fr(v.device_class)}`}),(0,Z.jsx)(`span`,{className:$(v,a,o,s,c),children:pr(v,l)})]},v.id)})()]})}function hr({spool:e,remaining:t,pct:n,isSelected:r,onToggleSelected:i,onEdit:a,onCopy:o,onRestore:s,onArchive:c,onDelete:l,onPrintLabel:u,onResetConsumedCounter:d,visibleColumns:f,assignmentMap:p,catalogMap:m,locationReadingsMap:h,currencySymbol:g,dateFormat:_,t:v,onSyncWeight:y,colorizeLocationSensors:b,locationSensorAboveColor:x,locationSensorBelowColor:C,locationSensorOptimalColor:w}){return(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer ${e.archived_at?`opacity-50`:``} ${r?`bg-bambu-green/10`:``}`,onClick:a,children:[(0,Z.jsx)(`td`,{className:`w-10 px-3 py-3`,onClick:e=>e.stopPropagation(),children:i&&(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":v(`inventory.bulk.selectRow`),checked:!!r,onChange:i})}),f.map(r=>(0,Z.jsx)(`td`,{className:`py-3 px-4`,children:nr[r]?.({spool:e,remaining:t,pct:n,assignmentMap:p,catalogMap:m,locationReadingsMap:h,currencySymbol:g,dateFormat:_,t:v,onSyncWeight:y,colorizeLocationSensors:b,locationSensorAboveColor:x,locationSensorBelowColor:C,locationSensorOptimalColor:w})},r)),(0,Z.jsx)(`td`,{className:`py-3 px-4`,children:(0,Z.jsxs)(`div`,{className:`flex items-center justify-end gap-1`,onClick:e=>e.stopPropagation(),children:[(0,Z.jsx)(`button`,{onClick:a,className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:v(`common.edit`),children:(0,Z.jsx)(I,{className:`w-4 h-4`})}),o&&(0,Z.jsx)(`button`,{onClick:o,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:v(`inventory.copySpool`),children:(0,Z.jsx)(N,{className:`w-4 h-4`})}),u&&(0,Z.jsx)(`button`,{onClick:u,className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:v(`inventory.labels.printOne`),children:(0,Z.jsx)(Xe,{className:`w-4 h-4`})}),d&&e.weight_used>0&&(0,Z.jsx)(`button`,{onClick:d,className:`p-1.5 text-bambu-gray hover:text-orange-600 dark:hover:text-orange-400 rounded transition-colors`,title:v(`inventory.resetConsumedCounterTooltip`),children:(0,Z.jsx)(S,{className:`w-4 h-4`})}),e.archived_at?(0,Z.jsx)(`button`,{onClick:s,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:v(`inventory.restore`),children:(0,Z.jsx)(He,{className:`w-4 h-4`})}):(0,Z.jsx)(`button`,{onClick:c,className:`p-1.5 text-bambu-gray hover:text-yellow-600 dark:hover:text-yellow-400 rounded transition-colors`,title:v(`inventory.archive`),children:(0,Z.jsx)(we,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:l,className:`p-1.5 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 rounded transition-colors`,title:v(`common.delete`),children:(0,Z.jsx)(et,{className:`w-4 h-4`})})]})})]})}function gr({spools:e,headerSpool:t,remaining:n,pct:r,isExpanded:i,onToggle:a,onEdit:o,onCopy:s,onArchive:c,onRestore:l,onDelete:u,onPrintLabel:d,onResetConsumedCounter:f,visibleColumns:p,assignmentMap:m,catalogMap:h,locationReadingsMap:g,currencySymbol:_,dateFormat:v,t:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:w,selectedIds:E,onToggleSelected:D,onToggleGroupSelected:O}){let k=!!E&&e.every(e=>E.has(e.id));return(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer bg-bambu-green/5`,onClick:a,children:[(0,Z.jsx)(`td`,{className:`w-10 px-3 py-3`,onClick:e=>e.stopPropagation(),children:O&&(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":y(`inventory.bulk.selectGroup`),checked:k,onChange:t=>O(e.map(e=>e.id),t.target.checked)})}),p.map((a,o)=>(0,Z.jsx)(`td`,{className:`py-3 px-4`,children:o===0?(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(T,{className:`w-4 h-4 text-bambu-gray transition-transform ${i?``:`-rotate-90`}`}),nr[a]?.({spool:t,remaining:n,pct:r,assignmentMap:m,catalogMap:h,locationReadingsMap:g,currencySymbol:_,dateFormat:v,t:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:w})]}):a===`id`?(0,Z.jsx)(`span`,{className:`text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full`,children:y(`inventory.groupedSpools`,{count:e.length})}):nr[a]?.({spool:t,remaining:n,pct:r,assignmentMap:m,catalogMap:h,locationReadingsMap:g,currencySymbol:_,dateFormat:v,t:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:w})},a)),(0,Z.jsx)(`td`,{className:`py-3 px-4`,children:(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:e.map(e=>`#${e.id}`).join(`, `)})})]}),i&&e.map(e=>{let t=Math.max(0,e.label_weight-e.weight_used);return(0,Z.jsx)(hr,{spool:e,remaining:t,pct:e.label_weight>0?t/e.label_weight*100:0,isSelected:E?.has(e.id),onToggleSelected:D?()=>D(e.id):void 0,onEdit:()=>o(e),onCopy:s?()=>s(e):void 0,onRestore:()=>l(e.id),onArchive:()=>c(e.id),onDelete:()=>u(e.id),onPrintLabel:d?()=>d(e.id):void 0,onResetConsumedCounter:f?()=>f(e.id):void 0,visibleColumns:p,assignmentMap:m,catalogMap:h,locationReadingsMap:g,currencySymbol:_,dateFormat:v,t:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:w},e.id)})]})}function _r({hasFilters:e,onAddSpool:t,t:n}){return(0,Z.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 px-4`,children:[(0,Z.jsxs)(`div`,{className:`relative mb-6`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl`}),(0,Z.jsxs)(`div`,{className:`relative flex items-center justify-center w-24 h-24 rounded-2xl bg-gradient-to-br from-bambu-dark-secondary to-bambu-dark-tertiary border border-bambu-dark-tertiary shadow-lg`,children:[(0,Z.jsx)(`div`,{className:`absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30`}),(0,Z.jsx)(`div`,{className:`absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20`}),e?(0,Z.jsx)(se,{className:`w-10 h-10 text-bambu-gray/40`,strokeWidth:1.5}):(0,Z.jsxs)(`div`,{className:`relative`,children:[(0,Z.jsx)(`div`,{className:`w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center`,children:(0,Z.jsx)(`div`,{className:`w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20`})}),(0,Z.jsx)(`div`,{className:`absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-bambu-green flex items-center justify-center shadow-md`,children:(0,Z.jsx)(`span`,{className:`text-white text-lg font-bold leading-none`,children:`+`})})]})]})]}),(0,Z.jsx)(`h3`,{className:`text-lg font-semibold text-white mb-2 text-center`,children:e?n(`inventory.noSpoolsMatch`):n(`inventory.noSpools`).split(`.`)[0]}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray text-center max-w-sm mb-6`,children:n(e?`inventory.noSpoolsMatchDesc`:`inventory.noSpools`)}),!e&&(0,Z.jsxs)(H,{onClick:t,children:[(0,Z.jsx)(oe,{className:`w-4 h-4`}),n(`inventory.addSpool`)]})]})}export{sr as default};
|