diff --git a/frontend/src/pages/FileManagerPage.tsx b/frontend/src/pages/FileManagerPage.tsx index 33dd3963c..9c3dadf97 100644 --- a/frontend/src/pages/FileManagerPage.tsx +++ b/frontend/src/pages/FileManagerPage.tsx @@ -559,7 +559,6 @@ function UploadModal({ folderId, onClose, onUploadComplete }: UploadModalProps) ref={fileInputRef} type="file" multiple - accept="*" className="hidden" onChange={handleFileSelect} /> diff --git a/static/assets/index-m8daLb5w.js b/static/assets/index-Du4KZbJd.js similarity index 98% rename from static/assets/index-m8daLb5w.js rename to static/assets/index-Du4KZbJd.js index 777f62bca..c397c9fe9 100644 --- a/static/assets/index-m8daLb5w.js +++ b/static/assets/index-Du4KZbJd.js @@ -8660,6 +8660,6 @@ Accuracy: ${t.time_accuracy.toFixed(0)}%`:t.actual_time_seconds?`Actual: ${mb(t. `+n+"]";if(typeof t=="object"){const r=Object.entries(t);return r.length===0?"{}":`{ `+r.map(([a,o])=>n+" "+JSON.stringify(a)+": "+IR(o,e+1)).join(`, `)+` -`+n+"}"}return String(t)}function fKe({setting:t,onClose:e,onDeleted:n,onDuplicate:r,onEdit:i}){const{showToast:a}=Gn(),o=an(),[l,c]=w.useState(!1),{data:u,isLoading:d}=ct({queryKey:["cloudSettingDetail",t.setting_id],queryFn:()=>pe.getCloudSettingDetail(t.setting_id)}),f=ft({mutationFn:()=>pe.deleteCloudSetting(t.setting_id),onSuccess:()=>{a("Preset deleted"),o.invalidateQueries({queryKey:["cloudSettings"]}),n()},onError:x=>a(x.message,"error")}),m=W0(t.setting_id),g=RL(t.name,u?.setting?.inherits);return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:s.jsx(Mt,{className:"w-full max-w-3xl max-h-[90vh] flex flex-col overflow-hidden",children:s.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[s.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("h2",{className:"text-xl font-semibold text-white truncate",children:t.name}),m&&s.jsx("span",{className:"px-2 py-0.5 text-xs font-medium bg-bambu-green/20 text-bambu-green rounded-full",children:"Editable"})]}),s.jsxs("div",{className:"flex items-center gap-2 mt-1 text-sm text-bambu-gray",children:[s.jsxs("span",{className:"capitalize",children:[t.type," preset"]}),g.printer&&s.jsxs(s.Fragment,{children:[s.jsx("span",{children:"•"}),s.jsx("span",{children:g.printer})]})]})]}),s.jsx("button",{onClick:e,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Bt,{className:"w-5 h-5"})})]}),s.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto p-4",children:d?s.jsx("div",{className:"flex items-center justify-center py-16",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?s.jsx("pre",{className:"text-xs text-bambu-gray font-mono whitespace-pre-wrap break-all bg-bambu-dark p-4 rounded-lg border border-bambu-dark-tertiary overflow-x-auto max-w-full",children:IR(u)}):s.jsx("div",{className:"text-center py-16 text-bambu-gray",children:"Failed to load preset details"})}),l?s.jsxs("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary bg-red-500/5",children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 text-red-400",children:[s.jsx(jn,{className:"w-5 h-5"}),s.jsx("span",{className:"font-medium",children:"Delete this preset?"})]}),s.jsxs("p",{className:"text-sm text-bambu-gray mb-4",children:['This will permanently delete "',t.name,'" from Bambu Cloud. This cannot be undone.']}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:()=>c(!1),disabled:f.isPending,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{variant:"danger",onClick:()=>f.mutate(),disabled:f.isPending,className:"flex-1",children:[f.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):s.jsx(ln,{className:"w-4 h-4"}),"Delete"]})]})]}):s.jsx("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:e,className:"flex-1",children:"Close"}),s.jsxs(Se,{variant:"secondary",onClick:r,children:[s.jsx(Jo,{className:"w-4 h-4"}),"Duplicate"]}),m&&s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"secondary",onClick:i,disabled:d||!u,children:[s.jsx(Wa,{className:"w-4 h-4"}),"Edit"]}),s.jsx(Se,{variant:"danger",onClick:()=>c(!0),children:s.jsx(ln,{className:"w-4 h-4"})})]})]})})]})})})}function fre(){try{const t=localStorage.getItem("bambusy_preset_templates");return t?JSON.parse(t):[]}catch{return[]}}function mre(t){localStorage.setItem("bambusy_preset_templates",JSON.stringify(t))}function mKe({onClose:t,onApply:e}){const{showToast:n}=Gn(),[r,i]=w.useState(fre),[a,o]=w.useState("all"),[l,c]=w.useState(null),[u,d]=w.useState(""),[f,m]=w.useState(""),[g,x]=w.useState("{}"),[y,b]=w.useState(null),[v,M]=w.useState(null),S=a==="all"?r:r.filter(L=>L.type===a),T=L=>{i(L),mre(L)},E=L=>{const X=r.filter(R=>R.id!==L);T(X),M(null),n("Template deleted")},k=L=>{c(L.id),d(L.name),m(L.description),x(JSON.stringify(L.settings,null,2)),b(null)},A=()=>{if(!(!l||!u.trim()))try{const L=JSON.parse(g),X=r.map(R=>R.id===l?{...R,name:u.trim(),description:f.trim(),settings:L}:R);T(X),c(null),n("Template updated")}catch(L){b(L.message)}},D=()=>{c(null),d(""),m(""),x("{}"),b(null)},C=L=>{const X=r.map(R=>R.id===L?{...R,showInModal:!R.showInModal}:R);T(X)},P={filament:{label:"Filament",icon:eD,color:"text-amber-400"},print:{label:"Process",icon:Dd,color:"text-blue-400"},printer:{label:"Printer",icon:zr,color:"text-purple-400"}},F=v?r.find(L=>L.id===v):null;return w.useEffect(()=>{const L=X=>{X.key==="Escape"&&(v?M(null):l?D():t())};return window.addEventListener("keydown",L),()=>window.removeEventListener("keydown",L)},[v,l,t]),s.jsxs("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:[F&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-60",children:s.jsx(Mt,{className:"w-full max-w-md",children:s.jsxs(Tt,{className:"p-6",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[s.jsx("div",{className:"p-2 bg-red-500/20 rounded-lg",children:s.jsx(jn,{className:"w-6 h-6 text-red-400"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Template"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"This action cannot be undone"})]})]}),s.jsxs("p",{className:"text-white mb-6",children:['Are you sure you want to delete "',s.jsx("span",{className:"font-medium",children:F.name}),'"?']}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:()=>M(null),className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:()=>E(v),className:"flex-1 bg-red-500 hover:bg-red-600",children:[s.jsx(ln,{className:"w-4 h-4"}),"Delete"]})]})]})})}),s.jsx(Mt,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",children:s.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(a0,{className:"w-5 h-5 text-amber-400"}),"Quick Templates"]}),s.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:s.jsx(Bt,{className:"w-5 h-5"})})]}),s.jsxs("div",{className:"flex items-center gap-2 p-4 border-b border-bambu-dark-tertiary",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Type:"}),["all","filament","print","printer"].map(L=>s.jsx("button",{onClick:()=>o(L),className:`px-3 py-1 text-sm rounded-lg transition-colors ${a===L?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:L==="all"?"All":P[L].label},L))]}),s.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:S.length===0?s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx(a0,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),s.jsx("p",{children:"No templates yet"}),s.jsx("p",{className:"text-sm mt-1",children:"Create templates from the preset editor"})]}):s.jsx("div",{className:"space-y-2",children:S.map(L=>{const X=P[L.type],R=X.icon;return l===L.id?s.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg border border-bambu-green",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3 mb-3",children:[s.jsx("input",{type:"text",value:u,onChange:q=>d(q.target.value),placeholder:"Template name",className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",autoFocus:!0}),s.jsx("input",{type:"text",value:f,onChange:q=>m(q.target.value),placeholder:"Description",className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"mb-3",children:[s.jsx("label",{className:"text-xs text-bambu-gray mb-1 block",children:"Settings (JSON)"}),s.jsx("textarea",{value:g,onChange:q=>{x(q.target.value),b(null)},rows:6,className:`w-full px-3 py-2 bg-bambu-dark-secondary border rounded text-white text-sm font-mono focus:outline-none ${y?"border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`}),y&&s.jsx("p",{className:"text-xs text-red-400 mt-1",children:y})]}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{size:"sm",onClick:A,disabled:!u.trim(),children:[s.jsx(hs,{className:"w-4 h-4"}),"Save"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:D,children:"Cancel"})]})]},L.id):s.jsxs("div",{className:"flex items-center gap-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary hover:border-bambu-gray-dark transition-colors",children:[s.jsx(R,{className:`w-5 h-5 ${X.color} flex-shrink-0`}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("p",{className:"text-sm font-medium text-white",children:L.name}),s.jsx("p",{className:"text-xs text-bambu-gray truncate",children:L.description})]}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark px-2 py-1 bg-bambu-dark-secondary rounded",children:[Object.keys(L.settings).length," fields"]}),s.jsx("button",{onClick:()=>C(L.id),className:`p-1 transition-colors ${L.showInModal?"text-bambu-green hover:text-bambu-green/70":"text-bambu-gray hover:text-white"}`,title:L.showInModal?"Shown in modals":"Hidden in modals",children:L.showInModal?s.jsx(_f,{className:"w-4 h-4"}):s.jsx(A2,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>e(L),className:"px-3 py-1 text-xs bg-bambu-green/20 text-bambu-green rounded hover:bg-bambu-green/30 transition-colors",children:"Apply"}),s.jsx("button",{onClick:()=>k(L),className:"p-1 text-bambu-gray hover:text-white",title:"Edit",children:s.jsx(Wa,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>M(L.id),className:"p-1 text-bambu-gray hover:text-red-400",title:"Delete",children:s.jsx(ln,{className:"w-4 h-4"})})]},L.id)})})})]})})]})}function pre({onClose:t,leftPreset:e,rightPreset:n,leftLabel:r,rightLabel:i}){const[a,o]=w.useState("changes"),[l,c]=w.useState(""),u=w.useMemo(()=>{const g=new Set([...Object.keys(e),...Object.keys(n)]),x=[];for(const y of g){if(y==="inherits"||y==="version")continue;const b=e[y],v=n[y],M=y in e,S=y in n;let T;!M&&S?T="added":M&&!S?T="removed":JSON.stringify(b)!==JSON.stringify(v)?T="changed":T="same",x.push({key:y,left:b,right:v,status:T})}return x.sort((y,b)=>{const v={changed:0,added:1,removed:2,same:3};return v[y.status]!==v[b.status]?v[y.status]-v[b.status]:y.key.localeCompare(b.key)})},[e,n]),d=w.useMemo(()=>{let g=[...u];if(a==="changes"&&(g=g.filter(x=>x.status!=="same")),l){const x=l.toLowerCase();g=g.filter(y=>y.key.toLowerCase().includes(x)||String(y.left).toLowerCase().includes(x)||String(y.right).toLowerCase().includes(x))}return g},[u,a,l]),f=w.useMemo(()=>({added:u.filter(g=>g.status==="added").length,removed:u.filter(g=>g.status==="removed").length,changed:u.filter(g=>g.status==="changed").length,same:u.filter(g=>g.status==="same").length}),[u]),m=g=>{if(g===void 0)return"—";if(g===null)return"null";if(Array.isArray(g))return g.length===0?"[]":g.length===1?String(g[0]):g.join(", ");if(typeof g=="object")return JSON.stringify(g);const x=String(g);if(x.includes("\\n")||x.length>100){const y=x.split("\\n").length;if(y>1)return`[${y} lines of G-code/script]`;if(x.length>100)return x.substring(0,100)+"…"}return x};return w.useEffect(()=>{const g=x=>{x.key==="Escape"&&t()};return window.addEventListener("keydown",g),()=>window.removeEventListener("keydown",g)},[t]),s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsx(Mt,{className:"w-full max-w-4xl max-h-[85vh] flex flex-col overflow-hidden",children:s.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[s.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Rp,{className:"w-5 h-5 text-blue-400"}),"Compare Presets"]}),s.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:s.jsx(Bt,{className:"w-5 h-5"})})]}),s.jsxs("div",{className:"flex-shrink-0 grid grid-cols-2 gap-4 p-4 border-b border-bambu-dark-tertiary bg-bambu-dark",children:[s.jsxs("div",{className:"text-center",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Left:"}),s.jsx("p",{className:"text-white font-medium truncate",children:r})]}),s.jsxs("div",{className:"text-center",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Right:"}),s.jsx("p",{className:"text-white font-medium truncate",children:i})]})]}),s.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[s.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[s.jsx(Jn,{className:"w-3.5 h-3.5"}),f.added," added"]}),s.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[s.jsx(Hj,{className:"w-3.5 h-3.5"}),f.removed," removed"]}),s.jsxs("span",{className:"flex items-center gap-1 text-amber-400",children:[s.jsx(Ij,{className:"w-3.5 h-3.5"}),f.changed," changed"]}),s.jsxs("span",{className:"flex items-center gap-1 text-bambu-gray",children:[s.jsx(CT,{className:"w-3.5 h-3.5"}),f.same," same"]})]}),s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsxs("div",{className:"relative",children:[s.jsx(ba,{className:"absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:l,onChange:g=>c(g.target.value),placeholder:"Search fields...",className:"pl-8 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none w-48"})]}),f.same>0&&s.jsxs("div",{className:"flex rounded overflow-hidden border border-bambu-dark-tertiary",children:[s.jsx("button",{onClick:()=>o("changes"),className:`px-3 py-1.5 text-sm transition-colors ${a==="changes"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:"Changes"}),s.jsx("button",{onClick:()=>o("all"),className:`px-3 py-1.5 text-sm transition-colors ${a==="all"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:"All"})]})]})]}),s.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto",children:d.length===0?s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx(CT,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),s.jsx("p",{children:a==="changes"?"No differences found":"No fields match the search"})]}):s.jsxs("table",{className:"w-full",children:[s.jsx("thead",{className:"sticky top-0 bg-bambu-dark-secondary",children:s.jsxs("tr",{className:"text-sm text-bambu-gray border-b border-bambu-dark-tertiary",children:[s.jsx("th",{className:"text-left p-3 w-1/3",children:"Field"}),s.jsx("th",{className:"text-left p-3 w-1/3",children:r}),s.jsx("th",{className:"text-left p-3 w-1/3",children:i})]})}),s.jsx("tbody",{children:d.map(g=>{const x={added:"bg-green-500/10",removed:"bg-red-500/10",changed:"bg-amber-500/10",same:""}[g.status],y={added:s.jsx(Jn,{className:"w-3.5 h-3.5 text-green-400"}),removed:s.jsx(Hj,{className:"w-3.5 h-3.5 text-red-400"}),changed:s.jsx(Ij,{className:"w-3.5 h-3.5 text-amber-400"}),same:s.jsx(CT,{className:"w-3.5 h-3.5 text-bambu-gray-dark"})}[g.status];return s.jsxs("tr",{className:`border-b border-bambu-dark-tertiary ${x}`,children:[s.jsx("td",{className:"p-3",children:s.jsxs("div",{className:"flex items-center gap-2",children:[y,s.jsx("span",{className:"text-sm text-white font-mono",children:g.key})]})}),s.jsx("td",{className:"p-3",children:s.jsx("span",{className:`text-sm font-mono break-all ${g.status==="removed"?"text-red-300":g.status==="changed"?"text-white":"text-bambu-gray"}`,children:m(g.left)})}),s.jsx("td",{className:"p-3",children:s.jsx("span",{className:`text-sm font-mono break-all ${g.status==="added"?"text-green-300":g.status==="changed"?"text-white":"text-bambu-gray"}`,children:m(g.right)})})]},g.key)})})]})})]})})})}function pKe({onClose:t,initialData:e,allPresets:n}){const{showToast:r}=Gn(),i=an(),a=!!e?.setting_id,[o,l]=w.useState("common"),[c,u]=w.useState(e?.type||"filament"),[d,f]=w.useState(e?.name?a?e.name:`${e.name} (Copy)`:""),[m,g]=w.useState(e?.base_id||""),[x,y]=w.useState(""),[b,v]=w.useState(e?.setting||{inherits:""}),[M,S]=w.useState(JSON.stringify(e?.setting||{inherits:""},null,2)),[T,E]=w.useState(null),[k,A]=w.useState(""),[D,C]=w.useState(!1),[P,F]=w.useState(""),[L,X]=w.useState(!1),[R,q]=w.useState(fre),[Y,W]=w.useState(!1),[Z,ie]=w.useState(""),[ae,U]=w.useState(""),[G,ee]=w.useState(!0),[re,le]=w.useState(null),[he,fe]=w.useState(!1),Te=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer})[c]||[],[n,c]),ge=w.useMemo(()=>Te.filter(ce=>W0(ce.setting_id)),[Te]),{data:_e}=ct({queryKey:["cloudFields",c],queryFn:()=>pe.getCloudFields(c==="print"?"process":c),staleTime:1e3*60*60}),{data:je}=ct({queryKey:["allPresetDetails",c,ge.map(ce=>ce.setting_id).join(",")],queryFn:async()=>{const ce={};for(let $e=0;$e{try{const He=await pe.getCloudSettingDetail(se.setting_id);return{id:se.setting_id,detail:He}}catch{return null}}))).forEach(se=>{se&&(ce[se.id]=se.detail)})}return ce},enabled:ge.length>0,staleTime:1e3*60*10}),{data:ne,isLoading:Qe}=ct({queryKey:["cloudSettingDetail",m],queryFn:()=>pe.getCloudSettingDetail(m),enabled:!!m});w.useEffect(()=>{o!=="json"&&S(JSON.stringify(b,null,2))},[b,o]);const B=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer}[c]||[]).filter(Ve=>!W0(Ve.setting_id)).sort((Ve,$e)=>Ve.name.localeCompare($e.name)),[n,c]);w.useEffect(()=>{if(!m)return;const ce=B.find(Ve=>Ve.setting_id===m);ce&&(y(ce.name),a||(v({inherits:ce.name}),S(JSON.stringify({inherits:ce.name},null,2))))},[m,B,a]);const H=w.useMemo(()=>{const ce=_e?.fields||[],Ve=new Set(ce.map(se=>se.key)),$e=new Set,qe=new Set(["inherits","updated_time","compatible_printers","compatible_prints"]);je&&Object.values(je).forEach(se=>{se?.setting&&Object.keys(se.setting).forEach(He=>{!Ve.has(He)&&!qe.has(He)&&$e.add(He)})}),Object.keys(b).forEach(se=>{!Ve.has(se)&&!qe.has(se)&&$e.add(se)});const it=Array.from($e).sort().map(se=>({key:se,label:se.replace(/_/g," ").replace(/\b\w/g,He=>He.toUpperCase()),type:"text",category:"discovered",description:"Discovered from presets"}));return[...ce,...it]},[_e,je,b]),K=H.filter(ce=>ce.label.toLowerCase().includes(k.toLowerCase())||ce.key.toLowerCase().includes(k.toLowerCase())),z=()=>{if(P.trim()){const ce=P.trim().toLowerCase().replace(/\s+/g,"_");V(ce,""),F(""),X(!1),r(`Field "${ce}" added`)}},V=(ce,Ve)=>{v($e=>{const qe={...$e};return Ve===""||Ve===void 0?delete qe[ce]:qe[ce]=Ve,qe})},Q=ce=>{v(Ve=>({...Ve,...ce.settings})),le(ce.name),r("Template applied")},Ne=()=>{if(!Z.trim())return;const ce={...b};if(delete ce.inherits,Object.keys(ce).length===0){r("No overrides to save","error");return}const Ve={id:Date.now().toString(),name:Z.trim(),description:ae.trim()||"Custom template",type:c,settings:ce,showInModal:G},$e=[...R,Ve];q($e),mre($e),W(!1),ie(""),U(""),ee(!0),r("Template saved")},ke=w.useMemo(()=>R.filter(ce=>ce.type===c&&ce.showInModal),[c,R]),Me=ce=>{S(ce);try{const Ve=JSON.parse(ce);v(Ve),E(null)}catch(Ve){E(Ve.message)}},$=ce=>{ce.preventDefault(),C(!1);const Ve=ce.dataTransfer.files[0];if(Ve&&Ve.name.endsWith(".json")){const $e=new FileReader;$e.onload=qe=>{try{const it=qe.target?.result,se=JSON.parse(it),He=se.setting||se;v(ye=>({...ye,...He})),S(JSON.stringify({...b,...He},null,2)),r("File imported successfully")}catch{r("Invalid JSON file","error")}},$e.readAsText(Ve)}},I=ft({mutationFn:()=>{const ce={...b},Ve=c==="filament"?"filament_settings_id":c==="print"?"print_settings_id":"printer_settings_id";ce[Ve]=`"${d}"`;const $e={type:c,name:d,base_id:m,setting:ce};return pe.createCloudSetting($e)},onSuccess:async()=>{r("Preset created successfully"),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ce=>r(ce.message,"error")}),J=ft({mutationFn:()=>{if(!e?.setting_id)throw new Error("No setting ID for update");return pe.updateCloudSetting(e.setting_id,{name:d,setting:b})},onSuccess:async()=>{r("Preset updated successfully"),i.removeQueries({queryKey:["cloudSettingDetail"]}),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ce=>r(ce.message,"error")}),xe=a?J:I,ve=ne?.setting?.inherits,oe=ve?B.find(ce=>ce.name===ve):void 0,{data:Ae}=ct({queryKey:["cloudSettingDetail",oe?.setting_id],queryFn:()=>pe.getCloudSettingDetail(oe.setting_id),enabled:!!oe?.setting_id}),De=w.useMemo(()=>{const ce=Ae?.setting||{},Ve=ne?.setting||{},$e=m&&je?.[m]?.setting?je[m].setting:{};return{...ce,...Ve,...$e}},[m,ne,Ae,je]),Ze=ce=>{if(ce==null)return"";if(Array.isArray(ce)){const Ve=[...new Set(ce.map($e=>String($e)))];return Ve.length===1?Ve[0]:ce.join(", ")}return String(ce)},ut=ce=>{const Ve=b[ce.key],$e=De[ce.key],qe=Ze($e),it=Qe?"Loading...":qe||"",se="w-full px-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none";if(ce.type==="boolean"){const He=Ve==="1"||Ve===void 0&&$e==="1";return s.jsx("button",{type:"button",onClick:()=>V(ce.key,Ve==="1"?"0":"1"),className:`w-8 h-5 rounded-full transition-colors ${He?"bg-bambu-green":"bg-bambu-dark-tertiary"}`,children:s.jsx("div",{className:`w-4 h-4 rounded-full bg-white shadow transition-transform ${He?"translate-x-3.5":"translate-x-0.5"}`})})}return ce.type==="select"?s.jsxs("select",{value:Ve||"",onChange:He=>V(ce.key,He.target.value),className:se,children:[s.jsx("option",{value:"",children:it}),ce.options?.map(He=>s.jsx("option",{value:He.value,children:He.label},He.value))]}):s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:ce.type==="number"?"number":"text",value:Ve!==void 0?String(Ve):"",onChange:He=>V(ce.key,He.target.value),step:ce.step,min:ce.min,max:ce.max,placeholder:it,className:se}),ce.unit&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:ce.unit})]})},Ye=w.useMemo(()=>ne?.setting?ne.setting:{},[ne]);return s.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onDragOver:ce=>{ce.preventDefault(),C(!0)},onDragLeave:()=>C(!1),onDrop:$,children:[he&&m&&s.jsx(pre,{onClose:()=>fe(!1),leftPreset:Ye,rightPreset:b,leftLabel:`Base: ${x||m}`,rightLabel:`Current: ${d||"New Preset"}`}),s.jsx(Mt,{className:"w-full max-w-6xl max-h-[90vh] flex flex-col",children:s.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("h2",{className:"text-xl font-semibold text-white",children:a?"Edit Preset":e?"Duplicate Preset":"Create New Preset"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize settings for your new preset"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[m&&s.jsxs("button",{onClick:()=>fe(!0),className:"flex items-center gap-2 px-3 py-2 text-sm text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",title:"Compare with base preset",children:[s.jsx(Rp,{className:"w-4 h-4"}),"Compare"]}),s.jsx("button",{onClick:t,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Bt,{className:"w-5 h-5"})})]})]}),D&&s.jsx("div",{className:"absolute inset-0 bg-bambu-green/10 border-2 border-dashed border-bambu-green rounded-lg flex items-center justify-center z-10",children:s.jsxs("div",{className:"text-center",children:[s.jsx(yi,{className:"w-12 h-12 text-bambu-green mx-auto mb-2"}),s.jsx("p",{className:"text-bambu-green font-medium",children:"Drop JSON file to import"})]})}),s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Type"}),s.jsxs("select",{value:c,onChange:ce=>{u(ce.target.value),g("")},className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"filament",children:"Filament"}),s.jsx("option",{value:"print",children:"Process"}),s.jsx("option",{value:"printer",children:"Printer"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Base Preset"}),s.jsxs("select",{value:m,onChange:ce=>g(ce.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"",children:"Select base preset..."}),B.map(ce=>s.jsx("option",{value:ce.setting_id,children:ce.name},ce.setting_id))]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Preset Name"}),s.jsx("input",{type:"text",value:d,onChange:ce=>f(ce.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",placeholder:"My Custom Preset"})]})]}),x&&s.jsx("div",{className:"text-xs text-bambu-gray",children:s.jsxs("p",{className:"flex items-center gap-1",children:[s.jsx(bs,{className:"w-3 h-3 text-bambu-green"}),"Inherits from: ",s.jsx("span",{className:"text-white",children:x}),Qe&&s.jsx(ht,{className:"w-3 h-3 animate-spin ml-1"})]})})]}),s.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary",children:[s.jsxs("button",{onClick:()=>l("common"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${o==="common"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(fz,{className:"w-4 h-4"}),"Common"]}),s.jsxs("button",{onClick:()=>l("fields"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${o==="fields"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(O2,{className:"w-4 h-4"}),"All Fields"]}),s.jsxs("button",{onClick:()=>l("json"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${o==="json"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(VW,{className:"w-4 h-4"}),"JSON",T&&s.jsx(ta,{className:"w-3 h-3 text-red-400"})]}),s.jsx("div",{className:"flex-1"}),s.jsxs("button",{onClick:()=>{const ce={name:d,type:c,base_id:m,setting:b},Ve=new Blob([JSON.stringify(ce,null,2)],{type:"application/json"}),$e=URL.createObjectURL(Ve),qe=document.createElement("a");qe.href=$e,qe.download=`${d||"preset"}.json`,document.body.appendChild(qe),qe.click(),document.body.removeChild(qe),URL.revokeObjectURL($e),r("Preset exported")},className:"flex items-center gap-2 px-4 py-3 text-sm text-bambu-gray hover:text-white transition-colors",title:"Export current settings to JSON file",children:[s.jsx(ei,{className:"w-4 h-4"}),"Export"]}),s.jsxs("button",{onClick:()=>document.getElementById("file-import")?.click(),className:"flex items-center gap-2 px-4 py-3 text-sm text-bambu-gray hover:text-white transition-colors",title:"Import settings from JSON file",children:[s.jsx(yi,{className:"w-4 h-4"}),"Import"]}),s.jsx("input",{id:"file-import",type:"file",accept:".json",className:"hidden",onChange:ce=>{const Ve=ce.target.files?.[0];if(Ve){const $e=new FileReader;$e.onload=qe=>{try{const it=JSON.parse(qe.target?.result),se=it.setting||it;v(He=>({...He,...se})),r("File imported")}catch{r("Invalid JSON","error")}},$e.readAsText(Ve)}}})]}),s.jsxs("div",{className:"flex-1 overflow-y-auto p-4",children:[o==="common"&&s.jsxs("div",{className:"space-y-6",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:[s.jsx(a0,{className:"w-4 h-4 text-amber-400"}),"Quick Templates"]}),Object.keys(b).filter(ce=>ce!=="inherits").length>0&&s.jsxs("button",{onClick:()=>W(!Y),className:"text-xs text-bambu-gray hover:text-white flex items-center gap-1 transition-colors",children:[s.jsx(hs,{className:"w-3 h-3"}),"Save as template"]})]}),Y&&s.jsxs("div",{className:"mb-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-2 mb-2",children:[s.jsx("input",{type:"text",value:Z,onChange:ce=>ie(ce.target.value),placeholder:"Template name",className:"px-3 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",autoFocus:!0}),s.jsx("input",{type:"text",value:ae,onChange:ce=>U(ce.target.value),placeholder:"Description (optional)",className:"px-3 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{size:"sm",onClick:Ne,disabled:!Z.trim(),children:[s.jsx(hs,{className:"w-3 h-3"}),"Save"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>W(!1),children:"Cancel"})]}),s.jsxs("button",{onClick:()=>ee(!G),className:`flex items-center gap-1.5 text-xs transition-colors ${G?"text-bambu-green":"text-bambu-gray hover:text-white"}`,children:[G?s.jsx(_f,{className:"w-3.5 h-3.5"}):s.jsx(A2,{className:"w-3.5 h-3.5"}),G?"Show in modals":"Hidden in modals"]})]})]}),re&&s.jsxs("div",{className:"mb-3 px-3 py-2 bg-bambu-green/10 border border-bambu-green/30 rounded-lg flex items-center gap-2",children:[s.jsx(bs,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("span",{className:"text-sm text-bambu-green",children:["Template applied: ",s.jsx("span",{className:"font-medium",children:re})]}),s.jsx("button",{onClick:()=>le(null),className:"ml-auto text-bambu-green/70 hover:text-bambu-green",children:s.jsx(Bt,{className:"w-4 h-4"})})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[ke.map(ce=>s.jsxs("button",{onClick:()=>Q(ce),className:"p-3 text-left bg-bambu-dark border border-bambu-dark-tertiary rounded-lg hover:border-bambu-gray-dark transition-colors",children:[s.jsx("p",{className:"text-sm font-medium text-white",children:ce.name}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:ce.description})]},ce.id)),ke.length===0&&s.jsx("p",{className:"col-span-3 text-center text-bambu-gray text-sm py-4",children:"No templates selected. Use the Templates button to enable templates for quick access."})]}),s.jsx("p",{className:"text-xs text-bambu-gray-dark mt-2 text-center",children:"Manage templates via the Templates button on the main page"})]}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Common Settings"}),s.jsx("div",{className:"grid grid-cols-2 gap-x-6 gap-y-3",children:H.slice(0,10).map(ce=>s.jsxs("div",{className:"flex items-center justify-between gap-4",children:[s.jsx("label",{className:"text-sm text-bambu-gray flex-shrink-0",children:ce.label}),s.jsx("div",{className:"w-48",children:ut(ce)})]},ce.key))})]}),Object.keys(b).length>1&&s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Current Overrides"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:Object.entries(b).filter(([ce])=>ce!=="inherits").map(([ce,Ve])=>s.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-green/10 text-bambu-green text-xs rounded",children:[ce,": ",String(Ve).slice(0,20),s.jsx("button",{onClick:()=>V(ce,void 0),className:"hover:text-white",children:s.jsx(Bt,{className:"w-3 h-3"})})]},ce))})]})]}),o==="fields"&&s.jsxs("div",{className:"grid grid-cols-2 gap-6",style:{height:"400px"},children:[s.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[s.jsx("h3",{className:"text-sm font-medium text-white",children:"Available Fields"}),s.jsx("span",{className:"text-xs text-bambu-gray",children:je?`${H.length} fields`:"Loading..."})]}),s.jsxs("div",{className:"relative mb-3 flex-shrink-0",children:[s.jsx(ba,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:k,onChange:ce=>A(ce.target.value),placeholder:"Search fields...",className:"w-full pl-10 pr-4 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"flex-1 overflow-y-auto space-y-1 pr-2 min-h-0",children:[K.filter(ce=>!(ce.key in b)).map(ce=>{const Ve=De[ce.key],$e=Ze(Ve);return s.jsxs("div",{onClick:()=>{v(qe=>({...qe,[ce.key]:$e||""}))},className:"flex items-center justify-between gap-2 p-2 rounded-lg hover:bg-bambu-dark-tertiary transition-colors cursor-pointer group",children:[s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsx("p",{className:"text-sm text-white truncate",children:ce.label}),s.jsx("p",{className:"text-xs text-bambu-gray-dark truncate",children:ce.key})]}),s.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[$e&&s.jsxs("span",{className:"text-xs text-bambu-gray bg-bambu-dark px-2 py-0.5 rounded max-w-32 truncate",title:$e,children:[$e.slice(0,20),$e.length>20?"...":""]}),s.jsx("div",{className:"w-6 h-6 flex items-center justify-center rounded bg-bambu-dark-tertiary group-hover:bg-bambu-green/20 transition-colors",children:s.jsx(Jn,{className:"w-4 h-4 text-bambu-gray group-hover:text-bambu-green transition-colors"})})]})]},ce.key)}),K.filter(ce=>!(ce.key in b)).length===0&&s.jsx("p",{className:"text-center text-bambu-gray py-4 text-sm",children:k?"No matching fields":"All fields added"})]}),s.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:L?s.jsxs("div",{className:"flex gap-2",children:[s.jsx("input",{type:"text",value:P,onChange:ce=>F(ce.target.value),onKeyDown:ce=>ce.key==="Enter"&&z(),placeholder:"custom_field_name",className:"flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white font-mono text-sm placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none",autoFocus:!0}),s.jsx(Se,{size:"sm",onClick:z,disabled:!P.trim(),children:s.jsx(Jn,{className:"w-4 h-4"})}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>{X(!1),F("")},children:s.jsx(Bt,{className:"w-4 h-4"})})]}):s.jsxs("button",{onClick:()=>X(!0),className:"w-full flex items-center justify-center gap-2 p-2 text-sm text-bambu-gray hover:text-white border border-dashed border-bambu-dark-tertiary hover:border-bambu-gray-dark rounded-lg transition-colors",children:[s.jsx(Jn,{className:"w-4 h-4"}),"Add custom field"]})})]}),s.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[s.jsx("h3",{className:"text-sm font-medium text-white",children:"Your Overrides"}),s.jsxs("span",{className:"text-xs text-bambu-gray",children:[Object.keys(b).filter(ce=>ce!=="inherits").length," fields"]})]}),s.jsxs("div",{className:"flex-1 overflow-y-auto space-y-2 pr-2 min-h-0",children:[Object.entries(b).filter(([ce])=>ce!=="inherits").map(([ce,Ve])=>{const $e=H.find(qe=>qe.key===ce);return s.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm font-medium text-white",children:$e?.label||ce}),s.jsx("p",{className:"text-xs text-bambu-gray-dark",children:ce})]}),s.jsx("button",{onClick:()=>V(ce,void 0),className:"p-1 text-bambu-gray hover:text-red-400 transition-colors",children:s.jsx(Bt,{className:"w-4 h-4"})})]}),$e?ut($e):s.jsx("input",{type:"text",value:String(Ve),onChange:qe=>V(ce,qe.target.value),className:"w-full px-3 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"})]},ce)}),Object.keys(b).filter(ce=>ce!=="inherits").length===0&&s.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[s.jsx(fz,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),s.jsx("p",{className:"text-sm",children:"No overrides yet"}),s.jsx("p",{className:"text-xs mt-1",children:"Click fields on the left to add them"})]})]}),Object.keys(b).filter(ce=>ce!=="inherits").length>0&&s.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:s.jsxs("button",{onClick:()=>{W(!0),l("common")},className:"w-full flex items-center justify-center gap-2 p-2 text-sm text-bambu-gray hover:text-white border border-dashed border-bambu-dark-tertiary hover:border-bambu-gray-dark rounded-lg transition-colors",children:[s.jsx(hs,{className:"w-4 h-4"}),"Save as template"]})})]})]}),o==="json"&&s.jsxs("div",{className:"space-y-2",children:[T&&s.jsxs("div",{className:"flex items-center gap-2 text-red-400 text-sm",children:[s.jsx(ta,{className:"w-4 h-4"}),T]}),s.jsx("textarea",{value:M,onChange:ce=>Me(ce.target.value),className:`w-full h-80 px-3 py-2 bg-bambu-dark border rounded-lg text-white text-xs font-mono focus:outline-none resize-none ${T?"border-red-500 focus:border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`,spellCheck:!1}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Tip: Drag & drop a .json file anywhere on this modal to import settings"})]})]}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:t,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:()=>xe.mutate(),disabled:xe.isPending||!d.trim()||!a&&!m||!!T,className:"flex-1",children:[xe.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):a?s.jsx(hs,{className:"w-4 h-4"}):s.jsx(Jn,{className:"w-4 h-4"}),a?"Save":e?"Duplicate":"Create"]})]})]})})]})}function gKe({settings:t,lastSyncTime:e,onRefresh:n,isRefreshing:r,printers:i}){const[a,o]=w.useState(""),[l,c]=w.useState("all"),[u,d]=w.useState("all"),[f,m]=w.useState("all"),[g,x]=w.useState("all"),[y,b]=w.useState("all"),[v,M]=w.useState("all"),[S,T]=w.useState(null),[E,k]=w.useState(!1),[A,D]=w.useState(!1),[C,P]=w.useState(null),[F,L]=w.useState(null),[X,R]=w.useState(null),[q,Y]=w.useState(!1),[W,Z]=w.useState([null,null]),[ie,ae]=w.useState(!1),[U,G]=w.useState(null),ee=an(),re=w.useMemo(()=>[...t.filament.map(K=>({...K,type:"filament"})),...t.printer.map(K=>({...K,type:"printer"})),...t.process.map(K=>({...K,type:"process"}))].map(K=>({...K,meta:RL(K.name)})),[t]),le=w.useMemo(()=>{const H=new Set,K=new Set,z=new Set;return re.forEach(V=>{V.meta.nozzle&&H.add(V.meta.nozzle),V.meta.filamentType&&K.add(V.meta.filamentType),V.meta.layerHeight&&z.add(V.meta.layerHeight)}),{printers:i.map(V=>({id:V.id.toString(),name:V.name})),nozzles:Array.from(H).sort((V,Q)=>parseFloat(V)-parseFloat(Q)),filaments:Array.from(K).sort(),layerHeights:Array.from(z).sort((V,Q)=>parseFloat(V)-parseFloat(Q))}},[re,i]),he=w.useMemo(()=>f==="all"?null:i.find(K=>K.id.toString()===f)?.model||null,[f,i]),fe=w.useMemo(()=>re.filter(H=>l==="all"||H.type===l).filter(H=>{if(u==="all")return!0;const K=W0(H.setting_id);return u==="custom"?K:!K}).filter(H=>{if(f==="all"||!he)return!0;const K=H.meta.printer?.toLowerCase()||"",z=he.toLowerCase();return K.includes(z)||z.includes(K)}).filter(H=>g==="all"||H.meta.nozzle===g).filter(H=>y==="all"||H.meta.filamentType===y).filter(H=>v==="all"||H.meta.layerHeight===v).filter(H=>a===""||H.name.toLowerCase().includes(a.toLowerCase())).sort((H,K)=>H.name.localeCompare(K.name)),[re,l,u,f,he,g,y,v,a]),Te=H=>{if(q){const K=W[0]?.setting_id===H.setting_id,z=W[1]?.setting_id===H.setting_id;if(K)Z([W[1],null]);else if(z)Z([W[0],null]);else if(!W[0])Z([H,null]);else if(W[1]){if(W[0].type!==H.type)return;Z([W[0],H])}else{if(W[0].type!==H.type)return;Z([W[0],H])}}else T(H)},ge=H=>{if(W[0]?.setting_id===H.setting_id)return 0;if(W[1]?.setting_id===H.setting_id)return 1},_e=async H=>{try{const K=await pe.getCloudSettingDetail(H.setting_id),z=H.type==="process"?"print":H.type;P({type:z,name:H.name,base_id:K.base_id||"GFSA00",setting:K.setting||{}}),T(null)}catch(K){console.error("Failed to fetch preset details for duplication:",K)}},je=async H=>{try{ee.removeQueries({queryKey:["cloudSettingDetail",H.setting_id]});const K=await pe.getCloudSettingDetail(H.setting_id),z=H.type==="process"?"print":H.type;L({type:z,name:H.name,base_id:K.base_id||"GFSA00",setting:K.setting||{},setting_id:H.setting_id}),T(null)}catch(K){console.error("Failed to fetch preset details for editing:",K)}},ne=()=>{c("all"),d("all"),m("all"),x("all"),b("all"),M("all"),o("")},Qe=l!=="all"||u!=="all"||f!=="all"||g!=="all"||y!=="all"||v!=="all"||a!=="",B=t.filament.length+t.printer.length+t.process.length;return s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"space-y-4 mb-6",children:[s.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsx(ba,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:a,onChange:H=>o(H.target.value),placeholder:"Search presets...",className:"w-full pl-10 pr-4 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{variant:q?"primary":"secondary",onClick:()=>{q?(Y(!1),Z([null,null])):Y(!0)},children:[s.jsx(Rp,{className:"w-4 h-4"}),q?"Cancel":"Compare"]}),s.jsxs(Se,{variant:"secondary",onClick:()=>D(!0),children:[s.jsx(a0,{className:"w-4 h-4"}),"Templates"]}),s.jsxs(Se,{variant:"secondary",onClick:n,disabled:r,children:[s.jsx(Mr,{className:`w-4 h-4 ${r?"animate-spin":""}`}),"Refresh"]}),s.jsxs(Se,{onClick:()=>k(!0),children:[s.jsx(Jn,{className:"w-4 h-4"}),"New Preset"]})]})]}),s.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[s.jsx(k2,{className:"w-4 h-4 text-bambu-gray"}),s.jsx(xp,{label:"Type",value:l,options:[{value:"all",label:"All",count:B},{value:"filament",label:"Filament",count:t.filament.length},{value:"printer",label:"Printer",count:t.printer.length},{value:"process",label:"Process",count:t.process.length}],onChange:H=>c(H)}),s.jsx(xp,{label:"Owner",value:u,options:[{value:"all",label:"All"},{value:"custom",label:"My Presets"},{value:"builtin",label:"Built-in"}],onChange:H=>d(H)}),le.printers.length>0&&s.jsx(xp,{label:"Printer",value:f,options:[{value:"all",label:"All"},...le.printers.map(H=>({value:H.id,label:H.name}))],onChange:m}),le.nozzles.length>0&&s.jsx(xp,{label:"Nozzle",value:g,options:[{value:"all",label:"All"},...le.nozzles.map(H=>({value:H,label:H}))],onChange:x}),le.filaments.length>0&&(l==="all"||l==="filament")&&s.jsx(xp,{label:"Filament",value:y,options:[{value:"all",label:"All"},...le.filaments.map(H=>({value:H,label:H}))],onChange:b}),le.layerHeights.length>0&&(l==="all"||l==="process")&&s.jsx(xp,{label:"Layer",value:v,options:[{value:"all",label:"All"},...le.layerHeights.map(H=>({value:H,label:H}))],onChange:M}),Qe&&s.jsx("button",{onClick:ne,className:"px-3 py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:"Clear filters"})]})]}),q&&s.jsx("div",{className:"mb-4 p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx(Rp,{className:"w-5 h-5 text-blue-400"}),s.jsx("span",{className:"text-white font-medium",children:"Compare Mode"}),s.jsx("span",{className:"text-bambu-gray",children:W[0]?`Select another ${W[0].type} preset`:"Click two presets of the same type to compare"})]}),s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${W[0]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:W[0]?W[0].name:"1. Select first"}),s.jsx(Ij,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${W[1]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:W[1]?W[1].name:"2. Select second"})]}),W[0]&&W[1]&&s.jsxs(Se,{size:"sm",onClick:async()=>{try{const[H,K]=await Promise.all([pe.getCloudSettingDetail(W[0].setting_id),pe.getCloudSettingDetail(W[1].setting_id)]);G([H.setting||{},K.setting||{}]),ae(!0)}catch{}},children:[s.jsx(Rp,{className:"w-4 h-4"}),"Compare Now"]})]})]})}),s.jsxs("div",{className:"flex flex-wrap items-center gap-4 mb-4 text-sm text-bambu-gray",children:[e&&s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx(Yn,{className:"w-3 h-3"}),"Last synced: ",uKe(e.toISOString())]}),s.jsxs("span",{children:["Showing ",fe.length," of ",B," presets"]}),s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx("span",{className:"w-1.5 h-1.5 rounded-full bg-bambu-green"}),s.jsx("span",{children:"= My preset (editable)"})]})]}),fe.length===0?s.jsxs("div",{className:"text-center py-16",children:[s.jsx(nl,{className:"w-12 h-12 text-bambu-gray-dark mx-auto mb-4"}),s.jsx("p",{className:"text-bambu-gray",children:"No presets found"}),Qe&&s.jsx("button",{onClick:ne,className:"mt-2 text-sm text-bambu-green hover:text-bambu-green-light",children:"Clear filters"})]}):s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[s.jsx(eD,{className:"w-4 h-4 text-amber-400"}),s.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Filament"}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",fe.filter(H=>H.type==="filament").length,")"]})]}),s.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[fe.filter(H=>H.type==="filament").map(H=>s.jsx(bj,{setting:H,onClick:()=>Te(H),onDuplicate:()=>_e(H),compareMode:q,isCompareSelected:ge(H)!==void 0,compareIndex:ge(H),compareDisabled:q&&!!W[0]&&W[0].type!==H.type},H.setting_id)),fe.filter(H=>H.type==="filament").length===0&&s.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No filament presets"})]})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[s.jsx(Dd,{className:"w-4 h-4 text-blue-400"}),s.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Process"}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",fe.filter(H=>H.type==="process").length,")"]})]}),s.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[fe.filter(H=>H.type==="process").map(H=>s.jsx(bj,{setting:H,onClick:()=>Te(H),onDuplicate:()=>_e(H),compareMode:q,isCompareSelected:ge(H)!==void 0,compareIndex:ge(H),compareDisabled:q&&!!W[0]&&W[0].type!==H.type},H.setting_id)),fe.filter(H=>H.type==="process").length===0&&s.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No process presets"})]})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[s.jsx(zr,{className:"w-4 h-4 text-purple-400"}),s.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Printer"}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",fe.filter(H=>H.type==="printer").length,")"]})]}),s.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[fe.filter(H=>H.type==="printer").map(H=>s.jsx(bj,{setting:H,onClick:()=>Te(H),onDuplicate:()=>_e(H),compareMode:q,isCompareSelected:ge(H)!==void 0,compareIndex:ge(H),compareDisabled:q&&!!W[0]&&W[0].type!==H.type},H.setting_id)),fe.filter(H=>H.type==="printer").length===0&&s.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No printer presets"})]})]})]}),S&&s.jsx(fKe,{setting:S,onClose:()=>T(null),onDeleted:()=>T(null),onDuplicate:()=>_e(S),onEdit:()=>je(S)}),(E||C||F||X)&&s.jsx(pKe,{onClose:()=>{k(!1),P(null),L(null),R(null)},initialData:F||C||(X?{type:X.type,name:"",base_id:"",setting:X.setting}:void 0),allPresets:t}),A&&s.jsx(mKe,{onClose:()=>D(!1),onApply:H=>{R({type:H.type,setting:H.settings}),D(!1)}}),ie&&U&&W[0]&&W[1]&&s.jsx(pre,{onClose:()=>{ae(!1),G(null)},leftPreset:U[0],rightPreset:U[1],leftLabel:W[0].name,rightLabel:W[1].name})]})}function xKe(){const t=an(),{showToast:e}=Gn(),[n,r]=w.useState("cloud"),[i,a]=w.useState(),{data:o,isLoading:l}=ct({queryKey:["cloudStatus"],queryFn:pe.getCloudStatus}),{data:c=[]}=ct({queryKey:["printers"],queryFn:pe.getPrinters}),{data:u,isLoading:d,refetch:f,dataUpdatedAt:m}=ct({queryKey:["cloudSettings"],queryFn:()=>pe.getCloudSettings(),enabled:!!o?.is_authenticated,retry:!1,staleTime:1e3*60*5});w.useEffect(()=>{m&&a(new Date(m))},[m]);const g=ft({mutationFn:pe.cloudLogout,onSuccess:()=>{t.invalidateQueries({queryKey:["cloudStatus"]}),t.removeQueries({queryKey:["cloudSettings"]}),e("Logged out")}}),x=()=>{t.invalidateQueries({queryKey:["cloudStatus"]})};return l?s.jsx("div",{className:"p-4 md:p-8 flex items-center justify-center min-h-[400px]",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):s.jsxs("div",{className:"p-6 lg:p-8",children:[s.jsxs("div",{className:"mb-6",children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Profiles"}),s.jsx("p",{className:"text-bambu-gray",children:"Manage your slicer presets and pressure advance calibrations"})]}),s.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary mb-6",children:[s.jsxs("button",{onClick:()=>r("cloud"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${n==="cloud"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(T2,{className:"w-4 h-4"}),"Cloud Profiles"]}),s.jsxs("button",{onClick:()=>r("kprofiles"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${n==="kprofiles"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(j2,{className:"w-4 h-4"}),"K-Profiles"]})]}),n==="cloud"&&s.jsxs(s.Fragment,{children:[o?.is_authenticated&&s.jsxs("div",{className:"flex items-center justify-between p-3 mb-6 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green animate-pulse"}),s.jsxs("span",{className:"text-sm text-bambu-gray",children:["Connected as ",s.jsx("span",{className:"text-white",children:o.email})]})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>g.mutate(),disabled:g.isPending,children:[s.jsx($j,{className:"w-4 h-4"}),"Logout"]})]}),o?.is_authenticated?d?s.jsx("div",{className:"flex items-center justify-center py-16",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?s.jsx(gKe,{settings:u,lastSyncTime:i,onRefresh:()=>f(),isRefreshing:d,printers:c}):s.jsxs("div",{className:"text-center py-16",children:[s.jsx("p",{className:"text-bambu-gray mb-4",children:"Failed to load profiles"}),s.jsx(Se,{onClick:()=>f(),children:"Retry"})]}):s.jsx(dKe,{onSuccess:x})]}),n==="kprofiles"&&s.jsx(cKe,{}),s.jsx(hKe,{})]})}const Cb={Droplet:eD,Flame:gue,Ruler:Kde,Sparkles:a0,Square:al,Cable:dce,Wrench:Od,Calendar:Zr,Timer:md,Cog:Hce,Fan:GW,Zap:Mf,Wind:sq,Thermometer:U2,Layers:nl,Box:Ls,Target:nq,RefreshCw:Mr,Settings:F2,Filter:k2,CircleDot:kce};function Jb(t){return t&&Cb[t]||Od}function bp(t,e){if(e==="days")return t<1?"Today":t===1?"1 day":t<7?`${Math.round(t)} days`:t<180?`${Math.round(t/7)} weeks`:`${Math.round(t/30)} months`;{if(t<1)return`${Math.round(t*60)}m`;if(t<24)return`${t<10?t.toFixed(1):Math.round(t)}h`;const n=t/24;if(n<7)return`${n<2?n.toFixed(1):Math.round(n)}d`;const r=n/7;return r<12?`${r<2?r.toFixed(1):Math.round(r)}w`:`${Math.round(r/4)}mo`}}function yj(t,e){return e==="days"?t===1?"1 day":t===7?"1 week":t===14?"2 weeks":t===30?"1 month":t===60?"2 months":t===90?"3 months":t===180?"6 months":t===365?"1 year":`${t} days`:`${t}h`}function bKe(t,e){const n=(e||"").toUpperCase().replace(/[- ]/g,""),r=n.includes("X1"),i=n.includes("P1"),a=n.includes("A1MINI"),o=n.includes("A1")&&!a,l=n.includes("H2D"),c=n.includes("H2C"),u=n.includes("H2S"),d=l||c||u,f=n.includes("P2S");switch(t){case"Lubricate Linear Rails":return r?"https://wiki.bambulab.com/en/x1/maintenance/basic-maintenance":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":a?"https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis":o?"https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis":d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":f?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":"https://wiki.bambulab.com/en/general/lead-screws-lubrication";case"Clean Nozzle/Hotend":return r||i?"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog":a||o?"https://wiki.bambulab.com/en/a1-mini/troubleshooting/nozzle-clog":d?"https://wiki.bambulab.com/en/h2/maintenance/nozzl-cold-pull-maintenance-and-cleaning":f?"https://wiki.bambulab.com/en/p2s/maintenance/cold-pull-maintenance-hotend":"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog";case"Check Belt Tension":return r?"https://wiki.bambulab.com/en/x1/maintenance/belt-tension":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":a?"https://wiki.bambulab.com/en/a1-mini/maintenance/belt_tension":o?"https://wiki.bambulab.com/en/a1/maintenance/belt_tension":l?"https://wiki.bambulab.com/en/h2/maintenance/belt-tension":c?"https://wiki.bambulab.com/en/h2c/maintenance/belt-tension":u?"https://wiki.bambulab.com/en/h2s/maintenance/belt-tension":f?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":"https://wiki.bambulab.com/en/x1/maintenance/belt-tension";case"Clean Carbon Rods":return r||i?"https://wiki.bambulab.com/en/general/carbon-rods-clearance":a||o||d||f?null:"https://wiki.bambulab.com/en/general/carbon-rods-clearance";case"Clean Build Plate":return"https://wiki.bambulab.com/en/filament-acc/acc/pei-plate-clean-guide";case"Check PTFE Tube":return r||i?"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube":a||o?"https://wiki.bambulab.com/en/a1-mini/maintenance/ptfe-tube":l?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":u?"https://wiki.bambulab.com/en/h2s/maintenance/replace-ptfe-tube-on-h2s-printer":c?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube";case"Replace HEPA Filter":case"HEPA Filter":case"Replace Carbon Filter":case"Carbon Filter":return d?"https://wiki.bambulab.com/en/h2/maintenance/replace-smoke-purifier-air-filte":"https://wiki.bambulab.com/en/x1/maintenance/replace-carbon-filter";case"Lubricate Left Nozzle Rail":case"Left Nozzle Rail":return d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;default:return null}}function yKe({item:t,onPerform:e,onToggle:n}){const r=Jb(t.maintenance_type_icon),i=t.interval_type||"hours",o=(()=>{if(i==="days"){const f=t.days_since_maintenance??0;return Math.max(0,Math.min(100,f/t.interval_hours*100))}return Math.max(0,Math.min(100,(t.interval_hours-t.hours_until_due)/t.interval_hours*100))})(),l=()=>t.enabled?t.is_due?"text-red-400":t.is_warning?"text-amber-400":"text-bambu-green":"text-bambu-gray",c=()=>t.enabled?t.is_due?"bg-red-500":t.is_warning?"bg-amber-500":"bg-bambu-green":"bg-bambu-gray/30",u=()=>t.enabled?t.is_due?"bg-red-500/5 border-red-500/20":t.is_warning?"bg-amber-500/5 border-amber-500/20":"bg-bambu-dark-secondary border-bambu-dark-tertiary":"bg-bambu-dark-secondary/50",d=()=>{if(!t.enabled)return"Disabled";if(i==="days"){const f=t.days_until_due??0;return t.is_due?`Overdue by ${bp(Math.abs(f),"days")}`:t.is_warning?`Due in ${bp(f,"days")}`:`${bp(f,"days")} left`}else return t.is_due?`Overdue by ${bp(Math.abs(t.hours_until_due),"hours")}`:t.is_warning?`Due in ${bp(t.hours_until_due,"hours")}`:`${bp(t.hours_until_due,"hours")} left`};return s.jsx("div",{className:`rounded-xl border p-4 transition-all ${u()}`,children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsxs("div",{className:`relative p-2.5 rounded-lg ${t.is_due?"bg-red-500/20":t.is_warning?"bg-amber-500/20":t.enabled?"bg-bambu-dark":"bg-bambu-dark/50"}`,children:[s.jsx(r,{className:`w-5 h-5 ${l()}`}),t.enabled&&(t.is_due||t.is_warning)&&s.jsx("span",{className:`absolute -top-1 -right-1 w-2.5 h-2.5 rounded-full ${t.is_due?"bg-red-500":"bg-amber-500"} animate-pulse`})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("h3",{className:`font-medium truncate ${t.enabled?"text-white":"text-bambu-gray"}`,children:t.maintenance_type_name}),i==="days"&&s.jsx("span",{title:"Time-based interval",children:s.jsx(Zr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"})}),(()=>{const f=t.maintenance_type_wiki_url||bKe(t.maintenance_type_name,t.printer_model);return f?s.jsx("a",{href:f,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-bambu-green transition-colors shrink-0",title:"View documentation",onClick:m=>m.stopPropagation(),children:s.jsx(Ur,{className:"w-3.5 h-3.5"})}):null})()]}),s.jsx("div",{className:"mt-2 mb-1.5",children:s.jsx("div",{className:"w-full h-1.5 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:`h-full rounded-full transition-all duration-500 ${c()}`,style:{width:`${o}%`}})})}),s.jsxs("div",{className:`text-xs flex items-center gap-1 ${l()}`,children:[t.is_due&&s.jsx(jn,{className:"w-3 h-3"}),t.is_warning&&!t.is_due&&s.jsx(Yn,{className:"w-3 h-3"}),!t.is_due&&!t.is_warning&&t.enabled&&s.jsx(bs,{className:"w-3 h-3"}),d()]})]}),s.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[s.jsx(Xn,{checked:t.enabled,onChange:f=>n(t.id,f)}),s.jsxs(Se,{size:"sm",variant:t.is_due?"primary":"secondary",onClick:()=>e(t.id),disabled:!t.enabled,className:"!px-3",children:[s.jsx(Vf,{className:"w-3.5 h-3.5"}),"Reset"]})]})]})})}function vKe({overview:t,onPerform:e,onToggle:n,onSetHours:r}){const[i,a]=w.useState(!0),[o,l]=w.useState(!1),[c,u]=w.useState(t.total_print_hours.toFixed(1)),d=[...t.maintenance_items].sort((g,x)=>g.is_due&&!x.is_due?-1:!g.is_due&&x.is_due?1:g.is_warning&&!x.is_warning?-1:!g.is_warning&&x.is_warning?1:g.maintenance_type_id-x.maintenance_type_id),f=d.find(g=>g.enabled&&(g.is_due||g.is_warning)),m=()=>{const g=parseFloat(c);!isNaN(g)&&g>=0&&(r(t.printer_id,g),l(!1))};return s.jsxs(Mt,{className:"overflow-hidden",children:[s.jsxs("div",{className:"p-5",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx("h2",{className:"text-xl font-semibold text-white",children:t.printer_name}),s.jsxs("div",{className:"flex items-center gap-2",children:[t.due_count>0&&s.jsxs("span",{className:"px-2.5 py-1 bg-red-500/20 text-red-400 text-xs font-medium rounded-full flex items-center gap-1.5",children:[s.jsx(jn,{className:"w-3 h-3"}),t.due_count," overdue"]}),t.warning_count>0&&s.jsxs("span",{className:"px-2.5 py-1 bg-amber-500/20 text-amber-400 text-xs font-medium rounded-full flex items-center gap-1.5",children:[s.jsx(Yn,{className:"w-3 h-3"}),t.warning_count," due soon"]}),t.due_count===0&&t.warning_count===0&&s.jsxs("span",{className:"px-2.5 py-1 bg-bambu-green/20 text-bambu-green text-xs font-medium rounded-full flex items-center gap-1.5",children:[s.jsx(bs,{className:"w-3 h-3"}),"All good"]})]})]}),s.jsxs("button",{onClick:()=>a(!i),className:"flex items-center gap-1.5 px-3 py-1.5 text-sm text-bambu-gray hover:text-white hover:bg-bambu-dark rounded-lg transition-colors",children:[i?s.jsx(eu,{className:"w-4 h-4"}):s.jsx(ea,{className:"w-4 h-4"}),i?"Collapse":"Expand"]})]}),s.jsxs("div",{className:"flex items-center gap-6 mt-4",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 bg-bambu-dark/50 rounded-lg",children:s.jsx(md,{className:"w-4 h-4 text-bambu-gray"})}),o?s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",value:c,onChange:g=>u(g.target.value),onKeyDown:g=>{g.key==="Enter"&&m(),g.key==="Escape"&&l(!1)},className:"w-24 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm",min:"0",step:"1",autoFocus:!0}),s.jsx("span",{className:"text-xs text-bambu-gray",children:"hours"}),s.jsx(Se,{size:"sm",onClick:m,children:"Save"}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>l(!1),children:"Cancel"})]}):s.jsxs("button",{onClick:()=>{u(Math.round(t.total_print_hours).toString()),l(!0)},className:"group",children:[s.jsxs("div",{className:"text-sm font-medium text-white group-hover:text-bambu-green transition-colors flex items-center gap-1",children:[Math.round(t.total_print_hours)," hours",s.jsx(wf,{className:"w-3 h-3 text-bambu-gray group-hover:text-bambu-green"})]}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Total print time"})]})]}),s.jsx("div",{className:"w-px h-10 bg-bambu-dark-tertiary"}),f&&s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${f.is_due?"bg-red-500/20":"bg-amber-500/20"}`,children:(()=>{const g=Jb(f.maintenance_type_icon);return s.jsx(g,{className:`w-4 h-4 ${f.is_due?"text-red-400":"text-amber-400"}`})})()}),s.jsxs("div",{children:[s.jsx("div",{className:`text-sm font-medium ${f.is_due?"text-red-400":"text-amber-400"}`,children:f.maintenance_type_name}),s.jsx("div",{className:`text-xs ${f.is_due?"text-red-400/70":"text-amber-400/70"}`,children:f.is_due?"Overdue":"Due soon"})]})]})]})]}),i&&s.jsx(Tt,{className:"pt-0 border-t border-bambu-dark-tertiary",children:s.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-3 pt-4",children:d.map(g=>s.jsx(yKe,{item:g,onPerform:e,onToggle:n},g.id))})})]})}function _Ke({overview:t,types:e,onUpdateInterval:n,onAddType:r,onUpdateType:i,onDeleteType:a,onAssignType:o,onRemoveItem:l}){const[c,u]=w.useState(null),[d,f]=w.useState(""),[m,g]=w.useState("hours"),[x,y]=w.useState(!1),[b,v]=w.useState(""),[M,S]=w.useState("100"),[T,E]=w.useState("hours"),[k,A]=w.useState("Wrench"),[D,C]=w.useState(""),[P,F]=w.useState(new Set),[L,X]=w.useState(null),R=w.useMemo(()=>t?t.map(z=>({id:z.printer_id,name:z.printer_name})):[],[t]),q=z=>t?t.filter(V=>V.maintenance_items.some(Q=>Q.maintenance_type_id===z)).map(V=>({printerId:V.printer_id,printerName:V.printer_name,itemId:V.maintenance_items.find(Q=>Q.maintenance_type_id===z)?.id})):[],Y=z=>{if(!t)return[];const V=new Set(q(z).map(Q=>Q.printerId));return R.filter(Q=>!V.has(Q.id))},[W,Z]=w.useState(null),[ie,ae]=w.useState(""),[U,G]=w.useState(""),[ee,re]=w.useState("hours"),[le,he]=w.useState("Wrench"),[fe,Te]=w.useState(""),ge=z=>{Z(z),ae(z.name),G(z.default_interval_hours.toString()),re(z.interval_type||"hours"),he(z.icon||"Wrench"),Te(z.wiki_url||"")},_e=()=>{W&&ie.trim()&&parseFloat(U)>0&&(i(W.id,{name:ie.trim(),default_interval_hours:parseFloat(U),interval_type:ee,icon:le,wiki_url:fe.trim()||null}),Z(null))},je=(z,V,Q)=>{const Ne=parseFloat(d);if(!isNaN(Ne)&&Ne>0){const ke=Math.abs(Ne-V)<.01?null:Ne;n(z,{custom_interval_hours:ke,custom_interval_type:m!==Q?m:null})}u(null)},ne=z=>{z.preventDefault(),b.trim()&&parseFloat(M)>0&&P.size>0&&(r({name:b.trim(),default_interval_hours:parseFloat(M),interval_type:T,icon:k,wiki_url:D.trim()||null},Array.from(P)),v(""),S("100"),E("hours"),C(""),F(new Set),y(!1))},Qe=z=>{F(V=>{const Q=new Set(V);return Q.has(z)?Q.delete(z):Q.add(z),Q})},B=t?.map(z=>({printerId:z.printer_id,printerName:z.printer_name,items:z.maintenance_items.sort((V,Q)=>V.maintenance_type_id-Q.maintenance_type_id)})).sort((z,V)=>z.printerName.localeCompare(V.printerName))||[],H=e.filter(z=>z.is_system),K=e.filter(z=>!z.is_system);return s.jsxs("div",{className:"space-y-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("div",{children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Maintenance Types"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"System types and your custom maintenance tasks"})]}),s.jsxs(Se,{onClick:()=>y(!x),children:[s.jsx(Jn,{className:"w-4 h-4"}),"Add Custom Type"]})]}),x&&s.jsx(Mt,{className:"mb-6",children:s.jsx(Tt,{className:"py-4",children:s.jsxs("form",{onSubmit:ne,children:[s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[s.jsxs("div",{className:"lg:col-span-2",children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Name"}),s.jsx("input",{type:"text",value:b,onChange:z=>v(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"e.g., Replace HEPA Filter",autoFocus:!0})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Interval Type"}),s.jsxs("select",{value:T,onChange:z=>{E(z.target.value),z.target.value==="days"?S("30"):S("100")},className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"hours",children:"Print Hours"}),s.jsx("option",{value:"days",children:"Calendar Days"})]})]}),s.jsxs("div",{children:[s.jsxs("label",{className:"block text-xs text-bambu-gray mb-1.5",children:["Interval (",T==="days"?"days":"hours",")"]}),s.jsx("input",{type:"number",value:M,onChange:z=>S(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",min:"1"})]})]}),s.jsx("div",{className:"mt-4 flex items-end justify-between",children:s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Icon"}),s.jsx("div",{className:"flex gap-1",children:Object.keys(Cb).map(z=>{const V=Cb[z];return s.jsx("button",{type:"button",onClick:()=>A(z),className:`p-2 rounded-lg transition-colors ${k===z?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:s.jsx(V,{className:"w-4 h-4"})},z)})})]})}),s.jsxs("div",{className:"mt-4",children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Documentation Link (optional)"}),s.jsx("input",{type:"url",value:D,onChange:z=>C(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"https://wiki.bambulab.com/..."})]}),s.jsxs("div",{className:"mt-4",children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Assign to Printers"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:R.map(z=>s.jsx("button",{type:"button",onClick:()=>Qe(z.id),className:`px-3 py-1.5 rounded-lg text-sm transition-colors ${P.has(z.id)?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:z.name},z.id))}),P.size===0&&s.jsx("p",{className:"text-xs text-orange-400 mt-1",children:"Select at least one printer"})]}),s.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:()=>{y(!1),F(new Set)},children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!b.trim()||P.size===0,children:"Add Type"})]})]})})}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3",children:[H.map(z=>{const V=Jb(z.icon),Q=z.interval_type||"hours";return s.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-dark rounded-lg",children:s.jsx(V,{className:"w-5 h-5 text-bambu-gray"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("div",{className:"text-sm font-medium text-white truncate",children:z.name}),s.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[Q==="days"?s.jsx(Zr,{className:"w-3 h-3"}):s.jsx(md,{className:"w-3 h-3"}),yj(z.default_interval_hours,Q)]})]})]})},z.id)}),K.map(z=>{const V=Jb(z.icon),Q=z.interval_type||"hours";if(W?.id===z.id)return s.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green",children:s.jsxs("div",{className:"space-y-3",children:[s.jsx("input",{type:"text",value:ie,onChange:I=>ae(I.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"Name",autoFocus:!0}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("select",{value:ee,onChange:I=>re(I.target.value),className:"flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"hours",children:"Print Hours"}),s.jsx("option",{value:"days",children:"Calendar Days"})]}),s.jsx("input",{type:"number",value:U,onChange:I=>G(I.target.value),className:"w-24 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",min:"1"})]}),s.jsx("div",{className:"flex flex-wrap gap-1",children:Object.keys(Cb).map(I=>{const J=Cb[I];return s.jsx("button",{type:"button",onClick:()=>he(I),className:`p-1.5 rounded transition-colors ${le===I?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:s.jsx(J,{className:"w-3.5 h-3.5"})},I)})}),s.jsx("input",{type:"url",value:fe,onChange:I=>Te(I.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"Documentation link (optional)"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{size:"sm",onClick:_e,disabled:!ie.trim(),children:"Save"}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>Z(null),children:"Cancel"})]})]})},z.id);const ke=q(z.id),Me=Y(z.id),$=L===z.id;return s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green/30",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-green/20 rounded-lg",children:s.jsx(V,{className:"w-5 h-5 text-bambu-green"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-sm font-medium text-white truncate",children:z.name}),s.jsx("span",{className:"px-1.5 py-0.5 bg-bambu-green/20 text-bambu-green text-[10px] font-medium rounded",children:"Custom"})]}),s.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[Q==="days"?s.jsx(Zr,{className:"w-3 h-3"}):s.jsx(md,{className:"w-3 h-3"}),yj(z.default_interval_hours,Q)]})]}),s.jsxs("button",{onClick:()=>X($?null:z.id),className:`px-2 py-1 rounded-lg border transition-colors flex items-center gap-1 ${ke.length>0?"border-bambu-green/50 bg-bambu-green/10 text-bambu-green hover:bg-bambu-green/20":"border-orange-400/50 bg-orange-400/10 text-orange-400 hover:bg-orange-400/20"}`,title:`${ke.length} printer(s) assigned - click to manage`,children:[s.jsx(zr,{className:"w-3 h-3"}),s.jsx("span",{className:"text-xs font-medium",children:ke.length}),s.jsx(ea,{className:`w-3 h-3 transition-transform ${$?"rotate-180":""}`})]}),s.jsx("button",{onClick:()=>ge(z),className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",children:s.jsx(wf,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>{confirm(`Delete "${z.name}"?`)&&a(z.id)},className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors",children:s.jsx(ln,{className:"w-4 h-4"})})]}),$&&s.jsxs("div",{className:"mt-3 pt-3 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Assigned to printers:"}),ke.length===0?s.jsx("p",{className:"text-xs text-orange-400",children:"No printers assigned"}):s.jsx("div",{className:"flex flex-wrap gap-1 mb-2",children:ke.map(I=>s.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark rounded text-xs text-white",children:[I.printerName,s.jsx("button",{onClick:()=>I.itemId&&l(I.itemId),className:"hover:text-red-400 ml-1",title:"Remove from this printer",children:"×"})]},I.printerId))}),Me.length>0&&s.jsxs("div",{className:"flex flex-wrap gap-1",children:[s.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:"Add:"}),Me.map(I=>s.jsxs("button",{onClick:()=>o(I.id,z.id),className:"px-2 py-1 bg-bambu-dark hover:bg-bambu-green/20 rounded text-xs text-bambu-gray hover:text-bambu-green transition-colors",children:["+ ",I.name]},I.id))]})]})]},z.id)})]})]}),B.length>0&&s.jsxs("div",{children:[s.jsxs("div",{className:"mb-4",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Interval Overrides"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize intervals for specific printers"})]}),s.jsx("div",{className:"space-y-4",children:B.map(z=>s.jsx(Mt,{children:s.jsxs(Tt,{className:"py-4",children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:z.printerName}),s.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2",children:z.items.map(V=>{const Q=Jb(V.maintenance_type_icon),Ne=e.find(J=>J.id===V.maintenance_type_id),ke=Ne?.default_interval_hours||V.interval_hours,Me=Ne?.interval_type||"hours",$=V.interval_type||"hours",I=c===V.id;return s.jsxs("div",{className:"flex items-center gap-2 p-2.5 bg-bambu-dark rounded-lg",children:[s.jsx(Q,{className:"w-4 h-4 text-bambu-gray shrink-0"}),s.jsx("span",{className:"text-xs text-bambu-gray flex-1 truncate",children:V.maintenance_type_name}),I?s.jsxs("div",{className:"flex items-center gap-1",children:[m==="days"?s.jsx(Zr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}):s.jsx(md,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}),s.jsxs("select",{value:m,onChange:J=>g(J.target.value),className:"px-1.5 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",children:[s.jsx("option",{value:"hours",children:"Print Hours"}),s.jsx("option",{value:"days",children:"Calendar Days"})]}),s.jsx("input",{type:"number",value:d,onChange:J=>f(J.target.value),onKeyDown:J=>{J.key==="Enter"&&je(V.id,ke,Me),J.key==="Escape"&&u(null)},className:"w-16 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",min:"1"}),s.jsx(Se,{size:"sm",onClick:()=>je(V.id,ke,Me),children:"OK"})]}):s.jsxs("button",{onClick:()=>{u(V.id),f(V.interval_hours.toString()),g($)},className:"px-2 py-1 bg-bambu-dark-tertiary hover:bg-bambu-dark-secondary border border-bambu-dark-tertiary hover:border-bambu-green rounded text-xs font-medium text-white transition-colors flex items-center gap-1",children:[$==="days"?s.jsx(Zr,{className:"w-3 h-3"}):s.jsx(md,{className:"w-3 h-3"}),yj(V.interval_hours,$),s.jsx(wf,{className:"w-3 h-3 text-bambu-gray"})]})]},V.id)})})]})},z.printerId))})]}),B.length===0&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"text-center py-12",children:[s.jsx(Yn,{className:"w-12 h-12 mx-auto mb-4 text-bambu-gray/30"}),s.jsx("p",{className:"text-bambu-gray",children:"No printers configured"}),s.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1",children:"Add printers to configure maintenance intervals"})]})})]})}function wKe(){const t=an(),{showToast:e}=Gn(),[n,r]=w.useState("status"),{data:i,isLoading:a}=ct({queryKey:["maintenanceOverview"],queryFn:pe.getMaintenanceOverview}),{data:o}=ct({queryKey:["maintenanceTypes"],queryFn:pe.getMaintenanceTypes}),l=ft({mutationFn:({id:S,notes:T})=>pe.performMaintenance(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Maintenance marked as complete")},onError:S=>{e(S.message,"error")}}),c=ft({mutationFn:({id:S,data:T})=>pe.updateMaintenanceItem(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]})},onError:S=>{e(S.message,"error")}}),u=ft({mutationFn:({id:S,data:T})=>pe.updateMaintenanceType(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type updated")},onError:S=>{e(S.message,"error")}}),d=ft({mutationFn:pe.deleteMaintenanceType,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type deleted")},onError:S=>{e(S.message,"error")}}),f=ft({mutationFn:({printerId:S,hours:T})=>pe.setPrinterHours(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Print hours updated")},onError:S=>{e(S.message,"error")}}),m=ft({mutationFn:({printerId:S,typeId:T})=>pe.assignMaintenanceType(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer assigned")},onError:S=>{e(S.message,"error")}}),g=ft({mutationFn:pe.removeMaintenanceItem,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer removed")},onError:S=>{e(S.message,"error")}}),x=S=>{l.mutate({id:S})},y=(S,T)=>{c.mutate({id:S,data:{enabled:T}})},b=(S,T)=>{f.mutate({printerId:S,hours:T})};if(a)return s.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})});const v=i?.reduce((S,T)=>S+T.due_count,0)||0,M=i?.reduce((S,T)=>S+T.warning_count,0)||0;return s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsxs("div",{className:"mb-6",children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Maintenance"}),s.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:n==="status"?s.jsxs(s.Fragment,{children:[v>0&&s.jsxs("span",{className:"text-red-400",children:[v," task",v!==1?"s":""," overdue"]}),v>0&&M>0&&" · ",M>0&&s.jsxs("span",{className:"text-amber-400",children:[M," due soon"]}),v===0&&M===0&&s.jsx("span",{className:"text-bambu-green",children:"All maintenance up to date"})]}):"Configure maintenance types and intervals"})]}),s.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[s.jsx("button",{onClick:()=>r("status"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${n==="status"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:"Status"}),s.jsx("button",{onClick:()=>r("settings"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${n==="settings"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:"Settings"})]}),n==="status"?s.jsx("div",{className:"space-y-6",children:i&&i.length>0?[...i].sort((S,T)=>{const E=S.due_count*10+S.warning_count,k=T.due_count*10+T.warning_count;return E!==k?k-E:S.printer_name.localeCompare(T.printer_name)}).map(S=>s.jsx(vKe,{overview:S,onPerform:x,onToggle:y,onSetHours:b},S.printer_id)):s.jsx(Mt,{children:s.jsxs(Tt,{className:"text-center py-16",children:[s.jsx(Od,{className:"w-16 h-16 mx-auto mb-4 text-bambu-gray/30"}),s.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No printers configured"}),s.jsx("p",{className:"text-bambu-gray",children:"Add printers to start tracking maintenance"})]})})}):s.jsx(_Ke,{overview:i,types:o||[],onUpdateInterval:(S,T)=>c.mutate({id:S,data:T}),onAddType:async(S,T)=>{const E=await pe.createMaintenanceType(S);for(const k of T)await pe.assignMaintenanceType(k,E.id);t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type added")},onUpdateType:(S,T)=>u.mutate({id:S,data:T}),onDeleteType:S=>d.mutate(S),onAssignType:(S,T)=>m.mutate({printerId:S,typeId:T}),onRemoveItem:S=>g.mutate(S)})]})}const qG=["#ef4444","#f97316","#eab308","#22c55e","#06b6d4","#3b82f6","#8b5cf6","#ec4899","#6b7280"];function gre({project:t,onClose:e,onSave:n,isLoading:r}){const[i,a]=w.useState(t?.name||""),[o,l]=w.useState(t?.description||""),[c,u]=w.useState(t?.color||qG[0]),[d,f]=w.useState(t?.target_count?.toString()||""),[m,g]=w.useState(t?.target_parts_count?.toString()||""),[x,y]=w.useState(t?.status||"active"),[b,v]=w.useState(t?.tags||""),[M,S]=w.useState(t?.due_date?.split("T")[0]||""),[T,E]=w.useState(t?.priority||"normal"),k=A=>{A.preventDefault(),n({name:i.trim(),description:o.trim()||void 0,color:c,target_count:d?parseInt(d,10):void 0,target_parts_count:m?parseInt(m,10):void 0,tags:b.trim()||void 0,due_date:M||void 0,priority:T,...t&&{status:x}})};return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:t?"Edit Project":"New Project"})}),s.jsxs("form",{onSubmit:k,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Name"}),s.jsx("input",{type:"text",value:i,onChange:A=>a(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., Voron 2.4 Build",required:!0})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Description"}),s.jsx("textarea",{value:o,onChange:A=>l(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green resize-none",placeholder:"Optional description...",rows:2})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Color"}),s.jsx("div",{className:"flex gap-2 flex-wrap",children:qG.map(A=>s.jsx("button",{type:"button",onClick:()=>u(A),className:`w-8 h-8 rounded-full transition-transform ${c===A?"ring-2 ring-white ring-offset-2 ring-offset-bambu-dark-secondary scale-110":""}`,style:{backgroundColor:A}},A))})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Target Plates"}),s.jsx("input",{type:"number",value:d,onChange:A=>f(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., 25",min:"1"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Number of print jobs"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Target Parts"}),s.jsx("input",{type:"number",value:m,onChange:A=>g(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., 150",min:"1"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Total objects needed"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Tags (comma-separated)"}),s.jsx("input",{type:"text",value:b,onChange:A=>v(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., voron, functional, gift"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Due Date"}),s.jsx("input",{type:"date",value:M,onChange:A=>S(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white focus:outline-none focus:border-bambu-green"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Priority"}),s.jsxs("select",{value:T,onChange:A=>E(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"low",children:"Low"}),s.jsx("option",{value:"normal",children:"Normal"}),s.jsx("option",{value:"high",children:"High"}),s.jsx("option",{value:"urgent",children:"Urgent"})]})]})]}),t&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Status"}),s.jsxs("select",{value:x,onChange:A=>y(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"active",children:"Active"}),s.jsx("option",{value:"completed",children:"Completed"}),s.jsx("option",{value:"archived",children:"Archived"})]})]}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!i.trim()||r,children:r?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):t?"Save":"Create"})]})]})]})})}function SKe({project:t,onClick:e,onEdit:n,onDelete:r}){const i=t.target_count?Math.round(t.archive_count/t.target_count*100):0,a=t.target_parts_count?Math.round(t.completed_count/t.target_parts_count*100):0,o=t.status==="completed",l=t.status==="archived",[c,u]=w.useState(!1),f=o?{icon:E2,color:"text-bambu-green",bg:"bg-bambu-green/10"}:l?{icon:Va,color:"text-bambu-gray",bg:"bg-bambu-gray/10"}:t.queue_count>0?{icon:Yn,color:"text-blue-400",bg:"bg-blue-400/10"}:{icon:ya,color:"text-bambu-gray",bg:"bg-bambu-gray/10"};return s.jsxs("div",{className:"group relative bg-gradient-to-br from-bambu-card to-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary hover:border-bambu-green/50 hover:shadow-lg hover:shadow-bambu-green/5 transition-all duration-300 cursor-pointer overflow-hidden",onClick:e,children:[s.jsx("div",{className:"absolute top-0 left-0 w-1.5 h-full",style:{backgroundColor:t.color||"#6b7280",boxShadow:`0 0 12px ${t.color||"#6b7280"}40`}}),s.jsxs("div",{className:"p-5 pl-6",children:[s.jsxs("div",{className:"flex items-start justify-between mb-4",children:[s.jsxs("div",{className:"flex items-center gap-3 min-w-0 flex-1",children:[s.jsx("div",{className:`p-2 rounded-lg ${f.bg} flex-shrink-0`,children:s.jsx(f.icon,{className:`w-5 h-5 ${f.color}`})}),s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[s.jsx("h3",{className:"font-semibold text-white truncate",children:t.name}),t.target_parts_count?s.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${a>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.completed_count,"/",t.target_parts_count," parts"]}):t.target_count?s.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${i>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.archive_count,"/",t.target_count," plates"]}):t.completed_count>0?s.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium bg-bambu-dark text-bambu-gray",children:[t.completed_count," parts"]}):null,o&&s.jsx("span",{className:"text-xs bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full whitespace-nowrap",children:"Done"}),l&&s.jsx("span",{className:"text-xs bg-bambu-gray/20 text-bambu-gray px-2 py-0.5 rounded-full whitespace-nowrap",children:"Archived"})]}),t.description&&s.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1 line-clamp-1",children:t.description}),t.archives&&t.archives.length>0&&(()=>{const m=t.archives.map(b=>b.filament_type).filter(Boolean).flatMap(b=>b.split(",").map(v=>v.trim())).filter(Boolean),g=[...new Set(m)],x=t.archives.map(b=>b.filament_color).filter(Boolean).flatMap(b=>b.split(",").map(v=>v.trim())).filter(b=>b.startsWith("#")||/^[0-9A-Fa-f]{6}$/.test(b)),y=[...new Set(x)];return g.length===0&&y.length===0?null:s.jsxs("div",{className:"flex items-center gap-2 mt-1.5",children:[g.slice(0,3).map(b=>s.jsx("span",{className:"text-[10px] px-1.5 py-0.5 bg-bambu-dark text-bambu-gray rounded",children:b},b)),y.length>0&&s.jsxs("div",{className:"flex items-center gap-0.5",children:[y.slice(0,5).map(b=>s.jsx("div",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:b.startsWith("#")?b:`#${b}`},title:b},b)),y.length>5&&s.jsxs("span",{className:"text-[10px] text-bambu-gray ml-0.5",children:["+",y.length-5]})]})]})})()]})]}),s.jsxs("div",{className:"relative",onClick:m=>m.stopPropagation(),children:[s.jsx("button",{className:"p-1.5 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors opacity-0 group-hover:opacity-100",onClick:()=>u(!c),children:s.jsx(wd,{className:"w-4 h-4"})}),c&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>u(!1)}),s.jsxs("div",{className:"absolute right-0 top-8 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]",children:[s.jsxs("button",{className:"w-full px-3 py-2 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{n(),u(!1)},children:[s.jsx(wf,{className:"w-4 h-4"}),"Edit"]}),s.jsxs("button",{className:"w-full px-3 py-2 text-left text-sm text-red-400 hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{r(),u(!1)},children:[s.jsx(ln,{className:"w-4 h-4"}),"Delete"]})]})]})]})]}),s.jsx("div",{className:"mb-4",children:t.target_count||t.target_parts_count?s.jsxs("div",{className:"space-y-3",children:[t.target_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[s.jsx("span",{className:"text-bambu-gray",children:"Plates"}),s.jsxs("span",{className:i>=100?"text-bambu-green font-medium":"text-white",children:[t.archive_count," / ",t.target_count]})]}),s.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:s.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(i,100)}%`,background:i>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${i>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.target_parts_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[s.jsx("span",{className:"text-bambu-gray",children:"Parts"}),s.jsxs("span",{className:a>=100?"text-bambu-green font-medium":"text-white",children:[t.completed_count," / ",t.target_parts_count]})]}),s.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:s.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(a,100)}%`,background:a>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${a>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.failed_count>0&&s.jsxs("div",{className:"text-xs text-red-400",children:[t.failed_count," failed"]})]}):t.completed_count>0||t.failed_count>0?s.jsxs("div",{className:"flex items-center gap-4 text-xs",children:[t.completed_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-bambu-gray",children:[s.jsx(Va,{className:"w-3.5 h-3.5"}),s.jsxs("span",{children:[t.completed_count," completed"]})]}),t.failed_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",children:[s.jsx(jn,{className:"w-3.5 h-3.5"}),s.jsxs("span",{children:[t.failed_count," failed"]})]}),t.queue_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",children:[s.jsx(Yn,{className:"w-3.5 h-3.5"}),s.jsxs("span",{children:[t.queue_count," in queue"]})]})]}):s.jsx("div",{className:"text-xs text-bambu-gray/60 italic",children:"No prints yet"})}),t.archives&&t.archives.length>0&&s.jsxs("div",{className:"mb-4",children:[s.jsx("div",{className:"grid grid-cols-4 gap-1.5",children:t.archives.slice(0,4).map(m=>s.jsxs("div",{className:"relative aspect-square rounded-lg bg-bambu-dark overflow-hidden border border-bambu-dark-tertiary",title:m.print_name||"Unknown",children:[m.thumbnail_path?s.jsx("img",{src:pe.getArchiveThumbnail(m.id),alt:m.print_name||"",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray/50",children:s.jsx(Ll,{className:"w-6 h-6"})}),m.status==="failed"&&s.jsx("div",{className:"absolute inset-0 bg-red-500/40 flex items-center justify-center",children:s.jsx(jn,{className:"w-4 h-4 text-white"})})]},m.id))}),t.archive_count>4&&s.jsxs("p",{className:"text-xs text-bambu-gray mt-1.5 text-center",children:["+",t.archive_count-4," more"]})]}),s.jsxs("div",{className:"flex items-center justify-between pt-3 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-4 text-xs text-bambu-gray",children:[s.jsxs("div",{className:"flex items-center gap-1.5",title:"Print jobs (plates)",children:[s.jsx(nl,{className:"w-3.5 h-3.5 text-blue-400"}),s.jsxs("span",{children:[t.archive_count," plates"]})]}),s.jsxs("div",{className:"flex items-center gap-1.5",title:"Parts printed",children:[s.jsx(Ll,{className:"w-3.5 h-3.5 text-bambu-green"}),s.jsxs("span",{children:[t.completed_count," parts"]})]}),t.failed_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",title:"Failed parts",children:[s.jsx(jn,{className:"w-3.5 h-3.5"}),s.jsx("span",{children:t.failed_count})]}),t.queue_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-yellow-400",title:"In queue",children:[s.jsx(Bj,{className:"w-3.5 h-3.5"}),s.jsx("span",{children:t.queue_count})]})]}),s.jsx(Ws,{className:"w-4 h-4 text-bambu-gray/50 group-hover:text-bambu-gray transition-colors"})]})]})]})}function MKe(){const t=cl(),e=an(),{showToast:n}=Gn(),[r,i]=w.useState(!1),[a,o]=w.useState(),[l,c]=w.useState("active"),[u,d]=w.useState(null),{data:f,isLoading:m}=ct({queryKey:["projects",l==="all"?void 0:l],queryFn:()=>pe.getProjects(l==="all"?void 0:l)}),g=ft({mutationFn:k=>pe.createProject(k),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),n("Project created","success")},onError:k=>{n(k.message,"error")}}),x=ft({mutationFn:({id:k,data:A})=>pe.updateProject(k,A),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),o(void 0),n("Project updated","success")},onError:k=>{n(k.message,"error")}}),y=ft({mutationFn:k=>pe.deleteProject(k),onSuccess:()=>{d(null),n("Project deleted","success"),setTimeout(()=>window.location.reload(),100)},onError:k=>{d(null),n(k.message,"error")}}),b=k=>{a?x.mutate({id:a.id,data:k}):g.mutate(k)},v=k=>{o(k),i(!0)},M=k=>{t(`/projects/${k.id}`)},S=k=>{d(k)},T=()=>{u!==null&&y.mutate(u)},E=f?.reduce((k,A)=>(k[A.status]=(k[A.status]||0)+1,k.all=(k.all||0)+1,k),{})||{};return s.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[s.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[s.jsxs("div",{children:[s.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:s.jsx(ya,{className:"w-6 h-6 text-bambu-green"})}),"Projects"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:"Organize and track your 3D printing projects"})]}),s.jsxs(Se,{onClick:()=>i(!0),className:"sm:w-auto w-full",children:[s.jsx(Jn,{className:"w-4 h-4 mr-2"}),"New Project"]})]}),s.jsx("div",{className:"flex gap-1 p-1 bg-bambu-dark rounded-xl w-fit",children:[{key:"active",label:"Active",icon:Yn},{key:"completed",label:"Completed",icon:E2},{key:"archived",label:"Archived",icon:Va},{key:"all",label:"All",icon:ya}].map(({key:k,label:A,icon:D})=>s.jsxs("button",{onClick:()=>c(k),className:`flex items-center gap-2 px-4 py-2 text-sm rounded-lg transition-all ${l===k?"bg-bambu-card text-white shadow-sm":"text-bambu-gray hover:text-white"}`,children:[s.jsx(D,{className:"w-4 h-4"}),s.jsx("span",{children:A}),E[k]>0&&s.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded-full ${l===k?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary"}`,children:E[k]})]},k))}),m?s.jsx("div",{className:"flex items-center justify-center py-20",children:s.jsxs("div",{className:"flex flex-col items-center gap-3",children:[s.jsx(ht,{className:"w-8 h-8 animate-spin text-bambu-green"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Loading projects..."})]})}):f?.length===0?s.jsxs("div",{className:"flex flex-col items-center justify-center py-20 px-4",children:[s.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:s.jsx(ya,{className:"w-12 h-12 text-bambu-gray/50"})}),s.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:l==="all"?"No projects yet":`No ${l} projects`}),s.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:l==="all"?"Create your first project to start organizing related prints, tracking progress, and managing your builds.":`You don't have any ${l} projects. Projects will appear here when their status changes.`}),l==="all"&&s.jsxs(Se,{onClick:()=>i(!0),children:[s.jsx(Jn,{className:"w-4 h-4 mr-2"}),"Create Your First Project"]})]}):s.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-8",children:f?.map(k=>s.jsx(SKe,{project:k,onClick:()=>M(k),onEdit:()=>v(k),onDelete:()=>S(k.id)},k.id))}),u!==null&&s.jsx(Un,{title:"Delete Project",message:"Are you sure you want to delete this project? Archives and queue items will be unlinked but not deleted.",confirmText:"Delete Project",variant:"danger",onConfirm:T,onCancel:()=>d(null)}),r&&s.jsx(gre,{project:a,onClose:()=>{i(!1),o(void 0)},onSave:b,isLoading:g.isPending||x.isPending})]})}function NKe(t){if(t<1)return`${Math.round(t*60)}m`;const e=Math.floor(t),n=Math.round((t-e)*60);return n>0?`${e}h ${n}m`:`${e}h`}function EKe(t){return t>=1e3?`${(t/1e3).toFixed(2)}kg`:`${Math.round(t)}g`}function TKe({status:t}){const e={active:"bg-bambu-green/20 text-bambu-green",completed:"bg-blue-500/20 text-blue-400",archived:"bg-bambu-gray/20 text-bambu-gray"},n=e[t]||e.active;return s.jsx("span",{className:`px-2 py-1 rounded text-sm font-medium ${n}`,children:t.charAt(0).toUpperCase()+t.slice(1)})}function KG({icon:t,label:e,value:n,subValue:r,hint:i,color:a="text-bambu-gray"}){return s.jsx(Mt,{children:s.jsx(Tt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",title:i,children:[s.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark ${a}`,children:s.jsx(t,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:e}),s.jsx("p",{className:"text-xl font-semibold text-white",children:n}),r&&s.jsx("p",{className:"text-xs text-bambu-gray/70",children:r})]})]})})})}function AKe({archives:t}){return t.length===0?s.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[s.jsx(Ll,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),s.jsx("p",{children:"No prints in this project yet"})]}):s.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-3",children:t.map(e=>s.jsxs(cs,{to:`/archives?search=${encodeURIComponent(e.print_name||"")}`,className:"group relative aspect-square rounded-lg bg-bambu-dark border border-bambu-dark-tertiary overflow-hidden hover:border-bambu-green transition-colors",children:[e.thumbnail_path?s.jsx("img",{src:pe.getArchiveThumbnail(e.id),alt:e.print_name||"Print",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:s.jsx(Ll,{className:"w-8 h-8"})}),e.status==="failed"&&s.jsx("div",{className:"absolute inset-0 bg-red-500/30 flex items-center justify-center",children:s.jsx(Ti,{className:"w-8 h-8 text-white"})}),e.status==="completed"&&s.jsx("div",{className:"absolute top-1 right-1",children:s.jsx(On,{className:"w-4 h-4 text-bambu-green"})}),s.jsx("div",{className:"absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/80 to-transparent p-2 opacity-0 group-hover:opacity-100 transition-opacity",children:s.jsx("p",{className:"text-xs text-white truncate",children:e.print_name||"Unknown"})})]},e.id))})}function CKe({priority:t}){const e={low:{color:"bg-gray-500/20 text-gray-400",label:"Low"},normal:{color:"bg-blue-500/20 text-blue-400",label:"Normal"},high:{color:"bg-orange-500/20 text-orange-400",label:"High"},urgent:{color:"bg-red-500/20 text-red-400",label:"Urgent"}},{color:n,label:r}=e[t]||e.normal;return s.jsxs("span",{className:`px-2 py-1 rounded text-xs font-medium flex items-center gap-1 ${n}`,children:[t==="urgent"&&s.jsx(jn,{className:"w-3 h-3"}),r]})}function kKe(t){return t?Jp(t,{year:"numeric",month:"short",day:"numeric"}):""}function vj(t){if(!t)return null;const e=pi(t);if(!e)return null;const n=new Date,r=Math.ceil((e.getTime()-n.getTime())/(1e3*60*60*24));return r<0?{color:"text-red-400",label:"Overdue"}:r===0?{color:"text-orange-400",label:"Due today"}:r<=3?{color:"text-yellow-400",label:`${r} days left`}:{color:"text-bambu-gray",label:`${r} days left`}}function jKe(){const{id:t}=HR(),e=cl(),n=an(),{showToast:r}=Gn(),[i,a]=w.useState(!1),[o,l]=w.useState(!1),[c,u]=w.useState(""),d=parseInt(t||"0",10),{data:f,isLoading:m,error:g}=ct({queryKey:["project",d],queryFn:()=>pe.getProject(d),enabled:d>0}),{data:x,isLoading:y}=ct({queryKey:["project-archives",d],queryFn:()=>pe.getProjectArchives(d),enabled:d>0}),{data:b,isLoading:v}=ct({queryKey:["project-bom",d],queryFn:()=>pe.getProjectBOM(d),enabled:d>0}),{data:M,isLoading:S}=ct({queryKey:["project-timeline",d],queryFn:()=>pe.getProjectTimeline(d,20),enabled:d>0}),{data:T}=ct({queryKey:["settings"],queryFn:pe.getSettings}),{data:E}=ct({queryKey:["project-folders",d],queryFn:()=>pe.getLibraryFoldersByProject(d),enabled:d>0}),k=T?.currency||"$",A=T?.time_format||"system",D=ft({mutationFn:Q=>pe.updateProject(d,Q),onSuccess:()=>{n.invalidateQueries({queryKey:["project",d]}),n.invalidateQueries({queryKey:["projects"]}),a(!1),l(!1),r("Project updated","success")},onError:Q=>{r(Q.message,"error")}}),C=()=>{u(f?.notes||""),l(!0)},P=()=>{D.mutate({notes:c})},F=()=>{l(!1),u("")},[L,X]=w.useState(""),[R,q]=w.useState(1),[Y,W]=w.useState(""),[Z,ie]=w.useState(""),[ae,U]=w.useState(""),[G,ee]=w.useState(!1),[re,le]=w.useState(!1),[he,fe]=w.useState({isOpen:!1,title:"",message:"",onConfirm:()=>{}}),Te=ft({mutationFn:Q=>pe.createBOMItem(d,Q),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",d]}),n.invalidateQueries({queryKey:["project",d]}),X(""),q(1),W(""),ie(""),U(""),ee(!1),r("Part added","success")},onError:Q=>r(Q.message,"error")}),ge=ft({mutationFn:({itemId:Q,data:Ne})=>pe.updateBOMItem(d,Q,Ne),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",d]}),n.invalidateQueries({queryKey:["project",d]})},onError:Q=>r(Q.message,"error")}),_e=ft({mutationFn:Q=>pe.deleteBOMItem(d,Q),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",d]}),n.invalidateQueries({queryKey:["project",d]}),r("Part removed","success")},onError:Q=>r(Q.message,"error")}),je=Q=>{Q.preventDefault(),L.trim()&&Te.mutate({name:L.trim(),quantity_needed:R,unit_price:Y?parseFloat(Y):void 0,sourcing_url:Z.trim()||void 0,remarks:ae.trim()||void 0})},ne=Q=>{const Ne=Q.is_complete?0:Q.quantity_needed;ge.mutate({itemId:Q.id,data:{quantity_acquired:Ne}})},Qe=(Q,Ne)=>{fe({isOpen:!0,title:"Delete Part",message:`Are you sure you want to delete "${Ne}"?`,onConfirm:()=>{fe(ke=>({...ke,isOpen:!1})),_e.mutate(Q)}})},B=ft({mutationFn:()=>pe.createTemplateFromProject(d),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),r("Template created","success")},onError:Q=>r(Q.message,"error")}),H=Q=>eg(Q,A,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"});if(m)return s.jsx("div",{className:"flex items-center justify-center py-24",children:s.jsx(ht,{className:"w-8 h-8 animate-spin text-bambu-green"})});if(g||!f)return s.jsxs("div",{className:"text-center py-24",children:[s.jsx("p",{className:"text-bambu-gray",children:g?`Error: ${g.message}`:"Project not found"}),s.jsx(Se,{variant:"secondary",className:"mt-4",onClick:()=>e("/projects"),children:"Back to Projects"})]});const K=f.stats,z=K?.progress_percent??0,V=K?.parts_progress_percent??0;return s.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[s.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[s.jsx(cs,{to:"/projects",className:"hover:text-white transition-colors",children:"Projects"}),s.jsx(Ws,{className:"w-4 h-4"}),s.jsx("span",{className:"text-white",children:f.name})]}),s.jsxs("div",{className:"flex items-start justify-between",children:[s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx("button",{onClick:()=>e("/projects"),className:"p-2 rounded-lg bg-bambu-card hover:bg-bambu-dark-tertiary transition-colors",children:s.jsx(BW,{className:"w-5 h-5 text-bambu-gray"})}),s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-4 h-4 rounded-full flex-shrink-0",style:{backgroundColor:f.color||"#6b7280"}}),s.jsxs("div",{children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:f.name}),f.description&&s.jsx("p",{className:"text-bambu-gray mt-1",children:f.description})]})]}),s.jsx(TKe,{status:f.status})]}),s.jsxs(Se,{onClick:()=>a(!0),children:[s.jsx(wf,{className:"w-4 h-4 mr-2"}),"Edit"]})]}),(f.target_count||f.target_parts_count)&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4 space-y-4",children:[f.target_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Plates Progress"}),s.jsxs("span",{className:"text-sm font-medium text-white",children:[K?.total_archives||0," / ",f.target_count," print jobs"]})]}),s.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(z,100)}%`,backgroundColor:z>=100?"#22c55e":f.color||"#6b7280"}})}),s.jsxs("div",{className:"flex justify-between mt-1",children:[s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[z.toFixed(0),"% complete"]}),K?.remaining_prints!=null&&K.remaining_prints>0&&s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[K.remaining_prints," remaining"]})]})]}),f.target_parts_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Parts Progress"}),s.jsxs("span",{className:"text-sm font-medium text-white",children:[K?.completed_prints||0," / ",f.target_parts_count," parts"]})]}),s.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(V,100)}%`,backgroundColor:V>=100?"#22c55e":f.color||"#6b7280"}})}),s.jsxs("div",{className:"flex justify-between mt-1",children:[s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[V.toFixed(0),"% complete"]}),K?.remaining_parts!=null&&K.remaining_parts>0&&s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[K.remaining_parts," remaining"]})]})]})]})}),K&&s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsx(Mt,{children:s.jsx(Tt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:s.jsx(Ll,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Print Jobs"}),s.jsxs("p",{className:"text-xl font-semibold text-white",children:[K.total_archives," ",s.jsx("span",{className:"text-sm font-normal text-bambu-gray",children:"total"})]}),K.failed_prints>0&&s.jsxs("p",{className:"text-sm text-status-error",children:[K.failed_prints," failed"]}),s.jsxs("p",{className:"text-sm text-bambu-gray",children:[K.completed_prints," parts printed"]})]})]})})}),s.jsx(KG,{icon:Yn,label:"Print Time",value:NKe(K.total_print_time_hours),color:"text-yellow-400"}),s.jsx(KG,{icon:zr,label:"Filament Used",value:EKe(K.total_filament_grams),color:"text-purple-400"})]}),K&&(K.estimated_cost>0||f.budget)&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsx("h2",{className:"text-lg font-semibold text-white mb-3",children:"Cost Tracking"}),s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Filament Cost"}),s.jsxs("p",{className:"text-lg font-semibold text-white",children:[k,K.estimated_cost.toFixed(2)]})]}),K.total_energy_kwh>0&&s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Energy"}),s.jsxs("p",{className:"text-lg font-semibold text-white",children:[K.total_energy_kwh.toFixed(2)," kWh",K.total_energy_cost>0&&s.jsxs("span",{className:"text-sm text-bambu-gray ml-1",children:["(",k,K.total_energy_cost.toFixed(2),")"]})]})]}),f.budget&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Budget"}),s.jsxs("p",{className:"text-lg font-semibold text-white",children:[k,f.budget.toFixed(2)]})]}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Remaining"}),s.jsxs("p",{className:`text-lg font-semibold ${f.budget-K.estimated_cost>=0?"text-bambu-green":"text-red-400"}`,children:[k,(f.budget-K.estimated_cost).toFixed(2)]})]})]})]})]})}),f.children&&f.children.length>0&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-3",children:[s.jsx(wue,{className:"w-5 h-5"}),"Sub-projects (",f.children.length,")"]}),s.jsx("div",{className:"space-y-2",children:f.children.map(Q=>s.jsxs(cs,{to:`/projects/${Q.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:Q.color||"#6b7280"}}),s.jsx("span",{className:"text-white",children:Q.name}),s.jsx("span",{className:`text-xs px-2 py-0.5 rounded ${Q.status==="completed"?"bg-status-ok/20 text-status-ok":Q.status==="archived"?"bg-bambu-gray/20 text-bambu-gray":"bg-blue-500/20 text-blue-400"}`,children:Q.status})]}),Q.progress_percent!==null&&s.jsxs("span",{className:"text-sm text-bambu-gray",children:[Q.progress_percent.toFixed(0),"%"]})]},Q.id))})]})}),f.parent_id&&f.parent_name&&s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(nl,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("span",{className:"text-bambu-gray",children:"Part of:"}),s.jsx(cs,{to:`/projects/${f.parent_id}`,className:"text-bambu-green hover:underline",children:f.parent_name})]}),(f.tags||f.due_date||f.priority!=="normal")&&s.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[f.priority&&f.priority!=="normal"&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-xs text-bambu-gray uppercase",children:"Priority:"}),s.jsx(CKe,{priority:f.priority})]}),f.due_date&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zr,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("span",{className:"text-sm text-white",children:kKe(f.due_date)}),vj(f.due_date)&&s.jsxs("span",{className:`text-xs ${vj(f.due_date).color}`,children:["(",vj(f.due_date).label,")"]})]}),f.tags&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zp,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("div",{className:"flex flex-wrap gap-1",children:f.tags.split(",").map((Q,Ne)=>s.jsx("span",{className:"px-2 py-0.5 bg-bambu-dark-tertiary text-bambu-gray text-xs rounded",children:Q.trim()},Ne))})]})]}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ba,{className:"w-5 h-5"}),"Notes"]}),o?s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{variant:"secondary",size:"sm",onClick:F,disabled:D.isPending,children:[s.jsx(Bt,{className:"w-4 h-4 mr-1"}),"Cancel"]}),s.jsxs(Se,{size:"sm",onClick:P,disabled:D.isPending,children:[D.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin mr-1"}):s.jsx(hs,{className:"w-4 h-4 mr-1"}),"Save"]})]}):s.jsxs(Se,{variant:"secondary",size:"sm",onClick:C,children:[s.jsx(wf,{className:"w-4 h-4 mr-1"}),"Edit"]})]}),o?s.jsx(kR,{content:c,onChange:u,placeholder:"Add notes about this project..."}):f.notes?s.jsx("div",{className:"prose prose-invert prose-sm max-w-none",dangerouslySetInnerHTML:{__html:f.notes}}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No notes yet. Click Edit to add notes."})]})}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsx("div",{className:"flex items-center justify-between mb-3",children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(el,{className:"w-5 h-5"}),"Files"]})}),s.jsxs("p",{className:"text-xs text-bambu-gray mb-3",children:[s.jsx(cs,{to:"/files",className:"text-bambu-green hover:underline",children:"Link folders from the File Manager"})," ","to this project for quick access."]}),E&&E.length>0?s.jsx("div",{className:"space-y-2",children:E.map(Q=>s.jsxs(cs,{to:`/files?folder=${Q.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[s.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[s.jsx(el,{className:"w-5 h-5 text-bambu-green flex-shrink-0"}),s.jsxs("div",{className:"min-w-0",children:[s.jsx("p",{className:"text-sm text-white truncate",children:Q.name}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[Q.file_count," file",Q.file_count!==1?"s":""]})]})]}),s.jsx(Ws,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"})]},Q.id))}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No folders linked. Go to File Manager and link a folder to this project."})]})}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(eq,{className:"w-5 h-5"}),"Bill of Materials",K&&K.bom_total_items>0&&s.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",K.bom_completed_items,"/",K.bom_total_items," acquired)"]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[b&&b.some(Q=>Q.is_complete)&&s.jsx("button",{onClick:()=>le(!re),className:`text-xs px-2 py-1 rounded transition-colors ${re?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:re?"Show all":"Hide done"}),!G&&s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>ee(!0),children:[s.jsx(Jn,{className:"w-4 h-4 mr-1"}),"Add Part"]})]})]}),G&&s.jsxs("form",{onSubmit:je,className:"bg-bambu-dark rounded-lg p-4 mb-4 space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[s.jsx("input",{type:"text",value:L,onChange:Q=>X(Q.target.value),className:"bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"Part name (e.g., M3x8 screws)",autoFocus:!0}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx("input",{type:"number",value:R,onChange:Q=>q(parseInt(Q.target.value)||1),className:"w-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white focus:outline-none focus:border-bambu-green",min:"1",placeholder:"Qty"}),s.jsx("input",{type:"number",step:"0.01",value:Y,onChange:Q=>W(Q.target.value),className:"flex-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:`Price (${k})`})]})]}),s.jsx("input",{type:"url",value:Z,onChange:Q=>ie(Q.target.value),className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"Sourcing URL (optional)"}),s.jsx("input",{type:"text",value:ae,onChange:Q=>U(Q.target.value),className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"Remarks (optional)"}),s.jsxs("div",{className:"flex justify-end gap-2",children:[s.jsx(Se,{type:"button",variant:"secondary",size:"sm",onClick:()=>ee(!1),children:"Cancel"}),s.jsx(Se,{type:"submit",size:"sm",disabled:!L.trim()||Te.isPending,children:Te.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Add Part"})]})]}),v?s.jsx("div",{className:"flex items-center justify-center py-4",children:s.jsx(ht,{className:"w-6 h-6 animate-spin text-bambu-green"})}):b&&b.length>0?s.jsxs("div",{className:"space-y-2",children:[b.filter(Q=>!re||!Q.is_complete).map(Q=>s.jsx("div",{className:`p-3 rounded-lg transition-colors ${Q.is_complete?"bg-status-ok/10":"bg-bambu-dark"}`,children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("button",{onClick:()=>ne(Q),disabled:ge.isPending,className:`w-5 h-5 mt-0.5 rounded border-2 flex items-center justify-center transition-colors flex-shrink-0 ${Q.is_complete?"bg-status-ok border-status-ok text-white":"border-bambu-gray hover:border-bambu-green"}`,children:Q.is_complete&&s.jsx(On,{className:"w-3 h-3"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center justify-between gap-2",children:[s.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[s.jsxs("p",{className:`text-sm font-medium ${Q.is_complete?"text-bambu-gray line-through":"text-white"}`,children:[Q.name,s.jsxs("span",{className:"text-bambu-gray font-normal ml-2",children:["x",Q.quantity_needed]})]}),Q.unit_price!==null&&s.jsxs("span",{className:"text-xs text-bambu-green whitespace-nowrap",children:[k,(Q.unit_price*Q.quantity_needed).toFixed(2)]})]}),s.jsx("button",{onClick:()=>Qe(Q.id,Q.name),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-red-400 transition-colors flex-shrink-0",title:"Delete",children:s.jsx(ln,{className:"w-4 h-4"})})]}),Q.sourcing_url&&s.jsxs("a",{href:Q.sourcing_url,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 mt-1 text-xs text-blue-400 hover:text-blue-300 transition-colors",onClick:Ne=>Ne.stopPropagation(),children:[s.jsx(Ur,{className:"w-3 h-3 flex-shrink-0"}),s.jsx("span",{className:"truncate",children:(()=>{try{return new URL(Q.sourcing_url).hostname.replace("www.","")}catch{return Q.sourcing_url}})()})]}),Q.remarks&&s.jsx("p",{className:"mt-1 text-xs text-bambu-gray/80 italic",children:Q.remarks})]})]})},Q.id)),b.some(Q=>Q.unit_price!==null)&&s.jsxs("div",{className:"pt-2 mt-2 border-t border-bambu-dark-tertiary flex justify-between text-sm",children:[s.jsx("span",{className:"text-bambu-gray",children:"Total cost:"}),s.jsxs("span",{className:"text-white font-medium",children:[k,b.reduce((Q,Ne)=>Q+(Ne.unit_price||0)*Ne.quantity_needed,0).toFixed(2)]})]})]}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No parts in the bill of materials. Add hardware, electronics, or other components to track what needs to be sourced."})]})}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsx("div",{className:"flex items-center justify-between mb-3",children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(hw,{className:"w-5 h-5"}),"Activity Timeline"]})}),S?s.jsx("div",{className:"flex items-center justify-center py-4",children:s.jsx(ht,{className:"w-6 h-6 animate-spin text-bambu-green"})}):M&&M.length>0?s.jsx("div",{className:"space-y-3",children:M.slice(0,10).map((Q,Ne)=>s.jsxs("div",{className:"flex gap-3",children:[s.jsxs("div",{className:`w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 ${Q.event_type==="print_completed"?"bg-status-ok/20 text-status-ok":Q.event_type==="print_failed"?"bg-status-error/20 text-status-error":Q.event_type==="print_started"?"bg-yellow-500/20 text-yellow-400":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[Q.event_type==="print_completed"&&s.jsx(On,{className:"w-4 h-4"}),Q.event_type==="print_failed"&&s.jsx(Ti,{className:"w-4 h-4"}),Q.event_type==="print_started"&&s.jsx(zr,{className:"w-4 h-4"}),Q.event_type==="queued"&&s.jsx(Bj,{className:"w-4 h-4"}),Q.event_type==="project_created"&&s.jsx(Jn,{className:"w-4 h-4"})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("p",{className:"text-sm text-white",children:Q.title}),Q.description&&s.jsx("p",{className:"text-xs text-bambu-gray truncate",children:Q.description}),s.jsx("p",{className:"text-xs text-bambu-gray/70",children:H(Q.timestamp)})]})]},Ne))}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No activity yet."})]})}),!f.is_template&&s.jsx("div",{className:"flex justify-end",children:s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>B.mutate(),disabled:B.isPending,children:[B.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin mr-2"}):s.jsx(Jo,{className:"w-4 h-4 mr-2"}),"Save as Template"]})}),K&&(K.queued_prints>0||K.in_progress_prints>0)&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Bj,{className:"w-5 h-5"}),"Queue"]}),s.jsx(cs,{to:`/queue?project=${d}`,className:"text-sm text-bambu-green hover:underline",children:"View all"})]}),s.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[K.in_progress_prints>0&&s.jsxs("span",{className:"text-yellow-400",children:[K.in_progress_prints," printing"]}),K.queued_prints>0&&s.jsxs("span",{className:"text-bambu-gray",children:[K.queued_prints," queued"]})]})]})}),s.jsxs("div",{children:[s.jsx("div",{className:"flex items-center justify-between mb-4",children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ll,{className:"w-5 h-5"}),"Prints (",x?.length||0,")"]})}),y?s.jsx("div",{className:"flex items-center justify-center py-8",children:s.jsx(ht,{className:"w-6 h-6 animate-spin text-bambu-green"})}):s.jsx(AKe,{archives:x||[]})]}),i&&s.jsx(gre,{project:{...f,archive_count:K?.total_archives||0,total_items:K?.total_items||0,completed_count:K?.completed_prints||0,failed_count:K?.failed_prints||0,queue_count:K?.queued_prints||0,progress_percent:K?.progress_percent||null,archives:[]},onClose:()=>a(!1),onSave:Q=>D.mutate(Q),isLoading:D.isPending}),he.isOpen&&s.jsx(Un,{title:he.title,message:he.message,confirmText:"Delete",variant:"danger",onConfirm:he.onConfirm,onCancel:()=>fe(Q=>({...Q,isOpen:!1}))})]})}function Ep(t){return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(1)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`}function PKe(t){if(!t)return"-";const e=Math.floor(t/3600),n=Math.floor(t%3600/60);return e>0?`${e}h ${n}m`:`${n}m`}function RKe({parentId:t,onClose:e,onSave:n,isLoading:r}){const[i,a]=w.useState(""),o=l=>{l.preventDefault(),n({name:i.trim(),parent_id:t})};return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"New Folder"})}),s.jsxs("form",{onSubmit:o,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Folder Name"}),s.jsx("input",{type:"text",value:i,onChange:l=>a(l.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., Functional Parts",autoFocus:!0,required:!0})]}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!i.trim()||r,children:r?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Create"})]})]})]})})}function DKe({type:t,currentName:e,onClose:n,onSave:r,isLoading:i}){const[a,o]=w.useState(e),l=c=>{c.preventDefault(),a.trim()&&a.trim()!==e&&r(a.trim())};return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Rename ",t==="file"?"File":"Folder"]})}),s.jsxs("form",{onSubmit:l,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Name"}),s.jsx("input",{type:"text",value:a,onChange:c=>o(c.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",autoFocus:!0,required:!0})]}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:n,children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!a.trim()||a.trim()===e||i,children:i?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Rename"})]})]})]})})}function OKe({folders:t,selectedFiles:e,currentFolderId:n,onClose:r,onMove:i,isLoading:a}){const[o,l]=w.useState(null),c=(d,f=0)=>{const m=[];for(const g of d)m.push({id:g.id,name:g.name,depth:f}),g.children.length>0&&m.push(...c(g.children,f+1));return m},u=[{id:null,name:"Root (No Folder)",depth:0},...c(t)];return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Move ",e.length," File(s)"]})}),s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1",children:u.map(d=>s.jsxs("button",{onClick:()=>l(d.id),disabled:d.id===n,className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${o===d.id?"bg-bambu-green/20 text-bambu-green":d.id===n?"opacity-50 cursor-not-allowed text-bambu-gray":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${12+d.depth*16}px`},children:[s.jsx(el,{className:"w-4 h-4"}),d.name,d.id===n&&s.jsx("span",{className:"text-xs text-bambu-gray ml-auto",children:"(current)"})]},d.id??"root"))}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:r,children:"Cancel"}),s.jsx(Se,{onClick:()=>i(o),disabled:a,children:a?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Move"})]})]})]})})}function LKe({folder:t,onClose:e,onLink:n,isLoading:r}){const[i,a]=w.useState("project"),[o,l]=w.useState(t.project_id||t.archive_id||null);w.useState(()=>{t.archive_id&&a("archive")});const{data:c}=ct({queryKey:["projects"],queryFn:()=>pe.getProjects()}),{data:u}=ct({queryKey:["archives-for-link"],queryFn:()=>pe.getArchives(void 0,void 0,100)}),d=()=>{n(i==="project"?{project_id:o,archive_id:0}:{project_id:0,archive_id:o})},f=()=>{n({project_id:0,archive_id:0})},m=t.project_id||t.archive_id;return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Pl,{className:"w-5 h-5 text-bambu-green"}),"Link Folder"]}),s.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:s.jsx(Bt,{className:"w-5 h-5 text-bambu-gray"})})]}),s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsxs("p",{className:"text-sm text-bambu-gray",children:['Link "',s.jsx("span",{className:"text-white",children:t.name}),'" to a project or archive for quick access.']}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("button",{onClick:()=>{a("project"),l(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${i==="project"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[s.jsx(HW,{className:"w-4 h-4"}),"Project"]}),s.jsxs("button",{onClick:()=>{a("archive"),l(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${i==="archive"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[s.jsx(Va,{className:"w-4 h-4"}),"Archive"]})]}),s.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1 bg-bambu-dark rounded-lg p-2",children:i==="project"?c&&c.length>0?c.map(g=>s.jsxs("button",{onClick:()=>l(g.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${o===g.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[s.jsx("div",{className:"w-3 h-3 rounded-full flex-shrink-0",style:{backgroundColor:g.color||"#00ae42"}}),s.jsx("span",{className:"truncate",children:g.name})]},g.id)):s.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:"No projects found"}):u&&u.length>0?u.map(g=>s.jsxs("button",{onClick:()=>l(g.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${o===g.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[s.jsx(nf,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),s.jsx("span",{className:"truncate",children:g.print_name||g.filename})]},g.id)):s.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:"No archives found"})})]}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-between",children:[m&&s.jsxs(Se,{variant:"danger",onClick:f,disabled:r,children:[s.jsx(aD,{className:"w-4 h-4 mr-2"}),"Unlink"]}),s.jsxs("div",{className:`flex gap-2 ${m?"":"ml-auto"}`,children:[s.jsx(Se,{variant:"secondary",onClick:e,children:"Cancel"}),s.jsx(Se,{onClick:d,disabled:!o||r,children:r?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Link"})]})]})]})})}function IKe({folderId:t,onClose:e,onUploadComplete:n}){const[r,i]=w.useState([]),[a,o]=w.useState(!1),[l,c]=w.useState(!1),[u,d]=w.useState(!0),[f,m]=w.useState(!1),g=w.useRef(null),x=P=>{P.preventDefault(),o(!0)},y=P=>{P.preventDefault(),o(!1)},b=P=>{P.preventDefault(),o(!1);const F=Array.from(P.dataTransfer.files);M(F)},v=P=>{P.target.files&&M(Array.from(P.target.files))},M=P=>{const F=P.map(L=>({file:L,status:"pending",isZip:L.name.toLowerCase().endsWith(".zip")}));i(L=>[...L,...F])},S=P=>{i(F=>F.filter((L,X)=>X!==P))},T=r.some(P=>P.isZip&&P.status==="pending"),E=async()=>{if(r.length!==0){c(!0);for(let P=0;PF.map((L,X)=>X===P?{...L,status:"uploading"}:L));try{if(r[P].isZip){const F=await pe.extractZipFile(r[P].file,t,u,f);i(L=>L.map((X,R)=>R===P?{...X,status:F.errors.length>0&&F.extracted===0?"error":"success",extractedCount:F.extracted,error:F.errors.length>0?`${F.errors.length} files failed`:void 0}:X))}else await pe.uploadLibraryFile(r[P].file,t),i(F=>F.map((L,X)=>X===P?{...L,status:"success"}:L))}catch(F){i(L=>L.map((X,R)=>R===P?{...X,status:"error",error:F instanceof Error?F.message:"Upload failed"}:X))}}c(!1),n(),e()}},k=r.filter(P=>P.status==="pending").length,A=r.filter(P=>P.status==="success").length,D=r.filter(P=>P.status==="error").length,C=r.length>0&&k===0&&!l;return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Upload Files"}),s.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:s.jsx(Bt,{className:"w-5 h-5 text-bambu-gray"})})]}),s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsxs("div",{onDragOver:x,onDragLeave:y,onDrop:b,onClick:()=>g.current?.click(),className:`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors ${a?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,children:[s.jsx(yi,{className:`w-10 h-10 mx-auto mb-3 ${a?"text-bambu-green":"text-bambu-gray"}`}),s.jsx("p",{className:"text-white font-medium",children:a?"Drop files here":"Drag & drop files here"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"or click to browse"}),s.jsx("p",{className:"text-xs text-bambu-gray/70 mt-2",children:"All file types supported. ZIP files will be extracted."})]}),s.jsx("input",{ref:g,type:"file",multiple:!0,accept:"*",className:"hidden",onChange:v}),T&&s.jsx("div",{className:"p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(Va,{className:"w-5 h-5 text-blue-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"flex-1",children:[s.jsx("p",{className:"text-sm text-blue-300 font-medium",children:"ZIP files detected"}),s.jsx("p",{className:"text-xs text-blue-300/70 mt-1",children:"ZIP files will be extracted. Choose how to handle folder structure:"}),s.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:u,onChange:P=>d(P.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsx("span",{className:"text-sm text-white",children:"Preserve folder structure from ZIP"})]}),s.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:f,onChange:P=>m(P.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsx("span",{className:"text-sm text-white",children:"Create folder from ZIP filename"})]})]})]})}),r.length>0&&s.jsx("div",{className:"max-h-48 overflow-y-auto space-y-2",children:r.map((P,F)=>s.jsxs("div",{className:"flex items-center gap-3 p-2 bg-bambu-dark rounded-lg",children:[P.isZip?s.jsx(Va,{className:"w-4 h-4 text-blue-400 flex-shrink-0"}):s.jsx(C2,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("p",{className:"text-sm text-white truncate",children:P.file.name}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[(P.file.size/1024/1024).toFixed(2)," MB",P.isZip&&P.status==="pending"&&s.jsx("span",{className:"text-blue-400 ml-2",children:"• Will be extracted"}),P.extractedCount!==void 0&&s.jsxs("span",{className:"text-green-400 ml-2",children:["• ",P.extractedCount," files extracted"]})]})]}),P.status==="pending"&&s.jsx("button",{onClick:()=>S(F),className:"p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Bt,{className:"w-4 h-4 text-bambu-gray"})}),P.status==="uploading"&&s.jsx(ht,{className:"w-4 h-4 text-bambu-green animate-spin"}),P.status==="success"&&s.jsx(On,{className:"w-4 h-4 text-green-500"}),P.status==="error"&&s.jsx("span",{title:P.error,children:s.jsx(Ti,{className:"w-4 h-4 text-red-500"})})]},F))}),C&&s.jsx("div",{className:"p-3 bg-bambu-dark rounded-lg",children:s.jsxs("p",{className:"text-sm text-white",children:["Upload complete: ",A," succeeded",D>0&&s.jsxs("span",{className:"text-red-400",children:[", ",D," failed"]})]})})]}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-end gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:e,children:C?"Close":"Cancel"}),!C&&s.jsx(Se,{onClick:E,disabled:k===0||l,children:l?s.jsxs(s.Fragment,{children:[s.jsx(ht,{className:"w-4 h-4 mr-2 animate-spin"}),"Uploading..."]}):s.jsxs(s.Fragment,{children:[s.jsx(yi,{className:"w-4 h-4 mr-2"}),"Upload ",k>0?`(${k})`:""]})})]})]})})}function xre({folder:t,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:a,depth:o=0}){const[l,c]=w.useState(!0),[u,d]=w.useState(!1),f=t.children.length>0,m=t.project_id||t.archive_id;return s.jsxs("div",{children:[s.jsxs("div",{className:`group flex items-center gap-1 px-2 py-1.5 rounded cursor-pointer transition-colors ${e===t.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${8+o*12}px`},onClick:()=>n(t.id),children:[f?s.jsx("button",{onClick:g=>{g.stopPropagation(),c(!l)},className:"p-0.5 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Ws,{className:`w-3.5 h-3.5 transition-transform ${l?"rotate-90":""}`})}):s.jsx("div",{className:"w-4.5"}),s.jsx(el,{className:"w-4 h-4 text-bambu-green flex-shrink-0"}),s.jsx("span",{className:"text-sm truncate flex-1",children:t.name}),m&&s.jsxs("button",{onClick:g=>{g.stopPropagation(),i(t)},className:"flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-blue-500/20 text-blue-400 hover:bg-blue-500/30 transition-colors",title:`${t.project_name?`Project: ${t.project_name}`:`Archive: ${t.archive_name}`} (click to change)`,children:[s.jsx(Pl,{className:"w-3 h-3"}),t.project_name?s.jsx(HW,{className:"w-3 h-3"}):s.jsx(Va,{className:"w-3 h-3"})]}),t.file_count>0&&s.jsx("span",{className:"text-xs text-bambu-gray",children:t.file_count}),!m&&s.jsx("button",{onClick:g=>{g.stopPropagation(),i(t)},className:"p-1 rounded hover:bg-bambu-dark-tertiary",title:"Link to project or archive",children:s.jsx(Pl,{className:"w-3.5 h-3.5 text-bambu-gray hover:text-bambu-green"})}),s.jsx("div",{className:"flex items-center gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity",onClick:g=>g.stopPropagation(),children:s.jsxs("div",{className:"relative",children:[s.jsx("button",{onClick:()=>d(!u),className:"p-1 rounded hover:bg-bambu-dark-tertiary",children:s.jsx(wd,{className:"w-3.5 h-3.5 text-bambu-gray"})}),u&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>d(!1)}),s.jsxs("div",{className:"absolute right-0 top-full mt-1 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]",children:[s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{a(t),d(!1)},children:[s.jsx(Wa,{className:"w-3.5 h-3.5"}),"Rename"]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{i(t),d(!1)},children:[s.jsx(Pl,{className:"w-3.5 h-3.5"}),m?"Change Link...":"Link to..."]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-red-400 hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{r(t.id),d(!1)},children:[s.jsx(ln,{className:"w-3.5 h-3.5"}),"Delete"]})]})]})]})})]}),f&&l&&s.jsx("div",{children:t.children.map(g=>s.jsx(xre,{folder:g,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:a,depth:o+1},g.id))})]})}function FR(t){const e=t.toLowerCase();return e.endsWith(".gcode")||e.includes(".gcode.")}function FKe({file:t,isSelected:e,onSelect:n,onDelete:r,onDownload:i,onAddToQueue:a,onPrint:o,onRename:l}){const[c,u]=w.useState(!1);return s.jsxs("div",{className:`group relative bg-bambu-card rounded-lg border transition-all cursor-pointer overflow-hidden ${e?"border-bambu-green ring-1 ring-bambu-green":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,onClick:()=>n(t.id),children:[s.jsxs("div",{className:"aspect-square bg-bambu-dark flex items-center justify-center overflow-hidden",children:[t.thumbnail_path?s.jsx("img",{src:pe.getLibraryFileThumbnailUrl(t.id),alt:t.filename,className:"w-full h-full object-cover"}):s.jsx(nf,{className:"w-12 h-12 text-bambu-gray/30"}),s.jsx("div",{className:`absolute top-2 right-2 text-xs px-1.5 py-0.5 rounded font-medium ${t.file_type==="3mf"?"bg-bambu-green/90 text-white":t.file_type==="gcode"?"bg-blue-500/90 text-white":t.file_type==="stl"?"bg-purple-500/90 text-white":"bg-bambu-gray/90 text-white"}`,children:t.file_type.toUpperCase()})]}),s.jsxs("div",{className:"p-3",children:[s.jsx("h3",{className:"text-sm font-medium text-white truncate",title:t.print_name||t.filename,children:t.print_name||t.filename}),s.jsxs("div",{className:"flex items-center gap-3 mt-1 text-xs text-bambu-gray",children:[s.jsx("span",{children:Ep(t.file_size)}),t.print_time_seconds&&s.jsxs("span",{className:"flex items-center gap-1",children:[s.jsx(Yn,{className:"w-3 h-3"}),PKe(t.print_time_seconds)]})]}),t.print_count>0&&s.jsxs("div",{className:"mt-1 text-xs text-bambu-green",children:["Printed ",t.print_count,"x"]})]}),s.jsxs("div",{className:"absolute bottom-2 right-2 opacity-100 md:opacity-0 md:group-hover:opacity-100 transition-opacity",onClick:d=>d.stopPropagation(),children:[s.jsx("button",{onClick:()=>u(!c),className:"p-1.5 rounded bg-bambu-dark-secondary/90 hover:bg-bambu-dark-tertiary",children:s.jsx(wd,{className:"w-4 h-4 text-bambu-gray"})}),c&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>u(!1)}),s.jsxs("div",{className:"absolute right-0 bottom-8 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[140px]",children:[o&&FR(t.filename)&&s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-bambu-green hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{o(t),u(!1)},children:[s.jsx(zr,{className:"w-3.5 h-3.5"}),"Print"]}),a&&FR(t.filename)&&s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{a(t.id),u(!1)},children:[s.jsx(Yn,{className:"w-3.5 h-3.5"}),"Add to Queue"]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{i(t.id),u(!1)},children:[s.jsx(ei,{className:"w-3.5 h-3.5"}),"Download"]}),l&&s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{l(t),u(!1)},children:[s.jsx(Wa,{className:"w-3.5 h-3.5"}),"Rename"]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-red-400 hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{r(t.id),u(!1)},children:[s.jsx(ln,{className:"w-3.5 h-3.5"}),"Delete"]})]})]})]}),s.jsx("div",{className:`absolute top-2 left-2 w-5 h-5 rounded border-2 flex items-center justify-center transition-all ${e?"bg-bambu-green border-bambu-green":"border-white/30 bg-black/30 opacity-100 md:opacity-0 md:group-hover:opacity-100"}`,children:e&&s.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})})]})}function UKe(){const t=an(),{showToast:e}=Gn(),[n]=wW(),r=n.get("folder"),i=r?parseInt(r,10):null,[a,o]=w.useState(i),[l,c]=w.useState([]),[u,d]=w.useState(!1),[f,m]=w.useState(!1),[g,x]=w.useState(!1),[y,b]=w.useState(null),[v,M]=w.useState(null),[S,T]=w.useState(null),[E,k]=w.useState(null),[A,D]=w.useState(null),[C,P]=w.useState(()=>localStorage.getItem("library-view-mode")||"grid"),[F,L]=w.useState(""),[X,R]=w.useState("all"),[q,Y]=w.useState("date"),[W,Z]=w.useState("desc");w.useEffect(()=>{const oe=n.get("folder");if(oe){const Ae=parseInt(oe,10);o(Ae)}},[n]);const{data:ie}=ct({queryKey:["settings"],queryFn:()=>pe.getSettings()}),{data:ae,isLoading:U}=ct({queryKey:["library-folders"],queryFn:()=>pe.getLibraryFolders()}),{data:G,isLoading:ee}=ct({queryKey:["library-files",a],queryFn:()=>pe.getLibraryFiles(a,a===null)}),{data:re}=ct({queryKey:["library-stats"],queryFn:()=>pe.getLibraryStats()}),le=w.useMemo(()=>{if(!G)return[];const oe=new Set(G.map(Ae=>Ae.file_type));return Array.from(oe).sort()},[G]),he=w.useMemo(()=>{if(!G)return[];let oe=[...G];if(F.trim()){const Ae=F.toLowerCase();oe=oe.filter(De=>De.filename.toLowerCase().includes(Ae)||De.print_name&&De.print_name.toLowerCase().includes(Ae))}return X!=="all"&&(oe=oe.filter(Ae=>Ae.file_type===X)),oe.sort((Ae,De)=>{let Ze=0;switch(q){case"name":Ze=(Ae.print_name||Ae.filename).localeCompare(De.print_name||De.filename);break;case"date":Ze=new Date(Ae.created_at).getTime()-new Date(De.created_at).getTime();break;case"size":Ze=Ae.file_size-De.file_size;break;case"type":Ze=Ae.file_type.localeCompare(De.file_type);break;case"prints":Ze=Ae.print_count-De.print_count;break}return W==="asc"?Ze:-Ze}),oe},[G,F,X,q,W]),fe=w.useMemo(()=>{if(!re||!ie)return!1;const oe=(ie.library_disk_warning_gb||5)*1024*1024*1024;return re.disk_free_bytespe.createLibraryFolder(oe),onSuccess:()=>{t.invalidateQueries({queryKey:["library-folders"]}),d(!1),e("Folder created","success")},onError:oe=>e(oe.message,"error")}),ge=ft({mutationFn:oe=>pe.deleteLibraryFolder(oe),onSuccess:()=>{t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-stats"]}),a===v?.id&&o(null),M(null),e("Folder deleted","success")},onError:oe=>{M(null),e(oe.message,"error")}}),_e=ft({mutationFn:oe=>pe.deleteLibraryFile(oe),onSuccess:()=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-stats"]}),c(oe=>oe.filter(Ae=>Ae!==v?.id)),M(null),e("File deleted","success")},onError:oe=>{M(null),e(oe.message,"error")}}),je=ft({mutationFn:oe=>pe.bulkDeleteLibrary(oe,[]),onSuccess:(oe,Ae)=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-stats"]}),e(`Deleted ${Ae.length} files`,"success"),c([]),M(null)},onError:oe=>{M(null),e(oe.message,"error")}}),ne=ft({mutationFn:({fileIds:oe,folderId:Ae})=>pe.moveLibraryFiles(oe,Ae),onSuccess:()=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),c([]),m(!1),e("Files moved","success")},onError:oe=>e(oe.message,"error")}),Qe=ft({mutationFn:({id:oe,data:Ae})=>pe.updateLibraryFolder(oe,Ae),onSuccess:(oe,Ae)=>{t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["project-folders"]}),t.invalidateQueries({queryKey:["archive-folders"]}),b(null);const De=Ae.data.project_id===0&&Ae.data.archive_id===0;e(De?"Folder unlinked":"Folder linked","success")},onError:oe=>e(oe.message,"error")}),B=ft({mutationFn:oe=>pe.addLibraryFilesToQueue(oe),onSuccess:oe=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["queue"]}),t.invalidateQueries({queryKey:["archives"]}),c([]),oe.added.length>0&&oe.errors.length===0?e(`Added ${oe.added.length} file${oe.added.length>1?"s":""} to queue`,"success"):oe.added.length>0&&oe.errors.length>0?e(`Added ${oe.added.length} file${oe.added.length>1?"s":""}, ${oe.errors.length} failed`,"success"):e(`Failed to add files: ${oe.errors[0]?.error||"Unknown error"}`,"error")},onError:oe=>e(oe.message,"error")}),H=ft({mutationFn:({id:oe,filename:Ae})=>pe.updateLibraryFile(oe,{filename:Ae}),onSuccess:()=>{t.invalidateQueries({queryKey:["library-files"]}),D(null),e("File renamed","success")},onError:oe=>{D(null),e(oe.message,"error")}}),K=ft({mutationFn:({id:oe,name:Ae})=>pe.updateLibraryFolder(oe,{name:Ae}),onSuccess:()=>{t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-files"]}),D(null),e("Folder renamed","success")},onError:oe=>{D(null),e(oe.message,"error")}}),z=w.useCallback(oe=>{const Ae=oe.toLowerCase();return Ae.endsWith(".gcode")||Ae.includes(".gcode.")},[]),V=w.useMemo(()=>G?G.filter(oe=>l.includes(oe.id)&&z(oe.filename)):[],[G,l,z]),Q=w.useCallback(oe=>{c(Ae=>Ae.includes(oe)?Ae.filter(De=>De!==oe):[...Ae,oe])},[]),Ne=w.useCallback(()=>{he.length>0&&c(he.map(oe=>oe.id))},[he]),ke=w.useCallback(()=>{c([])},[]),Me=()=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-stats"]})},$=oe=>{window.open(pe.getLibraryFileDownloadUrl(oe),"_blank")},I=()=>{v&&(v.type==="file"?_e.mutate(v.id):v.type==="folder"?ge.mutate(v.id):v.type==="bulk"&&je.mutate(l))},J=ge.isPending||_e.isPending||je.isPending,xe=oe=>{P(oe),localStorage.setItem("library-view-mode",oe)},ve=U||ee;return s.jsxs("div",{className:"p-4 md:p-8 h-[calc(100vh-64px)] flex flex-col",children:[s.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6",children:[s.jsxs("div",{children:[s.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:s.jsx(el,{className:"w-6 h-6 text-bambu-green"})}),"File Manager"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:"Organize and manage your print files"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"flex items-center bg-bambu-dark rounded-lg p-1",children:[s.jsx("button",{onClick:()=>xe("grid"),className:`p-1.5 rounded transition-colors ${C==="grid"?"bg-bambu-card text-white":"text-bambu-gray hover:text-white"}`,title:"Grid view",children:s.jsx(D2,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>xe("list"),className:`p-1.5 rounded transition-colors ${C==="list"?"bg-bambu-card text-white":"text-bambu-gray hover:text-white"}`,title:"List view",children:s.jsx(O2,{className:"w-4 h-4"})})]}),s.jsxs(Se,{variant:"secondary",onClick:()=>d(!0),children:[s.jsx(vue,{className:"w-4 h-4 mr-2"}),"New Folder"]}),s.jsxs(Se,{onClick:()=>x(!0),children:[s.jsx(yi,{className:"w-4 h-4 mr-2"}),"Upload"]})]})]}),fe&&re&&ie&&s.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-amber-500/10 border border-amber-500/30 rounded-lg",children:[s.jsx(jn,{className:"w-5 h-5 text-amber-500 flex-shrink-0"}),s.jsxs("div",{className:"flex-1",children:[s.jsx("p",{className:"text-sm text-amber-500 font-medium",children:"Low disk space warning"}),s.jsxs("p",{className:"text-xs text-amber-500/80",children:["Only ",Ep(re.disk_free_bytes)," free of ",Ep(re.disk_total_bytes)," total. Threshold is set to ",ie.library_disk_warning_gb," GB in settings."]})]})]}),re&&s.jsxs("div",{className:"flex items-center gap-6 mb-6 p-3 bg-bambu-card rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(C2,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"text-bambu-gray",children:"Files:"}),s.jsx("span",{className:"text-white font-medium",children:re.total_files})]}),s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(el,{className:"w-4 h-4 text-blue-400"}),s.jsx("span",{className:"text-bambu-gray",children:"Folders:"}),s.jsx("span",{className:"text-white font-medium",children:re.total_folders})]}),s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(P2,{className:"w-4 h-4 text-amber-400"}),s.jsx("span",{className:"text-bambu-gray",children:"Size:"}),s.jsx("span",{className:"text-white font-medium",children:Ep(re.total_size_bytes)})]}),s.jsxs("div",{className:"flex items-center gap-2 text-sm ml-auto",children:[s.jsx("span",{className:"text-bambu-gray",children:"Free:"}),s.jsx("span",{className:`font-medium ${fe?"text-amber-500":"text-white"}`,children:Ep(re.disk_free_bytes)})]})]}),s.jsxs("div",{className:"flex-1 flex gap-6 min-h-0",children:[s.jsxs("div",{className:"w-64 flex-shrink-0 bg-bambu-card rounded-lg border border-bambu-dark-tertiary overflow-hidden flex flex-col",children:[s.jsx("div",{className:"p-3 border-b border-bambu-dark-tertiary",children:s.jsx("h2",{className:"text-sm font-medium text-white",children:"Folders"})}),s.jsxs("div",{className:"flex-1 overflow-y-auto p-2",children:[s.jsxs("div",{className:`flex items-center gap-2 px-2 py-1.5 rounded cursor-pointer transition-colors ${a===null?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,onClick:()=>o(null),children:[s.jsx(nf,{className:"w-4 h-4"}),s.jsx("span",{className:"text-sm",children:"All Files"})]}),ae?.map(oe=>s.jsx(xre,{folder:oe,selectedFolderId:a,onSelect:o,onDelete:Ae=>M({type:"folder",id:Ae}),onLink:b,onRename:Ae=>D({type:"folder",id:Ae.id,name:Ae.name})},oe.id))]})]}),s.jsxs("div",{className:"flex-1 flex flex-col min-w-0",children:[G&&G.length>0&&s.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-bambu-card rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"relative flex-1 max-w-xs",children:[s.jsx(ba,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",placeholder:"Search files...",value:F,onChange:oe=>L(oe.target.value),className:"w-full pl-9 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(k2,{className:"w-4 h-4 text-bambu-gray"}),s.jsxs("select",{value:X,onChange:oe=>R(oe.target.value),className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"all",children:"All types"}),le.map(oe=>s.jsx("option",{value:oe,children:oe.toUpperCase()},oe))]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("select",{value:q,onChange:oe=>Y(oe.target.value),className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"date",children:"Date"}),s.jsx("option",{value:"name",children:"Name"}),s.jsx("option",{value:"size",children:"Size"}),s.jsx("option",{value:"type",children:"Type"}),s.jsx("option",{value:"prints",children:"Prints"})]}),s.jsx("button",{onClick:()=>Z(oe=>oe==="asc"?"desc":"asc"),className:"p-1.5 rounded bg-bambu-dark border border-bambu-dark-tertiary hover:border-bambu-green transition-colors",title:W==="asc"?"Ascending":"Descending",children:W==="asc"?s.jsx(Zle,{className:"w-4 h-4 text-white"}):s.jsx(Wle,{className:"w-4 h-4 text-white"})})]}),(F||X!=="all")&&s.jsxs("span",{className:"text-sm text-bambu-gray",children:[he.length," of ",G.length," files"]})]}),he.length>0&&s.jsxs("div",{className:"flex items-center gap-2 mb-4 p-2 bg-bambu-card rounded-lg border border-bambu-dark-tertiary",children:[l.length===he.length&&l.length>0?s.jsxs(Se,{variant:"secondary",size:"sm",onClick:ke,children:[s.jsx(al,{className:"w-4 h-4 mr-1"}),"Deselect All"]}):s.jsxs(Se,{variant:"secondary",size:"sm",onClick:Ne,children:[s.jsx(il,{className:"w-4 h-4 mr-1"}),"Select All"]}),l.length>0&&s.jsxs(s.Fragment,{children:[s.jsxs("span",{className:"text-sm text-bambu-gray ml-2",children:[l.length," selected"]}),s.jsx("div",{className:"flex-1"}),V.length===1&&s.jsxs(Se,{variant:"primary",size:"sm",onClick:()=>k(V[0]),children:[s.jsx(rl,{className:"w-4 h-4 mr-1"}),"Print"]}),V.length>0&&s.jsxs(Se,{variant:V.length===1?"secondary":"primary",size:"sm",onClick:()=>B.mutate(V.map(oe=>oe.id)),disabled:B.isPending,children:[s.jsx(Yn,{className:"w-4 h-4 mr-1"}),B.isPending?"Adding...":`Add to Queue${V.lengthm(!0),children:[s.jsx(Ade,{className:"w-4 h-4 mr-1"}),"Move"]}),s.jsxs(Se,{variant:"danger",size:"sm",onClick:()=>{l.length===1?M({type:"file",id:l[0]}):M({type:"bulk",id:0,count:l.length})},children:[s.jsx(ln,{className:"w-4 h-4 mr-1"}),"Delete"]}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:ke,children:"Clear"})]})]}),ve?s.jsx("div",{className:"flex-1 flex items-center justify-center",children:s.jsxs("div",{className:"flex flex-col items-center gap-3",children:[s.jsx(ht,{className:"w-8 h-8 animate-spin text-bambu-green"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Loading files..."})]})}):G?.length===0?s.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[s.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:s.jsx(nf,{className:"w-12 h-12 text-bambu-gray/50"})}),s.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:a!==null?"Folder is empty":"No files yet"}),s.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:a!==null?"Upload files or move files into this folder to get started.":"Upload files to start organizing your print-related files."}),s.jsxs(Se,{onClick:()=>x(!0),children:[s.jsx(Jn,{className:"w-4 h-4 mr-2"}),"Upload Files"]})]}):he.length===0?s.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[s.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:s.jsx(ba,{className:"w-12 h-12 text-bambu-gray/50"})}),s.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:"No matching files"}),s.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:"No files match your current search or filter criteria."}),s.jsx(Se,{variant:"secondary",onClick:()=>{L(""),R("all")},children:"Clear filters"})]}):C==="grid"?s.jsx("div",{className:"flex-1 overflow-y-auto",children:s.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6 gap-4",children:he.map(oe=>s.jsx(FKe,{file:oe,isSelected:l.includes(oe.id),onSelect:Q,onDelete:Ae=>M({type:"file",id:Ae}),onDownload:$,onAddToQueue:Ae=>B.mutate([Ae]),onPrint:T,onRename:Ae=>D({type:"file",id:Ae.id,name:Ae.filename})},oe.id))})}):s.jsx("div",{className:"flex-1 overflow-y-auto",children:s.jsxs("div",{className:"bg-bambu-card rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[s.jsxs("div",{className:"grid grid-cols-[auto_1fr_100px_100px_100px_80px] gap-4 px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary text-xs text-bambu-gray font-medium",children:[s.jsx("div",{className:"w-6"}),s.jsx("div",{children:"Name"}),s.jsx("div",{children:"Type"}),s.jsx("div",{children:"Size"}),s.jsx("div",{children:"Prints"}),s.jsx("div",{})]}),he.map(oe=>s.jsxs("div",{className:`grid grid-cols-[auto_1fr_100px_100px_100px_80px] gap-4 px-4 py-3 items-center border-b border-bambu-dark-tertiary last:border-b-0 cursor-pointer hover:bg-bambu-dark/50 transition-colors ${l.includes(oe.id)?"bg-bambu-green/10":""}`,onClick:()=>Q(oe.id),children:[s.jsx("div",{className:`w-5 h-5 rounded border-2 flex items-center justify-center ${l.includes(oe.id)?"bg-bambu-green border-bambu-green":"border-bambu-gray/50"}`,children:l.includes(oe.id)&&s.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})}),s.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[s.jsxs("div",{className:"relative group/thumb",children:[s.jsx("div",{className:"w-10 h-10 rounded bg-bambu-dark flex-shrink-0 overflow-hidden",children:oe.thumbnail_path?s.jsx("img",{src:pe.getLibraryFileThumbnailUrl(oe.id),alt:"",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center",children:s.jsx(nf,{className:"w-5 h-5 text-bambu-gray/50"})})}),oe.thumbnail_path&&s.jsx("div",{className:"absolute left-0 top-full mt-2 z-50 hidden group-hover/thumb:block",children:s.jsx("div",{className:"w-48 h-48 rounded-lg bg-bambu-dark-secondary border border-bambu-dark-tertiary shadow-xl overflow-hidden",children:s.jsx("img",{src:pe.getLibraryFileThumbnailUrl(oe.id),alt:oe.filename,className:"w-full h-full object-contain"})})})]}),s.jsx("div",{className:"min-w-0",children:s.jsx("div",{className:"text-sm text-white truncate",children:oe.print_name||oe.filename})})]}),s.jsx("div",{children:s.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded font-medium ${oe.file_type==="3mf"?"bg-bambu-green/20 text-bambu-green":oe.file_type==="gcode"?"bg-blue-500/20 text-blue-400":oe.file_type==="stl"?"bg-purple-500/20 text-purple-400":"bg-bambu-gray/20 text-bambu-gray"}`,children:oe.file_type.toUpperCase()})}),s.jsx("div",{className:"text-sm text-bambu-gray",children:Ep(oe.file_size)}),s.jsx("div",{className:"text-sm text-bambu-gray",children:oe.print_count>0?`${oe.print_count}x`:"-"}),s.jsxs("div",{className:"flex items-center gap-1",onClick:Ae=>Ae.stopPropagation(),children:[FR(oe.filename)&&s.jsxs(s.Fragment,{children:[s.jsx("button",{onClick:()=>T(oe),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green transition-colors",title:"Print",children:s.jsx(zr,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>B.mutate([oe.id]),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",title:"Add to Queue",disabled:B.isPending,children:s.jsx(Yn,{className:"w-4 h-4"})})]}),s.jsx("button",{onClick:()=>$(oe.id),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",title:"Download",children:s.jsx(ei,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>D({type:"file",id:oe.id,name:oe.filename}),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",title:"Rename",children:s.jsx(Wa,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>M({type:"file",id:oe.id}),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors",title:"Delete",children:s.jsx(ln,{className:"w-4 h-4"})})]})]},oe.id))]})})]})]}),u&&s.jsx(RKe,{parentId:a,onClose:()=>d(!1),onSave:oe=>Te.mutate(oe),isLoading:Te.isPending}),f&&ae&&s.jsx(OKe,{folders:ae,selectedFiles:l,currentFolderId:a,onClose:()=>m(!1),onMove:oe=>ne.mutate({fileIds:l,folderId:oe}),isLoading:ne.isPending}),g&&s.jsx(IKe,{folderId:a,onClose:()=>x(!1),onUploadComplete:Me}),y&&s.jsx(LKe,{folder:y,onClose:()=>b(null),onLink:oe=>Qe.mutate({id:y.id,data:oe}),isLoading:Qe.isPending}),v&&s.jsx(Un,{title:v.type==="folder"?"Delete Folder":v.type==="bulk"?`Delete ${v.count} Files`:"Delete File",message:v.type==="folder"?"Are you sure you want to delete this folder? All files inside will also be deleted.":v.type==="bulk"?`Are you sure you want to delete ${v.count} selected files? This action cannot be undone.`:"Are you sure you want to delete this file?",confirmText:"Delete",variant:"danger",isLoading:J,loadingText:"Deleting...",onConfirm:I,onCancel:()=>M(null)}),S&&s.jsx(Wd,{mode:"reprint",libraryFileId:S.id,archiveName:S.print_name||S.filename,onClose:()=>T(null),onSuccess:()=>{T(null),t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["archives"]})}}),E&&s.jsx(Wd,{mode:"reprint",libraryFileId:E.id,archiveName:E.print_name||E.filename,onClose:()=>k(null),onSuccess:()=>{k(null),c([]),t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["archives"]})}}),A&&s.jsx(DKe,{type:A.type,currentName:A.name,onClose:()=>D(null),onSave:oe=>{A.type==="file"?H.mutate({id:A.id,filename:oe}):K.mutate({id:A.id,name:oe})},isLoading:H.isPending||K.isPending})]})}const _j=5,zKe=2e3,BKe=3e4,$Ke=5e3;function HKe(){const{printerId:t}=HR(),e=parseInt(t||"0",10),[n,r]=w.useState("stream"),[i,a]=w.useState(!1),[o,l]=w.useState(!0),[c,u]=w.useState(Date.now()),[d,f]=w.useState(!1),[m,g]=w.useState(!1),[x,y]=w.useState(0),[b,v]=w.useState(!1),[M,S]=w.useState(0),[T,E]=w.useState(1),[k,A]=w.useState({x:0,y:0}),[D,C]=w.useState(!1),[P,F]=w.useState({x:0,y:0}),[L,X]=w.useState(null),[R,q]=w.useState(null),Y=w.useRef(null),W=w.useRef(null),Z=w.useRef(null),ie=w.useRef(null),ae=w.useRef(null),{data:U}=ct({queryKey:["printer",e],queryFn:()=>pe.getPrinter(e),enabled:e>0});w.useEffect(()=>(U&&(document.title=`${U.name} - Camera`),()=>{document.title="Bambuddy"}),[U]);const G=w.useRef(!1);w.useEffect(()=>{const J=`/api/v1/printers/${e}/camera/stop`;G.current=!1;const xe=()=>{e>0&&!G.current&&(G.current=!0,navigator.sendBeacon(J))},ve=()=>{xe()};window.addEventListener("beforeunload",ve);const oe=Y.current;return()=>{window.removeEventListener("beforeunload",ve),oe&&(oe.src=""),xe()}},[e]),w.useEffect(()=>{if(o&&!d){const xe=setTimeout(()=>{l(!1)},n==="stream"?3e3:2e4);return()=>clearTimeout(xe)}},[n,o,c,d]),w.useEffect(()=>{const J=()=>{const xe=!!document.fullscreenElement;g(xe),E(1),A({x:0,y:0}),n==="stream"&&!d&&(Y.current&&(Y.current.src=""),setTimeout(()=>{l(!0),u(Date.now())},200))};return document.addEventListener("fullscreenchange",J),()=>document.removeEventListener("fullscreenchange",J)},[n,d]),w.useEffect(()=>{let J;const xe=()=>{clearTimeout(J),J=setTimeout(()=>{localStorage.setItem("cameraWindowState",JSON.stringify({width:window.outerWidth,height:window.outerHeight,left:window.screenX,top:window.screenY}))},500)};return window.addEventListener("resize",xe),()=>{clearTimeout(J),window.removeEventListener("resize",xe)}},[]),w.useEffect(()=>()=>{Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),ae.current&&clearInterval(ae.current)},[]);const ee=w.useCallback(()=>{if(x>=_j){v(!1),a(!0);return}const J=Math.min(zKe*Math.pow(2,x),BKe);v(!0),S(Math.ceil(J/1e3)),ie.current=setInterval(()=>{S(xe=>xe<=1?(ie.current&&clearInterval(ie.current),0):xe-1)},1e3),Z.current=setTimeout(()=>{y(xe=>xe+1),v(!1),l(!0),a(!1),Y.current&&(Y.current.src=""),u(Date.now())},J)},[x]);w.useEffect(()=>{if(n!=="stream"||o||b||d){ae.current&&(clearInterval(ae.current),ae.current=null);return}return ae.current=setInterval(async()=>{try{const J=await fetch(`/api/v1/printers/${e}/camera/status`);if(J.ok){const xe=await J.json();(xe.stalled||!xe.active&&!i)&&(console.log(`Stream issue detected: stalled=${xe.stalled}, active=${xe.active}, reconnecting...`),ae.current&&(clearInterval(ae.current),ae.current=null),l(!1),ee())}}catch{}},$Ke),()=>{ae.current&&(clearInterval(ae.current),ae.current=null)}},[n,o,i,b,d,e,ee]);const re=()=>{l(!1),n==="stream"&&x<_j?ee():a(!0)},le=()=>{if(l(!1),a(!1),y(0),v(!1),Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),Y.current&&!localStorage.getItem("cameraWindowState")){const J=Y.current,xe=J.naturalWidth,ve=J.naturalHeight;if(xe>0&&ve>0){const De=window.outerWidth-window.innerWidth,Ze=window.outerHeight-window.innerHeight,ut=xe+16+De,Ye=ve+45+16+Ze;try{window.resizeTo(ut,Ye)}catch{}}}},he=()=>{e>0&&fetch(`/api/v1/printers/${e}/camera/stop`).catch(()=>{})},fe=J=>{n===J||d||(f(!0),l(!0),a(!1),y(0),v(!1),E(1),A({x:0,y:0}),Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),Y.current&&(Y.current.src=""),n==="stream"&&he(),setTimeout(()=>{r(J),u(Date.now()),f(!1)},100))},Te=()=>{d||(f(!0),l(!0),a(!1),y(0),v(!1),Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),Y.current&&(Y.current.src=""),n==="stream"&&he(),setTimeout(()=>{u(Date.now()),f(!1)},100))},ge=()=>{W.current&&(document.fullscreenElement?document.exitFullscreen():W.current.requestFullscreen())},_e=()=>{E(J=>Math.min(J+.5,4))},je=()=>{E(J=>{const xe=Math.max(J-.5,1);return xe===1&&A({x:0,y:0}),xe})},ne=J=>{J.preventDefault(),J.deltaY<0?_e():je()},Qe=J=>{T>1&&(J.preventDefault(),C(!0),F({x:J.clientX-k.x,y:J.clientY-k.y}))},B=w.useCallback(()=>{if(!W.current)return{x:300,y:200};const J=W.current.getBoundingClientRect(),xe=J.width*(T-1)/2,ve=J.height*(T-1)/2;return{x:Math.max(50,xe),y:Math.max(50,ve)}},[T]),H=J=>{if(D&&T>1){const xe=J.clientX-P.x,ve=J.clientY-P.y,oe=B();A({x:Math.max(-oe.x,Math.min(oe.x,xe)),y:Math.max(-oe.y,Math.min(oe.y,ve))})}},K=()=>{C(!1)},z=J=>{if(J.length<2)return 0;const xe=J[0].clientX-J[1].clientX,ve=J[0].clientY-J[1].clientY;return Math.sqrt(xe*xe+ve*ve)},V=J=>J.length<2?{x:J[0].clientX,y:J[0].clientY}:{x:(J[0].clientX+J[1].clientX)/2,y:(J[0].clientY+J[1].clientY)/2},Q=J=>{J.touches.length===2?(J.preventDefault(),X(z(J.touches)),q(V(J.touches))):J.touches.length===1&&T>1&&(J.preventDefault(),C(!0),F({x:J.touches[0].clientX-k.x,y:J.touches[0].clientY-k.y}))},Ne=J=>{if(J.touches.length===2&&L!==null){J.preventDefault();const xe=z(J.touches),ve=xe/L;E(Ae=>{const De=Math.max(1,Math.min(4,Ae*ve));return De===1&&A({x:0,y:0}),De}),X(xe);const oe=V(J.touches);if(R){const Ae=B();A(De=>({x:Math.max(-Ae.x,Math.min(Ae.x,De.x+(oe.x-R.x))),y:Math.max(-Ae.y,Math.min(Ae.y,De.y+(oe.y-R.y)))}))}q(oe)}else if(J.touches.length===1&&D&&T>1){J.preventDefault();const xe=J.touches[0].clientX-P.x,ve=J.touches[0].clientY-P.y,oe=B();A({x:Math.max(-oe.x,Math.min(oe.x,xe)),y:Math.max(-oe.y,Math.min(oe.y,ve))})}},ke=J=>{J.touches.length<2&&(X(null),q(null)),J.touches.length===0&&C(!1)},Me=()=>{E(1),A({x:0,y:0})},$=d?"":n==="stream"?`/api/v1/printers/${e}/camera/stream?fps=10&t=${c}`:`/api/v1/printers/${e}/camera/snapshot?t=${c}`,I=o||d||b;return e?s.jsxs("div",{ref:W,className:"min-h-screen bg-black flex flex-col",children:[s.jsxs("div",{className:"flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary",children:[s.jsxs("h1",{className:"text-sm font-medium text-white flex items-center gap-2",children:[s.jsx(Xp,{className:"w-4 h-4"}),U?.name||`Printer ${e}`]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"flex bg-bambu-dark rounded p-0.5",children:[s.jsx("button",{onClick:()=>fe("stream"),disabled:I,className:`px-3 py-1 text-xs rounded transition-colors ${n==="stream"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:"Live"}),s.jsx("button",{onClick:()=>fe("snapshot"),disabled:I,className:`px-3 py-1 text-xs rounded transition-colors ${n==="snapshot"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:"Snapshot"})]}),s.jsx("button",{onClick:Te,disabled:I,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50",title:n==="stream"?"Restart stream":"Refresh snapshot",children:s.jsx(Mr,{className:`w-4 h-4 text-bambu-gray ${I?"animate-spin":""}`})}),s.jsx("button",{onClick:ge,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded",title:m?"Exit fullscreen":"Fullscreen",children:m?s.jsx(QW,{className:"w-4 h-4 text-bambu-gray"}):s.jsx(gde,{className:"w-4 h-4 text-bambu-gray"})})]})]}),s.jsx("div",{className:"flex-1 flex items-center justify-center p-2 overflow-hidden",onWheel:ne,onMouseMove:H,onMouseUp:K,onMouseLeave:K,onTouchStart:Q,onTouchMove:Ne,onTouchEnd:ke,style:{touchAction:"none"},children:s.jsxs("div",{className:"relative w-full h-full flex items-center justify-center",children:[(o||d)&&!b&&s.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/50 z-10",children:s.jsxs("div",{className:"text-center",children:[s.jsx(Mr,{className:"w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:n==="stream"?"Connecting to camera...":"Capturing snapshot..."})]})}),b&&s.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/80 z-10",children:s.jsxs("div",{className:"text-center p-4",children:[s.jsx(Pg,{className:"w-10 h-10 text-orange-400 mx-auto mb-3"}),s.jsx("p",{className:"text-white mb-2",children:"Connection lost"}),s.jsxs("p",{className:"text-sm text-bambu-gray mb-3",children:["Reconnecting in ",M,"s... (attempt ",x+1,"/",_j,")"]}),s.jsx("button",{onClick:Te,className:"px-4 py-2 bg-bambu-green text-white text-sm rounded hover:bg-bambu-green/80 transition-colors",children:"Reconnect now"})]})}),i&&!b&&s.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black z-10",children:s.jsxs("div",{className:"text-center p-4",children:[s.jsx(jn,{className:"w-12 h-12 text-orange-400 mx-auto mb-3"}),s.jsx("p",{className:"text-white mb-2",children:"Camera unavailable"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-4 max-w-md",children:"Make sure the printer is powered on and connected."}),s.jsx("button",{onClick:Te,className:"px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors",children:"Retry"})]})}),s.jsx("img",{ref:Y,src:$,alt:"Camera stream",className:"max-w-full max-h-full object-contain select-none",style:{transform:`scale(${T}) translate(${k.x/T}px, ${k.y/T}px)`,cursor:T>1?D?"grabbing":"grab":"default"},onError:$?re:void 0,onLoad:$?le:void 0,onMouseDown:Qe,draggable:!1},c),s.jsxs("div",{className:"absolute bottom-4 left-4 flex items-center gap-1.5 bg-black/60 rounded-lg px-2 py-1.5",children:[s.jsx("button",{onClick:je,disabled:T<=1,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:"Zoom out",children:s.jsx(lD,{className:"w-4 h-4 text-white"})}),s.jsxs("button",{onClick:Me,className:"px-2 py-1 text-sm text-white hover:bg-white/10 rounded min-w-[48px]",title:"Reset zoom",children:[Math.round(T*100),"%"]}),s.jsx("button",{onClick:_e,disabled:T>=4,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:"Zoom in",children:s.jsx(oD,{className:"w-4 h-4 text-white"})})]})]})})]}):s.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:s.jsx("p",{className:"text-white",children:"Invalid printer ID"})})}function VKe(){const{id:t}=HR(),{mode:e}=Xd(),{data:n,isLoading:r,error:i}=ct({queryKey:["external-link",t],queryFn:()=>pe.getExternalLink(Number(t)),enabled:!!t});return r?s.jsx("div",{className:"flex items-center justify-center h-full",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):i||!n?s.jsxs("div",{className:"flex flex-col items-center justify-center h-full gap-4 text-bambu-gray",children:[s.jsx(jn,{className:"w-12 h-12"}),s.jsx("p",{children:"Link not found"})]}):s.jsx("iframe",{src:n.url,className:"h-full w-full border-0",style:{colorScheme:e},title:n.name,sandbox:"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"})}const XG=["DEBUG","INFO","WARNING","ERROR"],wj={DEBUG:"text-gray-400",INFO:"text-blue-400",WARNING:"text-yellow-400",ERROR:"text-red-400"},GKe={DEBUG:N2,INFO:tl,WARNING:jn,ERROR:ta};function WKe(){const t=an(),[e,n]=w.useState(!0),[r,i]=w.useState(new Set),[a,o]=w.useState(""),[l,c]=w.useState("ALL"),[u,d]=w.useState(!1),[f,m]=w.useState(!1),g=w.useRef(null),{data:x,isLoading:y,refetch:b}=ct({queryKey:["application-logs",l,a],queryFn:()=>Op.getLogs({limit:200,level:l==="ALL"?void 0:l,search:a||void 0}),refetchInterval:f?2e3:!1,enabled:u});w.useEffect(()=>{u||m(!1)},[u]);const v=ft({mutationFn:()=>Op.clearLogs(),onSuccess:()=>{t.invalidateQueries({queryKey:["application-logs"]})}});w.useEffect(()=>{e&&g.current&&x?.entries&&(g.current.scrollTop=g.current.scrollHeight)},[x?.entries,e]);const M=A=>{i(D=>{const C=new Set(D);return C.has(A)?C.delete(A):C.add(A),C})},S=A=>{const D=A.split(" ");return D.length>=2?D[1]:A},T=w.useMemo(()=>x?.entries??[],[x?.entries]),E=w.useMemo(()=>[...T].reverse(),[T]),k=({level:A})=>{const D=GKe[A]||tl;return s.jsx(D,{className:`w-3.5 h-3.5 ${wj[A]||"text-gray-400"}`})};return s.jsxs("div",{className:"bg-bambu-dark rounded-lg overflow-hidden",children:[s.jsxs("button",{onClick:()=>d(!u),className:"w-full flex items-center justify-between p-4 hover:bg-bambu-dark-tertiary/50 transition-colors",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${f?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:s.jsx(N2,{className:"w-5 h-5"})}),s.jsxs("div",{className:"text-left",children:[s.jsx("p",{className:"font-medium text-white",children:"Application Logs"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:f?`Live streaming - ${x?.filtered_count??0} entries`:"View and filter application logs"})]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[f&&s.jsxs("span",{className:"flex items-center gap-1.5 px-2 py-1 bg-bambu-green/20 rounded text-bambu-green text-xs",children:[s.jsx("span",{className:"w-1.5 h-1.5 bg-bambu-green rounded-full animate-pulse"}),"Live"]}),u?s.jsx(eu,{className:"w-5 h-5 text-bambu-gray"}):s.jsx(ea,{className:"w-5 h-5 text-bambu-gray"})]})]}),u&&s.jsxs("div",{className:"border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex flex-col gap-2 p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[f?s.jsxs("button",{onClick:A=>{A.stopPropagation(),m(!1)},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-red-500/20 text-red-400 hover:bg-red-500/30 rounded transition-colors",children:[s.jsx(al,{className:"w-4 h-4"}),"Stop"]}):s.jsxs("button",{onClick:A=>{A.stopPropagation(),m(!0),b()},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30 rounded transition-colors",children:[s.jsx(rl,{className:"w-4 h-4"}),"Start"]}),s.jsxs("button",{onClick:()=>v.mutate(),disabled:v.isPending||T.length===0,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:[s.jsx(ln,{className:"w-4 h-4"}),"Clear"]}),s.jsx("button",{onClick:()=>b(),disabled:y,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:s.jsx(Mr,{className:`w-4 h-4 ${y?"animate-spin":""}`})}),s.jsx("div",{className:"flex-1"}),s.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:e,onChange:A=>n(A.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark-tertiary"}),"Auto-scroll"]}),s.jsxs("span",{className:"text-sm text-bambu-gray",children:[x?.filtered_count??0,"/",x?.total_in_file??0]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsx(ba,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",placeholder:"Search message or logger name...",value:a,onChange:A=>o(A.target.value),className:"w-full pl-8 pr-8 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),a&&s.jsx("button",{onClick:()=>o(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:s.jsx(Bt,{className:"w-4 h-4"})})]}),s.jsxs("div",{className:"flex items-center gap-1 bg-bambu-dark-secondary rounded border border-bambu-dark-tertiary",children:[s.jsx("button",{onClick:()=>c("ALL"),className:`px-2 py-1.5 text-xs rounded-l transition-colors ${l==="ALL"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:"All"}),XG.map((A,D)=>s.jsx("button",{onClick:()=>c(A),className:`px-2 py-1.5 text-xs transition-colors flex items-center gap-1 ${D===XG.length-1?"rounded-r":""} ${l===A?`${wj[A]} bg-bambu-dark-tertiary`:"text-bambu-gray hover:text-white"}`,children:A},A))]})]})]}),s.jsx("div",{ref:g,className:"overflow-auto font-mono text-xs bg-black min-h-[300px] max-h-[500px]",children:T.length===0?s.jsxs("div",{className:"flex flex-col items-center justify-center h-[300px] text-bambu-gray",children:[s.jsx("p",{className:"mb-2",children:"No log entries found"}),s.jsx("p",{className:"text-sm",children:"Log file may be empty or cleared"})]}):s.jsx("div",{className:"divide-y divide-bambu-dark-tertiary/30",children:E.map((A,D)=>{const C=r.has(D),P=A.message.includes(` +`+n+"}"}return String(t)}function fKe({setting:t,onClose:e,onDeleted:n,onDuplicate:r,onEdit:i}){const{showToast:a}=Gn(),o=an(),[l,c]=w.useState(!1),{data:u,isLoading:d}=ct({queryKey:["cloudSettingDetail",t.setting_id],queryFn:()=>pe.getCloudSettingDetail(t.setting_id)}),f=ft({mutationFn:()=>pe.deleteCloudSetting(t.setting_id),onSuccess:()=>{a("Preset deleted"),o.invalidateQueries({queryKey:["cloudSettings"]}),n()},onError:x=>a(x.message,"error")}),m=W0(t.setting_id),g=RL(t.name,u?.setting?.inherits);return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:s.jsx(Mt,{className:"w-full max-w-3xl max-h-[90vh] flex flex-col overflow-hidden",children:s.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[s.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("h2",{className:"text-xl font-semibold text-white truncate",children:t.name}),m&&s.jsx("span",{className:"px-2 py-0.5 text-xs font-medium bg-bambu-green/20 text-bambu-green rounded-full",children:"Editable"})]}),s.jsxs("div",{className:"flex items-center gap-2 mt-1 text-sm text-bambu-gray",children:[s.jsxs("span",{className:"capitalize",children:[t.type," preset"]}),g.printer&&s.jsxs(s.Fragment,{children:[s.jsx("span",{children:"•"}),s.jsx("span",{children:g.printer})]})]})]}),s.jsx("button",{onClick:e,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Bt,{className:"w-5 h-5"})})]}),s.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto p-4",children:d?s.jsx("div",{className:"flex items-center justify-center py-16",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?s.jsx("pre",{className:"text-xs text-bambu-gray font-mono whitespace-pre-wrap break-all bg-bambu-dark p-4 rounded-lg border border-bambu-dark-tertiary overflow-x-auto max-w-full",children:IR(u)}):s.jsx("div",{className:"text-center py-16 text-bambu-gray",children:"Failed to load preset details"})}),l?s.jsxs("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary bg-red-500/5",children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 text-red-400",children:[s.jsx(jn,{className:"w-5 h-5"}),s.jsx("span",{className:"font-medium",children:"Delete this preset?"})]}),s.jsxs("p",{className:"text-sm text-bambu-gray mb-4",children:['This will permanently delete "',t.name,'" from Bambu Cloud. This cannot be undone.']}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:()=>c(!1),disabled:f.isPending,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{variant:"danger",onClick:()=>f.mutate(),disabled:f.isPending,className:"flex-1",children:[f.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):s.jsx(ln,{className:"w-4 h-4"}),"Delete"]})]})]}):s.jsx("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:e,className:"flex-1",children:"Close"}),s.jsxs(Se,{variant:"secondary",onClick:r,children:[s.jsx(Jo,{className:"w-4 h-4"}),"Duplicate"]}),m&&s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"secondary",onClick:i,disabled:d||!u,children:[s.jsx(Wa,{className:"w-4 h-4"}),"Edit"]}),s.jsx(Se,{variant:"danger",onClick:()=>c(!0),children:s.jsx(ln,{className:"w-4 h-4"})})]})]})})]})})})}function fre(){try{const t=localStorage.getItem("bambusy_preset_templates");return t?JSON.parse(t):[]}catch{return[]}}function mre(t){localStorage.setItem("bambusy_preset_templates",JSON.stringify(t))}function mKe({onClose:t,onApply:e}){const{showToast:n}=Gn(),[r,i]=w.useState(fre),[a,o]=w.useState("all"),[l,c]=w.useState(null),[u,d]=w.useState(""),[f,m]=w.useState(""),[g,x]=w.useState("{}"),[y,b]=w.useState(null),[v,M]=w.useState(null),S=a==="all"?r:r.filter(L=>L.type===a),T=L=>{i(L),mre(L)},E=L=>{const X=r.filter(R=>R.id!==L);T(X),M(null),n("Template deleted")},k=L=>{c(L.id),d(L.name),m(L.description),x(JSON.stringify(L.settings,null,2)),b(null)},A=()=>{if(!(!l||!u.trim()))try{const L=JSON.parse(g),X=r.map(R=>R.id===l?{...R,name:u.trim(),description:f.trim(),settings:L}:R);T(X),c(null),n("Template updated")}catch(L){b(L.message)}},D=()=>{c(null),d(""),m(""),x("{}"),b(null)},C=L=>{const X=r.map(R=>R.id===L?{...R,showInModal:!R.showInModal}:R);T(X)},P={filament:{label:"Filament",icon:eD,color:"text-amber-400"},print:{label:"Process",icon:Dd,color:"text-blue-400"},printer:{label:"Printer",icon:zr,color:"text-purple-400"}},F=v?r.find(L=>L.id===v):null;return w.useEffect(()=>{const L=X=>{X.key==="Escape"&&(v?M(null):l?D():t())};return window.addEventListener("keydown",L),()=>window.removeEventListener("keydown",L)},[v,l,t]),s.jsxs("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:[F&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-60",children:s.jsx(Mt,{className:"w-full max-w-md",children:s.jsxs(Tt,{className:"p-6",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[s.jsx("div",{className:"p-2 bg-red-500/20 rounded-lg",children:s.jsx(jn,{className:"w-6 h-6 text-red-400"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Template"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"This action cannot be undone"})]})]}),s.jsxs("p",{className:"text-white mb-6",children:['Are you sure you want to delete "',s.jsx("span",{className:"font-medium",children:F.name}),'"?']}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:()=>M(null),className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:()=>E(v),className:"flex-1 bg-red-500 hover:bg-red-600",children:[s.jsx(ln,{className:"w-4 h-4"}),"Delete"]})]})]})})}),s.jsx(Mt,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",children:s.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(a0,{className:"w-5 h-5 text-amber-400"}),"Quick Templates"]}),s.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:s.jsx(Bt,{className:"w-5 h-5"})})]}),s.jsxs("div",{className:"flex items-center gap-2 p-4 border-b border-bambu-dark-tertiary",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Type:"}),["all","filament","print","printer"].map(L=>s.jsx("button",{onClick:()=>o(L),className:`px-3 py-1 text-sm rounded-lg transition-colors ${a===L?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:L==="all"?"All":P[L].label},L))]}),s.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:S.length===0?s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx(a0,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),s.jsx("p",{children:"No templates yet"}),s.jsx("p",{className:"text-sm mt-1",children:"Create templates from the preset editor"})]}):s.jsx("div",{className:"space-y-2",children:S.map(L=>{const X=P[L.type],R=X.icon;return l===L.id?s.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg border border-bambu-green",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3 mb-3",children:[s.jsx("input",{type:"text",value:u,onChange:q=>d(q.target.value),placeholder:"Template name",className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",autoFocus:!0}),s.jsx("input",{type:"text",value:f,onChange:q=>m(q.target.value),placeholder:"Description",className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"mb-3",children:[s.jsx("label",{className:"text-xs text-bambu-gray mb-1 block",children:"Settings (JSON)"}),s.jsx("textarea",{value:g,onChange:q=>{x(q.target.value),b(null)},rows:6,className:`w-full px-3 py-2 bg-bambu-dark-secondary border rounded text-white text-sm font-mono focus:outline-none ${y?"border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`}),y&&s.jsx("p",{className:"text-xs text-red-400 mt-1",children:y})]}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{size:"sm",onClick:A,disabled:!u.trim(),children:[s.jsx(hs,{className:"w-4 h-4"}),"Save"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:D,children:"Cancel"})]})]},L.id):s.jsxs("div",{className:"flex items-center gap-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary hover:border-bambu-gray-dark transition-colors",children:[s.jsx(R,{className:`w-5 h-5 ${X.color} flex-shrink-0`}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("p",{className:"text-sm font-medium text-white",children:L.name}),s.jsx("p",{className:"text-xs text-bambu-gray truncate",children:L.description})]}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark px-2 py-1 bg-bambu-dark-secondary rounded",children:[Object.keys(L.settings).length," fields"]}),s.jsx("button",{onClick:()=>C(L.id),className:`p-1 transition-colors ${L.showInModal?"text-bambu-green hover:text-bambu-green/70":"text-bambu-gray hover:text-white"}`,title:L.showInModal?"Shown in modals":"Hidden in modals",children:L.showInModal?s.jsx(_f,{className:"w-4 h-4"}):s.jsx(A2,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>e(L),className:"px-3 py-1 text-xs bg-bambu-green/20 text-bambu-green rounded hover:bg-bambu-green/30 transition-colors",children:"Apply"}),s.jsx("button",{onClick:()=>k(L),className:"p-1 text-bambu-gray hover:text-white",title:"Edit",children:s.jsx(Wa,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>M(L.id),className:"p-1 text-bambu-gray hover:text-red-400",title:"Delete",children:s.jsx(ln,{className:"w-4 h-4"})})]},L.id)})})})]})})]})}function pre({onClose:t,leftPreset:e,rightPreset:n,leftLabel:r,rightLabel:i}){const[a,o]=w.useState("changes"),[l,c]=w.useState(""),u=w.useMemo(()=>{const g=new Set([...Object.keys(e),...Object.keys(n)]),x=[];for(const y of g){if(y==="inherits"||y==="version")continue;const b=e[y],v=n[y],M=y in e,S=y in n;let T;!M&&S?T="added":M&&!S?T="removed":JSON.stringify(b)!==JSON.stringify(v)?T="changed":T="same",x.push({key:y,left:b,right:v,status:T})}return x.sort((y,b)=>{const v={changed:0,added:1,removed:2,same:3};return v[y.status]!==v[b.status]?v[y.status]-v[b.status]:y.key.localeCompare(b.key)})},[e,n]),d=w.useMemo(()=>{let g=[...u];if(a==="changes"&&(g=g.filter(x=>x.status!=="same")),l){const x=l.toLowerCase();g=g.filter(y=>y.key.toLowerCase().includes(x)||String(y.left).toLowerCase().includes(x)||String(y.right).toLowerCase().includes(x))}return g},[u,a,l]),f=w.useMemo(()=>({added:u.filter(g=>g.status==="added").length,removed:u.filter(g=>g.status==="removed").length,changed:u.filter(g=>g.status==="changed").length,same:u.filter(g=>g.status==="same").length}),[u]),m=g=>{if(g===void 0)return"—";if(g===null)return"null";if(Array.isArray(g))return g.length===0?"[]":g.length===1?String(g[0]):g.join(", ");if(typeof g=="object")return JSON.stringify(g);const x=String(g);if(x.includes("\\n")||x.length>100){const y=x.split("\\n").length;if(y>1)return`[${y} lines of G-code/script]`;if(x.length>100)return x.substring(0,100)+"…"}return x};return w.useEffect(()=>{const g=x=>{x.key==="Escape"&&t()};return window.addEventListener("keydown",g),()=>window.removeEventListener("keydown",g)},[t]),s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsx(Mt,{className:"w-full max-w-4xl max-h-[85vh] flex flex-col overflow-hidden",children:s.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[s.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Rp,{className:"w-5 h-5 text-blue-400"}),"Compare Presets"]}),s.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:s.jsx(Bt,{className:"w-5 h-5"})})]}),s.jsxs("div",{className:"flex-shrink-0 grid grid-cols-2 gap-4 p-4 border-b border-bambu-dark-tertiary bg-bambu-dark",children:[s.jsxs("div",{className:"text-center",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Left:"}),s.jsx("p",{className:"text-white font-medium truncate",children:r})]}),s.jsxs("div",{className:"text-center",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Right:"}),s.jsx("p",{className:"text-white font-medium truncate",children:i})]})]}),s.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[s.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[s.jsx(Jn,{className:"w-3.5 h-3.5"}),f.added," added"]}),s.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[s.jsx(Hj,{className:"w-3.5 h-3.5"}),f.removed," removed"]}),s.jsxs("span",{className:"flex items-center gap-1 text-amber-400",children:[s.jsx(Ij,{className:"w-3.5 h-3.5"}),f.changed," changed"]}),s.jsxs("span",{className:"flex items-center gap-1 text-bambu-gray",children:[s.jsx(CT,{className:"w-3.5 h-3.5"}),f.same," same"]})]}),s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsxs("div",{className:"relative",children:[s.jsx(ba,{className:"absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:l,onChange:g=>c(g.target.value),placeholder:"Search fields...",className:"pl-8 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none w-48"})]}),f.same>0&&s.jsxs("div",{className:"flex rounded overflow-hidden border border-bambu-dark-tertiary",children:[s.jsx("button",{onClick:()=>o("changes"),className:`px-3 py-1.5 text-sm transition-colors ${a==="changes"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:"Changes"}),s.jsx("button",{onClick:()=>o("all"),className:`px-3 py-1.5 text-sm transition-colors ${a==="all"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:"All"})]})]})]}),s.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto",children:d.length===0?s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx(CT,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),s.jsx("p",{children:a==="changes"?"No differences found":"No fields match the search"})]}):s.jsxs("table",{className:"w-full",children:[s.jsx("thead",{className:"sticky top-0 bg-bambu-dark-secondary",children:s.jsxs("tr",{className:"text-sm text-bambu-gray border-b border-bambu-dark-tertiary",children:[s.jsx("th",{className:"text-left p-3 w-1/3",children:"Field"}),s.jsx("th",{className:"text-left p-3 w-1/3",children:r}),s.jsx("th",{className:"text-left p-3 w-1/3",children:i})]})}),s.jsx("tbody",{children:d.map(g=>{const x={added:"bg-green-500/10",removed:"bg-red-500/10",changed:"bg-amber-500/10",same:""}[g.status],y={added:s.jsx(Jn,{className:"w-3.5 h-3.5 text-green-400"}),removed:s.jsx(Hj,{className:"w-3.5 h-3.5 text-red-400"}),changed:s.jsx(Ij,{className:"w-3.5 h-3.5 text-amber-400"}),same:s.jsx(CT,{className:"w-3.5 h-3.5 text-bambu-gray-dark"})}[g.status];return s.jsxs("tr",{className:`border-b border-bambu-dark-tertiary ${x}`,children:[s.jsx("td",{className:"p-3",children:s.jsxs("div",{className:"flex items-center gap-2",children:[y,s.jsx("span",{className:"text-sm text-white font-mono",children:g.key})]})}),s.jsx("td",{className:"p-3",children:s.jsx("span",{className:`text-sm font-mono break-all ${g.status==="removed"?"text-red-300":g.status==="changed"?"text-white":"text-bambu-gray"}`,children:m(g.left)})}),s.jsx("td",{className:"p-3",children:s.jsx("span",{className:`text-sm font-mono break-all ${g.status==="added"?"text-green-300":g.status==="changed"?"text-white":"text-bambu-gray"}`,children:m(g.right)})})]},g.key)})})]})})]})})})}function pKe({onClose:t,initialData:e,allPresets:n}){const{showToast:r}=Gn(),i=an(),a=!!e?.setting_id,[o,l]=w.useState("common"),[c,u]=w.useState(e?.type||"filament"),[d,f]=w.useState(e?.name?a?e.name:`${e.name} (Copy)`:""),[m,g]=w.useState(e?.base_id||""),[x,y]=w.useState(""),[b,v]=w.useState(e?.setting||{inherits:""}),[M,S]=w.useState(JSON.stringify(e?.setting||{inherits:""},null,2)),[T,E]=w.useState(null),[k,A]=w.useState(""),[D,C]=w.useState(!1),[P,F]=w.useState(""),[L,X]=w.useState(!1),[R,q]=w.useState(fre),[Y,W]=w.useState(!1),[Z,ie]=w.useState(""),[ae,U]=w.useState(""),[G,ee]=w.useState(!0),[re,le]=w.useState(null),[he,fe]=w.useState(!1),Te=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer})[c]||[],[n,c]),ge=w.useMemo(()=>Te.filter(ce=>W0(ce.setting_id)),[Te]),{data:_e}=ct({queryKey:["cloudFields",c],queryFn:()=>pe.getCloudFields(c==="print"?"process":c),staleTime:1e3*60*60}),{data:je}=ct({queryKey:["allPresetDetails",c,ge.map(ce=>ce.setting_id).join(",")],queryFn:async()=>{const ce={};for(let $e=0;$e{try{const He=await pe.getCloudSettingDetail(se.setting_id);return{id:se.setting_id,detail:He}}catch{return null}}))).forEach(se=>{se&&(ce[se.id]=se.detail)})}return ce},enabled:ge.length>0,staleTime:1e3*60*10}),{data:ne,isLoading:Qe}=ct({queryKey:["cloudSettingDetail",m],queryFn:()=>pe.getCloudSettingDetail(m),enabled:!!m});w.useEffect(()=>{o!=="json"&&S(JSON.stringify(b,null,2))},[b,o]);const B=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer}[c]||[]).filter(Ve=>!W0(Ve.setting_id)).sort((Ve,$e)=>Ve.name.localeCompare($e.name)),[n,c]);w.useEffect(()=>{if(!m)return;const ce=B.find(Ve=>Ve.setting_id===m);ce&&(y(ce.name),a||(v({inherits:ce.name}),S(JSON.stringify({inherits:ce.name},null,2))))},[m,B,a]);const H=w.useMemo(()=>{const ce=_e?.fields||[],Ve=new Set(ce.map(se=>se.key)),$e=new Set,qe=new Set(["inherits","updated_time","compatible_printers","compatible_prints"]);je&&Object.values(je).forEach(se=>{se?.setting&&Object.keys(se.setting).forEach(He=>{!Ve.has(He)&&!qe.has(He)&&$e.add(He)})}),Object.keys(b).forEach(se=>{!Ve.has(se)&&!qe.has(se)&&$e.add(se)});const it=Array.from($e).sort().map(se=>({key:se,label:se.replace(/_/g," ").replace(/\b\w/g,He=>He.toUpperCase()),type:"text",category:"discovered",description:"Discovered from presets"}));return[...ce,...it]},[_e,je,b]),K=H.filter(ce=>ce.label.toLowerCase().includes(k.toLowerCase())||ce.key.toLowerCase().includes(k.toLowerCase())),z=()=>{if(P.trim()){const ce=P.trim().toLowerCase().replace(/\s+/g,"_");V(ce,""),F(""),X(!1),r(`Field "${ce}" added`)}},V=(ce,Ve)=>{v($e=>{const qe={...$e};return Ve===""||Ve===void 0?delete qe[ce]:qe[ce]=Ve,qe})},Q=ce=>{v(Ve=>({...Ve,...ce.settings})),le(ce.name),r("Template applied")},Ne=()=>{if(!Z.trim())return;const ce={...b};if(delete ce.inherits,Object.keys(ce).length===0){r("No overrides to save","error");return}const Ve={id:Date.now().toString(),name:Z.trim(),description:ae.trim()||"Custom template",type:c,settings:ce,showInModal:G},$e=[...R,Ve];q($e),mre($e),W(!1),ie(""),U(""),ee(!0),r("Template saved")},ke=w.useMemo(()=>R.filter(ce=>ce.type===c&&ce.showInModal),[c,R]),Me=ce=>{S(ce);try{const Ve=JSON.parse(ce);v(Ve),E(null)}catch(Ve){E(Ve.message)}},$=ce=>{ce.preventDefault(),C(!1);const Ve=ce.dataTransfer.files[0];if(Ve&&Ve.name.endsWith(".json")){const $e=new FileReader;$e.onload=qe=>{try{const it=qe.target?.result,se=JSON.parse(it),He=se.setting||se;v(ye=>({...ye,...He})),S(JSON.stringify({...b,...He},null,2)),r("File imported successfully")}catch{r("Invalid JSON file","error")}},$e.readAsText(Ve)}},I=ft({mutationFn:()=>{const ce={...b},Ve=c==="filament"?"filament_settings_id":c==="print"?"print_settings_id":"printer_settings_id";ce[Ve]=`"${d}"`;const $e={type:c,name:d,base_id:m,setting:ce};return pe.createCloudSetting($e)},onSuccess:async()=>{r("Preset created successfully"),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ce=>r(ce.message,"error")}),J=ft({mutationFn:()=>{if(!e?.setting_id)throw new Error("No setting ID for update");return pe.updateCloudSetting(e.setting_id,{name:d,setting:b})},onSuccess:async()=>{r("Preset updated successfully"),i.removeQueries({queryKey:["cloudSettingDetail"]}),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ce=>r(ce.message,"error")}),xe=a?J:I,ve=ne?.setting?.inherits,oe=ve?B.find(ce=>ce.name===ve):void 0,{data:Ae}=ct({queryKey:["cloudSettingDetail",oe?.setting_id],queryFn:()=>pe.getCloudSettingDetail(oe.setting_id),enabled:!!oe?.setting_id}),De=w.useMemo(()=>{const ce=Ae?.setting||{},Ve=ne?.setting||{},$e=m&&je?.[m]?.setting?je[m].setting:{};return{...ce,...Ve,...$e}},[m,ne,Ae,je]),Ze=ce=>{if(ce==null)return"";if(Array.isArray(ce)){const Ve=[...new Set(ce.map($e=>String($e)))];return Ve.length===1?Ve[0]:ce.join(", ")}return String(ce)},ut=ce=>{const Ve=b[ce.key],$e=De[ce.key],qe=Ze($e),it=Qe?"Loading...":qe||"",se="w-full px-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none";if(ce.type==="boolean"){const He=Ve==="1"||Ve===void 0&&$e==="1";return s.jsx("button",{type:"button",onClick:()=>V(ce.key,Ve==="1"?"0":"1"),className:`w-8 h-5 rounded-full transition-colors ${He?"bg-bambu-green":"bg-bambu-dark-tertiary"}`,children:s.jsx("div",{className:`w-4 h-4 rounded-full bg-white shadow transition-transform ${He?"translate-x-3.5":"translate-x-0.5"}`})})}return ce.type==="select"?s.jsxs("select",{value:Ve||"",onChange:He=>V(ce.key,He.target.value),className:se,children:[s.jsx("option",{value:"",children:it}),ce.options?.map(He=>s.jsx("option",{value:He.value,children:He.label},He.value))]}):s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:ce.type==="number"?"number":"text",value:Ve!==void 0?String(Ve):"",onChange:He=>V(ce.key,He.target.value),step:ce.step,min:ce.min,max:ce.max,placeholder:it,className:se}),ce.unit&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:ce.unit})]})},Ye=w.useMemo(()=>ne?.setting?ne.setting:{},[ne]);return s.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onDragOver:ce=>{ce.preventDefault(),C(!0)},onDragLeave:()=>C(!1),onDrop:$,children:[he&&m&&s.jsx(pre,{onClose:()=>fe(!1),leftPreset:Ye,rightPreset:b,leftLabel:`Base: ${x||m}`,rightLabel:`Current: ${d||"New Preset"}`}),s.jsx(Mt,{className:"w-full max-w-6xl max-h-[90vh] flex flex-col",children:s.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("h2",{className:"text-xl font-semibold text-white",children:a?"Edit Preset":e?"Duplicate Preset":"Create New Preset"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize settings for your new preset"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[m&&s.jsxs("button",{onClick:()=>fe(!0),className:"flex items-center gap-2 px-3 py-2 text-sm text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",title:"Compare with base preset",children:[s.jsx(Rp,{className:"w-4 h-4"}),"Compare"]}),s.jsx("button",{onClick:t,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Bt,{className:"w-5 h-5"})})]})]}),D&&s.jsx("div",{className:"absolute inset-0 bg-bambu-green/10 border-2 border-dashed border-bambu-green rounded-lg flex items-center justify-center z-10",children:s.jsxs("div",{className:"text-center",children:[s.jsx(yi,{className:"w-12 h-12 text-bambu-green mx-auto mb-2"}),s.jsx("p",{className:"text-bambu-green font-medium",children:"Drop JSON file to import"})]})}),s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Type"}),s.jsxs("select",{value:c,onChange:ce=>{u(ce.target.value),g("")},className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"filament",children:"Filament"}),s.jsx("option",{value:"print",children:"Process"}),s.jsx("option",{value:"printer",children:"Printer"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Base Preset"}),s.jsxs("select",{value:m,onChange:ce=>g(ce.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"",children:"Select base preset..."}),B.map(ce=>s.jsx("option",{value:ce.setting_id,children:ce.name},ce.setting_id))]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Preset Name"}),s.jsx("input",{type:"text",value:d,onChange:ce=>f(ce.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",placeholder:"My Custom Preset"})]})]}),x&&s.jsx("div",{className:"text-xs text-bambu-gray",children:s.jsxs("p",{className:"flex items-center gap-1",children:[s.jsx(bs,{className:"w-3 h-3 text-bambu-green"}),"Inherits from: ",s.jsx("span",{className:"text-white",children:x}),Qe&&s.jsx(ht,{className:"w-3 h-3 animate-spin ml-1"})]})})]}),s.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary",children:[s.jsxs("button",{onClick:()=>l("common"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${o==="common"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(fz,{className:"w-4 h-4"}),"Common"]}),s.jsxs("button",{onClick:()=>l("fields"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${o==="fields"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(O2,{className:"w-4 h-4"}),"All Fields"]}),s.jsxs("button",{onClick:()=>l("json"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${o==="json"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(VW,{className:"w-4 h-4"}),"JSON",T&&s.jsx(ta,{className:"w-3 h-3 text-red-400"})]}),s.jsx("div",{className:"flex-1"}),s.jsxs("button",{onClick:()=>{const ce={name:d,type:c,base_id:m,setting:b},Ve=new Blob([JSON.stringify(ce,null,2)],{type:"application/json"}),$e=URL.createObjectURL(Ve),qe=document.createElement("a");qe.href=$e,qe.download=`${d||"preset"}.json`,document.body.appendChild(qe),qe.click(),document.body.removeChild(qe),URL.revokeObjectURL($e),r("Preset exported")},className:"flex items-center gap-2 px-4 py-3 text-sm text-bambu-gray hover:text-white transition-colors",title:"Export current settings to JSON file",children:[s.jsx(ei,{className:"w-4 h-4"}),"Export"]}),s.jsxs("button",{onClick:()=>document.getElementById("file-import")?.click(),className:"flex items-center gap-2 px-4 py-3 text-sm text-bambu-gray hover:text-white transition-colors",title:"Import settings from JSON file",children:[s.jsx(yi,{className:"w-4 h-4"}),"Import"]}),s.jsx("input",{id:"file-import",type:"file",accept:".json",className:"hidden",onChange:ce=>{const Ve=ce.target.files?.[0];if(Ve){const $e=new FileReader;$e.onload=qe=>{try{const it=JSON.parse(qe.target?.result),se=it.setting||it;v(He=>({...He,...se})),r("File imported")}catch{r("Invalid JSON","error")}},$e.readAsText(Ve)}}})]}),s.jsxs("div",{className:"flex-1 overflow-y-auto p-4",children:[o==="common"&&s.jsxs("div",{className:"space-y-6",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:[s.jsx(a0,{className:"w-4 h-4 text-amber-400"}),"Quick Templates"]}),Object.keys(b).filter(ce=>ce!=="inherits").length>0&&s.jsxs("button",{onClick:()=>W(!Y),className:"text-xs text-bambu-gray hover:text-white flex items-center gap-1 transition-colors",children:[s.jsx(hs,{className:"w-3 h-3"}),"Save as template"]})]}),Y&&s.jsxs("div",{className:"mb-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-2 mb-2",children:[s.jsx("input",{type:"text",value:Z,onChange:ce=>ie(ce.target.value),placeholder:"Template name",className:"px-3 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",autoFocus:!0}),s.jsx("input",{type:"text",value:ae,onChange:ce=>U(ce.target.value),placeholder:"Description (optional)",className:"px-3 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{size:"sm",onClick:Ne,disabled:!Z.trim(),children:[s.jsx(hs,{className:"w-3 h-3"}),"Save"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>W(!1),children:"Cancel"})]}),s.jsxs("button",{onClick:()=>ee(!G),className:`flex items-center gap-1.5 text-xs transition-colors ${G?"text-bambu-green":"text-bambu-gray hover:text-white"}`,children:[G?s.jsx(_f,{className:"w-3.5 h-3.5"}):s.jsx(A2,{className:"w-3.5 h-3.5"}),G?"Show in modals":"Hidden in modals"]})]})]}),re&&s.jsxs("div",{className:"mb-3 px-3 py-2 bg-bambu-green/10 border border-bambu-green/30 rounded-lg flex items-center gap-2",children:[s.jsx(bs,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("span",{className:"text-sm text-bambu-green",children:["Template applied: ",s.jsx("span",{className:"font-medium",children:re})]}),s.jsx("button",{onClick:()=>le(null),className:"ml-auto text-bambu-green/70 hover:text-bambu-green",children:s.jsx(Bt,{className:"w-4 h-4"})})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[ke.map(ce=>s.jsxs("button",{onClick:()=>Q(ce),className:"p-3 text-left bg-bambu-dark border border-bambu-dark-tertiary rounded-lg hover:border-bambu-gray-dark transition-colors",children:[s.jsx("p",{className:"text-sm font-medium text-white",children:ce.name}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:ce.description})]},ce.id)),ke.length===0&&s.jsx("p",{className:"col-span-3 text-center text-bambu-gray text-sm py-4",children:"No templates selected. Use the Templates button to enable templates for quick access."})]}),s.jsx("p",{className:"text-xs text-bambu-gray-dark mt-2 text-center",children:"Manage templates via the Templates button on the main page"})]}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Common Settings"}),s.jsx("div",{className:"grid grid-cols-2 gap-x-6 gap-y-3",children:H.slice(0,10).map(ce=>s.jsxs("div",{className:"flex items-center justify-between gap-4",children:[s.jsx("label",{className:"text-sm text-bambu-gray flex-shrink-0",children:ce.label}),s.jsx("div",{className:"w-48",children:ut(ce)})]},ce.key))})]}),Object.keys(b).length>1&&s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Current Overrides"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:Object.entries(b).filter(([ce])=>ce!=="inherits").map(([ce,Ve])=>s.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-green/10 text-bambu-green text-xs rounded",children:[ce,": ",String(Ve).slice(0,20),s.jsx("button",{onClick:()=>V(ce,void 0),className:"hover:text-white",children:s.jsx(Bt,{className:"w-3 h-3"})})]},ce))})]})]}),o==="fields"&&s.jsxs("div",{className:"grid grid-cols-2 gap-6",style:{height:"400px"},children:[s.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[s.jsx("h3",{className:"text-sm font-medium text-white",children:"Available Fields"}),s.jsx("span",{className:"text-xs text-bambu-gray",children:je?`${H.length} fields`:"Loading..."})]}),s.jsxs("div",{className:"relative mb-3 flex-shrink-0",children:[s.jsx(ba,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:k,onChange:ce=>A(ce.target.value),placeholder:"Search fields...",className:"w-full pl-10 pr-4 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"flex-1 overflow-y-auto space-y-1 pr-2 min-h-0",children:[K.filter(ce=>!(ce.key in b)).map(ce=>{const Ve=De[ce.key],$e=Ze(Ve);return s.jsxs("div",{onClick:()=>{v(qe=>({...qe,[ce.key]:$e||""}))},className:"flex items-center justify-between gap-2 p-2 rounded-lg hover:bg-bambu-dark-tertiary transition-colors cursor-pointer group",children:[s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsx("p",{className:"text-sm text-white truncate",children:ce.label}),s.jsx("p",{className:"text-xs text-bambu-gray-dark truncate",children:ce.key})]}),s.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[$e&&s.jsxs("span",{className:"text-xs text-bambu-gray bg-bambu-dark px-2 py-0.5 rounded max-w-32 truncate",title:$e,children:[$e.slice(0,20),$e.length>20?"...":""]}),s.jsx("div",{className:"w-6 h-6 flex items-center justify-center rounded bg-bambu-dark-tertiary group-hover:bg-bambu-green/20 transition-colors",children:s.jsx(Jn,{className:"w-4 h-4 text-bambu-gray group-hover:text-bambu-green transition-colors"})})]})]},ce.key)}),K.filter(ce=>!(ce.key in b)).length===0&&s.jsx("p",{className:"text-center text-bambu-gray py-4 text-sm",children:k?"No matching fields":"All fields added"})]}),s.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:L?s.jsxs("div",{className:"flex gap-2",children:[s.jsx("input",{type:"text",value:P,onChange:ce=>F(ce.target.value),onKeyDown:ce=>ce.key==="Enter"&&z(),placeholder:"custom_field_name",className:"flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white font-mono text-sm placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none",autoFocus:!0}),s.jsx(Se,{size:"sm",onClick:z,disabled:!P.trim(),children:s.jsx(Jn,{className:"w-4 h-4"})}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>{X(!1),F("")},children:s.jsx(Bt,{className:"w-4 h-4"})})]}):s.jsxs("button",{onClick:()=>X(!0),className:"w-full flex items-center justify-center gap-2 p-2 text-sm text-bambu-gray hover:text-white border border-dashed border-bambu-dark-tertiary hover:border-bambu-gray-dark rounded-lg transition-colors",children:[s.jsx(Jn,{className:"w-4 h-4"}),"Add custom field"]})})]}),s.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[s.jsx("h3",{className:"text-sm font-medium text-white",children:"Your Overrides"}),s.jsxs("span",{className:"text-xs text-bambu-gray",children:[Object.keys(b).filter(ce=>ce!=="inherits").length," fields"]})]}),s.jsxs("div",{className:"flex-1 overflow-y-auto space-y-2 pr-2 min-h-0",children:[Object.entries(b).filter(([ce])=>ce!=="inherits").map(([ce,Ve])=>{const $e=H.find(qe=>qe.key===ce);return s.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm font-medium text-white",children:$e?.label||ce}),s.jsx("p",{className:"text-xs text-bambu-gray-dark",children:ce})]}),s.jsx("button",{onClick:()=>V(ce,void 0),className:"p-1 text-bambu-gray hover:text-red-400 transition-colors",children:s.jsx(Bt,{className:"w-4 h-4"})})]}),$e?ut($e):s.jsx("input",{type:"text",value:String(Ve),onChange:qe=>V(ce,qe.target.value),className:"w-full px-3 py-1.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"})]},ce)}),Object.keys(b).filter(ce=>ce!=="inherits").length===0&&s.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[s.jsx(fz,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),s.jsx("p",{className:"text-sm",children:"No overrides yet"}),s.jsx("p",{className:"text-xs mt-1",children:"Click fields on the left to add them"})]})]}),Object.keys(b).filter(ce=>ce!=="inherits").length>0&&s.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:s.jsxs("button",{onClick:()=>{W(!0),l("common")},className:"w-full flex items-center justify-center gap-2 p-2 text-sm text-bambu-gray hover:text-white border border-dashed border-bambu-dark-tertiary hover:border-bambu-gray-dark rounded-lg transition-colors",children:[s.jsx(hs,{className:"w-4 h-4"}),"Save as template"]})})]})]}),o==="json"&&s.jsxs("div",{className:"space-y-2",children:[T&&s.jsxs("div",{className:"flex items-center gap-2 text-red-400 text-sm",children:[s.jsx(ta,{className:"w-4 h-4"}),T]}),s.jsx("textarea",{value:M,onChange:ce=>Me(ce.target.value),className:`w-full h-80 px-3 py-2 bg-bambu-dark border rounded-lg text-white text-xs font-mono focus:outline-none resize-none ${T?"border-red-500 focus:border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`,spellCheck:!1}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Tip: Drag & drop a .json file anywhere on this modal to import settings"})]})]}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:t,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:()=>xe.mutate(),disabled:xe.isPending||!d.trim()||!a&&!m||!!T,className:"flex-1",children:[xe.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):a?s.jsx(hs,{className:"w-4 h-4"}):s.jsx(Jn,{className:"w-4 h-4"}),a?"Save":e?"Duplicate":"Create"]})]})]})})]})}function gKe({settings:t,lastSyncTime:e,onRefresh:n,isRefreshing:r,printers:i}){const[a,o]=w.useState(""),[l,c]=w.useState("all"),[u,d]=w.useState("all"),[f,m]=w.useState("all"),[g,x]=w.useState("all"),[y,b]=w.useState("all"),[v,M]=w.useState("all"),[S,T]=w.useState(null),[E,k]=w.useState(!1),[A,D]=w.useState(!1),[C,P]=w.useState(null),[F,L]=w.useState(null),[X,R]=w.useState(null),[q,Y]=w.useState(!1),[W,Z]=w.useState([null,null]),[ie,ae]=w.useState(!1),[U,G]=w.useState(null),ee=an(),re=w.useMemo(()=>[...t.filament.map(K=>({...K,type:"filament"})),...t.printer.map(K=>({...K,type:"printer"})),...t.process.map(K=>({...K,type:"process"}))].map(K=>({...K,meta:RL(K.name)})),[t]),le=w.useMemo(()=>{const H=new Set,K=new Set,z=new Set;return re.forEach(V=>{V.meta.nozzle&&H.add(V.meta.nozzle),V.meta.filamentType&&K.add(V.meta.filamentType),V.meta.layerHeight&&z.add(V.meta.layerHeight)}),{printers:i.map(V=>({id:V.id.toString(),name:V.name})),nozzles:Array.from(H).sort((V,Q)=>parseFloat(V)-parseFloat(Q)),filaments:Array.from(K).sort(),layerHeights:Array.from(z).sort((V,Q)=>parseFloat(V)-parseFloat(Q))}},[re,i]),he=w.useMemo(()=>f==="all"?null:i.find(K=>K.id.toString()===f)?.model||null,[f,i]),fe=w.useMemo(()=>re.filter(H=>l==="all"||H.type===l).filter(H=>{if(u==="all")return!0;const K=W0(H.setting_id);return u==="custom"?K:!K}).filter(H=>{if(f==="all"||!he)return!0;const K=H.meta.printer?.toLowerCase()||"",z=he.toLowerCase();return K.includes(z)||z.includes(K)}).filter(H=>g==="all"||H.meta.nozzle===g).filter(H=>y==="all"||H.meta.filamentType===y).filter(H=>v==="all"||H.meta.layerHeight===v).filter(H=>a===""||H.name.toLowerCase().includes(a.toLowerCase())).sort((H,K)=>H.name.localeCompare(K.name)),[re,l,u,f,he,g,y,v,a]),Te=H=>{if(q){const K=W[0]?.setting_id===H.setting_id,z=W[1]?.setting_id===H.setting_id;if(K)Z([W[1],null]);else if(z)Z([W[0],null]);else if(!W[0])Z([H,null]);else if(W[1]){if(W[0].type!==H.type)return;Z([W[0],H])}else{if(W[0].type!==H.type)return;Z([W[0],H])}}else T(H)},ge=H=>{if(W[0]?.setting_id===H.setting_id)return 0;if(W[1]?.setting_id===H.setting_id)return 1},_e=async H=>{try{const K=await pe.getCloudSettingDetail(H.setting_id),z=H.type==="process"?"print":H.type;P({type:z,name:H.name,base_id:K.base_id||"GFSA00",setting:K.setting||{}}),T(null)}catch(K){console.error("Failed to fetch preset details for duplication:",K)}},je=async H=>{try{ee.removeQueries({queryKey:["cloudSettingDetail",H.setting_id]});const K=await pe.getCloudSettingDetail(H.setting_id),z=H.type==="process"?"print":H.type;L({type:z,name:H.name,base_id:K.base_id||"GFSA00",setting:K.setting||{},setting_id:H.setting_id}),T(null)}catch(K){console.error("Failed to fetch preset details for editing:",K)}},ne=()=>{c("all"),d("all"),m("all"),x("all"),b("all"),M("all"),o("")},Qe=l!=="all"||u!=="all"||f!=="all"||g!=="all"||y!=="all"||v!=="all"||a!=="",B=t.filament.length+t.printer.length+t.process.length;return s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"space-y-4 mb-6",children:[s.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsx(ba,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:a,onChange:H=>o(H.target.value),placeholder:"Search presets...",className:"w-full pl-10 pr-4 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none"})]}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{variant:q?"primary":"secondary",onClick:()=>{q?(Y(!1),Z([null,null])):Y(!0)},children:[s.jsx(Rp,{className:"w-4 h-4"}),q?"Cancel":"Compare"]}),s.jsxs(Se,{variant:"secondary",onClick:()=>D(!0),children:[s.jsx(a0,{className:"w-4 h-4"}),"Templates"]}),s.jsxs(Se,{variant:"secondary",onClick:n,disabled:r,children:[s.jsx(Mr,{className:`w-4 h-4 ${r?"animate-spin":""}`}),"Refresh"]}),s.jsxs(Se,{onClick:()=>k(!0),children:[s.jsx(Jn,{className:"w-4 h-4"}),"New Preset"]})]})]}),s.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[s.jsx(k2,{className:"w-4 h-4 text-bambu-gray"}),s.jsx(xp,{label:"Type",value:l,options:[{value:"all",label:"All",count:B},{value:"filament",label:"Filament",count:t.filament.length},{value:"printer",label:"Printer",count:t.printer.length},{value:"process",label:"Process",count:t.process.length}],onChange:H=>c(H)}),s.jsx(xp,{label:"Owner",value:u,options:[{value:"all",label:"All"},{value:"custom",label:"My Presets"},{value:"builtin",label:"Built-in"}],onChange:H=>d(H)}),le.printers.length>0&&s.jsx(xp,{label:"Printer",value:f,options:[{value:"all",label:"All"},...le.printers.map(H=>({value:H.id,label:H.name}))],onChange:m}),le.nozzles.length>0&&s.jsx(xp,{label:"Nozzle",value:g,options:[{value:"all",label:"All"},...le.nozzles.map(H=>({value:H,label:H}))],onChange:x}),le.filaments.length>0&&(l==="all"||l==="filament")&&s.jsx(xp,{label:"Filament",value:y,options:[{value:"all",label:"All"},...le.filaments.map(H=>({value:H,label:H}))],onChange:b}),le.layerHeights.length>0&&(l==="all"||l==="process")&&s.jsx(xp,{label:"Layer",value:v,options:[{value:"all",label:"All"},...le.layerHeights.map(H=>({value:H,label:H}))],onChange:M}),Qe&&s.jsx("button",{onClick:ne,className:"px-3 py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:"Clear filters"})]})]}),q&&s.jsx("div",{className:"mb-4 p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx(Rp,{className:"w-5 h-5 text-blue-400"}),s.jsx("span",{className:"text-white font-medium",children:"Compare Mode"}),s.jsx("span",{className:"text-bambu-gray",children:W[0]?`Select another ${W[0].type} preset`:"Click two presets of the same type to compare"})]}),s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${W[0]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:W[0]?W[0].name:"1. Select first"}),s.jsx(Ij,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${W[1]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:W[1]?W[1].name:"2. Select second"})]}),W[0]&&W[1]&&s.jsxs(Se,{size:"sm",onClick:async()=>{try{const[H,K]=await Promise.all([pe.getCloudSettingDetail(W[0].setting_id),pe.getCloudSettingDetail(W[1].setting_id)]);G([H.setting||{},K.setting||{}]),ae(!0)}catch{}},children:[s.jsx(Rp,{className:"w-4 h-4"}),"Compare Now"]})]})]})}),s.jsxs("div",{className:"flex flex-wrap items-center gap-4 mb-4 text-sm text-bambu-gray",children:[e&&s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx(Yn,{className:"w-3 h-3"}),"Last synced: ",uKe(e.toISOString())]}),s.jsxs("span",{children:["Showing ",fe.length," of ",B," presets"]}),s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx("span",{className:"w-1.5 h-1.5 rounded-full bg-bambu-green"}),s.jsx("span",{children:"= My preset (editable)"})]})]}),fe.length===0?s.jsxs("div",{className:"text-center py-16",children:[s.jsx(nl,{className:"w-12 h-12 text-bambu-gray-dark mx-auto mb-4"}),s.jsx("p",{className:"text-bambu-gray",children:"No presets found"}),Qe&&s.jsx("button",{onClick:ne,className:"mt-2 text-sm text-bambu-green hover:text-bambu-green-light",children:"Clear filters"})]}):s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[s.jsx(eD,{className:"w-4 h-4 text-amber-400"}),s.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Filament"}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",fe.filter(H=>H.type==="filament").length,")"]})]}),s.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[fe.filter(H=>H.type==="filament").map(H=>s.jsx(bj,{setting:H,onClick:()=>Te(H),onDuplicate:()=>_e(H),compareMode:q,isCompareSelected:ge(H)!==void 0,compareIndex:ge(H),compareDisabled:q&&!!W[0]&&W[0].type!==H.type},H.setting_id)),fe.filter(H=>H.type==="filament").length===0&&s.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No filament presets"})]})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[s.jsx(Dd,{className:"w-4 h-4 text-blue-400"}),s.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Process"}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",fe.filter(H=>H.type==="process").length,")"]})]}),s.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[fe.filter(H=>H.type==="process").map(H=>s.jsx(bj,{setting:H,onClick:()=>Te(H),onDuplicate:()=>_e(H),compareMode:q,isCompareSelected:ge(H)!==void 0,compareIndex:ge(H),compareDisabled:q&&!!W[0]&&W[0].type!==H.type},H.setting_id)),fe.filter(H=>H.type==="process").length===0&&s.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No process presets"})]})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[s.jsx(zr,{className:"w-4 h-4 text-purple-400"}),s.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Printer"}),s.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",fe.filter(H=>H.type==="printer").length,")"]})]}),s.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[fe.filter(H=>H.type==="printer").map(H=>s.jsx(bj,{setting:H,onClick:()=>Te(H),onDuplicate:()=>_e(H),compareMode:q,isCompareSelected:ge(H)!==void 0,compareIndex:ge(H),compareDisabled:q&&!!W[0]&&W[0].type!==H.type},H.setting_id)),fe.filter(H=>H.type==="printer").length===0&&s.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No printer presets"})]})]})]}),S&&s.jsx(fKe,{setting:S,onClose:()=>T(null),onDeleted:()=>T(null),onDuplicate:()=>_e(S),onEdit:()=>je(S)}),(E||C||F||X)&&s.jsx(pKe,{onClose:()=>{k(!1),P(null),L(null),R(null)},initialData:F||C||(X?{type:X.type,name:"",base_id:"",setting:X.setting}:void 0),allPresets:t}),A&&s.jsx(mKe,{onClose:()=>D(!1),onApply:H=>{R({type:H.type,setting:H.settings}),D(!1)}}),ie&&U&&W[0]&&W[1]&&s.jsx(pre,{onClose:()=>{ae(!1),G(null)},leftPreset:U[0],rightPreset:U[1],leftLabel:W[0].name,rightLabel:W[1].name})]})}function xKe(){const t=an(),{showToast:e}=Gn(),[n,r]=w.useState("cloud"),[i,a]=w.useState(),{data:o,isLoading:l}=ct({queryKey:["cloudStatus"],queryFn:pe.getCloudStatus}),{data:c=[]}=ct({queryKey:["printers"],queryFn:pe.getPrinters}),{data:u,isLoading:d,refetch:f,dataUpdatedAt:m}=ct({queryKey:["cloudSettings"],queryFn:()=>pe.getCloudSettings(),enabled:!!o?.is_authenticated,retry:!1,staleTime:1e3*60*5});w.useEffect(()=>{m&&a(new Date(m))},[m]);const g=ft({mutationFn:pe.cloudLogout,onSuccess:()=>{t.invalidateQueries({queryKey:["cloudStatus"]}),t.removeQueries({queryKey:["cloudSettings"]}),e("Logged out")}}),x=()=>{t.invalidateQueries({queryKey:["cloudStatus"]})};return l?s.jsx("div",{className:"p-4 md:p-8 flex items-center justify-center min-h-[400px]",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):s.jsxs("div",{className:"p-6 lg:p-8",children:[s.jsxs("div",{className:"mb-6",children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Profiles"}),s.jsx("p",{className:"text-bambu-gray",children:"Manage your slicer presets and pressure advance calibrations"})]}),s.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary mb-6",children:[s.jsxs("button",{onClick:()=>r("cloud"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${n==="cloud"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(T2,{className:"w-4 h-4"}),"Cloud Profiles"]}),s.jsxs("button",{onClick:()=>r("kprofiles"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${n==="kprofiles"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[s.jsx(j2,{className:"w-4 h-4"}),"K-Profiles"]})]}),n==="cloud"&&s.jsxs(s.Fragment,{children:[o?.is_authenticated&&s.jsxs("div",{className:"flex items-center justify-between p-3 mb-6 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green animate-pulse"}),s.jsxs("span",{className:"text-sm text-bambu-gray",children:["Connected as ",s.jsx("span",{className:"text-white",children:o.email})]})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>g.mutate(),disabled:g.isPending,children:[s.jsx($j,{className:"w-4 h-4"}),"Logout"]})]}),o?.is_authenticated?d?s.jsx("div",{className:"flex items-center justify-center py-16",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?s.jsx(gKe,{settings:u,lastSyncTime:i,onRefresh:()=>f(),isRefreshing:d,printers:c}):s.jsxs("div",{className:"text-center py-16",children:[s.jsx("p",{className:"text-bambu-gray mb-4",children:"Failed to load profiles"}),s.jsx(Se,{onClick:()=>f(),children:"Retry"})]}):s.jsx(dKe,{onSuccess:x})]}),n==="kprofiles"&&s.jsx(cKe,{}),s.jsx(hKe,{})]})}const Cb={Droplet:eD,Flame:gue,Ruler:Kde,Sparkles:a0,Square:al,Cable:dce,Wrench:Od,Calendar:Zr,Timer:md,Cog:Hce,Fan:GW,Zap:Mf,Wind:sq,Thermometer:U2,Layers:nl,Box:Ls,Target:nq,RefreshCw:Mr,Settings:F2,Filter:k2,CircleDot:kce};function Jb(t){return t&&Cb[t]||Od}function bp(t,e){if(e==="days")return t<1?"Today":t===1?"1 day":t<7?`${Math.round(t)} days`:t<180?`${Math.round(t/7)} weeks`:`${Math.round(t/30)} months`;{if(t<1)return`${Math.round(t*60)}m`;if(t<24)return`${t<10?t.toFixed(1):Math.round(t)}h`;const n=t/24;if(n<7)return`${n<2?n.toFixed(1):Math.round(n)}d`;const r=n/7;return r<12?`${r<2?r.toFixed(1):Math.round(r)}w`:`${Math.round(r/4)}mo`}}function yj(t,e){return e==="days"?t===1?"1 day":t===7?"1 week":t===14?"2 weeks":t===30?"1 month":t===60?"2 months":t===90?"3 months":t===180?"6 months":t===365?"1 year":`${t} days`:`${t}h`}function bKe(t,e){const n=(e||"").toUpperCase().replace(/[- ]/g,""),r=n.includes("X1"),i=n.includes("P1"),a=n.includes("A1MINI"),o=n.includes("A1")&&!a,l=n.includes("H2D"),c=n.includes("H2C"),u=n.includes("H2S"),d=l||c||u,f=n.includes("P2S");switch(t){case"Lubricate Linear Rails":return r?"https://wiki.bambulab.com/en/x1/maintenance/basic-maintenance":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":a?"https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis":o?"https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis":d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":f?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":"https://wiki.bambulab.com/en/general/lead-screws-lubrication";case"Clean Nozzle/Hotend":return r||i?"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog":a||o?"https://wiki.bambulab.com/en/a1-mini/troubleshooting/nozzle-clog":d?"https://wiki.bambulab.com/en/h2/maintenance/nozzl-cold-pull-maintenance-and-cleaning":f?"https://wiki.bambulab.com/en/p2s/maintenance/cold-pull-maintenance-hotend":"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog";case"Check Belt Tension":return r?"https://wiki.bambulab.com/en/x1/maintenance/belt-tension":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":a?"https://wiki.bambulab.com/en/a1-mini/maintenance/belt_tension":o?"https://wiki.bambulab.com/en/a1/maintenance/belt_tension":l?"https://wiki.bambulab.com/en/h2/maintenance/belt-tension":c?"https://wiki.bambulab.com/en/h2c/maintenance/belt-tension":u?"https://wiki.bambulab.com/en/h2s/maintenance/belt-tension":f?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":"https://wiki.bambulab.com/en/x1/maintenance/belt-tension";case"Clean Carbon Rods":return r||i?"https://wiki.bambulab.com/en/general/carbon-rods-clearance":a||o||d||f?null:"https://wiki.bambulab.com/en/general/carbon-rods-clearance";case"Clean Build Plate":return"https://wiki.bambulab.com/en/filament-acc/acc/pei-plate-clean-guide";case"Check PTFE Tube":return r||i?"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube":a||o?"https://wiki.bambulab.com/en/a1-mini/maintenance/ptfe-tube":l?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":u?"https://wiki.bambulab.com/en/h2s/maintenance/replace-ptfe-tube-on-h2s-printer":c?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube";case"Replace HEPA Filter":case"HEPA Filter":case"Replace Carbon Filter":case"Carbon Filter":return d?"https://wiki.bambulab.com/en/h2/maintenance/replace-smoke-purifier-air-filte":"https://wiki.bambulab.com/en/x1/maintenance/replace-carbon-filter";case"Lubricate Left Nozzle Rail":case"Left Nozzle Rail":return d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;default:return null}}function yKe({item:t,onPerform:e,onToggle:n}){const r=Jb(t.maintenance_type_icon),i=t.interval_type||"hours",o=(()=>{if(i==="days"){const f=t.days_since_maintenance??0;return Math.max(0,Math.min(100,f/t.interval_hours*100))}return Math.max(0,Math.min(100,(t.interval_hours-t.hours_until_due)/t.interval_hours*100))})(),l=()=>t.enabled?t.is_due?"text-red-400":t.is_warning?"text-amber-400":"text-bambu-green":"text-bambu-gray",c=()=>t.enabled?t.is_due?"bg-red-500":t.is_warning?"bg-amber-500":"bg-bambu-green":"bg-bambu-gray/30",u=()=>t.enabled?t.is_due?"bg-red-500/5 border-red-500/20":t.is_warning?"bg-amber-500/5 border-amber-500/20":"bg-bambu-dark-secondary border-bambu-dark-tertiary":"bg-bambu-dark-secondary/50",d=()=>{if(!t.enabled)return"Disabled";if(i==="days"){const f=t.days_until_due??0;return t.is_due?`Overdue by ${bp(Math.abs(f),"days")}`:t.is_warning?`Due in ${bp(f,"days")}`:`${bp(f,"days")} left`}else return t.is_due?`Overdue by ${bp(Math.abs(t.hours_until_due),"hours")}`:t.is_warning?`Due in ${bp(t.hours_until_due,"hours")}`:`${bp(t.hours_until_due,"hours")} left`};return s.jsx("div",{className:`rounded-xl border p-4 transition-all ${u()}`,children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsxs("div",{className:`relative p-2.5 rounded-lg ${t.is_due?"bg-red-500/20":t.is_warning?"bg-amber-500/20":t.enabled?"bg-bambu-dark":"bg-bambu-dark/50"}`,children:[s.jsx(r,{className:`w-5 h-5 ${l()}`}),t.enabled&&(t.is_due||t.is_warning)&&s.jsx("span",{className:`absolute -top-1 -right-1 w-2.5 h-2.5 rounded-full ${t.is_due?"bg-red-500":"bg-amber-500"} animate-pulse`})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("h3",{className:`font-medium truncate ${t.enabled?"text-white":"text-bambu-gray"}`,children:t.maintenance_type_name}),i==="days"&&s.jsx("span",{title:"Time-based interval",children:s.jsx(Zr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"})}),(()=>{const f=t.maintenance_type_wiki_url||bKe(t.maintenance_type_name,t.printer_model);return f?s.jsx("a",{href:f,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-bambu-green transition-colors shrink-0",title:"View documentation",onClick:m=>m.stopPropagation(),children:s.jsx(Ur,{className:"w-3.5 h-3.5"})}):null})()]}),s.jsx("div",{className:"mt-2 mb-1.5",children:s.jsx("div",{className:"w-full h-1.5 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:`h-full rounded-full transition-all duration-500 ${c()}`,style:{width:`${o}%`}})})}),s.jsxs("div",{className:`text-xs flex items-center gap-1 ${l()}`,children:[t.is_due&&s.jsx(jn,{className:"w-3 h-3"}),t.is_warning&&!t.is_due&&s.jsx(Yn,{className:"w-3 h-3"}),!t.is_due&&!t.is_warning&&t.enabled&&s.jsx(bs,{className:"w-3 h-3"}),d()]})]}),s.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[s.jsx(Xn,{checked:t.enabled,onChange:f=>n(t.id,f)}),s.jsxs(Se,{size:"sm",variant:t.is_due?"primary":"secondary",onClick:()=>e(t.id),disabled:!t.enabled,className:"!px-3",children:[s.jsx(Vf,{className:"w-3.5 h-3.5"}),"Reset"]})]})]})})}function vKe({overview:t,onPerform:e,onToggle:n,onSetHours:r}){const[i,a]=w.useState(!0),[o,l]=w.useState(!1),[c,u]=w.useState(t.total_print_hours.toFixed(1)),d=[...t.maintenance_items].sort((g,x)=>g.is_due&&!x.is_due?-1:!g.is_due&&x.is_due?1:g.is_warning&&!x.is_warning?-1:!g.is_warning&&x.is_warning?1:g.maintenance_type_id-x.maintenance_type_id),f=d.find(g=>g.enabled&&(g.is_due||g.is_warning)),m=()=>{const g=parseFloat(c);!isNaN(g)&&g>=0&&(r(t.printer_id,g),l(!1))};return s.jsxs(Mt,{className:"overflow-hidden",children:[s.jsxs("div",{className:"p-5",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx("h2",{className:"text-xl font-semibold text-white",children:t.printer_name}),s.jsxs("div",{className:"flex items-center gap-2",children:[t.due_count>0&&s.jsxs("span",{className:"px-2.5 py-1 bg-red-500/20 text-red-400 text-xs font-medium rounded-full flex items-center gap-1.5",children:[s.jsx(jn,{className:"w-3 h-3"}),t.due_count," overdue"]}),t.warning_count>0&&s.jsxs("span",{className:"px-2.5 py-1 bg-amber-500/20 text-amber-400 text-xs font-medium rounded-full flex items-center gap-1.5",children:[s.jsx(Yn,{className:"w-3 h-3"}),t.warning_count," due soon"]}),t.due_count===0&&t.warning_count===0&&s.jsxs("span",{className:"px-2.5 py-1 bg-bambu-green/20 text-bambu-green text-xs font-medium rounded-full flex items-center gap-1.5",children:[s.jsx(bs,{className:"w-3 h-3"}),"All good"]})]})]}),s.jsxs("button",{onClick:()=>a(!i),className:"flex items-center gap-1.5 px-3 py-1.5 text-sm text-bambu-gray hover:text-white hover:bg-bambu-dark rounded-lg transition-colors",children:[i?s.jsx(eu,{className:"w-4 h-4"}):s.jsx(ea,{className:"w-4 h-4"}),i?"Collapse":"Expand"]})]}),s.jsxs("div",{className:"flex items-center gap-6 mt-4",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 bg-bambu-dark/50 rounded-lg",children:s.jsx(md,{className:"w-4 h-4 text-bambu-gray"})}),o?s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",value:c,onChange:g=>u(g.target.value),onKeyDown:g=>{g.key==="Enter"&&m(),g.key==="Escape"&&l(!1)},className:"w-24 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm",min:"0",step:"1",autoFocus:!0}),s.jsx("span",{className:"text-xs text-bambu-gray",children:"hours"}),s.jsx(Se,{size:"sm",onClick:m,children:"Save"}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>l(!1),children:"Cancel"})]}):s.jsxs("button",{onClick:()=>{u(Math.round(t.total_print_hours).toString()),l(!0)},className:"group",children:[s.jsxs("div",{className:"text-sm font-medium text-white group-hover:text-bambu-green transition-colors flex items-center gap-1",children:[Math.round(t.total_print_hours)," hours",s.jsx(wf,{className:"w-3 h-3 text-bambu-gray group-hover:text-bambu-green"})]}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Total print time"})]})]}),s.jsx("div",{className:"w-px h-10 bg-bambu-dark-tertiary"}),f&&s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${f.is_due?"bg-red-500/20":"bg-amber-500/20"}`,children:(()=>{const g=Jb(f.maintenance_type_icon);return s.jsx(g,{className:`w-4 h-4 ${f.is_due?"text-red-400":"text-amber-400"}`})})()}),s.jsxs("div",{children:[s.jsx("div",{className:`text-sm font-medium ${f.is_due?"text-red-400":"text-amber-400"}`,children:f.maintenance_type_name}),s.jsx("div",{className:`text-xs ${f.is_due?"text-red-400/70":"text-amber-400/70"}`,children:f.is_due?"Overdue":"Due soon"})]})]})]})]}),i&&s.jsx(Tt,{className:"pt-0 border-t border-bambu-dark-tertiary",children:s.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-3 pt-4",children:d.map(g=>s.jsx(yKe,{item:g,onPerform:e,onToggle:n},g.id))})})]})}function _Ke({overview:t,types:e,onUpdateInterval:n,onAddType:r,onUpdateType:i,onDeleteType:a,onAssignType:o,onRemoveItem:l}){const[c,u]=w.useState(null),[d,f]=w.useState(""),[m,g]=w.useState("hours"),[x,y]=w.useState(!1),[b,v]=w.useState(""),[M,S]=w.useState("100"),[T,E]=w.useState("hours"),[k,A]=w.useState("Wrench"),[D,C]=w.useState(""),[P,F]=w.useState(new Set),[L,X]=w.useState(null),R=w.useMemo(()=>t?t.map(z=>({id:z.printer_id,name:z.printer_name})):[],[t]),q=z=>t?t.filter(V=>V.maintenance_items.some(Q=>Q.maintenance_type_id===z)).map(V=>({printerId:V.printer_id,printerName:V.printer_name,itemId:V.maintenance_items.find(Q=>Q.maintenance_type_id===z)?.id})):[],Y=z=>{if(!t)return[];const V=new Set(q(z).map(Q=>Q.printerId));return R.filter(Q=>!V.has(Q.id))},[W,Z]=w.useState(null),[ie,ae]=w.useState(""),[U,G]=w.useState(""),[ee,re]=w.useState("hours"),[le,he]=w.useState("Wrench"),[fe,Te]=w.useState(""),ge=z=>{Z(z),ae(z.name),G(z.default_interval_hours.toString()),re(z.interval_type||"hours"),he(z.icon||"Wrench"),Te(z.wiki_url||"")},_e=()=>{W&&ie.trim()&&parseFloat(U)>0&&(i(W.id,{name:ie.trim(),default_interval_hours:parseFloat(U),interval_type:ee,icon:le,wiki_url:fe.trim()||null}),Z(null))},je=(z,V,Q)=>{const Ne=parseFloat(d);if(!isNaN(Ne)&&Ne>0){const ke=Math.abs(Ne-V)<.01?null:Ne;n(z,{custom_interval_hours:ke,custom_interval_type:m!==Q?m:null})}u(null)},ne=z=>{z.preventDefault(),b.trim()&&parseFloat(M)>0&&P.size>0&&(r({name:b.trim(),default_interval_hours:parseFloat(M),interval_type:T,icon:k,wiki_url:D.trim()||null},Array.from(P)),v(""),S("100"),E("hours"),C(""),F(new Set),y(!1))},Qe=z=>{F(V=>{const Q=new Set(V);return Q.has(z)?Q.delete(z):Q.add(z),Q})},B=t?.map(z=>({printerId:z.printer_id,printerName:z.printer_name,items:z.maintenance_items.sort((V,Q)=>V.maintenance_type_id-Q.maintenance_type_id)})).sort((z,V)=>z.printerName.localeCompare(V.printerName))||[],H=e.filter(z=>z.is_system),K=e.filter(z=>!z.is_system);return s.jsxs("div",{className:"space-y-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("div",{children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Maintenance Types"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"System types and your custom maintenance tasks"})]}),s.jsxs(Se,{onClick:()=>y(!x),children:[s.jsx(Jn,{className:"w-4 h-4"}),"Add Custom Type"]})]}),x&&s.jsx(Mt,{className:"mb-6",children:s.jsx(Tt,{className:"py-4",children:s.jsxs("form",{onSubmit:ne,children:[s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[s.jsxs("div",{className:"lg:col-span-2",children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Name"}),s.jsx("input",{type:"text",value:b,onChange:z=>v(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"e.g., Replace HEPA Filter",autoFocus:!0})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Interval Type"}),s.jsxs("select",{value:T,onChange:z=>{E(z.target.value),z.target.value==="days"?S("30"):S("100")},className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"hours",children:"Print Hours"}),s.jsx("option",{value:"days",children:"Calendar Days"})]})]}),s.jsxs("div",{children:[s.jsxs("label",{className:"block text-xs text-bambu-gray mb-1.5",children:["Interval (",T==="days"?"days":"hours",")"]}),s.jsx("input",{type:"number",value:M,onChange:z=>S(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",min:"1"})]})]}),s.jsx("div",{className:"mt-4 flex items-end justify-between",children:s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Icon"}),s.jsx("div",{className:"flex gap-1",children:Object.keys(Cb).map(z=>{const V=Cb[z];return s.jsx("button",{type:"button",onClick:()=>A(z),className:`p-2 rounded-lg transition-colors ${k===z?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:s.jsx(V,{className:"w-4 h-4"})},z)})})]})}),s.jsxs("div",{className:"mt-4",children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Documentation Link (optional)"}),s.jsx("input",{type:"url",value:D,onChange:z=>C(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"https://wiki.bambulab.com/..."})]}),s.jsxs("div",{className:"mt-4",children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Assign to Printers"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:R.map(z=>s.jsx("button",{type:"button",onClick:()=>Qe(z.id),className:`px-3 py-1.5 rounded-lg text-sm transition-colors ${P.has(z.id)?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:z.name},z.id))}),P.size===0&&s.jsx("p",{className:"text-xs text-orange-400 mt-1",children:"Select at least one printer"})]}),s.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:()=>{y(!1),F(new Set)},children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!b.trim()||P.size===0,children:"Add Type"})]})]})})}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3",children:[H.map(z=>{const V=Jb(z.icon),Q=z.interval_type||"hours";return s.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-dark rounded-lg",children:s.jsx(V,{className:"w-5 h-5 text-bambu-gray"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("div",{className:"text-sm font-medium text-white truncate",children:z.name}),s.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[Q==="days"?s.jsx(Zr,{className:"w-3 h-3"}):s.jsx(md,{className:"w-3 h-3"}),yj(z.default_interval_hours,Q)]})]})]})},z.id)}),K.map(z=>{const V=Jb(z.icon),Q=z.interval_type||"hours";if(W?.id===z.id)return s.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green",children:s.jsxs("div",{className:"space-y-3",children:[s.jsx("input",{type:"text",value:ie,onChange:I=>ae(I.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"Name",autoFocus:!0}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("select",{value:ee,onChange:I=>re(I.target.value),className:"flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"hours",children:"Print Hours"}),s.jsx("option",{value:"days",children:"Calendar Days"})]}),s.jsx("input",{type:"number",value:U,onChange:I=>G(I.target.value),className:"w-24 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",min:"1"})]}),s.jsx("div",{className:"flex flex-wrap gap-1",children:Object.keys(Cb).map(I=>{const J=Cb[I];return s.jsx("button",{type:"button",onClick:()=>he(I),className:`p-1.5 rounded transition-colors ${le===I?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:s.jsx(J,{className:"w-3.5 h-3.5"})},I)})}),s.jsx("input",{type:"url",value:fe,onChange:I=>Te(I.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"Documentation link (optional)"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{size:"sm",onClick:_e,disabled:!ie.trim(),children:"Save"}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>Z(null),children:"Cancel"})]})]})},z.id);const ke=q(z.id),Me=Y(z.id),$=L===z.id;return s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green/30",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-green/20 rounded-lg",children:s.jsx(V,{className:"w-5 h-5 text-bambu-green"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-sm font-medium text-white truncate",children:z.name}),s.jsx("span",{className:"px-1.5 py-0.5 bg-bambu-green/20 text-bambu-green text-[10px] font-medium rounded",children:"Custom"})]}),s.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[Q==="days"?s.jsx(Zr,{className:"w-3 h-3"}):s.jsx(md,{className:"w-3 h-3"}),yj(z.default_interval_hours,Q)]})]}),s.jsxs("button",{onClick:()=>X($?null:z.id),className:`px-2 py-1 rounded-lg border transition-colors flex items-center gap-1 ${ke.length>0?"border-bambu-green/50 bg-bambu-green/10 text-bambu-green hover:bg-bambu-green/20":"border-orange-400/50 bg-orange-400/10 text-orange-400 hover:bg-orange-400/20"}`,title:`${ke.length} printer(s) assigned - click to manage`,children:[s.jsx(zr,{className:"w-3 h-3"}),s.jsx("span",{className:"text-xs font-medium",children:ke.length}),s.jsx(ea,{className:`w-3 h-3 transition-transform ${$?"rotate-180":""}`})]}),s.jsx("button",{onClick:()=>ge(z),className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",children:s.jsx(wf,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>{confirm(`Delete "${z.name}"?`)&&a(z.id)},className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors",children:s.jsx(ln,{className:"w-4 h-4"})})]}),$&&s.jsxs("div",{className:"mt-3 pt-3 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Assigned to printers:"}),ke.length===0?s.jsx("p",{className:"text-xs text-orange-400",children:"No printers assigned"}):s.jsx("div",{className:"flex flex-wrap gap-1 mb-2",children:ke.map(I=>s.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark rounded text-xs text-white",children:[I.printerName,s.jsx("button",{onClick:()=>I.itemId&&l(I.itemId),className:"hover:text-red-400 ml-1",title:"Remove from this printer",children:"×"})]},I.printerId))}),Me.length>0&&s.jsxs("div",{className:"flex flex-wrap gap-1",children:[s.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:"Add:"}),Me.map(I=>s.jsxs("button",{onClick:()=>o(I.id,z.id),className:"px-2 py-1 bg-bambu-dark hover:bg-bambu-green/20 rounded text-xs text-bambu-gray hover:text-bambu-green transition-colors",children:["+ ",I.name]},I.id))]})]})]},z.id)})]})]}),B.length>0&&s.jsxs("div",{children:[s.jsxs("div",{className:"mb-4",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Interval Overrides"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize intervals for specific printers"})]}),s.jsx("div",{className:"space-y-4",children:B.map(z=>s.jsx(Mt,{children:s.jsxs(Tt,{className:"py-4",children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:z.printerName}),s.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2",children:z.items.map(V=>{const Q=Jb(V.maintenance_type_icon),Ne=e.find(J=>J.id===V.maintenance_type_id),ke=Ne?.default_interval_hours||V.interval_hours,Me=Ne?.interval_type||"hours",$=V.interval_type||"hours",I=c===V.id;return s.jsxs("div",{className:"flex items-center gap-2 p-2.5 bg-bambu-dark rounded-lg",children:[s.jsx(Q,{className:"w-4 h-4 text-bambu-gray shrink-0"}),s.jsx("span",{className:"text-xs text-bambu-gray flex-1 truncate",children:V.maintenance_type_name}),I?s.jsxs("div",{className:"flex items-center gap-1",children:[m==="days"?s.jsx(Zr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}):s.jsx(md,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}),s.jsxs("select",{value:m,onChange:J=>g(J.target.value),className:"px-1.5 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",children:[s.jsx("option",{value:"hours",children:"Print Hours"}),s.jsx("option",{value:"days",children:"Calendar Days"})]}),s.jsx("input",{type:"number",value:d,onChange:J=>f(J.target.value),onKeyDown:J=>{J.key==="Enter"&&je(V.id,ke,Me),J.key==="Escape"&&u(null)},className:"w-16 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",min:"1"}),s.jsx(Se,{size:"sm",onClick:()=>je(V.id,ke,Me),children:"OK"})]}):s.jsxs("button",{onClick:()=>{u(V.id),f(V.interval_hours.toString()),g($)},className:"px-2 py-1 bg-bambu-dark-tertiary hover:bg-bambu-dark-secondary border border-bambu-dark-tertiary hover:border-bambu-green rounded text-xs font-medium text-white transition-colors flex items-center gap-1",children:[$==="days"?s.jsx(Zr,{className:"w-3 h-3"}):s.jsx(md,{className:"w-3 h-3"}),yj(V.interval_hours,$),s.jsx(wf,{className:"w-3 h-3 text-bambu-gray"})]})]},V.id)})})]})},z.printerId))})]}),B.length===0&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"text-center py-12",children:[s.jsx(Yn,{className:"w-12 h-12 mx-auto mb-4 text-bambu-gray/30"}),s.jsx("p",{className:"text-bambu-gray",children:"No printers configured"}),s.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1",children:"Add printers to configure maintenance intervals"})]})})]})}function wKe(){const t=an(),{showToast:e}=Gn(),[n,r]=w.useState("status"),{data:i,isLoading:a}=ct({queryKey:["maintenanceOverview"],queryFn:pe.getMaintenanceOverview}),{data:o}=ct({queryKey:["maintenanceTypes"],queryFn:pe.getMaintenanceTypes}),l=ft({mutationFn:({id:S,notes:T})=>pe.performMaintenance(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Maintenance marked as complete")},onError:S=>{e(S.message,"error")}}),c=ft({mutationFn:({id:S,data:T})=>pe.updateMaintenanceItem(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]})},onError:S=>{e(S.message,"error")}}),u=ft({mutationFn:({id:S,data:T})=>pe.updateMaintenanceType(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type updated")},onError:S=>{e(S.message,"error")}}),d=ft({mutationFn:pe.deleteMaintenanceType,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type deleted")},onError:S=>{e(S.message,"error")}}),f=ft({mutationFn:({printerId:S,hours:T})=>pe.setPrinterHours(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Print hours updated")},onError:S=>{e(S.message,"error")}}),m=ft({mutationFn:({printerId:S,typeId:T})=>pe.assignMaintenanceType(S,T),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer assigned")},onError:S=>{e(S.message,"error")}}),g=ft({mutationFn:pe.removeMaintenanceItem,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer removed")},onError:S=>{e(S.message,"error")}}),x=S=>{l.mutate({id:S})},y=(S,T)=>{c.mutate({id:S,data:{enabled:T}})},b=(S,T)=>{f.mutate({printerId:S,hours:T})};if(a)return s.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})});const v=i?.reduce((S,T)=>S+T.due_count,0)||0,M=i?.reduce((S,T)=>S+T.warning_count,0)||0;return s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsxs("div",{className:"mb-6",children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Maintenance"}),s.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:n==="status"?s.jsxs(s.Fragment,{children:[v>0&&s.jsxs("span",{className:"text-red-400",children:[v," task",v!==1?"s":""," overdue"]}),v>0&&M>0&&" · ",M>0&&s.jsxs("span",{className:"text-amber-400",children:[M," due soon"]}),v===0&&M===0&&s.jsx("span",{className:"text-bambu-green",children:"All maintenance up to date"})]}):"Configure maintenance types and intervals"})]}),s.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[s.jsx("button",{onClick:()=>r("status"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${n==="status"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:"Status"}),s.jsx("button",{onClick:()=>r("settings"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${n==="settings"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:"Settings"})]}),n==="status"?s.jsx("div",{className:"space-y-6",children:i&&i.length>0?[...i].sort((S,T)=>{const E=S.due_count*10+S.warning_count,k=T.due_count*10+T.warning_count;return E!==k?k-E:S.printer_name.localeCompare(T.printer_name)}).map(S=>s.jsx(vKe,{overview:S,onPerform:x,onToggle:y,onSetHours:b},S.printer_id)):s.jsx(Mt,{children:s.jsxs(Tt,{className:"text-center py-16",children:[s.jsx(Od,{className:"w-16 h-16 mx-auto mb-4 text-bambu-gray/30"}),s.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No printers configured"}),s.jsx("p",{className:"text-bambu-gray",children:"Add printers to start tracking maintenance"})]})})}):s.jsx(_Ke,{overview:i,types:o||[],onUpdateInterval:(S,T)=>c.mutate({id:S,data:T}),onAddType:async(S,T)=>{const E=await pe.createMaintenanceType(S);for(const k of T)await pe.assignMaintenanceType(k,E.id);t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type added")},onUpdateType:(S,T)=>u.mutate({id:S,data:T}),onDeleteType:S=>d.mutate(S),onAssignType:(S,T)=>m.mutate({printerId:S,typeId:T}),onRemoveItem:S=>g.mutate(S)})]})}const qG=["#ef4444","#f97316","#eab308","#22c55e","#06b6d4","#3b82f6","#8b5cf6","#ec4899","#6b7280"];function gre({project:t,onClose:e,onSave:n,isLoading:r}){const[i,a]=w.useState(t?.name||""),[o,l]=w.useState(t?.description||""),[c,u]=w.useState(t?.color||qG[0]),[d,f]=w.useState(t?.target_count?.toString()||""),[m,g]=w.useState(t?.target_parts_count?.toString()||""),[x,y]=w.useState(t?.status||"active"),[b,v]=w.useState(t?.tags||""),[M,S]=w.useState(t?.due_date?.split("T")[0]||""),[T,E]=w.useState(t?.priority||"normal"),k=A=>{A.preventDefault(),n({name:i.trim(),description:o.trim()||void 0,color:c,target_count:d?parseInt(d,10):void 0,target_parts_count:m?parseInt(m,10):void 0,tags:b.trim()||void 0,due_date:M||void 0,priority:T,...t&&{status:x}})};return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:t?"Edit Project":"New Project"})}),s.jsxs("form",{onSubmit:k,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Name"}),s.jsx("input",{type:"text",value:i,onChange:A=>a(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., Voron 2.4 Build",required:!0})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Description"}),s.jsx("textarea",{value:o,onChange:A=>l(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green resize-none",placeholder:"Optional description...",rows:2})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Color"}),s.jsx("div",{className:"flex gap-2 flex-wrap",children:qG.map(A=>s.jsx("button",{type:"button",onClick:()=>u(A),className:`w-8 h-8 rounded-full transition-transform ${c===A?"ring-2 ring-white ring-offset-2 ring-offset-bambu-dark-secondary scale-110":""}`,style:{backgroundColor:A}},A))})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Target Plates"}),s.jsx("input",{type:"number",value:d,onChange:A=>f(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., 25",min:"1"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Number of print jobs"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Target Parts"}),s.jsx("input",{type:"number",value:m,onChange:A=>g(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., 150",min:"1"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Total objects needed"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Tags (comma-separated)"}),s.jsx("input",{type:"text",value:b,onChange:A=>v(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., voron, functional, gift"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Due Date"}),s.jsx("input",{type:"date",value:M,onChange:A=>S(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white focus:outline-none focus:border-bambu-green"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Priority"}),s.jsxs("select",{value:T,onChange:A=>E(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"low",children:"Low"}),s.jsx("option",{value:"normal",children:"Normal"}),s.jsx("option",{value:"high",children:"High"}),s.jsx("option",{value:"urgent",children:"Urgent"})]})]})]}),t&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Status"}),s.jsxs("select",{value:x,onChange:A=>y(A.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"active",children:"Active"}),s.jsx("option",{value:"completed",children:"Completed"}),s.jsx("option",{value:"archived",children:"Archived"})]})]}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!i.trim()||r,children:r?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):t?"Save":"Create"})]})]})]})})}function SKe({project:t,onClick:e,onEdit:n,onDelete:r}){const i=t.target_count?Math.round(t.archive_count/t.target_count*100):0,a=t.target_parts_count?Math.round(t.completed_count/t.target_parts_count*100):0,o=t.status==="completed",l=t.status==="archived",[c,u]=w.useState(!1),f=o?{icon:E2,color:"text-bambu-green",bg:"bg-bambu-green/10"}:l?{icon:Va,color:"text-bambu-gray",bg:"bg-bambu-gray/10"}:t.queue_count>0?{icon:Yn,color:"text-blue-400",bg:"bg-blue-400/10"}:{icon:ya,color:"text-bambu-gray",bg:"bg-bambu-gray/10"};return s.jsxs("div",{className:"group relative bg-gradient-to-br from-bambu-card to-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary hover:border-bambu-green/50 hover:shadow-lg hover:shadow-bambu-green/5 transition-all duration-300 cursor-pointer overflow-hidden",onClick:e,children:[s.jsx("div",{className:"absolute top-0 left-0 w-1.5 h-full",style:{backgroundColor:t.color||"#6b7280",boxShadow:`0 0 12px ${t.color||"#6b7280"}40`}}),s.jsxs("div",{className:"p-5 pl-6",children:[s.jsxs("div",{className:"flex items-start justify-between mb-4",children:[s.jsxs("div",{className:"flex items-center gap-3 min-w-0 flex-1",children:[s.jsx("div",{className:`p-2 rounded-lg ${f.bg} flex-shrink-0`,children:s.jsx(f.icon,{className:`w-5 h-5 ${f.color}`})}),s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[s.jsx("h3",{className:"font-semibold text-white truncate",children:t.name}),t.target_parts_count?s.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${a>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.completed_count,"/",t.target_parts_count," parts"]}):t.target_count?s.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${i>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.archive_count,"/",t.target_count," plates"]}):t.completed_count>0?s.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium bg-bambu-dark text-bambu-gray",children:[t.completed_count," parts"]}):null,o&&s.jsx("span",{className:"text-xs bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full whitespace-nowrap",children:"Done"}),l&&s.jsx("span",{className:"text-xs bg-bambu-gray/20 text-bambu-gray px-2 py-0.5 rounded-full whitespace-nowrap",children:"Archived"})]}),t.description&&s.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1 line-clamp-1",children:t.description}),t.archives&&t.archives.length>0&&(()=>{const m=t.archives.map(b=>b.filament_type).filter(Boolean).flatMap(b=>b.split(",").map(v=>v.trim())).filter(Boolean),g=[...new Set(m)],x=t.archives.map(b=>b.filament_color).filter(Boolean).flatMap(b=>b.split(",").map(v=>v.trim())).filter(b=>b.startsWith("#")||/^[0-9A-Fa-f]{6}$/.test(b)),y=[...new Set(x)];return g.length===0&&y.length===0?null:s.jsxs("div",{className:"flex items-center gap-2 mt-1.5",children:[g.slice(0,3).map(b=>s.jsx("span",{className:"text-[10px] px-1.5 py-0.5 bg-bambu-dark text-bambu-gray rounded",children:b},b)),y.length>0&&s.jsxs("div",{className:"flex items-center gap-0.5",children:[y.slice(0,5).map(b=>s.jsx("div",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:b.startsWith("#")?b:`#${b}`},title:b},b)),y.length>5&&s.jsxs("span",{className:"text-[10px] text-bambu-gray ml-0.5",children:["+",y.length-5]})]})]})})()]})]}),s.jsxs("div",{className:"relative",onClick:m=>m.stopPropagation(),children:[s.jsx("button",{className:"p-1.5 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors opacity-0 group-hover:opacity-100",onClick:()=>u(!c),children:s.jsx(wd,{className:"w-4 h-4"})}),c&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>u(!1)}),s.jsxs("div",{className:"absolute right-0 top-8 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]",children:[s.jsxs("button",{className:"w-full px-3 py-2 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{n(),u(!1)},children:[s.jsx(wf,{className:"w-4 h-4"}),"Edit"]}),s.jsxs("button",{className:"w-full px-3 py-2 text-left text-sm text-red-400 hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{r(),u(!1)},children:[s.jsx(ln,{className:"w-4 h-4"}),"Delete"]})]})]})]})]}),s.jsx("div",{className:"mb-4",children:t.target_count||t.target_parts_count?s.jsxs("div",{className:"space-y-3",children:[t.target_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[s.jsx("span",{className:"text-bambu-gray",children:"Plates"}),s.jsxs("span",{className:i>=100?"text-bambu-green font-medium":"text-white",children:[t.archive_count," / ",t.target_count]})]}),s.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:s.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(i,100)}%`,background:i>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${i>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.target_parts_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[s.jsx("span",{className:"text-bambu-gray",children:"Parts"}),s.jsxs("span",{className:a>=100?"text-bambu-green font-medium":"text-white",children:[t.completed_count," / ",t.target_parts_count]})]}),s.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:s.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(a,100)}%`,background:a>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${a>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.failed_count>0&&s.jsxs("div",{className:"text-xs text-red-400",children:[t.failed_count," failed"]})]}):t.completed_count>0||t.failed_count>0?s.jsxs("div",{className:"flex items-center gap-4 text-xs",children:[t.completed_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-bambu-gray",children:[s.jsx(Va,{className:"w-3.5 h-3.5"}),s.jsxs("span",{children:[t.completed_count," completed"]})]}),t.failed_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",children:[s.jsx(jn,{className:"w-3.5 h-3.5"}),s.jsxs("span",{children:[t.failed_count," failed"]})]}),t.queue_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",children:[s.jsx(Yn,{className:"w-3.5 h-3.5"}),s.jsxs("span",{children:[t.queue_count," in queue"]})]})]}):s.jsx("div",{className:"text-xs text-bambu-gray/60 italic",children:"No prints yet"})}),t.archives&&t.archives.length>0&&s.jsxs("div",{className:"mb-4",children:[s.jsx("div",{className:"grid grid-cols-4 gap-1.5",children:t.archives.slice(0,4).map(m=>s.jsxs("div",{className:"relative aspect-square rounded-lg bg-bambu-dark overflow-hidden border border-bambu-dark-tertiary",title:m.print_name||"Unknown",children:[m.thumbnail_path?s.jsx("img",{src:pe.getArchiveThumbnail(m.id),alt:m.print_name||"",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray/50",children:s.jsx(Ll,{className:"w-6 h-6"})}),m.status==="failed"&&s.jsx("div",{className:"absolute inset-0 bg-red-500/40 flex items-center justify-center",children:s.jsx(jn,{className:"w-4 h-4 text-white"})})]},m.id))}),t.archive_count>4&&s.jsxs("p",{className:"text-xs text-bambu-gray mt-1.5 text-center",children:["+",t.archive_count-4," more"]})]}),s.jsxs("div",{className:"flex items-center justify-between pt-3 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-4 text-xs text-bambu-gray",children:[s.jsxs("div",{className:"flex items-center gap-1.5",title:"Print jobs (plates)",children:[s.jsx(nl,{className:"w-3.5 h-3.5 text-blue-400"}),s.jsxs("span",{children:[t.archive_count," plates"]})]}),s.jsxs("div",{className:"flex items-center gap-1.5",title:"Parts printed",children:[s.jsx(Ll,{className:"w-3.5 h-3.5 text-bambu-green"}),s.jsxs("span",{children:[t.completed_count," parts"]})]}),t.failed_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",title:"Failed parts",children:[s.jsx(jn,{className:"w-3.5 h-3.5"}),s.jsx("span",{children:t.failed_count})]}),t.queue_count>0&&s.jsxs("div",{className:"flex items-center gap-1.5 text-yellow-400",title:"In queue",children:[s.jsx(Bj,{className:"w-3.5 h-3.5"}),s.jsx("span",{children:t.queue_count})]})]}),s.jsx(Ws,{className:"w-4 h-4 text-bambu-gray/50 group-hover:text-bambu-gray transition-colors"})]})]})]})}function MKe(){const t=cl(),e=an(),{showToast:n}=Gn(),[r,i]=w.useState(!1),[a,o]=w.useState(),[l,c]=w.useState("active"),[u,d]=w.useState(null),{data:f,isLoading:m}=ct({queryKey:["projects",l==="all"?void 0:l],queryFn:()=>pe.getProjects(l==="all"?void 0:l)}),g=ft({mutationFn:k=>pe.createProject(k),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),n("Project created","success")},onError:k=>{n(k.message,"error")}}),x=ft({mutationFn:({id:k,data:A})=>pe.updateProject(k,A),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),o(void 0),n("Project updated","success")},onError:k=>{n(k.message,"error")}}),y=ft({mutationFn:k=>pe.deleteProject(k),onSuccess:()=>{d(null),n("Project deleted","success"),setTimeout(()=>window.location.reload(),100)},onError:k=>{d(null),n(k.message,"error")}}),b=k=>{a?x.mutate({id:a.id,data:k}):g.mutate(k)},v=k=>{o(k),i(!0)},M=k=>{t(`/projects/${k.id}`)},S=k=>{d(k)},T=()=>{u!==null&&y.mutate(u)},E=f?.reduce((k,A)=>(k[A.status]=(k[A.status]||0)+1,k.all=(k.all||0)+1,k),{})||{};return s.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[s.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[s.jsxs("div",{children:[s.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:s.jsx(ya,{className:"w-6 h-6 text-bambu-green"})}),"Projects"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:"Organize and track your 3D printing projects"})]}),s.jsxs(Se,{onClick:()=>i(!0),className:"sm:w-auto w-full",children:[s.jsx(Jn,{className:"w-4 h-4 mr-2"}),"New Project"]})]}),s.jsx("div",{className:"flex gap-1 p-1 bg-bambu-dark rounded-xl w-fit",children:[{key:"active",label:"Active",icon:Yn},{key:"completed",label:"Completed",icon:E2},{key:"archived",label:"Archived",icon:Va},{key:"all",label:"All",icon:ya}].map(({key:k,label:A,icon:D})=>s.jsxs("button",{onClick:()=>c(k),className:`flex items-center gap-2 px-4 py-2 text-sm rounded-lg transition-all ${l===k?"bg-bambu-card text-white shadow-sm":"text-bambu-gray hover:text-white"}`,children:[s.jsx(D,{className:"w-4 h-4"}),s.jsx("span",{children:A}),E[k]>0&&s.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded-full ${l===k?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary"}`,children:E[k]})]},k))}),m?s.jsx("div",{className:"flex items-center justify-center py-20",children:s.jsxs("div",{className:"flex flex-col items-center gap-3",children:[s.jsx(ht,{className:"w-8 h-8 animate-spin text-bambu-green"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Loading projects..."})]})}):f?.length===0?s.jsxs("div",{className:"flex flex-col items-center justify-center py-20 px-4",children:[s.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:s.jsx(ya,{className:"w-12 h-12 text-bambu-gray/50"})}),s.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:l==="all"?"No projects yet":`No ${l} projects`}),s.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:l==="all"?"Create your first project to start organizing related prints, tracking progress, and managing your builds.":`You don't have any ${l} projects. Projects will appear here when their status changes.`}),l==="all"&&s.jsxs(Se,{onClick:()=>i(!0),children:[s.jsx(Jn,{className:"w-4 h-4 mr-2"}),"Create Your First Project"]})]}):s.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-8",children:f?.map(k=>s.jsx(SKe,{project:k,onClick:()=>M(k),onEdit:()=>v(k),onDelete:()=>S(k.id)},k.id))}),u!==null&&s.jsx(Un,{title:"Delete Project",message:"Are you sure you want to delete this project? Archives and queue items will be unlinked but not deleted.",confirmText:"Delete Project",variant:"danger",onConfirm:T,onCancel:()=>d(null)}),r&&s.jsx(gre,{project:a,onClose:()=>{i(!1),o(void 0)},onSave:b,isLoading:g.isPending||x.isPending})]})}function NKe(t){if(t<1)return`${Math.round(t*60)}m`;const e=Math.floor(t),n=Math.round((t-e)*60);return n>0?`${e}h ${n}m`:`${e}h`}function EKe(t){return t>=1e3?`${(t/1e3).toFixed(2)}kg`:`${Math.round(t)}g`}function TKe({status:t}){const e={active:"bg-bambu-green/20 text-bambu-green",completed:"bg-blue-500/20 text-blue-400",archived:"bg-bambu-gray/20 text-bambu-gray"},n=e[t]||e.active;return s.jsx("span",{className:`px-2 py-1 rounded text-sm font-medium ${n}`,children:t.charAt(0).toUpperCase()+t.slice(1)})}function KG({icon:t,label:e,value:n,subValue:r,hint:i,color:a="text-bambu-gray"}){return s.jsx(Mt,{children:s.jsx(Tt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",title:i,children:[s.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark ${a}`,children:s.jsx(t,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:e}),s.jsx("p",{className:"text-xl font-semibold text-white",children:n}),r&&s.jsx("p",{className:"text-xs text-bambu-gray/70",children:r})]})]})})})}function AKe({archives:t}){return t.length===0?s.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[s.jsx(Ll,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),s.jsx("p",{children:"No prints in this project yet"})]}):s.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-3",children:t.map(e=>s.jsxs(cs,{to:`/archives?search=${encodeURIComponent(e.print_name||"")}`,className:"group relative aspect-square rounded-lg bg-bambu-dark border border-bambu-dark-tertiary overflow-hidden hover:border-bambu-green transition-colors",children:[e.thumbnail_path?s.jsx("img",{src:pe.getArchiveThumbnail(e.id),alt:e.print_name||"Print",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:s.jsx(Ll,{className:"w-8 h-8"})}),e.status==="failed"&&s.jsx("div",{className:"absolute inset-0 bg-red-500/30 flex items-center justify-center",children:s.jsx(Ti,{className:"w-8 h-8 text-white"})}),e.status==="completed"&&s.jsx("div",{className:"absolute top-1 right-1",children:s.jsx(On,{className:"w-4 h-4 text-bambu-green"})}),s.jsx("div",{className:"absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/80 to-transparent p-2 opacity-0 group-hover:opacity-100 transition-opacity",children:s.jsx("p",{className:"text-xs text-white truncate",children:e.print_name||"Unknown"})})]},e.id))})}function CKe({priority:t}){const e={low:{color:"bg-gray-500/20 text-gray-400",label:"Low"},normal:{color:"bg-blue-500/20 text-blue-400",label:"Normal"},high:{color:"bg-orange-500/20 text-orange-400",label:"High"},urgent:{color:"bg-red-500/20 text-red-400",label:"Urgent"}},{color:n,label:r}=e[t]||e.normal;return s.jsxs("span",{className:`px-2 py-1 rounded text-xs font-medium flex items-center gap-1 ${n}`,children:[t==="urgent"&&s.jsx(jn,{className:"w-3 h-3"}),r]})}function kKe(t){return t?Jp(t,{year:"numeric",month:"short",day:"numeric"}):""}function vj(t){if(!t)return null;const e=pi(t);if(!e)return null;const n=new Date,r=Math.ceil((e.getTime()-n.getTime())/(1e3*60*60*24));return r<0?{color:"text-red-400",label:"Overdue"}:r===0?{color:"text-orange-400",label:"Due today"}:r<=3?{color:"text-yellow-400",label:`${r} days left`}:{color:"text-bambu-gray",label:`${r} days left`}}function jKe(){const{id:t}=HR(),e=cl(),n=an(),{showToast:r}=Gn(),[i,a]=w.useState(!1),[o,l]=w.useState(!1),[c,u]=w.useState(""),d=parseInt(t||"0",10),{data:f,isLoading:m,error:g}=ct({queryKey:["project",d],queryFn:()=>pe.getProject(d),enabled:d>0}),{data:x,isLoading:y}=ct({queryKey:["project-archives",d],queryFn:()=>pe.getProjectArchives(d),enabled:d>0}),{data:b,isLoading:v}=ct({queryKey:["project-bom",d],queryFn:()=>pe.getProjectBOM(d),enabled:d>0}),{data:M,isLoading:S}=ct({queryKey:["project-timeline",d],queryFn:()=>pe.getProjectTimeline(d,20),enabled:d>0}),{data:T}=ct({queryKey:["settings"],queryFn:pe.getSettings}),{data:E}=ct({queryKey:["project-folders",d],queryFn:()=>pe.getLibraryFoldersByProject(d),enabled:d>0}),k=T?.currency||"$",A=T?.time_format||"system",D=ft({mutationFn:Q=>pe.updateProject(d,Q),onSuccess:()=>{n.invalidateQueries({queryKey:["project",d]}),n.invalidateQueries({queryKey:["projects"]}),a(!1),l(!1),r("Project updated","success")},onError:Q=>{r(Q.message,"error")}}),C=()=>{u(f?.notes||""),l(!0)},P=()=>{D.mutate({notes:c})},F=()=>{l(!1),u("")},[L,X]=w.useState(""),[R,q]=w.useState(1),[Y,W]=w.useState(""),[Z,ie]=w.useState(""),[ae,U]=w.useState(""),[G,ee]=w.useState(!1),[re,le]=w.useState(!1),[he,fe]=w.useState({isOpen:!1,title:"",message:"",onConfirm:()=>{}}),Te=ft({mutationFn:Q=>pe.createBOMItem(d,Q),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",d]}),n.invalidateQueries({queryKey:["project",d]}),X(""),q(1),W(""),ie(""),U(""),ee(!1),r("Part added","success")},onError:Q=>r(Q.message,"error")}),ge=ft({mutationFn:({itemId:Q,data:Ne})=>pe.updateBOMItem(d,Q,Ne),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",d]}),n.invalidateQueries({queryKey:["project",d]})},onError:Q=>r(Q.message,"error")}),_e=ft({mutationFn:Q=>pe.deleteBOMItem(d,Q),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",d]}),n.invalidateQueries({queryKey:["project",d]}),r("Part removed","success")},onError:Q=>r(Q.message,"error")}),je=Q=>{Q.preventDefault(),L.trim()&&Te.mutate({name:L.trim(),quantity_needed:R,unit_price:Y?parseFloat(Y):void 0,sourcing_url:Z.trim()||void 0,remarks:ae.trim()||void 0})},ne=Q=>{const Ne=Q.is_complete?0:Q.quantity_needed;ge.mutate({itemId:Q.id,data:{quantity_acquired:Ne}})},Qe=(Q,Ne)=>{fe({isOpen:!0,title:"Delete Part",message:`Are you sure you want to delete "${Ne}"?`,onConfirm:()=>{fe(ke=>({...ke,isOpen:!1})),_e.mutate(Q)}})},B=ft({mutationFn:()=>pe.createTemplateFromProject(d),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),r("Template created","success")},onError:Q=>r(Q.message,"error")}),H=Q=>eg(Q,A,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"});if(m)return s.jsx("div",{className:"flex items-center justify-center py-24",children:s.jsx(ht,{className:"w-8 h-8 animate-spin text-bambu-green"})});if(g||!f)return s.jsxs("div",{className:"text-center py-24",children:[s.jsx("p",{className:"text-bambu-gray",children:g?`Error: ${g.message}`:"Project not found"}),s.jsx(Se,{variant:"secondary",className:"mt-4",onClick:()=>e("/projects"),children:"Back to Projects"})]});const K=f.stats,z=K?.progress_percent??0,V=K?.parts_progress_percent??0;return s.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[s.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[s.jsx(cs,{to:"/projects",className:"hover:text-white transition-colors",children:"Projects"}),s.jsx(Ws,{className:"w-4 h-4"}),s.jsx("span",{className:"text-white",children:f.name})]}),s.jsxs("div",{className:"flex items-start justify-between",children:[s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx("button",{onClick:()=>e("/projects"),className:"p-2 rounded-lg bg-bambu-card hover:bg-bambu-dark-tertiary transition-colors",children:s.jsx(BW,{className:"w-5 h-5 text-bambu-gray"})}),s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-4 h-4 rounded-full flex-shrink-0",style:{backgroundColor:f.color||"#6b7280"}}),s.jsxs("div",{children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:f.name}),f.description&&s.jsx("p",{className:"text-bambu-gray mt-1",children:f.description})]})]}),s.jsx(TKe,{status:f.status})]}),s.jsxs(Se,{onClick:()=>a(!0),children:[s.jsx(wf,{className:"w-4 h-4 mr-2"}),"Edit"]})]}),(f.target_count||f.target_parts_count)&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4 space-y-4",children:[f.target_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Plates Progress"}),s.jsxs("span",{className:"text-sm font-medium text-white",children:[K?.total_archives||0," / ",f.target_count," print jobs"]})]}),s.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(z,100)}%`,backgroundColor:z>=100?"#22c55e":f.color||"#6b7280"}})}),s.jsxs("div",{className:"flex justify-between mt-1",children:[s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[z.toFixed(0),"% complete"]}),K?.remaining_prints!=null&&K.remaining_prints>0&&s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[K.remaining_prints," remaining"]})]})]}),f.target_parts_count&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Parts Progress"}),s.jsxs("span",{className:"text-sm font-medium text-white",children:[K?.completed_prints||0," / ",f.target_parts_count," parts"]})]}),s.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(V,100)}%`,backgroundColor:V>=100?"#22c55e":f.color||"#6b7280"}})}),s.jsxs("div",{className:"flex justify-between mt-1",children:[s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[V.toFixed(0),"% complete"]}),K?.remaining_parts!=null&&K.remaining_parts>0&&s.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[K.remaining_parts," remaining"]})]})]})]})}),K&&s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsx(Mt,{children:s.jsx(Tt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:s.jsx(Ll,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Print Jobs"}),s.jsxs("p",{className:"text-xl font-semibold text-white",children:[K.total_archives," ",s.jsx("span",{className:"text-sm font-normal text-bambu-gray",children:"total"})]}),K.failed_prints>0&&s.jsxs("p",{className:"text-sm text-status-error",children:[K.failed_prints," failed"]}),s.jsxs("p",{className:"text-sm text-bambu-gray",children:[K.completed_prints," parts printed"]})]})]})})}),s.jsx(KG,{icon:Yn,label:"Print Time",value:NKe(K.total_print_time_hours),color:"text-yellow-400"}),s.jsx(KG,{icon:zr,label:"Filament Used",value:EKe(K.total_filament_grams),color:"text-purple-400"})]}),K&&(K.estimated_cost>0||f.budget)&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsx("h2",{className:"text-lg font-semibold text-white mb-3",children:"Cost Tracking"}),s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Filament Cost"}),s.jsxs("p",{className:"text-lg font-semibold text-white",children:[k,K.estimated_cost.toFixed(2)]})]}),K.total_energy_kwh>0&&s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Energy"}),s.jsxs("p",{className:"text-lg font-semibold text-white",children:[K.total_energy_kwh.toFixed(2)," kWh",K.total_energy_cost>0&&s.jsxs("span",{className:"text-sm text-bambu-gray ml-1",children:["(",k,K.total_energy_cost.toFixed(2),")"]})]})]}),f.budget&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Budget"}),s.jsxs("p",{className:"text-lg font-semibold text-white",children:[k,f.budget.toFixed(2)]})]}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Remaining"}),s.jsxs("p",{className:`text-lg font-semibold ${f.budget-K.estimated_cost>=0?"text-bambu-green":"text-red-400"}`,children:[k,(f.budget-K.estimated_cost).toFixed(2)]})]})]})]})]})}),f.children&&f.children.length>0&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-3",children:[s.jsx(wue,{className:"w-5 h-5"}),"Sub-projects (",f.children.length,")"]}),s.jsx("div",{className:"space-y-2",children:f.children.map(Q=>s.jsxs(cs,{to:`/projects/${Q.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:Q.color||"#6b7280"}}),s.jsx("span",{className:"text-white",children:Q.name}),s.jsx("span",{className:`text-xs px-2 py-0.5 rounded ${Q.status==="completed"?"bg-status-ok/20 text-status-ok":Q.status==="archived"?"bg-bambu-gray/20 text-bambu-gray":"bg-blue-500/20 text-blue-400"}`,children:Q.status})]}),Q.progress_percent!==null&&s.jsxs("span",{className:"text-sm text-bambu-gray",children:[Q.progress_percent.toFixed(0),"%"]})]},Q.id))})]})}),f.parent_id&&f.parent_name&&s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(nl,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("span",{className:"text-bambu-gray",children:"Part of:"}),s.jsx(cs,{to:`/projects/${f.parent_id}`,className:"text-bambu-green hover:underline",children:f.parent_name})]}),(f.tags||f.due_date||f.priority!=="normal")&&s.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[f.priority&&f.priority!=="normal"&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-xs text-bambu-gray uppercase",children:"Priority:"}),s.jsx(CKe,{priority:f.priority})]}),f.due_date&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zr,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("span",{className:"text-sm text-white",children:kKe(f.due_date)}),vj(f.due_date)&&s.jsxs("span",{className:`text-xs ${vj(f.due_date).color}`,children:["(",vj(f.due_date).label,")"]})]}),f.tags&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zp,{className:"w-4 h-4 text-bambu-gray"}),s.jsx("div",{className:"flex flex-wrap gap-1",children:f.tags.split(",").map((Q,Ne)=>s.jsx("span",{className:"px-2 py-0.5 bg-bambu-dark-tertiary text-bambu-gray text-xs rounded",children:Q.trim()},Ne))})]})]}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ba,{className:"w-5 h-5"}),"Notes"]}),o?s.jsxs("div",{className:"flex gap-2",children:[s.jsxs(Se,{variant:"secondary",size:"sm",onClick:F,disabled:D.isPending,children:[s.jsx(Bt,{className:"w-4 h-4 mr-1"}),"Cancel"]}),s.jsxs(Se,{size:"sm",onClick:P,disabled:D.isPending,children:[D.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin mr-1"}):s.jsx(hs,{className:"w-4 h-4 mr-1"}),"Save"]})]}):s.jsxs(Se,{variant:"secondary",size:"sm",onClick:C,children:[s.jsx(wf,{className:"w-4 h-4 mr-1"}),"Edit"]})]}),o?s.jsx(kR,{content:c,onChange:u,placeholder:"Add notes about this project..."}):f.notes?s.jsx("div",{className:"prose prose-invert prose-sm max-w-none",dangerouslySetInnerHTML:{__html:f.notes}}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No notes yet. Click Edit to add notes."})]})}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsx("div",{className:"flex items-center justify-between mb-3",children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(el,{className:"w-5 h-5"}),"Files"]})}),s.jsxs("p",{className:"text-xs text-bambu-gray mb-3",children:[s.jsx(cs,{to:"/files",className:"text-bambu-green hover:underline",children:"Link folders from the File Manager"})," ","to this project for quick access."]}),E&&E.length>0?s.jsx("div",{className:"space-y-2",children:E.map(Q=>s.jsxs(cs,{to:`/files?folder=${Q.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[s.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[s.jsx(el,{className:"w-5 h-5 text-bambu-green flex-shrink-0"}),s.jsxs("div",{className:"min-w-0",children:[s.jsx("p",{className:"text-sm text-white truncate",children:Q.name}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[Q.file_count," file",Q.file_count!==1?"s":""]})]})]}),s.jsx(Ws,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"})]},Q.id))}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No folders linked. Go to File Manager and link a folder to this project."})]})}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(eq,{className:"w-5 h-5"}),"Bill of Materials",K&&K.bom_total_items>0&&s.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",K.bom_completed_items,"/",K.bom_total_items," acquired)"]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[b&&b.some(Q=>Q.is_complete)&&s.jsx("button",{onClick:()=>le(!re),className:`text-xs px-2 py-1 rounded transition-colors ${re?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:re?"Show all":"Hide done"}),!G&&s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>ee(!0),children:[s.jsx(Jn,{className:"w-4 h-4 mr-1"}),"Add Part"]})]})]}),G&&s.jsxs("form",{onSubmit:je,className:"bg-bambu-dark rounded-lg p-4 mb-4 space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[s.jsx("input",{type:"text",value:L,onChange:Q=>X(Q.target.value),className:"bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"Part name (e.g., M3x8 screws)",autoFocus:!0}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx("input",{type:"number",value:R,onChange:Q=>q(parseInt(Q.target.value)||1),className:"w-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white focus:outline-none focus:border-bambu-green",min:"1",placeholder:"Qty"}),s.jsx("input",{type:"number",step:"0.01",value:Y,onChange:Q=>W(Q.target.value),className:"flex-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:`Price (${k})`})]})]}),s.jsx("input",{type:"url",value:Z,onChange:Q=>ie(Q.target.value),className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"Sourcing URL (optional)"}),s.jsx("input",{type:"text",value:ae,onChange:Q=>U(Q.target.value),className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded px-3 py-2 text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"Remarks (optional)"}),s.jsxs("div",{className:"flex justify-end gap-2",children:[s.jsx(Se,{type:"button",variant:"secondary",size:"sm",onClick:()=>ee(!1),children:"Cancel"}),s.jsx(Se,{type:"submit",size:"sm",disabled:!L.trim()||Te.isPending,children:Te.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Add Part"})]})]}),v?s.jsx("div",{className:"flex items-center justify-center py-4",children:s.jsx(ht,{className:"w-6 h-6 animate-spin text-bambu-green"})}):b&&b.length>0?s.jsxs("div",{className:"space-y-2",children:[b.filter(Q=>!re||!Q.is_complete).map(Q=>s.jsx("div",{className:`p-3 rounded-lg transition-colors ${Q.is_complete?"bg-status-ok/10":"bg-bambu-dark"}`,children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("button",{onClick:()=>ne(Q),disabled:ge.isPending,className:`w-5 h-5 mt-0.5 rounded border-2 flex items-center justify-center transition-colors flex-shrink-0 ${Q.is_complete?"bg-status-ok border-status-ok text-white":"border-bambu-gray hover:border-bambu-green"}`,children:Q.is_complete&&s.jsx(On,{className:"w-3 h-3"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center justify-between gap-2",children:[s.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[s.jsxs("p",{className:`text-sm font-medium ${Q.is_complete?"text-bambu-gray line-through":"text-white"}`,children:[Q.name,s.jsxs("span",{className:"text-bambu-gray font-normal ml-2",children:["x",Q.quantity_needed]})]}),Q.unit_price!==null&&s.jsxs("span",{className:"text-xs text-bambu-green whitespace-nowrap",children:[k,(Q.unit_price*Q.quantity_needed).toFixed(2)]})]}),s.jsx("button",{onClick:()=>Qe(Q.id,Q.name),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-red-400 transition-colors flex-shrink-0",title:"Delete",children:s.jsx(ln,{className:"w-4 h-4"})})]}),Q.sourcing_url&&s.jsxs("a",{href:Q.sourcing_url,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 mt-1 text-xs text-blue-400 hover:text-blue-300 transition-colors",onClick:Ne=>Ne.stopPropagation(),children:[s.jsx(Ur,{className:"w-3 h-3 flex-shrink-0"}),s.jsx("span",{className:"truncate",children:(()=>{try{return new URL(Q.sourcing_url).hostname.replace("www.","")}catch{return Q.sourcing_url}})()})]}),Q.remarks&&s.jsx("p",{className:"mt-1 text-xs text-bambu-gray/80 italic",children:Q.remarks})]})]})},Q.id)),b.some(Q=>Q.unit_price!==null)&&s.jsxs("div",{className:"pt-2 mt-2 border-t border-bambu-dark-tertiary flex justify-between text-sm",children:[s.jsx("span",{className:"text-bambu-gray",children:"Total cost:"}),s.jsxs("span",{className:"text-white font-medium",children:[k,b.reduce((Q,Ne)=>Q+(Ne.unit_price||0)*Ne.quantity_needed,0).toFixed(2)]})]})]}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No parts in the bill of materials. Add hardware, electronics, or other components to track what needs to be sourced."})]})}),s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsx("div",{className:"flex items-center justify-between mb-3",children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(hw,{className:"w-5 h-5"}),"Activity Timeline"]})}),S?s.jsx("div",{className:"flex items-center justify-center py-4",children:s.jsx(ht,{className:"w-6 h-6 animate-spin text-bambu-green"})}):M&&M.length>0?s.jsx("div",{className:"space-y-3",children:M.slice(0,10).map((Q,Ne)=>s.jsxs("div",{className:"flex gap-3",children:[s.jsxs("div",{className:`w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 ${Q.event_type==="print_completed"?"bg-status-ok/20 text-status-ok":Q.event_type==="print_failed"?"bg-status-error/20 text-status-error":Q.event_type==="print_started"?"bg-yellow-500/20 text-yellow-400":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[Q.event_type==="print_completed"&&s.jsx(On,{className:"w-4 h-4"}),Q.event_type==="print_failed"&&s.jsx(Ti,{className:"w-4 h-4"}),Q.event_type==="print_started"&&s.jsx(zr,{className:"w-4 h-4"}),Q.event_type==="queued"&&s.jsx(Bj,{className:"w-4 h-4"}),Q.event_type==="project_created"&&s.jsx(Jn,{className:"w-4 h-4"})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("p",{className:"text-sm text-white",children:Q.title}),Q.description&&s.jsx("p",{className:"text-xs text-bambu-gray truncate",children:Q.description}),s.jsx("p",{className:"text-xs text-bambu-gray/70",children:H(Q.timestamp)})]})]},Ne))}):s.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No activity yet."})]})}),!f.is_template&&s.jsx("div",{className:"flex justify-end",children:s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>B.mutate(),disabled:B.isPending,children:[B.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin mr-2"}):s.jsx(Jo,{className:"w-4 h-4 mr-2"}),"Save as Template"]})}),K&&(K.queued_prints>0||K.in_progress_prints>0)&&s.jsx(Mt,{children:s.jsxs(Tt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Bj,{className:"w-5 h-5"}),"Queue"]}),s.jsx(cs,{to:`/queue?project=${d}`,className:"text-sm text-bambu-green hover:underline",children:"View all"})]}),s.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[K.in_progress_prints>0&&s.jsxs("span",{className:"text-yellow-400",children:[K.in_progress_prints," printing"]}),K.queued_prints>0&&s.jsxs("span",{className:"text-bambu-gray",children:[K.queued_prints," queued"]})]})]})}),s.jsxs("div",{children:[s.jsx("div",{className:"flex items-center justify-between mb-4",children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ll,{className:"w-5 h-5"}),"Prints (",x?.length||0,")"]})}),y?s.jsx("div",{className:"flex items-center justify-center py-8",children:s.jsx(ht,{className:"w-6 h-6 animate-spin text-bambu-green"})}):s.jsx(AKe,{archives:x||[]})]}),i&&s.jsx(gre,{project:{...f,archive_count:K?.total_archives||0,total_items:K?.total_items||0,completed_count:K?.completed_prints||0,failed_count:K?.failed_prints||0,queue_count:K?.queued_prints||0,progress_percent:K?.progress_percent||null,archives:[]},onClose:()=>a(!1),onSave:Q=>D.mutate(Q),isLoading:D.isPending}),he.isOpen&&s.jsx(Un,{title:he.title,message:he.message,confirmText:"Delete",variant:"danger",onConfirm:he.onConfirm,onCancel:()=>fe(Q=>({...Q,isOpen:!1}))})]})}function Ep(t){return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(1)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`}function PKe(t){if(!t)return"-";const e=Math.floor(t/3600),n=Math.floor(t%3600/60);return e>0?`${e}h ${n}m`:`${n}m`}function RKe({parentId:t,onClose:e,onSave:n,isLoading:r}){const[i,a]=w.useState(""),o=l=>{l.preventDefault(),n({name:i.trim(),parent_id:t})};return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"New Folder"})}),s.jsxs("form",{onSubmit:o,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Folder Name"}),s.jsx("input",{type:"text",value:i,onChange:l=>a(l.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:"e.g., Functional Parts",autoFocus:!0,required:!0})]}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!i.trim()||r,children:r?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Create"})]})]})]})})}function DKe({type:t,currentName:e,onClose:n,onSave:r,isLoading:i}){const[a,o]=w.useState(e),l=c=>{c.preventDefault(),a.trim()&&a.trim()!==e&&r(a.trim())};return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Rename ",t==="file"?"File":"Folder"]})}),s.jsxs("form",{onSubmit:l,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Name"}),s.jsx("input",{type:"text",value:a,onChange:c=>o(c.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",autoFocus:!0,required:!0})]}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:n,children:"Cancel"}),s.jsx(Se,{type:"submit",disabled:!a.trim()||a.trim()===e||i,children:i?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Rename"})]})]})]})})}function OKe({folders:t,selectedFiles:e,currentFolderId:n,onClose:r,onMove:i,isLoading:a}){const[o,l]=w.useState(null),c=(d,f=0)=>{const m=[];for(const g of d)m.push({id:g.id,name:g.name,depth:f}),g.children.length>0&&m.push(...c(g.children,f+1));return m},u=[{id:null,name:"Root (No Folder)",depth:0},...c(t)];return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[s.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Move ",e.length," File(s)"]})}),s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1",children:u.map(d=>s.jsxs("button",{onClick:()=>l(d.id),disabled:d.id===n,className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${o===d.id?"bg-bambu-green/20 text-bambu-green":d.id===n?"opacity-50 cursor-not-allowed text-bambu-gray":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${12+d.depth*16}px`},children:[s.jsx(el,{className:"w-4 h-4"}),d.name,d.id===n&&s.jsx("span",{className:"text-xs text-bambu-gray ml-auto",children:"(current)"})]},d.id??"root"))}),s.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:r,children:"Cancel"}),s.jsx(Se,{onClick:()=>i(o),disabled:a,children:a?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Move"})]})]})]})})}function LKe({folder:t,onClose:e,onLink:n,isLoading:r}){const[i,a]=w.useState("project"),[o,l]=w.useState(t.project_id||t.archive_id||null);w.useState(()=>{t.archive_id&&a("archive")});const{data:c}=ct({queryKey:["projects"],queryFn:()=>pe.getProjects()}),{data:u}=ct({queryKey:["archives-for-link"],queryFn:()=>pe.getArchives(void 0,void 0,100)}),d=()=>{n(i==="project"?{project_id:o,archive_id:0}:{project_id:0,archive_id:o})},f=()=>{n({project_id:0,archive_id:0})},m=t.project_id||t.archive_id;return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Pl,{className:"w-5 h-5 text-bambu-green"}),"Link Folder"]}),s.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:s.jsx(Bt,{className:"w-5 h-5 text-bambu-gray"})})]}),s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsxs("p",{className:"text-sm text-bambu-gray",children:['Link "',s.jsx("span",{className:"text-white",children:t.name}),'" to a project or archive for quick access.']}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("button",{onClick:()=>{a("project"),l(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${i==="project"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[s.jsx(HW,{className:"w-4 h-4"}),"Project"]}),s.jsxs("button",{onClick:()=>{a("archive"),l(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${i==="archive"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[s.jsx(Va,{className:"w-4 h-4"}),"Archive"]})]}),s.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1 bg-bambu-dark rounded-lg p-2",children:i==="project"?c&&c.length>0?c.map(g=>s.jsxs("button",{onClick:()=>l(g.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${o===g.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[s.jsx("div",{className:"w-3 h-3 rounded-full flex-shrink-0",style:{backgroundColor:g.color||"#00ae42"}}),s.jsx("span",{className:"truncate",children:g.name})]},g.id)):s.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:"No projects found"}):u&&u.length>0?u.map(g=>s.jsxs("button",{onClick:()=>l(g.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${o===g.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[s.jsx(nf,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),s.jsx("span",{className:"truncate",children:g.print_name||g.filename})]},g.id)):s.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:"No archives found"})})]}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-between",children:[m&&s.jsxs(Se,{variant:"danger",onClick:f,disabled:r,children:[s.jsx(aD,{className:"w-4 h-4 mr-2"}),"Unlink"]}),s.jsxs("div",{className:`flex gap-2 ${m?"":"ml-auto"}`,children:[s.jsx(Se,{variant:"secondary",onClick:e,children:"Cancel"}),s.jsx(Se,{onClick:d,disabled:!o||r,children:r?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):"Link"})]})]})]})})}function IKe({folderId:t,onClose:e,onUploadComplete:n}){const[r,i]=w.useState([]),[a,o]=w.useState(!1),[l,c]=w.useState(!1),[u,d]=w.useState(!0),[f,m]=w.useState(!1),g=w.useRef(null),x=P=>{P.preventDefault(),o(!0)},y=P=>{P.preventDefault(),o(!1)},b=P=>{P.preventDefault(),o(!1);const F=Array.from(P.dataTransfer.files);M(F)},v=P=>{P.target.files&&M(Array.from(P.target.files))},M=P=>{const F=P.map(L=>({file:L,status:"pending",isZip:L.name.toLowerCase().endsWith(".zip")}));i(L=>[...L,...F])},S=P=>{i(F=>F.filter((L,X)=>X!==P))},T=r.some(P=>P.isZip&&P.status==="pending"),E=async()=>{if(r.length!==0){c(!0);for(let P=0;PF.map((L,X)=>X===P?{...L,status:"uploading"}:L));try{if(r[P].isZip){const F=await pe.extractZipFile(r[P].file,t,u,f);i(L=>L.map((X,R)=>R===P?{...X,status:F.errors.length>0&&F.extracted===0?"error":"success",extractedCount:F.extracted,error:F.errors.length>0?`${F.errors.length} files failed`:void 0}:X))}else await pe.uploadLibraryFile(r[P].file,t),i(F=>F.map((L,X)=>X===P?{...L,status:"success"}:L))}catch(F){i(L=>L.map((X,R)=>R===P?{...X,status:"error",error:F instanceof Error?F.message:"Upload failed"}:X))}}c(!1),n(),e()}},k=r.filter(P=>P.status==="pending").length,A=r.filter(P=>P.status==="success").length,D=r.filter(P=>P.status==="error").length,C=r.length>0&&k===0&&!l;return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Upload Files"}),s.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:s.jsx(Bt,{className:"w-5 h-5 text-bambu-gray"})})]}),s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsxs("div",{onDragOver:x,onDragLeave:y,onDrop:b,onClick:()=>g.current?.click(),className:`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors ${a?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,children:[s.jsx(yi,{className:`w-10 h-10 mx-auto mb-3 ${a?"text-bambu-green":"text-bambu-gray"}`}),s.jsx("p",{className:"text-white font-medium",children:a?"Drop files here":"Drag & drop files here"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"or click to browse"}),s.jsx("p",{className:"text-xs text-bambu-gray/70 mt-2",children:"All file types supported. ZIP files will be extracted."})]}),s.jsx("input",{ref:g,type:"file",multiple:!0,className:"hidden",onChange:v}),T&&s.jsx("div",{className:"p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(Va,{className:"w-5 h-5 text-blue-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"flex-1",children:[s.jsx("p",{className:"text-sm text-blue-300 font-medium",children:"ZIP files detected"}),s.jsx("p",{className:"text-xs text-blue-300/70 mt-1",children:"ZIP files will be extracted. Choose how to handle folder structure:"}),s.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:u,onChange:P=>d(P.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsx("span",{className:"text-sm text-white",children:"Preserve folder structure from ZIP"})]}),s.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:f,onChange:P=>m(P.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsx("span",{className:"text-sm text-white",children:"Create folder from ZIP filename"})]})]})]})}),r.length>0&&s.jsx("div",{className:"max-h-48 overflow-y-auto space-y-2",children:r.map((P,F)=>s.jsxs("div",{className:"flex items-center gap-3 p-2 bg-bambu-dark rounded-lg",children:[P.isZip?s.jsx(Va,{className:"w-4 h-4 text-blue-400 flex-shrink-0"}):s.jsx(C2,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("p",{className:"text-sm text-white truncate",children:P.file.name}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[(P.file.size/1024/1024).toFixed(2)," MB",P.isZip&&P.status==="pending"&&s.jsx("span",{className:"text-blue-400 ml-2",children:"• Will be extracted"}),P.extractedCount!==void 0&&s.jsxs("span",{className:"text-green-400 ml-2",children:["• ",P.extractedCount," files extracted"]})]})]}),P.status==="pending"&&s.jsx("button",{onClick:()=>S(F),className:"p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Bt,{className:"w-4 h-4 text-bambu-gray"})}),P.status==="uploading"&&s.jsx(ht,{className:"w-4 h-4 text-bambu-green animate-spin"}),P.status==="success"&&s.jsx(On,{className:"w-4 h-4 text-green-500"}),P.status==="error"&&s.jsx("span",{title:P.error,children:s.jsx(Ti,{className:"w-4 h-4 text-red-500"})})]},F))}),C&&s.jsx("div",{className:"p-3 bg-bambu-dark rounded-lg",children:s.jsxs("p",{className:"text-sm text-white",children:["Upload complete: ",A," succeeded",D>0&&s.jsxs("span",{className:"text-red-400",children:[", ",D," failed"]})]})})]}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-end gap-2",children:[s.jsx(Se,{variant:"secondary",onClick:e,children:C?"Close":"Cancel"}),!C&&s.jsx(Se,{onClick:E,disabled:k===0||l,children:l?s.jsxs(s.Fragment,{children:[s.jsx(ht,{className:"w-4 h-4 mr-2 animate-spin"}),"Uploading..."]}):s.jsxs(s.Fragment,{children:[s.jsx(yi,{className:"w-4 h-4 mr-2"}),"Upload ",k>0?`(${k})`:""]})})]})]})})}function xre({folder:t,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:a,depth:o=0}){const[l,c]=w.useState(!0),[u,d]=w.useState(!1),f=t.children.length>0,m=t.project_id||t.archive_id;return s.jsxs("div",{children:[s.jsxs("div",{className:`group flex items-center gap-1 px-2 py-1.5 rounded cursor-pointer transition-colors ${e===t.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${8+o*12}px`},onClick:()=>n(t.id),children:[f?s.jsx("button",{onClick:g=>{g.stopPropagation(),c(!l)},className:"p-0.5 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Ws,{className:`w-3.5 h-3.5 transition-transform ${l?"rotate-90":""}`})}):s.jsx("div",{className:"w-4.5"}),s.jsx(el,{className:"w-4 h-4 text-bambu-green flex-shrink-0"}),s.jsx("span",{className:"text-sm truncate flex-1",children:t.name}),m&&s.jsxs("button",{onClick:g=>{g.stopPropagation(),i(t)},className:"flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-blue-500/20 text-blue-400 hover:bg-blue-500/30 transition-colors",title:`${t.project_name?`Project: ${t.project_name}`:`Archive: ${t.archive_name}`} (click to change)`,children:[s.jsx(Pl,{className:"w-3 h-3"}),t.project_name?s.jsx(HW,{className:"w-3 h-3"}):s.jsx(Va,{className:"w-3 h-3"})]}),t.file_count>0&&s.jsx("span",{className:"text-xs text-bambu-gray",children:t.file_count}),!m&&s.jsx("button",{onClick:g=>{g.stopPropagation(),i(t)},className:"p-1 rounded hover:bg-bambu-dark-tertiary",title:"Link to project or archive",children:s.jsx(Pl,{className:"w-3.5 h-3.5 text-bambu-gray hover:text-bambu-green"})}),s.jsx("div",{className:"flex items-center gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity",onClick:g=>g.stopPropagation(),children:s.jsxs("div",{className:"relative",children:[s.jsx("button",{onClick:()=>d(!u),className:"p-1 rounded hover:bg-bambu-dark-tertiary",children:s.jsx(wd,{className:"w-3.5 h-3.5 text-bambu-gray"})}),u&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>d(!1)}),s.jsxs("div",{className:"absolute right-0 top-full mt-1 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]",children:[s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{a(t),d(!1)},children:[s.jsx(Wa,{className:"w-3.5 h-3.5"}),"Rename"]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{i(t),d(!1)},children:[s.jsx(Pl,{className:"w-3.5 h-3.5"}),m?"Change Link...":"Link to..."]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-red-400 hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{r(t.id),d(!1)},children:[s.jsx(ln,{className:"w-3.5 h-3.5"}),"Delete"]})]})]})]})})]}),f&&l&&s.jsx("div",{children:t.children.map(g=>s.jsx(xre,{folder:g,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:a,depth:o+1},g.id))})]})}function FR(t){const e=t.toLowerCase();return e.endsWith(".gcode")||e.includes(".gcode.")}function FKe({file:t,isSelected:e,onSelect:n,onDelete:r,onDownload:i,onAddToQueue:a,onPrint:o,onRename:l}){const[c,u]=w.useState(!1);return s.jsxs("div",{className:`group relative bg-bambu-card rounded-lg border transition-all cursor-pointer overflow-hidden ${e?"border-bambu-green ring-1 ring-bambu-green":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,onClick:()=>n(t.id),children:[s.jsxs("div",{className:"aspect-square bg-bambu-dark flex items-center justify-center overflow-hidden",children:[t.thumbnail_path?s.jsx("img",{src:pe.getLibraryFileThumbnailUrl(t.id),alt:t.filename,className:"w-full h-full object-cover"}):s.jsx(nf,{className:"w-12 h-12 text-bambu-gray/30"}),s.jsx("div",{className:`absolute top-2 right-2 text-xs px-1.5 py-0.5 rounded font-medium ${t.file_type==="3mf"?"bg-bambu-green/90 text-white":t.file_type==="gcode"?"bg-blue-500/90 text-white":t.file_type==="stl"?"bg-purple-500/90 text-white":"bg-bambu-gray/90 text-white"}`,children:t.file_type.toUpperCase()})]}),s.jsxs("div",{className:"p-3",children:[s.jsx("h3",{className:"text-sm font-medium text-white truncate",title:t.print_name||t.filename,children:t.print_name||t.filename}),s.jsxs("div",{className:"flex items-center gap-3 mt-1 text-xs text-bambu-gray",children:[s.jsx("span",{children:Ep(t.file_size)}),t.print_time_seconds&&s.jsxs("span",{className:"flex items-center gap-1",children:[s.jsx(Yn,{className:"w-3 h-3"}),PKe(t.print_time_seconds)]})]}),t.print_count>0&&s.jsxs("div",{className:"mt-1 text-xs text-bambu-green",children:["Printed ",t.print_count,"x"]})]}),s.jsxs("div",{className:"absolute bottom-2 right-2 opacity-100 md:opacity-0 md:group-hover:opacity-100 transition-opacity",onClick:d=>d.stopPropagation(),children:[s.jsx("button",{onClick:()=>u(!c),className:"p-1.5 rounded bg-bambu-dark-secondary/90 hover:bg-bambu-dark-tertiary",children:s.jsx(wd,{className:"w-4 h-4 text-bambu-gray"})}),c&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>u(!1)}),s.jsxs("div",{className:"absolute right-0 bottom-8 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[140px]",children:[o&&FR(t.filename)&&s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-bambu-green hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{o(t),u(!1)},children:[s.jsx(zr,{className:"w-3.5 h-3.5"}),"Print"]}),a&&FR(t.filename)&&s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{a(t.id),u(!1)},children:[s.jsx(Yn,{className:"w-3.5 h-3.5"}),"Add to Queue"]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{i(t.id),u(!1)},children:[s.jsx(ei,{className:"w-3.5 h-3.5"}),"Download"]}),l&&s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-white hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{l(t),u(!1)},children:[s.jsx(Wa,{className:"w-3.5 h-3.5"}),"Rename"]}),s.jsxs("button",{className:"w-full px-3 py-1.5 text-left text-sm text-red-400 hover:bg-bambu-dark flex items-center gap-2",onClick:()=>{r(t.id),u(!1)},children:[s.jsx(ln,{className:"w-3.5 h-3.5"}),"Delete"]})]})]})]}),s.jsx("div",{className:`absolute top-2 left-2 w-5 h-5 rounded border-2 flex items-center justify-center transition-all ${e?"bg-bambu-green border-bambu-green":"border-white/30 bg-black/30 opacity-100 md:opacity-0 md:group-hover:opacity-100"}`,children:e&&s.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})})]})}function UKe(){const t=an(),{showToast:e}=Gn(),[n]=wW(),r=n.get("folder"),i=r?parseInt(r,10):null,[a,o]=w.useState(i),[l,c]=w.useState([]),[u,d]=w.useState(!1),[f,m]=w.useState(!1),[g,x]=w.useState(!1),[y,b]=w.useState(null),[v,M]=w.useState(null),[S,T]=w.useState(null),[E,k]=w.useState(null),[A,D]=w.useState(null),[C,P]=w.useState(()=>localStorage.getItem("library-view-mode")||"grid"),[F,L]=w.useState(""),[X,R]=w.useState("all"),[q,Y]=w.useState("date"),[W,Z]=w.useState("desc");w.useEffect(()=>{const oe=n.get("folder");if(oe){const Ae=parseInt(oe,10);o(Ae)}},[n]);const{data:ie}=ct({queryKey:["settings"],queryFn:()=>pe.getSettings()}),{data:ae,isLoading:U}=ct({queryKey:["library-folders"],queryFn:()=>pe.getLibraryFolders()}),{data:G,isLoading:ee}=ct({queryKey:["library-files",a],queryFn:()=>pe.getLibraryFiles(a,a===null)}),{data:re}=ct({queryKey:["library-stats"],queryFn:()=>pe.getLibraryStats()}),le=w.useMemo(()=>{if(!G)return[];const oe=new Set(G.map(Ae=>Ae.file_type));return Array.from(oe).sort()},[G]),he=w.useMemo(()=>{if(!G)return[];let oe=[...G];if(F.trim()){const Ae=F.toLowerCase();oe=oe.filter(De=>De.filename.toLowerCase().includes(Ae)||De.print_name&&De.print_name.toLowerCase().includes(Ae))}return X!=="all"&&(oe=oe.filter(Ae=>Ae.file_type===X)),oe.sort((Ae,De)=>{let Ze=0;switch(q){case"name":Ze=(Ae.print_name||Ae.filename).localeCompare(De.print_name||De.filename);break;case"date":Ze=new Date(Ae.created_at).getTime()-new Date(De.created_at).getTime();break;case"size":Ze=Ae.file_size-De.file_size;break;case"type":Ze=Ae.file_type.localeCompare(De.file_type);break;case"prints":Ze=Ae.print_count-De.print_count;break}return W==="asc"?Ze:-Ze}),oe},[G,F,X,q,W]),fe=w.useMemo(()=>{if(!re||!ie)return!1;const oe=(ie.library_disk_warning_gb||5)*1024*1024*1024;return re.disk_free_bytespe.createLibraryFolder(oe),onSuccess:()=>{t.invalidateQueries({queryKey:["library-folders"]}),d(!1),e("Folder created","success")},onError:oe=>e(oe.message,"error")}),ge=ft({mutationFn:oe=>pe.deleteLibraryFolder(oe),onSuccess:()=>{t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-stats"]}),a===v?.id&&o(null),M(null),e("Folder deleted","success")},onError:oe=>{M(null),e(oe.message,"error")}}),_e=ft({mutationFn:oe=>pe.deleteLibraryFile(oe),onSuccess:()=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-stats"]}),c(oe=>oe.filter(Ae=>Ae!==v?.id)),M(null),e("File deleted","success")},onError:oe=>{M(null),e(oe.message,"error")}}),je=ft({mutationFn:oe=>pe.bulkDeleteLibrary(oe,[]),onSuccess:(oe,Ae)=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-stats"]}),e(`Deleted ${Ae.length} files`,"success"),c([]),M(null)},onError:oe=>{M(null),e(oe.message,"error")}}),ne=ft({mutationFn:({fileIds:oe,folderId:Ae})=>pe.moveLibraryFiles(oe,Ae),onSuccess:()=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),c([]),m(!1),e("Files moved","success")},onError:oe=>e(oe.message,"error")}),Qe=ft({mutationFn:({id:oe,data:Ae})=>pe.updateLibraryFolder(oe,Ae),onSuccess:(oe,Ae)=>{t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["project-folders"]}),t.invalidateQueries({queryKey:["archive-folders"]}),b(null);const De=Ae.data.project_id===0&&Ae.data.archive_id===0;e(De?"Folder unlinked":"Folder linked","success")},onError:oe=>e(oe.message,"error")}),B=ft({mutationFn:oe=>pe.addLibraryFilesToQueue(oe),onSuccess:oe=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["queue"]}),t.invalidateQueries({queryKey:["archives"]}),c([]),oe.added.length>0&&oe.errors.length===0?e(`Added ${oe.added.length} file${oe.added.length>1?"s":""} to queue`,"success"):oe.added.length>0&&oe.errors.length>0?e(`Added ${oe.added.length} file${oe.added.length>1?"s":""}, ${oe.errors.length} failed`,"success"):e(`Failed to add files: ${oe.errors[0]?.error||"Unknown error"}`,"error")},onError:oe=>e(oe.message,"error")}),H=ft({mutationFn:({id:oe,filename:Ae})=>pe.updateLibraryFile(oe,{filename:Ae}),onSuccess:()=>{t.invalidateQueries({queryKey:["library-files"]}),D(null),e("File renamed","success")},onError:oe=>{D(null),e(oe.message,"error")}}),K=ft({mutationFn:({id:oe,name:Ae})=>pe.updateLibraryFolder(oe,{name:Ae}),onSuccess:()=>{t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-files"]}),D(null),e("Folder renamed","success")},onError:oe=>{D(null),e(oe.message,"error")}}),z=w.useCallback(oe=>{const Ae=oe.toLowerCase();return Ae.endsWith(".gcode")||Ae.includes(".gcode.")},[]),V=w.useMemo(()=>G?G.filter(oe=>l.includes(oe.id)&&z(oe.filename)):[],[G,l,z]),Q=w.useCallback(oe=>{c(Ae=>Ae.includes(oe)?Ae.filter(De=>De!==oe):[...Ae,oe])},[]),Ne=w.useCallback(()=>{he.length>0&&c(he.map(oe=>oe.id))},[he]),ke=w.useCallback(()=>{c([])},[]),Me=()=>{t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["library-folders"]}),t.invalidateQueries({queryKey:["library-stats"]})},$=oe=>{window.open(pe.getLibraryFileDownloadUrl(oe),"_blank")},I=()=>{v&&(v.type==="file"?_e.mutate(v.id):v.type==="folder"?ge.mutate(v.id):v.type==="bulk"&&je.mutate(l))},J=ge.isPending||_e.isPending||je.isPending,xe=oe=>{P(oe),localStorage.setItem("library-view-mode",oe)},ve=U||ee;return s.jsxs("div",{className:"p-4 md:p-8 h-[calc(100vh-64px)] flex flex-col",children:[s.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6",children:[s.jsxs("div",{children:[s.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[s.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:s.jsx(el,{className:"w-6 h-6 text-bambu-green"})}),"File Manager"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:"Organize and manage your print files"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"flex items-center bg-bambu-dark rounded-lg p-1",children:[s.jsx("button",{onClick:()=>xe("grid"),className:`p-1.5 rounded transition-colors ${C==="grid"?"bg-bambu-card text-white":"text-bambu-gray hover:text-white"}`,title:"Grid view",children:s.jsx(D2,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>xe("list"),className:`p-1.5 rounded transition-colors ${C==="list"?"bg-bambu-card text-white":"text-bambu-gray hover:text-white"}`,title:"List view",children:s.jsx(O2,{className:"w-4 h-4"})})]}),s.jsxs(Se,{variant:"secondary",onClick:()=>d(!0),children:[s.jsx(vue,{className:"w-4 h-4 mr-2"}),"New Folder"]}),s.jsxs(Se,{onClick:()=>x(!0),children:[s.jsx(yi,{className:"w-4 h-4 mr-2"}),"Upload"]})]})]}),fe&&re&&ie&&s.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-amber-500/10 border border-amber-500/30 rounded-lg",children:[s.jsx(jn,{className:"w-5 h-5 text-amber-500 flex-shrink-0"}),s.jsxs("div",{className:"flex-1",children:[s.jsx("p",{className:"text-sm text-amber-500 font-medium",children:"Low disk space warning"}),s.jsxs("p",{className:"text-xs text-amber-500/80",children:["Only ",Ep(re.disk_free_bytes)," free of ",Ep(re.disk_total_bytes)," total. Threshold is set to ",ie.library_disk_warning_gb," GB in settings."]})]})]}),re&&s.jsxs("div",{className:"flex items-center gap-6 mb-6 p-3 bg-bambu-card rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(C2,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"text-bambu-gray",children:"Files:"}),s.jsx("span",{className:"text-white font-medium",children:re.total_files})]}),s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(el,{className:"w-4 h-4 text-blue-400"}),s.jsx("span",{className:"text-bambu-gray",children:"Folders:"}),s.jsx("span",{className:"text-white font-medium",children:re.total_folders})]}),s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(P2,{className:"w-4 h-4 text-amber-400"}),s.jsx("span",{className:"text-bambu-gray",children:"Size:"}),s.jsx("span",{className:"text-white font-medium",children:Ep(re.total_size_bytes)})]}),s.jsxs("div",{className:"flex items-center gap-2 text-sm ml-auto",children:[s.jsx("span",{className:"text-bambu-gray",children:"Free:"}),s.jsx("span",{className:`font-medium ${fe?"text-amber-500":"text-white"}`,children:Ep(re.disk_free_bytes)})]})]}),s.jsxs("div",{className:"flex-1 flex gap-6 min-h-0",children:[s.jsxs("div",{className:"w-64 flex-shrink-0 bg-bambu-card rounded-lg border border-bambu-dark-tertiary overflow-hidden flex flex-col",children:[s.jsx("div",{className:"p-3 border-b border-bambu-dark-tertiary",children:s.jsx("h2",{className:"text-sm font-medium text-white",children:"Folders"})}),s.jsxs("div",{className:"flex-1 overflow-y-auto p-2",children:[s.jsxs("div",{className:`flex items-center gap-2 px-2 py-1.5 rounded cursor-pointer transition-colors ${a===null?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,onClick:()=>o(null),children:[s.jsx(nf,{className:"w-4 h-4"}),s.jsx("span",{className:"text-sm",children:"All Files"})]}),ae?.map(oe=>s.jsx(xre,{folder:oe,selectedFolderId:a,onSelect:o,onDelete:Ae=>M({type:"folder",id:Ae}),onLink:b,onRename:Ae=>D({type:"folder",id:Ae.id,name:Ae.name})},oe.id))]})]}),s.jsxs("div",{className:"flex-1 flex flex-col min-w-0",children:[G&&G.length>0&&s.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-bambu-card rounded-lg border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"relative flex-1 max-w-xs",children:[s.jsx(ba,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",placeholder:"Search files...",value:F,onChange:oe=>L(oe.target.value),className:"w-full pl-9 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(k2,{className:"w-4 h-4 text-bambu-gray"}),s.jsxs("select",{value:X,onChange:oe=>R(oe.target.value),className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"all",children:"All types"}),le.map(oe=>s.jsx("option",{value:oe,children:oe.toUpperCase()},oe))]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("select",{value:q,onChange:oe=>Y(oe.target.value),className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[s.jsx("option",{value:"date",children:"Date"}),s.jsx("option",{value:"name",children:"Name"}),s.jsx("option",{value:"size",children:"Size"}),s.jsx("option",{value:"type",children:"Type"}),s.jsx("option",{value:"prints",children:"Prints"})]}),s.jsx("button",{onClick:()=>Z(oe=>oe==="asc"?"desc":"asc"),className:"p-1.5 rounded bg-bambu-dark border border-bambu-dark-tertiary hover:border-bambu-green transition-colors",title:W==="asc"?"Ascending":"Descending",children:W==="asc"?s.jsx(Zle,{className:"w-4 h-4 text-white"}):s.jsx(Wle,{className:"w-4 h-4 text-white"})})]}),(F||X!=="all")&&s.jsxs("span",{className:"text-sm text-bambu-gray",children:[he.length," of ",G.length," files"]})]}),he.length>0&&s.jsxs("div",{className:"flex items-center gap-2 mb-4 p-2 bg-bambu-card rounded-lg border border-bambu-dark-tertiary",children:[l.length===he.length&&l.length>0?s.jsxs(Se,{variant:"secondary",size:"sm",onClick:ke,children:[s.jsx(al,{className:"w-4 h-4 mr-1"}),"Deselect All"]}):s.jsxs(Se,{variant:"secondary",size:"sm",onClick:Ne,children:[s.jsx(il,{className:"w-4 h-4 mr-1"}),"Select All"]}),l.length>0&&s.jsxs(s.Fragment,{children:[s.jsxs("span",{className:"text-sm text-bambu-gray ml-2",children:[l.length," selected"]}),s.jsx("div",{className:"flex-1"}),V.length===1&&s.jsxs(Se,{variant:"primary",size:"sm",onClick:()=>k(V[0]),children:[s.jsx(rl,{className:"w-4 h-4 mr-1"}),"Print"]}),V.length>0&&s.jsxs(Se,{variant:V.length===1?"secondary":"primary",size:"sm",onClick:()=>B.mutate(V.map(oe=>oe.id)),disabled:B.isPending,children:[s.jsx(Yn,{className:"w-4 h-4 mr-1"}),B.isPending?"Adding...":`Add to Queue${V.lengthm(!0),children:[s.jsx(Ade,{className:"w-4 h-4 mr-1"}),"Move"]}),s.jsxs(Se,{variant:"danger",size:"sm",onClick:()=>{l.length===1?M({type:"file",id:l[0]}):M({type:"bulk",id:0,count:l.length})},children:[s.jsx(ln,{className:"w-4 h-4 mr-1"}),"Delete"]}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:ke,children:"Clear"})]})]}),ve?s.jsx("div",{className:"flex-1 flex items-center justify-center",children:s.jsxs("div",{className:"flex flex-col items-center gap-3",children:[s.jsx(ht,{className:"w-8 h-8 animate-spin text-bambu-green"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Loading files..."})]})}):G?.length===0?s.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[s.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:s.jsx(nf,{className:"w-12 h-12 text-bambu-gray/50"})}),s.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:a!==null?"Folder is empty":"No files yet"}),s.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:a!==null?"Upload files or move files into this folder to get started.":"Upload files to start organizing your print-related files."}),s.jsxs(Se,{onClick:()=>x(!0),children:[s.jsx(Jn,{className:"w-4 h-4 mr-2"}),"Upload Files"]})]}):he.length===0?s.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[s.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:s.jsx(ba,{className:"w-12 h-12 text-bambu-gray/50"})}),s.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:"No matching files"}),s.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:"No files match your current search or filter criteria."}),s.jsx(Se,{variant:"secondary",onClick:()=>{L(""),R("all")},children:"Clear filters"})]}):C==="grid"?s.jsx("div",{className:"flex-1 overflow-y-auto",children:s.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6 gap-4",children:he.map(oe=>s.jsx(FKe,{file:oe,isSelected:l.includes(oe.id),onSelect:Q,onDelete:Ae=>M({type:"file",id:Ae}),onDownload:$,onAddToQueue:Ae=>B.mutate([Ae]),onPrint:T,onRename:Ae=>D({type:"file",id:Ae.id,name:Ae.filename})},oe.id))})}):s.jsx("div",{className:"flex-1 overflow-y-auto",children:s.jsxs("div",{className:"bg-bambu-card rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[s.jsxs("div",{className:"grid grid-cols-[auto_1fr_100px_100px_100px_80px] gap-4 px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary text-xs text-bambu-gray font-medium",children:[s.jsx("div",{className:"w-6"}),s.jsx("div",{children:"Name"}),s.jsx("div",{children:"Type"}),s.jsx("div",{children:"Size"}),s.jsx("div",{children:"Prints"}),s.jsx("div",{})]}),he.map(oe=>s.jsxs("div",{className:`grid grid-cols-[auto_1fr_100px_100px_100px_80px] gap-4 px-4 py-3 items-center border-b border-bambu-dark-tertiary last:border-b-0 cursor-pointer hover:bg-bambu-dark/50 transition-colors ${l.includes(oe.id)?"bg-bambu-green/10":""}`,onClick:()=>Q(oe.id),children:[s.jsx("div",{className:`w-5 h-5 rounded border-2 flex items-center justify-center ${l.includes(oe.id)?"bg-bambu-green border-bambu-green":"border-bambu-gray/50"}`,children:l.includes(oe.id)&&s.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})}),s.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[s.jsxs("div",{className:"relative group/thumb",children:[s.jsx("div",{className:"w-10 h-10 rounded bg-bambu-dark flex-shrink-0 overflow-hidden",children:oe.thumbnail_path?s.jsx("img",{src:pe.getLibraryFileThumbnailUrl(oe.id),alt:"",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center",children:s.jsx(nf,{className:"w-5 h-5 text-bambu-gray/50"})})}),oe.thumbnail_path&&s.jsx("div",{className:"absolute left-0 top-full mt-2 z-50 hidden group-hover/thumb:block",children:s.jsx("div",{className:"w-48 h-48 rounded-lg bg-bambu-dark-secondary border border-bambu-dark-tertiary shadow-xl overflow-hidden",children:s.jsx("img",{src:pe.getLibraryFileThumbnailUrl(oe.id),alt:oe.filename,className:"w-full h-full object-contain"})})})]}),s.jsx("div",{className:"min-w-0",children:s.jsx("div",{className:"text-sm text-white truncate",children:oe.print_name||oe.filename})})]}),s.jsx("div",{children:s.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded font-medium ${oe.file_type==="3mf"?"bg-bambu-green/20 text-bambu-green":oe.file_type==="gcode"?"bg-blue-500/20 text-blue-400":oe.file_type==="stl"?"bg-purple-500/20 text-purple-400":"bg-bambu-gray/20 text-bambu-gray"}`,children:oe.file_type.toUpperCase()})}),s.jsx("div",{className:"text-sm text-bambu-gray",children:Ep(oe.file_size)}),s.jsx("div",{className:"text-sm text-bambu-gray",children:oe.print_count>0?`${oe.print_count}x`:"-"}),s.jsxs("div",{className:"flex items-center gap-1",onClick:Ae=>Ae.stopPropagation(),children:[FR(oe.filename)&&s.jsxs(s.Fragment,{children:[s.jsx("button",{onClick:()=>T(oe),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green transition-colors",title:"Print",children:s.jsx(zr,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>B.mutate([oe.id]),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",title:"Add to Queue",disabled:B.isPending,children:s.jsx(Yn,{className:"w-4 h-4"})})]}),s.jsx("button",{onClick:()=>$(oe.id),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",title:"Download",children:s.jsx(ei,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>D({type:"file",id:oe.id,name:oe.filename}),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",title:"Rename",children:s.jsx(Wa,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>M({type:"file",id:oe.id}),className:"p-1.5 rounded hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors",title:"Delete",children:s.jsx(ln,{className:"w-4 h-4"})})]})]},oe.id))]})})]})]}),u&&s.jsx(RKe,{parentId:a,onClose:()=>d(!1),onSave:oe=>Te.mutate(oe),isLoading:Te.isPending}),f&&ae&&s.jsx(OKe,{folders:ae,selectedFiles:l,currentFolderId:a,onClose:()=>m(!1),onMove:oe=>ne.mutate({fileIds:l,folderId:oe}),isLoading:ne.isPending}),g&&s.jsx(IKe,{folderId:a,onClose:()=>x(!1),onUploadComplete:Me}),y&&s.jsx(LKe,{folder:y,onClose:()=>b(null),onLink:oe=>Qe.mutate({id:y.id,data:oe}),isLoading:Qe.isPending}),v&&s.jsx(Un,{title:v.type==="folder"?"Delete Folder":v.type==="bulk"?`Delete ${v.count} Files`:"Delete File",message:v.type==="folder"?"Are you sure you want to delete this folder? All files inside will also be deleted.":v.type==="bulk"?`Are you sure you want to delete ${v.count} selected files? This action cannot be undone.`:"Are you sure you want to delete this file?",confirmText:"Delete",variant:"danger",isLoading:J,loadingText:"Deleting...",onConfirm:I,onCancel:()=>M(null)}),S&&s.jsx(Wd,{mode:"reprint",libraryFileId:S.id,archiveName:S.print_name||S.filename,onClose:()=>T(null),onSuccess:()=>{T(null),t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["archives"]})}}),E&&s.jsx(Wd,{mode:"reprint",libraryFileId:E.id,archiveName:E.print_name||E.filename,onClose:()=>k(null),onSuccess:()=>{k(null),c([]),t.invalidateQueries({queryKey:["library-files"]}),t.invalidateQueries({queryKey:["archives"]})}}),A&&s.jsx(DKe,{type:A.type,currentName:A.name,onClose:()=>D(null),onSave:oe=>{A.type==="file"?H.mutate({id:A.id,filename:oe}):K.mutate({id:A.id,name:oe})},isLoading:H.isPending||K.isPending})]})}const _j=5,zKe=2e3,BKe=3e4,$Ke=5e3;function HKe(){const{printerId:t}=HR(),e=parseInt(t||"0",10),[n,r]=w.useState("stream"),[i,a]=w.useState(!1),[o,l]=w.useState(!0),[c,u]=w.useState(Date.now()),[d,f]=w.useState(!1),[m,g]=w.useState(!1),[x,y]=w.useState(0),[b,v]=w.useState(!1),[M,S]=w.useState(0),[T,E]=w.useState(1),[k,A]=w.useState({x:0,y:0}),[D,C]=w.useState(!1),[P,F]=w.useState({x:0,y:0}),[L,X]=w.useState(null),[R,q]=w.useState(null),Y=w.useRef(null),W=w.useRef(null),Z=w.useRef(null),ie=w.useRef(null),ae=w.useRef(null),{data:U}=ct({queryKey:["printer",e],queryFn:()=>pe.getPrinter(e),enabled:e>0});w.useEffect(()=>(U&&(document.title=`${U.name} - Camera`),()=>{document.title="Bambuddy"}),[U]);const G=w.useRef(!1);w.useEffect(()=>{const J=`/api/v1/printers/${e}/camera/stop`;G.current=!1;const xe=()=>{e>0&&!G.current&&(G.current=!0,navigator.sendBeacon(J))},ve=()=>{xe()};window.addEventListener("beforeunload",ve);const oe=Y.current;return()=>{window.removeEventListener("beforeunload",ve),oe&&(oe.src=""),xe()}},[e]),w.useEffect(()=>{if(o&&!d){const xe=setTimeout(()=>{l(!1)},n==="stream"?3e3:2e4);return()=>clearTimeout(xe)}},[n,o,c,d]),w.useEffect(()=>{const J=()=>{const xe=!!document.fullscreenElement;g(xe),E(1),A({x:0,y:0}),n==="stream"&&!d&&(Y.current&&(Y.current.src=""),setTimeout(()=>{l(!0),u(Date.now())},200))};return document.addEventListener("fullscreenchange",J),()=>document.removeEventListener("fullscreenchange",J)},[n,d]),w.useEffect(()=>{let J;const xe=()=>{clearTimeout(J),J=setTimeout(()=>{localStorage.setItem("cameraWindowState",JSON.stringify({width:window.outerWidth,height:window.outerHeight,left:window.screenX,top:window.screenY}))},500)};return window.addEventListener("resize",xe),()=>{clearTimeout(J),window.removeEventListener("resize",xe)}},[]),w.useEffect(()=>()=>{Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),ae.current&&clearInterval(ae.current)},[]);const ee=w.useCallback(()=>{if(x>=_j){v(!1),a(!0);return}const J=Math.min(zKe*Math.pow(2,x),BKe);v(!0),S(Math.ceil(J/1e3)),ie.current=setInterval(()=>{S(xe=>xe<=1?(ie.current&&clearInterval(ie.current),0):xe-1)},1e3),Z.current=setTimeout(()=>{y(xe=>xe+1),v(!1),l(!0),a(!1),Y.current&&(Y.current.src=""),u(Date.now())},J)},[x]);w.useEffect(()=>{if(n!=="stream"||o||b||d){ae.current&&(clearInterval(ae.current),ae.current=null);return}return ae.current=setInterval(async()=>{try{const J=await fetch(`/api/v1/printers/${e}/camera/status`);if(J.ok){const xe=await J.json();(xe.stalled||!xe.active&&!i)&&(console.log(`Stream issue detected: stalled=${xe.stalled}, active=${xe.active}, reconnecting...`),ae.current&&(clearInterval(ae.current),ae.current=null),l(!1),ee())}}catch{}},$Ke),()=>{ae.current&&(clearInterval(ae.current),ae.current=null)}},[n,o,i,b,d,e,ee]);const re=()=>{l(!1),n==="stream"&&x<_j?ee():a(!0)},le=()=>{if(l(!1),a(!1),y(0),v(!1),Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),Y.current&&!localStorage.getItem("cameraWindowState")){const J=Y.current,xe=J.naturalWidth,ve=J.naturalHeight;if(xe>0&&ve>0){const De=window.outerWidth-window.innerWidth,Ze=window.outerHeight-window.innerHeight,ut=xe+16+De,Ye=ve+45+16+Ze;try{window.resizeTo(ut,Ye)}catch{}}}},he=()=>{e>0&&fetch(`/api/v1/printers/${e}/camera/stop`).catch(()=>{})},fe=J=>{n===J||d||(f(!0),l(!0),a(!1),y(0),v(!1),E(1),A({x:0,y:0}),Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),Y.current&&(Y.current.src=""),n==="stream"&&he(),setTimeout(()=>{r(J),u(Date.now()),f(!1)},100))},Te=()=>{d||(f(!0),l(!0),a(!1),y(0),v(!1),Z.current&&clearTimeout(Z.current),ie.current&&clearInterval(ie.current),Y.current&&(Y.current.src=""),n==="stream"&&he(),setTimeout(()=>{u(Date.now()),f(!1)},100))},ge=()=>{W.current&&(document.fullscreenElement?document.exitFullscreen():W.current.requestFullscreen())},_e=()=>{E(J=>Math.min(J+.5,4))},je=()=>{E(J=>{const xe=Math.max(J-.5,1);return xe===1&&A({x:0,y:0}),xe})},ne=J=>{J.preventDefault(),J.deltaY<0?_e():je()},Qe=J=>{T>1&&(J.preventDefault(),C(!0),F({x:J.clientX-k.x,y:J.clientY-k.y}))},B=w.useCallback(()=>{if(!W.current)return{x:300,y:200};const J=W.current.getBoundingClientRect(),xe=J.width*(T-1)/2,ve=J.height*(T-1)/2;return{x:Math.max(50,xe),y:Math.max(50,ve)}},[T]),H=J=>{if(D&&T>1){const xe=J.clientX-P.x,ve=J.clientY-P.y,oe=B();A({x:Math.max(-oe.x,Math.min(oe.x,xe)),y:Math.max(-oe.y,Math.min(oe.y,ve))})}},K=()=>{C(!1)},z=J=>{if(J.length<2)return 0;const xe=J[0].clientX-J[1].clientX,ve=J[0].clientY-J[1].clientY;return Math.sqrt(xe*xe+ve*ve)},V=J=>J.length<2?{x:J[0].clientX,y:J[0].clientY}:{x:(J[0].clientX+J[1].clientX)/2,y:(J[0].clientY+J[1].clientY)/2},Q=J=>{J.touches.length===2?(J.preventDefault(),X(z(J.touches)),q(V(J.touches))):J.touches.length===1&&T>1&&(J.preventDefault(),C(!0),F({x:J.touches[0].clientX-k.x,y:J.touches[0].clientY-k.y}))},Ne=J=>{if(J.touches.length===2&&L!==null){J.preventDefault();const xe=z(J.touches),ve=xe/L;E(Ae=>{const De=Math.max(1,Math.min(4,Ae*ve));return De===1&&A({x:0,y:0}),De}),X(xe);const oe=V(J.touches);if(R){const Ae=B();A(De=>({x:Math.max(-Ae.x,Math.min(Ae.x,De.x+(oe.x-R.x))),y:Math.max(-Ae.y,Math.min(Ae.y,De.y+(oe.y-R.y)))}))}q(oe)}else if(J.touches.length===1&&D&&T>1){J.preventDefault();const xe=J.touches[0].clientX-P.x,ve=J.touches[0].clientY-P.y,oe=B();A({x:Math.max(-oe.x,Math.min(oe.x,xe)),y:Math.max(-oe.y,Math.min(oe.y,ve))})}},ke=J=>{J.touches.length<2&&(X(null),q(null)),J.touches.length===0&&C(!1)},Me=()=>{E(1),A({x:0,y:0})},$=d?"":n==="stream"?`/api/v1/printers/${e}/camera/stream?fps=10&t=${c}`:`/api/v1/printers/${e}/camera/snapshot?t=${c}`,I=o||d||b;return e?s.jsxs("div",{ref:W,className:"min-h-screen bg-black flex flex-col",children:[s.jsxs("div",{className:"flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary",children:[s.jsxs("h1",{className:"text-sm font-medium text-white flex items-center gap-2",children:[s.jsx(Xp,{className:"w-4 h-4"}),U?.name||`Printer ${e}`]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"flex bg-bambu-dark rounded p-0.5",children:[s.jsx("button",{onClick:()=>fe("stream"),disabled:I,className:`px-3 py-1 text-xs rounded transition-colors ${n==="stream"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:"Live"}),s.jsx("button",{onClick:()=>fe("snapshot"),disabled:I,className:`px-3 py-1 text-xs rounded transition-colors ${n==="snapshot"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:"Snapshot"})]}),s.jsx("button",{onClick:Te,disabled:I,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50",title:n==="stream"?"Restart stream":"Refresh snapshot",children:s.jsx(Mr,{className:`w-4 h-4 text-bambu-gray ${I?"animate-spin":""}`})}),s.jsx("button",{onClick:ge,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded",title:m?"Exit fullscreen":"Fullscreen",children:m?s.jsx(QW,{className:"w-4 h-4 text-bambu-gray"}):s.jsx(gde,{className:"w-4 h-4 text-bambu-gray"})})]})]}),s.jsx("div",{className:"flex-1 flex items-center justify-center p-2 overflow-hidden",onWheel:ne,onMouseMove:H,onMouseUp:K,onMouseLeave:K,onTouchStart:Q,onTouchMove:Ne,onTouchEnd:ke,style:{touchAction:"none"},children:s.jsxs("div",{className:"relative w-full h-full flex items-center justify-center",children:[(o||d)&&!b&&s.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/50 z-10",children:s.jsxs("div",{className:"text-center",children:[s.jsx(Mr,{className:"w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:n==="stream"?"Connecting to camera...":"Capturing snapshot..."})]})}),b&&s.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/80 z-10",children:s.jsxs("div",{className:"text-center p-4",children:[s.jsx(Pg,{className:"w-10 h-10 text-orange-400 mx-auto mb-3"}),s.jsx("p",{className:"text-white mb-2",children:"Connection lost"}),s.jsxs("p",{className:"text-sm text-bambu-gray mb-3",children:["Reconnecting in ",M,"s... (attempt ",x+1,"/",_j,")"]}),s.jsx("button",{onClick:Te,className:"px-4 py-2 bg-bambu-green text-white text-sm rounded hover:bg-bambu-green/80 transition-colors",children:"Reconnect now"})]})}),i&&!b&&s.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black z-10",children:s.jsxs("div",{className:"text-center p-4",children:[s.jsx(jn,{className:"w-12 h-12 text-orange-400 mx-auto mb-3"}),s.jsx("p",{className:"text-white mb-2",children:"Camera unavailable"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-4 max-w-md",children:"Make sure the printer is powered on and connected."}),s.jsx("button",{onClick:Te,className:"px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors",children:"Retry"})]})}),s.jsx("img",{ref:Y,src:$,alt:"Camera stream",className:"max-w-full max-h-full object-contain select-none",style:{transform:`scale(${T}) translate(${k.x/T}px, ${k.y/T}px)`,cursor:T>1?D?"grabbing":"grab":"default"},onError:$?re:void 0,onLoad:$?le:void 0,onMouseDown:Qe,draggable:!1},c),s.jsxs("div",{className:"absolute bottom-4 left-4 flex items-center gap-1.5 bg-black/60 rounded-lg px-2 py-1.5",children:[s.jsx("button",{onClick:je,disabled:T<=1,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:"Zoom out",children:s.jsx(lD,{className:"w-4 h-4 text-white"})}),s.jsxs("button",{onClick:Me,className:"px-2 py-1 text-sm text-white hover:bg-white/10 rounded min-w-[48px]",title:"Reset zoom",children:[Math.round(T*100),"%"]}),s.jsx("button",{onClick:_e,disabled:T>=4,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:"Zoom in",children:s.jsx(oD,{className:"w-4 h-4 text-white"})})]})]})})]}):s.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:s.jsx("p",{className:"text-white",children:"Invalid printer ID"})})}function VKe(){const{id:t}=HR(),{mode:e}=Xd(),{data:n,isLoading:r,error:i}=ct({queryKey:["external-link",t],queryFn:()=>pe.getExternalLink(Number(t)),enabled:!!t});return r?s.jsx("div",{className:"flex items-center justify-center h-full",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):i||!n?s.jsxs("div",{className:"flex flex-col items-center justify-center h-full gap-4 text-bambu-gray",children:[s.jsx(jn,{className:"w-12 h-12"}),s.jsx("p",{children:"Link not found"})]}):s.jsx("iframe",{src:n.url,className:"h-full w-full border-0",style:{colorScheme:e},title:n.name,sandbox:"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"})}const XG=["DEBUG","INFO","WARNING","ERROR"],wj={DEBUG:"text-gray-400",INFO:"text-blue-400",WARNING:"text-yellow-400",ERROR:"text-red-400"},GKe={DEBUG:N2,INFO:tl,WARNING:jn,ERROR:ta};function WKe(){const t=an(),[e,n]=w.useState(!0),[r,i]=w.useState(new Set),[a,o]=w.useState(""),[l,c]=w.useState("ALL"),[u,d]=w.useState(!1),[f,m]=w.useState(!1),g=w.useRef(null),{data:x,isLoading:y,refetch:b}=ct({queryKey:["application-logs",l,a],queryFn:()=>Op.getLogs({limit:200,level:l==="ALL"?void 0:l,search:a||void 0}),refetchInterval:f?2e3:!1,enabled:u});w.useEffect(()=>{u||m(!1)},[u]);const v=ft({mutationFn:()=>Op.clearLogs(),onSuccess:()=>{t.invalidateQueries({queryKey:["application-logs"]})}});w.useEffect(()=>{e&&g.current&&x?.entries&&(g.current.scrollTop=g.current.scrollHeight)},[x?.entries,e]);const M=A=>{i(D=>{const C=new Set(D);return C.has(A)?C.delete(A):C.add(A),C})},S=A=>{const D=A.split(" ");return D.length>=2?D[1]:A},T=w.useMemo(()=>x?.entries??[],[x?.entries]),E=w.useMemo(()=>[...T].reverse(),[T]),k=({level:A})=>{const D=GKe[A]||tl;return s.jsx(D,{className:`w-3.5 h-3.5 ${wj[A]||"text-gray-400"}`})};return s.jsxs("div",{className:"bg-bambu-dark rounded-lg overflow-hidden",children:[s.jsxs("button",{onClick:()=>d(!u),className:"w-full flex items-center justify-between p-4 hover:bg-bambu-dark-tertiary/50 transition-colors",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${f?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:s.jsx(N2,{className:"w-5 h-5"})}),s.jsxs("div",{className:"text-left",children:[s.jsx("p",{className:"font-medium text-white",children:"Application Logs"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:f?`Live streaming - ${x?.filtered_count??0} entries`:"View and filter application logs"})]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[f&&s.jsxs("span",{className:"flex items-center gap-1.5 px-2 py-1 bg-bambu-green/20 rounded text-bambu-green text-xs",children:[s.jsx("span",{className:"w-1.5 h-1.5 bg-bambu-green rounded-full animate-pulse"}),"Live"]}),u?s.jsx(eu,{className:"w-5 h-5 text-bambu-gray"}):s.jsx(ea,{className:"w-5 h-5 text-bambu-gray"})]})]}),u&&s.jsxs("div",{className:"border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex flex-col gap-2 p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[f?s.jsxs("button",{onClick:A=>{A.stopPropagation(),m(!1)},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-red-500/20 text-red-400 hover:bg-red-500/30 rounded transition-colors",children:[s.jsx(al,{className:"w-4 h-4"}),"Stop"]}):s.jsxs("button",{onClick:A=>{A.stopPropagation(),m(!0),b()},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30 rounded transition-colors",children:[s.jsx(rl,{className:"w-4 h-4"}),"Start"]}),s.jsxs("button",{onClick:()=>v.mutate(),disabled:v.isPending||T.length===0,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:[s.jsx(ln,{className:"w-4 h-4"}),"Clear"]}),s.jsx("button",{onClick:()=>b(),disabled:y,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:s.jsx(Mr,{className:`w-4 h-4 ${y?"animate-spin":""}`})}),s.jsx("div",{className:"flex-1"}),s.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:e,onChange:A=>n(A.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark-tertiary"}),"Auto-scroll"]}),s.jsxs("span",{className:"text-sm text-bambu-gray",children:[x?.filtered_count??0,"/",x?.total_in_file??0]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsx(ba,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",placeholder:"Search message or logger name...",value:a,onChange:A=>o(A.target.value),className:"w-full pl-8 pr-8 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),a&&s.jsx("button",{onClick:()=>o(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:s.jsx(Bt,{className:"w-4 h-4"})})]}),s.jsxs("div",{className:"flex items-center gap-1 bg-bambu-dark-secondary rounded border border-bambu-dark-tertiary",children:[s.jsx("button",{onClick:()=>c("ALL"),className:`px-2 py-1.5 text-xs rounded-l transition-colors ${l==="ALL"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:"All"}),XG.map((A,D)=>s.jsx("button",{onClick:()=>c(A),className:`px-2 py-1.5 text-xs transition-colors flex items-center gap-1 ${D===XG.length-1?"rounded-r":""} ${l===A?`${wj[A]} bg-bambu-dark-tertiary`:"text-bambu-gray hover:text-white"}`,children:A},A))]})]})]}),s.jsx("div",{ref:g,className:"overflow-auto font-mono text-xs bg-black min-h-[300px] max-h-[500px]",children:T.length===0?s.jsxs("div",{className:"flex flex-col items-center justify-center h-[300px] text-bambu-gray",children:[s.jsx("p",{className:"mb-2",children:"No log entries found"}),s.jsx("p",{className:"text-sm",children:"Log file may be empty or cleared"})]}):s.jsx("div",{className:"divide-y divide-bambu-dark-tertiary/30",children:E.map((A,D)=>{const C=r.has(D),P=A.message.includes(` `);return s.jsx("div",{className:`p-2 cursor-pointer hover:bg-bambu-dark-secondary/50 transition-colors ${C?"bg-bambu-dark-secondary/30":""}`,onClick:()=>P&&M(D),children:s.jsxs("div",{className:"flex items-start gap-2",children:[s.jsx("span",{className:"text-bambu-gray/70 shrink-0 w-20",children:S(A.timestamp)}),s.jsx("span",{className:"shrink-0",children:s.jsx(k,{level:A.level})}),s.jsxs("span",{className:"text-purple-400/80 shrink-0 max-w-[200px] truncate",title:A.logger_name,children:["[",A.logger_name,"]"]}),s.jsx("span",{className:`flex-1 ${wj[A.level]||"text-white/80"} ${!C&&P?"truncate":""}`,children:C?s.jsx("pre",{className:"whitespace-pre-wrap break-all",children:A.message}):A.message.split(` `)[0]}),P&&s.jsx("span",{className:"text-bambu-gray/50 shrink-0",children:C?s.jsx(eu,{className:"w-3.5 h-3.5"}):s.jsx(ea,{className:"w-3.5 h-3.5"})})]})},D)})})}),s.jsx("div",{className:"flex items-center justify-between p-3 border-t border-bambu-dark-tertiary text-sm text-bambu-gray",children:f?s.jsxs("span",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"w-2 h-2 bg-green-500 rounded-full animate-pulse"}),"Auto-refreshing every 2 seconds"]}):s.jsx("span",{children:"Click Start to enable live log streaming"})})]})]})}function qKe(t){return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(1)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`}function Kr({icon:t,label:e,value:n,subValue:r,color:i="text-bambu-green"}){return s.jsxs("div",{className:"flex items-start gap-3 p-4 bg-bambu-dark rounded-lg",children:[s.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark-tertiary ${i}`,children:s.jsx(t,{className:"w-5 h-5"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:e}),s.jsx("p",{className:"text-lg font-semibold text-white truncate",children:n}),r&&s.jsx("p",{className:"text-xs text-bambu-gray mt-0.5",children:r})]})]})}function YG({percent:t,color:e="bg-bambu-green"}){return s.jsx("div",{className:"w-full h-2 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:`h-full ${e} transition-all duration-300`,style:{width:`${Math.min(100,t)}%`}})})}function ed({title:t,icon:e,children:n}){return s.jsxs(Mt,{className:"p-6",children:[s.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[s.jsx(e,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:t})]}),n]})}function KKe(){const{t}=Ag(),e=an(),[n,r]=w.useState(null),[i,a]=w.useState(!1),[o,l]=w.useState(!1),{data:c,isLoading:u,refetch:d,isFetching:f}=ct({queryKey:["systemInfo"],queryFn:pe.getSystemInfo,refetchInterval:3e4}),{data:m}=ct({queryKey:["debugLogging"],queryFn:Op.getDebugLoggingState,staleTime:10*1e3,refetchInterval:10*1e3}),{data:g}=ct({queryKey:["settings"],queryFn:pe.getSettings}),{data:x}=ct({queryKey:["library-stats"],queryFn:pe.getLibraryStats}),y=g?.time_format||"system",b=async()=>{l(!0);try{const T=await Op.setDebugLogging(!m?.enabled);e.setQueryData(["debugLogging"],T)}catch(T){console.error("Failed to toggle debug logging:",T)}finally{l(!1)}},v=async()=>{r(null),a(!0);try{await Op.downloadSupportBundle()}catch(T){r(T instanceof Error?T.message:"Failed to download support bundle")}finally{a(!1)}};if(u)return s.jsx("div",{className:"flex items-center justify-center h-64",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})});if(!c)return s.jsx("div",{className:"p-6 text-center text-bambu-gray",children:t("system.failedToLoad","Failed to load system information")});const M=c.storage.disk_percent_used>90?"bg-red-500":c.storage.disk_percent_used>75?"bg-yellow-500":"bg-bambu-green",S=c.memory.percent_used>90?"bg-red-500":c.memory.percent_used>75?"bg-yellow-500":"bg-bambu-green";return s.jsxs("div",{className:"p-6 space-y-6",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:t("system.title","System Information")}),s.jsx("p",{className:"text-bambu-gray mt-1",children:t("system.subtitle","Monitor system resources and database statistics")})]}),s.jsxs("button",{onClick:()=>d(),disabled:f,className:"flex items-center gap-2 px-4 py-2 bg-bambu-dark-secondary hover:bg-bambu-dark-tertiary rounded-lg transition-colors disabled:opacity-50",children:[s.jsx(Mr,{className:`w-4 h-4 ${f?"animate-spin":""}`}),t("common.refresh","Refresh")]})]}),s.jsx(ed,{title:t("system.application","Application"),icon:Ph,children:s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[s.jsx(Kr,{icon:Ph,label:t("system.version","Version"),value:`v${c.app.version}`}),s.jsx(Kr,{icon:Yn,label:t("system.uptime","System Uptime"),value:c.system.uptime_formatted}),s.jsx(Kr,{icon:Ph,label:t("system.hostname","Hostname"),value:c.system.hostname})]})}),s.jsx(ed,{title:t("support.title","Support & Troubleshooting"),icon:Uue,children:s.jsxs("div",{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:t("support.description","Enable debug logging to capture detailed information, then download a support bundle to share when reporting issues.")}),s.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${m?.enabled?"bg-amber-500/20 text-amber-500":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:s.jsx(N2,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"font-medium text-white",children:t("support.debugLogging","Debug Logging")}),s.jsxs("p",{className:"text-sm text-bambu-gray",children:[m?.enabled?t("support.debugLoggingEnabled","Capturing detailed logs"):t("support.debugLoggingDisabled","Normal logging level"),m?.enabled&&m.duration_seconds!==null&&s.jsxs("span",{className:"text-amber-400 ml-2",children:["(",Math.floor(m.duration_seconds/60),"m ",m.duration_seconds%60,"s)"]})]})]})]}),s.jsxs("button",{onClick:b,disabled:o,className:`px-4 py-2 rounded-lg font-medium transition-colors flex items-center gap-2 ${m?.enabled?"bg-amber-500/20 text-amber-400 hover:bg-amber-500/30":"bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30"} disabled:opacity-50`,children:[o&&s.jsx(ht,{className:"w-4 h-4 animate-spin"}),m?.enabled?t("support.disableDebug","Disable"):t("support.enableDebug","Enable")]})]}),s.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-green",children:s.jsx(ei,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"font-medium text-white",children:t("support.supportBundle","Support Bundle")}),s.jsx("p",{className:"text-sm text-bambu-gray",children:t("support.supportBundleDescription","Download system info and logs as a ZIP file")})]})]}),s.jsxs("button",{onClick:v,disabled:i||!m?.enabled,className:"px-4 py-2 rounded-lg font-medium bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30 transition-colors flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed",title:m?.enabled?void 0:t("support.enableDebugFirst","Enable debug logging first"),children:[i&&s.jsx(ht,{className:"w-4 h-4 animate-spin"}),t("common.download","Download")]})]}),n&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/30 rounded-lg text-red-400 text-sm",children:n}),!m?.enabled&&s.jsx("div",{className:"p-4 bg-bambu-dark-tertiary/50 rounded-lg",children:s.jsxs("p",{className:"text-sm text-bambu-gray",children:[s.jsx("span",{className:"text-amber-400 font-medium",children:t("support.instructions","To report an issue:")}),s.jsx("br",{}),"1. ",t("support.step1","Enable debug logging"),s.jsx("br",{}),"2. ",t("support.step2","Reproduce the issue"),s.jsx("br",{}),"3. ",t("support.step3","Download the support bundle"),s.jsx("br",{}),"4. ",t("support.step4","Attach the ZIP file to your issue report")]})}),s.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg space-y-3",children:[s.jsx("p",{className:"text-sm font-medium text-white",children:t("support.privacyTitle","What's in the support bundle?")}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 text-sm",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-bambu-green font-medium mb-1",children:t("support.collected","Collected:")}),s.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[s.jsxs("li",{children:["• ",t("support.collectItem1","App version and debug mode")]}),s.jsxs("li",{children:["• ",t("support.collectItem2","OS, architecture, Python version")]}),s.jsxs("li",{children:["• ",t("support.collectItem3","Database statistics (counts only)")]}),s.jsxs("li",{children:["• ",t("support.collectItem4","Printer models and nozzle counts")]}),s.jsxs("li",{children:["• ",t("support.collectItem5","Non-sensitive settings (themes, formats)")]}),s.jsxs("li",{children:["• ",t("support.collectItem6","Debug logs (sanitized)")]})]})]}),s.jsxs("div",{children:[s.jsx("p",{className:"text-red-400 font-medium mb-1",children:t("support.notCollected","NOT collected:")}),s.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[s.jsxs("li",{children:["• ",t("support.notItem1","Printer names, IPs, serial numbers")]}),s.jsxs("li",{children:["• ",t("support.notItem2","Access codes and passwords")]}),s.jsxs("li",{children:["• ",t("support.notItem3","Email addresses")]}),s.jsxs("li",{children:["• ",t("support.notItem4","API keys and tokens")]}),s.jsxs("li",{children:["• ",t("support.notItem5","Webhook URLs")]}),s.jsxs("li",{children:["• ",t("support.notItem6","Your hostname or username")]})]})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray/70",children:t("support.privacyNote","IP addresses in logs are replaced with [IP] and email addresses with [EMAIL].")})]}),s.jsx(WKe,{})]})}),s.jsxs(ed,{title:t("system.database","Database"),icon:Yp,children:[s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 mb-4",children:[s.jsx(Kr,{icon:Va,label:t("system.totalArchives","Total Archives"),value:c.database.archives}),s.jsx(Kr,{icon:E2,label:t("system.completed","Completed"),value:c.database.archives_completed,color:"text-green-500"}),s.jsx(Kr,{icon:Ti,label:t("system.failed","Failed"),value:c.database.archives_failed,color:"text-red-500"}),s.jsx(Kr,{icon:ht,label:t("system.printing","Printing"),value:c.database.archives_printing,color:"text-yellow-500"})]}),s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsx(Kr,{icon:zr,label:t("system.printers","Printers"),value:c.database.printers}),s.jsx(Kr,{icon:nD,label:t("system.filaments","Filaments"),value:c.database.filaments}),s.jsx(Kr,{icon:ya,label:t("system.projects","Projects"),value:c.database.projects}),s.jsx(Kr,{icon:zs,label:t("system.smartPlugs","Smart Plugs"),value:c.database.smart_plugs})]}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 mt-4",children:[s.jsx(Kr,{icon:Yn,label:t("system.totalPrintTime","Total Print Time"),value:c.database.total_print_time_formatted}),s.jsx(Kr,{icon:Va,label:t("system.totalFilament","Total Filament Used"),value:`${c.database.total_filament_kg} kg`,subValue:`${c.database.total_filament_grams.toLocaleString()} g`})]})]}),s.jsxs(ed,{title:t("system.connectedPrinters","Connected Printers"),icon:zr,children:[s.jsxs("div",{className:"flex items-center gap-4 mb-4",children:[s.jsx("div",{className:"text-3xl font-bold text-bambu-green",children:c.printers.connected}),s.jsx("div",{className:"text-bambu-gray",children:t("system.ofTotal","of {{total}} printers connected",{total:c.printers.total})})]}),c.printers.connected_list.length>0?s.jsx("div",{className:"space-y-2",children:c.printers.connected_list.map(T=>s.jsxs("div",{className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green"}),s.jsx("span",{className:"font-medium text-white",children:T.name})]}),s.jsxs("div",{className:"flex items-center gap-4 text-sm text-bambu-gray",children:[s.jsx("span",{children:T.model}),s.jsx("span",{className:`px-2 py-0.5 rounded ${T.state==="RUNNING"?"bg-bambu-green/20 text-bambu-green":T.state==="IDLE"?"bg-blue-500/20 text-blue-400":"bg-bambu-dark-tertiary"}`,children:T.state})]})]},T.id))}):s.jsx("p",{className:"text-bambu-gray",children:t("system.noPrintersConnected","No printers connected")})]}),s.jsx(ed,{title:t("system.storage","Storage"),icon:P2,children:s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[s.jsx("span",{className:"text-bambu-gray",children:t("system.diskUsage","Disk Usage")}),s.jsxs("span",{className:"text-white",children:[c.storage.disk_used_formatted," / ",c.storage.disk_total_formatted]})]}),s.jsx(YG,{percent:c.storage.disk_percent_used,color:M}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:[c.storage.disk_free_formatted," ",t("system.free","free")," (",(100-c.storage.disk_percent_used).toFixed(1),"%)"]})]}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[s.jsx(Kr,{icon:Va,label:t("system.archiveStorage","Archive Storage"),value:c.storage.archive_size_formatted}),s.jsx(Kr,{icon:Yp,label:t("system.databaseSize","Database Size"),value:c.storage.database_size_formatted}),x&&s.jsx(Kr,{icon:el,label:t("system.fileManagerStorage","File Manager"),value:qKe(x.total_size_bytes),subValue:`${x.total_files} files, ${x.total_folders} folders`})]})]})}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[s.jsx(ed,{title:t("system.memory","Memory"),icon:bde,children:s.jsx("div",{className:"space-y-4",children:s.jsxs("div",{children:[s.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[s.jsx("span",{className:"text-bambu-gray",children:t("system.memoryUsage","Memory Usage")}),s.jsxs("span",{className:"text-white",children:[c.memory.used_formatted," / ",c.memory.total_formatted]})]}),s.jsx(YG,{percent:c.memory.percent_used,color:S}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:[c.memory.available_formatted," ",t("system.available","available")]})]})})}),s.jsx(ed,{title:t("system.cpu","CPU"),icon:Xv,children:s.jsx("div",{className:"space-y-4",children:s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsx(Kr,{icon:Xv,label:t("system.cores","Cores"),value:c.cpu.count,subValue:`${c.cpu.count_logical} logical`}),s.jsx(Kr,{icon:Xv,label:t("system.usage","Usage"),value:`${c.cpu.percent}%`})]})})})]}),s.jsx(ed,{title:t("system.systemDetails","System Details"),icon:Ph,children:s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsx(Kr,{icon:Ph,label:t("system.os","Operating System"),value:c.system.platform,subValue:c.system.platform_release}),s.jsx(Kr,{icon:Xv,label:t("system.architecture","Architecture"),value:c.system.architecture}),s.jsx(Kr,{icon:Ph,label:t("system.python","Python"),value:c.system.python_version}),s.jsx(Kr,{icon:Yn,label:t("system.bootTime","Boot Time"),value:eg(c.system.boot_time,y)})]})})]})}function XKe(){const t=cl(),{login:e}=Yd(),{showToast:n}=Gn(),{mode:r}=Xd(),[i,a]=w.useState(""),[o,l]=w.useState(""),c=ft({mutationFn:()=>e(i,o),onSuccess:()=>{n("Logged in successfully"),t("/")},onError:d=>{n(d.message||"Login failed","error")}}),u=d=>{if(d.preventDefault(),!i||!o){n("Please enter username and password","error");return}c.mutate()};return s.jsx("div",{className:"min-h-screen flex items-center justify-center bg-bambu-dark p-4",children:s.jsxs("div",{className:"max-w-md w-full space-y-8 p-8 bg-gradient-to-br from-bambu-card to-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary shadow-lg",children:[s.jsxs("div",{className:"text-center",children:[s.jsx("div",{className:"flex items-center justify-center mb-6",children:s.jsx("img",{src:r==="dark"?"/img/bambuddy_logo_dark_transparent.png":"/img/bambuddy_logo_light.png",alt:"Bambuddy",className:"h-16"})}),s.jsx("h2",{className:"text-3xl font-bold text-white",children:"Bambuddy Login"}),s.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:"Sign in to your account"})]}),s.jsxs("form",{className:"mt-8 space-y-6",onSubmit:u,children:[s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{htmlFor:"username",className:"block text-sm font-medium text-white mb-2",children:"Username"}),s.jsx("input",{id:"username",type:"text",required:!0,value:i,onChange:d=>a(d.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:"Enter your username",autoComplete:"username"})]}),s.jsxs("div",{children:[s.jsx("label",{htmlFor:"password",className:"block text-sm font-medium text-white mb-2",children:"Password"}),s.jsx("input",{id:"password",type:"password",required:!0,value:o,onChange:d=>l(d.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:"Enter your password",autoComplete:"current-password"})]})]}),s.jsx("div",{children:s.jsx("button",{type:"submit",disabled:c.isPending,className:"w-full flex justify-center py-3 px-4 bg-bambu-green hover:bg-bambu-green-light text-white font-medium rounded-lg shadow-lg shadow-bambu-green/20 hover:shadow-bambu-green/30 focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:ring-offset-2 focus:ring-offset-bambu-dark-secondary transition-all disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-bambu-green",children:c.isPending?"Logging in...":"Sign in"})})]})]})})}function YKe(){const t=cl(),{showToast:e}=Gn(),{mode:n}=Xd(),{refreshAuth:r}=Yd(),[i,a]=w.useState(!1),[o,l]=w.useState(""),[c,u]=w.useState(""),[d,f]=w.useState(""),m=ft({mutationFn:()=>pe.setupAuth({auth_enabled:i,admin_username:i?o:void 0,admin_password:i?c:void 0}),onSuccess:async x=>{await r(),x.auth_enabled?x.admin_created?(e("Authentication enabled and admin user created"),t("/login")):(e("Authentication enabled using existing admin users"),t("/login")):(e("Setup completed"),t("/"))},onError:x=>{e(x.message,"error")}}),g=x=>{if(x.preventDefault(),i&&(o||c)){if(!o||!c){e("Please enter both admin username and password, or leave both empty to use existing admin users","error");return}if(c!==d){e("Passwords do not match","error");return}if(c.length<6){e("Password must be at least 6 characters","error");return}}m.mutate()};return s.jsx("div",{className:"min-h-screen flex items-center justify-center bg-bambu-dark p-4",children:s.jsxs("div",{className:"max-w-md w-full space-y-8 p-8 bg-gradient-to-br from-bambu-card to-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary shadow-lg",children:[s.jsxs("div",{className:"text-center",children:[s.jsx("div",{className:"flex items-center justify-center mb-6",children:s.jsx("img",{src:n==="dark"?"/img/bambuddy_logo_dark_transparent.png":"/img/bambuddy_logo_light.png",alt:"Bambuddy",className:"h-16"})}),s.jsx("h2",{className:"text-3xl font-bold text-white",children:"Bambuddy Setup"}),s.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:"Configure authentication for your Bambuddy instance"})]}),s.jsxs("form",{className:"mt-8 space-y-6",onSubmit:g,children:[s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center p-4 bg-bambu-dark-secondary/50 rounded-lg border border-bambu-dark-tertiary",children:[s.jsx("input",{id:"auth-enabled",type:"checkbox",checked:i,onChange:x=>a(x.target.checked),className:"h-4 w-4 text-bambu-green focus:ring-bambu-green border-bambu-dark-tertiary rounded bg-bambu-dark-secondary"}),s.jsx("label",{htmlFor:"auth-enabled",className:"ml-3 block text-sm font-medium text-white",children:"Enable Authentication"})]}),i&&s.jsxs("div",{className:"space-y-4 mt-4",children:[s.jsx("div",{className:"p-3 bg-bambu-dark-secondary/50 border border-bambu-dark-tertiary rounded-lg",children:s.jsxs("div",{className:"flex items-start gap-2",children:[s.jsx(tl,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"text-sm text-bambu-gray",children:[s.jsx("p",{className:"text-white font-medium mb-1",children:"Admin Account"}),s.jsx("p",{children:"If admin users already exist, authentication will be enabled using the existing admin accounts. Leave the fields below empty to use existing admins, or enter new credentials to create a new admin user."})]})]})}),s.jsxs("div",{children:[s.jsxs("label",{htmlFor:"admin-username",className:"block text-sm font-medium text-white mb-2",children:["Admin Username ",s.jsx("span",{className:"text-bambu-gray text-xs",children:"(optional if admin users exist)"})]}),s.jsx("input",{id:"admin-username",type:"text",value:o,onChange:x=>l(x.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:"Enter admin username (optional)",autoComplete:"username"})]}),s.jsxs("div",{children:[s.jsxs("label",{htmlFor:"admin-password",className:"block text-sm font-medium text-white mb-2",children:["Admin Password ",s.jsx("span",{className:"text-bambu-gray text-xs",children:"(optional if admin users exist)"})]}),s.jsx("input",{id:"admin-password",type:"password",value:c,onChange:x=>u(x.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:"Enter admin password (optional)",minLength:6,autoComplete:"new-password"})]}),c&&s.jsxs("div",{children:[s.jsx("label",{htmlFor:"confirm-password",className:"block text-sm font-medium text-white mb-2",children:"Confirm Password"}),s.jsx("input",{id:"confirm-password",type:"password",value:d,onChange:x=>f(x.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:"Confirm admin password",minLength:6,autoComplete:"new-password"})]})]})]}),s.jsx("div",{children:s.jsx("button",{type:"submit",disabled:m.isPending,className:"w-full flex justify-center py-3 px-4 bg-bambu-green hover:bg-bambu-green-light text-white font-medium rounded-lg shadow-lg shadow-bambu-green/20 hover:shadow-bambu-green/30 focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:ring-offset-2 focus:ring-offset-bambu-dark-secondary transition-all disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-bambu-green",children:m.isPending?"Setting up...":"Complete Setup"})})]})]})})}function QKe(){const t=cl(),{user:e}=Yd(),{showToast:n}=Gn(),r=an(),[i,a]=w.useState(!1),[o,l]=w.useState(null),[c,u]=w.useState(null),[d,f]=w.useState({username:"",password:"",role:"user"});w.useEffect(()=>{const E=k=>{k.key==="Escape"&&i&&(a(!1),f({username:"",password:"",role:"user"}))};return window.addEventListener("keydown",E),()=>window.removeEventListener("keydown",E)},[i]);const{data:m=[],isLoading:g}=ct({queryKey:["users"],queryFn:()=>pe.getUsers()}),x=ft({mutationFn:E=>pe.createUser(E),onSuccess:()=>{r.invalidateQueries({queryKey:["users"]}),a(!1),f({username:"",password:"",role:"user"}),n("User created successfully")},onError:E=>{n(E.message,"error")}}),y=ft({mutationFn:({id:E,data:k})=>pe.updateUser(E,k),onSuccess:()=>{r.invalidateQueries({queryKey:["users"]}),l(null),f({username:"",password:"",role:"user"}),n("User updated successfully")},onError:E=>{n(E.message,"error")}}),b=ft({mutationFn:E=>pe.deleteUser(E),onSuccess:()=>{r.invalidateQueries({queryKey:["users"]}),n("User deleted successfully")},onError:E=>{n(E.message,"error")}}),v=()=>{if(!d.username||!d.password){n("Please fill in all required fields","error");return}x.mutate(d)},M=E=>{const k={username:d.username||void 0,password:d.password||void 0,role:d.role};k.password||delete k.password,y.mutate({id:E,data:k})},S=E=>{u(E)},T=E=>{l(E.id),f({username:E.username,password:"",role:E.role})};return e?.role!=="admin"?s.jsx("div",{className:"p-6",children:s.jsx(Mt,{children:s.jsx(Tt,{className:"py-6",children:s.jsxs("div",{className:"flex items-center gap-3 text-red-400",children:[s.jsx(P1,{className:"w-5 h-5"}),s.jsx("p",{className:"text-white",children:"You do not have permission to access this page."})]})})})}):s.jsxs("div",{className:"p-6",children:[s.jsxs("div",{className:"flex justify-between items-center mb-6",children:[s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx("button",{onClick:()=>t("/settings?tab=users"),className:"p-2 rounded-lg bg-bambu-dark-secondary hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors",title:"Back to Settings",children:s.jsx(BW,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-2",children:[s.jsx(Wh,{className:"w-6 h-6 text-bambu-green"}),"User Management"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Manage users and their access to your Bambuddy instance"})]})]}),s.jsxs(Se,{onClick:()=>{a(!0),f({username:"",password:"",role:"user"})},children:[s.jsx(Jn,{className:"w-4 h-4"}),"Create User"]})]}),g?s.jsx("div",{className:"flex items-center justify-center py-12",children:s.jsx(ht,{className:"w-8 h-8 text-bambu-green animate-spin"})}):s.jsx(Mt,{children:s.jsx("div",{className:"overflow-x-auto",children:s.jsxs("table",{className:"min-w-full divide-y divide-bambu-dark-tertiary",children:[s.jsx("thead",{children:s.jsxs("tr",{children:[s.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wider",children:"Username"}),s.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wider",children:"Role"}),s.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wider",children:"Status"}),s.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-bambu-gray uppercase tracking-wider",children:"Actions"})]})}),s.jsx("tbody",{className:"divide-y divide-bambu-dark-tertiary",children:m.map(E=>s.jsxs("tr",{className:"hover:bg-bambu-dark-tertiary/50 transition-colors",children:[s.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm font-medium text-white",children:o===E.id?s.jsx("input",{type:"text",value:d.username,onChange:k=>f({...d,username:k.target.value}),className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green"}):E.username}),s.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm",children:o===E.id?s.jsxs("select",{value:d.role,onChange:k=>f({...d,role:k.target.value}),className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green",children:[s.jsx("option",{value:"user",children:"User"}),s.jsx("option",{value:"admin",children:"Admin"})]}):s.jsx("span",{className:`px-3 py-1 rounded-full text-xs font-medium ${E.role==="admin"?"bg-purple-500/20 text-purple-300":"bg-blue-500/20 text-blue-300"}`,children:E.role})}),s.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm",children:s.jsx("span",{className:`px-3 py-1 rounded-full text-xs font-medium ${E.is_active?"bg-bambu-green/20 text-bambu-green":"bg-red-500/20 text-red-400"}`,children:E.is_active?"Active":"Inactive"})}),s.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm font-medium",children:o===E.id?s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs(Se,{size:"sm",onClick:()=>M(E.id),disabled:y.isPending,children:[y.isPending?s.jsx(ht,{className:"w-4 h-4 animate-spin"}):s.jsx(hs,{className:"w-4 h-4"}),"Save"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>{l(null),f({username:"",password:"",role:"user"})},children:"Cancel"})]}):s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs(Se,{size:"sm",variant:"ghost",onClick:()=>T(E),children:[s.jsx(L2,{className:"w-4 h-4"}),"Edit"]}),E.id!==e?.id&&s.jsxs(Se,{size:"sm",variant:"ghost",onClick:()=>S(E.id),children:[s.jsx(ln,{className:"w-4 h-4"}),"Delete"]})]})})]},E.id))})]})})}),i&&s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:()=>{a(!1),f({username:"",password:"",role:"user"})},children:s.jsxs(Mt,{className:"w-full max-w-md",onClick:E=>E.stopPropagation(),children:[s.jsx(br,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Wh,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Create User"})]}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:()=>{a(!1),f({username:"",password:"",role:"user"})},children:s.jsx(Bt,{className:"w-5 h-5"})})]})}),s.jsxs(Tt,{children:[s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-2",children:"Username"}),s.jsx("input",{type:"text",value:d.username,onChange:E=>f({...d,username:E.target.value}),className:"w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:"Enter username",autoComplete:"username"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-2",children:"Password"}),s.jsx("input",{type:"password",value:d.password,onChange:E=>f({...d,password:E.target.value}),className:"w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:"Enter password",autoComplete:"new-password",minLength:6})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-white mb-2",children:"Role"}),s.jsxs("select",{value:d.role,onChange:E=>f({...d,role:E.target.value}),className:"w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",children:[s.jsx("option",{value:"user",children:"User"}),s.jsx("option",{value:"admin",children:"Admin"})]})]})]}),s.jsxs("div",{className:"mt-6 flex justify-end gap-3",children:[s.jsx(Se,{variant:"secondary",onClick:()=>{a(!1),f({username:"",password:"",role:"user"})},children:"Cancel"}),s.jsx(Se,{onClick:v,disabled:x.isPending||!d.username||!d.password,children:x.isPending?s.jsxs(s.Fragment,{children:[s.jsx(ht,{className:"w-4 h-4 animate-spin"}),"Creating..."]}):s.jsxs(s.Fragment,{children:[s.jsx(Jn,{className:"w-4 h-4"}),"Create User"]})})]})]})]})}),c!==null&&s.jsx(Un,{title:"Delete User",message:"Are you sure you want to delete this user? This action cannot be undone.",confirmText:"Delete User",variant:"danger",onConfirm:()=>{b.mutate(c),u(null)},onCancel:()=>u(null)})]})}function ZKe(){const t=w.useRef(null),e=w.useRef(null),n=an(),[r,i]=w.useState(!1),a=w.useRef(new Set),o=w.useRef(null),l=w.useRef(new Map),c=w.useRef(null),u=w.useRef([]),d=w.useRef(!1),f=w.useRef(()=>{}),m=w.useCallback(()=>{if(d.current||u.current.length===0)return;d.current=!0;const M=()=>{const S=u.current.shift();S?requestAnimationFrame(()=>{f.current(S),u.current.length>0?setTimeout(M,16):d.current=!1}):d.current=!1};M()},[]),g=w.useCallback(()=>{if(t.current?.readyState===WebSocket.OPEN)return;const S=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.host}/api/v1/ws`,T=new WebSocket(S);let E=null;T.onopen=()=>{console.log("[WebSocket] Connected"),i(!0),E=window.setInterval(()=>{T.readyState===WebSocket.OPEN&&T.send(JSON.stringify({type:"ping"}))},3e4)},T.onmessage=k=>{try{const A=JSON.parse(k.data);A.type==="printer_status"&&A.printer_id!==void 0&&A.data?f.current(A):(u.current.push(A),m())}catch{}},T.onclose=k=>{console.log("[WebSocket] Closed",k.code,k.reason),E&&(clearInterval(E),E=null),i(!1),t.current=null,e.current=window.setTimeout(()=>{g()},3e3)},T.onerror=k=>{console.error("[WebSocket] Error",k),T.close()},t.current=T},[m]),x=w.useCallback((M,S)=>{const T=l.current.get(M)||{};l.current.set(M,{...T,...S}),c.current||(c.current=window.setTimeout(()=>{const E=new Map(l.current);l.current.clear(),c.current=null,requestAnimationFrame(()=>{E.forEach((k,A)=>{n.setQueryData(["printerStatus",A],D=>{const C={...D,...k};return C.wifi_signal==null&&D?.wifi_signal!=null&&(C.wifi_signal=D.wifi_signal),C})})})},100))},[n]),y=w.useCallback(M=>{a.current.add(M),o.current&&clearTimeout(o.current),o.current=window.setTimeout(()=>{const S=Array.from(a.current);a.current.clear(),o.current=null;let T=0;S.forEach(E=>{setTimeout(()=>{requestAnimationFrame(()=>{n.invalidateQueries({queryKey:[E]})})},T),T+=500})},3e3)},[n]),b=w.useCallback(M=>{switch(M.type){case"printer_status":M.printer_id!==void 0&&M.data&&x(M.printer_id,M.data);break;case"print_start":M.printer_id!==void 0&&n.invalidateQueries({queryKey:["printerStatus",M.printer_id]});break;case"print_complete":y("archives"),y("archiveStats");break;case"archive_created":y("archives"),y("archiveStats");break;case"archive_updated":y("archives");break}},[n,y,x]);w.useEffect(()=>{f.current=b},[b]),w.useEffect(()=>(g(),()=>{e.current&&clearTimeout(e.current),o.current&&clearTimeout(o.current),c.current&&clearTimeout(c.current),t.current&&t.current.close()}),[g]);const v=w.useCallback(M=>{t.current?.readyState===WebSocket.OPEN&&t.current.send(JSON.stringify(M))},[]);return{isConnected:r,sendMessage:v}}const JKe=new jle({defaultOptions:{queries:{staleTime:1e3*60,retry:1}}});function eXe({children:t}){return ZKe(),s.jsx(s.Fragment,{children:t})}function tXe({children:t}){const{authEnabled:e,loading:n,user:r}=Yd();return n?s.jsx("div",{className:"min-h-screen flex items-center justify-center",children:"Loading..."}):e&&!r?s.jsx(ow,{to:"/login",replace:!0}):s.jsx(s.Fragment,{children:t})}function nXe({children:t}){const{authEnabled:e,loading:n,user:r}=Yd();return n?s.jsx("div",{className:"min-h-screen flex items-center justify-center",children:"Loading..."}):e?r?r.role!=="admin"?s.jsx(ow,{to:"/",replace:!0}):s.jsx(s.Fragment,{children:t}):s.jsx(ow,{to:"/login",replace:!0}):s.jsx(s.Fragment,{children:t})}function rXe({children:t}){const{authEnabled:e,loading:n}=Yd();return n?s.jsx("div",{className:"min-h-screen flex items-center justify-center",children:"Loading..."}):e?s.jsx(ow,{to:"/login",replace:!0}):s.jsx(s.Fragment,{children:t})}function iXe(){return s.jsx(nfe,{children:s.jsx(pfe,{children:s.jsx(Ple,{client:JKe,children:s.jsx(ofe,{children:s.jsx(Zoe,{children:s.jsxs(Aoe,{children:[s.jsx(Vi,{path:"/setup",element:s.jsx(rXe,{children:s.jsx(YKe,{})})}),s.jsx(Vi,{path:"/login",element:s.jsx(XKe,{})}),s.jsx(Vi,{path:"/camera/:printerId",element:s.jsx(HKe,{})}),s.jsxs(Vi,{element:s.jsx(tXe,{children:s.jsx(eXe,{children:s.jsx(dfe,{})})}),children:[s.jsx(Vi,{index:!0,element:s.jsx(Yke,{})}),s.jsx(Vi,{path:"archives",element:s.jsx(FGe,{})}),s.jsx(Vi,{path:"queue",element:s.jsx(wqe,{})}),s.jsx(Vi,{path:"stats",element:s.jsx(Lqe,{})}),s.jsx(Vi,{path:"profiles",element:s.jsx(xKe,{})}),s.jsx(Vi,{path:"maintenance",element:s.jsx(wKe,{})}),s.jsx(Vi,{path:"projects",element:s.jsx(MKe,{})}),s.jsx(Vi,{path:"projects/:id",element:s.jsx(jKe,{})}),s.jsx(Vi,{path:"files",element:s.jsx(UKe,{})}),s.jsx(Vi,{path:"settings",element:s.jsx(nXe,{children:s.jsx(sKe,{})})}),s.jsx(Vi,{path:"users",element:s.jsx(QKe,{})}),s.jsx(Vi,{path:"system",element:s.jsx(KKe,{})}),s.jsx(Vi,{path:"external/:id",element:s.jsx(VKe,{})})]})]})})})})})})}Pae.createRoot(document.getElementById("root")).render(s.jsx(w.StrictMode,{children:s.jsx(iXe,{})})); diff --git a/static/index.html b/static/index.html index 43c9ba98d..1c9360c1b 100644 --- a/static/index.html +++ b/static/index.html @@ -23,7 +23,7 @@ - +