u>=1e3?`${(u/1e3).toFixed(1)}kg`:`${u}g`,width:44}),r.jsx(nu,{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(DA,{type:"linear",dataKey:"stock",stroke:"#1DB954",strokeWidth:2,fill:`url(#cart-post-${t.id})`,dot:!1,activeDot:{r:3}}),e.reorderPointG>0&&r.jsx(ru,{y:e.reorderPointG,stroke:"#F59E0B",strokeDasharray:"5 3",strokeOpacity:.8,label:{value:"ROP",position:"insideTopRight",fill:"#F59E0B",fontSize:9}}),e.safetyStockG>0&&r.jsx(ru,{y:e.safetyStockG,stroke:"#6B7280",strokeDasharray:"3 3",strokeOpacity:.6,label:{value:"SS",position:"insideTopRight",fill:"#6B7280",fontSize:9}}),r.jsx(ru,{x:Sw(Um(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(ru,{x:Sw(Um(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 Dit({forecast:t,onClose:e,onAdd:n}){const{t:a}=kt(),i=[t.group.brand,t.group.material,t.group.subtype].filter(Boolean).join(" "),[s,o]=v.useState("qty"),[l,c]=v.useState("1"),[d,u]=v.useState("30"),[m,p]=v.useState(""),f=v.useMemo(()=>{if(!t.dailyRateG||t.dailyRateG<=0)return null;const g=t.dailyRateG*Number(d),x=t.group.spools.length>0?t.group.spools.reduce((_,N)=>_+N.label_weight,0)/t.group.spools.length:1e3;return Math.ceil(g/x)},[t,d]),b=s==="qty"?parseInt(l,10)||1:f??1;function w(g){g.preventDefault(),n({material:t.group.material,subtype:t.group.subtype,brand:t.group.brand,quantity_spools:b,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(lb,{className:"w-5 h-5 text-bambu-green"}),r.jsx("h2",{className:"text-base font-semibold text-white",children:a("forecast.addToCartTitle")})]}),r.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white transition-colors",children:r.jsx(Bt,{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:a("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:a("forecast.byDuration")})]}),s==="qty"?r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("label",{className:"text-xs text-bambu-gray",children:a("forecast.numberOfSpools")}),r.jsx("input",{type:"number",min:1,max:99,value:l,onChange:g=>c(g.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:a("forecast.lastHowManyDays")}),r.jsx("input",{type:"number",min:1,max:365,value:d,onChange:g=>u(g.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:a("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:a("forecast.noUsageQty")})]}),r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("label",{className:"text-xs text-bambu-gray",children:a("forecast.noteOptional")}),r.jsx("input",{type:"text",maxLength:200,value:m,onChange:g=>p(g.target.value),placeholder:a("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:a("forecast.addNSpools",{count:b})}),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:a("forecast.cancel")})]})]})]})})}const YI={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},Fit=new Set(["material","subtype","brand","rgba","color_name","label_weight"]);function zce(t,e=!1,n=!1){const a={};return e||n?(!t.material&&!t.spoolman_filament_id&&(a.material="Material is required"),{isValid:Object.keys(a).length===0,errors:a}):(t.slicer_filament||(a.slicer_filament="Slicer preset is required"),t.material||(a.material="Material is required"),t.brand||(a.brand="Brand is required"),t.subtype||(a.subtype="Subtype is required"),{isValid:Object.keys(a).length===0,errors:a})}const Uce=["PLA","PETG","ABS","TPU","ASA","PC","PA","PVA","HIPS","PA-CF","PETG-CF","PLA-CF"],Lit=["Bambu","PolyLite","PolyTerra","eSUN","Overture","Fiberon","SUNLU","Inland","Hatchbox","Generic"],_w=["Basic","Matte","Silk","Silk+","Tough","Tough+","HF","High Flow","Engineering","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"],Bce=[{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"}],Rit=[{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"}],bQ=[...Bce,...Rit],Hce="bambuddy-recent-colors",Iit=8,Oit=[{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 Jh(t){let e=t.replace(/@.*$/,"").trim();e=e.replace(/\(Custom\)/i,"").trim(),e=e.replace(/^[#*]+\s*/,"").trim();const n=["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 a="",i=-1;for(const c of n){const d=e.toUpperCase().indexOf(c.toUpperCase());if(d!==-1){a=c,i=d;break}}let s="";i>0&&(s=e.substring(0,i).trim(),s=s.replace(/[-_\s]+$/,""));let o="";i!==-1&&a&&(o=e.substring(i+a.length).trim(),o=o.replace(/^[-_\s]+/,""));let l="";for(const c of _w)if(o.toLowerCase().includes(c.toLowerCase())){l=c;break}if(!l&&s)for(const c of _w){const d=new RegExp(`\\s+${c}$`,"i");if(d.test(s)){l=c,s=s.replace(d,"").trim();break}}return{brand:s,material:a,variant:l}}function qce(t,e){const n=new Set(Lit);for(const a of t){const{brand:i}=Jh(a.name);i&&i.length>1&&n.add(i)}if(e)for(const a of e)if(a.filament_vendor&&a.filament_vendor.length>1)n.add(a.filament_vendor);else{const{brand:i}=Jh(a.name);i&&i.length>1&&n.add(i)}return Array.from(n).sort((a,i)=>a.localeCompare(i))}function zit(t){const e=t.filter(a=>a.preset_type==="filament");return e.length===0?[]:e.map(a=>{const i=a.filament_type||String(a.id),s=Array.from(new Set([i,String(a.id)]));return{code:i,name:a.name,displayName:a.name,isCustom:!1,allCodes:s}}).sort((a,i)=>a.displayName.localeCompare(i.displayName))}function $ce(t,e,n,a){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=n&&n.length>0?zit(n):[],c=[];if(a&&a.length>0)for(const u of a){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?Oit:d.sort((u,m)=>u.displayName.localeCompare(m.displayName))}function Vce(t,e){if(!t)return;let n=e.find(a=>a.code===t);if(n||(n=e.find(a=>a.allCodes.includes(t))),!n){const a=t.toLowerCase();n=e.find(i=>i.code.toLowerCase()===a||i.allCodes.some(s=>s.toLowerCase()===a))}return n}function Gce(){try{const t=localStorage.getItem(Hce);if(t)return JSON.parse(t)}catch{}return[]}function Wce(t,e){const n=e.filter(i=>i.hex.toUpperCase()!==t.hex.toUpperCase()),a=[t,...n].slice(0,Iit);try{localStorage.setItem(Hce,JSON.stringify(a))}catch{}return a}function fF(t,e){if(!e.material)return!1;const a=(t.name||"").replace(/^High Flow[_\s]+/i,"").replace(/^Standard[_\s]+/i,"").replace(/^HF[_\s]+/i,"").replace(/^S[_\s]+/i,"").trim(),i=Jh(a);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())||a.toLowerCase().includes(e.subtype.toLowerCase())))}function Kce({formData:t,updateField:e,cloudAuthenticated:n,loadingCloudPresets:a,presetInputValue:i,setPresetInputValue:s,selectedPresetOption:o,filamentOptions:l,availableBrands:c,availableMaterials:d,quickAdd:u,quantity:m,onQuantityChange:p,errors:f}){const{t:b}=kt(),[w,g]=v.useState(!1),[x,_]=v.useState(!1),[N,C]=v.useState(!1),[P,T]=v.useState(!1),[A,F]=v.useState(""),[S,M]=v.useState(""),[z,I]=v.useState(""),[H,R]=v.useState(String(t.label_weight)),[Z,re]=v.useState(!1),J=v.useRef(null),de=v.useRef(null),me=v.useRef(null),ue=v.useRef(null);v.useEffect(()=>{const $=K=>{J.current&&!J.current.contains(K.target)&&g(!1),ue.current&&!ue.current.contains(K.target)&&T(!1),de.current&&!de.current.contains(K.target)&&_(!1),me.current&&!me.current.contains(K.target)&&C(!1)};return document.addEventListener("mousedown",$),()=>document.removeEventListener("mousedown",$)},[]);const W=v.useMemo(()=>{if(!i)return l;const $=i.toLowerCase();return l.filter(K=>K.displayName.toLowerCase().includes($)||K.code.toLowerCase().includes($))},[l,i]),ae=v.useMemo(()=>{if(!A)return c;const $=A.toLowerCase();return c.filter(D=>D.toLowerCase().includes($)).sort((D,j)=>{const B=D.toLowerCase()===$,X=j.toLowerCase()===$;return B&&!X?-1:!B&&X?1:D.localeCompare(j)})},[c,A]),pe=v.useMemo(()=>{if(!S)return _w;const $=S.toLowerCase();return _w.filter(K=>K.toLowerCase().includes($))},[S]),ye=v.useMemo(()=>{if(!z)return d;const $=z.toLowerCase();return d.filter(D=>D.toLowerCase().includes($)).sort((D,j)=>{const B=D.toLowerCase()===$,X=j.toLowerCase()===$;return B&&!X?-1:!B&&X?1:D.localeCompare(j)})},[z,d]);v.useEffect(()=>{Z||R(String(t.label_weight))},[t.label_weight,Z]);const Te=$=>{e("slicer_filament",$.code),s($.displayName),g(!1);const K=Jh($.name);K.material&&e("material",K.material),K.brand&&e("brand",K.brand),K.variant&&e("subtype",K.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:a?r.jsxs(r.Fragment,{children:[r.jsx(ut,{className:"w-3 h-3 animate-spin"})," ",b("inventory.loadingPresets")]}):n?r.jsxs(r.Fragment,{children:[r.jsx(Hh,{className:"w-3 h-3 text-bambu-green"})," ",b("inventory.cloudConnected")]}):r.jsxs(r.Fragment,{children:[r.jsx(UF,{className:"w-3 h-3"})," ",b("inventory.cloudNotConnected")]})}),!u&&r.jsxs("div",{children:[r.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[b("inventory.slicerPreset")," *"]}),r.jsxs("div",{className:"relative",ref:J,children:[r.jsx(Sr,{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:b("inventory.searchPresets"),value:i,onChange:$=>{s($.target.value),g(!0)},onFocus:()=>{g(!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:W.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noPresetsFound")}):W.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary truncate ${o?.code===$.code?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>Te($),children:$.displayName},`${$.code}::${$.name}`))})]}),o&&r.jsxs("div",{className:"mt-1 text-xs text-bambu-gray",children:[b("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:[b("inventory.material")," *"]}),r.jsxs("div",{className:"relative",ref:ue,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:b("inventory.selectMaterial"),value:P?z:t.material,onChange:$=>{I($.target.value),T(!0)},onFocus:()=>{T(!0),I("")}}),r.jsx(Ln,{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:[ye.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noResults")}):ye.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.material===$?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("material",$),T(!1),I("")},children:$},$)),z&&!ye.includes(z)&&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",z),T(!1),I("")},children:b("inventory.useCustomMaterial",{material:z})})]})]}),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:[b("inventory.brand"),!u&&" *"]}),r.jsxs("div",{className:"relative",ref:de,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:b("inventory.searchBrand"),value:x?A:t.brand,onChange:$=>{F($.target.value),_(!0)},onFocus:()=>{_(!0),F("")}}),r.jsx(Ln,{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:[ae.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noResults")}):ae.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.brand===$?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("brand",$),_(!1),F("")},children:$},$)),A&&!ae.includes(A)&&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",A),_(!1),F("")},children:b("inventory.useCustomBrand",{brand:A})})]})]}),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:[b("inventory.subtype"),!u&&" *"]}),r.jsxs("div",{className:"relative",ref:me,children:[r.jsx("input",{type:"text",value:N?S:t.subtype,onChange:$=>{M($.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(Ln,{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:[pe.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noResults")}):pe.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.subtype===$?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("subtype",$),C(!1),M("")},children:$},$)),S&&!_w.some($=>$.toLowerCase()===S.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",S),C(!1),M("")},children:b("inventory.useCustomBrand",{brand:S})})]})]}),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:b("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:H,min:0,onFocus:()=>re(!0),onChange:$=>R($.target.value),onBlur:()=>{re(!1);const $=H.trim(),K=Number($);if(!$||!Number.isFinite(K)||K<0){R(String(t.label_weight));return}const D=Math.round(K);e("label_weight",D),R(String(D))}}),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:b("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:$=>{const K=Math.max(1,Math.min(100,parseInt($.target.value)||1));p(K)}})]})]})}function Uit(t){const e=t.split(/[\s,]+/).map(i=>i.trim().replace(/^#/,"")).filter(Boolean),n=[],a=[];for(const i of e)(i.length===6||i.length===8)&&/^[0-9a-fA-F]+$/.test(i)?n.push(i.toLowerCase()):a.push(i);return{value:n.join(","),invalid:a}}function Yce({formData:t,updateField:e,recentColors:n,onColorUsed:a,catalogColors:i}){const{t:s}=kt(),[o,l]=v.useState(!1),[c,d]=v.useState(""),u=t.rgba.replace("#","").substring(0,6),m=F=>u.toUpperCase()===F.toUpperCase(),p=(F,S)=>{e("rgba",F.toUpperCase()+"FF"),e("color_name",S),a({name:S,hex:F})},f=v.useMemo(()=>{if(i.length===0)return[];const F=t.brand?.trim(),S=t.material?.toLowerCase().trim(),M=t.subtype?.toLowerCase().trim();if(!F&&!S)return[];const z=F?F.toLowerCase().split(/[\s\-_]+/).filter(R=>R.length>=2):[],I=R=>{if(z.length===0)return!0;const Z=R.toLowerCase();return z.some(re=>Z.includes(re))};if(F&&!S){const R=i.filter(Z=>I(Z.manufacturer));if(R.length>0)return R.map(Z=>({name:Z.color_name,hex:Z.hex_color.replace("#","").substring(0,6),manufacturer:Z.manufacturer,material:typeof Z.material=="string"?Z.material:void 0}))}const H=S&&M?`${S} ${M}`:"";if(H){const R=i.filter(re=>I(re.manufacturer)&&re.material?.toLowerCase()===H);if(R.length>0)return R.map(re=>({name:re.color_name,hex:re.hex_color.replace("#","").substring(0,6),manufacturer:re.manufacturer,material:typeof re.material=="string"?re.material:void 0}));const Z=H.replace(/\+$/,"");if(Z!==H){const re=i.filter(J=>I(J.manufacturer)&&J.material?.toLowerCase()===Z);if(re.length>0)return re.map(J=>({name:J.color_name,hex:J.hex_color.replace("#","").substring(0,6),manufacturer:J.manufacturer,material:typeof J.material=="string"?J.material:void 0}))}}if(S){const R=i.filter(Z=>I(Z.manufacturer)&&(!Z.material||Z.material.toLowerCase().startsWith(S)));if(R.length>0)return R.map(Z=>({name:Z.color_name,hex:Z.hex_color.replace("#","").substring(0,6),manufacturer:Z.manufacturer,material:typeof Z.material=="string"?Z.material:void 0}))}return[]},[i,t.brand,t.material,t.subtype]),b=v.useMemo(()=>{if(!c)return f;if(f.length===0)return[];const F=c.toLowerCase();return f.filter(M=>M.name.toLowerCase().includes(F)||(M.manufacturer?.toLowerCase().includes(F)??!1)||(M.material?.toLowerCase().includes(F)??!1))},[c,f]),w=f.length>0,g=v.useMemo(()=>c?bQ.filter(F=>F.name.toLowerCase().includes(c.toLowerCase())):o?bQ:Bce,[c,o]),[x,_]=v.useState(t.extra_colors),[N,C]=v.useState([]),P=v.useRef(t.extra_colors);v.useEffect(()=>{t.extra_colors!==P.current&&(_(t.extra_colors),C([]),P.current=t.extra_colors)},[t.extra_colors]);const T=v.useMemo(()=>rj({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]),A=F=>{if(_(F),!F.trim()){C([]),P.current="",e("extra_colors","");return}const{value:S,invalid:M}=Uit(F);C(M),P.current=S,e("extra_colors",S)};return r.jsxs("div",{className:"space-y-3",children:[r.jsx("div",{className:"h-10 rounded-lg border border-bambu-dark-tertiary",style:T,"data-testid":"color-preview-banner"}),n.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(Zn,{className:"w-3 h-3"}),r.jsx("span",{children:s("inventory.recentColors")})]}),r.jsx("div",{className:"flex flex-wrap gap-1.5",children:n.map(F=>r.jsx("button",{type:"button",onClick:()=>p(F.hex,F.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 ${m(F.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${F.hex}`},title:F.name},F.hex))})]}),r.jsxs("div",{className:"relative",children:[r.jsx(Sr,{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:F=>d(F.target.value)})]}),w?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(F=>r.jsx("button",{type:"button",onClick:()=>p(F.hex,F.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 hover:z-20 relative group ${m(F.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${F.hex}`},title:F.manufacturer&&F.material?`${F.name} (${F.manufacturer} — ${F.material})`:F.manufacturer?`${F.name} (${F.manufacturer})`:F.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:F.manufacturer&&F.material?`${F.name} (${F.manufacturer} — ${F.material})`:F.manufacturer?`${F.name} (${F.manufacturer})`:F.name})},`${F.hex}-${F.name}-${F.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(tl,{className:"w-3 h-3"})]}):r.jsxs(r.Fragment,{children:[s("inventory.showAll")," ",r.jsx(Ln,{className:"w-3 h-3"})]})})]}),r.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[g.map(F=>r.jsx("button",{type:"button",onClick:()=>p(F.hex,F.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 hover:z-20 relative group ${m(F.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${F.hex}`},title:F.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:F.name})},F.hex)),g.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:F=>e("color_name",F.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:u.toUpperCase(),onChange:F=>{const S=F.target.value.replace("#","").replace(/[^0-9A-Fa-f]/g,"").toUpperCase();if(S.length>8)return;const M=S.length===8?S:S.length===7?S.substring(0,6)+"FF":S.padEnd(6,"0")+"FF";e("rgba",M)}})]}),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:F=>{const S=F.target.value.replace("#","").toUpperCase();e("rgba",S+"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:x,onChange:F=>A(F.target.value),"data-testid":"extra-colors-input"}),N.length>0&&r.jsx("p",{className:"text-xs text-red-400 mt-1",children:s("inventory.extraColorsInvalid",{tokens:N.join(", ")})}),!N.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(Dg,{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:F=>e("effect_type",F.target.value),"data-testid":"effect-type-select",children:WI.map(F=>r.jsx("option",{value:F.value,children:s(F.labelKey)},F.value||"none"))}),r.jsx(rU,{rgba:t.rgba,extraColors:t.extra_colors,effectType:t.effect_type,subtype:t.subtype,effectSize:"preview",className:"w-10 h-10",shape:"square"})]})]})]})]})}function Bit({catalog:t,value:e,onChange:n,catalogId:a,onCatalogIdChange:i}){const{t:s}=kt(),[o,l]=v.useState(!1),[c,d]=v.useState(""),u=v.useRef(null),m=v.useRef(null);v.useEffect(()=>{const w=g=>{u.current&&!u.current.contains(g.target)&&l(!1)};return document.addEventListener("mousedown",w),()=>document.removeEventListener("mousedown",w)},[]),v.useEffect(()=>{if(t.length===0)return;const w=t.filter(g=>g.weight===e);if(a){const g=t.find(x=>x.id===a);if(g&&g.weight===e)return}w.length===1?i(w[0].id):w.length===0&&a!==null&&i(null)},[e,t,a,i]);const p=v.useMemo(()=>{if(!c)return t;const w=c.toLowerCase();return t.filter(g=>g.name.toLowerCase().includes(w)||g.weight.toString().includes(w))},[t,c]),f=v.useMemo(()=>t.filter(w=>w.weight===e),[t,e]),b=v.useMemo(()=>{if(o)return c;if(a){const w=t.find(g=>g.id===a);if(w)return w.name}return f.length>0?f[0].name:""},[o,c,a,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(FZ,{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:b,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 ${(a?w.id===a:w.weight===e)?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{i(w.id),n(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 g=parseInt(w.target.value);!isNaN(g)&&g>=0&&n(g)}}),r.jsx("span",{className:"text-bambu-gray text-sm",children:"g"})]})]})]})}function Xce({formData:t,updateField:e,spoolCatalog:n,currencySymbol:a,availableCategories:i,globalLowStockThreshold:s,spoolmanMode:o=!1}){const{t:l}=kt(),{showToast:c}=rn(),[d,u]=v.useState(""),[m,p]=v.useState(!1),[f,b]=v.useState(""),[w,g]=v.useState(!1),x=Math.max(0,t.label_weight-t.weight_used),_=t.core_weight+x;return v.useEffect(()=>{m||u(String(_))},[m,_]),v.useEffect(()=>{w||b(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(Bit,{catalog:n,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:()=>g(!0),onChange:N=>{b(N.target.value)},onBlur:()=>{g(!1);const N=f.trim(),C=Number(N);if(!N||!Number.isFinite(C)||C<0||C>t.label_weight){b(String(x));return}const P=Math.round(C);e("weight_used",Math.max(0,t.label_weight-P)),b(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(_));return}const A=Math.round(C),F=Math.max(0,Math.min(t.label_weight,A-t.core_weight));e("weight_used",Math.max(0,t.label_weight-F)),u(String(A))},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:a}),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,a.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 Hit({filaments:t,isLoading:e,selectedId:n,onSelect:a}){const{t:i}=kt(),[s,o]=v.useState(!1),[l,c]=v.useState(""),d=v.useRef(null),u=v.useRef(null),m=v.useMemo(()=>t.find(g=>g.id===n)??null,[t,n]);v.useEffect(()=>{const g=x=>{d.current&&!d.current.contains(x.target)&&(o(!1),c(""))};return document.addEventListener("mousedown",g),()=>document.removeEventListener("mousedown",g)},[]);const p=v.useMemo(()=>{const g=l.toLowerCase().trim();return g?t.filter(x=>x.name.toLowerCase().includes(g)||(x.material?.toLowerCase().includes(g)??!1)||(x.vendor?.name.toLowerCase().includes(g)??!1)||(x.color_name?.toLowerCase().includes(g)??!1)):t},[t,l]);v.useEffect(()=>{s&&u.current?.focus()},[s]);const f=()=>{o(!0),c("")},b=g=>{a(g),o(!1),c("")},w=g=>g?`#${g.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(ut,{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(Br,{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(Ln,{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:g=>c(g.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(ut,{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(g=>r.jsx("li",{children:r.jsxs("button",{type:"button",onClick:()=>b(g),className:`w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-bambu-dark-tertiary transition-colors ${g.id===n?"bg-bambu-dark-tertiary":""}`,children:[r.jsx("span",{className:"w-4 h-4 rounded-full shrink-0 border border-white/20",style:{backgroundColor:w(g.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:[g.vendor?.name?`${g.vendor.name} — `:"",g.name]}),r.jsxs("span",{className:"block text-bambu-gray text-xs truncate",children:[[g.material,g.color_name].filter(Boolean).join(" · "),g.weight?` · ${g.weight}g`:""]})]})]})},g.id))})]})]})}function Qce({formData:t,printersWithCalibrations:e,selectedProfiles:n,setSelectedProfiles:a,expandedPrinters:i,setExpandedPrinters:s}){const{t:o}=kt(),l=p=>{s(f=>{const b=new Set(f);return b.has(p)?b.delete(p):b.add(p),b})},c=(p,f,b)=>{const w=`${p}:${f}:${b??"null"}`,g=`${p}:${b??"null"}`;a(x=>{const _=new Set(x);if(_.has(w))_.delete(w);else{for(const N of Array.from(_)){const C=N.split(":");`${C[0]}:${C[2]}`===g&&_.delete(N)}_.add(w)}return _})},d=()=>{const p=new Set;for(const{printer:f,calibrations:b}of e){if(!f.connected)continue;const w=b.filter(x=>fF(x,t)),g=new Map;for(const x of w){const _=`${x.extruder_id??"null"}`;g.has(_)||g.set(_,[]),g.get(_).push(x)}for(const[x,_]of g)if(_.length>0){const C=[..._].sort((P,T)=>T.k_value-P.k_value)[0];p.add(`${f.id}:${C.cali_idx}:${x}`)}}a(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:b})=>f.connected?p+b.filter(w=>fF(w,t)).length:p,0),m=(p,f)=>{const b=`${p.id}:${f.cali_idx}:${f.extruder_id??"null"}`,w=n.has(b);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(Dg,{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 b=i.has(String(p.id)),w=f.filter(C=>fF(C,t)),g=w.length,x=w.some(C=>C.extruder_id!==void 0&&C.extruder_id!==null&&C.extruder_id>0),_=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:[b?r.jsx(Ln,{className:"w-4 h-4 text-bambu-gray"}):r.jsx(Va,{className:"w-4 h-4 text-bambu-gray"}),r.jsx("span",{className:"font-medium text-white",children:p.name}),g>0?r.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:[g," ",o(g!==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")})]}),b&&r.jsx("div",{className:"px-4 py-3 space-y-3 bg-bambu-dark border-t border-bambu-dark-tertiary",children:p.connected?g===0?r.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.noKProfilesMatch")}):x?r.jsxs(r.Fragment,{children:[_.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:_.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)})}),n.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:n.size})," ",o("inventory.profilesSelected")]})})]})}function qit(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 $it={completed:"text-bambu-green",failed:"text-red-400",aborted:"text-yellow-400"};function Vit({spoolId:t}){const{t:e}=kt(),n=en(),{showToast:a}=rn(),{data:i,isLoading:s}=We({queryKey:["spool-usage",t],queryFn:()=>se.getSpoolUsageHistory(t)}),o=it({mutationFn:()=>se.clearSpoolUsageHistory(t),onSuccess:()=>{n.invalidateQueries({queryKey:["spool-usage",t]}),a(e("inventory.historyCleared"),"success")}});return s?r.jsx("div",{className:"flex justify-center py-4",children:r.jsx(ut,{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(Zn,{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(Me,{variant:"ghost",size:"sm",onClick:()=>o.mutate(),disabled:o.isPending,className:"text-xs text-bambu-gray hover:text-red-400",children:[r.jsx(Jt,{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:qit(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:$it[l.status]||"text-bambu-gray",children:l.status})]})]},l.id))})]})}const xQ={tag_uid:null,tray_uuid:null,tag_type:null,data_origin:null};function Git({isOpen:t,onClose:e,spool:n,mode:a,printersWithCalibrations:i=[],currencySymbol:s,onSpoolsCreated:o,spoolmanMode:l=!1,spoolsQueryKey:c=["inventory-spools"]}){const{t:d}=kt(),u=en(),{showToast:m}=rn(),p=a==="edit",f=a==="copy",[b,w]=v.useState(YI),[g,x]=v.useState({}),[_,N]=v.useState("filament"),[C,P]=v.useState(!1),[T,A]=v.useState(!1),[F,S]=v.useState(!1),[M,z]=v.useState(1),[I,H]=v.useState(!1),[R,Z]=v.useState(!1),[re,J]=v.useState([]),[de,me]=v.useState(""),[ue,W]=v.useState([]),[ae,pe]=v.useState([]),[ye,Te]=v.useState([]),[$,K]=v.useState([]),[D,j]=v.useState([]),[B,X]=v.useState([]),[O,ie]=v.useState(new Set),[U,G]=v.useState(new Set),Y=i.length>0?i:B,Q=O.size,{data:oe=[],isLoading:ee,error:ge}=We({queryKey:["spoolman-inventory-filaments"],queryFn:()=>se.getSpoolmanInventoryFilaments(),enabled:l&&t,staleTime:6e4,retry:!1});v.useEffect(()=>{j(Gce())},[]),v.useEffect(()=>{t&&((async()=>{Z(!0);try{const nt=await se.getCloudStatus();if(H(nt.is_authenticated),nt.is_authenticated){const gt=await se.getFilamentPresets();J(gt)}}catch(nt){console.error("Failed to fetch cloud presets:",nt),H(!1)}finally{Z(!1)}})(),l||se.getSpoolCatalog().then(W).catch(console.error),se.getColorCatalog().then(K).catch(console.error),se.getLocalPresets().then(nt=>pe(nt.filament)).catch(console.error),se.getBuiltinFilaments().then(Te).catch(console.error),i.length===0&&(async()=>{try{const nt=await se.getPrinters(),gt=await Promise.all(nt.map(ot=>se.getPrinterStatus(ot.id).catch(()=>null))),yt=[];for(let ot=0;ot({cali_idx:cn.slot_id,filament_id:cn.filament_id,setting_id:cn.setting_id||"",name:cn.name,k_value:parseFloat(cn.k_value)||0,n_coef:parseFloat(cn.n_coef)||0,extruder_id:cn.extruder_id,nozzle_diameter:cn.nozzle_diameter}))}catch{}yt.push({printer:{...Ht,connected:Zt},calibrations:Yt})}X(yt)}catch(nt){console.error("Failed to fetch printer calibrations:",nt)}})())},[t,i.length]);const ce=v.useMemo(()=>$ce(re,new Set,ae,ye),[re,ae,ye]),le=v.useMemo(()=>{const Be=qce(re,ae),nt=$.map(yt=>yt.manufacturer?.trim()).filter(yt=>!!yt),gt=new Set([...Be,...nt]);return Array.from(gt).sort((yt,ot)=>yt.localeCompare(ot))},[re,ae,$]),q=v.useMemo(()=>{const Be=$.map(gt=>gt.material?.trim()).filter(gt=>!!gt),nt=new Set([...Uce,...Be]);return Array.from(nt).sort((gt,yt)=>gt.localeCompare(yt))},[$]),L=v.useMemo(()=>{const Be=[];for(const nt of $){const gt=nt.manufacturer?.trim(),yt=nt.material?.trim();gt&&yt&&Be.push({brand:gt,material:yt})}for(const nt of re){const gt=Jh(nt.name);gt.brand&>.material&&Be.push({brand:gt.brand,material:gt.material})}for(const nt of ae){const gt=Jh(nt.name),yt=nt.filament_vendor?.trim()||gt.brand,ot=gt.material;yt&&ot&&Be.push({brand:yt,material:ot})}return Be},[re,$,ae]),ne=v.useMemo(()=>{const Be=new Map;for(const nt of L){const gt=nt.brand.toLowerCase(),yt=nt.material.toLowerCase();Be.has(gt)||Be.set(gt,new Set),Be.get(gt).add(yt)}return Be},[L]),xe=v.useMemo(()=>{const Be=new Map;for(const nt of L){const gt=nt.brand.toLowerCase(),yt=nt.material.toLowerCase();Be.has(yt)||Be.set(yt,new Set),Be.get(yt).add(gt)}return Be},[L]),we=v.useMemo(()=>{if(!b.material)return le;const Be=b.material.toLowerCase(),nt=xe.get(Be);return!nt||nt.size===0?le:le.filter(gt=>nt.has(gt.toLowerCase()))},[le,b.material,xe]),Ne=v.useMemo(()=>{if(!b.brand)return q;const Be=b.brand.toLowerCase(),nt=ne.get(Be);return!nt||nt.size===0?q:q.filter(gt=>nt.has(gt.toLowerCase()))},[q,b.brand,ne]),ve=v.useMemo(()=>Vce(b.slicer_filament,ce),[b.slicer_filament,ce]);v.useEffect(()=>{if(t){if(n){const Be=n.rgba&&/^[0-9A-Fa-f]{8}$/.test(n.rgba)?n.rgba:"808080FF";if(w({material:n.material||"",subtype:n.subtype||"",brand:n.brand||"",color_name:n.color_name||"",rgba:Be,extra_colors:n.extra_colors||"",effect_type:n.effect_type||"",label_weight:n.label_weight||1e3,core_weight:n.core_weight||250,core_weight_catalog_id:n.core_weight_catalog_id??null,weight_used:f?0:n.weight_used||0,slicer_filament:n.slicer_filament||"",note:n.note||"",cost_per_kg:n.cost_per_kg??null,category:n.category||"",low_stock_threshold_pct:n.low_stock_threshold_pct??null,storage_location:n.storage_location||"",spoolman_filament_id:null}),me(n.slicer_filament_name||n.slicer_filament||""),n.k_profiles&&n.k_profiles.length>0){const nt=new Set;for(const gt of n.k_profiles)gt.cali_idx!==null&>.cali_idx!==void 0&&nt.add(`${gt.printer_id}:${gt.cali_idx}:${gt.extruder??"null"}`);ie(nt)}else ie(new Set)}else w(YI),me(""),ie(new Set),S(!1),z(1);x({}),N("filament"),P(!1),A(!1)}},[t,n,a,f]),v.useEffect(()=>{t&&Y.length>0&&G(new Set(Y.map(Be=>String(Be.printer.id))))},[t,Y]);const be=(Be,nt)=>{const gt=Fit.has(Be);l&>&&b.spoolman_filament_id!==null&&m(d("inventory.spoolmanFilamentUnlinked"),"info"),w(yt=>({...yt,[Be]:nt,...l&>&&yt.spoolman_filament_id!==null?{spoolman_filament_id:null}:{}})),Be==="weight_used"&&P(!0),Be==="storage_location"&&A(!0),g[Be]&&x(yt=>({...yt,[Be]:void 0}))},Ee=Be=>{const nt=Be.material||"",gt=Be.name||"",yt=nt&>.startsWith(nt)?gt.slice(nt.length).trim():gt,ot=(Be.color_hex??"").replace("#","").toUpperCase(),Ht=/^[0-9A-F]{6}$/.test(ot)?ot:"808080";w(Kt=>({...Kt,spoolman_filament_id:Be.id,material:nt,subtype:yt,brand:Be.vendor?.name||"",rgba:`${Ht}FF`,color_name:Be.color_name||"",label_weight:Be.weight??Kt.label_weight})),m(d("inventory.spoolmanFilamentSelected"),"success")},ze=Be=>{j(nt=>Wce(Be,nt))},_e=it({mutationFn:Be=>l?se.createSpoolmanInventorySpool(Be):se.createSpool(Be),onSuccess:async Be=>{Be?.id&&!await fe(Be.id)||(await u.invalidateQueries({queryKey:c}),o&&o([Be]),m(d("inventory.spoolCreated"),"success"),e())},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Oe=it({mutationFn:({data:Be,qty:nt})=>l?se.bulkCreateSpoolmanInventorySpools(Be,nt):se.bulkCreateSpools(Be,nt),onSuccess:async Be=>{const nt=l&&"created"in Be?Be:null,gt=nt?nt.created:Be;if(O.size>0)for(const yt of gt)await fe(yt.id);await u.invalidateQueries({queryKey:c}),o&&o(gt),nt&&nt.failed_count>0?m(d("inventory.spoolsPartiallyCreated",{created:gt.length,total:nt.requested_count}),"warning"):m(d("inventory.spoolsCreated",{count:gt.length}),"success"),e()},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Ie=it({mutationFn:Be=>l?se.updateSpoolmanInventorySpool(n.id,Be):se.updateSpool(n.id,Be),onSuccess:async()=>{n?.id&&!await fe(n.id)||(await u.invalidateQueries({queryKey:c}),m(d("inventory.spoolUpdated"),"success"),e())},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Ke=it({mutationFn:()=>l?se.updateSpoolmanInventorySpool(n.id,xQ):se.updateSpool(n.id,xQ),onSuccess:async()=>{await u.invalidateQueries({queryKey:c}),m(d("inventory.rfidCleared","RFID tag cleared"),"success"),e()},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.tagClearFailed"),"error")}}),{data:rt}=We({queryKey:["spool-assignments"],queryFn:()=>se.getAssignments(),enabled:t&&p}),st=n?rt?.find(Be=>Be.spool_id===n.id):void 0,{data:Le}=We({queryKey:["inventory-spools"],queryFn:()=>se.getSpools(!0),enabled:t}),{data:tt}=We({queryKey:["settings"],queryFn:se.getSettings,enabled:t}),Ce=(()=>{const Be=new Set;for(const nt of Le??[]){const gt=nt.category?.trim();gt&&Be.add(gt)}return Array.from(Be).sort((nt,gt)=>nt.localeCompare(gt))})(),Xe=tt?.low_stock_threshold??20,Ue=it({mutationFn:()=>{if(!st)throw new Error("No assignment");return se.unassignSpool(st.printer_id,st.ams_id,st.tray_id)},onSuccess:async()=>{await u.invalidateQueries({queryKey:["spool-assignments"]}),m(d("inventory.unassignSuccess","Spool unassigned"),"success"),e()},onError:Be=>{m(Be.message,"error")}}),fe=async Be=>{const nt=l?se.saveSpoolmanKProfiles:se.saveSpoolKProfiles;if(O.size===0)try{return await nt(Be,[]),!0}catch(ot){return console.error("Failed to save K-profiles:",ot),m(d("inventory.kProfileSaveFailed"),"warning"),!1}const gt=[];let yt=0;for(const ot of O){const[Ht,Kt,Zt]=ot.split(":"),Yt=parseInt(Ht),gn=parseInt(Kt),cn=Zt==="null"?0:parseInt(Zt),pt=Y.find(at=>at.printer.id===Yt);if(pt){const at=pt.calibrations.find(vt=>vt.cali_idx===gn);at?gt.push({printer_id:Yt,extruder:cn,nozzle_diameter:at.nozzle_diameter||"0.4",k_value:at.k_value,name:at.name||null,cali_idx:at.cali_idx,setting_id:at.setting_id||null}):yt++}else yt++}if(yt>0)return console.error(`saveKProfiles: ${yt} profile key(s) could not be resolved`,Array.from(O)),m(d("inventory.kProfileSaveFailed"),"warning"),!1;if(gt.length>0)try{return await nt(Be,gt),!0}catch(ot){return console.error("Failed to save K-profiles:",ot),m(d("inventory.kProfileSaveFailed"),"warning"),!1}return!0};if(v.useEffect(()=>{if(!t)return;const Be=nt=>{nt.key==="Escape"&&e()};return document.addEventListener("keydown",Be),()=>document.removeEventListener("keydown",Be)},[t,e]),!t)return null;const Ae=()=>{const Be=zce(b,F,l);if(!Be.isValid){x(Be.errors),(Be.errors.slicer_filament||Be.errors.material||Be.errors.brand||Be.errors.subtype)&&N("filament");return}const nt=ve?.displayName||de||null,gt={material:b.material||null,subtype:b.subtype||null,brand:b.brand||null,color_name:b.color_name||null,rgba:b.rgba||null,extra_colors:b.extra_colors||null,effect_type:b.effect_type||null,label_weight:b.label_weight,...l?{}:{core_weight:b.core_weight,core_weight_catalog_id:b.core_weight_catalog_id},slicer_filament:b.slicer_filament||null,slicer_filament_name:nt,nozzle_temp_min:null,nozzle_temp_max:null,note:b.note||null,cost_per_kg:b.cost_per_kg,category:b.category.trim()||null,low_stock_threshold_pct:b.low_stock_threshold_pct,...l?{spoolman_filament_id:b.spoolman_filament_id}:{}};(!p||C)&&(gt.weight_used=b.weight_used),(!p||T)&&(gt.storage_location=b.storage_location||null),p?Ie.mutate(gt):M>1?Oe.mutate({data:gt,qty:M}):_e.mutate(gt)},$e=_e.isPending||Oe.isPending||Ie.isPending||Ke.isPending||Ue.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.jsx("h2",{className:"text-lg font-semibold text-white",children:d(p?"inventory.editSpool":f?"inventory.copySpool":"inventory.addSpool")}),r.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white rounded transition-colors",children:r.jsx(Bt,{className:"w-5 h-5"})})]}),a==="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:()=>{S(!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 ${_==="filament"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[r.jsx(jS,{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 ${_==="pa-profile"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[r.jsx(yfe,{className:"w-4 h-4"}),d("inventory.paProfileTab"),Q>0&&r.jsx("span",{className:"text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:Q})]})]}),r.jsx("div",{className:"p-4 overflow-y-auto flex-1",style:{scrollbarGutter:"stable"},children:_==="filament"?r.jsxs("div",{className:"space-y-6",children:[l&&!p&&r.jsx("div",{children:ge?r.jsx("p",{className:"text-sm text-red-400 px-1",children:d("inventory.spoolmanCatalogLoadFailed")}):r.jsx(Hit,{filaments:oe,isLoading:ee,selectedId:b.spoolman_filament_id,onSelect:Ee})}),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(Kce,{formData:b,updateField:be,cloudAuthenticated:I,loadingCloudPresets:R,presetInputValue:de,setPresetInputValue:me,selectedPresetOption:ve,filamentOptions:ce,availableBrands:we,availableMaterials:Ne,quickAdd:F,quantity:M,onQuantityChange:z,errors:g})]}),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(Yce,{formData:b,updateField:be,recentColors:D,onColorUsed:ze,catalogColors:$})]}),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(Xce,{formData:b,updateField:be,spoolCatalog:ue,currencySymbol:s,availableCategories:Ce,globalLowStockThreshold:Xe,spoolmanMode:l})]}),p&&n&&!l&&r.jsx("div",{children:r.jsx(Vit,{spoolId:n.id})})]}):r.jsx(Qce,{formData:b,updateField:be,printersWithCalibrations:Y,selectedProfiles:O,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(Me,{variant:"secondary",onClick:()=>Ke.mutate(),disabled:$e||!n?.tag_uid,children:[r.jsx($m,{className:"w-4 h-4"}),d("inventory.clearRfid","Clear RFID Tag")]}),r.jsxs(Me,{variant:"secondary",onClick:()=>Ue.mutate(),disabled:$e||!st,children:[r.jsx(iu,{className:"w-4 h-4"}),d("inventory.unassignSpool","Unassign")]})]}),r.jsxs("div",{className:"flex gap-2 ml-auto",children:[r.jsx(Me,{variant:"secondary",onClick:e,children:d("common.cancel")}),r.jsx(Me,{onClick:Ae,disabled:$e,children:$e?r.jsxs(r.Fragment,{children:[r.jsx(ut,{className:"w-4 h-4 animate-spin"}),d("common.saving")]}):r.jsxs(r.Fragment,{children:[r.jsx(us,{className:"w-4 h-4"}),d(p?"common.save":f?"inventory.copySpool":"inventory.addSpool")]})})]})]})]})]})}function Wit({isOpen:t,onClose:e,columns:n,defaultColumns:a,onSave:i}){const{t:s}=kt(),[o,l]=v.useState(n),[c,d]=v.useState(null),u=v.useRef(null);if(v.useEffect(()=>{t&&l(n.map(C=>({...C})))},[t,n]),v.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,A)=>A===C?{...T,visible:!T.visible}:T))},p=(C,P)=>{P<0||P>=o.length||l(T=>{const A=[...T],[F]=A.splice(C,1);return A.splice(P,0,F),A})},f=(C,P)=>{u.current=P,d(P),C.dataTransfer.effectAllowed="move"},b=(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()},g=()=>{u.current=null,d(null)},x=()=>{l(a.map(C=>({...C})))},_=()=>{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(Ct,{className:"w-full max-w-md max-h-[80vh] flex flex-col",onClick:C=>C.stopPropagation(),children:r.jsxs(At,{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=>b(T,P),onDrop:w,onDragEnd:g,children:[r.jsx("div",{className:"cursor-grab text-bambu-gray/50 hover:text-bambu-gray",children:r.jsx(Ph,{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(tl,{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(Ln,{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(Dl,{className:"w-4 h-4"}):r.jsx(sb,{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(Me,{variant:"secondary",onClick:x,className:"mr-auto",children:[r.jsx(gs,{className:"w-4 h-4"}),s("inventory.reset")]}),r.jsx(Me,{variant:"secondary",onClick:e,children:s("inventory.cancel")}),r.jsx(Me,{onClick:_,children:s("inventory.applyChanges")})]})]})})})}const Kit=[{value:"ams_30x15",i18nKey:"ams",fallbackLabel:"AMS holder (30 × 15 mm)",fallbackHint:"Single label per page; fits the popular AMS filament label holder."},{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 Yit(t){const e=window.URL.createObjectURL(t);if(!window.open(e,"_blank","noopener,noreferrer")){const a=document.createElement("a");a.href=e,a.download="bambuddy-labels.pdf",document.body.appendChild(a),a.click(),document.body.removeChild(a)}setTimeout(()=>window.URL.revokeObjectURL(e),6e4)}function Xit(t){if(!t)return{backgroundColor:"#808080"};const e=t.replace(/^#/,"").slice(0,6);return e.length===6?{backgroundColor:`#${e}`}:{backgroundColor:"#808080"}}function Qit(t){const e=t.color_name??`${t.material}${t.subtype?` ${t.subtype}`:""}`,n=t.brand?` · ${t.brand}`:"";return`${e}${n}`}function Zit(t){return[t.color_name,t.material,t.subtype,t.brand,`#${t.id}`].filter(Boolean).join(" ").toLowerCase()}function Jit({isOpen:t,onClose:e,availableSpools:n,initialSelectedIds:a,spoolmanMode:i}){const{t:s}=kt(),{showToast:o}=rn(),[l,c]=v.useState(null),[d,u]=v.useState(new Set),[m,p]=v.useState(""),[f,b]=v.useState("");v.useEffect(()=>{if(t){const M=new Set(n.map(z=>z.id));u(new Set(a.filter(z=>M.has(z)))),p(""),b(""),c(null)}},[t]);const w=v.useMemo(()=>[...n].sort((M,z)=>M.id-z.id),[n]),g=v.useMemo(()=>{const M=new Set;for(const z of w)z.material&&M.add(z.material.toUpperCase());return[...M].sort()},[w]),x=v.useMemo(()=>{const M=m.trim().toLowerCase();return w.filter(z=>!(f&&(z.material||"").toUpperCase()!==f||M&&!Zit(z).includes(M)))},[w,m,f]),_=x.length>0&&x.every(M=>d.has(M.id));if(!t)return null;const N=d.size,C=N===0;function P(M){u(z=>{const I=new Set(z);return I.has(M)?I.delete(M):I.add(M),I})}function T(){u(M=>{const z=new Set(M);for(const I of x)z.add(I.id);return z})}function A(){u(M=>{const z=new Set(M);for(const I of x)z.delete(I.id);return z})}function F(){u(new Set)}async function S(M){if(C||l)return;const z=[...d].sort((I,H)=>I-H);c(M);try{const I=i?await se.printSpoolmanSpoolLabels({spool_ids:z,template:M}):await se.printSpoolLabels({spool_ids:z,template:M});Yit(I),e()}catch(I){const H=I instanceof Error?I.message:String(I);o(s("inventory.labels.error","Could not generate labels: {{msg}}",{msg:H}),"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(pr,{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")}),N>0&&r.jsxs("span",{className:"text-sm text-bambu-gray",children:["(",s("inventory.labels.selectedCount","{{count}} selected",{count:N}),")"]})]}),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(Bt,{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(Sr,{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:M=>p(M.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"})]}),g.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:()=>b(""),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")}),g.map(M=>r.jsx("button",{type:"button",onClick:()=>b(M),className:`px-2 py-0.5 text-xs rounded-full border transition ${f===M?"bg-bambu-green text-bambu-dark border-bambu-green":"bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:M},M))]})]}),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:_?A:T,disabled:x.length===0,className:"text-bambu-green hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed",children:_?s("inventory.labels.deselectVisible","Deselect visible"):s("inventory.labels.selectVisible","Select all visible ({{count}})",{count:x.length})}),r.jsx("button",{type:"button",onClick:F,disabled:N===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:x.length===0?r.jsx("div",{className:"text-center text-sm text-bambu-gray py-6",children:w.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:x.map(M=>{const z=d.has(M.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:[z?r.jsx(bs,{className:"w-4 h-4 text-bambu-green shrink-0"}):r.jsx(so,{className:"w-4 h-4 text-bambu-gray shrink-0"}),r.jsx("input",{type:"checkbox",checked:z,onChange:()=>P(M.id),className:"sr-only"}),r.jsx("span",{className:"w-4 h-4 rounded border border-black/20 shrink-0",style:Xit(M.rgba)}),r.jsx("span",{className:"flex-1 min-w-0 truncate text-sm text-white",children:Qit(M)}),r.jsxs("span",{className:"text-xs font-mono text-bambu-gray shrink-0",children:["#",M.id]})]})},M.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:Kit.map(M=>{const z=l===M.value,I=s(`inventory.labels.templates.${M.i18nKey}.label`,M.fallbackLabel),H=s(`inventory.labels.templates.${M.i18nKey}.hint`,M.fallbackHint);return r.jsxs("button",{disabled:C||l!==null,onClick:()=>S(M.value),title:`${I} — ${H}`,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:I}),r.jsx("div",{className:"text-xs text-bambu-gray mt-0.5 truncate",children:H})]}),z&&r.jsx(ut,{className:"w-4 h-4 animate-spin text-bambu-green shrink-0"})]},M.value)})}),r.jsx("div",{className:"flex justify-end gap-2 px-5 py-2 border-t border-bambu-dark-tertiary",children:r.jsx(Me,{variant:"secondary",onClick:e,disabled:l!==null,children:s("common.cancel","Cancel")})})]})]})}function yQ(t){return`${t.material}|${t.subtype||""}|${t.brand||""}|${t.color_name||""}|${t.rgba||""}|${t.extra_colors||""}|${t.effect_type||""}|${t.label_weight}`}const Zce="bambuddy-inventory-columns",HN=[{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 est(){try{const t=localStorage.getItem(Zce);if(t){const e=JSON.parse(t),n=new Set(HN.map(o=>o.id)),a=new Set(e.map(o=>o.id)),i=e.filter(o=>n.has(o.id)),s=HN.filter(o=>!a.has(o.id));return[...i,...s]}}catch{}return HN.map(t=>({...t}))}function tst(t){try{localStorage.setItem(Zce,JSON.stringify(t))}catch{}}function au(t,e=!1){return e&&t>=1e3?`${(t/1e3).toFixed(1)}kg`:`${Math.round(t)}g`}const nst={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 gF(t,e="system"){if(!t)return"-";const n=Xn(t);return n?YF(n,e):"-"}const rst={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")},XI={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:gF(t.created_at,e)}),encode_time:({spool:t,dateFormat:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:gF(t.encode_time,e)}),last_used_time:({spool:t,dateFormat:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.last_used?gF(t.last_used,e):"Never"}),rgba:({spool:t})=>r.jsx("div",{className:"flex items-center justify-center",children:r.jsx(rU,{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:PT(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 n=e[t.id];if(!n)return r.jsx("span",{className:"text-sm text-bambu-gray",children:"-"});const a=n.printer_name||`Printer ${n.printer_id}`,i=n.ams_id===254||n.ams_id===255,s=!i&&n.ams_id>=128,o=c_(n.ams_id,n.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:[a," ",o,n.ams_label?` (${n.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:au(t.label_weight)}),net:({remaining:t})=>r.jsx("span",{className:"text-sm text-white",children:au(t)}),gross:({spool:t,remaining:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:au(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?au(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 n=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:n?.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 n=t.last_scale_weight;if(n==null)return r.jsx("span",{className:"text-sm text-bambu-gray/50",title:"No scale measurement",children:"-"});const a=t.core_weight||0,i=Math.max(0,t.label_weight-t.weight_used)+a;let s,o;n0?`+${Math.round(s)}`:`${Math.round(s)}`,c=o?`Scale: ${Math.round(n)}g
+`),N=new Blob([_],{type:"text/csv"}),C=URL.createObjectURL(N),P=document.createElement("a");P.href=C,P.download=`shopping-list-${new Date().toISOString().slice(0,10)}.csv`,P.click(),setTimeout(()=>URL.revokeObjectURL(C),100)}return r.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary",children:[r.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30",children:[r.jsxs("div",{className:"flex items-center gap-3",children:[r.jsx(lb,{className:"w-4 h-4 text-bambu-green"}),r.jsx("h3",{className:"text-sm font-semibold text-white",children:l("forecast.shoppingList")}),r.jsx("span",{className:"text-xs text-bambu-gray",children:l("forecast.shoppingListItems",{count:t.length})}),t.length>0&&r.jsxs("div",{className:"flex bg-bambu-dark-tertiary rounded-md p-0.5 ml-1",children:[r.jsxs("button",{onClick:()=>u("list"),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${d==="list"?"bg-bambu-dark-secondary text-white shadow":"text-bambu-gray hover:text-white"}`,children:[r.jsx(Br,{className:"w-3 h-3"}),l("forecast.listView")]}),r.jsxs("button",{onClick:()=>u("logistics"),className:`flex items-center gap-1.5 px-2 py-0.5 text-xs font-medium rounded transition-colors ${d==="logistics"?"bg-bambu-dark-secondary text-white shadow":"text-bambu-gray hover:text-white"}`,children:[r.jsx(Hfe,{className:"w-3 h-3"}),l("forecast.logisticsView"),b.length>0&&r.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:b.length})]})]})]}),r.jsxs("div",{className:"flex items-center gap-2",children:[t.length>0&&r.jsxs(r.Fragment,{children:[r.jsxs("button",{onClick:w,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:[r.jsx(Yr,{className:"w-3 h-3"}),l("forecast.downloadCsv")]}),a&&r.jsx("button",{onClick:o,className:"text-xs text-red-400 hover:text-red-300 transition-colors px-2 py-1 rounded border border-red-500/20 hover:bg-red-500/10",children:l("forecast.clearAll")})]}),r.jsx("button",{onClick:i,className:"p-1 text-bambu-gray hover:text-white transition-colors",children:r.jsx(Bt,{className:"w-4 h-4"})})]})]}),t.length===0?r.jsxs("div",{className:"flex flex-col items-center py-8 text-bambu-gray",children:[r.jsx(Br,{className:"w-8 h-8 mb-2 opacity-30"}),r.jsx("p",{className:"text-sm",children:l("forecast.shoppingListEmpty")})]}):d==="list"?r.jsx("div",{className:"overflow-x-auto",children:r.jsxs("table",{className:"w-full",children:[r.jsx("thead",{children:r.jsxs("tr",{className:"border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/20",children:[r.jsx("th",{className:"px-4 py-2 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.qty")}),r.jsx("th",{className:"px-4 py-2 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.material")}),r.jsx("th",{className:"px-4 py-2 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.weight")}),r.jsx("th",{className:"px-4 py-2 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.leadTime")}),r.jsx("th",{className:"px-4 py-2 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.expectedRestock")}),r.jsx("th",{className:"px-4 py-2 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.status")}),r.jsx("th",{className:"px-4 py-2 text-left text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.note")}),r.jsx("th",{className:"px-4 py-2 text-right text-xs font-medium text-bambu-gray uppercase tracking-wide",children:l("forecast.actions")})]})}),r.jsx("tbody",{className:"divide-y divide-bambu-dark-tertiary",children:t.map(g=>{const x=[g.brand,g.material,g.subtype].filter(Boolean).join(" "),_=b.some(z=>z.item.id===g.id),N=p.get(ww(g.material,g.subtype,g.brand))??null,C=N&&N.totalSpools>0?N.totalLabelG/N.totalSpools:1e3,P=Math.round(g.quantity_spools*C),T=N?.effectiveLeadTimeDays??n??0,A=T>0?Um(new Date,T):null,F=g.status==="purchased"||g.status==="received",S=g.status==="received",M=m.isPending;return r.jsxs("tr",{className:`hover:bg-bambu-dark-tertiary/30 transition-colors ${_&&!F?"bg-red-500/5":""}`,children:[r.jsx("td",{className:"px-4 py-2.5",children:r.jsxs("span",{className:"text-sm font-semibold text-bambu-green",children:[g.quantity_spools,"×"]})}),r.jsx("td",{className:"px-4 py-2.5",children:r.jsxs("div",{className:"flex items-center gap-2",children:[r.jsx("span",{className:"text-sm text-white",children:x}),_&&!F&&r.jsx(Nn,{className:"w-3.5 h-3.5 text-red-400 flex-shrink-0","aria-label":l("forecast.stockBreakBefore")})]})}),r.jsx("td",{className:"px-4 py-2.5",children:r.jsx("span",{className:"text-sm text-white",children:P>=1e3?`${(P/1e3).toFixed(1)}kg`:`${P}g`})}),r.jsx("td",{className:"px-4 py-2.5",children:r.jsx("span",{className:"text-sm text-bambu-gray",children:T>0?`${T}d`:"—"})}),r.jsx("td",{className:"px-4 py-2.5",children:r.jsx("span",{className:"text-sm text-bambu-gray",children:A?wS(A):"—"})}),r.jsx("td",{className:"px-4 py-2.5",children:r.jsx("span",{className:`inline-flex items-center px-2 py-0.5 rounded text-xs font-medium ${S?"bg-bambu-green/20 text-bambu-green":F?"bg-blue-400/20 text-blue-400":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:l(S?"forecast.received":F?"forecast.purchased":"forecast.pending")})}),r.jsx("td",{className:"px-4 py-2.5",children:r.jsx("span",{className:"text-xs text-bambu-gray",children:g.note||"—"})}),r.jsx("td",{className:"px-4 py-2.5",children:r.jsx("div",{className:"flex items-center justify-end gap-1",children:a&&r.jsxs(r.Fragment,{children:[r.jsx("button",{onClick:()=>m.mutate({id:g.id,status:F?"pending":"purchased"}),disabled:M||S,title:l(F?"forecast.resetToPending":"forecast.markPurchased"),className:`p-1 rounded transition-colors disabled:opacity-30 ${F?"text-blue-400 hover:text-bambu-gray":"text-bambu-gray hover:text-blue-400"}`,children:F?r.jsx(gs,{className:"w-3.5 h-3.5"}):r.jsx(Tge,{className:"w-3.5 h-3.5"})}),r.jsx("button",{onClick:()=>m.mutate({id:g.id,status:"received",item:g,avgSpoolG:C}),disabled:M||!F||S,title:l("forecast.markReceived"),className:"p-1 rounded transition-colors text-bambu-gray hover:text-bambu-green disabled:opacity-30",children:r.jsx(Axe,{className:"w-3.5 h-3.5"})}),r.jsx("button",{onClick:()=>s(g.id),className:"p-1 text-bambu-gray hover:text-red-400 transition-colors",title:l("forecast.remove"),children:r.jsx(Jt,{className:"w-3.5 h-3.5"})})]})})})]},g.id)})})]})}):r.jsx("div",{className:"divide-y divide-bambu-dark-tertiary",children:f.filter(({item:g})=>g.status!=="received").map(({item:g,forecast:x})=>r.jsx(Eit,{item:g,forecast:x,globalLeadTime:n,canWrite:a,onRemove:()=>s(g.id)},g.id))})]})}function Eit({item:t,forecast:e,globalLeadTime:n,canWrite:a,onRemove:i}){const{t:s}=kt(),o=[t.brand,t.material,t.subtype].filter(Boolean).join(" "),l=v.useMemo(()=>{if(!e||e.dailyRateG===null||e.dailyRateG<=0)return null;const u=e.dailyRateG,m=e.effectiveLeadTimeDays,p=e.totalSpools>0?e.totalLabelG/e.totalSpools:1e3,f=t.quantity_spools*p,b=Math.max(0,e.totalRemainingG-u*m),w=b+f,g=Math.ceil(w/u),x=Math.min(m+g+5,365),_=[];for(let N=0;N<=x;N++){const C=Sw(Um(new Date,N));if(N===m)_.push({day:N,label:C,stock:Math.round(b),rop:Math.round(e.reorderPointG),safetyStock:Math.round(e.safetyStockG)}),_.push({day:N,label:C,stock:Math.round(w),rop:Math.round(e.reorderPointG),safetyStock:Math.round(e.safetyStockG),arrival:!0});else{const P=N{if(!e||e.dailyRateG===null||e.dailyRateG<=0)return null;const u=Math.floor(e.totalRemainingG/e.dailyRateG);return uu>=1e3?`${(u/1e3).toFixed(1)}kg`:`${u}g`,width:44}),r.jsx(nu,{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(DA,{type:"linear",dataKey:"stock",stroke:"#1DB954",strokeWidth:2,fill:`url(#cart-post-${t.id})`,dot:!1,activeDot:{r:3}}),e.reorderPointG>0&&r.jsx(ru,{y:e.reorderPointG,stroke:"#F59E0B",strokeDasharray:"5 3",strokeOpacity:.8,label:{value:"ROP",position:"insideTopRight",fill:"#F59E0B",fontSize:9}}),e.safetyStockG>0&&r.jsx(ru,{y:e.safetyStockG,stroke:"#6B7280",strokeDasharray:"3 3",strokeOpacity:.6,label:{value:"SS",position:"insideTopRight",fill:"#6B7280",fontSize:9}}),r.jsx(ru,{x:Sw(Um(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(ru,{x:Sw(Um(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 Dit({forecast:t,onClose:e,onAdd:n}){const{t:a}=kt(),i=[t.group.brand,t.group.material,t.group.subtype].filter(Boolean).join(" "),[s,o]=v.useState("qty"),[l,c]=v.useState("1"),[d,u]=v.useState("30"),[m,p]=v.useState(""),f=v.useMemo(()=>{if(!t.dailyRateG||t.dailyRateG<=0)return null;const g=t.dailyRateG*Number(d),x=t.group.spools.length>0?t.group.spools.reduce((_,N)=>_+N.label_weight,0)/t.group.spools.length:1e3;return Math.ceil(g/x)},[t,d]),b=s==="qty"?parseInt(l,10)||1:f??1;function w(g){g.preventDefault(),n({material:t.group.material,subtype:t.group.subtype,brand:t.group.brand,quantity_spools:b,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(lb,{className:"w-5 h-5 text-bambu-green"}),r.jsx("h2",{className:"text-base font-semibold text-white",children:a("forecast.addToCartTitle")})]}),r.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white transition-colors",children:r.jsx(Bt,{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:a("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:a("forecast.byDuration")})]}),s==="qty"?r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("label",{className:"text-xs text-bambu-gray",children:a("forecast.numberOfSpools")}),r.jsx("input",{type:"number",min:1,max:99,value:l,onChange:g=>c(g.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:a("forecast.lastHowManyDays")}),r.jsx("input",{type:"number",min:1,max:365,value:d,onChange:g=>u(g.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:a("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:a("forecast.noUsageQty")})]}),r.jsxs("div",{className:"space-y-1.5",children:[r.jsx("label",{className:"text-xs text-bambu-gray",children:a("forecast.noteOptional")}),r.jsx("input",{type:"text",maxLength:200,value:m,onChange:g=>p(g.target.value),placeholder:a("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:a("forecast.addNSpools",{count:b})}),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:a("forecast.cancel")})]})]})]})})}const YI={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},Fit=new Set(["material","subtype","brand","rgba","color_name","label_weight"]);function zce(t,e=!1,n=!1){const a={};return e||n?(!t.material&&!t.spoolman_filament_id&&(a.material="Material is required"),{isValid:Object.keys(a).length===0,errors:a}):(t.slicer_filament||(a.slicer_filament="Slicer preset is required"),t.material||(a.material="Material is required"),t.brand||(a.brand="Brand is required"),t.subtype||(a.subtype="Subtype is required"),{isValid:Object.keys(a).length===0,errors:a})}const Uce=["PLA","PETG","ABS","TPU","ASA","PC","PA","PVA","HIPS","PA-CF","PETG-CF","PLA-CF"],Lit=["Bambu","PolyLite","PolyTerra","eSUN","Overture","Fiberon","SUNLU","Inland","Hatchbox","Generic"],_w=["Basic","Matte","Silk","Silk+","Tough","Tough+","HF","High Flow","Engineering","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"],Bce=[{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"}],Rit=[{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"}],bQ=[...Bce,...Rit],Hce="bambuddy-recent-colors",Iit=8,Oit=[{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 Jh(t){let e=t.replace(/@.*$/,"").trim();e=e.replace(/\(Custom\)/i,"").trim(),e=e.replace(/^[#*]+\s*/,"").trim();const n=["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 a="",i=-1;for(const c of n){const d=e.toUpperCase().indexOf(c.toUpperCase());if(d!==-1){a=c,i=d;break}}let s="";i>0&&(s=e.substring(0,i).trim(),s=s.replace(/[-_\s]+$/,""));let o="";i!==-1&&a&&(o=e.substring(i+a.length).trim(),o=o.replace(/^[-_\s]+/,""));let l="";for(const c of _w)if(o.toLowerCase().includes(c.toLowerCase())){l=c;break}if(!l&&s)for(const c of _w){const d=new RegExp(`\\s+${c}$`,"i");if(d.test(s)){l=c,s=s.replace(d,"").trim();break}}return{brand:s,material:a,variant:l}}function qce(t,e){const n=new Set(Lit);for(const a of t){const{brand:i}=Jh(a.name);i&&i.length>1&&n.add(i)}if(e)for(const a of e)if(a.filament_vendor&&a.filament_vendor.length>1)n.add(a.filament_vendor);else{const{brand:i}=Jh(a.name);i&&i.length>1&&n.add(i)}return Array.from(n).sort((a,i)=>a.localeCompare(i))}function zit(t){const e=t.filter(a=>a.preset_type==="filament");return e.length===0?[]:e.map(a=>{const i=a.filament_type||String(a.id),s=Array.from(new Set([i,String(a.id)]));return{code:i,name:a.name,displayName:a.name,isCustom:!1,allCodes:s}}).sort((a,i)=>a.displayName.localeCompare(i.displayName))}function $ce(t,e,n,a){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=n&&n.length>0?zit(n):[],c=[];if(a&&a.length>0)for(const u of a){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?Oit:d.sort((u,m)=>u.displayName.localeCompare(m.displayName))}function Vce(t,e){if(!t)return;let n=e.find(a=>a.code===t);if(n||(n=e.find(a=>a.allCodes.includes(t))),!n){const a=t.toLowerCase();n=e.find(i=>i.code.toLowerCase()===a||i.allCodes.some(s=>s.toLowerCase()===a))}return n}function Gce(){try{const t=localStorage.getItem(Hce);if(t)return JSON.parse(t)}catch{}return[]}function Wce(t,e){const n=e.filter(i=>i.hex.toUpperCase()!==t.hex.toUpperCase()),a=[t,...n].slice(0,Iit);try{localStorage.setItem(Hce,JSON.stringify(a))}catch{}return a}function fF(t,e){if(!e.material)return!1;const a=(t.name||"").replace(/^High Flow[_\s]+/i,"").replace(/^Standard[_\s]+/i,"").replace(/^HF[_\s]+/i,"").replace(/^S[_\s]+/i,"").trim(),i=Jh(a);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())||a.toLowerCase().includes(e.subtype.toLowerCase())))}function Kce({formData:t,updateField:e,cloudAuthenticated:n,loadingCloudPresets:a,presetInputValue:i,setPresetInputValue:s,selectedPresetOption:o,filamentOptions:l,availableBrands:c,availableMaterials:d,quickAdd:u,quantity:m,onQuantityChange:p,errors:f}){const{t:b}=kt(),[w,g]=v.useState(!1),[x,_]=v.useState(!1),[N,C]=v.useState(!1),[P,T]=v.useState(!1),[A,F]=v.useState(""),[S,M]=v.useState(""),[z,I]=v.useState(""),[H,R]=v.useState(String(t.label_weight)),[Z,re]=v.useState(!1),J=v.useRef(null),de=v.useRef(null),me=v.useRef(null),ue=v.useRef(null);v.useEffect(()=>{const $=K=>{J.current&&!J.current.contains(K.target)&&g(!1),ue.current&&!ue.current.contains(K.target)&&T(!1),de.current&&!de.current.contains(K.target)&&_(!1),me.current&&!me.current.contains(K.target)&&C(!1)};return document.addEventListener("mousedown",$),()=>document.removeEventListener("mousedown",$)},[]);const W=v.useMemo(()=>{if(!i)return l;const $=i.toLowerCase();return l.filter(K=>K.displayName.toLowerCase().includes($)||K.code.toLowerCase().includes($))},[l,i]),ae=v.useMemo(()=>{if(!A)return c;const $=A.toLowerCase();return c.filter(D=>D.toLowerCase().includes($)).sort((D,j)=>{const B=D.toLowerCase()===$,X=j.toLowerCase()===$;return B&&!X?-1:!B&&X?1:D.localeCompare(j)})},[c,A]),pe=v.useMemo(()=>{if(!S)return _w;const $=S.toLowerCase();return _w.filter(K=>K.toLowerCase().includes($))},[S]),ye=v.useMemo(()=>{if(!z)return d;const $=z.toLowerCase();return d.filter(D=>D.toLowerCase().includes($)).sort((D,j)=>{const B=D.toLowerCase()===$,X=j.toLowerCase()===$;return B&&!X?-1:!B&&X?1:D.localeCompare(j)})},[z,d]);v.useEffect(()=>{Z||R(String(t.label_weight))},[t.label_weight,Z]);const Te=$=>{e("slicer_filament",$.code),s($.displayName),g(!1);const K=Jh($.name);K.material&&e("material",K.material),K.brand&&e("brand",K.brand),K.variant&&e("subtype",K.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:a?r.jsxs(r.Fragment,{children:[r.jsx(ut,{className:"w-3 h-3 animate-spin"})," ",b("inventory.loadingPresets")]}):n?r.jsxs(r.Fragment,{children:[r.jsx(Hh,{className:"w-3 h-3 text-bambu-green"})," ",b("inventory.cloudConnected")]}):r.jsxs(r.Fragment,{children:[r.jsx(UF,{className:"w-3 h-3"})," ",b("inventory.cloudNotConnected")]})}),!u&&r.jsxs("div",{children:[r.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[b("inventory.slicerPreset")," *"]}),r.jsxs("div",{className:"relative",ref:J,children:[r.jsx(Sr,{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:b("inventory.searchPresets"),value:i,onChange:$=>{s($.target.value),g(!0)},onFocus:()=>{g(!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:W.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noPresetsFound")}):W.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary truncate ${o?.code===$.code?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>Te($),children:$.displayName},`${$.code}::${$.name}`))})]}),o&&r.jsxs("div",{className:"mt-1 text-xs text-bambu-gray",children:[b("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:[b("inventory.material")," *"]}),r.jsxs("div",{className:"relative",ref:ue,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:b("inventory.selectMaterial"),value:P?z:t.material,onChange:$=>{I($.target.value),T(!0)},onFocus:()=>{T(!0),I("")}}),r.jsx(Ln,{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:[ye.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noResults")}):ye.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.material===$?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("material",$),T(!1),I("")},children:$},$)),z&&!ye.includes(z)&&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",z),T(!1),I("")},children:b("inventory.useCustomMaterial",{material:z})})]})]}),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:[b("inventory.brand"),!u&&" *"]}),r.jsxs("div",{className:"relative",ref:de,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:b("inventory.searchBrand"),value:x?A:t.brand,onChange:$=>{F($.target.value),_(!0)},onFocus:()=>{_(!0),F("")}}),r.jsx(Ln,{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:[ae.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noResults")}):ae.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.brand===$?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("brand",$),_(!1),F("")},children:$},$)),A&&!ae.includes(A)&&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",A),_(!1),F("")},children:b("inventory.useCustomBrand",{brand:A})})]})]}),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:[b("inventory.subtype"),!u&&" *"]}),r.jsxs("div",{className:"relative",ref:me,children:[r.jsx("input",{type:"text",value:N?S:t.subtype,onChange:$=>{M($.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(Ln,{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:[pe.length===0?r.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:b("inventory.noResults")}):pe.map($=>r.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.subtype===$?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("subtype",$),C(!1),M("")},children:$},$)),S&&!_w.some($=>$.toLowerCase()===S.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",S),C(!1),M("")},children:b("inventory.useCustomBrand",{brand:S})})]})]}),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:b("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:H,min:0,onFocus:()=>re(!0),onChange:$=>R($.target.value),onBlur:()=>{re(!1);const $=H.trim(),K=Number($);if(!$||!Number.isFinite(K)||K<0){R(String(t.label_weight));return}const D=Math.round(K);e("label_weight",D),R(String(D))}}),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:b("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:$=>{const K=Math.max(1,Math.min(100,parseInt($.target.value)||1));p(K)}})]})]})}function Uit(t){const e=t.split(/[\s,]+/).map(i=>i.trim().replace(/^#/,"")).filter(Boolean),n=[],a=[];for(const i of e)(i.length===6||i.length===8)&&/^[0-9a-fA-F]+$/.test(i)?n.push(i.toLowerCase()):a.push(i);return{value:n.join(","),invalid:a}}function Yce({formData:t,updateField:e,recentColors:n,onColorUsed:a,catalogColors:i}){const{t:s}=kt(),[o,l]=v.useState(!1),[c,d]=v.useState(""),u=t.rgba.replace("#","").substring(0,6),m=F=>u.toUpperCase()===F.toUpperCase(),p=(F,S)=>{e("rgba",F.toUpperCase()+"FF"),e("color_name",S),a({name:S,hex:F})},f=v.useMemo(()=>{if(i.length===0)return[];const F=t.brand?.trim(),S=t.material?.toLowerCase().trim(),M=t.subtype?.toLowerCase().trim();if(!F&&!S)return[];const z=F?F.toLowerCase().split(/[\s\-_]+/).filter(R=>R.length>=2):[],I=R=>{if(z.length===0)return!0;const Z=R.toLowerCase();return z.some(re=>Z.includes(re))};if(F&&!S){const R=i.filter(Z=>I(Z.manufacturer));if(R.length>0)return R.map(Z=>({name:Z.color_name,hex:Z.hex_color.replace("#","").substring(0,6),manufacturer:Z.manufacturer,material:typeof Z.material=="string"?Z.material:void 0}))}const H=S&&M?`${S} ${M}`:"";if(H){const R=i.filter(re=>I(re.manufacturer)&&re.material?.toLowerCase()===H);if(R.length>0)return R.map(re=>({name:re.color_name,hex:re.hex_color.replace("#","").substring(0,6),manufacturer:re.manufacturer,material:typeof re.material=="string"?re.material:void 0}));const Z=H.replace(/\+$/,"");if(Z!==H){const re=i.filter(J=>I(J.manufacturer)&&J.material?.toLowerCase()===Z);if(re.length>0)return re.map(J=>({name:J.color_name,hex:J.hex_color.replace("#","").substring(0,6),manufacturer:J.manufacturer,material:typeof J.material=="string"?J.material:void 0}))}}if(S){const R=i.filter(Z=>I(Z.manufacturer)&&(!Z.material||Z.material.toLowerCase().startsWith(S)));if(R.length>0)return R.map(Z=>({name:Z.color_name,hex:Z.hex_color.replace("#","").substring(0,6),manufacturer:Z.manufacturer,material:typeof Z.material=="string"?Z.material:void 0}))}return[]},[i,t.brand,t.material,t.subtype]),b=v.useMemo(()=>{if(!c)return f;if(f.length===0)return[];const F=c.toLowerCase();return f.filter(M=>M.name.toLowerCase().includes(F)||(M.manufacturer?.toLowerCase().includes(F)??!1)||(M.material?.toLowerCase().includes(F)??!1))},[c,f]),w=f.length>0,g=v.useMemo(()=>c?bQ.filter(F=>F.name.toLowerCase().includes(c.toLowerCase())):o?bQ:Bce,[c,o]),[x,_]=v.useState(t.extra_colors),[N,C]=v.useState([]),P=v.useRef(t.extra_colors);v.useEffect(()=>{t.extra_colors!==P.current&&(_(t.extra_colors),C([]),P.current=t.extra_colors)},[t.extra_colors]);const T=v.useMemo(()=>rj({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]),A=F=>{if(_(F),!F.trim()){C([]),P.current="",e("extra_colors","");return}const{value:S,invalid:M}=Uit(F);C(M),P.current=S,e("extra_colors",S)};return r.jsxs("div",{className:"space-y-3",children:[r.jsx("div",{className:"h-10 rounded-lg border border-bambu-dark-tertiary",style:T,"data-testid":"color-preview-banner"}),n.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(Zn,{className:"w-3 h-3"}),r.jsx("span",{children:s("inventory.recentColors")})]}),r.jsx("div",{className:"flex flex-wrap gap-1.5",children:n.map(F=>r.jsx("button",{type:"button",onClick:()=>p(F.hex,F.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 ${m(F.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${F.hex}`},title:F.name},F.hex))})]}),r.jsxs("div",{className:"relative",children:[r.jsx(Sr,{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:F=>d(F.target.value)})]}),w?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(F=>r.jsx("button",{type:"button",onClick:()=>p(F.hex,F.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 hover:z-20 relative group ${m(F.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${F.hex}`},title:F.manufacturer&&F.material?`${F.name} (${F.manufacturer} — ${F.material})`:F.manufacturer?`${F.name} (${F.manufacturer})`:F.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:F.manufacturer&&F.material?`${F.name} (${F.manufacturer} — ${F.material})`:F.manufacturer?`${F.name} (${F.manufacturer})`:F.name})},`${F.hex}-${F.name}-${F.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(tl,{className:"w-3 h-3"})]}):r.jsxs(r.Fragment,{children:[s("inventory.showAll")," ",r.jsx(Ln,{className:"w-3 h-3"})]})})]}),r.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[g.map(F=>r.jsx("button",{type:"button",onClick:()=>p(F.hex,F.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 hover:z-20 relative group ${m(F.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${F.hex}`},title:F.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:F.name})},F.hex)),g.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:F=>e("color_name",F.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:u.toUpperCase(),onChange:F=>{const S=F.target.value.replace("#","").replace(/[^0-9A-Fa-f]/g,"").toUpperCase();if(S.length>8)return;const M=S.length===8?S:S.length===7?S.substring(0,6)+"FF":S.padEnd(6,"0")+"FF";e("rgba",M)}})]}),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:F=>{const S=F.target.value.replace("#","").toUpperCase();e("rgba",S+"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:x,onChange:F=>A(F.target.value),"data-testid":"extra-colors-input"}),N.length>0&&r.jsx("p",{className:"text-xs text-red-400 mt-1",children:s("inventory.extraColorsInvalid",{tokens:N.join(", ")})}),!N.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(Dg,{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:F=>e("effect_type",F.target.value),"data-testid":"effect-type-select",children:WI.map(F=>r.jsx("option",{value:F.value,children:s(F.labelKey)},F.value||"none"))}),r.jsx(rU,{rgba:t.rgba,extraColors:t.extra_colors,effectType:t.effect_type,subtype:t.subtype,effectSize:"preview",className:"w-10 h-10",shape:"square"})]})]})]})]})}function Bit({catalog:t,value:e,onChange:n,catalogId:a,onCatalogIdChange:i}){const{t:s}=kt(),[o,l]=v.useState(!1),[c,d]=v.useState(""),u=v.useRef(null),m=v.useRef(null);v.useEffect(()=>{const w=g=>{u.current&&!u.current.contains(g.target)&&l(!1)};return document.addEventListener("mousedown",w),()=>document.removeEventListener("mousedown",w)},[]),v.useEffect(()=>{if(t.length===0)return;const w=t.filter(g=>g.weight===e);if(a){const g=t.find(x=>x.id===a);if(g&&g.weight===e)return}w.length===1?i(w[0].id):w.length===0&&a!==null&&i(null)},[e,t,a,i]);const p=v.useMemo(()=>{if(!c)return t;const w=c.toLowerCase();return t.filter(g=>g.name.toLowerCase().includes(w)||g.weight.toString().includes(w))},[t,c]),f=v.useMemo(()=>t.filter(w=>w.weight===e),[t,e]),b=v.useMemo(()=>{if(o)return c;if(a){const w=t.find(g=>g.id===a);if(w)return w.name}return f.length>0?f[0].name:""},[o,c,a,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(FZ,{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:b,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 ${(a?w.id===a:w.weight===e)?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{i(w.id),n(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 g=parseInt(w.target.value);!isNaN(g)&&g>=0&&n(g)}}),r.jsx("span",{className:"text-bambu-gray text-sm",children:"g"})]})]})]})}function Xce({formData:t,updateField:e,spoolCatalog:n,currencySymbol:a,availableCategories:i,globalLowStockThreshold:s,spoolmanMode:o=!1}){const{t:l}=kt(),{showToast:c}=rn(),[d,u]=v.useState(""),[m,p]=v.useState(!1),[f,b]=v.useState(""),[w,g]=v.useState(!1),x=Math.max(0,t.label_weight-t.weight_used),_=t.core_weight+x;return v.useEffect(()=>{m||u(String(_))},[m,_]),v.useEffect(()=>{w||b(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(Bit,{catalog:n,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:()=>g(!0),onChange:N=>{b(N.target.value)},onBlur:()=>{g(!1);const N=f.trim(),C=Number(N);if(!N||!Number.isFinite(C)||C<0||C>t.label_weight){b(String(x));return}const P=Math.round(C);e("weight_used",Math.max(0,t.label_weight-P)),b(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(_));return}const A=Math.round(C),F=Math.max(0,Math.min(t.label_weight,A-t.core_weight));e("weight_used",Math.max(0,t.label_weight-F)),u(String(A))},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:a}),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,a.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 Hit({filaments:t,isLoading:e,selectedId:n,onSelect:a}){const{t:i}=kt(),[s,o]=v.useState(!1),[l,c]=v.useState(""),d=v.useRef(null),u=v.useRef(null),m=v.useMemo(()=>t.find(g=>g.id===n)??null,[t,n]);v.useEffect(()=>{const g=x=>{d.current&&!d.current.contains(x.target)&&(o(!1),c(""))};return document.addEventListener("mousedown",g),()=>document.removeEventListener("mousedown",g)},[]);const p=v.useMemo(()=>{const g=l.toLowerCase().trim();return g?t.filter(x=>x.name.toLowerCase().includes(g)||(x.material?.toLowerCase().includes(g)??!1)||(x.vendor?.name.toLowerCase().includes(g)??!1)||(x.color_name?.toLowerCase().includes(g)??!1)):t},[t,l]);v.useEffect(()=>{s&&u.current?.focus()},[s]);const f=()=>{o(!0),c("")},b=g=>{a(g),o(!1),c("")},w=g=>g?`#${g.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(ut,{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(Br,{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(Ln,{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:g=>c(g.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(ut,{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(g=>r.jsx("li",{children:r.jsxs("button",{type:"button",onClick:()=>b(g),className:`w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-bambu-dark-tertiary transition-colors ${g.id===n?"bg-bambu-dark-tertiary":""}`,children:[r.jsx("span",{className:"w-4 h-4 rounded-full shrink-0 border border-white/20",style:{backgroundColor:w(g.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:[g.vendor?.name?`${g.vendor.name} — `:"",g.name]}),r.jsxs("span",{className:"block text-bambu-gray text-xs truncate",children:[[g.material,g.color_name].filter(Boolean).join(" · "),g.weight?` · ${g.weight}g`:""]})]})]})},g.id))})]})]})}function Qce({formData:t,printersWithCalibrations:e,selectedProfiles:n,setSelectedProfiles:a,expandedPrinters:i,setExpandedPrinters:s}){const{t:o}=kt(),l=p=>{s(f=>{const b=new Set(f);return b.has(p)?b.delete(p):b.add(p),b})},c=(p,f,b)=>{const w=`${p}:${f}:${b??"null"}`,g=`${p}:${b??"null"}`;a(x=>{const _=new Set(x);if(_.has(w))_.delete(w);else{for(const N of Array.from(_)){const C=N.split(":");`${C[0]}:${C[2]}`===g&&_.delete(N)}_.add(w)}return _})},d=()=>{const p=new Set;for(const{printer:f,calibrations:b}of e){if(!f.connected)continue;const w=b.filter(x=>fF(x,t)),g=new Map;for(const x of w){const _=`${x.extruder_id??"null"}`;g.has(_)||g.set(_,[]),g.get(_).push(x)}for(const[x,_]of g)if(_.length>0){const C=[..._].sort((P,T)=>T.k_value-P.k_value)[0];p.add(`${f.id}:${C.cali_idx}:${x}`)}}a(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:b})=>f.connected?p+b.filter(w=>fF(w,t)).length:p,0),m=(p,f)=>{const b=`${p.id}:${f.cali_idx}:${f.extruder_id??"null"}`,w=n.has(b);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(Dg,{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 b=i.has(String(p.id)),w=f.filter(C=>fF(C,t)),g=w.length,x=w.some(C=>C.extruder_id!==void 0&&C.extruder_id!==null&&C.extruder_id>0),_=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:[b?r.jsx(Ln,{className:"w-4 h-4 text-bambu-gray"}):r.jsx(Va,{className:"w-4 h-4 text-bambu-gray"}),r.jsx("span",{className:"font-medium text-white",children:p.name}),g>0?r.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:[g," ",o(g!==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")})]}),b&&r.jsx("div",{className:"px-4 py-3 space-y-3 bg-bambu-dark border-t border-bambu-dark-tertiary",children:p.connected?g===0?r.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.noKProfilesMatch")}):x?r.jsxs(r.Fragment,{children:[_.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:_.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)})}),n.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:n.size})," ",o("inventory.profilesSelected")]})})]})}function qit(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 $it={completed:"text-bambu-green",failed:"text-red-400",aborted:"text-yellow-400"};function Vit({spoolId:t}){const{t:e}=kt(),n=en(),{showToast:a}=rn(),{data:i,isLoading:s}=We({queryKey:["spool-usage",t],queryFn:()=>se.getSpoolUsageHistory(t)}),o=it({mutationFn:()=>se.clearSpoolUsageHistory(t),onSuccess:()=>{n.invalidateQueries({queryKey:["spool-usage",t]}),a(e("inventory.historyCleared"),"success")}});return s?r.jsx("div",{className:"flex justify-center py-4",children:r.jsx(ut,{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(Zn,{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(Me,{variant:"ghost",size:"sm",onClick:()=>o.mutate(),disabled:o.isPending,className:"text-xs text-bambu-gray hover:text-red-400",children:[r.jsx(Jt,{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:qit(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:$it[l.status]||"text-bambu-gray",children:l.status})]})]},l.id))})]})}const xQ={tag_uid:null,tray_uuid:null,tag_type:null,data_origin:null};function Git({isOpen:t,onClose:e,spool:n,mode:a,printersWithCalibrations:i=[],currencySymbol:s,onSpoolsCreated:o,spoolmanMode:l=!1,spoolsQueryKey:c=["inventory-spools"]}){const{t:d}=kt(),u=en(),{showToast:m}=rn(),p=a==="edit",f=a==="copy",[b,w]=v.useState(YI),[g,x]=v.useState({}),[_,N]=v.useState("filament"),[C,P]=v.useState(!1),[T,A]=v.useState(!1),[F,S]=v.useState(!1),[M,z]=v.useState(1),[I,H]=v.useState(!1),[R,Z]=v.useState(!1),[re,J]=v.useState([]),[de,me]=v.useState(""),[ue,W]=v.useState([]),[ae,pe]=v.useState([]),[ye,Te]=v.useState([]),[$,K]=v.useState([]),[D,j]=v.useState([]),[B,X]=v.useState([]),[O,ie]=v.useState(new Set),[U,G]=v.useState(new Set),Y=i.length>0?i:B,Q=O.size,{data:oe=[],isLoading:ee,error:ge}=We({queryKey:["spoolman-inventory-filaments"],queryFn:()=>se.getSpoolmanInventoryFilaments(),enabled:l&&t,staleTime:6e4,retry:!1});v.useEffect(()=>{j(Gce())},[]),v.useEffect(()=>{t&&((async()=>{Z(!0);try{const nt=await se.getCloudStatus();if(H(nt.is_authenticated),nt.is_authenticated){const gt=await se.getFilamentPresets();J(gt)}}catch(nt){console.error("Failed to fetch cloud presets:",nt),H(!1)}finally{Z(!1)}})(),l||se.getSpoolCatalog().then(W).catch(console.error),se.getColorCatalog().then(K).catch(console.error),se.getLocalPresets().then(nt=>pe(nt.filament)).catch(console.error),se.getBuiltinFilaments().then(Te).catch(console.error),i.length===0&&(async()=>{try{const nt=await se.getPrinters(),gt=await Promise.all(nt.map(ot=>se.getPrinterStatus(ot.id).catch(()=>null))),yt=[];for(let ot=0;ot({cali_idx:cn.slot_id,filament_id:cn.filament_id,setting_id:cn.setting_id||"",name:cn.name,k_value:parseFloat(cn.k_value)||0,n_coef:parseFloat(cn.n_coef)||0,extruder_id:cn.extruder_id,nozzle_diameter:cn.nozzle_diameter}))}catch{}yt.push({printer:{...Ht,connected:Zt},calibrations:Yt})}X(yt)}catch(nt){console.error("Failed to fetch printer calibrations:",nt)}})())},[t,i.length]);const ce=v.useMemo(()=>$ce(re,new Set,ae,ye),[re,ae,ye]),le=v.useMemo(()=>{const Be=qce(re,ae),nt=$.map(yt=>yt.manufacturer?.trim()).filter(yt=>!!yt),gt=new Set([...Be,...nt]);return Array.from(gt).sort((yt,ot)=>yt.localeCompare(ot))},[re,ae,$]),q=v.useMemo(()=>{const Be=$.map(gt=>gt.material?.trim()).filter(gt=>!!gt),nt=new Set([...Uce,...Be]);return Array.from(nt).sort((gt,yt)=>gt.localeCompare(yt))},[$]),L=v.useMemo(()=>{const Be=[];for(const nt of $){const gt=nt.manufacturer?.trim(),yt=nt.material?.trim();gt&&yt&&Be.push({brand:gt,material:yt})}for(const nt of re){const gt=Jh(nt.name);gt.brand&>.material&&Be.push({brand:gt.brand,material:gt.material})}for(const nt of ae){const gt=Jh(nt.name),yt=nt.filament_vendor?.trim()||gt.brand,ot=gt.material;yt&&ot&&Be.push({brand:yt,material:ot})}return Be},[re,$,ae]),ne=v.useMemo(()=>{const Be=new Map;for(const nt of L){const gt=nt.brand.toLowerCase(),yt=nt.material.toLowerCase();Be.has(gt)||Be.set(gt,new Set),Be.get(gt).add(yt)}return Be},[L]),xe=v.useMemo(()=>{const Be=new Map;for(const nt of L){const gt=nt.brand.toLowerCase(),yt=nt.material.toLowerCase();Be.has(yt)||Be.set(yt,new Set),Be.get(yt).add(gt)}return Be},[L]),we=v.useMemo(()=>{if(!b.material)return le;const Be=b.material.toLowerCase(),nt=xe.get(Be);return!nt||nt.size===0?le:le.filter(gt=>nt.has(gt.toLowerCase()))},[le,b.material,xe]),Ne=v.useMemo(()=>{if(!b.brand)return q;const Be=b.brand.toLowerCase(),nt=ne.get(Be);return!nt||nt.size===0?q:q.filter(gt=>nt.has(gt.toLowerCase()))},[q,b.brand,ne]),ve=v.useMemo(()=>Vce(b.slicer_filament,ce),[b.slicer_filament,ce]);v.useEffect(()=>{if(t){if(n){const Be=n.rgba&&/^[0-9A-Fa-f]{8}$/.test(n.rgba)?n.rgba:"808080FF";if(w({material:n.material||"",subtype:n.subtype||"",brand:n.brand||"",color_name:n.color_name_is_synthesized?"":n.color_name||"",rgba:Be,extra_colors:n.extra_colors||"",effect_type:n.effect_type||"",label_weight:n.label_weight||1e3,core_weight:n.core_weight||250,core_weight_catalog_id:n.core_weight_catalog_id??null,weight_used:f?0:n.weight_used||0,slicer_filament:n.slicer_filament||"",note:n.note||"",cost_per_kg:n.cost_per_kg??null,category:n.category||"",low_stock_threshold_pct:n.low_stock_threshold_pct??null,storage_location:n.storage_location||"",spoolman_filament_id:null}),me(n.slicer_filament_name||n.slicer_filament||""),n.k_profiles&&n.k_profiles.length>0){const nt=new Set;for(const gt of n.k_profiles)gt.cali_idx!==null&>.cali_idx!==void 0&&nt.add(`${gt.printer_id}:${gt.cali_idx}:${gt.extruder??"null"}`);ie(nt)}else ie(new Set)}else w(YI),me(""),ie(new Set),S(!1),z(1);x({}),N("filament"),P(!1),A(!1)}},[t,n,a,f]),v.useEffect(()=>{t&&Y.length>0&&G(new Set(Y.map(Be=>String(Be.printer.id))))},[t,Y]);const be=(Be,nt)=>{const gt=Fit.has(Be);l&>&&b.spoolman_filament_id!==null&&m(d("inventory.spoolmanFilamentUnlinked"),"info"),w(yt=>({...yt,[Be]:nt,...l&>&&yt.spoolman_filament_id!==null?{spoolman_filament_id:null}:{}})),Be==="weight_used"&&P(!0),Be==="storage_location"&&A(!0),g[Be]&&x(yt=>({...yt,[Be]:void 0}))},Ee=Be=>{const nt=Be.material||"",gt=Be.name||"",yt=nt&>.startsWith(nt)?gt.slice(nt.length).trim():gt,ot=(Be.color_hex??"").replace("#","").toUpperCase(),Ht=/^[0-9A-F]{6}$/.test(ot)?ot:"808080";w(Kt=>({...Kt,spoolman_filament_id:Be.id,material:nt,subtype:yt,brand:Be.vendor?.name||"",rgba:`${Ht}FF`,color_name:Be.color_name||"",label_weight:Be.weight??Kt.label_weight})),m(d("inventory.spoolmanFilamentSelected"),"success")},ze=Be=>{j(nt=>Wce(Be,nt))},_e=it({mutationFn:Be=>l?se.createSpoolmanInventorySpool(Be):se.createSpool(Be),onSuccess:async Be=>{Be?.id&&!await fe(Be.id)||(await u.invalidateQueries({queryKey:c}),o&&o([Be]),m(d("inventory.spoolCreated"),"success"),e())},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Oe=it({mutationFn:({data:Be,qty:nt})=>l?se.bulkCreateSpoolmanInventorySpools(Be,nt):se.bulkCreateSpools(Be,nt),onSuccess:async Be=>{const nt=l&&"created"in Be?Be:null,gt=nt?nt.created:Be;if(O.size>0)for(const yt of gt)await fe(yt.id);await u.invalidateQueries({queryKey:c}),o&&o(gt),nt&&nt.failed_count>0?m(d("inventory.spoolsPartiallyCreated",{created:gt.length,total:nt.requested_count}),"warning"):m(d("inventory.spoolsCreated",{count:gt.length}),"success"),e()},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Ie=it({mutationFn:Be=>l?se.updateSpoolmanInventorySpool(n.id,Be):se.updateSpool(n.id,Be),onSuccess:async()=>{n?.id&&!await fe(n.id)||(await u.invalidateQueries({queryKey:c}),m(d("inventory.spoolUpdated"),"success"),e())},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.saveFailed"),"error")}}),Ke=it({mutationFn:()=>l?se.updateSpoolmanInventorySpool(n.id,xQ):se.updateSpool(n.id,xQ),onSuccess:async()=>{await u.invalidateQueries({queryKey:c}),m(d("inventory.rfidCleared","RFID tag cleared"),"success"),e()},onError:Be=>{Be instanceof _i&&Be.status===503?m(d("inventory.spoolmanUnreachable"),"error"):m(d("inventory.tagClearFailed"),"error")}}),{data:rt}=We({queryKey:["spool-assignments"],queryFn:()=>se.getAssignments(),enabled:t&&p}),st=n?rt?.find(Be=>Be.spool_id===n.id):void 0,{data:Le}=We({queryKey:["inventory-spools"],queryFn:()=>se.getSpools(!0),enabled:t}),{data:tt}=We({queryKey:["settings"],queryFn:se.getSettings,enabled:t}),Ce=(()=>{const Be=new Set;for(const nt of Le??[]){const gt=nt.category?.trim();gt&&Be.add(gt)}return Array.from(Be).sort((nt,gt)=>nt.localeCompare(gt))})(),Xe=tt?.low_stock_threshold??20,Ue=it({mutationFn:()=>{if(!st)throw new Error("No assignment");return se.unassignSpool(st.printer_id,st.ams_id,st.tray_id)},onSuccess:async()=>{await u.invalidateQueries({queryKey:["spool-assignments"]}),m(d("inventory.unassignSuccess","Spool unassigned"),"success"),e()},onError:Be=>{m(Be.message,"error")}}),fe=async Be=>{const nt=l?se.saveSpoolmanKProfiles:se.saveSpoolKProfiles;if(O.size===0)try{return await nt(Be,[]),!0}catch(ot){return console.error("Failed to save K-profiles:",ot),m(d("inventory.kProfileSaveFailed"),"warning"),!1}const gt=[];let yt=0;for(const ot of O){const[Ht,Kt,Zt]=ot.split(":"),Yt=parseInt(Ht),gn=parseInt(Kt),cn=Zt==="null"?0:parseInt(Zt),pt=Y.find(at=>at.printer.id===Yt);if(pt){const at=pt.calibrations.find(vt=>vt.cali_idx===gn);at?gt.push({printer_id:Yt,extruder:cn,nozzle_diameter:at.nozzle_diameter||"0.4",k_value:at.k_value,name:at.name||null,cali_idx:at.cali_idx,setting_id:at.setting_id||null}):yt++}else yt++}if(yt>0)return console.error(`saveKProfiles: ${yt} profile key(s) could not be resolved`,Array.from(O)),m(d("inventory.kProfileSaveFailed"),"warning"),!1;if(gt.length>0)try{return await nt(Be,gt),!0}catch(ot){return console.error("Failed to save K-profiles:",ot),m(d("inventory.kProfileSaveFailed"),"warning"),!1}return!0};if(v.useEffect(()=>{if(!t)return;const Be=nt=>{nt.key==="Escape"&&e()};return document.addEventListener("keydown",Be),()=>document.removeEventListener("keydown",Be)},[t,e]),!t)return null;const Ae=()=>{const Be=zce(b,F,l);if(!Be.isValid){x(Be.errors),(Be.errors.slicer_filament||Be.errors.material||Be.errors.brand||Be.errors.subtype)&&N("filament");return}const nt=ve?.displayName||de||null,gt={material:b.material||null,subtype:b.subtype||null,brand:b.brand||null,color_name:b.color_name||null,rgba:b.rgba||null,extra_colors:b.extra_colors||null,effect_type:b.effect_type||null,label_weight:b.label_weight,...l?{}:{core_weight:b.core_weight,core_weight_catalog_id:b.core_weight_catalog_id},slicer_filament:b.slicer_filament||null,slicer_filament_name:nt,nozzle_temp_min:null,nozzle_temp_max:null,note:b.note||null,cost_per_kg:b.cost_per_kg,category:b.category.trim()||null,low_stock_threshold_pct:b.low_stock_threshold_pct,...l?{spoolman_filament_id:b.spoolman_filament_id}:{}};(!p||C)&&(gt.weight_used=b.weight_used),(!p||T)&&(gt.storage_location=b.storage_location||null),p?Ie.mutate(gt):M>1?Oe.mutate({data:gt,qty:M}):_e.mutate(gt)},$e=_e.isPending||Oe.isPending||Ie.isPending||Ke.isPending||Ue.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.jsx("h2",{className:"text-lg font-semibold text-white",children:d(p?"inventory.editSpool":f?"inventory.copySpool":"inventory.addSpool")}),r.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white rounded transition-colors",children:r.jsx(Bt,{className:"w-5 h-5"})})]}),a==="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:()=>{S(!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 ${_==="filament"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[r.jsx(jS,{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 ${_==="pa-profile"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[r.jsx(yfe,{className:"w-4 h-4"}),d("inventory.paProfileTab"),Q>0&&r.jsx("span",{className:"text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:Q})]})]}),r.jsx("div",{className:"p-4 overflow-y-auto flex-1",style:{scrollbarGutter:"stable"},children:_==="filament"?r.jsxs("div",{className:"space-y-6",children:[l&&!p&&r.jsx("div",{children:ge?r.jsx("p",{className:"text-sm text-red-400 px-1",children:d("inventory.spoolmanCatalogLoadFailed")}):r.jsx(Hit,{filaments:oe,isLoading:ee,selectedId:b.spoolman_filament_id,onSelect:Ee})}),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(Kce,{formData:b,updateField:be,cloudAuthenticated:I,loadingCloudPresets:R,presetInputValue:de,setPresetInputValue:me,selectedPresetOption:ve,filamentOptions:ce,availableBrands:we,availableMaterials:Ne,quickAdd:F,quantity:M,onQuantityChange:z,errors:g})]}),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(Yce,{formData:b,updateField:be,recentColors:D,onColorUsed:ze,catalogColors:$})]}),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(Xce,{formData:b,updateField:be,spoolCatalog:ue,currencySymbol:s,availableCategories:Ce,globalLowStockThreshold:Xe,spoolmanMode:l})]}),p&&n&&!l&&r.jsx("div",{children:r.jsx(Vit,{spoolId:n.id})})]}):r.jsx(Qce,{formData:b,updateField:be,printersWithCalibrations:Y,selectedProfiles:O,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(Me,{variant:"secondary",onClick:()=>Ke.mutate(),disabled:$e||!n?.tag_uid,children:[r.jsx($m,{className:"w-4 h-4"}),d("inventory.clearRfid","Clear RFID Tag")]}),r.jsxs(Me,{variant:"secondary",onClick:()=>Ue.mutate(),disabled:$e||!st,children:[r.jsx(iu,{className:"w-4 h-4"}),d("inventory.unassignSpool","Unassign")]})]}),r.jsxs("div",{className:"flex gap-2 ml-auto",children:[r.jsx(Me,{variant:"secondary",onClick:e,children:d("common.cancel")}),r.jsx(Me,{onClick:Ae,disabled:$e,children:$e?r.jsxs(r.Fragment,{children:[r.jsx(ut,{className:"w-4 h-4 animate-spin"}),d("common.saving")]}):r.jsxs(r.Fragment,{children:[r.jsx(us,{className:"w-4 h-4"}),d(p?"common.save":f?"inventory.copySpool":"inventory.addSpool")]})})]})]})]})]})}function Wit({isOpen:t,onClose:e,columns:n,defaultColumns:a,onSave:i}){const{t:s}=kt(),[o,l]=v.useState(n),[c,d]=v.useState(null),u=v.useRef(null);if(v.useEffect(()=>{t&&l(n.map(C=>({...C})))},[t,n]),v.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,A)=>A===C?{...T,visible:!T.visible}:T))},p=(C,P)=>{P<0||P>=o.length||l(T=>{const A=[...T],[F]=A.splice(C,1);return A.splice(P,0,F),A})},f=(C,P)=>{u.current=P,d(P),C.dataTransfer.effectAllowed="move"},b=(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()},g=()=>{u.current=null,d(null)},x=()=>{l(a.map(C=>({...C})))},_=()=>{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(Ct,{className:"w-full max-w-md max-h-[80vh] flex flex-col",onClick:C=>C.stopPropagation(),children:r.jsxs(At,{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=>b(T,P),onDrop:w,onDragEnd:g,children:[r.jsx("div",{className:"cursor-grab text-bambu-gray/50 hover:text-bambu-gray",children:r.jsx(Ph,{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(tl,{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(Ln,{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(Dl,{className:"w-4 h-4"}):r.jsx(sb,{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(Me,{variant:"secondary",onClick:x,className:"mr-auto",children:[r.jsx(gs,{className:"w-4 h-4"}),s("inventory.reset")]}),r.jsx(Me,{variant:"secondary",onClick:e,children:s("inventory.cancel")}),r.jsx(Me,{onClick:_,children:s("inventory.applyChanges")})]})]})})})}const Kit=[{value:"ams_30x15",i18nKey:"ams",fallbackLabel:"AMS holder (30 × 15 mm)",fallbackHint:"Single label per page; fits the popular AMS filament label holder."},{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 Yit(t){const e=window.URL.createObjectURL(t);if(!window.open(e,"_blank","noopener,noreferrer")){const a=document.createElement("a");a.href=e,a.download="bambuddy-labels.pdf",document.body.appendChild(a),a.click(),document.body.removeChild(a)}setTimeout(()=>window.URL.revokeObjectURL(e),6e4)}function Xit(t){if(!t)return{backgroundColor:"#808080"};const e=t.replace(/^#/,"").slice(0,6);return e.length===6?{backgroundColor:`#${e}`}:{backgroundColor:"#808080"}}function Qit(t){const e=t.color_name??`${t.material}${t.subtype?` ${t.subtype}`:""}`,n=t.brand?` · ${t.brand}`:"";return`${e}${n}`}function Zit(t){return[t.color_name,t.material,t.subtype,t.brand,`#${t.id}`].filter(Boolean).join(" ").toLowerCase()}function Jit({isOpen:t,onClose:e,availableSpools:n,initialSelectedIds:a,spoolmanMode:i}){const{t:s}=kt(),{showToast:o}=rn(),[l,c]=v.useState(null),[d,u]=v.useState(new Set),[m,p]=v.useState(""),[f,b]=v.useState("");v.useEffect(()=>{if(t){const M=new Set(n.map(z=>z.id));u(new Set(a.filter(z=>M.has(z)))),p(""),b(""),c(null)}},[t]);const w=v.useMemo(()=>[...n].sort((M,z)=>M.id-z.id),[n]),g=v.useMemo(()=>{const M=new Set;for(const z of w)z.material&&M.add(z.material.toUpperCase());return[...M].sort()},[w]),x=v.useMemo(()=>{const M=m.trim().toLowerCase();return w.filter(z=>!(f&&(z.material||"").toUpperCase()!==f||M&&!Zit(z).includes(M)))},[w,m,f]),_=x.length>0&&x.every(M=>d.has(M.id));if(!t)return null;const N=d.size,C=N===0;function P(M){u(z=>{const I=new Set(z);return I.has(M)?I.delete(M):I.add(M),I})}function T(){u(M=>{const z=new Set(M);for(const I of x)z.add(I.id);return z})}function A(){u(M=>{const z=new Set(M);for(const I of x)z.delete(I.id);return z})}function F(){u(new Set)}async function S(M){if(C||l)return;const z=[...d].sort((I,H)=>I-H);c(M);try{const I=i?await se.printSpoolmanSpoolLabels({spool_ids:z,template:M}):await se.printSpoolLabels({spool_ids:z,template:M});Yit(I),e()}catch(I){const H=I instanceof Error?I.message:String(I);o(s("inventory.labels.error","Could not generate labels: {{msg}}",{msg:H}),"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(pr,{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")}),N>0&&r.jsxs("span",{className:"text-sm text-bambu-gray",children:["(",s("inventory.labels.selectedCount","{{count}} selected",{count:N}),")"]})]}),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(Bt,{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(Sr,{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:M=>p(M.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"})]}),g.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:()=>b(""),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")}),g.map(M=>r.jsx("button",{type:"button",onClick:()=>b(M),className:`px-2 py-0.5 text-xs rounded-full border transition ${f===M?"bg-bambu-green text-bambu-dark border-bambu-green":"bg-bambu-dark text-bambu-gray border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:M},M))]})]}),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:_?A:T,disabled:x.length===0,className:"text-bambu-green hover:underline disabled:opacity-50 disabled:no-underline disabled:cursor-not-allowed",children:_?s("inventory.labels.deselectVisible","Deselect visible"):s("inventory.labels.selectVisible","Select all visible ({{count}})",{count:x.length})}),r.jsx("button",{type:"button",onClick:F,disabled:N===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:x.length===0?r.jsx("div",{className:"text-center text-sm text-bambu-gray py-6",children:w.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:x.map(M=>{const z=d.has(M.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:[z?r.jsx(bs,{className:"w-4 h-4 text-bambu-green shrink-0"}):r.jsx(so,{className:"w-4 h-4 text-bambu-gray shrink-0"}),r.jsx("input",{type:"checkbox",checked:z,onChange:()=>P(M.id),className:"sr-only"}),r.jsx("span",{className:"w-4 h-4 rounded border border-black/20 shrink-0",style:Xit(M.rgba)}),r.jsx("span",{className:"flex-1 min-w-0 truncate text-sm text-white",children:Qit(M)}),r.jsxs("span",{className:"text-xs font-mono text-bambu-gray shrink-0",children:["#",M.id]})]})},M.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:Kit.map(M=>{const z=l===M.value,I=s(`inventory.labels.templates.${M.i18nKey}.label`,M.fallbackLabel),H=s(`inventory.labels.templates.${M.i18nKey}.hint`,M.fallbackHint);return r.jsxs("button",{disabled:C||l!==null,onClick:()=>S(M.value),title:`${I} — ${H}`,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:I}),r.jsx("div",{className:"text-xs text-bambu-gray mt-0.5 truncate",children:H})]}),z&&r.jsx(ut,{className:"w-4 h-4 animate-spin text-bambu-green shrink-0"})]},M.value)})}),r.jsx("div",{className:"flex justify-end gap-2 px-5 py-2 border-t border-bambu-dark-tertiary",children:r.jsx(Me,{variant:"secondary",onClick:e,disabled:l!==null,children:s("common.cancel","Cancel")})})]})]})}function yQ(t){return`${t.material}|${t.subtype||""}|${t.brand||""}|${t.color_name||""}|${t.rgba||""}|${t.extra_colors||""}|${t.effect_type||""}|${t.label_weight}`}const Zce="bambuddy-inventory-columns",HN=[{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 est(){try{const t=localStorage.getItem(Zce);if(t){const e=JSON.parse(t),n=new Set(HN.map(o=>o.id)),a=new Set(e.map(o=>o.id)),i=e.filter(o=>n.has(o.id)),s=HN.filter(o=>!a.has(o.id));return[...i,...s]}}catch{}return HN.map(t=>({...t}))}function tst(t){try{localStorage.setItem(Zce,JSON.stringify(t))}catch{}}function au(t,e=!1){return e&&t>=1e3?`${(t/1e3).toFixed(1)}kg`:`${Math.round(t)}g`}const nst={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 gF(t,e="system"){if(!t)return"-";const n=Xn(t);return n?YF(n,e):"-"}const rst={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")},XI={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:gF(t.created_at,e)}),encode_time:({spool:t,dateFormat:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:gF(t.encode_time,e)}),last_used_time:({spool:t,dateFormat:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:t.last_used?gF(t.last_used,e):"Never"}),rgba:({spool:t})=>r.jsx("div",{className:"flex items-center justify-center",children:r.jsx(rU,{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:PT(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 n=e[t.id];if(!n)return r.jsx("span",{className:"text-sm text-bambu-gray",children:"-"});const a=n.printer_name||`Printer ${n.printer_id}`,i=n.ams_id===254||n.ams_id===255,s=!i&&n.ams_id>=128,o=c_(n.ams_id,n.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:[a," ",o,n.ams_label?` (${n.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:au(t.label_weight)}),net:({remaining:t})=>r.jsx("span",{className:"text-sm text-white",children:au(t)}),gross:({spool:t,remaining:e})=>r.jsx("span",{className:"text-sm text-bambu-gray",children:au(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?au(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 n=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:n?.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 n=t.last_scale_weight;if(n==null)return r.jsx("span",{className:"text-sm text-bambu-gray/50",title:"No scale measurement",children:"-"});const a=t.core_weight||0,i=Math.max(0,t.label_weight-t.weight_used)+a;let s,o;n0?`+${Math.round(s)}`:`${Math.round(s)}`,c=o?`Scale: ${Math.round(n)}g
Calculated: ${Math.round(i)}g
Difference: ${l}g (within tolerance)`:`Scale: ${Math.round(n)}g
Calculated: ${Math.round(i)}g
diff --git a/static/index.html b/static/index.html
index 759ae0dc0..1b65cdfec 100644
--- a/static/index.html
+++ b/static/index.html
@@ -26,7 +26,7 @@
-
+