SpoolBuddyWriteTagPage-u9VsCLPe.js 33 KB

1
  1. import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{T as t,a as n,k as r,v as i,w as a}from"./client-Wixdakxn.js";import{u as o}from"./chunk-62JRHF6Z-B7-_tDz8.js";import{t as s}from"./useQuery-DkJQ4hAK.js";import{a as c}from"./info-Cs6q0Q_N.js";import{t as l}from"./chevron-right-BbvQOXI_.js";import{n as u}from"./ToastContext-FG4Q6YJB.js";import{t as d}from"./sparkles-CdDZgnnX.js";import{a as f,c as p}from"./colors-BBBvvSKR.js";import{t as m}from"./useColorCatalogVersion-me0iWO3m.js";import{n as h}from"./currency-R7oUEamO.js";import{n as ee}from"./useUiSettings-BC4O5ZgX.js";import{t as g}from"./NumberInput-VlZYDJ-L.js";import{n as _,o as v,r as y,s as te,t as b}from"./AdditionalSection-BimvJ0xv.js";import{b as ne,c as x,f as re,g as S,h as C,i as w,n as T,o as E,r as D,s as O,t as k,u as A}from"./utils-CRm_p6_K.js";import{n as j}from"./spoolPaint-Imx_KAjK.js";var M=e(r(),1),N=a();function P({formData:e,printersWithCalibrations:n,selectedProfiles:r,setSelectedProfiles:i,expandedPrinters:a,setExpandedPrinters:o}){let{t:s}=t(),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 r=`${e}:${t}:${n??`null`}`,a=`${e}:${n??`null`}`;i(e=>{let t=new Set(e);if(t.has(r))t.delete(r);else{for(let e of Array.from(t)){let n=e.split(`:`);`${n[0]}:${n[2]}`===a&&t.delete(e)}t.add(r)}return t})},p=()=>{let t=new Set;for(let{printer:r,calibrations:i}of n){if(!r.connected)continue;let n=i.filter(t=>E(t,e)),a=new Map;for(let e of n){let t=`${e.extruder_id??`null`}`;a.has(t)||a.set(t,[]),a.get(t).push(e)}for(let[e,n]of a)if(n.length>0){let i=[...n].sort((e,t)=>t.k_value-e.k_value)[0];t.add(`${r.id}:${i.cali_idx}:${e}`)}}i(t)};if(!e.material)return(0,N.jsx)(`div`,{className:`p-6 bg-bambu-dark rounded-lg text-center`,children:(0,N.jsx)(`p`,{className:`text-bambu-gray`,children:s(`inventory.selectMaterialFirst`)})});if(n.length===0)return(0,N.jsx)(`div`,{className:`p-6 bg-bambu-dark rounded-lg text-center`,children:(0,N.jsx)(`p`,{className:`text-bambu-gray`,children:s(`inventory.noPrintersConfigured`)})});let m=n.reduce((t,{printer:n,calibrations:r})=>n.connected?t+r.filter(t=>E(t,e)).length:t,0),h=(e,t)=>{let n=`${e.id}:${t.cali_idx}:${t.extruder_id??`null`}`,i=r.has(n);return(0,N.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,N.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,N.jsx)(`div`,{className:`flex-1 min-w-0`,children:(0,N.jsx)(`span`,{className:`text-sm font-medium ${i?`text-bambu-green`:`text-white`}`,children:t.name||t.filament_id})}),(0,N.jsxs)(`div`,{className:`flex items-center gap-2 shrink-0`,children:[t.nozzle_diameter&&(0,N.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,N.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,N.jsxs)(`div`,{className:`space-y-4`,children:[(0,N.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,N.jsxs)(`p`,{className:`text-xs text-bambu-gray`,children:[s(`inventory.matchingFilter`),`: `,e.brand||s(`inventory.anyBrand`),` / `,e.material,` / `,e.subtype||s(`inventory.anyVariant`)]}),m>0&&(0,N.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,N.jsx)(d,{className:`w-3.5 h-3.5`}),s(`inventory.autoSelect`),` (`,m,`)`]})]}),(0,N.jsx)(`div`,{className:`space-y-3`,children:n.map(({printer:t,calibrations:n})=>{let r=a.has(String(t.id)),i=n.filter(t=>E(t,e)),o=i.length,d=i.some(e=>e.extruder_id!==void 0&&e.extruder_id!==null&&e.extruder_id>0),f=i.filter(e=>e.extruder_id===1),p=i.filter(e=>e.extruder_id===0||e.extruder_id===void 0||e.extruder_id===null);return(0,N.jsxs)(`div`,{className:`border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:[(0,N.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,N.jsxs)(`div`,{className:`flex items-center gap-3`,children:[r?(0,N.jsx)(c,{className:`w-4 h-4 text-bambu-gray`}):(0,N.jsx)(l,{className:`w-4 h-4 text-bambu-gray`}),(0,N.jsx)(`span`,{className:`font-medium text-white`,children:t.name}),o>0?(0,N.jsxs)(`span`,{className:`text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green`,children:[o,` `,s(o===1?`inventory.match`:`inventory.matches`)]}):(0,N.jsx)(`span`,{className:`text-xs px-2 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray`,children:s(`inventory.noMatches`)})]}),(0,N.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?s(`inventory.connected`):s(`inventory.offline`)})]}),r&&(0,N.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,N.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-2`,children:s(`inventory.noKProfilesMatch`)}):d?(0,N.jsxs)(N.Fragment,{children:[f.length>0&&(0,N.jsxs)(`div`,{className:`space-y-2`,children:[(0,N.jsx)(`p`,{className:`text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:s(`inventory.leftNozzle`)}),(0,N.jsx)(`div`,{className:`space-y-2`,children:f.map(e=>h(t,e))})]}),p.length>0&&(0,N.jsxs)(`div`,{className:`space-y-2`,children:[(0,N.jsx)(`p`,{className:`text-xs font-medium text-bambu-gray uppercase tracking-wide`,children:s(`inventory.rightNozzle`)}),(0,N.jsx)(`div`,{className:`space-y-2`,children:p.map(e=>h(t,e))})]})]}):(0,N.jsx)(`div`,{className:`space-y-2`,children:i.map(e=>h(t,e))}):(0,N.jsx)(`p`,{className:`text-sm text-bambu-gray italic py-2`,children:s(`inventory.printerOffline`)})})]},t.id)})}),r.size>0&&(0,N.jsx)(`div`,{className:`p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg`,children:(0,N.jsxs)(`p`,{className:`text-sm text-white`,children:[(0,N.jsx)(`span`,{className:`font-semibold`,children:r.size}),` `,s(`inventory.profilesSelected`)]})})]})}function F(e){return p(e.color_name,e.rgba,e.color_name_is_synthesized)}var I=[`PLA`,`PETG`,`ABS`,`ASA`,`TPU`,`PA`,`PC`,`PVA`,`HIPS`];function ie(){let{t:e}=t(),r=m(),{showToast:a}=u(),{sbState:c}=o(),[l,d]=(0,M.useState)(`existing`),[f,p]=(0,M.useState)(null),[g,_]=(0,M.useState)(``),[v,y]=(0,M.useState)(`idle`),[te,b]=(0,M.useState)(``),[ne,x]=(0,M.useState)(!1),[re,S]=(0,M.useState)(!1),[C,w]=(0,M.useState)(null),{data:T}=s({queryKey:[`spoolman-settings`],queryFn:n.getSpoolmanSettings,staleTime:300*1e3}),E=T!==void 0,D=T?.spoolman_enabled===`true`&&!!T?.spoolman_url,{data:O=[],refetch:k}=s({queryKey:[D?`spoolman-inventory-spools`:`inventory-spools`,`write-tag`],queryFn:()=>D?n.getSpoolmanInventorySpools(!1):n.getSpools(!1),refetchInterval:1e4,enabled:E}),{data:A=[]}=s({queryKey:[`spoolbuddy-devices`],queryFn:()=>i.getDevices(),refetchInterval:5e3}),{data:j}=ee(),P=A[0],I=c.deviceOnline,ie=h(j?.currency||`USD`),z=(0,M.useMemo)(()=>{let e;if(l===`existing`)e=O.filter(e=>!e.tag_uid&&!e.tray_uuid&&!e.archived_at);else if(l===`replace`)e=O.filter(e=>(e.tag_uid||e.tray_uuid)&&!e.archived_at);else return[];if(g){let t=g.toLowerCase();e=e.filter(e=>e.material?.toLowerCase().includes(t)||F(e)?.toLowerCase().includes(t)||e.color_name?.toLowerCase().includes(t)||e.brand?.toLowerCase().includes(t)||e.subtype?.toLowerCase().includes(t))}return e},[O,l,g,r]),B=(0,M.useCallback)(e=>{let t=e.detail;t.sak===0&&(S(!0),w(t.tag_uid??null))},[]),V=(0,M.useCallback)(e=>{let t=e.detail;S(!0),w(t.tag_uid??null)},[]),H=(0,M.useCallback)(()=>{S(!1),w(null)},[]),U=(0,M.useCallback)(t=>{t.detail.spool_id===f?.id&&(y(`success`),b(e(`spoolbuddy.writeTag.writeSuccess`,`Tag written successfully!`)),k(),setTimeout(()=>{y(`idle`),p(null),b(``)},5e3))},[f,e,k]),W=(0,M.useCallback)(t=>{let n=t.detail;n.spool_id===f?.id&&(y(`error`),b(n.message??e(`spoolbuddy.writeTag.writeFailed`,`Write failed`)))},[f,e]),G=(0,M.useCallback)(t=>{let n=t.detail;if(n.spool_id===f?.id){let t=n.reason;y(`error`),b(e(t===`spoolman_not_configured`?`spoolbuddy.writeTag.linkFailedSpoolmanNotConfigured`:t===`spool_not_found`?`spoolbuddy.writeTag.linkFailedSpoolNotFound`:`spoolbuddy.writeTag.linkFailedSpoolman`)),k()}},[f,e,k]);(0,M.useEffect)(()=>(window.addEventListener(`spoolbuddy-tag-link-failed`,G),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),window.removeEventListener(`spoolbuddy-tag-link-failed`,G)}),[B,V,H,U,W,G]),(0,M.useEffect)(()=>{p(null),y(`idle`),b(``),_(``)},[l]);let K=async()=>{if(!(!f||!P)){y(`writing`),b(e(`spoolbuddy.writeTag.waiting`,`Waiting for SpoolBuddy...`));try{let e=await i.writeTag(P.device_id,f.id);if(e?.warnings?.length)for(let t of e.warnings)a(t,`warning`)}catch(t){y(`error`);let n=t?.status;b(n===503?e(`spoolbuddy.writeTag.spoolmanUnavailable`):n===409?e(`spoolbuddy.writeTag.linkedToAms`):e(`spoolbuddy.writeTag.queueFailed`,`Failed to queue write command`))}}},se=async()=>{if(P){try{await i.cancelWrite(P.device_id)}catch{}y(`idle`),b(``)}},q=async()=>{if(!(!f||!L(f))){x(!0),y(`idle`),b(``);try{D?await n.linkTagToSpoolmanSpool(f.id,{tag_uid:``,tray_uuid:``}):await n.linkTagToSpool(f.id,{tag_uid:``,tray_uuid:``,data_origin:`manual`}),await k(),p(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{x(!1)}}},J=(0,M.useCallback)(e=>{p(e),y(`idle`),b(``),k()},[k]),ce=f&&I&&v!==`writing`&&v!==`success`;return(0,N.jsxs)(`div`,{className:`flex flex-col h-full`,children:[(0,N.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,N.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,N.jsxs)(`div`,{className:`flex flex-1 overflow-hidden`,children:[(0,N.jsx)(`div`,{className:`flex-1 flex flex-col overflow-hidden border-r border-bambu-dark-tertiary`,children:l===`new`?(0,N.jsx)(R,{currencySymbol:ie,onCreated:J,selectedSpool:f,spoolmanMode:D,t:e}):(0,N.jsxs)(N.Fragment,{children:[(0,N.jsx)(`div`,{className:`p-3 shrink-0`,children:(0,N.jsx)(`input`,{type:`text`,value:g,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,N.jsx)(`div`,{className:`flex-1 overflow-y-auto px-3 pb-3 space-y-2`,children:z.length===0?(0,N.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,N.jsx)(ae,{spool:e,selected:f?.id===e.id,showTag:l===`replace`,onClick:()=>{p(e),y(`idle`),b(``)}},e.id))})]})}),(0,N.jsx)(`div`,{className:`w-[340px] flex flex-col items-center justify-center p-6 shrink-0`,children:(0,N.jsx)(oe,{writeStatus:v,writeMessage:te,selectedSpool:f,tagOnReader:re,tagUid:C,deviceOnline:I,canWrite:!!ce,isReplace:l===`replace`,canUntag:l===`replace`&&!!f&&L(f),untagging:ne,onWrite:K,onUntag:q,onCancel:se,onRetry:()=>{y(`idle`),b(``)},t:e})})]})]})}function L(e){return!!(e.tag_uid||e.tray_uuid)}function ae({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,N.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,N.jsx)(`div`,{className:`w-8 h-8 rounded-full shrink-0 border border-white/10`,style:j(e)??(e.rgba?f(e.rgba):{backgroundColor:`#666`})}),(0,N.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,N.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,N.jsxs)(`span`,{className:`text-sm font-medium text-white truncate`,children:[e.brand?`${e.brand} `:``,e.material,e.subtype?` ${e.subtype}`:``]}),(0,N.jsxs)(`span`,{className:`text-[10px] font-mono text-zinc-500 shrink-0`,children:[`#`,e.id]})]}),(0,N.jsxs)(`div`,{className:`flex items-center gap-2 text-xs text-zinc-400`,children:[F(e)&&(0,N.jsx)(`span`,{children:F(e)}),(0,N.jsxs)(`span`,{children:[i,`g / `,e.label_weight,`g (`,a,`%)`]})]}),n&&e.tag_uid&&(0,N.jsx)(`div`,{className:`text-xs text-zinc-500 mt-0.5 font-mono`,children:e.tag_uid})]}),t&&(0,N.jsx)(`svg`,{className:`w-5 h-5 text-bambu-green shrink-0`,fill:`currentColor`,viewBox:`0 0 20 20`,children:(0,N.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 R({currencySymbol:e,onCreated:t,selectedSpool:r,spoolmanMode:i,t:a}){let{data:o=[]}=s({queryKey:[i?`spoolman-inventory-spools`:`inventory-spools`,`write-tag-form`],queryFn:()=>i?n.getSpoolmanInventorySpools(!0):n.getSpools(!0)}),{data:c}=ee(),[l,u]=(0,M.useState)(`simple`),[d,p]=(0,M.useState)(`filament`),[m,h]=(0,M.useState)(v),[E,ie]=(0,M.useState)(!1),[L,ae]=(0,M.useState)({}),[R,oe]=(0,M.useState)(!1),[z,B]=(0,M.useState)(1),[V,H]=(0,M.useState)(!1),[U,W]=(0,M.useState)(null),[G,K]=(0,M.useState)(!1),[se,q]=(0,M.useState)(!1),[J,ce]=(0,M.useState)([]),[Y,le]=(0,M.useState)([]),[ue,de]=(0,M.useState)([]),[fe,pe]=(0,M.useState)([]),[X,me]=(0,M.useState)([]),[he,ge]=(0,M.useState)(``),[_e,ve]=(0,M.useState)([]),[Z,ye]=(0,M.useState)([]),[be,xe]=(0,M.useState)(new Set),[Se,Ce]=(0,M.useState)(new Set);(0,M.useEffect)(()=>{ve(O())},[]),(0,M.useEffect)(()=>{let e=!1;return(async()=>{if(l===`full`){q(!0);try{let[t,r]=await Promise.allSettled([(async()=>(await n.getCloudStatus()).is_authenticated?{connected:!0,presets:await n.getFilamentPresets()}:{connected:!1,presets:[]})(),(async()=>(await n.orcaCloudStatus()).connected?{connected:!0,presets:(await n.orcaCloudListProfiles()).filament}:{connected:!1,presets:[]})()]);if(e)return;let i=t.status===`fulfilled`&&t.value.connected,a=r.status===`fulfilled`&&r.value.connected,o=t.status===`fulfilled`?t.value.presets:[],s=r.status===`fulfilled`?r.value.presets:[];K(i||a),ce([...o,...s])}catch{e||K(!1)}finally{e||q(!1)}n.getSpoolCatalog().then(t=>{e||pe(t)}).catch(()=>void 0),n.getColorCatalog().then(t=>{e||me(t)}).catch(()=>void 0),n.getLocalPresets().then(t=>{e||le(t.filament)}).catch(()=>void 0),n.getBuiltinFilaments().then(t=>{e||de(t)}).catch(()=>void 0);try{let t=await n.getPrinters(),r=await Promise.all(t.map(e=>n.getPrinterStatus(e.id).catch(()=>null))),i=[];for(let e=0;e<t.length;e++){let n=t[e],a=r[e],o=a?.connected??!1,s=[];o&&(s=await D(n.id,a)),i.push({printer:{...n,connected:o},calibrations:s})}e||ye(i)}catch{}}})(),()=>{e=!0}},[l]),(0,M.useEffect)(()=>{Z.length>0&&Ce(new Set(Z.map(e=>String(e.printer.id))))},[Z]);let we=(0,M.useMemo)(()=>k(J,new Set,Y,ue),[J,Y,ue]),Te=(0,M.useMemo)(()=>w(m.slicer_filament,we),[m.slicer_filament,we]),Ee=(0,M.useMemo)(()=>{let e=T(J,Y),t=X.map(e=>e.manufacturer?.trim()).filter(e=>!!e);return Array.from(new Set([...e,...t])).sort((e,t)=>e.localeCompare(t))},[J,Y,X]),De=(0,M.useMemo)(()=>{let e=X.map(e=>e.material?.trim()).filter(e=>!!e);return Array.from(new Set([...ne,...e])).sort((e,t)=>e.localeCompare(t))},[X]),Q=(0,M.useMemo)(()=>{let e=[];for(let t of X){let n=t.manufacturer?.trim(),r=t.material?.trim();n&&r&&e.push({brand:n,material:r})}for(let t of J){let n=A(t.name);n.brand&&n.material&&e.push({brand:n.brand,material:n.material})}for(let t of Y){let n=A(t.name),r=t.filament_vendor?.trim()||n.brand,i=n.material;r&&i&&e.push({brand:r,material:i})}return e},[J,X,Y]),Oe=(0,M.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]),ke=(0,M.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]),Ae=(0,M.useMemo)(()=>C(Ee,m.brand),[Ee,m.brand]),je=(0,M.useMemo)(()=>C(De,m.material),[De,m.material]),Me=(0,M.useMemo)(()=>x(Ae,m.material,ke),[Ae,m.material,ke]),Ne=(0,M.useMemo)(()=>x(je,m.brand,Oe),[je,m.brand,Oe]),$=(e,t)=>{h(n=>({...n,[e]:t})),e===`core_weight`&&ie(!0),L[e]&&ae(t=>({...t,[e]:void 0}))},Pe=e=>{ve(t=>re(e,t))},Fe=async e=>{let t=i?n.saveSpoolmanKProfiles:n.saveSpoolKProfiles;if(be.size===0)return;let r=[];for(let e of be){let[t,n,i]=e.split(`:`),a=parseInt(t),o=parseInt(n),s=i===`null`?0:parseInt(i),c=Z.find(e=>e.printer.id===a);if(c){let e=c.calibrations.find(e=>e.cali_idx===o&&(e.extruder_id??0)===s);e&&r.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})}}r.length>0&&(r=await S(r,m),await t(e,r))},Ie=async()=>{W(null);let e=te(m,l===`simple`||R);if(!e.isValid){ae(e.errors),p(`filament`);return}let r=Te?.displayName||he||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?E?{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:r,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};H(!0);try{if(z>1){let e=i?await n.bulkCreateSpoolmanInventorySpools(o,z):await n.bulkCreateSpools(o,z),r=i&&e&&typeof e==`object`&&`created`in e?e.created:e;for(let e of r)await Fe(e.id);r.length>0&&t(r[0])}else{let e=i?await n.createSpoolmanInventorySpool(o):await n.createSpool(o);await Fe(e.id),t(e)}}catch{W(a(`spoolbuddy.writeTag.createFailed`,`Failed to create spool`))}finally{H(!1)}},Le=`#${(m.rgba||`808080FF`).slice(0,6)}`;return(0,N.jsxs)(`div`,{className:`p-3 space-y-3 overflow-y-auto h-full`,children:[(0,N.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,N.jsx)(`span`,{className:`text-sm text-zinc-200`,children:a(`spoolbuddy.writeTag.viewMode`,`View`)}),(0,N.jsxs)(`div`,{className:`flex rounded-lg overflow-hidden border border-bambu-dark-tertiary`,children:[(0,N.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,N.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`?r?(0,N.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,N.jsx)(`div`,{className:`w-12 h-12 rounded-full mb-4 border border-white/10`,style:j(r,`preview`)??(r.rgba?f(r.rgba):{backgroundColor:`#666`})}),(0,N.jsxs)(`p`,{className:`text-white font-medium`,children:[r.brand?`${r.brand} `:``,r.material]}),F(r)&&(0,N.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:F(r)}),(0,N.jsxs)(`p`,{className:`text-zinc-500 text-xs mt-1`,children:[r.label_weight,`g`]}),(0,N.jsx)(`p`,{className:`text-bambu-green text-sm mt-4`,children:a(`spoolbuddy.writeTag.spoolCreated`,`Spool created! Ready to write.`)})]}):(0,N.jsxs)(`div`,{className:`p-4 space-y-4 overflow-y-auto bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg`,children:[(0,N.jsxs)(`div`,{children:[(0,N.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.material`,`Material`)}),(0,N.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:I.map(e=>(0,N.jsx)(`option`,{value:e,children:e},e))})]}),(0,N.jsxs)(`div`,{className:`flex gap-3`,children:[(0,N.jsxs)(`div`,{className:`flex-1`,children:[(0,N.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.colorName`,`Color Name`)}),(0,N.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,N.jsxs)(`div`,{children:[(0,N.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.color`,`Color`)}),(0,N.jsx)(`input`,{type:`color`,value:Le,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,N.jsxs)(`div`,{children:[(0,N.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.brand`,`Brand`)}),(0,N.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,N.jsxs)(`div`,{children:[(0,N.jsx)(`label`,{className:`block text-xs text-zinc-400 mb-1`,children:a(`spoolbuddy.writeTag.weight`,`Weight (g)`)}),(0,N.jsx)(g,{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,N.jsx)(`button`,{onClick:Ie,disabled:V||!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:V?a(`spoolbuddy.writeTag.creating`,`Creating...`):a(`spoolbuddy.writeTag.createSpool`,`Create Spool`)})]}):(0,N.jsxs)(N.Fragment,{children:[(0,N.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,N.jsx)(`span`,{className:`text-sm text-zinc-200`,children:a(`inventory.quickAdd`,`Quick Add`)}),(0,N.jsx)(`button`,{type:`button`,onClick:()=>oe(e=>!e),className:`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${R?`bg-bambu-green`:`bg-bambu-dark-tertiary`}`,children:(0,N.jsx)(`span`,{className:`inline-block h-4.5 w-4.5 rounded-full bg-white transition-transform ${R?`translate-x-6`:`translate-x-1`}`})})]}),(0,N.jsxs)(`div`,{className:`flex border border-bambu-dark-tertiary rounded-lg overflow-hidden`,children:[(0,N.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`)}),!R&&(0,N.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,N.jsx)(`div`,{className:`bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg p-3`,children:d===`filament`?(0,N.jsxs)(`div`,{className:`space-y-4`,children:[(0,N.jsx)(y,{formData:m,updateField:$,cloudAuthenticated:G,loadingCloudPresets:se,presetInputValue:he,setPresetInputValue:ge,selectedPresetOption:Te,filamentOptions:we,availableBrands:Ae,availableMaterials:je,suggestedBrands:Me,suggestedMaterials:Ne,quickAdd:R,detailsRequired:!R,quantity:z,onQuantityChange:B,errors:L}),(0,N.jsx)(_,{formData:m,updateField:$,recentColors:_e,onColorUsed:Pe,catalogColors:X}),(0,N.jsx)(b,{formData:m,updateField:$,spoolCatalog:fe,currencySymbol:e,availableCategories:Array.from(new Set(o.map(e=>e.category?.trim()).filter(e=>!!e))).sort((e,t)=>e.localeCompare(t)),availableMaterialNumbers:Array.from(new Set(o.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,N.jsx)(P,{formData:m,printersWithCalibrations:Z,selectedProfiles:be,setSelectedProfiles:xe,expandedPrinters:Se,setExpandedPrinters:Ce})})]}),U&&(0,N.jsx)(`div`,{className:`text-sm text-red-400 bg-red-900/20 border border-red-900/40 rounded-lg px-3 py-2`,children:U}),l===`full`&&(0,N.jsx)(`button`,{onClick:Ie,disabled:V,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:V?a(`spoolbuddy.writeTag.creating`,`Creating...`):a(`spoolbuddy.writeTag.createSpool`,`Create Spool`)}),l===`full`&&r&&(0,N.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,N.jsx)(`div`,{className:`w-12 h-12 rounded-full mb-4 border border-white/10`,style:j(r,`preview`)??(r.rgba?f(r.rgba):{backgroundColor:`#666`})}),(0,N.jsxs)(`p`,{className:`text-white font-medium`,children:[r.brand?`${r.brand} `:``,r.material]}),F(r)&&(0,N.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:F(r)}),(0,N.jsxs)(`p`,{className:`text-zinc-500 text-xs mt-1`,children:[r.label_weight,`g`]}),(0,N.jsx)(`p`,{className:`text-bambu-green text-sm mt-4`,children:a(`spoolbuddy.writeTag.spoolCreated`,`Spool created! Ready to write.`)})]})]})}function oe({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:p,onRetry:m,t:h}){if(e===`success`)return(0,N.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4`,children:[(0,N.jsx)(`div`,{className:`w-16 h-16 rounded-full bg-green-500/20 flex items-center justify-center`,children:(0,N.jsx)(`svg`,{className:`w-8 h-8 text-green-400`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,N.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:2,d:`M5 13l4 4L19 7`})})}),(0,N.jsx)(`p`,{className:`text-green-400 font-medium`,children:t}),n&&(0,N.jsxs)(`p`,{className:`text-zinc-400 text-sm`,children:[n.brand?`${n.brand} `:``,n.material,n.subtype?` ${n.subtype}`:``,F(n)?` - ${F(n)}`:``]})]});if(e===`error`)return(0,N.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4`,children:[(0,N.jsx)(`div`,{className:`w-16 h-16 rounded-full bg-red-500/20 flex items-center justify-center`,children:(0,N.jsx)(`svg`,{className:`w-8 h-8 text-red-400`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,N.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:2,d:`M6 18L18 6M6 6l12 12`})})}),(0,N.jsx)(`p`,{className:`text-red-400 font-medium`,children:t}),(0,N.jsx)(`button`,{onClick:m,className:`px-4 py-2 bg-bambu-dark-tertiary hover:bg-bambu-dark-secondary text-white text-sm rounded transition-colors`,children:h(`spoolbuddy.writeTag.tryAgain`,`Try Again`)})]});if(e===`writing`)return(0,N.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4`,children:[(0,N.jsxs)(`div`,{className:`relative w-16 h-16`,children:[(0,N.jsx)(`div`,{className:`absolute inset-0 rounded-full border-2 border-bambu-green/30 animate-ping`}),(0,N.jsx)(`div`,{className:`absolute inset-2 rounded-full border-2 border-bambu-green/50 animate-pulse`}),(0,N.jsx)(`div`,{className:`absolute inset-0 flex items-center justify-center`,children:(0,N.jsx)(z,{className:`w-8 h-8 text-bambu-green`})})]}),(0,N.jsx)(`p`,{className:`text-bambu-green font-medium`,children:h(`spoolbuddy.writeTag.writing`,`Writing tag...`)}),(0,N.jsx)(`p`,{className:`text-zinc-500 text-xs`,children:t}),(0,N.jsx)(`button`,{onClick:p,className:`px-4 py-2 bg-bambu-dark-tertiary hover:bg-bambu-dark-secondary text-zinc-400 text-sm rounded transition-colors`,children:h(`spoolbuddy.writeTag.cancel`,`Cancel`)})]});if(!a)return(0,N.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-3`,children:[(0,N.jsx)(z,{className:`w-12 h-12 text-zinc-600`}),(0,N.jsx)(`p`,{className:`text-zinc-500 text-sm`,children:h(`spoolbuddy.writeTag.deviceOffline`,`SpoolBuddy is offline`)})]});if(!n)return(0,N.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-3`,children:[(0,N.jsx)(z,{className:`w-12 h-12 text-zinc-600`}),(0,N.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:h(`spoolbuddy.writeTag.selectSpool`,`Select a spool, then place a blank NTAG on the reader`)})]});let ee=j(n,`preview`)??(n.rgba?f(n.rgba):{backgroundColor:`#666`});return(0,N.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-4 w-full`,children:[(0,N.jsx)(`div`,{className:`relative w-16 h-16`,children:r?(0,N.jsxs)(N.Fragment,{children:[(0,N.jsx)(`div`,{className:`absolute inset-0 rounded-full bg-bambu-green/10`}),(0,N.jsx)(`div`,{className:`absolute inset-0 flex items-center justify-center`,children:(0,N.jsx)(z,{className:`w-8 h-8 text-bambu-green`})})]}):(0,N.jsxs)(N.Fragment,{children:[(0,N.jsx)(`div`,{className:`absolute inset-0 rounded-full border-2 border-zinc-600 animate-pulse`}),(0,N.jsx)(`div`,{className:`absolute inset-0 flex items-center justify-center`,children:(0,N.jsx)(z,{className:`w-8 h-8 text-zinc-500`})})]})}),r?(0,N.jsxs)(`div`,{className:`space-y-1`,children:[(0,N.jsx)(`p`,{className:`text-bambu-green text-sm font-medium`,children:h(`spoolbuddy.writeTag.tagReady`,`Tag detected — ready to write`)}),i&&(0,N.jsx)(`p`,{className:`text-zinc-500 text-xs font-mono`,children:i})]}):(0,N.jsx)(`p`,{className:`text-zinc-400 text-sm`,children:h(`spoolbuddy.writeTag.placeTag`,`Place an NTAG on the reader`)}),(0,N.jsxs)(`div`,{className:`w-full bg-bambu-dark-secondary rounded-lg p-3 space-y-2`,children:[(0,N.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,N.jsx)(`div`,{className:`w-8 h-8 rounded-full border border-white/10 shrink-0`,style:ee}),(0,N.jsxs)(`div`,{className:`text-left min-w-0`,children:[(0,N.jsxs)(`p`,{className:`text-white text-sm font-medium truncate`,children:[n.brand?`${n.brand} `:``,n.material]}),F(n)&&(0,N.jsx)(`p`,{className:`text-zinc-400 text-xs`,children:F(n)})]})]}),(0,N.jsxs)(`div`,{className:`text-xs text-zinc-500`,children:[n.label_weight,`g`]})]}),s&&n.tag_uid&&(0,N.jsx)(`p`,{className:`text-yellow-500/80 text-xs`,children:h(`spoolbuddy.writeTag.replaceWarning`,`Old tag will be unlinked. New tag will replace it.`)}),(0,N.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?h(`spoolbuddy.writeTag.replaceTag`,`Replace Tag`):h(`spoolbuddy.writeTag.writeTag`,`Write Tag`)}),s&&c&&(0,N.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?h(`spoolbuddy.writeTag.untagging`,`Removing tag...`):h(`spoolbuddy.writeTag.untagSpool`,`Untag Selected Spool`)})]})}function z({className:e}){return(0,N.jsxs)(`svg`,{className:e,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:1.5,children:[(0,N.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,N.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,d:`M12.53 18.22l-.53.53-.53-.53a.75.75 0 011.06 0z`})]})}export{ie as SpoolBuddyWriteTagPage};