u>=1e3?`${(u/1e3).toFixed(1)}kg`:`${u}g`,width:44}),r.jsx(ru,{contentStyle:{background:"#1a1a2e",border:"1px solid #374151",borderRadius:8,fontSize:11},labelStyle:{color:"#9CA3AF"},formatter:(u,m)=>typeof u!="number"?"":m==="stock"?`${u}g — ${s("forecast.stock")}`:m==="rop"?`${u}g — ${s("forecast.reorderPoint")}`:m==="safetyStock"?`${u}g — ${s("forecast.safetyMarginLabel")}`:`${u}`}),r.jsx(qA,{type:"linear",dataKey:"stock",stroke:"#1DB954",strokeWidth:2,fill:`url(#cart-post-${t.id})`,dot:!1,activeDot:{r:3}}),e.reorderPointG>0&&r.jsx(nu,{y:e.reorderPointG,stroke:"#F59E0B",strokeDasharray:"5 3",strokeOpacity:.8,label:{value:"ROP",position:"insideTopRight",fill:"#F59E0B",fontSize:9}}),e.safetyStockG>0&&r.jsx(nu,{y:e.safetyStockG,stroke:"#6B7280",strokeDasharray:"3 3",strokeOpacity:.6,label:{value:"SS",position:"insideTopRight",fill:"#6B7280",fontSize:9}}),r.jsx(nu,{x:Aw(Gm(new Date,l.lt)),stroke:"#3B82F6",strokeWidth:1.5,strokeDasharray:"4 3",strokeOpacity:.9,label:{value:`+${l.arrivalG>=1e3?`${(l.arrivalG/1e3).toFixed(1)}kg`:`${Math.round(l.arrivalG)}g`} arrives (d${l.lt})`,position:"insideTopLeft",fill:"#3B82F6",fontSize:9}}),c!==null&&r.jsx(nu,{x:Aw(Gm(new Date,c)),stroke:"#EF4444",strokeWidth:1.5,strokeOpacity:.9,label:{value:"OUT",position:"insideTopLeft",fill:"#EF4444",fontSize:9}})]})}),r.jsxs("div",{className:"flex flex-wrap items-center gap-3 mt-2 text-[10px] text-bambu-gray",children:[r.jsxs("span",{className:"flex items-center gap-1",children:[r.jsx("span",{className:"inline-block w-4 border-t-2 border-yellow-400 border-dashed"})," ",s("forecast.ropLabel")]}),r.jsxs("span",{className:"flex items-center gap-1",children:[r.jsx("span",{className:"inline-block w-4 border-t border-bambu-gray border-dashed"})," ",s("forecast.safetyStockLegend")]}),r.jsxs("span",{className:"flex items-center gap-1",children:[r.jsx("span",{className:"inline-block w-4 border-t-2 border-blue-400 border-dashed"})," ",s("forecast.stockArrivalLegend")]}),d&&r.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[r.jsx("span",{className:"inline-block w-4 border-t-2 border-red-400"})," ",s("forecast.stockoutLegend")]})]})]}):null]})}function Est({forecast:t,onClose:e,onAdd:a}){const{t:n}=vt(),i=[t.group.brand,t.group.material,t.group.subtype].filter(Boolean).join(" "),[s,o]=y.useState("qty"),[l,c]=y.useState("1"),[d,u]=y.useState("30"),[m,p]=y.useState(""),f=y.useMemo(()=>{if(!t.dailyRateG||t.dailyRateG<=0)return null;const b=t.dailyRateG*Number(d),x=t.group.spools.length>0?t.group.spools.reduce((S,N)=>S+N.label_weight,0)/t.group.spools.length:1e3;return Math.ceil(b/x)},[t,d]),g=s==="qty"?parseInt(l,10)||1:f??1;function w(b){b.preventDefault(),a({material:t.group.material,subtype:t.group.subtype,brand:t.group.brand,quantity_spools:g,note:m||null})}return r.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4",children:r.jsxs("div",{className:"bg-bambu-dark-secondary rounded-2xl border border-bambu-dark-tertiary w-full max-w-sm shadow-2xl",children:[r.jsxs("div",{className:"flex items-center justify-between px-5 pt-5 pb-4 border-b border-bambu-dark-tertiary",children:[r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsx(mb,{className:"w-5 h-5 text-bambu-green"}),r.jsx("h2",{className:"text-base font-semibold text-white",children:n("forecast.addToCartTitle")})]}),r.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white transition-colors",children:r.jsx($t,{className:"w-5 h-5"})})]}),r.jsxs("form",{onSubmit:w,className:"p-5 space-y-4",children:[r.jsx("div",{className:"text-sm text-bambu-gray",children:i}),r.jsxs("div",{className:"flex bg-bambu-dark-tertiary rounded-lg p-0.5",children:[r.jsx("button",{type:"button",onClick:()=>o("qty"),className:`flex-1 py-1.5 text-xs font-medium rounded-md transition-colors ${s==="qty"?"bg-bambu-dark-secondary text-white shadow":"text-bambu-gray hover:text-white"}`,children:n("forecast.byQuantity")}),r.jsx("button",{type:"button",onClick:()=>o("duration"),className:`flex-1 py-1.5 text-xs font-medium rounded-md transition-colors ${s==="duration"?"bg-bambu-dark-secondary text-white shadow":"text-bambu-gray hover:text-white"}`,children:n("forecast.byDuration")})]}),s==="qty"?r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("label",{className:"text-xs text-bambu-gray",children:n("forecast.numberOfSpools")}),r.jsx("input",{type:"number",min:1,max:99,value:l,onChange:b=>c(b.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})]}):r.jsxs("div",{className:"space-y-2",children:[r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("label",{className:"text-xs text-bambu-gray",children:n("forecast.lastHowManyDays")}),r.jsx("input",{type:"number",min:1,max:365,value:d,onChange:b=>u(b.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})]}),t.dailyRateG!==null?r.jsxs("div",{className:"flex items-center gap-2 px-3 py-2 bg-bambu-dark-tertiary/50 rounded-lg",children:[r.jsx("span",{className:"text-xs text-bambu-gray",children:"≈"}),r.jsx("span",{className:"text-sm font-semibold text-bambu-green",children:n("forecast.spoolCount",{count:f??0})}),r.jsxs("span",{className:"text-xs text-bambu-gray",children:["at ",t.dailyRateG.toFixed(1),"g/day"]})]}):r.jsx("div",{className:"text-xs text-yellow-400 px-1",children:n("forecast.noUsageQty")})]}),r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("label",{className:"text-xs text-bambu-gray",children:n("forecast.noteOptional")}),r.jsx("input",{type:"text",maxLength:200,value:m,onChange:b=>p(b.target.value),placeholder:n("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"})]}),r.jsxs("div",{className:"flex items-center gap-3 pt-1",children:[r.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:n("forecast.addNSpools",{count:g})}),r.jsx("button",{type:"button",onClick:e,className:"px-4 py-2 text-sm text-bambu-gray hover:text-white border border-bambu-dark-tertiary rounded-lg transition-colors",children:n("forecast.cancel")})]})]})]})})}const sO={material:"",subtype:"",brand:"",color_name:"",rgba:"808080FF",extra_colors:"",effect_type:"",label_weight:1e3,core_weight:250,core_weight_catalog_id:null,weight_used:0,slicer_filament:"",note:"",cost_per_kg:null,category:"",low_stock_threshold_pct:null,storage_location:"",spoolman_filament_id:null},Dst=new Set(["material","subtype","brand","rgba","color_name","label_weight"]);function mde(t,e=!1,a=!1){const n={};return e||a?(!t.material&&!t.spoolman_filament_id&&(n.material="Material is required"),{isValid:Object.keys(n).length===0,errors:n}):(t.slicer_filament||(n.slicer_filament="Slicer preset is required"),t.material||(n.material="Material is required"),t.brand||(n.brand="Brand is required"),t.subtype||(n.subtype="Subtype is required"),{isValid:Object.keys(n).length===0,errors:n})}const pde=["PLA","PETG","ABS","TPU","ASA","PC","PA","PVA","HIPS","PA-CF","PETG-CF","PLA-CF"],Fst=["Bambu","PolyLite","PolyTerra","eSUN","Overture","Fiberon","SUNLU","Inland","Hatchbox","Generic"],jw=["Basic","Matte","Silk","Silk+","Tough","Tough+","HF","High Flow","Engineering","CF","GF","Galaxy","Glow","Marble","Metal","Rainbow","Sparkle","Wood","Translucent","Transparent","Clear","Lite","Pro","Plus","Max","Super","Ultra","Flex","Soft","Hard","Strong","Impact","Heat Resistant","UV Resistant","ESD","Conductive","Magnetic","Gradient","Dual Color","Tri Color","Multicolor"],hde=[{name:"Black",hex:"000000"},{name:"White",hex:"FFFFFF"},{name:"Gray",hex:"808080"},{name:"Red",hex:"FF0000"},{name:"Orange",hex:"FFA500"},{name:"Yellow",hex:"FFFF00"},{name:"Green",hex:"00AE42"},{name:"Blue",hex:"0066FF"},{name:"Purple",hex:"8B00FF"},{name:"Pink",hex:"FF69B4"},{name:"Brown",hex:"8B4513"},{name:"Silver",hex:"C0C0C0"}],Lst=[{name:"Dark Red",hex:"8B0000"},{name:"Crimson",hex:"DC143C"},{name:"Coral",hex:"FF7F50"},{name:"Salmon",hex:"FA8072"},{name:"Dark Orange",hex:"FF8C00"},{name:"Peach",hex:"FFDAB9"},{name:"Gold",hex:"FFD700"},{name:"Khaki",hex:"F0E68C"},{name:"Lemon",hex:"FFF44F"},{name:"Lime",hex:"32CD32"},{name:"Forest Green",hex:"228B22"},{name:"Olive",hex:"808000"},{name:"Mint",hex:"98FF98"},{name:"Teal",hex:"008080"},{name:"Navy",hex:"000080"},{name:"Sky Blue",hex:"87CEEB"},{name:"Royal Blue",hex:"4169E1"},{name:"Cyan",hex:"00FFFF"},{name:"Turquoise",hex:"40E0D0"},{name:"Violet",hex:"EE82EE"},{name:"Magenta",hex:"FF00FF"},{name:"Indigo",hex:"4B0082"},{name:"Lavender",hex:"E6E6FA"},{name:"Plum",hex:"DDA0DD"},{name:"Hot Pink",hex:"FF69B4"},{name:"Rose",hex:"FF007F"},{name:"Blush",hex:"FFB6C1"},{name:"Chocolate",hex:"D2691E"},{name:"Tan",hex:"D2B48C"},{name:"Beige",hex:"F5F5DC"},{name:"Maroon",hex:"800000"},{name:"Dark Gray",hex:"404040"},{name:"Light Gray",hex:"D3D3D3"},{name:"Charcoal",hex:"36454F"},{name:"Ivory",hex:"FFFFF0"},{name:"Bambu Green",hex:"00AE42"},{name:"Jade White",hex:"E8E8E8"},{name:"Titan Gray",hex:"5A5A5A"}],FX=[...hde,...Lst],fde="bambuddy-recent-colors",Rst=8,Ist=[{code:"GFL00",name:"Bambu PLA Basic",displayName:"Bambu PLA Basic",isCustom:!1,allCodes:["GFL00"]},{code:"GFL01",name:"Bambu PLA Matte",displayName:"Bambu PLA Matte",isCustom:!1,allCodes:["GFL01"]},{code:"GFL05",name:"Generic PLA",displayName:"Generic PLA",isCustom:!1,allCodes:["GFL05"]},{code:"GFG00",name:"Bambu PETG Basic",displayName:"Bambu PETG Basic",isCustom:!1,allCodes:["GFG00"]},{code:"GFG05",name:"Generic PETG",displayName:"Generic PETG",isCustom:!1,allCodes:["GFG05"]},{code:"GFB00",name:"Bambu ABS Basic",displayName:"Bambu ABS Basic",isCustom:!1,allCodes:["GFB00"]},{code:"GFB05",name:"Generic ABS",displayName:"Generic ABS",isCustom:!1,allCodes:["GFB05"]},{code:"GFA00",name:"Bambu ASA Basic",displayName:"Bambu ASA Basic",isCustom:!1,allCodes:["GFA00"]},{code:"GFU00",name:"Bambu TPU 95A",displayName:"Bambu TPU 95A",isCustom:!1,allCodes:["GFU00"]},{code:"GFU05",name:"Generic TPU",displayName:"Generic TPU",isCustom:!1,allCodes:["GFU05"]},{code:"GFC00",name:"Bambu PC Basic",displayName:"Bambu PC Basic",isCustom:!1,allCodes:["GFC00"]},{code:"GFN00",name:"Bambu PA Basic",displayName:"Bambu PA Basic",isCustom:!1,allCodes:["GFN00"]},{code:"GFN05",name:"Generic PA",displayName:"Generic PA",isCustom:!1,allCodes:["GFN05"]},{code:"GFS00",name:"Bambu PLA-CF",displayName:"Bambu PLA-CF",isCustom:!1,allCodes:["GFS00"]},{code:"GFT00",name:"Bambu PETG-CF",displayName:"Bambu PETG-CF",isCustom:!1,allCodes:["GFT00"]},{code:"GFNC0",name:"Bambu PA-CF",displayName:"Bambu PA-CF",isCustom:!1,allCodes:["GFNC0"]},{code:"GFV00",name:"Bambu PVA",displayName:"Bambu PVA",isCustom:!1,allCodes:["GFV00"]}];function rf(t){let e=t.replace(/@.*$/,"").trim();e=e.replace(/\(Custom\)/i,"").trim(),e=e.replace(/^[#*]+\s*/,"").trim();const a=["PLA-CF","PETG-CF","ABS-GF","ASA-CF","PA-CF","PAHT-CF","PA6-CF","PA6-GF","PPA-CF","PPA-GF","PET-CF","PPS-CF","PC-CF","PC-ABS","ABS-GF","PCTG","PETG","PLA","ABS","ASA","PC","PA","TPU","PVA","HIPS","BVOH","PPS","PEEK","PEI"];let n="",i=-1;for(const c of a){const d=e.toUpperCase().indexOf(c.toUpperCase());if(d!==-1){n=c,i=d;break}}let s="";i>0&&(s=e.substring(0,i).trim(),s=s.replace(/[-_\s]+$/,""));let o="";i!==-1&&n&&(o=e.substring(i+n.length).trim(),o=o.replace(/^[-_\s]+/,""));let l="";for(const c of jw)if(o.toLowerCase().includes(c.toLowerCase())){l=c;break}if(!l&&s)for(const c of jw){const d=new RegExp(`\\s+${c}$`,"i");if(d.test(s)){l=c,s=s.replace(d,"").trim();break}}return{brand:s,material:n,variant:l}}function gde(t,e){const a=new Set(Fst);for(const n of t){const{brand:i}=rf(n.name);i&&i.length>1&&a.add(i)}if(e)for(const n of e)if(n.filament_vendor&&n.filament_vendor.length>1)a.add(n.filament_vendor);else{const{brand:i}=rf(n.name);i&&i.length>1&&a.add(i)}return Array.from(a).sort((n,i)=>n.localeCompare(i))}function Ost(t){const e=t.filter(n=>n.preset_type==="filament");return e.length===0?[]:e.map(n=>{const i=String(n.id),s=n.filament_type||i,o=Array.from(new Set([i,s]));return{code:i,name:n.name,displayName:n.name,isCustom:!1,allCodes:o}}).sort((n,i)=>n.displayName.localeCompare(i.displayName))}function bde(t,e,a,n){const i=[],s=[],o=new Set;for(const u of t)if(u.is_custom){const m=u.name.toUpperCase();(e.size===0||Array.from(e).some(f=>m.includes(f))||!m.includes("@"))&&(i.push({code:u.setting_id,name:u.name,displayName:`${u.name} (Custom)`,isCustom:!0,allCodes:[u.setting_id]}),o.add(u.setting_id))}else s.push({code:u.setting_id,name:u.name,displayName:u.name,isCustom:!1,allCodes:[u.setting_id]}),o.add(u.setting_id);const l=a&&a.length>0?Ost(a):[],c=[];if(n&&n.length>0)for(const u of n){const m=u.filament_id.startsWith("GF")?"GFS"+u.filament_id.slice(2):u.filament_id;o.has(u.filament_id)||o.has(m)||c.push({code:u.filament_id,name:u.name,displayName:u.name,isCustom:!1,allCodes:[u.filament_id,m]})}const d=[...i,...s,...l,...c];return d.length===0?Ist:d.sort((u,m)=>u.displayName.localeCompare(m.displayName))}function xde(t,e){if(!t)return;let a=e.find(n=>n.code===t);if(a||(a=e.find(n=>n.allCodes.includes(t))),!a){const n=t.toLowerCase();a=e.find(i=>i.code.toLowerCase()===n||i.allCodes.some(s=>s.toLowerCase()===n))}return a}function yde(){try{const t=localStorage.getItem(fde);if(t)return JSON.parse(t)}catch{}return[]}function vde(t,e){const a=e.filter(i=>i.hex.toUpperCase()!==t.hex.toUpperCase()),n=[t,...a].slice(0,Rst);try{localStorage.setItem(fde,JSON.stringify(n))}catch{}return n}function P3(t,e){if(!e.material)return!1;const n=(t.name||"").replace(/^High Flow[_\s]+/i,"").replace(/^Standard[_\s]+/i,"").replace(/^HF[_\s]+/i,"").replace(/^S[_\s]+/i,"").trim(),i=rf(n);return!(!(i.material.toUpperCase()===e.material.toUpperCase())||e.brand&&!(i.brand.toLowerCase().includes(e.brand.toLowerCase())||e.brand.toLowerCase().includes(i.brand.toLowerCase()))||e.subtype&&!(i.variant.toLowerCase().includes(e.subtype.toLowerCase())||e.subtype.toLowerCase().includes(i.variant.toLowerCase())||n.toLowerCase().includes(e.subtype.toLowerCase())))}function wde({formData:t,updateField:e,cloudAuthenticated:a,loadingCloudPresets:n,presetInputValue:i,setPresetInputValue:s,selectedPresetOption:o,filamentOptions:l,availableBrands:c,availableMaterials:d,quickAdd:u,quantity:m,onQuantityChange:p,errors:f}){const{t:g}=vt(),[w,b]=y.useState(!1),[x,S]=y.useState(!1),[N,C]=y.useState(!1),[P,T]=y.useState(!1),[j,F]=y.useState(""),[k,M]=y.useState(""),[q,I]=y.useState(""),[O,L]=y.useState(String(t.label_weight)),[X,ee]=y.useState(!1),Q=y.useRef(null),le=y.useRef(null),de=y.useRef(null),me=y.useRef(null);y.useEffect(()=>{const H=J=>{Q.current&&!Q.current.contains(J.target)&&b(!1),me.current&&!me.current.contains(J.target)&&T(!1),le.current&&!le.current.contains(J.target)&&S(!1),de.current&&!de.current.contains(J.target)&&C(!1)};return document.addEventListener("mousedown",H),()=>document.removeEventListener("mousedown",H)},[]);const Z=y.useMemo(()=>{if(!i)return l;const H=i.toLowerCase();return l.filter(J=>J.displayName.toLowerCase().includes(H)||J.code.toLowerCase().includes(H))},[l,i]),ce=y.useMemo(()=>{if(!j)return c;const H=j.toLowerCase();return c.filter(A=>A.toLowerCase().includes(H)).sort((A,E)=>{const B=A.toLowerCase()===H,W=E.toLowerCase()===H;return B&&!W?-1:!B&&W?1:A.localeCompare(E)})},[c,j]),he=y.useMemo(()=>{if(!k)return jw;const H=k.toLowerCase();return jw.filter(J=>J.toLowerCase().includes(H))},[k]),ue=y.useMemo(()=>{if(!q)return d;const H=q.toLowerCase();return d.filter(A=>A.toLowerCase().includes(H)).sort((A,E)=>{const B=A.toLowerCase()===H,W=E.toLowerCase()===H;return B&&!W?-1:!B&&W?1:A.localeCompare(E)})},[q,d]);y.useEffect(()=>{X||L(String(t.label_weight))},[t.label_weight,X]);const xe=H=>{e("slicer_filament",H.code),s(H.displayName),b(!1);const J=rf(H.name);J.material&&e("material",J.material),J.brand&&e("brand",J.brand),J.variant&&e("subtype",J.variant)};return r.jsxs("div",{className:"space-y-4",children:[!u&&r.jsx("div",{className:"flex items-center gap-2 text-xs text-bambu-gray",children:n?r.jsxs(r.Fragment,{children:[r.jsx(pt,{className:"w-3 h-3 animate-spin"})," ",g("inventory.loadingPresets")]}):a?r.jsxs(r.Fragment,{children:[r.jsx(Gh,{className:"w-3 h-3 text-bambu-green"})," ",g("inventory.cloudConnected")]}):r.jsxs(r.Fragment,{children:[r.jsx(Z3,{className:"w-3 h-3"})," ",g("inventory.cloudNotConnected")]})}),!u&&r.jsxs("div",{children:[r.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[g("inventory.slicerPreset")," *"]}),r.jsxs("div",{className:"relative",ref:Q,children:[r.jsx(fr,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),r.jsx("input",{type:"text",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/50 focus:outline-none focus:border-bambu-green",placeholder:g("inventory.searchPresets"),value:i,onChange:H=>{s(H.target.value),b(!0)},onFocus:()=>{b(!0),s("")}}),w&&r.jsx("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto",children:Z.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:g("inventory.noPresetsFound")}):Z.map(H=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary truncate ${o?.code===H.code?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>xe(H),children:H.displayName},`${H.code}::${H.name}`))})]}),o&&r.jsxs("div",{className:"mt-1 text-xs text-bambu-gray",children:[g("inventory.selectedPreset"),": ",r.jsx("span",{className:"font-mono text-bambu-green",children:o.code})]}),f?.slicer_filament&&r.jsx("p",{className:"mt-1 text-xs text-red-400",children:f.slicer_filament})]}),r.jsxs("div",{children:[r.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[g("inventory.material")," *"]}),r.jsxs("div",{className:"relative",ref:me,children:[r.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:g("inventory.selectMaterial"),value:P?q:t.material,onChange:H=>{I(H.target.value),T(!0)},onFocus:()=>{T(!0),I("")}}),r.jsx(Da,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),P&&r.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[ue.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:g("inventory.noResults")}):ue.map(H=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.material===H?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("material",H),T(!1),I("")},children:H},H)),q&&!ue.includes(q)&&r.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:()=>{e("material",q),T(!1),I("")},children:g("inventory.useCustomMaterial",{material:q})})]})]}),f?.material&&r.jsx("p",{className:"mt-1 text-xs text-red-400",children:f.material})]}),r.jsxs("div",{children:[r.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[g("inventory.brand"),!u&&" *"]}),r.jsxs("div",{className:"relative",ref:le,children:[r.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:g("inventory.searchBrand"),value:x?j:t.brand,onChange:H=>{F(H.target.value),S(!0)},onFocus:()=>{S(!0),F("")}}),r.jsx(Da,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),x&&r.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[ce.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:g("inventory.noResults")}):ce.map(H=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.brand===H?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("brand",H),S(!1),F("")},children:H},H)),j&&!ce.includes(j)&&r.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:()=>{e("brand",j),S(!1),F("")},children:g("inventory.useCustomBrand",{brand:j})})]})]}),f?.brand&&r.jsx("p",{className:"mt-1 text-xs text-red-400",children:f.brand})]}),r.jsxs("div",{children:[r.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[g("inventory.subtype"),!u&&" *"]}),r.jsxs("div",{className:"relative",ref:de,children:[r.jsx("input",{type:"text",value:N?k:t.subtype,onChange:H=>{M(H.target.value),C(!0)},onFocus:()=>{C(!0),M("")},placeholder:"Basic, Matte, Silk...",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"}),r.jsx(Da,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),N&&r.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[he.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:g("inventory.noResults")}):he.map(H=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.subtype===H?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("subtype",H),C(!1),M("")},children:H},H)),k&&!jw.some(H=>H.toLowerCase()===k.toLowerCase().trim())&&r.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:()=>{e("subtype",k),C(!1),M("")},children:g("inventory.useCustomBrand",{brand:k})})]})]}),f?.subtype&&r.jsx("p",{className:"mt-1 text-xs text-red-400",children:f.subtype})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:g("inventory.labelWeight")}),r.jsxs("div",{className:"relative",children:[r.jsx("input",{type:"number",className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green",value:O,min:0,onFocus:()=>ee(!0),onChange:H=>L(H.target.value),onBlur:()=>{ee(!1);const H=O.trim(),J=Number(H);if(!H||!Number.isFinite(J)||J<0){L(String(t.label_weight));return}const A=Math.round(J);e("label_weight",A),L(String(A))}}),r.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]})]}),u&&r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:g("inventory.quantity")}),r.jsx("input",{type:"number",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",value:m,min:1,max:100,onChange:H=>{const J=Math.max(1,Math.min(100,parseInt(H.target.value)||1));p(J)}})]})]})}function zst(t){const e=t.split(/[\s,]+/).map(i=>i.trim().replace(/^#/,"")).filter(Boolean),a=[],n=[];for(const i of e)(i.length===6||i.length===8)&&/^[0-9a-fA-F]+$/.test(i)?a.push(i.toLowerCase()):n.push(i);return{value:a.join(","),invalid:n}}function Sde({formData:t,updateField:e,recentColors:a,onColorUsed:n,catalogColors:i}){const{t:s}=vt(),[o,l]=y.useState(!1),[c,d]=y.useState(""),u=t.rgba.replace("#","").substring(0,6),[m,p]=y.useState(u);y.useEffect(()=>{p(u)},[u]);const f=M=>u.toUpperCase()===M.toUpperCase(),g=(M,q,I,O)=>{if(e("rgba",M.toUpperCase()+"FF"),e("color_name",q),I!==void 0){const L=I??"";C(L),T([]),j.current=L,e("extra_colors",L)}O!==void 0&&e("effect_type",O??""),n({name:q,hex:M})},w=y.useMemo(()=>{if(i.length===0)return[];const M=t.brand?.trim(),q=t.material?.toLowerCase().trim(),I=t.subtype?.toLowerCase().trim();if(!M&&!q)return[];const O=M?M.toLowerCase().split(/[\s\-_]+/).filter(ee=>ee.length>=2):[],L=ee=>{if(O.length===0)return!0;const Q=ee.toLowerCase();return O.some(le=>Q.includes(le))};if(M&&!q){const ee=i.filter(Q=>L(Q.manufacturer));if(ee.length>0)return ee.map(Q=>({name:Q.color_name,hex:Q.hex_color.replace("#","").substring(0,6),manufacturer:Q.manufacturer,material:typeof Q.material=="string"?Q.material:void 0,extra_colors:Q.extra_colors??null,effect_type:Q.effect_type??null}))}const X=q&&I?`${q} ${I}`:"";if(X){const ee=i.filter(le=>L(le.manufacturer)&&le.material?.toLowerCase()===X);if(ee.length>0)return ee.map(le=>({name:le.color_name,hex:le.hex_color.replace("#","").substring(0,6),manufacturer:le.manufacturer,material:typeof le.material=="string"?le.material:void 0,extra_colors:le.extra_colors??null,effect_type:le.effect_type??null}));const Q=X.replace(/\+$/,"");if(Q!==X){const le=i.filter(de=>L(de.manufacturer)&&de.material?.toLowerCase()===Q);if(le.length>0)return le.map(de=>({name:de.color_name,hex:de.hex_color.replace("#","").substring(0,6),manufacturer:de.manufacturer,material:typeof de.material=="string"?de.material:void 0}))}}if(q){const ee=i.filter(Q=>L(Q.manufacturer)&&(!Q.material||Q.material.toLowerCase().startsWith(q)));if(ee.length>0)return ee.map(Q=>({name:Q.color_name,hex:Q.hex_color.replace("#","").substring(0,6),manufacturer:Q.manufacturer,material:typeof Q.material=="string"?Q.material:void 0,extra_colors:Q.extra_colors??null,effect_type:Q.effect_type??null}))}return[]},[i,t.brand,t.material,t.subtype]),b=y.useMemo(()=>{if(!c)return w;if(w.length===0)return[];const M=c.toLowerCase();return w.filter(I=>I.name.toLowerCase().includes(M)||(I.manufacturer?.toLowerCase().includes(M)??!1)||(I.material?.toLowerCase().includes(M)??!1))},[c,w]),x=w.length>0,S=y.useMemo(()=>c?FX.filter(M=>M.name.toLowerCase().includes(c.toLowerCase())):o?FX:hde,[c,o]),[N,C]=y.useState(t.extra_colors),[P,T]=y.useState([]),j=y.useRef(t.extra_colors);y.useEffect(()=>{t.extra_colors!==j.current&&(C(t.extra_colors),T([]),j.current=t.extra_colors)},[t.extra_colors]);const F=y.useMemo(()=>pj({rgba:t.rgba,extraColors:t.extra_colors,effectType:t.effect_type,subtype:t.subtype,effectSize:"bar"}),[t.rgba,t.extra_colors,t.effect_type,t.subtype]),k=M=>{if(C(M),!M.trim()){T([]),j.current="",e("extra_colors","");return}const{value:q,invalid:I}=zst(M);T(I),j.current=q,e("extra_colors",q)};return r.jsxs("div",{className:"space-y-3",children:[r.jsx("div",{className:"h-10 rounded-lg border border-bambu-dark-tertiary",style:F,"data-testid":"color-preview-banner"}),a.length>0&&r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-bambu-gray shrink-0",children:[r.jsx(Za,{className:"w-3 h-3"}),r.jsx("span",{children:s("inventory.recentColors")})]}),r.jsx("div",{className:"flex flex-wrap gap-1.5",children:a.map(M=>r.jsx("button",{type:"button",onClick:()=>g(M.hex,M.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 ${f(M.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${M.hex}`},title:M.name},M.hex))})]}),r.jsxs("div",{className:"relative",children:[r.jsx(fr,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),r.jsx("input",{type:"text",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/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.searchColors"),value:c,onChange:M=>d(M.target.value)})]}),x?r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("span",{className:"text-xs text-bambu-gray",children:c?s("inventory.searchResults"):`${t.brand}${t.material?` ${t.material}`:""}`}),r.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[b.map(M=>r.jsx("button",{type:"button",onClick:()=>g(M.hex,M.name,M.extra_colors,M.effect_type),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 hover:z-20 relative group ${f(M.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${M.hex}`},title:M.manufacturer&&M.material?`${M.name} (${M.manufacturer} — ${M.material})`:M.manufacturer?`${M.name} (${M.manufacturer})`:M.name,children:r.jsx("span",{className:"absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-20 shadow-lg text-white",children:M.manufacturer&&M.material?`${M.name} (${M.manufacturer} — ${M.material})`:M.manufacturer?`${M.name} (${M.manufacturer})`:M.name})},`${M.hex}-${M.name}-${M.manufacturer??""}`)),b.length===0&&r.jsx("p",{className:"text-sm text-bambu-gray py-1",children:s("inventory.noColorsFound")})]})]}):r.jsxs("div",{className:"space-y-1.5",children:[r.jsxs("div",{className:"flex items-center justify-between text-xs text-bambu-gray",children:[r.jsx("span",{children:s(c?"inventory.searchResults":o?"inventory.allColors":"inventory.commonColors")}),!c&&r.jsx("button",{type:"button",onClick:()=>l(!o),className:"flex items-center gap-1 hover:text-white transition-colors",children:o?r.jsxs(r.Fragment,{children:[s("inventory.showLess")," ",r.jsx(al,{className:"w-3 h-3"})]}):r.jsxs(r.Fragment,{children:[s("inventory.showAll")," ",r.jsx(Da,{className:"w-3 h-3"})]})})]}),r.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[S.map(M=>r.jsx("button",{type:"button",onClick:()=>g(M.hex,M.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 hover:z-20 relative group ${f(M.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${M.hex}`},title:M.name,children:r.jsx("span",{className:"absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-20 shadow-lg text-white",children:M.name})},M.hex)),S.length===0&&r.jsx("p",{className:"text-sm text-bambu-gray py-1",children:s("inventory.noColorsFound")})]})]}),r.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:s("inventory.colorName")}),r.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.colorNamePlaceholder"),value:t.color_name,onChange:M=>e("color_name",M.target.value)})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:s("inventory.hexColor")}),r.jsxs("div",{className:"flex gap-2",children:[r.jsxs("div",{className:"relative flex-1",children:[r.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-bambu-gray",children:"#"}),r.jsx("input",{type:"text",className:"w-full pl-7 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm font-mono uppercase focus:outline-none focus:border-bambu-green",placeholder:"RRGGBB",value:m.toUpperCase(),onChange:M=>{const q=M.target.value.replace("#","").replace(/[^0-9A-Fa-f]/g,"").toUpperCase(),I=q.length>6?q.substring(0,6):q;p(I),I.length===6&&e("rgba",I+"FF")},onBlur:()=>{if(m.length>0&&m.length<6){const M=m.padEnd(6,"0");p(M),e("rgba",M+"FF")}}})]}),r.jsx("input",{type:"color",className:"w-11 h-[38px] rounded-lg cursor-pointer border border-bambu-dark-tertiary shrink-0 bg-transparent",value:`#${u}`,onChange:M=>{const q=M.target.value.replace("#","").toUpperCase();e("rgba",q+"FF")},title:s("inventory.pickColor")})]})]})]}),r.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3 pt-2 border-t border-bambu-dark-tertiary/50",children:[r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:s("inventory.extraColorsLabel")}),r.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm font-mono placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.extraColorsPlaceholder"),value:N,onChange:M=>k(M.target.value),"data-testid":"extra-colors-input"}),P.length>0&&r.jsx("p",{className:"text-xs text-red-400 mt-1",children:s("inventory.extraColorsInvalid",{tokens:P.join(", ")})}),!P.length&&r.jsx("p",{className:"text-xs text-bambu-gray/70 mt-1",children:s("inventory.extraColorsHint")})]}),r.jsxs("div",{children:[r.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1 flex items-center gap-1.5",children:[r.jsx(Ig,{className:"w-3.5 h-3.5"}),s("inventory.colorEffectLabel")]}),r.jsxs("div",{className:"flex gap-2 items-stretch",children:[r.jsx("select",{className:"flex-1 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",value:t.effect_type,onChange:M=>e("effect_type",M.target.value),"data-testid":"effect-type-select",children:nO.map(M=>r.jsx("option",{value:M.value,children:s(M.labelKey)},M.value||"none"))}),r.jsx(bU,{rgba:t.rgba,extraColors:t.extra_colors,effectType:t.effect_type,subtype:t.subtype,effectSize:"preview",className:"w-10 h-10",shape:"square"})]})]})]})]})}function Ust({catalog:t,value:e,onChange:a,catalogId:n,onCatalogIdChange:i}){const{t:s}=vt(),[o,l]=y.useState(!1),[c,d]=y.useState(""),u=y.useRef(null),m=y.useRef(null);y.useEffect(()=>{const w=b=>{u.current&&!u.current.contains(b.target)&&l(!1)};return document.addEventListener("mousedown",w),()=>document.removeEventListener("mousedown",w)},[]),y.useEffect(()=>{if(t.length===0)return;const w=t.filter(b=>b.weight===e);if(n){const b=t.find(x=>x.id===n);if(b&&b.weight===e)return}w.length===1?i(w[0].id):w.length===0&&n!==null&&i(null)},[e,t,n,i]);const p=y.useMemo(()=>{if(!c)return t;const w=c.toLowerCase();return t.filter(b=>b.name.toLowerCase().includes(w)||b.weight.toString().includes(w))},[t,c]),f=y.useMemo(()=>t.filter(w=>w.weight===e),[t,e]),g=y.useMemo(()=>{if(o)return c;if(n){const w=t.find(b=>b.id===n);if(w)return w.name}return f.length>0?f[0].name:""},[o,c,n,t,f]);return r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:r.jsxs("span",{className:"flex items-center gap-2",children:[r.jsx(ZZ,{className:"w-3.5 h-3.5 text-bambu-gray"}),s("inventory.coreWeight")]})}),r.jsxs("div",{className:"flex gap-2 items-center",children:[r.jsxs("div",{className:"flex-1 min-w-0 relative",ref:u,children:[r.jsx("input",{ref:m,type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.searchSpoolWeight"),value:g,onFocus:()=>{l(!0),d("")},onChange:w=>{d(w.target.value),l(!0)}}),o&&r.jsx("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto",children:p.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:s("inventory.noResults")}):p.map(w=>r.jsxs("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex justify-between items-center ${(n?w.id===n:w.weight===e)?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{i(w.id),a(w.weight),l(!1),d("")},children:[r.jsx("span",{className:"truncate",children:w.name}),r.jsxs("span",{className:"font-mono text-xs text-bambu-gray ml-2 shrink-0",children:[w.weight,"g"]})]},w.id))})]}),r.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[r.jsx("input",{type:"number",className:"w-16 px-2 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm text-center font-mono focus:outline-none focus:border-bambu-green",value:e,min:0,max:2e3,onChange:w=>{const b=parseInt(w.target.value);!isNaN(b)&&b>=0&&a(b)}}),r.jsx("span",{className:"text-bambu-gray text-sm",children:"g"})]})]})]})}function kde({formData:t,updateField:e,spoolCatalog:a,currencySymbol:n,availableCategories:i,globalLowStockThreshold:s,spoolmanMode:o=!1}){const{t:l}=vt(),{showToast:c}=na(),[d,u]=y.useState(""),[m,p]=y.useState(!1),[f,g]=y.useState(""),[w,b]=y.useState(!1),x=Math.max(0,t.label_weight-t.weight_used),S=t.core_weight+x;return y.useEffect(()=>{m||u(String(S))},[m,S]),y.useEffect(()=>{w||g(String(x))},[w,x]),r.jsxs("div",{className:"space-y-4",children:[o?r.jsx("p",{className:"text-xs text-bambu-gray px-1",children:l("inventory.spoolWeightManagedBySpoolman")}):r.jsx(Ust,{catalog:a,value:t.core_weight,onChange:N=>e("core_weight",N),catalogId:t.core_weight_catalog_id,onCatalogIdChange:N=>e("core_weight_catalog_id",N)}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:l("inventory.currentWeight")}),r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsxs("div",{className:"relative flex-1",children:[r.jsx("input",{type:"number",value:f,min:0,max:t.label_weight,onFocus:()=>b(!0),onChange:N=>{g(N.target.value)},onBlur:()=>{b(!1);const N=f.trim(),C=Number(N);if(!N||!Number.isFinite(C)||C<0||C>t.label_weight){g(String(x));return}const P=Math.round(C);e("weight_used",Math.max(0,t.label_weight-P)),g(String(P))},className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),r.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]}),r.jsxs("span",{className:"text-xs text-bambu-gray shrink-0",children:["/ ",t.label_weight,"g"]})]})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:l("inventory.measuredWeight")}),r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsxs("div",{className:"relative flex-1",children:[r.jsx("input",{type:"number",value:d,min:0,onFocus:()=>p(!0),onChange:N=>{u(N.target.value)},onBlur:()=>{p(!1);const N=d.trim(),C=Number(N),P=t.core_weight,T=t.core_weight+t.label_weight;if(!N||!Number.isFinite(C)||CT){c(l("inventory.measuredWeightError",{min:P,max:T}),"error"),u(String(S));return}const j=Math.round(C),F=Math.max(0,Math.min(t.label_weight,j-t.core_weight));e("weight_used",Math.max(0,t.label_weight-F)),u(String(j))},className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),r.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]}),r.jsxs("span",{className:"text-xs text-bambu-gray shrink-0",children:["/ ",t.core_weight+t.label_weight,"g"]})]})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:l("inventory.costPerKg","Cost per kg")}),r.jsx("div",{className:"flex items-center gap-2",children:r.jsxs("div",{className:"relative flex-1",children:[r.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-bambu-gray text-sm pointer-events-none",children:n}),r.jsx("input",{type:"number",value:t.cost_per_kg??"",min:0,step:.01,placeholder:"0.00",onChange:N=>{const C=N.target.value===""?null:parseFloat(N.target.value);e("cost_per_kg",C)},style:{paddingLeft:`${Math.max(2,n.length*.6+1)}rem`},className:"w-full py-2 pr-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"})]})})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",htmlFor:"spool-category",children:l("inventory.category")}),r.jsx("input",{id:"spool-category",type:"text",list:"spool-category-options",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:l("inventory.categoryPlaceholder"),value:t.category,maxLength:50,onChange:N=>e("category",N.target.value)}),i.length>0&&r.jsx("datalist",{id:"spool-category-options",children:i.map(N=>r.jsx("option",{value:N},N))})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",htmlFor:"spool-low-stock-threshold",children:l("inventory.lowStockThresholdOverride")}),r.jsx("div",{className:"flex items-center gap-2",children:r.jsxs("div",{className:"relative flex-1",children:[r.jsx("input",{id:"spool-low-stock-threshold",type:"number",className:"w-full px-3 py-2 pr-8 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:String(s),value:t.low_stock_threshold_pct??"",min:1,max:99,step:1,onChange:N=>{const C=N.target.value;if(C===""){e("low_stock_threshold_pct",null);return}const P=Number(C);Number.isFinite(P)&&e("low_stock_threshold_pct",Math.min(99,Math.max(1,Math.round(P))))}}),r.jsx("span",{className:"absolute right-3 top-1/2 -translate-y-1/2 text-xs text-bambu-gray pointer-events-none",children:"%"})]})}),r.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:l("inventory.lowStockThresholdOverrideHelp",{global:s})})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:l("inventory.note")}),r.jsx("textarea",{className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green resize-none min-h-[80px]",placeholder:l("inventory.notePlaceholder"),value:t.note,onChange:N=>e("note",N.target.value)})]}),r.jsxs("div",{children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:l("inventory.storageLocation")}),r.jsx("input",{type:"text",maxLength:255,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:l("inventory.storageLocationPlaceholder"),value:t.storage_location,onChange:N=>e("storage_location",N.target.value)})]})]})}function Bst({filaments:t,isLoading:e,selectedId:a,onSelect:n}){const{t:i}=vt(),[s,o]=y.useState(!1),[l,c]=y.useState(""),d=y.useRef(null),u=y.useRef(null),m=y.useMemo(()=>t.find(b=>b.id===a)??null,[t,a]);y.useEffect(()=>{const b=x=>{d.current&&!d.current.contains(x.target)&&(o(!1),c(""))};return document.addEventListener("mousedown",b),()=>document.removeEventListener("mousedown",b)},[]);const p=y.useMemo(()=>{const b=l.toLowerCase().trim();return b?t.filter(x=>x.name.toLowerCase().includes(b)||(x.material?.toLowerCase().includes(b)??!1)||(x.vendor?.name.toLowerCase().includes(b)??!1)||(x.color_name?.toLowerCase().includes(b)??!1)):t},[t,l]);y.useEffect(()=>{s&&u.current?.focus()},[s]);const f=()=>{o(!0),c("")},g=b=>{n(b),o(!1),c("")},w=b=>b?`#${b.replace("#","")}`:"#808080";return r.jsxs("div",{ref:d,className:"relative",children:[r.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:i("inventory.pickFromSpoolmanCatalog")}),r.jsxs("button",{type:"button",onClick:f,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:[e?r.jsx(pt,{className:"w-4 h-4 text-bambu-gray animate-spin shrink-0"}):m?r.jsxs(r.Fragment,{children:[r.jsx("span",{className:"w-4 h-4 rounded-full shrink-0 border border-white/20",style:{backgroundColor:w(m.color_hex)},"aria-label":i("inventory.spoolmanFilamentColorSwatch")}),r.jsxs("span",{className:"text-white text-sm truncate flex-1",children:[m.vendor?.name?`${m.vendor.name} — `:"",m.name]})]}):r.jsxs(r.Fragment,{children:[r.jsx(qr,{className:"w-4 h-4 text-bambu-gray shrink-0"}),r.jsx("span",{className:"text-bambu-gray text-sm flex-1",children:i("inventory.spoolmanFilamentCatalog")})]}),r.jsx(Da,{className:"w-4 h-4 text-bambu-gray shrink-0 ml-auto"})]}),s&&r.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl",children:[r.jsx("div",{className:"p-2 border-b border-bambu-dark-tertiary",children:r.jsx("input",{ref:u,type:"text",value:l,onChange:b=>c(b.target.value),placeholder:i("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"})}),r.jsx("ul",{className:"max-h-56 overflow-y-auto py-1",children:e?r.jsxs("li",{className:"flex items-center justify-center gap-2 py-4 text-bambu-gray text-sm",children:[r.jsx(pt,{className:"w-4 h-4 animate-spin"}),r.jsx("span",{children:i("common.loading","Loading…")})]}):p.length===0?r.jsx("li",{className:"py-4 text-center text-bambu-gray text-sm",children:i("inventory.noSpoolmanFilaments")}):p.map(b=>r.jsx("li",{children:r.jsxs("button",{type:"button",onClick:()=>g(b),className:`w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-bambu-dark-tertiary transition-colors ${b.id===a?"bg-bambu-dark-tertiary":""}`,children:[r.jsx("span",{className:"w-4 h-4 rounded-full shrink-0 border border-white/20",style:{backgroundColor:w(b.color_hex)},"aria-label":i("inventory.spoolmanFilamentColorSwatch")}),r.jsxs("span",{className:"flex-1 min-w-0",children:[r.jsxs("span",{className:"block text-white text-sm truncate",children:[b.vendor?.name?`${b.vendor.name} — `:"",b.name]}),r.jsxs("span",{className:"block text-bambu-gray text-xs truncate",children:[[b.material,b.color_name].filter(Boolean).join(" · "),b.weight?` · ${b.weight}g`:""]})]})]})},b.id))})]})]})}function _de({formData:t,printersWithCalibrations:e,selectedProfiles:a,setSelectedProfiles:n,expandedPrinters:i,setExpandedPrinters:s}){const{t:o}=vt(),l=p=>{s(f=>{const g=new Set(f);return g.has(p)?g.delete(p):g.add(p),g})},c=(p,f,g)=>{const w=`${p}:${f}:${g??"null"}`,b=`${p}:${g??"null"}`;n(x=>{const S=new Set(x);if(S.has(w))S.delete(w);else{for(const N of Array.from(S)){const C=N.split(":");`${C[0]}:${C[2]}`===b&&S.delete(N)}S.add(w)}return S})},d=()=>{const p=new Set;for(const{printer:f,calibrations:g}of e){if(!f.connected)continue;const w=g.filter(x=>P3(x,t)),b=new Map;for(const x of w){const S=`${x.extruder_id??"null"}`;b.has(S)||b.set(S,[]),b.get(S).push(x)}for(const[x,S]of b)if(S.length>0){const C=[...S].sort((P,T)=>T.k_value-P.k_value)[0];p.add(`${f.id}:${C.cali_idx}:${x}`)}}n(p)};if(!t.material)return r.jsx("div",{className:"p-6 bg-bambu-dark rounded-lg text-center",children:r.jsx("p",{className:"text-bambu-gray",children:o("inventory.selectMaterialFirst")})});if(e.length===0)return r.jsx("div",{className:"p-6 bg-bambu-dark rounded-lg text-center",children:r.jsx("p",{className:"text-bambu-gray",children:o("inventory.noPrintersConfigured")})});const u=e.reduce((p,{printer:f,calibrations:g})=>f.connected?p+g.filter(w=>P3(w,t)).length:p,0),m=(p,f)=>{const g=`${p.id}:${f.cali_idx}:${f.extruder_id??"null"}`,w=a.has(g);return r.jsxs("label",{className:`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-all border ${w?"bg-bambu-green/10 border-bambu-green/30":"bg-bambu-dark border-transparent hover:bg-bambu-dark/80"}`,children:[r.jsx("input",{type:"checkbox",checked:w,onChange:()=>c(String(p.id),f.cali_idx,f.extruder_id),className:"w-4 h-4 rounded border-bambu-dark-tertiary text-bambu-green focus:ring-bambu-green"}),r.jsx("div",{className:"flex-1 min-w-0",children:r.jsx("span",{className:`text-sm font-medium ${w?"text-bambu-green":"text-white"}`,children:f.name||f.filament_id})}),r.jsx("div",{className:"flex items-center gap-2 shrink-0",children:r.jsxs("span",{className:"text-xs font-mono px-2 py-0.5 rounded bg-bambu-dark text-bambu-gray",children:["K=",f.k_value.toFixed(3)]})})]},`${f.cali_idx}-${f.extruder_id}`)};return r.jsxs("div",{className:"space-y-4",children:[r.jsxs("div",{className:"flex items-center justify-between",children:[r.jsxs("p",{className:"text-xs text-bambu-gray",children:[o("inventory.matchingFilter"),": ",t.brand||o("inventory.anyBrand")," / ",t.material," / ",t.subtype||o("inventory.anyVariant")]}),u>0&&r.jsxs("button",{type:"button",onClick:d,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",children:[r.jsx(Ig,{className:"w-3.5 h-3.5"}),o("inventory.autoSelect")," (",u,")"]})]}),r.jsx("div",{className:"space-y-3",children:e.map(({printer:p,calibrations:f})=>{const g=i.has(String(p.id)),w=f.filter(C=>P3(C,t)),b=w.length,x=w.some(C=>C.extruder_id!==void 0&&C.extruder_id!==null&&C.extruder_id>0),S=w.filter(C=>C.extruder_id===1),N=w.filter(C=>C.extruder_id===0||C.extruder_id===void 0||C.extruder_id===null);return r.jsxs("div",{className:"border border-bambu-dark-tertiary rounded-lg overflow-hidden",children:[r.jsxs("button",{type:"button",onClick:()=>l(String(p.id)),className:"w-full px-4 py-3 flex items-center justify-between bg-bambu-dark-secondary hover:bg-bambu-dark-tertiary transition-colors",children:[r.jsxs("div",{className:"flex items-center gap-3",children:[g?r.jsx(Da,{className:"w-4 h-4 text-bambu-gray"}):r.jsx(Hn,{className:"w-4 h-4 text-bambu-gray"}),r.jsx("span",{className:"font-medium text-white",children:p.name}),b>0?r.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:[b," ",o(b!==1?"inventory.matches":"inventory.match")]}):r.jsx("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray",children:o("inventory.noMatches")})]}),r.jsx("span",{className:`text-xs px-2 py-1 rounded-full ${p.connected?"bg-green-500/20 text-green-500":"bg-bambu-gray/20 text-bambu-gray"}`,children:p.connected?o("inventory.connected"):o("inventory.offline")})]}),g&&r.jsx("div",{className:"px-4 py-3 space-y-3 bg-bambu-dark border-t border-bambu-dark-tertiary",children:p.connected?b===0?r.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.noKProfilesMatch")}):x?r.jsxs(r.Fragment,{children:[S.length>0&&r.jsxs("div",{className:"space-y-2",children:[r.jsx("p",{className:"text-xs font-medium text-bambu-gray uppercase tracking-wide",children:o("inventory.leftNozzle")}),r.jsx("div",{className:"space-y-2",children:S.map(C=>m(p,C))})]}),N.length>0&&r.jsxs("div",{className:"space-y-2",children:[r.jsx("p",{className:"text-xs font-medium text-bambu-gray uppercase tracking-wide",children:o("inventory.rightNozzle")}),r.jsx("div",{className:"space-y-2",children:N.map(C=>m(p,C))})]})]}):r.jsx("div",{className:"space-y-2",children:w.map(C=>m(p,C))}):r.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.printerOffline")})})]},p.id)})}),a.size>0&&r.jsx("div",{className:"p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:r.jsxs("p",{className:"text-sm text-white",children:[r.jsx("span",{className:"font-semibold",children:a.size})," ",o("inventory.profilesSelected")]})})]})}function qst(t){const e=new Date(t);return e.toLocaleDateString("en-GB",{day:"2-digit",month:"2-digit",year:"2-digit"})+" "+e.toLocaleTimeString("en-GB",{hour:"2-digit",minute:"2-digit"})}const Hst={completed:"text-bambu-green",failed:"text-red-400",aborted:"text-yellow-400"};function $st({spoolId:t}){const{t:e}=vt(),a=ta(),{showToast:n}=na(),{data:i,isLoading:s}=Qe({queryKey:["spool-usage",t],queryFn:()=>re.getSpoolUsageHistory(t)}),o=ot({mutationFn:()=>re.clearSpoolUsageHistory(t),onSuccess:()=>{a.invalidateQueries({queryKey:["spool-usage",t]}),n(e("inventory.historyCleared"),"success")}});return s?r.jsx("div",{className:"flex justify-center py-4",children:r.jsx(pt,{className:"w-5 h-5 animate-spin text-bambu-green"})}):!i||i.length===0?r.jsxs("div",{className:"text-center py-4 text-bambu-gray text-sm",children:[r.jsx(Za,{className:"w-5 h-5 mx-auto mb-2 opacity-50"}),e("inventory.noUsageHistory")]}):r.jsxs("div",{className:"space-y-2",children:[r.jsxs("div",{className:"flex items-center justify-between",children:[r.jsx("h4",{className:"text-sm font-medium text-white",children:e("inventory.usageHistory")}),r.jsxs(je,{variant:"ghost",size:"sm",onClick:()=>o.mutate(),disabled:o.isPending,className:"text-xs text-bambu-gray hover:text-red-400",children:[r.jsx(ea,{className:"w-3 h-3 mr-1"}),e("inventory.clearHistory")]})]}),r.jsx("div",{className:"max-h-48 overflow-y-auto space-y-1",children:i.map(l=>r.jsxs("div",{className:"flex items-center justify-between p-2 rounded bg-bambu-dark/50 text-xs",children:[r.jsxs("div",{className:"flex-1 min-w-0",children:[r.jsx("span",{className:"text-bambu-gray",children:qst(l.created_at)}),l.print_name&&r.jsx("span",{className:"text-white ml-2 truncate",title:l.print_name,children:l.print_name})]}),r.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0 ml-2",children:[r.jsxs("span",{className:"text-white font-medium",children:[l.weight_used.toFixed(1),"g"]}),r.jsxs("span",{className:"text-bambu-gray",children:["(",l.percent_used,"%)"]}),r.jsx("span",{className:Hst[l.status]||"text-bambu-gray",children:l.status})]})]},l.id))})]})}const LX={tag_uid:null,tray_uuid:null,tag_type:null,data_origin:null};function Vst({isOpen:t,onClose:e,spool:a,mode:n,printersWithCalibrations:i=[],currencySymbol:s,onSpoolsCreated:o,spoolmanMode:l=!1,spoolsQueryKey:c=["inventory-spools"]}){const{t:d}=vt(),u=ta(),{showToast:m}=na(),p=n==="edit",f=n==="copy",[g,w]=y.useState(sO),[b,x]=y.useState({}),[S,N]=y.useState("filament"),[C,P]=y.useState(!1),[T,j]=y.useState(!1),[F,k]=y.useState(!1),[M,q]=y.useState(1),[I,O]=y.useState(!1),[L,X]=y.useState(!1),[ee,Q]=y.useState([]),[le,de]=y.useState(""),[me,Z]=y.useState([]),[ce,he]=y.useState([]),[ue,xe]=y.useState([]),[H,J]=y.useState([]),[A,E]=y.useState([]),[B,W]=y.useState([]),[z,ie]=y.useState(new Set),[U,G]=y.useState(new Set),K=i.length>0?i:B,Y=z.size,{data:se=[],isLoading:oe,error:fe}=Qe({queryKey:["spoolman-inventory-filaments"],queryFn:()=>re.getSpoolmanInventoryFilaments(),enabled:l&&t,staleTime:6e4,retry:!1});y.useEffect(()=>{E(yde())},[]),y.useEffect(()=>{t&&((async()=>{X(!0);try{const at=await re.getCloudStatus();if(O(at.is_authenticated),at.is_authenticated){const nt=await re.getFilamentPresets();Q(nt)}}catch(at){console.error("Failed to fetch cloud presets:",at),O(!1)}finally{X(!1)}})(),l||re.getSpoolCatalog().then(Z).catch(console.error),re.getColorCatalog().then(J).catch(console.error),re.getLocalPresets().then(at=>he(at.filament)).catch(console.error),re.getBuiltinFilaments().then(xe).catch(console.error),i.length===0&&(async()=>{try{const at=await re.getPrinters(),nt=await Promise.all(at.map(Bt=>re.getPrinterStatus(Bt.id).catch(()=>null))),At=[];for(let Bt=0;Bt({cali_idx:ut.slot_id,filament_id:ut.filament_id,setting_id:ut.setting_id||"",name:ut.name,k_value:parseFloat(ut.k_value)||0,n_coef:parseFloat(ut.n_coef)||0,extruder_id:ut.extruder_id,nozzle_diameter:ut.nozzle_diameter}))}catch{}At.push({printer:{...gt,connected:Xt},calibrations:ia})}W(At)}catch(at){console.error("Failed to fetch printer calibrations:",at)}})())},[t,i.length]);const ae=y.useMemo(()=>bde(ee,new Set,ce,ue),[ee,ce,ue]),pe=y.useMemo(()=>{const Pe=gde(ee,ce),at=H.map(At=>At.manufacturer?.trim()).filter(At=>!!At),nt=new Set([...Pe,...at]);return Array.from(nt).sort((At,Bt)=>At.localeCompare(Bt))},[ee,ce,H]),$=y.useMemo(()=>{const Pe=H.map(nt=>nt.material?.trim()).filter(nt=>!!nt),at=new Set([...pde,...Pe]);return Array.from(at).sort((nt,At)=>nt.localeCompare(At))},[H]),R=y.useMemo(()=>{const Pe=[];for(const at of H){const nt=at.manufacturer?.trim(),At=at.material?.trim();nt&&At&&Pe.push({brand:nt,material:At})}for(const at of ee){const nt=rf(at.name);nt.brand&&nt.material&&Pe.push({brand:nt.brand,material:nt.material})}for(const at of ce){const nt=rf(at.name),At=at.filament_vendor?.trim()||nt.brand,Bt=nt.material;At&&Bt&&Pe.push({brand:At,material:Bt})}return Pe},[ee,H,ce]),ne=y.useMemo(()=>{const Pe=new Map;for(const at of R){const nt=at.brand.toLowerCase(),At=at.material.toLowerCase();Pe.has(nt)||Pe.set(nt,new Set),Pe.get(nt).add(At)}return Pe},[R]),be=y.useMemo(()=>{const Pe=new Map;for(const at of R){const nt=at.brand.toLowerCase(),At=at.material.toLowerCase();Pe.has(At)||Pe.set(At,new Set),Pe.get(At).add(nt)}return Pe},[R]),ve=y.useMemo(()=>{if(!g.material)return pe;const Pe=g.material.toLowerCase(),at=be.get(Pe);return!at||at.size===0?pe:pe.filter(nt=>at.has(nt.toLowerCase()))},[pe,g.material,be]),ke=y.useMemo(()=>{if(!g.brand)return $;const Pe=g.brand.toLowerCase(),at=ne.get(Pe);return!at||at.size===0?$:$.filter(nt=>at.has(nt.toLowerCase()))},[$,g.brand,ne]),ye=y.useMemo(()=>xde(g.slicer_filament,ae),[g.slicer_filament,ae]);y.useEffect(()=>{if(t){if(a){const Pe=a.rgba&&/^[0-9A-Fa-f]{8}$/.test(a.rgba)?a.rgba:"808080FF";if(w({material:a.material||"",subtype:a.subtype||"",brand:a.brand||"",color_name:a.color_name_is_synthesized?"":a.color_name||"",rgba:Pe,extra_colors:a.extra_colors||"",effect_type:a.effect_type||"",label_weight:a.label_weight||1e3,core_weight:a.core_weight||250,core_weight_catalog_id:a.core_weight_catalog_id??null,weight_used:f?0:a.weight_used||0,slicer_filament:a.slicer_filament||"",note:a.note||"",cost_per_kg:a.cost_per_kg??null,category:a.category||"",low_stock_threshold_pct:a.low_stock_threshold_pct??null,storage_location:a.storage_location||"",spoolman_filament_id:null}),de(a.slicer_filament_name||a.slicer_filament||""),a.k_profiles&&a.k_profiles.length>0){const at=new Set;for(const nt of a.k_profiles)nt.cali_idx!==null&&nt.cali_idx!==void 0&&at.add(`${nt.printer_id}:${nt.cali_idx}:${nt.extruder??"null"}`);ie(at)}else ie(new Set)}else w(sO),de(""),ie(new Set),k(!1),q(1);x({}),N("filament"),P(!1),j(!1)}},[t,a,n,f]),y.useEffect(()=>{t&&K.length>0&&G(new Set(K.map(Pe=>String(Pe.printer.id))))},[t,K]);const ge=(Pe,at)=>{const nt=Dst.has(Pe);l&&nt&&g.spoolman_filament_id!==null&&m(d("inventory.spoolmanFilamentUnlinked"),"info"),w(At=>({...At,[Pe]:at,...l&&nt&&At.spoolman_filament_id!==null?{spoolman_filament_id:null}:{}})),Pe==="weight_used"&&P(!0),Pe==="storage_location"&&j(!0),b[Pe]&&x(At=>({...At,[Pe]:void 0}))},Me=Pe=>{const at=Pe.material||"",nt=Pe.name||"",At=at&&nt.startsWith(at)?nt.slice(at.length).trim():nt,Bt=(Pe.color_hex??"").replace("#","").toUpperCase(),gt=/^[0-9A-F]{6}$/.test(Bt)?Bt:"808080";w(Qt=>({...Qt,spoolman_filament_id:Pe.id,material:at,subtype:At,brand:Pe.vendor?.name||"",rgba:`${gt}FF`,color_name:Pe.color_name||"",label_weight:Pe.weight??Qt.label_weight})),m(d("inventory.spoolmanFilamentSelected"),"success")},Be=Pe=>{E(at=>vde(Pe,at))},Le=ot({mutationFn:Pe=>l?re.createSpoolmanInventorySpool(Pe):re.createSpool(Pe),onSuccess:async Pe=>{Pe?.id&&!await Fe(Pe.id)||(await u.invalidateQueries({queryKey:c}),o&&o([Pe]),m(d("inventory.spoolCreated"),"success"),e())},onError:Pe=>{Pe instanceof cs&&Pe.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Oe=ot({mutationFn:({data:Pe,qty:at})=>l?re.bulkCreateSpoolmanInventorySpools(Pe,at):re.bulkCreateSpools(Pe,at),onSuccess:async Pe=>{const at=l&&"created"in Pe?Pe:null,nt=at?at.created:Pe;if(z.size>0)for(const At of nt)await Fe(At.id);await u.invalidateQueries({queryKey:c}),o&&o(nt),at&&at.failed_count>0?m(d("inventory.spoolsPartiallyCreated",{created:nt.length,total:at.requested_count}),"warning"):m(d("inventory.spoolsCreated",{count:nt.length}),"success"),e()},onError:Pe=>{Pe instanceof cs&&Pe.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Re=ot({mutationFn:Pe=>l?re.updateSpoolmanInventorySpool(a.id,Pe):re.updateSpool(a.id,Pe),onSuccess:async()=>{a?.id&&!await Fe(a.id)||(await u.invalidateQueries({queryKey:c}),m(d("inventory.spoolUpdated"),"success"),e())},onError:Pe=>{Pe instanceof cs&&Pe.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Ke=ot({mutationFn:()=>l?re.updateSpoolmanInventorySpool(a.id,LX):re.updateSpool(a.id,LX),onSuccess:async()=>{await u.invalidateQueries({queryKey:c}),m(d("inventory.rfidCleared","RFID tag cleared"),"success"),e()},onError:Pe=>{Pe instanceof cs&&Pe.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.tagClearFailed"),"error")}}),{data:Ue}=Qe({queryKey:["spool-assignments"],queryFn:()=>re.getAssignments(),enabled:t&&p&&!l}),{data:lt}=Qe({queryKey:["spoolman-slot-assignments-all"],queryFn:()=>re.getSpoolmanSlotAssignments(),enabled:t&&p&&l}),Ee=(()=>{if(a)return l?lt?.find(Pe=>Pe.spoolman_spool_id===a.id):Ue?.find(Pe=>Pe.spool_id===a.id)})(),{data:st}=Qe({queryKey:["inventory-spools"],queryFn:()=>re.getSpools(!0),enabled:t}),{data:Ne}=Qe({queryKey:["settings"],queryFn:re.getSettings,enabled:t}),Ze=(()=>{const Pe=new Set;for(const at of st??[]){const nt=at.category?.trim();nt&&Pe.add(nt)}return Array.from(Pe).sort((at,nt)=>at.localeCompare(nt))})(),Ye=Ne?.low_stock_threshold??20,Te=ot({mutationFn:async()=>{if(!Ee)throw new Error("No assignment");if(l){if(!a)throw new Error("No spool");await re.unassignSpoolmanSlot(a.id);return}await re.unassignSpool(Ee.printer_id,Ee.ams_id,Ee.tray_id)},onSuccess:async()=>{l?(await u.invalidateQueries({queryKey:["spoolman-slot-assignments-all"]}),await u.invalidateQueries({queryKey:["spoolman-slot-assignments"]})):await u.invalidateQueries({queryKey:["spool-assignments"]}),m(d("inventory.unassignSuccess","Spool unassigned"),"success"),e()},onError:Pe=>{m(Pe.message,"error")}}),Fe=async Pe=>{const at=l?re.saveSpoolmanKProfiles:re.saveSpoolKProfiles;if(z.size===0)try{return await at(Pe,[]),!0}catch(Bt){return console.error("Failed to save K-profiles:",Bt),m(d("inventory.kProfileSaveFailed"),"warning"),!1}const nt=[];let At=0;for(const Bt of z){const[gt,Qt,Xt]=Bt.split(":"),ia=parseInt(gt),da=parseInt(Qt),ut=Xt==="null"?0:parseInt(Xt),_t=K.find(Ct=>Ct.printer.id===ia);if(_t){const Ct=_t.calibrations.find(dt=>dt.cali_idx===da);Ct?nt.push({printer_id:ia,extruder:ut,nozzle_diameter:Ct.nozzle_diameter||"0.4",k_value:Ct.k_value,name:Ct.name||null,cali_idx:Ct.cali_idx,setting_id:Ct.setting_id||null}):At++}else At++}if(At>0)return console.error(`saveKProfiles: ${At} profile key(s) could not be resolved`,Array.from(z)),m(d("inventory.kProfileSaveFailed"),"warning"),!1;if(nt.length>0)try{return await at(Pe,nt),!0}catch(Bt){return console.error("Failed to save K-profiles:",Bt),m(d("inventory.kProfileSaveFailed"),"warning"),!1}return!0};if(y.useEffect(()=>{if(!t)return;const Pe=at=>{at.key==="Escape"&&e()};return document.addEventListener("keydown",Pe),()=>document.removeEventListener("keydown",Pe)},[t,e]),!t)return null;const et=()=>{const Pe=mde(g,F,l);if(!Pe.isValid){x(Pe.errors),(Pe.errors.slicer_filament||Pe.errors.material||Pe.errors.brand||Pe.errors.subtype)&&N("filament");return}const at=ye?.displayName||le||null,nt={material:g.material||null,subtype:g.subtype||null,brand:g.brand||null,color_name:g.color_name||null,rgba:g.rgba||null,extra_colors:g.extra_colors||null,effect_type:g.effect_type||null,label_weight:g.label_weight,...l?{}:{core_weight:g.core_weight,core_weight_catalog_id:g.core_weight_catalog_id},slicer_filament:g.slicer_filament||null,slicer_filament_name:at,nozzle_temp_min:null,nozzle_temp_max:null,note:g.note||null,cost_per_kg:g.cost_per_kg,category:g.category.trim()||null,low_stock_threshold_pct:g.low_stock_threshold_pct,...l?{spoolman_filament_id:g.spoolman_filament_id}:{}};(!p||C)&&(nt.weight_used=g.weight_used),(!p||T)&&(nt.storage_location=g.storage_location||null),p?Re.mutate(nt):M>1?Oe.mutate({data:nt,qty:M}):Le.mutate(nt)},We=Le.isPending||Oe.isPending||Re.isPending||Ke.isPending||Te.isPending;return r.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[r.jsx("div",{className:"absolute inset-0 bg-black/60 backdrop-blur-sm",onClick:e}),r.jsxs("div",{className:"relative w-full max-w-xl mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col",children:[r.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0",children:[r.jsxs("h2",{className:"text-lg font-semibold text-white flex items-baseline gap-2",children:[d(p?"inventory.editSpool":f?"inventory.copySpool":"inventory.addSpool"),p&&a&&r.jsxs("span",{className:"text-sm font-mono text-bambu-gray",children:["#",a.id]})]}),r.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white rounded transition-colors",children:r.jsx($t,{className:"w-5 h-5"})})]}),n==="create"&&r.jsxs("div",{className:"flex items-center justify-between px-4 py-2 border-b border-bambu-dark-tertiary flex-shrink-0",children:[r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsx(Nc,{className:"w-4 h-4 text-amber-400"}),r.jsx("span",{className:"text-sm text-white",children:d("inventory.quickAdd")})]}),r.jsx("button",{type:"button",onClick:()=>{k(!F),F||N("filament")},className:`relative inline-flex h-5 w-9 items-center rounded-full transition-colors ${F?"bg-bambu-green":"bg-bambu-dark-tertiary"}`,children:r.jsx("span",{className:`inline-block h-3.5 w-3.5 rounded-full bg-white transition-transform ${F?"translate-x-4":"translate-x-0.5"}`})})]}),r.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary flex-shrink-0",children:[r.jsxs("button",{onClick:()=>N("filament"),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${S==="filament"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[r.jsx(LS,{className:"w-4 h-4"}),d("inventory.filamentInfoTab")]}),!F&&r.jsxs("button",{onClick:()=>N("pa-profile"),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${S==="pa-profile"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[r.jsx(Wfe,{className:"w-4 h-4"}),d("inventory.paProfileTab"),Y>0&&r.jsx("span",{className:"text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:Y})]})]}),r.jsx("div",{className:"p-4 overflow-y-auto flex-1",style:{scrollbarGutter:"stable"},children:S==="filament"?r.jsxs("div",{className:"space-y-6",children:[l&&!p&&r.jsx("div",{children:fe?r.jsx("p",{className:"text-sm text-red-400 px-1",children:d("inventory.spoolmanCatalogLoadFailed")}):r.jsx(Bst,{filaments:se,isLoading:oe,selectedId:g.spoolman_filament_id,onSelect:Me})}),r.jsxs("div",{children:[r.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:d("inventory.filamentInfo")}),r.jsx(wde,{formData:g,updateField:ge,cloudAuthenticated:I,loadingCloudPresets:L,presetInputValue:le,setPresetInputValue:de,selectedPresetOption:ye,filamentOptions:ae,availableBrands:ve,availableMaterials:ke,quickAdd:F,quantity:M,onQuantityChange:q,errors:b})]}),r.jsxs("div",{children:[r.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:d("inventory.color")}),r.jsx(Sde,{formData:g,updateField:ge,recentColors:A,onColorUsed:Be,catalogColors:H})]}),r.jsxs("div",{children:[r.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:d("inventory.additional")}),r.jsx(kde,{formData:g,updateField:ge,spoolCatalog:me,currencySymbol:s,availableCategories:Ze,globalLowStockThreshold:Ye,spoolmanMode:l})]}),p&&a&&!l&&r.jsx("div",{children:r.jsx($st,{spoolId:a.id})})]}):r.jsx(_de,{formData:g,updateField:ge,printersWithCalibrations:K,selectedProfiles:z,setSelectedProfiles:ie,expandedPrinters:U,setExpandedPrinters:G})}),r.jsxs("div",{className:"flex gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0",children:[p&&r.jsxs("div",{className:"flex gap-2 mr-auto",children:[r.jsxs(je,{variant:"secondary",onClick:()=>Ke.mutate(),disabled:We||!a?.tag_uid,children:[r.jsx(Ym,{className:"w-4 h-4"}),d("inventory.clearRfid","Clear RFID Tag")]}),r.jsxs(je,{variant:"secondary",onClick:()=>Te.mutate(),disabled:We||!Ee,children:[r.jsx(su,{className:"w-4 h-4"}),d("inventory.unassignSpool","Unassign")]})]}),r.jsxs("div",{className:"flex gap-2 ml-auto",children:[r.jsx(je,{variant:"secondary",onClick:e,children:d("common.cancel")}),r.jsx(je,{onClick:et,disabled:We,children:We?r.jsxs(r.Fragment,{children:[r.jsx(pt,{className:"w-4 h-4 animate-spin"}),d("common.saving")]}):r.jsxs(r.Fragment,{children:[r.jsx(fs,{className:"w-4 h-4"}),d(p?"common.save":f?"inventory.copySpool":"inventory.addSpool")]})})]})]})]})]})}function Gst({isOpen:t,onClose:e,columns:a,defaultColumns:n,onSave:i}){const{t:s}=vt(),[o,l]=y.useState(a),[c,d]=y.useState(null),u=y.useRef(null);if(y.useEffect(()=>{t&&l(a.map(C=>({...C})))},[t,a]),y.useEffect(()=>{if(!t)return;const C=P=>{P.key==="Escape"&&e()};return window.addEventListener("keydown",C),()=>window.removeEventListener("keydown",C)},[t,e]),!t)return null;const m=C=>{l(P=>P.map((T,j)=>j===C?{...T,visible:!T.visible}:T))},p=(C,P)=>{P<0||P>=o.length||l(T=>{const j=[...T],[F]=j.splice(C,1);return j.splice(P,0,F),j})},f=(C,P)=>{u.current=P,d(P),C.dataTransfer.effectAllowed="move"},g=(C,P)=>{C.preventDefault(),C.dataTransfer.dropEffect="move";const T=u.current;T!==null&&T!==P&&(p(T,P),u.current=P,d(P))},w=C=>{C.preventDefault()},b=()=>{u.current=null,d(null)},x=()=>{l(n.map(C=>({...C})))},S=()=>{i(o),e()},N=o.filter(C=>C.visible).length;return r.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:e,children:r.jsx(Tt,{className:"w-full max-w-md max-h-[80vh] flex flex-col",onClick:C=>C.stopPropagation(),children:r.jsxs(Mt,{className:"p-6 flex flex-col min-h-0",children:[r.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:s("inventory.configureColumns")}),r.jsxs("p",{className:"text-sm text-bambu-gray mb-4",children:[s("inventory.configureColumnsDesc"),r.jsxs("span",{className:"ml-2 text-bambu-gray/60",children:["(",N," ",s("inventory.of")," ",o.length," ",s("inventory.visible"),")"]})]}),r.jsx("div",{className:"space-y-1 overflow-y-auto flex-1 min-h-0 pr-1",children:o.map((C,P)=>r.jsxs("div",{className:`flex items-center gap-2 p-2 rounded-lg border transition-colors ${c===P?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary bg-bambu-dark-tertiary/50"} ${C.visible?"":"opacity-50"}`,draggable:!0,onDragStart:T=>f(T,P),onDragOver:T=>g(T,P),onDrop:w,onDragEnd:b,children:[r.jsx("div",{className:"cursor-grab text-bambu-gray/50 hover:text-bambu-gray",children:r.jsx(Mh,{className:"w-4 h-4"})}),r.jsx("span",{className:"flex-1 font-medium text-sm text-white",children:C.label}),r.jsxs("div",{className:"flex items-center gap-0.5",children:[r.jsx("button",{onClick:()=>p(P,P-1),disabled:P===0,className:"p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed",title:s("inventory.moveUp"),children:r.jsx(al,{className:"w-4 h-4"})}),r.jsx("button",{onClick:()=>p(P,P+1),disabled:P===o.length-1,className:"p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed",title:s("inventory.moveDown"),children:r.jsx(Da,{className:"w-4 h-4"})})]}),r.jsx("button",{onClick:()=>m(P),className:`p-1.5 rounded transition-colors ${C.visible?"text-bambu-green hover:bg-bambu-green/10":"text-bambu-gray/50 hover:bg-bambu-dark-secondary"}`,title:C.visible?s("inventory.hideColumn"):s("inventory.showColumn"),children:C.visible?r.jsx(Ml,{className:"w-4 h-4"}):r.jsx(db,{className:"w-4 h-4"})})]},C.id))}),r.jsxs("div",{className:"flex items-center gap-3 mt-4 pt-4 border-t border-bambu-dark-tertiary",children:[r.jsxs(je,{variant:"secondary",onClick:x,className:"mr-auto",children:[r.jsx(vs,{className:"w-4 h-4"}),s("inventory.reset")]}),r.jsx(je,{variant:"secondary",onClick:e,children:s("inventory.cancel")}),r.jsx(je,{onClick:S,children:s("inventory.applyChanges")})]})]})})})}const Wst=[{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."}];function Kst(t){const e=window.URL.createObjectURL(t);if(!window.open(e,"_blank","noopener,noreferrer")){const n=document.createElement("a");n.href=e,n.download="bambuddy-labels.pdf",document.body.appendChild(n),n.click(),document.body.removeChild(n)}setTimeout(()=>window.URL.revokeObjectURL(e),6e4)}function Qst(t){if(!t)return{backgroundColor:"#808080"};const e=t.replace(/^#/,"").slice(0,6);return e.length===6?{backgroundColor:`#${e}`}:{backgroundColor:"#808080"}}function Yst(t){const e=t.color_name??`${t.material}${t.subtype?` ${t.subtype}`:""}`,a=t.brand?` · ${t.brand}`:"";return`${e}${a}`}function Xst(t){return[t.color_name,t.material,t.subtype,t.brand,`#${t.id}`].filter(Boolean).join(" ").toLowerCase()}function RX(t){if(!t)return[1,0];const e=t.replace(/^#/,"").slice(0,6);if(e.length!==6)return[1,0];const a=parseInt(e.slice(0,2),16),n=parseInt(e.slice(2,4),16),i=parseInt(e.slice(4,6),16);if([a,n,i].some(Number.isNaN))return[1,0];const s=a/255,o=n/255,l=i/255,c=Math.max(s,o,l),d=Math.min(s,o,l),u=(c+d)/2,m=c-d;if((m===0?0:m/(1-Math.abs(2*u-1)))<.1)return[1,u];let f=0;return c===s?f=(o-l)/m%6:c===o?f=(l-s)/m+2:f=(s-o)/m+4,f=f*60,f<0&&(f+=360),[0,f]}function Zst({isOpen:t,onClose:e,availableSpools:a,initialSelectedIds:n,spoolmanMode:i}){const{t:s}=vt(),{showToast:o}=na(),[l,c]=y.useState(null),[d,u]=y.useState(new Set),[m,p]=y.useState(""),[f,g]=y.useState(""),[w,b]=y.useState("id");y.useEffect(()=>{if(t){const I=new Set(a.map(O=>O.id));u(new Set(n.filter(O=>I.has(O)))),p(""),g(""),b("id"),c(null)}},[t]);const x=y.useMemo(()=>{const I=[...a];return w==="color"?(I.sort((O,L)=>{const X=RX(O.rgba),ee=RX(L.rgba);return X[0]!==ee[0]?X[0]-ee[0]:X[1]!==ee[1]?X[1]-ee[1]:O.id-L.id}),I):(I.sort((O,L)=>O.id-L.id),I)},[a,w]),S=y.useMemo(()=>{const I=new Set;for(const O of x)O.material&&I.add(O.material.toUpperCase());return[...I].sort()},[x]),N=y.useMemo(()=>{const I=m.trim().toLowerCase();return x.filter(O=>!(f&&(O.material||"").toUpperCase()!==f||I&&!Xst(O).includes(I)))},[x,m,f]),C=N.length>0&&N.every(I=>d.has(I.id));if(!t)return null;const P=d.size,T=P===0;function j(I){u(O=>{const L=new Set(O);return L.has(I)?L.delete(I):L.add(I),L})}function F(){u(I=>{const O=new Set(I);for(const L of N)O.add(L.id);return O})}function k(){u(I=>{const O=new Set(I);for(const L of N)O.delete(L.id);return O})}function M(){u(new Set)}async function q(I){if(T||l)return;const O=x.filter(L=>d.has(L.id)).map(L=>L.id);c(I);try{const L=i?await re.printSpoolmanSpoolLabels({spool_ids:O,template:I}):await re.printSpoolLabels({spool_ids:O,template:I});Kst(L),e()}catch(L){const X=L instanceof Error?L.message:String(L);o(s("inventory.labels.error","Could not generate labels: {{msg}}",{msg:X}),"error")}finally{c(null)}}return r.jsxs("div",{className:"fixed inset-0 z-50 flex items-start sm:items-center justify-center p-4 overflow-y-auto",children:[r.jsx("div",{className:"absolute inset-0 bg-black/60 backdrop-blur-sm",onClick:e}),r.jsxs("div",{className:"relative w-full max-w-3xl bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] overflow-hidden flex flex-col my-auto",children:[r.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsx(gr,{className:"w-5 h-5 text-bambu-green"}),r.jsx("h2",{className:"text-lg font-semibold text-white",children:s("inventory.labels.title","Print spool labels")}),P>0&&r.jsxs("span",{className:"text-sm text-bambu-gray",children:["(",s("inventory.labels.selectedCount","{{count}} selected",{count:P}),")"]})]}),r.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white rounded transition-colors","aria-label":s("common.close","Close"),children:r.jsx($t,{className:"w-5 h-5"})})]}),r.jsxs("div",{className:"p-4 space-y-2 border-b border-bambu-dark-tertiary",children:[r.jsxs("div",{className:"relative",children:[r.jsx(fr,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),r.jsx("input",{type:"search",value:m,onChange:I=>p(I.target.value),placeholder:s("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"})]}),S.length>1&&r.jsxs("div",{className:"flex flex-wrap items-center gap-1.5",children:[r.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:s("inventory.labels.filterByMaterial","Material:")}),r.jsx("button",{type:"button",onClick:()=>g(""),className:`px-2 py-0.5 text-xs rounded-full border transition ${f===""?"bg-bambu-green text-bambu-dark border-bambu-green":"bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:s("inventory.labels.allMaterials","All")}),S.map(I=>r.jsx("button",{type:"button",onClick:()=>g(I),className:`px-2 py-0.5 text-xs rounded-full border transition ${f===I?"bg-bambu-green text-bambu-dark border-bambu-green":"bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:I},I))]}),r.jsxs("div",{className:"flex flex-wrap items-center gap-1.5",children:[r.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:s("inventory.labels.sortBy.label")}),r.jsx("button",{type:"button",onClick:()=>b("id"),className:`px-2 py-0.5 text-xs rounded-full border transition ${w==="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:s("inventory.labels.sortBy.id")}),r.jsx("button",{type:"button",onClick:()=>b("color"),className:`px-2 py-0.5 text-xs rounded-full border transition ${w==="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:s("inventory.labels.sortBy.color")})]})]}),r.jsxs("div",{className:"px-4 pt-3 pb-2 flex items-center justify-between gap-3 flex-wrap",children:[r.jsx("span",{className:"text-sm text-bambu-gray",children:s("inventory.labels.pickSpools","Pick which spools to print labels for:")}),r.jsxs("div",{className:"flex items-center gap-3 text-xs",children:[r.jsx("button",{type:"button",onClick:C?k:F,disabled:N.length===0,className:"text-bambu-green hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed",children:C?s("inventory.labels.deselectVisible","Deselect visible"):s("inventory.labels.selectVisible","Select all visible ({{count}})",{count:N.length})}),r.jsx("button",{type:"button",onClick:M,disabled:P===0,className:"text-bambu-gray hover:text-white hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed",children:s("inventory.labels.clearAll","Clear all")})]})]}),r.jsx("div",{className:"flex-1 overflow-y-auto px-2 pb-2 min-h-0",children:N.length===0?r.jsx("div",{className:"text-center text-sm text-bambu-gray py-6",children:x.length===0?s("inventory.labels.noSpoolsToShow","No spools to show. Adjust your filter and try again."):s("inventory.labels.noMatches","No spools match the current search or filter.")}):r.jsx("ul",{className:"space-y-0.5",children:N.map(I=>{const O=d.has(I.id);return r.jsx("li",{children:r.jsxs("label",{className:"flex items-center gap-3 px-2 py-1.5 rounded hover:bg-bambu-dark-tertiary/50 cursor-pointer",children:[O?r.jsx(ws,{className:"w-4 h-4 text-bambu-green shrink-0"}):r.jsx(lo,{className:"w-4 h-4 text-bambu-gray shrink-0"}),r.jsx("input",{type:"checkbox",checked:O,onChange:()=>j(I.id),className:"sr-only"}),r.jsx("span",{className:"w-4 h-4 rounded border border-black/20 shrink-0",style:Qst(I.rgba)}),r.jsx("span",{className:"flex-1 min-w-0 truncate text-sm text-white",children:Yst(I)}),r.jsxs("span",{className:"text-xs font-mono text-bambu-gray shrink-0",children:["#",I.id]})]})},I.id)})})}),r.jsx("div",{className:"px-3 pt-2 pb-2 grid grid-cols-1 sm:grid-cols-2 gap-2 border-t border-bambu-dark-tertiary",children:Wst.map(I=>{const O=l===I.value,L=s(`inventory.labels.templates.${I.i18nKey}.label`,I.fallbackLabel),X=s(`inventory.labels.templates.${I.i18nKey}.hint`,I.fallbackHint);return r.jsxs("button",{disabled:T||l!==null,onClick:()=>q(I.value),title:`${L} — ${X}`,className:"w-full text-left p-2.5 rounded-lg border border-bambu-dark-tertiary bg-bambu-dark hover:border-bambu-green hover:bg-bambu-green/10 disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:border-bambu-dark-tertiary disabled:hover:bg-bambu-dark transition flex items-center gap-3",children:[r.jsxs("div",{className:"flex-1 min-w-0",children:[r.jsx("div",{className:"font-medium text-white text-sm truncate",children:L}),r.jsx("div",{className:"text-xs text-bambu-gray mt-0.5 truncate",children:X})]}),O&&r.jsx(pt,{className:"w-4 h-4 animate-spin text-bambu-green shrink-0"})]},I.value)})}),r.jsx("div",{className:"flex justify-end gap-2 px-5 py-2 border-t border-bambu-dark-tertiary",children:r.jsx(je,{variant:"secondary",onClick:e,disabled:l!==null,children:s("common.cancel","Cancel")})})]})]})}function Jst(t){const e=t[0];let a=0,n=0,i=0;for(const s of t)a+=s.label_weight,n+=s.weight_used,i+=s.core_weight;return{...e,label_weight:a,weight_used:n,core_weight:i}}function IX(t){return`${t.material}|${t.subtype||""}|${t.brand||""}|${t.color_name||""}|${t.rgba||""}|${t.extra_colors||""}|${t.effect_type||""}|${t.label_weight}`}const Nde="bambuddy-inventory-columns",QN=[{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:"label_weight",label:"Label",visible:!0},{id:"net",label:"Net",visible:!0},{id:"gross",label:"Gross",visible:!1},{id:"added_full",label:"Full",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:"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 eot(){try{const t=localStorage.getItem(Nde);if(t){const e=JSON.parse(t),a=new Set(QN.map(o=>o.id)),n=new Set(e.map(o=>o.id)),i=e.filter(o=>a.has(o.id)),s=QN.filter(o=>!n.has(o.id));return[...i,...s]}}catch{}return QN.map(t=>({...t}))}function tot(t){try{localStorage.setItem(Nde,JSON.stringify(t))}catch{}}function iu(t,e=!1){return e&&t>=1e3?`${(t/1e3).toFixed(1)}kg`:`${Math.round(t)}g`}const aot={PLA:"bg-green-500/20 text-green-400",ABS:"bg-red-500/20 text-red-400",PETG:"bg-blue-500/20 text-blue-400",TPU:"bg-purple-500/20 text-purple-400",ASA:"bg-orange-500/20 text-orange-400",PA:"bg-yellow-500/20 text-yellow-400",PC:"bg-cyan-500/20 text-cyan-400",PET:"bg-sky-500/20 text-sky-400"};function T3(t,e="system"){if(!t)return"-";const a=Qa(t);return a?oL(a,e):"-"}const rot={id:()=>"#",added_time:()=>"Added",encode_time:()=>"Encoded",last_used_time:()=>"Last Used",rgba:t=>t("inventory.color"),material:t=>t("inventory.material"),subtype:t=>t("inventory.subtype"),color_name:t=>t("inventory.colorName"),brand:t=>t("inventory.brand"),slicer_filament:t=>t("inventory.slicerFilament"),location:()=>"Location",storage_location:t=>t("inventory.storageLocation"),label_weight:t=>t("inventory.labelWeight"),net:t=>t("inventory.net"),gross:()=>"Gross",added_full:()=>"Full",used:t=>t("inventory.weightUsed"),printed_total:()=>"Printed Total",printed_since_weight:()=>"Printed Since Weight",note:t=>t("inventory.note"),pa_k:()=>"PA(K)",tag_id:()=>"Tag ID",data_origin:()=>"Data Origin",tag_type:()=>"Linked Tag Type",stock:t=>t("inventory.stock"),remaining:t=>t("inventory.remaining"),spool_name:t=>t("inventory.spoolName"),cost_per_kg:t=>t("inventory.costPerKg"),weight_check:t=>t("inventory.weightCheck")},oO={id:({spool:t})=>r.jsx("span",{className:"text-sm font-medium text-white",children:t.id}),added_time:({spool:t,dateFormat:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:T3(t.created_at,e)}),encode_time:({spool:t,dateFormat:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:T3(t.encode_time,e)}),last_used_time:({spool:t,dateFormat:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.last_used?T3(t.last_used,e):"Never"}),rgba:({spool:t})=>r.jsx("div",{className:"flex items-center justify-center",children:r.jsx(bU,{rgba:t.rgba,extraColors:t.extra_colors,effectType:t.effect_type,effectSize:"table",subtype:t.subtype})}),material:({spool:t})=>r.jsx("span",{className:"text-sm text-white",children:t.material}),subtype:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.subtype||"-"}),color_name:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:FT(t.color_name,t.rgba)||"-"}),brand:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.brand||"-"}),slicer_filament:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",title:t.slicer_filament||void 0,children:t.slicer_filament_name||t.slicer_filament||"-"}),location:({spool:t,assignmentMap:e})=>{const a=e[t.id];if(!a)return r.jsx("span",{className:"text-sm text-bambu-gray",children:"-"});const n=a.printer_name||`Printer ${a.printer_id}`,i=a.ams_id===254||a.ams_id===255,s=!i&&a.ams_id>=128,o=hk(a.ams_id,a.tray_id,s,i);return r.jsxs("span",{className:"inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-purple-500/20 text-purple-400",children:[n," ",o,a.ams_label?` (${a.ams_label})`:""]})},storage_location:({spool:t})=>t.storage_location?r.jsx("span",{className:"inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-blue-500/20 text-blue-400",children:t.storage_location}):r.jsx("span",{className:"text-sm text-bambu-gray",children:"-"}),label_weight:({spool:t})=>r.jsx("span",{className:"text-sm text-white",children:iu(t.label_weight)}),net:({remaining:t})=>r.jsx("span",{className:"text-sm text-white",children:iu(t)}),gross:({spool:t,remaining:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:iu(e+t.core_weight)}),added_full:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.added_full==null?"-":t.added_full?"Yes":"No"}),used:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.weight_used>0?iu(t.weight_used):"-"}),printed_total:()=>r.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"}),printed_since_weight:()=>r.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"}),note:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray max-w-[150px] truncate block",title:t.note||void 0,children:t.note||"-"}),pa_k:({spool:t})=>(t.k_profiles?.length??0)===0?r.jsx("span",{className:"text-sm text-bambu-gray",children:"-"}):r.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:t})=>{const e=t.tag_uid||t.tray_uuid;return e?r.jsx("span",{className:"text-sm text-bambu-gray font-mono",title:e,children:e.length>12?`${e.slice(0,6)}...${e.slice(-4)}`:e}):r.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"})},data_origin:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.data_origin||"-"}),tag_type:({spool:t})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.tag_type||"-"}),stock:({spool:t,t:e})=>t.slicer_filament?r.jsx("span",{className:"text-sm text-bambu-gray",children:"-"}):r.jsx("span",{className:"inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-amber-500/20 text-amber-400",children:e("inventory.stock")}),remaining:({remaining:t,pct:e})=>r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsx("div",{className:"flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden",children:r.jsx("div",{className:`h-full rounded-full ${e>50?"bg-bambu-green":e>20?"bg-yellow-500":"bg-red-500"}`,style:{width:`${Math.min(e,100)}%`}})}),r.jsxs("span",{className:"text-xs text-bambu-gray min-w-[40px] text-right",children:[Math.round(t),"g"]})]}),spool_name:({spool:t,catalogMap:e})=>{const a=t.core_weight_catalog_id!=null?e[t.core_weight_catalog_id]:void 0;return r.jsx("span",{className:"text-sm text-bambu-gray",children:a?.name||"-"})},cost_per_kg:({spool:t,currencySymbol:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.cost_per_kg!=null?`${e}${t.cost_per_kg.toFixed(2)}`:"-"}),weight_check:({spool:t,onSyncWeight:e})=>{const a=t.last_scale_weight;if(a==null)return r.jsx("span",{className:"text-sm text-bambu-gray/50",title:"No scale measurement",children:"-"});const n=t.core_weight||0,i=Math.max(0,t.label_weight-t.weight_used)+n;let s,o;a0?`+${Math.round(s)}`:`${Math.round(s)}`,c=o?`Scale: ${Math.round(a)}g
Calculated: ${Math.round(i)}g
Difference: ${l}g (within tolerance)`:`Scale: ${Math.round(a)}g
diff --git a/static/index.html b/static/index.html
index bcbebf677..5e0884c85 100644
--- a/static/index.html
+++ b/static/index.html
@@ -26,7 +26,7 @@
-
+