import{i as e}from"./chunk-aKtaBQYM.js";import{C as t,D as n,a as r,v as i,w as a}from"./client-BdOyoZJz.js";import{t as o}from"./useQuery-CCy0EwsM.js";import{a as s}from"./info-DcT0QU8O.js";import{n as c}from"./ToastContext-VFOrCCoA.js";import{t as l}from"./sparkles-AUof3WTI.js";import{Hn as u,Mn as d,N as f,at as p,ft as m,lt as h,ot as g}from"./index-BoVbGTav.js";import{n as _,o as v,r as y,s as ee,t as b}from"./AdditionalSection-Bcoa_hnW.js";import{c as x,f as S,i as te,m as C,n as ne,o as w,r as T,s as re,t as E,u as D,v as O}from"./utils-D0ctH_jZ.js";import{n as k}from"./spoolPaint-CrvpHqdD.js";var A=e(n(),1),j=t();function ie({formData:e,printersWithCalibrations:t,selectedProfiles:n,setSelectedProfiles:r,expandedPrinters:i,setExpandedPrinters:o}){let{t:c}=a(),u=e=>{o(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},f=(e,t,n)=>{let i=`${e}:${t}:${n??`null`}`,a=`${e}:${n??`null`}`;r(e=>{let t=new Set(e);if(t.has(i))t.delete(i);else{for(let e of Array.from(t)){let n=e.split(`:`);`${n[0]}:${n[2]}`===a&&t.delete(e)}t.add(i)}return t})},p=()=>{let n=new Set;for(let{printer:r,calibrations:i}of t){if(!r.connected)continue;let t=i.filter(t=>w(t,e)),a=new Map;for(let e of t){let t=`${e.extruder_id??`null`}`;a.has(t)||a.set(t,[]),a.get(t).push(e)}for(let[e,t]of a)if(t.length>0){let i=[...t].sort((e,t)=>t.k_value-e.k_value)[0];n.add(`${r.id}:${i.cali_idx}:${e}`)}}r(n)};if(!e.material)return(0,j.jsx)(`div`,{className:`p-6 bg-bambu-dark rounded-lg text-center`,children:(0,j.jsx)(`p`,{className:`text-bambu-gray`,children:c(`inventory.selectMaterialFirst`)})});if(t.length===0)return(0,j.jsx)(`div`,{className:`p-6 bg-bambu-dark rounded-lg text-center`,children:(0,j.jsx)(`p`,{className:`text-bambu-gray`,children:c(`inventory.noPrintersConfigured`)})});let m=t.reduce((t,{printer:n,calibrations:r})=>n.connected?t+r.filter(t=>w(t,e)).length:t,0),h=(e,t)=>{let r=`${e.id}:${t.cali_idx}:${t.extruder_id??`null`}`,i=n.has(r);return(0,j.jsxs)(`label`,{className:`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-all border ${i?`bg-bambu-green/10 border-bambu-green/30`:`bg-bambu-dark border-transparent hover:bg-bambu-dark/80`}`,children:[(0,j.jsx)(`input`,{type:`checkbox`,checked:i,onChange:()=>f(String(e.id),t.cali_idx,t.extruder_id),className:`w-4 h-4 rounded border-bambu-dark-tertiary text-bambu-green focus:ring-bambu-green`}),(0,j.jsx)(`div`,{className:`flex-1 min-w-0`,children:(0,j.jsx)(`span`,{className:`text-sm font-medium ${i?`text-bambu-green`:`text-white`}`,children:t.name||t.filament_id})}),(0,j.jsxs)(`div`,{className:`flex items-center gap-2 shrink-0`,children:[t.nozzle_diameter&&(0,j.jsxs)(`span`,{className:`text-xs font-mono px-2 py-0.5 rounded bg-bambu-dark text-bambu-gray`,children:[t.nozzle_diameter,`mm`]}),(0,j.jsxs)(`span`,{className:`text-xs font-mono px-2 py-0.5 rounded bg-bambu-dark text-bambu-gray`,children:[`K=`,t.k_value.toFixed(3)]})]})]},`${t.cali_idx}-${t.extruder_id}`)};return(0,j.jsxs)(`div`,{className:`space-y-4`,children:[(0,j.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,j.jsxs)(`p`,{className:`text-xs text-bambu-gray`,children:[c(`inventory.matchingFilter`),`: `,e.brand||c(`inventory.anyBrand`),` / `,e.material,` / `,e.subtype||c(`inventory.anyVariant`)]}),m>0&&(0,j.jsxs)(`button`,{type:`button`,onClick:p,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:[(0,j.jsx)(l,{className:`w-3.5 h-3.5`}),c(`inventory.autoSelect`),` (`,m,`)`]})]}),(0,j.jsx)(`div`,{className:`space-y-3`,children:t.map(({printer:t,calibrations:n})=>{let r=i.has(String(t.id)),a=n.filter(t=>w(t,e)),o=a.length,l=a.some(e=>e.extruder_id!==void 0&&e.extruder_id!==null&&e.extruder_id>0),f=a.filter(e=>e.extruder_id===1),p=a.filter(e=>e.extruder_id===0||e.extruder_id===void 0||e.extruder_id===null);return(0,j.jsxs)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:[(0,j.jsxs)(`button`,{type:`button`,onClick:()=>u(String(t.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:[(0,j.jsxs)(`div`,{className:`flex items-center gap-3`,children:[r?(0,j.jsx)(s,{className:`w-4 h-4 text-bambu-gray`}):(0,j.jsx)(d,{className:`w-4 h-4 text-bambu-gray`}),(0,j.jsx)(`span`,{className:`font-medium text-white`,children:t.name}),o>0?(0,j.jsxs)(`span`,{className:`text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green`,children:[o,` `,c(o===1?`inventory.match`:`inventory.matches`)]}):(0,j.jsx)(`span`,{className:`text-xs px-2 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray`,children:c(`inventory.noMatches`)})]}),(0,j.jsx)(`span`,{className:`text-xs px-2 py-1 rounded-full ${t.connected?`bg-green-500/20 text-green-500`:`bg-bambu-gray/20 text-bambu-gray`}`,children:t.connected?c(`inventory.connected`):c(`inventory.offline`)})]}),r&&(0,j.jsx)(`div`,{className:`px-4 py-3 space-y-3 bg-bambu-dark border-t border-bambu-dark-tertiary`,children:t.connected?o===0?(0,j.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-2`,children:c(`inventory.noKProfilesMatch`)}):l?(0,j.jsxs)(j.Fragment,{children:[f.length>0&&(0,j.jsxs)(`div`,{className:`space-y-2`,children:[(0,j.jsx)(`p`,{className:`text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.leftNozzle`)}),(0,j.jsx)(`div`,{className:`space-y-2`,children:f.map(e=>h(t,e))})]}),p.length>0&&(0,j.jsxs)(`div`,{className:`space-y-2`,children:[(0,j.jsx)(`p`,{className:`text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:c(`inventory.rightNozzle`)}),(0,j.jsx)(`div`,{className:`space-y-2`,children:p.map(e=>h(t,e))})]})]}):(0,j.jsx)(`div`,{className:`space-y-2`,children:a.map(e=>h(t,e))}):(0,j.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-2`,children:c(`inventory.printerOffline`)})})]},t.id)})}),n.size>0&&(0,j.jsx)(`div`,{className:`p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg`,children:(0,j.jsxs)(`p`,{className:`text-sm text-white`,children:[(0,j.jsx)(`span`,{className:`font-semibold`,children:n.size}),` `,c(`inventory.profilesSelected`)]})})]})}function M(e){return m(e.color_name,e.rgba,e.color_name_is_synthesized)}var N=[`PLA`,`PETG`,`ABS`,`ASA`,`TPU`,`PA`,`PC`,`PVA`,`HIPS`];function P(){let{t:e}=a(),t=g(),{showToast:n}=c(),{sbState:s}=u(),[l,d]=(0,A.useState)(`existing`),[f,m]=(0,A.useState)(null),[h,_]=(0,A.useState)(``),[v,y]=(0,A.useState)(`idle`),[ee,b]=(0,A.useState)(``),[x,S]=(0,A.useState)(!1),[te,C]=(0,A.useState)(!1),[ne,w]=(0,A.useState)(null),{data:T}=o({queryKey:[`spoolman-settings`],queryFn:r.getSpoolmanSettings,staleTime:300*1e3}),re=T!==void 0,E=T?.spoolman_enabled===`true`&&!!T?.spoolman_url,{data:D=[],refetch:O}=o({queryKey:[E?`spoolman-inventory-spools`:`inventory-spools`,`write-tag`],queryFn:()=>E?r.getSpoolmanInventorySpools(!1):r.getSpools(!1),refetchInterval:1e4,enabled:re}),{data:k=[]}=o({queryKey:[`spoolbuddy-devices`],queryFn:()=>i.getDevices(),refetchInterval:5e3}),{data:ie}=o({queryKey:[`settings`],queryFn:r.getSettings}),N=k[0],P=s.deviceOnline,R=p(ie?.currency||`USD`),z=(0,A.useMemo)(()=>{let e;if(l===`existing`)e=D.filter(e=>!e.tag_uid&&!e.archived_at);else if(l===`replace`)e=D.filter(e=>(e.tag_uid||e.tray_uuid)&&!e.archived_at);else return[];if(h){let t=h.toLowerCase();e=e.filter(e=>e.material?.toLowerCase().includes(t)||M(e)?.toLowerCase().includes(t)||e.color_name?.toLowerCase().includes(t)||e.brand?.toLowerCase().includes(t)||e.subtype?.toLowerCase().includes(t))}return e},[D,l,h,t]),B=(0,A.useCallback)(e=>{let t=e.detail;(t.sak??t.data?.sak)===0&&(C(!0),w(t.tag_uid??t.data?.tag_uid??null))},[]),V=(0,A.useCallback)(e=>{let t=e.detail;C(!0),w(t.tag_uid??t.data?.tag_uid??null)},[]),H=(0,A.useCallback)(()=>{C(!1),w(null)},[]),U=(0,A.useCallback)(t=>{let n=t.detail;(n.spool_id===f?.id||n.data?.spool_id===f?.id)&&(y(`success`),b(e(`spoolbuddy.writeTag.writeSuccess`,`Tag written successfully!`)),O(),setTimeout(()=>{y(`idle`),m(null),b(``)},5e3))},[f,e,O]),W=(0,A.useCallback)(t=>{let n=t.detail;(n.spool_id===f?.id||n.data?.spool_id===f?.id)&&(y(`error`),b(n.message??n.data?.message??e(`spoolbuddy.writeTag.writeFailed`,`Write failed`)))},[f,e]);(0,A.useEffect)(()=>(window.addEventListener(`spoolbuddy-unknown-tag`,B),window.addEventListener(`spoolbuddy-tag-matched`,V),window.addEventListener(`spoolbuddy-tag-removed`,H),window.addEventListener(`spoolbuddy-tag-written`,U),window.addEventListener(`spoolbuddy-tag-write-failed`,W),()=>{window.removeEventListener(`spoolbuddy-unknown-tag`,B),window.removeEventListener(`spoolbuddy-tag-matched`,V),window.removeEventListener(`spoolbuddy-tag-removed`,H),window.removeEventListener(`spoolbuddy-tag-written`,U),window.removeEventListener(`spoolbuddy-tag-write-failed`,W)}),[B,V,H,U,W]),(0,A.useEffect)(()=>{m(null),y(`idle`),b(``),_(``)},[l]);let G=async()=>{if(!(!f||!N)){y(`writing`),b(e(`spoolbuddy.writeTag.waiting`,`Waiting for SpoolBuddy...`));try{let e=await i.writeTag(N.device_id,f.id);if(e?.warnings?.length)for(let t of e.warnings)n(t,`warning`)}catch{y(`error`),b(e(`spoolbuddy.writeTag.queueFailed`,`Failed to queue write command`))}}},oe=async()=>{if(N){try{await i.cancelWrite(N.device_id)}catch{}y(`idle`),b(``)}},K=async()=>{if(!(!f||!F(f))){S(!0),y(`idle`),b(``);try{E?await r.linkTagToSpoolmanSpool(f.id,{tag_uid:``,tray_uuid:``}):await r.linkTagToSpool(f.id,{tag_uid:``,tray_uuid:``,data_origin:`manual`}),await O(),m(null),y(`success`),b(e(`spoolbuddy.writeTag.untagSuccess`,`Tag removed from spool`)),setTimeout(()=>{y(`idle`),b(``)},2500)}catch{y(`error`),b(e(`spoolbuddy.writeTag.untagFailed`,`Failed to remove tag from spool`))}finally{S(!1)}}},q=(0,A.useCallback)(e=>{m(e),y(`idle`),b(``),O()},[O]),se=f&&P&&v!==`writing`&&v!==`success`;return(0,j.jsxs)(`div`,{className:`flex flex-col h-full`,children:[(0,j.jsx)(`div`,{className:`flex border-b border-bambu-dark-tertiary shrink-0`,children:[{key:`existing`,label:e(`spoolbuddy.writeTag.tabExisting`,`Existing Spool`)},{key:`new`,label:e(`spoolbuddy.writeTag.tabNew`,`New Spool`)},{key:`replace`,label:e(`spoolbuddy.writeTag.tabReplace`,`Replace Tag`)}].map(e=>(0,j.jsx)(`button`,{onClick:()=>d(e.key),className:`flex-1 py-3 text-sm font-medium transition-colors ${l===e.key?`text-bambu-green border-b-2 border-bambu-green bg-bambu-dark`:`text-zinc-400 hover:text-zinc-200 hover:bg-bambu-dark-tertiary`}`,children:e.label},e.key))}),(0,j.jsxs)(`div`,{className:`flex flex-1 overflow-hidden`,children:[(0,j.jsx)(`div`,{className:`flex-1 flex flex-col overflow-hidden border-r border-bambu-dark-tertiary`,children:l===`new`?(0,j.jsx)(L,{currencySymbol:R,onCreated:q,selectedSpool:f,spoolmanMode:E,t:e}):(0,j.jsxs)(j.Fragment,{children:[(0,j.jsx)(`div`,{className:`p-3 shrink-0`,children:(0,j.jsx)(`input`,{type:`text`,value:h,onChange:e=>_(e.target.value),placeholder:e(`spoolbuddy.writeTag.searchPlaceholder`,`Search by material, color, brand...`),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-sm text-white placeholder-zinc-500 focus:outline-none focus:border-bambu-green`})}),(0,j.jsx)(`div`,{className:`flex-1 overflow-y-auto px-3 pb-3 space-y-2`,children:z.length===0?(0,j.jsx)(`div`,{className:`text-center text-zinc-500 py-8 text-sm`,children:l===`existing`?e(`spoolbuddy.writeTag.noUntaggedSpools`,`No spools without tags`):e(`spoolbuddy.writeTag.noTaggedSpools`,`No spools with tags`)}):z.map(e=>(0,j.jsx)(I,{spool:e,selected:f?.id===e.id,showTag:l===`replace`,onClick:()=>{m(e),y(`idle`),b(``)}},e.id))})]})}),(0,j.jsx)(`div`,{className:`w-[340px] flex flex-col items-center justify-center p-6 shrink-0`,children:(0,j.jsx)(ae,{writeStatus:v,writeMessage:ee,selectedSpool:f,tagOnReader:te,tagUid:ne,deviceOnline:P,canWrite:!!se,isReplace:l===`replace`,canUntag:l===`replace`&&!!f&&F(f),untagging:x,onWrite:G,onUntag:K,onCancel:oe,onRetry:()=>{y(`idle`),b(``)},t:e})})]})]})}function F(e){return!!(e.tag_uid||e.tray_uuid)}function I({spool:e,selected:t,showTag:n,onClick:r}){let i=Math.max(0,e.label_weight-e.weight_used),a=e.label_weight>0?Math.round(i/e.label_weight*100):0;return(0,j.jsxs)(`button`,{onClick:r,className:`w-full flex items-center gap-3 p-3 rounded-lg text-left transition-colors ${t?`bg-bambu-green/15 border border-bambu-green/50`:`bg-bambu-dark-secondary hover:bg-bambu-dark-tertiary border border-transparent`}`,children:[(0,j.jsx)(`div`,{className:`w-8 h-8 rounded-full shrink-0 border border-white/10`,style:k(e)??(e.rgba?h(e.rgba):{backgroundColor:`#666`})}),(0,j.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,j.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,j.jsxs)(`span`,{className:`text-sm font-medium text-white truncate`,children:[e.brand?`${e.brand} `:``,e.material,e.subtype?` ${e.subtype}`:``]}),(0,j.jsxs)(`span`,{className:`text-[10px] font-mono text-zinc-500 shrink-0`,children:[`#`,e.id]})]}),(0,j.jsxs)(`div`,{className:`flex items-center gap-2 text-xs text-zinc-400`,children:[M(e)&&(0,j.jsx)(`span`,{children:M(e)}),(0,j.jsxs)(`span`,{children:[i,`g / `,e.label_weight,`g (`,a,`%)`]})]}),n&&e.tag_uid&&(0,j.jsx)(`div`,{className:`text-xs text-zinc-500 mt-0.5 font-mono`,children:e.tag_uid})]}),t&&(0,j.jsx)(`svg`,{className:`w-5 h-5 text-bambu-green shrink-0`,fill:`currentColor`,viewBox:`0 0 20 20`,children:(0,j.jsx)(`path`,{fillRule:`evenodd`,d:`M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z`,clipRule:`evenodd`})})]})}function L({currencySymbol:e,onCreated:t,selectedSpool:n,spoolmanMode:i,t:a}){let{data:s=[]}=o({queryKey:[i?`spoolman-inventory-spools`:`inventory-spools`,`write-tag-form`],queryFn:()=>i?r.getSpoolmanInventorySpools(!0):r.getSpools(!0)}),{data:c}=o({queryKey:[`settings`],queryFn:r.getSettings}),[l,u]=(0,A.useState)(`simple`),[d,p]=(0,A.useState)(`filament`),[m,g]=(0,A.useState)(v),[w,P]=(0,A.useState)(!1),[F,I]=(0,A.useState)({}),[L,ae]=(0,A.useState)(!1),[R,z]=(0,A.useState)(1),[B,V]=(0,A.useState)(!1),[H,U]=(0,A.useState)(null),[W,G]=(0,A.useState)(!1),[oe,K]=(0,A.useState)(!1),[q,se]=(0,A.useState)([]),[J,ce]=(0,A.useState)([]),[le,ue]=(0,A.useState)([]),[de,fe]=(0,A.useState)([]),[Y,pe]=(0,A.useState)([]),[me,he]=(0,A.useState)(``),[ge,_e]=(0,A.useState)([]),[X,ve]=(0,A.useState)([]),[Z,ye]=(0,A.useState)(new Set),[be,xe]=(0,A.useState)(new Set);(0,A.useEffect)(()=>{_e(re())},[]),(0,A.useEffect)(()=>{let e=!1;return(async()=>{if(l===`full`){K(!0);try{let[t,n]=await Promise.allSettled([(async()=>(await r.getCloudStatus()).is_authenticated?{connected:!0,presets:await r.getFilamentPresets()}:{connected:!1,presets:[]})(),(async()=>(await r.orcaCloudStatus()).connected?{connected:!0,presets:(await r.orcaCloudListProfiles()).filament}:{connected:!1,presets:[]})()]);if(e)return;let i=t.status===`fulfilled`&&t.value.connected,a=n.status===`fulfilled`&&n.value.connected,o=t.status===`fulfilled`?t.value.presets:[],s=n.status===`fulfilled`?n.value.presets:[];G(i||a),se([...o,...s])}catch{e||G(!1)}finally{e||K(!1)}r.getSpoolCatalog().then(t=>{e||fe(t)}).catch(()=>void 0),r.getColorCatalog().then(t=>{e||pe(t)}).catch(()=>void 0),r.getLocalPresets().then(t=>{e||ce(t.filament)}).catch(()=>void 0),r.getBuiltinFilaments().then(t=>{e||ue(t)}).catch(()=>void 0);try{let t=await r.getPrinters(),n=await Promise.all(t.map(e=>r.getPrinterStatus(e.id).catch(()=>null))),i=[];for(let e=0;e{e=!0}},[l]),(0,A.useEffect)(()=>{X.length>0&&xe(new Set(X.map(e=>String(e.printer.id))))},[X]);let Se=(0,A.useMemo)(()=>E(q,new Set,J,le),[q,J,le]),Ce=(0,A.useMemo)(()=>te(m.slicer_filament,Se),[m.slicer_filament,Se]),we=(0,A.useMemo)(()=>{let e=ne(q,J),t=Y.map(e=>e.manufacturer?.trim()).filter(e=>!!e);return Array.from(new Set([...e,...t])).sort((e,t)=>e.localeCompare(t))},[q,J,Y]),Te=(0,A.useMemo)(()=>{let e=Y.map(e=>e.material?.trim()).filter(e=>!!e);return Array.from(new Set([...O,...e])).sort((e,t)=>e.localeCompare(t))},[Y]),Q=(0,A.useMemo)(()=>{let e=[];for(let t of Y){let n=t.manufacturer?.trim(),r=t.material?.trim();n&&r&&e.push({brand:n,material:r})}for(let t of q){let n=D(t.name);n.brand&&n.material&&e.push({brand:n.brand,material:n.material})}for(let t of J){let n=D(t.name),r=t.filament_vendor?.trim()||n.brand,i=n.material;r&&i&&e.push({brand:r,material:i})}return e},[q,Y,J]),Ee=(0,A.useMemo)(()=>{let e=new Map;for(let t of Q){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(n)||e.set(n,new Set),e.get(n).add(r)}return e},[Q]),De=(0,A.useMemo)(()=>{let e=new Map;for(let t of Q){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(r)||e.set(r,new Set),e.get(r).add(n)}return e},[Q]),Oe=(0,A.useMemo)(()=>C(we,m.brand),[we,m.brand]),ke=(0,A.useMemo)(()=>C(Te,m.material),[Te,m.material]),Ae=(0,A.useMemo)(()=>x(Oe,m.material,De),[Oe,m.material,De]),je=(0,A.useMemo)(()=>x(ke,m.brand,Ee),[ke,m.brand,Ee]),$=(e,t)=>{g(n=>({...n,[e]:t})),e===`core_weight`&&P(!0),F[e]&&I(t=>({...t,[e]:void 0}))},Me=e=>{_e(t=>S(e,t))},Ne=async e=>{let t=i?r.saveSpoolmanKProfiles:r.saveSpoolKProfiles;if(Z.size===0){try{await t(e,[])}catch{}return}let n=[];for(let e of Z){let[t,r,i]=e.split(`:`),a=parseInt(t),o=parseInt(r),s=i===`null`?0:parseInt(i),c=X.find(e=>e.printer.id===a);if(c){let e=c.calibrations.find(e=>e.cali_idx===o&&(e.extruder_id??0)===s);e&&n.push({printer_id:a,extruder:s,nozzle_diameter:e.nozzle_diameter||`0.4`,k_value:e.k_value,name:e.name||null,cali_idx:e.cali_idx,setting_id:e.setting_id||null})}}n.length>0&&await t(e,n)},Pe=async()=>{U(null);let e=ee(m,l===`simple`?!0:L);if(!e.isValid){I(e.errors),p(`filament`);return}let n=Ce?.displayName||me||null,o={material:m.material,subtype:m.subtype||null,brand:m.brand||null,color_name:m.color_name||null,rgba:m.rgba||null,extra_colors:m.extra_colors||null,effect_type:m.effect_type||null,label_weight:m.label_weight,...i?w?{core_weight:m.core_weight}:{}:{core_weight:m.core_weight,core_weight_catalog_id:m.core_weight_catalog_id},weight_used:m.weight_used,slicer_filament:m.slicer_filament||null,slicer_filament_name:n,nozzle_temp_min:null,nozzle_temp_max:null,note:m.note||null,cost_per_kg:m.cost_per_kg,added_full:null,last_used:null,encode_time:null,tag_uid:null,tray_uuid:null,data_origin:null,tag_type:null,last_scale_weight:null,last_weighed_at:null,category:m.category.trim()||null,low_stock_threshold_pct:m.low_stock_threshold_pct,material_number:m.material_number.trim()||null};V(!0);try{if(R>1){let e=i?await r.bulkCreateSpoolmanInventorySpools(o,R):await r.bulkCreateSpools(o,R),n=i&&e&&typeof e==`object`&&`created`in e?e.created:e;for(let e of n)await Ne(e.id);n.length>0&&t(n[0])}else{let e=i?await r.createSpoolmanInventorySpool(o):await r.createSpool(o);await Ne(e.id),t(e)}}catch{U(a(`spoolbuddy.writeTag.createFailed`,`Failed to create spool`))}finally{V(!1)}},Fe=`#${(m.rgba||`808080FF`).slice(0,6)}`;return(0,j.jsxs)(`div`,{className:`p-3 space-y-3 overflow-y-auto h-full`,children:[(0,j.jsxs)(`div`,{className:`flex items-center justify-between px-2 py-2 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary`,children:[(0,j.jsx)(`span`,{className:`text-sm text-zinc-200`,children:a(`spoolbuddy.writeTag.viewMode`,`View`)}),(0,j.jsxs)(`div`,{className:`flex rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,j.jsx)(`button`,{type:`button`,onClick:()=>u(`simple`),className:`px-3 py-1.5 text-xs font-medium ${l===`simple`?`bg-bambu-green/20 text-bambu-green`:`bg-bambu-dark text-zinc-400`}`,children:a(`spoolbuddy.writeTag.simpleView`,`Simple`)}),(0,j.jsx)(`button`,{type:`button`,onClick:()=>u(`full`),className:`px-3 py-1.5 text-xs font-medium ${l===`full`?`bg-bambu-green/20 text-bambu-green`:`bg-bambu-dark text-zinc-400`}`,children:a(`spoolbuddy.writeTag.fullView`,`Full`)})]})]}),l===`simple`?n?(0,j.jsxs)(`div`,{className:`flex flex-col items-center justify-center h-full p-6 text-center bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg`,children:[(0,j.jsx)(`div`,{className:`w-12 h-12 rounded-full mb-4 border border-white/10`,style:k(n,`preview`)??(n.rgba?h(n.rgba):{backgroundColor:`#666`})}),(0,j.jsxs)(`p`,{className:`text-white font-medium`,children:[n.brand?`${n.brand} `:``,n.material]}),M(n)&&(0,j.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:M(n)}),(0,j.jsxs)(`p`,{className:`text-zinc-500 text-xs mt-1`,children:[n.label_weight,`g`]}),(0,j.jsx)(`p`,{className:`text-bambu-green text-sm mt-4`,children:a(`spoolbuddy.writeTag.spoolCreated`,`Spool created! Ready to write.`)})]}):(0,j.jsxs)(`div`,{className:`p-4 space-y-4 overflow-y-auto bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg`,children:[(0,j.jsxs)(`div`,{children:[(0,j.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.material`,`Material`)}),(0,j.jsx)(`select`,{value:m.material,onChange:e=>$(`material`,e.target.value),className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`,children:N.map(e=>(0,j.jsx)(`option`,{value:e,children:e},e))})]}),(0,j.jsxs)(`div`,{className:`flex gap-3`,children:[(0,j.jsxs)(`div`,{className:`flex-1`,children:[(0,j.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.colorName`,`Color Name`)}),(0,j.jsx)(`input`,{type:`text`,value:m.color_name,onChange:e=>$(`color_name`,e.target.value),placeholder:`Jade White`,className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-sm text-white placeholder-zinc-500 focus:outline-none focus:border-bambu-green`})]}),(0,j.jsxs)(`div`,{children:[(0,j.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.color`,`Color`)}),(0,j.jsx)(`input`,{type:`color`,value:Fe,onChange:e=>$(`rgba`,e.target.value.replace(`#`,``).toUpperCase()+`FF`),className:`w-10 h-9 bg-transparent border border-bambu-dark-tertiary rounded cursor-pointer`})]})]}),(0,j.jsxs)(`div`,{children:[(0,j.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.brand`,`Brand`)}),(0,j.jsx)(`input`,{type:`text`,value:m.brand,onChange:e=>$(`brand`,e.target.value),placeholder:`Polymaker`,className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-sm text-white placeholder-zinc-500 focus:outline-none focus:border-bambu-green`})]}),(0,j.jsxs)(`div`,{children:[(0,j.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.weight`,`Weight (g)`)}),(0,j.jsx)(f,{value:m.label_weight,onChange:e=>$(`label_weight`,e),fallback:0,min:0,max:1e4,className:`w-full px-3 py-2 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-sm text-white focus:outline-none focus:border-bambu-green`})]}),(0,j.jsx)(`button`,{onClick:Pe,disabled:B||!m.material,className:`w-full py-2.5 bg-bambu-green hover:bg-bambu-green/80 disabled:opacity-50 disabled:cursor-not-allowed text-white text-sm font-medium rounded transition-colors`,children:B?a(`spoolbuddy.writeTag.creating`,`Creating...`):a(`spoolbuddy.writeTag.createSpool`,`Create Spool`)})]}):(0,j.jsxs)(j.Fragment,{children:[(0,j.jsxs)(`div`,{className:`flex items-center justify-between px-2 py-2 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary`,children:[(0,j.jsx)(`span`,{className:`text-sm text-zinc-200`,children:a(`inventory.quickAdd`,`Quick Add`)}),(0,j.jsx)(`button`,{type:`button`,onClick:()=>ae(e=>!e),className:`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${L?`bg-bambu-green`:`bg-bambu-dark-tertiary`}`,children:(0,j.jsx)(`span`,{className:`inline-block h-4.5 w-4.5 rounded-full bg-white transition-transform ${L?`translate-x-6`:`translate-x-1`}`})})]}),(0,j.jsxs)(`div`,{className:`flex border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:[(0,j.jsx)(`button`,{onClick:()=>p(`filament`),className:`flex-1 py-2.5 text-sm font-medium ${d===`filament`?`bg-bambu-green/15 text-bambu-green`:`bg-bambu-dark-secondary text-zinc-400`}`,children:a(`inventory.filamentInfoTab`,`Filament`)}),!L&&(0,j.jsx)(`button`,{onClick:()=>p(`pa-profile`),className:`flex-1 py-2.5 text-sm font-medium ${d===`pa-profile`?`bg-bambu-green/15 text-bambu-green`:`bg-bambu-dark-secondary text-zinc-400`}`,children:a(`inventory.paProfileTab`,`PA Profile`)})]}),(0,j.jsx)(`div`,{className:`bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg p-3`,children:d===`filament`?(0,j.jsxs)(`div`,{className:`space-y-4`,children:[(0,j.jsx)(y,{formData:m,updateField:$,cloudAuthenticated:W,loadingCloudPresets:oe,presetInputValue:me,setPresetInputValue:he,selectedPresetOption:Ce,filamentOptions:Se,availableBrands:Oe,availableMaterials:ke,suggestedBrands:Ae,suggestedMaterials:je,quickAdd:L,detailsRequired:!L,quantity:R,onQuantityChange:z,errors:F}),(0,j.jsx)(_,{formData:m,updateField:$,recentColors:ge,onColorUsed:Me,catalogColors:Y}),(0,j.jsx)(b,{formData:m,updateField:$,spoolCatalog:de,currencySymbol:e,availableCategories:Array.from(new Set(s.map(e=>e.category?.trim()).filter(e=>!!e))).sort((e,t)=>e.localeCompare(t)),availableMaterialNumbers:Array.from(new Set(s.map(e=>e.material_number?.trim()).filter(e=>!!e))).sort((e,t)=>e.localeCompare(t,void 0,{numeric:!0})),globalLowStockThreshold:c?.low_stock_threshold??20,spoolmanMode:i})]}):(0,j.jsx)(ie,{formData:m,updateField:$,printersWithCalibrations:X,selectedProfiles:Z,setSelectedProfiles:ye,expandedPrinters:be,setExpandedPrinters:xe})})]}),H&&(0,j.jsx)(`div`,{className:`text-sm text-red-400 bg-red-900/20 border border-red-900/40 rounded-lg px-3 py-2`,children:H}),l===`full`&&(0,j.jsx)(`button`,{onClick:Pe,disabled:B,className:`w-full py-3 bg-bambu-green hover:bg-bambu-green/80 disabled:opacity-50 disabled:cursor-not-allowed text-white text-sm font-medium rounded transition-colors`,children:B?a(`spoolbuddy.writeTag.creating`,`Creating...`):a(`spoolbuddy.writeTag.createSpool`,`Create Spool`)}),l===`full`&&n&&(0,j.jsxs)(`div`,{className:`flex flex-col items-center justify-center p-4 text-center bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg`,children:[(0,j.jsx)(`div`,{className:`w-12 h-12 rounded-full mb-4 border border-white/10`,style:k(n,`preview`)??(n.rgba?h(n.rgba):{backgroundColor:`#666`})}),(0,j.jsxs)(`p`,{className:`text-white font-medium`,children:[n.brand?`${n.brand} `:``,n.material]}),M(n)&&(0,j.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:M(n)}),(0,j.jsxs)(`p`,{className:`text-zinc-500 text-xs mt-1`,children:[n.label_weight,`g`]}),(0,j.jsx)(`p`,{className:`text-bambu-green text-sm mt-4`,children:a(`spoolbuddy.writeTag.spoolCreated`,`Spool created! Ready to write.`)})]})]})}function ae({writeStatus:e,writeMessage:t,selectedSpool:n,tagOnReader:r,tagUid:i,deviceOnline:a,canWrite:o,isReplace:s,canUntag:c,untagging:l,onWrite:u,onUntag:d,onCancel:f,onRetry:p,t:m}){if(e===`success`)return(0,j.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4`,children:[(0,j.jsx)(`div`,{className:`w-16 h-16 rounded-full bg-green-500/20 flex items-center justify-center`,children:(0,j.jsx)(`svg`,{className:`w-8 h-8 text-green-400`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,j.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:2,d:`M5 13l4 4L19 7`})})}),(0,j.jsx)(`p`,{className:`text-green-400 font-medium`,children:t}),n&&(0,j.jsxs)(`p`,{className:`text-zinc-400 text-sm`,children:[n.brand?`${n.brand} `:``,n.material,n.subtype?` ${n.subtype}`:``,M(n)?` - ${M(n)}`:``]})]});if(e===`error`)return(0,j.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4`,children:[(0,j.jsx)(`div`,{className:`w-16 h-16 rounded-full bg-red-500/20 flex items-center justify-center`,children:(0,j.jsx)(`svg`,{className:`w-8 h-8 text-red-400`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,j.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:2,d:`M6 18L18 6M6 6l12 12`})})}),(0,j.jsx)(`p`,{className:`text-red-400 font-medium`,children:t}),(0,j.jsx)(`button`,{onClick:p,className:`px-4 py-2 bg-bambu-dark-tertiary hover:bg-bambu-dark-secondary text-white text-sm rounded transition-colors`,children:m(`spoolbuddy.writeTag.tryAgain`,`Try Again`)})]});if(e===`writing`)return(0,j.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4`,children:[(0,j.jsxs)(`div`,{className:`relative w-16 h-16`,children:[(0,j.jsx)(`div`,{className:`absolute inset-0 rounded-full border-2 border-bambu-green/30 animate-ping`}),(0,j.jsx)(`div`,{className:`absolute inset-2 rounded-full border-2 border-bambu-green/50 animate-pulse`}),(0,j.jsx)(`div`,{className:`absolute inset-0 flex items-center justify-center`,children:(0,j.jsx)(R,{className:`w-8 h-8 text-bambu-green`})})]}),(0,j.jsx)(`p`,{className:`text-bambu-green font-medium`,children:m(`spoolbuddy.writeTag.writing`,`Writing tag...`)}),(0,j.jsx)(`p`,{className:`text-zinc-500 text-xs`,children:t}),(0,j.jsx)(`button`,{onClick:f,className:`px-4 py-2 bg-bambu-dark-tertiary hover:bg-bambu-dark-secondary text-zinc-400 text-sm rounded transition-colors`,children:m(`spoolbuddy.writeTag.cancel`,`Cancel`)})]});if(!a)return(0,j.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-3`,children:[(0,j.jsx)(R,{className:`w-12 h-12 text-zinc-600`}),(0,j.jsx)(`p`,{className:`text-zinc-500 text-sm`,children:m(`spoolbuddy.writeTag.deviceOffline`,`SpoolBuddy is offline`)})]});if(!n)return(0,j.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-3`,children:[(0,j.jsx)(R,{className:`w-12 h-12 text-zinc-600`}),(0,j.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:m(`spoolbuddy.writeTag.selectSpool`,`Select a spool, then place a blank NTAG on the reader`)})]});let g=k(n,`preview`)??(n.rgba?h(n.rgba):{backgroundColor:`#666`});return(0,j.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4 w-full`,children:[(0,j.jsx)(`div`,{className:`relative w-16 h-16`,children:r?(0,j.jsxs)(j.Fragment,{children:[(0,j.jsx)(`div`,{className:`absolute inset-0 rounded-full bg-bambu-green/10`}),(0,j.jsx)(`div`,{className:`absolute inset-0 flex items-center justify-center`,children:(0,j.jsx)(R,{className:`w-8 h-8 text-bambu-green`})})]}):(0,j.jsxs)(j.Fragment,{children:[(0,j.jsx)(`div`,{className:`absolute inset-0 rounded-full border-2 border-zinc-600 animate-pulse`}),(0,j.jsx)(`div`,{className:`absolute inset-0 flex items-center justify-center`,children:(0,j.jsx)(R,{className:`w-8 h-8 text-zinc-500`})})]})}),r?(0,j.jsxs)(`div`,{className:`space-y-1`,children:[(0,j.jsx)(`p`,{className:`text-bambu-green text-sm font-medium`,children:m(`spoolbuddy.writeTag.tagReady`,`Tag detected — ready to write`)}),i&&(0,j.jsx)(`p`,{className:`text-zinc-500 text-xs font-mono`,children:i})]}):(0,j.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:m(`spoolbuddy.writeTag.placeTag`,`Place an NTAG on the reader`)}),(0,j.jsxs)(`div`,{className:`w-full bg-bambu-dark-secondary rounded-lg p-3 space-y-2`,children:[(0,j.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,j.jsx)(`div`,{className:`w-8 h-8 rounded-full border border-white/10 shrink-0`,style:g}),(0,j.jsxs)(`div`,{className:`text-left min-w-0`,children:[(0,j.jsxs)(`p`,{className:`text-white text-sm font-medium truncate`,children:[n.brand?`${n.brand} `:``,n.material]}),M(n)&&(0,j.jsx)(`p`,{className:`text-zinc-400 text-xs`,children:M(n)})]})]}),(0,j.jsxs)(`div`,{className:`text-xs text-zinc-500`,children:[n.label_weight,`g`]})]}),s&&n.tag_uid&&(0,j.jsx)(`p`,{className:`text-yellow-500/80 text-xs`,children:m(`spoolbuddy.writeTag.replaceWarning`,`Old tag will be unlinked. New tag will replace it.`)}),(0,j.jsx)(`button`,{onClick:u,disabled:!o,className:`w-full py-3 bg-bambu-green hover:bg-bambu-green/80 disabled:opacity-40 disabled:cursor-not-allowed text-white font-medium rounded-lg transition-colors text-sm`,children:s?m(`spoolbuddy.writeTag.replaceTag`,`Replace Tag`):m(`spoolbuddy.writeTag.writeTag`,`Write Tag`)}),s&&c&&(0,j.jsx)(`button`,{onClick:d,disabled:l,className:`w-full py-2.5 bg-bambu-dark-tertiary hover:bg-bambu-dark-secondary disabled:opacity-40 disabled:cursor-not-allowed text-zinc-200 rounded-lg transition-colors text-sm`,children:l?m(`spoolbuddy.writeTag.untagging`,`Removing tag...`):m(`spoolbuddy.writeTag.untagSpool`,`Untag Selected Spool`)})]})}function R({className:e}){return(0,j.jsxs)(`svg`,{className:e,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:1.5,children:[(0,j.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,d:`M8.288 15.038a5.25 5.25 0 017.424 0M5.106 11.856c3.807-3.808 9.98-3.808 13.788 0M1.924 8.674c5.565-5.565 14.587-5.565 20.152 0`}),(0,j.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,d:`M12.53 18.22l-.53.53-.53-.53a.75.75 0 011.06 0z`})]})}export{P as SpoolBuddyWriteTagPage};