diff --git a/frontend/src/pages/SystemInfoPage.tsx b/frontend/src/pages/SystemInfoPage.tsx index 92b7fe9c0..3633a0030 100644 --- a/frontend/src/pages/SystemInfoPage.tsx +++ b/frontend/src/pages/SystemInfoPage.tsx @@ -105,8 +105,9 @@ export function SystemInfoPage() { const handleToggleDebugLogging = async () => { setDebugToggling(true); try { - await supportApi.setDebugLogging(!debugLoggingState?.enabled); - queryClient.invalidateQueries({ queryKey: ['debugLogging'] }); + const newState = await supportApi.setDebugLogging(!debugLoggingState?.enabled); + // Immediately update the cache with the new state (includes fresh enabled_at timestamp) + queryClient.setQueryData(['debugLogging'], newState); } catch (err) { console.error('Failed to toggle debug logging:', err); } finally { diff --git a/static/assets/index-BXGONv-e.js b/static/assets/index-CCnKMi2S.js similarity index 99% rename from static/assets/index-BXGONv-e.js rename to static/assets/index-CCnKMi2S.js index 0038dc226..d6ce00880 100644 --- a/static/assets/index-BXGONv-e.js +++ b/static/assets/index-CCnKMi2S.js @@ -8660,4 +8660,4 @@ Accuracy: ${t.time_accuracy.toFixed(0)}%`:t.actual_time_seconds?`Actual: ${Vx(t. `+n+"]";if(typeof t=="object"){const r=Object.entries(t);return r.length===0?"{}":`{ `+r.map(([a,s])=>n+" "+JSON.stringify(a)+": "+Zj(s,e+1)).join(`, `)+` -`+n+"}"}return String(t)}function NWe({setting:t,onClose:e,onDeleted:n,onDuplicate:r,onEdit:i}){const{showToast:a}=vr(),s=dn(),[l,c]=S.useState(!1),{data:u,isLoading:d}=pt({queryKey:["cloudSettingDetail",t.setting_id],queryFn:()=>_e.getCloudSettingDetail(t.setting_id)}),f=_t({mutationFn:()=>_e.deleteCloudSetting(t.setting_id),onSuccess:()=>{a("Preset deleted"),s.invalidateQueries({queryKey:["cloudSettings"]}),n()},onError:b=>a(b.message,"error")}),g=wb(t.setting_id),p=qL(t.name,u?.setting?.inherits);return o.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:o.jsx(At,{className:"w-full max-w-3xl max-h-[90vh] flex flex-col overflow-hidden",children:o.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[o.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("h2",{className:"text-xl font-semibold text-white truncate",children:t.name}),g&&o.jsx("span",{className:"px-2 py-0.5 text-xs font-medium bg-bambu-green/20 text-bambu-green rounded-full",children:"Editable"})]}),o.jsxs("div",{className:"flex items-center gap-2 mt-1 text-sm text-bambu-gray",children:[o.jsxs("span",{className:"capitalize",children:[t.type," preset"]}),p.printer&&o.jsxs(o.Fragment,{children:[o.jsx("span",{children:"•"}),o.jsx("span",{children:p.printer})]})]})]}),o.jsx("button",{onClick:e,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:o.jsx(Wt,{className:"w-5 h-5"})})]}),o.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto p-4",children:d?o.jsx("div",{className:"flex items-center justify-center py-16",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?o.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:Zj(u)}):o.jsx("div",{className:"text-center py-16 text-bambu-gray",children:"Failed to load preset details"})}),l?o.jsxs("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary bg-red-500/5",children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 text-red-400",children:[o.jsx(qn,{className:"w-5 h-5"}),o.jsx("span",{className:"font-medium",children:"Delete this preset?"})]}),o.jsxs("p",{className:"text-sm text-bambu-gray mb-4",children:['This will permanently delete "',t.name,'" from Bambu Cloud. This cannot be undone.']}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:()=>c(!1),disabled:f.isPending,className:"flex-1",children:"Cancel"}),o.jsxs(Pe,{variant:"danger",onClick:()=>f.mutate(),disabled:f.isPending,className:"flex-1",children:[f.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):o.jsx(Rn,{className:"w-4 h-4"}),"Delete"]})]})]}):o.jsx("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary",children:o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:e,className:"flex-1",children:"Close"}),o.jsxs(Pe,{variant:"secondary",onClick:r,children:[o.jsx(vl,{className:"w-4 h-4"}),"Duplicate"]}),g&&o.jsxs(o.Fragment,{children:[o.jsxs(Pe,{variant:"secondary",onClick:i,disabled:d||!u,children:[o.jsx(Bc,{className:"w-4 h-4"}),"Edit"]}),o.jsx(Pe,{variant:"danger",onClick:()=>c(!0),children:o.jsx(Rn,{className:"w-4 h-4"})})]})]})})]})})})}function Qte(){try{const t=localStorage.getItem("bambusy_preset_templates");return t?JSON.parse(t):[]}catch{return[]}}function Jte(t){localStorage.setItem("bambusy_preset_templates",JSON.stringify(t))}function CWe({onClose:t,onApply:e}){const{showToast:n}=vr(),[r,i]=S.useState(Qte),[a,s]=S.useState("all"),[l,c]=S.useState(null),[u,d]=S.useState(""),[f,g]=S.useState(""),[p,b]=S.useState("{}"),[v,x]=S.useState(null),[y,E]=S.useState(null),w=a==="all"?r:r.filter(I=>I.type===a),A=I=>{i(I),Jte(I)},T=I=>{const q=r.filter(R=>R.id!==I);A(q),E(null),n("Template deleted")},C=I=>{c(I.id),d(I.name),g(I.description),b(JSON.stringify(I.settings,null,2)),x(null)},N=()=>{if(!(!l||!u.trim()))try{const I=JSON.parse(p),q=r.map(R=>R.id===l?{...R,name:u.trim(),description:f.trim(),settings:I}:R);A(q),c(null),n("Template updated")}catch(I){x(I.message)}},O=()=>{c(null),d(""),g(""),b("{}"),x(null)},k=I=>{const q=r.map(R=>R.id===I?{...R,showInModal:!R.showInModal}:R);A(q)},j={filament:{label:"Filament",icon:uR,color:"text-amber-400"},print:{label:"Process",icon:aM,color:"text-blue-400"},printer:{label:"Printer",icon:qi,color:"text-purple-400"}},B=y?r.find(I=>I.id===y):null;return S.useEffect(()=>{const I=q=>{q.key==="Escape"&&(y?E(null):l?O():t())};return window.addEventListener("keydown",I),()=>window.removeEventListener("keydown",I)},[y,l,t]),o.jsxs("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:[B&&o.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-60",children:o.jsx(At,{className:"w-full max-w-md",children:o.jsxs(Tt,{className:"p-6",children:[o.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[o.jsx("div",{className:"p-2 bg-red-500/20 rounded-lg",children:o.jsx(qn,{className:"w-6 h-6 text-red-400"})}),o.jsxs("div",{children:[o.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Template"}),o.jsx("p",{className:"text-sm text-bambu-gray",children:"This action cannot be undone"})]})]}),o.jsxs("p",{className:"text-white mb-6",children:['Are you sure you want to delete "',o.jsx("span",{className:"font-medium",children:B.name}),'"?']}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:()=>E(null),className:"flex-1",children:"Cancel"}),o.jsxs(Pe,{onClick:()=>T(y),className:"flex-1 bg-red-500 hover:bg-red-600",children:[o.jsx(Rn,{className:"w-4 h-4"}),"Delete"]})]})]})})}),o.jsx(At,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",children:o.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[o.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(O0,{className:"w-5 h-5 text-amber-400"}),"Quick Templates"]}),o.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:o.jsx(Wt,{className:"w-5 h-5"})})]}),o.jsxs("div",{className:"flex items-center gap-2 p-4 border-b border-bambu-dark-tertiary",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Type:"}),["all","filament","print","printer"].map(I=>o.jsx("button",{onClick:()=>s(I),className:`px-3 py-1 text-sm rounded-lg transition-colors ${a===I?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:I==="all"?"All":j[I].label},I))]}),o.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:w.length===0?o.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[o.jsx(O0,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),o.jsx("p",{children:"No templates yet"}),o.jsx("p",{className:"text-sm mt-1",children:"Create templates from the preset editor"})]}):o.jsx("div",{className:"space-y-2",children:w.map(I=>{const q=j[I.type],R=q.icon;return l===I.id?o.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg border border-bambu-green",children:[o.jsxs("div",{className:"grid grid-cols-2 gap-3 mb-3",children:[o.jsx("input",{type:"text",value:u,onChange:W=>d(W.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}),o.jsx("input",{type:"text",value:f,onChange:W=>g(W.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"})]}),o.jsxs("div",{className:"mb-3",children:[o.jsx("label",{className:"text-xs text-bambu-gray mb-1 block",children:"Settings (JSON)"}),o.jsx("textarea",{value:p,onChange:W=>{b(W.target.value),x(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 ${v?"border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`}),v&&o.jsx("p",{className:"text-xs text-red-400 mt-1",children:v})]}),o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{size:"sm",onClick:N,disabled:!u.trim(),children:[o.jsx(Ts,{className:"w-4 h-4"}),"Save"]}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:O,children:"Cancel"})]})]},I.id):o.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:[o.jsx(R,{className:`w-5 h-5 ${q.color} flex-shrink-0`}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("p",{className:"text-sm font-medium text-white",children:I.name}),o.jsx("p",{className:"text-xs text-bambu-gray truncate",children:I.description})]}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark px-2 py-1 bg-bambu-dark-secondary rounded",children:[Object.keys(I.settings).length," fields"]}),o.jsx("button",{onClick:()=>k(I.id),className:`p-1 transition-colors ${I.showInModal?"text-bambu-green hover:text-bambu-green/70":"text-bambu-gray hover:text-white"}`,title:I.showInModal?"Shown in modals":"Hidden in modals",children:I.showInModal?o.jsx(tf,{className:"w-4 h-4"}):o.jsx(nM,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>e(I),className:"px-3 py-1 text-xs bg-bambu-green/20 text-bambu-green rounded hover:bg-bambu-green/30 transition-colors",children:"Apply"}),o.jsx("button",{onClick:()=>C(I),className:"p-1 text-bambu-gray hover:text-white",title:"Edit",children:o.jsx(Bc,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>E(I.id),className:"p-1 text-bambu-gray hover:text-red-400",title:"Delete",children:o.jsx(Rn,{className:"w-4 h-4"})})]},I.id)})})})]})})]})}function ene({onClose:t,leftPreset:e,rightPreset:n,leftLabel:r,rightLabel:i}){const[a,s]=S.useState("changes"),[l,c]=S.useState(""),u=S.useMemo(()=>{const p=new Set([...Object.keys(e),...Object.keys(n)]),b=[];for(const v of p){if(v==="inherits"||v==="version")continue;const x=e[v],y=n[v],E=v in e,w=v in n;let A;!E&&w?A="added":E&&!w?A="removed":JSON.stringify(x)!==JSON.stringify(y)?A="changed":A="same",b.push({key:v,left:x,right:y,status:A})}return b.sort((v,x)=>{const y={changed:0,added:1,removed:2,same:3};return y[v.status]!==y[x.status]?y[v.status]-y[x.status]:v.key.localeCompare(x.key)})},[e,n]),d=S.useMemo(()=>{let p=[...u];if(a==="changes"&&(p=p.filter(b=>b.status!=="same")),l){const b=l.toLowerCase();p=p.filter(v=>v.key.toLowerCase().includes(b)||String(v.left).toLowerCase().includes(b)||String(v.right).toLowerCase().includes(b))}return p},[u,a,l]),f=S.useMemo(()=>({added:u.filter(p=>p.status==="added").length,removed:u.filter(p=>p.status==="removed").length,changed:u.filter(p=>p.status==="changed").length,same:u.filter(p=>p.status==="same").length}),[u]),g=p=>{if(p===void 0)return"—";if(p===null)return"null";if(Array.isArray(p))return p.length===0?"[]":p.length===1?String(p[0]):p.join(", ");if(typeof p=="object")return JSON.stringify(p);const b=String(p);if(b.includes("\\n")||b.length>100){const v=b.split("\\n").length;if(v>1)return`[${v} lines of G-code/script]`;if(b.length>100)return b.substring(0,100)+"…"}return b};return S.useEffect(()=>{const p=b=>{b.key==="Escape"&&t()};return window.addEventListener("keydown",p),()=>window.removeEventListener("keydown",p)},[t]),o.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:o.jsx(At,{className:"w-full max-w-4xl max-h-[85vh] flex flex-col overflow-hidden",children:o.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[o.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(dp,{className:"w-5 h-5 text-blue-400"}),"Compare Presets"]}),o.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:o.jsx(Wt,{className:"w-5 h-5"})})]}),o.jsxs("div",{className:"flex-shrink-0 grid grid-cols-2 gap-4 p-4 border-b border-bambu-dark-tertiary bg-bambu-dark",children:[o.jsxs("div",{className:"text-center",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Left:"}),o.jsx("p",{className:"text-white font-medium truncate",children:r})]}),o.jsxs("div",{className:"text-center",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Right:"}),o.jsx("p",{className:"text-white font-medium truncate",children:i})]})]}),o.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[o.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[o.jsx(fr,{className:"w-3.5 h-3.5"}),f.added," added"]}),o.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[o.jsx(n3,{className:"w-3.5 h-3.5"}),f.removed," removed"]}),o.jsxs("span",{className:"flex items-center gap-1 text-amber-400",children:[o.jsx(Zk,{className:"w-3.5 h-3.5"}),f.changed," changed"]}),o.jsxs("span",{className:"flex items-center gap-1 text-bambu-gray",children:[o.jsx(YA,{className:"w-3.5 h-3.5"}),f.same," same"]})]}),o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsxs("div",{className:"relative",children:[o.jsx(_l,{className:"absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),o.jsx("input",{type:"text",value:l,onChange:p=>c(p.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&&o.jsxs("div",{className:"flex rounded overflow-hidden border border-bambu-dark-tertiary",children:[o.jsx("button",{onClick:()=>s("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"}),o.jsx("button",{onClick:()=>s("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"})]})]})]}),o.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto",children:d.length===0?o.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[o.jsx(YA,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),o.jsx("p",{children:a==="changes"?"No differences found":"No fields match the search"})]}):o.jsxs("table",{className:"w-full",children:[o.jsx("thead",{className:"sticky top-0 bg-bambu-dark-secondary",children:o.jsxs("tr",{className:"text-sm text-bambu-gray border-b border-bambu-dark-tertiary",children:[o.jsx("th",{className:"text-left p-3 w-1/3",children:"Field"}),o.jsx("th",{className:"text-left p-3 w-1/3",children:r}),o.jsx("th",{className:"text-left p-3 w-1/3",children:i})]})}),o.jsx("tbody",{children:d.map(p=>{const b={added:"bg-green-500/10",removed:"bg-red-500/10",changed:"bg-amber-500/10",same:""}[p.status],v={added:o.jsx(fr,{className:"w-3.5 h-3.5 text-green-400"}),removed:o.jsx(n3,{className:"w-3.5 h-3.5 text-red-400"}),changed:o.jsx(Zk,{className:"w-3.5 h-3.5 text-amber-400"}),same:o.jsx(YA,{className:"w-3.5 h-3.5 text-bambu-gray-dark"})}[p.status];return o.jsxs("tr",{className:`border-b border-bambu-dark-tertiary ${b}`,children:[o.jsx("td",{className:"p-3",children:o.jsxs("div",{className:"flex items-center gap-2",children:[v,o.jsx("span",{className:"text-sm text-white font-mono",children:p.key})]})}),o.jsx("td",{className:"p-3",children:o.jsx("span",{className:`text-sm font-mono break-all ${p.status==="removed"?"text-red-300":p.status==="changed"?"text-white":"text-bambu-gray"}`,children:g(p.left)})}),o.jsx("td",{className:"p-3",children:o.jsx("span",{className:`text-sm font-mono break-all ${p.status==="added"?"text-green-300":p.status==="changed"?"text-white":"text-bambu-gray"}`,children:g(p.right)})})]},p.key)})})]})})]})})})}function kWe({onClose:t,initialData:e,allPresets:n}){const{showToast:r}=vr(),i=dn(),a=!!e?.setting_id,[s,l]=S.useState("common"),[c,u]=S.useState(e?.type||"filament"),[d,f]=S.useState(e?.name?a?e.name:`${e.name} (Copy)`:""),[g,p]=S.useState(e?.base_id||""),[b,v]=S.useState(""),[x,y]=S.useState(e?.setting||{inherits:""}),[E,w]=S.useState(JSON.stringify(e?.setting||{inherits:""},null,2)),[A,T]=S.useState(null),[C,N]=S.useState(""),[O,k]=S.useState(!1),[j,B]=S.useState(""),[I,q]=S.useState(!1),[R,W]=S.useState(Qte),[K,G]=S.useState(!1),[J,ee]=S.useState(""),[Y,F]=S.useState(""),[$,ne]=S.useState(!0),[oe,se]=S.useState(null),[pe,Z]=S.useState(!1),Me=S.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer})[c]||[],[n,c]),he=S.useMemo(()=>Me.filter(ae=>wb(ae.setting_id)),[Me]),{data:ue}=pt({queryKey:["cloudFields",c],queryFn:()=>_e.getCloudFields(c==="print"?"process":c),staleTime:1e3*60*60}),{data:Ae}=pt({queryKey:["allPresetDetails",c,he.map(ae=>ae.setting_id).join(",")],queryFn:async()=>{const ae={};for(let He=0;He{try{const rt=await _e.getCloudSettingDetail(ge.setting_id);return{id:ge.setting_id,detail:rt}}catch{return null}}))).forEach(ge=>{ge&&(ae[ge.id]=ge.detail)})}return ae},enabled:he.length>0,staleTime:1e3*60*10}),{data:fe,isLoading:je}=pt({queryKey:["cloudSettingDetail",g],queryFn:()=>_e.getCloudSettingDetail(g),enabled:!!g});S.useEffect(()=>{s!=="json"&&w(JSON.stringify(x,null,2))},[x,s]);const z=S.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer}[c]||[]).filter($e=>!wb($e.setting_id)).sort(($e,He)=>$e.name.localeCompare(He.name)),[n,c]);S.useEffect(()=>{if(!g)return;const ae=z.find($e=>$e.setting_id===g);ae&&(v(ae.name),a||(y({inherits:ae.name}),w(JSON.stringify({inherits:ae.name},null,2))))},[g,z,a]);const H=S.useMemo(()=>{const ae=ue?.fields||[],$e=new Set(ae.map(ge=>ge.key)),He=new Set,Ze=new Set(["inherits","updated_time","compatible_printers","compatible_prints"]);Ae&&Object.values(Ae).forEach(ge=>{ge?.setting&&Object.keys(ge.setting).forEach(rt=>{!$e.has(rt)&&!Ze.has(rt)&&He.add(rt)})}),Object.keys(x).forEach(ge=>{!$e.has(ge)&&!Ze.has(ge)&&He.add(ge)});const ot=Array.from(He).sort().map(ge=>({key:ge,label:ge.replace(/_/g," ").replace(/\b\w/g,rt=>rt.toUpperCase()),type:"text",category:"discovered",description:"Discovered from presets"}));return[...ae,...ot]},[ue,Ae,x]),re=H.filter(ae=>ae.label.toLowerCase().includes(C.toLowerCase())||ae.key.toLowerCase().includes(C.toLowerCase())),V=()=>{if(j.trim()){const ae=j.trim().toLowerCase().replace(/\s+/g,"_");Q(ae,""),B(""),q(!1),r(`Field "${ae}" added`)}},Q=(ae,$e)=>{y(He=>{const Ze={...He};return $e===""||$e===void 0?delete Ze[ae]:Ze[ae]=$e,Ze})},le=ae=>{y($e=>({...$e,...ae.settings})),se(ae.name),r("Template applied")},ye=()=>{if(!J.trim())return;const ae={...x};if(delete ae.inherits,Object.keys(ae).length===0){r("No overrides to save","error");return}const $e={id:Date.now().toString(),name:J.trim(),description:Y.trim()||"Custom template",type:c,settings:ae,showInModal:$},He=[...R,$e];W(He),Jte(He),G(!1),ee(""),F(""),ne(!0),r("Template saved")},Ne=S.useMemo(()=>R.filter(ae=>ae.type===c&&ae.showInModal),[c,R]),Ee=ae=>{w(ae);try{const $e=JSON.parse(ae);y($e),T(null)}catch($e){T($e.message)}},U=ae=>{ae.preventDefault(),k(!1);const $e=ae.dataTransfer.files[0];if($e&&$e.name.endsWith(".json")){const He=new FileReader;He.onload=Ze=>{try{const ot=Ze.target?.result,ge=JSON.parse(ot),rt=ge.setting||ge;y(xe=>({...xe,...rt})),w(JSON.stringify({...x,...rt},null,2)),r("File imported successfully")}catch{r("Invalid JSON file","error")}},He.readAsText($e)}},L=_t({mutationFn:()=>{const ae={...x},$e=c==="filament"?"filament_settings_id":c==="print"?"print_settings_id":"printer_settings_id";ae[$e]=`"${d}"`;const He={type:c,name:d,base_id:g,setting:ae};return _e.createCloudSetting(He)},onSuccess:async()=>{r("Preset created successfully"),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ae=>r(ae.message,"error")}),te=_t({mutationFn:()=>{if(!e?.setting_id)throw new Error("No setting ID for update");return _e.updateCloudSetting(e.setting_id,{name:d,setting:x})},onSuccess:async()=>{r("Preset updated successfully"),i.removeQueries({queryKey:["cloudSettingDetail"]}),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ae=>r(ae.message,"error")}),ve=a?te:L,Se=fe?.setting?.inherits,be=Se?z.find(ae=>ae.name===Se):void 0,{data:Ke}=pt({queryKey:["cloudSettingDetail",be?.setting_id],queryFn:()=>_e.getCloudSettingDetail(be.setting_id),enabled:!!be?.setting_id}),Ge=S.useMemo(()=>{const ae=Ke?.setting||{},$e=fe?.setting||{},He=g&&Ae?.[g]?.setting?Ae[g].setting:{};return{...ae,...$e,...He}},[g,fe,Ke,Ae]),qe=ae=>{if(ae==null)return"";if(Array.isArray(ae)){const $e=[...new Set(ae.map(He=>String(He)))];return $e.length===1?$e[0]:ae.join(", ")}return String(ae)},lt=ae=>{const $e=x[ae.key],He=Ge[ae.key],Ze=qe(He),ot=je?"Loading...":Ze||"",ge="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(ae.type==="boolean"){const rt=$e==="1"||$e===void 0&&He==="1";return o.jsx("button",{type:"button",onClick:()=>Q(ae.key,$e==="1"?"0":"1"),className:`w-8 h-5 rounded-full transition-colors ${rt?"bg-bambu-green":"bg-bambu-dark-tertiary"}`,children:o.jsx("div",{className:`w-4 h-4 rounded-full bg-white shadow transition-transform ${rt?"translate-x-3.5":"translate-x-0.5"}`})})}return ae.type==="select"?o.jsxs("select",{value:$e||"",onChange:rt=>Q(ae.key,rt.target.value),className:ge,children:[o.jsx("option",{value:"",children:ot}),ae.options?.map(rt=>o.jsx("option",{value:rt.value,children:rt.label},rt.value))]}):o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("input",{type:ae.type==="number"?"number":"text",value:$e!==void 0?String($e):"",onChange:rt=>Q(ae.key,rt.target.value),step:ae.step,min:ae.min,max:ae.max,placeholder:ot,className:ge}),ae.unit&&o.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:ae.unit})]})},me=S.useMemo(()=>fe?.setting?fe.setting:{},[fe]);return o.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onDragOver:ae=>{ae.preventDefault(),k(!0)},onDragLeave:()=>k(!1),onDrop:U,children:[pe&&g&&o.jsx(ene,{onClose:()=>Z(!1),leftPreset:me,rightPreset:x,leftLabel:`Base: ${b||g}`,rightLabel:`Current: ${d||"New Preset"}`}),o.jsx(At,{className:"w-full max-w-6xl max-h-[90vh] flex flex-col",children:o.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[o.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("div",{children:[o.jsx("h2",{className:"text-xl font-semibold text-white",children:a?"Edit Preset":e?"Duplicate Preset":"Create New Preset"}),o.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize settings for your new preset"})]}),o.jsxs("div",{className:"flex items-center gap-2",children:[g&&o.jsxs("button",{onClick:()=>Z(!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:[o.jsx(dp,{className:"w-4 h-4"}),"Compare"]}),o.jsx("button",{onClick:t,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:o.jsx(Wt,{className:"w-5 h-5"})})]})]}),O&&o.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:o.jsxs("div",{className:"text-center",children:[o.jsx(ua,{className:"w-12 h-12 text-bambu-green mx-auto mb-2"}),o.jsx("p",{className:"text-bambu-green font-medium",children:"Drop JSON file to import"})]})}),o.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary space-y-3",children:[o.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Type"}),o.jsxs("select",{value:c,onChange:ae=>{u(ae.target.value),p("")},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:[o.jsx("option",{value:"filament",children:"Filament"}),o.jsx("option",{value:"print",children:"Process"}),o.jsx("option",{value:"printer",children:"Printer"})]})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Base Preset"}),o.jsxs("select",{value:g,onChange:ae=>p(ae.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:[o.jsx("option",{value:"",children:"Select base preset..."}),z.map(ae=>o.jsx("option",{value:ae.setting_id,children:ae.name},ae.setting_id))]})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Preset Name"}),o.jsx("input",{type:"text",value:d,onChange:ae=>f(ae.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"})]})]}),b&&o.jsx("div",{className:"text-xs text-bambu-gray",children:o.jsxs("p",{className:"flex items-center gap-1",children:[o.jsx(go,{className:"w-3 h-3 text-bambu-green"}),"Inherits from: ",o.jsx("span",{className:"text-white",children:b}),je&&o.jsx(xt,{className:"w-3 h-3 animate-spin ml-1"})]})})]}),o.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary",children:[o.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 ${s==="common"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[o.jsx(TU,{className:"w-4 h-4"}),"Common"]}),o.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 ${s==="fields"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[o.jsx(xR,{className:"w-4 h-4"}),"All Fields"]}),o.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 ${s==="json"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[o.jsx(H9,{className:"w-4 h-4"}),"JSON",A&&o.jsx(Sa,{className:"w-3 h-3 text-red-400"})]}),o.jsx("div",{className:"flex-1"}),o.jsxs("button",{onClick:()=>{const ae={name:d,type:c,base_id:g,setting:x},$e=new Blob([JSON.stringify(ae,null,2)],{type:"application/json"}),He=URL.createObjectURL($e),Ze=document.createElement("a");Ze.href=He,Ze.download=`${d||"preset"}.json`,document.body.appendChild(Ze),Ze.click(),document.body.removeChild(Ze),URL.revokeObjectURL(He),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:[o.jsx(Wi,{className:"w-4 h-4"}),"Export"]}),o.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:[o.jsx(ua,{className:"w-4 h-4"}),"Import"]}),o.jsx("input",{id:"file-import",type:"file",accept:".json",className:"hidden",onChange:ae=>{const $e=ae.target.files?.[0];if($e){const He=new FileReader;He.onload=Ze=>{try{const ot=JSON.parse(Ze.target?.result),ge=ot.setting||ot;y(rt=>({...rt,...ge})),r("File imported")}catch{r("Invalid JSON","error")}},He.readAsText($e)}}})]}),o.jsxs("div",{className:"flex-1 overflow-y-auto p-4",children:[s==="common"&&o.jsxs("div",{className:"space-y-6",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:[o.jsx(O0,{className:"w-4 h-4 text-amber-400"}),"Quick Templates"]}),Object.keys(x).filter(ae=>ae!=="inherits").length>0&&o.jsxs("button",{onClick:()=>G(!K),className:"text-xs text-bambu-gray hover:text-white flex items-center gap-1 transition-colors",children:[o.jsx(Ts,{className:"w-3 h-3"}),"Save as template"]})]}),K&&o.jsxs("div",{className:"mb-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"grid grid-cols-2 gap-2 mb-2",children:[o.jsx("input",{type:"text",value:J,onChange:ae=>ee(ae.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}),o.jsx("input",{type:"text",value:Y,onChange:ae=>F(ae.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"})]}),o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{size:"sm",onClick:ye,disabled:!J.trim(),children:[o.jsx(Ts,{className:"w-3 h-3"}),"Save"]}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>G(!1),children:"Cancel"})]}),o.jsxs("button",{onClick:()=>ne(!$),className:`flex items-center gap-1.5 text-xs transition-colors ${$?"text-bambu-green":"text-bambu-gray hover:text-white"}`,children:[$?o.jsx(tf,{className:"w-3.5 h-3.5"}):o.jsx(nM,{className:"w-3.5 h-3.5"}),$?"Show in modals":"Hidden in modals"]})]})]}),oe&&o.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:[o.jsx(go,{className:"w-4 h-4 text-bambu-green"}),o.jsxs("span",{className:"text-sm text-bambu-green",children:["Template applied: ",o.jsx("span",{className:"font-medium",children:oe})]}),o.jsx("button",{onClick:()=>se(null),className:"ml-auto text-bambu-green/70 hover:text-bambu-green",children:o.jsx(Wt,{className:"w-4 h-4"})})]}),o.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[Ne.map(ae=>o.jsxs("button",{onClick:()=>le(ae),className:"p-3 text-left bg-bambu-dark border border-bambu-dark-tertiary rounded-lg hover:border-bambu-gray-dark transition-colors",children:[o.jsx("p",{className:"text-sm font-medium text-white",children:ae.name}),o.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:ae.description})]},ae.id)),Ne.length===0&&o.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."})]}),o.jsx("p",{className:"text-xs text-bambu-gray-dark mt-2 text-center",children:"Manage templates via the Templates button on the main page"})]}),o.jsxs("div",{children:[o.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Common Settings"}),o.jsx("div",{className:"grid grid-cols-2 gap-x-6 gap-y-3",children:H.slice(0,10).map(ae=>o.jsxs("div",{className:"flex items-center justify-between gap-4",children:[o.jsx("label",{className:"text-sm text-bambu-gray flex-shrink-0",children:ae.label}),o.jsx("div",{className:"w-48",children:lt(ae)})]},ae.key))})]}),Object.keys(x).length>1&&o.jsxs("div",{children:[o.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Current Overrides"}),o.jsx("div",{className:"flex flex-wrap gap-2",children:Object.entries(x).filter(([ae])=>ae!=="inherits").map(([ae,$e])=>o.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-green/10 text-bambu-green text-xs rounded",children:[ae,": ",String($e).slice(0,20),o.jsx("button",{onClick:()=>Q(ae,void 0),className:"hover:text-white",children:o.jsx(Wt,{className:"w-3 h-3"})})]},ae))})]})]}),s==="fields"&&o.jsxs("div",{className:"grid grid-cols-2 gap-6",style:{height:"400px"},children:[o.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[o.jsx("h3",{className:"text-sm font-medium text-white",children:"Available Fields"}),o.jsx("span",{className:"text-xs text-bambu-gray",children:Ae?`${H.length} fields`:"Loading..."})]}),o.jsxs("div",{className:"relative mb-3 flex-shrink-0",children:[o.jsx(_l,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),o.jsx("input",{type:"text",value:C,onChange:ae=>N(ae.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"})]}),o.jsxs("div",{className:"flex-1 overflow-y-auto space-y-1 pr-2 min-h-0",children:[re.filter(ae=>!(ae.key in x)).map(ae=>{const $e=Ge[ae.key],He=qe($e);return o.jsxs("div",{onClick:()=>{y(Ze=>({...Ze,[ae.key]:He||""}))},className:"flex items-center justify-between gap-2 p-2 rounded-lg hover:bg-bambu-dark-tertiary transition-colors cursor-pointer group",children:[o.jsxs("div",{className:"min-w-0 flex-1",children:[o.jsx("p",{className:"text-sm text-white truncate",children:ae.label}),o.jsx("p",{className:"text-xs text-bambu-gray-dark truncate",children:ae.key})]}),o.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[He&&o.jsxs("span",{className:"text-xs text-bambu-gray bg-bambu-dark px-2 py-0.5 rounded max-w-32 truncate",title:He,children:[He.slice(0,20),He.length>20?"...":""]}),o.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:o.jsx(fr,{className:"w-4 h-4 text-bambu-gray group-hover:text-bambu-green transition-colors"})})]})]},ae.key)}),re.filter(ae=>!(ae.key in x)).length===0&&o.jsx("p",{className:"text-center text-bambu-gray py-4 text-sm",children:C?"No matching fields":"All fields added"})]}),o.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:I?o.jsxs("div",{className:"flex gap-2",children:[o.jsx("input",{type:"text",value:j,onChange:ae=>B(ae.target.value),onKeyDown:ae=>ae.key==="Enter"&&V(),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}),o.jsx(Pe,{size:"sm",onClick:V,disabled:!j.trim(),children:o.jsx(fr,{className:"w-4 h-4"})}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>{q(!1),B("")},children:o.jsx(Wt,{className:"w-4 h-4"})})]}):o.jsxs("button",{onClick:()=>q(!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:[o.jsx(fr,{className:"w-4 h-4"}),"Add custom field"]})})]}),o.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[o.jsx("h3",{className:"text-sm font-medium text-white",children:"Your Overrides"}),o.jsxs("span",{className:"text-xs text-bambu-gray",children:[Object.keys(x).filter(ae=>ae!=="inherits").length," fields"]})]}),o.jsxs("div",{className:"flex-1 overflow-y-auto space-y-2 pr-2 min-h-0",children:[Object.entries(x).filter(([ae])=>ae!=="inherits").map(([ae,$e])=>{const He=H.find(Ze=>Ze.key===ae);return o.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center justify-between mb-2",children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-sm font-medium text-white",children:He?.label||ae}),o.jsx("p",{className:"text-xs text-bambu-gray-dark",children:ae})]}),o.jsx("button",{onClick:()=>Q(ae,void 0),className:"p-1 text-bambu-gray hover:text-red-400 transition-colors",children:o.jsx(Wt,{className:"w-4 h-4"})})]}),He?lt(He):o.jsx("input",{type:"text",value:String($e),onChange:Ze=>Q(ae,Ze.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"})]},ae)}),Object.keys(x).filter(ae=>ae!=="inherits").length===0&&o.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[o.jsx(TU,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),o.jsx("p",{className:"text-sm",children:"No overrides yet"}),o.jsx("p",{className:"text-xs mt-1",children:"Click fields on the left to add them"})]})]}),Object.keys(x).filter(ae=>ae!=="inherits").length>0&&o.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:o.jsxs("button",{onClick:()=>{G(!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:[o.jsx(Ts,{className:"w-4 h-4"}),"Save as template"]})})]})]}),s==="json"&&o.jsxs("div",{className:"space-y-2",children:[A&&o.jsxs("div",{className:"flex items-center gap-2 text-red-400 text-sm",children:[o.jsx(Sa,{className:"w-4 h-4"}),A]}),o.jsx("textarea",{value:E,onChange:ae=>Ee(ae.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 ${A?"border-red-500 focus:border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`,spellCheck:!1}),o.jsx("p",{className:"text-xs text-bambu-gray",children:"Tip: Drag & drop a .json file anywhere on this modal to import settings"})]})]}),o.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:t,className:"flex-1",children:"Cancel"}),o.jsxs(Pe,{onClick:()=>ve.mutate(),disabled:ve.isPending||!d.trim()||!a&&!g||!!A,className:"flex-1",children:[ve.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):a?o.jsx(Ts,{className:"w-4 h-4"}):o.jsx(fr,{className:"w-4 h-4"}),a?"Save":e?"Duplicate":"Create"]})]})]})})]})}function PWe({settings:t,lastSyncTime:e,onRefresh:n,isRefreshing:r,printers:i}){const[a,s]=S.useState(""),[l,c]=S.useState("all"),[u,d]=S.useState("all"),[f,g]=S.useState("all"),[p,b]=S.useState("all"),[v,x]=S.useState("all"),[y,E]=S.useState("all"),[w,A]=S.useState(null),[T,C]=S.useState(!1),[N,O]=S.useState(!1),[k,j]=S.useState(null),[B,I]=S.useState(null),[q,R]=S.useState(null),[W,K]=S.useState(!1),[G,J]=S.useState([null,null]),[ee,Y]=S.useState(!1),[F,$]=S.useState(null),ne=dn(),oe=S.useMemo(()=>[...t.filament.map(re=>({...re,type:"filament"})),...t.printer.map(re=>({...re,type:"printer"})),...t.process.map(re=>({...re,type:"process"}))].map(re=>({...re,meta:qL(re.name)})),[t]),se=S.useMemo(()=>{const H=new Set,re=new Set,V=new Set;return oe.forEach(Q=>{Q.meta.nozzle&&H.add(Q.meta.nozzle),Q.meta.filamentType&&re.add(Q.meta.filamentType),Q.meta.layerHeight&&V.add(Q.meta.layerHeight)}),{printers:i.map(Q=>({id:Q.id.toString(),name:Q.name})),nozzles:Array.from(H).sort((Q,le)=>parseFloat(Q)-parseFloat(le)),filaments:Array.from(re).sort(),layerHeights:Array.from(V).sort((Q,le)=>parseFloat(Q)-parseFloat(le))}},[oe,i]),pe=S.useMemo(()=>f==="all"?null:i.find(re=>re.id.toString()===f)?.model||null,[f,i]),Z=S.useMemo(()=>oe.filter(H=>l==="all"||H.type===l).filter(H=>{if(u==="all")return!0;const re=wb(H.setting_id);return u==="custom"?re:!re}).filter(H=>{if(f==="all"||!pe)return!0;const re=H.meta.printer?.toLowerCase()||"",V=pe.toLowerCase();return re.includes(V)||V.includes(re)}).filter(H=>p==="all"||H.meta.nozzle===p).filter(H=>v==="all"||H.meta.filamentType===v).filter(H=>y==="all"||H.meta.layerHeight===y).filter(H=>a===""||H.name.toLowerCase().includes(a.toLowerCase())).sort((H,re)=>H.name.localeCompare(re.name)),[oe,l,u,f,pe,p,v,y,a]),Me=H=>{if(W){const re=G[0]?.setting_id===H.setting_id,V=G[1]?.setting_id===H.setting_id;if(re)J([G[1],null]);else if(V)J([G[0],null]);else if(!G[0])J([H,null]);else if(G[1]){if(G[0].type!==H.type)return;J([G[0],H])}else{if(G[0].type!==H.type)return;J([G[0],H])}}else A(H)},he=H=>{if(G[0]?.setting_id===H.setting_id)return 0;if(G[1]?.setting_id===H.setting_id)return 1},ue=async H=>{try{const re=await _e.getCloudSettingDetail(H.setting_id),V=H.type==="process"?"print":H.type;j({type:V,name:H.name,base_id:re.base_id||"GFSA00",setting:re.setting||{}}),A(null)}catch(re){console.error("Failed to fetch preset details for duplication:",re)}},Ae=async H=>{try{ne.removeQueries({queryKey:["cloudSettingDetail",H.setting_id]});const re=await _e.getCloudSettingDetail(H.setting_id),V=H.type==="process"?"print":H.type;I({type:V,name:H.name,base_id:re.base_id||"GFSA00",setting:re.setting||{},setting_id:H.setting_id}),A(null)}catch(re){console.error("Failed to fetch preset details for editing:",re)}},fe=()=>{c("all"),d("all"),g("all"),b("all"),x("all"),E("all"),s("")},je=l!=="all"||u!=="all"||f!=="all"||p!=="all"||v!=="all"||y!=="all"||a!=="",z=t.filament.length+t.printer.length+t.process.length;return o.jsxs(o.Fragment,{children:[o.jsxs("div",{className:"space-y-4 mb-6",children:[o.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[o.jsxs("div",{className:"relative flex-1",children:[o.jsx(_l,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),o.jsx("input",{type:"text",value:a,onChange:H=>s(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"})]}),o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{variant:W?"primary":"secondary",onClick:()=>{W?(K(!1),J([null,null])):K(!0)},children:[o.jsx(dp,{className:"w-4 h-4"}),W?"Cancel":"Compare"]}),o.jsxs(Pe,{variant:"secondary",onClick:()=>O(!0),children:[o.jsx(O0,{className:"w-4 h-4"}),"Templates"]}),o.jsxs(Pe,{variant:"secondary",onClick:n,disabled:r,children:[o.jsx(li,{className:`w-4 h-4 ${r?"animate-spin":""}`}),"Refresh"]}),o.jsxs(Pe,{onClick:()=>C(!0),children:[o.jsx(fr,{className:"w-4 h-4"}),"New Preset"]})]})]}),o.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[o.jsx(fR,{className:"w-4 h-4 text-bambu-gray"}),o.jsx(Ym,{label:"Type",value:l,options:[{value:"all",label:"All",count:z},{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)}),o.jsx(Ym,{label:"Owner",value:u,options:[{value:"all",label:"All"},{value:"custom",label:"My Presets"},{value:"builtin",label:"Built-in"}],onChange:H=>d(H)}),se.printers.length>0&&o.jsx(Ym,{label:"Printer",value:f,options:[{value:"all",label:"All"},...se.printers.map(H=>({value:H.id,label:H.name}))],onChange:g}),se.nozzles.length>0&&o.jsx(Ym,{label:"Nozzle",value:p,options:[{value:"all",label:"All"},...se.nozzles.map(H=>({value:H,label:H}))],onChange:b}),se.filaments.length>0&&(l==="all"||l==="filament")&&o.jsx(Ym,{label:"Filament",value:v,options:[{value:"all",label:"All"},...se.filaments.map(H=>({value:H,label:H}))],onChange:x}),se.layerHeights.length>0&&(l==="all"||l==="process")&&o.jsx(Ym,{label:"Layer",value:y,options:[{value:"all",label:"All"},...se.layerHeights.map(H=>({value:H,label:H}))],onChange:E}),je&&o.jsx("button",{onClick:fe,className:"px-3 py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:"Clear filters"})]})]}),W&&o.jsx("div",{className:"mb-4 p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{className:"flex items-center gap-4",children:[o.jsx(dp,{className:"w-5 h-5 text-blue-400"}),o.jsx("span",{className:"text-white font-medium",children:"Compare Mode"}),o.jsx("span",{className:"text-bambu-gray",children:G[0]?`Select another ${G[0].type} preset`:"Click two presets of the same type to compare"})]}),o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${G[0]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:G[0]?G[0].name:"1. Select first"}),o.jsx(Zk,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${G[1]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:G[1]?G[1].name:"2. Select second"})]}),G[0]&&G[1]&&o.jsxs(Pe,{size:"sm",onClick:async()=>{try{const[H,re]=await Promise.all([_e.getCloudSettingDetail(G[0].setting_id),_e.getCloudSettingDetail(G[1].setting_id)]);$([H.setting||{},re.setting||{}]),Y(!0)}catch{}},children:[o.jsx(dp,{className:"w-4 h-4"}),"Compare Now"]})]})]})}),o.jsxs("div",{className:"flex flex-wrap items-center gap-4 mb-4 text-sm text-bambu-gray",children:[e&&o.jsxs("div",{className:"flex items-center gap-1",children:[o.jsx(ur,{className:"w-3 h-3"}),"Last synced: ",EWe(e.toISOString())]}),o.jsxs("span",{children:["Showing ",Z.length," of ",z," presets"]}),o.jsxs("div",{className:"flex items-center gap-1",children:[o.jsx("span",{className:"w-1.5 h-1.5 rounded-full bg-bambu-green"}),o.jsx("span",{children:"= My preset (editable)"})]})]}),Z.length===0?o.jsxs("div",{className:"text-center py-16",children:[o.jsx(vd,{className:"w-12 h-12 text-bambu-gray-dark mx-auto mb-4"}),o.jsx("p",{className:"text-bambu-gray",children:"No presets found"}),je&&o.jsx("button",{onClick:fe,className:"mt-2 text-sm text-bambu-green hover:text-bambu-green-light",children:"Clear filters"})]}):o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[o.jsx(uR,{className:"w-4 h-4 text-amber-400"}),o.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Filament"}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",Z.filter(H=>H.type==="filament").length,")"]})]}),o.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[Z.filter(H=>H.type==="filament").map(H=>o.jsx(Ok,{setting:H,onClick:()=>Me(H),onDuplicate:()=>ue(H),compareMode:W,isCompareSelected:he(H)!==void 0,compareIndex:he(H),compareDisabled:W&&!!G[0]&&G[0].type!==H.type},H.setting_id)),Z.filter(H=>H.type==="filament").length===0&&o.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No filament presets"})]})]}),o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[o.jsx(aM,{className:"w-4 h-4 text-blue-400"}),o.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Process"}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",Z.filter(H=>H.type==="process").length,")"]})]}),o.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[Z.filter(H=>H.type==="process").map(H=>o.jsx(Ok,{setting:H,onClick:()=>Me(H),onDuplicate:()=>ue(H),compareMode:W,isCompareSelected:he(H)!==void 0,compareIndex:he(H),compareDisabled:W&&!!G[0]&&G[0].type!==H.type},H.setting_id)),Z.filter(H=>H.type==="process").length===0&&o.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No process presets"})]})]}),o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[o.jsx(qi,{className:"w-4 h-4 text-purple-400"}),o.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Printer"}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",Z.filter(H=>H.type==="printer").length,")"]})]}),o.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[Z.filter(H=>H.type==="printer").map(H=>o.jsx(Ok,{setting:H,onClick:()=>Me(H),onDuplicate:()=>ue(H),compareMode:W,isCompareSelected:he(H)!==void 0,compareIndex:he(H),compareDisabled:W&&!!G[0]&&G[0].type!==H.type},H.setting_id)),Z.filter(H=>H.type==="printer").length===0&&o.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No printer presets"})]})]})]}),w&&o.jsx(NWe,{setting:w,onClose:()=>A(null),onDeleted:()=>A(null),onDuplicate:()=>ue(w),onEdit:()=>Ae(w)}),(T||k||B||q)&&o.jsx(kWe,{onClose:()=>{C(!1),j(null),I(null),R(null)},initialData:B||k||(q?{type:q.type,name:"",base_id:"",setting:q.setting}:void 0),allPresets:t}),N&&o.jsx(CWe,{onClose:()=>O(!1),onApply:H=>{R({type:H.type,setting:H.settings}),O(!1)}}),ee&&F&&G[0]&&G[1]&&o.jsx(ene,{onClose:()=>{Y(!1),$(null)},leftPreset:F[0],rightPreset:F[1],leftLabel:G[0].name,rightLabel:G[1].name})]})}function jWe(){const t=dn(),{showToast:e}=vr(),[n,r]=S.useState("cloud"),[i,a]=S.useState(),{data:s,isLoading:l}=pt({queryKey:["cloudStatus"],queryFn:_e.getCloudStatus}),{data:c=[]}=pt({queryKey:["printers"],queryFn:_e.getPrinters}),{data:u,isLoading:d,refetch:f,dataUpdatedAt:g}=pt({queryKey:["cloudSettings"],queryFn:()=>_e.getCloudSettings(),enabled:!!s?.is_authenticated,retry:!1,staleTime:1e3*60*5});S.useEffect(()=>{g&&a(new Date(g))},[g]);const p=_t({mutationFn:_e.cloudLogout,onSuccess:()=>{t.invalidateQueries({queryKey:["cloudStatus"]}),t.removeQueries({queryKey:["cloudSettings"]}),e("Logged out")}}),b=()=>{t.invalidateQueries({queryKey:["cloudStatus"]})};return l?o.jsx("div",{className:"p-4 md:p-8 flex items-center justify-center min-h-[400px]",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):o.jsxs("div",{className:"p-6 lg:p-8",children:[o.jsxs("div",{className:"mb-6",children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:"Profiles"}),o.jsx("p",{className:"text-bambu-gray",children:"Manage your slicer presets and pressure advance calibrations"})]}),o.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary mb-6",children:[o.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:[o.jsx(tM,{className:"w-4 h-4"}),"Cloud Profiles"]}),o.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:[o.jsx(rM,{className:"w-4 h-4"}),"K-Profiles"]})]}),n==="cloud"&&o.jsxs(o.Fragment,{children:[s?.is_authenticated&&o.jsxs("div",{className:"flex items-center justify-between p-3 mb-6 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green animate-pulse"}),o.jsxs("span",{className:"text-sm text-bambu-gray",children:["Connected as ",o.jsx("span",{className:"text-white",children:s.email})]})]}),o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>p.mutate(),disabled:p.isPending,children:[o.jsx(Vce,{className:"w-4 h-4"}),"Logout"]})]}),s?.is_authenticated?d?o.jsx("div",{className:"flex items-center justify-center py-16",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?o.jsx(PWe,{settings:u,lastSyncTime:i,onRefresh:()=>f(),isRefreshing:d,printers:c}):o.jsxs("div",{className:"text-center py-16",children:[o.jsx("p",{className:"text-bambu-gray mb-4",children:"Failed to load profiles"}),o.jsx(Pe,{onClick:()=>f(),children:"Retry"})]}):o.jsx(AWe,{onSuccess:b})]}),n==="kprofiles"&&o.jsx(MWe,{}),o.jsx(TWe,{})]})}const i0={Droplet:uR,Flame:ece,Ruler:Rue,Sparkles:O0,Square:af,Cable:Zoe,Wrench:_d,Calendar:Jr,Timer:td,Cog:Tle,Fan:$9,Zap:sf,Wind:tW,Thermometer:sM,Layers:vd,Box:bd,Target:X9,RefreshCw:li,Settings:wR,Filter:fR,CircleDot:mle};function N0(t){return t&&i0[t]||_d}function Zm(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 Lk(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 RWe({item:t,onPerform:e,onToggle:n}){const r=N0(t.maintenance_type_icon),i=t.interval_type||"hours",s=(()=>{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 ${Zm(Math.abs(f),"days")}`:t.is_warning?`Due in ${Zm(f,"days")}`:`${Zm(f,"days")} left`}else return t.is_due?`Overdue by ${Zm(Math.abs(t.hours_until_due),"hours")}`:t.is_warning?`Due in ${Zm(t.hours_until_due,"hours")}`:`${Zm(t.hours_until_due,"hours")} left`};return o.jsx("div",{className:`rounded-xl border p-4 transition-all ${u()}`,children:o.jsxs("div",{className:"flex items-start gap-3",children:[o.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:[o.jsx(r,{className:`w-5 h-5 ${l()}`}),t.enabled&&(t.is_due||t.is_warning)&&o.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`})]}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("h3",{className:`font-medium truncate ${t.enabled?"text-white":"text-bambu-gray"}`,children:t.maintenance_type_name}),i==="days"&&o.jsx("span",{title:"Time-based interval",children:o.jsx(Jr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"})})]}),o.jsx("div",{className:"mt-2 mb-1.5",children:o.jsx("div",{className:"w-full h-1.5 bg-bambu-dark rounded-full overflow-hidden",children:o.jsx("div",{className:`h-full rounded-full transition-all duration-500 ${c()}`,style:{width:`${s}%`}})})}),o.jsxs("div",{className:`text-xs flex items-center gap-1 ${l()}`,children:[t.is_due&&o.jsx(qn,{className:"w-3 h-3"}),t.is_warning&&!t.is_due&&o.jsx(ur,{className:"w-3 h-3"}),!t.is_due&&!t.is_warning&&t.enabled&&o.jsx(go,{className:"w-3 h-3"}),d()]})]}),o.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[o.jsx(Vn,{checked:t.enabled,onChange:f=>n(t.id,f)}),o.jsxs(Pe,{size:"sm",variant:t.is_due?"primary":"secondary",onClick:()=>e(t.id),disabled:!t.enabled,className:"!px-3",children:[o.jsx(sg,{className:"w-3.5 h-3.5"}),"Reset"]})]})]})})}function DWe({overview:t,onPerform:e,onToggle:n,onSetHours:r}){const[i,a]=S.useState(!0),[s,l]=S.useState(!1),[c,u]=S.useState(t.total_print_hours.toFixed(1)),d=[...t.maintenance_items].sort((p,b)=>p.is_due&&!b.is_due?-1:!p.is_due&&b.is_due?1:p.is_warning&&!b.is_warning?-1:!p.is_warning&&b.is_warning?1:p.maintenance_type_id-b.maintenance_type_id),f=d.find(p=>p.enabled&&(p.is_due||p.is_warning)),g=()=>{const p=parseFloat(c);!isNaN(p)&&p>=0&&(r(t.printer_id,p),l(!1))};return o.jsxs(At,{className:"overflow-hidden",children:[o.jsxs("div",{className:"p-5",children:[o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{className:"flex items-center gap-4",children:[o.jsx("h2",{className:"text-xl font-semibold text-white",children:t.printer_name}),o.jsxs("div",{className:"flex items-center gap-2",children:[t.due_count>0&&o.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:[o.jsx(qn,{className:"w-3 h-3"}),t.due_count," overdue"]}),t.warning_count>0&&o.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:[o.jsx(ur,{className:"w-3 h-3"}),t.warning_count," due soon"]}),t.due_count===0&&t.warning_count===0&&o.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:[o.jsx(go,{className:"w-3 h-3"}),"All good"]})]})]}),o.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?o.jsx(Tb,{className:"w-4 h-4"}):o.jsx(Os,{className:"w-4 h-4"}),i?"Collapse":"Expand"]})]}),o.jsxs("div",{className:"flex items-center gap-6 mt-4",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2 bg-bambu-dark/50 rounded-lg",children:o.jsx(td,{className:"w-4 h-4 text-bambu-gray"})}),s?o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("input",{type:"number",value:c,onChange:p=>u(p.target.value),onKeyDown:p=>{p.key==="Enter"&&g(),p.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}),o.jsx("span",{className:"text-xs text-bambu-gray",children:"hours"}),o.jsx(Pe,{size:"sm",onClick:g,children:"Save"}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>l(!1),children:"Cancel"})]}):o.jsxs("button",{onClick:()=>{u(Math.round(t.total_print_hours).toString()),l(!0)},className:"group",children:[o.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",o.jsx(nf,{className:"w-3 h-3 text-bambu-gray group-hover:text-bambu-green"})]}),o.jsx("div",{className:"text-xs text-bambu-gray",children:"Total print time"})]})]}),o.jsx("div",{className:"w-px h-10 bg-bambu-dark-tertiary"}),f&&o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:`p-2 rounded-lg ${f.is_due?"bg-red-500/20":"bg-amber-500/20"}`,children:(()=>{const p=N0(f.maintenance_type_icon);return o.jsx(p,{className:`w-4 h-4 ${f.is_due?"text-red-400":"text-amber-400"}`})})()}),o.jsxs("div",{children:[o.jsx("div",{className:`text-sm font-medium ${f.is_due?"text-red-400":"text-amber-400"}`,children:f.maintenance_type_name}),o.jsx("div",{className:`text-xs ${f.is_due?"text-red-400/70":"text-amber-400/70"}`,children:f.is_due?"Overdue":"Due soon"})]})]})]})]}),i&&o.jsx(Tt,{className:"pt-0 border-t border-bambu-dark-tertiary",children:o.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-3 pt-4",children:d.map(p=>o.jsx(RWe,{item:p,onPerform:e,onToggle:n},p.id))})})]})}function OWe({overview:t,types:e,onUpdateInterval:n,onAddType:r,onUpdateType:i,onDeleteType:a,onAssignType:s,onRemoveItem:l}){const[c,u]=S.useState(null),[d,f]=S.useState(""),[g,p]=S.useState("hours"),[b,v]=S.useState(!1),[x,y]=S.useState(""),[E,w]=S.useState("100"),[A,T]=S.useState("hours"),[C,N]=S.useState("Wrench"),[O,k]=S.useState(new Set),[j,B]=S.useState(null),I=S.useMemo(()=>t?t.map(je=>({id:je.printer_id,name:je.printer_name})):[],[t]),q=je=>t?t.filter(z=>z.maintenance_items.some(H=>H.maintenance_type_id===je)).map(z=>({printerId:z.printer_id,printerName:z.printer_name,itemId:z.maintenance_items.find(H=>H.maintenance_type_id===je)?.id})):[],R=je=>{if(!t)return[];const z=new Set(q(je).map(H=>H.printerId));return I.filter(H=>!z.has(H.id))},[W,K]=S.useState(null),[G,J]=S.useState(""),[ee,Y]=S.useState(""),[F,$]=S.useState("hours"),[ne,oe]=S.useState("Wrench"),se=je=>{K(je),J(je.name),Y(je.default_interval_hours.toString()),$(je.interval_type||"hours"),oe(je.icon||"Wrench")},pe=()=>{W&&G.trim()&&parseFloat(ee)>0&&(i(W.id,{name:G.trim(),default_interval_hours:parseFloat(ee),interval_type:F,icon:ne}),K(null))},Z=(je,z,H)=>{const re=parseFloat(d);if(!isNaN(re)&&re>0){const V=Math.abs(re-z)<.01?null:re;n(je,{custom_interval_hours:V,custom_interval_type:g!==H?g:null})}u(null)},Me=je=>{je.preventDefault(),x.trim()&&parseFloat(E)>0&&O.size>0&&(r({name:x.trim(),default_interval_hours:parseFloat(E),interval_type:A,icon:C},Array.from(O)),y(""),w("100"),T("hours"),k(new Set),v(!1))},he=je=>{k(z=>{const H=new Set(z);return H.has(je)?H.delete(je):H.add(je),H})},ue=t?.map(je=>({printerId:je.printer_id,printerName:je.printer_name,items:je.maintenance_items.sort((z,H)=>z.maintenance_type_id-H.maintenance_type_id)})).sort((je,z)=>je.printerName.localeCompare(z.printerName))||[],Ae=e.filter(je=>je.is_system),fe=e.filter(je=>!je.is_system);return o.jsxs("div",{className:"space-y-8",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center justify-between mb-4",children:[o.jsxs("div",{children:[o.jsx("h2",{className:"text-lg font-semibold text-white",children:"Maintenance Types"}),o.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"System types and your custom maintenance tasks"})]}),o.jsxs(Pe,{onClick:()=>v(!b),children:[o.jsx(fr,{className:"w-4 h-4"}),"Add Custom Type"]})]}),b&&o.jsx(At,{className:"mb-6",children:o.jsx(Tt,{className:"py-4",children:o.jsxs("form",{onSubmit:Me,children:[o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[o.jsxs("div",{className:"lg:col-span-2",children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Name"}),o.jsx("input",{type:"text",value:x,onChange:je=>y(je.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})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Interval Type"}),o.jsxs("select",{value:A,onChange:je=>{T(je.target.value),je.target.value==="days"?w("30"):w("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:[o.jsx("option",{value:"hours",children:"Print Hours"}),o.jsx("option",{value:"days",children:"Calendar Days"})]})]}),o.jsxs("div",{children:[o.jsxs("label",{className:"block text-xs text-bambu-gray mb-1.5",children:["Interval (",A==="days"?"days":"hours",")"]}),o.jsx("input",{type:"number",value:E,onChange:je=>w(je.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"})]})]}),o.jsx("div",{className:"mt-4 flex items-end justify-between",children:o.jsxs("div",{children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Icon"}),o.jsx("div",{className:"flex gap-1",children:Object.keys(i0).map(je=>{const z=i0[je];return o.jsx("button",{type:"button",onClick:()=>N(je),className:`p-2 rounded-lg transition-colors ${C===je?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:o.jsx(z,{className:"w-4 h-4"})},je)})})]})}),o.jsxs("div",{className:"mt-4",children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Assign to Printers"}),o.jsx("div",{className:"flex flex-wrap gap-2",children:I.map(je=>o.jsx("button",{type:"button",onClick:()=>he(je.id),className:`px-3 py-1.5 rounded-lg text-sm transition-colors ${O.has(je.id)?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:je.name},je.id))}),O.size===0&&o.jsx("p",{className:"text-xs text-orange-400 mt-1",children:"Select at least one printer"})]}),o.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[o.jsx(Pe,{type:"button",variant:"secondary",onClick:()=>{v(!1),k(new Set)},children:"Cancel"}),o.jsx(Pe,{type:"submit",disabled:!x.trim()||O.size===0,children:"Add Type"})]})]})})}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3",children:[Ae.map(je=>{const z=N0(je.icon),H=je.interval_type||"hours";return o.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-dark-tertiary",children:o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2.5 bg-bambu-dark rounded-lg",children:o.jsx(z,{className:"w-5 h-5 text-bambu-gray"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("div",{className:"text-sm font-medium text-white truncate",children:je.name}),o.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[H==="days"?o.jsx(Jr,{className:"w-3 h-3"}):o.jsx(td,{className:"w-3 h-3"}),Lk(je.default_interval_hours,H)]})]})]})},je.id)}),fe.map(je=>{const z=N0(je.icon),H=je.interval_type||"hours";if(W?.id===je.id)return o.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green",children:o.jsxs("div",{className:"space-y-3",children:[o.jsx("input",{type:"text",value:G,onChange:ye=>J(ye.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}),o.jsxs("div",{className:"flex gap-2",children:[o.jsxs("select",{value:F,onChange:ye=>$(ye.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:[o.jsx("option",{value:"hours",children:"Print Hours"}),o.jsx("option",{value:"days",children:"Calendar Days"})]}),o.jsx("input",{type:"number",value:ee,onChange:ye=>Y(ye.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"})]}),o.jsx("div",{className:"flex flex-wrap gap-1",children:Object.keys(i0).map(ye=>{const Ne=i0[ye];return o.jsx("button",{type:"button",onClick:()=>oe(ye),className:`p-1.5 rounded transition-colors ${ne===ye?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:o.jsx(Ne,{className:"w-3.5 h-3.5"})},ye)})}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{size:"sm",onClick:pe,disabled:!G.trim(),children:"Save"}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>K(null),children:"Cancel"})]})]})},je.id);const V=q(je.id),Q=R(je.id),le=j===je.id;return o.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green/30",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2.5 bg-bambu-green/20 rounded-lg",children:o.jsx(z,{className:"w-5 h-5 text-bambu-green"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("span",{className:"text-sm font-medium text-white truncate",children:je.name}),o.jsx("span",{className:"px-1.5 py-0.5 bg-bambu-green/20 text-bambu-green text-[10px] font-medium rounded",children:"Custom"})]}),o.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[H==="days"?o.jsx(Jr,{className:"w-3 h-3"}):o.jsx(td,{className:"w-3 h-3"}),Lk(je.default_interval_hours,H)]})]}),o.jsxs("button",{onClick:()=>B(le?null:je.id),className:`px-2 py-1 rounded-lg border transition-colors flex items-center gap-1 ${V.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:`${V.length} printer(s) assigned - click to manage`,children:[o.jsx(qi,{className:"w-3 h-3"}),o.jsx("span",{className:"text-xs font-medium",children:V.length}),o.jsx(Os,{className:`w-3 h-3 transition-transform ${le?"rotate-180":""}`})]}),o.jsx("button",{onClick:()=>se(je),className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",children:o.jsx(nf,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>{confirm(`Delete "${je.name}"?`)&&a(je.id)},className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors",children:o.jsx(Rn,{className:"w-4 h-4"})})]}),le&&o.jsxs("div",{className:"mt-3 pt-3 border-t border-bambu-dark-tertiary",children:[o.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Assigned to printers:"}),V.length===0?o.jsx("p",{className:"text-xs text-orange-400",children:"No printers assigned"}):o.jsx("div",{className:"flex flex-wrap gap-1 mb-2",children:V.map(ye=>o.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark rounded text-xs text-white",children:[ye.printerName,o.jsx("button",{onClick:()=>ye.itemId&&l(ye.itemId),className:"hover:text-red-400 ml-1",title:"Remove from this printer",children:"×"})]},ye.printerId))}),Q.length>0&&o.jsxs("div",{className:"flex flex-wrap gap-1",children:[o.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:"Add:"}),Q.map(ye=>o.jsxs("button",{onClick:()=>s(ye.id,je.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:["+ ",ye.name]},ye.id))]})]})]},je.id)})]})]}),ue.length>0&&o.jsxs("div",{children:[o.jsxs("div",{className:"mb-4",children:[o.jsx("h2",{className:"text-lg font-semibold text-white",children:"Interval Overrides"}),o.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize intervals for specific printers"})]}),o.jsx("div",{className:"space-y-4",children:ue.map(je=>o.jsx(At,{children:o.jsxs(Tt,{className:"py-4",children:[o.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:je.printerName}),o.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2",children:je.items.map(z=>{const H=N0(z.maintenance_type_icon),re=e.find(Ne=>Ne.id===z.maintenance_type_id),V=re?.default_interval_hours||z.interval_hours,Q=re?.interval_type||"hours",le=z.interval_type||"hours",ye=c===z.id;return o.jsxs("div",{className:"flex items-center gap-2 p-2.5 bg-bambu-dark rounded-lg",children:[o.jsx(H,{className:"w-4 h-4 text-bambu-gray shrink-0"}),o.jsx("span",{className:"text-xs text-bambu-gray flex-1 truncate",children:z.maintenance_type_name}),ye?o.jsxs("div",{className:"flex items-center gap-1",children:[g==="days"?o.jsx(Jr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}):o.jsx(td,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}),o.jsxs("select",{value:g,onChange:Ne=>p(Ne.target.value),className:"px-1.5 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",children:[o.jsx("option",{value:"hours",children:"Print Hours"}),o.jsx("option",{value:"days",children:"Calendar Days"})]}),o.jsx("input",{type:"number",value:d,onChange:Ne=>f(Ne.target.value),onKeyDown:Ne=>{Ne.key==="Enter"&&Z(z.id,V,Q),Ne.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"}),o.jsx(Pe,{size:"sm",onClick:()=>Z(z.id,V,Q),children:"OK"})]}):o.jsxs("button",{onClick:()=>{u(z.id),f(z.interval_hours.toString()),p(le)},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:[le==="days"?o.jsx(Jr,{className:"w-3 h-3"}):o.jsx(td,{className:"w-3 h-3"}),Lk(z.interval_hours,le),o.jsx(nf,{className:"w-3 h-3 text-bambu-gray"})]})]},z.id)})})]})},je.printerId))})]}),ue.length===0&&o.jsx(At,{children:o.jsxs(Tt,{className:"text-center py-12",children:[o.jsx(ur,{className:"w-12 h-12 mx-auto mb-4 text-bambu-gray/30"}),o.jsx("p",{className:"text-bambu-gray",children:"No printers configured"}),o.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1",children:"Add printers to configure maintenance intervals"})]})})]})}function LWe(){const t=dn(),{showToast:e}=vr(),[n,r]=S.useState("status"),{data:i,isLoading:a}=pt({queryKey:["maintenanceOverview"],queryFn:_e.getMaintenanceOverview}),{data:s}=pt({queryKey:["maintenanceTypes"],queryFn:_e.getMaintenanceTypes}),l=_t({mutationFn:({id:w,notes:A})=>_e.performMaintenance(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Maintenance marked as complete")},onError:w=>{e(w.message,"error")}}),c=_t({mutationFn:({id:w,data:A})=>_e.updateMaintenanceItem(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]})},onError:w=>{e(w.message,"error")}}),u=_t({mutationFn:({id:w,data:A})=>_e.updateMaintenanceType(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type updated")},onError:w=>{e(w.message,"error")}}),d=_t({mutationFn:_e.deleteMaintenanceType,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type deleted")},onError:w=>{e(w.message,"error")}}),f=_t({mutationFn:({printerId:w,hours:A})=>_e.setPrinterHours(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Print hours updated")},onError:w=>{e(w.message,"error")}}),g=_t({mutationFn:({printerId:w,typeId:A})=>_e.assignMaintenanceType(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer assigned")},onError:w=>{e(w.message,"error")}}),p=_t({mutationFn:_e.removeMaintenanceItem,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer removed")},onError:w=>{e(w.message,"error")}}),b=w=>{l.mutate({id:w})},v=(w,A)=>{c.mutate({id:w,data:{enabled:A}})},x=(w,A)=>{f.mutate({printerId:w,hours:A})};if(a)return o.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})});const y=i?.reduce((w,A)=>w+A.due_count,0)||0,E=i?.reduce((w,A)=>w+A.warning_count,0)||0;return o.jsxs("div",{className:"p-4 md:p-8",children:[o.jsxs("div",{className:"mb-6",children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:"Maintenance"}),o.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:n==="status"?o.jsxs(o.Fragment,{children:[y>0&&o.jsxs("span",{className:"text-red-400",children:[y," task",y!==1?"s":""," overdue"]}),y>0&&E>0&&" · ",E>0&&o.jsxs("span",{className:"text-amber-400",children:[E," due soon"]}),y===0&&E===0&&o.jsx("span",{className:"text-bambu-green",children:"All maintenance up to date"})]}):"Configure maintenance types and intervals"})]}),o.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[o.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"}),o.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"?o.jsx("div",{className:"space-y-6",children:i&&i.length>0?[...i].sort((w,A)=>{const T=w.due_count*10+w.warning_count,C=A.due_count*10+A.warning_count;return T!==C?C-T:w.printer_name.localeCompare(A.printer_name)}).map(w=>o.jsx(DWe,{overview:w,onPerform:b,onToggle:v,onSetHours:x},w.printer_id)):o.jsx(At,{children:o.jsxs(Tt,{className:"text-center py-16",children:[o.jsx(_d,{className:"w-16 h-16 mx-auto mb-4 text-bambu-gray/30"}),o.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No printers configured"}),o.jsx("p",{className:"text-bambu-gray",children:"Add printers to start tracking maintenance"})]})})}):o.jsx(OWe,{overview:i,types:s||[],onUpdateInterval:(w,A)=>c.mutate({id:w,data:A}),onAddType:async(w,A)=>{const T=await _e.createMaintenanceType(w);for(const C of A)await _e.assignMaintenanceType(C,T.id);t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type added")},onUpdateType:(w,A)=>u.mutate({id:w,data:A}),onDeleteType:w=>d.mutate(w),onAssignType:(w,A)=>g.mutate({printerId:w,typeId:A}),onRemoveItem:w=>p.mutate(w)})]})}const e9=["#ef4444","#f97316","#eab308","#22c55e","#06b6d4","#3b82f6","#8b5cf6","#ec4899","#6b7280"];function tne({project:t,onClose:e,onSave:n,isLoading:r}){const[i,a]=S.useState(t?.name||""),[s,l]=S.useState(t?.description||""),[c,u]=S.useState(t?.color||e9[0]),[d,f]=S.useState(t?.target_count?.toString()||""),[g,p]=S.useState(t?.status||"active"),[b,v]=S.useState(t?.tags||""),[x,y]=S.useState(t?.due_date?.split("T")[0]||""),[E,w]=S.useState(t?.priority||"normal"),A=T=>{T.preventDefault(),n({name:i.trim(),description:s.trim()||void 0,color:c,target_count:d?parseInt(d,10):void 0,tags:b.trim()||void 0,due_date:x||void 0,priority:E,...t&&{status:g}})};return o.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:o.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[o.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:o.jsx("h2",{className:"text-lg font-semibold text-white",children:t?"Edit Project":"New Project"})}),o.jsxs("form",{onSubmit:A,className:"p-4 space-y-4",children:[o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Name"}),o.jsx("input",{type:"text",value:i,onChange:T=>a(T.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})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Description"}),o.jsx("textarea",{value:s,onChange:T=>l(T.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})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Color"}),o.jsx("div",{className:"flex gap-2 flex-wrap",children:e9.map(T=>o.jsx("button",{type:"button",onClick:()=>u(T),className:`w-8 h-8 rounded-full transition-transform ${c===T?"ring-2 ring-white ring-offset-2 ring-offset-bambu-dark-secondary scale-110":""}`,style:{backgroundColor:T}},T))})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Target Print Count (optional)"}),o.jsx("input",{type:"number",value:d,onChange:T=>f(T.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., 50 parts to print",min:"1"})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Tags (comma-separated)"}),o.jsx("input",{type:"text",value:b,onChange:T=>v(T.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"})]}),o.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Due Date"}),o.jsx("input",{type:"date",value:x,onChange:T=>y(T.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"})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Priority"}),o.jsxs("select",{value:E,onChange:T=>w(T.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:[o.jsx("option",{value:"low",children:"Low"}),o.jsx("option",{value:"normal",children:"Normal"}),o.jsx("option",{value:"high",children:"High"}),o.jsx("option",{value:"urgent",children:"Urgent"})]})]})]}),t&&o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Status"}),o.jsxs("select",{value:g,onChange:T=>p(T.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:[o.jsx("option",{value:"active",children:"Active"}),o.jsx("option",{value:"completed",children:"Completed"}),o.jsx("option",{value:"archived",children:"Archived"})]})]}),o.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[o.jsx(Pe,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),o.jsx(Pe,{type:"submit",disabled:!i.trim()||r,children:r?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):t?"Save":"Create"})]})]})]})})}function IWe({project:t,onClick:e,onEdit:n,onDelete:r}){const i=t.progress_percent??0,a=t.status==="completed",s=t.status==="archived",[l,c]=S.useState(!1),d=a?{icon:cR,color:"text-bambu-green",bg:"bg-bambu-green/10"}:s?{icon:co,color:"text-bambu-gray",bg:"bg-bambu-gray/10"}:t.queue_count>0?{icon:ur,color:"text-blue-400",bg:"bg-blue-400/10"}:{icon:Bo,color:"text-bambu-gray",bg:"bg-bambu-gray/10"};return o.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:[o.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`}}),o.jsxs("div",{className:"p-5 pl-6",children:[o.jsxs("div",{className:"flex items-start justify-between mb-4",children:[o.jsxs("div",{className:"flex items-center gap-3 min-w-0 flex-1",children:[o.jsx("div",{className:`p-2 rounded-lg ${d.bg} flex-shrink-0`,children:o.jsx(d.icon,{className:`w-5 h-5 ${d.color}`})}),o.jsxs("div",{className:"min-w-0 flex-1",children:[o.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[o.jsx("h3",{className:"font-semibold text-white truncate",children:t.name}),t.target_count?o.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.total_items,"/",t.target_count," items"]}):t.total_items>0?o.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium bg-bambu-dark text-bambu-gray",children:[t.total_items," item",t.total_items!==1?"s":""]}):null,a&&o.jsx("span",{className:"text-xs bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full whitespace-nowrap",children:"Done"}),s&&o.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&&o.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1 line-clamp-1",children:t.description}),t.archives&&t.archives.length>0&&(()=>{const f=[...new Set(t.archives.map(p=>p.filament_type).filter(Boolean))],g=[...new Set(t.archives.map(p=>p.filament_color).filter(Boolean))];return f.length===0&&g.length===0?null:o.jsxs("div",{className:"flex items-center gap-2 mt-1.5",children:[f.slice(0,3).map(p=>o.jsx("span",{className:"text-[10px] px-1.5 py-0.5 bg-bambu-dark text-bambu-gray rounded",children:p},p)),g.length>0&&o.jsxs("div",{className:"flex items-center gap-0.5",children:[g.slice(0,5).map(p=>o.jsx("div",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:p.startsWith("#")?p:`#${p}`},title:p},p)),g.length>5&&o.jsxs("span",{className:"text-[10px] text-bambu-gray ml-0.5",children:["+",g.length-5]})]})]})})()]})]}),o.jsxs("div",{className:"relative",onClick:f=>f.stopPropagation(),children:[o.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:()=>c(!l),children:o.jsx(o0,{className:"w-4 h-4"})}),l&&o.jsxs(o.Fragment,{children:[o.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>c(!1)}),o.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:[o.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(),c(!1)},children:[o.jsx(nf,{className:"w-4 h-4"}),"Edit"]}),o.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(),c(!1)},children:[o.jsx(Rn,{className:"w-4 h-4"}),"Delete"]})]})]})]})]}),o.jsx("div",{className:"mb-4",children:t.target_count?o.jsxs(o.Fragment,{children:[o.jsxs("div",{className:"flex items-center justify-between text-xs mb-2",children:[o.jsx("span",{className:"text-bambu-gray",children:"Progress"}),o.jsxs("span",{className:i>=100?"text-bambu-green font-medium":"text-white",children:[t.total_items," / ",t.target_count]})]}),o.jsx("div",{className:"h-2.5 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:o.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`}})}),o.jsxs("div",{className:"text-right text-xs text-bambu-gray/60 mt-1",children:[i.toFixed(0),"% complete"]})]}):t.total_items>0?o.jsxs("div",{className:"flex items-center gap-4 text-xs",children:[o.jsxs("div",{className:"flex items-center gap-1.5 text-bambu-gray",children:[o.jsx(co,{className:"w-3.5 h-3.5"}),o.jsxs("span",{children:[t.total_items," item",t.total_items!==1?"s":""," completed"]})]}),t.queue_count>0&&o.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",children:[o.jsx(ur,{className:"w-3.5 h-3.5"}),o.jsxs("span",{children:[t.queue_count," in queue"]})]})]}):o.jsx("div",{className:"text-xs text-bambu-gray/60 italic",children:"No prints yet"})}),t.archives&&t.archives.length>0&&o.jsxs("div",{className:"mb-4",children:[o.jsx("div",{className:"grid grid-cols-4 gap-1.5",children:t.archives.slice(0,4).map(f=>o.jsxs("div",{className:"relative aspect-square rounded-lg bg-bambu-dark overflow-hidden border border-bambu-dark-tertiary",title:f.print_name||"Unknown",children:[f.thumbnail_path?o.jsx("img",{src:_e.getArchiveThumbnail(f.id),alt:f.print_name||"",className:"w-full h-full object-cover"}):o.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray/50",children:o.jsx(zc,{className:"w-6 h-6"})}),f.status==="failed"&&o.jsx("div",{className:"absolute inset-0 bg-red-500/40 flex items-center justify-center",children:o.jsx(qn,{className:"w-4 h-4 text-white"})})]},f.id))}),t.archive_count>4&&o.jsxs("p",{className:"text-xs text-bambu-gray mt-1.5 text-center",children:["+",t.archive_count-4," more"]})]}),o.jsxs("div",{className:"flex items-center justify-between pt-3 border-t border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center gap-4 text-xs text-bambu-gray",children:[o.jsxs("div",{className:"flex items-center gap-1.5",title:"Total items printed",children:[o.jsx(co,{className:"w-3.5 h-3.5"}),o.jsx("span",{children:t.total_items})]}),t.queue_count>0&&o.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",title:"In queue",children:[o.jsx(t3,{className:"w-3.5 h-3.5"}),o.jsx("span",{children:t.queue_count})]})]}),o.jsx(Ko,{className:"w-4 h-4 text-bambu-gray/50 group-hover:text-bambu-gray transition-colors"})]})]})]})}function FWe(){const t=Eb(),e=dn(),{showToast:n}=vr(),[r,i]=S.useState(!1),[a,s]=S.useState(),[l,c]=S.useState("active"),[u,d]=S.useState(null),{data:f,isLoading:g}=pt({queryKey:["projects",l==="all"?void 0:l],queryFn:()=>_e.getProjects(l==="all"?void 0:l)}),p=_t({mutationFn:C=>_e.createProject(C),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),n("Project created","success")},onError:C=>{n(C.message,"error")}}),b=_t({mutationFn:({id:C,data:N})=>_e.updateProject(C,N),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),s(void 0),n("Project updated","success")},onError:C=>{n(C.message,"error")}}),v=_t({mutationFn:C=>_e.deleteProject(C),onSuccess:()=>{d(null),n("Project deleted","success"),setTimeout(()=>window.location.reload(),100)},onError:C=>{d(null),n(C.message,"error")}}),x=C=>{a?b.mutate({id:a.id,data:C}):p.mutate(C)},y=C=>{s(C),i(!0)},E=C=>{t(`/projects/${C.id}`)},w=C=>{d(C)},A=()=>{u!==null&&v.mutate(u)},T=f?.reduce((C,N)=>(C[N.status]=(C[N.status]||0)+1,C.all=(C.all||0)+1,C),{})||{};return o.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[o.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[o.jsxs("div",{children:[o.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[o.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:o.jsx(Bo,{className:"w-6 h-6 text-bambu-green"})}),"Projects"]}),o.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:"Organize and track your 3D printing projects"})]}),o.jsxs(Pe,{onClick:()=>i(!0),className:"sm:w-auto w-full",children:[o.jsx(fr,{className:"w-4 h-4 mr-2"}),"New Project"]})]}),o.jsx("div",{className:"flex gap-1 p-1 bg-bambu-dark rounded-xl w-fit",children:[{key:"active",label:"Active",icon:ur},{key:"completed",label:"Completed",icon:cR},{key:"archived",label:"Archived",icon:co},{key:"all",label:"All",icon:Bo}].map(({key:C,label:N,icon:O})=>o.jsxs("button",{onClick:()=>c(C),className:`flex items-center gap-2 px-4 py-2 text-sm rounded-lg transition-all ${l===C?"bg-bambu-card text-white shadow-sm":"text-bambu-gray hover:text-white"}`,children:[o.jsx(O,{className:"w-4 h-4"}),o.jsx("span",{children:N}),T[C]>0&&o.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded-full ${l===C?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary"}`,children:T[C]})]},C))}),g?o.jsx("div",{className:"flex items-center justify-center py-20",children:o.jsxs("div",{className:"flex flex-col items-center gap-3",children:[o.jsx(xt,{className:"w-8 h-8 animate-spin text-bambu-green"}),o.jsx("p",{className:"text-sm text-bambu-gray",children:"Loading projects..."})]})}):f?.length===0?o.jsxs("div",{className:"flex flex-col items-center justify-center py-20 px-4",children:[o.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:o.jsx(Bo,{className:"w-12 h-12 text-bambu-gray/50"})}),o.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:l==="all"?"No projects yet":`No ${l} projects`}),o.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"&&o.jsxs(Pe,{onClick:()=>i(!0),children:[o.jsx(fr,{className:"w-4 h-4 mr-2"}),"Create Your First Project"]})]}):o.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-8",children:f?.map(C=>o.jsx(IWe,{project:C,onClick:()=>E(C),onEdit:()=>y(C),onDelete:()=>w(C.id)},C.id))}),u!==null&&o.jsx(mr,{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:A,onCancel:()=>d(null)}),r&&o.jsx(tne,{project:a,onClose:()=>{i(!1),s(void 0)},onSave:x,isLoading:p.isPending||b.isPending})]})}function UWe(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 zWe(t){return t>=1e3?`${(t/1e3).toFixed(2)}kg`:`${Math.round(t)}g`}function BWe({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 o.jsx("span",{className:`px-2 py-1 rounded text-sm font-medium ${n}`,children:t.charAt(0).toUpperCase()+t.slice(1)})}function u1({icon:t,label:e,value:n,subValue:r,color:i="text-bambu-gray"}){return o.jsx(At,{children:o.jsx(Tt,{className:"p-4",children:o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark ${i}`,children:o.jsx(t,{className:"w-5 h-5"})}),o.jsxs("div",{children:[o.jsx("p",{className:"text-sm text-bambu-gray",children:e}),o.jsx("p",{className:"text-xl font-semibold text-white",children:n}),r&&o.jsx("p",{className:"text-xs text-bambu-gray/70",children:r})]})]})})})}function HWe({archives:t}){return t.length===0?o.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[o.jsx(zc,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),o.jsx("p",{children:"No prints in this project yet"})]}):o.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=>o.jsxs(Sc,{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?o.jsx("img",{src:_e.getArchiveThumbnail(e.id),alt:e.print_name||"Print",className:"w-full h-full object-cover"}):o.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:o.jsx(zc,{className:"w-8 h-8"})}),e.status==="failed"&&o.jsx("div",{className:"absolute inset-0 bg-red-500/30 flex items-center justify-center",children:o.jsx(Ra,{className:"w-8 h-8 text-white"})}),e.status==="completed"&&o.jsx("div",{className:"absolute top-1 right-1",children:o.jsx(Ar,{className:"w-4 h-4 text-bambu-green"})}),o.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:o.jsx("p",{className:"text-xs text-white truncate",children:e.print_name||"Unknown"})})]},e.id))})}function $We({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 o.jsxs("span",{className:`px-2 py-1 rounded text-xs font-medium flex items-center gap-1 ${n}`,children:[t==="urgent"&&o.jsx(qn,{className:"w-3 h-3"}),r]})}function VWe(t){return t?new Date(t).toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"}):""}function Ik(t){if(!t)return null;const e=new Date(t),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 GWe(){const{id:t}=eR(),e=Eb(),n=dn(),{showToast:r}=vr(),[i,a]=S.useState(!1),[s,l]=S.useState(!1),[c,u]=S.useState(""),[d,f]=S.useState(!1),g=S.useRef(null),p=parseInt(t||"0",10),{data:b,isLoading:v,error:x}=pt({queryKey:["project",p],queryFn:()=>_e.getProject(p),enabled:p>0}),{data:y,isLoading:E}=pt({queryKey:["project-archives",p],queryFn:()=>_e.getProjectArchives(p),enabled:p>0}),{data:w,isLoading:A}=pt({queryKey:["project-bom",p],queryFn:()=>_e.getProjectBOM(p),enabled:p>0}),{data:T,isLoading:C}=pt({queryKey:["project-timeline",p],queryFn:()=>_e.getProjectTimeline(p,20),enabled:p>0}),{data:N}=pt({queryKey:["settings"],queryFn:_e.getSettings}),O=N?.currency||"$",k=_t({mutationFn:U=>_e.updateProject(p,U),onSuccess:()=>{n.invalidateQueries({queryKey:["project",p]}),n.invalidateQueries({queryKey:["projects"]}),a(!1),l(!1),r("Project updated","success")},onError:U=>{r(U.message,"error")}}),j=()=>{u(b?.notes||""),l(!0)},B=()=>{k.mutate({notes:c})},I=()=>{l(!1),u("")},q=async U=>{const L=U.target.files?.[0];if(L){f(!0);try{const te=await _e.uploadProjectAttachment(p,L);n.invalidateQueries({queryKey:["project",p]}),r(`Uploaded: ${te.original_name}`,"success")}catch(te){r(te.message,"error")}finally{f(!1),g.current&&(g.current.value="")}}},R=(U,L)=>{Ae({isOpen:!0,title:"Delete Attachment",message:`Are you sure you want to delete "${L}"?`,onConfirm:async()=>{Ae(te=>({...te,isOpen:!1}));try{await _e.deleteProjectAttachment(p,U),n.invalidateQueries({queryKey:["project",p]}),r("Attachment deleted","success")}catch(te){r(te.message,"error")}}})},W=U=>U<1024?`${U} B`:U<1024*1024?`${(U/1024).toFixed(1)} KB`:`${(U/(1024*1024)).toFixed(1)} MB`,[K,G]=S.useState(""),[J,ee]=S.useState(1),[Y,F]=S.useState(""),[$,ne]=S.useState(""),[oe,se]=S.useState(""),[pe,Z]=S.useState(!1),[Me,he]=S.useState(!1),[ue,Ae]=S.useState({isOpen:!1,title:"",message:"",onConfirm:()=>{}}),fe=_t({mutationFn:U=>_e.createBOMItem(p,U),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",p]}),n.invalidateQueries({queryKey:["project",p]}),G(""),ee(1),F(""),ne(""),se(""),Z(!1),r("Part added","success")},onError:U=>r(U.message,"error")}),je=_t({mutationFn:({itemId:U,data:L})=>_e.updateBOMItem(p,U,L),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",p]}),n.invalidateQueries({queryKey:["project",p]})},onError:U=>r(U.message,"error")}),z=_t({mutationFn:U=>_e.deleteBOMItem(p,U),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",p]}),n.invalidateQueries({queryKey:["project",p]}),r("Part removed","success")},onError:U=>r(U.message,"error")}),H=U=>{U.preventDefault(),K.trim()&&fe.mutate({name:K.trim(),quantity_needed:J,unit_price:Y?parseFloat(Y):void 0,sourcing_url:$.trim()||void 0,remarks:oe.trim()||void 0})},re=U=>{const L=U.is_complete?0:U.quantity_needed;je.mutate({itemId:U.id,data:{quantity_acquired:L}})},V=(U,L)=>{Ae({isOpen:!0,title:"Delete Part",message:`Are you sure you want to delete "${L}"?`,onConfirm:()=>{Ae(te=>({...te,isOpen:!1})),z.mutate(U)}})},Q=_t({mutationFn:()=>_e.createTemplateFromProject(p),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),r("Template created","success")},onError:U=>r(U.message,"error")}),le=U=>new Date(U).toLocaleDateString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"});if(v)return o.jsx("div",{className:"flex items-center justify-center py-24",children:o.jsx(xt,{className:"w-8 h-8 animate-spin text-bambu-green"})});if(x||!b)return o.jsxs("div",{className:"text-center py-24",children:[o.jsx("p",{className:"text-bambu-gray",children:x?`Error: ${x.message}`:"Project not found"}),o.jsx(Pe,{variant:"secondary",className:"mt-4",onClick:()=>e("/projects"),children:"Back to Projects"})]});const ye=b.stats,Ne=ye?.progress_percent??0,Ee=ye&&ye.total_items>0?(ye.completed_prints/ye.total_items*100).toFixed(0):null;return o.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[o.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[o.jsx(Sc,{to:"/projects",className:"hover:text-white transition-colors",children:"Projects"}),o.jsx(Ko,{className:"w-4 h-4"}),o.jsx("span",{className:"text-white",children:b.name})]}),o.jsxs("div",{className:"flex items-start justify-between",children:[o.jsxs("div",{className:"flex items-center gap-4",children:[o.jsx("button",{onClick:()=>e("/projects"),className:"p-2 rounded-lg bg-bambu-card hover:bg-bambu-dark-tertiary transition-colors",children:o.jsx(Loe,{className:"w-5 h-5 text-bambu-gray"})}),o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-4 h-4 rounded-full flex-shrink-0",style:{backgroundColor:b.color||"#6b7280"}}),o.jsxs("div",{children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:b.name}),b.description&&o.jsx("p",{className:"text-bambu-gray mt-1",children:b.description})]})]}),o.jsx(BWe,{status:b.status})]}),o.jsxs(Pe,{onClick:()=>a(!0),children:[o.jsx(nf,{className:"w-4 h-4 mr-2"}),"Edit"]})]}),b.target_count&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-2",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Progress"}),o.jsxs("span",{className:"text-sm font-medium text-white",children:[ye?.completed_prints||0," / ",b.target_count," items"]})]}),o.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:o.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(Ne,100)}%`,backgroundColor:Ne>=100?"#22c55e":b.color||"#6b7280"}})}),o.jsxs("div",{className:"flex justify-between mt-1",children:[o.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[Ne.toFixed(0),"% complete"]}),b.target_count-(ye?.completed_prints||0)>0&&o.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[b.target_count-(ye?.completed_prints||0)," remaining"]})]})]})}),ye&&o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsx(u1,{icon:zc,label:"Total Items",value:ye.total_items,subValue:`${ye.total_archives} print job${ye.total_archives!==1?"s":""}`,color:"text-bambu-green"}),o.jsx(u1,{icon:Ar,label:"Completed",value:ye.completed_prints,subValue:ye.failed_prints>0?`${ye.failed_prints} failed`:Ee?`${Ee}% success`:void 0,color:"text-blue-400"}),o.jsx(u1,{icon:ur,label:"Print Time",value:UWe(ye.total_print_time_hours),color:"text-yellow-400"}),o.jsx(u1,{icon:qi,label:"Filament Used",value:zWe(ye.total_filament_grams),color:"text-purple-400"})]}),ye&&(ye.estimated_cost>0||b.budget)&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsx("h2",{className:"text-lg font-semibold text-white mb-3",children:"Cost Tracking"}),o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Filament Cost"}),o.jsxs("p",{className:"text-lg font-semibold text-white",children:[O,ye.estimated_cost.toFixed(2)]})]}),ye.total_energy_kwh>0&&o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Energy"}),o.jsxs("p",{className:"text-lg font-semibold text-white",children:[ye.total_energy_kwh.toFixed(2)," kWh",ye.total_energy_cost>0&&o.jsxs("span",{className:"text-sm text-bambu-gray ml-1",children:["(",O,ye.total_energy_cost.toFixed(2),")"]})]})]}),b.budget&&o.jsxs(o.Fragment,{children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Budget"}),o.jsxs("p",{className:"text-lg font-semibold text-white",children:[O,b.budget.toFixed(2)]})]}),o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Remaining"}),o.jsxs("p",{className:`text-lg font-semibold ${b.budget-ye.estimated_cost>=0?"text-bambu-green":"text-red-400"}`,children:[O,(b.budget-ye.estimated_cost).toFixed(2)]})]})]})]})]})}),b.children&&b.children.length>0&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-3",children:[o.jsx(ace,{className:"w-5 h-5"}),"Sub-projects (",b.children.length,")"]}),o.jsx("div",{className:"space-y-2",children:b.children.map(U=>o.jsxs(Sc,{to:`/projects/${U.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:U.color||"#6b7280"}}),o.jsx("span",{className:"text-white",children:U.name}),o.jsx("span",{className:`text-xs px-2 py-0.5 rounded ${U.status==="completed"?"bg-bambu-green/20 text-bambu-green":U.status==="archived"?"bg-bambu-gray/20 text-bambu-gray":"bg-blue-500/20 text-blue-400"}`,children:U.status})]}),U.progress_percent!==null&&o.jsxs("span",{className:"text-sm text-bambu-gray",children:[U.progress_percent.toFixed(0),"%"]})]},U.id))})]})}),b.parent_id&&b.parent_name&&o.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[o.jsx(vd,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("span",{className:"text-bambu-gray",children:"Part of:"}),o.jsx(Sc,{to:`/projects/${b.parent_id}`,className:"text-bambu-green hover:underline",children:b.parent_name})]}),(b.tags||b.due_date||b.priority!=="normal")&&o.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[b.priority&&b.priority!=="normal"&&o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("span",{className:"text-xs text-bambu-gray uppercase",children:"Priority:"}),o.jsx($We,{priority:b.priority})]}),b.due_date&&o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx(Jr,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("span",{className:"text-sm text-white",children:VWe(b.due_date)}),Ik(b.due_date)&&o.jsxs("span",{className:`text-xs ${Ik(b.due_date).color}`,children:["(",Ik(b.due_date).label,")"]})]}),b.tags&&o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx(Cp,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("div",{className:"flex flex-wrap gap-1",children:b.tags.split(",").map((U,L)=>o.jsx("span",{className:"px-2 py-0.5 bg-bambu-dark-tertiary text-bambu-gray text-xs rounded",children:U.trim()},L))})]})]}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(As,{className:"w-5 h-5"}),"Notes"]}),s?o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:I,disabled:k.isPending,children:[o.jsx(Wt,{className:"w-4 h-4 mr-1"}),"Cancel"]}),o.jsxs(Pe,{size:"sm",onClick:B,disabled:k.isPending,children:[k.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin mr-1"}):o.jsx(Ts,{className:"w-4 h-4 mr-1"}),"Save"]})]}):o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:j,children:[o.jsx(nf,{className:"w-4 h-4 mr-1"}),"Edit"]})]}),s?o.jsx($j,{content:c,onChange:u,placeholder:"Add notes about this project..."}):b.notes?o.jsx("div",{className:"prose prose-invert prose-sm max-w-none",dangerouslySetInnerHTML:{__html:b.notes}}):o.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No notes yet. Click Edit to add notes."})]})}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(pue,{className:"w-5 h-5"}),"Attachments (",b.attachments?.length||0,")"]}),o.jsxs("div",{children:[o.jsx("input",{ref:g,type:"file",onChange:q,className:"hidden"}),o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>g.current?.click(),disabled:d,children:[d?o.jsx(xt,{className:"w-4 h-4 animate-spin mr-1"}):o.jsx(ua,{className:"w-4 h-4 mr-1"}),"Upload"]})]})]}),o.jsx("p",{className:"text-xs text-bambu-gray mb-3",children:"Upload any file: images (PNG, JPG), PDFs, STL files, or documents."}),b.attachments&&b.attachments.length>0?o.jsx("div",{className:"space-y-2",children:b.attachments.map(U=>o.jsxs("div",{className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[o.jsx(hR,{className:"w-5 h-5 text-bambu-gray flex-shrink-0"}),o.jsxs("div",{className:"min-w-0",children:[o.jsx("p",{className:"text-sm text-white truncate",children:U.original_name}),o.jsx("p",{className:"text-xs text-bambu-gray",children:W(U.size)})]})]}),o.jsxs("div",{className:"flex items-center gap-1 flex-shrink-0",children:[o.jsx("a",{href:_e.getProjectAttachmentUrl(p,U.filename),download:U.original_name,className:"p-2 rounded hover:bg-bambu-dark-tertiary transition-colors text-bambu-gray hover:text-white",title:"Download",children:o.jsx(Wi,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>R(U.filename,U.original_name),className:"p-2 rounded hover:bg-bambu-dark-tertiary transition-colors text-bambu-gray hover:text-red-400",title:"Delete",children:o.jsx(Rn,{className:"w-4 h-4"})})]})]},U.filename))}):o.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No attachments yet. Click Upload to add files."})]})}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-4",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(q9,{className:"w-5 h-5"}),"Bill of Materials",ye&&ye.bom_total_items>0&&o.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",ye.bom_completed_items,"/",ye.bom_total_items," acquired)"]})]}),o.jsxs("div",{className:"flex items-center gap-2",children:[w&&w.some(U=>U.is_complete)&&o.jsx("button",{onClick:()=>he(!Me),className:`text-xs px-2 py-1 rounded transition-colors ${Me?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:Me?"Show all":"Hide done"}),!pe&&o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>Z(!0),children:[o.jsx(fr,{className:"w-4 h-4 mr-1"}),"Add Part"]})]})]}),pe&&o.jsxs("form",{onSubmit:H,className:"bg-bambu-dark rounded-lg p-4 mb-4 space-y-3",children:[o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[o.jsx("input",{type:"text",value:K,onChange:U=>G(U.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}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx("input",{type:"number",value:J,onChange:U=>ee(parseInt(U.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"}),o.jsx("input",{type:"number",step:"0.01",value:Y,onChange:U=>F(U.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 (${O})`})]})]}),o.jsx("input",{type:"url",value:$,onChange:U=>ne(U.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)"}),o.jsx("input",{type:"text",value:oe,onChange:U=>se(U.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)"}),o.jsxs("div",{className:"flex justify-end gap-2",children:[o.jsx(Pe,{type:"button",variant:"secondary",size:"sm",onClick:()=>Z(!1),children:"Cancel"}),o.jsx(Pe,{type:"submit",size:"sm",disabled:!K.trim()||fe.isPending,children:fe.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):"Add Part"})]})]}),A?o.jsx("div",{className:"flex items-center justify-center py-4",children:o.jsx(xt,{className:"w-6 h-6 animate-spin text-bambu-green"})}):w&&w.length>0?o.jsxs("div",{className:"space-y-2",children:[w.filter(U=>!Me||!U.is_complete).map(U=>o.jsx("div",{className:`p-3 rounded-lg transition-colors ${U.is_complete?"bg-bambu-green/10":"bg-bambu-dark"}`,children:o.jsxs("div",{className:"flex items-start gap-3",children:[o.jsx("button",{onClick:()=>re(U),disabled:je.isPending,className:`w-5 h-5 mt-0.5 rounded border-2 flex items-center justify-center transition-colors flex-shrink-0 ${U.is_complete?"bg-bambu-green border-bambu-green text-white":"border-bambu-gray hover:border-bambu-green"}`,children:U.is_complete&&o.jsx(Ar,{className:"w-3 h-3"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center justify-between gap-2",children:[o.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[o.jsxs("p",{className:`text-sm font-medium ${U.is_complete?"text-bambu-gray line-through":"text-white"}`,children:[U.name,o.jsxs("span",{className:"text-bambu-gray font-normal ml-2",children:["x",U.quantity_needed]})]}),U.unit_price!==null&&o.jsxs("span",{className:"text-xs text-bambu-green whitespace-nowrap",children:[O,(U.unit_price*U.quantity_needed).toFixed(2)]})]}),o.jsx("button",{onClick:()=>V(U.id,U.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:o.jsx(Rn,{className:"w-4 h-4"})})]}),U.sourcing_url&&o.jsxs("a",{href:U.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:L=>L.stopPropagation(),children:[o.jsx(_i,{className:"w-3 h-3 flex-shrink-0"}),o.jsx("span",{className:"truncate",children:(()=>{try{return new URL(U.sourcing_url).hostname.replace("www.","")}catch{return U.sourcing_url}})()})]}),U.remarks&&o.jsx("p",{className:"mt-1 text-xs text-bambu-gray/80 italic",children:U.remarks})]})]})},U.id)),w.some(U=>U.unit_price!==null)&&o.jsxs("div",{className:"pt-2 mt-2 border-t border-bambu-dark-tertiary flex justify-between text-sm",children:[o.jsx("span",{className:"text-bambu-gray",children:"Total cost:"}),o.jsxs("span",{className:"text-white font-medium",children:[O,w.reduce((U,L)=>U+(L.unit_price||0)*L.quantity_needed,0).toFixed(2)]})]})]}):o.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."})]})}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsx("div",{className:"flex items-center justify-between mb-3",children:o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(G1,{className:"w-5 h-5"}),"Activity Timeline"]})}),C?o.jsx("div",{className:"flex items-center justify-center py-4",children:o.jsx(xt,{className:"w-6 h-6 animate-spin text-bambu-green"})}):T&&T.length>0?o.jsx("div",{className:"space-y-3",children:T.slice(0,10).map((U,L)=>o.jsxs("div",{className:"flex gap-3",children:[o.jsxs("div",{className:`w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 ${U.event_type==="print_completed"?"bg-bambu-green/20 text-bambu-green":U.event_type==="print_failed"?"bg-red-500/20 text-red-400":U.event_type==="print_started"?"bg-yellow-500/20 text-yellow-400":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[U.event_type==="print_completed"&&o.jsx(Ar,{className:"w-4 h-4"}),U.event_type==="print_failed"&&o.jsx(Ra,{className:"w-4 h-4"}),U.event_type==="print_started"&&o.jsx(qi,{className:"w-4 h-4"}),U.event_type==="queued"&&o.jsx(t3,{className:"w-4 h-4"}),U.event_type==="project_created"&&o.jsx(fr,{className:"w-4 h-4"})]}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("p",{className:"text-sm text-white",children:U.title}),U.description&&o.jsx("p",{className:"text-xs text-bambu-gray truncate",children:U.description}),o.jsx("p",{className:"text-xs text-bambu-gray/70",children:le(U.timestamp)})]})]},L))}):o.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No activity yet."})]})}),!b.is_template&&o.jsx("div",{className:"flex justify-end",children:o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>Q.mutate(),disabled:Q.isPending,children:[Q.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin mr-2"}):o.jsx(vl,{className:"w-4 h-4 mr-2"}),"Save as Template"]})}),ye&&(ye.queued_prints>0||ye.in_progress_prints>0)&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(t3,{className:"w-5 h-5"}),"Queue"]}),o.jsx(Sc,{to:`/queue?project=${p}`,className:"text-sm text-bambu-green hover:underline",children:"View all"})]}),o.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[ye.in_progress_prints>0&&o.jsxs("span",{className:"text-yellow-400",children:[ye.in_progress_prints," printing"]}),ye.queued_prints>0&&o.jsxs("span",{className:"text-bambu-gray",children:[ye.queued_prints," queued"]})]})]})}),o.jsxs("div",{children:[o.jsx("div",{className:"flex items-center justify-between mb-4",children:o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(zc,{className:"w-5 h-5"}),"Prints (",y?.length||0,")"]})}),E?o.jsx("div",{className:"flex items-center justify-center py-8",children:o.jsx(xt,{className:"w-6 h-6 animate-spin text-bambu-green"})}):o.jsx(HWe,{archives:y||[]})]}),i&&o.jsx(tne,{project:{...b,archive_count:ye?.total_archives||0,total_items:ye?.total_items||0,queue_count:ye?.queued_prints||0,progress_percent:ye?.progress_percent||null,archives:[]},onClose:()=>a(!1),onSave:U=>k.mutate(U),isLoading:k.isPending}),ue.isOpen&&o.jsx(mr,{title:ue.title,message:ue.message,confirmText:"Delete",variant:"danger",onConfirm:ue.onConfirm,onCancel:()=>Ae(U=>({...U,isOpen:!1}))})]})}const Fk=5,WWe=2e3,qWe=3e4,KWe=5e3;function XWe(){const{printerId:t}=eR(),e=parseInt(t||"0",10),[n,r]=S.useState("stream"),[i,a]=S.useState(!1),[s,l]=S.useState(!0),[c,u]=S.useState(Date.now()),[d,f]=S.useState(!1),[g,p]=S.useState(!1),[b,v]=S.useState(0),[x,y]=S.useState(!1),[E,w]=S.useState(0),A=S.useRef(null),T=S.useRef(null),C=S.useRef(null),N=S.useRef(null),O=S.useRef(null),{data:k}=pt({queryKey:["printer",e],queryFn:()=>_e.getPrinter(e),enabled:e>0});S.useEffect(()=>(k&&(document.title=`${k.name} - Camera`),()=>{document.title="Bambuddy"}),[k]);const j=S.useRef(!1);S.useEffect(()=>{const Y=`/api/v1/printers/${e}/camera/stop`;j.current=!1;const F=()=>{e>0&&!j.current&&(j.current=!0,navigator.sendBeacon(Y))},$=()=>{F()};return window.addEventListener("beforeunload",$),()=>{window.removeEventListener("beforeunload",$),A.current&&(A.current.src=""),F()}},[e]),S.useEffect(()=>{if(s&&!d){const F=setTimeout(()=>{l(!1)},n==="stream"?3e3:2e4);return()=>clearTimeout(F)}},[n,s,c,d]),S.useEffect(()=>{const Y=()=>{const F=!!document.fullscreenElement;p(F),n==="stream"&&!d&&(A.current&&(A.current.src=""),setTimeout(()=>{l(!0),u(Date.now())},200))};return document.addEventListener("fullscreenchange",Y),()=>document.removeEventListener("fullscreenchange",Y)},[n,d]),S.useEffect(()=>{let Y;const F=()=>{clearTimeout(Y),Y=setTimeout(()=>{localStorage.setItem("cameraWindowState",JSON.stringify({width:window.outerWidth,height:window.outerHeight,left:window.screenX,top:window.screenY}))},500)};return window.addEventListener("resize",F),()=>{clearTimeout(Y),window.removeEventListener("resize",F)}},[]),S.useEffect(()=>()=>{C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),O.current&&clearInterval(O.current)},[]);const B=S.useCallback(()=>{if(b>=Fk){y(!1),a(!0);return}const Y=Math.min(WWe*Math.pow(2,b),qWe);y(!0),w(Math.ceil(Y/1e3)),N.current=setInterval(()=>{w(F=>F<=1?(N.current&&clearInterval(N.current),0):F-1)},1e3),C.current=setTimeout(()=>{v(F=>F+1),y(!1),l(!0),a(!1),A.current&&(A.current.src=""),u(Date.now())},Y)},[b]);S.useEffect(()=>{if(n!=="stream"||s||i||x||d){O.current&&(clearInterval(O.current),O.current=null);return}return O.current=setInterval(async()=>{try{const Y=await fetch(`/api/v1/printers/${e}/camera/status`);Y.ok&&(await Y.json()).stalled&&(console.log("Stream stall detected, auto-reconnecting..."),O.current&&(clearInterval(O.current),O.current=null),l(!1),B())}catch{}},KWe),()=>{O.current&&(clearInterval(O.current),O.current=null)}},[n,s,i,x,d,e,B]);const I=()=>{l(!1),n==="stream"&&b{if(l(!1),a(!1),v(0),y(!1),C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),A.current&&!localStorage.getItem("cameraWindowState")){const Y=A.current,F=Y.naturalWidth,$=Y.naturalHeight;if(F>0&&$>0){const se=window.outerWidth-window.innerWidth,pe=window.outerHeight-window.innerHeight,Z=F+16+se,Me=$+45+16+pe;try{window.resizeTo(Z,Me)}catch{}}}},R=()=>{e>0&&fetch(`/api/v1/printers/${e}/camera/stop`).catch(()=>{})},W=Y=>{n===Y||d||(f(!0),l(!0),a(!1),v(0),y(!1),C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),A.current&&(A.current.src=""),n==="stream"&&R(),setTimeout(()=>{r(Y),u(Date.now()),f(!1)},100))},K=()=>{d||(f(!0),l(!0),a(!1),v(0),y(!1),C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),A.current&&(A.current.src=""),n==="stream"&&R(),setTimeout(()=>{u(Date.now()),f(!1)},100))},G=()=>{T.current&&(document.fullscreenElement?document.exitFullscreen():T.current.requestFullscreen())},J=d?"":n==="stream"?`/api/v1/printers/${e}/camera/stream?fps=10&t=${c}`:`/api/v1/printers/${e}/camera/snapshot?t=${c}`,ee=s||d||x;return e?o.jsxs("div",{ref:T,className:"min-h-screen bg-black flex flex-col",children:[o.jsxs("div",{className:"flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary",children:[o.jsxs("h1",{className:"text-sm font-medium text-white flex items-center gap-2",children:[o.jsx(R0,{className:"w-4 h-4"}),k?.name||`Printer ${e}`]}),o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsxs("div",{className:"flex bg-bambu-dark rounded p-0.5",children:[o.jsx("button",{onClick:()=>W("stream"),disabled:ee,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"}),o.jsx("button",{onClick:()=>W("snapshot"),disabled:ee,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"})]}),o.jsx("button",{onClick:K,disabled:ee,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50",title:n==="stream"?"Restart stream":"Refresh snapshot",children:o.jsx(li,{className:`w-4 h-4 text-bambu-gray ${ee?"animate-spin":""}`})}),o.jsx("button",{onClick:G,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded",title:g?"Exit fullscreen":"Fullscreen",children:g?o.jsx(sue,{className:"w-4 h-4 text-bambu-gray"}):o.jsx(Qce,{className:"w-4 h-4 text-bambu-gray"})})]})]}),o.jsx("div",{className:"flex-1 flex items-center justify-center p-2",children:o.jsxs("div",{className:"relative w-full h-full flex items-center justify-center",children:[(s||d)&&!x&&o.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/50 z-10",children:o.jsxs("div",{className:"text-center",children:[o.jsx(li,{className:"w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2"}),o.jsx("p",{className:"text-sm text-bambu-gray",children:n==="stream"?"Connecting to camera...":"Capturing snapshot..."})]})}),x&&o.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/80 z-10",children:o.jsxs("div",{className:"text-center p-4",children:[o.jsx(kb,{className:"w-10 h-10 text-orange-400 mx-auto mb-3"}),o.jsx("p",{className:"text-white mb-2",children:"Connection lost"}),o.jsxs("p",{className:"text-sm text-bambu-gray mb-3",children:["Reconnecting in ",E,"s... (attempt ",b+1,"/",Fk,")"]}),o.jsx("button",{onClick:K,className:"px-4 py-2 bg-bambu-green text-white text-sm rounded hover:bg-bambu-green/80 transition-colors",children:"Reconnect now"})]})}),i&&!x&&o.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black z-10",children:o.jsxs("div",{className:"text-center p-4",children:[o.jsx(qn,{className:"w-12 h-12 text-orange-400 mx-auto mb-3"}),o.jsx("p",{className:"text-white mb-2",children:"Camera unavailable"}),o.jsx("p",{className:"text-xs text-bambu-gray mb-4 max-w-md",children:"Make sure the printer is powered on and connected."}),o.jsx("button",{onClick:K,className:"px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors",children:"Retry"})]})}),o.jsx("img",{ref:A,src:J,alt:"Camera stream",className:"max-w-full max-h-full object-contain",onError:J?I:void 0,onLoad:J?q:void 0},c)]})})]}):o.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:o.jsx("p",{className:"text-white",children:"Invalid printer ID"})})}function YWe(){const{id:t}=eR(),{mode:e}=og(),{data:n,isLoading:r,error:i}=pt({queryKey:["external-link",t],queryFn:()=>_e.getExternalLink(Number(t)),enabled:!!t});return r?o.jsx("div",{className:"flex items-center justify-center h-full",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):i||!n?o.jsxs("div",{className:"flex flex-col items-center justify-center h-full gap-4 text-bambu-gray",children:[o.jsx(qn,{className:"w-12 h-12"}),o.jsx("p",{children:"Link not found"})]}):o.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"})}function Yr({icon:t,label:e,value:n,subValue:r,color:i="text-bambu-green"}){return o.jsxs("div",{className:"flex items-start gap-3 p-4 bg-bambu-dark rounded-lg",children:[o.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark-tertiary ${i}`,children:o.jsx(t,{className:"w-5 h-5"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("p",{className:"text-sm text-bambu-gray",children:e}),o.jsx("p",{className:"text-lg font-semibold text-white truncate",children:n}),r&&o.jsx("p",{className:"text-xs text-bambu-gray mt-0.5",children:r})]})]})}function t9({percent:t,color:e="bg-bambu-green"}){return o.jsx("div",{className:"w-full h-2 bg-bambu-dark rounded-full overflow-hidden",children:o.jsx("div",{className:`h-full ${e} transition-all duration-300`,style:{width:`${Math.min(100,t)}%`}})})}function Bu({title:t,icon:e,children:n}){return o.jsxs(At,{className:"p-6",children:[o.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[o.jsx(e,{className:"w-5 h-5 text-bambu-green"}),o.jsx("h2",{className:"text-lg font-semibold text-white",children:t})]}),n]})}function ZWe(){const{t}=ng(),e=dn(),[n,r]=S.useState(null),[i,a]=S.useState(!1),[s,l]=S.useState(!1),{data:c,isLoading:u,refetch:d,isFetching:f}=pt({queryKey:["systemInfo"],queryFn:_e.getSystemInfo,refetchInterval:3e4}),{data:g}=pt({queryKey:["debugLogging"],queryFn:p1.getDebugLoggingState,staleTime:10*1e3,refetchInterval:10*1e3}),p=async()=>{l(!0);try{await p1.setDebugLogging(!g?.enabled),e.invalidateQueries({queryKey:["debugLogging"]})}catch(y){console.error("Failed to toggle debug logging:",y)}finally{l(!1)}},b=async()=>{r(null),a(!0);try{await p1.downloadSupportBundle()}catch(y){r(y instanceof Error?y.message:"Failed to download support bundle")}finally{a(!1)}};if(u)return o.jsx("div",{className:"flex items-center justify-center h-64",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})});if(!c)return o.jsx("div",{className:"p-6 text-center text-bambu-gray",children:t("system.failedToLoad","Failed to load system information")});const v=c.storage.disk_percent_used>90?"bg-red-500":c.storage.disk_percent_used>75?"bg-yellow-500":"bg-bambu-green",x=c.memory.percent_used>90?"bg-red-500":c.memory.percent_used>75?"bg-yellow-500":"bg-bambu-green";return o.jsxs("div",{className:"p-6 space-y-6",children:[o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:t("system.title","System Information")}),o.jsx("p",{className:"text-bambu-gray mt-1",children:t("system.subtitle","Monitor system resources and database statistics")})]}),o.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:[o.jsx(li,{className:`w-4 h-4 ${f?"animate-spin":""}`}),t("common.refresh","Refresh")]})]}),o.jsx(Bu,{title:t("system.application","Application"),icon:ph,children:o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[o.jsx(Yr,{icon:ph,label:t("system.version","Version"),value:`v${c.app.version}`}),o.jsx(Yr,{icon:ur,label:t("system.uptime","System Uptime"),value:c.system.uptime_formatted}),o.jsx(Yr,{icon:ph,label:t("system.hostname","Hostname"),value:c.system.hostname})]})}),o.jsx(Bu,{title:t("support.title","Support & Troubleshooting"),icon:vce,children:o.jsxs("div",{className:"space-y-4",children:[o.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.")}),o.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:`p-2 rounded-lg ${g?.enabled?"bg-amber-500/20 text-amber-500":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:o.jsx(B9,{className:"w-5 h-5"})}),o.jsxs("div",{children:[o.jsx("p",{className:"font-medium text-white",children:t("support.debugLogging","Debug Logging")}),o.jsxs("p",{className:"text-sm text-bambu-gray",children:[g?.enabled?t("support.debugLoggingEnabled","Capturing detailed logs"):t("support.debugLoggingDisabled","Normal logging level"),g?.enabled&&g.duration_seconds!==null&&o.jsxs("span",{className:"text-amber-400 ml-2",children:["(",Math.floor(g.duration_seconds/60),"m ",g.duration_seconds%60,"s)"]})]})]})]}),o.jsxs("button",{onClick:p,disabled:s,className:`px-4 py-2 rounded-lg font-medium transition-colors flex items-center gap-2 ${g?.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:[s&&o.jsx(xt,{className:"w-4 h-4 animate-spin"}),g?.enabled?t("support.disableDebug","Disable"):t("support.enableDebug","Enable")]})]}),o.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-green",children:o.jsx(Wi,{className:"w-5 h-5"})}),o.jsxs("div",{children:[o.jsx("p",{className:"font-medium text-white",children:t("support.supportBundle","Support Bundle")}),o.jsx("p",{className:"text-sm text-bambu-gray",children:t("support.supportBundleDescription","Download system info and logs as a ZIP file")})]})]}),o.jsxs("button",{onClick:b,disabled:i||!g?.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:g?.enabled?void 0:t("support.enableDebugFirst","Enable debug logging first"),children:[i&&o.jsx(xt,{className:"w-4 h-4 animate-spin"}),t("common.download","Download")]})]}),n&&o.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/30 rounded-lg text-red-400 text-sm",children:n}),!g?.enabled&&o.jsx("div",{className:"p-4 bg-bambu-dark-tertiary/50 rounded-lg",children:o.jsxs("p",{className:"text-sm text-bambu-gray",children:[o.jsx("span",{className:"text-amber-400 font-medium",children:t("support.instructions","To report an issue:")}),o.jsx("br",{}),"1. ",t("support.step1","Enable debug logging"),o.jsx("br",{}),"2. ",t("support.step2","Reproduce the issue"),o.jsx("br",{}),"3. ",t("support.step3","Download the support bundle"),o.jsx("br",{}),"4. ",t("support.step4","Attach the ZIP file to your issue report")]})}),o.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg space-y-3",children:[o.jsx("p",{className:"text-sm font-medium text-white",children:t("support.privacyTitle","What's in the support bundle?")}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 text-sm",children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-bambu-green font-medium mb-1",children:t("support.collected","Collected:")}),o.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[o.jsxs("li",{children:["• ",t("support.collectItem1","App version and debug mode")]}),o.jsxs("li",{children:["• ",t("support.collectItem2","OS, architecture, Python version")]}),o.jsxs("li",{children:["• ",t("support.collectItem3","Database statistics (counts only)")]}),o.jsxs("li",{children:["• ",t("support.collectItem4","Printer models and nozzle counts")]}),o.jsxs("li",{children:["• ",t("support.collectItem5","Non-sensitive settings (themes, formats)")]}),o.jsxs("li",{children:["• ",t("support.collectItem6","Debug logs (sanitized)")]})]})]}),o.jsxs("div",{children:[o.jsx("p",{className:"text-red-400 font-medium mb-1",children:t("support.notCollected","NOT collected:")}),o.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[o.jsxs("li",{children:["• ",t("support.notItem1","Printer names, IPs, serial numbers")]}),o.jsxs("li",{children:["• ",t("support.notItem2","Access codes and passwords")]}),o.jsxs("li",{children:["• ",t("support.notItem3","Email addresses")]}),o.jsxs("li",{children:["• ",t("support.notItem4","API keys and tokens")]}),o.jsxs("li",{children:["• ",t("support.notItem5","Webhook URLs")]}),o.jsxs("li",{children:["• ",t("support.notItem6","Your hostname or username")]})]})]})]}),o.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].")})]})]})}),o.jsxs(Bu,{title:t("system.database","Database"),icon:Ep,children:[o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 mb-4",children:[o.jsx(Yr,{icon:co,label:t("system.totalArchives","Total Archives"),value:c.database.archives}),o.jsx(Yr,{icon:cR,label:t("system.completed","Completed"),value:c.database.archives_completed,color:"text-green-500"}),o.jsx(Yr,{icon:Ra,label:t("system.failed","Failed"),value:c.database.archives_failed,color:"text-red-500"}),o.jsx(Yr,{icon:xt,label:t("system.printing","Printing"),value:c.database.archives_printing,color:"text-yellow-500"})]}),o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsx(Yr,{icon:qi,label:t("system.printers","Printers"),value:c.database.printers}),o.jsx(Yr,{icon:bR,label:t("system.filaments","Filaments"),value:c.database.filaments}),o.jsx(Yr,{icon:Bo,label:t("system.projects","Projects"),value:c.database.projects}),o.jsx(Yr,{icon:uo,label:t("system.smartPlugs","Smart Plugs"),value:c.database.smart_plugs})]}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 mt-4",children:[o.jsx(Yr,{icon:ur,label:t("system.totalPrintTime","Total Print Time"),value:c.database.total_print_time_formatted}),o.jsx(Yr,{icon:co,label:t("system.totalFilament","Total Filament Used"),value:`${c.database.total_filament_kg} kg`,subValue:`${c.database.total_filament_grams.toLocaleString()} g`})]})]}),o.jsxs(Bu,{title:t("system.connectedPrinters","Connected Printers"),icon:qi,children:[o.jsxs("div",{className:"flex items-center gap-4 mb-4",children:[o.jsx("div",{className:"text-3xl font-bold text-bambu-green",children:c.printers.connected}),o.jsx("div",{className:"text-bambu-gray",children:t("system.ofTotal","of {{total}} printers connected",{total:c.printers.total})})]}),c.printers.connected_list.length>0?o.jsx("div",{className:"space-y-2",children:c.printers.connected_list.map(y=>o.jsxs("div",{className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green"}),o.jsx("span",{className:"font-medium text-white",children:y.name})]}),o.jsxs("div",{className:"flex items-center gap-4 text-sm text-bambu-gray",children:[o.jsx("span",{children:y.model}),o.jsx("span",{className:`px-2 py-0.5 rounded ${y.state==="RUNNING"?"bg-bambu-green/20 text-bambu-green":y.state==="IDLE"?"bg-blue-500/20 text-blue-400":"bg-bambu-dark-tertiary"}`,children:y.state})]})]},y.id))}):o.jsx("p",{className:"text-bambu-gray",children:t("system.noPrintersConnected","No printers connected")})]}),o.jsx(Bu,{title:t("system.storage","Storage"),icon:pR,children:o.jsxs("div",{className:"space-y-4",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[o.jsx("span",{className:"text-bambu-gray",children:t("system.diskUsage","Disk Usage")}),o.jsxs("span",{className:"text-white",children:[c.storage.disk_used_formatted," / ",c.storage.disk_total_formatted]})]}),o.jsx(t9,{percent:c.storage.disk_percent_used,color:v}),o.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),"%)"]})]}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[o.jsx(Yr,{icon:co,label:t("system.archiveStorage","Archive Storage"),value:c.storage.archive_size_formatted}),o.jsx(Yr,{icon:Ep,label:t("system.databaseSize","Database Size"),value:c.storage.database_size_formatted})]})]})}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[o.jsx(Bu,{title:t("system.memory","Memory"),icon:eue,children:o.jsx("div",{className:"space-y-4",children:o.jsxs("div",{children:[o.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[o.jsx("span",{className:"text-bambu-gray",children:t("system.memoryUsage","Memory Usage")}),o.jsxs("span",{className:"text-white",children:[c.memory.used_formatted," / ",c.memory.total_formatted]})]}),o.jsx(t9,{percent:c.memory.percent_used,color:x}),o.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:[c.memory.available_formatted," ",t("system.available","available")]})]})})}),o.jsx(Bu,{title:t("system.cpu","CPU"),icon:Cv,children:o.jsx("div",{className:"space-y-4",children:o.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[o.jsx(Yr,{icon:Cv,label:t("system.cores","Cores"),value:c.cpu.count,subValue:`${c.cpu.count_logical} logical`}),o.jsx(Yr,{icon:Cv,label:t("system.usage","Usage"),value:`${c.cpu.percent}%`})]})})})]}),o.jsx(Bu,{title:t("system.systemDetails","System Details"),icon:ph,children:o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsx(Yr,{icon:ph,label:t("system.os","Operating System"),value:c.system.platform,subValue:c.system.platform_release}),o.jsx(Yr,{icon:Cv,label:t("system.architecture","Architecture"),value:c.system.architecture}),o.jsx(Yr,{icon:ph,label:t("system.python","Python"),value:c.system.python_version}),o.jsx(Yr,{icon:ur,label:t("system.bootTime","Boot Time"),value:new Date(c.system.boot_time).toLocaleString()})]})})]})}function QWe(){const t=S.useRef(null),e=S.useRef(null),n=dn(),[r,i]=S.useState(!1),a=S.useRef(new Set),s=S.useRef(null),l=S.useRef(new Map),c=S.useRef(null),u=S.useRef([]),d=S.useRef(!1),f=S.useRef(()=>{}),g=S.useCallback(()=>{if(d.current||u.current.length===0)return;d.current=!0;const E=()=>{const w=u.current.shift();w?requestAnimationFrame(()=>{f.current(w),u.current.length>0?setTimeout(E,16):d.current=!1}):d.current=!1};E()},[]),p=S.useCallback(()=>{if(t.current?.readyState===WebSocket.OPEN)return;const w=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.host}/api/v1/ws`,A=new WebSocket(w);let T=null;A.onopen=()=>{console.log("[WebSocket] Connected"),i(!0),T=window.setInterval(()=>{A.readyState===WebSocket.OPEN&&A.send(JSON.stringify({type:"ping"}))},3e4)},A.onmessage=C=>{try{const N=JSON.parse(C.data);N.type==="printer_status"&&N.printer_id!==void 0&&N.data?f.current(N):(u.current.push(N),g())}catch{}},A.onclose=C=>{console.log("[WebSocket] Closed",C.code,C.reason),T&&(clearInterval(T),T=null),i(!1),t.current=null,e.current=window.setTimeout(()=>{p()},3e3)},A.onerror=C=>{console.error("[WebSocket] Error",C),A.close()},t.current=A},[]),b=S.useCallback((E,w)=>{const A=l.current.get(E)||{};l.current.set(E,{...A,...w}),c.current||(c.current=window.setTimeout(()=>{const T=new Map(l.current);l.current.clear(),c.current=null,requestAnimationFrame(()=>{T.forEach((C,N)=>{n.setQueryData(["printerStatus",N],O=>{const k={...O,...C};return k.wifi_signal==null&&O?.wifi_signal!=null&&(k.wifi_signal=O.wifi_signal),k})})})},100))},[n]),v=S.useCallback(E=>{a.current.add(E),s.current&&clearTimeout(s.current),s.current=window.setTimeout(()=>{const w=Array.from(a.current);a.current.clear(),s.current=null;let A=0;w.forEach(T=>{setTimeout(()=>{requestAnimationFrame(()=>{n.invalidateQueries({queryKey:[T]})})},A),A+=500})},3e3)},[n]),x=S.useCallback(E=>{switch(E.type){case"printer_status":E.printer_id!==void 0&&E.data&&b(E.printer_id,E.data);break;case"print_start":E.printer_id!==void 0&&n.invalidateQueries({queryKey:["printerStatus",E.printer_id]});break;case"print_complete":E.printer_id!==void 0&&n.invalidateQueries({queryKey:["printerStatus",E.printer_id]}),v("archives"),v("archiveStats");break;case"archive_created":v("archives"),v("archiveStats");break;case"archive_updated":v("archives");break}},[n,v,b]);S.useEffect(()=>{f.current=x},[x]),S.useEffect(()=>(p(),()=>{e.current&&clearTimeout(e.current),s.current&&clearTimeout(s.current),c.current&&clearTimeout(c.current),t.current&&t.current.close()}),[p]);const y=S.useCallback(E=>{t.current?.readyState===WebSocket.OPEN&&t.current.send(JSON.stringify(E))},[]);return{isConnected:r,sendMessage:y}}const JWe=new foe({defaultOptions:{queries:{staleTime:1e3*60,retry:1}}});function eqe({children:t}){return QWe(),o.jsx(o.Fragment,{children:t})}function tqe(){return o.jsx(Hde,{children:o.jsx(Jde,{children:o.jsx(moe,{client:JWe,children:o.jsx(eqe,{children:o.jsx(Ose,{children:o.jsxs(hse,{children:[o.jsx(Za,{path:"/camera/:printerId",element:o.jsx(XWe,{})}),o.jsxs(Za,{path:"/",element:o.jsx(Yde,{}),children:[o.jsx(Za,{index:!0,element:o.jsx(vCe,{})}),o.jsx(Za,{path:"archives",element:o.jsx(J7e,{})}),o.jsx(Za,{path:"queue",element:o.jsx(F9e,{})}),o.jsx(Za,{path:"stats",element:o.jsx(Q9e,{})}),o.jsx(Za,{path:"profiles",element:o.jsx(jWe,{})}),o.jsx(Za,{path:"maintenance",element:o.jsx(LWe,{})}),o.jsx(Za,{path:"projects",element:o.jsx(FWe,{})}),o.jsx(Za,{path:"projects/:id",element:o.jsx(GWe,{})}),o.jsx(Za,{path:"settings",element:o.jsx(_We,{})}),o.jsx(Za,{path:"system",element:o.jsx(ZWe,{})}),o.jsx(Za,{path:"external/:id",element:o.jsx(YWe,{})})]})]})})})})})})}gie.createRoot(document.getElementById("root")).render(o.jsx(S.StrictMode,{children:o.jsx(tqe,{})})); +`+n+"}"}return String(t)}function NWe({setting:t,onClose:e,onDeleted:n,onDuplicate:r,onEdit:i}){const{showToast:a}=vr(),s=dn(),[l,c]=S.useState(!1),{data:u,isLoading:d}=pt({queryKey:["cloudSettingDetail",t.setting_id],queryFn:()=>_e.getCloudSettingDetail(t.setting_id)}),f=_t({mutationFn:()=>_e.deleteCloudSetting(t.setting_id),onSuccess:()=>{a("Preset deleted"),s.invalidateQueries({queryKey:["cloudSettings"]}),n()},onError:b=>a(b.message,"error")}),g=wb(t.setting_id),p=qL(t.name,u?.setting?.inherits);return o.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:o.jsx(At,{className:"w-full max-w-3xl max-h-[90vh] flex flex-col overflow-hidden",children:o.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[o.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("h2",{className:"text-xl font-semibold text-white truncate",children:t.name}),g&&o.jsx("span",{className:"px-2 py-0.5 text-xs font-medium bg-bambu-green/20 text-bambu-green rounded-full",children:"Editable"})]}),o.jsxs("div",{className:"flex items-center gap-2 mt-1 text-sm text-bambu-gray",children:[o.jsxs("span",{className:"capitalize",children:[t.type," preset"]}),p.printer&&o.jsxs(o.Fragment,{children:[o.jsx("span",{children:"•"}),o.jsx("span",{children:p.printer})]})]})]}),o.jsx("button",{onClick:e,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:o.jsx(Wt,{className:"w-5 h-5"})})]}),o.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto p-4",children:d?o.jsx("div",{className:"flex items-center justify-center py-16",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?o.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:Zj(u)}):o.jsx("div",{className:"text-center py-16 text-bambu-gray",children:"Failed to load preset details"})}),l?o.jsxs("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary bg-red-500/5",children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 text-red-400",children:[o.jsx(qn,{className:"w-5 h-5"}),o.jsx("span",{className:"font-medium",children:"Delete this preset?"})]}),o.jsxs("p",{className:"text-sm text-bambu-gray mb-4",children:['This will permanently delete "',t.name,'" from Bambu Cloud. This cannot be undone.']}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:()=>c(!1),disabled:f.isPending,className:"flex-1",children:"Cancel"}),o.jsxs(Pe,{variant:"danger",onClick:()=>f.mutate(),disabled:f.isPending,className:"flex-1",children:[f.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):o.jsx(Rn,{className:"w-4 h-4"}),"Delete"]})]})]}):o.jsx("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary",children:o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:e,className:"flex-1",children:"Close"}),o.jsxs(Pe,{variant:"secondary",onClick:r,children:[o.jsx(vl,{className:"w-4 h-4"}),"Duplicate"]}),g&&o.jsxs(o.Fragment,{children:[o.jsxs(Pe,{variant:"secondary",onClick:i,disabled:d||!u,children:[o.jsx(Bc,{className:"w-4 h-4"}),"Edit"]}),o.jsx(Pe,{variant:"danger",onClick:()=>c(!0),children:o.jsx(Rn,{className:"w-4 h-4"})})]})]})})]})})})}function Qte(){try{const t=localStorage.getItem("bambusy_preset_templates");return t?JSON.parse(t):[]}catch{return[]}}function Jte(t){localStorage.setItem("bambusy_preset_templates",JSON.stringify(t))}function CWe({onClose:t,onApply:e}){const{showToast:n}=vr(),[r,i]=S.useState(Qte),[a,s]=S.useState("all"),[l,c]=S.useState(null),[u,d]=S.useState(""),[f,g]=S.useState(""),[p,b]=S.useState("{}"),[v,x]=S.useState(null),[y,E]=S.useState(null),w=a==="all"?r:r.filter(I=>I.type===a),A=I=>{i(I),Jte(I)},T=I=>{const q=r.filter(R=>R.id!==I);A(q),E(null),n("Template deleted")},C=I=>{c(I.id),d(I.name),g(I.description),b(JSON.stringify(I.settings,null,2)),x(null)},N=()=>{if(!(!l||!u.trim()))try{const I=JSON.parse(p),q=r.map(R=>R.id===l?{...R,name:u.trim(),description:f.trim(),settings:I}:R);A(q),c(null),n("Template updated")}catch(I){x(I.message)}},O=()=>{c(null),d(""),g(""),b("{}"),x(null)},k=I=>{const q=r.map(R=>R.id===I?{...R,showInModal:!R.showInModal}:R);A(q)},j={filament:{label:"Filament",icon:uR,color:"text-amber-400"},print:{label:"Process",icon:aM,color:"text-blue-400"},printer:{label:"Printer",icon:qi,color:"text-purple-400"}},B=y?r.find(I=>I.id===y):null;return S.useEffect(()=>{const I=q=>{q.key==="Escape"&&(y?E(null):l?O():t())};return window.addEventListener("keydown",I),()=>window.removeEventListener("keydown",I)},[y,l,t]),o.jsxs("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:[B&&o.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-60",children:o.jsx(At,{className:"w-full max-w-md",children:o.jsxs(Tt,{className:"p-6",children:[o.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[o.jsx("div",{className:"p-2 bg-red-500/20 rounded-lg",children:o.jsx(qn,{className:"w-6 h-6 text-red-400"})}),o.jsxs("div",{children:[o.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Template"}),o.jsx("p",{className:"text-sm text-bambu-gray",children:"This action cannot be undone"})]})]}),o.jsxs("p",{className:"text-white mb-6",children:['Are you sure you want to delete "',o.jsx("span",{className:"font-medium",children:B.name}),'"?']}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:()=>E(null),className:"flex-1",children:"Cancel"}),o.jsxs(Pe,{onClick:()=>T(y),className:"flex-1 bg-red-500 hover:bg-red-600",children:[o.jsx(Rn,{className:"w-4 h-4"}),"Delete"]})]})]})})}),o.jsx(At,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",children:o.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[o.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(O0,{className:"w-5 h-5 text-amber-400"}),"Quick Templates"]}),o.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:o.jsx(Wt,{className:"w-5 h-5"})})]}),o.jsxs("div",{className:"flex items-center gap-2 p-4 border-b border-bambu-dark-tertiary",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Type:"}),["all","filament","print","printer"].map(I=>o.jsx("button",{onClick:()=>s(I),className:`px-3 py-1 text-sm rounded-lg transition-colors ${a===I?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:I==="all"?"All":j[I].label},I))]}),o.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:w.length===0?o.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[o.jsx(O0,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),o.jsx("p",{children:"No templates yet"}),o.jsx("p",{className:"text-sm mt-1",children:"Create templates from the preset editor"})]}):o.jsx("div",{className:"space-y-2",children:w.map(I=>{const q=j[I.type],R=q.icon;return l===I.id?o.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg border border-bambu-green",children:[o.jsxs("div",{className:"grid grid-cols-2 gap-3 mb-3",children:[o.jsx("input",{type:"text",value:u,onChange:W=>d(W.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}),o.jsx("input",{type:"text",value:f,onChange:W=>g(W.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"})]}),o.jsxs("div",{className:"mb-3",children:[o.jsx("label",{className:"text-xs text-bambu-gray mb-1 block",children:"Settings (JSON)"}),o.jsx("textarea",{value:p,onChange:W=>{b(W.target.value),x(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 ${v?"border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`}),v&&o.jsx("p",{className:"text-xs text-red-400 mt-1",children:v})]}),o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{size:"sm",onClick:N,disabled:!u.trim(),children:[o.jsx(Ts,{className:"w-4 h-4"}),"Save"]}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:O,children:"Cancel"})]})]},I.id):o.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:[o.jsx(R,{className:`w-5 h-5 ${q.color} flex-shrink-0`}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("p",{className:"text-sm font-medium text-white",children:I.name}),o.jsx("p",{className:"text-xs text-bambu-gray truncate",children:I.description})]}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark px-2 py-1 bg-bambu-dark-secondary rounded",children:[Object.keys(I.settings).length," fields"]}),o.jsx("button",{onClick:()=>k(I.id),className:`p-1 transition-colors ${I.showInModal?"text-bambu-green hover:text-bambu-green/70":"text-bambu-gray hover:text-white"}`,title:I.showInModal?"Shown in modals":"Hidden in modals",children:I.showInModal?o.jsx(tf,{className:"w-4 h-4"}):o.jsx(nM,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>e(I),className:"px-3 py-1 text-xs bg-bambu-green/20 text-bambu-green rounded hover:bg-bambu-green/30 transition-colors",children:"Apply"}),o.jsx("button",{onClick:()=>C(I),className:"p-1 text-bambu-gray hover:text-white",title:"Edit",children:o.jsx(Bc,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>E(I.id),className:"p-1 text-bambu-gray hover:text-red-400",title:"Delete",children:o.jsx(Rn,{className:"w-4 h-4"})})]},I.id)})})})]})})]})}function ene({onClose:t,leftPreset:e,rightPreset:n,leftLabel:r,rightLabel:i}){const[a,s]=S.useState("changes"),[l,c]=S.useState(""),u=S.useMemo(()=>{const p=new Set([...Object.keys(e),...Object.keys(n)]),b=[];for(const v of p){if(v==="inherits"||v==="version")continue;const x=e[v],y=n[v],E=v in e,w=v in n;let A;!E&&w?A="added":E&&!w?A="removed":JSON.stringify(x)!==JSON.stringify(y)?A="changed":A="same",b.push({key:v,left:x,right:y,status:A})}return b.sort((v,x)=>{const y={changed:0,added:1,removed:2,same:3};return y[v.status]!==y[x.status]?y[v.status]-y[x.status]:v.key.localeCompare(x.key)})},[e,n]),d=S.useMemo(()=>{let p=[...u];if(a==="changes"&&(p=p.filter(b=>b.status!=="same")),l){const b=l.toLowerCase();p=p.filter(v=>v.key.toLowerCase().includes(b)||String(v.left).toLowerCase().includes(b)||String(v.right).toLowerCase().includes(b))}return p},[u,a,l]),f=S.useMemo(()=>({added:u.filter(p=>p.status==="added").length,removed:u.filter(p=>p.status==="removed").length,changed:u.filter(p=>p.status==="changed").length,same:u.filter(p=>p.status==="same").length}),[u]),g=p=>{if(p===void 0)return"—";if(p===null)return"null";if(Array.isArray(p))return p.length===0?"[]":p.length===1?String(p[0]):p.join(", ");if(typeof p=="object")return JSON.stringify(p);const b=String(p);if(b.includes("\\n")||b.length>100){const v=b.split("\\n").length;if(v>1)return`[${v} lines of G-code/script]`;if(b.length>100)return b.substring(0,100)+"…"}return b};return S.useEffect(()=>{const p=b=>{b.key==="Escape"&&t()};return window.addEventListener("keydown",p),()=>window.removeEventListener("keydown",p)},[t]),o.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:o.jsx(At,{className:"w-full max-w-4xl max-h-[85vh] flex flex-col overflow-hidden",children:o.jsxs(Tt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[o.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(dp,{className:"w-5 h-5 text-blue-400"}),"Compare Presets"]}),o.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:o.jsx(Wt,{className:"w-5 h-5"})})]}),o.jsxs("div",{className:"flex-shrink-0 grid grid-cols-2 gap-4 p-4 border-b border-bambu-dark-tertiary bg-bambu-dark",children:[o.jsxs("div",{className:"text-center",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Left:"}),o.jsx("p",{className:"text-white font-medium truncate",children:r})]}),o.jsxs("div",{className:"text-center",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Right:"}),o.jsx("p",{className:"text-white font-medium truncate",children:i})]})]}),o.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[o.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[o.jsx(fr,{className:"w-3.5 h-3.5"}),f.added," added"]}),o.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[o.jsx(n3,{className:"w-3.5 h-3.5"}),f.removed," removed"]}),o.jsxs("span",{className:"flex items-center gap-1 text-amber-400",children:[o.jsx(Zk,{className:"w-3.5 h-3.5"}),f.changed," changed"]}),o.jsxs("span",{className:"flex items-center gap-1 text-bambu-gray",children:[o.jsx(YA,{className:"w-3.5 h-3.5"}),f.same," same"]})]}),o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsxs("div",{className:"relative",children:[o.jsx(_l,{className:"absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),o.jsx("input",{type:"text",value:l,onChange:p=>c(p.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&&o.jsxs("div",{className:"flex rounded overflow-hidden border border-bambu-dark-tertiary",children:[o.jsx("button",{onClick:()=>s("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"}),o.jsx("button",{onClick:()=>s("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"})]})]})]}),o.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto",children:d.length===0?o.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[o.jsx(YA,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),o.jsx("p",{children:a==="changes"?"No differences found":"No fields match the search"})]}):o.jsxs("table",{className:"w-full",children:[o.jsx("thead",{className:"sticky top-0 bg-bambu-dark-secondary",children:o.jsxs("tr",{className:"text-sm text-bambu-gray border-b border-bambu-dark-tertiary",children:[o.jsx("th",{className:"text-left p-3 w-1/3",children:"Field"}),o.jsx("th",{className:"text-left p-3 w-1/3",children:r}),o.jsx("th",{className:"text-left p-3 w-1/3",children:i})]})}),o.jsx("tbody",{children:d.map(p=>{const b={added:"bg-green-500/10",removed:"bg-red-500/10",changed:"bg-amber-500/10",same:""}[p.status],v={added:o.jsx(fr,{className:"w-3.5 h-3.5 text-green-400"}),removed:o.jsx(n3,{className:"w-3.5 h-3.5 text-red-400"}),changed:o.jsx(Zk,{className:"w-3.5 h-3.5 text-amber-400"}),same:o.jsx(YA,{className:"w-3.5 h-3.5 text-bambu-gray-dark"})}[p.status];return o.jsxs("tr",{className:`border-b border-bambu-dark-tertiary ${b}`,children:[o.jsx("td",{className:"p-3",children:o.jsxs("div",{className:"flex items-center gap-2",children:[v,o.jsx("span",{className:"text-sm text-white font-mono",children:p.key})]})}),o.jsx("td",{className:"p-3",children:o.jsx("span",{className:`text-sm font-mono break-all ${p.status==="removed"?"text-red-300":p.status==="changed"?"text-white":"text-bambu-gray"}`,children:g(p.left)})}),o.jsx("td",{className:"p-3",children:o.jsx("span",{className:`text-sm font-mono break-all ${p.status==="added"?"text-green-300":p.status==="changed"?"text-white":"text-bambu-gray"}`,children:g(p.right)})})]},p.key)})})]})})]})})})}function kWe({onClose:t,initialData:e,allPresets:n}){const{showToast:r}=vr(),i=dn(),a=!!e?.setting_id,[s,l]=S.useState("common"),[c,u]=S.useState(e?.type||"filament"),[d,f]=S.useState(e?.name?a?e.name:`${e.name} (Copy)`:""),[g,p]=S.useState(e?.base_id||""),[b,v]=S.useState(""),[x,y]=S.useState(e?.setting||{inherits:""}),[E,w]=S.useState(JSON.stringify(e?.setting||{inherits:""},null,2)),[A,T]=S.useState(null),[C,N]=S.useState(""),[O,k]=S.useState(!1),[j,B]=S.useState(""),[I,q]=S.useState(!1),[R,W]=S.useState(Qte),[K,G]=S.useState(!1),[J,ee]=S.useState(""),[Y,F]=S.useState(""),[$,ne]=S.useState(!0),[oe,se]=S.useState(null),[pe,Z]=S.useState(!1),Me=S.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer})[c]||[],[n,c]),he=S.useMemo(()=>Me.filter(ae=>wb(ae.setting_id)),[Me]),{data:ue}=pt({queryKey:["cloudFields",c],queryFn:()=>_e.getCloudFields(c==="print"?"process":c),staleTime:1e3*60*60}),{data:Ae}=pt({queryKey:["allPresetDetails",c,he.map(ae=>ae.setting_id).join(",")],queryFn:async()=>{const ae={};for(let He=0;He{try{const rt=await _e.getCloudSettingDetail(ge.setting_id);return{id:ge.setting_id,detail:rt}}catch{return null}}))).forEach(ge=>{ge&&(ae[ge.id]=ge.detail)})}return ae},enabled:he.length>0,staleTime:1e3*60*10}),{data:fe,isLoading:je}=pt({queryKey:["cloudSettingDetail",g],queryFn:()=>_e.getCloudSettingDetail(g),enabled:!!g});S.useEffect(()=>{s!=="json"&&w(JSON.stringify(x,null,2))},[x,s]);const z=S.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer}[c]||[]).filter($e=>!wb($e.setting_id)).sort(($e,He)=>$e.name.localeCompare(He.name)),[n,c]);S.useEffect(()=>{if(!g)return;const ae=z.find($e=>$e.setting_id===g);ae&&(v(ae.name),a||(y({inherits:ae.name}),w(JSON.stringify({inherits:ae.name},null,2))))},[g,z,a]);const H=S.useMemo(()=>{const ae=ue?.fields||[],$e=new Set(ae.map(ge=>ge.key)),He=new Set,Ze=new Set(["inherits","updated_time","compatible_printers","compatible_prints"]);Ae&&Object.values(Ae).forEach(ge=>{ge?.setting&&Object.keys(ge.setting).forEach(rt=>{!$e.has(rt)&&!Ze.has(rt)&&He.add(rt)})}),Object.keys(x).forEach(ge=>{!$e.has(ge)&&!Ze.has(ge)&&He.add(ge)});const ot=Array.from(He).sort().map(ge=>({key:ge,label:ge.replace(/_/g," ").replace(/\b\w/g,rt=>rt.toUpperCase()),type:"text",category:"discovered",description:"Discovered from presets"}));return[...ae,...ot]},[ue,Ae,x]),re=H.filter(ae=>ae.label.toLowerCase().includes(C.toLowerCase())||ae.key.toLowerCase().includes(C.toLowerCase())),V=()=>{if(j.trim()){const ae=j.trim().toLowerCase().replace(/\s+/g,"_");Q(ae,""),B(""),q(!1),r(`Field "${ae}" added`)}},Q=(ae,$e)=>{y(He=>{const Ze={...He};return $e===""||$e===void 0?delete Ze[ae]:Ze[ae]=$e,Ze})},le=ae=>{y($e=>({...$e,...ae.settings})),se(ae.name),r("Template applied")},ye=()=>{if(!J.trim())return;const ae={...x};if(delete ae.inherits,Object.keys(ae).length===0){r("No overrides to save","error");return}const $e={id:Date.now().toString(),name:J.trim(),description:Y.trim()||"Custom template",type:c,settings:ae,showInModal:$},He=[...R,$e];W(He),Jte(He),G(!1),ee(""),F(""),ne(!0),r("Template saved")},Ne=S.useMemo(()=>R.filter(ae=>ae.type===c&&ae.showInModal),[c,R]),Ee=ae=>{w(ae);try{const $e=JSON.parse(ae);y($e),T(null)}catch($e){T($e.message)}},U=ae=>{ae.preventDefault(),k(!1);const $e=ae.dataTransfer.files[0];if($e&&$e.name.endsWith(".json")){const He=new FileReader;He.onload=Ze=>{try{const ot=Ze.target?.result,ge=JSON.parse(ot),rt=ge.setting||ge;y(xe=>({...xe,...rt})),w(JSON.stringify({...x,...rt},null,2)),r("File imported successfully")}catch{r("Invalid JSON file","error")}},He.readAsText($e)}},L=_t({mutationFn:()=>{const ae={...x},$e=c==="filament"?"filament_settings_id":c==="print"?"print_settings_id":"printer_settings_id";ae[$e]=`"${d}"`;const He={type:c,name:d,base_id:g,setting:ae};return _e.createCloudSetting(He)},onSuccess:async()=>{r("Preset created successfully"),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ae=>r(ae.message,"error")}),te=_t({mutationFn:()=>{if(!e?.setting_id)throw new Error("No setting ID for update");return _e.updateCloudSetting(e.setting_id,{name:d,setting:x})},onSuccess:async()=>{r("Preset updated successfully"),i.removeQueries({queryKey:["cloudSettingDetail"]}),await i.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ae=>r(ae.message,"error")}),ve=a?te:L,Se=fe?.setting?.inherits,be=Se?z.find(ae=>ae.name===Se):void 0,{data:Ke}=pt({queryKey:["cloudSettingDetail",be?.setting_id],queryFn:()=>_e.getCloudSettingDetail(be.setting_id),enabled:!!be?.setting_id}),Ge=S.useMemo(()=>{const ae=Ke?.setting||{},$e=fe?.setting||{},He=g&&Ae?.[g]?.setting?Ae[g].setting:{};return{...ae,...$e,...He}},[g,fe,Ke,Ae]),qe=ae=>{if(ae==null)return"";if(Array.isArray(ae)){const $e=[...new Set(ae.map(He=>String(He)))];return $e.length===1?$e[0]:ae.join(", ")}return String(ae)},lt=ae=>{const $e=x[ae.key],He=Ge[ae.key],Ze=qe(He),ot=je?"Loading...":Ze||"",ge="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(ae.type==="boolean"){const rt=$e==="1"||$e===void 0&&He==="1";return o.jsx("button",{type:"button",onClick:()=>Q(ae.key,$e==="1"?"0":"1"),className:`w-8 h-5 rounded-full transition-colors ${rt?"bg-bambu-green":"bg-bambu-dark-tertiary"}`,children:o.jsx("div",{className:`w-4 h-4 rounded-full bg-white shadow transition-transform ${rt?"translate-x-3.5":"translate-x-0.5"}`})})}return ae.type==="select"?o.jsxs("select",{value:$e||"",onChange:rt=>Q(ae.key,rt.target.value),className:ge,children:[o.jsx("option",{value:"",children:ot}),ae.options?.map(rt=>o.jsx("option",{value:rt.value,children:rt.label},rt.value))]}):o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("input",{type:ae.type==="number"?"number":"text",value:$e!==void 0?String($e):"",onChange:rt=>Q(ae.key,rt.target.value),step:ae.step,min:ae.min,max:ae.max,placeholder:ot,className:ge}),ae.unit&&o.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:ae.unit})]})},me=S.useMemo(()=>fe?.setting?fe.setting:{},[fe]);return o.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onDragOver:ae=>{ae.preventDefault(),k(!0)},onDragLeave:()=>k(!1),onDrop:U,children:[pe&&g&&o.jsx(ene,{onClose:()=>Z(!1),leftPreset:me,rightPreset:x,leftLabel:`Base: ${b||g}`,rightLabel:`Current: ${d||"New Preset"}`}),o.jsx(At,{className:"w-full max-w-6xl max-h-[90vh] flex flex-col",children:o.jsxs(Tt,{className:"p-0 flex flex-col h-full",children:[o.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[o.jsxs("div",{children:[o.jsx("h2",{className:"text-xl font-semibold text-white",children:a?"Edit Preset":e?"Duplicate Preset":"Create New Preset"}),o.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize settings for your new preset"})]}),o.jsxs("div",{className:"flex items-center gap-2",children:[g&&o.jsxs("button",{onClick:()=>Z(!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:[o.jsx(dp,{className:"w-4 h-4"}),"Compare"]}),o.jsx("button",{onClick:t,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:o.jsx(Wt,{className:"w-5 h-5"})})]})]}),O&&o.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:o.jsxs("div",{className:"text-center",children:[o.jsx(ua,{className:"w-12 h-12 text-bambu-green mx-auto mb-2"}),o.jsx("p",{className:"text-bambu-green font-medium",children:"Drop JSON file to import"})]})}),o.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary space-y-3",children:[o.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Type"}),o.jsxs("select",{value:c,onChange:ae=>{u(ae.target.value),p("")},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:[o.jsx("option",{value:"filament",children:"Filament"}),o.jsx("option",{value:"print",children:"Process"}),o.jsx("option",{value:"printer",children:"Printer"})]})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Base Preset"}),o.jsxs("select",{value:g,onChange:ae=>p(ae.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:[o.jsx("option",{value:"",children:"Select base preset..."}),z.map(ae=>o.jsx("option",{value:ae.setting_id,children:ae.name},ae.setting_id))]})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Preset Name"}),o.jsx("input",{type:"text",value:d,onChange:ae=>f(ae.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"})]})]}),b&&o.jsx("div",{className:"text-xs text-bambu-gray",children:o.jsxs("p",{className:"flex items-center gap-1",children:[o.jsx(go,{className:"w-3 h-3 text-bambu-green"}),"Inherits from: ",o.jsx("span",{className:"text-white",children:b}),je&&o.jsx(xt,{className:"w-3 h-3 animate-spin ml-1"})]})})]}),o.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary",children:[o.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 ${s==="common"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[o.jsx(TU,{className:"w-4 h-4"}),"Common"]}),o.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 ${s==="fields"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[o.jsx(xR,{className:"w-4 h-4"}),"All Fields"]}),o.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 ${s==="json"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[o.jsx(H9,{className:"w-4 h-4"}),"JSON",A&&o.jsx(Sa,{className:"w-3 h-3 text-red-400"})]}),o.jsx("div",{className:"flex-1"}),o.jsxs("button",{onClick:()=>{const ae={name:d,type:c,base_id:g,setting:x},$e=new Blob([JSON.stringify(ae,null,2)],{type:"application/json"}),He=URL.createObjectURL($e),Ze=document.createElement("a");Ze.href=He,Ze.download=`${d||"preset"}.json`,document.body.appendChild(Ze),Ze.click(),document.body.removeChild(Ze),URL.revokeObjectURL(He),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:[o.jsx(Wi,{className:"w-4 h-4"}),"Export"]}),o.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:[o.jsx(ua,{className:"w-4 h-4"}),"Import"]}),o.jsx("input",{id:"file-import",type:"file",accept:".json",className:"hidden",onChange:ae=>{const $e=ae.target.files?.[0];if($e){const He=new FileReader;He.onload=Ze=>{try{const ot=JSON.parse(Ze.target?.result),ge=ot.setting||ot;y(rt=>({...rt,...ge})),r("File imported")}catch{r("Invalid JSON","error")}},He.readAsText($e)}}})]}),o.jsxs("div",{className:"flex-1 overflow-y-auto p-4",children:[s==="common"&&o.jsxs("div",{className:"space-y-6",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:[o.jsx(O0,{className:"w-4 h-4 text-amber-400"}),"Quick Templates"]}),Object.keys(x).filter(ae=>ae!=="inherits").length>0&&o.jsxs("button",{onClick:()=>G(!K),className:"text-xs text-bambu-gray hover:text-white flex items-center gap-1 transition-colors",children:[o.jsx(Ts,{className:"w-3 h-3"}),"Save as template"]})]}),K&&o.jsxs("div",{className:"mb-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"grid grid-cols-2 gap-2 mb-2",children:[o.jsx("input",{type:"text",value:J,onChange:ae=>ee(ae.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}),o.jsx("input",{type:"text",value:Y,onChange:ae=>F(ae.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"})]}),o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{size:"sm",onClick:ye,disabled:!J.trim(),children:[o.jsx(Ts,{className:"w-3 h-3"}),"Save"]}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>G(!1),children:"Cancel"})]}),o.jsxs("button",{onClick:()=>ne(!$),className:`flex items-center gap-1.5 text-xs transition-colors ${$?"text-bambu-green":"text-bambu-gray hover:text-white"}`,children:[$?o.jsx(tf,{className:"w-3.5 h-3.5"}):o.jsx(nM,{className:"w-3.5 h-3.5"}),$?"Show in modals":"Hidden in modals"]})]})]}),oe&&o.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:[o.jsx(go,{className:"w-4 h-4 text-bambu-green"}),o.jsxs("span",{className:"text-sm text-bambu-green",children:["Template applied: ",o.jsx("span",{className:"font-medium",children:oe})]}),o.jsx("button",{onClick:()=>se(null),className:"ml-auto text-bambu-green/70 hover:text-bambu-green",children:o.jsx(Wt,{className:"w-4 h-4"})})]}),o.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[Ne.map(ae=>o.jsxs("button",{onClick:()=>le(ae),className:"p-3 text-left bg-bambu-dark border border-bambu-dark-tertiary rounded-lg hover:border-bambu-gray-dark transition-colors",children:[o.jsx("p",{className:"text-sm font-medium text-white",children:ae.name}),o.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:ae.description})]},ae.id)),Ne.length===0&&o.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."})]}),o.jsx("p",{className:"text-xs text-bambu-gray-dark mt-2 text-center",children:"Manage templates via the Templates button on the main page"})]}),o.jsxs("div",{children:[o.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Common Settings"}),o.jsx("div",{className:"grid grid-cols-2 gap-x-6 gap-y-3",children:H.slice(0,10).map(ae=>o.jsxs("div",{className:"flex items-center justify-between gap-4",children:[o.jsx("label",{className:"text-sm text-bambu-gray flex-shrink-0",children:ae.label}),o.jsx("div",{className:"w-48",children:lt(ae)})]},ae.key))})]}),Object.keys(x).length>1&&o.jsxs("div",{children:[o.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:"Current Overrides"}),o.jsx("div",{className:"flex flex-wrap gap-2",children:Object.entries(x).filter(([ae])=>ae!=="inherits").map(([ae,$e])=>o.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-green/10 text-bambu-green text-xs rounded",children:[ae,": ",String($e).slice(0,20),o.jsx("button",{onClick:()=>Q(ae,void 0),className:"hover:text-white",children:o.jsx(Wt,{className:"w-3 h-3"})})]},ae))})]})]}),s==="fields"&&o.jsxs("div",{className:"grid grid-cols-2 gap-6",style:{height:"400px"},children:[o.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[o.jsx("h3",{className:"text-sm font-medium text-white",children:"Available Fields"}),o.jsx("span",{className:"text-xs text-bambu-gray",children:Ae?`${H.length} fields`:"Loading..."})]}),o.jsxs("div",{className:"relative mb-3 flex-shrink-0",children:[o.jsx(_l,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),o.jsx("input",{type:"text",value:C,onChange:ae=>N(ae.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"})]}),o.jsxs("div",{className:"flex-1 overflow-y-auto space-y-1 pr-2 min-h-0",children:[re.filter(ae=>!(ae.key in x)).map(ae=>{const $e=Ge[ae.key],He=qe($e);return o.jsxs("div",{onClick:()=>{y(Ze=>({...Ze,[ae.key]:He||""}))},className:"flex items-center justify-between gap-2 p-2 rounded-lg hover:bg-bambu-dark-tertiary transition-colors cursor-pointer group",children:[o.jsxs("div",{className:"min-w-0 flex-1",children:[o.jsx("p",{className:"text-sm text-white truncate",children:ae.label}),o.jsx("p",{className:"text-xs text-bambu-gray-dark truncate",children:ae.key})]}),o.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[He&&o.jsxs("span",{className:"text-xs text-bambu-gray bg-bambu-dark px-2 py-0.5 rounded max-w-32 truncate",title:He,children:[He.slice(0,20),He.length>20?"...":""]}),o.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:o.jsx(fr,{className:"w-4 h-4 text-bambu-gray group-hover:text-bambu-green transition-colors"})})]})]},ae.key)}),re.filter(ae=>!(ae.key in x)).length===0&&o.jsx("p",{className:"text-center text-bambu-gray py-4 text-sm",children:C?"No matching fields":"All fields added"})]}),o.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:I?o.jsxs("div",{className:"flex gap-2",children:[o.jsx("input",{type:"text",value:j,onChange:ae=>B(ae.target.value),onKeyDown:ae=>ae.key==="Enter"&&V(),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}),o.jsx(Pe,{size:"sm",onClick:V,disabled:!j.trim(),children:o.jsx(fr,{className:"w-4 h-4"})}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>{q(!1),B("")},children:o.jsx(Wt,{className:"w-4 h-4"})})]}):o.jsxs("button",{onClick:()=>q(!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:[o.jsx(fr,{className:"w-4 h-4"}),"Add custom field"]})})]}),o.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[o.jsx("h3",{className:"text-sm font-medium text-white",children:"Your Overrides"}),o.jsxs("span",{className:"text-xs text-bambu-gray",children:[Object.keys(x).filter(ae=>ae!=="inherits").length," fields"]})]}),o.jsxs("div",{className:"flex-1 overflow-y-auto space-y-2 pr-2 min-h-0",children:[Object.entries(x).filter(([ae])=>ae!=="inherits").map(([ae,$e])=>{const He=H.find(Ze=>Ze.key===ae);return o.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center justify-between mb-2",children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-sm font-medium text-white",children:He?.label||ae}),o.jsx("p",{className:"text-xs text-bambu-gray-dark",children:ae})]}),o.jsx("button",{onClick:()=>Q(ae,void 0),className:"p-1 text-bambu-gray hover:text-red-400 transition-colors",children:o.jsx(Wt,{className:"w-4 h-4"})})]}),He?lt(He):o.jsx("input",{type:"text",value:String($e),onChange:Ze=>Q(ae,Ze.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"})]},ae)}),Object.keys(x).filter(ae=>ae!=="inherits").length===0&&o.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[o.jsx(TU,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),o.jsx("p",{className:"text-sm",children:"No overrides yet"}),o.jsx("p",{className:"text-xs mt-1",children:"Click fields on the left to add them"})]})]}),Object.keys(x).filter(ae=>ae!=="inherits").length>0&&o.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:o.jsxs("button",{onClick:()=>{G(!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:[o.jsx(Ts,{className:"w-4 h-4"}),"Save as template"]})})]})]}),s==="json"&&o.jsxs("div",{className:"space-y-2",children:[A&&o.jsxs("div",{className:"flex items-center gap-2 text-red-400 text-sm",children:[o.jsx(Sa,{className:"w-4 h-4"}),A]}),o.jsx("textarea",{value:E,onChange:ae=>Ee(ae.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 ${A?"border-red-500 focus:border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`,spellCheck:!1}),o.jsx("p",{className:"text-xs text-bambu-gray",children:"Tip: Drag & drop a .json file anywhere on this modal to import settings"})]})]}),o.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex gap-2",children:[o.jsx(Pe,{variant:"secondary",onClick:t,className:"flex-1",children:"Cancel"}),o.jsxs(Pe,{onClick:()=>ve.mutate(),disabled:ve.isPending||!d.trim()||!a&&!g||!!A,className:"flex-1",children:[ve.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):a?o.jsx(Ts,{className:"w-4 h-4"}):o.jsx(fr,{className:"w-4 h-4"}),a?"Save":e?"Duplicate":"Create"]})]})]})})]})}function PWe({settings:t,lastSyncTime:e,onRefresh:n,isRefreshing:r,printers:i}){const[a,s]=S.useState(""),[l,c]=S.useState("all"),[u,d]=S.useState("all"),[f,g]=S.useState("all"),[p,b]=S.useState("all"),[v,x]=S.useState("all"),[y,E]=S.useState("all"),[w,A]=S.useState(null),[T,C]=S.useState(!1),[N,O]=S.useState(!1),[k,j]=S.useState(null),[B,I]=S.useState(null),[q,R]=S.useState(null),[W,K]=S.useState(!1),[G,J]=S.useState([null,null]),[ee,Y]=S.useState(!1),[F,$]=S.useState(null),ne=dn(),oe=S.useMemo(()=>[...t.filament.map(re=>({...re,type:"filament"})),...t.printer.map(re=>({...re,type:"printer"})),...t.process.map(re=>({...re,type:"process"}))].map(re=>({...re,meta:qL(re.name)})),[t]),se=S.useMemo(()=>{const H=new Set,re=new Set,V=new Set;return oe.forEach(Q=>{Q.meta.nozzle&&H.add(Q.meta.nozzle),Q.meta.filamentType&&re.add(Q.meta.filamentType),Q.meta.layerHeight&&V.add(Q.meta.layerHeight)}),{printers:i.map(Q=>({id:Q.id.toString(),name:Q.name})),nozzles:Array.from(H).sort((Q,le)=>parseFloat(Q)-parseFloat(le)),filaments:Array.from(re).sort(),layerHeights:Array.from(V).sort((Q,le)=>parseFloat(Q)-parseFloat(le))}},[oe,i]),pe=S.useMemo(()=>f==="all"?null:i.find(re=>re.id.toString()===f)?.model||null,[f,i]),Z=S.useMemo(()=>oe.filter(H=>l==="all"||H.type===l).filter(H=>{if(u==="all")return!0;const re=wb(H.setting_id);return u==="custom"?re:!re}).filter(H=>{if(f==="all"||!pe)return!0;const re=H.meta.printer?.toLowerCase()||"",V=pe.toLowerCase();return re.includes(V)||V.includes(re)}).filter(H=>p==="all"||H.meta.nozzle===p).filter(H=>v==="all"||H.meta.filamentType===v).filter(H=>y==="all"||H.meta.layerHeight===y).filter(H=>a===""||H.name.toLowerCase().includes(a.toLowerCase())).sort((H,re)=>H.name.localeCompare(re.name)),[oe,l,u,f,pe,p,v,y,a]),Me=H=>{if(W){const re=G[0]?.setting_id===H.setting_id,V=G[1]?.setting_id===H.setting_id;if(re)J([G[1],null]);else if(V)J([G[0],null]);else if(!G[0])J([H,null]);else if(G[1]){if(G[0].type!==H.type)return;J([G[0],H])}else{if(G[0].type!==H.type)return;J([G[0],H])}}else A(H)},he=H=>{if(G[0]?.setting_id===H.setting_id)return 0;if(G[1]?.setting_id===H.setting_id)return 1},ue=async H=>{try{const re=await _e.getCloudSettingDetail(H.setting_id),V=H.type==="process"?"print":H.type;j({type:V,name:H.name,base_id:re.base_id||"GFSA00",setting:re.setting||{}}),A(null)}catch(re){console.error("Failed to fetch preset details for duplication:",re)}},Ae=async H=>{try{ne.removeQueries({queryKey:["cloudSettingDetail",H.setting_id]});const re=await _e.getCloudSettingDetail(H.setting_id),V=H.type==="process"?"print":H.type;I({type:V,name:H.name,base_id:re.base_id||"GFSA00",setting:re.setting||{},setting_id:H.setting_id}),A(null)}catch(re){console.error("Failed to fetch preset details for editing:",re)}},fe=()=>{c("all"),d("all"),g("all"),b("all"),x("all"),E("all"),s("")},je=l!=="all"||u!=="all"||f!=="all"||p!=="all"||v!=="all"||y!=="all"||a!=="",z=t.filament.length+t.printer.length+t.process.length;return o.jsxs(o.Fragment,{children:[o.jsxs("div",{className:"space-y-4 mb-6",children:[o.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[o.jsxs("div",{className:"relative flex-1",children:[o.jsx(_l,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),o.jsx("input",{type:"text",value:a,onChange:H=>s(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"})]}),o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{variant:W?"primary":"secondary",onClick:()=>{W?(K(!1),J([null,null])):K(!0)},children:[o.jsx(dp,{className:"w-4 h-4"}),W?"Cancel":"Compare"]}),o.jsxs(Pe,{variant:"secondary",onClick:()=>O(!0),children:[o.jsx(O0,{className:"w-4 h-4"}),"Templates"]}),o.jsxs(Pe,{variant:"secondary",onClick:n,disabled:r,children:[o.jsx(li,{className:`w-4 h-4 ${r?"animate-spin":""}`}),"Refresh"]}),o.jsxs(Pe,{onClick:()=>C(!0),children:[o.jsx(fr,{className:"w-4 h-4"}),"New Preset"]})]})]}),o.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[o.jsx(fR,{className:"w-4 h-4 text-bambu-gray"}),o.jsx(Ym,{label:"Type",value:l,options:[{value:"all",label:"All",count:z},{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)}),o.jsx(Ym,{label:"Owner",value:u,options:[{value:"all",label:"All"},{value:"custom",label:"My Presets"},{value:"builtin",label:"Built-in"}],onChange:H=>d(H)}),se.printers.length>0&&o.jsx(Ym,{label:"Printer",value:f,options:[{value:"all",label:"All"},...se.printers.map(H=>({value:H.id,label:H.name}))],onChange:g}),se.nozzles.length>0&&o.jsx(Ym,{label:"Nozzle",value:p,options:[{value:"all",label:"All"},...se.nozzles.map(H=>({value:H,label:H}))],onChange:b}),se.filaments.length>0&&(l==="all"||l==="filament")&&o.jsx(Ym,{label:"Filament",value:v,options:[{value:"all",label:"All"},...se.filaments.map(H=>({value:H,label:H}))],onChange:x}),se.layerHeights.length>0&&(l==="all"||l==="process")&&o.jsx(Ym,{label:"Layer",value:y,options:[{value:"all",label:"All"},...se.layerHeights.map(H=>({value:H,label:H}))],onChange:E}),je&&o.jsx("button",{onClick:fe,className:"px-3 py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:"Clear filters"})]})]}),W&&o.jsx("div",{className:"mb-4 p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{className:"flex items-center gap-4",children:[o.jsx(dp,{className:"w-5 h-5 text-blue-400"}),o.jsx("span",{className:"text-white font-medium",children:"Compare Mode"}),o.jsx("span",{className:"text-bambu-gray",children:G[0]?`Select another ${G[0].type} preset`:"Click two presets of the same type to compare"})]}),o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${G[0]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:G[0]?G[0].name:"1. Select first"}),o.jsx(Zk,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${G[1]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:G[1]?G[1].name:"2. Select second"})]}),G[0]&&G[1]&&o.jsxs(Pe,{size:"sm",onClick:async()=>{try{const[H,re]=await Promise.all([_e.getCloudSettingDetail(G[0].setting_id),_e.getCloudSettingDetail(G[1].setting_id)]);$([H.setting||{},re.setting||{}]),Y(!0)}catch{}},children:[o.jsx(dp,{className:"w-4 h-4"}),"Compare Now"]})]})]})}),o.jsxs("div",{className:"flex flex-wrap items-center gap-4 mb-4 text-sm text-bambu-gray",children:[e&&o.jsxs("div",{className:"flex items-center gap-1",children:[o.jsx(ur,{className:"w-3 h-3"}),"Last synced: ",EWe(e.toISOString())]}),o.jsxs("span",{children:["Showing ",Z.length," of ",z," presets"]}),o.jsxs("div",{className:"flex items-center gap-1",children:[o.jsx("span",{className:"w-1.5 h-1.5 rounded-full bg-bambu-green"}),o.jsx("span",{children:"= My preset (editable)"})]})]}),Z.length===0?o.jsxs("div",{className:"text-center py-16",children:[o.jsx(vd,{className:"w-12 h-12 text-bambu-gray-dark mx-auto mb-4"}),o.jsx("p",{className:"text-bambu-gray",children:"No presets found"}),je&&o.jsx("button",{onClick:fe,className:"mt-2 text-sm text-bambu-green hover:text-bambu-green-light",children:"Clear filters"})]}):o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[o.jsx(uR,{className:"w-4 h-4 text-amber-400"}),o.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Filament"}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",Z.filter(H=>H.type==="filament").length,")"]})]}),o.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[Z.filter(H=>H.type==="filament").map(H=>o.jsx(Ok,{setting:H,onClick:()=>Me(H),onDuplicate:()=>ue(H),compareMode:W,isCompareSelected:he(H)!==void 0,compareIndex:he(H),compareDisabled:W&&!!G[0]&&G[0].type!==H.type},H.setting_id)),Z.filter(H=>H.type==="filament").length===0&&o.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No filament presets"})]})]}),o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[o.jsx(aM,{className:"w-4 h-4 text-blue-400"}),o.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Process"}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",Z.filter(H=>H.type==="process").length,")"]})]}),o.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[Z.filter(H=>H.type==="process").map(H=>o.jsx(Ok,{setting:H,onClick:()=>Me(H),onDuplicate:()=>ue(H),compareMode:W,isCompareSelected:he(H)!==void 0,compareIndex:he(H),compareDisabled:W&&!!G[0]&&G[0].type!==H.type},H.setting_id)),Z.filter(H=>H.type==="process").length===0&&o.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No process presets"})]})]}),o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[o.jsx(qi,{className:"w-4 h-4 text-purple-400"}),o.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:"Printer"}),o.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",Z.filter(H=>H.type==="printer").length,")"]})]}),o.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[Z.filter(H=>H.type==="printer").map(H=>o.jsx(Ok,{setting:H,onClick:()=>Me(H),onDuplicate:()=>ue(H),compareMode:W,isCompareSelected:he(H)!==void 0,compareIndex:he(H),compareDisabled:W&&!!G[0]&&G[0].type!==H.type},H.setting_id)),Z.filter(H=>H.type==="printer").length===0&&o.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:"No printer presets"})]})]})]}),w&&o.jsx(NWe,{setting:w,onClose:()=>A(null),onDeleted:()=>A(null),onDuplicate:()=>ue(w),onEdit:()=>Ae(w)}),(T||k||B||q)&&o.jsx(kWe,{onClose:()=>{C(!1),j(null),I(null),R(null)},initialData:B||k||(q?{type:q.type,name:"",base_id:"",setting:q.setting}:void 0),allPresets:t}),N&&o.jsx(CWe,{onClose:()=>O(!1),onApply:H=>{R({type:H.type,setting:H.settings}),O(!1)}}),ee&&F&&G[0]&&G[1]&&o.jsx(ene,{onClose:()=>{Y(!1),$(null)},leftPreset:F[0],rightPreset:F[1],leftLabel:G[0].name,rightLabel:G[1].name})]})}function jWe(){const t=dn(),{showToast:e}=vr(),[n,r]=S.useState("cloud"),[i,a]=S.useState(),{data:s,isLoading:l}=pt({queryKey:["cloudStatus"],queryFn:_e.getCloudStatus}),{data:c=[]}=pt({queryKey:["printers"],queryFn:_e.getPrinters}),{data:u,isLoading:d,refetch:f,dataUpdatedAt:g}=pt({queryKey:["cloudSettings"],queryFn:()=>_e.getCloudSettings(),enabled:!!s?.is_authenticated,retry:!1,staleTime:1e3*60*5});S.useEffect(()=>{g&&a(new Date(g))},[g]);const p=_t({mutationFn:_e.cloudLogout,onSuccess:()=>{t.invalidateQueries({queryKey:["cloudStatus"]}),t.removeQueries({queryKey:["cloudSettings"]}),e("Logged out")}}),b=()=>{t.invalidateQueries({queryKey:["cloudStatus"]})};return l?o.jsx("div",{className:"p-4 md:p-8 flex items-center justify-center min-h-[400px]",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):o.jsxs("div",{className:"p-6 lg:p-8",children:[o.jsxs("div",{className:"mb-6",children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:"Profiles"}),o.jsx("p",{className:"text-bambu-gray",children:"Manage your slicer presets and pressure advance calibrations"})]}),o.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary mb-6",children:[o.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:[o.jsx(tM,{className:"w-4 h-4"}),"Cloud Profiles"]}),o.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:[o.jsx(rM,{className:"w-4 h-4"}),"K-Profiles"]})]}),n==="cloud"&&o.jsxs(o.Fragment,{children:[s?.is_authenticated&&o.jsxs("div",{className:"flex items-center justify-between p-3 mb-6 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green animate-pulse"}),o.jsxs("span",{className:"text-sm text-bambu-gray",children:["Connected as ",o.jsx("span",{className:"text-white",children:s.email})]})]}),o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>p.mutate(),disabled:p.isPending,children:[o.jsx(Vce,{className:"w-4 h-4"}),"Logout"]})]}),s?.is_authenticated?d?o.jsx("div",{className:"flex items-center justify-center py-16",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):u?o.jsx(PWe,{settings:u,lastSyncTime:i,onRefresh:()=>f(),isRefreshing:d,printers:c}):o.jsxs("div",{className:"text-center py-16",children:[o.jsx("p",{className:"text-bambu-gray mb-4",children:"Failed to load profiles"}),o.jsx(Pe,{onClick:()=>f(),children:"Retry"})]}):o.jsx(AWe,{onSuccess:b})]}),n==="kprofiles"&&o.jsx(MWe,{}),o.jsx(TWe,{})]})}const i0={Droplet:uR,Flame:ece,Ruler:Rue,Sparkles:O0,Square:af,Cable:Zoe,Wrench:_d,Calendar:Jr,Timer:td,Cog:Tle,Fan:$9,Zap:sf,Wind:tW,Thermometer:sM,Layers:vd,Box:bd,Target:X9,RefreshCw:li,Settings:wR,Filter:fR,CircleDot:mle};function N0(t){return t&&i0[t]||_d}function Zm(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 Lk(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 RWe({item:t,onPerform:e,onToggle:n}){const r=N0(t.maintenance_type_icon),i=t.interval_type||"hours",s=(()=>{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 ${Zm(Math.abs(f),"days")}`:t.is_warning?`Due in ${Zm(f,"days")}`:`${Zm(f,"days")} left`}else return t.is_due?`Overdue by ${Zm(Math.abs(t.hours_until_due),"hours")}`:t.is_warning?`Due in ${Zm(t.hours_until_due,"hours")}`:`${Zm(t.hours_until_due,"hours")} left`};return o.jsx("div",{className:`rounded-xl border p-4 transition-all ${u()}`,children:o.jsxs("div",{className:"flex items-start gap-3",children:[o.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:[o.jsx(r,{className:`w-5 h-5 ${l()}`}),t.enabled&&(t.is_due||t.is_warning)&&o.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`})]}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("h3",{className:`font-medium truncate ${t.enabled?"text-white":"text-bambu-gray"}`,children:t.maintenance_type_name}),i==="days"&&o.jsx("span",{title:"Time-based interval",children:o.jsx(Jr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"})})]}),o.jsx("div",{className:"mt-2 mb-1.5",children:o.jsx("div",{className:"w-full h-1.5 bg-bambu-dark rounded-full overflow-hidden",children:o.jsx("div",{className:`h-full rounded-full transition-all duration-500 ${c()}`,style:{width:`${s}%`}})})}),o.jsxs("div",{className:`text-xs flex items-center gap-1 ${l()}`,children:[t.is_due&&o.jsx(qn,{className:"w-3 h-3"}),t.is_warning&&!t.is_due&&o.jsx(ur,{className:"w-3 h-3"}),!t.is_due&&!t.is_warning&&t.enabled&&o.jsx(go,{className:"w-3 h-3"}),d()]})]}),o.jsxs("div",{className:"flex items-center gap-2 shrink-0",children:[o.jsx(Vn,{checked:t.enabled,onChange:f=>n(t.id,f)}),o.jsxs(Pe,{size:"sm",variant:t.is_due?"primary":"secondary",onClick:()=>e(t.id),disabled:!t.enabled,className:"!px-3",children:[o.jsx(sg,{className:"w-3.5 h-3.5"}),"Reset"]})]})]})})}function DWe({overview:t,onPerform:e,onToggle:n,onSetHours:r}){const[i,a]=S.useState(!0),[s,l]=S.useState(!1),[c,u]=S.useState(t.total_print_hours.toFixed(1)),d=[...t.maintenance_items].sort((p,b)=>p.is_due&&!b.is_due?-1:!p.is_due&&b.is_due?1:p.is_warning&&!b.is_warning?-1:!p.is_warning&&b.is_warning?1:p.maintenance_type_id-b.maintenance_type_id),f=d.find(p=>p.enabled&&(p.is_due||p.is_warning)),g=()=>{const p=parseFloat(c);!isNaN(p)&&p>=0&&(r(t.printer_id,p),l(!1))};return o.jsxs(At,{className:"overflow-hidden",children:[o.jsxs("div",{className:"p-5",children:[o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{className:"flex items-center gap-4",children:[o.jsx("h2",{className:"text-xl font-semibold text-white",children:t.printer_name}),o.jsxs("div",{className:"flex items-center gap-2",children:[t.due_count>0&&o.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:[o.jsx(qn,{className:"w-3 h-3"}),t.due_count," overdue"]}),t.warning_count>0&&o.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:[o.jsx(ur,{className:"w-3 h-3"}),t.warning_count," due soon"]}),t.due_count===0&&t.warning_count===0&&o.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:[o.jsx(go,{className:"w-3 h-3"}),"All good"]})]})]}),o.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?o.jsx(Tb,{className:"w-4 h-4"}):o.jsx(Os,{className:"w-4 h-4"}),i?"Collapse":"Expand"]})]}),o.jsxs("div",{className:"flex items-center gap-6 mt-4",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2 bg-bambu-dark/50 rounded-lg",children:o.jsx(td,{className:"w-4 h-4 text-bambu-gray"})}),s?o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("input",{type:"number",value:c,onChange:p=>u(p.target.value),onKeyDown:p=>{p.key==="Enter"&&g(),p.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}),o.jsx("span",{className:"text-xs text-bambu-gray",children:"hours"}),o.jsx(Pe,{size:"sm",onClick:g,children:"Save"}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>l(!1),children:"Cancel"})]}):o.jsxs("button",{onClick:()=>{u(Math.round(t.total_print_hours).toString()),l(!0)},className:"group",children:[o.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",o.jsx(nf,{className:"w-3 h-3 text-bambu-gray group-hover:text-bambu-green"})]}),o.jsx("div",{className:"text-xs text-bambu-gray",children:"Total print time"})]})]}),o.jsx("div",{className:"w-px h-10 bg-bambu-dark-tertiary"}),f&&o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:`p-2 rounded-lg ${f.is_due?"bg-red-500/20":"bg-amber-500/20"}`,children:(()=>{const p=N0(f.maintenance_type_icon);return o.jsx(p,{className:`w-4 h-4 ${f.is_due?"text-red-400":"text-amber-400"}`})})()}),o.jsxs("div",{children:[o.jsx("div",{className:`text-sm font-medium ${f.is_due?"text-red-400":"text-amber-400"}`,children:f.maintenance_type_name}),o.jsx("div",{className:`text-xs ${f.is_due?"text-red-400/70":"text-amber-400/70"}`,children:f.is_due?"Overdue":"Due soon"})]})]})]})]}),i&&o.jsx(Tt,{className:"pt-0 border-t border-bambu-dark-tertiary",children:o.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-3 pt-4",children:d.map(p=>o.jsx(RWe,{item:p,onPerform:e,onToggle:n},p.id))})})]})}function OWe({overview:t,types:e,onUpdateInterval:n,onAddType:r,onUpdateType:i,onDeleteType:a,onAssignType:s,onRemoveItem:l}){const[c,u]=S.useState(null),[d,f]=S.useState(""),[g,p]=S.useState("hours"),[b,v]=S.useState(!1),[x,y]=S.useState(""),[E,w]=S.useState("100"),[A,T]=S.useState("hours"),[C,N]=S.useState("Wrench"),[O,k]=S.useState(new Set),[j,B]=S.useState(null),I=S.useMemo(()=>t?t.map(je=>({id:je.printer_id,name:je.printer_name})):[],[t]),q=je=>t?t.filter(z=>z.maintenance_items.some(H=>H.maintenance_type_id===je)).map(z=>({printerId:z.printer_id,printerName:z.printer_name,itemId:z.maintenance_items.find(H=>H.maintenance_type_id===je)?.id})):[],R=je=>{if(!t)return[];const z=new Set(q(je).map(H=>H.printerId));return I.filter(H=>!z.has(H.id))},[W,K]=S.useState(null),[G,J]=S.useState(""),[ee,Y]=S.useState(""),[F,$]=S.useState("hours"),[ne,oe]=S.useState("Wrench"),se=je=>{K(je),J(je.name),Y(je.default_interval_hours.toString()),$(je.interval_type||"hours"),oe(je.icon||"Wrench")},pe=()=>{W&&G.trim()&&parseFloat(ee)>0&&(i(W.id,{name:G.trim(),default_interval_hours:parseFloat(ee),interval_type:F,icon:ne}),K(null))},Z=(je,z,H)=>{const re=parseFloat(d);if(!isNaN(re)&&re>0){const V=Math.abs(re-z)<.01?null:re;n(je,{custom_interval_hours:V,custom_interval_type:g!==H?g:null})}u(null)},Me=je=>{je.preventDefault(),x.trim()&&parseFloat(E)>0&&O.size>0&&(r({name:x.trim(),default_interval_hours:parseFloat(E),interval_type:A,icon:C},Array.from(O)),y(""),w("100"),T("hours"),k(new Set),v(!1))},he=je=>{k(z=>{const H=new Set(z);return H.has(je)?H.delete(je):H.add(je),H})},ue=t?.map(je=>({printerId:je.printer_id,printerName:je.printer_name,items:je.maintenance_items.sort((z,H)=>z.maintenance_type_id-H.maintenance_type_id)})).sort((je,z)=>je.printerName.localeCompare(z.printerName))||[],Ae=e.filter(je=>je.is_system),fe=e.filter(je=>!je.is_system);return o.jsxs("div",{className:"space-y-8",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex items-center justify-between mb-4",children:[o.jsxs("div",{children:[o.jsx("h2",{className:"text-lg font-semibold text-white",children:"Maintenance Types"}),o.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"System types and your custom maintenance tasks"})]}),o.jsxs(Pe,{onClick:()=>v(!b),children:[o.jsx(fr,{className:"w-4 h-4"}),"Add Custom Type"]})]}),b&&o.jsx(At,{className:"mb-6",children:o.jsx(Tt,{className:"py-4",children:o.jsxs("form",{onSubmit:Me,children:[o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[o.jsxs("div",{className:"lg:col-span-2",children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Name"}),o.jsx("input",{type:"text",value:x,onChange:je=>y(je.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})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Interval Type"}),o.jsxs("select",{value:A,onChange:je=>{T(je.target.value),je.target.value==="days"?w("30"):w("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:[o.jsx("option",{value:"hours",children:"Print Hours"}),o.jsx("option",{value:"days",children:"Calendar Days"})]})]}),o.jsxs("div",{children:[o.jsxs("label",{className:"block text-xs text-bambu-gray mb-1.5",children:["Interval (",A==="days"?"days":"hours",")"]}),o.jsx("input",{type:"number",value:E,onChange:je=>w(je.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"})]})]}),o.jsx("div",{className:"mt-4 flex items-end justify-between",children:o.jsxs("div",{children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Icon"}),o.jsx("div",{className:"flex gap-1",children:Object.keys(i0).map(je=>{const z=i0[je];return o.jsx("button",{type:"button",onClick:()=>N(je),className:`p-2 rounded-lg transition-colors ${C===je?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:o.jsx(z,{className:"w-4 h-4"})},je)})})]})}),o.jsxs("div",{className:"mt-4",children:[o.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:"Assign to Printers"}),o.jsx("div",{className:"flex flex-wrap gap-2",children:I.map(je=>o.jsx("button",{type:"button",onClick:()=>he(je.id),className:`px-3 py-1.5 rounded-lg text-sm transition-colors ${O.has(je.id)?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:je.name},je.id))}),O.size===0&&o.jsx("p",{className:"text-xs text-orange-400 mt-1",children:"Select at least one printer"})]}),o.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[o.jsx(Pe,{type:"button",variant:"secondary",onClick:()=>{v(!1),k(new Set)},children:"Cancel"}),o.jsx(Pe,{type:"submit",disabled:!x.trim()||O.size===0,children:"Add Type"})]})]})})}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3",children:[Ae.map(je=>{const z=N0(je.icon),H=je.interval_type||"hours";return o.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-dark-tertiary",children:o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2.5 bg-bambu-dark rounded-lg",children:o.jsx(z,{className:"w-5 h-5 text-bambu-gray"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("div",{className:"text-sm font-medium text-white truncate",children:je.name}),o.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[H==="days"?o.jsx(Jr,{className:"w-3 h-3"}):o.jsx(td,{className:"w-3 h-3"}),Lk(je.default_interval_hours,H)]})]})]})},je.id)}),fe.map(je=>{const z=N0(je.icon),H=je.interval_type||"hours";if(W?.id===je.id)return o.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green",children:o.jsxs("div",{className:"space-y-3",children:[o.jsx("input",{type:"text",value:G,onChange:ye=>J(ye.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}),o.jsxs("div",{className:"flex gap-2",children:[o.jsxs("select",{value:F,onChange:ye=>$(ye.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:[o.jsx("option",{value:"hours",children:"Print Hours"}),o.jsx("option",{value:"days",children:"Calendar Days"})]}),o.jsx("input",{type:"number",value:ee,onChange:ye=>Y(ye.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"})]}),o.jsx("div",{className:"flex flex-wrap gap-1",children:Object.keys(i0).map(ye=>{const Ne=i0[ye];return o.jsx("button",{type:"button",onClick:()=>oe(ye),className:`p-1.5 rounded transition-colors ${ne===ye?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:o.jsx(Ne,{className:"w-3.5 h-3.5"})},ye)})}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx(Pe,{size:"sm",onClick:pe,disabled:!G.trim(),children:"Save"}),o.jsx(Pe,{size:"sm",variant:"secondary",onClick:()=>K(null),children:"Cancel"})]})]})},je.id);const V=q(je.id),Q=R(je.id),le=j===je.id;return o.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green/30",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2.5 bg-bambu-green/20 rounded-lg",children:o.jsx(z,{className:"w-5 h-5 text-bambu-green"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("span",{className:"text-sm font-medium text-white truncate",children:je.name}),o.jsx("span",{className:"px-1.5 py-0.5 bg-bambu-green/20 text-bambu-green text-[10px] font-medium rounded",children:"Custom"})]}),o.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[H==="days"?o.jsx(Jr,{className:"w-3 h-3"}):o.jsx(td,{className:"w-3 h-3"}),Lk(je.default_interval_hours,H)]})]}),o.jsxs("button",{onClick:()=>B(le?null:je.id),className:`px-2 py-1 rounded-lg border transition-colors flex items-center gap-1 ${V.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:`${V.length} printer(s) assigned - click to manage`,children:[o.jsx(qi,{className:"w-3 h-3"}),o.jsx("span",{className:"text-xs font-medium",children:V.length}),o.jsx(Os,{className:`w-3 h-3 transition-transform ${le?"rotate-180":""}`})]}),o.jsx("button",{onClick:()=>se(je),className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors",children:o.jsx(nf,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>{confirm(`Delete "${je.name}"?`)&&a(je.id)},className:"p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors",children:o.jsx(Rn,{className:"w-4 h-4"})})]}),le&&o.jsxs("div",{className:"mt-3 pt-3 border-t border-bambu-dark-tertiary",children:[o.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Assigned to printers:"}),V.length===0?o.jsx("p",{className:"text-xs text-orange-400",children:"No printers assigned"}):o.jsx("div",{className:"flex flex-wrap gap-1 mb-2",children:V.map(ye=>o.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark rounded text-xs text-white",children:[ye.printerName,o.jsx("button",{onClick:()=>ye.itemId&&l(ye.itemId),className:"hover:text-red-400 ml-1",title:"Remove from this printer",children:"×"})]},ye.printerId))}),Q.length>0&&o.jsxs("div",{className:"flex flex-wrap gap-1",children:[o.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:"Add:"}),Q.map(ye=>o.jsxs("button",{onClick:()=>s(ye.id,je.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:["+ ",ye.name]},ye.id))]})]})]},je.id)})]})]}),ue.length>0&&o.jsxs("div",{children:[o.jsxs("div",{className:"mb-4",children:[o.jsx("h2",{className:"text-lg font-semibold text-white",children:"Interval Overrides"}),o.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Customize intervals for specific printers"})]}),o.jsx("div",{className:"space-y-4",children:ue.map(je=>o.jsx(At,{children:o.jsxs(Tt,{className:"py-4",children:[o.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:je.printerName}),o.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2",children:je.items.map(z=>{const H=N0(z.maintenance_type_icon),re=e.find(Ne=>Ne.id===z.maintenance_type_id),V=re?.default_interval_hours||z.interval_hours,Q=re?.interval_type||"hours",le=z.interval_type||"hours",ye=c===z.id;return o.jsxs("div",{className:"flex items-center gap-2 p-2.5 bg-bambu-dark rounded-lg",children:[o.jsx(H,{className:"w-4 h-4 text-bambu-gray shrink-0"}),o.jsx("span",{className:"text-xs text-bambu-gray flex-1 truncate",children:z.maintenance_type_name}),ye?o.jsxs("div",{className:"flex items-center gap-1",children:[g==="days"?o.jsx(Jr,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}):o.jsx(td,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}),o.jsxs("select",{value:g,onChange:Ne=>p(Ne.target.value),className:"px-1.5 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",children:[o.jsx("option",{value:"hours",children:"Print Hours"}),o.jsx("option",{value:"days",children:"Calendar Days"})]}),o.jsx("input",{type:"number",value:d,onChange:Ne=>f(Ne.target.value),onKeyDown:Ne=>{Ne.key==="Enter"&&Z(z.id,V,Q),Ne.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"}),o.jsx(Pe,{size:"sm",onClick:()=>Z(z.id,V,Q),children:"OK"})]}):o.jsxs("button",{onClick:()=>{u(z.id),f(z.interval_hours.toString()),p(le)},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:[le==="days"?o.jsx(Jr,{className:"w-3 h-3"}):o.jsx(td,{className:"w-3 h-3"}),Lk(z.interval_hours,le),o.jsx(nf,{className:"w-3 h-3 text-bambu-gray"})]})]},z.id)})})]})},je.printerId))})]}),ue.length===0&&o.jsx(At,{children:o.jsxs(Tt,{className:"text-center py-12",children:[o.jsx(ur,{className:"w-12 h-12 mx-auto mb-4 text-bambu-gray/30"}),o.jsx("p",{className:"text-bambu-gray",children:"No printers configured"}),o.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1",children:"Add printers to configure maintenance intervals"})]})})]})}function LWe(){const t=dn(),{showToast:e}=vr(),[n,r]=S.useState("status"),{data:i,isLoading:a}=pt({queryKey:["maintenanceOverview"],queryFn:_e.getMaintenanceOverview}),{data:s}=pt({queryKey:["maintenanceTypes"],queryFn:_e.getMaintenanceTypes}),l=_t({mutationFn:({id:w,notes:A})=>_e.performMaintenance(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Maintenance marked as complete")},onError:w=>{e(w.message,"error")}}),c=_t({mutationFn:({id:w,data:A})=>_e.updateMaintenanceItem(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]})},onError:w=>{e(w.message,"error")}}),u=_t({mutationFn:({id:w,data:A})=>_e.updateMaintenanceType(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type updated")},onError:w=>{e(w.message,"error")}}),d=_t({mutationFn:_e.deleteMaintenanceType,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type deleted")},onError:w=>{e(w.message,"error")}}),f=_t({mutationFn:({printerId:w,hours:A})=>_e.setPrinterHours(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),t.invalidateQueries({queryKey:["maintenanceSummary"]}),e("Print hours updated")},onError:w=>{e(w.message,"error")}}),g=_t({mutationFn:({printerId:w,typeId:A})=>_e.assignMaintenanceType(w,A),onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer assigned")},onError:w=>{e(w.message,"error")}}),p=_t({mutationFn:_e.removeMaintenanceItem,onSuccess:()=>{t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Printer removed")},onError:w=>{e(w.message,"error")}}),b=w=>{l.mutate({id:w})},v=(w,A)=>{c.mutate({id:w,data:{enabled:A}})},x=(w,A)=>{f.mutate({printerId:w,hours:A})};if(a)return o.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})});const y=i?.reduce((w,A)=>w+A.due_count,0)||0,E=i?.reduce((w,A)=>w+A.warning_count,0)||0;return o.jsxs("div",{className:"p-4 md:p-8",children:[o.jsxs("div",{className:"mb-6",children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:"Maintenance"}),o.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:n==="status"?o.jsxs(o.Fragment,{children:[y>0&&o.jsxs("span",{className:"text-red-400",children:[y," task",y!==1?"s":""," overdue"]}),y>0&&E>0&&" · ",E>0&&o.jsxs("span",{className:"text-amber-400",children:[E," due soon"]}),y===0&&E===0&&o.jsx("span",{className:"text-bambu-green",children:"All maintenance up to date"})]}):"Configure maintenance types and intervals"})]}),o.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[o.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"}),o.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"?o.jsx("div",{className:"space-y-6",children:i&&i.length>0?[...i].sort((w,A)=>{const T=w.due_count*10+w.warning_count,C=A.due_count*10+A.warning_count;return T!==C?C-T:w.printer_name.localeCompare(A.printer_name)}).map(w=>o.jsx(DWe,{overview:w,onPerform:b,onToggle:v,onSetHours:x},w.printer_id)):o.jsx(At,{children:o.jsxs(Tt,{className:"text-center py-16",children:[o.jsx(_d,{className:"w-16 h-16 mx-auto mb-4 text-bambu-gray/30"}),o.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No printers configured"}),o.jsx("p",{className:"text-bambu-gray",children:"Add printers to start tracking maintenance"})]})})}):o.jsx(OWe,{overview:i,types:s||[],onUpdateInterval:(w,A)=>c.mutate({id:w,data:A}),onAddType:async(w,A)=>{const T=await _e.createMaintenanceType(w);for(const C of A)await _e.assignMaintenanceType(C,T.id);t.invalidateQueries({queryKey:["maintenanceTypes"]}),t.invalidateQueries({queryKey:["maintenanceOverview"]}),e("Maintenance type added")},onUpdateType:(w,A)=>u.mutate({id:w,data:A}),onDeleteType:w=>d.mutate(w),onAssignType:(w,A)=>g.mutate({printerId:w,typeId:A}),onRemoveItem:w=>p.mutate(w)})]})}const e9=["#ef4444","#f97316","#eab308","#22c55e","#06b6d4","#3b82f6","#8b5cf6","#ec4899","#6b7280"];function tne({project:t,onClose:e,onSave:n,isLoading:r}){const[i,a]=S.useState(t?.name||""),[s,l]=S.useState(t?.description||""),[c,u]=S.useState(t?.color||e9[0]),[d,f]=S.useState(t?.target_count?.toString()||""),[g,p]=S.useState(t?.status||"active"),[b,v]=S.useState(t?.tags||""),[x,y]=S.useState(t?.due_date?.split("T")[0]||""),[E,w]=S.useState(t?.priority||"normal"),A=T=>{T.preventDefault(),n({name:i.trim(),description:s.trim()||void 0,color:c,target_count:d?parseInt(d,10):void 0,tags:b.trim()||void 0,due_date:x||void 0,priority:E,...t&&{status:g}})};return o.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:o.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[o.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:o.jsx("h2",{className:"text-lg font-semibold text-white",children:t?"Edit Project":"New Project"})}),o.jsxs("form",{onSubmit:A,className:"p-4 space-y-4",children:[o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Name"}),o.jsx("input",{type:"text",value:i,onChange:T=>a(T.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})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Description"}),o.jsx("textarea",{value:s,onChange:T=>l(T.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})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Color"}),o.jsx("div",{className:"flex gap-2 flex-wrap",children:e9.map(T=>o.jsx("button",{type:"button",onClick:()=>u(T),className:`w-8 h-8 rounded-full transition-transform ${c===T?"ring-2 ring-white ring-offset-2 ring-offset-bambu-dark-secondary scale-110":""}`,style:{backgroundColor:T}},T))})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Target Print Count (optional)"}),o.jsx("input",{type:"number",value:d,onChange:T=>f(T.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., 50 parts to print",min:"1"})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Tags (comma-separated)"}),o.jsx("input",{type:"text",value:b,onChange:T=>v(T.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"})]}),o.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Due Date"}),o.jsx("input",{type:"date",value:x,onChange:T=>y(T.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"})]}),o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Priority"}),o.jsxs("select",{value:E,onChange:T=>w(T.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:[o.jsx("option",{value:"low",children:"Low"}),o.jsx("option",{value:"normal",children:"Normal"}),o.jsx("option",{value:"high",children:"High"}),o.jsx("option",{value:"urgent",children:"Urgent"})]})]})]}),t&&o.jsxs("div",{children:[o.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:"Status"}),o.jsxs("select",{value:g,onChange:T=>p(T.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:[o.jsx("option",{value:"active",children:"Active"}),o.jsx("option",{value:"completed",children:"Completed"}),o.jsx("option",{value:"archived",children:"Archived"})]})]}),o.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[o.jsx(Pe,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),o.jsx(Pe,{type:"submit",disabled:!i.trim()||r,children:r?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):t?"Save":"Create"})]})]})]})})}function IWe({project:t,onClick:e,onEdit:n,onDelete:r}){const i=t.progress_percent??0,a=t.status==="completed",s=t.status==="archived",[l,c]=S.useState(!1),d=a?{icon:cR,color:"text-bambu-green",bg:"bg-bambu-green/10"}:s?{icon:co,color:"text-bambu-gray",bg:"bg-bambu-gray/10"}:t.queue_count>0?{icon:ur,color:"text-blue-400",bg:"bg-blue-400/10"}:{icon:Bo,color:"text-bambu-gray",bg:"bg-bambu-gray/10"};return o.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:[o.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`}}),o.jsxs("div",{className:"p-5 pl-6",children:[o.jsxs("div",{className:"flex items-start justify-between mb-4",children:[o.jsxs("div",{className:"flex items-center gap-3 min-w-0 flex-1",children:[o.jsx("div",{className:`p-2 rounded-lg ${d.bg} flex-shrink-0`,children:o.jsx(d.icon,{className:`w-5 h-5 ${d.color}`})}),o.jsxs("div",{className:"min-w-0 flex-1",children:[o.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[o.jsx("h3",{className:"font-semibold text-white truncate",children:t.name}),t.target_count?o.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.total_items,"/",t.target_count," items"]}):t.total_items>0?o.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium bg-bambu-dark text-bambu-gray",children:[t.total_items," item",t.total_items!==1?"s":""]}):null,a&&o.jsx("span",{className:"text-xs bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full whitespace-nowrap",children:"Done"}),s&&o.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&&o.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1 line-clamp-1",children:t.description}),t.archives&&t.archives.length>0&&(()=>{const f=[...new Set(t.archives.map(p=>p.filament_type).filter(Boolean))],g=[...new Set(t.archives.map(p=>p.filament_color).filter(Boolean))];return f.length===0&&g.length===0?null:o.jsxs("div",{className:"flex items-center gap-2 mt-1.5",children:[f.slice(0,3).map(p=>o.jsx("span",{className:"text-[10px] px-1.5 py-0.5 bg-bambu-dark text-bambu-gray rounded",children:p},p)),g.length>0&&o.jsxs("div",{className:"flex items-center gap-0.5",children:[g.slice(0,5).map(p=>o.jsx("div",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:p.startsWith("#")?p:`#${p}`},title:p},p)),g.length>5&&o.jsxs("span",{className:"text-[10px] text-bambu-gray ml-0.5",children:["+",g.length-5]})]})]})})()]})]}),o.jsxs("div",{className:"relative",onClick:f=>f.stopPropagation(),children:[o.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:()=>c(!l),children:o.jsx(o0,{className:"w-4 h-4"})}),l&&o.jsxs(o.Fragment,{children:[o.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>c(!1)}),o.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:[o.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(),c(!1)},children:[o.jsx(nf,{className:"w-4 h-4"}),"Edit"]}),o.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(),c(!1)},children:[o.jsx(Rn,{className:"w-4 h-4"}),"Delete"]})]})]})]})]}),o.jsx("div",{className:"mb-4",children:t.target_count?o.jsxs(o.Fragment,{children:[o.jsxs("div",{className:"flex items-center justify-between text-xs mb-2",children:[o.jsx("span",{className:"text-bambu-gray",children:"Progress"}),o.jsxs("span",{className:i>=100?"text-bambu-green font-medium":"text-white",children:[t.total_items," / ",t.target_count]})]}),o.jsx("div",{className:"h-2.5 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:o.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`}})}),o.jsxs("div",{className:"text-right text-xs text-bambu-gray/60 mt-1",children:[i.toFixed(0),"% complete"]})]}):t.total_items>0?o.jsxs("div",{className:"flex items-center gap-4 text-xs",children:[o.jsxs("div",{className:"flex items-center gap-1.5 text-bambu-gray",children:[o.jsx(co,{className:"w-3.5 h-3.5"}),o.jsxs("span",{children:[t.total_items," item",t.total_items!==1?"s":""," completed"]})]}),t.queue_count>0&&o.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",children:[o.jsx(ur,{className:"w-3.5 h-3.5"}),o.jsxs("span",{children:[t.queue_count," in queue"]})]})]}):o.jsx("div",{className:"text-xs text-bambu-gray/60 italic",children:"No prints yet"})}),t.archives&&t.archives.length>0&&o.jsxs("div",{className:"mb-4",children:[o.jsx("div",{className:"grid grid-cols-4 gap-1.5",children:t.archives.slice(0,4).map(f=>o.jsxs("div",{className:"relative aspect-square rounded-lg bg-bambu-dark overflow-hidden border border-bambu-dark-tertiary",title:f.print_name||"Unknown",children:[f.thumbnail_path?o.jsx("img",{src:_e.getArchiveThumbnail(f.id),alt:f.print_name||"",className:"w-full h-full object-cover"}):o.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray/50",children:o.jsx(zc,{className:"w-6 h-6"})}),f.status==="failed"&&o.jsx("div",{className:"absolute inset-0 bg-red-500/40 flex items-center justify-center",children:o.jsx(qn,{className:"w-4 h-4 text-white"})})]},f.id))}),t.archive_count>4&&o.jsxs("p",{className:"text-xs text-bambu-gray mt-1.5 text-center",children:["+",t.archive_count-4," more"]})]}),o.jsxs("div",{className:"flex items-center justify-between pt-3 border-t border-bambu-dark-tertiary",children:[o.jsxs("div",{className:"flex items-center gap-4 text-xs text-bambu-gray",children:[o.jsxs("div",{className:"flex items-center gap-1.5",title:"Total items printed",children:[o.jsx(co,{className:"w-3.5 h-3.5"}),o.jsx("span",{children:t.total_items})]}),t.queue_count>0&&o.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",title:"In queue",children:[o.jsx(t3,{className:"w-3.5 h-3.5"}),o.jsx("span",{children:t.queue_count})]})]}),o.jsx(Ko,{className:"w-4 h-4 text-bambu-gray/50 group-hover:text-bambu-gray transition-colors"})]})]})]})}function FWe(){const t=Eb(),e=dn(),{showToast:n}=vr(),[r,i]=S.useState(!1),[a,s]=S.useState(),[l,c]=S.useState("active"),[u,d]=S.useState(null),{data:f,isLoading:g}=pt({queryKey:["projects",l==="all"?void 0:l],queryFn:()=>_e.getProjects(l==="all"?void 0:l)}),p=_t({mutationFn:C=>_e.createProject(C),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),n("Project created","success")},onError:C=>{n(C.message,"error")}}),b=_t({mutationFn:({id:C,data:N})=>_e.updateProject(C,N),onSuccess:()=>{e.invalidateQueries({queryKey:["projects"]}),i(!1),s(void 0),n("Project updated","success")},onError:C=>{n(C.message,"error")}}),v=_t({mutationFn:C=>_e.deleteProject(C),onSuccess:()=>{d(null),n("Project deleted","success"),setTimeout(()=>window.location.reload(),100)},onError:C=>{d(null),n(C.message,"error")}}),x=C=>{a?b.mutate({id:a.id,data:C}):p.mutate(C)},y=C=>{s(C),i(!0)},E=C=>{t(`/projects/${C.id}`)},w=C=>{d(C)},A=()=>{u!==null&&v.mutate(u)},T=f?.reduce((C,N)=>(C[N.status]=(C[N.status]||0)+1,C.all=(C.all||0)+1,C),{})||{};return o.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[o.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[o.jsxs("div",{children:[o.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[o.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:o.jsx(Bo,{className:"w-6 h-6 text-bambu-green"})}),"Projects"]}),o.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:"Organize and track your 3D printing projects"})]}),o.jsxs(Pe,{onClick:()=>i(!0),className:"sm:w-auto w-full",children:[o.jsx(fr,{className:"w-4 h-4 mr-2"}),"New Project"]})]}),o.jsx("div",{className:"flex gap-1 p-1 bg-bambu-dark rounded-xl w-fit",children:[{key:"active",label:"Active",icon:ur},{key:"completed",label:"Completed",icon:cR},{key:"archived",label:"Archived",icon:co},{key:"all",label:"All",icon:Bo}].map(({key:C,label:N,icon:O})=>o.jsxs("button",{onClick:()=>c(C),className:`flex items-center gap-2 px-4 py-2 text-sm rounded-lg transition-all ${l===C?"bg-bambu-card text-white shadow-sm":"text-bambu-gray hover:text-white"}`,children:[o.jsx(O,{className:"w-4 h-4"}),o.jsx("span",{children:N}),T[C]>0&&o.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded-full ${l===C?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary"}`,children:T[C]})]},C))}),g?o.jsx("div",{className:"flex items-center justify-center py-20",children:o.jsxs("div",{className:"flex flex-col items-center gap-3",children:[o.jsx(xt,{className:"w-8 h-8 animate-spin text-bambu-green"}),o.jsx("p",{className:"text-sm text-bambu-gray",children:"Loading projects..."})]})}):f?.length===0?o.jsxs("div",{className:"flex flex-col items-center justify-center py-20 px-4",children:[o.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:o.jsx(Bo,{className:"w-12 h-12 text-bambu-gray/50"})}),o.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:l==="all"?"No projects yet":`No ${l} projects`}),o.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"&&o.jsxs(Pe,{onClick:()=>i(!0),children:[o.jsx(fr,{className:"w-4 h-4 mr-2"}),"Create Your First Project"]})]}):o.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-8",children:f?.map(C=>o.jsx(IWe,{project:C,onClick:()=>E(C),onEdit:()=>y(C),onDelete:()=>w(C.id)},C.id))}),u!==null&&o.jsx(mr,{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:A,onCancel:()=>d(null)}),r&&o.jsx(tne,{project:a,onClose:()=>{i(!1),s(void 0)},onSave:x,isLoading:p.isPending||b.isPending})]})}function UWe(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 zWe(t){return t>=1e3?`${(t/1e3).toFixed(2)}kg`:`${Math.round(t)}g`}function BWe({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 o.jsx("span",{className:`px-2 py-1 rounded text-sm font-medium ${n}`,children:t.charAt(0).toUpperCase()+t.slice(1)})}function u1({icon:t,label:e,value:n,subValue:r,color:i="text-bambu-gray"}){return o.jsx(At,{children:o.jsx(Tt,{className:"p-4",children:o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark ${i}`,children:o.jsx(t,{className:"w-5 h-5"})}),o.jsxs("div",{children:[o.jsx("p",{className:"text-sm text-bambu-gray",children:e}),o.jsx("p",{className:"text-xl font-semibold text-white",children:n}),r&&o.jsx("p",{className:"text-xs text-bambu-gray/70",children:r})]})]})})})}function HWe({archives:t}){return t.length===0?o.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[o.jsx(zc,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),o.jsx("p",{children:"No prints in this project yet"})]}):o.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=>o.jsxs(Sc,{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?o.jsx("img",{src:_e.getArchiveThumbnail(e.id),alt:e.print_name||"Print",className:"w-full h-full object-cover"}):o.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:o.jsx(zc,{className:"w-8 h-8"})}),e.status==="failed"&&o.jsx("div",{className:"absolute inset-0 bg-red-500/30 flex items-center justify-center",children:o.jsx(Ra,{className:"w-8 h-8 text-white"})}),e.status==="completed"&&o.jsx("div",{className:"absolute top-1 right-1",children:o.jsx(Ar,{className:"w-4 h-4 text-bambu-green"})}),o.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:o.jsx("p",{className:"text-xs text-white truncate",children:e.print_name||"Unknown"})})]},e.id))})}function $We({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 o.jsxs("span",{className:`px-2 py-1 rounded text-xs font-medium flex items-center gap-1 ${n}`,children:[t==="urgent"&&o.jsx(qn,{className:"w-3 h-3"}),r]})}function VWe(t){return t?new Date(t).toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"}):""}function Ik(t){if(!t)return null;const e=new Date(t),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 GWe(){const{id:t}=eR(),e=Eb(),n=dn(),{showToast:r}=vr(),[i,a]=S.useState(!1),[s,l]=S.useState(!1),[c,u]=S.useState(""),[d,f]=S.useState(!1),g=S.useRef(null),p=parseInt(t||"0",10),{data:b,isLoading:v,error:x}=pt({queryKey:["project",p],queryFn:()=>_e.getProject(p),enabled:p>0}),{data:y,isLoading:E}=pt({queryKey:["project-archives",p],queryFn:()=>_e.getProjectArchives(p),enabled:p>0}),{data:w,isLoading:A}=pt({queryKey:["project-bom",p],queryFn:()=>_e.getProjectBOM(p),enabled:p>0}),{data:T,isLoading:C}=pt({queryKey:["project-timeline",p],queryFn:()=>_e.getProjectTimeline(p,20),enabled:p>0}),{data:N}=pt({queryKey:["settings"],queryFn:_e.getSettings}),O=N?.currency||"$",k=_t({mutationFn:U=>_e.updateProject(p,U),onSuccess:()=>{n.invalidateQueries({queryKey:["project",p]}),n.invalidateQueries({queryKey:["projects"]}),a(!1),l(!1),r("Project updated","success")},onError:U=>{r(U.message,"error")}}),j=()=>{u(b?.notes||""),l(!0)},B=()=>{k.mutate({notes:c})},I=()=>{l(!1),u("")},q=async U=>{const L=U.target.files?.[0];if(L){f(!0);try{const te=await _e.uploadProjectAttachment(p,L);n.invalidateQueries({queryKey:["project",p]}),r(`Uploaded: ${te.original_name}`,"success")}catch(te){r(te.message,"error")}finally{f(!1),g.current&&(g.current.value="")}}},R=(U,L)=>{Ae({isOpen:!0,title:"Delete Attachment",message:`Are you sure you want to delete "${L}"?`,onConfirm:async()=>{Ae(te=>({...te,isOpen:!1}));try{await _e.deleteProjectAttachment(p,U),n.invalidateQueries({queryKey:["project",p]}),r("Attachment deleted","success")}catch(te){r(te.message,"error")}}})},W=U=>U<1024?`${U} B`:U<1024*1024?`${(U/1024).toFixed(1)} KB`:`${(U/(1024*1024)).toFixed(1)} MB`,[K,G]=S.useState(""),[J,ee]=S.useState(1),[Y,F]=S.useState(""),[$,ne]=S.useState(""),[oe,se]=S.useState(""),[pe,Z]=S.useState(!1),[Me,he]=S.useState(!1),[ue,Ae]=S.useState({isOpen:!1,title:"",message:"",onConfirm:()=>{}}),fe=_t({mutationFn:U=>_e.createBOMItem(p,U),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",p]}),n.invalidateQueries({queryKey:["project",p]}),G(""),ee(1),F(""),ne(""),se(""),Z(!1),r("Part added","success")},onError:U=>r(U.message,"error")}),je=_t({mutationFn:({itemId:U,data:L})=>_e.updateBOMItem(p,U,L),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",p]}),n.invalidateQueries({queryKey:["project",p]})},onError:U=>r(U.message,"error")}),z=_t({mutationFn:U=>_e.deleteBOMItem(p,U),onSuccess:()=>{n.invalidateQueries({queryKey:["project-bom",p]}),n.invalidateQueries({queryKey:["project",p]}),r("Part removed","success")},onError:U=>r(U.message,"error")}),H=U=>{U.preventDefault(),K.trim()&&fe.mutate({name:K.trim(),quantity_needed:J,unit_price:Y?parseFloat(Y):void 0,sourcing_url:$.trim()||void 0,remarks:oe.trim()||void 0})},re=U=>{const L=U.is_complete?0:U.quantity_needed;je.mutate({itemId:U.id,data:{quantity_acquired:L}})},V=(U,L)=>{Ae({isOpen:!0,title:"Delete Part",message:`Are you sure you want to delete "${L}"?`,onConfirm:()=>{Ae(te=>({...te,isOpen:!1})),z.mutate(U)}})},Q=_t({mutationFn:()=>_e.createTemplateFromProject(p),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),r("Template created","success")},onError:U=>r(U.message,"error")}),le=U=>new Date(U).toLocaleDateString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"});if(v)return o.jsx("div",{className:"flex items-center justify-center py-24",children:o.jsx(xt,{className:"w-8 h-8 animate-spin text-bambu-green"})});if(x||!b)return o.jsxs("div",{className:"text-center py-24",children:[o.jsx("p",{className:"text-bambu-gray",children:x?`Error: ${x.message}`:"Project not found"}),o.jsx(Pe,{variant:"secondary",className:"mt-4",onClick:()=>e("/projects"),children:"Back to Projects"})]});const ye=b.stats,Ne=ye?.progress_percent??0,Ee=ye&&ye.total_items>0?(ye.completed_prints/ye.total_items*100).toFixed(0):null;return o.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[o.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[o.jsx(Sc,{to:"/projects",className:"hover:text-white transition-colors",children:"Projects"}),o.jsx(Ko,{className:"w-4 h-4"}),o.jsx("span",{className:"text-white",children:b.name})]}),o.jsxs("div",{className:"flex items-start justify-between",children:[o.jsxs("div",{className:"flex items-center gap-4",children:[o.jsx("button",{onClick:()=>e("/projects"),className:"p-2 rounded-lg bg-bambu-card hover:bg-bambu-dark-tertiary transition-colors",children:o.jsx(Loe,{className:"w-5 h-5 text-bambu-gray"})}),o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-4 h-4 rounded-full flex-shrink-0",style:{backgroundColor:b.color||"#6b7280"}}),o.jsxs("div",{children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:b.name}),b.description&&o.jsx("p",{className:"text-bambu-gray mt-1",children:b.description})]})]}),o.jsx(BWe,{status:b.status})]}),o.jsxs(Pe,{onClick:()=>a(!0),children:[o.jsx(nf,{className:"w-4 h-4 mr-2"}),"Edit"]})]}),b.target_count&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-2",children:[o.jsx("span",{className:"text-sm text-bambu-gray",children:"Progress"}),o.jsxs("span",{className:"text-sm font-medium text-white",children:[ye?.completed_prints||0," / ",b.target_count," items"]})]}),o.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:o.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(Ne,100)}%`,backgroundColor:Ne>=100?"#22c55e":b.color||"#6b7280"}})}),o.jsxs("div",{className:"flex justify-between mt-1",children:[o.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[Ne.toFixed(0),"% complete"]}),b.target_count-(ye?.completed_prints||0)>0&&o.jsxs("span",{className:"text-xs text-bambu-gray/70",children:[b.target_count-(ye?.completed_prints||0)," remaining"]})]})]})}),ye&&o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsx(u1,{icon:zc,label:"Total Items",value:ye.total_items,subValue:`${ye.total_archives} print job${ye.total_archives!==1?"s":""}`,color:"text-bambu-green"}),o.jsx(u1,{icon:Ar,label:"Completed",value:ye.completed_prints,subValue:ye.failed_prints>0?`${ye.failed_prints} failed`:Ee?`${Ee}% success`:void 0,color:"text-blue-400"}),o.jsx(u1,{icon:ur,label:"Print Time",value:UWe(ye.total_print_time_hours),color:"text-yellow-400"}),o.jsx(u1,{icon:qi,label:"Filament Used",value:zWe(ye.total_filament_grams),color:"text-purple-400"})]}),ye&&(ye.estimated_cost>0||b.budget)&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsx("h2",{className:"text-lg font-semibold text-white mb-3",children:"Cost Tracking"}),o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Filament Cost"}),o.jsxs("p",{className:"text-lg font-semibold text-white",children:[O,ye.estimated_cost.toFixed(2)]})]}),ye.total_energy_kwh>0&&o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Energy"}),o.jsxs("p",{className:"text-lg font-semibold text-white",children:[ye.total_energy_kwh.toFixed(2)," kWh",ye.total_energy_cost>0&&o.jsxs("span",{className:"text-sm text-bambu-gray ml-1",children:["(",O,ye.total_energy_cost.toFixed(2),")"]})]})]}),b.budget&&o.jsxs(o.Fragment,{children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Budget"}),o.jsxs("p",{className:"text-lg font-semibold text-white",children:[O,b.budget.toFixed(2)]})]}),o.jsxs("div",{children:[o.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:"Remaining"}),o.jsxs("p",{className:`text-lg font-semibold ${b.budget-ye.estimated_cost>=0?"text-bambu-green":"text-red-400"}`,children:[O,(b.budget-ye.estimated_cost).toFixed(2)]})]})]})]})]})}),b.children&&b.children.length>0&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-3",children:[o.jsx(ace,{className:"w-5 h-5"}),"Sub-projects (",b.children.length,")"]}),o.jsx("div",{className:"space-y-2",children:b.children.map(U=>o.jsxs(Sc,{to:`/projects/${U.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:U.color||"#6b7280"}}),o.jsx("span",{className:"text-white",children:U.name}),o.jsx("span",{className:`text-xs px-2 py-0.5 rounded ${U.status==="completed"?"bg-bambu-green/20 text-bambu-green":U.status==="archived"?"bg-bambu-gray/20 text-bambu-gray":"bg-blue-500/20 text-blue-400"}`,children:U.status})]}),U.progress_percent!==null&&o.jsxs("span",{className:"text-sm text-bambu-gray",children:[U.progress_percent.toFixed(0),"%"]})]},U.id))})]})}),b.parent_id&&b.parent_name&&o.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[o.jsx(vd,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("span",{className:"text-bambu-gray",children:"Part of:"}),o.jsx(Sc,{to:`/projects/${b.parent_id}`,className:"text-bambu-green hover:underline",children:b.parent_name})]}),(b.tags||b.due_date||b.priority!=="normal")&&o.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[b.priority&&b.priority!=="normal"&&o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx("span",{className:"text-xs text-bambu-gray uppercase",children:"Priority:"}),o.jsx($We,{priority:b.priority})]}),b.due_date&&o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx(Jr,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("span",{className:"text-sm text-white",children:VWe(b.due_date)}),Ik(b.due_date)&&o.jsxs("span",{className:`text-xs ${Ik(b.due_date).color}`,children:["(",Ik(b.due_date).label,")"]})]}),b.tags&&o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsx(Cp,{className:"w-4 h-4 text-bambu-gray"}),o.jsx("div",{className:"flex flex-wrap gap-1",children:b.tags.split(",").map((U,L)=>o.jsx("span",{className:"px-2 py-0.5 bg-bambu-dark-tertiary text-bambu-gray text-xs rounded",children:U.trim()},L))})]})]}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(As,{className:"w-5 h-5"}),"Notes"]}),s?o.jsxs("div",{className:"flex gap-2",children:[o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:I,disabled:k.isPending,children:[o.jsx(Wt,{className:"w-4 h-4 mr-1"}),"Cancel"]}),o.jsxs(Pe,{size:"sm",onClick:B,disabled:k.isPending,children:[k.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin mr-1"}):o.jsx(Ts,{className:"w-4 h-4 mr-1"}),"Save"]})]}):o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:j,children:[o.jsx(nf,{className:"w-4 h-4 mr-1"}),"Edit"]})]}),s?o.jsx($j,{content:c,onChange:u,placeholder:"Add notes about this project..."}):b.notes?o.jsx("div",{className:"prose prose-invert prose-sm max-w-none",dangerouslySetInnerHTML:{__html:b.notes}}):o.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No notes yet. Click Edit to add notes."})]})}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(pue,{className:"w-5 h-5"}),"Attachments (",b.attachments?.length||0,")"]}),o.jsxs("div",{children:[o.jsx("input",{ref:g,type:"file",onChange:q,className:"hidden"}),o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>g.current?.click(),disabled:d,children:[d?o.jsx(xt,{className:"w-4 h-4 animate-spin mr-1"}):o.jsx(ua,{className:"w-4 h-4 mr-1"}),"Upload"]})]})]}),o.jsx("p",{className:"text-xs text-bambu-gray mb-3",children:"Upload any file: images (PNG, JPG), PDFs, STL files, or documents."}),b.attachments&&b.attachments.length>0?o.jsx("div",{className:"space-y-2",children:b.attachments.map(U=>o.jsxs("div",{className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[o.jsx(hR,{className:"w-5 h-5 text-bambu-gray flex-shrink-0"}),o.jsxs("div",{className:"min-w-0",children:[o.jsx("p",{className:"text-sm text-white truncate",children:U.original_name}),o.jsx("p",{className:"text-xs text-bambu-gray",children:W(U.size)})]})]}),o.jsxs("div",{className:"flex items-center gap-1 flex-shrink-0",children:[o.jsx("a",{href:_e.getProjectAttachmentUrl(p,U.filename),download:U.original_name,className:"p-2 rounded hover:bg-bambu-dark-tertiary transition-colors text-bambu-gray hover:text-white",title:"Download",children:o.jsx(Wi,{className:"w-4 h-4"})}),o.jsx("button",{onClick:()=>R(U.filename,U.original_name),className:"p-2 rounded hover:bg-bambu-dark-tertiary transition-colors text-bambu-gray hover:text-red-400",title:"Delete",children:o.jsx(Rn,{className:"w-4 h-4"})})]})]},U.filename))}):o.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No attachments yet. Click Upload to add files."})]})}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-4",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(q9,{className:"w-5 h-5"}),"Bill of Materials",ye&&ye.bom_total_items>0&&o.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",ye.bom_completed_items,"/",ye.bom_total_items," acquired)"]})]}),o.jsxs("div",{className:"flex items-center gap-2",children:[w&&w.some(U=>U.is_complete)&&o.jsx("button",{onClick:()=>he(!Me),className:`text-xs px-2 py-1 rounded transition-colors ${Me?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:Me?"Show all":"Hide done"}),!pe&&o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>Z(!0),children:[o.jsx(fr,{className:"w-4 h-4 mr-1"}),"Add Part"]})]})]}),pe&&o.jsxs("form",{onSubmit:H,className:"bg-bambu-dark rounded-lg p-4 mb-4 space-y-3",children:[o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[o.jsx("input",{type:"text",value:K,onChange:U=>G(U.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}),o.jsxs("div",{className:"flex gap-2",children:[o.jsx("input",{type:"number",value:J,onChange:U=>ee(parseInt(U.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"}),o.jsx("input",{type:"number",step:"0.01",value:Y,onChange:U=>F(U.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 (${O})`})]})]}),o.jsx("input",{type:"url",value:$,onChange:U=>ne(U.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)"}),o.jsx("input",{type:"text",value:oe,onChange:U=>se(U.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)"}),o.jsxs("div",{className:"flex justify-end gap-2",children:[o.jsx(Pe,{type:"button",variant:"secondary",size:"sm",onClick:()=>Z(!1),children:"Cancel"}),o.jsx(Pe,{type:"submit",size:"sm",disabled:!K.trim()||fe.isPending,children:fe.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin"}):"Add Part"})]})]}),A?o.jsx("div",{className:"flex items-center justify-center py-4",children:o.jsx(xt,{className:"w-6 h-6 animate-spin text-bambu-green"})}):w&&w.length>0?o.jsxs("div",{className:"space-y-2",children:[w.filter(U=>!Me||!U.is_complete).map(U=>o.jsx("div",{className:`p-3 rounded-lg transition-colors ${U.is_complete?"bg-bambu-green/10":"bg-bambu-dark"}`,children:o.jsxs("div",{className:"flex items-start gap-3",children:[o.jsx("button",{onClick:()=>re(U),disabled:je.isPending,className:`w-5 h-5 mt-0.5 rounded border-2 flex items-center justify-center transition-colors flex-shrink-0 ${U.is_complete?"bg-bambu-green border-bambu-green text-white":"border-bambu-gray hover:border-bambu-green"}`,children:U.is_complete&&o.jsx(Ar,{className:"w-3 h-3"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsxs("div",{className:"flex items-center justify-between gap-2",children:[o.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[o.jsxs("p",{className:`text-sm font-medium ${U.is_complete?"text-bambu-gray line-through":"text-white"}`,children:[U.name,o.jsxs("span",{className:"text-bambu-gray font-normal ml-2",children:["x",U.quantity_needed]})]}),U.unit_price!==null&&o.jsxs("span",{className:"text-xs text-bambu-green whitespace-nowrap",children:[O,(U.unit_price*U.quantity_needed).toFixed(2)]})]}),o.jsx("button",{onClick:()=>V(U.id,U.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:o.jsx(Rn,{className:"w-4 h-4"})})]}),U.sourcing_url&&o.jsxs("a",{href:U.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:L=>L.stopPropagation(),children:[o.jsx(_i,{className:"w-3 h-3 flex-shrink-0"}),o.jsx("span",{className:"truncate",children:(()=>{try{return new URL(U.sourcing_url).hostname.replace("www.","")}catch{return U.sourcing_url}})()})]}),U.remarks&&o.jsx("p",{className:"mt-1 text-xs text-bambu-gray/80 italic",children:U.remarks})]})]})},U.id)),w.some(U=>U.unit_price!==null)&&o.jsxs("div",{className:"pt-2 mt-2 border-t border-bambu-dark-tertiary flex justify-between text-sm",children:[o.jsx("span",{className:"text-bambu-gray",children:"Total cost:"}),o.jsxs("span",{className:"text-white font-medium",children:[O,w.reduce((U,L)=>U+(L.unit_price||0)*L.quantity_needed,0).toFixed(2)]})]})]}):o.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."})]})}),o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsx("div",{className:"flex items-center justify-between mb-3",children:o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(G1,{className:"w-5 h-5"}),"Activity Timeline"]})}),C?o.jsx("div",{className:"flex items-center justify-center py-4",children:o.jsx(xt,{className:"w-6 h-6 animate-spin text-bambu-green"})}):T&&T.length>0?o.jsx("div",{className:"space-y-3",children:T.slice(0,10).map((U,L)=>o.jsxs("div",{className:"flex gap-3",children:[o.jsxs("div",{className:`w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 ${U.event_type==="print_completed"?"bg-bambu-green/20 text-bambu-green":U.event_type==="print_failed"?"bg-red-500/20 text-red-400":U.event_type==="print_started"?"bg-yellow-500/20 text-yellow-400":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[U.event_type==="print_completed"&&o.jsx(Ar,{className:"w-4 h-4"}),U.event_type==="print_failed"&&o.jsx(Ra,{className:"w-4 h-4"}),U.event_type==="print_started"&&o.jsx(qi,{className:"w-4 h-4"}),U.event_type==="queued"&&o.jsx(t3,{className:"w-4 h-4"}),U.event_type==="project_created"&&o.jsx(fr,{className:"w-4 h-4"})]}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("p",{className:"text-sm text-white",children:U.title}),U.description&&o.jsx("p",{className:"text-xs text-bambu-gray truncate",children:U.description}),o.jsx("p",{className:"text-xs text-bambu-gray/70",children:le(U.timestamp)})]})]},L))}):o.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:"No activity yet."})]})}),!b.is_template&&o.jsx("div",{className:"flex justify-end",children:o.jsxs(Pe,{variant:"secondary",size:"sm",onClick:()=>Q.mutate(),disabled:Q.isPending,children:[Q.isPending?o.jsx(xt,{className:"w-4 h-4 animate-spin mr-2"}):o.jsx(vl,{className:"w-4 h-4 mr-2"}),"Save as Template"]})}),ye&&(ye.queued_prints>0||ye.in_progress_prints>0)&&o.jsx(At,{children:o.jsxs(Tt,{className:"p-4",children:[o.jsxs("div",{className:"flex items-center justify-between mb-3",children:[o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(t3,{className:"w-5 h-5"}),"Queue"]}),o.jsx(Sc,{to:`/queue?project=${p}`,className:"text-sm text-bambu-green hover:underline",children:"View all"})]}),o.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[ye.in_progress_prints>0&&o.jsxs("span",{className:"text-yellow-400",children:[ye.in_progress_prints," printing"]}),ye.queued_prints>0&&o.jsxs("span",{className:"text-bambu-gray",children:[ye.queued_prints," queued"]})]})]})}),o.jsxs("div",{children:[o.jsx("div",{className:"flex items-center justify-between mb-4",children:o.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[o.jsx(zc,{className:"w-5 h-5"}),"Prints (",y?.length||0,")"]})}),E?o.jsx("div",{className:"flex items-center justify-center py-8",children:o.jsx(xt,{className:"w-6 h-6 animate-spin text-bambu-green"})}):o.jsx(HWe,{archives:y||[]})]}),i&&o.jsx(tne,{project:{...b,archive_count:ye?.total_archives||0,total_items:ye?.total_items||0,queue_count:ye?.queued_prints||0,progress_percent:ye?.progress_percent||null,archives:[]},onClose:()=>a(!1),onSave:U=>k.mutate(U),isLoading:k.isPending}),ue.isOpen&&o.jsx(mr,{title:ue.title,message:ue.message,confirmText:"Delete",variant:"danger",onConfirm:ue.onConfirm,onCancel:()=>Ae(U=>({...U,isOpen:!1}))})]})}const Fk=5,WWe=2e3,qWe=3e4,KWe=5e3;function XWe(){const{printerId:t}=eR(),e=parseInt(t||"0",10),[n,r]=S.useState("stream"),[i,a]=S.useState(!1),[s,l]=S.useState(!0),[c,u]=S.useState(Date.now()),[d,f]=S.useState(!1),[g,p]=S.useState(!1),[b,v]=S.useState(0),[x,y]=S.useState(!1),[E,w]=S.useState(0),A=S.useRef(null),T=S.useRef(null),C=S.useRef(null),N=S.useRef(null),O=S.useRef(null),{data:k}=pt({queryKey:["printer",e],queryFn:()=>_e.getPrinter(e),enabled:e>0});S.useEffect(()=>(k&&(document.title=`${k.name} - Camera`),()=>{document.title="Bambuddy"}),[k]);const j=S.useRef(!1);S.useEffect(()=>{const Y=`/api/v1/printers/${e}/camera/stop`;j.current=!1;const F=()=>{e>0&&!j.current&&(j.current=!0,navigator.sendBeacon(Y))},$=()=>{F()};return window.addEventListener("beforeunload",$),()=>{window.removeEventListener("beforeunload",$),A.current&&(A.current.src=""),F()}},[e]),S.useEffect(()=>{if(s&&!d){const F=setTimeout(()=>{l(!1)},n==="stream"?3e3:2e4);return()=>clearTimeout(F)}},[n,s,c,d]),S.useEffect(()=>{const Y=()=>{const F=!!document.fullscreenElement;p(F),n==="stream"&&!d&&(A.current&&(A.current.src=""),setTimeout(()=>{l(!0),u(Date.now())},200))};return document.addEventListener("fullscreenchange",Y),()=>document.removeEventListener("fullscreenchange",Y)},[n,d]),S.useEffect(()=>{let Y;const F=()=>{clearTimeout(Y),Y=setTimeout(()=>{localStorage.setItem("cameraWindowState",JSON.stringify({width:window.outerWidth,height:window.outerHeight,left:window.screenX,top:window.screenY}))},500)};return window.addEventListener("resize",F),()=>{clearTimeout(Y),window.removeEventListener("resize",F)}},[]),S.useEffect(()=>()=>{C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),O.current&&clearInterval(O.current)},[]);const B=S.useCallback(()=>{if(b>=Fk){y(!1),a(!0);return}const Y=Math.min(WWe*Math.pow(2,b),qWe);y(!0),w(Math.ceil(Y/1e3)),N.current=setInterval(()=>{w(F=>F<=1?(N.current&&clearInterval(N.current),0):F-1)},1e3),C.current=setTimeout(()=>{v(F=>F+1),y(!1),l(!0),a(!1),A.current&&(A.current.src=""),u(Date.now())},Y)},[b]);S.useEffect(()=>{if(n!=="stream"||s||i||x||d){O.current&&(clearInterval(O.current),O.current=null);return}return O.current=setInterval(async()=>{try{const Y=await fetch(`/api/v1/printers/${e}/camera/status`);Y.ok&&(await Y.json()).stalled&&(console.log("Stream stall detected, auto-reconnecting..."),O.current&&(clearInterval(O.current),O.current=null),l(!1),B())}catch{}},KWe),()=>{O.current&&(clearInterval(O.current),O.current=null)}},[n,s,i,x,d,e,B]);const I=()=>{l(!1),n==="stream"&&b{if(l(!1),a(!1),v(0),y(!1),C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),A.current&&!localStorage.getItem("cameraWindowState")){const Y=A.current,F=Y.naturalWidth,$=Y.naturalHeight;if(F>0&&$>0){const se=window.outerWidth-window.innerWidth,pe=window.outerHeight-window.innerHeight,Z=F+16+se,Me=$+45+16+pe;try{window.resizeTo(Z,Me)}catch{}}}},R=()=>{e>0&&fetch(`/api/v1/printers/${e}/camera/stop`).catch(()=>{})},W=Y=>{n===Y||d||(f(!0),l(!0),a(!1),v(0),y(!1),C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),A.current&&(A.current.src=""),n==="stream"&&R(),setTimeout(()=>{r(Y),u(Date.now()),f(!1)},100))},K=()=>{d||(f(!0),l(!0),a(!1),v(0),y(!1),C.current&&clearTimeout(C.current),N.current&&clearInterval(N.current),A.current&&(A.current.src=""),n==="stream"&&R(),setTimeout(()=>{u(Date.now()),f(!1)},100))},G=()=>{T.current&&(document.fullscreenElement?document.exitFullscreen():T.current.requestFullscreen())},J=d?"":n==="stream"?`/api/v1/printers/${e}/camera/stream?fps=10&t=${c}`:`/api/v1/printers/${e}/camera/snapshot?t=${c}`,ee=s||d||x;return e?o.jsxs("div",{ref:T,className:"min-h-screen bg-black flex flex-col",children:[o.jsxs("div",{className:"flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary",children:[o.jsxs("h1",{className:"text-sm font-medium text-white flex items-center gap-2",children:[o.jsx(R0,{className:"w-4 h-4"}),k?.name||`Printer ${e}`]}),o.jsxs("div",{className:"flex items-center gap-2",children:[o.jsxs("div",{className:"flex bg-bambu-dark rounded p-0.5",children:[o.jsx("button",{onClick:()=>W("stream"),disabled:ee,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"}),o.jsx("button",{onClick:()=>W("snapshot"),disabled:ee,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"})]}),o.jsx("button",{onClick:K,disabled:ee,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50",title:n==="stream"?"Restart stream":"Refresh snapshot",children:o.jsx(li,{className:`w-4 h-4 text-bambu-gray ${ee?"animate-spin":""}`})}),o.jsx("button",{onClick:G,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded",title:g?"Exit fullscreen":"Fullscreen",children:g?o.jsx(sue,{className:"w-4 h-4 text-bambu-gray"}):o.jsx(Qce,{className:"w-4 h-4 text-bambu-gray"})})]})]}),o.jsx("div",{className:"flex-1 flex items-center justify-center p-2",children:o.jsxs("div",{className:"relative w-full h-full flex items-center justify-center",children:[(s||d)&&!x&&o.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/50 z-10",children:o.jsxs("div",{className:"text-center",children:[o.jsx(li,{className:"w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2"}),o.jsx("p",{className:"text-sm text-bambu-gray",children:n==="stream"?"Connecting to camera...":"Capturing snapshot..."})]})}),x&&o.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/80 z-10",children:o.jsxs("div",{className:"text-center p-4",children:[o.jsx(kb,{className:"w-10 h-10 text-orange-400 mx-auto mb-3"}),o.jsx("p",{className:"text-white mb-2",children:"Connection lost"}),o.jsxs("p",{className:"text-sm text-bambu-gray mb-3",children:["Reconnecting in ",E,"s... (attempt ",b+1,"/",Fk,")"]}),o.jsx("button",{onClick:K,className:"px-4 py-2 bg-bambu-green text-white text-sm rounded hover:bg-bambu-green/80 transition-colors",children:"Reconnect now"})]})}),i&&!x&&o.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black z-10",children:o.jsxs("div",{className:"text-center p-4",children:[o.jsx(qn,{className:"w-12 h-12 text-orange-400 mx-auto mb-3"}),o.jsx("p",{className:"text-white mb-2",children:"Camera unavailable"}),o.jsx("p",{className:"text-xs text-bambu-gray mb-4 max-w-md",children:"Make sure the printer is powered on and connected."}),o.jsx("button",{onClick:K,className:"px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors",children:"Retry"})]})}),o.jsx("img",{ref:A,src:J,alt:"Camera stream",className:"max-w-full max-h-full object-contain",onError:J?I:void 0,onLoad:J?q:void 0},c)]})})]}):o.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:o.jsx("p",{className:"text-white",children:"Invalid printer ID"})})}function YWe(){const{id:t}=eR(),{mode:e}=og(),{data:n,isLoading:r,error:i}=pt({queryKey:["external-link",t],queryFn:()=>_e.getExternalLink(Number(t)),enabled:!!t});return r?o.jsx("div",{className:"flex items-center justify-center h-full",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):i||!n?o.jsxs("div",{className:"flex flex-col items-center justify-center h-full gap-4 text-bambu-gray",children:[o.jsx(qn,{className:"w-12 h-12"}),o.jsx("p",{children:"Link not found"})]}):o.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"})}function Yr({icon:t,label:e,value:n,subValue:r,color:i="text-bambu-green"}){return o.jsxs("div",{className:"flex items-start gap-3 p-4 bg-bambu-dark rounded-lg",children:[o.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark-tertiary ${i}`,children:o.jsx(t,{className:"w-5 h-5"})}),o.jsxs("div",{className:"flex-1 min-w-0",children:[o.jsx("p",{className:"text-sm text-bambu-gray",children:e}),o.jsx("p",{className:"text-lg font-semibold text-white truncate",children:n}),r&&o.jsx("p",{className:"text-xs text-bambu-gray mt-0.5",children:r})]})]})}function t9({percent:t,color:e="bg-bambu-green"}){return o.jsx("div",{className:"w-full h-2 bg-bambu-dark rounded-full overflow-hidden",children:o.jsx("div",{className:`h-full ${e} transition-all duration-300`,style:{width:`${Math.min(100,t)}%`}})})}function Bu({title:t,icon:e,children:n}){return o.jsxs(At,{className:"p-6",children:[o.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[o.jsx(e,{className:"w-5 h-5 text-bambu-green"}),o.jsx("h2",{className:"text-lg font-semibold text-white",children:t})]}),n]})}function ZWe(){const{t}=ng(),e=dn(),[n,r]=S.useState(null),[i,a]=S.useState(!1),[s,l]=S.useState(!1),{data:c,isLoading:u,refetch:d,isFetching:f}=pt({queryKey:["systemInfo"],queryFn:_e.getSystemInfo,refetchInterval:3e4}),{data:g}=pt({queryKey:["debugLogging"],queryFn:p1.getDebugLoggingState,staleTime:10*1e3,refetchInterval:10*1e3}),p=async()=>{l(!0);try{const y=await p1.setDebugLogging(!g?.enabled);e.setQueryData(["debugLogging"],y)}catch(y){console.error("Failed to toggle debug logging:",y)}finally{l(!1)}},b=async()=>{r(null),a(!0);try{await p1.downloadSupportBundle()}catch(y){r(y instanceof Error?y.message:"Failed to download support bundle")}finally{a(!1)}};if(u)return o.jsx("div",{className:"flex items-center justify-center h-64",children:o.jsx(xt,{className:"w-8 h-8 text-bambu-green animate-spin"})});if(!c)return o.jsx("div",{className:"p-6 text-center text-bambu-gray",children:t("system.failedToLoad","Failed to load system information")});const v=c.storage.disk_percent_used>90?"bg-red-500":c.storage.disk_percent_used>75?"bg-yellow-500":"bg-bambu-green",x=c.memory.percent_used>90?"bg-red-500":c.memory.percent_used>75?"bg-yellow-500":"bg-bambu-green";return o.jsxs("div",{className:"p-6 space-y-6",children:[o.jsxs("div",{className:"flex items-center justify-between",children:[o.jsxs("div",{children:[o.jsx("h1",{className:"text-2xl font-bold text-white",children:t("system.title","System Information")}),o.jsx("p",{className:"text-bambu-gray mt-1",children:t("system.subtitle","Monitor system resources and database statistics")})]}),o.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:[o.jsx(li,{className:`w-4 h-4 ${f?"animate-spin":""}`}),t("common.refresh","Refresh")]})]}),o.jsx(Bu,{title:t("system.application","Application"),icon:ph,children:o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[o.jsx(Yr,{icon:ph,label:t("system.version","Version"),value:`v${c.app.version}`}),o.jsx(Yr,{icon:ur,label:t("system.uptime","System Uptime"),value:c.system.uptime_formatted}),o.jsx(Yr,{icon:ph,label:t("system.hostname","Hostname"),value:c.system.hostname})]})}),o.jsx(Bu,{title:t("support.title","Support & Troubleshooting"),icon:vce,children:o.jsxs("div",{className:"space-y-4",children:[o.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.")}),o.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:`p-2 rounded-lg ${g?.enabled?"bg-amber-500/20 text-amber-500":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:o.jsx(B9,{className:"w-5 h-5"})}),o.jsxs("div",{children:[o.jsx("p",{className:"font-medium text-white",children:t("support.debugLogging","Debug Logging")}),o.jsxs("p",{className:"text-sm text-bambu-gray",children:[g?.enabled?t("support.debugLoggingEnabled","Capturing detailed logs"):t("support.debugLoggingDisabled","Normal logging level"),g?.enabled&&g.duration_seconds!==null&&o.jsxs("span",{className:"text-amber-400 ml-2",children:["(",Math.floor(g.duration_seconds/60),"m ",g.duration_seconds%60,"s)"]})]})]})]}),o.jsxs("button",{onClick:p,disabled:s,className:`px-4 py-2 rounded-lg font-medium transition-colors flex items-center gap-2 ${g?.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:[s&&o.jsx(xt,{className:"w-4 h-4 animate-spin"}),g?.enabled?t("support.disableDebug","Disable"):t("support.enableDebug","Enable")]})]}),o.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-green",children:o.jsx(Wi,{className:"w-5 h-5"})}),o.jsxs("div",{children:[o.jsx("p",{className:"font-medium text-white",children:t("support.supportBundle","Support Bundle")}),o.jsx("p",{className:"text-sm text-bambu-gray",children:t("support.supportBundleDescription","Download system info and logs as a ZIP file")})]})]}),o.jsxs("button",{onClick:b,disabled:i||!g?.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:g?.enabled?void 0:t("support.enableDebugFirst","Enable debug logging first"),children:[i&&o.jsx(xt,{className:"w-4 h-4 animate-spin"}),t("common.download","Download")]})]}),n&&o.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/30 rounded-lg text-red-400 text-sm",children:n}),!g?.enabled&&o.jsx("div",{className:"p-4 bg-bambu-dark-tertiary/50 rounded-lg",children:o.jsxs("p",{className:"text-sm text-bambu-gray",children:[o.jsx("span",{className:"text-amber-400 font-medium",children:t("support.instructions","To report an issue:")}),o.jsx("br",{}),"1. ",t("support.step1","Enable debug logging"),o.jsx("br",{}),"2. ",t("support.step2","Reproduce the issue"),o.jsx("br",{}),"3. ",t("support.step3","Download the support bundle"),o.jsx("br",{}),"4. ",t("support.step4","Attach the ZIP file to your issue report")]})}),o.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg space-y-3",children:[o.jsx("p",{className:"text-sm font-medium text-white",children:t("support.privacyTitle","What's in the support bundle?")}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 text-sm",children:[o.jsxs("div",{children:[o.jsx("p",{className:"text-bambu-green font-medium mb-1",children:t("support.collected","Collected:")}),o.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[o.jsxs("li",{children:["• ",t("support.collectItem1","App version and debug mode")]}),o.jsxs("li",{children:["• ",t("support.collectItem2","OS, architecture, Python version")]}),o.jsxs("li",{children:["• ",t("support.collectItem3","Database statistics (counts only)")]}),o.jsxs("li",{children:["• ",t("support.collectItem4","Printer models and nozzle counts")]}),o.jsxs("li",{children:["• ",t("support.collectItem5","Non-sensitive settings (themes, formats)")]}),o.jsxs("li",{children:["• ",t("support.collectItem6","Debug logs (sanitized)")]})]})]}),o.jsxs("div",{children:[o.jsx("p",{className:"text-red-400 font-medium mb-1",children:t("support.notCollected","NOT collected:")}),o.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[o.jsxs("li",{children:["• ",t("support.notItem1","Printer names, IPs, serial numbers")]}),o.jsxs("li",{children:["• ",t("support.notItem2","Access codes and passwords")]}),o.jsxs("li",{children:["• ",t("support.notItem3","Email addresses")]}),o.jsxs("li",{children:["• ",t("support.notItem4","API keys and tokens")]}),o.jsxs("li",{children:["• ",t("support.notItem5","Webhook URLs")]}),o.jsxs("li",{children:["• ",t("support.notItem6","Your hostname or username")]})]})]})]}),o.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].")})]})]})}),o.jsxs(Bu,{title:t("system.database","Database"),icon:Ep,children:[o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 mb-4",children:[o.jsx(Yr,{icon:co,label:t("system.totalArchives","Total Archives"),value:c.database.archives}),o.jsx(Yr,{icon:cR,label:t("system.completed","Completed"),value:c.database.archives_completed,color:"text-green-500"}),o.jsx(Yr,{icon:Ra,label:t("system.failed","Failed"),value:c.database.archives_failed,color:"text-red-500"}),o.jsx(Yr,{icon:xt,label:t("system.printing","Printing"),value:c.database.archives_printing,color:"text-yellow-500"})]}),o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsx(Yr,{icon:qi,label:t("system.printers","Printers"),value:c.database.printers}),o.jsx(Yr,{icon:bR,label:t("system.filaments","Filaments"),value:c.database.filaments}),o.jsx(Yr,{icon:Bo,label:t("system.projects","Projects"),value:c.database.projects}),o.jsx(Yr,{icon:uo,label:t("system.smartPlugs","Smart Plugs"),value:c.database.smart_plugs})]}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 mt-4",children:[o.jsx(Yr,{icon:ur,label:t("system.totalPrintTime","Total Print Time"),value:c.database.total_print_time_formatted}),o.jsx(Yr,{icon:co,label:t("system.totalFilament","Total Filament Used"),value:`${c.database.total_filament_kg} kg`,subValue:`${c.database.total_filament_grams.toLocaleString()} g`})]})]}),o.jsxs(Bu,{title:t("system.connectedPrinters","Connected Printers"),icon:qi,children:[o.jsxs("div",{className:"flex items-center gap-4 mb-4",children:[o.jsx("div",{className:"text-3xl font-bold text-bambu-green",children:c.printers.connected}),o.jsx("div",{className:"text-bambu-gray",children:t("system.ofTotal","of {{total}} printers connected",{total:c.printers.total})})]}),c.printers.connected_list.length>0?o.jsx("div",{className:"space-y-2",children:c.printers.connected_list.map(y=>o.jsxs("div",{className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg",children:[o.jsxs("div",{className:"flex items-center gap-3",children:[o.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green"}),o.jsx("span",{className:"font-medium text-white",children:y.name})]}),o.jsxs("div",{className:"flex items-center gap-4 text-sm text-bambu-gray",children:[o.jsx("span",{children:y.model}),o.jsx("span",{className:`px-2 py-0.5 rounded ${y.state==="RUNNING"?"bg-bambu-green/20 text-bambu-green":y.state==="IDLE"?"bg-blue-500/20 text-blue-400":"bg-bambu-dark-tertiary"}`,children:y.state})]})]},y.id))}):o.jsx("p",{className:"text-bambu-gray",children:t("system.noPrintersConnected","No printers connected")})]}),o.jsx(Bu,{title:t("system.storage","Storage"),icon:pR,children:o.jsxs("div",{className:"space-y-4",children:[o.jsxs("div",{children:[o.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[o.jsx("span",{className:"text-bambu-gray",children:t("system.diskUsage","Disk Usage")}),o.jsxs("span",{className:"text-white",children:[c.storage.disk_used_formatted," / ",c.storage.disk_total_formatted]})]}),o.jsx(t9,{percent:c.storage.disk_percent_used,color:v}),o.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),"%)"]})]}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[o.jsx(Yr,{icon:co,label:t("system.archiveStorage","Archive Storage"),value:c.storage.archive_size_formatted}),o.jsx(Yr,{icon:Ep,label:t("system.databaseSize","Database Size"),value:c.storage.database_size_formatted})]})]})}),o.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[o.jsx(Bu,{title:t("system.memory","Memory"),icon:eue,children:o.jsx("div",{className:"space-y-4",children:o.jsxs("div",{children:[o.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[o.jsx("span",{className:"text-bambu-gray",children:t("system.memoryUsage","Memory Usage")}),o.jsxs("span",{className:"text-white",children:[c.memory.used_formatted," / ",c.memory.total_formatted]})]}),o.jsx(t9,{percent:c.memory.percent_used,color:x}),o.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:[c.memory.available_formatted," ",t("system.available","available")]})]})})}),o.jsx(Bu,{title:t("system.cpu","CPU"),icon:Cv,children:o.jsx("div",{className:"space-y-4",children:o.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[o.jsx(Yr,{icon:Cv,label:t("system.cores","Cores"),value:c.cpu.count,subValue:`${c.cpu.count_logical} logical`}),o.jsx(Yr,{icon:Cv,label:t("system.usage","Usage"),value:`${c.cpu.percent}%`})]})})})]}),o.jsx(Bu,{title:t("system.systemDetails","System Details"),icon:ph,children:o.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[o.jsx(Yr,{icon:ph,label:t("system.os","Operating System"),value:c.system.platform,subValue:c.system.platform_release}),o.jsx(Yr,{icon:Cv,label:t("system.architecture","Architecture"),value:c.system.architecture}),o.jsx(Yr,{icon:ph,label:t("system.python","Python"),value:c.system.python_version}),o.jsx(Yr,{icon:ur,label:t("system.bootTime","Boot Time"),value:new Date(c.system.boot_time).toLocaleString()})]})})]})}function QWe(){const t=S.useRef(null),e=S.useRef(null),n=dn(),[r,i]=S.useState(!1),a=S.useRef(new Set),s=S.useRef(null),l=S.useRef(new Map),c=S.useRef(null),u=S.useRef([]),d=S.useRef(!1),f=S.useRef(()=>{}),g=S.useCallback(()=>{if(d.current||u.current.length===0)return;d.current=!0;const E=()=>{const w=u.current.shift();w?requestAnimationFrame(()=>{f.current(w),u.current.length>0?setTimeout(E,16):d.current=!1}):d.current=!1};E()},[]),p=S.useCallback(()=>{if(t.current?.readyState===WebSocket.OPEN)return;const w=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.host}/api/v1/ws`,A=new WebSocket(w);let T=null;A.onopen=()=>{console.log("[WebSocket] Connected"),i(!0),T=window.setInterval(()=>{A.readyState===WebSocket.OPEN&&A.send(JSON.stringify({type:"ping"}))},3e4)},A.onmessage=C=>{try{const N=JSON.parse(C.data);N.type==="printer_status"&&N.printer_id!==void 0&&N.data?f.current(N):(u.current.push(N),g())}catch{}},A.onclose=C=>{console.log("[WebSocket] Closed",C.code,C.reason),T&&(clearInterval(T),T=null),i(!1),t.current=null,e.current=window.setTimeout(()=>{p()},3e3)},A.onerror=C=>{console.error("[WebSocket] Error",C),A.close()},t.current=A},[]),b=S.useCallback((E,w)=>{const A=l.current.get(E)||{};l.current.set(E,{...A,...w}),c.current||(c.current=window.setTimeout(()=>{const T=new Map(l.current);l.current.clear(),c.current=null,requestAnimationFrame(()=>{T.forEach((C,N)=>{n.setQueryData(["printerStatus",N],O=>{const k={...O,...C};return k.wifi_signal==null&&O?.wifi_signal!=null&&(k.wifi_signal=O.wifi_signal),k})})})},100))},[n]),v=S.useCallback(E=>{a.current.add(E),s.current&&clearTimeout(s.current),s.current=window.setTimeout(()=>{const w=Array.from(a.current);a.current.clear(),s.current=null;let A=0;w.forEach(T=>{setTimeout(()=>{requestAnimationFrame(()=>{n.invalidateQueries({queryKey:[T]})})},A),A+=500})},3e3)},[n]),x=S.useCallback(E=>{switch(E.type){case"printer_status":E.printer_id!==void 0&&E.data&&b(E.printer_id,E.data);break;case"print_start":E.printer_id!==void 0&&n.invalidateQueries({queryKey:["printerStatus",E.printer_id]});break;case"print_complete":E.printer_id!==void 0&&n.invalidateQueries({queryKey:["printerStatus",E.printer_id]}),v("archives"),v("archiveStats");break;case"archive_created":v("archives"),v("archiveStats");break;case"archive_updated":v("archives");break}},[n,v,b]);S.useEffect(()=>{f.current=x},[x]),S.useEffect(()=>(p(),()=>{e.current&&clearTimeout(e.current),s.current&&clearTimeout(s.current),c.current&&clearTimeout(c.current),t.current&&t.current.close()}),[p]);const y=S.useCallback(E=>{t.current?.readyState===WebSocket.OPEN&&t.current.send(JSON.stringify(E))},[]);return{isConnected:r,sendMessage:y}}const JWe=new foe({defaultOptions:{queries:{staleTime:1e3*60,retry:1}}});function eqe({children:t}){return QWe(),o.jsx(o.Fragment,{children:t})}function tqe(){return o.jsx(Hde,{children:o.jsx(Jde,{children:o.jsx(moe,{client:JWe,children:o.jsx(eqe,{children:o.jsx(Ose,{children:o.jsxs(hse,{children:[o.jsx(Za,{path:"/camera/:printerId",element:o.jsx(XWe,{})}),o.jsxs(Za,{path:"/",element:o.jsx(Yde,{}),children:[o.jsx(Za,{index:!0,element:o.jsx(vCe,{})}),o.jsx(Za,{path:"archives",element:o.jsx(J7e,{})}),o.jsx(Za,{path:"queue",element:o.jsx(F9e,{})}),o.jsx(Za,{path:"stats",element:o.jsx(Q9e,{})}),o.jsx(Za,{path:"profiles",element:o.jsx(jWe,{})}),o.jsx(Za,{path:"maintenance",element:o.jsx(LWe,{})}),o.jsx(Za,{path:"projects",element:o.jsx(FWe,{})}),o.jsx(Za,{path:"projects/:id",element:o.jsx(GWe,{})}),o.jsx(Za,{path:"settings",element:o.jsx(_We,{})}),o.jsx(Za,{path:"system",element:o.jsx(ZWe,{})}),o.jsx(Za,{path:"external/:id",element:o.jsx(YWe,{})})]})]})})})})})})}gie.createRoot(document.getElementById("root")).render(o.jsx(S.StrictMode,{children:o.jsx(tqe,{})})); diff --git a/static/index.html b/static/index.html index 8da53d05b..983b04a37 100644 --- a/static/index.html +++ b/static/index.html @@ -23,7 +23,7 @@ - +