| 12 |
- import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{C as t,T as n,a as r,k as i,n as a,r as o,t as s,v as c,w as l}from"./client-D0DsfzyI.js";import{f as u}from"./chunk-62JRHF6Z-BE3YOxZ-.js";import{n as d}from"./QueryClientProvider-7fwlhGqG.js";import{t as f}from"./useQuery-V3hrDXyt.js";import{t as p}from"./useMutation-CIV8rT2m.js";import{t as m}from"./useQueries-DRfEnQ4k.js";import{i as h,n as g,r as _}from"./haSensorDisplay-BTTP2A3O.js";import{t as v}from"./archive-DIC0z-vH.js";import{t as y}from"./arrow-down-B9381F0Y.js";import{t as b}from"./arrow-up-down-Cx2VtGsK.js";import{t as x}from"./arrow-up-DOjUXtxy.js";import{i as S,n as C,r as w,t as T}from"./ReferenceLine-BZ6ds6UO.js";import{t as E}from"./check-ALrKwZCy.js";import{a as D,i as O,n as k,t as A}from"./info-CyyLzSyY.js";import{t as ee}from"./chevron-right-VCpkK2iX.js";import{t as te}from"./chevron-left-Yr2zjJKj.js";import{n as j,t as ne}from"./chevrons-right-BSYKMGf_.js";import{n as re,r as ie}from"./ToastContext-DweVvsym.js";import{t as M}from"./circle-minus-CU7laNBJ.js";import{t as ae}from"./clock-Ph8uouMp.js";import{t as oe}from"./columns-2-B2vX8NZx.js";import{t as N}from"./copy-CrnSJ7fc.js";import{t as P}from"./download-wPA-7Cg0.js";import{t as F}from"./external-link-YIUWrW3M.js";import{t as I}from"./file-text-BAMy6mBm.js";import{t as se}from"./layers-ZQ5ZDFAk.js";import{t as ce}from"./layout-grid-B5nuOcJs.js";import{t as le}from"./link-2-CGHuas6-.js";import{t as L}from"./loader-circle-BBaMq1FO.js";import{t as ue}from"./lock-DXk8Mit5.js";import{t as de}from"./map-pin-O1Z2Tp9R.js";import{t as fe}from"./package-DzvAZ8xd.js";import{t as pe}from"./palette-BIGOzW3q.js";import{_ as me,c as he,g as ge,h as _e,s as ve}from"./locationSensorDefaults-B_g9hfac.js";import{t as ye}from"./pencil-D1HyG8ie.js";import{t as be}from"./plus-CWC_e4-u.js";import{t as xe}from"./printer-B5PznyTY.js";import{t as Se}from"./qr-code-BdtPLvrY.js";import{t as Ce}from"./refresh-cw-CzhatsWh.js";import{t as we}from"./rotate-ccw-BkmUek-t.js";import{t as Te}from"./save-DMYmLuA6.js";import{t as Ee}from"./search-DzZ_Dp-L.js";import{t as R}from"./shopping-cart-C_Qff3rQ.js";import{t as De}from"./sparkles-C2kpvkVz.js";import{t as Oe}from"./square-check-big-D1UxNlKU.js";import{t as ke}from"./square-C4ISZdTw.js";import{t as Ae}from"./tag-PuGsAKNJ.js";import{t as z}from"./trash-2-Cb2lU-A1.js";import{L as B,Rn as je,Xt as Me,c as Ne,p as Pe,s as V}from"./CartesianChart-Dy1ffPu6.js";import{t as H}from"./triangle-alert-CQjQSYqL.js";import{t as Fe}from"./unlink-BK5zTwpX.js";import{t as Ie}from"./upload-CTkcx0Ze.js";import{t as U}from"./wand-sparkles-DUF_2nqC.js";import{t as Le}from"./x-ggs7VM0r.js";import{t as Re}from"./zap-6VNvVvJ9.js";import{n as ze}from"./AuthContext-Dv62aHTL.js";import{t as W}from"./Button-BNugOc_r.js";import{t as Be}from"./ConfirmModal-Fme0QPE6.js";import{r as G,v as Ve,y as He}from"./date-G9KUauqz.js";import{p as Ue}from"./amsHelpers-DDsrDUnz.js";import{a as K,c as q,t as We}from"./colors-BBBvvSKR.js";import{t as Ge}from"./useColorCatalogVersion-B5sA4KTZ.js";import{n as Ke}from"./currency-LmqFrzn3.js";import{n as qe}from"./useUiSettings-BQUmq34q.js";import{n as Je}from"./filamentSwatchHelpers-C3r5m07g.js";import{t as Ye}from"./FilamentSwatch-Dk24MBVV.js";import{a as Xe,c as J,i as Ze,o as Qe,s as $e}from"./index-DWPuhOQT.js";import{i as et,r as tt}from"./slicerPrinterMatch-CUdoNag2.js";import{t as nt}from"./ColumnConfigModal-C0MqaOb7.js";import{n as rt,t as it}from"./AreaChart-CaiEhTRe.js";import{a as at,i as ot,n as st,o as ct,r as lt,s as ut,t as Y}from"./AdditionalSection-D6T5xKTV.js";import{S as dt,a as ft,b as pt,c as mt,d as ht,f as gt,h as _t,i as vt,l as yt,n as bt,o as xt,p as St,r as Ct,s as wt,t as Tt,u as Et,v as Dt,y as Ot}from"./utils-BCMnjJsD.js";import{n as kt,r as At,t as X}from"./nozzleFlow-DwF_jePd.js";import{t as jt}from"./AssignToAmsModal-DHVxJ--C.js";import{n as Mt}from"./inventorySearch-DctUdLGi.js";import{n as Nt,t as Pt}from"./spoolCost-BlxK3a05.js";var Ft=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`}]]),It=t(`chart-no-axes-column`,[[`path`,{d:`M5 21v-6`,key:`1hz6c0`}],[`path`,{d:`M12 21V3`,key:`1lcnhd`}],[`path`,{d:`M19 21V9`,key:`unv183`}]]),Lt=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`}]]),Rt=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`}]]),zt=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`}]]),Bt=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`}]]),Vt=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`}]]),Z=e(i(),1);function Ht(e){let t=e?Ve(e)?.getTime():void 0;return t===void 0||Number.isNaN(t)?null:t}function Ut(e,t){let n=Ht(e.created_at);return n===null?t:t.filter(e=>(Ht(e.created_at)??n)>=n)}var Q=l(),Wt=90;function Gt(e,t){let n=Ve(e)?.getTime();return n!==void 0&&!Number.isNaN(n)&&t-n<=Wt*864e5}var Kt=1.65,qt=[`#1DB954`,`#3B82F6`,`#F59E0B`,`#EF4444`,`#8B5CF6`];function Jt(e,t,n,r){return`${e}||${t??``}||${n??``}||${r??``}`}function Yt(e,t,n,r){return[e,t,n,r&&n&&r.trim().toLowerCase()===n.trim().toLowerCase()?null:r].filter(Boolean).join(` `)}function Xt(e,t){let n=new Date(e);return n.setDate(n.getDate()+Math.round(t)),n}function Zt(e){return e.toLocaleDateString(void 0,{month:`short`,day:`numeric`,year:`numeric`})}function Qt(e){return e.toLocaleDateString(void 0,{month:`short`,day:`numeric`})}function $t(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 en(e,t,n,r){let i=[];for(let n of e){let e=Math.max(0,n.weight_used-(n.weight_used_baseline??0));if(e<=0)continue;let a=Ht(n.last_used)??Ht(n.archived_at),o=a===null?r:Math.min(a,r),s=Ht(n.first_used)??t.get(n.id)??Ht(n.created_at),c=s===null?o:Math.min(s,o);i.push({consumed:e,start:c,end:o})}if(i.length===0)return null;let a=r-Wt*864e5,o=n?a:Math.max(a,Math.min(...i.map(e=>e.start))),s=(r-o)/864e5;if(s<1)return null;let c=0;for(let{consumed:e,start:t,end:n}of i){let r=n-t;if(r<864e5){n>=o&&(c+=e);continue}let i=n-Math.max(t,o);i>0&&(c+=e*i/r)}return c/s}function tn(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:Qt(Xt(new Date,i)),stock:Math.round(t),rop:Math.round(e.reorderPointG)}),t===0)break}return r}function nn({spools:e,spoolmanMode:t=!1}){let i=d(),{showToast:a}=re(),{t:o}=n(),{hasPermission:s,hasAnyPermission:c}=ze(),l=s(`inventory:forecast_read`),u=c(`inventory:forecast_write`,`inventory:update`),[p,m]=(0,Z.useState)(!1),[h,g]=(0,Z.useState)(`material`),[_,v]=(0,Z.useState)(`asc`),[y,b]=(0,Z.useState)(``),[x,S]=(0,Z.useState)(``),[C,w]=(0,Z.useState)(null),[T,E]=(0,Z.useState)(!1),[k,A]=(0,Z.useState)(!1),[ee,te]=(0,Z.useState)(30),{data:j}=qe({enabled:l}),{data:ne=[]}=f({queryKey:[`sku-settings`],queryFn:r.getSkuSettings,staleTime:6e4,enabled:l}),{data:ie=[]}=f({queryKey:[`all-usage-history-forecast`,t],queryFn:()=>r.getAllUsageHistory(5e3,void 0,t),staleTime:6e4,enabled:l}),{data:M=[]}=f({queryKey:[`shopping-list`],queryFn:r.getShoppingList,staleTime:3e4,enabled:l}),ae=j?.forecast_global_lead_time_days??0,oe=(0,Z.useMemo)(()=>{let e=new Map;for(let t of ne)e.set(Jt(t.material,t.subtype,t.brand,t.color_name),t);return e},[ne]),N=(0,Z.useMemo)(()=>{let e=new Map;for(let n of ie){let r=t?n.spoolman_spool_id:n.spool_id;if(r==null)continue;let i=e.get(r)??[];i.push(n),e.set(r,i)}return e},[ie,t]),P=(0,Z.useMemo)(()=>{let t=new Map;for(let n of e){if(n.archived_at)continue;let e=Jt(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:[],usedUp:[],usedBefore:!1};r.spools.push(n),t.set(e,r)}let n=Date.now();for(let r of e){if(!r.archived_at)continue;let e=t.get(Jt(r.material,r.subtype,r.brand,r.color_name));e&&(Gt(r.archived_at,n)?e.usedUp.push(r):r.weight_used>0&&(e.usedBefore=!0))}return[...t.values()]},[e]),F=(0,Z.useMemo)(()=>{let e=new Date;e.setHours(0,0,0,0);let n=Date.now();return P.map(r=>{let i=oe.get(r.key)??(r.colorName===null?null:oe.get(Jt(r.material,r.subtype,r.brand,null))??null),a=i?.lead_time_days??0,o=Math.max(ae,a),s=i?.safety_margin_value??14,c=i?.safety_margin_unit??`days`,l=r.spools.reduce((e,t)=>e+Math.max(0,t.label_weight-t.weight_used),0),u=r.spools.reduce((e,t)=>e+t.label_weight,0),d=[...r.spools,...r.usedUp],f=d.reduce((e,t)=>e+Math.max(0,t.weight_used-(t.weight_used_baseline??0)),0),p=new Map(d.map(e=>{let n=N.get(e.id)??[];return[e.id,t?Ut(e,n):n]})),m=[];for(let e of d)(e.weight_used_baseline??0)===0&&m.push(...p.get(e.id)??[]);let h=null,g=null,_=`none`,v=$t(m);if(v!==null)h=v.rate,g=v.stdDev,_=`history`;else{let e=new Map;for(let t of d)for(let n of p.get(t.id)??[]){let r=Ht(n.created_at);r!==null&&r<(e.get(t.id)??1/0)&&e.set(t.id,r)}let t=en(d,e,r.usedBefore,n);t!==null&&(h=t,_=`delta`)}let y=Kt*(g??(h===null?0:h*.2))*Math.sqrt(o)+(c===`g`?s:h===null?s*5:h*s),b=h===null?0:h*o+y,x=h&&h>0?Math.floor(l/h):null,S=x===null?null:Xt(e,x),C=h&&h>0?Math.floor((l-b)/h):null,w=C===null?null:Xt(e,Math.max(0,C)),T=x!==null&&o>0&&x<=o,E=!T&&C!==null&&C<=0;return{group:r,settings:i,totalRemainingG:l,totalLabelG:u,totalSpools:r.spools.length,totalUsedG:f,dailyRateG:h,dailyRateStdDev:g,rateTier:_,effectiveLeadTimeDays:o,safetyStockG:y,reorderPointG:b,daysRemaining:x,daysUntilROP:C,projectedEmptyDate:S,reorderTriggerDate:w,reorderAlert:E,stockBreakAlert:T}})},[P,oe,N,ae,t]),I=(0,Z.useMemo)(()=>[...new Set(P.map(e=>e.material))].sort(),[P]),se=(0,Z.useMemo)(()=>[...new Set(P.map(e=>e.brand).filter(Boolean))].sort(),[P]),ce=(0,Z.useMemo)(()=>{let e=[...F];return y&&(e=e.filter(e=>e.group.material===y)),x&&(e=e.filter(e=>e.group.brand===x)),e.sort((e,t)=>{let n=0,r=0;switch(h){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 _===`asc`?i:-i}),e},[F,h,_,y,x]),le=(0,Z.useMemo)(()=>F.filter(e=>!e.settings?.alerts_snoozed&&(e.stockBreakAlert||e.reorderAlert)),[F]),L=(0,Z.useMemo)(()=>[...F].filter(e=>e.dailyRateG!==null).sort((e,t)=>t.totalUsedG-e.totalUsedG).slice(0,5),[F]);if(!l)return(0,Q.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 text-bambu-gray gap-3`,children:[(0,Q.jsx)(ue,{className:`w-8 h-8 opacity-40`}),(0,Q.jsx)(`p`,{className:`text-sm`,children:o(`forecast.noReadAccess`)})]});function de(e){h===e?v(e=>e===`asc`?`desc`:`asc`):(g(e),v(e===`days_left`?`asc`:`desc`))}let fe=M.length>0?M.length:null;return(0,Q.jsxs)(`div`,{className:`space-y-5`,children:[(0,Q.jsxs)(`div`,{className:`flex flex-wrap items-center gap-3`,children:[le.length>0&&(0,Q.jsxs)(`button`,{onClick:()=>m(e=>!e),className:`flex items-center gap-2 px-3 py-1.5 rounded-lg border text-sm font-medium transition-colors ${le.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,Q.jsx)(H,{className:`w-4 h-4`}),o(`forecast.alertCount`,{count:le.length}),p?(0,Q.jsx)(O,{className:`w-3.5 h-3.5`}):(0,Q.jsx)(D,{className:`w-3.5 h-3.5`})]}),u&&(0,Q.jsx)(cn,{value:ae,onSave:e=>{r.updateSettings({forecast_global_lead_time_days:e}).then(()=>{i.invalidateQueries({queryKey:[`settings`]}),a(o(`forecast.globalLeadTimeSaved`),`success`)})}}),(0,Q.jsxs)(`select`,{value:y,onChange:e=>b(e.target.value),className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${y?`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,Q.jsx)(`option`,{value:``,children:o(`inventory.material`)}),I.map(e=>(0,Q.jsx)(`option`,{value:e,children:e},e))]}),(0,Q.jsxs)(`select`,{value:x,onChange:e=>S(e.target.value),className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${x?`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,Q.jsx)(`option`,{value:``,children:o(`inventory.brand`)}),se.map(e=>(0,Q.jsx)(`option`,{value:e,children:e},e))]}),(0,Q.jsxs)(`button`,{onClick:()=>E(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,Q.jsx)(R,{className:`w-4 h-4`}),(0,Q.jsx)(`span`,{className:`hidden sm:inline`,children:o(`forecast.shoppingList`)}),fe&&(0,Q.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:fe})]})]}),p&&le.length>0&&(0,Q.jsx)(`div`,{className:`space-y-2`,children:le.map(e=>(0,Q.jsx)(an,{forecast:e,onCart:()=>w(e)},e.group.key))}),T&&(0,Q.jsx)(pn,{items:M,forecasts:F,globalLeadTime:ae,canWrite:u,onClose:()=>E(!1),onRemove:e=>{r.removeFromShoppingList(e).then(()=>i.invalidateQueries({queryKey:[`shopping-list`]})).catch(()=>a(o(`forecast.failedSaveSettings`),`error`))},onClear:()=>A(!0)}),k&&(0,Q.jsx)(Be,{title:o(`forecast.clearAllConfirmTitle`),message:o(`forecast.clearAllConfirmMessage`,{count:M.length}),confirmText:o(`forecast.clearAll`),variant:`danger`,onConfirm:()=>{A(!1),r.clearShoppingList().then(()=>i.invalidateQueries({queryKey:[`shopping-list`]})).catch(()=>a(o(`forecast.failedSaveSettings`),`error`))},onCancel:()=>A(!1)}),L.length>0&&(0,Q.jsx)(sn,{forecasts:L,days:ee,onDaysChange:te}),F.length===0?(0,Q.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 text-bambu-gray`,children:[(0,Q.jsx)(je,{className:`w-10 h-10 mb-3 opacity-40`}),(0,Q.jsx)(`p`,{className:`text-sm`,children:o(`forecast.noSpools`)})]}):(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Q.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Q.jsxs)(`table`,{className:`w-full`,children:[(0,Q.jsx)(`thead`,{children:(0,Q.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Q.jsx)(`th`,{className:`w-8 px-4 py-3`}),(0,Q.jsx)(rn,{col:`material`,active:h,dir:_,onSort:de,children:o(`forecast.sku`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:o(`locations.spools`)}),(0,Q.jsx)(rn,{col:`stock`,active:h,dir:_,onSort:de,children:o(`forecast.stock`)}),(0,Q.jsx)(rn,{col:`used`,active:h,dir:_,onSort:de,children:o(`forecast.dailyRate`)}),(0,Q.jsx)(rn,{col:`days_left`,active:h,dir:_,onSort:de,children:o(`forecast.daysLeft`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:o(`forecast.emptyBy`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:o(`forecast.reorderBy`)}),(0,Q.jsx)(`th`,{className:`w-24 px-4 py-3`})]})}),(0,Q.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:ce.map(e=>(0,Q.jsx)(ln,{forecast:e,globalLeadTime:ae,canWrite:u,onSaved:()=>i.invalidateQueries({queryKey:[`sku-settings`]}),onCart:()=>w(e),showToast:a},e.group.key))})]})}),(0,Q.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,Q.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-bambu-green inline-block`}),o(`forecast.trendLegend`)]}),(0,Q.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-blue-400 inline-block`}),o(`forecast.estimatedLegend`)]}),(0,Q.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-bambu-gray/40 inline-block`}),o(`forecast.noDataLegend`)]})]})]}),C&&(0,Q.jsx)(hn,{forecast:C,onClose:()=>w(null),onAdd:e=>{r.addToShoppingList(e).then(()=>{i.invalidateQueries({queryKey:[`shopping-list`]}),a(o(`forecast.addedToCart`),`success`),w(null),E(!0)}).catch(()=>a(o(`forecast.failedAddItem`),`error`))}})]})}function rn({col:e,active:t,dir:n,onSort:r,children:i}){return(0,Q.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,Q.jsxs)(`span`,{className:`inline-flex items-center`,children:[i,t===e?n===`asc`?(0,Q.jsx)(x,{className:`w-3 h-3 ml-1 text-bambu-green`}):(0,Q.jsx)(y,{className:`w-3 h-3 ml-1 text-bambu-green`}):(0,Q.jsx)(b,{className:`w-3 h-3 ml-1 opacity-40`})]})})}function an({forecast:e,onCart:t}){let{t:r}=n(),i=Yt(e.group.brand,e.group.material,e.group.subtype,e.group.colorName),a=e.stockBreakAlert;return(0,Q.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,Q.jsx)(H,{className:`w-4 h-4 flex-shrink-0`}),(0,Q.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Q.jsx)(`span`,{className:`font-medium`,children:i}),a?(0,Q.jsxs)(`span`,{className:`ml-2 text-xs opacity-80`,children:[r(`forecast.stockBreakRisk`),` — `,r(`forecast.stockBreakDetail`,{days:e.daysRemaining,lt:e.effectiveLeadTimeDays})]}):(0,Q.jsxs)(`span`,{className:`ml-2 text-xs opacity-80`,children:[r(`forecast.reorderNow`),` — `,r(`forecast.reorderTriggerPassed`,{date:e.reorderTriggerDate?Zt(e.reorderTriggerDate):`—`})]})]}),(0,Q.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,Q.jsx)(R,{className:`w-3 h-3`}),` `,r(`forecast.order`)]})]})}var on=[{label:`1W`,value:7},{label:`1M`,value:30},{label:`6M`,value:180}];function sn({forecasts:e,days:t,onDaysChange:r}){let{t:i}=n(),a=Array.from({length:t+1},(e,t)=>t),o=e.map((e,n)=>({key:e.group.key,label:Yt(e.group.brand,e.group.material,e.group.subtype,e.group.colorName),color:qt[n%qt.length],rop:e.reorderPointG,points:tn(e,t)})),s=a.map(e=>{let t={day:e,label:Qt(Xt(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,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary p-4`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 mb-4`,children:[(0,Q.jsx)(je,{className:`w-4 h-4 text-bambu-green`}),(0,Q.jsx)(`h3`,{className:`text-sm font-semibold text-white`,children:i(`forecast.chartTitle`)}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray ml-1 hidden sm:inline`,children:i(`forecast.dashedLinesROP`)}),(0,Q.jsx)(`div`,{className:`ml-auto flex items-center bg-bambu-dark-tertiary rounded-lg p-0.5`,children:on.map(e=>(0,Q.jsx)(`button`,{onClick:()=>r(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,Q.jsx)(Me,{width:`100%`,height:220,children:(0,Q.jsxs)(it,{data:l,margin:{top:4,right:8,bottom:0,left:0},children:[(0,Q.jsx)(`defs`,{children:o.map(e=>(0,Q.jsxs)(`linearGradient`,{id:`grad-${e.key}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Q.jsx)(`stop`,{offset:`5%`,stopColor:e.color,stopOpacity:.25}),(0,Q.jsx)(`stop`,{offset:`95%`,stopColor:e.color,stopOpacity:.02})]},e.key))}),(0,Q.jsx)(Pe,{strokeDasharray:`3 3`,stroke:`#374151`,strokeOpacity:.5}),(0,Q.jsx)(Ne,{dataKey:`label`,tick:{fill:`#6B7280`,fontSize:10},interval:Math.max(0,Math.ceil(c/8)-1),axisLine:!1,tickLine:!1}),(0,Q.jsx)(V,{tick:{fill:`#6B7280`,fontSize:10},axisLine:!1,tickLine:!1,tickFormatter:e=>e>=1e3?`${(e/1e3).toFixed(1)}kg`:`${e}g`,width:48}),(0,Q.jsx)(B,{content:({label:e,payload:t})=>t?.length?(0,Q.jsxs)(`div`,{style:{background:`#1a1a2e`,border:`1px solid #374151`,borderRadius:8,fontSize:12,padding:`8px 12px`},children:[(0,Q.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,Q.jsxs)(`div`,{style:{display:`flex`,alignItems:`center`,gap:6,color:`#E5E7EB`,marginBottom:2},children:[(0,Q.jsx)(`span`,{style:{color:t?.color??`#9CA3AF`,fontSize:10},children:`●`}),(0,Q.jsx)(`span`,{children:t?.label??String(e.dataKey)}),(0,Q.jsxs)(`span`,{style:{color:`#9CA3AF`,marginLeft:4},children:[e.value,`g`]})]},String(e.dataKey)):null})]}):null}),(0,Q.jsx)(C,{formatter:e=>(0,Q.jsx)(`span`,{style:{color:`#9CA3AF`,fontSize:11},children:o.find(t=>t.key===e)?.label??e})}),o.map(e=>(0,Q.jsx)(rt,{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,Q.jsx)(T,{y:e.rop,stroke:e.color,strokeDasharray:`4 3`,strokeOpacity:.6},`rop-${e.key}`))]})})]})}function cn({value:e,onSave:t}){let{t:r}=n(),[i,a]=(0,Z.useState)(!1),[o,s]=(0,Z.useState)(String(e));function c(){let e=parseInt(o,10);isNaN(e)||e<0||(t(e),a(!1))}return(0,Q.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,Q.jsx)(A,{className:`w-3.5 h-3.5 flex-shrink-0`,"aria-label":r(`forecast.globalLeadTimeHint`)}),(0,Q.jsxs)(`span`,{className:`hidden sm:inline`,children:[r(`forecast.globalLeadTime`),`:`]}),i?(0,Q.jsxs)(`form`,{className:`flex items-center gap-1.5`,onSubmit:e=>{e.preventDefault(),c()},children:[(0,Q.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,Q.jsx)(`span`,{className:`text-bambu-gray`,children:`d`}),(0,Q.jsx)(`button`,{type:`submit`,className:`px-2 py-0.5 bg-bambu-green text-white text-xs rounded hover:bg-bambu-green/80`,children:r(`forecast.save`)}),(0,Q.jsx)(`button`,{type:`button`,onClick:()=>a(!1),className:`text-xs text-bambu-gray hover:text-white`,children:`✕`})]}):(0,Q.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Q.jsxs)(`span`,{className:`font-semibold text-white`,children:[e,`d`]}),(0,Q.jsx)(`button`,{onClick:()=>{s(String(e)),a(!0)},className:`p-0.5 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Q.jsx)(me,{className:`w-3 h-3`})})]})]})}function ln({forecast:e,globalLeadTime:t,canWrite:i,onSaved:a,onCart:o,showToast:s}){let{t:c}=n(),[l,u]=(0,Z.useState)(!1),[d,f]=(0,Z.useState)(!1),[m,h]=(0,Z.useState)(!1),[g,_]=(0,Z.useState)(String(e.settings?.lead_time_days??0)),[v,y]=(0,Z.useState)(String(e.settings?.safety_margin_value??14)),[b,x]=(0,Z.useState)(e.settings?.safety_margin_unit??`days`);(0,Z.useEffect)(()=>{d||_(String(e.settings?.lead_time_days??0))},[e.settings?.lead_time_days,d]),(0,Z.useEffect)(()=>{m||(y(String(e.settings?.safety_margin_value??14)),x(e.settings?.safety_margin_unit??`days`))},[e.settings?.safety_margin_value,e.settings?.safety_margin_unit,m]);let C=p({mutationFn:r.upsertSkuSettings,onSuccess:()=>{a(),s(c(`forecast.settingsSaved`),`success`)},onError:()=>s(c(`forecast.failedSaveSettings`),`error`)}),w=e.settings?.alerts_snoozed??!1,T=Yt(e.group.brand,e.group.material,e.group.subtype,e.group.colorName),k=e.group.spools[0]?.rgba?K(e.group.spools[0].rgba):{backgroundColor:`#4B5563`},A=e.totalLabelG>0?Math.round(e.totalRemainingG/e.totalLabelG*100):0,ee=w||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 te(t,n,r,i=w){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 j(t){t.stopPropagation(),te(e.settings?.lead_time_days??0,e.settings?.safety_margin_value??14,e.settings?.safety_margin_unit??`days`,!w)}let ne=e.rateTier===`history`?(0,Q.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,Q.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-bambu-green`}),c(`forecast.trend`)]}):e.rateTier===`delta`?(0,Q.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,Q.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-blue-400`}),c(`forecast.estimated`)]}):(0,Q.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,Q.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-bambu-gray/40`}),c(`forecast.noData`)]});return(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/50 cursor-pointer hover:bg-bambu-dark-tertiary/30 transition-colors ${w?``:e.stockBreakAlert?`bg-red-500/5`:e.reorderAlert?`bg-yellow-500/5`:``} ${w?`opacity-50`:``}`,onClick:()=>u(e=>!e),children:[(0,Q.jsx)(`td`,{className:`px-4 py-3`,children:(0,Q.jsx)(`span`,{className:`block w-5 h-5 rounded-full border border-black/20`,style:k})}),(0,Q.jsx)(`td`,{className:`px-4 py-3`,children:(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:T})}),(0,Q.jsx)(`td`,{className:`px-4 py-3`,children:(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.totalSpools})}),(0,Q.jsx)(`td`,{className:`px-4 py-3 min-w-[140px]`,children:(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Q.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,Q.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(e.totalRemainingG),`g`]})]})}),(0,Q.jsx)(`td`,{className:`px-4 py-3`,children:(0,Q.jsxs)(`div`,{className:`flex items-center gap-1.5 flex-wrap`,children:[(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:e.dailyRateG===null?`—`:`${e.dailyRateG.toFixed(1)}g/d`}),ne]})}),(0,Q.jsx)(`td`,{className:`px-4 py-3`,children:(0,Q.jsx)(`span`,{className:`text-sm font-semibold ${ee}`,children:e.daysRemaining===null?(0,Q.jsx)(`span`,{className:`text-bambu-gray font-normal`,children:`—`}):`${e.daysRemaining}d`})}),(0,Q.jsx)(`td`,{className:`px-4 py-3`,children:(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.projectedEmptyDate?Zt(e.projectedEmptyDate):`—`})}),(0,Q.jsx)(`td`,{className:`px-4 py-3`,children:(0,Q.jsx)(`span`,{className:`text-sm ${!w&&e.reorderAlert?`text-yellow-700 dark:text-yellow-400`:`text-bambu-gray`}`,children:e.reorderTriggerDate?Zt(e.reorderTriggerDate):`—`})}),(0,Q.jsx)(`td`,{className:`px-4 py-3`,onClick:e=>e.stopPropagation(),children:(0,Q.jsxs)(`div`,{className:`flex items-center justify-end gap-1`,children:[i&&(0,Q.jsx)(`button`,{onClick:o,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:c(`forecast.addToCart`),children:(0,Q.jsx)(R,{className:`w-4 h-4`})}),!w&&(e.stockBreakAlert?(0,Q.jsx)(H,{className:`w-4 h-4 text-red-600 dark:text-red-400`,"aria-label":c(`forecast.stockBreakRisk`)}):e.reorderAlert?(0,Q.jsx)(H,{className:`w-4 h-4 text-yellow-600 dark:text-yellow-400`,"aria-label":c(`forecast.reorderNow`)}):e.daysRemaining===null?null:(0,Q.jsx)(E,{className:`w-4 h-4 text-bambu-green/50`})),i&&(0,Q.jsx)(`button`,{onClick:j,className:`p-1 rounded transition-colors ${w?`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(w?`forecast.alertsEnabled`:`forecast.alertsSnoozed`),children:(0,Q.jsx)(S,{className:`w-3.5 h-3.5`})}),(0,Q.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,Q.jsx)(O,{className:`w-4 h-4`}):(0,Q.jsx)(D,{className:`w-4 h-4`})})]})})]}),l&&(0,Q.jsx)(`tr`,{className:`bg-bambu-dark-tertiary/10`,children:(0,Q.jsx)(`td`,{colSpan:9,className:`px-6 py-4`,children:(0,Q.jsxs)(`div`,{className:`space-y-3`,children:[(0,Q.jsxs)(`div`,{className:`grid gap-2 ${i?`grid-cols-2 sm:grid-cols-4`:`grid-cols-2`}`,children:[(0,Q.jsx)(un,{label:c(`forecast.effectiveLeadTime`),value:`${e.effectiveLeadTimeDays}d`,hint:c(`forecast.effectiveLeadTimeHint`,{global:t,sku:e.settings?.lead_time_days??0})}),(0,Q.jsx)(un,{label:c(`forecast.reorderPoint`),value:`${Math.round(e.reorderPointG)}g`,hint:c(`forecast.reorderPointHint`)}),i&&(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(dn,{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)),f(!0)},onSave:()=>{let t=parseInt(g,10);!isNaN(t)&&t>=0&&(te(t,e.settings?.safety_margin_value??14,b),f(!1))},onCancel:()=>f(!1),isPending:C.isPending,saveLabel:c(`forecast.save`),cancelLabel:c(`forecast.cancel`)}),(0,Q.jsx)(fn,{value:e.settings?.safety_margin_value??14,unit:b,editing:m,inputValue:v,dailyRateG:e.dailyRateG,onInputChange:y,onUnitChange:e=>x(e),onEdit:()=>{y(String(e.settings?.safety_margin_value??14)),x(e.settings?.safety_margin_unit??`days`),h(!0)},onSave:()=>{let t=parseInt(v,10);!isNaN(t)&&t>=0&&(te(e.settings?.lead_time_days??0,t,b),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,Q.jsxs)(`div`,{className:`border-t border-bambu-dark-tertiary pt-3`,children:[(0,Q.jsx)(`p`,{className:`text-xs text-bambu-gray mb-2`,children:c(`forecast.individualSpools`)}),(0,Q.jsx)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:(0,Q.jsxs)(`table`,{className:`w-full`,children:[(0,Q.jsx)(`thead`,{children:(0,Q.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:`#`}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.remaining`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.used`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`forecast.labelWeight`)})]})}),(0,Q.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,Q.jsxs)(`tr`,{className:`hover:bg-bambu-dark-tertiary/30 transition-colors`,children:[(0,Q.jsx)(`td`,{className:`px-4 py-2`,children:(0,Q.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray/70`,children:[`#`,e.id]})}),(0,Q.jsx)(`td`,{className:`px-4 py-2`,children:(0,Q.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Q.jsx)(`div`,{className:`w-24 h-1.5 bg-bambu-dark-tertiary rounded-full overflow-hidden flex-shrink-0`,children:(0,Q.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,Q.jsxs)(`span`,{className:`text-sm text-white`,children:[Math.round(t),`g`]})]})}),(0,Q.jsx)(`td`,{className:`px-4 py-2`,children:(0,Q.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[Math.round(Math.max(0,e.weight_used-(e.weight_used_baseline??0))),`g`]})}),(0,Q.jsx)(`td`,{className:`px-4 py-2`,children:(0,Q.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[e.label_weight,`g`]})})]},e.id)})})]})})]})]})})})]})}function un({label:e,value:t,hint:n}){return(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3`,title:n,children:[(0,Q.jsx)(`div`,{className:`text-xs font-medium text-white mb-1`,children:e}),(0,Q.jsx)(`div`,{className:`text-lg font-semibold text-white`,children:t})]})}function dn({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,Q.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3 space-y-1`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`text-xs font-medium text-white`,children:e}),(0,Q.jsx)(`span`,{title:t,children:(0,Q.jsx)(A,{className:`w-3 h-3 text-bambu-gray/50`})})]}),r?(0,Q.jsxs)(`form`,{className:`flex items-center gap-2`,onSubmit:e=>{e.preventDefault(),c()},children:[(0,Q.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,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:n}),(0,Q.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,Q.jsx)(`button`,{type:`button`,onClick:l,disabled:u,className:`px-2 py-1 text-xs text-bambu-gray hover:text-white`,children:f})]}):(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`span`,{className:`text-lg font-semibold text-white`,children:i}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:n}),(0,Q.jsx)(`button`,{onClick:s,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Q.jsx)(me,{className:`w-3 h-3`})})]})]})}function fn({value:e,unit:t,editing:r,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}=n(),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?h(`forecast.safetyMarginHintGApprox`,{days:Math.round(e/a)}):``});return(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3 space-y-1`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`text-xs font-medium text-white`,children:m}),(0,Q.jsx)(`span`,{title:_,children:(0,Q.jsx)(A,{className:`w-3 h-3 text-bambu-gray/50`})})]}),r?(0,Q.jsxs)(`form`,{className:`flex items-center gap-2 flex-wrap`,onSubmit:e=>{e.preventDefault(),l()},children:[(0,Q.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,Q.jsxs)(`div`,{className:`flex bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded overflow-hidden text-xs`,children:[(0,Q.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,Q.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,Q.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,Q.jsx)(`button`,{type:`button`,onClick:u,disabled:d,className:`px-2 py-1 text-xs text-bambu-gray hover:text-white`,children:p})]}):(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`span`,{className:`text-lg font-semibold text-white`,children:e}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:t}),g!==null&&t===`days`&&(0,Q.jsxs)(`span`,{className:`text-lg font-semibold text-white`,children:[`≈ `,g,`g`]}),t===`g`&&!!a&&(0,Q.jsxs)(`span`,{className:`text-lg font-semibold text-white`,children:[`≈ `,Math.round(e/a),`d`]}),(0,Q.jsx)(`button`,{onClick:c,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Q.jsx)(me,{className:`w-3 h-3`})})]})]})}function pn({items:e,forecasts:t,globalLeadTime:i,canWrite:a,onClose:o,onRemove:s,onClear:c}){let{t:l}=n(),u=d(),[f,m]=(0,Z.useState)(`list`),h=p({mutationFn:async({id:e,status:t,item:n,avgSpoolG:i})=>{if(await r.updateShoppingListStatus(e,t),t===`received`&&n){let t=i??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 r.bulkCreateSpools(a,n.quantity_spools),await r.removeFromShoppingList(e)}},onSuccess:()=>{u.invalidateQueries({queryKey:[`shopping-list`]}),u.invalidateQueries({queryKey:[`spools`]})}}),g=(0,Z.useMemo)(()=>{let e=new Map;for(let n of t)e.set(n.group.key,n);return e},[t]),_=(0,Z.useMemo)(()=>e.map(e=>({item:e,forecast:g.get(Jt(e.material,e.subtype,e.brand,e.color_name))??null})),[e,g]),v=(0,Z.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`)],n=e.map(e=>{let t=g.get(Jt(e.material,e.subtype,e.brand,e.color_name))??null,n=t&&t.totalSpools>0?t.totalLabelG/t.totalSpools:1e3,r=Math.round(e.quantity_spools*n),a=t?.effectiveLeadTimeDays??i??0,o=a>0?Zt(Xt(new Date,a)):``;return[e.quantity_spools,e.material,e.brand??``,e.subtype??``,e.color_name??``,r,a||``,o,e.status,e.note??``].map(e=>`"${String(e).replace(/"/g,`""`)}"`).join(`,`)}),r=[t.join(`,`),...n].join(`
- `),a=new Blob([r],{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,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Q.jsx)(R,{className:`w-4 h-4 text-bambu-green`}),(0,Q.jsx)(`h3`,{className:`text-sm font-semibold text-white`,children:l(`forecast.shoppingList`)}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:l(`forecast.shoppingListItems`,{count:e.length})}),e.length>0&&(0,Q.jsxs)(`div`,{className:`flex bg-bambu-dark-tertiary rounded-md p-0.5 ml-1`,children:[(0,Q.jsxs)(`button`,{onClick:()=>m(`list`),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${f===`list`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:[(0,Q.jsx)(fe,{className:`w-3 h-3`}),l(`forecast.listView`)]}),(0,Q.jsxs)(`button`,{onClick:()=>m(`logistics`),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${f===`logistics`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:[(0,Q.jsx)(It,{className:`w-3 h-3`}),l(`forecast.logisticsView`),v.length>0&&(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[e.length>0&&(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.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,Q.jsx)(P,{className:`w-3 h-3`}),l(`forecast.downloadCsv`)]}),a&&(0,Q.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,Q.jsx)(`button`,{onClick:o,className:`p-1 text-bambu-gray hover:text-white transition-colors`,children:(0,Q.jsx)(Le,{className:`w-4 h-4`})})]})]}),e.length===0?(0,Q.jsxs)(`div`,{className:`flex flex-col items-center py-8 text-bambu-gray`,children:[(0,Q.jsx)(fe,{className:`w-8 h-8 mb-2 opacity-30`}),(0,Q.jsx)(`p`,{className:`text-sm`,children:l(`forecast.shoppingListEmpty`)})]}):f===`list`?(0,Q.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Q.jsxs)(`table`,{className:`w-full`,children:[(0,Q.jsx)(`thead`,{children:(0,Q.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/20`,children:[(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.qty`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.material`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.weight`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.leadTime`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.expectedRestock`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.status`)}),(0,Q.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.note`)}),(0,Q.jsx)(`th`,{className:`px-4 py-2 text-right text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.actions`)})]})}),(0,Q.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:e.map(e=>{let t=Yt(e.brand,e.material,e.subtype,e.color_name),n=v.some(t=>t.item.id===e.id),r=g.get(Jt(e.material,e.subtype,e.brand,e.color_name))??null,o=r&&r.totalSpools>0?r.totalLabelG/r.totalSpools:1e3,c=Math.round(e.quantity_spools*o),u=r?.effectiveLeadTimeDays??i??0,d=u>0?Xt(new Date,u):null,f=e.status===`purchased`||e.status===`received`,p=e.status===`received`,m=h.isPending;return(0,Q.jsxs)(`tr`,{className:`hover:bg-bambu-dark-tertiary/30 transition-colors ${n&&!f?`bg-red-500/5`:``}`,children:[(0,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.jsxs)(`span`,{className:`text-sm font-semibold text-bambu-green`,children:[e.quantity_spools,`×`]})}),(0,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:t}),n&&!f&&(0,Q.jsx)(H,{className:`w-3.5 h-3.5 text-red-600 dark:text-red-400 flex-shrink-0`,"aria-label":l(`forecast.stockBreakBefore`)})]})}),(0,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:c>=1e3?`${(c/1e3).toFixed(1)}kg`:`${c}g`})}),(0,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:u>0?`${u}d`:`—`})}),(0,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:d?Zt(d):`—`})}),(0,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.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,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:e.note||`—`})}),(0,Q.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Q.jsx)(`div`,{className:`flex items-center justify-end gap-1`,children:a&&(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.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,Q.jsx)(we,{className:`w-4 h-4`}):(0,Q.jsx)(Lt,{className:`w-4 h-4`})}),(0,Q.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,Q.jsx)(zt,{className:`w-4 h-4`})}),(0,Q.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,Q.jsx)(z,{className:`w-3.5 h-3.5`})})]})})})]},e.id)})})]})}):(0,Q.jsx)(`div`,{className:`divide-y divide-bambu-dark-tertiary`,children:_.filter(({item:e})=>e.status!==`received`).map(({item:e,forecast:t})=>(0,Q.jsx)(mn,{item:e,forecast:t,globalLeadTime:i,canWrite:a,onRemove:()=>s(e.id)},e.id))})]})}function mn({item:e,forecast:t,globalLeadTime:r,canWrite:i,onRemove:a}){let{t:o}=n(),s=Yt(e.brand,e.material,e.subtype,e.color_name),c=(0,Z.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=Qt(Xt(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,Z.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,Q.jsxs)(`div`,{className:`px-4 py-4 ${u?`bg-red-500/5`:``}`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center justify-between mb-3`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 min-w-0`,children:[u?(0,Q.jsx)(H,{className:`w-4 h-4 text-red-600 dark:text-red-400 flex-shrink-0`}):(0,Q.jsx)(E,{className:`w-4 h-4 text-bambu-green/60 flex-shrink-0`}),(0,Q.jsx)(`span`,{className:`text-sm font-medium text-white truncate`,children:s}),(0,Q.jsxs)(`span`,{className:`text-xs text-bambu-gray flex-shrink-0`,children:[o(`forecast.spoolCount`,{count:e.quantity_spools}),` ordered`]})]}),i&&(0,Q.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,Q.jsx)(z,{className:`w-3.5 h-3.5`})})]}),u&&(0,Q.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,Q.jsx)(`span`,{className:`font-medium`,children:o(`forecast.stockBreakIn`,{days:l})}),` `,o(`forecast.stockRunsOutBefore`,{lt:t.effectiveLeadTimeDays}),t.dailyRateG!==null&&(0,Q.jsxs)(`span`,{children:[` `,o(`forecast.atRate`,{rate:t.dailyRateG.toFixed(1)}),` `,(0,Q.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,Q.jsx)(`div`,{className:`py-4 text-center text-xs text-bambu-gray`,children:o(`forecast.noUsageData`)}):c?(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsxs)(`div`,{className:`grid grid-cols-5 gap-2 mb-3`,children:[(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.stock`)}),(0,Q.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[Math.round(t.totalRemainingG),`g`]})]}),(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.leadTime`)}),(0,Q.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[t.effectiveLeadTimeDays,`d`]}),(0,Q.jsxs)(`div`,{className:`text-[10px] text-bambu-gray/60`,children:[`max(g:`,r,`, sku:`,t.settings?.lead_time_days??0,`)`]})]}),(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.safetyMarginLabel`)}),(0,Q.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[Math.round(t.safetyStockG),`g`]})]}),(0,Q.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,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.daysLeft`)}),(0,Q.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,Q.jsxs)(`div`,{className:`bg-bambu-green/15 rounded-lg px-2.5 py-2 text-center`,children:[(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.onArrival`)}),(0,Q.jsxs)(`div`,{className:`text-sm font-semibold text-bambu-green`,children:[Math.round(c.arrivalG),`g`]}),(0,Q.jsxs)(`div`,{className:`text-[10px] text-bambu-gray/60`,children:[`+`,o(`forecast.spoolCount`,{count:e.quantity_spools})]})]})]}),(0,Q.jsx)(Me,{width:`100%`,height:180,children:(0,Q.jsxs)(it,{data:c.points,margin:{top:8,right:8,bottom:0,left:0},children:[(0,Q.jsxs)(`defs`,{children:[(0,Q.jsxs)(`linearGradient`,{id:`cart-pre-${e.id}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Q.jsx)(`stop`,{offset:`5%`,stopColor:u?`#EF4444`:`#1DB954`,stopOpacity:.25}),(0,Q.jsx)(`stop`,{offset:`95%`,stopColor:u?`#EF4444`:`#1DB954`,stopOpacity:.02})]}),(0,Q.jsxs)(`linearGradient`,{id:`cart-post-${e.id}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Q.jsx)(`stop`,{offset:`5%`,stopColor:`#1DB954`,stopOpacity:.3}),(0,Q.jsx)(`stop`,{offset:`95%`,stopColor:`#1DB954`,stopOpacity:.03})]})]}),(0,Q.jsx)(Pe,{strokeDasharray:`3 3`,stroke:`#374151`,strokeOpacity:.4}),(0,Q.jsx)(Ne,{dataKey:`label`,tick:{fill:`#6B7280`,fontSize:9},interval:Math.max(0,Math.ceil(c.maxDays/6)-1),axisLine:!1,tickLine:!1}),(0,Q.jsx)(V,{tick:{fill:`#6B7280`,fontSize:9},axisLine:!1,tickLine:!1,tickFormatter:e=>e>=1e3?`${(e/1e3).toFixed(1)}kg`:`${e}g`,width:44}),(0,Q.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,Q.jsx)(rt,{type:`linear`,dataKey:`stock`,stroke:`#1DB954`,strokeWidth:2,fill:`url(#cart-post-${e.id})`,dot:!1,activeDot:{r:3}}),t.reorderPointG>0&&(0,Q.jsx)(T,{y:t.reorderPointG,stroke:`#F59E0B`,strokeDasharray:`5 3`,strokeOpacity:.8,label:{value:`ROP`,position:`insideTopRight`,fill:`#F59E0B`,fontSize:9}}),t.safetyStockG>0&&(0,Q.jsx)(T,{y:t.safetyStockG,stroke:`#6B7280`,strokeDasharray:`3 3`,strokeOpacity:.6,label:{value:`SS`,position:`insideTopRight`,fill:`#6B7280`,fontSize:9}}),(0,Q.jsx)(T,{x:Qt(Xt(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,Q.jsx)(T,{x:Qt(Xt(new Date,l)),stroke:`#EF4444`,strokeWidth:1.5,strokeOpacity:.9,label:{value:`OUT`,position:`insideTopLeft`,fill:`#EF4444`,fontSize:9}})]})}),(0,Q.jsxs)(`div`,{className:`flex flex-wrap items-center gap-3 mt-2 text-[10px] text-bambu-gray`,children:[(0,Q.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Q.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-yellow-400 border-dashed`}),` `,o(`forecast.ropLabel`)]}),(0,Q.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Q.jsx)(`span`,{className:`inline-block w-4 border-t border-bambu-gray border-dashed`}),` `,o(`forecast.safetyStockLegend`)]}),(0,Q.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Q.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-blue-400 border-dashed`}),` `,o(`forecast.stockArrivalLegend`)]}),u&&(0,Q.jsxs)(`span`,{className:`flex items-center gap-1 text-red-700 dark:text-red-400`,children:[(0,Q.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-red-400`}),` `,o(`forecast.stockoutLegend`)]})]})]}):null]})}function hn({forecast:e,onClose:t,onAdd:r}){let{t:i}=n(),a=Yt(e.group.brand,e.group.material,e.group.subtype,e.group.colorName),[o,s]=(0,Z.useState)(`qty`),[c,l]=(0,Z.useState)(`1`),[u,d]=(0,Z.useState)(`30`),[f,p]=(0,Z.useState)(``),m=(0,Z.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(),r({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,Q.jsx)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4`,children:(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-2xl border border-bambu-dark-tertiary w-full max-w-sm shadow-2xl`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center justify-between px-5 pt-5 pb-4 border-b border-bambu-dark-tertiary`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(R,{className:`w-5 h-5 text-bambu-green`}),(0,Q.jsx)(`h2`,{className:`text-base font-semibold text-white`,children:i(`forecast.addToCartTitle`)})]}),(0,Q.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white transition-colors`,children:(0,Q.jsx)(Le,{className:`w-5 h-5`})})]}),(0,Q.jsxs)(`form`,{onSubmit:g,className:`p-5 space-y-4`,children:[(0,Q.jsx)(`div`,{className:`text-sm text-bambu-gray`,children:a}),(0,Q.jsxs)(`div`,{className:`flex bg-bambu-dark-tertiary rounded-lg p-0.5`,children:[(0,Q.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,Q.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,Q.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Q.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.numberOfSpools`)}),(0,Q.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,Q.jsxs)(`div`,{className:`space-y-2`,children:[(0,Q.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Q.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.lastHowManyDays`)}),(0,Q.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,Q.jsx)(`div`,{className:`text-xs text-yellow-700 dark:text-yellow-400 px-1`,children:i(`forecast.noUsageQty`)}):(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 px-3 py-2 bg-bambu-dark-tertiary/50 rounded-lg`,children:[(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`≈`}),(0,Q.jsx)(`span`,{className:`text-sm font-semibold text-bambu-green`,children:i(`forecast.spoolCount`,{count:m??0})}),(0,Q.jsxs)(`span`,{className:`text-xs text-bambu-gray`,children:[`at `,e.dailyRateG.toFixed(1),`g/day`]})]})]}),(0,Q.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Q.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.noteOptional`)}),(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center gap-3 pt-1`,children:[(0,Q.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,Q.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 gn({links:e,onChange:t,currencySymbol:i}){let{t:a}=n(),{showToast:o}=re(),c=d(),l=(0,Z.useRef)(null),[u,p]=(0,Z.useState)(!1),[m,h]=(0,Z.useState)(``),[g,_]=(0,Z.useState)(!1),{data:v=[],isLoading:y,isError:b}=f({queryKey:J,queryFn:r.getSuppliers});(0,Z.useEffect)(()=>{if(!u)return;let e=e=>{l.current&&!l.current.contains(e.target)&&(p(!1),h(``))};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[u]);let x=(0,Z.useMemo)(()=>new Set(e.map(e=>e.supplier_id)),[e]),S=(0,Z.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(``)},T=async()=>{let e=m.trim();if(e){_(!0);try{let t=await r.createSupplier({name:e});await Xe(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)}}},E=(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,Q.jsxs)(`div`,{children:[(0,Q.jsx)(`label`,{className:`block text-sm font-medium text-bambu-gray mb-1`,children:a(`inventory.suppliers.label`)}),e.length>0&&(0,Q.jsx)(`div`,{className:`space-y-2 mb-2`,children:e.map(e=>(0,Q.jsxs)(`div`,{className:`p-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.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,Q.jsx)(Bt,{className:`w-3 h-3`}),e.supplier_name]}),(0,Q.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,Q.jsx)(`input`,{type:`checkbox`,className:`w-3.5 h-3.5 accent-bambu-green`,checked:e.is_purchase_source,onChange:t=>E(e.supplier_id,{is_purchase_source:t.target.checked})}),a(`inventory.suppliers.purchaseSource`)]}),(0,Q.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,Q.jsx)(Le,{className:`w-3.5 h-3.5`})})]}),(0,Q.jsxs)(`div`,{className:`flex gap-2 mt-2`,children:[(0,Q.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=>E(e.supplier_id,{supplier_article_number:t.target.value})}),(0,Q.jsxs)(`div`,{className:`relative w-28`,children:[(0,Q.jsx)(`span`,{className:`absolute left-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray pointer-events-none`,children:i}),(0,Q.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=>E(e.supplier_id,{quoted_price_per_kg:t.target.value===``?null:parseFloat(t.target.value)})})]})]})]},e.supplier_id))}),(0,Q.jsxs)(`div`,{className:`relative`,ref:l,children:[(0,Q.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,Q.jsx)(`span`,{children:a(`inventory.suppliers.addSupplier`)}),(0,Q.jsx)(D,{className:`w-4 h-4 transition-transform ${u?`rotate-180`:``}`})]}),u&&(0,Q.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,Q.jsx)(`div`,{className:`p-2 border-b border-bambu-dark-tertiary`,children:(0,Q.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,Q.jsx)(`div`,{className:`max-h-48 overflow-y-auto py-1`,children:y?(0,Q.jsxs)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray flex items-center gap-2`,children:[(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}),a(`common.loading`)]}):b?(0,Q.jsxs)(`div`,{className:`px-3 py-2 text-sm text-red-400 flex items-center gap-2`,children:[(0,Q.jsx)(H,{className:`w-4 h-4`}),a(`inventory.suppliers.loadFailed`)]}):(0,Q.jsxs)(Q.Fragment,{children:[S.map(e=>(0,Q.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,Q.jsx)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray`,children:a(`inventory.suppliers.noneLeft`)}),m.trim()&&!C&&(0,Q.jsxs)(`button`,{type:`button`,onClick:T,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,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Q.jsx)(be,{className:`w-4 h-4`}),a(`inventory.suppliers.createNew`,{name:m.trim()})]})]})})]})]}),(0,Q.jsx)(`p`,{className:`text-xs text-bambu-gray mt-1`,children:a(`inventory.suppliers.help`)})]})}function _n({filaments:e,isLoading:t,selectedId:r,onSelect:i}){let{t:a}=n(),[o,s]=(0,Z.useState)(!1),[c,l]=(0,Z.useState)(``),u=(0,Z.useRef)(null),d=(0,Z.useRef)(null),f=(0,Z.useMemo)(()=>e.find(e=>e.id===r)??null,[e,r]);(0,Z.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,Z.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,Z.useEffect)(()=>{o&&d.current?.focus()},[o]);let m=()=>{s(!0),l(``)},h=e=>{i(e),s(!1),l(``)};return(0,Q.jsxs)(`div`,{ref:u,className:`relative`,children:[(0,Q.jsx)(`label`,{className:`block text-sm font-medium text-bambu-gray mb-1`,children:a(`inventory.pickFromSpoolmanCatalog`)}),(0,Q.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,Q.jsx)(L,{className:`w-4 h-4 text-bambu-gray animate-spin shrink-0`}):f?(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(`span`,{className:`w-4 h-4 rounded-full shrink-0 border border-white/20`,style:K(f.color_hex),"aria-label":a(`inventory.spoolmanFilamentColorSwatch`)}),(0,Q.jsxs)(`span`,{className:`text-white text-sm truncate flex-1`,children:[f.vendor?.name?`${f.vendor.name} — `:``,f.name]})]}):(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(fe,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Q.jsx)(`span`,{className:`text-bambu-gray text-sm flex-1`,children:a(`inventory.spoolmanFilamentCatalog`)})]}),(0,Q.jsx)(D,{className:`w-4 h-4 text-bambu-gray shrink-0 ml-auto`})]}),o&&(0,Q.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,Q.jsx)(`div`,{className:`p-2 border-b border-bambu-dark-tertiary`,children:(0,Q.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,Q.jsx)(`ul`,{className:`max-h-56 overflow-y-auto py-1`,children:t?(0,Q.jsxs)(`li`,{className:`flex items-center justify-center gap-2 py-4 text-bambu-gray text-sm`,children:[(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}),(0,Q.jsx)(`span`,{children:a(`common.loading`,`Loading…`)})]}):p.length===0?(0,Q.jsx)(`li`,{className:`py-4 text-center text-bambu-gray text-sm`,children:a(`inventory.noSpoolmanFilaments`)}):p.map(e=>(0,Q.jsx)(`li`,{children:(0,Q.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===r?`bg-bambu-dark-tertiary`:``}`,children:[(0,Q.jsx)(`span`,{className:`w-4 h-4 rounded-full shrink-0 border border-white/20`,style:K(e.color_hex),"aria-label":a(`inventory.spoolmanFilamentColorSwatch`)}),(0,Q.jsxs)(`span`,{className:`flex-1 min-w-0`,children:[(0,Q.jsxs)(`span`,{className:`block text-white text-sm truncate`,children:[e.vendor?.name?`${e.vendor.name} — `:``,e.name]}),(0,Q.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 vn(e,t){let n=e.nozzles??[];return At(n[(e.printer.nozzle_count??1)>1&&t>0?t:0]?.nozzle_type)??At(n[0]?.nozzle_type)}function yn(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 bn(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 xn({formData:e,printersWithCalibrations:t,filamentOptions:r,modelPresets:i,setModelPresets:a,selectedProfiles:o,setSelectedProfiles:s,selectedGroupId:c,setSelectedGroupId:l,printerModels:u,isLoading:d=!1}){let{t:f}=n(),p=(0,Z.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([...dt,...t.flatMap(yn)])).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([...dt,...yn(e)])).sort((e,t)=>parseFloat(e)-parseFloat(t))}))]},[t]),m=p.find(e=>e.id===c)??p[0],h=(0,Z.useMemo)(()=>{let e=new Map;return(t,n)=>{if(!t)return r;let i=e.get(t);if(i||(i=r.filter(e=>{let n=tt(e.name,u??{});return!n||et(n,t)}),e.set(t,i)),n&&!i.some(e=>e.code===n)){let e=r.find(e=>e.code===n);if(e)return[e,...i]}return i}},[r,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=>bn(e,{left:f(`inventory.leftNozzle`),right:f(`inventory.rightNozzle`),single:f(`inventory.nozzle`)}),y=t=>t.printer.connected?t.calibrations.filter(t=>xt(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(ft(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=ft(e,t,n);return r?a.set(o,r):a.delete(o),a})},C=()=>{let t=r.find(t=>t.code===e.slicer_filament);if(!t)return;let n=t.name.split(`@`)[0].trim().toLowerCase();n&&a(e=>{let t=new Map(e);for(let e of p){if(!e.model)continue;let r=h(e.model,void 0).filter(e=>e.name.toLowerCase().startsWith(n)&&tt(e.name,u??{})!==null);if(r.length!==0)for(let n of e.diameters){let i=r.find(e=>RegExp(`\\b${n.replace(`.`,`\\.`)}\\s*nozzle\\b`,`i`).test(e.name)),a=r.find(e=>!/\b[\d.]+\s*nozzle\b/i.test(e.name)),o=i??a;o&&t.set(ht(e.model,n),{code:o.code,name:o.name})}}return t})};if(t.length===0)return(0,Q.jsx)(`div`,{className:`p-6 bg-bambu-dark rounded-lg text-center`,children:(0,Q.jsxs)(`p`,{className:`text-bambu-gray flex items-center justify-center gap-2`,children:[d&&(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}),f(d?`common.loading`:`inventory.noPrintersConfigured`)]})});let w=(e,t,n)=>{let r=ht(e,t),a=i.get(r),o=h(e,a?.code);return(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`div`,{className:`flex-1 min-w-0`,children:(0,Q.jsx)(ot,{ariaLabel:`${e} ${t}mm ${f(`inventory.filamentPreset`)}`,value:a?.code??``,options:o,inheritLabel:n,disabled:!e,onChange:n=>x(e,t,n)})}),(0,Q.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,Q.jsxs)(`div`,{className:`space-y-3`,children:[(g||e.color_name)&&(0,Q.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,Q.jsx)(`span`,{className:`w-5 h-5 rounded-full border border-white/15 shrink-0`,style:{background:_},"aria-hidden":`true`}),g&&(0,Q.jsx)(`span`,{className:`text-sm text-white truncate`,children:g}),e.color_name&&(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray truncate`,children:e.color_name})]}),(0,Q.jsxs)(`div`,{className:`flex flex-col md:flex-row gap-4`,children:[(0,Q.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,Q.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,Q.jsxs)(`div`,{className:`min-w-0 flex-1`,children:[(0,Q.jsx)(`div`,{className:`text-sm font-semibold truncate`,children:e.model||f(`inventory.unknownModel`)}),(0,Q.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,Q.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,Q.jsx)(E,{className:`w-3.5 h-3.5 text-bambu-green shrink-0`})]},e.id)})}),(0,Q.jsx)(`div`,{className:`flex-1 min-w-0`,children:m&&(0,Q.jsxs)(`div`,{className:`space-y-4`,children:[(0,Q.jsxs)(`div`,{className:`flex items-start justify-between gap-3`,children:[(0,Q.jsxs)(`div`,{className:`min-w-0`,children:[(0,Q.jsx)(`h4`,{className:`text-base font-semibold text-white truncate`,children:m.model||f(`inventory.unknownModel`)}),(0,Q.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:m.printers.map(e=>e.printer.name).join(`, `)})]}),e.slicer_filament&&(0,Q.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,Q.jsx)(De,{className:`w-3.5 h-3.5`}),f(`inventory.autoMatchPresets`)]})]}),(0,Q.jsxs)(`div`,{className:`space-y-2`,children:[(0,Q.jsx)(`p`,{className:`text-xs font-semibold text-bambu-gray uppercase tracking-wide`,children:f(`inventory.filamentPreset`)}),m.model?(0,Q.jsx)(`div`,{className:`space-y-2`,children:m.diameters.map(e=>(0,Q.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Q.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray w-14 shrink-0`,children:[e,`mm`]}),(0,Q.jsx)(`div`,{className:`flex-1 min-w-0`,children:w(m.model,e,f(`inventory.presetUseSpoolDefault`))})]},e))}):(0,Q.jsx)(`p`,{className:`text-sm text-bambu-gray italic`,children:f(`inventory.presetNeedsModel`)})]}),(0,Q.jsxs)(`div`,{className:`space-y-2`,children:[(0,Q.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,Q.jsxs)(`div`,{className:`p-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Q.jsx)(xe,{className:`w-3.5 h-3.5 text-bambu-gray shrink-0`}),(0,Q.jsx)(`span`,{className:`text-sm font-semibold text-white truncate`,children:e.printer.name}),(0,Q.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,Q.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-1`,children:f(`inventory.noKProfilesMatch`)}):(0,Q.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,Q.jsx)(`span`,{}),v(e).map(e=>(0,Q.jsx)(`span`,{className:`text-[11px] font-semibold uppercase tracking-wide text-bambu-gray`,children:e.label},e.extruder)),m.diameters.map(n=>(0,Q.jsxs)(Z.Fragment,{children:[(0,Q.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=ft(e.printer.id,r.extruder,n),s=o.get(a);if(i.length===0)return(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray/50 px-2 py-1.5`,title:f(`inventory.noKProfilesMatch`),children:`—`},a);let c=vn(e,r.extruder),l=At(s?.nozzle_id),u=!!s&&!X(s.nozzle_id,c);return(0,Q.jsxs)(`select`,{title:u?f(`inventory.kProfileFlowMismatch`,{profile:kt(l),fitted:kt(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,Q.jsx)(`option`,{value:``,children:f(`inventory.kProfileNotSet`)}),i.map(e=>{let t=kt(At(e.nozzle_id));return(0,Q.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,Q.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-1`,children:f(`inventory.printerOffline`)})]},e.printer.id)}):(0,Q.jsx)(`p`,{className:`text-sm text-bambu-gray italic`,children:f(`inventory.selectMaterialFirst`)})]})]})})]})]})}function Sn({product:e,colorName:t,prefilled:r,checked:i,onCheckedChange:a,scope:o,onScopeChange:s,disabled:c}){let{t:l}=n(),u=t.trim();return(0,Q.jsxs)(`div`,{className:`mt-3 p-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg space-y-2`,children:[r&&(0,Q.jsxs)(`p`,{className:`flex items-center gap-1.5 text-xs text-bambu-green`,children:[(0,Q.jsx)(De,{className:`w-3.5 h-3.5 shrink-0`,"aria-hidden":`true`}),l(`inventory.profileDefaults.prefilled`,{product:e})]}),(0,Q.jsxs)(`label`,{className:`flex items-center gap-2 text-sm ${c?`text-bambu-gray`:`text-white cursor-pointer`}`,children:[(0,Q.jsx)(`input`,{type:`checkbox`,checked:i&&!c,disabled:c,onChange:e=>a(e.target.checked),className:`w-4 h-4 accent-bambu-green`}),c?l(`inventory.profileDefaults.saveNeedsMaterial`):l(`inventory.profileDefaults.saveAsDefault`,{product:e})]}),i&&!c&&(0,Q.jsxs)(`div`,{role:`radiogroup`,"aria-label":l(`inventory.profileDefaults.scope`),className:`flex flex-wrap gap-4 pl-6`,children:[(0,Q.jsxs)(`label`,{className:`flex items-center gap-2 text-sm text-white cursor-pointer`,children:[(0,Q.jsx)(`input`,{type:`radio`,name:`profile-default-scope`,checked:o===`all`||!u,onChange:()=>s(`all`),className:`accent-bambu-green`}),l(`inventory.profileDefaults.allColours`)]}),(0,Q.jsxs)(`label`,{className:`flex items-center gap-2 text-sm ${u?`text-white cursor-pointer`:`text-bambu-gray`}`,children:[(0,Q.jsx)(`input`,{type:`radio`,name:`profile-default-scope`,checked:o===`colour`&&!!u,disabled:!u,onChange:()=>s(`colour`),className:`accent-bambu-green`}),u?l(`inventory.profileDefaults.colourOnly`,{colour:u}):l(`inventory.profileDefaults.colourOnlyNeedsColour`,{tab:l(`inventory.colorAndCostTab`)})]})]})]})}function Cn(e){let t=Ve(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 wn={completed:`text-bambu-green`,failed:`text-red-700 dark:text-red-400`,aborted:`text-yellow-700 dark:text-yellow-400`};function Tn({spoolId:e,spoolman:t=!1,createdAt:i=null}){let{t:a}=n(),o=d(),{showToast:s}=re(),{data:c,isLoading:l}=f({queryKey:[`spool-usage`,t,e],queryFn:()=>t?r.getSpoolmanSpoolUsageHistory(e):r.getSpoolUsageHistory(e)}),u=c&&t?Ut({created_at:i},c):c,m=p({mutationFn:()=>t?r.clearSpoolmanSpoolUsageHistory(e):r.clearSpoolUsageHistory(e),onSuccess:()=>{o.invalidateQueries({queryKey:[`spool-usage`,t,e]}),s(a(`inventory.historyCleared`),`success`)}});return l?(0,Q.jsx)(`div`,{className:`flex justify-center py-4`,children:(0,Q.jsx)(L,{className:`w-5 h-5 animate-spin text-bambu-green`})}):!u||u.length===0?(0,Q.jsxs)(`div`,{className:`text-center py-4 text-bambu-gray text-sm`,children:[(0,Q.jsx)(ae,{className:`w-5 h-5 mx-auto mb-2 opacity-50`}),a(`inventory.noUsageHistory`)]}):(0,Q.jsxs)(`div`,{className:`space-y-2`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,Q.jsx)(`h4`,{className:`text-sm font-medium text-white`,children:a(`inventory.usageHistory`)}),(0,Q.jsxs)(W,{variant:`ghost`,size:`sm`,onClick:()=>m.mutate(),disabled:m.isPending,className:`text-xs text-bambu-gray hover:text-red-700 dark:hover:text-red-400`,children:[(0,Q.jsx)(z,{className:`w-3 h-3 mr-1`}),a(`inventory.clearHistory`)]})]}),(0,Q.jsx)(`div`,{className:`max-h-48 overflow-y-auto space-y-1`,children:u.map(e=>(0,Q.jsxs)(`div`,{className:`flex items-center justify-between p-2 rounded bg-bambu-dark/50 text-xs`,children:[(0,Q.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Q.jsx)(`span`,{className:`text-bambu-gray`,children:Cn(e.created_at)}),e.print_name&&(0,Q.jsx)(`span`,{className:`text-white ml-2 truncate`,title:e.print_name,children:e.print_name})]}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 flex-shrink-0 ml-2`,children:[(0,Q.jsxs)(`span`,{className:`text-white font-medium`,children:[e.weight_used.toFixed(1),`g`]}),(0,Q.jsxs)(`span`,{className:`text-bambu-gray`,children:[`(`,e.percent_used,`%)`]}),(0,Q.jsx)(`span`,{className:wn[e.status]||`text-bambu-gray`,children:e.status})]})]},e.id))})]})}var En={tag_uid:null,tray_uuid:null,tag_type:null,data_origin:null};function Dn({isOpen:e,onClose:t,spool:i,mode:a,printersWithCalibrations:o=[],currencySymbol:c,onSpoolsCreated:l,spoolmanMode:u=!1,spoolsQueryKey:m=[`inventory-spools`]}){let{t:h}=n(),g=d(),{showToast:_}=re(),v=()=>Qe(g,m),y=a===`edit`,b=a===`copy`,[x,S]=(0,Z.useState)(ct),C=(0,Z.useRef)(x);C.current=x;let[w,T]=(0,Z.useState)({}),[E,D]=(0,Z.useState)(`filament`),[O,k]=(0,Z.useState)(!1),[A,ee]=(0,Z.useState)(!1),[te,j]=(0,Z.useState)(!1),[ne,ie]=(0,Z.useState)(!1),[M,ae]=(0,Z.useState)([]),[oe,N]=(0,Z.useState)(!1),[P,F]=(0,Z.useState)(!1),[I,se]=(0,Z.useState)(1),[ce,ue]=(0,Z.useState)(!1);(0,Z.useEffect)(()=>{ue(!1)},[e,i?.id]);let[de,fe]=(0,Z.useState)(!1),[me,he]=(0,Z.useState)(!1),[ge,_e]=(0,Z.useState)([]),[ve,ye]=(0,Z.useState)(new Set),[be,xe]=(0,Z.useState)(``),[Se,Ce]=(0,Z.useState)([]),[we,Ee]=(0,Z.useState)([]),[R,De]=(0,Z.useState)([]),[Oe,ke]=(0,Z.useState)([]),[z,B]=(0,Z.useState)([]),[je,Me]=(0,Z.useState)([]),[Ne,Pe]=(0,Z.useState)([]),[V,H]=(0,Z.useState)(!1),[Ie,U]=(0,Z.useState)(new Map),[ze,Be]=(0,Z.useState)(new Map),[G,Ue]=(0,Z.useState)(``),[K,q]=(0,Z.useState)(!1),[We,Ge]=(0,Z.useState)(!1),Ke=(0,Z.useRef)(new Map),Je=(0,Z.useRef)(new Map),Ye=(0,Z.useRef)(new Set),Xe=(0,Z.useRef)(new Set),J=(0,Z.useRef)(!1),[$e,et]=(0,Z.useState)(!1),[tt,nt]=(0,Z.useState)(`all`),rt=(e,t,n)=>{for(let r of new Set([...e.keys(),...t.keys()]))e.get(r)!==t.get(r)&&n.add(r)},it=e=>{q(!0),Be(t=>{let n=typeof e==`function`?e(t):e;return rt(t,n,Xe.current),n})},ot=e=>{q(!0),U(t=>{let n=typeof e==`function`?e(t):e;return rt(t,n,Ye.current),n})},dt=o.length>0?o:Ne,ft=Ie.size+ze.size,{data:xt=[],isLoading:Dt,error:Ot}=f({queryKey:[`spoolman-inventory-filaments`],queryFn:()=>r.getSpoolmanInventoryFilaments(),enabled:u&&e,staleTime:6e4,retry:!1});(0,Z.useEffect)(()=>{Me(wt())},[]),(0,Z.useEffect)(()=>{let t=!1;return e&&((async()=>{he(!0);try{let[e,n]=await Promise.allSettled([(async()=>(await r.getCloudStatus()).is_authenticated?{connected:!0,presets:await r.getFilamentPresets()}:{connected:!1,presets:[]})(),(async()=>(await r.orcaCloudStatus()).connected?{connected:!0,presets:(await r.orcaCloudListProfiles()).filament}:{connected:!1,presets:[]})()]);if(t)return;let i=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:[];fe(i||a),_e([...o,...s]),ye(new Set(s.map(e=>e.setting_id)))}catch(e){if(t)return;console.error(`Failed to fetch cloud presets:`,e),fe(!1)}finally{t||he(!1)}})(),r.getSpoolCatalog().then(Ce).catch(console.error),r.getColorCatalog().then(B).catch(console.error),r.getLocalPresets().then(e=>De(e.filament)).catch(console.error),r.getBuiltinFilaments().then(ke).catch(console.error),r.getLocations().then(e=>Ee(e.map(e=>({id:e.id,name:e.name})))).catch(console.error),o.length===0&&(async()=>{H(!0);try{let e=await r.getPrinters(),n=await Promise.all(e.map(e=>r.getPrinterStatus(e.id).catch(()=>null))),i=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||Pe(i)}catch(e){console.error(`Failed to fetch printer calibrations:`,e)}finally{t||H(!1)}})()),()=>{t=!0}},[e,o.length]);let kt=(0,Z.useMemo)(()=>Tt(ge,new Set,R,Oe,ve),[ge,R,Oe,ve]),X=(0,Z.useMemo)(()=>{let e=bt(ge,R),t=z.map(e=>e.manufacturer?.trim()).filter(e=>!!e),n=new Set([...e,...t]);return Array.from(n).sort((e,t)=>e.localeCompare(t))},[ge,R,z]),Mt=(0,Z.useMemo)(()=>{let e=z.map(e=>e.material?.trim()).filter(e=>!!e),t=new Set([...pt,...e]);return Array.from(t).sort((e,t)=>e.localeCompare(t))},[z]),Nt=(0,Z.useMemo)(()=>{let e=[];for(let t of z){let n=t.manufacturer?.trim(),r=t.material?.trim();n&&r&&e.push({brand:n,material:r})}for(let t of ge){let n=Et(t.name);n.brand&&n.material&&e.push({brand:n.brand,material:n.material})}for(let t of R){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},[ge,z,R]),Pt=(0,Z.useMemo)(()=>{let e=new Map;for(let t of Nt){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(n)||e.set(n,new Set),e.get(n).add(r)}return e},[Nt]),It=(0,Z.useMemo)(()=>{let e=new Map;for(let t of Nt){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(r)||e.set(r,new Set),e.get(r).add(n)}return e},[Nt]),Lt=(0,Z.useMemo)(()=>_t(X,x.brand),[X,x.brand]),Rt=(0,Z.useMemo)(()=>_t(Mt,x.material),[Mt,x.material]),zt=(0,Z.useMemo)(()=>mt(Lt,x.material,It),[Lt,x.material,It]),Bt=(0,Z.useMemo)(()=>mt(Rt,x.brand,Pt),[Rt,x.brand,Pt]),Vt=(0,Z.useMemo)(()=>vt(x.slicer_filament,kt),[x.slicer_filament,kt]);(0,Z.useEffect)(()=>{if(e){if(i){let e=i.rgba&&/^[0-9A-Fa-f]{8}$/.test(i.rgba)?i.rgba:`808080FF`;S({material:i.material||``,subtype:i.subtype||``,brand:i.brand||``,color_name:i.color_name_is_synthesized?``:i.color_name||``,rgba:e,extra_colors:i.extra_colors||``,effect_type:i.effect_type||``,label_weight:i.label_weight||1e3,core_weight:i.core_weight??250,core_weight_catalog_id:i.core_weight_catalog_id??null,weight_used:b?0:i.weight_used||0,slicer_filament:i.slicer_filament||``,note:i.note||``,cost_per_kg:i.cost_per_kg??null,category:i.category||``,low_stock_threshold_pct:i.low_stock_threshold_pct??null,material_number:i.material_number||``,location_id:i.location_id??null,last_dried_at:(()=>{let e=b?null:Ve(i.last_dried_at);return e?He(e):``})(),spoolman_filament_id:null}),xe(i.slicer_filament_name||i.slicer_filament||``),U(St(i.k_profiles??[])),ae((i.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&&e.is_purchase_source})))}else S(ct),xe(``),U(new Map),ae([]);F(!1),se(1),T({}),D(`filament`),Ue(``),Be(new Map),k(!1),ie(!1),ee(b&&i?.core_weight_is_inherited===!1),j(!1),N(!1),q(!1),Ge(!1),Ke.current=new Map,Je.current=new Map,Ye.current=new Set,Xe.current=new Set,J.current=!1,et(!1),nt(`all`)}},[e,i,a,b]),(0,Z.useEffect)(()=>{if(!e||!i)return;let t=!1;return(u?r.getSpoolmanFilamentPresets:r.getSpoolFilamentPresets)(i.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||``});Be(n)}).catch(e=>{t||(console.error(`Failed to load filament preset overrides:`,e),_(h(`inventory.filamentPresetsLoadFailed`),`warning`))}),()=>{t=!0}},[e,i?.id,u]);let Ht=!y&&!b;(0,Z.useEffect)(()=>{if(!e||!Ht)return;let t=x.material.trim(),n=!1,i=e=>{if(n)return;let t=St(e?.k_profiles??[]),r=Ke.current,i=Je.current;U(e=>{let n=new Map(e);for(let[e,t]of r)n.get(e)===t&&n.delete(e);let i=new Map;for(let[e,r]of t)n.has(e)||Ye.current.has(e)||(n.set(e,r),i.set(e,r));return Ke.current=i,n});let a=new Map;for(let t of e?.presets??[])a.set(ht(t.printer_model,t.nozzle_diameter||``),{code:t.slicer_filament||``,name:t.slicer_filament_name||``});Be(e=>{let t=new Map(e);for(let[e,n]of i)t.get(e)===n&&t.delete(e);let n=new Map;for(let[e,r]of a)t.has(e)||Xe.current.has(e)||(t.set(e,r),n.set(e,r));return Je.current=n,t}),Ge(t.size>0||a.size>0||!!e?.slicer_filament),e?.slicer_filament&&(!C.current.slicer_filament||J.current)?(S(t=>({...t,slicer_filament:e.slicer_filament||``})),xe(e.slicer_filament_name||e.slicer_filament),J.current=!0):J.current&&=(S(e=>({...e,slicer_filament:``})),xe(``),!1)};if(!t){i(null);return}let a=setTimeout(()=>{r.resolveProfileDefault({brand:x.brand.trim(),material:t,subtype:x.subtype.trim(),color_name:x.color_name.trim()}).then(i).catch(e=>{n||console.error(`Failed to look up the profile default:`,e)})},300);return()=>{n=!0,clearTimeout(a)}},[e,Ht,x.brand,x.material,x.subtype,x.color_name]),(0,Z.useEffect)(()=>{if(!e||!i||te||x.location_id!=null)return;let t=i.storage_location?.trim();if(!t||we.length===0)return;let n=we.find(e=>e.name.toLowerCase()===t.toLowerCase());n&&S(e=>e.location_id===n.id?e:{...e,location_id:n.id})},[e,i,we,x.location_id,te]);let Ut=(e,t)=>{let n=at.has(e);u&&n&&x.spoolman_filament_id!==null&&_(h(`inventory.spoolmanFilamentUnlinked`),`info`),S(r=>({...r,[e]:t,...u&&n&&r.spoolman_filament_id!==null?{spoolman_filament_id:null}:{}})),e===`weight_used`&&k(!0),e===`core_weight`&&ee(!0),e===`location_id`&&j(!0),e===`last_dried_at`&&ie(!0),e===`slicer_filament`&&(J.current=!1),w[e]&&T(t=>({...t,[e]:void 0}))},Wt=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`)},Gt=e=>{Me(t=>gt(e,t))},Kt=p({mutationFn:e=>u?r.createSpoolmanInventorySpool(e):r.createSpool(e),onSuccess:async e=>{e?.id&&(!await mn(e.id)||!await dn(e.id))||(await vn(),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`)}}),qt=p({mutationFn:({data:e,qty:t})=>u?r.bulkCreateSpoolmanInventorySpools(e,t):r.bulkCreateSpools(e,t),onSuccess:async e=>{let n=u&&`created`in e?e:null,r=n?n.created:e;for(let e of r)await mn(e.id);if(un)for(let e of r)await dn(e.id);await vn(),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`)}}),Jt=p({mutationFn:e=>u?r.updateSpoolmanInventorySpool(i.id,e):r.updateSpool(i.id,e),onSuccess:async()=>{i?.id&&(!await mn(i.id)||!await dn(i.id))||(await vn(),await v(),_(h(`inventory.spoolUpdated`),`success`),t())},onError:e=>{e instanceof s&&e.status===503?_(h(`inventory.spoolmanUnreachable`),`error`):_(h(`inventory.saveFailed`),`error`)}}),Yt=p({mutationFn:()=>u?r.updateSpoolmanInventorySpool(i.id,En):r.updateSpool(i.id,En),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:Xt,isLoading:Zt}=f({queryKey:[`spool-assignments`],queryFn:()=>r.getAssignments(),enabled:e&&y&&!u}),{data:Qt,isLoading:$t}=f({queryKey:[`spoolman-slot-assignments`,`all`],queryFn:()=>r.getSpoolmanSlotAssignments(),enabled:e&&y&&u}),en=(()=>{if(i)return u?Qt?.find(e=>e.spoolman_spool_id===i.id):Xt?.find(e=>e.spool_id===i.id)})(),tn=u?$t:Zt,{data:nn}=f({queryKey:[`inventory-spools`],queryFn:()=>r.getSpools(!0),enabled:e}),{data:rn}=qe({enabled:e}),{data:an}=f({queryKey:[`slicerPrinterModels`],queryFn:r.getSlicerPrinterModels,enabled:e,staleTime:1/0}),on=(()=>{let e=new Set;for(let t of nn??[]){let n=t.category?.trim();n&&e.add(n)}return Array.from(e).sort((e,t)=>e.localeCompare(t))})(),sn=(()=>{let e=new Set;for(let t of nn??[]){let n=t.material_number?.trim();n&&e.add(n)}return Array.from(e).sort((e,t)=>e.localeCompare(t,void 0,{numeric:!0}))})(),cn=rn?.low_stock_threshold??20,ln=p({mutationFn:async()=>{if(!en)throw Error(`No assignment`);if(u){if(!i)throw Error(`No spool`);await r.unassignSpoolmanSlot(i.id);return}await r.unassignSpool(en.printer_id,en.ams_id,en.tray_id)},onSuccess:async()=>{u?(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`)}}),un=oe||b,dn=async e=>{if(!un)return!0;let t=u?r.setSpoolmanSpoolSuppliers:r.setSpoolSuppliers;try{return await t(e,M.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}},fn=y||b||K,pn=()=>{let e=[];for(let[t,n]of Ie){let[r,i,a]=t.split(`:`);e.push({printer_id:parseInt(r),extruder:parseInt(i),nozzle_diameter:a||`0.4`,nozzle_type:At(n.nozzle_id),k_value:n.k_value,name:n.name||null,cali_idx:n.cali_idx,setting_id:n.setting_id||null})}let t=[];for(let[e,n]of ze){let{model:r,diameter:i}=yt(e);r&&t.push({printer_model:r,nozzle_diameter:i,slicer_filament:n.code||null,slicer_filament_name:n.name||null})}return{profiles:e,presets:t}},mn=async e=>{if(!fn)return!0;let t=u?r.saveSpoolmanKProfiles:r.saveSpoolKProfiles,n=u?r.saveSpoolmanFilamentPresets:r.saveSpoolFilamentPresets,{profiles:i,presets:a}=pn();try{await t(e,i)}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},hn=[x.brand,x.material,x.subtype].map(e=>e.trim()).filter(Boolean).join(` `),vn=async()=>{if(!$e||!x.material.trim())return;let{profiles:e,presets:t}=pn(),n=x.slicer_filament||null;try{await r.saveProfileDefault({brand:x.brand.trim(),material:x.material.trim(),subtype:x.subtype.trim(),color_name:tt===`colour`?x.color_name.trim():``,slicer_filament:n,slicer_filament_name:n&&(Vt?.displayName||be)||null,presets:t,k_profiles:e}),await g.invalidateQueries({queryKey:[`profile-defaults`]})}catch(e){console.error(`Failed to save the profile default:`,e),_(h(`inventory.profileDefaults.saveFailed`),`warning`)}};if((0,Z.useEffect)(()=>{if(!e)return;let n=e=>{e.key===`Escape`&&!ce&&t()};return document.addEventListener(`keydown`,n),()=>document.removeEventListener(`keydown`,n)},[e,t,ce]),!e)return null;let yn=()=>{let e=ut(x,P,u,a);if(!e.isValid){T(e.errors),(e.errors.slicer_filament||e.errors.material||e.errors.brand||e.errors.subtype)&&D(`filament`);return}let t=Vt?.displayName||be||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,...u?A?{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,...u?{spoolman_filament_id:x.spoolman_filament_id}:{}};(!y||O)&&(n.weight_used=x.weight_used),(!y||te)&&(n.location_id=x.location_id),y&&ne&&(n.last_dried_at=x.last_dried_at?new Date(x.last_dried_at).toISOString():null),y?Jt.mutate(n):I>1?qt.mutate({data:n,qty:I}):Kt.mutate(n)},bn=Kt.isPending||qt.isPending||Jt.isPending||Yt.isPending||ln.isPending;return(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,Q.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:t}),(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Q.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&&i&&(0,Q.jsxs)(`span`,{className:`text-sm font-mono text-bambu-gray`,children:[`#`,i.id]})]}),(0,Q.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Q.jsx)(Le,{className:`w-5 h-5`})})]}),a===`create`&&(0,Q.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-2 border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(Re,{className:`w-4 h-4 text-amber-600 dark:text-amber-400`}),(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:h(`inventory.quickAdd`)})]}),(0,Q.jsx)(`button`,{type:`button`,onClick:()=>{F(!P),P||D(`filament`)},className:`relative inline-flex h-5 w-9 items-center rounded-full transition-colors ${P?`bg-bambu-green`:`bg-bambu-dark-tertiary`}`,children:(0,Q.jsx)(`span`,{className:`inline-block h-3.5 w-3.5 rounded-full bg-white transition-transform ${P?`translate-x-4`:`translate-x-0.5`}`})})]}),(0,Q.jsxs)(`div`,{className:`flex border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Q.jsxs)(`button`,{onClick:()=>D(`filament`),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${E===`filament`?`text-bambu-green border-b-2 border-bambu-green`:`text-bambu-gray hover:text-white`}`,children:[(0,Q.jsx)(Ft,{className:`w-4 h-4`}),h(`inventory.filamentInfoTab`)]}),!P&&(0,Q.jsxs)(`button`,{onClick:()=>D(`appearance`),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${E===`appearance`?`text-bambu-green border-b-2 border-bambu-green`:`text-bambu-gray hover:text-white`}`,children:[(0,Q.jsx)(pe,{className:`w-4 h-4`}),h(`inventory.colorAndCostTab`)]}),!P&&(0,Q.jsxs)(`button`,{onClick:()=>D(`printers`),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${E===`printers`?`text-bambu-green border-b-2 border-bambu-green`:`text-bambu-gray hover:text-white`}`,children:[(0,Q.jsx)(Re,{className:`w-4 h-4`}),h(`inventory.printersTab`),ft>0&&(0,Q.jsx)(`span`,{className:`text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green`,children:ft})]})]}),(0,Q.jsxs)(`div`,{className:`p-4 overflow-y-auto flex-1`,style:{scrollbarGutter:`stable`},children:[E===`filament`?(0,Q.jsxs)(`div`,{className:`space-y-6`,children:[u&&!y&&(0,Q.jsx)(`div`,{children:Ot?(0,Q.jsx)(`p`,{className:`text-sm text-red-700 dark:text-red-400 px-1`,children:h(`inventory.spoolmanCatalogLoadFailed`)}):(0,Q.jsx)(_n,{filaments:xt,isLoading:Dt,selectedId:x.spoolman_filament_id,onSelect:Wt})}),(0,Q.jsxs)(`div`,{children:[(0,Q.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.filamentInfo`)}),(0,Q.jsx)(lt,{formData:x,updateField:Ut,cloudAuthenticated:de,loadingCloudPresets:me,presetInputValue:be,setPresetInputValue:xe,selectedPresetOption:Vt,filamentOptions:kt,availableBrands:Lt,availableMaterials:Rt,suggestedBrands:zt,suggestedMaterials:Bt,quickAdd:P,detailsRequired:!P&&!u&&a===`create`,quantity:I,onQuantityChange:se,errors:w})]})]}):E===`appearance`?(0,Q.jsxs)(`div`,{className:`space-y-6`,children:[(0,Q.jsxs)(`div`,{children:[(0,Q.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.color`)}),(0,Q.jsx)(st,{formData:x,updateField:Ut,recentColors:je,onColorUsed:Gt,catalogColors:z})]}),(0,Q.jsxs)(`div`,{children:[(0,Q.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.additional`)}),(0,Q.jsx)(Y,{formData:x,updateField:Ut,spoolCatalog:Se,currencySymbol:c,availableCategories:on,availableMaterialNumbers:sn,availableLocations:we,showLastDried:y,onCreateLocation:async e=>{try{let t=await r.createLocation({name:e});return Ee(e=>[...e,{id:t.id,name:t.name}].sort((e,t)=>e.name.localeCompare(t.name))),await Ze(g),{id:t.id,name:t.name}}catch(e){console.error(e);let t=e instanceof Error?e.message:h(`locations.saveFailed`);return _(t||h(`locations.saveFailed`),`error`),null}},globalLowStockThreshold:cn,spoolmanMode:u})]}),(0,Q.jsx)(`div`,{children:(0,Q.jsx)(gn,{links:M,onChange:e=>{ae(e),N(!0)},currencySymbol:c})}),y&&i&&(0,Q.jsx)(`div`,{children:(0,Q.jsx)(Tn,{spoolId:i.id,spoolman:u,createdAt:i.created_at})})]}):(0,Q.jsx)(xn,{formData:x,printersWithCalibrations:dt,filamentOptions:kt,modelPresets:ze,setModelPresets:it,selectedProfiles:Ie,setSelectedProfiles:ot,selectedGroupId:G,setSelectedGroupId:Ue,printerModels:an,isLoading:V}),E===`printers`&&(0,Q.jsx)(Sn,{product:hn,colorName:x.color_name,prefilled:We,checked:$e,onCheckedChange:et,scope:tt,onScopeChange:nt,disabled:!x.material.trim()})]}),(0,Q.jsxs)(`div`,{className:`flex gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0`,children:[y&&(0,Q.jsxs)(`div`,{className:`flex gap-2 mr-auto`,children:[(0,Q.jsxs)(W,{variant:`secondary`,onClick:()=>Yt.mutate(),disabled:bn||!(i?.tag_uid||i?.tray_uuid),children:[(0,Q.jsx)(Ae,{className:`w-4 h-4`}),h(`inventory.clearRfid`,`Clear RFID Tag`)]}),en?(0,Q.jsxs)(W,{variant:`secondary`,onClick:()=>ln.mutate(),disabled:bn||tn,children:[(0,Q.jsx)(Fe,{className:`w-4 h-4`}),h(`inventory.unassignSpool`,`Unassign`)]}):(0,Q.jsxs)(W,{variant:`secondary`,onClick:()=>ue(!0),disabled:bn||tn,children:[(0,Q.jsx)(le,{className:`w-4 h-4`}),h(`inventory.assignSpool`,`Assign Spool`)]})]}),(0,Q.jsxs)(`div`,{className:`flex gap-2 ml-auto`,children:[(0,Q.jsx)(W,{variant:`secondary`,onClick:t,children:h(`common.cancel`)}),(0,Q.jsx)(W,{onClick:yn,disabled:bn,children:bn?(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}),h(`common.saving`)]}):(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(Te,{className:`w-4 h-4`}),h(y?`common.save`:b?`inventory.copySpool`:`inventory.addSpool`)]})})]})]})]})]}),y&&i&&(0,Q.jsx)(jt,{isOpen:ce,onClose:()=>ue(!1),spool:i,printerId:null,variant:`dialog`,spoolmanMode:u})]})}var On={a4:[210,297],letter:[215.9,279.4]},kn=.01,An=15e3;function jn(e,t,n=1){return Math.ceil((n-1+t)/e)}function Mn(e,t){return new Intl.NumberFormat(t,{maximumFractionDigits:2}).format(e)}var Nn=[`label_w_mm`,`label_h_mm`,`cols`,`rows`,`top_mm`,`left_mm`,`col_gap_mm`,`row_gap_mm`],Pn={page:`a4`,label_w_mm:63.5,label_h_mm:38.1,cols:3,rows:7,top_mm:15.15,left_mm:7,col_gap_mm:2.5,row_gap_mm:0};function $(e){let t={page:e.page};for(let n of Nn)t[n]=String(e[n]);return t}function Fn(e){let t={page:e.page};for(let n of Nn){let r=e[n].trim().replace(`,`,`.`),i=Number(r);if(r===``||!Number.isFinite(i))return null;t[n]=i}return t}function In(e){if(!e||typeof e!=`object`)return!1;let t=e;return(t.page===`a4`||t.page===`letter`)&&Nn.every(e=>typeof t[e]==`string`)}function Ln(e){return[e.left_mm+e.cols*e.label_w_mm+(e.cols-1)*e.col_gap_mm,e.top_mm+e.rows*e.label_h_mm+(e.rows-1)*e.row_gap_mm]}function Rn(e){let t=Fn(e);if(!t)return{key:`incomplete`,fallback:`Fill in every field with a number.`};if(t.label_w_mm<40||t.label_h_mm<20)return{key:`tooSmall`,fallback:`Labels must be at least {{minWidth}} mm wide and {{minHeight}} mm tall, or the text doesn’t fit.`,params:{minWidth:40,minHeight:20}};let n=[t.label_w_mm,t.label_h_mm,t.top_mm,t.left_mm,t.col_gap_mm,t.row_gap_mm];if(Math.max(...n)>300)return{key:`tooLong`,fallback:`Sizes, margins and gaps can be at most {{max}} mm.`,params:{max:300}};if(!Number.isInteger(t.cols)||t.cols<1||t.cols>20)return{key:`columns`,fallback:`Columns must be a whole number from 1 to {{max}}.`,params:{max:20}};if(!Number.isInteger(t.rows)||t.rows<1||t.rows>40)return{key:`rows`,fallback:`Rows must be a whole number from 1 to {{max}}.`,params:{max:40}};if(Math.min(t.top_mm,t.left_mm,t.col_gap_mm,t.row_gap_mm)<0)return{key:`negative`,fallback:`Margins and gaps can’t be negative.`};let[r,i]=On[t.page],[a,o]=Ln(t);return a>r+kn?{key:`tooWide`,fallback:`The labels are {{size}} mm wide with the left margin, but the page is {{page}} mm.`,params:{size:a,page:r}}:o>i+kn?{key:`tooTall`,fallback:`The labels are {{size}} mm tall with the top margin, but the page is {{page}} mm.`,params:{size:o,page:i}}:null}var zn=[{field:`label_w_mm`,i18nKey:`labelWidth`,fallback:`Label width`,unit:!0},{field:`label_h_mm`,i18nKey:`labelHeight`,fallback:`Label height`,unit:!0},{field:`cols`,i18nKey:`columns`,fallback:`Columns`,unit:!1},{field:`rows`,i18nKey:`rows`,fallback:`Rows`,unit:!1},{field:`top_mm`,i18nKey:`topMargin`,fallback:`Top margin`,unit:!0},{field:`left_mm`,i18nKey:`leftMargin`,fallback:`Left margin`,unit:!0},{field:`col_gap_mm`,i18nKey:`columnGap`,fallback:`Gap between columns`,unit:!0},{field:`row_gap_mm`,i18nKey:`rowGap`,fallback:`Gap between rows`,unit:!0}];function Bn({layout:e,startingPosition:t=1}){let{t:r}=n(),[i,a]=On[e.page],o=[];for(let n=0;n<e.rows;n++)for(let r=0;r<e.cols;r++){let s=e.left_mm+r*(e.label_w_mm+e.col_gap_mm),c=e.top_mm+n*(e.label_h_mm+e.row_gap_mm),l=s+e.label_w_mm>i+.01||c+e.label_h_mm>a+.01,u=n*e.cols+r<t-1;o.push((0,Q.jsx)(`rect`,{"data-testid":l?`label-sheet-cell-off-page`:`label-sheet-cell`,x:s,y:c,width:e.label_w_mm,height:e.label_h_mm,rx:1,className:l?`fill-red-400/40 stroke-red-500`:u?`fill-transparent stroke-gray-300`:`fill-bambu-green/25 stroke-bambu-green`,strokeWidth:.6},`${n}-${r}`))}return(0,Q.jsxs)(`svg`,{"data-testid":`label-sheet-diagram`,role:`img`,"aria-label":r(`inventory.labels.custom.diagram`,`Drawing of the sheet with every label in place`),viewBox:`-4 -4 ${i+8} ${a+8}`,className:`h-48 w-auto mx-auto overflow-visible`,children:[(0,Q.jsx)(`rect`,{x:0,y:0,width:i,height:a,className:`fill-white stroke-gray-400`,strokeWidth:.6}),o]})}function Vn({draft:e,onChange:t,problem:r,startingPosition:i}){let{t:a,i18n:o}=n(),s=(0,Z.useId)(),c=(0,Z.useId)(),l=Fn(e),u=r?.params?Object.fromEntries(Object.entries(r.params).map(([e,t])=>[e,Mn(t,o.language)])):void 0,d=l!==null&&Number.isInteger(l.cols)&&Number.isInteger(l.rows)&&l.cols>=1&&l.cols<=20&&l.rows>=1&&l.rows<=40&&l.label_w_mm>0&&l.label_h_mm>0,f=`w-full px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green`;return(0,Q.jsxs)(`fieldset`,{"data-testid":`label-sheet-editor`,className:`space-y-3`,children:[(0,Q.jsx)(`legend`,{className:`text-sm font-medium text-white`,children:a(`inventory.labels.custom.layoutTitle`,`Sheet layout`)}),(0,Q.jsxs)(`div`,{className:`grid grid-cols-2 gap-x-3 gap-y-2`,children:[(0,Q.jsxs)(`label`,{className:`col-span-2 space-y-0.5`,children:[(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:a(`inventory.labels.custom.page`,`Page`)}),(0,Q.jsxs)(`select`,{"data-testid":`label-sheet-page`,value:e.page,onChange:n=>t({...e,page:n.target.value}),className:f,children:[(0,Q.jsx)(`option`,{value:`a4`,children:a(`inventory.labels.custom.pageA4`,`A4 (210 × 297 mm)`)}),(0,Q.jsx)(`option`,{value:`letter`,children:a(`inventory.labels.custom.pageLetter`,`US Letter (215.9 × 279.4 mm)`)})]})]}),zn.map(({field:n,i18nKey:i,fallback:o,unit:l})=>(0,Q.jsxs)(`label`,{className:`space-y-0.5`,children:[(0,Q.jsxs)(`span`,{className:`text-xs text-bambu-gray`,children:[a(`inventory.labels.custom.${i}`,o),l&&` (mm)`]}),(0,Q.jsx)(`input`,{"data-testid":`label-sheet-${n}`,type:`text`,inputMode:l?`decimal`:`numeric`,value:e[n],onChange:r=>t({...e,[n]:r.target.value}),"aria-describedby":r?`${s} ${c}`:s,"aria-invalid":r?!0:void 0,className:f})]},n))]}),(0,Q.jsx)(`p`,{id:s,className:`text-xs text-bambu-gray`,children:a(`inventory.labels.custom.measureHint`,`Measure from the top-left corner of the page: margins to the first label, gaps between neighbouring labels.`)}),r&&(0,Q.jsx)(`p`,{id:c,"data-testid":`label-sheet-problem`,"aria-live":`polite`,className:`text-xs text-red-400`,children:a(`inventory.labels.custom.problems.${r.key}`,r.fallback,u)}),d&&l&&(0,Q.jsx)(Bn,{layout:l,startingPosition:i})]})}var Hn=[{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.`}],Un={avery_l7160:21,avery_5160:30},Wn=[203,300,600],Gn={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`}},Kn=`bambuddy-label-print-options`,qn=[`labelTemplates`];function Jn(e){return`saved:${e}`}function Yn(e){return e.startsWith(`saved:`)?Number(e.slice(6)):null}function Xn(e){return typeof e==`string`?e===`custom`||/^saved:[1-9]\d*$/.test(e)?!0:Hn.some(t=>t.value===e):!1}var Zn={template:Hn[0].value,fields:{},customDraft:$(Pn),monochrome:!1,format:`pdf`,dpi:300};function Qn(){let e;try{e=JSON.parse(localStorage.getItem(Kn)??`null`)}catch{return Zn}if(!e||typeof e!=`object`)return Zn;let t=e,n={};if(t.fields&&typeof t.fields==`object`)for(let[e,r]of Object.entries(t.fields))!Xn(e)||!Array.isArray(r)||(n[e]=r.filter(e=>o.includes(e)));return{template:Xn(t.template)?t.template:Zn.template,fields:n,customDraft:In(t.customDraft)?t.customDraft:Zn.customDraft,monochrome:t.monochrome===!0,format:t.format===`png`?`png`:`pdf`,dpi:Wn.includes(t.dpi)?t.dpi:Zn.dpi}}function $n(e){try{localStorage.setItem(Kn,JSON.stringify(e))}catch{}}function er(e,t){return e.fields[t]??a}function tr(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 nr(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 rr(e){return K(e)}function ir(e){return q(e.color_name,e.rgba,e.color_name_is_synthesized)}function ar(e){return`${ir(e)??`${e.material}${e.subtype?` ${e.subtype}`:``}`}${e.brand?` · ${e.brand}`:``}`}function or(e){return[ir(e),e.color_name,e.material,e.subtype,e.brand,`#${e.id}`].filter(Boolean).join(` `).toLowerCase()}function sr(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 cr({isOpen:e,onClose:t,availableSpools:i,initialSelectedIds:c,spoolmanMode:l}){let{t:u,i18n:m}=n(),h=Ge(),{showToast:g}=re(),{hasPermission:_}=ze(),v=_(`inventory:update`),y=d(),[b,x]=(0,Z.useState)(!1),[S,C]=(0,Z.useState)(new Set),[w,T]=(0,Z.useState)(``),[E,D]=(0,Z.useState)(``),[O,k]=(0,Z.useState)(`id`),[A,ee]=(0,Z.useState)(Zn),[te,j]=(0,Z.useState)(`1`),[ne,ie]=(0,Z.useState)(null),[M,ae]=(0,Z.useState)(!1),[oe,N]=(0,Z.useState)(null),F=(0,Z.useRef)(null),[I,se]=(0,Z.useState)(null),[ce,le]=(0,Z.useState)(null),[ue,de]=(0,Z.useState)(!1),fe=(0,Z.useRef)(0),pe=f({queryKey:qn,queryFn:r.getLabelTemplates,enabled:e,staleTime:0}),me=(0,Z.useMemo)(()=>pe.data??[],[pe.data]),he=p({mutationFn:({id:e,name:t,layout:n})=>e===void 0?r.createLabelTemplate({name:t,layout:n}):r.updateLabelTemplate(e,{name:t,layout:n}),onSuccess:e=>{y.setQueryData(qn,(t=[])=>[...t.filter(t=>t.id!==e.id),e]),y.invalidateQueries({queryKey:qn})},onError:e=>{if(e instanceof s&&e.status===409){g(u(`inventory.labels.custom.nameTaken`,`A label template with this name already exists.`),`error`);return}let t=e instanceof Error?e.message:String(e);g(u(`inventory.labels.custom.saveFailed`,`Could not save the template: {{msg}}`,{msg:t}),`error`)}}),ge=p({mutationFn:e=>r.deleteLabelTemplate(e),onSuccess:(e,t)=>{y.setQueryData(qn,(e=[])=>e.filter(e=>e.id!==t)),y.invalidateQueries({queryKey:qn})},onError:e=>{let t=e instanceof Error?e.message:String(e);g(u(`inventory.labels.custom.deleteFailed`,`Could not delete the template: {{msg}}`,{msg:t}),`error`)}});(0,Z.useEffect)(()=>{if(e){fe.current=Date.now();let e=new Set(i.map(e=>e.id));C(new Set(c.filter(t=>e.has(t)))),T(``),D(``),k(`id`),ee(Qn()),j(`1`),x(!1),se(null),le(null),de(!1)}},[e]);let _e=(0,Z.useMemo)(()=>{let e=[...i];return O===`color`?(e.sort((e,t)=>{let n=sr(e.rgba),r=sr(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)},[i,O]),ve=(0,Z.useMemo)(()=>{let e=new Set;for(let t of _e)t.material&&e.add(t.material.toUpperCase());return[...e].sort()},[_e]),ye=(0,Z.useMemo)(()=>{let e=w.trim().toLowerCase();return _e.filter(t=>!(E&&(t.material||``).toUpperCase()!==E||e&&!or(t).includes(e)))},[_e,w,E,h]),be=ye.length>0&&ye.every(e=>S.has(e.id)),Se=A.template,Ce=er(A,Se),we=Ce.join(`,`),Te=Yn(Se),R=Te===null?void 0:me.find(e=>e.id===Te),De=Se===`custom`||Te!==null,Ae=Se===`custom`?A.customDraft:I?I.draft:R?$(R.layout):null,z=Ae?Rn(Ae):null,B=Ae&&!z?Fn(Ae):null,je=!De||B!==null,Me=De?`custom`:Se,Ne=B?JSON.stringify(B):``,Pe=pe.isSuccess&&!pe.isFetching&&pe.dataUpdatedAt>=fe.current;(0,Z.useEffect)(()=>{!e||!Pe||Te===null||me.some(e=>e.id===Te)||(se(null),le(null),de(!1),ee(e=>{let t={...e,template:Zn.template};return $n(t),t}))},[e,Pe,me,Te]);let V=_e.find(e=>S.has(e.id))?.id??null;function H(e){F.current&&window.URL.revokeObjectURL(F.current),F.current=e,ie(e)}if((0,Z.useEffect)(()=>{if(!e||V===null||!je){H(null),N(null),ae(!1);return}let t=new AbortController;ae(!0);let n=window.setTimeout(async()=>{let e={spool_id:V,template:Me,...Ne?{layout:JSON.parse(Ne)}:{},monochrome:A.monochrome,fields:we?we.split(`,`):[]};try{let n=l?await r.previewSpoolmanSpoolLabel(e,t.signal):await r.previewSpoolLabel(e,t.signal);if(t.signal.aborted)return;H(window.URL.createObjectURL(n)),N(null)}catch(e){if(t.signal.aborted)return;H(null),N(e instanceof Error?e.message:String(e))}finally{t.signal.aborted||ae(!1)}},250);return()=>{window.clearTimeout(n),t.abort()}},[e,V,je,Me,Ne,we,A.monochrome,l]),(0,Z.useEffect)(()=>()=>{F.current&&window.URL.revokeObjectURL(F.current)},[]),!e)return null;let Fe=S.size,Ie=Fe===0,U=De?B?B.cols*B.rows:void 0:Un[Se],Re=Number(te),G=U===void 0||Number.isInteger(Re)&&Re>=1&&Re<=U,Ve=Hn.find(e=>e.value===Se)??Hn[0],He=A.format===`png`&&U!==void 0&&G?jn(U,Fe,Re):0,Ue=He*A.dpi>An,K=!Ie&&!b&&G&&je&&!Ue;function q(e){ee(t=>{let n={...t,...e};return $n(n),n})}function We(e){se(null),le(null),q({template:e})}function Ke(e){return u(`inventory.labels.custom.summary`,`{{page}} · {{width}} × {{height}} mm · {{capacity}} labels ({{cols}} × {{rows}})`,{page:e.page===`a4`?u(`inventory.labels.custom.pageShortA4`,`A4`):u(`inventory.labels.custom.pageShortLetter`,`Letter`),width:Mn(e.label_w_mm,m.language),height:Mn(e.label_h_mm,m.language),capacity:e.cols*e.rows,cols:e.cols,rows:e.rows})}function qe(e,t){let n=Se;he.mutate({name:e.trim(),layout:t},{onSuccess:e=>{se(null),le(null);let t=Jn(e.id);ee(e=>{let r={...e,template:t,fields:{...e.fields,[t]:er(e,n)}};return $n(r),r}),g(u(`inventory.labels.custom.saved`,`Template "{{name}}" saved`,{name:e.name}),`success`)}})}function Je(e,t,n){he.mutate({id:e,name:t.trim(),layout:n},{onSuccess:e=>{se(null),g(u(`inventory.labels.custom.saved`,`Template "{{name}}" saved`,{name:e.name}),`success`)}})}function Ye(e){ge.mutate(e.id,{onSuccess:()=>{de(!1),se(null),q({template:Zn.template}),g(u(`inventory.labels.custom.deleted`,`Template "{{name}}" deleted`,{name:e.name}),`success`)}})}function Xe(e){let t=o.filter(t=>e.includes(t));q({fields:{...A.fields,[Se]:t}})}function J(e){Xe(Ce.includes(e)?Ce.filter(t=>t!==e):[...Ce,e])}function Ze(e){C(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})}function Qe(){C(e=>{let t=new Set(e);for(let e of ye)t.add(e.id);return t})}function $e(){C(e=>{let t=new Set(e);for(let e of ye)t.delete(e.id);return t})}function et(){C(new Set)}async function tt(){if(!K)return;let e={spool_ids:_e.filter(e=>S.has(e.id)).map(e=>e.id),template:Me,...B?{layout:B}:{},monochrome:A.monochrome,starting_position:U===void 0?1:Re,fields:Ce,format:A.format,dpi:A.dpi};x(!0);try{let n=l?await r.printSpoolmanSpoolLabels(e):await r.printSpoolLabels(e);A.format===`pdf`?tr(n):nr(n,Me),t()}catch(e){let t=e instanceof Error?e.message:String(e);g(u(`inventory.labels.error`,`Could not generate labels: {{msg}}`,{msg:t}),`error`)}finally{x(!1)}}let nt=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,Q.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-start sm:items-center justify-center p-4 overflow-y-auto`,children:[(0,Q.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:t}),(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(xe,{className:`w-5 h-5 text-bambu-green`}),(0,Q.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:u(`inventory.labels.title`,`Print spool labels`)}),Fe>0&&(0,Q.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[`(`,u(`inventory.labels.selectedCount`,`{{count}} selected`,{count:Fe}),`)`]})]}),(0,Q.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,"aria-label":u(`common.close`,`Close`),children:(0,Q.jsx)(Le,{className:`w-5 h-5`})})]}),(0,Q.jsxs)(`div`,{className:`flex-1 min-h-0 overflow-y-auto md:overflow-hidden grid grid-cols-1 md:grid-cols-2`,children:[(0,Q.jsxs)(`div`,{className:`flex flex-col min-h-0 md:border-r border-bambu-dark-tertiary`,children:[(0,Q.jsxs)(`div`,{className:`p-4 space-y-2 border-b border-bambu-dark-tertiary`,children:[(0,Q.jsxs)(`div`,{className:`relative`,children:[(0,Q.jsx)(Ee,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none`}),(0,Q.jsx)(`input`,{type:`search`,value:w,onChange:e=>T(e.target.value),placeholder:u(`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`})]}),ve.length>1&&(0,Q.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray mr-1`,children:u(`inventory.labels.filterByMaterial`,`Material:`)}),(0,Q.jsx)(`button`,{type:`button`,onClick:()=>D(``),className:`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`}`,children:u(`inventory.labels.allMaterials`,`All`)}),ve.map(e=>(0,Q.jsx)(`button`,{type:`button`,onClick:()=>D(e),className:`px-2 py-0.5 text-xs rounded-full border transition ${E===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,Q.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray mr-1`,children:u(`inventory.labels.sortBy.label`)}),(0,Q.jsx)(`button`,{type:`button`,onClick:()=>k(`id`),className:`px-2 py-0.5 text-xs rounded-full border transition ${O===`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:u(`inventory.labels.sortBy.id`)}),(0,Q.jsx)(`button`,{type:`button`,onClick:()=>k(`color`),className:`px-2 py-0.5 text-xs rounded-full border transition ${O===`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:u(`inventory.labels.sortBy.color`)})]})]}),(0,Q.jsxs)(`div`,{className:`px-4 pt-3 pb-2 flex items-center justify-between gap-3 flex-wrap`,children:[(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:u(`inventory.labels.pickSpools`,`Pick which spools to print labels for:`)}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-3 text-xs`,children:[(0,Q.jsx)(`button`,{type:`button`,onClick:be?$e:Qe,disabled:ye.length===0,className:`text-bambu-green hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed`,children:be?u(`inventory.labels.deselectVisible`,`Deselect visible`):u(`inventory.labels.selectVisible`,`Select all visible ({{count}})`,{count:ye.length})}),(0,Q.jsx)(`button`,{type:`button`,onClick:et,disabled:Fe===0,className:`text-bambu-gray hover:text-white hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed`,children:u(`inventory.labels.clearAll`,`Clear all`)})]})]}),(0,Q.jsx)(`div`,{className:`flex-1 overflow-y-auto px-2 pb-2 min-h-0 max-h-72 md:max-h-none`,children:ye.length===0?(0,Q.jsx)(`div`,{className:`text-center text-sm text-bambu-gray py-6`,children:_e.length===0?u(`inventory.labels.noSpoolsToShow`,`No spools to show. Adjust your filter and try again.`):u(`inventory.labels.noMatches`,`No spools match the current search or filter.`)}):(0,Q.jsx)(`ul`,{className:`space-y-0.5`,children:ye.map(e=>{let t=S.has(e.id);return(0,Q.jsx)(`li`,{children:(0,Q.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,Q.jsx)(Oe,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Q.jsx)(ke,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Q.jsx)(`input`,{type:`checkbox`,checked:t,onChange:()=>Ze(e.id),className:`sr-only`}),(0,Q.jsx)(`span`,{className:`w-4 h-4 rounded border border-black/20 shrink-0`,style:rr(e.rgba)}),(0,Q.jsx)(`span`,{className:`flex-1 min-w-0 truncate text-sm text-white`,children:ar(e)}),(0,Q.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray shrink-0`,children:[`#`,e.id]})]})},e.id)})})})]}),(0,Q.jsxs)(`div`,{className:`md:overflow-y-auto p-4 space-y-4 border-t md:border-t-0 border-bambu-dark-tertiary`,children:[(0,Q.jsxs)(`div`,{className:`space-y-1`,children:[(0,Q.jsx)(`label`,{htmlFor:`label-template`,className:`text-sm font-medium text-white`,children:u(`inventory.labels.template`,`Label size`)}),(0,Q.jsxs)(`select`,{id:`label-template`,"data-testid":`label-template-select`,value:Se,onChange:e=>We(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:[Hn.map(e=>(0,Q.jsx)(`option`,{value:e.value,children:u(`inventory.labels.templates.${e.i18nKey}.label`,e.fallbackLabel)},e.value)),me.length>0&&(0,Q.jsx)(`optgroup`,{label:u(`inventory.labels.custom.savedGroup`,`Saved templates`),children:me.map(e=>(0,Q.jsx)(`option`,{value:Jn(e.id),children:e.name},e.id))}),(0,Q.jsx)(`option`,{value:`custom`,children:u(`inventory.labels.custom.option`,`Custom sheet…`)})]}),(0,Q.jsx)(`p`,{"data-testid":`label-template-hint`,className:`text-xs text-bambu-gray`,children:Se===`custom`?u(`inventory.labels.custom.hint`,`Describe your own label sheet below.`):Te===null?u(`inventory.labels.templates.${Ve.i18nKey}.hint`,Ve.fallbackHint):R?Ke(R.layout):null})]}),R&&!I&&(0,Q.jsxs)(`div`,{className:`space-y-2`,children:[(0,Q.jsx)(Bn,{layout:R.layout,startingPosition:G?Re:1}),v&&(0,Q.jsxs)(`div`,{className:`flex justify-center gap-4 text-xs`,children:[(0,Q.jsx)(`button`,{type:`button`,"data-testid":`label-template-edit`,onClick:()=>se({name:R.name,draft:$(R.layout)}),className:`text-bambu-green hover:underline`,children:u(`inventory.labels.custom.edit`,`Edit`)}),(0,Q.jsx)(`button`,{type:`button`,"data-testid":`label-template-delete`,onClick:()=>de(!0),className:`text-red-400 hover:underline`,children:u(`inventory.labels.custom.delete`,`Delete`)})]})]}),R&&I&&(0,Q.jsxs)(`div`,{className:`space-y-3`,children:[(0,Q.jsx)(lr,{value:I.name,onChange:e=>se({...I,name:e})}),(0,Q.jsx)(Vn,{draft:I.draft,onChange:e=>se({...I,draft:e}),problem:z,startingPosition:G?Re:1}),(0,Q.jsxs)(`div`,{className:`flex flex-wrap justify-end gap-2`,children:[(0,Q.jsx)(W,{variant:`secondary`,onClick:()=>se(null),disabled:he.isPending,children:u(`common.cancel`,`Cancel`)}),(0,Q.jsx)(W,{variant:`secondary`,"data-testid":`label-template-save-as-new`,disabled:!B||!I.name.trim()||he.isPending,onClick:()=>B&&qe(I.name,B),children:u(`inventory.labels.custom.saveAsNew`,`Save as new`)}),(0,Q.jsx)(W,{"data-testid":`label-template-save-changes`,disabled:!B||!I.name.trim()||he.isPending,onClick:()=>B&&Je(R.id,I.name,B),children:u(`inventory.labels.custom.saveChanges`,`Save changes`)})]})]}),Se===`custom`&&(0,Q.jsxs)(`div`,{className:`space-y-3`,children:[(0,Q.jsx)(Vn,{draft:A.customDraft,onChange:e=>q({customDraft:e}),problem:z,startingPosition:G?Re:1}),v&&(ce===null?(0,Q.jsx)(`button`,{type:`button`,"data-testid":`label-template-save-as`,disabled:!B,onClick:()=>le(``),className:`text-xs text-bambu-green hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed`,children:u(`inventory.labels.custom.saveAs`,`Save as template…`)}):(0,Q.jsxs)(`div`,{className:`flex flex-wrap items-end gap-2`,children:[(0,Q.jsx)(`div`,{className:`flex-1 min-w-40`,children:(0,Q.jsx)(lr,{value:ce,onChange:le,autoFocus:!0})}),(0,Q.jsx)(W,{variant:`secondary`,onClick:()=>le(null),disabled:he.isPending,children:u(`common.cancel`,`Cancel`)}),(0,Q.jsx)(W,{"data-testid":`label-template-save`,disabled:!B||!ce.trim()||he.isPending,onClick:()=>B&&qe(ce,B),children:u(`inventory.labels.custom.save`,`Save`)})]}))]}),(0,Q.jsxs)(`div`,{className:`space-y-1`,children:[(0,Q.jsx)(`div`,{className:`text-sm font-medium text-white`,children:V===null?u(`inventory.labels.preview`,`Preview`):u(`inventory.labels.previewOf`,`Preview of #{{id}}`,{id:V})}),(0,Q.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:[V===null?(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray text-center`,children:u(`inventory.labels.previewNoSelection`,`Select a spool to see its label.`)}):je?oe?(0,Q.jsx)(`span`,{className:`text-sm text-red-400 text-center`,children:u(`inventory.labels.previewError`,`Could not render the preview: {{msg}}`,{msg:oe})}):ne?(0,Q.jsx)(`img`,{src:ne,alt:u(`inventory.labels.previewAlt`,`Label preview`),className:`max-h-56 max-w-full object-contain bg-white shadow`}):null:(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray text-center`,children:u(`inventory.labels.custom.previewWaiting`,`The preview appears once the sheet layout is complete.`)}),M&&(0,Q.jsx)(L,{className:`absolute top-2 right-2 w-4 h-4 animate-spin text-bambu-green`})]}),(0,Q.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:u(`inventory.labels.previewHint`,`Lines that don't fit on this label size are left out.`)})]}),(0,Q.jsxs)(`fieldset`,{className:`space-y-2`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center justify-between gap-2`,children:[(0,Q.jsx)(`legend`,{className:`text-sm font-medium text-white`,children:u(`inventory.labels.content`,`Print on the label`)}),(0,Q.jsx)(`button`,{type:`button`,onClick:()=>Xe(a),className:`text-xs text-bambu-green hover:underline`,children:u(`inventory.labels.resetFields`,`Reset to default`)})]}),(0,Q.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 gap-x-3 gap-y-1`,children:o.map(e=>{let t=Ce.includes(e),{i18nKey:n,fallback:r}=Gn[e];return(0,Q.jsxs)(`label`,{className:`flex items-center gap-2 py-0.5 cursor-pointer select-none`,children:[t?(0,Q.jsx)(Oe,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Q.jsx)(ke,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Q.jsx)(`input`,{type:`checkbox`,"data-testid":`label-field-${e}`,checked:t,onChange:()=>J(e),className:`sr-only`}),(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:u(`inventory.labels.fields.${n}`,r)})]},e)})})]}),(0,Q.jsxs)(`div`,{className:`space-y-3`,children:[(0,Q.jsxs)(`label`,{className:`inline-flex items-center gap-2 cursor-pointer select-none`,children:[A.monochrome?(0,Q.jsx)(Oe,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Q.jsx)(ke,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Q.jsx)(`input`,{type:`checkbox`,checked:A.monochrome,onChange:e=>q({monochrome:e.target.checked}),className:`sr-only`}),(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:u(`inventory.labels.monochrome`,`Monochrome (black & white printer)`)}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:u(`inventory.labels.monochromeHint`,`Drops the colour swatch and widens the text`)})]}),U!==void 0&&(0,Q.jsxs)(`div`,{className:`flex items-start gap-3`,children:[(0,Q.jsx)(`label`,{htmlFor:`label-starting-position`,className:`text-sm text-white whitespace-nowrap pt-1.5`,children:u(`inventory.labels.startingPosition`,`Starting label position`)}),(0,Q.jsx)(`input`,{id:`label-starting-position`,"data-testid":`label-starting-position`,type:`number`,min:1,max:U,step:1,value:te,onChange:e=>j(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,Q.jsx)(`div`,{id:`label-starting-position-help`,"data-testid":`label-starting-position-status`,className:`text-xs pt-1.5 ${G?`text-bambu-gray`:`text-red-400`}`,children:G?Re===1?u(`inventory.labels.startingPositionFirst`,`Printing starts at position 1.`):u(`inventory.labels.startingPositionSkipped`,`Positions 1 through {{lastPosition}} will be left blank on the first sheet.`,{lastPosition:Re-1}):u(`inventory.labels.startingPositionInvalid`,`Enter a whole number from 1 to {{capacity}}.`,{capacity:U})})]}),U!==void 0&&(0,Q.jsx)(`p`,{"data-testid":`label-scale-hint`,className:`text-xs text-bambu-gray`,children:u(`inventory.labels.scaleHint`,`Print sheets at 100% (actual size), not "Fit to page": a shrunk page drifts off the labels.`)}),(0,Q.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{className:`text-sm text-white mr-1`,children:u(`inventory.labels.format`,`Output:`)}),(0,Q.jsx)(`button`,{type:`button`,"data-testid":`label-format-pdf`,"aria-pressed":A.format===`pdf`,onClick:()=>q({format:`pdf`}),className:nt(A.format===`pdf`),children:`PDF`}),(0,Q.jsx)(`button`,{type:`button`,"data-testid":`label-format-png`,"aria-pressed":A.format===`png`,onClick:()=>q({format:`png`}),className:nt(A.format===`png`),children:`PNG`}),A.format===`png`&&(0,Q.jsx)(`select`,{"data-testid":`label-dpi-select`,"aria-label":u(`inventory.labels.dpi`,`Resolution`),value:A.dpi,onChange:e=>q({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:Wn.map(e=>(0,Q.jsxs)(`option`,{value:e,children:[e,` dpi`]},e))})]}),A.format===`png`&&(0,Q.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:u(`inventory.labels.pngHint`,`For label printer software that takes images. Match your printer's resolution; several labels come as a ZIP.`)}),Ue&&(0,Q.jsx)(`p`,{"data-testid":`label-png-too-large`,className:`text-xs text-red-400`,children:u(`inventory.labels.pngTooLarge`,`PNG export is limited to {{maxPages}} sheet pages at {{dpi}} dpi, and this is {{pages}}. Use PDF, a lower resolution or fewer spools.`,{maxPages:Math.floor(15e3/A.dpi),dpi:A.dpi,pages:He})})]})]})]}),(0,Q.jsxs)(`div`,{className:`flex justify-end gap-2 px-5 py-3 border-t border-bambu-dark-tertiary`,children:[(0,Q.jsx)(W,{variant:`secondary`,onClick:t,disabled:b,children:u(`common.cancel`,`Cancel`)}),(0,Q.jsxs)(W,{"data-testid":`label-print-button`,onClick:tt,disabled:!K,children:[b?(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}):A.format===`pdf`?(0,Q.jsx)(xe,{className:`w-4 h-4`}):(0,Q.jsx)(P,{className:`w-4 h-4`}),A.format===`pdf`?u(`inventory.labels.createPdf`,`Create PDF`):u(`inventory.labels.downloadPng`,`Download PNG`)]})]})]}),ue&&R&&(0,Q.jsx)(Be,{title:u(`inventory.labels.custom.deleteTitle`,`Delete label template`),message:u(`inventory.labels.custom.deleteMessage`,`Delete "{{name}}" for everyone? Labels already printed are not affected.`,{name:R.name}),confirmText:u(`inventory.labels.custom.delete`,`Delete`),variant:`danger`,overlayZIndex:`z-[60]`,isLoading:ge.isPending,onConfirm:()=>Ye(R),onCancel:()=>de(!1)})]})}function lr({value:e,onChange:t,autoFocus:r=!1}){let{t:i}=n();return(0,Q.jsxs)(`label`,{className:`block space-y-0.5`,children:[(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:i(`inventory.labels.custom.name`,`Template name`)}),(0,Q.jsx)(`input`,{"data-testid":`label-template-name`,type:`text`,maxLength:100,autoFocus:r,value:e,onChange:e=>t(e.target.value),className:`w-full px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green`})]})}function ur({onClose:e,onImported:t}){let{t:i}=n(),[a,o]=(0,Z.useState)(null),[s,c]=(0,Z.useState)(!1),[l,u]=(0,Z.useState)(null),[d,f]=(0,Z.useState)(!1),[p,m]=(0,Z.useState)(!1),[h,g]=(0,Z.useState)(null),_=(0,Z.useRef)(null),v=async e=>{o(e),u(null),g(null),f(!0);try{let t=await r.importSpoolsCsvPreview(e);u(t)}catch(e){g(e instanceof Error?e.message:i(`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 r.importSpoolsCsv(a)).created)}catch(e){g(e instanceof Error?e.message:i(`inventory.csv.importError`,`Import failed`)),m(!1)}}},S=e=>e===`valid`?(0,Q.jsx)(ie,{className:`w-4 h-4 text-green-500 flex-shrink-0`}):e===`error`?(0,Q.jsx)(k,{className:`w-4 h-4 text-red-500 flex-shrink-0`}):(0,Q.jsx)(M,{className:`w-4 h-4 text-bambu-gray flex-shrink-0`}),C=l?.valid_count??0;return(0,Q.jsx)(`div`,{className:`fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4`,children:(0,Q.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,Q.jsxs)(`div`,{className:`p-4 border-b border-bambu-dark-tertiary flex items-center justify-between`,children:[(0,Q.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:i(`inventory.csv.modalTitle`,`Import spools from CSV`)}),(0,Q.jsx)(`button`,{onClick:e,className:`p-1 hover:bg-bambu-dark rounded`,children:(0,Q.jsx)(Le,{className:`w-5 h-5 text-bambu-gray`})})]}),(0,Q.jsxs)(`div`,{className:`p-4 space-y-4 overflow-y-auto flex-1`,children:[(0,Q.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,Q.jsx)(Ie,{className:`w-9 h-9 mx-auto mb-2 ${s?`text-bambu-green`:`text-bambu-gray`}`}),a?(0,Q.jsxs)(`p`,{className:`text-white font-medium flex items-center justify-center gap-2`,children:[(0,Q.jsx)(I,{className:`w-4 h-4`}),` `,a.name]}):(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(`p`,{className:`text-white font-medium`,children:i(`inventory.csv.selectFile`,`Choose a CSV file or drag it here`)}),(0,Q.jsx)(`p`,{className:`text-xs text-bambu-gray/70 mt-1`,children:i(`inventory.csv.dragHint`,`Header: material (required), brand, subtype, color_name, rgba, …`)})]})]}),(0,Q.jsx)(`input`,{ref:_,type:`file`,accept:`.csv,text/csv`,className:`hidden`,onChange:y}),d&&(0,Q.jsxs)(`div`,{className:`flex items-center justify-center gap-2 text-bambu-gray py-4`,children:[(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}),i(`inventory.csv.parsing`,`Reading file…`)]}),h&&(0,Q.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,Q.jsx)(k,{className:`w-5 h-5 text-red-600 dark:text-red-400 mt-0.5 flex-shrink-0`}),(0,Q.jsx)(`p`,{className:`text-sm text-red-700 dark:text-red-300 break-words`,children:h})]}),l&&(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsxs)(`div`,{className:`flex flex-wrap gap-3 text-sm`,children:[(0,Q.jsx)(`span`,{className:`px-2 py-1 rounded bg-green-50 dark:bg-green-500/10 text-green-700 dark:text-green-400`,children:i(`inventory.csv.validCount`,`{{count}} valid`,{count:l.valid_count})}),(0,Q.jsx)(`span`,{className:`px-2 py-1 rounded bg-red-50 dark:bg-red-500/10 text-red-700 dark:text-red-400`,children:i(`inventory.csv.errorCount`,`{{count}} error`,{count:l.error_count})}),(0,Q.jsx)(`span`,{className:`px-2 py-1 rounded bg-bambu-dark text-bambu-gray`,children:i(`inventory.csv.skippedCount`,`{{count}} skipped`,{count:l.skipped_count})})]}),l.warnings.length>0&&(0,Q.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,Q.jsx)(`p`,{className:`text-xs text-yellow-700 dark:text-yellow-300`,children:e},t))}),l.rows.length>0&&(0,Q.jsx)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:(0,Q.jsx)(`div`,{className:`max-h-72 overflow-y-auto`,children:(0,Q.jsxs)(`table`,{className:`w-full text-sm`,children:[(0,Q.jsx)(`thead`,{className:`bg-bambu-dark sticky top-0`,children:(0,Q.jsxs)(`tr`,{className:`text-left text-bambu-gray`,children:[(0,Q.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:i(`inventory.csv.colRow`,`Row`)}),(0,Q.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:i(`inventory.csv.colStatus`,`Status`)}),(0,Q.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:i(`inventory.material`,`Material`)}),(0,Q.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:i(`inventory.brand`,`Brand`)}),(0,Q.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:i(`inventory.csv.colColor`,`Color`)})]})}),(0,Q.jsx)(`tbody`,{children:l.rows.map(e=>(0,Q.jsxs)(`tr`,{className:`border-t border-bambu-dark-tertiary`,children:[(0,Q.jsx)(`td`,{className:`px-3 py-2 text-bambu-gray`,children:e.row_number}),(0,Q.jsx)(`td`,{className:`px-3 py-2`,children:(0,Q.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[S(e.status),e.status===`error`&&e.reason&&(0,Q.jsx)(`span`,{className:`text-xs text-red-700 dark:text-red-400 break-words`,children:e.reason})]})}),(0,Q.jsx)(`td`,{className:`px-3 py-2 text-white`,children:e.material||`—`}),(0,Q.jsx)(`td`,{className:`px-3 py-2 text-white`,children:e.brand||`—`}),(0,Q.jsx)(`td`,{className:`px-3 py-2`,children:(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[e.rgba&&(0,Q.jsx)(`span`,{className:`inline-block w-4 h-4 rounded-full border border-bambu-dark-tertiary flex-shrink-0`,style:K(e.rgba)}),(0,Q.jsx)(`span`,{className:`text-white`,children:e.color_name||`—`}),e.resolved_color&&!e.cross_material_color&&(0,Q.jsx)(`span`,{title:i(`inventory.csv.colorResolved`,`Color filled from catalog`),children:(0,Q.jsx)(U,{className:`w-3.5 h-3.5 text-bambu-green flex-shrink-0`})}),e.cross_material_color&&(0,Q.jsx)(`span`,{title:i(`inventory.csv.colorCrossMaterial`,`Color taken from a different material — no exact match in catalog`),children:(0,Q.jsx)(H,{className:`w-3.5 h-3.5 text-yellow-500 flex-shrink-0`})}),e.duplicate_of_existing&&(0,Q.jsx)(`span`,{title:i(`inventory.csv.duplicateExisting`,`A spool with this material, brand and color already exists — it will still be imported as a new spool`),children:(0,Q.jsx)(N,{className:`w-3.5 h-3.5 text-amber-600 dark:text-amber-400 flex-shrink-0`})})]})})]},e.row_number))})]})})})]})]}),(0,Q.jsxs)(`div`,{className:`p-4 border-t border-bambu-dark-tertiary flex justify-end gap-2`,children:[(0,Q.jsx)(W,{variant:`secondary`,onClick:e,disabled:p,children:i(`common.cancel`)}),(0,Q.jsx)(W,{onClick:x,disabled:!l||C===0||p,children:p?(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(L,{className:`w-4 h-4 mr-2 animate-spin`}),i(`inventory.csv.importing`,`Importing…`)]}):C>0?i(`inventory.csv.importValidRows`,`Import {{count}} valid rows`,{count:C}):i(`inventory.csv.noValidRows`,`No valid rows`)})]})]})})}function dr({open:e,onClose:t}){let{t:i}=n(),{showToast:a}=re(),o=d(),{data:c=[],isLoading:l,isError:u}=f({queryKey:J,queryFn:r.getSuppliers,enabled:e}),[p,m]=(0,Z.useState)(``),[h,g]=(0,Z.useState)(!1),[_,v]=(0,Z.useState)(null),[y,b]=(0,Z.useState)(``),[x,S]=(0,Z.useState)(``),[C,w]=(0,Z.useState)(``),[T,D]=(0,Z.useState)(``),[O,k]=(0,Z.useState)(!1),[A,ee]=(0,Z.useState)(null);if((0,Z.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 te=c.filter(e=>e.name.toLowerCase().includes(p.toLowerCase())),j=()=>{b(``),S(``),w(``),D(``)},ne=()=>({name:y.trim(),website:x.trim()||null,customer_number:C.trim()||null,note:T.trim()||null}),ie=async()=>{if(!y.trim()){a(i(`inventory.suppliers.nameRequired`),`error`);return}k(!0);try{await r.createSupplier(ne()),await Xe(o),g(!1),j(),a(i(`inventory.suppliers.added`),`success`)}catch(e){console.error(`SuppliersModal.handleAdd failed:`,e),a(i(e instanceof s&&e.status===409?`inventory.suppliers.duplicateName`:`inventory.suppliers.addFailed`),`error`)}finally{k(!1)}},M=e=>{v(e.id),b(e.name),S(e.website??``),w(e.customer_number??``),D(e.note??``)},ae=()=>{v(null),j()},oe=async e=>{if(!y.trim()){a(i(`inventory.suppliers.nameRequired`),`error`);return}k(!0);try{await r.updateSupplier(e,ne()),await Xe(o),ae(),a(i(`inventory.suppliers.updated`),`success`)}catch(e){console.error(`SuppliersModal.handleUpdate failed:`,e),a(i(e instanceof s&&e.status===409?`inventory.suppliers.duplicateName`:`inventory.suppliers.updateFailed`),`error`)}finally{k(!1)}},N=async()=>{if(A)try{await r.deleteSupplier(A.id),await Xe(o),a(i(`inventory.suppliers.deleted`),`success`)}catch(e){console.error(`SuppliersModal.handleDelete failed:`,e),e instanceof s&&e.status===409?a(i(`inventory.suppliers.deleteBlocked`,{count:A.spool_count}),`error`):a(i(`inventory.suppliers.deleteFailed`),`error`)}finally{ee(null)}},P=(0,Q.jsxs)(`div`,{className:`space-y-2`,children:[(0,Q.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:i(`inventory.suppliers.namePlaceholder`),value:y,maxLength:200,onChange:e=>b(e.target.value)}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.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:i(`inventory.suppliers.websitePlaceholder`),value:x,maxLength:500,onChange:e=>S(e.target.value)}),(0,Q.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:i(`inventory.suppliers.customerNumberPlaceholder`),value:C,maxLength:100,onChange:e=>w(e.target.value)})]}),(0,Q.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:i(`inventory.suppliers.notePlaceholder`),value:T,maxLength:500,onChange:e=>D(e.target.value)})]});return(0,Q.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,Q.jsx)(`div`,{className:`absolute inset-0 bg-black/60`,onClick:t}),(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center justify-between gap-4 px-6 py-4 border-b border-bambu-dark-tertiary`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(Bt,{className:`w-5 h-5 text-bambu-gray`}),(0,Q.jsx)(`h2`,{id:`suppliers-modal-title`,className:`text-lg font-semibold text-white`,children:i(`inventory.suppliers.title`)}),(0,Q.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[`(`,c.length,`)`]})]}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsxs)(`button`,{onClick:()=>{g(!0),v(null),j()},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,Q.jsx)(be,{className:`w-4 h-4`}),(0,Q.jsx)(`span`,{className:`hidden sm:inline`,children:i(`common.add`)})]}),(0,Q.jsx)(`button`,{onClick:t,className:`p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors`,"aria-label":i(`common.close`),children:(0,Q.jsx)(Le,{className:`w-5 h-5`})})]})]}),(0,Q.jsxs)(`div`,{className:`flex-1 min-h-0 overflow-y-auto px-6 py-4 space-y-4`,children:[(0,Q.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:i(`inventory.suppliers.description`)}),(0,Q.jsxs)(`div`,{className:`relative`,children:[(0,Q.jsx)(Ee,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray`}),(0,Q.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:i(`inventory.suppliers.search`),value:p,onChange:e=>m(e.target.value)})]}),h&&(0,Q.jsxs)(`div`,{className:`p-4 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary space-y-3`,children:[(0,Q.jsx)(`h3`,{className:`text-sm font-medium text-white`,children:i(`inventory.suppliers.addNew`)}),P,(0,Q.jsxs)(`div`,{className:`flex gap-2 justify-end`,children:[(0,Q.jsx)(`button`,{onClick:()=>{g(!1),j()},className:`px-3 py-2 rounded-lg text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary`,children:i(`common.cancel`)}),(0,Q.jsxs)(`button`,{onClick:ie,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,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Q.jsx)(E,{className:`w-4 h-4`}),i(`common.add`)]})]})]}),l?(0,Q.jsxs)(`div`,{className:`flex items-center justify-center py-8 text-bambu-gray`,children:[(0,Q.jsx)(L,{className:`w-5 h-5 animate-spin mr-2`}),i(`common.loading`)]}):u?(0,Q.jsxs)(`div`,{className:`flex items-center justify-center gap-2 py-8 text-sm text-red-400`,children:[(0,Q.jsx)(H,{className:`w-4 h-4`}),i(`inventory.suppliers.loadFailed`)]}):(0,Q.jsx)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg`,children:(0,Q.jsxs)(`table`,{className:`w-full text-sm`,children:[(0,Q.jsx)(`thead`,{className:`bg-bambu-dark sticky top-0`,children:(0,Q.jsxs)(`tr`,{children:[(0,Q.jsx)(`th`,{className:`px-4 py-2 text-left text-bambu-gray font-medium`,children:i(`common.name`)}),(0,Q.jsx)(`th`,{className:`px-4 py-2 text-left text-bambu-gray font-medium hidden sm:table-cell`,children:i(`inventory.suppliers.customerNumber`)}),(0,Q.jsx)(`th`,{className:`px-4 py-2 text-right text-bambu-gray font-medium w-20`,children:i(`inventory.suppliers.spools`)}),(0,Q.jsx)(`th`,{className:`px-4 py-2 w-24`})]})}),(0,Q.jsx)(`tbody`,{children:te.length===0?(0,Q.jsx)(`tr`,{children:(0,Q.jsx)(`td`,{colSpan:4,className:`px-4 py-8 text-center text-bambu-gray`,children:i(p?`inventory.suppliers.noMatch`:`inventory.suppliers.empty`)})}):te.map(e=>(0,Q.jsx)(`tr`,{className:`border-t border-bambu-dark-tertiary hover:bg-bambu-dark align-top`,children:_===e.id?(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(`td`,{className:`px-4 py-2`,colSpan:3,children:P}),(0,Q.jsx)(`td`,{className:`px-4 py-2`,children:(0,Q.jsxs)(`div`,{className:`flex justify-end gap-1`,children:[(0,Q.jsx)(`button`,{onClick:()=>oe(e.id),disabled:O,className:`p-1.5 rounded hover:bg-green-500/20 text-green-500`,children:O?(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Q.jsx)(E,{className:`w-4 h-4`})}),(0,Q.jsx)(`button`,{onClick:ae,className:`p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray`,children:(0,Q.jsx)(Le,{className:`w-4 h-4`})})]})})]}):(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsxs)(`td`,{className:`px-4 py-2 text-white`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Q.jsx)(`span`,{children:e.name}),e.website&&(0,Q.jsx)(`a`,{href:e.website,target:`_blank`,rel:`noopener noreferrer`,className:`text-bambu-gray hover:text-bambu-green`,title:e.website,children:(0,Q.jsx)(F,{className:`w-3.5 h-3.5`})})]}),e.note&&(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mt-0.5`,children:e.note})]}),(0,Q.jsx)(`td`,{className:`px-4 py-2 text-bambu-gray hidden sm:table-cell`,children:e.customer_number||`-`}),(0,Q.jsx)(`td`,{className:`px-4 py-2 text-right font-mono text-bambu-gray`,children:e.spool_count}),(0,Q.jsx)(`td`,{className:`px-4 py-2`,children:(0,Q.jsxs)(`div`,{className:`flex justify-end gap-1`,children:[(0,Q.jsx)(`button`,{onClick:()=>{M(e),g(!1)},className:`p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white`,children:(0,Q.jsx)(ye,{className:`w-4 h-4`})}),(0,Q.jsx)(`button`,{onClick:()=>ee(e),className:`p-1.5 rounded bg-red-500/10 hover:bg-red-500/20 text-red-500`,children:(0,Q.jsx)(z,{className:`w-4 h-4`})})]})})]})},e.id))})]})})]})]}),A&&(0,Q.jsx)(Be,{title:i(`inventory.suppliers.deleteTitle`),message:A.spool_count>0?i(`inventory.suppliers.deleteConfirmReferenced`,{name:A.name,count:A.spool_count}):i(`inventory.suppliers.deleteConfirm`,{name:A.name}),confirmText:i(`common.delete`),variant:`danger`,onConfirm:N,onCancel:()=>ee(null)})]})}var fr=[{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 pr({value:e,onChange:t,options:r,allowCustom:i,placeholderKey:a,disabled:o}){let{t:s}=n(),c=(0,Z.useRef)(null),[l,u]=(0,Z.useState)(!1),[d,f]=(0,Z.useState)(``);(0,Z.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:r.find(t=>t.value===e)?.label??e,m=(0,Z.useMemo)(()=>{if(!l)return r;let e=d.trim().toLowerCase();return e?r.filter(t=>t.label.toLowerCase().includes(e)||t.value.toLowerCase().includes(e)):r},[l,d,r]),h=l&&d.trim()&&!r.some(e=>e.value.toLowerCase()===d.trim().toLowerCase());return(0,Q.jsxs)(`div`,{className:`relative`,ref:c,children:[(0,Q.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,Q.jsx)(D,{className:`absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none`}),l&&(0,Q.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,Q.jsx)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray`,children:s(`inventory.noResults`)}),m.map(n=>(0,Q.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,Q.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 mr(...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 hr({isOpen:e,selectedCount:t,isPending:i,availableLocations:a,availableMaterials:o,availableSubtypes:s,availableBrands:c,availableCategories:l,availableMaterialNumbers:u,availableSlicerFilaments:d,availableSlicerFilamentNames:p,spoolmanMode:m=!1,onClose:h,onApply:g}){let{t:_}=n(),v=(0,Z.useMemo)(()=>fr.filter(e=>!(m&&e.internalOnly)),[m]),{data:y=[]}=f({queryKey:[`bulk-edit-cloud-presets`],enabled:e,staleTime:300*1e3,queryFn:async()=>{let e=[];try{if((await r.getCloudStatus()).is_authenticated){let t=await r.getFilamentPresets();e.push(...t)}}catch{}try{if((await r.orcaCloudStatus()).connected){let t=await r.orcaCloudListProfiles();e.push(...t.filament)}}catch{}return e}}),{data:b}=f({queryKey:[`bulk-edit-local-presets`],enabled:e,staleTime:300*1e3,queryFn:r.getLocalPresets}),{data:x=[]}=f({queryKey:[`builtin-filaments`],enabled:e,staleTime:300*1e3,queryFn:r.getBuiltinFilaments}),S=(0,Z.useMemo)(()=>Tt(y,new Set,b?.filament??[],x),[y,b,x]),[C,w]=(0,Z.useState)({}),T=(0,Z.useMemo)(()=>mr(pt,o).map(e=>({value:e,label:e})),[o]),E=(0,Z.useMemo)(()=>mr(Ot,s).map(e=>({value:e,label:e})),[s]),D=(0,Z.useMemo)(()=>mr(Dt,c).map(e=>({value:e,label:e})),[c]),O=(0,Z.useMemo)(()=>mr(l).map(e=>({value:e,label:e})),[l]),k=(0,Z.useMemo)(()=>mr(u).map(e=>({value:e,label:e})),[u]),A=(0,Z.useMemo)(()=>{let e=S.map(e=>({value:e.code,label:e.displayName})),t=d.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,d]),ee=(0,Z.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]),te=(0,Z.useMemo)(()=>a.map(e=>({value:String(e.id),label:e.name})),[a]);if(!e)return null;let j=(e,t)=>{w(n=>({...n,[e]:t}))},ne=e=>{w(t=>{let n={...t};return delete n[e],n})},re=(()=>{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})(),ie=Object.keys(re).length>0,M=v.some(e=>{let t=C[e.id];return t===void 0||t.trim()===``?!1:re[e.id]===void 0}),ae=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`?te:[],oe=e=>{let t=C[e.id]??``;if(e.type===`searchable`||e.type===`searchableClosed`)return(0,Q.jsx)(pr,{value:t,onChange:t=>{t===``?ne(e.id):j(e.id,t)},options:ae(e.id),allowCustom:e.type===`searchable`,disabled:i});if(e.type===`textarea`)return(0,Q.jsx)(`textarea`,{disabled:i,value:t,onChange:t=>j(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 n=t.trim().replace(/^#/,``),r=n.length===6?`${n}FF`:n,a=t.trim()!==``&&!/^[0-9A-Fa-f]{8}$/.test(r);return(0,Q.jsxs)(`div`,{children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`input`,{type:`color`,disabled:i,value:`#${(t||`808080`).replace(/^#/,``).slice(0,6)}`,onChange:t=>j(e.id,t.target.value.replace(/^#/,``).toUpperCase()),className:`h-9 w-12 rounded cursor-pointer`}),(0,Q.jsx)(`input`,{type:`text`,disabled:i,value:t,onChange:t=>j(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,Q.jsx)(`p`,{className:`mt-1 text-xs text-red-600 dark:text-red-400`,children:_(`inventory.bulk.invalidHex`)})]})}return(0,Q.jsx)(`input`,{type:e.type===`number`?`number`:`text`,disabled:i,value:t,onChange:t=>j(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,Q.jsx)(`div`,{className:`fixed inset-0 bg-black/50 flex items-center justify-center p-4 z-50`,onClick:i?void 0:h,children:(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center justify-between p-5 border-b border-bambu-dark-tertiary`,children:[(0,Q.jsxs)(`div`,{children:[(0,Q.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:_(`inventory.bulk.editTitle`)}),(0,Q.jsx)(`p`,{className:`text-sm text-bambu-gray mt-0.5`,children:_(`inventory.bulk.editSubtitle`,{count:t})})]}),(0,Q.jsx)(`button`,{onClick:h,disabled:i,className:`p-1 text-bambu-gray hover:text-white transition-colors`,"aria-label":_(`common.close`),children:(0,Q.jsx)(Le,{className:`w-5 h-5`})})]}),(0,Q.jsx)(`p`,{className:`px-5 pt-4 text-xs text-bambu-gray`,children:_(`inventory.bulk.editHint`)}),(0,Q.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,Q.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,Q.jsx)(`div`,{className:`pt-2`,children:(0,Q.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,checked:t,onChange:t=>{t.target.checked?j(e.id,``):ne(e.id)},"aria-label":_(`inventory.bulk.toggleField`)})}),(0,Q.jsxs)(`div`,{className:`flex-1`,children:[(0,Q.jsx)(`label`,{className:`block text-sm text-bambu-gray mb-1`,children:_(e.labelKey)}),oe(e)]})]},e.id)})}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-3 p-5 border-t border-bambu-dark-tertiary`,children:[(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:_(`inventory.bulk.changeCount`,{count:Object.keys(re).length})}),(0,Q.jsxs)(`div`,{className:`ml-auto flex gap-2`,children:[(0,Q.jsx)(W,{variant:`secondary`,onClick:h,disabled:i,children:_(`common.cancel`)}),(0,Q.jsx)(W,{onClick:()=>g(re),disabled:!ie||i||M,children:i?(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(L,{className:`w-4 h-4 mr-2 animate-spin`}),_(`inventory.bulk.applyPending`)]}):_(`inventory.bulk.applyButton`,{count:t})})]})]})]})})}function gr(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 _r(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 vr(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 yr=`bambuddy-inventory-columns`,br=`none`.padStart(65,`_`);function xr(e){return(e||``).toLowerCase().replace(/\d+/g,e=>e.padStart(64,`0`))}var Sr=[{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 Cr(){try{let e=localStorage.getItem(yr);if(e){let t=JSON.parse(e),n=new Set(Sr.map(e=>e.id)),r=new Set(t.map(e=>e.id)),i=[...t.filter(e=>n.has(e.id))];for(let e of Sr){if(r.has(e.id))continue;let t=Sr.indexOf(e),n=i.length;for(let e=t-1;e>=0;e--){let t=i.findIndex(t=>t.id===Sr[e].id);if(t!==-1){n=t+1;break}}i.splice(n,0,e)}return i}}catch{}return Sr.map(e=>({...e}))}function wr(e){try{localStorage.setItem(yr,JSON.stringify(e))}catch{}}function Tr(e,t=!1){return t&&e>=1e3?`${(e/1e3).toFixed(1)}kg`:`${Math.round(e)}g`}var Er={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 Dr(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 Or(e,t=`system`){if(!e)return`-`;let n=Ve(e);return n?G(n,t):`-`}var kr={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`)},Ar={id:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm font-medium text-white`,children:e.id}),added_time:({spool:e,dateFormat:t})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Or(e.created_at,t)}),encode_time:({spool:e,dateFormat:t})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Or(e.encode_time,t)}),last_used_time:({spool:e,dateFormat:t})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.last_used?Or(e.last_used,t):`Never`}),last_dried:({spool:e,dateFormat:t})=>{if(!e.last_dried_at)return(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`});let n=Dr(e);return(0,Q.jsxs)(`div`,{className:`text-sm text-bambu-gray`,children:[(0,Q.jsx)(`div`,{children:Or(e.last_dried_at,t)}),n&&(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray/60`,children:n})]})},rgba:({spool:e})=>(0,Q.jsx)(`div`,{className:`flex items-center justify-center`,children:(0,Q.jsx)(Ye,{rgba:e.rgba,extraColors:e.extra_colors,effectType:e.effect_type,effectSize:`table`,subtype:e.subtype})}),material:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:e.material}),subtype:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.subtype||`-`}),color_name:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:q(e.color_name,e.rgba,e.color_name_is_synthesized)||`-`}),brand:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.brand||`-`}),slicer_filament:({spool:e})=>(0,Q.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,Q.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,Q.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,` `,Ue(n.ams_id,n.tray_id,a,i),n.ams_label?` (${n.ams_label})`:``]})},storage_location:({spool:e})=>e.storage_location?(0,Q.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,Q.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,Q.jsx)(`span`,{title:s.name,className:`text-sm ${Hr(s,r,i,a,o)}`,children:Vr(s,n)}):(0,Q.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,Q.jsx)(`span`,{title:s.name,className:`text-sm ${Hr(s,r,i,a,o)}`,children:Vr(s,n)}):(0,Q.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,Q.jsx)(`span`,{title:s.name,className:`text-sm ${Hr(s,r,i,a,o)}`,children:Vr(s,n)}):(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},label_weight:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:Tr(e.label_weight)}),net:({remaining:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-white`,children:Tr(e)}),gross:({spool:e,remaining:t})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Tr(t+e.core_weight)}),used:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.weight_used>0?Tr(e.weight_used):`-`}),printed_total:()=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`}),printed_since_weight:()=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`}),note:({spool:e})=>(0,Q.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,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.material_number||`-`}),suppliers:({spool:e})=>{let t=e.suppliers??[];return t.length===0?(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Q.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,Q.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,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Q.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,Q.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,Q.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},data_origin:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.data_origin||`-`}),tag_type:({spool:e})=>(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.tag_type||`-`}),stock:({spool:e,t})=>e.slicer_filament?(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Q.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,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Q.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,Q.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,Q.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}=Pt(e);return(0,Q.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[r==null?`-`:`${t}${r.toFixed(2)}`,i&&(0,Q.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,Q.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,Q.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,Q.jsxs)(`span`,{children:[Math.round(n),`g`]}),o?(0,Q.jsx)(E,{className:`w-3 h-3`}):(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(H,{className:`w-3 h-3`}),t&&(0,Q.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,Q.jsx)(Ce,{className:`w-3.5 h-3.5`})})]})]})}},jr={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=>We(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||``} ${Ue(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=>xr(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=>Pt(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},Mr=`bambuddy-inventory-sort`;function Nr(){try{let e=localStorage.getItem(Mr);if(e)return JSON.parse(e)}catch{}return null}function Pr(e){try{e?localStorage.setItem(Mr,JSON.stringify(e)):localStorage.removeItem(Mr)}catch{}}function Fr(){let{data:e}=f({queryKey:[`spoolman-settings`],queryFn:r.getSpoolmanSettings,staleTime:300*1e3}),t=e!==void 0;return(0,Q.jsx)(Ir,{spoolmanMode:e?.spoolman_enabled===`true`&&!!e?.spoolman_url,spoolmanModeReady:t})}function Ir({spoolmanMode:e=!1,spoolmanModeReady:t=!0}){let{t:i}=n(),a=Ge(),o=d(),{showToast:l}=re(),{hasPermission:g,loading:_}=ze(),S=!_&&g(`inventory:forecast_read`),[C,T]=u(),[E,O]=(0,Z.useState)(null),k=(0,Z.useRef)(!1),[A,ie]=(0,Z.useState)(!1),[M,N]=(0,Z.useState)(null),[F,I]=(0,Z.useState)(null),[le,pe]=(0,Z.useState)(!1),[he,ve]=(0,Z.useState)(!1),[ye,Ce]=(0,Z.useState)(!1),[Te,R]=(0,Z.useState)(!1),[De,Oe]=(0,Z.useState)(`active`),[ke,Ae]=(0,Z.useState)(`all`),[B,Me]=(0,Z.useState)(``),[Ne,Pe]=(0,Z.useState)(``),[V,Fe]=(0,Z.useState)(``),[U,Re]=(0,Z.useState)(``),[G,Ve]=(0,Z.useState)(``),[He,Ue]=(0,Z.useState)(``),[K,We]=(0,Z.useState)(`all`),[Ye,Xe]=(0,Z.useState)(``),[J,Ze]=(0,Z.useState)(`table`),[et,tt]=(0,Z.useState)(Nr),[rt,it]=(0,Z.useState)(Cr),[at,ot]=(0,Z.useState)(!1),[st,ct]=(0,Z.useState)(()=>{try{return localStorage.getItem(`bambuddy-inventory-group`)===`true`}catch{return!1}}),[lt,ut]=(0,Z.useState)(new Set),[Y,dt]=(0,Z.useState)(new Set),[ft,pt]=(0,Z.useState)(!1),[mt,ht]=(0,Z.useState)(null),gt=(0,Z.useCallback)(e=>{dt(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[]),_t=(0,Z.useCallback)(()=>dt(new Set),[]);(0,Z.useEffect)(()=>{dt(new Set)},[De,ke,B,Ne,V,U,G,He,K,Ye]);let[vt,yt]=(0,Z.useState)(0),[bt,xt]=(0,Z.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:St}=qe(),Ct=St?.date_format||`system`,wt=(St?.location_sensor_poll_interval||120)*1e3,{colorize:Tt,aboveColor:Et,belowColor:Dt,optimalColor:Ot}=_e(),kt=e?[`spoolman-inventory-spools`]:[`inventory-spools`],At=()=>Qe(o,kt),{data:X,isLoading:jt}=f({queryKey:kt,queryFn:()=>e?r.getSpoolmanInventorySpools(!0):r.getSpools(!0),refetchInterval:3e4}),Pt=(0,Z.useCallback)(async()=>{ve(!0);try{await r.exportSpoolsCsv()}catch(e){l(e instanceof Error?e.message:i(`inventory.csv.exportError`,`Export failed`),`error`)}finally{ve(!1)}},[l,i]),Ft=e?i(`inventory.csv.spoolmanHint`,`In Spoolman mode, use Spoolman's built-in CSV import/export.`):void 0,{data:It=[]}=f({queryKey:$e,queryFn:r.getLocations}),Lt=C.get(`location_id`),zt=Lt===`__none__`?`__none__`:Lt&&/^\d+$/.test(Lt)&&Number(Lt)>0?Lt:``,Ht=C.get(`spool`),Ut=Ht&&/^\d+$/.test(Ht)&&Number(Ht)>0?Number(Ht):null,Wt=X?.find(e=>e.id===Ut)??null,Gt=(0,Z.useCallback)(()=>{k.current=!0,T(e=>(e.delete(`spool`),e),{replace:!0})},[T]),{data:Kt,isError:qt,error:Jt}=f({queryKey:e?[`spoolman-inventory-spool`,Ut]:[`inventory-spool`,Ut],queryFn:()=>e?r.getSpoolmanInventorySpool(Ut):r.getSpool(Ut),enabled:t&&Ut!==null&&Wt===null,staleTime:1/0,retry:(e,t)=>e<2&&!(t instanceof s&&t.status===404)});(0,Z.useEffect)(()=>{if(!k.current){if(t&&Ut&&Wt){Gt(),O({spool:Wt,mode:`edit`});return}if(Kt){Gt(),O({spool:Kt,mode:`edit`});return}if(qt){Gt();let e=Jt instanceof s&&Jt.status===404;l(i(e?`inventory.deepLinkSpoolNotFound`:`inventory.deepLinkFetchFailed`),`error`)}}},[t,Ut,Wt,Kt,qt,Jt,Gt,l,i]);let{data:Yt}=f({queryKey:[`spool-assignments`],queryFn:()=>r.getAssignments(),refetchInterval:3e4,enabled:t&&!e}),Xt=e?void 0:Yt,{data:Zt=[],isError:Qt}=f({queryKey:[`spoolman-slot-assignments`,`all`],queryFn:()=>r.getSpoolmanSlotAssignments(),enabled:e,refetchInterval:3e4,staleTime:1e4,retry:1}),$t=(0,Z.useRef)(!1);(0,Z.useEffect)(()=>{Qt&&!$t.current?($t.current=!0,l(i(`inventory.spoolmanUnreachable`),`error`)):Qt||($t.current=!1)},[Qt,l,i]);let{data:en}=f({queryKey:[`spool-catalog`],queryFn:()=>r.getSpoolCatalog()}),tn=p({mutationFn:t=>e?r.deleteSpoolmanInventorySpool(t):r.deleteSpool(t),onSuccess:()=>{At(),l(i(`inventory.spoolDeleted`),`success`)},onError:e=>{e instanceof s&&e.status===404?l(i(`inventory.deleteSpoolNotFound`),`error`):e instanceof s&&e.status===503?l(i(`inventory.spoolmanUnreachable`),`error`):l(i(`inventory.deleteFailed`),`error`)}}),rn=p({mutationFn:t=>e?r.archiveSpoolmanInventorySpool(t):r.archiveSpool(t),onSuccess:()=>{At(),l(i(`inventory.spoolArchived`),`success`)},onError:e=>{e instanceof s&&e.status===404?l(i(`inventory.archiveSpoolNotFound`),`error`):e instanceof s&&e.status===503?l(i(`inventory.spoolmanUnreachable`),`error`):l(i(`inventory.archiveFailed`),`error`)}}),an=p({mutationFn:t=>e?r.restoreSpoolmanInventorySpool(t):r.restoreSpool(t),onSuccess:()=>{At(),l(i(`inventory.spoolRestored`),`success`)},onError:e=>{e instanceof s&&e.status===404?l(i(`inventory.restoreSpoolNotFound`),`error`):e instanceof s&&e.status===503?l(i(`inventory.spoolmanUnreachable`),`error`):l(i(`inventory.restoreFailed`),`error`)}}),on=p({mutationFn:t=>e?r.resetSpoolmanInventorySpoolConsumedCounter(t):r.resetSpoolConsumedCounter(t),onSuccess:()=>{o.invalidateQueries({queryKey:kt}),l(i(`inventory.consumedCounterReset`),`success`)},onError:()=>{l(i(`inventory.resetConsumedCounterFailed`),`error`)}}),sn=e=>(e.not_found?.length??0)+(e.errors?.length??0),cn=p({mutationFn:t=>e?r.bulkResetSpoolmanInventorySpoolConsumedCounter(t):r.bulkResetSpoolConsumedCounter(t),onSuccess:e=>{o.invalidateQueries({queryKey:kt});let t=sn(e);if(e.reset===0&&t>0){l(i(`inventory.bulk.resetAllFailed`,{count:t}),`error`);return}t>0?l(i(`inventory.bulk.resetPartial`,{ok:e.reset,failed:t}),`warning`):l(i(`inventory.allConsumedCountersReset`,{count:e.reset}),`success`),ht(null),_t()},onError:()=>{l(i(`inventory.resetConsumedCounterFailed`),`error`)}}),ln=p({mutationFn:async({ids:t,update:n})=>e?r.bulkUpdateSpoolmanInventorySpools(t,n):r.bulkUpdateSpools(t,n),onSuccess:e=>{At();let t=sn(e);if(e.updated===0){l(i(`inventory.bulk.updateAllFailed`,{count:t}),`error`);return}t>0?l(i(`inventory.bulk.updatePartial`,{ok:e.updated,failed:t}),`warning`):l(i(`inventory.bulk.updateSuccess`,{count:e.updated}),`success`),pt(!1),_t()},onError:e=>{l(e.message||i(`inventory.bulk.updateFailed`),`error`)}}),un=p({mutationFn:async t=>e?r.bulkDeleteSpoolmanInventorySpools(t):r.bulkDeleteSpools(t),onSuccess:e=>{At();let t=sn(e);if(e.deleted===0){l(i(`inventory.bulk.deleteAllFailed`,{count:t}),`error`);return}t>0?l(i(`inventory.bulk.deletePartial`,{ok:e.deleted,failed:t}),`warning`):l(i(`inventory.bulk.deleteSuccess`,{count:e.deleted}),`success`),ht(null),_t()},onError:e=>{l(e.message||i(`inventory.bulk.deleteFailed`),`error`)}}),dn=p({mutationFn:async t=>e?r.bulkArchiveSpoolmanInventorySpools(t):r.bulkArchiveSpools(t),onSuccess:e=>{At();let t=sn(e);if(e.archived===0&&t>0){l(i(`inventory.bulk.archiveAllFailed`,{count:t}),`error`);return}t>0?l(i(`inventory.bulk.archivePartial`,{ok:e.archived,failed:t}),`warning`):l(i(`inventory.bulk.archiveSuccess`,{count:e.archived}),`success`),ht(null),_t()},onError:e=>{l(e.message||i(`inventory.bulk.archiveFailed`),`error`)}}),fn=p({mutationFn:async t=>e?r.bulkRestoreSpoolmanInventorySpools(t):r.bulkRestoreSpools(t),onSuccess:e=>{At();let t=sn(e);if(e.restored===0&&t>0){l(i(`inventory.bulk.restoreAllFailed`,{count:t}),`error`);return}t>0?l(i(`inventory.bulk.restorePartial`,{ok:e.restored,failed:t}),`warning`):l(i(`inventory.bulk.restoreSuccess`,{count:e.restored}),`success`),ht(null),_t()},onError:e=>{l(e.message||i(`inventory.bulk.restoreFailed`),`error`)}}),pn=(0,Z.useMemo)(()=>(X??[]).map(e=>e.id),[X]),mn=async t=>{if(t.last_scale_weight!=null)try{e?await r.syncSpoolmanSpoolWeight(t.id,t.last_scale_weight):await c.updateSpoolWeight(t.id,t.last_scale_weight),o.invalidateQueries({queryKey:kt});let n=[t.brand,t.material,q(t.color_name,t.rgba,t.color_name_is_synthesized)].filter(Boolean).join(` `);l(`Synced "${n}" to scale weight`,`success`)}catch(e){let t=e instanceof s&&e.status===404,n=e instanceof s&&e.status===503;l(i(t?`inventory.syncWeightSpoolNotFound`:n?`inventory.syncWeightSpoolmanUnreachable`:`inventory.syncWeightFailed`),`error`)}},hn=St?.low_stock_threshold??20,[gn,_n]=(0,Z.useState)(!1),[vn,yn]=(0,Z.useState)(hn.toString());(0,Z.useEffect)(()=>{gn||yn(hn.toString())},[hn,gn]);let bn=p({mutationFn:e=>r.updateSettings({low_stock_threshold:e}),onSuccess:()=>{o.invalidateQueries({queryKey:[`settings`]}),l(i(`common.saved`),`success`),_n(!1)},onError:()=>{l(i(`inventory.lowStockThresholdError`),`error`)}}),xn=(0,Z.useMemo)(()=>{if(!X)return null;let e=0,t=0,n=0,r=0,i={};for(let a of X){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??hn)&&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}},[X,hn]),Sn=(Xt?.length??0)+(e?Zt.length:0),Cn=Ke(St?.currency||`USD`),wn=(0,Z.useMemo)(()=>{let e={};for(let t of Xt||[])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 Zt)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},[Xt,Zt]),Tn=(0,Z.useMemo)(()=>{let e={};for(let t of en||[])e[t.id]=t;return e},[en]),{data:En}=f({queryKey:[`locationHaSensors`],queryFn:()=>r.getLocationHASensors()}),On=(0,Z.useMemo)(()=>new Set((En??[]).map(e=>e.location_id)),[En]),kn=(0,Z.useMemo)(()=>{let e=new Set;for(let t of X||[])t.location_id&&On.has(t.location_id)&&e.add(t.location_id);return Array.from(e)},[X,On]),An=J===`cards`||J===`table`&&rt.some(e=>e.visible&&(e.id===`temperature`||e.id===`humidity`||e.id===`battery`)),jn=m({queries:kn.map(e=>({queryKey:[`locationHaSensorReadings`,e],queryFn:()=>r.getLocationHASensorReadings(e,!1),refetchInterval:wt,enabled:An}))}),Mn=(0,Z.useMemo)(()=>{let e={};return kn.forEach((t,n)=>{let r=jn[n]?.data;r&&(e[t]=r)}),e},[kn,jn]),Nn=(0,Z.useMemo)(()=>xn?Object.entries(xn.byMaterial).sort((e,t)=>t[1].weight-e[1].weight).slice(0,4):[],[xn]),Pn=(0,Z.useMemo)(()=>{let e=X||[];if(e=De===`active`?e.filter(e=>!e.archived_at):e.filter(e=>!!e.archived_at),ke===`used`?e=e.filter(e=>e.weight_used>0):ke===`new`?e=e.filter(e=>e.weight_used===0):ke===`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??hn)})),B&&(e=e.filter(e=>e.material===B)),Ne&&(e=e.filter(e=>e.brand===Ne)),V&&(e=V===`__none__`?e.filter(e=>!e.category):e.filter(e=>e.category===V)),U&&(e=U===br?e.filter(e=>!e.material_number?.trim()):e.filter(e=>e.material_number===U)),G)if(G===`__none__`)e=e.filter(e=>(e.suppliers??[]).length===0);else{let t=Number(G);e=e.filter(e=>(e.suppliers??[]).some(e=>e.supplier_id===t))}if(He){let t=Number(He);e=e.filter(e=>e.core_weight_catalog_id===t)}if(zt)if(zt===`__none__`)e=e.filter(e=>!e.location_id&&!e.storage_location?.trim());else{let t=Number(zt),n=It.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 K===`stock`?e=e.filter(e=>!e.slicer_filament):K===`configured`&&(e=e.filter(e=>!!e.slicer_filament)),Ye&&(e=Mt(e,Ye)),e},[X,De,ke,B,Ne,V,U,G,He,K,zt,Ye,hn,It,a]),$=()=>yt(0),Fn=(0,Z.useCallback)(e=>{T(t=>(t.delete(`location_id`),e&&t.set(`location_id`,e),t),{replace:!0}),$()},[T]),In=[...new Set(X?.map(e=>e.material)||[])].sort(),Ln=[...new Set(X?.map(e=>e.brand).filter(Boolean)||[])].sort(),Rn=[...new Set(X?.map(e=>e.category?.trim()).filter(Boolean)||[])].sort(),zn=(X??[]).some(e=>!e.category),Bn=[...new Set(X?.map(e=>e.material_number?.trim()).filter(Boolean)||[])].sort((e,t)=>e.localeCompare(t,void 0,{numeric:!0})),Vn=(X??[]).some(e=>!e.material_number?.trim()),Hn=(0,Z.useMemo)(()=>{let e=new Map;for(let t of X??[])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))},[X]),Un=(X??[]).some(e=>(e.suppliers??[]).length===0),Wn=[...new Set(X?.map(e=>e.core_weight_catalog_id).filter(e=>e!=null)||[])].sort((e,t)=>{let n=(Tn[e]?.name||``).toLowerCase(),r=(Tn[t]?.name||``).toLowerCase();return n.localeCompare(r)}),Gn=(X??[]).some(e=>!e.location_id&&!e.storage_location?.trim()),Kn=De!==`active`||ke!==`all`||!!B||!!Ne||!!V||!!U||!!G||!!He||!!zt||K!==`all`||!!Ye,qn=e=>{it(e),wr(e)},Jn=(0,Z.useMemo)(()=>rt.filter(e=>e.visible).map(e=>e.id),[rt]),Yn=e=>{jr[e]&&(tt(t=>{let n;return n=t?.column===e?t.direction===`asc`?{column:e,direction:`desc`}:null:{column:e,direction:`asc`},Pr(n),n}),$())},Xn=(0,Z.useMemo)(()=>{if(!et)return Pn;let e=jr[et.column];return e?[...Pn].sort((t,n)=>{let r=e(t,wn,Mn),i=e(n,wn,Mn);return r<i?et.direction===`asc`?-1:1:r>i?et.direction===`asc`?1:-1:0}):Pn},[Pn,et,wn,Mn]),Zn=(0,Z.useMemo)(()=>{if(!st)return Xn.map(e=>({type:`single`,spool:e}));let e=new Map;for(let t of Xn)if(!(t.weight_used>0||wn[t.id])){let n=vr(t),r=e.get(n);r?r.push(t):e.set(n,[t])}let t=[],n=new Set;for(let r of Xn){if(r.weight_used>0||wn[r.id]){t.push({type:`single`,spool:r});continue}let i=vr(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},[Xn,st,wn]),Qn=bt===-1,$n=Zn.length,er=Qn?$n||1:bt,tr=Math.max(1,Math.ceil($n/er)),nr=Qn?0:Math.min(vt,tr-1),rr=Qn?Zn:Zn.slice(nr*er,(nr+1)*er),ir=()=>{let e=!st;ct(e),ut(new Set),$();try{localStorage.setItem(`bambuddy-inventory-group`,String(e))}catch{}},ar=e=>{ut(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},or=e=>{xt(e),yt(0);try{localStorage.setItem(`bambuddy-inventory-pageSize`,String(e))}catch{}};return(0,Q.jsxs)(`div`,{className:`p-4 md:p-8 space-y-6`,children:[(0,Q.jsxs)(`div`,{className:`flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between`,children:[(0,Q.jsxs)(`div`,{children:[(0,Q.jsxs)(`h1`,{className:`text-2xl font-bold text-white flex items-center gap-3`,children:[(0,Q.jsx)(fe,{className:`w-7 h-7 text-bambu-green`}),i(`inventory.title`)]}),(0,Q.jsx)(`p`,{className:`text-bambu-gray mt-1`,children:i(`inventory.subtitle`)})]}),(0,Q.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2`,children:[(0,Q.jsxs)(W,{variant:`secondary`,disabled:e,onClick:()=>pe(!0),title:Ft,children:[(0,Q.jsx)(Ie,{className:`w-4 h-4`}),i(`inventory.csv.importButton`,`Import CSV`)]}),(0,Q.jsxs)(W,{variant:`secondary`,disabled:e||he,onClick:Pt,title:Ft,children:[he?(0,Q.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Q.jsx)(P,{className:`w-4 h-4`}),i(`inventory.csv.exportButton`,`Export CSV`)]}),(0,Q.jsxs)(W,{variant:`secondary`,onClick:()=>Ce(!0),children:[(0,Q.jsx)(de,{className:`w-4 h-4`}),i(`locations.manage`)]}),(0,Q.jsxs)(W,{variant:`secondary`,onClick:()=>R(!0),children:[(0,Q.jsx)(Bt,{className:`w-4 h-4`}),i(`inventory.suppliers.title`)]}),(0,Q.jsxs)(W,{variant:`secondary`,disabled:Pn.length===0,onClick:()=>I(Y.size>0?[...Y]:Pn.map(e=>e.id)),title:Pn.length===0?i(`inventory.labels.noSpoolsTitle`,`No spools to label`):Y.size>0?i(`inventory.labels.selectedTitle`,`Print labels for the {{count}} selected spools`,{count:Y.size}):i(`inventory.labels.bulkTitle`,`Pick spools to print labels for from the {{count}} currently shown`,{count:Pn.length}),children:[(0,Q.jsx)(xe,{className:`w-4 h-4`}),i(`inventory.labels.printLabels`,`Print labels…`)]}),(0,Q.jsxs)(W,{variant:`secondary`,onClick:()=>ie(!0),children:[(0,Q.jsx)(Se,{className:`w-4 h-4`}),i(`inventory.qrAssign.button`)]}),(0,Q.jsxs)(W,{onClick:()=>O({spool:null,mode:`create`}),children:[(0,Q.jsx)(be,{className:`w-4 h-4`}),i(`inventory.addSpool`)]})]})]}),A&&(0,Q.jsx)(Nt,{isOpen:A,onClose:()=>ie(!1),spoolmanMode:e}),xn&&!jt&&(0,Q.jsxs)(`div`,{className:`grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3`,children:[(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Q.jsx)(fe,{className:`w-4 h-4 text-bambu-green`}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:i(`inventory.totalInventory`)})]}),(0,Q.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Tr(xn.totalWeight,!0)}),(0,Q.jsxs)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:[xn.totalSpools,` `,xn.totalSpools===1?i(`inventory.spool`):i(`inventory.spools`)]})]}),(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center justify-between gap-2 mb-1`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(je,{className:`w-4 h-4 text-blue-600 dark:text-blue-400`}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:i(`inventory.totalConsumed`)})]}),xn.totalConsumed>0&&pn.length>0&&(0,Q.jsx)(`button`,{onClick:()=>N({type:`reset-all-consumed-counters`}),className:`p-1 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 rounded transition-colors`,title:i(`inventory.resetAllConsumedCountersTooltip`),"aria-label":i(`inventory.resetAllConsumedCounters`),children:(0,Q.jsx)(w,{className:`w-3.5 h-3.5`})})]}),(0,Q.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Tr(xn.totalConsumed,!0)}),(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:i(`inventory.sinceTracking`)})]}),(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Q.jsx)(se,{className:`w-4 h-4 text-green-600 dark:text-green-400`}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:i(`inventory.byMaterial`)})]}),(0,Q.jsx)(`div`,{className:`flex flex-wrap gap-1.5 mt-1`,children:Nn.map(([e,t])=>(0,Q.jsxs)(`span`,{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${Er[e]||`bg-bambu-dark-tertiary text-bambu-gray`}`,children:[e,` `,(0,Q.jsx)(`span`,{className:`opacity-70`,children:Tr(t.weight,!0)})]},e))})]}),(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Q.jsx)(xe,{className:`w-4 h-4 text-purple-600 dark:text-purple-400`}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:i(`inventory.inPrinter`)})]}),(0,Q.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Sn}),(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:i(`inventory.loadedInAms`)})]}),(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Q.jsx)(H,{className:`w-4 h-4 text-yellow-600 dark:text-yellow-400`}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:i(`inventory.lowStock`)})]}),(0,Q.jsx)(`div`,{className:`text-xl font-bold ${xn.lowStock>0?`text-yellow-700 dark:text-yellow-400`:`text-white`}`,children:xn.lowStock}),(0,Q.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1 flex items-center gap-2`,children:gn?(0,Q.jsxs)(`form`,{onSubmit:e=>{e.preventDefault();let t=parseFloat(vn);!isNaN(t)&&t>=.1&&t<=99.9?bn.mutate(t):l(i(`inventory.lowStockThresholdError`),`error`)},className:`flex items-center gap-2`,children:[(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`<`}),(0,Q.jsx)(`input`,{type:`text`,inputMode:`decimal`,pattern:`^\\d{0,2}(\\.\\d?)?$`,maxLength:4,value:vn,onChange:e=>{let t=e.target.value.replace(/[^\d.]/g,``);/^\d{0,2}(\.\d?)?$/.test(t)&&yn(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:bn.isPending}),(0,Q.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`%`}),(0,Q.jsx)(W,{type:`submit`,size:`sm`,disabled:bn.isPending,children:i(`common.save`)}),(0,Q.jsx)(W,{type:`button`,size:`sm`,variant:`ghost`,onClick:()=>_n(!1),disabled:bn.isPending,children:i(`common.cancel`)})]}):(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsxs)(`span`,{className:`text-bambu-gray`,children:[`< `,hn,`%`]}),(0,Q.jsx)(`button`,{className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:i(`common.edit`),onClick:()=>{yn(hn.toString()),_n(!0)},children:(0,Q.jsx)(me,{className:`w-4 h-4`})})]})})]})]}),(0,Q.jsxs)(`div`,{className:`flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between`,children:[(0,Q.jsxs)(`div`,{className:`relative flex-1 max-w-md ${J===`forecast`?`invisible pointer-events-none`:``}`,children:[(0,Q.jsx)(Ee,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50`}),(0,Q.jsx)(`input`,{type:`text`,value:Ye,onChange:e=>{Xe(e.target.value),$()},placeholder:i(`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`}),Ye&&(0,Q.jsx)(`button`,{onClick:()=>{Xe(``),$()},className:`absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white`,children:(0,Q.jsx)(Le,{className:`w-4 h-4`})})]}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[J===`table`&&(0,Q.jsxs)(`button`,{onClick:()=>ot(!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:i(`inventory.configureColumns`),children:[(0,Q.jsx)(oe,{className:`w-4 h-4`}),(0,Q.jsx)(`span`,{className:`hidden sm:inline`,children:i(`inventory.columns`)})]}),J!==`forecast`&&(0,Q.jsxs)(`button`,{onClick:ir,className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium border rounded-lg transition-colors ${st?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,title:i(`inventory.groupSimilar`),children:[(0,Q.jsx)(Rt,{className:`w-4 h-4`}),(0,Q.jsx)(`span`,{className:`hidden sm:inline`,children:i(`inventory.groupSimilar`)})]}),(0,Q.jsxs)(`div`,{className:`flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:[(0,Q.jsxs)(`button`,{onClick:()=>Ze(`table`),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${J===`table`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Q.jsx)(Vt,{className:`w-4 h-4`}),(0,Q.jsx)(`span`,{className:`hidden sm:inline`,children:i(`inventory.table`)})]}),(0,Q.jsxs)(`button`,{onClick:()=>Ze(`cards`),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${J===`cards`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Q.jsx)(ce,{className:`w-4 h-4`}),(0,Q.jsx)(`span`,{className:`hidden sm:inline`,children:i(`inventory.cards`)})]}),(0,Q.jsxs)(`button`,{onClick:()=>S&&Ze(`forecast`),disabled:!S,title:S?void 0:i(`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 ${J===`forecast`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[S?(0,Q.jsx)(h,{className:`w-4 h-4`}):(0,Q.jsx)(ue,{className:`w-4 h-4`}),(0,Q.jsx)(`span`,{className:`hidden sm:inline`,children:i(`forecast.title`)})]})]})]})]}),(0,Q.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2 ${J===`forecast`?`hidden`:``}`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Q.jsxs)(`button`,{onClick:()=>{Oe(`active`),$()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${De===`active`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Q.jsx)(fe,{className:`w-3.5 h-3.5`}),i(`inventory.active`)]}),(0,Q.jsxs)(`button`,{onClick:()=>{Oe(`archived`),$()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${De===`archived`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Q.jsx)(v,{className:`w-3.5 h-3.5`}),i(`inventory.archived`)]})]}),(0,Q.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Q.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Q.jsx)(`button`,{onClick:()=>{Ae(`all`),$()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${ke===`all`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:i(`inventory.all`)}),(0,Q.jsxs)(`button`,{onClick:()=>{Ae(`used`),$()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${ke===`used`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Q.jsx)(ae,{className:`w-3.5 h-3.5`}),i(`inventory.used`)]}),(0,Q.jsx)(`button`,{onClick:()=>{Ae(`new`),$()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${ke===`new`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:i(`inventory.new`)}),(0,Q.jsxs)(`button`,{onClick:()=>{Ae(`lowstock`),$()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${ke===`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,Q.jsx)(H,{className:`w-3.5 h-3.5`}),i(`inventory.lowStock`)]})]}),(0,Q.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Q.jsx)(`button`,{onClick:()=>{We(`all`),$()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${K===`all`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:i(`inventory.all`)}),(0,Q.jsx)(`button`,{onClick:()=>{We(`stock`),$()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${K===`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:i(`inventory.stock`)}),(0,Q.jsx)(`button`,{onClick:()=>{We(`configured`),$()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${K===`configured`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:i(`inventory.configured`)})]}),(0,Q.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Q.jsxs)(`select`,{value:B,onChange:e=>{Me(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,Q.jsx)(`option`,{value:``,children:i(`inventory.material`)}),In.map(e=>(0,Q.jsx)(`option`,{value:e,children:e},e))]}),(0,Q.jsxs)(`select`,{value:Ne,onChange:e=>{Pe(e.target.value),$()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${Ne?`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,Q.jsx)(`option`,{value:``,children:i(`inventory.brand`)}),Ln.map(e=>(0,Q.jsx)(`option`,{value:e,children:e},e))]}),(Rn.length>0||V)&&(0,Q.jsxs)(`select`,{value:V,onChange:e=>{Fe(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,Q.jsx)(`option`,{value:``,children:i(`inventory.category`)}),Rn.map(e=>(0,Q.jsx)(`option`,{value:e,children:e},e)),zn&&(0,Q.jsx)(`option`,{value:`__none__`,children:i(`inventory.categoryNone`)})]}),(Bn.length>0||U)&&(0,Q.jsxs)(`select`,{value:U,onChange:e=>{Re(e.target.value),$()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${U?`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,Q.jsx)(`option`,{value:``,children:i(`inventory.materialNumber`)}),Bn.map(e=>(0,Q.jsx)(`option`,{value:e,children:e},e)),Vn&&(0,Q.jsx)(`option`,{value:br,children:i(`inventory.materialNumberNone`)})]}),(Hn.length>0||G)&&(0,Q.jsxs)(`select`,{value:G,onChange:e=>{Ve(e.target.value),$()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${G?`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,Q.jsx)(`option`,{value:``,children:i(`inventory.suppliers.label`)}),Hn.map(e=>(0,Q.jsx)(`option`,{value:String(e.id),children:e.name},e.id)),Un&&(0,Q.jsx)(`option`,{value:`__none__`,children:i(`inventory.suppliers.none`)})]}),Wn.length>0&&(0,Q.jsxs)(`select`,{value:He,onChange:e=>{Ue(e.target.value),$()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${He?`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,Q.jsx)(`option`,{value:``,children:i(`inventory.spoolName`)}),Wn.map(e=>(0,Q.jsx)(`option`,{value:e,children:Tn[e]?.name||`#${e}`},e))]}),(It.length>0||zt)&&(0,Q.jsxs)(`select`,{value:zt,onChange:e=>{Fn(e.target.value)},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${zt?`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,Q.jsx)(`option`,{value:``,children:i(`inventory.storageLocation`)}),It.map(e=>(0,Q.jsx)(`option`,{value:String(e.id),children:e.name},e.id)),Gn&&(0,Q.jsx)(`option`,{value:`__none__`,children:i(`inventory.storageLocationNone`)})]}),Kn&&(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Q.jsxs)(`button`,{onClick:()=>{Oe(`active`),Ae(`all`),Me(``),Pe(``),Fe(``),Re(``),Ve(``),Ue(``),We(`all`),Xe(``),T(e=>(e.delete(`location_id`),e),{replace:!0}),$()},className:`flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors`,children:[(0,Q.jsx)(Le,{className:`w-3.5 h-3.5`}),i(`inventory.clearFilters`)]})]}),J!==`forecast`&&(0,Q.jsxs)(`span`,{className:`ml-auto text-xs text-bambu-gray`,children:[Xn.length,` `,Xn.length===1?i(`inventory.spool`):i(`inventory.spools`),st&&$n<Xn.length&&` (${$n} ${i(`inventory.groupedRows`)})`]})]}),Y.size>0&&J!==`forecast`&&(0,Q.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,Q.jsx)(`span`,{className:`text-sm text-bambu-green font-medium`,children:i(`inventory.bulk.selectionCount`,{count:Y.size})}),(0,Q.jsxs)(`div`,{className:`ml-auto flex flex-wrap items-center gap-2`,children:[(0,Q.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>pt(!0),children:[(0,Q.jsx)(me,{className:`w-3.5 h-3.5 mr-1.5`}),i(`inventory.bulk.edit`)]}),(0,Q.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>I([...Y]),children:[(0,Q.jsx)(xe,{className:`w-3.5 h-3.5 mr-1.5`}),i(`inventory.bulk.printLabels`)]}),(0,Q.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>ht(`reset-consumed-counter`),children:[(0,Q.jsx)(w,{className:`w-3.5 h-3.5 mr-1.5`}),i(`inventory.bulk.resetUsage`)]}),De===`archived`?(0,Q.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>ht(`restore`),children:[(0,Q.jsx)(we,{className:`w-3.5 h-3.5 mr-1.5`}),i(`inventory.bulk.restore`)]}):(0,Q.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>ht(`archive`),children:[(0,Q.jsx)(v,{className:`w-3.5 h-3.5 mr-1.5`}),i(`inventory.bulk.archive`)]}),(0,Q.jsxs)(W,{size:`sm`,variant:`danger`,onClick:()=>ht(`delete`),children:[(0,Q.jsx)(z,{className:`w-3.5 h-3.5 mr-1.5`}),i(`inventory.bulk.delete`)]}),(0,Q.jsx)(`button`,{className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,onClick:_t,title:i(`inventory.bulk.clearSelection`),"aria-label":i(`inventory.bulk.clearSelection`),children:(0,Q.jsx)(Le,{className:`w-4 h-4`})})]})]}),jt?(0,Q.jsx)(`div`,{className:`flex justify-center py-16`,children:(0,Q.jsx)(L,{className:`w-8 h-8 text-bambu-green animate-spin`})}):J===`forecast`?(0,Q.jsx)(nn,{spools:X||[],spoolmanMode:e}):J===`cards`?rr.length>0?(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4`,children:rr.map(e=>{if(e.type===`group`){let{key:t,spools:n,representative:r}=e,a=n.reduce((e,t)=>e+Math.max(0,t.label_weight-t.weight_used),0),o=Je({rgba:r.rgba,extraColors:r.extra_colors,effectType:r.effect_type,subtype:r.subtype,effectSize:`groupheader`}),s=lt.has(t);return(0,Q.jsxs)(`div`,{className:`col-span-full`,children:[(0,Q.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:()=>ar(t),children:[(0,Q.jsx)(`div`,{className:`h-10 flex items-center px-4 gap-3`,style:o,children:(0,Q.jsx)(`span`,{className:`bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium`,children:q(r.color_name,r.rgba,r.color_name_is_synthesized)||`-`})}),(0,Q.jsxs)(`div`,{className:`px-4 py-3 flex items-center justify-between`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Q.jsx)(D,{className:`w-4 h-4 text-bambu-gray transition-transform ${s?``:`-rotate-90`}`}),(0,Q.jsxs)(`div`,{children:[(0,Q.jsxs)(`h3`,{className:`font-semibold text-white`,children:[r.material,r.subtype?` ${r.subtype}`:``]}),(0,Q.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:r.brand||`-`})]})]}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`span`,{className:`text-sm text-bambu-gray`,title:i(`inventory.remaining`),children:Tr(a)}),(0,Q.jsx)(`span`,{className:`text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full`,children:i(`inventory.groupedSpools`,{count:n.length})})]})]})]}),s&&(0,Q.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:n.map(e=>{let t=Math.max(0,e.label_weight-e.weight_used);return(0,Q.jsx)(Rr,{spool:e,remaining:t,pct:e.label_weight>0?t/e.label_weight*100:0,onClick:()=>O({spool:e,mode:`edit`}),onPrintLabel:()=>I([e.id]),onCopy:()=>O({spool:e,mode:`copy`}),t:i,colorizeLocationSensors:Tt,locationSensorAboveColor:Et,locationSensorBelowColor:Dt,locationSensorOptimalColor:Ot,dateFormat:Ct},e.id)})})]},`group-${t}`)}let t=e.spool,n=Math.max(0,t.label_weight-t.weight_used);return(0,Q.jsx)(Rr,{spool:t,remaining:n,pct:t.label_weight>0?n/t.label_weight*100:0,onClick:()=>O({spool:t,mode:`edit`}),onPrintLabel:()=>I([t.id]),onCopy:()=>O({spool:t,mode:`copy`}),t:i,colorizeLocationSensors:Tt,locationSensorAboveColor:Et,locationSensorBelowColor:Dt,locationSensorOptimalColor:Ot,dateFormat:Ct},t.id)})}),(0,Q.jsx)(Lr,{pageIndex:nr,pageSize:bt,totalRows:$n,totalPages:tr,onPageChange:yt,onPageSizeChange:or,t:i})]}):(0,Q.jsx)(Kr,{hasFilters:Kn,onAddSpool:()=>O({spool:null,mode:`create`}),t:i}):rr.length>0?(0,Q.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Q.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Q.jsxs)(`table`,{className:`w-full`,children:[(0,Q.jsx)(`thead`,{children:(0,Q.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Q.jsx)(`th`,{className:`w-10 px-3 py-3`,children:(0,Q.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":i(`inventory.bulk.selectAllVisible`),checked:rr.length>0&&rr.every(e=>(e.type===`group`?e.spools.map(e=>e.id):[e.spool.id]).every(e=>Y.has(e))),onChange:e=>{if(e.target.checked){let e=new Set(Y);for(let t of rr){let n=t.type===`group`?t.spools.map(e=>e.id):[t.spool.id];for(let t of n)e.add(t)}dt(e)}else{let e=new Set(Y);for(let t of rr){let n=t.type===`group`?t.spools.map(e=>e.id):[t.spool.id];for(let t of n)e.delete(t)}dt(e)}}})}),Jn.map(e=>{let t=!!jr[e],n=et?.column===e;return(0,Q.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`:``} ${n?`text-bambu-green`:`text-bambu-gray`}`,onClick:t?()=>Yn(e):void 0,children:(0,Q.jsxs)(`span`,{className:`inline-flex items-center gap-1`,children:[kr[e]?.(i)??e,t&&(n?et.direction===`asc`?(0,Q.jsx)(x,{className:`w-3 h-3`}):(0,Q.jsx)(y,{className:`w-3 h-3`}):(0,Q.jsx)(b,{className:`w-3 h-3 opacity-30`}))]})},e)}),(0,Q.jsx)(`th`,{className:`text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:i(`common.actions`)})]})}),(0,Q.jsx)(`tbody`,{children:rr.map(e=>{if(e.type===`group`){let{key:t,spools:n}=e,r=lt.has(t),a=gr(n),o=Math.max(0,a.label_weight-a.weight_used);return(0,Q.jsx)(Gr,{spools:n,headerSpool:a,remaining:o,pct:a.label_weight>0?o/a.label_weight*100:0,isExpanded:r,onToggle:()=>ar(t),selectedIds:Y,onToggleSelected:gt,onToggleGroupSelected:(e,t)=>{dt(n=>{let r=new Set(n);for(let n of e)t?r.add(n):r.delete(n);return r})},onEdit:e=>O({spool:e,mode:`edit`}),onCopy:e=>O({spool:e,mode:`copy`}),onArchive:e=>N({type:`archive`,spoolId:e}),onRestore:e=>an.mutate(e),onDelete:e=>N({type:`delete`,spoolId:e}),onPrintLabel:e=>I([e]),onResetConsumedCounter:e=>N({type:`reset-consumed-counter`,spoolId:e}),visibleColumns:Jn,assignmentMap:wn,catalogMap:Tn,locationReadingsMap:Mn,currencySymbol:Cn,dateFormat:Ct,t:i,onSyncWeight:mn,colorizeLocationSensors:Tt,locationSensorAboveColor:Et,locationSensorBelowColor:Dt,locationSensorOptimalColor:Ot},`group-${t}`)}let t=e.spool,n=Math.max(0,t.label_weight-t.weight_used);return(0,Q.jsx)(Wr,{spool:t,remaining:n,pct:t.label_weight>0?n/t.label_weight*100:0,isSelected:Y.has(t.id),onToggleSelected:()=>gt(t.id),onEdit:()=>O({spool:t,mode:`edit`}),onCopy:()=>O({spool:t,mode:`copy`}),onRestore:()=>an.mutate(t.id),onArchive:()=>N({type:`archive`,spoolId:t.id}),onDelete:()=>N({type:`delete`,spoolId:t.id}),onPrintLabel:()=>I([t.id]),onResetConsumedCounter:()=>N({type:`reset-consumed-counter`,spoolId:t.id}),visibleColumns:Jn,assignmentMap:wn,catalogMap:Tn,locationReadingsMap:Mn,currencySymbol:Cn,dateFormat:Ct,t:i,onSyncWeight:mn,colorizeLocationSensors:Tt,locationSensorAboveColor:Et,locationSensorBelowColor:Dt,locationSensorOptimalColor:Ot},t.id)})})]})}),(0,Q.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,Q.jsx)(`span`,{className:`text-bambu-gray`,children:Qn?`${$n} ${i($n===1?`inventory.spool`:`inventory.spools`)}`:(0,Q.jsxs)(Q.Fragment,{children:[i(`inventory.showing`),` `,nr*er+1,` `,i(`inventory.to`),` `,Math.min((nr+1)*er,$n),` `,i(`inventory.of`),` `,$n,` `,i(`inventory.spools`)]})}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`span`,{className:`text-bambu-gray`,children:i(`inventory.show`)}),(0,Q.jsxs)(`select`,{value:bt,onChange:e=>or(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,Q.jsx)(`option`,{value:e,children:e},e)),(0,Q.jsx)(`option`,{value:-1,children:i(`inventory.all`)})]}),!Qn&&(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(`button`,{onClick:()=>yt(0),disabled:nr===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,Q.jsx)(j,{className:`w-4 h-4`})}),(0,Q.jsx)(`button`,{onClick:()=>yt(e=>Math.max(0,e-1)),disabled:nr===0,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Q.jsx)(te,{className:`w-4 h-4`})}),(0,Q.jsxs)(`span`,{className:`text-bambu-gray px-2 whitespace-nowrap`,children:[i(`inventory.page`),` `,nr+1,` `,i(`inventory.of`),` `,tr]}),(0,Q.jsx)(`button`,{onClick:()=>yt(e=>Math.min(tr-1,e+1)),disabled:nr>=tr-1,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Q.jsx)(ee,{className:`w-4 h-4`})}),(0,Q.jsx)(`button`,{onClick:()=>yt(tr-1),disabled:nr>=tr-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,Q.jsx)(ne,{className:`w-4 h-4`})})]})]})]})]}):(0,Q.jsx)(Kr,{hasFilters:Kn,onAddSpool:()=>O({spool:null,mode:`create`}),t:i}),E!==null&&(0,Q.jsx)(Dn,{isOpen:!0,onClose:()=>O(null),spool:E.spool,mode:E.mode,currencySymbol:Cn,spoolmanMode:e,spoolsQueryKey:kt}),M&&(0,Q.jsx)(Be,{title:M.type===`delete`?i(`common.delete`):M.type===`archive`?i(`inventory.archive`):M.type===`reset-consumed-counter`?i(`inventory.resetConsumedCounter`):i(`inventory.resetAllConsumedCounters`),message:M.type===`delete`?i(`inventory.deleteConfirm`):M.type===`archive`?i(`inventory.archiveConfirm`):M.type===`reset-consumed-counter`?i(`inventory.resetConsumedCounterConfirm`):i(`inventory.resetAllConsumedCountersConfirm`,{count:pn.length}),confirmText:M.type===`delete`?i(`common.delete`):M.type===`archive`?i(`inventory.archive`):i(`inventory.resetConsumedCounter`),variant:M.type===`archive`?`warning`:`danger`,onConfirm:()=>{M.type===`delete`?tn.mutate(M.spoolId):M.type===`archive`?rn.mutate(M.spoolId):M.type===`reset-consumed-counter`?on.mutate(M.spoolId):cn.mutate(pn),N(null)},onCancel:()=>N(null)}),(0,Q.jsx)(nt,{isOpen:at,onClose:()=>ot(!1),columns:rt,defaultColumns:Sr,onSave:qn}),(0,Q.jsx)(cr,{isOpen:F!==null,onClose:()=>I(null),availableSpools:Pn,initialSelectedIds:F??[],spoolmanMode:e}),(0,Q.jsx)(hr,{isOpen:ft,selectedCount:Y.size,isPending:ln.isPending,availableLocations:It.map(e=>({id:e.id,name:e.name})),availableMaterials:_r((X??[]).map(e=>e.material)),availableSubtypes:_r((X??[]).map(e=>e.subtype)),availableBrands:_r((X??[]).map(e=>e.brand)),availableCategories:_r((X??[]).map(e=>e.category)),availableMaterialNumbers:_r((X??[]).map(e=>e.material_number)),availableSlicerFilaments:_r((X??[]).map(e=>e.slicer_filament)),availableSlicerFilamentNames:_r((X??[]).map(e=>e.slicer_filament_name)),spoolmanMode:e,onClose:()=>pt(!1),onApply:e=>ln.mutate({ids:[...Y],update:e})}),mt&&(0,Q.jsx)(Be,{title:i(mt===`delete`?`inventory.bulk.deleteTitle`:mt===`archive`?`inventory.bulk.archiveTitle`:mt===`restore`?`inventory.bulk.restoreTitle`:`inventory.bulk.resetUsageTitle`),message:i(mt===`delete`?`inventory.bulk.deleteMessage`:mt===`archive`?`inventory.bulk.archiveMessage`:mt===`restore`?`inventory.bulk.restoreMessage`:`inventory.bulk.resetUsageMessage`,{count:Y.size}),confirmText:i(mt===`delete`?`common.delete`:mt===`archive`?`inventory.archive`:mt===`restore`?`inventory.restore`:`inventory.resetConsumedCounter`),variant:mt===`delete`?`danger`:`warning`,isLoading:un.isPending||dn.isPending||fn.isPending||cn.isPending,onConfirm:()=>{let e=[...Y];mt===`delete`?un.mutate(e):mt===`archive`?dn.mutate(e):mt===`restore`?fn.mutate(e):cn.mutate(e)},onCancel:()=>ht(null)}),le&&(0,Q.jsx)(ur,{onClose:()=>pe(!1),onImported:e=>{pe(!1),o.invalidateQueries({queryKey:kt}),l(i(`inventory.csv.importSuccess`,`{{count}} spools imported`,{count:e}),`success`)}}),(0,Q.jsx)(ge,{open:ye,onClose:()=>Ce(!1),onPickLocation:e=>Fn(String(e))}),(0,Q.jsx)(dr,{open:Te,onClose:()=>R(!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,Q.jsxs)(`div`,{className:`flex items-center justify-between pt-2 text-sm`,children:[(0,Q.jsx)(`span`,{className:`text-bambu-gray`,children:s?`${n} ${o(n===1?`inventory.spool`:`inventory.spools`)}`:(0,Q.jsxs)(Q.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,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`span`,{className:`text-bambu-gray`,children:o(`inventory.show`)}),(0,Q.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,Q.jsx)(`option`,{value:e,children:e},e)),(0,Q.jsx)(`option`,{value:-1,children:o(`inventory.all`)})]}),!s&&(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.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,Q.jsx)(j,{className:`w-4 h-4`})}),(0,Q.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,Q.jsx)(te,{className:`w-4 h-4`})}),(0,Q.jsxs)(`span`,{className:`text-bambu-gray px-2 whitespace-nowrap`,children:[o(`inventory.page`),` `,e+1,` `,o(`inventory.of`),` `,r]}),(0,Q.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,Q.jsx)(ee,{className:`w-4 h-4`})}),(0,Q.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,Q.jsx)(ne,{className:`w-4 h-4`})})]})]})]})}function Rr({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=Je({rgba:e.rgba,extraColors:e.extra_colors,effectType:e.effect_type,subtype:e.subtype,effectSize:`card`});return(0,Q.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,Q.jsxs)(`div`,{className:`h-14 flex items-center justify-center`,style:f,children:[(0,Q.jsx)(`span`,{className:`bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium`,children:q(e.color_name,e.rgba,e.color_name_is_synthesized)||`-`}),a&&(0,Q.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,Q.jsx)(N,{className:`w-3.5 h-3.5`})})]}),(0,Q.jsxs)(`div`,{className:`p-4 space-y-3`,children:[(0,Q.jsxs)(`div`,{className:`flex items-start justify-between gap-2`,children:[(0,Q.jsxs)(`div`,{children:[(0,Q.jsxs)(`h3`,{className:`font-semibold text-white`,children:[e.material,e.subtype?` ${e.subtype}`:``]}),(0,Q.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:e.brand||`-`})]}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-1`,children:[i&&(0,Q.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,Q.jsx)(xe,{className:`w-4 h-4`})}),(0,Q.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded`,children:[`#`,e.id]})]})]}),(0,Q.jsxs)(`div`,{children:[(0,Q.jsxs)(`div`,{className:`flex justify-between text-xs text-bambu-gray mb-1`,children:[(0,Q.jsx)(`span`,{children:o(`inventory.remaining`)}),(0,Q.jsxs)(`span`,{children:[Math.round(n),`%`]})]}),(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Q.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,Q.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(t),`g`]})]})]}),(0,Q.jsxs)(`div`,{className:`grid grid-cols-2 gap-2 text-xs`,children:[e.last_dried_at&&(0,Q.jsxs)(`div`,{className:`col-span-2`,children:[(0,Q.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.lastDried`),`: `]}),(0,Q.jsx)(`span`,{className:`text-bambu-gray`,children:[Or(e.last_dried_at,d),Dr(e)].filter(Boolean).join(` · `)})]}),(0,Q.jsxs)(`div`,{children:[(0,Q.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.labelWeight`),`: `]}),(0,Q.jsx)(`span`,{className:`text-bambu-gray`,children:Tr(e.label_weight)})]}),(0,Q.jsxs)(`div`,{children:[(0,Q.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.weightUsed`),`: `]}),(0,Q.jsx)(`span`,{className:`text-bambu-gray`,children:e.weight_used>0?Tr(e.weight_used):`-`})]})]}),e.location_id&&(0,Q.jsx)(Ur,{locationId:e.location_id,locationName:e.storage_location??null,isLast:!e.note,colorize:s,aboveColor:c,belowColor:l,optimalColor:u}),e.note&&(0,Q.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 zr={temperature:0,humidity:1,battery:2};function Br(e){return e===`humidity`?`mr-[2px]`:e===`battery`?`mr-[3px]`:``}function Vr(e,t){return g(e,t,{decimals:2})}function Hr(e,t,n,r,i){return t&&he(ve(e),n,r,i)||`text-bambu-gray`}function Ur({locationId:e,locationName:t,isLast:i,colorize:a,aboveColor:o,belowColor:s,optimalColor:c}){let{t:l}=n(),{data:u}=qe(),d=(u?.location_sensor_poll_interval||120)*1e3,{data:p}=f({queryKey:[`locationHaSensors`],queryFn:()=>r.getLocationHASensors()}),m=(p??[]).some(t=>t.location_id===e),{data:h}=f({queryKey:[`locationHaSensorReadings`,e],queryFn:()=>r.getLocationHASensorReadings(e,!1),refetchInterval:d,enabled:m}),g=h?.filter(e=>e.show_on_card);if(!g?.length)return null;let v=g.find(e=>e.device_class===`battery`),y=g.filter(e=>e.device_class!==`battery`).sort((e,t)=>(zr[e.device_class??``]??99)-(zr[t.device_class??``]??99));return(0,Q.jsxs)(`div`,{className:`flex items-center gap-2 pt-3 border-t border-bambu-dark-tertiary text-xs text-bambu-gray ${i?`-mb-1`:``}`,children:[t&&(0,Q.jsx)(`span`,{className:`truncate`,children:t}),(0,Q.jsx)(`span`,{className:`text-bambu-gray/40`,children:`|`}),(0,Q.jsx)(`div`,{className:`flex items-center gap-3`,children:y.map((e,t)=>{let n=_(e),r=t===0?`ml-[-3.6px]`:``;return(0,Q.jsxs)(`span`,{title:e.name,className:`flex items-center gap-[3px]`,children:[(0,Q.jsx)(n,{className:`w-3 h-3 ${Br(e.device_class)} ${r}`}),(0,Q.jsx)(`span`,{className:Hr(e,a,o,s,c),children:Vr(e,l)})]},e.id)})}),v&&(()=>{let e=_(v);return(0,Q.jsxs)(`span`,{title:v.name,className:`flex items-center gap-[3px] ml-auto`,children:[(0,Q.jsx)(e,{className:`w-3 h-3 ${Br(v.device_class)}`}),(0,Q.jsx)(`span`,{className:Hr(v,a,o,s,c),children:Vr(v,l)})]},v.id)})()]})}function Wr({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:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:T}){return(0,Q.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,Q.jsx)(`td`,{className:`w-10 px-3 py-3`,onClick:e=>e.stopPropagation(),children:i&&(0,Q.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":y(`inventory.bulk.selectRow`),checked:!!r,onChange:i})}),f.map(r=>(0,Q.jsx)(`td`,{className:`py-3 px-4`,children:Ar[r]?.({spool:e,remaining:t,pct:n,assignmentMap:p,catalogMap:m,locationReadingsMap:h,currencySymbol:g,dateFormat:_,t:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:T})},r)),(0,Q.jsx)(`td`,{className:`py-3 px-4`,children:(0,Q.jsxs)(`div`,{className:`flex items-center justify-end gap-1`,onClick:e=>e.stopPropagation(),children:[(0,Q.jsx)(`button`,{onClick:a,className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:y(`common.edit`),children:(0,Q.jsx)(me,{className:`w-4 h-4`})}),o&&(0,Q.jsx)(`button`,{onClick:o,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:y(`inventory.copySpool`),children:(0,Q.jsx)(N,{className:`w-4 h-4`})}),u&&(0,Q.jsx)(`button`,{onClick:u,className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:y(`inventory.labels.printOne`),children:(0,Q.jsx)(xe,{className:`w-4 h-4`})}),d&&e.weight_used>0&&(0,Q.jsx)(`button`,{onClick:d,className:`p-1.5 text-bambu-gray hover:text-orange-600 dark:hover:text-orange-400 rounded transition-colors`,title:y(`inventory.resetConsumedCounterTooltip`),children:(0,Q.jsx)(w,{className:`w-4 h-4`})}),e.archived_at?(0,Q.jsx)(`button`,{onClick:s,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:y(`inventory.restore`),children:(0,Q.jsx)(we,{className:`w-4 h-4`})}):(0,Q.jsx)(`button`,{onClick:c,className:`p-1.5 text-bambu-gray hover:text-yellow-600 dark:hover:text-yellow-400 rounded transition-colors`,title:y(`inventory.archive`),children:(0,Q.jsx)(v,{className:`w-4 h-4`})}),(0,Q.jsx)(`button`,{onClick:l,className:`p-1.5 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 rounded transition-colors`,title:y(`common.delete`),children:(0,Q.jsx)(z,{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:T,onToggleSelected:E,onToggleGroupSelected:O}){let k=!!T&&e.every(e=>T.has(e.id));return(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.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,Q.jsx)(`td`,{className:`w-10 px-3 py-3`,onClick:e=>e.stopPropagation(),children:O&&(0,Q.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,Q.jsx)(`td`,{className:`py-3 px-4`,children:o===0?(0,Q.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Q.jsx)(D,{className:`w-4 h-4 text-bambu-gray transition-transform ${i?``:`-rotate-90`}`}),Ar[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,Q.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})}):Ar[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,Q.jsx)(`td`,{className:`py-3 px-4`,children:(0,Q.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,Q.jsx)(Wr,{spool:e,remaining:t,pct:e.label_weight>0?t/e.label_weight*100:0,isSelected:T?.has(e.id),onToggleSelected:E?()=>E(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 Kr({hasFilters:e,onAddSpool:t,t:n}){return(0,Q.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 px-4`,children:[(0,Q.jsxs)(`div`,{className:`relative mb-6`,children:[(0,Q.jsx)(`div`,{className:`absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl`}),(0,Q.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,Q.jsx)(`div`,{className:`absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30`}),(0,Q.jsx)(`div`,{className:`absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20`}),e?(0,Q.jsx)(Ee,{className:`w-10 h-10 text-bambu-gray/40`,strokeWidth:1.5}):(0,Q.jsxs)(`div`,{className:`relative`,children:[(0,Q.jsx)(`div`,{className:`w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center`,children:(0,Q.jsx)(`div`,{className:`w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20`})}),(0,Q.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,Q.jsx)(`span`,{className:`text-white text-lg font-bold leading-none`,children:`+`})})]})]})]}),(0,Q.jsx)(`h3`,{className:`text-lg font-semibold text-white mb-2 text-center`,children:e?n(`inventory.noSpoolsMatch`):n(`inventory.noSpools`).split(`.`)[0]}),(0,Q.jsx)(`p`,{className:`text-sm text-bambu-gray text-center max-w-sm mb-6`,children:n(e?`inventory.noSpoolsMatchDesc`:`inventory.noSpools`)}),!e&&(0,Q.jsxs)(W,{onClick:t,children:[(0,Q.jsx)(fe,{className:`w-4 h-4`}),n(`inventory.addSpool`)]})]})}export{Fr as default};
|