| 1 |
- 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-C5b1pVUf.js";import{t as o}from"./useQuery-DOr0golm.js";import{a as s}from"./info-7SrTpN6e.js";import{n as c}from"./ToastContext-CypVB8nG.js";import{t as l}from"./sparkles-DKx0jOii.js";import{B as u,N as d,Nn as f,Un as p,ct as m,dt as h,mt as g,st as ee}from"./index-D3tAzFbt.js";import{n as _,o as v,r as y,s as te,t as b}from"./AdditionalSection-aV59zOxS.js";import{c as x,f as S,i as ne,m as C,n as re,o as w,r as T,s as ie,t as E,u as D,v as O}from"./utils-uriNY2Iq.js";import{n as k}from"./spoolPaint-CrvpHqdD.js";var A=e(n(),1),j=t();function ae({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})},d=(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:()=>d(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),d=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)(f,{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:[d.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:d.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 g(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=m(),{showToast:n}=c(),{sbState:s}=p(),[l,d]=(0,A.useState)(`existing`),[f,h]=(0,A.useState)(null),[g,_]=(0,A.useState)(``),[v,y]=(0,A.useState)(`idle`),[te,b]=(0,A.useState)(``),[x,S]=(0,A.useState)(!1),[ne,C]=(0,A.useState)(!1),[re,w]=(0,A.useState)(null),{data:T}=o({queryKey:[`spoolman-settings`],queryFn:r.getSpoolmanSettings,staleTime:300*1e3}),ie=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:ie}),{data:k=[]}=o({queryKey:[`spoolbuddy-devices`],queryFn:()=>i.getDevices(),refetchInterval:5e3}),{data:ae}=u(),N=k[0],P=s.deviceOnline,L=ee(ae?.currency||`USD`),ce=(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(g){let t=g.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,g,t]),R=(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))},[]),z=(0,A.useCallback)(e=>{let t=e.detail;C(!0),w(t.tag_uid??t.data?.tag_uid??null)},[]),B=(0,A.useCallback)(()=>{C(!1),w(null)},[]),V=(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`),h(null),b(``)},5e3))},[f,e,O]),H=(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`,R),window.addEventListener(`spoolbuddy-tag-matched`,z),window.addEventListener(`spoolbuddy-tag-removed`,B),window.addEventListener(`spoolbuddy-tag-written`,V),window.addEventListener(`spoolbuddy-tag-write-failed`,H),()=>{window.removeEventListener(`spoolbuddy-unknown-tag`,R),window.removeEventListener(`spoolbuddy-tag-matched`,z),window.removeEventListener(`spoolbuddy-tag-removed`,B),window.removeEventListener(`spoolbuddy-tag-written`,V),window.removeEventListener(`spoolbuddy-tag-write-failed`,H)}),[R,z,B,V,H]),(0,A.useEffect)(()=>{h(null),y(`idle`),b(``),_(``)},[l]);let U=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`))}}},le=async()=>{if(N){try{await i.cancelWrite(N.device_id)}catch{}y(`idle`),b(``)}},W=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(),h(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)}}},G=(0,A.useCallback)(e=>{h(e),y(`idle`),b(``),O()},[O]),ue=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)(I,{currencySymbol:L,onCreated:G,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: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,j.jsx)(`div`,{className:`flex-1 overflow-y-auto px-3 pb-3 space-y-2`,children:ce.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`)}):ce.map(e=>(0,j.jsx)(oe,{spool:e,selected:f?.id===e.id,showTag:l===`replace`,onClick:()=>{h(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)(se,{writeStatus:v,writeMessage:te,selectedSpool:f,tagOnReader:ne,tagUid:re,deviceOnline:P,canWrite:!!ue,isReplace:l===`replace`,canUntag:l===`replace`&&!!f&&F(f),untagging:x,onWrite:U,onUntag:W,onCancel:le,onRetry:()=>{y(`idle`),b(``)},t:e})})]})]})}function F(e){return!!(e.tag_uid||e.tray_uuid)}function oe({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 I({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}=u(),[l,f]=(0,A.useState)(`simple`),[p,m]=(0,A.useState)(`filament`),[g,ee]=(0,A.useState)(v),[w,P]=(0,A.useState)(!1),[F,oe]=(0,A.useState)({}),[I,se]=(0,A.useState)(!1),[L,ce]=(0,A.useState)(1),[R,z]=(0,A.useState)(!1),[B,V]=(0,A.useState)(null),[H,U]=(0,A.useState)(!1),[le,W]=(0,A.useState)(!1),[G,ue]=(0,A.useState)([]),[K,de]=(0,A.useState)([]),[fe,pe]=(0,A.useState)([]),[me,he]=(0,A.useState)([]),[q,ge]=(0,A.useState)([]),[_e,ve]=(0,A.useState)(``),[ye,be]=(0,A.useState)([]),[J,xe]=(0,A.useState)([]),[Y,Se]=(0,A.useState)(new Set),[Ce,we]=(0,A.useState)(new Set);(0,A.useEffect)(()=>{be(ie())},[]),(0,A.useEffect)(()=>{let e=!1;return(async()=>{if(l===`full`){W(!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:[];U(i||a),ue([...o,...s])}catch{e||U(!1)}finally{e||W(!1)}r.getSpoolCatalog().then(t=>{e||he(t)}).catch(()=>void 0),r.getColorCatalog().then(t=>{e||ge(t)}).catch(()=>void 0),r.getLocalPresets().then(t=>{e||de(t.filament)}).catch(()=>void 0),r.getBuiltinFilaments().then(t=>{e||pe(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<t.length;e++){let r=t[e],a=n[e],o=a?.connected??!1,s=[];o&&(s=await T(r.id,a)),i.push({printer:{...r,connected:o},calibrations:s})}e||xe(i)}catch{}}})(),()=>{e=!0}},[l]),(0,A.useEffect)(()=>{J.length>0&&we(new Set(J.map(e=>String(e.printer.id))))},[J]);let Te=(0,A.useMemo)(()=>E(G,new Set,K,fe),[G,K,fe]),Ee=(0,A.useMemo)(()=>ne(g.slicer_filament,Te),[g.slicer_filament,Te]),De=(0,A.useMemo)(()=>{let e=re(G,K),t=q.map(e=>e.manufacturer?.trim()).filter(e=>!!e);return Array.from(new Set([...e,...t])).sort((e,t)=>e.localeCompare(t))},[G,K,q]),Oe=(0,A.useMemo)(()=>{let e=q.map(e=>e.material?.trim()).filter(e=>!!e);return Array.from(new Set([...O,...e])).sort((e,t)=>e.localeCompare(t))},[q]),X=(0,A.useMemo)(()=>{let e=[];for(let t of q){let n=t.manufacturer?.trim(),r=t.material?.trim();n&&r&&e.push({brand:n,material:r})}for(let t of G){let n=D(t.name);n.brand&&n.material&&e.push({brand:n.brand,material:n.material})}for(let t of K){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},[G,q,K]),ke=(0,A.useMemo)(()=>{let e=new Map;for(let t of X){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(n)||e.set(n,new Set),e.get(n).add(r)}return e},[X]),Ae=(0,A.useMemo)(()=>{let e=new Map;for(let t of X){let n=t.brand.toLowerCase(),r=t.material.toLowerCase();e.has(r)||e.set(r,new Set),e.get(r).add(n)}return e},[X]),Z=(0,A.useMemo)(()=>C(De,g.brand),[De,g.brand]),Q=(0,A.useMemo)(()=>C(Oe,g.material),[Oe,g.material]),je=(0,A.useMemo)(()=>x(Z,g.material,Ae),[Z,g.material,Ae]),Me=(0,A.useMemo)(()=>x(Q,g.brand,ke),[Q,g.brand,ke]),$=(e,t)=>{ee(n=>({...n,[e]:t})),e===`core_weight`&&P(!0),F[e]&&oe(t=>({...t,[e]:void 0}))},Ne=e=>{be(t=>S(e,t))},Pe=async e=>{let t=i?r.saveSpoolmanKProfiles:r.saveSpoolKProfiles;if(Y.size===0){try{await t(e,[])}catch{}return}let n=[];for(let e of Y){let[t,r,i]=e.split(`:`),a=parseInt(t),o=parseInt(r),s=i===`null`?0:parseInt(i),c=J.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)},Fe=async()=>{V(null);let e=te(g,l===`simple`?!0:I);if(!e.isValid){oe(e.errors),m(`filament`);return}let n=Ee?.displayName||_e||null,o={material:g.material,subtype:g.subtype||null,brand:g.brand||null,color_name:g.color_name||null,rgba:g.rgba||null,extra_colors:g.extra_colors||null,effect_type:g.effect_type||null,label_weight:g.label_weight,...i?w?{core_weight:g.core_weight}:{}:{core_weight:g.core_weight,core_weight_catalog_id:g.core_weight_catalog_id},weight_used:g.weight_used,slicer_filament:g.slicer_filament||null,slicer_filament_name:n,nozzle_temp_min:null,nozzle_temp_max:null,note:g.note||null,cost_per_kg:g.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:g.category.trim()||null,low_stock_threshold_pct:g.low_stock_threshold_pct,material_number:g.material_number.trim()||null};z(!0);try{if(L>1){let e=i?await r.bulkCreateSpoolmanInventorySpools(o,L):await r.bulkCreateSpools(o,L),n=i&&e&&typeof e==`object`&&`created`in e?e.created:e;for(let e of n)await Pe(e.id);n.length>0&&t(n[0])}else{let e=i?await r.createSpoolmanInventorySpool(o):await r.createSpool(o);await Pe(e.id),t(e)}}catch{V(a(`spoolbuddy.writeTag.createFailed`,`Failed to create spool`))}finally{z(!1)}},Ie=`#${(g.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:()=>f(`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:()=>f(`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:g.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:g.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:Ie,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:g.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)(d,{value:g.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:Fe,disabled:R||!g.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:R?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:()=>se(e=>!e),className:`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${I?`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 ${I?`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:()=>m(`filament`),className:`flex-1 py-2.5 text-sm font-medium ${p===`filament`?`bg-bambu-green/15 text-bambu-green`:`bg-bambu-dark-secondary text-zinc-400`}`,children:a(`inventory.filamentInfoTab`,`Filament`)}),!I&&(0,j.jsx)(`button`,{onClick:()=>m(`pa-profile`),className:`flex-1 py-2.5 text-sm font-medium ${p===`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:p===`filament`?(0,j.jsxs)(`div`,{className:`space-y-4`,children:[(0,j.jsx)(y,{formData:g,updateField:$,cloudAuthenticated:H,loadingCloudPresets:le,presetInputValue:_e,setPresetInputValue:ve,selectedPresetOption:Ee,filamentOptions:Te,availableBrands:Z,availableMaterials:Q,suggestedBrands:je,suggestedMaterials:Me,quickAdd:I,detailsRequired:!I,quantity:L,onQuantityChange:ce,errors:F}),(0,j.jsx)(_,{formData:g,updateField:$,recentColors:ye,onColorUsed:Ne,catalogColors:q}),(0,j.jsx)(b,{formData:g,updateField:$,spoolCatalog:me,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)(ae,{formData:g,updateField:$,printersWithCalibrations:J,selectedProfiles:Y,setSelectedProfiles:Se,expandedPrinters:Ce,setExpandedPrinters:we})})]}),B&&(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:B}),l===`full`&&(0,j.jsx)(`button`,{onClick:Fe,disabled:R,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:R?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 se({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)(L,{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)(L,{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)(L,{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)(L,{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)(L,{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 L({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};
|