| 123 |
- import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{A as t,E as n,T as r,i,n as a,o,t as s,w as c,y as l}from"./client-CI_mRuxq.js";import{f as u}from"./chunk-62JRHF6Z-Br72nRi-.js";import{n as d}from"./QueryClientProvider-DdZM8x2F.js";import{t as f}from"./useQuery-Bwf7GX7W.js";import{t as p}from"./useMutation-BtjUf9pa.js";import{t as m}from"./useQueries-DEDit9Zk.js";import{i as h,n as g,r as _}from"./haSensorDisplay-Bm2Mwh0e.js";import{t as v}from"./archive-B-wtL-q0.js";import{t as y}from"./arrow-down-BtddaT9J.js";import{t as b}from"./arrow-up-down-Ceii4T7h.js";import{t as x}from"./arrow-up-ByitPx4A.js";import{i as S,n as C,r as w,t as T}from"./ReferenceLine-DORUTbT2.js";import{t as E}from"./check--Gl3Qz6Q.js";import{a as D,i as O,n as k,t as A}from"./info-BNmQbMxB.js";import{t as ee}from"./chevron-right-fOlAyNkM.js";import{t as te}from"./chevron-left-CFzDQ0Bm.js";import{n as j,t as ne}from"./chevrons-right-DbHAraQI.js";import{n as re,r as ie}from"./ToastContext-HhLLmSZv.js";import{t as M}from"./circle-minus-CPviT500.js";import{t as ae}from"./clock-uIAVFPqu.js";import{t as oe}from"./columns-2-B7eVie_3.js";import{t as N}from"./copy-Dd-N9KqF.js";import{t as P}from"./download-DOWEx8PL.js";import{t as F}from"./external-link-B3nahbe-.js";import{t as I}from"./file-text-D5G1HhEj.js";import{t as se}from"./layers-CK_hXGQk.js";import{t as ce}from"./layout-grid-0zWpgVyt.js";import{t as le}from"./link-2-B1ujuCyd.js";import{t as L}from"./loader-circle-DUxlVQ2M.js";import{t as ue}from"./lock-Cml94_EY.js";import{t as de}from"./map-pin-CbQ6h_bK.js";import{t as fe}from"./package-Rb_dxU1-.js";import{t as pe}from"./palette-DIe-DyAe.js";import{_ as me,c as he,g as ge,h as _e,s as ve}from"./locationSensorDefaults-BeHYmC19.js";import{t as ye}from"./pencil-C-9pohG8.js";import{t as be}from"./plus-D4XYacto.js";import{t as xe}from"./printer-NKWUWzJ6.js";import{t as Se}from"./qr-code-jYrkj2ze.js";import{t as Ce}from"./refresh-cw-Qjc-4VoM.js";import{t as we}from"./rotate-ccw-V-BYw3Z8.js";import{t as Te}from"./save-Bfmb5K38.js";import{t as Ee}from"./search-O3kvZEQc.js";import{t as R}from"./shopping-cart-DNsIjD_k.js";import{t as De}from"./sparkles-CO1li--h.js";import{t as Oe}from"./square-check-big-DtFQ826Q.js";import{t as ke}from"./square-CgA9232_.js";import{t as Ae}from"./tag-bO2A0-in.js";import{t as z}from"./trash-2-Bl7fCef0.js";import{L as B,Rn as je,Xt as Me,c as Ne,p as Pe,s as V}from"./CartesianChart-CkIcV5cK.js";import{t as H}from"./triangle-alert-CEoZASIL.js";import{t as Fe}from"./unlink-Dl_oGx5b.js";import{t as Ie}from"./upload-DnWZ532z.js";import{t as U}from"./wand-sparkles-BLfD5cOW.js";import{t as Le}from"./x-BXE5NeAR.js";import{t as Re}from"./zap-ClNkWAwz.js";import{n as ze}from"./AuthContext-kGqbKbCL.js";import{t as W}from"./Button-BWGu1oiu.js";import{t as Be}from"./ConfirmModal-hUTo4339.js";import{r as G,v as Ve,y as He}from"./date-G9KUauqz.js";import{g as Ue}from"./amsHelpers-m0tbln8b.js";import{a as K,c as We,t as Ge}from"./colors-D_-ITfUn.js";import{t as Ke}from"./useColorCatalogVersion-p84g0m2K.js";import{n as qe}from"./currency-LmqFrzn3.js";import{n as Je}from"./useUiSettings-BpMPI5a2.js";import{n as Ye}from"./filamentSwatchHelpers-C3r5m07g.js";import{t as Xe}from"./FilamentSwatch-AN4JnOMA.js";import{a as Ze,c as Qe,l as q,o as $e,s as et}from"./index-ByEtDKgd.js";import{i as tt,r as nt}from"./slicerPrinterMatch-CUdoNag2.js";import{t as rt}from"./ColumnConfigModal-D71tLKQM.js";import{n as it,t as at}from"./AreaChart-DkPCKOxM.js";import{a as ot,i as st,n as ct,o as lt,r as ut,s as dt,t as J}from"./AdditionalSection-oqynIyS4.js";import{S as ft,a as pt,b as mt,c as ht,d as gt,f as _t,h as vt,i as yt,l as bt,n as xt,o as St,p as Ct,r as wt,s as Tt,t as Et,u as Dt,v as Ot,y as kt}from"./utils-CmAEpDlt.js";import{n as At,r as jt,t as Y}from"./nozzleFlow-DwF_jePd.js";import{t as Mt}from"./AssignToAmsModal-Bela8YWk.js";import{n as Nt}from"./inventorySearch-CPokNAPH.js";import{n as Pt,t as Ft}from"./spoolCost-CrjAznh-.js";var It=c(`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`}]]),Lt=c(`chart-no-axes-column`,[[`path`,{d:`M5 21v-6`,key:`1hz6c0`}],[`path`,{d:`M12 21V3`,key:`1lcnhd`}],[`path`,{d:`M19 21V9`,key:`unv183`}]]),Rt=c(`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`}]]),zt=c(`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`}]]),Bt=c(`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`}]]),Vt=c(`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`}]]),Ht=c(`table-properties`,[[`path`,{d:`M15 3v18`,key:`14nvp0`}],[`rect`,{width:`18`,height:`18`,x:`3`,y:`3`,rx:`2`,key:`afitv7`}],[`path`,{d:`M21 9H3`,key:`1338ky`}],[`path`,{d:`M21 15H3`,key:`9uk58r`}]]),X=e(t(),1),Ut=[`=`,`+`,`-`,`@`,`=`,`+`,`-`,`@`];function Wt(e){let t=String(e??``);if(typeof e==`string`&&t!==``){let e=Ut.some(e=>t.trimStart().startsWith(e));(` \r
- `.includes(t[0])||e)&&(t=`'${t}`)}return`"${t.replace(/"/g,`""`)}"`}function Gt(e){let t=e?Ve(e)?.getTime():void 0;return t===void 0||Number.isNaN(t)?null:t}function Kt(e,t){let n=Gt(e.created_at);return n===null?t:t.filter(e=>(Gt(e.created_at)??n)>=n)}var Z=r(),qt=90;function Jt(e,t){let n=Ve(e)?.getTime();return n!==void 0&&!Number.isNaN(n)&&t-n<=qt*864e5}var Yt=1.65,Xt=[`#1DB954`,`#3B82F6`,`#F59E0B`,`#EF4444`,`#8B5CF6`];function Zt(e,t,n,r){return`${e}||${t??``}||${n??``}||${r??``}`}function Qt(e,t,n,r){return[e,t,n,r&&n&&r.trim().toLowerCase()===n.trim().toLowerCase()?null:r].filter(Boolean).join(` `)}function $t(e,t){let n=new Date(e);return n.setDate(n.getDate()+Math.round(t)),n}function en(e){return e.toLocaleDateString(void 0,{month:`short`,day:`numeric`,year:`numeric`})}function tn(e){return e.toLocaleDateString(void 0,{month:`short`,day:`numeric`})}function nn(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 rn(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=Gt(n.last_used)??Gt(n.archived_at),o=a===null?r:Math.min(a,r),s=Gt(n.first_used)??t.get(n.id)??Gt(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-qt*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 an(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:tn($t(new Date,i)),stock:Math.round(t),rop:Math.round(e.reorderPointG)}),t===0)break}return r}function on({spools:e,spoolmanMode:t=!1}){let r=d(),{showToast:i}=re(),{t:a}=n(),{hasPermission:s,hasAnyPermission:c}=ze(),l=s(`inventory:forecast_read`),u=c(`inventory:forecast_write`,`inventory:update`),[p,m]=(0,X.useState)(!1),[h,g]=(0,X.useState)(`material`),[_,v]=(0,X.useState)(`asc`),[y,b]=(0,X.useState)(``),[x,S]=(0,X.useState)(``),[C,w]=(0,X.useState)(null),[T,E]=(0,X.useState)(!1),[k,A]=(0,X.useState)(!1),[ee,te]=(0,X.useState)(30),{data:j}=Je({enabled:l}),{data:ne=[]}=f({queryKey:[`sku-settings`],queryFn:o.getSkuSettings,staleTime:6e4,enabled:l}),{data:ie=[]}=f({queryKey:[`all-usage-history-forecast`,t],queryFn:()=>o.getAllUsageHistory(5e3,void 0,t),staleTime:6e4,enabled:l}),{data:M=[]}=f({queryKey:[`shopping-list`],queryFn:o.getShoppingList,staleTime:3e4,enabled:l}),ae=j?.forecast_global_lead_time_days??0,oe=(0,X.useMemo)(()=>{let e=new Map;for(let t of ne)e.set(Zt(t.material,t.subtype,t.brand,t.color_name),t);return e},[ne]),N=(0,X.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,X.useMemo)(()=>{let t=new Map;for(let n of e){if(n.archived_at)continue;let e=Zt(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(Zt(r.material,r.subtype,r.brand,r.color_name));e&&(Jt(r.archived_at,n)?e.usedUp.push(r):r.weight_used>0&&(e.usedBefore=!0))}return[...t.values()]},[e]),F=(0,X.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(Zt(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?Kt(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=nn(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=Gt(n.created_at);r!==null&&r<(e.get(t.id)??1/0)&&e.set(t.id,r)}let t=rn(d,e,r.usedBefore,n);t!==null&&(h=t,_=`delta`)}let y=Yt*(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:$t(e,x),C=h&&h>0?Math.floor((l-b)/h):null,w=C===null?null:$t(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,X.useMemo)(()=>[...new Set(P.map(e=>e.material))].sort(),[P]),se=(0,X.useMemo)(()=>[...new Set(P.map(e=>e.brand).filter(Boolean))].sort(),[P]),ce=(0,X.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,X.useMemo)(()=>F.filter(e=>!e.settings?.alerts_snoozed&&(e.stockBreakAlert||e.reorderAlert)),[F]),L=(0,X.useMemo)(()=>[...F].filter(e=>e.dailyRateG!==null).sort((e,t)=>t.totalUsedG-e.totalUsedG).slice(0,5),[F]);if(!l)return(0,Z.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 text-bambu-gray gap-3`,children:[(0,Z.jsx)(ue,{className:`w-8 h-8 opacity-40`}),(0,Z.jsx)(`p`,{className:`text-sm`,children:a(`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,Z.jsxs)(`div`,{className:`space-y-5`,children:[(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-3`,children:[le.length>0&&(0,Z.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,Z.jsx)(H,{className:`w-4 h-4`}),a(`forecast.alertCount`,{count:le.length}),p?(0,Z.jsx)(O,{className:`w-3.5 h-3.5`}):(0,Z.jsx)(D,{className:`w-3.5 h-3.5`})]}),u&&(0,Z.jsx)(dn,{value:ae,onSave:e=>{o.updateSettings({forecast_global_lead_time_days:e}).then(()=>{r.invalidateQueries({queryKey:[`settings`]}),i(a(`forecast.globalLeadTimeSaved`),`success`)})}}),(0,Z.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,Z.jsx)(`option`,{value:``,children:a(`inventory.material`)}),I.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(0,Z.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,Z.jsx)(`option`,{value:``,children:a(`inventory.brand`)}),se.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(0,Z.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,Z.jsx)(R,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:a(`forecast.shoppingList`)}),fe&&(0,Z.jsx)(`span`,{className:`absolute -top-1.5 -right-1.5 w-4 h-4 rounded-full bg-bambu-green text-white text-[10px] font-bold flex items-center justify-center`,children:fe})]})]}),p&&le.length>0&&(0,Z.jsx)(`div`,{className:`space-y-2`,children:le.map(e=>(0,Z.jsx)(cn,{forecast:e,onCart:()=>w(e)},e.group.key))}),T&&(0,Z.jsx)(gn,{items:M,forecasts:F,globalLeadTime:ae,canWrite:u,onClose:()=>E(!1),onRemove:e=>{o.removeFromShoppingList(e).then(()=>r.invalidateQueries({queryKey:[`shopping-list`]})).catch(()=>i(a(`forecast.failedSaveSettings`),`error`))},onClear:()=>A(!0)}),k&&(0,Z.jsx)(Be,{title:a(`forecast.clearAllConfirmTitle`),message:a(`forecast.clearAllConfirmMessage`,{count:M.length}),confirmText:a(`forecast.clearAll`),variant:`danger`,onConfirm:()=>{A(!1),o.clearShoppingList().then(()=>r.invalidateQueries({queryKey:[`shopping-list`]})).catch(()=>i(a(`forecast.failedSaveSettings`),`error`))},onCancel:()=>A(!1)}),L.length>0&&(0,Z.jsx)(un,{forecasts:L,days:ee,onDaysChange:te}),F.length===0?(0,Z.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 text-bambu-gray`,children:[(0,Z.jsx)(je,{className:`w-10 h-10 mb-3 opacity-40`}),(0,Z.jsx)(`p`,{className:`text-sm`,children:a(`forecast.noSpools`)})]}):(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsx)(`th`,{className:`w-8 px-4 py-3`}),(0,Z.jsx)(sn,{col:`material`,active:h,dir:_,onSort:de,children:a(`forecast.sku`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:a(`locations.spools`)}),(0,Z.jsx)(sn,{col:`stock`,active:h,dir:_,onSort:de,children:a(`forecast.stock`)}),(0,Z.jsx)(sn,{col:`used`,active:h,dir:_,onSort:de,children:a(`forecast.dailyRate`)}),(0,Z.jsx)(sn,{col:`days_left`,active:h,dir:_,onSort:de,children:a(`forecast.daysLeft`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:a(`forecast.emptyBy`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:a(`forecast.reorderBy`)}),(0,Z.jsx)(`th`,{className:`w-24 px-4 py-3`})]})}),(0,Z.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:ce.map(e=>(0,Z.jsx)(fn,{forecast:e,globalLeadTime:ae,canWrite:u,onSaved:()=>r.invalidateQueries({queryKey:[`sku-settings`]}),onCart:()=>w(e),showToast:i},e.group.key))})]})}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-4 px-4 py-3 text-xs text-bambu-gray border-t border-bambu-dark-tertiary bg-bambu-dark-tertiary/20`,children:[(0,Z.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-bambu-green inline-block`}),a(`forecast.trendLegend`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-blue-400 inline-block`}),a(`forecast.estimatedLegend`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`w-2 h-2 rounded-full bg-bambu-gray/40 inline-block`}),a(`forecast.noDataLegend`)]})]})]}),C&&(0,Z.jsx)(vn,{forecast:C,onClose:()=>w(null),onAdd:e=>{o.addToShoppingList(e).then(()=>{r.invalidateQueries({queryKey:[`shopping-list`]}),i(a(`forecast.addedToCart`),`success`),w(null),E(!0)}).catch(()=>i(a(`forecast.failedAddItem`),`error`))}})]})}function sn({col:e,active:t,dir:n,onSort:r,children:i}){return(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide cursor-pointer select-none hover:text-white transition-colors`,onClick:()=>r(e),children:(0,Z.jsxs)(`span`,{className:`inline-flex items-center`,children:[i,t===e?n===`asc`?(0,Z.jsx)(x,{className:`w-3 h-3 ml-1 text-bambu-green`}):(0,Z.jsx)(y,{className:`w-3 h-3 ml-1 text-bambu-green`}):(0,Z.jsx)(b,{className:`w-3 h-3 ml-1 opacity-40`})]})})}function cn({forecast:e,onCart:t}){let{t:r}=n(),i=Qt(e.group.brand,e.group.material,e.group.subtype,e.group.colorName),a=e.stockBreakAlert;return(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 px-4 py-3 rounded-lg border text-sm ${a?`bg-red-50 dark:bg-red-500/10 border-red-300 dark:border-red-500/30 text-red-700 dark:text-red-300`:`bg-yellow-50 dark:bg-yellow-500/10 border-yellow-300 dark:border-yellow-500/30 text-yellow-700 dark:text-yellow-300`}`,children:[(0,Z.jsx)(H,{className:`w-4 h-4 flex-shrink-0`}),(0,Z.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Z.jsx)(`span`,{className:`font-medium`,children:i}),a?(0,Z.jsxs)(`span`,{className:`ml-2 text-xs opacity-80`,children:[r(`forecast.stockBreakRisk`),` — `,r(`forecast.stockBreakDetail`,{days:e.daysRemaining,lt:e.effectiveLeadTimeDays})]}):(0,Z.jsxs)(`span`,{className:`ml-2 text-xs opacity-80`,children:[r(`forecast.reorderNow`),` — `,r(`forecast.reorderTriggerPassed`,{date:e.reorderTriggerDate?en(e.reorderTriggerDate):`—`})]})]}),(0,Z.jsxs)(`button`,{onClick:t,className:`flex items-center gap-1.5 px-2.5 py-1 rounded border border-current text-xs opacity-70 hover:opacity-100 transition-opacity`,children:[(0,Z.jsx)(R,{className:`w-3 h-3`}),` `,r(`forecast.order`)]})]})}var ln=[{label:`1W`,value:7},{label:`1M`,value:30},{label:`6M`,value:180}];function un({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:Qt(e.group.brand,e.group.material,e.group.subtype,e.group.colorName),color:Xt[n%Xt.length],rop:e.reorderPointG,points:an(e,t)})),s=a.map(e=>{let t={day:e,label:tn($t(new Date,e))};for(let n of o){let r=n.points.find(t=>t.day===e);t[n.key]=r?.stock??0}return t}),c=(()=>{for(let e=t;e>=0;e--)if(o.some(t=>s[e]?.[t.key]>0))return e;return t})(),l=s.slice(0,c+1),u=o.filter(e=>e.rop>0);return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-4`,children:[(0,Z.jsx)(je,{className:`w-4 h-4 text-bambu-green`}),(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-white`,children:i(`forecast.chartTitle`)}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray ml-1 hidden sm:inline`,children:i(`forecast.dashedLinesROP`)}),(0,Z.jsx)(`div`,{className:`ml-auto flex items-center bg-bambu-dark-tertiary rounded-lg p-0.5`,children:ln.map(e=>(0,Z.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,Z.jsx)(Me,{width:`100%`,height:220,children:(0,Z.jsxs)(at,{data:l,margin:{top:4,right:8,bottom:0,left:0},children:[(0,Z.jsx)(`defs`,{children:o.map(e=>(0,Z.jsxs)(`linearGradient`,{id:`grad-${e.key}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Z.jsx)(`stop`,{offset:`5%`,stopColor:e.color,stopOpacity:.25}),(0,Z.jsx)(`stop`,{offset:`95%`,stopColor:e.color,stopOpacity:.02})]},e.key))}),(0,Z.jsx)(Pe,{strokeDasharray:`3 3`,stroke:`#374151`,strokeOpacity:.5}),(0,Z.jsx)(Ne,{dataKey:`label`,tick:{fill:`#6B7280`,fontSize:10},interval:Math.max(0,Math.ceil(c/8)-1),axisLine:!1,tickLine:!1}),(0,Z.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,Z.jsx)(B,{content:({label:e,payload:t})=>t?.length?(0,Z.jsxs)(`div`,{style:{background:`#1a1a2e`,border:`1px solid #374151`,borderRadius:8,fontSize:12,padding:`8px 12px`},children:[(0,Z.jsx)(`div`,{style:{color:`#9CA3AF`,marginBottom:6},children:e}),t.map(e=>{let t=o.find(t=>t.key===String(e.dataKey));return typeof e.value==`number`?(0,Z.jsxs)(`div`,{style:{display:`flex`,alignItems:`center`,gap:6,color:`#E5E7EB`,marginBottom:2},children:[(0,Z.jsx)(`span`,{style:{color:t?.color??`#9CA3AF`,fontSize:10},children:`●`}),(0,Z.jsx)(`span`,{children:t?.label??String(e.dataKey)}),(0,Z.jsxs)(`span`,{style:{color:`#9CA3AF`,marginLeft:4},children:[e.value,`g`]})]},String(e.dataKey)):null})]}):null}),(0,Z.jsx)(C,{formatter:e=>(0,Z.jsx)(`span`,{style:{color:`#9CA3AF`,fontSize:11},children:o.find(t=>t.key===e)?.label??e})}),o.map(e=>(0,Z.jsx)(it,{type:`monotone`,dataKey:e.key,stroke:e.color,strokeWidth:2,fill:`url(#grad-${e.key})`,dot:!1,activeDot:{r:3}},e.key)),u.map(e=>(0,Z.jsx)(T,{y:e.rop,stroke:e.color,strokeDasharray:`4 3`,strokeOpacity:.6},`rop-${e.key}`))]})})]})}function dn({value:e,onSave:t}){let{t:r}=n(),[i,a]=(0,X.useState)(!1),[o,s]=(0,X.useState)(String(e));function c(){let e=parseInt(o,10);isNaN(e)||e<0||(t(e),a(!1))}return(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 px-3 py-1.5 bg-bambu-dark-tertiary/40 rounded-lg border border-bambu-dark-tertiary text-xs text-bambu-gray`,children:[(0,Z.jsx)(A,{className:`w-3.5 h-3.5 flex-shrink-0`,"aria-label":r(`forecast.globalLeadTimeHint`)}),(0,Z.jsxs)(`span`,{className:`hidden sm:inline`,children:[r(`forecast.globalLeadTime`),`:`]}),i?(0,Z.jsxs)(`form`,{className:`flex items-center gap-1.5`,onSubmit:e=>{e.preventDefault(),c()},children:[(0,Z.jsx)(`input`,{type:`number`,min:0,max:365,value:o,onChange:e=>s(e.target.value),className:`w-14 px-1.5 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`,autoFocus:!0}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:`d`}),(0,Z.jsx)(`button`,{type:`submit`,className:`px-2 py-0.5 bg-bambu-green text-white text-xs rounded hover:bg-bambu-green/80`,children:r(`forecast.save`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>a(!1),className:`text-xs text-bambu-gray hover:text-white`,children:`✕`})]}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsxs)(`span`,{className:`font-semibold text-white`,children:[e,`d`]}),(0,Z.jsx)(`button`,{onClick:()=>{s(String(e)),a(!0)},className:`p-0.5 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(me,{className:`w-3 h-3`})})]})]})}function fn({forecast:e,globalLeadTime:t,canWrite:r,onSaved:i,onCart:a,showToast:s}){let{t:c}=n(),[l,u]=(0,X.useState)(!1),[d,f]=(0,X.useState)(!1),[m,h]=(0,X.useState)(!1),[g,_]=(0,X.useState)(String(e.settings?.lead_time_days??0)),[v,y]=(0,X.useState)(String(e.settings?.safety_margin_value??14)),[b,x]=(0,X.useState)(e.settings?.safety_margin_unit??`days`);(0,X.useEffect)(()=>{d||_(String(e.settings?.lead_time_days??0))},[e.settings?.lead_time_days,d]),(0,X.useEffect)(()=>{m||(y(String(e.settings?.safety_margin_value??14)),x(e.settings?.safety_margin_unit??`days`))},[e.settings?.safety_margin_value,e.settings?.safety_margin_unit,m]);let C=p({mutationFn:o.upsertSkuSettings,onSuccess:()=>{i(),s(c(`forecast.settingsSaved`),`success`)},onError:()=>s(c(`forecast.failedSaveSettings`),`error`)}),w=e.settings?.alerts_snoozed??!1,T=Qt(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,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-bambu-green/15 text-bambu-green`,children:[(0,Z.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-bambu-green`}),c(`forecast.trend`)]}):e.rateTier===`delta`?(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-blue-100 dark:bg-blue-400/15 text-blue-700 dark:text-blue-400`,children:[(0,Z.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-blue-400`}),c(`forecast.estimated`)]}):(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-bambu-dark-tertiary text-bambu-gray/60`,children:[(0,Z.jsx)(`span`,{className:`w-1.5 h-1.5 rounded-full bg-bambu-gray/40`}),c(`forecast.noData`)]});return(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/50 cursor-pointer hover:bg-bambu-dark-tertiary/30 transition-colors ${w?``:e.stockBreakAlert?`bg-red-500/5`:e.reorderAlert?`bg-yellow-500/5`:``} ${w?`opacity-50`:``}`,onClick:()=>u(e=>!e),children:[(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`block w-5 h-5 rounded-full border border-black/20`,style:k})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:T})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.totalSpools})}),(0,Z.jsx)(`td`,{className:`px-4 py-3 min-w-[140px]`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${A>50?`bg-bambu-green`:A>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(A,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(e.totalRemainingG),`g`]})]})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5 flex-wrap`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:e.dailyRateG===null?`—`:`${e.dailyRateG.toFixed(1)}g/d`}),ne]})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm font-semibold ${ee}`,children:e.daysRemaining===null?(0,Z.jsx)(`span`,{className:`text-bambu-gray font-normal`,children:`—`}):`${e.daysRemaining}d`})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.projectedEmptyDate?en(e.projectedEmptyDate):`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,children:(0,Z.jsx)(`span`,{className:`text-sm ${!w&&e.reorderAlert?`text-yellow-700 dark:text-yellow-400`:`text-bambu-gray`}`,children:e.reorderTriggerDate?en(e.reorderTriggerDate):`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-3`,onClick:e=>e.stopPropagation(),children:(0,Z.jsxs)(`div`,{className:`flex items-center justify-end gap-1`,children:[r&&(0,Z.jsx)(`button`,{onClick:a,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:c(`forecast.addToCart`),children:(0,Z.jsx)(R,{className:`w-4 h-4`})}),!w&&(e.stockBreakAlert?(0,Z.jsx)(H,{className:`w-4 h-4 text-red-600 dark:text-red-400`,"aria-label":c(`forecast.stockBreakRisk`)}):e.reorderAlert?(0,Z.jsx)(H,{className:`w-4 h-4 text-yellow-600 dark:text-yellow-400`,"aria-label":c(`forecast.reorderNow`)}):e.daysRemaining===null?null:(0,Z.jsx)(E,{className:`w-4 h-4 text-bambu-green/50`})),r&&(0,Z.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,Z.jsx)(S,{className:`w-3.5 h-3.5`})}),(0,Z.jsx)(`button`,{onClick:e=>{e.stopPropagation(),u(e=>!e)},className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,children:l?(0,Z.jsx)(O,{className:`w-4 h-4`}):(0,Z.jsx)(D,{className:`w-4 h-4`})})]})})]}),l&&(0,Z.jsx)(`tr`,{className:`bg-bambu-dark-tertiary/10`,children:(0,Z.jsx)(`td`,{colSpan:9,className:`px-6 py-4`,children:(0,Z.jsxs)(`div`,{className:`space-y-3`,children:[(0,Z.jsxs)(`div`,{className:`grid gap-2 ${r?`grid-cols-2 sm:grid-cols-4`:`grid-cols-2`}`,children:[(0,Z.jsx)(pn,{label:c(`forecast.effectiveLeadTime`),value:`${e.effectiveLeadTimeDays}d`,hint:c(`forecast.effectiveLeadTimeHint`,{global:t,sku:e.settings?.lead_time_days??0})}),(0,Z.jsx)(pn,{label:c(`forecast.reorderPoint`),value:`${Math.round(e.reorderPointG)}g`,hint:c(`forecast.reorderPointHint`)}),r&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(mn,{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,Z.jsx)(hn,{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,Z.jsxs)(`div`,{className:`border-t border-bambu-dark-tertiary pt-3`,children:[(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray mb-2`,children:c(`forecast.individualSpools`)}),(0,Z.jsx)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:`#`}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.remaining`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.used`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`forecast.labelWeight`)})]})}),(0,Z.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:e.group.spools.map(e=>{let t=Math.max(0,e.label_weight-e.weight_used),n=e.label_weight>0?t/e.label_weight*100:0;return(0,Z.jsxs)(`tr`,{className:`hover:bg-bambu-dark-tertiary/30 transition-colors`,children:[(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray/70`,children:[`#`,e.id]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsx)(`div`,{className:`w-24 h-1.5 bg-bambu-dark-tertiary rounded-full overflow-hidden flex-shrink-0`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${n>50?`bg-bambu-green`:n>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(n,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-sm text-white`,children:[Math.round(t),`g`]})]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[Math.round(Math.max(0,e.weight_used-(e.weight_used_baseline??0))),`g`]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[e.label_weight,`g`]})})]},e.id)})})]})})]})]})})})]})}function pn({label:e,value:t,hint:n}){return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3`,title:n,children:[(0,Z.jsx)(`div`,{className:`text-xs font-medium text-white mb-1`,children:e}),(0,Z.jsx)(`div`,{className:`text-lg font-semibold text-white`,children:t})]})}function mn({label:e,hint:t,unit:n,editing:r,value:i,inputValue:a,onInputChange:o,onEdit:s,onSave:c,onCancel:l,isPending:u,saveLabel:d=`Save`,cancelLabel:f=`Cancel`}){return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3 space-y-1`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs font-medium text-white`,children:e}),(0,Z.jsx)(`span`,{title:t,children:(0,Z.jsx)(A,{className:`w-3 h-3 text-bambu-gray/50`})})]}),r?(0,Z.jsxs)(`form`,{className:`flex items-center gap-2`,onSubmit:e=>{e.preventDefault(),c()},children:[(0,Z.jsx)(`input`,{type:`number`,min:0,max:365,value:a,onChange:e=>o(e.target.value),className:`w-20 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`,autoFocus:!0,disabled:u}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:n}),(0,Z.jsx)(`button`,{type:`submit`,disabled:u,className:`px-2 py-1 bg-bambu-green text-white text-xs rounded hover:bg-bambu-green/80 disabled:opacity-50`,children:d}),(0,Z.jsx)(`button`,{type:`button`,onClick:l,disabled:u,className:`px-2 py-1 text-xs text-bambu-gray hover:text-white`,children:f})]}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-lg font-semibold text-white`,children:i}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:n}),(0,Z.jsx)(`button`,{onClick:s,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(me,{className:`w-3 h-3`})})]})]})}function hn({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,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg p-3 space-y-1`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs font-medium text-white`,children:m}),(0,Z.jsx)(`span`,{title:_,children:(0,Z.jsx)(A,{className:`w-3 h-3 text-bambu-gray/50`})})]}),r?(0,Z.jsxs)(`form`,{className:`flex items-center gap-2 flex-wrap`,onSubmit:e=>{e.preventDefault(),l()},children:[(0,Z.jsx)(`input`,{type:`number`,min:0,max:t===`g`?1e4:365,value:i,onChange:e=>o(e.target.value),className:`w-20 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`,autoFocus:!0,disabled:d}),(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded overflow-hidden text-xs`,children:[(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`days`),className:`px-2 py-1 transition-colors ${t===`days`?`bg-bambu-green text-white`:`text-bambu-gray hover:text-white`}`,children:`days`}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`g`),className:`px-2 py-1 transition-colors ${t===`g`?`bg-bambu-green text-white`:`text-bambu-gray hover:text-white`}`,children:`g`})]}),(0,Z.jsx)(`button`,{type:`submit`,disabled:d,className:`px-2 py-1 bg-bambu-green text-white text-xs rounded hover:bg-bambu-green/80 disabled:opacity-50`,children:f}),(0,Z.jsx)(`button`,{type:`button`,onClick:u,disabled:d,className:`px-2 py-1 text-xs text-bambu-gray hover:text-white`,children:p})]}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-lg font-semibold text-white`,children:e}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:t}),g!==null&&t===`days`&&(0,Z.jsxs)(`span`,{className:`text-lg font-semibold text-white`,children:[`≈ `,g,`g`]}),t===`g`&&!!a&&(0,Z.jsxs)(`span`,{className:`text-lg font-semibold text-white`,children:[`≈ `,Math.round(e/a),`d`]}),(0,Z.jsx)(`button`,{onClick:c,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(me,{className:`w-3 h-3`})})]})]})}function gn({items:e,forecasts:t,globalLeadTime:r,canWrite:i,onClose:a,onRemove:s,onClear:c}){let{t:l}=n(),u=d(),[f,m]=(0,X.useState)(`list`),h=p({mutationFn:async({id:e,status:t,item:n,avgSpoolG:r})=>{if(await o.updateShoppingListStatus(e,t),t===`received`&&n){let t=r??1e3,i={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 o.bulkCreateSpools(i,n.quantity_spools),await o.removeFromShoppingList(e)}},onSuccess:()=>{u.invalidateQueries({queryKey:[`shopping-list`]}),u.invalidateQueries({queryKey:[`spools`]})}}),g=(0,X.useMemo)(()=>{let e=new Map;for(let n of t)e.set(n.group.key,n);return e},[t]),_=(0,X.useMemo)(()=>e.map(e=>({item:e,forecast:g.get(Zt(e.material,e.subtype,e.brand,e.color_name))??null})),[e,g]),v=(0,X.useMemo)(()=>_.filter(({forecast:e})=>!e||e.dailyRateG===null?!1:e.stockBreakAlert||e.daysRemaining!==null&&e.daysRemaining<=e.effectiveLeadTimeDays),[_]);function y(){let t=[l(`forecast.qty`),l(`forecast.material`),l(`inventory.brand`),l(`inventory.subtype`),l(`inventory.color`),`${l(`forecast.weight`)} (g)`,`${l(`forecast.leadTime`)} (d)`,l(`forecast.expectedRestock`),l(`forecast.status`),l(`forecast.note`)],n=e.map(e=>{let t=g.get(Zt(e.material,e.subtype,e.brand,e.color_name))??null,n=t&&t.totalSpools>0?t.totalLabelG/t.totalSpools:1e3,i=Math.round(e.quantity_spools*n),a=t?.effectiveLeadTimeDays??r??0,o=a>0?en($t(new Date,a)):``;return[e.quantity_spools,e.material,e.brand??``,e.subtype??``,e.color_name??``,i,a||``,o,e.status,e.note??``].map(e=>Wt(e)).join(`,`)}),i=[t.join(`,`),...n].join(`
- `),a=new Blob([i],{type:`text/csv`}),o=URL.createObjectURL(a),s=document.createElement(`a`);s.href=o,s.download=`shopping-list-${new Date().toISOString().slice(0,10)}.csv`,s.click(),setTimeout(()=>URL.revokeObjectURL(o),100)}return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsx)(R,{className:`w-4 h-4 text-bambu-green`}),(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-white`,children:l(`forecast.shoppingList`)}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:l(`forecast.shoppingListItems`,{count:e.length})}),e.length>0&&(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-tertiary rounded-md p-0.5 ml-1`,children:[(0,Z.jsxs)(`button`,{onClick:()=>m(`list`),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${f===`list`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:[(0,Z.jsx)(fe,{className:`w-3 h-3`}),l(`forecast.listView`)]}),(0,Z.jsxs)(`button`,{onClick:()=>m(`logistics`),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${f===`logistics`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:[(0,Z.jsx)(Lt,{className:`w-3 h-3`}),l(`forecast.logisticsView`),v.length>0&&(0,Z.jsx)(`span`,{className:`w-3.5 h-3.5 rounded-full bg-red-500 text-white text-[9px] font-bold flex items-center justify-center`,children:v.length})]})]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[e.length>0&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`button`,{onClick:y,className:`flex items-center gap-1.5 text-xs text-bambu-gray hover:text-white transition-colors px-2 py-1 rounded border border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`,children:[(0,Z.jsx)(P,{className:`w-3 h-3`}),l(`forecast.downloadCsv`)]}),i&&(0,Z.jsx)(`button`,{onClick:c,className:`text-xs text-red-700 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300 transition-colors px-2 py-1 rounded border border-red-200 dark:border-red-500/20 hover:bg-red-50 dark:hover:bg-red-500/10`,children:l(`forecast.clearAll`)})]}),(0,Z.jsx)(`button`,{onClick:a,className:`p-1 text-bambu-gray hover:text-white transition-colors`,children:(0,Z.jsx)(Le,{className:`w-4 h-4`})})]})]}),e.length===0?(0,Z.jsxs)(`div`,{className:`flex flex-col items-center py-8 text-bambu-gray`,children:[(0,Z.jsx)(fe,{className:`w-8 h-8 mb-2 opacity-30`}),(0,Z.jsx)(`p`,{className:`text-sm`,children:l(`forecast.shoppingListEmpty`)})]}):f===`list`?(0,Z.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/20`,children:[(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.qty`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.material`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.weight`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.leadTime`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.expectedRestock`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.status`)}),(0,Z.jsx)(`th`,{className:`px-4 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.note`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 text-right text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:l(`forecast.actions`)})]})}),(0,Z.jsx)(`tbody`,{className:`divide-y divide-bambu-dark-tertiary`,children:e.map(e=>{let t=Qt(e.brand,e.material,e.subtype,e.color_name),n=v.some(t=>t.item.id===e.id),a=g.get(Zt(e.material,e.subtype,e.brand,e.color_name))??null,o=a&&a.totalSpools>0?a.totalLabelG/a.totalSpools:1e3,c=Math.round(e.quantity_spools*o),u=a?.effectiveLeadTimeDays??r??0,d=u>0?$t(new Date,u):null,f=e.status===`purchased`||e.status===`received`,p=e.status===`received`,m=h.isPending;return(0,Z.jsxs)(`tr`,{className:`hover:bg-bambu-dark-tertiary/30 transition-colors ${n&&!f?`bg-red-500/5`:``}`,children:[(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsxs)(`span`,{className:`text-sm font-semibold text-bambu-green`,children:[e.quantity_spools,`×`]})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:t}),n&&!f&&(0,Z.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,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:c>=1e3?`${(c/1e3).toFixed(1)}kg`:`${c}g`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:u>0?`${u}d`:`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:d?en(d):`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`inline-flex items-center px-2 py-0.5 rounded text-xs font-medium ${p?`bg-bambu-green/20 text-bambu-green`:f?`bg-blue-100 dark:bg-blue-400/20 text-blue-700 dark:text-blue-400`:`bg-bambu-dark-tertiary text-bambu-gray`}`,children:l(p?`forecast.received`:f?`forecast.purchased`:`forecast.pending`)})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:e.note||`—`})}),(0,Z.jsx)(`td`,{className:`px-4 py-2.5`,children:(0,Z.jsx)(`div`,{className:`flex items-center justify-end gap-1`,children:i&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`button`,{onClick:()=>h.mutate({id:e.id,status:f?`pending`:`purchased`}),disabled:m||p,title:l(f?`forecast.resetToPending`:`forecast.markPurchased`),className:`p-1.5 rounded transition-colors disabled:opacity-30 ${f?`text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300`:`text-blue-600/50 dark:text-blue-400/50 hover:text-blue-600 dark:hover:text-blue-400`}`,children:f?(0,Z.jsx)(we,{className:`w-4 h-4`}):(0,Z.jsx)(Rt,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>h.mutate({id:e.id,status:`received`,item:e,avgSpoolG:o}),disabled:m||!f||p,title:l(`forecast.markReceived`),className:`p-1.5 rounded transition-colors text-bambu-green/50 hover:text-bambu-green disabled:opacity-30`,children:(0,Z.jsx)(Bt,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>s(e.id),className:`p-1 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 transition-colors`,title:l(`forecast.remove`),children:(0,Z.jsx)(z,{className:`w-3.5 h-3.5`})})]})})})]},e.id)})})]})}):(0,Z.jsx)(`div`,{className:`divide-y divide-bambu-dark-tertiary`,children:_.filter(({item:e})=>e.status!==`received`).map(({item:e,forecast:t})=>(0,Z.jsx)(_n,{item:e,forecast:t,globalLeadTime:r,canWrite:i,onRemove:()=>s(e.id)},e.id))})]})}function _n({item:e,forecast:t,globalLeadTime:r,canWrite:i,onRemove:a}){let{t:o}=n(),s=Qt(e.brand,e.material,e.subtype,e.color_name),c=(0,X.useMemo)(()=>{if(!t||t.dailyRateG===null||t.dailyRateG<=0)return null;let n=t.dailyRateG,r=t.effectiveLeadTimeDays,i=t.totalSpools>0?t.totalLabelG/t.totalSpools:1e3,a=e.quantity_spools*i,o=Math.max(0,t.totalRemainingG-n*r),s=o+a,c=Math.ceil(s/n),l=Math.min(r+c+5,365),u=[];for(let e=0;e<=l;e++){let i=tn($t(new Date,e));if(e===r)u.push({day:e,label:i,stock:Math.round(o),rop:Math.round(t.reorderPointG),safetyStock:Math.round(t.safetyStockG)}),u.push({day:e,label:i,stock:Math.round(s),rop:Math.round(t.reorderPointG),safetyStock:Math.round(t.safetyStockG),arrival:!0});else{let a=e<r?Math.max(0,t.totalRemainingG-n*e):Math.max(0,s-n*(e-r));u.push({day:e,label:i,stock:Math.round(a),rop:Math.round(t.reorderPointG),safetyStock:Math.round(t.safetyStockG)})}}return{points:u,lt:r,maxDays:l,arrivalG:a,peakG:s,stockAtArrival:o}},[t,e.quantity_spools]),l=(0,X.useMemo)(()=>{if(!t||t.dailyRateG===null||t.dailyRateG<=0)return null;let e=Math.floor(t.totalRemainingG/t.dailyRateG);return e<t.effectiveLeadTimeDays?e:null},[t]),u=l!==null;return(0,Z.jsxs)(`div`,{className:`px-4 py-4 ${u?`bg-red-500/5`:``}`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between mb-3`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 min-w-0`,children:[u?(0,Z.jsx)(H,{className:`w-4 h-4 text-red-600 dark:text-red-400 flex-shrink-0`}):(0,Z.jsx)(E,{className:`w-4 h-4 text-bambu-green/60 flex-shrink-0`}),(0,Z.jsx)(`span`,{className:`text-sm font-medium text-white truncate`,children:s}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray flex-shrink-0`,children:[o(`forecast.spoolCount`,{count:e.quantity_spools}),` ordered`]})]}),i&&(0,Z.jsx)(`button`,{onClick:a,className:`p-1 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 transition-colors flex-shrink-0`,children:(0,Z.jsx)(z,{className:`w-3.5 h-3.5`})})]}),u&&(0,Z.jsxs)(`div`,{className:`mb-3 px-3 py-2 rounded-lg bg-red-50 dark:bg-red-500/10 border border-red-200 dark:border-red-500/20 text-xs text-red-700 dark:text-red-300`,children:[(0,Z.jsx)(`span`,{className:`font-medium`,children:o(`forecast.stockBreakIn`,{days:l})}),` `,o(`forecast.stockRunsOutBefore`,{lt:t.effectiveLeadTimeDays}),t.dailyRateG!==null&&(0,Z.jsxs)(`span`,{children:[` `,o(`forecast.atRate`,{rate:t.dailyRateG.toFixed(1)}),` `,(0,Z.jsx)(`span`,{className:`font-semibold`,children:o(`forecast.moreSpools`,{count:Math.ceil((t.dailyRateG*t.effectiveLeadTimeDays-t.totalRemainingG)/(t.totalLabelG/(t.totalSpools||1)||1e3))})}),` `,o(`forecast.bridgeGap`)]})]}),!t||t.dailyRateG===null?(0,Z.jsx)(`div`,{className:`py-4 text-center text-xs text-bambu-gray`,children:o(`forecast.noUsageData`)}):c?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`div`,{className:`grid grid-cols-5 gap-2 mb-3`,children:[(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.stock`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[Math.round(t.totalRemainingG),`g`]})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.leadTime`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[t.effectiveLeadTimeDays,`d`]}),(0,Z.jsxs)(`div`,{className:`text-[10px] text-bambu-gray/60`,children:[`max(g:`,r,`, sku:`,t.settings?.lead_time_days??0,`)`]})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-tertiary/40 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.safetyMarginLabel`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-white`,children:[Math.round(t.safetyStockG),`g`]})]}),(0,Z.jsxs)(`div`,{className:`rounded-lg px-2.5 py-2 text-center ${u?`bg-red-100 dark:bg-red-500/15`:`bg-bambu-dark-tertiary/40`}`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.daysLeft`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold ${u?`text-red-700 dark:text-red-400`:`text-green-700 dark:text-green-400`}`,children:[t.daysRemaining??`—`,`d`]})]}),c&&(0,Z.jsxs)(`div`,{className:`bg-bambu-green/15 rounded-lg px-2.5 py-2 text-center`,children:[(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mb-0.5`,children:o(`forecast.onArrival`)}),(0,Z.jsxs)(`div`,{className:`text-sm font-semibold text-bambu-green`,children:[Math.round(c.arrivalG),`g`]}),(0,Z.jsxs)(`div`,{className:`text-[10px] text-bambu-gray/60`,children:[`+`,o(`forecast.spoolCount`,{count:e.quantity_spools})]})]})]}),(0,Z.jsx)(Me,{width:`100%`,height:180,children:(0,Z.jsxs)(at,{data:c.points,margin:{top:8,right:8,bottom:0,left:0},children:[(0,Z.jsxs)(`defs`,{children:[(0,Z.jsxs)(`linearGradient`,{id:`cart-pre-${e.id}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Z.jsx)(`stop`,{offset:`5%`,stopColor:u?`#EF4444`:`#1DB954`,stopOpacity:.25}),(0,Z.jsx)(`stop`,{offset:`95%`,stopColor:u?`#EF4444`:`#1DB954`,stopOpacity:.02})]}),(0,Z.jsxs)(`linearGradient`,{id:`cart-post-${e.id}`,x1:`0`,y1:`0`,x2:`0`,y2:`1`,children:[(0,Z.jsx)(`stop`,{offset:`5%`,stopColor:`#1DB954`,stopOpacity:.3}),(0,Z.jsx)(`stop`,{offset:`95%`,stopColor:`#1DB954`,stopOpacity:.03})]})]}),(0,Z.jsx)(Pe,{strokeDasharray:`3 3`,stroke:`#374151`,strokeOpacity:.4}),(0,Z.jsx)(Ne,{dataKey:`label`,tick:{fill:`#6B7280`,fontSize:9},interval:Math.max(0,Math.ceil(c.maxDays/6)-1),axisLine:!1,tickLine:!1}),(0,Z.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,Z.jsx)(B,{contentStyle:{background:`#1a1a2e`,border:`1px solid #374151`,borderRadius:8,fontSize:11},labelStyle:{color:`#9CA3AF`},formatter:(e,t)=>typeof e==`number`?t===`stock`?`${e}g — ${o(`forecast.stock`)}`:t===`rop`?`${e}g — ${o(`forecast.reorderPoint`)}`:t===`safetyStock`?`${e}g — ${o(`forecast.safetyMarginLabel`)}`:`${e}`:``}),(0,Z.jsx)(it,{type:`linear`,dataKey:`stock`,stroke:`#1DB954`,strokeWidth:2,fill:`url(#cart-post-${e.id})`,dot:!1,activeDot:{r:3}}),t.reorderPointG>0&&(0,Z.jsx)(T,{y:t.reorderPointG,stroke:`#F59E0B`,strokeDasharray:`5 3`,strokeOpacity:.8,label:{value:`ROP`,position:`insideTopRight`,fill:`#F59E0B`,fontSize:9}}),t.safetyStockG>0&&(0,Z.jsx)(T,{y:t.safetyStockG,stroke:`#6B7280`,strokeDasharray:`3 3`,strokeOpacity:.6,label:{value:`SS`,position:`insideTopRight`,fill:`#6B7280`,fontSize:9}}),(0,Z.jsx)(T,{x:tn($t(new Date,c.lt)),stroke:`#3B82F6`,strokeWidth:1.5,strokeDasharray:`4 3`,strokeOpacity:.9,label:{value:`+${c.arrivalG>=1e3?`${(c.arrivalG/1e3).toFixed(1)}kg`:`${Math.round(c.arrivalG)}g`} arrives (d${c.lt})`,position:`insideTopLeft`,fill:`#3B82F6`,fontSize:9}}),l!==null&&(0,Z.jsx)(T,{x:tn($t(new Date,l)),stroke:`#EF4444`,strokeWidth:1.5,strokeOpacity:.9,label:{value:`OUT`,position:`insideTopLeft`,fill:`#EF4444`,fontSize:9}})]})}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-3 mt-2 text-[10px] text-bambu-gray`,children:[(0,Z.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-yellow-400 border-dashed`}),` `,o(`forecast.ropLabel`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t border-bambu-gray border-dashed`}),` `,o(`forecast.safetyStockLegend`)]}),(0,Z.jsxs)(`span`,{className:`flex items-center gap-1`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-blue-400 border-dashed`}),` `,o(`forecast.stockArrivalLegend`)]}),u&&(0,Z.jsxs)(`span`,{className:`flex items-center gap-1 text-red-700 dark:text-red-400`,children:[(0,Z.jsx)(`span`,{className:`inline-block w-4 border-t-2 border-red-400`}),` `,o(`forecast.stockoutLegend`)]})]})]}):null]})}function vn({forecast:e,onClose:t,onAdd:r}){let{t:i}=n(),a=Qt(e.group.brand,e.group.material,e.group.subtype,e.group.colorName),[o,s]=(0,X.useState)(`qty`),[c,l]=(0,X.useState)(`1`),[u,d]=(0,X.useState)(`30`),[f,p]=(0,X.useState)(``),m=(0,X.useMemo)(()=>{if(!e.dailyRateG||e.dailyRateG<=0)return null;let t=e.dailyRateG*Number(u),n=e.group.spools.length>0?e.group.spools.reduce((e,t)=>e+t.label_weight,0)/e.group.spools.length:1e3;return Math.ceil(t/n)},[e,u]),h=o===`qty`?parseInt(c,10)||1:m??1;function g(t){t.preventDefault(),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,Z.jsx)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4`,children:(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-2xl border border-bambu-dark-tertiary w-full max-w-sm shadow-2xl`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-5 pt-5 pb-4 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(R,{className:`w-5 h-5 text-bambu-green`}),(0,Z.jsx)(`h2`,{className:`text-base font-semibold text-white`,children:i(`forecast.addToCartTitle`)})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white transition-colors`,children:(0,Z.jsx)(Le,{className:`w-5 h-5`})})]}),(0,Z.jsxs)(`form`,{onSubmit:g,className:`p-5 space-y-4`,children:[(0,Z.jsx)(`div`,{className:`text-sm text-bambu-gray`,children:a}),(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-tertiary rounded-lg p-0.5`,children:[(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`qty`),className:`flex-1 py-1.5 text-xs font-medium rounded-md transition-colors ${o===`qty`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:i(`forecast.byQuantity`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>s(`duration`),className:`flex-1 py-1.5 text-xs font-medium rounded-md transition-colors ${o===`duration`?`bg-bambu-dark-secondary text-white shadow`:`text-bambu-gray hover:text-white`}`,children:i(`forecast.byDuration`)})]}),o===`qty`?(0,Z.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Z.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.numberOfSpools`)}),(0,Z.jsx)(`input`,{type:`number`,min:1,max:99,value:c,onChange:e=>l(e.target.value),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,autoFocus:!0})]}):(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Z.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.lastHowManyDays`)}),(0,Z.jsx)(`input`,{type:`number`,min:1,max:365,value:u,onChange:e=>d(e.target.value),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,autoFocus:!0})]}),e.dailyRateG===null?(0,Z.jsx)(`div`,{className:`text-xs text-yellow-700 dark:text-yellow-400 px-1`,children:i(`forecast.noUsageQty`)}):(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 px-3 py-2 bg-bambu-dark-tertiary/50 rounded-lg`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`≈`}),(0,Z.jsx)(`span`,{className:`text-sm font-semibold text-bambu-green`,children:i(`forecast.spoolCount`,{count:m??0})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray`,children:[`at `,e.dailyRateG.toFixed(1),`g/day`]})]})]}),(0,Z.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,Z.jsx)(`label`,{className:`text-xs text-bambu-gray`,children:i(`forecast.noteOptional`)}),(0,Z.jsx)(`input`,{type:`text`,maxLength:200,value:f,onChange:e=>p(e.target.value),placeholder:i(`forecast.notePlaceholder`),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/40 focus:outline-none focus:border-bambu-green`})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 pt-1`,children:[(0,Z.jsx)(`button`,{type:`submit`,className:`flex-1 py-2 bg-bambu-green text-white text-sm font-medium rounded-lg hover:bg-bambu-green/80 transition-colors`,children:i(`forecast.addNSpools`,{count:h})}),(0,Z.jsx)(`button`,{type:`button`,onClick:t,className:`px-4 py-2 text-sm text-bambu-gray hover:text-white border border-bambu-dark-tertiary rounded-lg transition-colors`,children:i(`forecast.cancel`)})]})]})]})})}function yn({links:e,onChange:t,currencySymbol:r}){let{t:i}=n(),{showToast:a}=re(),c=d(),l=(0,X.useRef)(null),[u,p]=(0,X.useState)(!1),[m,h]=(0,X.useState)(``),[g,_]=(0,X.useState)(!1),{data:v=[],isLoading:y,isError:b}=f({queryKey:q,queryFn:o.getSuppliers});(0,X.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,X.useMemo)(()=>new Set(e.map(e=>e.supplier_id)),[e]),S=(0,X.useMemo)(()=>v.filter(e=>!x.has(e.id)).filter(e=>e.name.toLowerCase().includes(m.trim().toLowerCase())),[v,x,m]),C=v.some(e=>e.name.toLowerCase()===m.trim().toLowerCase()),w=n=>{t([...e,{supplier_id:n.id,supplier_name:n.name,supplier_article_number:``,quoted_price_per_kg:null,is_purchase_source:!1}]),p(!1),h(``)},T=async()=>{let e=m.trim();if(e){_(!0);try{let t=await o.createSupplier({name:e});await $e(c),w(t)}catch(e){console.error(`SupplierSection.createAndAdd failed:`,e),a(i(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,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`label`,{className:`block text-sm font-medium text-bambu-gray mb-1`,children:i(`inventory.suppliers.label`)}),e.length>0&&(0,Z.jsx)(`div`,{className:`space-y-2 mb-2`,children:e.map(e=>(0,Z.jsxs)(`div`,{className:`p-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs bg-bambu-green/10 text-bambu-green`,children:[(0,Z.jsx)(Vt,{className:`w-3 h-3`}),e.supplier_name]}),(0,Z.jsxs)(`label`,{className:`ml-auto flex items-center gap-1.5 text-xs text-bambu-gray cursor-pointer`,title:i(`inventory.suppliers.purchaseSourceHelp`),children:[(0,Z.jsx)(`input`,{type:`checkbox`,className:`w-3.5 h-3.5 accent-bambu-green`,checked:e.is_purchase_source,onChange:t=>E(e.supplier_id,{is_purchase_source:t.target.checked})}),i(`inventory.suppliers.purchaseSource`)]}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>O(e.supplier_id),className:`p-1 rounded text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary`,"aria-label":i(`common.remove`),children:(0,Z.jsx)(Le,{className:`w-3.5 h-3.5`})})]}),(0,Z.jsxs)(`div`,{className:`flex gap-2 mt-2`,children:[(0,Z.jsx)(`input`,{type:`text`,className:`flex-1 min-w-0 px-2 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`,placeholder:i(`inventory.suppliers.articleNumberPlaceholder`),value:e.supplier_article_number,maxLength:100,onChange:t=>E(e.supplier_id,{supplier_article_number:t.target.value})}),(0,Z.jsxs)(`div`,{className:`relative w-28`,children:[(0,Z.jsx)(`span`,{className:`absolute left-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray pointer-events-none`,children:r}),(0,Z.jsx)(`input`,{type:`number`,className:`w-full pl-6 pr-2 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs text-right placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`,placeholder:i(`inventory.suppliers.pricePlaceholder`),title:i(`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,Z.jsxs)(`div`,{className:`relative`,ref:l,children:[(0,Z.jsxs)(`button`,{type:`button`,onClick:()=>p(e=>!e),className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-sm text-bambu-gray hover:text-white flex items-center justify-between transition-colors focus:outline-none focus:border-bambu-green`,children:[(0,Z.jsx)(`span`,{children:i(`inventory.suppliers.addSupplier`)}),(0,Z.jsx)(D,{className:`w-4 h-4 transition-transform ${u?`rotate-180`:``}`})]}),u&&(0,Z.jsxs)(`div`,{className:`absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg`,children:[(0,Z.jsx)(`div`,{className:`p-2 border-b border-bambu-dark-tertiary`,children:(0,Z.jsx)(`input`,{type:`text`,autoFocus:!0,className:`w-full px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`,placeholder:i(`inventory.suppliers.searchPlaceholder`),value:m,onChange:e=>h(e.target.value)})}),(0,Z.jsx)(`div`,{className:`max-h-48 overflow-y-auto py-1`,children:y?(0,Z.jsxs)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray flex items-center gap-2`,children:[(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}),i(`common.loading`)]}):b?(0,Z.jsxs)(`div`,{className:`px-3 py-2 text-sm text-red-400 flex items-center gap-2`,children:[(0,Z.jsx)(H,{className:`w-4 h-4`}),i(`inventory.suppliers.loadFailed`)]}):(0,Z.jsxs)(Z.Fragment,{children:[S.map(e=>(0,Z.jsx)(`button`,{type:`button`,onClick:()=>w(e),className:`w-full px-3 py-2 text-left text-sm text-white hover:bg-bambu-dark-tertiary`,children:e.name},e.id)),S.length===0&&!m.trim()&&(0,Z.jsx)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray`,children:i(`inventory.suppliers.noneLeft`)}),m.trim()&&!C&&(0,Z.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,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(be,{className:`w-4 h-4`}),i(`inventory.suppliers.createNew`,{name:m.trim()})]})]})})]})]}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray mt-1`,children:i(`inventory.suppliers.help`)})]})}function bn({filaments:e,isLoading:t,selectedId:r,onSelect:i}){let{t:a}=n(),[o,s]=(0,X.useState)(!1),[c,l]=(0,X.useState)(``),u=(0,X.useRef)(null),d=(0,X.useRef)(null),f=(0,X.useMemo)(()=>e.find(e=>e.id===r)??null,[e,r]);(0,X.useEffect)(()=>{let e=e=>{u.current&&!u.current.contains(e.target)&&(s(!1),l(``))};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[]);let p=(0,X.useMemo)(()=>{let t=c.toLowerCase().trim();return t?e.filter(e=>e.name.toLowerCase().includes(t)||(e.material?.toLowerCase().includes(t)??!1)||(e.vendor?.name.toLowerCase().includes(t)??!1)||(e.color_name?.toLowerCase().includes(t)??!1)):e},[e,c]);(0,X.useEffect)(()=>{o&&d.current?.focus()},[o]);let m=()=>{s(!0),l(``)},h=e=>{i(e),s(!1),l(``)};return(0,Z.jsxs)(`div`,{ref:u,className:`relative`,children:[(0,Z.jsx)(`label`,{className:`block text-sm font-medium text-bambu-gray mb-1`,children:a(`inventory.pickFromSpoolmanCatalog`)}),(0,Z.jsxs)(`button`,{type:`button`,onClick:m,className:`w-full flex items-center gap-2 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-left focus:outline-none focus:border-bambu-green hover:border-bambu-gray transition-colors`,children:[t?(0,Z.jsx)(L,{className:`w-4 h-4 text-bambu-gray animate-spin shrink-0`}):f?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`span`,{className:`w-4 h-4 rounded-full shrink-0 border border-white/20`,style:K(f.color_hex),"aria-label":a(`inventory.spoolmanFilamentColorSwatch`)}),(0,Z.jsxs)(`span`,{className:`text-white text-sm truncate flex-1`,children:[f.vendor?.name?`${f.vendor.name} — `:``,f.name]})]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(fe,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`span`,{className:`text-bambu-gray text-sm flex-1`,children:a(`inventory.spoolmanFilamentCatalog`)})]}),(0,Z.jsx)(D,{className:`w-4 h-4 text-bambu-gray shrink-0 ml-auto`})]}),o&&(0,Z.jsxs)(`div`,{className:`absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl`,children:[(0,Z.jsx)(`div`,{className:`p-2 border-b border-bambu-dark-tertiary`,children:(0,Z.jsx)(`input`,{ref:d,type:`text`,value:c,onChange:e=>l(e.target.value),placeholder:a(`inventory.pickFromSpoolmanCatalog`),className:`w-full px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm placeholder-bambu-gray focus:outline-none focus:border-bambu-green`})}),(0,Z.jsx)(`ul`,{className:`max-h-56 overflow-y-auto py-1`,children:t?(0,Z.jsxs)(`li`,{className:`flex items-center justify-center gap-2 py-4 text-bambu-gray text-sm`,children:[(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}),(0,Z.jsx)(`span`,{children:a(`common.loading`,`Loading…`)})]}):p.length===0?(0,Z.jsx)(`li`,{className:`py-4 text-center text-bambu-gray text-sm`,children:a(`inventory.noSpoolmanFilaments`)}):p.map(e=>(0,Z.jsx)(`li`,{children:(0,Z.jsxs)(`button`,{type:`button`,onClick:()=>h(e),className:`w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-bambu-dark-tertiary transition-colors ${e.id===r?`bg-bambu-dark-tertiary`:``}`,children:[(0,Z.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,Z.jsxs)(`span`,{className:`flex-1 min-w-0`,children:[(0,Z.jsxs)(`span`,{className:`block text-white text-sm truncate`,children:[e.vendor?.name?`${e.vendor.name} — `:``,e.name]}),(0,Z.jsxs)(`span`,{className:`block text-bambu-gray text-xs truncate`,children:[[e.material,e.color_name].filter(Boolean).join(` · `),e.weight?` · ${e.weight}g`:``]})]})]})},e.id))})]})]})}function xn(e,t){let n=e.nozzles??[];return jt(n[(e.printer.nozzle_count??1)>1&&t>0?t:0]?.nozzle_type)??jt(n[0]?.nozzle_type)}function Sn(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 Cn(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 wn({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,X.useMemo)(()=>{let e=new Map,n=[];for(let r of t){let t=(r.printer.model||``).trim();if(!t){n.push(r);continue}let i=e.get(t);i?i.push(r):e.set(t,[r])}return[...Array.from(e.entries()).map(([e,t])=>({id:`m:${e}`,model:e,printers:t,diameters:Array.from(new Set([...ft,...t.flatMap(Sn)])).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([...ft,...Sn(e)])).sort((e,t)=>parseFloat(e)-parseFloat(t))}))]},[t]),m=p.find(e=>e.id===c)??p[0],h=(0,X.useMemo)(()=>{let e=new Map;return(t,n)=>{if(!t)return r;let i=e.get(t);if(i||(i=r.filter(e=>{let n=nt(e.name,u??{});return!n||tt(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=>Cn(e,{left:f(`inventory.leftNozzle`),right:f(`inventory.rightNozzle`),single:f(`inventory.nozzle`)}),y=t=>t.printer.connected?t.calibrations.filter(t=>St(t,e)):[],b=e=>{let t=0;for(let n of e.printers){let r=y(n);for(let i of v(n))for(let a of e.diameters)r.some(e=>(e.extruder_id??0)===i.extruder&&((e.nozzle_diameter??``).trim()||`0.4`)===a)&&(o.get(pt(n.printer.id,i.extruder,a))||t++)}return t},x=(e,t,n)=>{a(r=>{let i=new Map(r),a=gt(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=pt(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)&&nt(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(gt(e.model,n),{code:o.code,name:o.name})}}return t})};if(t.length===0)return(0,Z.jsx)(`div`,{className:`p-6 bg-bambu-dark rounded-lg text-center`,children:(0,Z.jsxs)(`p`,{className:`text-bambu-gray flex items-center justify-center gap-2`,children:[d&&(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}),f(d?`common.loading`:`inventory.noPrintersConfigured`)]})});let w=(e,t,n)=>{let r=gt(e,t),a=i.get(r),o=h(e,a?.code);return(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 min-w-0`,children:(0,Z.jsx)(st,{ariaLabel:`${e} ${t}mm ${f(`inventory.filamentPreset`)}`,value:a?.code??``,options:o,inheritLabel:n,disabled:!e,onChange:n=>x(e,t,n)})}),(0,Z.jsx)(`span`,{className:`text-[10px] font-semibold uppercase tracking-wide px-2 py-1 rounded-full shrink-0 ${a?`bg-bambu-green/20 text-bambu-green`:`bg-bambu-dark-tertiary text-bambu-gray`}`,children:f(a?`inventory.presetOverride`:`inventory.presetInherited`)})]})};return(0,Z.jsxs)(`div`,{className:`space-y-3`,children:[(g||e.color_name)&&(0,Z.jsxs)(`div`,{className:`flex items-center gap-2.5 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Z.jsx)(`span`,{className:`w-5 h-5 rounded-full border border-white/15 shrink-0`,style:{background:_},"aria-hidden":`true`}),g&&(0,Z.jsx)(`span`,{className:`text-sm text-white truncate`,children:g}),e.color_name&&(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray truncate`,children:e.color_name})]}),(0,Z.jsxs)(`div`,{className:`flex flex-col md:flex-row gap-4`,children:[(0,Z.jsx)(`div`,{role:`tablist`,"aria-label":f(`inventory.printersTab`),"aria-orientation":`vertical`,className:`md:w-52 md:shrink-0 md:self-start md:sticky md:top-0 flex md:flex-col gap-1.5 overflow-x-auto md:overflow-x-visible`,children:p.map(e=>{let t=e===m,n=b(e);return(0,Z.jsxs)(`button`,{type:`button`,role:`tab`,onClick:()=>l(e.id),"aria-selected":t,className:`flex items-center gap-2 px-3 py-2 rounded-lg border text-left transition-colors shrink-0 md:shrink ${t?`bg-bambu-green/10 border-bambu-green/40 text-white`:`bg-transparent border-transparent text-bambu-gray hover:bg-bambu-dark hover:text-white`}`,children:[(0,Z.jsxs)(`div`,{className:`min-w-0 flex-1`,children:[(0,Z.jsx)(`div`,{className:`text-sm font-semibold truncate`,children:e.model||f(`inventory.unknownModel`)}),(0,Z.jsx)(`div`,{className:`text-[11px] text-bambu-gray`,children:e.printers.length===1?f(`inventory.onePrinter`):f(`inventory.nPrinters`,{n:e.printers.length})})]}),n>0?(0,Z.jsx)(`span`,{className:`text-[10px] font-mono px-1.5 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray shrink-0`,children:n}):(0,Z.jsx)(E,{className:`w-3.5 h-3.5 text-bambu-green shrink-0`})]},e.id)})}),(0,Z.jsx)(`div`,{className:`flex-1 min-w-0`,children:m&&(0,Z.jsxs)(`div`,{className:`space-y-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-start justify-between gap-3`,children:[(0,Z.jsxs)(`div`,{className:`min-w-0`,children:[(0,Z.jsx)(`h4`,{className:`text-base font-semibold text-white truncate`,children:m.model||f(`inventory.unknownModel`)}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:m.printers.map(e=>e.printer.name).join(`, `)})]}),e.slicer_filament&&(0,Z.jsxs)(`button`,{type:`button`,onClick:C,title:f(`inventory.autoMatchPresetsHint`),className:`flex items-center gap-1.5 px-2 py-1 text-xs bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white hover:border-bambu-green transition-colors shrink-0`,children:[(0,Z.jsx)(De,{className:`w-3.5 h-3.5`}),f(`inventory.autoMatchPresets`)]})]}),(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsx)(`p`,{className:`text-xs font-semibold text-bambu-gray uppercase tracking-wide`,children:f(`inventory.filamentPreset`)}),m.model?(0,Z.jsx)(`div`,{className:`space-y-2`,children:m.diameters.map(e=>(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray w-14 shrink-0`,children:[e,`mm`]}),(0,Z.jsx)(`div`,{className:`flex-1 min-w-0`,children:w(m.model,e,f(`inventory.presetUseSpoolDefault`))})]},e))}):(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic`,children:f(`inventory.presetNeedsModel`)})]}),(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsx)(`p`,{className:`text-xs font-semibold text-bambu-gray uppercase tracking-wide`,children:f(`inventory.kProfilesPerPrinter`)}),e.material?m.printers.map(e=>{let t=y(e);return(0,Z.jsxs)(`div`,{className:`p-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(xe,{className:`w-3.5 h-3.5 text-bambu-gray shrink-0`}),(0,Z.jsx)(`span`,{className:`text-sm font-semibold text-white truncate`,children:e.printer.name}),(0,Z.jsx)(`span`,{className:`text-[10px] font-semibold uppercase tracking-wide px-2 py-0.5 rounded-full shrink-0 ${e.printer.connected?`bg-green-500/20 text-green-500`:`bg-bambu-dark-tertiary text-bambu-gray`}`,children:e.printer.connected?f(`inventory.connected`):f(`inventory.offline`)})]}),e.printer.connected?t.length===0?(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-1`,children:f(`inventory.noKProfilesMatch`)}):(0,Z.jsxs)(`div`,{className:`grid gap-x-3 gap-y-1.5 items-center`,style:{gridTemplateColumns:`3.5rem repeat(${v(e).length}, minmax(0, 1fr))`},children:[(0,Z.jsx)(`span`,{}),v(e).map(e=>(0,Z.jsx)(`span`,{className:`text-[11px] font-semibold uppercase tracking-wide text-bambu-gray`,children:e.label},e.extruder)),m.diameters.map(n=>(0,Z.jsxs)(X.Fragment,{children:[(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray`,children:[n,`mm`]}),v(e).map(r=>{let i=t.filter(e=>(e.extruder_id??0)===r.extruder&&((e.nozzle_diameter??``).trim()||`0.4`)===n),a=pt(e.printer.id,r.extruder,n),s=o.get(a);if(i.length===0)return(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray/50 px-2 py-1.5`,title:f(`inventory.noKProfilesMatch`),children:`—`},a);let c=xn(e,r.extruder),l=jt(s?.nozzle_id),u=!!s&&!Y(s.nozzle_id,c);return(0,Z.jsxs)(`select`,{title:u?f(`inventory.kProfileFlowMismatch`,{profile:At(l),fitted:At(c)}):void 0,"aria-label":`${e.printer.name} ${r.label} ${n}mm`,value:s?String(s.cali_idx):``,onChange:t=>{let a=i.find(e=>String(e.cali_idx)===t.target.value)??null;S(e.printer.id,r.extruder,n,a)},className:`min-w-0 px-2 py-1.5 bg-bambu-dark-secondary border rounded-lg text-sm text-white focus:outline-none focus:border-bambu-green ${u?`border-amber-500/60`:`border-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:f(`inventory.kProfileNotSet`)}),i.map(e=>{let t=At(jt(e.nozzle_id));return(0,Z.jsx)(`option`,{value:e.cali_idx,children:`${t?`[${t}] `:``}${e.name||e.filament_id} K=${e.k_value.toFixed(3)}`},e.cali_idx)})]},a)})]},n))]}):(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-1`,children:f(`inventory.printerOffline`)})]},e.printer.id)}):(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray italic`,children:f(`inventory.selectMaterialFirst`)})]})]})})]})]})}function Tn({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,Z.jsxs)(`div`,{className:`mt-3 p-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg space-y-2`,children:[r&&(0,Z.jsxs)(`p`,{className:`flex items-center gap-1.5 text-xs text-bambu-green`,children:[(0,Z.jsx)(De,{className:`w-3.5 h-3.5 shrink-0`,"aria-hidden":`true`}),l(`inventory.profileDefaults.prefilled`,{product:e})]}),(0,Z.jsxs)(`label`,{className:`flex items-center gap-2 text-sm ${c?`text-bambu-gray`:`text-white cursor-pointer`}`,children:[(0,Z.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,Z.jsxs)(`div`,{role:`radiogroup`,"aria-label":l(`inventory.profileDefaults.scope`),className:`flex flex-wrap gap-4 pl-6`,children:[(0,Z.jsxs)(`label`,{className:`flex items-center gap-2 text-sm text-white cursor-pointer`,children:[(0,Z.jsx)(`input`,{type:`radio`,name:`profile-default-scope`,checked:o===`all`||!u,onChange:()=>s(`all`),className:`accent-bambu-green`}),l(`inventory.profileDefaults.allColours`)]}),(0,Z.jsxs)(`label`,{className:`flex items-center gap-2 text-sm ${u?`text-white cursor-pointer`:`text-bambu-gray`}`,children:[(0,Z.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 En(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 Dn={completed:`text-bambu-green`,failed:`text-red-700 dark:text-red-400`,aborted:`text-yellow-700 dark:text-yellow-400`};function On({spoolId:e,spoolman:t=!1,createdAt:r=null}){let{t:i}=n(),a=d(),{showToast:s}=re(),{data:c,isLoading:l}=f({queryKey:[`spool-usage`,t,e],queryFn:()=>t?o.getSpoolmanSpoolUsageHistory(e):o.getSpoolUsageHistory(e)}),u=c&&t?Kt({created_at:r},c):c,m=p({mutationFn:()=>t?o.clearSpoolmanSpoolUsageHistory(e):o.clearSpoolUsageHistory(e),onSuccess:()=>{a.invalidateQueries({queryKey:[`spool-usage`,t,e]}),s(i(`inventory.historyCleared`),`success`)}});return l?(0,Z.jsx)(`div`,{className:`flex justify-center py-4`,children:(0,Z.jsx)(L,{className:`w-5 h-5 animate-spin text-bambu-green`})}):!u||u.length===0?(0,Z.jsxs)(`div`,{className:`text-center py-4 text-bambu-gray text-sm`,children:[(0,Z.jsx)(ae,{className:`w-5 h-5 mx-auto mb-2 opacity-50`}),i(`inventory.noUsageHistory`)]}):(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,Z.jsx)(`h4`,{className:`text-sm font-medium text-white`,children:i(`inventory.usageHistory`)}),(0,Z.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,Z.jsx)(z,{className:`w-3 h-3 mr-1`}),i(`inventory.clearHistory`)]})]}),(0,Z.jsx)(`div`,{className:`max-h-48 overflow-y-auto space-y-1`,children:u.map(e=>(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-2 rounded bg-bambu-dark/50 text-xs`,children:[(0,Z.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:En(e.created_at)}),e.print_name&&(0,Z.jsx)(`span`,{className:`text-white ml-2 truncate`,title:e.print_name,children:e.print_name})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 flex-shrink-0 ml-2`,children:[(0,Z.jsxs)(`span`,{className:`text-white font-medium`,children:[e.weight_used.toFixed(1),`g`]}),(0,Z.jsxs)(`span`,{className:`text-bambu-gray`,children:[`(`,e.percent_used,`%)`]}),(0,Z.jsx)(`span`,{className:Dn[e.status]||`text-bambu-gray`,children:e.status})]})]},e.id))})]})}var kn={tag_uid:null,tray_uuid:null,tag_type:null,data_origin:null};function An({isOpen:e,onClose:t,spool:r,mode:i,printersWithCalibrations:a=[],currencySymbol:c,onSpoolsCreated:l,spoolmanMode:u=!1,spoolsQueryKey:m=[`inventory-spools`]}){let{t:h}=n(),g=d(),{showToast:_}=re(),v=()=>et(g,m),y=i===`edit`,b=i===`copy`,[x,S]=(0,X.useState)(lt),C=(0,X.useRef)(x);C.current=x;let[w,T]=(0,X.useState)({}),[E,D]=(0,X.useState)(`filament`),[O,k]=(0,X.useState)(!1),[A,ee]=(0,X.useState)(!1),[te,j]=(0,X.useState)(!1),[ne,ie]=(0,X.useState)(!1),[M,ae]=(0,X.useState)([]),[oe,N]=(0,X.useState)(!1),[P,F]=(0,X.useState)(!1),[I,se]=(0,X.useState)(1),[ce,ue]=(0,X.useState)(!1);(0,X.useEffect)(()=>{ue(!1)},[e,r?.id]);let[de,fe]=(0,X.useState)(!1),[me,he]=(0,X.useState)(!1),[ge,_e]=(0,X.useState)([]),[ve,ye]=(0,X.useState)(new Set),[be,xe]=(0,X.useState)(``),[Se,Ce]=(0,X.useState)([]),[we,Ee]=(0,X.useState)([]),[R,De]=(0,X.useState)([]),[Oe,ke]=(0,X.useState)([]),[z,B]=(0,X.useState)([]),[je,Me]=(0,X.useState)([]),[Ne,Pe]=(0,X.useState)([]),[V,H]=(0,X.useState)(!1),[Ie,U]=(0,X.useState)(new Map),[ze,Be]=(0,X.useState)(new Map),[G,Ue]=(0,X.useState)(``),[K,We]=(0,X.useState)(!1),[Ge,Ke]=(0,X.useState)(!1),qe=(0,X.useRef)(new Map),Ye=(0,X.useRef)(new Map),Xe=(0,X.useRef)(new Set),Qe=(0,X.useRef)(new Set),q=(0,X.useRef)(!1),[$e,tt]=(0,X.useState)(!1),[nt,rt]=(0,X.useState)(`all`),it=(e,t,n)=>{for(let r of new Set([...e.keys(),...t.keys()]))e.get(r)!==t.get(r)&&n.add(r)},at=e=>{We(!0),Be(t=>{let n=typeof e==`function`?e(t):e;return it(t,n,Qe.current),n})},st=e=>{We(!0),U(t=>{let n=typeof e==`function`?e(t):e;return it(t,n,Xe.current),n})},ft=a.length>0?a:Ne,pt=Ie.size+ze.size,{data:St=[],isLoading:Ot,error:kt}=f({queryKey:[`spoolman-inventory-filaments`],queryFn:()=>o.getSpoolmanInventoryFilaments(),enabled:u&&e,staleTime:6e4,retry:!1});(0,X.useEffect)(()=>{Me(Tt())},[]),(0,X.useEffect)(()=>{let t=!1;return e&&((async()=>{he(!0);try{let[e,n]=await Promise.allSettled([(async()=>(await o.getCloudStatus()).is_authenticated?{connected:!0,presets:await o.getFilamentPresets()}:{connected:!1,presets:[]})(),(async()=>(await o.orcaCloudStatus()).connected?{connected:!0,presets:(await o.orcaCloudListProfiles()).filament}:{connected:!1,presets:[]})()]);if(t)return;let r=e.status===`fulfilled`&&e.value.connected,i=n.status===`fulfilled`&&n.value.connected,a=e.status===`fulfilled`?e.value.presets:[],s=n.status===`fulfilled`?n.value.presets:[];fe(r||i),_e([...a,...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)}})(),o.getSpoolCatalog().then(Ce).catch(console.error),o.getColorCatalog().then(B).catch(console.error),o.getLocalPresets().then(e=>De(e.filament)).catch(console.error),o.getBuiltinFilaments().then(ke).catch(console.error),o.getLocations().then(e=>Ee(e.map(e=>({id:e.id,name:e.name})))).catch(console.error),a.length===0&&(async()=>{H(!0);try{let e=await o.getPrinters(),n=await Promise.all(e.map(e=>o.getPrinterStatus(e.id).catch(()=>null))),r=await Promise.all(e.map(async(e,t)=>{let r=n[t],i=r?.connected??!1,a=[];return i&&(a=await wt(e.id,r)),{printer:{...e,connected:i},calibrations:a,nozzles:r?.nozzles}}));t||Pe(r)}catch(e){console.error(`Failed to fetch printer calibrations:`,e)}finally{t||H(!1)}})()),()=>{t=!0}},[e,a.length]);let At=(0,X.useMemo)(()=>Et(ge,new Set,R,Oe,ve),[ge,R,Oe,ve]),Y=(0,X.useMemo)(()=>{let e=xt(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]),Nt=(0,X.useMemo)(()=>{let e=z.map(e=>e.material?.trim()).filter(e=>!!e),t=new Set([...mt,...e]);return Array.from(t).sort((e,t)=>e.localeCompare(t))},[z]),Pt=(0,X.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=Dt(t.name);n.brand&&n.material&&e.push({brand:n.brand,material:n.material})}for(let t of R){let n=Dt(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]),Ft=(0,X.useMemo)(()=>{let e=new Map;for(let t of Pt){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(n)||e.set(n,new Set),e.get(n).add(r)}return e},[Pt]),Lt=(0,X.useMemo)(()=>{let e=new Map;for(let t of Pt){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(r)||e.set(r,new Set),e.get(r).add(n)}return e},[Pt]),Rt=(0,X.useMemo)(()=>vt(Y,x.brand),[Y,x.brand]),zt=(0,X.useMemo)(()=>vt(Nt,x.material),[Nt,x.material]),Bt=(0,X.useMemo)(()=>ht(Rt,x.material,Lt),[Rt,x.material,Lt]),Vt=(0,X.useMemo)(()=>ht(zt,x.brand,Ft),[zt,x.brand,Ft]),Ht=(0,X.useMemo)(()=>yt(x.slicer_filament,At),[x.slicer_filament,At]);(0,X.useEffect)(()=>{if(e){if(r){let e=r.rgba&&/^[0-9A-Fa-f]{8}$/.test(r.rgba)?r.rgba:`808080FF`;S({material:r.material||``,subtype:r.subtype||``,brand:r.brand||``,color_name:r.color_name_is_synthesized?``:r.color_name||``,rgba:e,extra_colors:r.extra_colors||``,effect_type:r.effect_type||``,label_weight:r.label_weight||1e3,core_weight:r.core_weight??250,core_weight_catalog_id:r.core_weight_catalog_id??null,weight_used:b?0:r.weight_used||0,slicer_filament:r.slicer_filament||``,note:r.note||``,cost_per_kg:r.cost_per_kg??null,category:r.category||``,low_stock_threshold_pct:r.low_stock_threshold_pct??null,material_number:r.material_number||``,location_id:r.location_id??null,last_dried_at:(()=>{let e=b?null:Ve(r.last_dried_at);return e?He(e):``})(),spoolman_filament_id:null}),xe(r.slicer_filament_name||r.slicer_filament||``),U(Ct(r.k_profiles??[])),ae((r.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(lt),xe(``),U(new Map),ae([]);F(!1),se(1),T({}),D(`filament`),Ue(``),Be(new Map),k(!1),ie(!1),ee(b&&r?.core_weight_is_inherited===!1),j(!1),N(!1),We(!1),Ke(!1),qe.current=new Map,Ye.current=new Map,Xe.current=new Set,Qe.current=new Set,q.current=!1,tt(!1),rt(`all`)}},[e,r,i,b]),(0,X.useEffect)(()=>{if(!e||!r)return;let t=!1;return(u?o.getSpoolmanFilamentPresets:o.getSpoolFilamentPresets)(r.id).then(e=>{if(t)return;let n=new Map;for(let t of e)n.set(gt(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,r?.id,u]);let Ut=!y&&!b;(0,X.useEffect)(()=>{if(!e||!Ut)return;let t=x.material.trim(),n=!1,r=e=>{if(n)return;let t=Ct(e?.k_profiles??[]),r=qe.current,i=Ye.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)||Xe.current.has(e)||(n.set(e,r),i.set(e,r));return qe.current=i,n});let a=new Map;for(let t of e?.presets??[])a.set(gt(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)||Qe.current.has(e)||(t.set(e,r),n.set(e,r));return Ye.current=n,t}),Ke(t.size>0||a.size>0||!!e?.slicer_filament),e?.slicer_filament&&(!C.current.slicer_filament||q.current)?(S(t=>({...t,slicer_filament:e.slicer_filament||``})),xe(e.slicer_filament_name||e.slicer_filament),q.current=!0):q.current&&=(S(e=>({...e,slicer_filament:``})),xe(``),!1)};if(!t){r(null);return}let i=setTimeout(()=>{o.resolveProfileDefault({brand:x.brand.trim(),material:t,subtype:x.subtype.trim(),color_name:x.color_name.trim()}).then(r).catch(e=>{n||console.error(`Failed to look up the profile default:`,e)})},300);return()=>{n=!0,clearTimeout(i)}},[e,Ut,x.brand,x.material,x.subtype,x.color_name]),(0,X.useEffect)(()=>{if(!e||!r||te||x.location_id!=null)return;let t=r.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,r,we,x.location_id,te]);let Wt=(e,t)=>{let n=ot.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`&&(q.current=!1),w[e]&&T(t=>({...t,[e]:void 0}))},Gt=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`)},Kt=e=>{Me(t=>_t(e,t))},qt=p({mutationFn:e=>u?o.createSpoolmanInventorySpool(e):o.createSpool(e),onSuccess:async e=>{e?.id&&(!await hn(e.id)||!await fn(e.id))||(await _n(),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`)}}),Jt=p({mutationFn:({data:e,qty:t})=>u?o.bulkCreateSpoolmanInventorySpools(e,t):o.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 hn(e.id);if(dn)for(let e of r)await fn(e.id);await _n(),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`)}}),Yt=p({mutationFn:e=>u?o.updateSpoolmanInventorySpool(r.id,e):o.updateSpool(r.id,e),onSuccess:async()=>{r?.id&&(!await hn(r.id)||!await fn(r.id))||(await _n(),await v(),_(h(`inventory.spoolUpdated`),`success`),t())},onError:e=>{e instanceof s&&e.status===503?_(h(`inventory.spoolmanUnreachable`),`error`):_(h(`inventory.saveFailed`),`error`)}}),Xt=p({mutationFn:()=>u?o.updateSpoolmanInventorySpool(r.id,kn):o.updateSpool(r.id,kn),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:Zt,isLoading:Qt}=f({queryKey:[`spool-assignments`],queryFn:()=>o.getAssignments(),enabled:e&&y&&!u}),{data:$t,isLoading:en}=f({queryKey:[`spoolman-slot-assignments`,`all`],queryFn:()=>o.getSpoolmanSlotAssignments(),enabled:e&&y&&u}),tn=(()=>{if(r)return u?$t?.find(e=>e.spoolman_spool_id===r.id):Zt?.find(e=>e.spool_id===r.id)})(),nn=u?en:Qt,{data:rn}=f({queryKey:[`inventory-spools`],queryFn:()=>o.getSpools(!0),enabled:e}),{data:an}=Je({enabled:e}),{data:on}=f({queryKey:[`slicerPrinterModels`],queryFn:o.getSlicerPrinterModels,enabled:e,staleTime:1/0}),sn=(()=>{let e=new Set;for(let t of rn??[]){let n=t.category?.trim();n&&e.add(n)}return Array.from(e).sort((e,t)=>e.localeCompare(t))})(),cn=(()=>{let e=new Set;for(let t of rn??[]){let n=t.material_number?.trim();n&&e.add(n)}return Array.from(e).sort((e,t)=>e.localeCompare(t,void 0,{numeric:!0}))})(),ln=an?.low_stock_threshold??20,un=p({mutationFn:async()=>{if(!tn)throw Error(`No assignment`);if(u){if(!r)throw Error(`No spool`);await o.unassignSpoolmanSlot(r.id);return}await o.unassignSpool(tn.printer_id,tn.ams_id,tn.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`)}}),dn=oe||b,fn=async e=>{if(!dn)return!0;let t=u?o.setSpoolmanSpoolSuppliers:o.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}},pn=y||b||K,mn=()=>{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:jt(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}=bt(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}},hn=async e=>{if(!pn)return!0;let t=u?o.saveSpoolmanKProfiles:o.saveSpoolKProfiles,n=u?o.saveSpoolmanFilamentPresets:o.saveSpoolFilamentPresets,{profiles:r,presets:i}=mn();try{await t(e,r)}catch(e){return console.error(`Failed to save K-profiles:`,e),_(h(`inventory.kProfileSaveFailed`),`warning`),!1}try{await n(e,i)}catch(e){return console.error(`Failed to save filament preset overrides:`,e),_(h(`inventory.filamentPresetSaveFailed`),`warning`),!1}return!0},gn=[x.brand,x.material,x.subtype].map(e=>e.trim()).filter(Boolean).join(` `),_n=async()=>{if(!$e||!x.material.trim())return;let{profiles:e,presets:t}=mn(),n=x.slicer_filament||null;try{await o.saveProfileDefault({brand:x.brand.trim(),material:x.material.trim(),subtype:x.subtype.trim(),color_name:nt===`colour`?x.color_name.trim():``,slicer_filament:n,slicer_filament_name:n&&(Ht?.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,X.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 vn=()=>{let e=dt(x,P,u,i);if(!e.isValid){T(e.errors),(e.errors.slicer_filament||e.errors.material||e.errors.brand||e.errors.subtype)&&D(`filament`);return}let t=x.slicer_filament&&(Ht?.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?Yt.mutate(n):I>1?Jt.mutate({data:n,qty:I}):qt.mutate(n)},xn=qt.isPending||Jt.isPending||Yt.isPending||Xt.isPending||un.isPending;return(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:t}),(0,Z.jsxs)(`div`,{className:`relative w-full max-w-5xl mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Z.jsxs)(`h2`,{className:`text-lg font-semibold text-white flex items-baseline gap-2`,children:[h(y?`inventory.editSpool`:b?`inventory.copySpool`:`inventory.addSpool`),y&&r&&(0,Z.jsxs)(`span`,{className:`text-sm font-mono text-bambu-gray`,children:[`#`,r.id]})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,Z.jsx)(Le,{className:`w-5 h-5`})})]}),i===`create`&&(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-2 border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(Re,{className:`w-4 h-4 text-amber-600 dark:text-amber-400`}),(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:h(`inventory.quickAdd`)})]}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>{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,Z.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,Z.jsxs)(`div`,{className:`flex border-b border-bambu-dark-tertiary flex-shrink-0`,children:[(0,Z.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,Z.jsx)(It,{className:`w-4 h-4`}),h(`inventory.filamentInfoTab`)]}),!P&&(0,Z.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,Z.jsx)(pe,{className:`w-4 h-4`}),h(`inventory.colorAndCostTab`)]}),!P&&(0,Z.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,Z.jsx)(Re,{className:`w-4 h-4`}),h(`inventory.printersTab`),pt>0&&(0,Z.jsx)(`span`,{className:`text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green`,children:pt})]})]}),(0,Z.jsxs)(`div`,{className:`p-4 overflow-y-auto flex-1`,style:{scrollbarGutter:`stable`},children:[E===`filament`?(0,Z.jsxs)(`div`,{className:`space-y-6`,children:[u&&!y&&(0,Z.jsx)(`div`,{children:kt?(0,Z.jsx)(`p`,{className:`text-sm text-red-700 dark:text-red-400 px-1`,children:h(`inventory.spoolmanCatalogLoadFailed`)}):(0,Z.jsx)(bn,{filaments:St,isLoading:Ot,selectedId:x.spoolman_filament_id,onSelect:Gt})}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.filamentInfo`)}),(0,Z.jsx)(ut,{formData:x,updateField:Wt,cloudAuthenticated:de,loadingCloudPresets:me,presetInputValue:be,setPresetInputValue:xe,selectedPresetOption:Ht,filamentOptions:At,availableBrands:Rt,availableMaterials:zt,suggestedBrands:Bt,suggestedMaterials:Vt,quickAdd:P,detailsRequired:!P&&!u&&i===`create`,quantity:I,onQuantityChange:se,errors:w})]})]}):E===`appearance`?(0,Z.jsxs)(`div`,{className:`space-y-6`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.color`)}),(0,Z.jsx)(ct,{formData:x,updateField:Wt,recentColors:je,onColorUsed:Kt,catalogColors:z})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h3`,{className:`text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3`,children:h(`inventory.additional`)}),(0,Z.jsx)(J,{formData:x,updateField:Wt,spoolCatalog:Se,currencySymbol:c,availableCategories:sn,availableMaterialNumbers:cn,availableLocations:we,showLastDried:y,onCreateLocation:async e=>{try{let t=await o.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:ln,spoolmanMode:u})]}),(0,Z.jsx)(`div`,{children:(0,Z.jsx)(yn,{links:M,onChange:e=>{ae(e),N(!0)},currencySymbol:c})}),y&&r&&(0,Z.jsx)(`div`,{children:(0,Z.jsx)(On,{spoolId:r.id,spoolman:u,createdAt:r.created_at})})]}):(0,Z.jsx)(wn,{formData:x,printersWithCalibrations:ft,filamentOptions:At,modelPresets:ze,setModelPresets:at,selectedProfiles:Ie,setSelectedProfiles:st,selectedGroupId:G,setSelectedGroupId:Ue,printerModels:on,isLoading:V}),E===`printers`&&(0,Z.jsx)(Tn,{product:gn,colorName:x.color_name,prefilled:Ge,checked:$e,onCheckedChange:tt,scope:nt,onScopeChange:rt,disabled:!x.material.trim()})]}),(0,Z.jsxs)(`div`,{className:`flex gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0`,children:[y&&(0,Z.jsxs)(`div`,{className:`flex gap-2 mr-auto`,children:[(0,Z.jsxs)(W,{variant:`secondary`,onClick:()=>Xt.mutate(),disabled:xn||!(r?.tag_uid||r?.tray_uuid),children:[(0,Z.jsx)(Ae,{className:`w-4 h-4`}),h(`inventory.clearRfid`,`Clear RFID Tag`)]}),tn?(0,Z.jsxs)(W,{variant:`secondary`,onClick:()=>un.mutate(),disabled:xn||nn,children:[(0,Z.jsx)(Fe,{className:`w-4 h-4`}),h(`inventory.unassignSpool`,`Unassign`)]}):(0,Z.jsxs)(W,{variant:`secondary`,onClick:()=>ue(!0),disabled:xn||nn,children:[(0,Z.jsx)(le,{className:`w-4 h-4`}),h(`inventory.assignSpool`,`Assign Spool`)]})]}),(0,Z.jsxs)(`div`,{className:`flex gap-2 ml-auto`,children:[(0,Z.jsx)(W,{variant:`secondary`,onClick:t,children:h(`common.cancel`)}),(0,Z.jsx)(W,{onClick:vn,disabled:xn,children:xn?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}),h(`common.saving`)]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(Te,{className:`w-4 h-4`}),h(y?`common.save`:b?`inventory.copySpool`:`inventory.addSpool`)]})})]})]})]})]}),y&&r&&(0,Z.jsx)(Mt,{isOpen:ce,onClose:()=>ue(!1),spool:r,printerId:null,variant:`dialog`,spoolmanMode:u})]})}var jn={a4:[210,297],letter:[215.9,279.4]},Mn=.01,Nn=15e3;function Pn(e,t,n=1){return Math.ceil((n-1+t)/e)}function Fn(e,t){return new Intl.NumberFormat(t,{maximumFractionDigits:2}).format(e)}var Q=[`label_w_mm`,`label_h_mm`,`cols`,`rows`,`top_mm`,`left_mm`,`col_gap_mm`,`row_gap_mm`],In={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 Ln(e){let t={page:e.page};for(let n of Q)t[n]=String(e[n]);return t}function Rn(e){let t={page:e.page};for(let n of Q){let r=e[n].trim().replace(`,`,`.`),i=Number(r);if(r===``||!Number.isFinite(i))return null;t[n]=i}return t}function zn(e){if(!e||typeof e!=`object`)return!1;let t=e;return(t.page===`a4`||t.page===`letter`)&&Q.every(e=>typeof t[e]==`string`)}function Bn(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 Vn(e){let t=Rn(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]=jn[t.page],[a,o]=Bn(t);return a>r+Mn?{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+Mn?{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 Hn=[{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 Un({layout:e,startingPosition:t=1}){let{t:r}=n(),[i,a]=jn[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,Z.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,Z.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,Z.jsx)(`rect`,{x:0,y:0,width:i,height:a,className:`fill-white stroke-gray-400`,strokeWidth:.6}),o]})}function Wn({draft:e,onChange:t,problem:r,startingPosition:i}){let{t:a,i18n:o}=n(),s=(0,X.useId)(),c=(0,X.useId)(),l=Rn(e),u=r?.params?Object.fromEntries(Object.entries(r.params).map(([e,t])=>[e,Fn(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,Z.jsxs)(`fieldset`,{"data-testid":`label-sheet-editor`,className:`space-y-3`,children:[(0,Z.jsx)(`legend`,{className:`text-sm font-medium text-white`,children:a(`inventory.labels.custom.layoutTitle`,`Sheet layout`)}),(0,Z.jsxs)(`div`,{className:`grid grid-cols-2 gap-x-3 gap-y-2`,children:[(0,Z.jsxs)(`label`,{className:`col-span-2 space-y-0.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:a(`inventory.labels.custom.page`,`Page`)}),(0,Z.jsxs)(`select`,{"data-testid":`label-sheet-page`,value:e.page,onChange:n=>t({...e,page:n.target.value}),className:f,children:[(0,Z.jsx)(`option`,{value:`a4`,children:a(`inventory.labels.custom.pageA4`,`A4 (210 × 297 mm)`)}),(0,Z.jsx)(`option`,{value:`letter`,children:a(`inventory.labels.custom.pageLetter`,`US Letter (215.9 × 279.4 mm)`)})]})]}),Hn.map(({field:n,i18nKey:i,fallback:o,unit:l})=>(0,Z.jsxs)(`label`,{className:`space-y-0.5`,children:[(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray`,children:[a(`inventory.labels.custom.${i}`,o),l&&` (mm)`]}),(0,Z.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,Z.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,Z.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,Z.jsx)(Un,{layout:l,startingPosition:i})]})}var Gn=[{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.`}],Kn={avery_l7160:21,avery_5160:30},qn=[203,300,600],Jn={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`}},Yn=`bambuddy-label-print-options`,Xn=[`labelTemplates`];function Zn(e){return`saved:${e}`}function Qn(e){return e.startsWith(`saved:`)?Number(e.slice(6)):null}function $n(e){return typeof e==`string`?e===`custom`||/^saved:[1-9]\d*$/.test(e)?!0:Gn.some(t=>t.value===e):!1}var $={template:Gn[0].value,fields:{},customDraft:Ln(In),monochrome:!1,format:`pdf`,dpi:300};function er(){let e;try{e=JSON.parse(localStorage.getItem(Yn)??`null`)}catch{return $}if(!e||typeof e!=`object`)return $;let t=e,n={};if(t.fields&&typeof t.fields==`object`)for(let[e,r]of Object.entries(t.fields))!$n(e)||!Array.isArray(r)||(n[e]=r.filter(e=>i.includes(e)));return{template:$n(t.template)?t.template:$.template,fields:n,customDraft:zn(t.customDraft)?t.customDraft:$.customDraft,monochrome:t.monochrome===!0,format:t.format===`png`?`png`:`pdf`,dpi:qn.includes(t.dpi)?t.dpi:$.dpi}}function tr(e){try{localStorage.setItem(Yn,JSON.stringify(e))}catch{}}function nr(e,t){return e.fields[t]??a}function rr(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 ir(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 ar(e){return K(e)}function or(e){return We(e.color_name,e.rgba,e.color_name_is_synthesized)}function sr(e){return`${or(e)??`${e.material}${e.subtype?` ${e.subtype}`:``}`}${e.brand?` · ${e.brand}`:``}`}function cr(e){return[or(e),e.color_name,e.material,e.subtype,e.brand,`#${e.id}`].filter(Boolean).join(` `).toLowerCase()}function lr(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 ur({isOpen:e,onClose:t,availableSpools:r,initialSelectedIds:c,spoolmanMode:l}){let{t:u,i18n:m}=n(),h=Ke(),{showToast:g}=re(),{hasPermission:_}=ze(),v=_(`inventory:update`),y=d(),[b,x]=(0,X.useState)(!1),[S,C]=(0,X.useState)(new Set),[w,T]=(0,X.useState)(``),[E,D]=(0,X.useState)(``),[O,k]=(0,X.useState)(`id`),[A,ee]=(0,X.useState)($),[te,j]=(0,X.useState)(`1`),[ne,ie]=(0,X.useState)(null),[M,ae]=(0,X.useState)(!1),[oe,N]=(0,X.useState)(null),F=(0,X.useRef)(null),[I,se]=(0,X.useState)(null),[ce,le]=(0,X.useState)(null),[ue,de]=(0,X.useState)(!1),fe=(0,X.useRef)(0),pe=f({queryKey:Xn,queryFn:o.getLabelTemplates,enabled:e,staleTime:0}),me=(0,X.useMemo)(()=>pe.data??[],[pe.data]),he=p({mutationFn:({id:e,name:t,layout:n})=>e===void 0?o.createLabelTemplate({name:t,layout:n}):o.updateLabelTemplate(e,{name:t,layout:n}),onSuccess:e=>{y.setQueryData(Xn,(t=[])=>[...t.filter(t=>t.id!==e.id),e]),y.invalidateQueries({queryKey:Xn})},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=>o.deleteLabelTemplate(e),onSuccess:(e,t)=>{y.setQueryData(Xn,(e=[])=>e.filter(e=>e.id!==t)),y.invalidateQueries({queryKey:Xn})},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,X.useEffect)(()=>{if(e){fe.current=Date.now();let e=new Set(r.map(e=>e.id));C(new Set(c.filter(t=>e.has(t)))),T(``),D(``),k(`id`),ee(er()),j(`1`),x(!1),se(null),le(null),de(!1)}},[e]);let _e=(0,X.useMemo)(()=>{let e=[...r];return O===`color`?(e.sort((e,t)=>{let n=lr(e.rgba),r=lr(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)},[r,O]),ve=(0,X.useMemo)(()=>{let e=new Set;for(let t of _e)t.material&&e.add(t.material.toUpperCase());return[...e].sort()},[_e]),ye=(0,X.useMemo)(()=>{let e=w.trim().toLowerCase();return _e.filter(t=>!(E&&(t.material||``).toUpperCase()!==E||e&&!cr(t).includes(e)))},[_e,w,E,h]),be=ye.length>0&&ye.every(e=>S.has(e.id)),Se=A.template,Ce=nr(A,Se),we=Ce.join(`,`),Te=Qn(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?Ln(R.layout):null,z=Ae?Vn(Ae):null,B=Ae&&!z?Rn(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,X.useEffect)(()=>{!e||!Pe||Te===null||me.some(e=>e.id===Te)||(se(null),le(null),de(!1),ee(e=>{let t={...e,template:$.template};return tr(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,X.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 o.previewSpoolmanSpoolLabel(e,t.signal):await o.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,X.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:Kn[Se],Re=Number(te),G=U===void 0||Number.isInteger(Re)&&Re>=1&&Re<=U,Ve=Gn.find(e=>e.value===Se)??Gn[0],He=A.format===`png`&&U!==void 0&&G?Pn(U,Fe,Re):0,Ue=He*A.dpi>Nn,K=!Ie&&!b&&G&&je&&!Ue;function We(e){ee(t=>{let n={...t,...e};return tr(n),n})}function Ge(e){se(null),le(null),We({template:e})}function qe(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:Fn(e.label_w_mm,m.language),height:Fn(e.label_h_mm,m.language),capacity:e.cols*e.rows,cols:e.cols,rows:e.rows})}function Je(e,t){let n=Se;he.mutate({name:e.trim(),layout:t},{onSuccess:e=>{se(null),le(null);let t=Zn(e.id);ee(e=>{let r={...e,template:t,fields:{...e.fields,[t]:nr(e,n)}};return tr(r),r}),g(u(`inventory.labels.custom.saved`,`Template "{{name}}" saved`,{name:e.name}),`success`)}})}function Ye(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 Xe(e){ge.mutate(e.id,{onSuccess:()=>{de(!1),se(null),We({template:$.template}),g(u(`inventory.labels.custom.deleted`,`Template "{{name}}" deleted`,{name:e.name}),`success`)}})}function Ze(e){let t=i.filter(t=>e.includes(t));We({fields:{...A.fields,[Se]:t}})}function Qe(e){Ze(Ce.includes(e)?Ce.filter(t=>t!==e):[...Ce,e])}function q(e){C(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})}function $e(){C(e=>{let t=new Set(e);for(let e of ye)t.add(e.id);return t})}function et(){C(e=>{let t=new Set(e);for(let e of ye)t.delete(e.id);return t})}function tt(){C(new Set)}async function nt(){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 o.printSpoolmanSpoolLabels(e):await o.printSpoolLabels(e);A.format===`pdf`?rr(n):ir(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 rt=e=>`px-2 py-0.5 text-xs rounded-full border transition ${e?`bg-bambu-green text-bambu-dark border-bambu-green`:`bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray`}`;return(0,Z.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-start sm:items-center justify-center p-4 overflow-y-auto`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:t}),(0,Z.jsxs)(`div`,{"data-testid":`label-template-picker-panel`,className:`relative w-full max-w-5xl bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] overflow-clip flex flex-col my-auto`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(xe,{className:`w-5 h-5 text-bambu-green`}),(0,Z.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:u(`inventory.labels.title`,`Print spool labels`)}),Fe>0&&(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[`(`,u(`inventory.labels.selectedCount`,`{{count}} selected`,{count:Fe}),`)`]})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,"aria-label":u(`common.close`,`Close`),children:(0,Z.jsx)(Le,{className:`w-5 h-5`})})]}),(0,Z.jsxs)(`div`,{className:`flex-1 min-h-0 overflow-y-auto md:overflow-hidden grid grid-cols-1 md:grid-cols-2`,children:[(0,Z.jsxs)(`div`,{className:`flex flex-col min-h-0 md:border-r border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`p-4 space-y-2 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`relative`,children:[(0,Z.jsx)(Ee,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none`}),(0,Z.jsx)(`input`,{type:`search`,value: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,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray mr-1`,children:u(`inventory.labels.filterByMaterial`,`Material:`)}),(0,Z.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,Z.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,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray mr-1`,children:u(`inventory.labels.sortBy.label`)}),(0,Z.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,Z.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,Z.jsxs)(`div`,{className:`px-4 pt-3 pb-2 flex items-center justify-between gap-3 flex-wrap`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:u(`inventory.labels.pickSpools`,`Pick which spools to print labels for:`)}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 text-xs`,children:[(0,Z.jsx)(`button`,{type:`button`,onClick:be?et:$e,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,Z.jsx)(`button`,{type:`button`,onClick:tt,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,Z.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,Z.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,Z.jsx)(`ul`,{className:`space-y-0.5`,children:ye.map(e=>{let t=S.has(e.id);return(0,Z.jsx)(`li`,{children:(0,Z.jsxs)(`label`,{className:`flex items-center gap-3 px-2 py-1.5 rounded hover:bg-bambu-dark-tertiary/50 cursor-pointer`,children:[t?(0,Z.jsx)(Oe,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Z.jsx)(ke,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`input`,{type:`checkbox`,checked:t,onChange:()=>q(e.id),className:`sr-only`}),(0,Z.jsx)(`span`,{className:`w-4 h-4 rounded border border-black/20 shrink-0`,style:ar(e.rgba)}),(0,Z.jsx)(`span`,{className:`flex-1 min-w-0 truncate text-sm text-white`,children:sr(e)}),(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray shrink-0`,children:[`#`,e.id]})]})},e.id)})})})]}),(0,Z.jsxs)(`div`,{className:`md:overflow-y-auto p-4 space-y-4 border-t md:border-t-0 border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`space-y-1`,children:[(0,Z.jsx)(`label`,{htmlFor:`label-template`,className:`text-sm font-medium text-white`,children:u(`inventory.labels.template`,`Label size`)}),(0,Z.jsxs)(`select`,{id:`label-template`,"data-testid":`label-template-select`,value:Se,onChange:e=>Ge(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:[Gn.map(e=>(0,Z.jsx)(`option`,{value:e.value,children:u(`inventory.labels.templates.${e.i18nKey}.label`,e.fallbackLabel)},e.value)),me.length>0&&(0,Z.jsx)(`optgroup`,{label:u(`inventory.labels.custom.savedGroup`,`Saved templates`),children:me.map(e=>(0,Z.jsx)(`option`,{value:Zn(e.id),children:e.name},e.id))}),(0,Z.jsx)(`option`,{value:`custom`,children:u(`inventory.labels.custom.option`,`Custom sheet…`)})]}),(0,Z.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?qe(R.layout):null})]}),R&&!I&&(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsx)(Un,{layout:R.layout,startingPosition:G?Re:1}),v&&(0,Z.jsxs)(`div`,{className:`flex justify-center gap-4 text-xs`,children:[(0,Z.jsx)(`button`,{type:`button`,"data-testid":`label-template-edit`,onClick:()=>se({name:R.name,draft:Ln(R.layout)}),className:`text-bambu-green hover:underline`,children:u(`inventory.labels.custom.edit`,`Edit`)}),(0,Z.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,Z.jsxs)(`div`,{className:`space-y-3`,children:[(0,Z.jsx)(dr,{value:I.name,onChange:e=>se({...I,name:e})}),(0,Z.jsx)(Wn,{draft:I.draft,onChange:e=>se({...I,draft:e}),problem:z,startingPosition:G?Re:1}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap justify-end gap-2`,children:[(0,Z.jsx)(W,{variant:`secondary`,onClick:()=>se(null),disabled:he.isPending,children:u(`common.cancel`,`Cancel`)}),(0,Z.jsx)(W,{variant:`secondary`,"data-testid":`label-template-save-as-new`,disabled:!B||!I.name.trim()||he.isPending,onClick:()=>B&&Je(I.name,B),children:u(`inventory.labels.custom.saveAsNew`,`Save as new`)}),(0,Z.jsx)(W,{"data-testid":`label-template-save-changes`,disabled:!B||!I.name.trim()||he.isPending,onClick:()=>B&&Ye(R.id,I.name,B),children:u(`inventory.labels.custom.saveChanges`,`Save changes`)})]})]}),Se===`custom`&&(0,Z.jsxs)(`div`,{className:`space-y-3`,children:[(0,Z.jsx)(Wn,{draft:A.customDraft,onChange:e=>We({customDraft:e}),problem:z,startingPosition:G?Re:1}),v&&(ce===null?(0,Z.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,Z.jsxs)(`div`,{className:`flex flex-wrap items-end gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 min-w-40`,children:(0,Z.jsx)(dr,{value:ce,onChange:le,autoFocus:!0})}),(0,Z.jsx)(W,{variant:`secondary`,onClick:()=>le(null),disabled:he.isPending,children:u(`common.cancel`,`Cancel`)}),(0,Z.jsx)(W,{"data-testid":`label-template-save`,disabled:!B||!ce.trim()||he.isPending,onClick:()=>B&&Je(ce,B),children:u(`inventory.labels.custom.save`,`Save`)})]}))]}),(0,Z.jsxs)(`div`,{className:`space-y-1`,children:[(0,Z.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,Z.jsxs)(`div`,{"data-testid":`label-preview`,className:`relative flex items-center justify-center min-h-40 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary`,children:[V===null?(0,Z.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,Z.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,Z.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,Z.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,Z.jsx)(L,{className:`absolute top-2 right-2 w-4 h-4 animate-spin text-bambu-green`})]}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray`,children:u(`inventory.labels.previewHint`,`Lines that don't fit on this label size are left out.`)})]}),(0,Z.jsxs)(`fieldset`,{className:`space-y-2`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between gap-2`,children:[(0,Z.jsx)(`legend`,{className:`text-sm font-medium text-white`,children:u(`inventory.labels.content`,`Print on the label`)}),(0,Z.jsx)(`button`,{type:`button`,onClick:()=>Ze(a),className:`text-xs text-bambu-green hover:underline`,children:u(`inventory.labels.resetFields`,`Reset to default`)})]}),(0,Z.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 gap-x-3 gap-y-1`,children:i.map(e=>{let t=Ce.includes(e),{i18nKey:n,fallback:r}=Jn[e];return(0,Z.jsxs)(`label`,{className:`flex items-center gap-2 py-0.5 cursor-pointer select-none`,children:[t?(0,Z.jsx)(Oe,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Z.jsx)(ke,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`input`,{type:`checkbox`,"data-testid":`label-field-${e}`,checked:t,onChange:()=>Qe(e),className:`sr-only`}),(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:u(`inventory.labels.fields.${n}`,r)})]},e)})})]}),(0,Z.jsxs)(`div`,{className:`space-y-3`,children:[(0,Z.jsxs)(`label`,{className:`inline-flex items-center gap-2 cursor-pointer select-none`,children:[A.monochrome?(0,Z.jsx)(Oe,{className:`w-4 h-4 text-bambu-green shrink-0`}):(0,Z.jsx)(ke,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,Z.jsx)(`input`,{type:`checkbox`,checked:A.monochrome,onChange:e=>We({monochrome:e.target.checked}),className:`sr-only`}),(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:u(`inventory.labels.monochrome`,`Monochrome (black & white printer)`)}),(0,Z.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,Z.jsxs)(`div`,{className:`flex items-start gap-3`,children:[(0,Z.jsx)(`label`,{htmlFor:`label-starting-position`,className:`text-sm text-white whitespace-nowrap pt-1.5`,children:u(`inventory.labels.startingPosition`,`Starting label position`)}),(0,Z.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,Z.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,Z.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,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-white mr-1`,children:u(`inventory.labels.format`,`Output:`)}),(0,Z.jsx)(`button`,{type:`button`,"data-testid":`label-format-pdf`,"aria-pressed":A.format===`pdf`,onClick:()=>We({format:`pdf`}),className:rt(A.format===`pdf`),children:`PDF`}),(0,Z.jsx)(`button`,{type:`button`,"data-testid":`label-format-png`,"aria-pressed":A.format===`png`,onClick:()=>We({format:`png`}),className:rt(A.format===`png`),children:`PNG`}),A.format===`png`&&(0,Z.jsx)(`select`,{"data-testid":`label-dpi-select`,"aria-label":u(`inventory.labels.dpi`,`Resolution`),value:A.dpi,onChange:e=>We({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:qn.map(e=>(0,Z.jsxs)(`option`,{value:e,children:[e,` dpi`]},e))})]}),A.format===`png`&&(0,Z.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,Z.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,Z.jsxs)(`div`,{className:`flex justify-end gap-2 px-5 py-3 border-t border-bambu-dark-tertiary`,children:[(0,Z.jsx)(W,{variant:`secondary`,onClick:t,disabled:b,children:u(`common.cancel`,`Cancel`)}),(0,Z.jsxs)(W,{"data-testid":`label-print-button`,onClick:nt,disabled:!K,children:[b?(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}):A.format===`pdf`?(0,Z.jsx)(xe,{className:`w-4 h-4`}):(0,Z.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,Z.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:()=>Xe(R),onCancel:()=>de(!1)})]})}function dr({value:e,onChange:t,autoFocus:r=!1}){let{t:i}=n();return(0,Z.jsxs)(`label`,{className:`block space-y-0.5`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:i(`inventory.labels.custom.name`,`Template name`)}),(0,Z.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 fr({onClose:e,onImported:t}){let{t:r}=n(),[i,a]=(0,X.useState)(null),[s,c]=(0,X.useState)(!1),[l,u]=(0,X.useState)(null),[d,f]=(0,X.useState)(!1),[p,m]=(0,X.useState)(!1),[h,g]=(0,X.useState)(null),_=(0,X.useRef)(null),v=async e=>{a(e),u(null),g(null),f(!0);try{let t=await o.importSpoolsCsvPreview(e);u(t)}catch(e){g(e instanceof Error?e.message:r(`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(i){m(!0),g(null);try{t((await o.importSpoolsCsv(i)).created)}catch(e){g(e instanceof Error?e.message:r(`inventory.csv.importError`,`Import failed`)),m(!1)}}},S=e=>e===`valid`?(0,Z.jsx)(ie,{className:`w-4 h-4 text-green-500 flex-shrink-0`}):e===`error`?(0,Z.jsx)(k,{className:`w-4 h-4 text-red-500 flex-shrink-0`}):(0,Z.jsx)(M,{className:`w-4 h-4 text-bambu-gray flex-shrink-0`}),C=l?.valid_count??0;return(0,Z.jsx)(`div`,{className:`fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4`,children:(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg w-full max-w-3xl border border-bambu-dark-tertiary flex flex-col max-h-[90vh]`,children:[(0,Z.jsxs)(`div`,{className:`p-4 border-b border-bambu-dark-tertiary flex items-center justify-between`,children:[(0,Z.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:r(`inventory.csv.modalTitle`,`Import spools from CSV`)}),(0,Z.jsx)(`button`,{onClick:e,className:`p-1 hover:bg-bambu-dark rounded`,children:(0,Z.jsx)(Le,{className:`w-5 h-5 text-bambu-gray`})})]}),(0,Z.jsxs)(`div`,{className:`p-4 space-y-4 overflow-y-auto flex-1`,children:[(0,Z.jsxs)(`div`,{onDragOver:e=>{e.preventDefault(),c(!0)},onDragLeave:e=>{e.preventDefault(),c(!1)},onDrop:b,onClick:()=>_.current?.click(),className:`border-2 border-dashed rounded-lg p-6 text-center cursor-pointer transition-colors ${s?`border-bambu-green bg-bambu-green/10`:`border-bambu-dark-tertiary hover:border-bambu-green/50`}`,children:[(0,Z.jsx)(Ie,{className:`w-9 h-9 mx-auto mb-2 ${s?`text-bambu-green`:`text-bambu-gray`}`}),i?(0,Z.jsxs)(`p`,{className:`text-white font-medium flex items-center justify-center gap-2`,children:[(0,Z.jsx)(I,{className:`w-4 h-4`}),` `,i.name]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`p`,{className:`text-white font-medium`,children:r(`inventory.csv.selectFile`,`Choose a CSV file or drag it here`)}),(0,Z.jsx)(`p`,{className:`text-xs text-bambu-gray/70 mt-1`,children:r(`inventory.csv.dragHint`,`Header: material (required), brand, subtype, color_name, rgba, …`)})]})]}),(0,Z.jsx)(`input`,{ref:_,type:`file`,accept:`.csv,text/csv`,className:`hidden`,onChange:y}),d&&(0,Z.jsxs)(`div`,{className:`flex items-center justify-center gap-2 text-bambu-gray py-4`,children:[(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}),r(`inventory.csv.parsing`,`Reading file…`)]}),h&&(0,Z.jsxs)(`div`,{className:`p-3 bg-red-50 dark:bg-red-500/10 border border-red-300 dark:border-red-500/30 rounded-lg flex items-start gap-3`,children:[(0,Z.jsx)(k,{className:`w-5 h-5 text-red-600 dark:text-red-400 mt-0.5 flex-shrink-0`}),(0,Z.jsx)(`p`,{className:`text-sm text-red-700 dark:text-red-300 break-words`,children:h})]}),l&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`div`,{className:`flex flex-wrap gap-3 text-sm`,children:[(0,Z.jsx)(`span`,{className:`px-2 py-1 rounded bg-green-50 dark:bg-green-500/10 text-green-700 dark:text-green-400`,children:r(`inventory.csv.validCount`,`{{count}} valid`,{count:l.valid_count})}),(0,Z.jsx)(`span`,{className:`px-2 py-1 rounded bg-red-50 dark:bg-red-500/10 text-red-700 dark:text-red-400`,children:r(`inventory.csv.errorCount`,`{{count}} error`,{count:l.error_count})}),(0,Z.jsx)(`span`,{className:`px-2 py-1 rounded bg-bambu-dark text-bambu-gray`,children:r(`inventory.csv.skippedCount`,`{{count}} skipped`,{count:l.skipped_count})})]}),l.warnings.length>0&&(0,Z.jsx)(`div`,{className:`p-3 bg-yellow-50 dark:bg-yellow-500/10 border border-yellow-300 dark:border-yellow-500/30 rounded-lg space-y-1`,children:l.warnings.map((e,t)=>(0,Z.jsx)(`p`,{className:`text-xs text-yellow-700 dark:text-yellow-300`,children:e},t))}),l.rows.length>0&&(0,Z.jsx)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`max-h-72 overflow-y-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full text-sm`,children:[(0,Z.jsx)(`thead`,{className:`bg-bambu-dark sticky top-0`,children:(0,Z.jsxs)(`tr`,{className:`text-left text-bambu-gray`,children:[(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:r(`inventory.csv.colRow`,`Row`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:`#`}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:r(`inventory.csv.colStatus`,`Status`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:r(`inventory.material`,`Material`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:r(`inventory.brand`,`Brand`)}),(0,Z.jsx)(`th`,{className:`px-3 py-2 font-medium`,children:r(`inventory.csv.colColor`,`Color`)})]})}),(0,Z.jsx)(`tbody`,{children:l.rows.map(e=>(0,Z.jsxs)(`tr`,{className:`border-t border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`td`,{className:`px-3 py-2 text-bambu-gray`,children:e.row_number}),(0,Z.jsx)(`td`,{className:`px-3 py-2 text-white`,children:e.keeps_id??`—`}),(0,Z.jsx)(`td`,{className:`px-3 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[S(e.status),e.status===`error`&&e.reason&&(0,Z.jsx)(`span`,{className:`text-xs text-red-700 dark:text-red-400 break-words`,children:e.reason})]})}),(0,Z.jsx)(`td`,{className:`px-3 py-2 text-white`,children:e.material||`—`}),(0,Z.jsx)(`td`,{className:`px-3 py-2 text-white`,children:e.brand||`—`}),(0,Z.jsx)(`td`,{className:`px-3 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[e.rgba&&(0,Z.jsx)(`span`,{className:`inline-block w-4 h-4 rounded-full border border-bambu-dark-tertiary flex-shrink-0`,style:K(e.rgba)}),(0,Z.jsx)(`span`,{className:`text-white`,children:e.color_name||`—`}),e.resolved_color&&!e.cross_material_color&&(0,Z.jsx)(`span`,{title:r(`inventory.csv.colorResolved`,`Color filled from catalog`),children:(0,Z.jsx)(U,{className:`w-3.5 h-3.5 text-bambu-green flex-shrink-0`})}),e.cross_material_color&&(0,Z.jsx)(`span`,{title:r(`inventory.csv.colorCrossMaterial`,`Color taken from a different material — no exact match in catalog`),children:(0,Z.jsx)(H,{className:`w-3.5 h-3.5 text-yellow-500 flex-shrink-0`})}),e.duplicate_of_existing&&(0,Z.jsx)(`span`,{title:r(`inventory.csv.duplicateExisting`,`A spool with this material, brand and color already exists — it will still be imported as a new spool`),children:(0,Z.jsx)(N,{className:`w-3.5 h-3.5 text-amber-600 dark:text-amber-400 flex-shrink-0`})})]})})]},e.row_number))})]})})})]})]}),(0,Z.jsxs)(`div`,{className:`p-4 border-t border-bambu-dark-tertiary flex justify-end gap-2`,children:[(0,Z.jsx)(W,{variant:`secondary`,onClick:e,disabled:p,children:r(`common.cancel`)}),(0,Z.jsx)(W,{onClick:x,disabled:!l||C===0||p,children:p?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(L,{className:`w-4 h-4 mr-2 animate-spin`}),r(`inventory.csv.importing`,`Importing…`)]}):C>0?r(`inventory.csv.importValidRows`,`Import {{count}} valid rows`,{count:C}):r(`inventory.csv.noValidRows`,`No valid rows`)})]})]})})}function pr({open:e,onClose:t}){let{t:r}=n(),{showToast:i}=re(),a=d(),{data:c=[],isLoading:l,isError:u}=f({queryKey:q,queryFn:o.getSuppliers,enabled:e}),[p,m]=(0,X.useState)(``),[h,g]=(0,X.useState)(!1),[_,v]=(0,X.useState)(null),[y,b]=(0,X.useState)(``),[x,S]=(0,X.useState)(``),[C,w]=(0,X.useState)(``),[T,D]=(0,X.useState)(``),[O,k]=(0,X.useState)(!1),[A,ee]=(0,X.useState)(null);if((0,X.useEffect)(()=>{if(!e)return;let n=e=>{e.key===`Escape`&&t()};return window.addEventListener(`keydown`,n),()=>window.removeEventListener(`keydown`,n)},[e,t]),!e)return null;let 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()){i(r(`inventory.suppliers.nameRequired`),`error`);return}k(!0);try{await o.createSupplier(ne()),await $e(a),g(!1),j(),i(r(`inventory.suppliers.added`),`success`)}catch(e){console.error(`SuppliersModal.handleAdd failed:`,e),i(r(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()){i(r(`inventory.suppliers.nameRequired`),`error`);return}k(!0);try{await o.updateSupplier(e,ne()),await $e(a),ae(),i(r(`inventory.suppliers.updated`),`success`)}catch(e){console.error(`SuppliersModal.handleUpdate failed:`,e),i(r(e instanceof s&&e.status===409?`inventory.suppliers.duplicateName`:`inventory.suppliers.updateFailed`),`error`)}finally{k(!1)}},N=async()=>{if(A)try{await o.deleteSupplier(A.id),await $e(a),i(r(`inventory.suppliers.deleted`),`success`)}catch(e){console.error(`SuppliersModal.handleDelete failed:`,e),e instanceof s&&e.status===409?i(r(`inventory.suppliers.deleteBlocked`,{count:A.spool_count}),`error`):i(r(`inventory.suppliers.deleteFailed`),`error`)}finally{ee(null)}},P=(0,Z.jsxs)(`div`,{className:`space-y-2`,children:[(0,Z.jsx)(`input`,{type:`text`,className:`w-full px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:r(`inventory.suppliers.namePlaceholder`),value:y,maxLength:200,onChange:e=>b(e.target.value)}),(0,Z.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Z.jsx)(`input`,{type:`text`,className:`flex-1 min-w-0 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:r(`inventory.suppliers.websitePlaceholder`),value:x,maxLength:500,onChange:e=>S(e.target.value)}),(0,Z.jsx)(`input`,{type:`text`,className:`w-40 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:r(`inventory.suppliers.customerNumberPlaceholder`),value:C,maxLength:100,onChange:e=>w(e.target.value)})]}),(0,Z.jsx)(`input`,{type:`text`,className:`w-full px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:r(`inventory.suppliers.notePlaceholder`),value:T,maxLength:500,onChange:e=>D(e.target.value)})]});return(0,Z.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 bg-black/60`,onClick:t}),(0,Z.jsxs)(`div`,{className:`relative w-full max-w-3xl mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col`,role:`dialog`,"aria-modal":`true`,"aria-labelledby":`suppliers-modal-title`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between gap-4 px-6 py-4 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(Vt,{className:`w-5 h-5 text-bambu-gray`}),(0,Z.jsx)(`h2`,{id:`suppliers-modal-title`,className:`text-lg font-semibold text-white`,children:r(`inventory.suppliers.title`)}),(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[`(`,c.length,`)`]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsxs)(`button`,{onClick:()=>{g(!0),v(null),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,Z.jsx)(be,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:r(`common.add`)})]}),(0,Z.jsx)(`button`,{onClick:t,className:`p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors`,"aria-label":r(`common.close`),children:(0,Z.jsx)(Le,{className:`w-5 h-5`})})]})]}),(0,Z.jsxs)(`div`,{className:`flex-1 min-h-0 overflow-y-auto px-6 py-4 space-y-4`,children:[(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:r(`inventory.suppliers.description`)}),(0,Z.jsxs)(`div`,{className:`relative`,children:[(0,Z.jsx)(Ee,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray`}),(0,Z.jsx)(`input`,{type:`text`,className:`w-full pl-10 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none`,placeholder:r(`inventory.suppliers.search`),value:p,onChange:e=>m(e.target.value)})]}),h&&(0,Z.jsxs)(`div`,{className:`p-4 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary space-y-3`,children:[(0,Z.jsx)(`h3`,{className:`text-sm font-medium text-white`,children:r(`inventory.suppliers.addNew`)}),P,(0,Z.jsxs)(`div`,{className:`flex gap-2 justify-end`,children:[(0,Z.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:r(`common.cancel`)}),(0,Z.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,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(E,{className:`w-4 h-4`}),r(`common.add`)]})]})]}),l?(0,Z.jsxs)(`div`,{className:`flex items-center justify-center py-8 text-bambu-gray`,children:[(0,Z.jsx)(L,{className:`w-5 h-5 animate-spin mr-2`}),r(`common.loading`)]}):u?(0,Z.jsxs)(`div`,{className:`flex items-center justify-center gap-2 py-8 text-sm text-red-400`,children:[(0,Z.jsx)(H,{className:`w-4 h-4`}),r(`inventory.suppliers.loadFailed`)]}):(0,Z.jsx)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg`,children:(0,Z.jsxs)(`table`,{className:`w-full text-sm`,children:[(0,Z.jsx)(`thead`,{className:`bg-bambu-dark sticky top-0`,children:(0,Z.jsxs)(`tr`,{children:[(0,Z.jsx)(`th`,{className:`px-4 py-2 text-left text-bambu-gray font-medium`,children:r(`common.name`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 text-left text-bambu-gray font-medium hidden sm:table-cell`,children:r(`inventory.suppliers.customerNumber`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 text-right text-bambu-gray font-medium w-20`,children:r(`inventory.suppliers.spools`)}),(0,Z.jsx)(`th`,{className:`px-4 py-2 w-24`})]})}),(0,Z.jsx)(`tbody`,{children:te.length===0?(0,Z.jsx)(`tr`,{children:(0,Z.jsx)(`td`,{colSpan:4,className:`px-4 py-8 text-center text-bambu-gray`,children:r(p?`inventory.suppliers.noMatch`:`inventory.suppliers.empty`)})}):te.map(e=>(0,Z.jsx)(`tr`,{className:`border-t border-bambu-dark-tertiary hover:bg-bambu-dark align-top`,children:_===e.id?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`td`,{className:`px-4 py-2`,colSpan:3,children:P}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex justify-end gap-1`,children:[(0,Z.jsx)(`button`,{onClick:()=>oe(e.id),disabled:O,className:`p-1.5 rounded hover:bg-green-500/20 text-green-500`,children:O?(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(E,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:ae,className:`p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray`,children:(0,Z.jsx)(Le,{className:`w-4 h-4`})})]})})]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`td`,{className:`px-4 py-2 text-white`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-1.5`,children:[(0,Z.jsx)(`span`,{children:e.name}),e.website&&(0,Z.jsx)(`a`,{href:e.website,target:`_blank`,rel:`noopener noreferrer`,className:`text-bambu-gray hover:text-bambu-green`,title:e.website,children:(0,Z.jsx)(F,{className:`w-3.5 h-3.5`})})]}),e.note&&(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-0.5`,children:e.note})]}),(0,Z.jsx)(`td`,{className:`px-4 py-2 text-bambu-gray hidden sm:table-cell`,children:e.customer_number||`-`}),(0,Z.jsx)(`td`,{className:`px-4 py-2 text-right font-mono text-bambu-gray`,children:e.spool_count}),(0,Z.jsx)(`td`,{className:`px-4 py-2`,children:(0,Z.jsxs)(`div`,{className:`flex justify-end gap-1`,children:[(0,Z.jsx)(`button`,{onClick:()=>{M(e),g(!1)},className:`p-1.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white`,children:(0,Z.jsx)(ye,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>ee(e),className:`p-1.5 rounded bg-red-500/10 hover:bg-red-500/20 text-red-500`,children:(0,Z.jsx)(z,{className:`w-4 h-4`})})]})})]})},e.id))})]})})]})]}),A&&(0,Z.jsx)(Be,{title:r(`inventory.suppliers.deleteTitle`),message:A.spool_count>0?r(`inventory.suppliers.deleteConfirmReferenced`,{name:A.name,count:A.spool_count}):r(`inventory.suppliers.deleteConfirm`,{name:A.name}),confirmText:r(`common.delete`),variant:`danger`,onConfirm:N,onCancel:()=>ee(null)})]})}var mr=[{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 hr({value:e,onChange:t,options:r,allowCustom:i,placeholderKey:a,disabled:o}){let{t:s}=n(),c=(0,X.useRef)(null),[l,u]=(0,X.useState)(!1),[d,f]=(0,X.useState)(``);(0,X.useEffect)(()=>{if(!l)return;let e=e=>{c.current&&!c.current.contains(e.target)&&(u(!1),f(``))},t=e=>{e.key===`Escape`&&(u(!1),f(``))};return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t)}},[l]);let p=l?d:r.find(t=>t.value===e)?.label??e,m=(0,X.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,Z.jsxs)(`div`,{className:`relative`,ref:c,children:[(0,Z.jsx)(`input`,{type:`text`,disabled:o,value:p,onChange:e=>{f(e.target.value),u(!0),i&&t(e.target.value)},onFocus:()=>{u(!0),f(``)},placeholder:a?s(a):void 0,className:`w-full px-3 py-2 pr-9 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none`}),(0,Z.jsx)(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,Z.jsxs)(`div`,{className:`absolute z-50 left-0 right-0 mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto`,children:[m.length===0&&!i&&(0,Z.jsx)(`div`,{className:`px-3 py-2 text-sm text-bambu-gray`,children:s(`inventory.noResults`)}),m.map(n=>(0,Z.jsx)(`button`,{type:`button`,className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${e===n.value?`bg-bambu-green/10 text-bambu-green`:`text-white`}`,onClick:()=>{t(n.value),u(!1),f(``)},children:n.label},n.value)),i&&h&&(0,Z.jsx)(`button`,{type:`button`,className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary`,onClick:()=>{t(d.trim()),u(!1),f(``)},children:s(`inventory.bulk.useCustom`,{value:d.trim()})})]})]})}function gr(...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 _r({isOpen:e,selectedCount:t,isPending:r,availableLocations:i,availableMaterials:a,availableSubtypes:s,availableBrands:c,availableCategories:l,availableMaterialNumbers:u,availableSlicerFilaments:d,availableSlicerFilamentNames:p,spoolmanMode:m=!1,onClose:h,onApply:g}){let{t:_}=n(),v=(0,X.useMemo)(()=>mr.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 o.getCloudStatus()).is_authenticated){let t=await o.getFilamentPresets();e.push(...t)}}catch{}try{if((await o.orcaCloudStatus()).connected){let t=await o.orcaCloudListProfiles();e.push(...t.filament)}}catch{}return e}}),{data:b}=f({queryKey:[`bulk-edit-local-presets`],enabled:e,staleTime:300*1e3,queryFn:o.getLocalPresets}),{data:x=[]}=f({queryKey:[`builtin-filaments`],enabled:e,staleTime:300*1e3,queryFn:o.getBuiltinFilaments}),S=(0,X.useMemo)(()=>Et(y,new Set,b?.filament??[],x),[y,b,x]),[C,w]=(0,X.useState)({}),T=(0,X.useMemo)(()=>gr(mt,a).map(e=>({value:e,label:e})),[a]),E=(0,X.useMemo)(()=>gr(kt,s).map(e=>({value:e,label:e})),[s]),D=(0,X.useMemo)(()=>gr(Ot,c).map(e=>({value:e,label:e})),[c]),O=(0,X.useMemo)(()=>gr(l).map(e=>({value:e,label:e})),[l]),k=(0,X.useMemo)(()=>gr(u).map(e=>({value:e,label:e})),[u]),A=(0,X.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,X.useMemo)(()=>{let e=S.map(e=>({value:e.displayName,label:e.displayName})),t=p.filter(t=>!e.some(e=>e.value===t)).map(e=>({value:e,label:e}));return[...e,...t].sort((e,t)=>e.label.localeCompare(t.label))},[S,p]),te=(0,X.useMemo)(()=>i.map(e=>({value:String(e.id),label:e.name})),[i]);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,Z.jsx)(hr,{value:t,onChange:t=>{t===``?ne(e.id):j(e.id,t)},options:ae(e.id),allowCustom:e.type===`searchable`,disabled:r});if(e.type===`textarea`)return(0,Z.jsx)(`textarea`,{disabled:r,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(/^#/,``),i=n.length===6?`${n}FF`:n,a=t.trim()!==``&&!/^[0-9A-Fa-f]{8}$/.test(i);return(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`input`,{type:`color`,disabled:r,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,Z.jsx)(`input`,{type:`text`,disabled:r,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,Z.jsx)(`p`,{className:`mt-1 text-xs text-red-600 dark:text-red-400`,children:_(`inventory.bulk.invalidHex`)})]})}return(0,Z.jsx)(`input`,{type:e.type===`number`?`number`:`text`,disabled:r,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,Z.jsx)(`div`,{className:`fixed inset-0 bg-black/50 flex items-center justify-center p-4 z-50`,onClick:r?void 0:h,children:(0,Z.jsxs)(`div`,{className:`w-full max-w-3xl bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg max-h-[90vh] flex flex-col`,onClick:e=>e.stopPropagation(),children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between p-5 border-b border-bambu-dark-tertiary`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:_(`inventory.bulk.editTitle`)}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray mt-0.5`,children:_(`inventory.bulk.editSubtitle`,{count:t})})]}),(0,Z.jsx)(`button`,{onClick:h,disabled:r,className:`p-1 text-bambu-gray hover:text-white transition-colors`,"aria-label":_(`common.close`),children:(0,Z.jsx)(Le,{className:`w-5 h-5`})})]}),(0,Z.jsx)(`p`,{className:`px-5 pt-4 text-xs text-bambu-gray`,children:_(`inventory.bulk.editHint`)}),(0,Z.jsx)(`div`,{className:`flex-1 overflow-y-auto p-5 space-y-3`,children:v.map(e=>{let t=C[e.id]!==void 0;return(0,Z.jsxs)(`div`,{className:`flex items-start gap-3 rounded-md p-2 transition-colors ${t?`bg-bambu-green/5 border border-bambu-green/30`:`border border-transparent`}`,children:[(0,Z.jsx)(`div`,{className:`pt-2`,children:(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,checked:t,onChange:t=>{t.target.checked?j(e.id,``):ne(e.id)},"aria-label":_(`inventory.bulk.toggleField`)})}),(0,Z.jsxs)(`div`,{className:`flex-1`,children:[(0,Z.jsx)(`label`,{className:`block text-sm text-bambu-gray mb-1`,children:_(e.labelKey)}),oe(e)]})]},e.id)})}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-3 p-5 border-t border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:_(`inventory.bulk.changeCount`,{count:Object.keys(re).length})}),(0,Z.jsxs)(`div`,{className:`ml-auto flex gap-2`,children:[(0,Z.jsx)(W,{variant:`secondary`,onClick:h,disabled:r,children:_(`common.cancel`)}),(0,Z.jsx)(W,{onClick:()=>g(re),disabled:!ie||r||M,children:r?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(L,{className:`w-4 h-4 mr-2 animate-spin`}),_(`inventory.bulk.applyPending`)]}):_(`inventory.bulk.applyButton`,{count:t})})]})]})]})})}function vr(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,last_scale_weight:null,last_weighed_at:null}}function yr(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 br(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 xr=`bambuddy-inventory-columns`,Sr=`none`.padStart(65,`_`);function Cr(e){return(e||``).toLowerCase().replace(/\d+/g,e=>e.padStart(64,`0`))}var wr=[{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 Tr(){try{let e=localStorage.getItem(xr);if(e){let t=JSON.parse(e),n=new Set(wr.map(e=>e.id)),r=new Set(t.map(e=>e.id)),i=[...t.filter(e=>n.has(e.id))];for(let e of wr){if(r.has(e.id))continue;let t=wr.indexOf(e),n=i.length;for(let e=t-1;e>=0;e--){let t=i.findIndex(t=>t.id===wr[e].id);if(t!==-1){n=t+1;break}}i.splice(n,0,e)}return i}}catch{}return wr.map(e=>({...e}))}function Er(e){try{localStorage.setItem(xr,JSON.stringify(e))}catch{}}function Dr(e,t=!1){return t&&e>=1e3?`${(e/1e3).toFixed(1)}kg`:`${Math.round(e)}g`}var Or={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 kr(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 Ar(e,t=`system`){if(!e)return`-`;let n=Ve(e);return n?G(n,t):`-`}var jr={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`)},Mr={id:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm font-medium text-white`,children:e.id}),added_time:({spool:e,dateFormat:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Ar(e.created_at,t)}),encode_time:({spool:e,dateFormat:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Ar(e.encode_time,t)}),last_used_time:({spool:e,dateFormat:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.last_used?Ar(e.last_used,t):`Never`}),last_dried:({spool:e,dateFormat:t})=>{if(!e.last_dried_at)return(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`});let n=kr(e);return(0,Z.jsxs)(`div`,{className:`text-sm text-bambu-gray`,children:[(0,Z.jsx)(`div`,{children:Ar(e.last_dried_at,t)}),n&&(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray/60`,children:n})]})},rgba:({spool:e})=>(0,Z.jsx)(`div`,{className:`flex items-center justify-center`,children:(0,Z.jsx)(Xe,{rgba:e.rgba,extraColors:e.extra_colors,effectType:e.effect_type,effectSize:`table`,subtype:e.subtype})}),material:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:e.material}),subtype:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.subtype||`-`}),color_name:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:We(e.color_name,e.rgba,e.color_name_is_synthesized)||`-`}),brand:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.brand||`-`}),slicer_filament:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,title:e.slicer_filament||void 0,children:e.slicer_filament_name||e.slicer_filament||`-`}),location:({spool:e,assignmentMap:t})=>{let n=t[e.id];return n?(0,Z.jsxs)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-purple-100 dark:bg-purple-500/20 text-purple-700 dark:text-purple-400`,children:[n.printer_name||`Printer ${n.printer_id}`,` `,Ue(n),n.ams_label?` (${n.ams_label})`:``]}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`})},storage_location:({spool:e})=>e.storage_location?(0,Z.jsx)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-blue-100 dark:bg-blue-500/20 text-blue-700 dark:text-blue-400`,children:e.storage_location}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}),temperature:({spool:e,locationReadingsMap:t,t:n,colorizeLocationSensors:r,locationSensorAboveColor:i,locationSensorBelowColor:a,locationSensorOptimalColor:o})=>{let s=e.location_id?t[e.location_id]?.find(e=>e.device_class===`temperature`):void 0;return s?(0,Z.jsx)(`span`,{title:s.name,className:`text-sm ${Wr(s,r,i,a,o)}`,children:Ur(s,n)}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},humidity:({spool:e,locationReadingsMap:t,t:n,colorizeLocationSensors:r,locationSensorAboveColor:i,locationSensorBelowColor:a,locationSensorOptimalColor:o})=>{let s=e.location_id?t[e.location_id]?.find(e=>e.device_class===`humidity`):void 0;return s?(0,Z.jsx)(`span`,{title:s.name,className:`text-sm ${Wr(s,r,i,a,o)}`,children:Ur(s,n)}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},battery:({spool:e,locationReadingsMap:t,t:n,colorizeLocationSensors:r,locationSensorAboveColor:i,locationSensorBelowColor:a,locationSensorOptimalColor:o})=>{let s=e.location_id?t[e.location_id]?.find(e=>e.device_class===`battery`):void 0;return s?(0,Z.jsx)(`span`,{title:s.name,className:`text-sm ${Wr(s,r,i,a,o)}`,children:Ur(s,n)}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},label_weight:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:Dr(e.label_weight)}),net:({remaining:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-white`,children:Dr(e)}),gross:({spool:e,remaining:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:Dr(t+e.core_weight)}),used:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.weight_used>0?Dr(e.weight_used):`-`}),printed_total:()=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`}),printed_since_weight:()=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`}),note:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray max-w-[150px] truncate block`,title:e.note||void 0,children:e.note||`-`}),material_number:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.material_number||`-`}),suppliers:({spool:e})=>{let t=e.suppliers??[];return t.length===0?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Z.jsx)(`div`,{className:`flex flex-wrap gap-1 max-w-[220px]`,children:[...t].sort((e,t)=>Number(t.is_purchase_source)-Number(e.is_purchase_source)).map(e=>(0,Z.jsx)(`span`,{title:e.supplier_article_number||void 0,className:`inline-flex items-center px-1.5 py-0.5 rounded-full text-[10px] font-medium ${e.is_purchase_source?`bg-bambu-green/20 text-bambu-green`:`bg-bambu-gray/10 text-bambu-gray`}`,children:e.supplier_name},e.id))})},pa_k:({spool:e})=>(e.k_profiles?.length??0)===0?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Z.jsx)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-bambu-green/20 text-bambu-green`,children:`K`}),tag_id:({spool:e})=>{let t=e.tag_uid||e.tray_uuid;return t?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray font-mono`,title:t,children:t.length>12?`${t.slice(0,6)}...${t.slice(-4)}`:t}):(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,children:`-`})},data_origin:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.data_origin||`-`}),tag_type:({spool:e})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.tag_type||`-`}),stock:({spool:e,t})=>e.slicer_filament?(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:`-`}):(0,Z.jsx)(`span`,{className:`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-amber-100 dark:bg-amber-500/20 text-amber-700 dark:text-amber-400`,children:t(`inventory.stock`)}),remaining:({remaining:e,pct:t})=>(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${t>50?`bg-bambu-green`:t>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(t,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(e),`g`]})]}),spool_name:({spool:e,catalogMap:t})=>(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:(e.core_weight_catalog_id==null?void 0:t[e.core_weight_catalog_id])?.name||`-`}),cost_per_kg:({spool:e,currencySymbol:t,t:n})=>{let{rate:r,fromFilament:i}=Ft(e);return(0,Z.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[r==null?`-`:`${t}${r.toFixed(2)}`,i&&(0,Z.jsx)(`span`,{className:`ml-1 text-xs opacity-70`,children:n(`inventory.costFromFilament`)})]})},weight_check:({spool:e,onSyncWeight:t})=>{let n=e.last_scale_weight;if(n==null)return(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray/50`,title:`No scale measurement`,children:`-`});let r=e.core_weight||0,i=Math.max(0,e.label_weight-e.weight_used)+r,a,o;n<r?(a=n-r,o=!0):(a=n-i,o=Math.abs(a)<=50);let s=a>0?`+${Math.round(a)}`:`${Math.round(a)}`,c=o?`Scale: ${Math.round(n)}g\nCalculated: ${Math.round(i)}g\nDifference: ${s}g (within tolerance)`:`Scale: ${Math.round(n)}g\nCalculated: ${Math.round(i)}g\nDifference: ${s}g (mismatch!)`;return(0,Z.jsxs)(`div`,{className:`flex items-center gap-1 text-sm font-medium ${o?`text-green-700 dark:text-green-400`:`text-yellow-700 dark:text-yellow-400`}`,title:c,children:[(0,Z.jsxs)(`span`,{children:[Math.round(n),`g`]}),o?(0,Z.jsx)(E,{className:`w-3 h-3`}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(H,{className:`w-3 h-3`}),t&&(0,Z.jsx)(`button`,{type:`button`,onClick:n=>{n.stopPropagation(),n.preventDefault(),t(e)},className:`p-1 hover:bg-bambu-green/20 rounded transition-colors text-bambu-green`,title:`Sync: trust scale weight and reset tracking`,children:(0,Z.jsx)(Ce,{className:`w-3.5 h-3.5`})})]})]})}},Nr={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=>Ge(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_label?` (${n.ams_label})`:``;return`${n.printer_name||``} ${Ue(n)}${r}`},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=>Cr(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=>Ft(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},Pr=`bambuddy-inventory-sort`;function Fr(){try{let e=localStorage.getItem(Pr);if(e)return JSON.parse(e)}catch{}return null}function Ir(e){try{e?localStorage.setItem(Pr,JSON.stringify(e)):localStorage.removeItem(Pr)}catch{}}function Lr(){let{data:e}=f({queryKey:[`spoolman-settings`],queryFn:o.getSpoolmanSettings,staleTime:300*1e3}),t=e!==void 0;return(0,Z.jsx)(Rr,{spoolmanMode:e?.spoolman_enabled===`true`&&!!e?.spoolman_url,spoolmanModeReady:t})}function Rr({spoolmanMode:e=!1,spoolmanModeReady:t=!0}){let{t:r}=n(),i=Ke(),a=d(),{showToast:c}=re(),{hasPermission:g,loading:_}=ze(),S=!_&&g(`inventory:forecast_read`),[C,T]=u(),[E,O]=(0,X.useState)(null),k=(0,X.useRef)(!1),[A,ie]=(0,X.useState)(!1),[M,N]=(0,X.useState)(null),[F,I]=(0,X.useState)(null),[le,pe]=(0,X.useState)(!1),[he,ve]=(0,X.useState)(!1),[ye,Ce]=(0,X.useState)(!1),[Te,R]=(0,X.useState)(!1),[De,Oe]=(0,X.useState)(`active`),[ke,Ae]=(0,X.useState)(`all`),[B,Me]=(0,X.useState)(``),[Ne,Pe]=(0,X.useState)(``),[V,Fe]=(0,X.useState)(``),[U,Re]=(0,X.useState)(``),[G,Ve]=(0,X.useState)(``),[He,Ue]=(0,X.useState)(``),[K,Ge]=(0,X.useState)(`all`),[Xe,Ze]=(0,X.useState)(``),[q,$e]=(0,X.useState)(`table`),[tt,nt]=(0,X.useState)(Fr),[it,at]=(0,X.useState)(Tr),[ot,st]=(0,X.useState)(!1),[ct,lt]=(0,X.useState)(()=>{try{return localStorage.getItem(`bambuddy-inventory-group`)===`true`}catch{return!1}}),[ut,dt]=(0,X.useState)(new Set),[J,ft]=(0,X.useState)(new Set),[pt,mt]=(0,X.useState)(!1),[ht,gt]=(0,X.useState)(null),_t=(0,X.useCallback)(e=>{ft(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[]),vt=(0,X.useCallback)(()=>ft(new Set),[]);(0,X.useEffect)(()=>{ft(new Set)},[De,ke,B,Ne,V,U,G,He,K,Xe]);let[yt,bt]=(0,X.useState)(0),[xt,St]=(0,X.useState)(()=>{try{let e=localStorage.getItem(`bambuddy-inventory-pageSize`);if(e){let t=Number(e);if([15,30,50,100,-1].includes(t))return t}}catch{}return 15}),{data:Ct}=Je(),wt=Ct?.date_format||`system`,Tt=(Ct?.location_sensor_poll_interval||120)*1e3,{colorize:Et,aboveColor:Dt,belowColor:Ot,optimalColor:kt}=_e(),At=e?[`spoolman-inventory-spools`]:[`inventory-spools`],jt=()=>et(a,At),{data:Y,isLoading:Mt}=f({queryKey:At,queryFn:()=>e?o.getSpoolmanInventorySpools(!0):o.getSpools(!0),refetchInterval:3e4}),Ft=(0,X.useCallback)(async()=>{ve(!0);try{await o.exportSpoolsCsv()}catch(e){c(e instanceof Error?e.message:r(`inventory.csv.exportError`,`Export failed`),`error`)}finally{ve(!1)}},[c,r]),It=e?r(`inventory.csv.spoolmanHint`,`In Spoolman mode, use Spoolman's built-in CSV import/export.`):void 0,{data:Lt=[]}=f({queryKey:Qe,queryFn:o.getLocations}),Rt=C.get(`location_id`),Bt=Rt===`__none__`?`__none__`:Rt&&/^\d+$/.test(Rt)&&Number(Rt)>0?Rt:``,Ut=C.get(`spool`),Wt=Ut&&/^\d+$/.test(Ut)&&Number(Ut)>0?Number(Ut):null,Gt=Y?.find(e=>e.id===Wt)??null,Kt=(0,X.useCallback)(()=>{k.current=!0,T(e=>(e.delete(`spool`),e),{replace:!0})},[T]),{data:qt,isError:Jt,error:Yt}=f({queryKey:e?[`spoolman-inventory-spool`,Wt]:[`inventory-spool`,Wt],queryFn:()=>e?o.getSpoolmanInventorySpool(Wt):o.getSpool(Wt),enabled:t&&Wt!==null&&Gt===null,staleTime:1/0,retry:(e,t)=>e<2&&!(t instanceof s&&t.status===404)});(0,X.useEffect)(()=>{if(!k.current){if(t&&Wt&&Gt){Kt(),O({spool:Gt,mode:`edit`});return}if(qt){Kt(),O({spool:qt,mode:`edit`});return}if(Jt){Kt();let e=Yt instanceof s&&Yt.status===404;c(r(e?`inventory.deepLinkSpoolNotFound`:`inventory.deepLinkFetchFailed`),`error`)}}},[t,Wt,Gt,qt,Jt,Yt,Kt,c,r]);let{data:Xt}=f({queryKey:[`spool-assignments`],queryFn:()=>o.getAssignments(),refetchInterval:3e4,enabled:t&&!e}),Zt=e?void 0:Xt,{data:Qt=[],isError:$t}=f({queryKey:[`spoolman-slot-assignments`,`all`],queryFn:()=>o.getSpoolmanSlotAssignments(),enabled:e,refetchInterval:3e4,staleTime:1e4,retry:1}),en=(0,X.useRef)(!1);(0,X.useEffect)(()=>{$t&&!en.current?(en.current=!0,c(r(`inventory.spoolmanUnreachable`),`error`)):$t||(en.current=!1)},[$t,c,r]);let{data:tn}=f({queryKey:[`spool-catalog`],queryFn:()=>o.getSpoolCatalog()}),nn=p({mutationFn:t=>e?o.deleteSpoolmanInventorySpool(t):o.deleteSpool(t),onSuccess:()=>{jt(),c(r(`inventory.spoolDeleted`),`success`)},onError:e=>{e instanceof s&&e.status===404?c(r(`inventory.deleteSpoolNotFound`),`error`):e instanceof s&&e.status===503?c(r(`inventory.spoolmanUnreachable`),`error`):c(r(`inventory.deleteFailed`),`error`)}}),rn=p({mutationFn:t=>e?o.archiveSpoolmanInventorySpool(t):o.archiveSpool(t),onSuccess:()=>{jt(),c(r(`inventory.spoolArchived`),`success`)},onError:e=>{e instanceof s&&e.status===404?c(r(`inventory.archiveSpoolNotFound`),`error`):e instanceof s&&e.status===503?c(r(`inventory.spoolmanUnreachable`),`error`):c(r(`inventory.archiveFailed`),`error`)}}),an=p({mutationFn:t=>e?o.restoreSpoolmanInventorySpool(t):o.restoreSpool(t),onSuccess:()=>{jt(),c(r(`inventory.spoolRestored`),`success`)},onError:e=>{e instanceof s&&e.status===404?c(r(`inventory.restoreSpoolNotFound`),`error`):e instanceof s&&e.status===503?c(r(`inventory.spoolmanUnreachable`),`error`):c(r(`inventory.restoreFailed`),`error`)}}),sn=p({mutationFn:t=>e?o.resetSpoolmanInventorySpoolConsumedCounter(t):o.resetSpoolConsumedCounter(t),onSuccess:()=>{a.invalidateQueries({queryKey:At}),c(r(`inventory.consumedCounterReset`),`success`)},onError:()=>{c(r(`inventory.resetConsumedCounterFailed`),`error`)}}),cn=e=>(e.not_found?.length??0)+(e.errors?.length??0),ln=p({mutationFn:t=>e?o.bulkResetSpoolmanInventorySpoolConsumedCounter(t):o.bulkResetSpoolConsumedCounter(t),onSuccess:e=>{a.invalidateQueries({queryKey:At});let t=cn(e);if(e.reset===0&&t>0){c(r(`inventory.bulk.resetAllFailed`,{count:t}),`error`);return}t>0?c(r(`inventory.bulk.resetPartial`,{ok:e.reset,failed:t}),`warning`):c(r(`inventory.allConsumedCountersReset`,{count:e.reset}),`success`),gt(null),vt()},onError:()=>{c(r(`inventory.resetConsumedCounterFailed`),`error`)}}),un=p({mutationFn:async({ids:t,update:n})=>e?o.bulkUpdateSpoolmanInventorySpools(t,n):o.bulkUpdateSpools(t,n),onSuccess:e=>{jt();let t=cn(e);if(e.updated===0){c(r(`inventory.bulk.updateAllFailed`,{count:t}),`error`);return}t>0?c(r(`inventory.bulk.updatePartial`,{ok:e.updated,failed:t}),`warning`):c(r(`inventory.bulk.updateSuccess`,{count:e.updated}),`success`),mt(!1),vt()},onError:e=>{c(e.message||r(`inventory.bulk.updateFailed`),`error`)}}),dn=p({mutationFn:async t=>e?o.bulkDeleteSpoolmanInventorySpools(t):o.bulkDeleteSpools(t),onSuccess:e=>{jt();let t=cn(e);if(e.deleted===0){c(r(`inventory.bulk.deleteAllFailed`,{count:t}),`error`);return}t>0?c(r(`inventory.bulk.deletePartial`,{ok:e.deleted,failed:t}),`warning`):c(r(`inventory.bulk.deleteSuccess`,{count:e.deleted}),`success`),gt(null),vt()},onError:e=>{c(e.message||r(`inventory.bulk.deleteFailed`),`error`)}}),fn=p({mutationFn:async t=>e?o.bulkArchiveSpoolmanInventorySpools(t):o.bulkArchiveSpools(t),onSuccess:e=>{jt();let t=cn(e);if(e.archived===0&&t>0){c(r(`inventory.bulk.archiveAllFailed`,{count:t}),`error`);return}t>0?c(r(`inventory.bulk.archivePartial`,{ok:e.archived,failed:t}),`warning`):c(r(`inventory.bulk.archiveSuccess`,{count:e.archived}),`success`),gt(null),vt()},onError:e=>{c(e.message||r(`inventory.bulk.archiveFailed`),`error`)}}),pn=p({mutationFn:async t=>e?o.bulkRestoreSpoolmanInventorySpools(t):o.bulkRestoreSpools(t),onSuccess:e=>{jt();let t=cn(e);if(e.restored===0&&t>0){c(r(`inventory.bulk.restoreAllFailed`,{count:t}),`error`);return}t>0?c(r(`inventory.bulk.restorePartial`,{ok:e.restored,failed:t}),`warning`):c(r(`inventory.bulk.restoreSuccess`,{count:e.restored}),`success`),gt(null),vt()},onError:e=>{c(e.message||r(`inventory.bulk.restoreFailed`),`error`)}}),mn=(0,X.useMemo)(()=>(Y??[]).map(e=>e.id),[Y]),hn=async t=>{if(t.last_scale_weight!=null)try{e?await o.syncSpoolmanSpoolWeight(t.id,t.last_scale_weight):await l.updateSpoolWeight(t.id,t.last_scale_weight),a.invalidateQueries({queryKey:At});let n=[t.brand,t.material,We(t.color_name,t.rgba,t.color_name_is_synthesized)].filter(Boolean).join(` `);c(`Synced "${n}" to scale weight`,`success`)}catch(e){let t=e instanceof s&&e.status===404,n=e instanceof s&&e.status===503;c(r(t?`inventory.syncWeightSpoolNotFound`:n?`inventory.syncWeightSpoolmanUnreachable`:`inventory.syncWeightFailed`),`error`)}},gn=Ct?.low_stock_threshold??20,[_n,vn]=(0,X.useState)(!1),[yn,bn]=(0,X.useState)(gn.toString());(0,X.useEffect)(()=>{_n||bn(gn.toString())},[gn,_n]);let xn=p({mutationFn:e=>o.updateSettings({low_stock_threshold:e}),onSuccess:()=>{a.invalidateQueries({queryKey:[`settings`]}),c(r(`common.saved`),`success`),vn(!1)},onError:()=>{c(r(`inventory.lowStockThresholdError`),`error`)}}),Sn=(0,X.useMemo)(()=>{if(!Y)return null;let e=0,t=0,n=0,r=0,i={};for(let a of Y){if(t+=Math.max(0,a.weight_used-(a.weight_used_baseline??0)),a.archived_at)continue;r++;let o=Math.max(0,a.label_weight-a.weight_used);e+=o,(a.label_weight>0?o/a.label_weight*100:0)<(a.low_stock_threshold_pct??gn)&&n++;let s=a.material||`Unknown`;i[s]||(i[s]={count:0,weight:0}),i[s].count++,i[s].weight+=o}return{totalWeight:e,totalConsumed:t,lowStock:n,byMaterial:i,totalSpools:r}},[Y,gn]),Cn=(Zt?.length??0)+(e?Qt.length:0),wn=qe(Ct?.currency||`USD`),Tn=(0,X.useMemo)(()=>{let e={};for(let t of Zt||[])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,printer_model:t.printer_model??null};for(let t of Qt)typeof t?.spoolman_spool_id!=`number`||t.spoolman_spool_id<=0||typeof t.printer_id!=`number`||typeof t.ams_id!=`number`||typeof t.tray_id!=`number`||e[t.spoolman_spool_id]||(e[t.spoolman_spool_id]={printer_id:t.printer_id,printer_name:t.printer_name??null,ams_id:t.ams_id,tray_id:t.tray_id,ams_label:t.ams_label??null,printer_model:t.printer_model??null});return e},[Zt,Qt]),En=(0,X.useMemo)(()=>{let e={};for(let t of tn||[])e[t.id]=t;return e},[tn]),{data:Dn}=f({queryKey:[`locationHaSensors`],queryFn:()=>o.getLocationHASensors()}),On=(0,X.useMemo)(()=>new Set((Dn??[]).map(e=>e.location_id)),[Dn]),kn=(0,X.useMemo)(()=>{let e=new Set;for(let t of Y||[])t.location_id&&On.has(t.location_id)&&e.add(t.location_id);return Array.from(e)},[Y,On]),jn=q===`cards`||q===`table`&&it.some(e=>e.visible&&(e.id===`temperature`||e.id===`humidity`||e.id===`battery`)),Mn=m({queries:kn.map(e=>({queryKey:[`locationHaSensorReadings`,e],queryFn:()=>o.getLocationHASensorReadings(e,!1),refetchInterval:Tt,enabled:jn}))}),Nn=(0,X.useMemo)(()=>{let e={};return kn.forEach((t,n)=>{let r=Mn[n]?.data;r&&(e[t]=r)}),e},[kn,Mn]),Pn=(0,X.useMemo)(()=>Sn?Object.entries(Sn.byMaterial).sort((e,t)=>t[1].weight-e[1].weight).slice(0,4):[],[Sn]),Fn=(0,X.useMemo)(()=>{let e=Y||[];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??gn)})),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===Sr?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(Bt)if(Bt===`__none__`)e=e.filter(e=>!e.location_id&&!e.storage_location?.trim());else{let t=Number(Bt),n=Lt.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)),Xe&&(e=Nt(e,Xe)),e},[Y,De,ke,B,Ne,V,U,G,He,K,Bt,Xe,gn,Lt,i]),Q=()=>bt(0),In=(0,X.useCallback)(e=>{T(t=>(t.delete(`location_id`),e&&t.set(`location_id`,e),t),{replace:!0}),Q()},[T]),Ln=[...new Set(Y?.map(e=>e.material)||[])].sort(),Rn=[...new Set(Y?.map(e=>e.brand).filter(Boolean)||[])].sort(),zn=[...new Set(Y?.map(e=>e.category?.trim()).filter(Boolean)||[])].sort(),Bn=(Y??[]).some(e=>!e.category),Vn=[...new Set(Y?.map(e=>e.material_number?.trim()).filter(Boolean)||[])].sort((e,t)=>e.localeCompare(t,void 0,{numeric:!0})),Hn=(Y??[]).some(e=>!e.material_number?.trim()),Un=(0,X.useMemo)(()=>{let e=new Map;for(let t of Y??[])for(let n of t.suppliers??[])e.set(n.supplier_id,n.supplier_name);return[...e.entries()].map(([e,t])=>({id:e,name:t})).sort((e,t)=>e.name.localeCompare(t.name))},[Y]),Wn=(Y??[]).some(e=>(e.suppliers??[]).length===0),Gn=[...new Set(Y?.map(e=>e.core_weight_catalog_id).filter(e=>e!=null)||[])].sort((e,t)=>{let n=(En[e]?.name||``).toLowerCase(),r=(En[t]?.name||``).toLowerCase();return n.localeCompare(r)}),Kn=(Y??[]).some(e=>!e.location_id&&!e.storage_location?.trim()),qn=De!==`active`||ke!==`all`||!!B||!!Ne||!!V||!!U||!!G||!!He||!!Bt||K!==`all`||!!Xe,Jn=e=>{at(e),Er(e)},Yn=(0,X.useMemo)(()=>it.filter(e=>e.visible).map(e=>e.id),[it]),Xn=e=>{Nr[e]&&(nt(t=>{let n;return n=t?.column===e?t.direction===`asc`?{column:e,direction:`desc`}:null:{column:e,direction:`asc`},Ir(n),n}),Q())},Zn=(0,X.useMemo)(()=>{if(!tt)return Fn;let e=Nr[tt.column];return e?[...Fn].sort((t,n)=>{let r=e(t,Tn,Nn),i=e(n,Tn,Nn);return r<i?tt.direction===`asc`?-1:1:r>i?tt.direction===`asc`?1:-1:0}):Fn},[Fn,tt,Tn,Nn]),Qn=(0,X.useMemo)(()=>{if(!ct)return Zn.map(e=>({type:`single`,spool:e}));let e=new Map;for(let t of Zn)if(!(t.weight_used>0||Tn[t.id])){let n=br(t),r=e.get(n);r?r.push(t):e.set(n,[t])}let t=[],n=new Set;for(let r of Zn){if(r.weight_used>0||Tn[r.id]){t.push({type:`single`,spool:r});continue}let i=br(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},[Zn,ct,Tn]),$n=xt===-1,$=Qn.length,er=$n?$||1:xt,tr=Math.max(1,Math.ceil($/er)),nr=$n?0:Math.min(yt,tr-1),rr=$n?Qn:Qn.slice(nr*er,(nr+1)*er),ir=()=>{let e=!ct;lt(e),dt(new Set),Q();try{localStorage.setItem(`bambuddy-inventory-group`,String(e))}catch{}},ar=e=>{dt(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},or=e=>{St(e),bt(0);try{localStorage.setItem(`bambuddy-inventory-pageSize`,String(e))}catch{}};return(0,Z.jsxs)(`div`,{className:`p-4 md:p-8 space-y-6`,children:[(0,Z.jsxs)(`div`,{className:`flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`h1`,{className:`text-2xl font-bold text-white flex items-center gap-3`,children:[(0,Z.jsx)(fe,{className:`w-7 h-7 text-bambu-green`}),r(`inventory.title`)]}),(0,Z.jsx)(`p`,{className:`text-bambu-gray mt-1`,children:r(`inventory.subtitle`)})]}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2`,children:[(0,Z.jsxs)(W,{variant:`secondary`,disabled:e,onClick:()=>pe(!0),title:It,children:[(0,Z.jsx)(Ie,{className:`w-4 h-4`}),r(`inventory.csv.importButton`,`Import CSV`)]}),(0,Z.jsxs)(W,{variant:`secondary`,disabled:e||he,onClick:Ft,title:It,children:[he?(0,Z.jsx)(L,{className:`w-4 h-4 animate-spin`}):(0,Z.jsx)(P,{className:`w-4 h-4`}),r(`inventory.csv.exportButton`,`Export CSV`)]}),(0,Z.jsxs)(W,{variant:`secondary`,onClick:()=>Ce(!0),children:[(0,Z.jsx)(de,{className:`w-4 h-4`}),r(`locations.manage`)]}),(0,Z.jsxs)(W,{variant:`secondary`,onClick:()=>R(!0),children:[(0,Z.jsx)(Vt,{className:`w-4 h-4`}),r(`inventory.suppliers.title`)]}),(0,Z.jsxs)(W,{variant:`secondary`,disabled:Fn.length===0,onClick:()=>I(J.size>0?[...J]:Fn.map(e=>e.id)),title:Fn.length===0?r(`inventory.labels.noSpoolsTitle`,`No spools to label`):J.size>0?r(`inventory.labels.selectedTitle`,`Print labels for the {{count}} selected spools`,{count:J.size}):r(`inventory.labels.bulkTitle`,`Pick spools to print labels for from the {{count}} currently shown`,{count:Fn.length}),children:[(0,Z.jsx)(xe,{className:`w-4 h-4`}),r(`inventory.labels.printLabels`,`Print labels…`)]}),(0,Z.jsxs)(W,{variant:`secondary`,onClick:()=>ie(!0),children:[(0,Z.jsx)(Se,{className:`w-4 h-4`}),r(`inventory.qrAssign.button`)]}),(0,Z.jsxs)(W,{onClick:()=>O({spool:null,mode:`create`}),children:[(0,Z.jsx)(be,{className:`w-4 h-4`}),r(`inventory.addSpool`)]})]})]}),A&&(0,Z.jsx)(Pt,{isOpen:A,onClose:()=>ie(!1),spoolmanMode:e}),Sn&&!Mt&&(0,Z.jsxs)(`div`,{className:`grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3`,children:[(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(fe,{className:`w-4 h-4 text-bambu-green`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:r(`inventory.totalInventory`)})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Dr(Sn.totalWeight,!0)}),(0,Z.jsxs)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:[Sn.totalSpools,` `,Sn.totalSpools===1?r(`inventory.spool`):r(`inventory.spools`)]})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between gap-2 mb-1`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(je,{className:`w-4 h-4 text-blue-600 dark:text-blue-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:r(`inventory.totalConsumed`)})]}),Sn.totalConsumed>0&&mn.length>0&&(0,Z.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:r(`inventory.resetAllConsumedCountersTooltip`),"aria-label":r(`inventory.resetAllConsumedCounters`),children:(0,Z.jsx)(w,{className:`w-3.5 h-3.5`})})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Dr(Sn.totalConsumed,!0)}),(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:r(`inventory.sinceTracking`)})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(se,{className:`w-4 h-4 text-green-600 dark:text-green-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:r(`inventory.byMaterial`)})]}),(0,Z.jsx)(`div`,{className:`flex flex-wrap gap-1.5 mt-1`,children:Pn.map(([e,t])=>(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${Or[e]||`bg-bambu-dark-tertiary text-bambu-gray`}`,children:[e,` `,(0,Z.jsx)(`span`,{className:`opacity-70`,children:Dr(t.weight,!0)})]},e))})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(xe,{className:`w-4 h-4 text-purple-600 dark:text-purple-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:r(`inventory.inPrinter`)})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold text-white`,children:Cn}),(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1`,children:r(`inventory.loadedInAms`)})]}),(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg p-4`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 mb-1`,children:[(0,Z.jsx)(H,{className:`w-4 h-4 text-yellow-600 dark:text-yellow-400`}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray font-medium uppercase tracking-wide`,children:r(`inventory.lowStock`)})]}),(0,Z.jsx)(`div`,{className:`text-xl font-bold ${Sn.lowStock>0?`text-yellow-700 dark:text-yellow-400`:`text-white`}`,children:Sn.lowStock}),(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray mt-1 flex items-center gap-2`,children:_n?(0,Z.jsxs)(`form`,{onSubmit:e=>{e.preventDefault();let t=parseFloat(yn);!isNaN(t)&&t>=.1&&t<=99.9?xn.mutate(t):c(r(`inventory.lowStockThresholdError`),`error`)},className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`<`}),(0,Z.jsx)(`input`,{type:`text`,inputMode:`decimal`,pattern:`^\\d{0,2}(\\.\\d?)?$`,maxLength:4,value:yn,onChange:e=>{let t=e.target.value.replace(/[^\d.]/g,``);/^\d{0,2}(\.\d?)?$/.test(t)&&bn(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:xn.isPending}),(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:`%`}),(0,Z.jsx)(W,{type:`submit`,size:`sm`,disabled:xn.isPending,children:r(`common.save`)}),(0,Z.jsx)(W,{type:`button`,size:`sm`,variant:`ghost`,onClick:()=>vn(!1),disabled:xn.isPending,children:r(`common.cancel`)})]}):(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray`,children:[`< `,gn,`%`]}),(0,Z.jsx)(`button`,{className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:r(`common.edit`),onClick:()=>{bn(gn.toString()),vn(!0)},children:(0,Z.jsx)(me,{className:`w-4 h-4`})})]})})]})]}),(0,Z.jsxs)(`div`,{className:`flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between`,children:[(0,Z.jsxs)(`div`,{className:`relative flex-1 max-w-md ${q===`forecast`?`invisible pointer-events-none`:``}`,children:[(0,Z.jsx)(Ee,{className:`absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50`}),(0,Z.jsx)(`input`,{type:`text`,value:Xe,onChange:e=>{Ze(e.target.value),Q()},placeholder:r(`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`}),Xe&&(0,Z.jsx)(`button`,{onClick:()=>{Ze(``),Q()},className:`absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white`,children:(0,Z.jsx)(Le,{className:`w-4 h-4`})})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[q===`table`&&(0,Z.jsxs)(`button`,{onClick:()=>st(!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:r(`inventory.configureColumns`),children:[(0,Z.jsx)(oe,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:r(`inventory.columns`)})]}),q!==`forecast`&&(0,Z.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 ${ct?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,title:r(`inventory.groupSimilar`),children:[(0,Z.jsx)(zt,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:r(`inventory.groupSimilar`)})]}),(0,Z.jsxs)(`div`,{className:`flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:[(0,Z.jsxs)(`button`,{onClick:()=>$e(`table`),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${q===`table`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(Ht,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:r(`inventory.table`)})]}),(0,Z.jsxs)(`button`,{onClick:()=>$e(`cards`),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${q===`cards`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(ce,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:r(`inventory.cards`)})]}),(0,Z.jsxs)(`button`,{onClick:()=>S&&$e(`forecast`),disabled:!S,title:S?void 0:r(`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 ${q===`forecast`?`bg-bambu-green text-white`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:[S?(0,Z.jsx)(h,{className:`w-4 h-4`}):(0,Z.jsx)(ue,{className:`w-4 h-4`}),(0,Z.jsx)(`span`,{className:`hidden sm:inline`,children:r(`forecast.title`)})]})]})]})]}),(0,Z.jsxs)(`div`,{className:`flex flex-wrap items-center gap-2 ${q===`forecast`?`hidden`:``}`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Z.jsxs)(`button`,{onClick:()=>{Oe(`active`),Q()},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,Z.jsx)(fe,{className:`w-3.5 h-3.5`}),r(`inventory.active`)]}),(0,Z.jsxs)(`button`,{onClick:()=>{Oe(`archived`),Q()},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,Z.jsx)(v,{className:`w-3.5 h-3.5`}),r(`inventory.archived`)]})]}),(0,Z.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Z.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Z.jsx)(`button`,{onClick:()=>{Ae(`all`),Q()},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:r(`inventory.all`)}),(0,Z.jsxs)(`button`,{onClick:()=>{Ae(`used`),Q()},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,Z.jsx)(ae,{className:`w-3.5 h-3.5`}),r(`inventory.used`)]}),(0,Z.jsx)(`button`,{onClick:()=>{Ae(`new`),Q()},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:r(`inventory.new`)}),(0,Z.jsxs)(`button`,{onClick:()=>{Ae(`lowstock`),Q()},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,Z.jsx)(H,{className:`w-3.5 h-3.5`}),r(`inventory.lowStock`)]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden`,children:[(0,Z.jsx)(`button`,{onClick:()=>{Ge(`all`),Q()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${K===`all`?`bg-bambu-green/20 text-bambu-green`:`text-bambu-gray hover:bg-bambu-dark-tertiary`}`,children:r(`inventory.all`)}),(0,Z.jsx)(`button`,{onClick:()=>{Ge(`stock`),Q()},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:r(`inventory.stock`)}),(0,Z.jsx)(`button`,{onClick:()=>{Ge(`configured`),Q()},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:r(`inventory.configured`)})]}),(0,Z.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Z.jsxs)(`select`,{value:B,onChange:e=>{Me(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${B?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:r(`inventory.material`)}),Ln.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(0,Z.jsxs)(`select`,{value:Ne,onChange:e=>{Pe(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${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,Z.jsx)(`option`,{value:``,children:r(`inventory.brand`)}),Rn.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e))]}),(zn.length>0||V)&&(0,Z.jsxs)(`select`,{value:V,onChange:e=>{Fe(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${V?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:r(`inventory.category`)}),zn.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e)),Bn&&(0,Z.jsx)(`option`,{value:`__none__`,children:r(`inventory.categoryNone`)})]}),(Vn.length>0||U)&&(0,Z.jsxs)(`select`,{value:U,onChange:e=>{Re(e.target.value),Q()},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,Z.jsx)(`option`,{value:``,children:r(`inventory.materialNumber`)}),Vn.map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e)),Hn&&(0,Z.jsx)(`option`,{value:Sr,children:r(`inventory.materialNumberNone`)})]}),(Un.length>0||G)&&(0,Z.jsxs)(`select`,{value:G,onChange:e=>{Ve(e.target.value),Q()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${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,Z.jsx)(`option`,{value:``,children:r(`inventory.suppliers.label`)}),Un.map(e=>(0,Z.jsx)(`option`,{value:String(e.id),children:e.name},e.id)),Wn&&(0,Z.jsx)(`option`,{value:`__none__`,children:r(`inventory.suppliers.none`)})]}),Gn.length>0&&(0,Z.jsxs)(`select`,{value:He,onChange:e=>{Ue(e.target.value),Q()},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,Z.jsx)(`option`,{value:``,children:r(`inventory.spoolName`)}),Gn.map(e=>(0,Z.jsx)(`option`,{value:e,children:En[e]?.name||`#${e}`},e))]}),(Lt.length>0||Bt)&&(0,Z.jsxs)(`select`,{value:Bt,onChange:e=>{In(e.target.value)},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${Bt?`bg-bambu-green/20 text-bambu-green border-bambu-green/30`:`bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary`}`,children:[(0,Z.jsx)(`option`,{value:``,children:r(`inventory.storageLocation`)}),Lt.map(e=>(0,Z.jsx)(`option`,{value:String(e.id),children:e.name},e.id)),Kn&&(0,Z.jsx)(`option`,{value:`__none__`,children:r(`inventory.storageLocationNone`)})]}),qn&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`div`,{className:`w-px h-5 bg-bambu-dark-tertiary`}),(0,Z.jsxs)(`button`,{onClick:()=>{Oe(`active`),Ae(`all`),Me(``),Pe(``),Fe(``),Re(``),Ve(``),Ue(``),Ge(`all`),Ze(``),T(e=>(e.delete(`location_id`),e),{replace:!0}),Q()},className:`flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors`,children:[(0,Z.jsx)(Le,{className:`w-3.5 h-3.5`}),r(`inventory.clearFilters`)]})]}),q!==`forecast`&&(0,Z.jsxs)(`span`,{className:`ml-auto text-xs text-bambu-gray`,children:[Zn.length,` `,Zn.length===1?r(`inventory.spool`):r(`inventory.spools`),ct&&$<Zn.length&&` (${$} ${r(`inventory.groupedRows`)})`]})]}),J.size>0&&q!==`forecast`&&(0,Z.jsxs)(`div`,{className:`sticky top-2 z-10 mb-4 flex items-center gap-2 px-3 py-2 bg-bambu-green/10 border border-bambu-green/30 rounded-lg backdrop-blur-sm`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-bambu-green font-medium`,children:r(`inventory.bulk.selectionCount`,{count:J.size})}),(0,Z.jsxs)(`div`,{className:`ml-auto flex flex-wrap items-center gap-2`,children:[(0,Z.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>mt(!0),children:[(0,Z.jsx)(me,{className:`w-3.5 h-3.5 mr-1.5`}),r(`inventory.bulk.edit`)]}),(0,Z.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>I([...J]),children:[(0,Z.jsx)(xe,{className:`w-3.5 h-3.5 mr-1.5`}),r(`inventory.bulk.printLabels`)]}),(0,Z.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>gt(`reset-consumed-counter`),children:[(0,Z.jsx)(w,{className:`w-3.5 h-3.5 mr-1.5`}),r(`inventory.bulk.resetUsage`)]}),De===`archived`?(0,Z.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>gt(`restore`),children:[(0,Z.jsx)(we,{className:`w-3.5 h-3.5 mr-1.5`}),r(`inventory.bulk.restore`)]}):(0,Z.jsxs)(W,{size:`sm`,variant:`secondary`,onClick:()=>gt(`archive`),children:[(0,Z.jsx)(v,{className:`w-3.5 h-3.5 mr-1.5`}),r(`inventory.bulk.archive`)]}),(0,Z.jsxs)(W,{size:`sm`,variant:`danger`,onClick:()=>gt(`delete`),children:[(0,Z.jsx)(z,{className:`w-3.5 h-3.5 mr-1.5`}),r(`inventory.bulk.delete`)]}),(0,Z.jsx)(`button`,{className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,onClick:vt,title:r(`inventory.bulk.clearSelection`),"aria-label":r(`inventory.bulk.clearSelection`),children:(0,Z.jsx)(Le,{className:`w-4 h-4`})})]})]}),Mt?(0,Z.jsx)(`div`,{className:`flex justify-center py-16`,children:(0,Z.jsx)(L,{className:`w-8 h-8 text-bambu-green animate-spin`})}):q===`forecast`?(0,Z.jsx)(on,{spools:Y||[],spoolmanMode:e}):q===`cards`?rr.length>0?(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4`,children:rr.map(e=>{if(e.type===`group`){let{key:t,spools:n,representative:i}=e,a=n.reduce((e,t)=>e+Math.max(0,t.label_weight-t.weight_used),0),o=Ye({rgba:i.rgba,extraColors:i.extra_colors,effectType:i.effect_type,subtype:i.subtype,effectSize:`groupheader`}),s=ut.has(t);return(0,Z.jsxs)(`div`,{className:`col-span-full`,children:[(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-green/30 hover:border-bambu-green transition-colors cursor-pointer`,onClick:()=>ar(t),children:[(0,Z.jsx)(`div`,{className:`h-10 flex items-center px-4 gap-3`,style:o,children:(0,Z.jsx)(`span`,{className:`bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium`,children:We(i.color_name,i.rgba,i.color_name_is_synthesized)||`-`})}),(0,Z.jsxs)(`div`,{className:`px-4 py-3 flex items-center justify-between`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,Z.jsx)(D,{className:`w-4 h-4 text-bambu-gray transition-transform ${s?``:`-rotate-90`}`}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`h3`,{className:`font-semibold text-white`,children:[i.material,i.subtype?` ${i.subtype}`:``]}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:i.brand||`-`})]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-sm text-bambu-gray`,title:r(`inventory.remaining`),children:Dr(a)}),(0,Z.jsx)(`span`,{className:`text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full`,children:r(`inventory.groupedSpools`,{count:n.length})})]})]})]}),s&&(0,Z.jsx)(`div`,{className:`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4 mt-2 ml-4`,children:n.map(e=>{let t=Math.max(0,e.label_weight-e.weight_used);return(0,Z.jsx)(Br,{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:r,colorizeLocationSensors:Et,locationSensorAboveColor:Dt,locationSensorBelowColor:Ot,locationSensorOptimalColor:kt,dateFormat:wt},e.id)})})]},`group-${t}`)}let t=e.spool,n=Math.max(0,t.label_weight-t.weight_used);return(0,Z.jsx)(Br,{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:r,colorizeLocationSensors:Et,locationSensorAboveColor:Dt,locationSensorBelowColor:Ot,locationSensorOptimalColor:kt,dateFormat:wt},t.id)})}),(0,Z.jsx)(zr,{pageIndex:nr,pageSize:xt,totalRows:$,totalPages:tr,onPageChange:bt,onPageSizeChange:or,t:r})]}):(0,Z.jsx)(Jr,{hasFilters:qn,onAddSpool:()=>O({spool:null,mode:`create`}),t:r}):rr.length>0?(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,Z.jsx)(`div`,{className:`overflow-x-auto`,children:(0,Z.jsxs)(`table`,{className:`w-full`,children:[(0,Z.jsx)(`thead`,{children:(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30`,children:[(0,Z.jsx)(`th`,{className:`w-10 px-3 py-3`,children:(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":r(`inventory.bulk.selectAllVisible`),checked:rr.length>0&&rr.every(e=>(e.type===`group`?e.spools.map(e=>e.id):[e.spool.id]).every(e=>J.has(e))),onChange:e=>{if(e.target.checked){let e=new Set(J);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)}ft(e)}else{let e=new Set(J);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)}ft(e)}}})}),Yn.map(e=>{let t=!!Nr[e],n=tt?.column===e;return(0,Z.jsx)(`th`,{className:`text-left py-3 px-4 text-xs font-medium uppercase tracking-wide select-none ${e===`remaining`?`min-w-[150px]`:``} ${t?`cursor-pointer hover:text-bambu-green transition-colors`:``} ${n?`text-bambu-green`:`text-bambu-gray`}`,onClick:t?()=>Xn(e):void 0,children:(0,Z.jsxs)(`span`,{className:`inline-flex items-center gap-1`,children:[jr[e]?.(r)??e,t&&(n?tt.direction===`asc`?(0,Z.jsx)(x,{className:`w-3 h-3`}):(0,Z.jsx)(y,{className:`w-3 h-3`}):(0,Z.jsx)(b,{className:`w-3 h-3 opacity-30`}))]})},e)}),(0,Z.jsx)(`th`,{className:`text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:r(`common.actions`)})]})}),(0,Z.jsx)(`tbody`,{children:rr.map(e=>{if(e.type===`group`){let{key:t,spools:n}=e,i=ut.has(t),a=vr(n),o=Math.max(0,a.label_weight-a.weight_used);return(0,Z.jsx)(qr,{spools:n,headerSpool:a,remaining:o,pct:a.label_weight>0?o/a.label_weight*100:0,isExpanded:i,onToggle:()=>ar(t),selectedIds:J,onToggleSelected:_t,onToggleGroupSelected:(e,t)=>{ft(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:Yn,assignmentMap:Tn,catalogMap:En,locationReadingsMap:Nn,currencySymbol:wn,dateFormat:wt,t:r,onSyncWeight:hn,colorizeLocationSensors:Et,locationSensorAboveColor:Dt,locationSensorBelowColor:Ot,locationSensorOptimalColor:kt},`group-${t}`)}let t=e.spool,n=Math.max(0,t.label_weight-t.weight_used);return(0,Z.jsx)(Kr,{spool:t,remaining:n,pct:t.label_weight>0?n/t.label_weight*100:0,isSelected:J.has(t.id),onToggleSelected:()=>_t(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:Yn,assignmentMap:Tn,catalogMap:En,locationReadingsMap:Nn,currencySymbol:wn,dateFormat:wt,t:r,onSyncWeight:hn,colorizeLocationSensors:Et,locationSensorAboveColor:Dt,locationSensorBelowColor:Ot,locationSensorOptimalColor:kt},t.id)})})]})}),(0,Z.jsxs)(`div`,{className:`flex items-center justify-between px-4 py-3 bg-bambu-dark-tertiary/50 border-t border-bambu-dark-tertiary text-sm`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:$n?`${$} ${r($===1?`inventory.spool`:`inventory.spools`)}`:(0,Z.jsxs)(Z.Fragment,{children:[r(`inventory.showing`),` `,nr*er+1,` `,r(`inventory.to`),` `,Math.min((nr+1)*er,$),` `,r(`inventory.of`),` `,$,` `,r(`inventory.spools`)]})}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:r(`inventory.show`)}),(0,Z.jsxs)(`select`,{value:xt,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,Z.jsx)(`option`,{value:e,children:e},e)),(0,Z.jsx)(`option`,{value:-1,children:r(`inventory.all`)})]}),!$n&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`button`,{onClick:()=>bt(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,Z.jsx)(j,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>bt(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,Z.jsx)(te,{className:`w-4 h-4`})}),(0,Z.jsxs)(`span`,{className:`text-bambu-gray px-2 whitespace-nowrap`,children:[r(`inventory.page`),` `,nr+1,` `,r(`inventory.of`),` `,tr]}),(0,Z.jsx)(`button`,{onClick:()=>bt(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,Z.jsx)(ee,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>bt(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,Z.jsx)(ne,{className:`w-4 h-4`})})]})]})]})]}):(0,Z.jsx)(Jr,{hasFilters:qn,onAddSpool:()=>O({spool:null,mode:`create`}),t:r}),E!==null&&(0,Z.jsx)(An,{isOpen:!0,onClose:()=>O(null),spool:E.spool,mode:E.mode,currencySymbol:wn,spoolmanMode:e,spoolsQueryKey:At}),M&&(0,Z.jsx)(Be,{title:M.type===`delete`?r(`common.delete`):M.type===`archive`?r(`inventory.archive`):M.type===`reset-consumed-counter`?r(`inventory.resetConsumedCounter`):r(`inventory.resetAllConsumedCounters`),message:M.type===`delete`?r(`inventory.deleteConfirm`):M.type===`archive`?r(`inventory.archiveConfirm`):M.type===`reset-consumed-counter`?r(`inventory.resetConsumedCounterConfirm`):r(`inventory.resetAllConsumedCountersConfirm`,{count:mn.length}),confirmText:M.type===`delete`?r(`common.delete`):M.type===`archive`?r(`inventory.archive`):r(`inventory.resetConsumedCounter`),variant:M.type===`archive`?`warning`:`danger`,onConfirm:()=>{M.type===`delete`?nn.mutate(M.spoolId):M.type===`archive`?rn.mutate(M.spoolId):M.type===`reset-consumed-counter`?sn.mutate(M.spoolId):ln.mutate(mn),N(null)},onCancel:()=>N(null)}),(0,Z.jsx)(rt,{isOpen:ot,onClose:()=>st(!1),columns:it,defaultColumns:wr,onSave:Jn}),(0,Z.jsx)(ur,{isOpen:F!==null,onClose:()=>I(null),availableSpools:Fn,initialSelectedIds:F??[],spoolmanMode:e}),(0,Z.jsx)(_r,{isOpen:pt,selectedCount:J.size,isPending:un.isPending,availableLocations:Lt.map(e=>({id:e.id,name:e.name})),availableMaterials:yr((Y??[]).map(e=>e.material)),availableSubtypes:yr((Y??[]).map(e=>e.subtype)),availableBrands:yr((Y??[]).map(e=>e.brand)),availableCategories:yr((Y??[]).map(e=>e.category)),availableMaterialNumbers:yr((Y??[]).map(e=>e.material_number)),availableSlicerFilaments:yr((Y??[]).map(e=>e.slicer_filament)),availableSlicerFilamentNames:yr((Y??[]).map(e=>e.slicer_filament_name)),spoolmanMode:e,onClose:()=>mt(!1),onApply:e=>un.mutate({ids:[...J],update:e})}),ht&&(0,Z.jsx)(Be,{title:r(ht===`delete`?`inventory.bulk.deleteTitle`:ht===`archive`?`inventory.bulk.archiveTitle`:ht===`restore`?`inventory.bulk.restoreTitle`:`inventory.bulk.resetUsageTitle`),message:r(ht===`delete`?`inventory.bulk.deleteMessage`:ht===`archive`?`inventory.bulk.archiveMessage`:ht===`restore`?`inventory.bulk.restoreMessage`:`inventory.bulk.resetUsageMessage`,{count:J.size}),confirmText:r(ht===`delete`?`common.delete`:ht===`archive`?`inventory.archive`:ht===`restore`?`inventory.restore`:`inventory.resetConsumedCounter`),variant:ht===`delete`?`danger`:`warning`,isLoading:dn.isPending||fn.isPending||pn.isPending||ln.isPending,onConfirm:()=>{let e=[...J];ht===`delete`?dn.mutate(e):ht===`archive`?fn.mutate(e):ht===`restore`?pn.mutate(e):ln.mutate(e)},onCancel:()=>gt(null)}),le&&(0,Z.jsx)(fr,{onClose:()=>pe(!1),onImported:e=>{pe(!1),a.invalidateQueries({queryKey:At}),c(r(`inventory.csv.importSuccess`,`{{count}} spools imported`,{count:e}),`success`)}}),(0,Z.jsx)(ge,{open:ye,onClose:()=>Ce(!1),onPickLocation:e=>In(String(e))}),(0,Z.jsx)(pr,{open:Te,onClose:()=>R(!1)})]})}function zr({pageIndex:e,pageSize:t,totalRows:n,totalPages:r,onPageChange:i,onPageSizeChange:a,t:o}){let s=t===-1;if(r<=1&&!s)return null;let c=s?n||1:t;return(0,Z.jsxs)(`div`,{className:`flex items-center justify-between pt-2 text-sm`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:s?`${n} ${o(n===1?`inventory.spool`:`inventory.spools`)}`:(0,Z.jsxs)(Z.Fragment,{children:[o(`inventory.showing`),` `,e*c+1,` `,o(`inventory.to`),` `,Math.min((e+1)*c,n),` `,o(`inventory.of`),` `,n,` `,o(`inventory.spools`)]})}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:o(`inventory.show`)}),(0,Z.jsxs)(`select`,{value:t,onChange:e=>a(Number(e.target.value)),className:`px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green`,children:[[15,30,50,100].map(e=>(0,Z.jsx)(`option`,{value:e,children:e},e)),(0,Z.jsx)(`option`,{value:-1,children:o(`inventory.all`)})]}),!s&&(0,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsx)(`button`,{onClick:()=>i(0),disabled:e===0,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(j,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>i(Math.max(0,e-1)),disabled:e===0,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(te,{className:`w-4 h-4`})}),(0,Z.jsxs)(`span`,{className:`text-bambu-gray px-2 whitespace-nowrap`,children:[o(`inventory.page`),` `,e+1,` `,o(`inventory.of`),` `,r]}),(0,Z.jsx)(`button`,{onClick:()=>i(Math.min(r-1,e+1)),disabled:e>=r-1,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(ee,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:()=>i(r-1),disabled:e>=r-1,className:`p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors`,children:(0,Z.jsx)(ne,{className:`w-4 h-4`})})]})]})]})}function Br({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=Ye({rgba:e.rgba,extraColors:e.extra_colors,effectType:e.effect_type,subtype:e.subtype,effectSize:`card`});return(0,Z.jsxs)(`div`,{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary hover:border-bambu-green transition-colors cursor-pointer ${e.archived_at?`opacity-50`:``}`,onClick:r,children:[(0,Z.jsxs)(`div`,{className:`h-14 flex items-center justify-center`,style:f,children:[(0,Z.jsx)(`span`,{className:`bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium`,children:We(e.color_name,e.rgba,e.color_name_is_synthesized)||`-`}),a&&(0,Z.jsx)(`button`,{type:`button`,onClick:e=>{e.stopPropagation(),a()},className:`p-1.5 bg-black/20 hover:bg-black/40 text-white rounded-full transition-colors`,title:o(`inventory.copySpool`),children:(0,Z.jsx)(N,{className:`w-3.5 h-3.5`})})]}),(0,Z.jsxs)(`div`,{className:`p-4 space-y-3`,children:[(0,Z.jsxs)(`div`,{className:`flex items-start justify-between gap-2`,children:[(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`h3`,{className:`font-semibold text-white`,children:[e.material,e.subtype?` ${e.subtype}`:``]}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:e.brand||`-`})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-1`,children:[i&&(0,Z.jsx)(`button`,{onClick:e=>{e.stopPropagation(),i()},className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,title:o(`inventory.labels.printOne`),"aria-label":o(`inventory.labels.printOne`),children:(0,Z.jsx)(xe,{className:`w-4 h-4`})}),(0,Z.jsxs)(`span`,{className:`text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded`,children:[`#`,e.id]})]})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`div`,{className:`flex justify-between text-xs text-bambu-gray mb-1`,children:[(0,Z.jsx)(`span`,{children:o(`inventory.remaining`)}),(0,Z.jsxs)(`span`,{children:[Math.round(n),`%`]})]}),(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(`div`,{className:`flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden`,children:(0,Z.jsx)(`div`,{className:`h-full rounded-full ${n>50?`bg-bambu-green`:n>20?`bg-yellow-500`:`bg-red-500`}`,style:{width:`${Math.min(n,100)}%`}})}),(0,Z.jsxs)(`span`,{className:`text-xs text-bambu-gray min-w-[40px] text-right`,children:[Math.round(t),`g`]})]})]}),(0,Z.jsxs)(`div`,{className:`grid grid-cols-2 gap-2 text-xs`,children:[e.last_dried_at&&(0,Z.jsxs)(`div`,{className:`col-span-2`,children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.lastDried`),`: `]}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:[Ar(e.last_dried_at,d),kr(e)].filter(Boolean).join(` · `)})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.labelWeight`),`: `]}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:Dr(e.label_weight)})]}),(0,Z.jsxs)(`div`,{children:[(0,Z.jsxs)(`span`,{className:`text-bambu-gray/60`,children:[o(`inventory.weightUsed`),`: `]}),(0,Z.jsx)(`span`,{className:`text-bambu-gray`,children:e.weight_used>0?Dr(e.weight_used):`-`})]})]}),e.location_id&&(0,Z.jsx)(Gr,{locationId:e.location_id,locationName:e.storage_location??null,isLast:!e.note,colorize:s,aboveColor:c,belowColor:l,optimalColor:u}),e.note&&(0,Z.jsx)(`div`,{className:`text-xs text-bambu-gray/60 pt-3 border-t border-bambu-dark-tertiary truncate`,title:e.note,children:e.note})]})]})}var Vr={temperature:0,humidity:1,battery:2};function Hr(e){return e===`humidity`?`mr-[2px]`:e===`battery`?`mr-[3px]`:``}function Ur(e,t){return g(e,t,{decimals:2})}function Wr(e,t,n,r,i){return t&&he(ve(e),n,r,i)||`text-bambu-gray`}function Gr({locationId:e,locationName:t,isLast:r,colorize:i,aboveColor:a,belowColor:s,optimalColor:c}){let{t:l}=n(),{data:u}=Je(),d=(u?.location_sensor_poll_interval||120)*1e3,{data:p}=f({queryKey:[`locationHaSensors`],queryFn:()=>o.getLocationHASensors()}),m=(p??[]).some(t=>t.location_id===e),{data:h}=f({queryKey:[`locationHaSensorReadings`,e],queryFn:()=>o.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)=>(Vr[e.device_class??``]??99)-(Vr[t.device_class??``]??99));return(0,Z.jsxs)(`div`,{className:`flex items-center gap-2 pt-3 border-t border-bambu-dark-tertiary text-xs text-bambu-gray ${r?`-mb-1`:``}`,children:[t&&(0,Z.jsx)(`span`,{className:`truncate`,children:t}),(0,Z.jsx)(`span`,{className:`text-bambu-gray/40`,children:`|`}),(0,Z.jsx)(`div`,{className:`flex items-center gap-3`,children:y.map((e,t)=>{let n=_(e),r=t===0?`ml-[-3.6px]`:``;return(0,Z.jsxs)(`span`,{title:e.name,className:`flex items-center gap-[3px]`,children:[(0,Z.jsx)(n,{className:`w-3 h-3 ${Hr(e.device_class)} ${r}`}),(0,Z.jsx)(`span`,{className:Wr(e,i,a,s,c),children:Ur(e,l)})]},e.id)})}),v&&(()=>{let e=_(v);return(0,Z.jsxs)(`span`,{title:v.name,className:`flex items-center gap-[3px] ml-auto`,children:[(0,Z.jsx)(e,{className:`w-3 h-3 ${Hr(v.device_class)}`}),(0,Z.jsx)(`span`,{className:Wr(v,i,a,s,c),children:Ur(v,l)})]},v.id)})()]})}function Kr({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,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer ${e.archived_at?`opacity-50`:``} ${r?`bg-bambu-green/10`:``}`,onClick:a,children:[(0,Z.jsx)(`td`,{className:`w-10 px-3 py-3`,onClick:e=>e.stopPropagation(),children:i&&(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":y(`inventory.bulk.selectRow`),checked:!!r,onChange:i})}),f.map(r=>(0,Z.jsx)(`td`,{className:`py-3 px-4`,children:Mr[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,Z.jsx)(`td`,{className:`py-3 px-4`,children:(0,Z.jsxs)(`div`,{className:`flex items-center justify-end gap-1`,onClick:e=>e.stopPropagation(),children:[(0,Z.jsx)(`button`,{onClick:a,className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:y(`common.edit`),children:(0,Z.jsx)(me,{className:`w-4 h-4`})}),o&&(0,Z.jsx)(`button`,{onClick:o,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:y(`inventory.copySpool`),children:(0,Z.jsx)(N,{className:`w-4 h-4`})}),u&&(0,Z.jsx)(`button`,{onClick:u,className:`p-1.5 text-bambu-gray hover:text-white rounded transition-colors`,title:y(`inventory.labels.printOne`),children:(0,Z.jsx)(xe,{className:`w-4 h-4`})}),d&&e.weight_used>0&&(0,Z.jsx)(`button`,{onClick:d,className:`p-1.5 text-bambu-gray hover:text-orange-600 dark:hover:text-orange-400 rounded transition-colors`,title:y(`inventory.resetConsumedCounterTooltip`),children:(0,Z.jsx)(w,{className:`w-4 h-4`})}),e.archived_at?(0,Z.jsx)(`button`,{onClick:s,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors`,title:y(`inventory.restore`),children:(0,Z.jsx)(we,{className:`w-4 h-4`})}):(0,Z.jsx)(`button`,{onClick:c,className:`p-1.5 text-bambu-gray hover:text-yellow-600 dark:hover:text-yellow-400 rounded transition-colors`,title:y(`inventory.archive`),children:(0,Z.jsx)(v,{className:`w-4 h-4`})}),(0,Z.jsx)(`button`,{onClick:l,className:`p-1.5 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 rounded transition-colors`,title:y(`common.delete`),children:(0,Z.jsx)(z,{className:`w-4 h-4`})})]})})]})}function qr({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,Z.jsxs)(Z.Fragment,{children:[(0,Z.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer bg-bambu-green/5`,onClick:a,children:[(0,Z.jsx)(`td`,{className:`w-10 px-3 py-3`,onClick:e=>e.stopPropagation(),children:O&&(0,Z.jsx)(`input`,{type:`checkbox`,className:`h-4 w-4 cursor-pointer`,"aria-label":y(`inventory.bulk.selectGroup`),checked:k,onChange:t=>O(e.map(e=>e.id),t.target.checked)})}),p.map((a,o)=>(0,Z.jsx)(`td`,{className:`py-3 px-4`,children:o===0?(0,Z.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,Z.jsx)(D,{className:`w-4 h-4 text-bambu-gray transition-transform ${i?``:`-rotate-90`}`}),Mr[a]?.({spool:t,remaining:n,pct:r,assignmentMap:m,catalogMap:h,locationReadingsMap:g,currencySymbol:_,dateFormat:v,t:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:w})]}):a===`id`?(0,Z.jsx)(`span`,{className:`text-xs font-medium bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full`,children:y(`inventory.groupedSpools`,{count:e.length})}):Mr[a]?.({spool:t,remaining:n,pct:r,assignmentMap:m,catalogMap:h,locationReadingsMap:g,currencySymbol:_,dateFormat:v,t:y,onSyncWeight:b,colorizeLocationSensors:x,locationSensorAboveColor:S,locationSensorBelowColor:C,locationSensorOptimalColor:w})},a)),(0,Z.jsx)(`td`,{className:`py-3 px-4`,children:(0,Z.jsx)(`span`,{className:`text-xs text-bambu-gray`,children:e.map(e=>`#${e.id}`).join(`, `)})})]}),i&&e.map(e=>{let t=Math.max(0,e.label_weight-e.weight_used);return(0,Z.jsx)(Kr,{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 Jr({hasFilters:e,onAddSpool:t,t:n}){return(0,Z.jsxs)(`div`,{className:`flex flex-col items-center justify-center py-16 px-4`,children:[(0,Z.jsxs)(`div`,{className:`relative mb-6`,children:[(0,Z.jsx)(`div`,{className:`absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl`}),(0,Z.jsxs)(`div`,{className:`relative flex items-center justify-center w-24 h-24 rounded-2xl bg-gradient-to-br from-bambu-dark-secondary to-bambu-dark-tertiary border border-bambu-dark-tertiary shadow-lg`,children:[(0,Z.jsx)(`div`,{className:`absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30`}),(0,Z.jsx)(`div`,{className:`absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20`}),e?(0,Z.jsx)(Ee,{className:`w-10 h-10 text-bambu-gray/40`,strokeWidth:1.5}):(0,Z.jsxs)(`div`,{className:`relative`,children:[(0,Z.jsx)(`div`,{className:`w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center`,children:(0,Z.jsx)(`div`,{className:`w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20`})}),(0,Z.jsx)(`div`,{className:`absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-bambu-green flex items-center justify-center shadow-md`,children:(0,Z.jsx)(`span`,{className:`text-white text-lg font-bold leading-none`,children:`+`})})]})]})]}),(0,Z.jsx)(`h3`,{className:`text-lg font-semibold text-white mb-2 text-center`,children:e?n(`inventory.noSpoolsMatch`):n(`inventory.noSpools`).split(`.`)[0]}),(0,Z.jsx)(`p`,{className:`text-sm text-bambu-gray text-center max-w-sm mb-6`,children:n(e?`inventory.noSpoolsMatchDesc`:`inventory.noSpools`)}),!e&&(0,Z.jsxs)(W,{onClick:t,children:[(0,Z.jsx)(fe,{className:`w-4 h-4`}),n(`inventory.addSpool`)]})]})}export{Lr as default};
|