From 8981c11baccef39b17e4eb66cc09503f8beb604d Mon Sep 17 00:00:00 2001 From: maziggy Date: Mon, 16 Feb 2026 08:21:09 +0100 Subject: [PATCH] fix: spool form scrollbar flicker in Edge (#364), delete SD card file after print (#374) Add scrollbar-gutter: stable to spool form modal to prevent scrollbar layout thrashing on Windows Edge at non-default zoom levels. Delete uploaded .3mf from printer SD card root after print completion to prevent phantom prints on power cycle (reported on P1S). --- CHANGELOG.md | 1 + frontend/src/components/SpoolFormModal.tsx | 2 +- static/assets/{index-LH77R5cV.js => index-B6_GHLIb.js} | 2 +- static/index.html | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) rename static/assets/{index-LH77R5cV.js => index-B6_GHLIb.js} (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a70c2fbf..97c65386c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ All notable changes to Bambuddy will be documented in this file. - **K-Profile Auto-Select Fails for Non-BL Spools on Dual-Nozzle Printers** — When assigning a third-party spool to an AMS slot on dual-nozzle printers (H2D, H2D Pro), the MQTT auto-configure step crashed with `'SpoolKProfile' object has no attribute 'extruder_id'`. The K-profile model uses `extruder` (not `extruder_id`). Fixed the attribute name so K-profile matching correctly filters by nozzle on dual-extruder printers. - **Loose Archive Name Matching Could Cause Wrong Archive Reuse** ([#374](https://github.com/maziggy/bambuddy/issues/374)) — The `on_print_start` callback used `ilike('%{name}%')` to find existing "printing" archives, which meant a print named "Clip" could incorrectly match "Cable Clip" or "Clip Stand". This could cause a new print to reuse the wrong archive or skip creating one. Tightened to exact `print_name` match or exact filename variants (`.3mf`, `.gcode.3mf`). - **Phantom Prints on Power Cycle** ([#374](https://github.com/maziggy/bambuddy/issues/374)) — The print queue uploaded `.3mf` files to the printer's SD card root (`/`) but never deleted them after the print finished. Some printers (e.g. P1S) auto-start files found in the root directory on power cycle, causing ghost prints on every reboot. Now deletes the uploaded file from the SD card after print completion (best-effort, non-blocking). +- **Spool Form Scrollbar Flicker in Edge** ([#364](https://github.com/maziggy/bambuddy/issues/364)) — The Add/Edit Spool modal's scrollable area used `overflow-y: auto`, which on Windows Edge (where scrollbars take layout space) caused the scrollbar to appear and disappear on hover — making the color picker unusable at certain zoom levels. Added `scrollbar-gutter: stable` to reserve scrollbar space and prevent layout thrashing. - **Archive Duplicate Badge Misses Name-Based Duplicates** ([#315](https://github.com/maziggy/bambuddy/issues/315)) — The duplicate badge on archive cards only matched by file content hash, so re-sliced prints of the same model (different GCODE, same print name) were not flagged as duplicates. Now also matches by print name (case-insensitive), consistent with the detail view's duplicate detection. ### Improved diff --git a/frontend/src/components/SpoolFormModal.tsx b/frontend/src/components/SpoolFormModal.tsx index 2fab4433a..5b2a8e2eb 100644 --- a/frontend/src/components/SpoolFormModal.tsx +++ b/frontend/src/components/SpoolFormModal.tsx @@ -411,7 +411,7 @@ export function SpoolFormModal({ isOpen, onClose, spool, printersWithCalibration {/* Content */} -
+
{activeTab === 'filament' ? (
{/* Filament Info Section */} diff --git a/static/assets/index-LH77R5cV.js b/static/assets/index-B6_GHLIb.js similarity index 99% rename from static/assets/index-LH77R5cV.js rename to static/assets/index-B6_GHLIb.js index 4226ec97b..ea83aef99 100644 --- a/static/assets/index-LH77R5cV.js +++ b/static/assets/index-B6_GHLIb.js @@ -8679,6 +8679,6 @@ Accuracy: ${t.time_accuracy.toFixed(0)}%`:t.actual_time_seconds?`Actual: ${Ug(t. `+n+"]";if(typeof t=="object"){const r=Object.entries(t);return r.length===0?"{}":`{ `+r.map(([s,o])=>n+" "+JSON.stringify(s)+": "+mF(o,e+1)).join(`, `)+` -`+n+"}"}return String(t)}function MZe({setting:t,onClose:e,onDeleted:n,onDuplicate:r,onEdit:i,hasPermission:s,t:o}){const{showToast:l}=mn(),c=tn(),[u,d]=w.useState(!1),{data:h,isLoading:f}=tt({queryKey:["cloudSettingDetail",t.setting_id],queryFn:()=>se.getCloudSettingDetail(t.setting_id)}),p=lt({mutationFn:()=>se.deleteCloudSetting(t.setting_id),onSuccess:()=>{l(o("profiles.presets.toast.deleted")),c.invalidateQueries({queryKey:["cloudSettings"]}),n()},onError:b=>l(b.message,"error")}),x=mv(t.setting_id),v=bI(t.name,h?.setting?.inherits);return a.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:a.jsx(_t,{className:"w-full max-w-3xl max-h-[90vh] flex flex-col overflow-hidden",children:a.jsxs(Nt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[a.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("h2",{className:"text-xl font-semibold text-white truncate",children:t.name}),x&&a.jsx("span",{className:"px-2 py-0.5 text-xs font-medium bg-bambu-green/20 text-bambu-green rounded-full",children:o("profiles.presets.editable")})]}),a.jsxs("div",{className:"flex items-center gap-2 mt-1 text-sm text-bambu-gray",children:[a.jsx("span",{className:"capitalize",children:o(`profiles.presets.types.${t.type}`)}),v.printer&&a.jsxs(a.Fragment,{children:[a.jsx("span",{children:"•"}),a.jsx("span",{children:v.printer})]})]})]}),a.jsx("button",{onClick:e,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto p-4",children:f?a.jsx("div",{className:"flex items-center justify-center py-16",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):h?a.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:mF(h)}):a.jsx("div",{className:"text-center py-16 text-bambu-gray",children:o("profiles.presets.failedToLoadDetails")})}),u?a.jsxs("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary bg-red-500/5",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 text-red-400",children:[a.jsx(Cn,{className:"w-5 h-5"}),a.jsx("span",{className:"font-medium",children:o("profiles.presets.deleteConfirm")})]}),a.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:o("profiles.presets.deleteWarning",{name:t.name})}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:()=>d(!1),disabled:p.isPending,className:"flex-1",children:o("common.cancel")}),a.jsxs(Me,{variant:"danger",onClick:()=>p.mutate(),disabled:p.isPending,className:"flex-1",children:[p.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):a.jsx(en,{className:"w-4 h-4"}),o("common.delete")]})]})]}):a.jsx("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary",children:a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:e,className:"flex-1",children:o("common.close")}),a.jsxs(Me,{variant:"secondary",onClick:r,disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.presets.noDuplicatePermission"),children:[a.jsx(Wl,{className:"w-4 h-4"}),o("profiles.presets.duplicate")]}),x&&a.jsxs(a.Fragment,{children:[a.jsxs(Me,{variant:"secondary",onClick:i,disabled:f||!h||!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.presets.noEditPermission"),children:[a.jsx($i,{className:"w-4 h-4"}),o("common.edit")]}),a.jsx(Me,{variant:"danger",onClick:()=>d(!0),disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.presets.noDeletePermission"),children:a.jsx(en,{className:"w-4 h-4"})})]})]})})]})})})}function Gae(){try{const t=localStorage.getItem("bambusy_preset_templates");return t?JSON.parse(t):[]}catch{return[]}}function qae(t){localStorage.setItem("bambusy_preset_templates",JSON.stringify(t))}function AZe({onClose:t,onApply:e,t:n}){const{showToast:r}=mn(),[i,s]=w.useState(Gae),[o,l]=w.useState("all"),[c,u]=w.useState(null),[d,h]=w.useState(""),[f,p]=w.useState(""),[x,v]=w.useState("{}"),[b,y]=w.useState(null),[S,N]=w.useState(null),A=o==="all"?i:i.filter(B=>B.type===o),M=B=>{s(B),qae(B)},T=B=>{const R=i.filter(V=>V.id!==B);M(R),N(null),r(n("profiles.templates.toast.deleted"))},k=B=>{u(B.id),h(B.name),p(B.description),v(JSON.stringify(B.settings,null,2)),y(null)},j=()=>{if(!(!c||!d.trim()))try{const B=JSON.parse(x),R=i.map(V=>V.id===c?{...V,name:d.trim(),description:f.trim(),settings:B}:V);M(R),u(null),r(n("profiles.templates.toast.updated"))}catch(B){y(B.message)}},E=()=>{u(null),h(""),p(""),v("{}"),y(null)},P=B=>{const R=i.map(V=>V.id===B?{...V,showInModal:!V.showInModal}:V);M(R)},U={filament:{label:n("profiles.presets.types.filament"),icon:Jk,color:"text-amber-400"},print:{label:n("profiles.presets.types.process"),icon:td,color:"text-blue-400"},printer:{label:n("profiles.presets.types.printer"),icon:Cr,color:"text-purple-400"}},L=S?i.find(B=>B.id===S):null;return w.useEffect(()=>{const B=R=>{R.key==="Escape"&&(S?N(null):c?E():t())};return window.addEventListener("keydown",B),()=>window.removeEventListener("keydown",B)},[S,c,t]),a.jsxs("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:[L&&a.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-60",children:a.jsx(_t,{className:"w-full max-w-md",children:a.jsxs(Nt,{className:"p-6",children:[a.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[a.jsx("div",{className:"p-2 bg-red-500/20 rounded-lg",children:a.jsx(Cn,{className:"w-6 h-6 text-red-400"})}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-lg font-semibold text-white",children:n("profiles.templates.deleteTitle")}),a.jsx("p",{className:"text-sm text-bambu-gray",children:n("profiles.templates.deleteWarning")})]})]}),a.jsx("p",{className:"text-white mb-6",children:n("profiles.templates.deleteConfirm",{name:L.name})}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:()=>N(null),className:"flex-1",children:n("common.cancel")}),a.jsxs(Me,{onClick:()=>T(S),className:"flex-1 bg-red-500 hover:bg-red-600",children:[a.jsx(en,{className:"w-4 h-4"}),n("common.delete")]})]})]})})}),a.jsx(_t,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",children:a.jsxs(Nt,{className:"p-0 flex flex-col h-full",children:[a.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(pb,{className:"w-5 h-5 text-amber-400"}),n("profiles.templates.title")]}),a.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsxs("div",{className:"flex items-center gap-2 p-4 border-b border-bambu-dark-tertiary",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:n("profiles.templates.typeFilter")}),["all","filament","print","printer"].map(B=>a.jsx("button",{onClick:()=>l(B),className:`px-3 py-1 text-sm rounded-lg transition-colors ${o===B?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:B==="all"?n("common.all"):U[B].label},B))]}),a.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:A.length===0?a.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[a.jsx(pb,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),a.jsx("p",{children:n("profiles.templates.noTemplates")}),a.jsx("p",{className:"text-sm mt-1",children:n("profiles.templates.createFirst")})]}):a.jsx("div",{className:"space-y-2",children:A.map(B=>{const R=U[B.type],V=R.icon;return c===B.id?a.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg border border-bambu-green",children:[a.jsxs("div",{className:"grid grid-cols-2 gap-3 mb-3",children:[a.jsx("input",{type:"text",value:d,onChange:q=>h(q.target.value),placeholder:n("profiles.templates.namePlaceholder"),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}),a.jsx("input",{type:"text",value:f,onChange:q=>p(q.target.value),placeholder:n("profiles.templates.descriptionPlaceholder"),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"})]}),a.jsxs("div",{className:"mb-3",children:[a.jsx("label",{className:"text-xs text-bambu-gray mb-1 block",children:n("profiles.templates.settingsJson")}),a.jsx("textarea",{value:x,onChange:q=>{v(q.target.value),y(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 ${b?"border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`}),b&&a.jsx("p",{className:"text-xs text-red-400 mt-1",children:b})]}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{size:"sm",onClick:j,disabled:!d.trim(),children:[a.jsx(bs,{className:"w-4 h-4"}),"Save"]}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:E,children:"Cancel"})]})]},B.id):a.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:[a.jsx(V,{className:`w-5 h-5 ${R.color} flex-shrink-0`}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm font-medium text-white",children:B.name}),a.jsx("p",{className:"text-xs text-bambu-gray truncate",children:B.description})]}),a.jsx("span",{className:"text-xs text-bambu-gray-dark px-2 py-1 bg-bambu-dark-secondary rounded",children:n("profiles.templates.fieldsCount",{count:Object.keys(B.settings).length})}),a.jsx("button",{onClick:()=>P(B.id),className:`p-1 transition-colors ${B.showInModal?"text-bambu-green hover:text-bambu-green/70":"text-bambu-gray hover:text-white"}`,title:B.showInModal?n("profiles.templates.shownInModals"):n("profiles.templates.hiddenInModals"),children:B.showInModal?a.jsx(Kl,{className:"w-4 h-4"}):a.jsx(gv,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>e(B),className:"px-3 py-1 text-xs bg-bambu-green/20 text-bambu-green rounded hover:bg-bambu-green/30 transition-colors",children:n("profiles.templates.apply")}),a.jsx("button",{onClick:()=>k(B),className:"p-1 text-bambu-gray hover:text-white",title:n("common.edit"),children:a.jsx($i,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>N(B.id),className:"p-1 text-bambu-gray hover:text-red-400",title:n("common.delete"),children:a.jsx(en,{className:"w-4 h-4"})})]},B.id)})})})]})})]})}function Wae({onClose:t,leftPreset:e,rightPreset:n,leftLabel:r,rightLabel:i,t:s}){const[o,l]=w.useState("changes"),[c,u]=w.useState(""),d=w.useMemo(()=>{const x=new Set([...Object.keys(e),...Object.keys(n)]),v=[];for(const b of x){if(b==="inherits"||b==="version")continue;const y=e[b],S=n[b],N=b in e,A=b in n;let M;!N&&A?M="added":N&&!A?M="removed":JSON.stringify(y)!==JSON.stringify(S)?M="changed":M="same",v.push({key:b,left:y,right:S,status:M})}return v.sort((b,y)=>{const S={changed:0,added:1,removed:2,same:3};return S[b.status]!==S[y.status]?S[b.status]-S[y.status]:b.key.localeCompare(y.key)})},[e,n]),h=w.useMemo(()=>{let x=[...d];if(o==="changes"&&(x=x.filter(v=>v.status!=="same")),c){const v=c.toLowerCase();x=x.filter(b=>b.key.toLowerCase().includes(v)||String(b.left).toLowerCase().includes(v)||String(b.right).toLowerCase().includes(v))}return x},[d,o,c]),f=w.useMemo(()=>({added:d.filter(x=>x.status==="added").length,removed:d.filter(x=>x.status==="removed").length,changed:d.filter(x=>x.status==="changed").length,same:d.filter(x=>x.status==="same").length}),[d]),p=x=>{if(x===void 0)return"—";if(x===null)return"null";if(Array.isArray(x))return x.length===0?"[]":x.length===1?String(x[0]):x.join(", ");if(typeof x=="object")return JSON.stringify(x);const v=String(x);if(v.includes("\\n")||v.length>100){const b=v.split("\\n").length;if(b>1)return`[${b} lines of G-code/script]`;if(v.length>100)return v.substring(0,100)+"…"}return v};return w.useEffect(()=>{const x=v=>{v.key==="Escape"&&t()};return window.addEventListener("keydown",x),()=>window.removeEventListener("keydown",x)},[t]),a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsx(_t,{className:"w-full max-w-4xl max-h-[85vh] flex flex-col overflow-hidden",children:a.jsxs(Nt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[a.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(Yg,{className:"w-5 h-5 text-blue-400"}),s("profiles.diff.title")]}),a.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsxs("div",{className:"flex-shrink-0 grid grid-cols-2 gap-4 p-4 border-b border-bambu-dark-tertiary bg-bambu-dark",children:[a.jsxs("div",{className:"text-center",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:s("profiles.diff.left")}),a.jsx("p",{className:"text-white font-medium truncate",children:r})]}),a.jsxs("div",{className:"text-center",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:s("profiles.diff.right")}),a.jsx("p",{className:"text-white font-medium truncate",children:i})]})]}),a.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[a.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[a.jsx(ar,{className:"w-3.5 h-3.5"}),f.added," ",s("profiles.diff.added")]}),a.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[a.jsx(x3,{className:"w-3.5 h-3.5"}),f.removed," ",s("profiles.diff.removed")]}),a.jsxs("span",{className:"flex items-center gap-1 text-amber-400",children:[a.jsx(d3,{className:"w-3.5 h-3.5"}),f.changed," ",s("profiles.diff.changed")]}),a.jsxs("span",{className:"flex items-center gap-1 text-bambu-gray",children:[a.jsx(ZT,{className:"w-3.5 h-3.5"}),f.same," ",s("profiles.diff.same")]})]}),a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsxs("div",{className:"relative",children:[a.jsx(Or,{className:"absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",value:c,onChange:x=>u(x.target.value),placeholder:s("profiles.diff.searchFields"),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&&a.jsxs("div",{className:"flex rounded overflow-hidden border border-bambu-dark-tertiary",children:[a.jsx("button",{onClick:()=>l("changes"),className:`px-3 py-1.5 text-sm transition-colors ${o==="changes"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:s("profiles.diff.changes")}),a.jsx("button",{onClick:()=>l("all"),className:`px-3 py-1.5 text-sm transition-colors ${o==="all"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:s("common.all")})]})]})]}),a.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto",children:h.length===0?a.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[a.jsx(ZT,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),a.jsx("p",{children:s(o==="changes"?"profiles.diff.noDifferences":"profiles.diff.noFieldsMatch")})]}):a.jsxs("table",{className:"w-full",children:[a.jsx("thead",{className:"sticky top-0 bg-bambu-dark-secondary",children:a.jsxs("tr",{className:"text-sm text-bambu-gray border-b border-bambu-dark-tertiary",children:[a.jsx("th",{className:"text-left p-3 w-1/3",children:s("profiles.diff.field")}),a.jsx("th",{className:"text-left p-3 w-1/3",children:r}),a.jsx("th",{className:"text-left p-3 w-1/3",children:i})]})}),a.jsx("tbody",{children:h.map(x=>{const v={added:"bg-green-500/10",removed:"bg-red-500/10",changed:"bg-amber-500/10",same:""}[x.status],b={added:a.jsx(ar,{className:"w-3.5 h-3.5 text-green-400"}),removed:a.jsx(x3,{className:"w-3.5 h-3.5 text-red-400"}),changed:a.jsx(d3,{className:"w-3.5 h-3.5 text-amber-400"}),same:a.jsx(ZT,{className:"w-3.5 h-3.5 text-bambu-gray-dark"})}[x.status];return a.jsxs("tr",{className:`border-b border-bambu-dark-tertiary ${v}`,children:[a.jsx("td",{className:"p-3",children:a.jsxs("div",{className:"flex items-center gap-2",children:[b,a.jsx("span",{className:"text-sm text-white font-mono",children:x.key})]})}),a.jsx("td",{className:"p-3",children:a.jsx("span",{className:`text-sm font-mono break-all ${x.status==="removed"?"text-red-300":x.status==="changed"?"text-white":"text-bambu-gray"}`,children:p(x.left)})}),a.jsx("td",{className:"p-3",children:a.jsx("span",{className:`text-sm font-mono break-all ${x.status==="added"?"text-green-300":x.status==="changed"?"text-white":"text-bambu-gray"}`,children:p(x.right)})})]},x.key)})})]})})]})})})}function TZe({onClose:t,initialData:e,allPresets:n,t:r}){const{showToast:i}=mn(),s=tn(),o=!!e?.setting_id,[l,c]=w.useState("common"),[u,d]=w.useState(e?.type||"filament"),[h,f]=w.useState(e?.name?o?e.name:`${e.name} (Copy)`:""),[p,x]=w.useState(e?.base_id||""),[v,b]=w.useState(""),[y,S]=w.useState(e?.setting||{inherits:""}),[N,A]=w.useState(JSON.stringify(e?.setting||{inherits:""},null,2)),[M,T]=w.useState(null),[k,j]=w.useState(""),[E,P]=w.useState(!1),[U,L]=w.useState(""),[B,R]=w.useState(!1),[V,q]=w.useState(Gae),[I,K]=w.useState(!1),[Y,le]=w.useState(""),[H,X]=w.useState(""),[te,J]=w.useState(!0),[Se,fe]=w.useState(null),[me,De]=w.useState(!1),de=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer})[u]||[],[n,u]),ce=w.useMemo(()=>de.filter(ke=>mv(ke.setting_id)),[de]),{data:Ne}=tt({queryKey:["cloudFields",u],queryFn:()=>se.getCloudFields(u==="print"?"process":u),staleTime:1e3*60*60}),{data:ie}=tt({queryKey:["allPresetDetails",u,ce.map(ke=>ke.setting_id).join(",")],queryFn:async()=>{const ke={};for(let je=0;je{try{const Pe=await se.getCloudSettingDetail(Je.setting_id);return{id:Je.setting_id,detail:Pe}}catch{return null}}))).forEach(Je=>{Je&&(ke[Je.id]=Je.detail)})}return ke},enabled:ce.length>0,staleTime:1e3*60*10}),{data:Le,isLoading:$}=tt({queryKey:["cloudSettingDetail",p],queryFn:()=>se.getCloudSettingDetail(p),enabled:!!p});w.useEffect(()=>{l!=="json"&&A(JSON.stringify(y,null,2))},[y,l]);const W=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer}[u]||[]).filter(Ue=>!mv(Ue.setting_id)).sort((Ue,je)=>Ue.name.localeCompare(je.name)),[n,u]);w.useEffect(()=>{if(!p)return;const ke=W.find(Ue=>Ue.setting_id===p);ke&&(b(ke.name),o||(S({inherits:ke.name}),A(JSON.stringify({inherits:ke.name},null,2))))},[p,W,o]);const ue=w.useMemo(()=>{const ke=Ne?.fields||[],Ue=new Set(ke.map(Je=>Je.key)),je=new Set,Ve=new Set(["inherits","updated_time","compatible_printers","compatible_prints"]);ie&&Object.values(ie).forEach(Je=>{Je?.setting&&Object.keys(Je.setting).forEach(Pe=>{!Ue.has(Pe)&&!Ve.has(Pe)&&je.add(Pe)})}),Object.keys(y).forEach(Je=>{!Ue.has(Je)&&!Ve.has(Je)&&je.add(Je)});const Ae=Array.from(je).sort().map(Je=>({key:Je,label:Je.replace(/_/g," ").replace(/\b\w/g,Pe=>Pe.toUpperCase()),type:"text",category:"discovered",description:r("profiles.presets.discoveredFromPresets")}));return[...ke,...Ae]},[Ne,ie,y,r]),Z=ue.filter(ke=>ke.label.toLowerCase().includes(k.toLowerCase())||ke.key.toLowerCase().includes(k.toLowerCase())),oe=()=>{if(U.trim()){const ke=U.trim().toLowerCase().replace(/\s+/g,"_");Q(ke,""),L(""),R(!1),i(r("profiles.presets.toast.fieldAdded",{key:ke}))}},Q=(ke,Ue)=>{S(je=>{const Ve={...je};return Ue===""||Ue===void 0?delete Ve[ke]:Ve[ke]=Ue,Ve})},he=ke=>{S(Ue=>({...Ue,...ke.settings})),fe(ke.name),i(r("profiles.templates.toast.applied"))},be=()=>{if(!Y.trim())return;const ke={...y};if(delete ke.inherits,Object.keys(ke).length===0){i(r("profiles.presets.noOverridesToSave"),"error");return}const Ue={id:Date.now().toString(),name:Y.trim(),description:H.trim()||r("profiles.presets.customTemplate"),type:u,settings:ke,showInModal:te},je=[...V,Ue];q(je),qae(je),K(!1),le(""),X(""),J(!0),i(r("profiles.templates.toast.created"))},ee=w.useMemo(()=>V.filter(ke=>ke.type===u&&ke.showInModal),[u,V]),z=ke=>{A(ke);try{const Ue=JSON.parse(ke);S(Ue),T(null)}catch(Ue){T(Ue.message)}},D=ke=>{ke.preventDefault(),P(!1);const Ue=ke.dataTransfer.files[0];if(Ue&&Ue.name.endsWith(".json")){const je=new FileReader;je.onload=Ve=>{try{const Ae=Ve.target?.result,Je=JSON.parse(Ae),Pe=Je.setting||Je;S(Ye=>({...Ye,...Pe})),A(JSON.stringify({...y,...Pe},null,2)),i(r("profiles.presets.fileImported"))}catch{i(r("profiles.presets.invalidJsonFile"),"error")}},je.readAsText(Ue)}},G=lt({mutationFn:()=>{const ke={...y},Ue=u==="filament"?"filament_settings_id":u==="print"?"print_settings_id":"printer_settings_id";ke[Ue]=`"${h}"`;const je={type:u,name:h,base_id:p,setting:ke};return se.createCloudSetting(je)},onSuccess:async()=>{i(r("profiles.presets.toast.created")),await s.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ke=>i(ke.message,"error")}),pe=lt({mutationFn:()=>{if(!e?.setting_id)throw new Error(r("profiles.presets.noSettingId"));return se.updateCloudSetting(e.setting_id,{name:h,setting:y})},onSuccess:async()=>{i(r("profiles.presets.toast.updated")),s.removeQueries({queryKey:["cloudSettingDetail"]}),await s.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ke=>i(ke.message,"error")}),ve=o?pe:G,xe=Le?.setting?.inherits,ze=xe?W.find(ke=>ke.name===xe):void 0,{data:_e}=tt({queryKey:["cloudSettingDetail",ze?.setting_id],queryFn:()=>se.getCloudSettingDetail(ze.setting_id),enabled:!!ze?.setting_id}),He=w.useMemo(()=>{const ke=_e?.setting||{},Ue=Le?.setting||{},je=p&&ie?.[p]?.setting?ie[p].setting:{};return{...ke,...Ue,...je}},[p,Le,_e,ie]),Ze=ke=>{if(ke==null)return"";if(Array.isArray(ke)){const Ue=[...new Set(ke.map(je=>String(je)))];return Ue.length===1?Ue[0]:ke.join(", ")}return String(ke)},we=ke=>{const Ue=y[ke.key],je=He[ke.key],Ve=Ze(je),Ae=$?r("common.loading"):Ve||"",Je="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(ke.type==="boolean"){const Pe=Ue==="1"||Ue===void 0&&je==="1";return a.jsx("button",{type:"button",onClick:()=>Q(ke.key,Ue==="1"?"0":"1"),className:`w-8 h-5 rounded-full transition-colors ${Pe?"bg-bambu-green":"bg-bambu-dark-tertiary"}`,children:a.jsx("div",{className:`w-4 h-4 rounded-full bg-white shadow transition-transform ${Pe?"translate-x-3.5":"translate-x-0.5"}`})})}return ke.type==="select"?a.jsxs("select",{value:Ue||"",onChange:Pe=>Q(ke.key,Pe.target.value),className:Je,children:[a.jsx("option",{value:"",children:Ae}),ke.options?.map(Pe=>a.jsx("option",{value:Pe.value,children:Pe.label},Pe.value))]}):a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("input",{type:ke.type==="number"?"number":"text",value:Ue!==void 0?String(Ue):"",onChange:Pe=>Q(ke.key,Pe.target.value),step:ke.step,min:ke.min,max:ke.max,placeholder:Ae,className:Je}),ke.unit&&a.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:ke.unit})]})},ye=w.useMemo(()=>Le?.setting?Le.setting:{},[Le]);return a.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onDragOver:ke=>{ke.preventDefault(),P(!0)},onDragLeave:()=>P(!1),onDrop:D,children:[me&&p&&a.jsx(Wae,{onClose:()=>De(!1),leftPreset:ye,rightPreset:y,leftLabel:r("profiles.presets.baseLabel",{name:v||p}),rightLabel:r("profiles.presets.currentLabel",{name:h||r("profiles.presets.newPreset")}),t:r}),a.jsx(_t,{className:"w-full max-w-6xl max-h-[90vh] flex flex-col overflow-y-auto",children:a.jsxs(Nt,{className:"p-0 flex flex-col h-full",children:[a.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{children:[a.jsx("h2",{className:"text-xl font-semibold text-white",children:r(o?"profiles.presets.editPreset":e?"profiles.presets.duplicatePreset":"profiles.presets.createNewPreset")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:r("profiles.presets.customizeSettings")})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[p&&a.jsxs("button",{onClick:()=>De(!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:r("profiles.presets.compareWithBase"),children:[a.jsx(Yg,{className:"w-4 h-4"}),r("profiles.presets.compare")]}),a.jsx("button",{onClick:t,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:a.jsx(Ft,{className:"w-5 h-5"})})]})]}),E&&a.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:a.jsxs("div",{className:"text-center",children:[a.jsx(ia,{className:"w-12 h-12 text-bambu-green mx-auto mb-2"}),a.jsx("p",{className:"text-bambu-green font-medium",children:r("profiles.presets.dropJsonToImport")})]})}),a.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary space-y-3",children:[a.jsxs("div",{className:"grid grid-cols-3 gap-4 max-[640px]:grid-cols-1",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:r("common.type")}),a.jsxs("select",{value:u,onChange:ke=>{d(ke.target.value),x("")},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:[a.jsx("option",{value:"filament",children:r("profiles.presets.types.filament")}),a.jsx("option",{value:"print",children:r("profiles.presets.types.process")}),a.jsx("option",{value:"printer",children:r("profiles.presets.types.printer")})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:r("profiles.presets.basePreset")}),a.jsxs("select",{value:p,onChange:ke=>x(ke.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:[a.jsx("option",{value:"",children:r("profiles.presets.selectBasePreset")}),W.map(ke=>a.jsx("option",{value:ke.setting_id,children:ke.name},ke.setting_id))]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:r("profiles.presets.presetName")}),a.jsx("input",{type:"text",value:h,onChange:ke=>f(ke.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:r("profiles.presets.myCustomPreset")})]})]}),v&&a.jsx("div",{className:"text-xs text-bambu-gray",children:a.jsxs("p",{className:"flex items-center gap-1",children:[a.jsx(Xr,{className:"w-3 h-3 text-bambu-green"}),r("profiles.presets.inheritsFrom")," ",a.jsx("span",{className:"text-white",children:v}),$&&a.jsx(at,{className:"w-3 h-3 animate-spin ml-1"})]})})]}),a.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary max-[640px]:flex-wrap max-[640px]:items-center",children:[a.jsxs("button",{onClick:()=>c("common"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${l==="common"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(HB,{className:"w-4 h-4"}),r("profiles.presets.tabs.common")]}),a.jsxs("button",{onClick:()=>c("fields"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${l==="fields"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(rC,{className:"w-4 h-4"}),r("profiles.presets.tabs.allFields")]}),a.jsxs("button",{onClick:()=>c("json"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${l==="json"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(uX,{className:"w-4 h-4"}),"JSON",M&&a.jsx(Bi,{className:"w-3 h-3 text-red-400"})]}),a.jsx("div",{className:"flex-1 max-[640px]:hidden"}),a.jsxs("button",{onClick:()=>{const ke={name:h,type:u,base_id:p,setting:y},Ue=new Blob([JSON.stringify(ke,null,2)],{type:"application/json"}),je=URL.createObjectURL(Ue),Ve=document.createElement("a");Ve.href=je,Ve.download=`${h||"preset"}.json`,document.body.appendChild(Ve),Ve.click(),document.body.removeChild(Ve),URL.revokeObjectURL(je),i(r("profiles.presets.toast.exported"))},className:"flex items-center gap-2 px-4 py-3 text-sm text-bambu-gray hover:text-white transition-colors",title:r("profiles.presets.exportToJson"),children:[a.jsx(di,{className:"w-4 h-4"}),r("common.download")]}),a.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:r("profiles.presets.importFromJson"),children:[a.jsx(ia,{className:"w-4 h-4"}),r("common.upload")]}),a.jsx("input",{id:"file-import",type:"file",accept:".json",className:"hidden",onChange:ke=>{const Ue=ke.target.files?.[0];if(Ue){const je=new FileReader;je.onload=Ve=>{try{const Ae=JSON.parse(Ve.target?.result),Je=Ae.setting||Ae;S(Pe=>({...Pe,...Je})),i(r("profiles.presets.fileImported"))}catch{i(r("profiles.presets.invalidJson"),"error")}},je.readAsText(Ue)}}})]}),a.jsxs("div",{className:"flex-1 p-4",children:[l==="common"&&a.jsxs("div",{className:"space-y-6",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-3",children:[a.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:[a.jsx(pb,{className:"w-4 h-4 text-amber-400"}),r("profiles.templates.title")]}),Object.keys(y).filter(ke=>ke!=="inherits").length>0&&a.jsxs("button",{onClick:()=>K(!I),className:"text-xs text-bambu-gray hover:text-white flex items-center gap-1 transition-colors",children:[a.jsx(bs,{className:"w-3 h-3"}),r("profiles.presets.saveAsTemplate")]})]}),I&&a.jsxs("div",{className:"mb-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"grid grid-cols-2 gap-2 mb-2",children:[a.jsx("input",{type:"text",value:Y,onChange:ke=>le(ke.target.value),placeholder:r("profiles.templates.namePlaceholder"),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}),a.jsx("input",{type:"text",value:H,onChange:ke=>X(ke.target.value),placeholder:r("profiles.templates.descriptionPlaceholder"),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"})]}),a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{size:"sm",onClick:be,disabled:!Y.trim(),children:[a.jsx(bs,{className:"w-3 h-3"}),r("common.save")]}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>K(!1),children:r("common.cancel")})]}),a.jsxs("button",{onClick:()=>J(!te),className:`flex items-center gap-1.5 text-xs transition-colors ${te?"text-bambu-green":"text-bambu-gray hover:text-white"}`,children:[te?a.jsx(Kl,{className:"w-3.5 h-3.5"}):a.jsx(gv,{className:"w-3.5 h-3.5"}),r(te?"profiles.templates.shownInModals":"profiles.templates.hiddenInModals")]})]})]}),Se&&a.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:[a.jsx(Xr,{className:"w-4 h-4 text-bambu-green"}),a.jsxs("span",{className:"text-sm text-bambu-green",children:[r("profiles.presets.templateApplied")," ",a.jsx("span",{className:"font-medium",children:Se})]}),a.jsx("button",{onClick:()=>fe(null),className:"ml-auto text-bambu-green/70 hover:text-bambu-green",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),a.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[ee.map(ke=>a.jsxs("button",{onClick:()=>he(ke),className:"p-3 text-left bg-bambu-dark border border-bambu-dark-tertiary rounded-lg hover:border-bambu-gray-dark transition-colors",children:[a.jsx("p",{className:"text-sm font-medium text-white",children:ke.name}),a.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:ke.description})]},ke.id)),ee.length===0&&a.jsx("p",{className:"col-span-3 text-center text-bambu-gray text-sm py-4",children:r("profiles.presets.noTemplatesSelected")})]}),a.jsx("p",{className:"text-xs text-bambu-gray-dark mt-2 text-center",children:r("profiles.presets.manageTemplatesHint")})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:r("profiles.presets.commonSettings")}),a.jsx("div",{className:"grid grid-cols-2 gap-x-6 gap-y-3",children:ue.slice(0,10).map(ke=>a.jsxs("div",{className:"flex items-center justify-between gap-4 max-[640px]:flex-col max-[640px]:items-start",children:[a.jsx("label",{className:"text-sm text-bambu-gray flex-shrink-0",children:ke.label}),a.jsx("div",{className:"w-48 max-[640px]:w-full",children:we(ke)})]},ke.key))})]}),Object.keys(y).length>1&&a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:r("profiles.presets.currentOverrides")}),a.jsx("div",{className:"flex flex-wrap gap-2",children:Object.entries(y).filter(([ke])=>ke!=="inherits").map(([ke,Ue])=>a.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-green/10 text-bambu-green text-xs rounded",children:[ke,": ",String(Ue).slice(0,20),a.jsx("button",{onClick:()=>Q(ke,void 0),className:"hover:text-white",children:a.jsx(Ft,{className:"w-3 h-3"})})]},ke))})]})]}),l==="fields"&&a.jsxs("div",{className:"grid grid-cols-2 gap-6",style:{height:"400px"},children:[a.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[a.jsx("h3",{className:"text-sm font-medium text-white",children:r("profiles.presets.availableFields")}),a.jsx("span",{className:"text-xs text-bambu-gray",children:ie?r("profiles.templates.fieldsCount",{count:ue.length}):r("common.loading")})]}),a.jsxs("div",{className:"relative mb-3 flex-shrink-0",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",value:k,onChange:ke=>j(ke.target.value),placeholder:r("profiles.presets.searchFieldsPlaceholder"),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"})]}),a.jsxs("div",{className:"flex-1 overflow-y-auto space-y-1 pr-2 min-h-0",children:[Z.filter(ke=>!(ke.key in y)).map(ke=>{const Ue=He[ke.key],je=Ze(Ue);return a.jsxs("div",{onClick:()=>{S(Ve=>({...Ve,[ke.key]:je||""}))},className:"flex items-center justify-between gap-2 p-2 rounded-lg hover:bg-bambu-dark-tertiary transition-colors cursor-pointer group",children:[a.jsxs("div",{className:"min-w-0 flex-1",children:[a.jsx("p",{className:"text-sm text-white truncate",children:ke.label}),a.jsx("p",{className:"text-xs text-bambu-gray-dark truncate",children:ke.key})]}),a.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[je&&a.jsxs("span",{className:"text-xs text-bambu-gray bg-bambu-dark px-2 py-0.5 rounded max-w-32 truncate",title:je,children:[je.slice(0,20),je.length>20?"...":""]}),a.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:a.jsx(ar,{className:"w-4 h-4 text-bambu-gray group-hover:text-bambu-green transition-colors"})})]})]},ke.key)}),Z.filter(ke=>!(ke.key in y)).length===0&&a.jsx("p",{className:"text-center text-bambu-gray py-4 text-sm",children:r(k?"profiles.presets.noMatchingFields":"profiles.presets.allFieldsAdded")})]}),a.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:B?a.jsxs("div",{className:"flex gap-2",children:[a.jsx("input",{type:"text",value:U,onChange:ke=>L(ke.target.value),onKeyDown:ke=>ke.key==="Enter"&&oe(),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}),a.jsx(Me,{size:"sm",onClick:oe,disabled:!U.trim(),children:a.jsx(ar,{className:"w-4 h-4"})}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>{R(!1),L("")},children:a.jsx(Ft,{className:"w-4 h-4"})})]}):a.jsxs("button",{onClick:()=>R(!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:[a.jsx(ar,{className:"w-4 h-4"}),r("profiles.presets.addCustomField")]})})]}),a.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[a.jsx("h3",{className:"text-sm font-medium text-white",children:r("profiles.presets.yourOverrides")}),a.jsx("span",{className:"text-xs text-bambu-gray",children:r("profiles.templates.fieldsCount",{count:Object.keys(y).filter(ke=>ke!=="inherits").length})})]}),a.jsxs("div",{className:"flex-1 overflow-y-auto space-y-2 pr-2 min-h-0",children:[Object.entries(y).filter(([ke])=>ke!=="inherits").map(([ke,Ue])=>{const je=ue.find(Ve=>Ve.key===ke);return a.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center justify-between mb-2",children:[a.jsxs("div",{children:[a.jsx("p",{className:"text-sm font-medium text-white",children:je?.label||ke}),a.jsx("p",{className:"text-xs text-bambu-gray-dark",children:ke})]}),a.jsx("button",{onClick:()=>Q(ke,void 0),className:"p-1 text-bambu-gray hover:text-red-400 transition-colors",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),je?we(je):a.jsx("input",{type:"text",value:String(Ue),onChange:Ve=>Q(ke,Ve.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"})]},ke)}),Object.keys(y).filter(ke=>ke!=="inherits").length===0&&a.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[a.jsx(HB,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),a.jsx("p",{className:"text-sm",children:r("profiles.presets.noOverridesYet")}),a.jsx("p",{className:"text-xs mt-1",children:r("profiles.presets.clickFieldsToAdd")})]})]}),Object.keys(y).filter(ke=>ke!=="inherits").length>0&&a.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:a.jsxs("button",{onClick:()=>{K(!0),c("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:[a.jsx(bs,{className:"w-4 h-4"}),r("profiles.presets.saveAsTemplate")]})})]})]}),l==="json"&&a.jsxs("div",{className:"space-y-2",children:[M&&a.jsxs("div",{className:"flex items-center gap-2 text-red-400 text-sm",children:[a.jsx(Bi,{className:"w-4 h-4"}),M]}),a.jsx("textarea",{value:N,onChange:ke=>z(ke.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 ${M?"border-red-500 focus:border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`,spellCheck:!1}),a.jsx("p",{className:"text-xs text-bambu-gray",children:r("profiles.presets.jsonTip")})]})]}),a.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:t,className:"flex-1",children:r("common.cancel")}),a.jsxs(Me,{onClick:()=>ve.mutate(),disabled:ve.isPending||!h.trim()||!o&&!p||!!M,className:"flex-1",children:[ve.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):o?a.jsx(bs,{className:"w-4 h-4"}):a.jsx(ar,{className:"w-4 h-4"}),r(o?"common.save":e?"common.duplicate":"common.create")]})]})]})})]})}function EZe({settings:t,lastSyncTime:e,onRefresh:n,isRefreshing:r,printers:i,hasPermission:s,t:o}){const[l,c]=w.useState(""),[u,d]=w.useState("all"),[h,f]=w.useState("all"),[p,x]=w.useState("all"),[v,b]=w.useState("all"),[y,S]=w.useState("all"),[N,A]=w.useState("all"),[M,T]=w.useState(null),[k,j]=w.useState(!1),[E,P]=w.useState(!1),[U,L]=w.useState(null),[B,R]=w.useState(null),[V,q]=w.useState(null),[I,K]=w.useState(!1),[Y,le]=w.useState([null,null]),[H,X]=w.useState(!1),[te,J]=w.useState(null),Se=tn(),fe=w.useMemo(()=>[...t.filament.map(oe=>({...oe,type:"filament"})),...t.printer.map(oe=>({...oe,type:"printer"})),...t.process.map(oe=>({...oe,type:"process"}))].map(oe=>({...oe,meta:bI(oe.name)})),[t]),me=w.useMemo(()=>{const Z=new Set,oe=new Set,Q=new Set;return fe.forEach(he=>{he.meta.nozzle&&Z.add(he.meta.nozzle),he.meta.filamentType&&oe.add(he.meta.filamentType),he.meta.layerHeight&&Q.add(he.meta.layerHeight)}),{printers:i.map(he=>({id:he.id.toString(),name:he.name})),nozzles:Array.from(Z).sort((he,be)=>parseFloat(he)-parseFloat(be)),filaments:Array.from(oe).sort(),layerHeights:Array.from(Q).sort((he,be)=>parseFloat(he)-parseFloat(be))}},[fe,i]),De=w.useMemo(()=>p==="all"?null:i.find(oe=>oe.id.toString()===p)?.model||null,[p,i]),de=w.useMemo(()=>fe.filter(Z=>u==="all"||Z.type===u).filter(Z=>{if(h==="all")return!0;const oe=mv(Z.setting_id);return h==="custom"?oe:!oe}).filter(Z=>{if(p==="all"||!De)return!0;const oe=Z.meta.printer?.toLowerCase()||"",Q=De.toLowerCase();return oe.includes(Q)||Q.includes(oe)}).filter(Z=>v==="all"||Z.meta.nozzle===v).filter(Z=>y==="all"||Z.meta.filamentType===y).filter(Z=>N==="all"||Z.meta.layerHeight===N).filter(Z=>l===""||Z.name.toLowerCase().includes(l.toLowerCase())).sort((Z,oe)=>Z.name.localeCompare(oe.name)),[fe,u,h,p,De,v,y,N,l]),ce=Z=>{if(I){const oe=Y[0]?.setting_id===Z.setting_id,Q=Y[1]?.setting_id===Z.setting_id;if(oe)le([Y[1],null]);else if(Q)le([Y[0],null]);else if(!Y[0])le([Z,null]);else if(Y[1]){if(Y[0].type!==Z.type)return;le([Y[0],Z])}else{if(Y[0].type!==Z.type)return;le([Y[0],Z])}}else T(Z)},Ne=Z=>{if(Y[0]?.setting_id===Z.setting_id)return 0;if(Y[1]?.setting_id===Z.setting_id)return 1},ie=async Z=>{try{const oe=await se.getCloudSettingDetail(Z.setting_id),Q=Z.type==="process"?"print":Z.type;L({type:Q,name:Z.name,base_id:oe.base_id||"GFSA00",setting:oe.setting||{}}),T(null)}catch(oe){console.error("Failed to fetch preset details for duplication:",oe)}},Le=async Z=>{try{Se.removeQueries({queryKey:["cloudSettingDetail",Z.setting_id]});const oe=await se.getCloudSettingDetail(Z.setting_id),Q=Z.type==="process"?"print":Z.type;R({type:Q,name:Z.name,base_id:oe.base_id||"GFSA00",setting:oe.setting||{},setting_id:Z.setting_id}),T(null)}catch(oe){console.error("Failed to fetch preset details for editing:",oe)}},$=()=>{d("all"),f("all"),x("all"),b("all"),S("all"),A("all"),c("")},W=u!=="all"||h!=="all"||p!=="all"||v!=="all"||y!=="all"||N!=="all"||l!=="",ue=t.filament.length+t.printer.length+t.process.length;return a.jsxs(a.Fragment,{children:[a.jsxs("div",{className:"space-y-4 mb-6",children:[a.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",value:l,onChange:Z=>c(Z.target.value),placeholder:o("profiles.cloudView.searchPlaceholder"),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"})]}),a.jsxs("div",{className:"flex gap-2 max-[550px]:flex-wrap max-[550px]:items-center",children:[a.jsxs(Me,{variant:I?"primary":"secondary",onClick:()=>{I?(K(!1),le([null,null])):K(!0)},children:[a.jsx(Yg,{className:"w-4 h-4"}),o(I?"common.cancel":"profiles.presets.compare")]}),a.jsxs(Me,{variant:"secondary",onClick:()=>P(!0),disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.cloudView.noTemplatesPermission"),children:[a.jsx(pb,{className:"w-4 h-4"}),o("profiles.cloudView.templates")]}),a.jsxs(Me,{variant:"secondary",onClick:n,disabled:r||!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.cloudView.noRefreshPermission"),children:[a.jsx(kr,{className:`w-4 h-4 ${r?"animate-spin":""}`}),o("profiles.cloudView.refresh")]}),a.jsxs(Me,{onClick:()=>j(!0),disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.cloudView.noCreatePermission"),children:[a.jsx(ar,{className:"w-4 h-4"}),o("profiles.cloudView.newPreset")]})]})]}),a.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[a.jsx(tC,{className:"w-4 h-4 text-bambu-gray"}),a.jsx(jg,{label:o("profiles.cloudView.filters.type"),value:u,options:[{value:"all",label:o("profiles.cloudView.filters.all"),count:ue},{value:"filament",label:o("profiles.cloudView.filters.filament"),count:t.filament.length},{value:"printer",label:o("profiles.cloudView.filters.printer"),count:t.printer.length},{value:"process",label:o("profiles.cloudView.filters.process"),count:t.process.length}],onChange:Z=>d(Z)}),a.jsx(jg,{label:o("profiles.cloudView.filters.owner"),value:h,options:[{value:"all",label:o("profiles.cloudView.filters.all")},{value:"custom",label:o("profiles.cloudView.filters.myPresets")},{value:"builtin",label:o("profiles.cloudView.filters.builtIn")}],onChange:Z=>f(Z)}),me.printers.length>0&&a.jsx(jg,{label:o("profiles.cloudView.filters.printer"),value:p,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.printers.map(Z=>({value:Z.id,label:Z.name}))],onChange:x}),me.nozzles.length>0&&a.jsx(jg,{label:o("profiles.cloudView.filters.nozzle"),value:v,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.nozzles.map(Z=>({value:Z,label:Z}))],onChange:b}),me.filaments.length>0&&(u==="all"||u==="filament")&&a.jsx(jg,{label:o("profiles.cloudView.filters.filament"),value:y,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.filaments.map(Z=>({value:Z,label:Z}))],onChange:S}),me.layerHeights.length>0&&(u==="all"||u==="process")&&a.jsx(jg,{label:o("profiles.cloudView.filters.layer"),value:N,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.layerHeights.map(Z=>({value:Z,label:Z}))],onChange:A}),W&&a.jsx("button",{onClick:$,className:"px-3 py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:o("profiles.cloudView.clearFilters")})]})]}),I&&a.jsx("div",{className:"mb-4 p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex items-center gap-4",children:[a.jsx(Yg,{className:"w-5 h-5 text-blue-400"}),a.jsx("span",{className:"text-white font-medium",children:o("profiles.cloudView.compareMode")}),a.jsx("span",{className:"text-bambu-gray",children:Y[0]?o("profiles.cloudView.selectAnotherPreset",{type:Y[0].type}):o("profiles.cloudView.clickTwoPresets")})]}),a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${Y[0]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:Y[0]?Y[0].name:o("profiles.cloudView.selectFirst")}),a.jsx(d3,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${Y[1]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:Y[1]?Y[1].name:o("profiles.cloudView.selectSecond")})]}),Y[0]&&Y[1]&&a.jsxs(Me,{size:"sm",onClick:async()=>{try{const[Z,oe]=await Promise.all([se.getCloudSettingDetail(Y[0].setting_id),se.getCloudSettingDetail(Y[1].setting_id)]);J([Z.setting||{},oe.setting||{}]),X(!0)}catch{}},children:[a.jsx(Yg,{className:"w-4 h-4"}),o("profiles.cloudView.compareNow")]})]})]})}),a.jsxs("div",{className:"flex flex-wrap items-center gap-4 mb-4 text-sm text-bambu-gray",children:[e&&a.jsxs("div",{className:"flex items-center gap-1",children:[a.jsx(Qn,{className:"w-3 h-3"}),o("profiles.cloudView.lastSynced")," ",NZe(e.toISOString(),o)]}),a.jsx("span",{children:o("profiles.cloudView.showingCount",{showing:de.length,total:ue})}),a.jsxs("div",{className:"flex items-center gap-1",children:[a.jsx("span",{className:"w-1.5 h-1.5 rounded-full bg-bambu-green"}),a.jsxs("span",{children:["= ",o("profiles.presets.myPreset")]})]})]}),de.length===0?a.jsxs("div",{className:"text-center py-16",children:[a.jsx(rs,{className:"w-12 h-12 text-bambu-gray-dark mx-auto mb-4"}),a.jsx("p",{className:"text-bambu-gray",children:o("profiles.cloudView.noPresetsFound")}),W&&a.jsx("button",{onClick:$,className:"mt-2 text-sm text-bambu-green hover:text-bambu-green-light",children:o("profiles.cloudView.clearFilters")})]}):a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[a.jsx(Jk,{className:"w-4 h-4 text-amber-400"}),a.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:o("profiles.cloudView.columns.filament")}),a.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",de.filter(Z=>Z.type==="filament").length,")"]})]}),a.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[de.filter(Z=>Z.type==="filament").map(Z=>a.jsx($j,{setting:Z,onClick:()=>ce(Z),onDuplicate:()=>ie(Z),compareMode:I,isCompareSelected:Ne(Z)!==void 0,compareIndex:Ne(Z),compareDisabled:I&&!!Y[0]&&Y[0].type!==Z.type,t:o},Z.setting_id)),de.filter(Z=>Z.type==="filament").length===0&&a.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:o("profiles.cloudView.noFilamentPresets")})]})]}),a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[a.jsx(td,{className:"w-4 h-4 text-blue-400"}),a.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:o("profiles.cloudView.columns.process")}),a.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",de.filter(Z=>Z.type==="process").length,")"]})]}),a.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[de.filter(Z=>Z.type==="process").map(Z=>a.jsx($j,{setting:Z,onClick:()=>ce(Z),onDuplicate:()=>ie(Z),compareMode:I,isCompareSelected:Ne(Z)!==void 0,compareIndex:Ne(Z),compareDisabled:I&&!!Y[0]&&Y[0].type!==Z.type,t:o},Z.setting_id)),de.filter(Z=>Z.type==="process").length===0&&a.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:o("profiles.cloudView.noProcessPresets")})]})]}),a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[a.jsx(Cr,{className:"w-4 h-4 text-purple-400"}),a.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:o("profiles.cloudView.columns.printer")}),a.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",de.filter(Z=>Z.type==="printer").length,")"]})]}),a.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[de.filter(Z=>Z.type==="printer").map(Z=>a.jsx($j,{setting:Z,onClick:()=>ce(Z),onDuplicate:()=>ie(Z),compareMode:I,isCompareSelected:Ne(Z)!==void 0,compareIndex:Ne(Z),compareDisabled:I&&!!Y[0]&&Y[0].type!==Z.type,t:o},Z.setting_id)),de.filter(Z=>Z.type==="printer").length===0&&a.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:o("profiles.cloudView.noPrinterPresets")})]})]})]}),M&&a.jsx(MZe,{setting:M,onClose:()=>T(null),onDeleted:()=>T(null),onDuplicate:()=>ie(M),onEdit:()=>Le(M),hasPermission:s,t:o}),(k||U||B||V)&&a.jsx(TZe,{onClose:()=>{j(!1),L(null),R(null),q(null)},initialData:B||U||(V?{type:V.type,name:"",base_id:"",setting:V.setting}:void 0),allPresets:t,t:o}),E&&a.jsx(AZe,{onClose:()=>P(!1),onApply:Z=>{q({type:Z.type,setting:Z.settings}),P(!1)},t:o}),H&&te&&Y[0]&&Y[1]&&a.jsx(Wae,{onClose:()=>{X(!1),J(null)},leftPreset:te[0],rightPreset:te[1],leftLabel:Y[0].name,rightLabel:Y[1].name,t:o})]})}function PZe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r}=Ir(),[i,s]=w.useState("cloud"),[o,l]=w.useState(),{data:c,isLoading:u}=tt({queryKey:["cloudStatus"],queryFn:se.getCloudStatus}),{data:d=[]}=tt({queryKey:["printers"],queryFn:se.getPrinters}),{data:h,isLoading:f,refetch:p,dataUpdatedAt:x}=tt({queryKey:["cloudSettings"],queryFn:()=>se.getCloudSettings(),enabled:!!c?.is_authenticated,retry:!1,staleTime:1e3*60*5});w.useEffect(()=>{x&&l(new Date(x))},[x]);const v=lt({mutationFn:se.cloudLogout,onSuccess:()=>{e.invalidateQueries({queryKey:["cloudStatus"]}),e.removeQueries({queryKey:["cloudSettings"]}),n(t("profiles.toast.loggedOut"))}}),b=()=>{e.invalidateQueries({queryKey:["cloudStatus"]})};return u?a.jsx("div",{className:"p-4 md:p-8 flex items-center justify-center min-h-[400px]",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):a.jsxs("div",{className:"p-6 lg:p-8",children:[a.jsxs("div",{className:"mb-6",children:[a.jsx("h1",{className:"text-2xl font-bold text-white",children:t("profiles.title")}),a.jsx("p",{className:"text-bambu-gray",children:t("profiles.subtitle")})]}),a.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary mb-6",children:[a.jsxs("button",{onClick:()=>s("cloud"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${i==="cloud"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(Gb,{className:"w-4 h-4"}),t("profiles.tabs.cloud")]}),a.jsxs("button",{onClick:()=>s("local"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${i==="local"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(qb,{className:"w-4 h-4"}),t("profiles.tabs.local")]}),a.jsxs("button",{onClick:()=>s("kprofiles"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${i==="kprofiles"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(nC,{className:"w-4 h-4"}),t("profiles.tabs.kprofiles")]})]}),i==="cloud"&&a.jsxs(a.Fragment,{children:[c?.is_authenticated&&a.jsxs("div",{className:"flex items-center justify-between p-3 mb-6 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green animate-pulse"}),a.jsxs("span",{className:"text-sm text-bambu-gray",children:[t("profiles.connectedAs")," ",a.jsx("span",{className:"text-white",children:c.email})]})]}),a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>v.mutate(),disabled:v.isPending||!r("cloud:auth"),title:r("cloud:auth")?void 0:t("profiles.noLogoutPermission"),children:[a.jsx(b3,{className:"w-4 h-4"}),t("profiles.logout")]})]}),c?.is_authenticated?f?a.jsx("div",{className:"flex items-center justify-center py-16",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):h?a.jsx(EZe,{settings:h,lastSyncTime:o,onRefresh:()=>p(),isRefreshing:f,printers:d,hasPermission:r,t}):a.jsxs("div",{className:"text-center py-16",children:[a.jsx("p",{className:"text-bambu-gray mb-4",children:t("profiles.failedToLoad")}),a.jsx(Me,{onClick:()=>p(),children:t("profiles.retry")})]}):a.jsx(kZe,{onSuccess:b,t})]}),i==="local"&&a.jsx(SZe,{}),i==="kprofiles"&&a.jsx(yZe,{}),a.jsx(CZe,{})]})}const Ky={Droplet:Jk,Flame:Xhe,Ruler:Mfe,Sparkles:pb,Square:gl,Cable:Rde,Wrench:Rf,Calendar:ii,Timer:Ou,Cog:xhe,Fan:dX,Zap:Gh,Wind:wX,Thermometer:cC,Layers:rs,Box:Qi,Target:yX,RefreshCw:kr,Settings:lC,Filter:tC,CircleDot:the};function x0(t){return t&&Ky[t]||Rf}function Dg(t,e,n){if(e==="days"){if(t<1)return n?n("common.today"):"Today";if(t===1)return n?n("maintenance.day"):"1 day";if(t<7){const i=Math.round(t);return n?n("maintenance.days",{count:i}):`${i} days`}if(t<180){const i=Math.round(t/7);return i===1?n?n("maintenance.week"):"1 week":n?n("maintenance.weeks",{count:i}):`${i} weeks`}const r=Math.round(t/30);return r===1?n?n("maintenance.month"):"1 month":n?n("maintenance.months",{count:r}):`${r} months`}else{if(t<1)return`${Math.round(t*60)}m`;if(t<24)return`${t<10?t.toFixed(1):Math.round(t)}h`;const r=t/24;if(r<7)return`${r<2?r.toFixed(1):Math.round(r)}d`;const i=r/7;return i<12?`${i<2?i.toFixed(1):Math.round(i)}w`:`${Math.round(i/4)}mo`}}function Vj(t,e,n){return e==="days"?t===1?n?n("maintenance.day"):"1 day":t===7?n?n("maintenance.week"):"1 week":t===14?n?n("maintenance.weeks",{count:2}):"2 weeks":t===30?n?n("maintenance.month"):"1 month":t===60?n?n("maintenance.months",{count:2}):"2 months":t===90?n?n("maintenance.months",{count:3}):"3 months":t===180?n?n("maintenance.months",{count:6}):"6 months":t===365?n?n("maintenance.year"):"1 year":n?n("maintenance.days",{count:t}):`${t} days`:`${t}h`}function jZe(t,e){const n=(e||"").toUpperCase().replace(/[- ]/g,""),r=n.includes("X1"),i=n.includes("P1"),s=n.includes("A1MINI"),o=n.includes("A1")&&!s,l=n.includes("H2D"),c=n.includes("H2C"),u=n.includes("H2S"),d=l||c||u,h=n.includes("P2S");switch(t){case"Lubricate Carbon Rods":return r?"https://wiki.bambulab.com/en/x1/maintenance/basic-maintenance":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":h?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":null;case"Lubricate Linear Rails":return s?"https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis":o?"https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis":d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;case"Clean Nozzle/Hotend":return r||i?"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog":s||o?"https://wiki.bambulab.com/en/a1-mini/troubleshooting/nozzle-clog":d?"https://wiki.bambulab.com/en/h2/maintenance/nozzl-cold-pull-maintenance-and-cleaning":h?"https://wiki.bambulab.com/en/p2s/maintenance/cold-pull-maintenance-hotend":"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog";case"Check Belt Tension":return r?"https://wiki.bambulab.com/en/x1/maintenance/belt-tension":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":s?"https://wiki.bambulab.com/en/a1-mini/maintenance/belt_tension":o?"https://wiki.bambulab.com/en/a1/maintenance/belt_tension":l?"https://wiki.bambulab.com/en/h2/maintenance/belt-tension":c?"https://wiki.bambulab.com/en/h2c/maintenance/belt-tension":u?"https://wiki.bambulab.com/en/h2s/maintenance/belt-tension":h?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":"https://wiki.bambulab.com/en/x1/maintenance/belt-tension";case"Clean Carbon Rods":return r||i||h?"https://wiki.bambulab.com/en/general/carbon-rods-clearance":null;case"Clean Linear Rails":return s?"https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis":o?"https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis":d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;case"Clean Build Plate":return"https://wiki.bambulab.com/en/filament-acc/acc/pei-plate-clean-guide";case"Check PTFE Tube":return r||i?"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube":s||o?"https://wiki.bambulab.com/en/a1-mini/maintenance/ptfe-tube":l?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":u?"https://wiki.bambulab.com/en/h2s/maintenance/replace-ptfe-tube-on-h2s-printer":c?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube";case"Replace HEPA Filter":case"HEPA Filter":case"Replace Carbon Filter":case"Carbon Filter":return d?"https://wiki.bambulab.com/en/h2/maintenance/replace-smoke-purifier-air-filte":"https://wiki.bambulab.com/en/x1/maintenance/replace-carbon-filter";case"Lubricate Left Nozzle Rail":case"Left Nozzle Rail":return d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;default:return null}}function DZe({item:t,onPerform:e,onToggle:n,hasPermission:r,t:i}){const s=x0(t.maintenance_type_icon),o=t.interval_type||"hours",c=(()=>{if(o==="days"){const p=t.days_since_maintenance??0;return Math.max(0,Math.min(100,p/t.interval_hours*100))}return Math.max(0,Math.min(100,(t.interval_hours-t.hours_until_due)/t.interval_hours*100))})(),u=()=>t.enabled?t.is_due?"text-red-400":t.is_warning?"text-amber-400":"text-bambu-green":"text-bambu-gray",d=()=>t.enabled?t.is_due?"bg-red-500":t.is_warning?"bg-amber-500":"bg-bambu-green":"bg-bambu-gray/30",h=()=>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",f=()=>{if(!t.enabled)return i("common.disabled");if(o==="days"){const p=t.days_until_due??0;return t.is_due?i("maintenance.overdueBy",{duration:Dg(Math.abs(p),"days",i)}):t.is_warning?i("maintenance.dueIn",{duration:Dg(p,"days",i)}):i("maintenance.timeLeft",{duration:Dg(p,"days",i)})}else return t.is_due?i("maintenance.overdueBy",{duration:Dg(Math.abs(t.hours_until_due),"hours",i)}):t.is_warning?i("maintenance.dueIn",{duration:Dg(t.hours_until_due,"hours",i)}):i("maintenance.timeLeft",{duration:Dg(t.hours_until_due,"hours",i)})};return a.jsx("div",{className:`rounded-xl border p-4 transition-all ${h()}`,children:a.jsxs("div",{className:"flex items-start gap-3 max-[550px]:flex-wrap",children:[a.jsxs("div",{className:`relative p-2.5 rounded-lg shrink-0 ${t.is_due?"bg-red-500/20":t.is_warning?"bg-amber-500/20":t.enabled?"bg-bambu-dark":"bg-bambu-dark/50"}`,children:[a.jsx(s,{className:`w-5 h-5 ${u()}`}),t.enabled&&(t.is_due||t.is_warning)&&a.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`})]}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("h3",{className:`font-medium truncate ${t.enabled?"text-white":"text-bambu-gray"}`,children:t.maintenance_type_name}),o==="days"&&a.jsx("span",{title:i("maintenance.timeBasedInterval"),children:a.jsx(ii,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"})}),(()=>{const p=t.maintenance_type_wiki_url||jZe(t.maintenance_type_name,t.printer_model);return p?a.jsx("a",{href:p,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-bambu-green transition-colors shrink-0",title:i("maintenance.viewDocumentation"),onClick:x=>x.stopPropagation(),children:a.jsx(Kr,{className:"w-3.5 h-3.5"})}):null})()]}),a.jsx("div",{className:"mt-2 mb-1.5",children:a.jsx("div",{className:"w-full h-1.5 bg-bambu-dark rounded-full overflow-hidden",children:a.jsx("div",{className:`h-full rounded-full transition-all duration-500 ${d()}`,style:{width:`${c}%`}})})}),a.jsxs("div",{className:`text-xs flex items-center gap-1 ${u()}`,children:[t.is_due&&a.jsx(Cn,{className:"w-3 h-3"}),t.is_warning&&!t.is_due&&a.jsx(Qn,{className:"w-3 h-3"}),!t.is_due&&!t.is_warning&&t.enabled&&a.jsx(Xr,{className:"w-3 h-3"}),f()]})]}),a.jsxs("div",{className:"flex items-center gap-2 shrink-0 max-[550px]:w-full max-[550px]:justify-end max-[550px]:mt-1",children:[a.jsx("span",{title:r("maintenance:update")?void 0:i("maintenance.noPermissionUpdate"),children:a.jsx(zn,{checked:t.enabled,onChange:p=>n(t.id,p),disabled:!r("maintenance:update")})}),a.jsxs(Me,{size:"sm",variant:t.is_due?"primary":"secondary",onClick:()=>e(t.id),disabled:!t.enabled||!r("maintenance:update"),title:r("maintenance:update")?void 0:i("maintenance.noPermissionPerform"),className:"!px-3",children:[a.jsx(Do,{className:"w-3.5 h-3.5"}),i("common.reset")]})]})]})})}function RZe({overview:t,onPerform:e,onToggle:n,onSetHours:r,hasPermission:i,t:s}){const[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,h]=w.useState(t.total_print_hours.toFixed(1)),f=[...t.maintenance_items].sort((v,b)=>v.is_due&&!b.is_due?-1:!v.is_due&&b.is_due?1:v.is_warning&&!b.is_warning?-1:!v.is_warning&&b.is_warning?1:v.maintenance_type_id-b.maintenance_type_id),p=f.find(v=>v.enabled&&(v.is_due||v.is_warning)),x=()=>{const v=parseFloat(d);!isNaN(v)&&v>=0&&(r(t.printer_id,v),u(!1))};return a.jsxs(_t,{className:"overflow-hidden",children:[a.jsxs("div",{className:"p-5",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex items-center gap-4",children:[a.jsx("h2",{className:"text-xl font-semibold text-white",children:t.printer_name}),a.jsxs("div",{className:"flex items-center gap-2",children:[t.due_count>0&&a.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:[a.jsx(Cn,{className:"w-3 h-3"}),s("maintenance.overdueCount",{count:t.due_count})]}),t.warning_count>0&&a.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:[a.jsx(Qn,{className:"w-3 h-3"}),s("maintenance.dueSoonCount",{count:t.warning_count})]}),t.due_count===0&&t.warning_count===0&&a.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:[a.jsx(Xr,{className:"w-3 h-3"}),s("maintenance.allGood")]})]})]}),a.jsxs("button",{onClick:()=>l(!o),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:[o?a.jsx(ql,{className:"w-4 h-4"}):a.jsx(nr,{className:"w-4 h-4"}),s(o?"common.collapse":"common.expand")]})]}),a.jsxs("div",{className:"flex items-center gap-6 mt-4",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2 bg-bambu-dark/50 rounded-lg",children:a.jsx(Ou,{className:"w-4 h-4 text-bambu-gray"})}),c?a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("input",{type:"number",value:d,onChange:v=>h(v.target.value),onKeyDown:v=>{v.key==="Enter"&&x(),v.key==="Escape"&&u(!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}),a.jsx("span",{className:"text-xs text-bambu-gray",children:s("common.hours")}),a.jsx(Me,{size:"sm",onClick:x,children:s("common.save")}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>u(!1),children:s("common.cancel")})]}):a.jsxs("button",{onClick:()=>{i("maintenance:update")&&(h(Math.round(t.total_print_hours).toString()),u(!0))},className:`group ${i("maintenance:update")?"":"cursor-not-allowed opacity-60"}`,title:i("maintenance:update")?void 0:s("maintenance.noPermissionEditHours"),children:[a.jsxs("div",{className:`text-sm font-medium text-white ${i("maintenance:update")?"group-hover:text-bambu-green":""} transition-colors flex items-center gap-1`,children:[Math.round(t.total_print_hours)," ",s("common.hours"),a.jsx(Pf,{className:`w-3 h-3 text-bambu-gray ${i("maintenance:update")?"group-hover:text-bambu-green":""}`})]}),a.jsx("div",{className:"text-xs text-bambu-gray",children:s("maintenance.totalPrintTime")})]})]}),a.jsx("div",{className:"w-px h-10 bg-bambu-dark-tertiary"}),p&&a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:`p-2 rounded-lg ${p.is_due?"bg-red-500/20":"bg-amber-500/20"}`,children:(()=>{const v=x0(p.maintenance_type_icon);return a.jsx(v,{className:`w-4 h-4 ${p.is_due?"text-red-400":"text-amber-400"}`})})()}),a.jsxs("div",{children:[a.jsx("div",{className:`text-sm font-medium ${p.is_due?"text-red-400":"text-amber-400"}`,children:p.maintenance_type_name}),a.jsx("div",{className:`text-xs ${p.is_due?"text-red-400/70":"text-amber-400/70"}`,children:p.is_due?s("common.overdue"):s("maintenance.dueSoon")})]})]})]})]}),o&&a.jsx(Nt,{className:"pt-0 border-t border-bambu-dark-tertiary",children:a.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-3 pt-4",children:f.map(v=>a.jsx(DZe,{item:v,onPerform:e,onToggle:n,hasPermission:i,t:s},v.id))})})]})}function FZe({overview:t,types:e,onUpdateInterval:n,onAddType:r,onUpdateType:i,onDeleteType:s,onRestoreDefaults:o,isRestoringDefaults:l,onAssignType:c,onRemoveItem:u,hasPermission:d,t:h}){const[f,p]=w.useState(null),[x,v]=w.useState(""),[b,y]=w.useState("hours"),[S,N]=w.useState(!1),[A,M]=w.useState(""),[T,k]=w.useState("100"),[j,E]=w.useState("hours"),[P,U]=w.useState("Wrench"),[L,B]=w.useState(""),[R,V]=w.useState(new Set),[q,I]=w.useState(null),[K,Y]=w.useState(null),le=w.useMemo(()=>t?t.map(z=>({id:z.printer_id,name:z.printer_name})):[],[t]),H=z=>t?t.filter(D=>D.maintenance_items.some(G=>G.maintenance_type_id===z)).map(D=>({printerId:D.printer_id,printerName:D.printer_name,itemId:D.maintenance_items.find(G=>G.maintenance_type_id===z)?.id})):[],X=z=>{if(!t)return[];const D=new Set(H(z).map(G=>G.printerId));return le.filter(G=>!D.has(G.id))},[te,J]=w.useState(null),[Se,fe]=w.useState(""),[me,De]=w.useState(""),[de,ce]=w.useState("hours"),[Ne,ie]=w.useState("Wrench"),[Le,$]=w.useState(""),W=z=>{J(z),fe(z.name),De(z.default_interval_hours.toString()),ce(z.interval_type||"hours"),ie(z.icon||"Wrench"),$(z.wiki_url||"")},ue=()=>{te&&Se.trim()&&parseFloat(me)>0&&(i(te.id,{name:Se.trim(),default_interval_hours:parseFloat(me),interval_type:de,icon:Ne,wiki_url:Le.trim()||null}),J(null))},Z=(z,D,G)=>{const pe=parseFloat(x);if(!isNaN(pe)&&pe>0){const ve=Math.abs(pe-D)<.01?null:pe;n(z,{custom_interval_hours:ve,custom_interval_type:b!==G?b:null})}p(null)},oe=z=>{z.preventDefault(),A.trim()&&parseFloat(T)>0&&R.size>0&&(r({name:A.trim(),default_interval_hours:parseFloat(T),interval_type:j,icon:P,wiki_url:L.trim()||null},Array.from(R)),M(""),k("100"),E("hours"),B(""),V(new Set),N(!1))},Q=z=>{V(D=>{const G=new Set(D);return G.has(z)?G.delete(z):G.add(z),G})},he=t?.map(z=>({printerId:z.printer_id,printerName:z.printer_name,items:z.maintenance_items.sort((D,G)=>D.maintenance_type_id-G.maintenance_type_id)})).sort((z,D)=>z.printerName.localeCompare(D.printerName))||[],be=e.filter(z=>z.is_system),ee=e.filter(z=>!z.is_system);return a.jsxs("div",{className:"space-y-8",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-4",children:[a.jsxs("div",{children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:h("maintenance.maintenanceTypes")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:h("maintenance.maintenanceTypesDescription")})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:o,disabled:!d("maintenance:delete")||l,title:d("maintenance:delete")?void 0:h("maintenance.noPermissionDeleteTypes"),children:h("maintenance.restoreDefaults")}),a.jsxs(Me,{onClick:()=>N(!S),disabled:!d("maintenance:create"),title:d("maintenance:create")?void 0:h("maintenance.noPermissionEditTypes"),children:[a.jsx(ar,{className:"w-4 h-4"}),h("maintenance.addCustomType")]})]})]}),S&&a.jsx(_t,{className:"mb-6",children:a.jsx(Nt,{className:"py-4",children:a.jsxs("form",{onSubmit:oe,children:[a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[a.jsxs("div",{className:"lg:col-span-2",children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("common.name")}),a.jsx("input",{type:"text",value:A,onChange:z=>M(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:h("maintenance.exampleName"),autoFocus:!0})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.intervalType")}),a.jsxs("select",{value:j,onChange:z=>{E(z.target.value),z.target.value==="days"?k("30"):k("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:[a.jsx("option",{value:"hours",children:h("maintenance.printHours")}),a.jsx("option",{value:"days",children:h("maintenance.calendarDays")})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.intervalValue",{type:j==="days"?h("maintenance.calendarDays").toLowerCase():h("common.hours")})}),a.jsx("input",{type:"number",value:T,onChange:z=>k(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",min:"1"})]})]}),a.jsx("div",{className:"mt-4 flex items-end justify-between",children:a.jsxs("div",{children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.icon")}),a.jsx("div",{className:"flex gap-1",children:Object.keys(Ky).map(z=>{const D=Ky[z];return a.jsx("button",{type:"button",onClick:()=>U(z),className:`p-2 rounded-lg transition-colors ${P===z?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:a.jsx(D,{className:"w-4 h-4"})},z)})})]})}),a.jsxs("div",{className:"mt-4",children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.documentationLink")}),a.jsx("input",{type:"url",value:L,onChange:z=>B(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"https://wiki.bambulab.com/..."})]}),a.jsxs("div",{className:"mt-4",children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.assignToPrinters")}),a.jsx("div",{className:"flex flex-wrap gap-2",children:le.map(z=>a.jsx("button",{type:"button",onClick:()=>Q(z.id),className:`px-3 py-1.5 rounded-lg text-sm transition-colors ${R.has(z.id)?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:z.name},z.id))}),R.size===0&&a.jsx("p",{className:"text-xs text-orange-400 mt-1",children:h("maintenance.selectAtLeastOnePrinter")})]}),a.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:()=>{N(!1),V(new Set)},children:h("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!A.trim()||R.size===0,children:h("maintenance.addType")})]})]})})}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3",children:[be.map(z=>{const D=x0(z.icon),G=z.interval_type||"hours";return a.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-dark-tertiary",children:a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-dark rounded-lg",children:a.jsx(D,{className:"w-5 h-5 text-bambu-gray"})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("div",{className:"text-sm font-medium text-white truncate",children:z.name}),a.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[G==="days"?a.jsx(ii,{className:"w-3 h-3"}):a.jsx(Ou,{className:"w-3 h-3"}),Vj(z.default_interval_hours,G,h)]})]}),a.jsx("button",{onClick:()=>{d("maintenance:delete")&&Y(z)},disabled:!d("maintenance:delete"),title:d("maintenance:delete")?void 0:h("maintenance.noPermissionDeleteTypes"),className:`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors ${d("maintenance:delete")?"":"opacity-50 cursor-not-allowed"}`,children:a.jsx(en,{className:"w-4 h-4"})})]})},z.id)}),ee.map(z=>{const D=x0(z.icon),G=z.interval_type||"hours";if(te?.id===z.id)return a.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green",children:a.jsxs("div",{className:"space-y-3",children:[a.jsx("input",{type:"text",value:Se,onChange:_e=>fe(_e.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:h("common.name"),autoFocus:!0}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs("select",{value:de,onChange:_e=>ce(_e.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:[a.jsx("option",{value:"hours",children:h("maintenance.printHours")}),a.jsx("option",{value:"days",children:h("maintenance.calendarDays")})]}),a.jsx("input",{type:"number",value:me,onChange:_e=>De(_e.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"})]}),a.jsx("div",{className:"flex flex-wrap gap-1",children:Object.keys(Ky).map(_e=>{const He=Ky[_e];return a.jsx("button",{type:"button",onClick:()=>ie(_e),className:`p-1.5 rounded transition-colors ${Ne===_e?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:a.jsx(He,{className:"w-3.5 h-3.5"})},_e)})}),a.jsx("input",{type:"url",value:Le,onChange:_e=>$(_e.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:h("maintenance.documentationLink")}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{size:"sm",onClick:ue,disabled:!Se.trim(),children:h("common.save")}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>J(null),children:h("common.cancel")})]})]})},z.id);const ve=H(z.id),xe=X(z.id),ze=q===z.id;return a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green/30",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-green/20 rounded-lg",children:a.jsx(D,{className:"w-5 h-5 text-bambu-green"})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-sm font-medium text-white truncate",children:z.name}),a.jsx("span",{className:"px-1.5 py-0.5 bg-bambu-green/20 text-bambu-green text-[10px] font-medium rounded",children:h("maintenance.custom")})]}),a.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[G==="days"?a.jsx(ii,{className:"w-3 h-3"}):a.jsx(Ou,{className:"w-3 h-3"}),Vj(z.default_interval_hours,G,h)]})]}),a.jsxs("button",{onClick:()=>I(ze?null:z.id),className:`px-2 py-1 rounded-lg border transition-colors flex items-center gap-1 ${ve.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:h("maintenance.printersAssignedClick",{count:ve.length}),children:[a.jsx(Cr,{className:"w-3 h-3"}),a.jsx("span",{className:"text-xs font-medium",children:ve.length}),a.jsx(nr,{className:`w-3 h-3 transition-transform ${ze?"rotate-180":""}`})]}),a.jsx("button",{onClick:()=>W(z),disabled:!d("maintenance:update"),title:d("maintenance:update")?void 0:h("maintenance.noPermissionEditTypes"),className:`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors ${d("maintenance:update")?"":"opacity-50 cursor-not-allowed"}`,children:a.jsx(Pf,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>{confirm(h("maintenance.deleteTypeConfirm",{name:z.name}))&&s(z.id)},disabled:!d("maintenance:delete"),title:d("maintenance:delete")?void 0:h("maintenance.noPermissionDeleteTypes"),className:`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors ${d("maintenance:delete")?"":"opacity-50 cursor-not-allowed"}`,children:a.jsx(en,{className:"w-4 h-4"})})]}),ze&&a.jsxs("div",{className:"mt-3 pt-3 border-t border-bambu-dark-tertiary",children:[a.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:h("maintenance.assignedToPrinters")}),ve.length===0?a.jsx("p",{className:"text-xs text-orange-400",children:h("maintenance.noPrintersAssigned")}):a.jsx("div",{className:"flex flex-wrap gap-1 mb-2",children:ve.map(_e=>a.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark rounded text-xs text-white",children:[_e.printerName,a.jsx("button",{onClick:()=>_e.itemId&&u(_e.itemId),disabled:!d("maintenance:delete"),title:d("maintenance:delete")?h("maintenance.removeFromPrinter"):h("maintenance.noPermissionRemovePrinter"),className:`ml-1 ${d("maintenance:delete")?"hover:text-red-400":"opacity-50 cursor-not-allowed"}`,children:"×"})]},_e.printerId))}),xe.length>0&&a.jsxs("div",{className:"flex flex-wrap gap-1",children:[a.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:h("maintenance.addPrinterShort")}),xe.map(_e=>a.jsxs("button",{onClick:()=>c(_e.id,z.id),disabled:!d("maintenance:create"),title:d("maintenance:create")?void 0:h("maintenance.noPermissionAssignPrinter"),className:`px-2 py-1 bg-bambu-dark rounded text-xs transition-colors ${d("maintenance:create")?"hover:bg-bambu-green/20 text-bambu-gray hover:text-bambu-green":"opacity-50 cursor-not-allowed text-bambu-gray"}`,children:["+ ",_e.name]},_e.id))]})]})]},z.id)})]})]}),he.length>0&&a.jsxs("div",{children:[a.jsxs("div",{className:"mb-4",children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:h("maintenance.intervalOverrides")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:h("maintenance.intervalOverridesDescription")})]}),a.jsx("div",{className:"space-y-4",children:he.map(z=>a.jsx(_t,{children:a.jsxs(Nt,{className:"py-4",children:[a.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:z.printerName}),a.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2",children:z.items.map(D=>{const G=x0(D.maintenance_type_icon),pe=e.find(He=>He.id===D.maintenance_type_id),ve=pe?.default_interval_hours||D.interval_hours,xe=pe?.interval_type||"hours",ze=D.interval_type||"hours",_e=f===D.id;return a.jsxs("div",{className:"flex items-center gap-2 p-2.5 bg-bambu-dark rounded-lg",children:[a.jsx(G,{className:"w-4 h-4 text-bambu-gray shrink-0"}),a.jsx("span",{className:"text-xs text-bambu-gray flex-1 truncate",children:D.maintenance_type_name}),_e?a.jsxs("div",{className:"flex items-center gap-1",children:[b==="days"?a.jsx(ii,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}):a.jsx(Ou,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}),a.jsxs("select",{value:b,onChange:He=>y(He.target.value),className:"px-1.5 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",children:[a.jsx("option",{value:"hours",children:h("maintenance.printHours")}),a.jsx("option",{value:"days",children:h("maintenance.calendarDays")})]}),a.jsx("input",{type:"number",value:x,onChange:He=>v(He.target.value),onKeyDown:He=>{He.key==="Enter"&&Z(D.id,ve,xe),He.key==="Escape"&&p(null)},className:"w-16 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",min:"1"}),a.jsx(Me,{size:"sm",onClick:()=>Z(D.id,ve,xe),children:"OK"})]}):a.jsxs("button",{onClick:()=>{d("maintenance:update")&&(p(D.id),v(D.interval_hours.toString()),y(ze))},disabled:!d("maintenance:update"),title:d("maintenance:update")?void 0:h("maintenance.noPermissionEditIntervals"),className:`px-2 py-1 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-xs font-medium text-white transition-colors flex items-center gap-1 ${d("maintenance:update")?"hover:bg-bambu-dark-secondary hover:border-bambu-green":"opacity-50 cursor-not-allowed"}`,children:[ze==="days"?a.jsx(ii,{className:"w-3 h-3"}):a.jsx(Ou,{className:"w-3 h-3"}),Vj(D.interval_hours,ze,h),a.jsx(Pf,{className:"w-3 h-3 text-bambu-gray"})]})]},D.id)})})]})},z.printerId))})]}),he.length===0&&a.jsx(_t,{children:a.jsxs(Nt,{className:"text-center py-12",children:[a.jsx(Qn,{className:"w-12 h-12 mx-auto mb-4 text-bambu-gray/30"}),a.jsx("p",{className:"text-bambu-gray",children:h("common.noPrinters")}),a.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1",children:h("maintenance.intervalOverridesDescription")})]})}),K&&a.jsx(kn,{title:h("maintenance.deleteSystemTypeTitle"),message:h("maintenance.deleteSystemTypeMessage",{name:K.name}),confirmText:h("common.delete"),cancelText:h("common.cancel"),variant:"danger",cancelVariant:"primary",cardClassName:"bg-red-950/70 border border-red-800/70",onConfirm:()=>{s(K.id),Y(null)},onCancel:()=>Y(null)})]})}function LZe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r}=Ir(),[i,s]=w.useState("status"),{data:o,isLoading:l}=tt({queryKey:["maintenanceOverview"],queryFn:se.getMaintenanceOverview}),{data:c}=tt({queryKey:["maintenanceTypes"],queryFn:se.getMaintenanceTypes}),u=lt({mutationFn:({id:T,notes:k})=>se.performMaintenance(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),e.invalidateQueries({queryKey:["maintenanceSummary"]}),n(t("maintenance.maintenanceComplete"))},onError:T=>{n(T.message,"error")}}),d=lt({mutationFn:({id:T,data:k})=>se.updateMaintenanceItem(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]})},onError:T=>{n(T.message,"error")}}),h=lt({mutationFn:({id:T,data:k})=>se.updateMaintenanceType(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.typeUpdated"))},onError:T=>{n(T.message,"error")}}),f=lt({mutationFn:se.deleteMaintenanceType,onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.typeDeleted"))},onError:T=>{n(T.message,"error")}}),p=lt({mutationFn:se.restoreDefaultMaintenanceTypes,onSuccess:T=>{e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.defaultsRestored",{count:T.restored}))},onError:T=>{n(T.message,"error")}}),x=lt({mutationFn:({printerId:T,hours:k})=>se.setPrinterHours(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),e.invalidateQueries({queryKey:["maintenanceSummary"]}),n(t("maintenance.printHoursUpdated"))},onError:T=>{n(T.message,"error")}}),v=lt({mutationFn:({printerId:T,typeId:k})=>se.assignMaintenanceType(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.printerAssigned"))},onError:T=>{n(T.message,"error")}}),b=lt({mutationFn:se.removeMaintenanceItem,onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.printerRemoved"))},onError:T=>{n(T.message,"error")}}),y=T=>{u.mutate({id:T})},S=(T,k)=>{d.mutate({id:T,data:{enabled:k}})},N=(T,k)=>{x.mutate({printerId:T,hours:k})};if(l)return a.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})});const A=o?.reduce((T,k)=>T+k.due_count,0)||0,M=o?.reduce((T,k)=>T+k.warning_count,0)||0;return a.jsxs("div",{className:"p-4 md:p-8",children:[a.jsxs("div",{className:"mb-6",children:[a.jsx("h1",{className:"text-2xl font-bold text-white",children:t("maintenance.title")}),a.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:i==="status"?a.jsxs(a.Fragment,{children:[A>0&&a.jsx("span",{className:"text-red-400",children:t("maintenance.dueCount",{count:A})}),A>0&&M>0&&" · ",M>0&&a.jsx("span",{className:"text-amber-400",children:t("maintenance.warningCount",{count:M})}),A===0&&M===0&&a.jsx("span",{className:"text-bambu-green",children:t("maintenance.allOk")})]}):t("maintenance.configureSettings")})]}),a.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[a.jsx("button",{onClick:()=>s("status"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${i==="status"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:t("maintenance.statusTab")}),a.jsx("button",{onClick:()=>s("settings"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${i==="settings"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:t("maintenance.settingsTab")})]}),i==="status"?a.jsx("div",{className:"space-y-6",children:o&&o.length>0?[...o].sort((T,k)=>{const j=T.due_count*10+T.warning_count,E=k.due_count*10+k.warning_count;return j!==E?E-j:T.printer_name.localeCompare(k.printer_name)}).map(T=>a.jsx(RZe,{overview:T,onPerform:y,onToggle:S,onSetHours:N,hasPermission:r,t},T.printer_id)):a.jsx(_t,{children:a.jsxs(Nt,{className:"text-center py-16",children:[a.jsx(Rf,{className:"w-16 h-16 mx-auto mb-4 text-bambu-gray/30"}),a.jsx("p",{className:"text-lg font-medium text-white mb-2",children:t("common.noPrinters")}),a.jsx("p",{className:"text-bambu-gray",children:t("maintenance.configureSettings")})]})})}):a.jsx(FZe,{overview:o,types:c||[],onUpdateInterval:(T,k)=>d.mutate({id:T,data:k}),onAddType:async(T,k)=>{const j=await se.createMaintenanceType(T);for(const E of k)await se.assignMaintenanceType(E,j.id);e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.typeUpdated"))},onUpdateType:(T,k)=>h.mutate({id:T,data:k}),onDeleteType:T=>f.mutate(T),onRestoreDefaults:()=>p.mutate(),isRestoringDefaults:p.isPending,onAssignType:(T,k)=>v.mutate({printerId:T,typeId:k}),onRemoveItem:T=>b.mutate(T),hasPermission:r,t})]})}const cK=["#ef4444","#f97316","#eab308","#22c55e","#06b6d4","#3b82f6","#8b5cf6","#ec4899","#6b7280"];function Kae({project:t,onClose:e,onSave:n,isLoading:r,t:i}){const[s,o]=w.useState(t?.name||""),[l,c]=w.useState(t?.description||""),[u,d]=w.useState(t?.color||cK[0]),[h,f]=w.useState(t?.target_count?.toString()||""),[p,x]=w.useState(t?.target_parts_count?.toString()||""),[v,b]=w.useState(t?.status||"active"),[y,S]=w.useState(t?.tags||""),[N,A]=w.useState(t?.due_date?.split("T")[0]||""),[M,T]=w.useState(t?.priority||"normal"),k=j=>{j.preventDefault(),n({name:s.trim(),description:l.trim()||void 0,color:u,target_count:h?parseInt(h,10):void 0,target_parts_count:p?parseInt(p,10):void 0,tags:y.trim()||void 0,due_date:N||void 0,priority:M,...t&&{status:v}})};return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:i(t?"projects.editProject":"projects.newProject")})}),a.jsxs("form",{onSubmit:k,className:"p-4 space-y-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("common.name")}),a.jsx("input",{type:"text",value:s,onChange:j=>o(j.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:i("projects.namePlaceholder"),required:!0})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("common.description")}),a.jsx("textarea",{value:l,onChange:j=>c(j.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:i("projects.descriptionPlaceholder"),rows:2})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.color")}),a.jsx("div",{className:"flex gap-2 flex-wrap",children:cK.map(j=>a.jsx("button",{type:"button",onClick:()=>d(j),className:`w-8 h-8 rounded-full transition-transform ${u===j?"ring-2 ring-white ring-offset-2 ring-offset-bambu-dark-secondary scale-110":""}`,style:{backgroundColor:j}},j))})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.targetPlates")}),a.jsx("input",{type:"number",value:h,onChange:j=>f(j.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:i("projects.targetPlatesPlaceholder"),min:"1"}),a.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("projects.targetPlatesHelp")})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.targetParts")}),a.jsx("input",{type:"number",value:p,onChange:j=>x(j.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:i("projects.targetPartsPlaceholder"),min:"1"}),a.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("projects.targetPartsHelp")})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.tagsLabel")}),a.jsx("input",{type:"text",value:y,onChange:j=>S(j.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:i("projects.tagsPlaceholder")})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.dueDate")}),a.jsx("input",{type:"date",value:N,onChange:j=>A(j.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"})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.priority")}),a.jsxs("select",{value:M,onChange:j=>T(j.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:[a.jsx("option",{value:"low",children:i("projects.priorityLow")}),a.jsx("option",{value:"normal",children:i("projects.priorityNormal")}),a.jsx("option",{value:"high",children:i("projects.priorityHigh")}),a.jsx("option",{value:"urgent",children:i("projects.priorityUrgent")})]})]})]}),t&&a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("common.status")}),a.jsxs("select",{value:v,onChange:j=>b(j.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:[a.jsx("option",{value:"active",children:i("projects.statusActive")}),a.jsx("option",{value:"completed",children:i("projects.statusCompleted")}),a.jsx("option",{value:"archived",children:i("projects.statusArchived")})]})]}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!s.trim()||r,children:r?a.jsx(at,{className:"w-4 h-4 animate-spin"}):i(t?"common.save":"projects.create")})]})]})]})})}function OZe({project:t,onClick:e,onEdit:n,onDelete:r,hasPermission:i,t:s}){const o=t.target_count?Math.round(t.archive_count/t.target_count*100):0,l=t.target_parts_count?Math.round(t.completed_count/t.target_parts_count*100):0,c=t.status==="completed",u=t.status==="archived",[d,h]=w.useState(!1),p=c?{icon:fb,color:"text-bambu-green",bg:"bg-bambu-green/10"}:u?{icon:Rs,color:"text-bambu-gray",bg:"bg-bambu-gray/10"}:t.queue_count>0?{icon:Qn,color:"text-blue-400",bg:"bg-blue-400/10"}:{icon:xs,color:"text-bambu-gray",bg:"bg-bambu-gray/10"};return a.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:[a.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`}}),a.jsxs("div",{className:"p-5 pl-6",children:[a.jsxs("div",{className:"flex items-start justify-between mb-4",children:[a.jsxs("div",{className:"flex items-center gap-3 min-w-0 flex-1",children:[a.jsx("div",{className:`p-2 rounded-lg ${p.bg} flex-shrink-0`,children:a.jsx(p.icon,{className:`w-5 h-5 ${p.color}`})}),a.jsxs("div",{className:"min-w-0 flex-1",children:[a.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[a.jsx("h3",{className:"font-semibold text-white truncate",children:t.name}),t.target_parts_count?a.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${l>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.completed_count,"/",t.target_parts_count," ",s("projects.parts")]}):t.target_count?a.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${o>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.archive_count,"/",t.target_count," ",s("projects.plates")]}):t.completed_count>0?a.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium bg-bambu-dark text-bambu-gray",children:[t.completed_count," ",s("projects.parts")]}):null,c&&a.jsx("span",{className:"text-xs bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full whitespace-nowrap",children:s("projects.done")}),u&&a.jsx("span",{className:"text-xs bg-bambu-gray/20 text-bambu-gray px-2 py-0.5 rounded-full whitespace-nowrap",children:s("projects.statusArchived")})]}),t.description&&a.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1 line-clamp-1",children:t.description}),t.archives&&t.archives.length>0&&(()=>{const x=t.archives.map(S=>S.filament_type).filter(Boolean).flatMap(S=>S.split(",").map(N=>N.trim())).filter(Boolean),v=[...new Set(x)],b=t.archives.map(S=>S.filament_color).filter(Boolean).flatMap(S=>S.split(",").map(N=>N.trim())).filter(S=>S.startsWith("#")||/^[0-9A-Fa-f]{6}$/.test(S)),y=[...new Set(b)];return v.length===0&&y.length===0?null:a.jsxs("div",{className:"flex items-center gap-2 mt-1.5",children:[v.slice(0,3).map(S=>a.jsx("span",{className:"text-[10px] px-1.5 py-0.5 bg-bambu-dark text-bambu-gray rounded",children:S},S)),y.length>0&&a.jsxs("div",{className:"flex items-center gap-0.5",children:[y.slice(0,5).map(S=>a.jsx("div",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:S.startsWith("#")?S:`#${S}`},title:S},S)),y.length>5&&a.jsxs("span",{className:"text-[10px] text-bambu-gray ml-0.5",children:["+",y.length-5]})]})]})})()]})]}),a.jsxs("div",{className:"relative",onClick:x=>x.stopPropagation(),children:[a.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:()=>h(!d),children:a.jsx(Ah,{className:"w-4 h-4"})}),d&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>h(!1)}),a.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:[a.jsxs("button",{className:`w-full px-3 py-2 text-left text-sm flex items-center gap-2 ${i("projects:update")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{i("projects:update")&&(n(),h(!1))},disabled:!i("projects:update"),title:i("projects:update")?void 0:s("projects.noEditPermission"),children:[a.jsx(Pf,{className:"w-4 h-4"}),s("common.edit")]}),a.jsxs("button",{className:`w-full px-3 py-2 text-left text-sm flex items-center gap-2 ${i("projects:delete")?"text-red-400 hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{i("projects:delete")&&(r(),h(!1))},disabled:!i("projects:delete"),title:i("projects:delete")?void 0:s("projects.noDeletePermission"),children:[a.jsx(en,{className:"w-4 h-4"}),s("common.delete")]})]})]})]})]}),a.jsx("div",{className:"mb-4",children:t.target_count||t.target_parts_count?a.jsxs("div",{className:"space-y-3",children:[t.target_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[a.jsx("span",{className:"text-bambu-gray",children:s("projects.plates")}),a.jsxs("span",{className:o>=100?"text-bambu-green font-medium":"text-white",children:[t.archive_count," / ",t.target_count]})]}),a.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:a.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(o,100)}%`,background:o>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${o>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.target_parts_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[a.jsx("span",{className:"text-bambu-gray",children:s("projects.parts")}),a.jsxs("span",{className:l>=100?"text-bambu-green font-medium":"text-white",children:[t.completed_count," / ",t.target_parts_count]})]}),a.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:a.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(l,100)}%`,background:l>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${l>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.failed_count>0&&a.jsxs("div",{className:"text-xs text-red-400",children:[t.failed_count," ",s("projects.failed")]})]}):t.completed_count>0||t.failed_count>0?a.jsxs("div",{className:"flex items-center gap-4 text-xs",children:[t.completed_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-bambu-gray",children:[a.jsx(Rs,{className:"w-3.5 h-3.5"}),a.jsxs("span",{children:[t.completed_count," ",s("projects.completed")]})]}),t.failed_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",children:[a.jsx(Cn,{className:"w-3.5 h-3.5"}),a.jsxs("span",{children:[t.failed_count," ",s("projects.failed")]})]}),t.queue_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",children:[a.jsx(Qn,{className:"w-3.5 h-3.5"}),a.jsxs("span",{children:[t.queue_count," ",s("projects.inQueue")]})]})]}):a.jsx("div",{className:"text-xs text-bambu-gray/60 italic",children:s("projects.noPrintsYet")})}),t.archives&&t.archives.length>0&&a.jsxs("div",{className:"mb-4",children:[a.jsx("div",{className:"grid grid-cols-4 gap-1.5",children:t.archives.slice(0,4).map(x=>a.jsxs("div",{className:"relative aspect-square rounded-lg bg-bambu-dark overflow-hidden border border-bambu-dark-tertiary",title:x.print_name||"Unknown",children:[x.thumbnail_path?a.jsx("img",{src:se.getArchiveThumbnail(x.id),alt:x.print_name||"",className:"w-full h-full object-cover"}):a.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray/50",children:a.jsx(ma,{className:"w-6 h-6"})}),x.status==="failed"&&a.jsx("div",{className:"absolute inset-0 bg-red-500/40 flex items-center justify-center",children:a.jsx(Cn,{className:"w-4 h-4 text-white"})})]},x.id))}),t.archive_count>4&&a.jsx("p",{className:"text-xs text-bambu-gray mt-1.5 text-center",children:s("common.more",{count:t.archive_count-4})})]}),a.jsxs("div",{className:"flex items-center justify-between pt-3 border-t border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-4 text-xs text-bambu-gray",children:[a.jsxs("div",{className:"flex items-center gap-1.5",title:s("projects.printJobs"),children:[a.jsx(rs,{className:"w-3.5 h-3.5 text-blue-400"}),a.jsxs("span",{children:[t.archive_count," ",s("projects.plates")]})]}),a.jsxs("div",{className:"flex items-center gap-1.5",title:s("projects.partsPrinted"),children:[a.jsx(ma,{className:"w-3.5 h-3.5 text-bambu-green"}),a.jsxs("span",{children:[t.completed_count," ",s("projects.parts")]})]}),t.failed_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",title:s("projects.failedParts"),children:[a.jsx(Cn,{className:"w-3.5 h-3.5"}),a.jsx("span",{children:t.failed_count})]}),t.queue_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-yellow-400",title:s("projects.inQueue"),children:[a.jsx(g3,{className:"w-3.5 h-3.5"}),a.jsx("span",{children:t.queue_count})]})]}),a.jsx(fa,{className:"w-4 h-4 text-bambu-gray/50 group-hover:text-bambu-gray transition-colors"})]})]})]})}function IZe(){const{t}=Vt(),e=Hc(),n=tn(),{showToast:r}=mn(),{hasPermission:i}=Ir(),[s,o]=w.useState(!1),[l,c]=w.useState(),[u,d]=w.useState("active"),[h,f]=w.useState(null),{data:p,isLoading:x}=tt({queryKey:["projects",u==="all"?void 0:u],queryFn:()=>se.getProjects(u==="all"?void 0:u)}),v=lt({mutationFn:B=>se.createProject(B),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),o(!1),r(t("projects.toast.created"),"success")},onError:B=>{r(B.message,"error")}}),b=lt({mutationFn:({id:B,data:R})=>se.updateProject(B,R),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),o(!1),c(void 0),r(t("projects.toast.updated"),"success")},onError:B=>{r(B.message,"error")}}),y=lt({mutationFn:B=>se.deleteProject(B),onSuccess:()=>{f(null),r(t("projects.toast.deleted"),"success"),setTimeout(()=>window.location.reload(),100)},onError:B=>{f(null),r(B.message,"error")}}),S=lt({mutationFn:B=>se.importProject(B),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),r(t("projects.toast.imported"),"success")},onError:B=>{r(B.message,"error")}}),N=w.useRef(null),A=async()=>{try{const B=await se.getProjects(),R=await Promise.all(B.map(async K=>await se.exportProjectJson(K.id))),V=new Blob([JSON.stringify(R,null,2)],{type:"application/json"}),q=URL.createObjectURL(V),I=document.createElement("a");I.href=q,I.download=`bambuddy_projects_${new Date().toISOString().split("T")[0]}.json`,I.click(),URL.revokeObjectURL(q),r(t("projects.toast.exported"),"success")}catch(B){r(B.message,"error")}},M=()=>{N.current?.click()},T=async B=>{const R=B.target.files?.[0];if(R){try{if(R.name.toLowerCase().endsWith(".zip"))await se.importProjectFile(R),n.invalidateQueries({queryKey:["projects"]}),r(t("projects.toast.imported"),"success");else{const q=await R.text(),I=JSON.parse(q),K=Array.isArray(I)?I:[I];for(const Y of K)await S.mutateAsync(Y);K.length>1&&r(t("projects.toast.multipleImported",{count:K.length}),"success")}}catch(V){r(`${t("projects.toast.importFailed")}: ${V.message}`,"error")}B.target.value=""}},k=B=>{l?b.mutate({id:l.id,data:B}):v.mutate(B)},j=B=>{c(B),o(!0)},E=B=>{e(`/projects/${B.id}`)},P=B=>{f(B)},U=()=>{h!==null&&y.mutate(h)},L=p?.reduce((B,R)=>(B[R.status]=(B[R.status]||0)+1,B.all=(B.all||0)+1,B),{})||{};return a.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[a.jsx("input",{ref:N,type:"file",accept:".json,.zip",onChange:T,className:"hidden"}),a.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[a.jsxs("div",{children:[a.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:a.jsx(xs,{className:"w-6 h-6 text-bambu-green"})}),t("projects.title")]}),a.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:t("projects.subtitle")})]}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{variant:"secondary",onClick:M,disabled:!i("projects:create"),title:i("projects:create")?t("projects.importProject"):t("projects.noImportPermission"),children:[a.jsx(ia,{className:"w-4 h-4 mr-2"}),t("projects.import")]}),a.jsxs(Me,{variant:"secondary",onClick:A,disabled:!i("projects:read"),title:i("projects:read")?t("projects.exportAll"):t("projects.noExportPermission"),children:[a.jsx(di,{className:"w-4 h-4 mr-2"}),t("projects.export")]}),a.jsxs(Me,{onClick:()=>o(!0),className:"sm:w-auto w-full",disabled:!i("projects:create"),title:i("projects:create")?void 0:t("projects.noCreatePermission"),children:[a.jsx(ar,{className:"w-4 h-4 mr-2"}),t("projects.newProject")]})]})]}),a.jsx("div",{className:"flex gap-1 p-1 bg-bambu-dark rounded-xl w-fit",children:[{key:"active",label:t("projects.statusActive"),icon:Qn},{key:"completed",label:t("projects.statusCompleted"),icon:fb},{key:"archived",label:t("projects.statusArchived"),icon:Rs},{key:"all",label:t("common.all"),icon:xs}].map(({key:B,label:R,icon:V})=>a.jsxs("button",{onClick:()=>d(B),className:`flex items-center gap-2 px-4 py-2 text-sm rounded-lg transition-all ${u===B?"bg-bambu-card text-white shadow-sm":"text-bambu-gray hover:text-white"}`,children:[a.jsx(V,{className:"w-4 h-4"}),a.jsx("span",{children:R}),L[B]>0&&a.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded-full ${u===B?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary"}`,children:L[B]})]},B))}),x?a.jsx("div",{className:"flex items-center justify-center py-20",children:a.jsxs("div",{className:"flex flex-col items-center gap-3",children:[a.jsx(at,{className:"w-8 h-8 animate-spin text-bambu-green"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:t("projects.loading")})]})}):p?.length===0?a.jsxs("div",{className:"flex flex-col items-center justify-center py-20 px-4",children:[a.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:a.jsx(xs,{className:"w-12 h-12 text-bambu-gray/50"})}),a.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:u==="all"?t("projects.noProjects"):t("projects.noProjectsFiltered",{status:u})}),a.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:u==="all"?t("projects.createFirst"):t("projects.noProjectsFilteredHelp",{status:u})}),u==="all"&&a.jsxs(Me,{onClick:()=>o(!0),disabled:!i("projects:create"),title:i("projects:create")?void 0:t("projects.noCreatePermission"),children:[a.jsx(ar,{className:"w-4 h-4 mr-2"}),t("projects.createFirstButton")]})]}):a.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-8",children:p?.map(B=>a.jsx(OZe,{project:B,onClick:()=>E(B),onEdit:()=>j(B),onDelete:()=>P(B.id),hasPermission:i,t},B.id))}),h!==null&&a.jsx(kn,{title:t("projects.deleteProject"),message:t("projects.deleteConfirm"),confirmText:t("projects.deleteProject"),variant:"danger",onConfirm:U,onCancel:()=>f(null)}),s&&a.jsx(Kae,{project:l,onClose:()=>{o(!1),c(void 0)},onSave:k,isLoading:v.isPending||b.isPending,t})]})}function zZe(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 UZe(t){return t>=1e3?`${(t/1e3).toFixed(2)}kg`:`${Math.round(t)}g`}function BZe({status:t,t:e}){const n={active:"bg-bambu-green/20 text-bambu-green",completed:"bg-blue-500/20 text-blue-400",archived:"bg-bambu-gray/20 text-bambu-gray"},r=n[t]||n.active,i={active:e("projectDetail.status.active"),completed:e("projectDetail.status.completed"),archived:e("projectDetail.status.archived")};return a.jsx("span",{className:`px-2 py-1 rounded text-sm font-medium ${r}`,children:i[t]||t.charAt(0).toUpperCase()+t.slice(1)})}function uK({icon:t,label:e,value:n,subValue:r,hint:i,color:s="text-bambu-gray"}){return a.jsx(_t,{children:a.jsx(Nt,{className:"p-4",children:a.jsxs("div",{className:"flex items-center gap-3",title:i,children:[a.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark ${s}`,children:a.jsx(t,{className:"w-5 h-5"})}),a.jsxs("div",{children:[a.jsx("p",{className:"text-sm text-bambu-gray",children:e}),a.jsx("p",{className:"text-xl font-semibold text-white",children:n}),r&&a.jsx("p",{className:"text-xs text-bambu-gray/70",children:r})]})]})})})}function HZe({archives:t,t:e}){return t.length===0?a.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[a.jsx(ma,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),a.jsx("p",{children:e("projectDetail.noPrints")})]}):a.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(n=>a.jsxs(Xs,{to:`/archives?search=${encodeURIComponent(n.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:[n.thumbnail_path?a.jsx("img",{src:se.getArchiveThumbnail(n.id),alt:n.print_name||"Print",className:"w-full h-full object-cover"}):a.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:a.jsx(ma,{className:"w-8 h-8"})}),n.status==="failed"&&a.jsx("div",{className:"absolute inset-0 bg-red-500/30 flex items-center justify-center",children:a.jsx(ha,{className:"w-8 h-8 text-white"})}),n.status==="completed"&&a.jsx("div",{className:"absolute top-1 right-1",children:a.jsx(gr,{className:"w-4 h-4 text-bambu-green"})}),a.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:a.jsx("p",{className:"text-xs text-white truncate",children:n.print_name||"Unknown"})})]},n.id))})}function $Ze({priority:t,t:e}){const n={low:{color:"bg-gray-500/20 text-gray-400",label:e("projectDetail.priority.low")},normal:{color:"bg-blue-500/20 text-blue-400",label:e("projectDetail.priority.normal")},high:{color:"bg-orange-500/20 text-orange-400",label:e("projectDetail.priority.high")},urgent:{color:"bg-red-500/20 text-red-400",label:e("projectDetail.priority.urgent")}},{color:r,label:i}=n[t]||n.normal;return a.jsxs("span",{className:`px-2 py-1 rounded text-xs font-medium flex items-center gap-1 ${r}`,children:[t==="urgent"&&a.jsx(Cn,{className:"w-3 h-3"}),i]})}function VZe(t){return t?gb(t,{year:"numeric",month:"short",day:"numeric"}):""}function Gj(t,e){if(!t)return null;const n=Zi(t);if(!n)return null;const r=new Date,i=Math.ceil((n.getTime()-r.getTime())/(1e3*60*60*24));return i<0?{color:"text-red-400",label:e("projectDetail.dueDate.overdue")}:i===0?{color:"text-orange-400",label:e("projectDetail.dueDate.today")}:i<=3?{color:"text-yellow-400",label:e("projectDetail.dueDate.daysLeft",{count:i})}:{color:"text-bambu-gray",label:e("projectDetail.dueDate.daysLeft",{count:i})}}function GZe(){const{id:t}=Yk(),e=Hc(),{t:n}=Vt(),r=tn(),{showToast:i}=mn(),{hasPermission:s}=Ir(),[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,h]=w.useState(""),f=parseInt(t||"0",10),{data:p,isLoading:x,error:v}=tt({queryKey:["project",f],queryFn:()=>se.getProject(f),enabled:f>0}),{data:b,isLoading:y}=tt({queryKey:["project-archives",f],queryFn:()=>se.getProjectArchives(f),enabled:f>0}),{data:S,isLoading:N}=tt({queryKey:["project-bom",f],queryFn:()=>se.getProjectBOM(f),enabled:f>0}),{data:A,isLoading:M}=tt({queryKey:["project-timeline",f],queryFn:()=>se.getProjectTimeline(f,20),enabled:f>0}),{data:T}=tt({queryKey:["settings"],queryFn:se.getSettings}),{data:k}=tt({queryKey:["project-folders",f],queryFn:()=>se.getLibraryFoldersByProject(f),enabled:f>0}),j=Pu(T?.currency||"USD"),E=T?.time_format||"system",P=lt({mutationFn:je=>se.updateProject(f,je),onSuccess:()=>{r.invalidateQueries({queryKey:["project",f]}),r.invalidateQueries({queryKey:["projects"]}),l(!1),u(!1),i(n("projectDetail.toast.projectUpdated"),"success")},onError:je=>{i(je.message,"error")}}),U=()=>{h(p?.notes||""),u(!0)},L=()=>{P.mutate({notes:d})},B=()=>{u(!1),h("")},[R,V]=w.useState(""),[q,I]=w.useState(1),[K,Y]=w.useState(""),[le,H]=w.useState(""),[X,te]=w.useState(""),[J,Se]=w.useState(!1),[fe,me]=w.useState(!1),[De,de]=w.useState(null),[ce,Ne]=w.useState(""),[ie,Le]=w.useState(1),[$,W]=w.useState(""),[ue,Z]=w.useState(""),[oe,Q]=w.useState(""),[he,be]=w.useState({isOpen:!1,title:"",message:"",onConfirm:()=>{}}),ee=lt({mutationFn:je=>se.createBOMItem(f,je),onSuccess:()=>{r.invalidateQueries({queryKey:["project-bom",f]}),r.invalidateQueries({queryKey:["project",f]}),V(""),I(1),Y(""),H(""),te(""),Se(!1),i(n("projectDetail.toast.partAdded"),"success")},onError:je=>i(je.message,"error")}),z=lt({mutationFn:({itemId:je,data:Ve})=>se.updateBOMItem(f,je,Ve),onSuccess:()=>{r.invalidateQueries({queryKey:["project-bom",f]}),r.invalidateQueries({queryKey:["project",f]}),de(null)},onError:je=>i(je.message,"error")}),D=lt({mutationFn:je=>se.deleteBOMItem(f,je),onSuccess:()=>{r.invalidateQueries({queryKey:["project-bom",f]}),r.invalidateQueries({queryKey:["project",f]}),i(n("projectDetail.toast.partRemoved"),"success")},onError:je=>i(je.message,"error")}),G=je=>{je.preventDefault(),R.trim()&&ee.mutate({name:R.trim(),quantity_needed:q,unit_price:K?parseFloat(K):void 0,sourcing_url:le.trim()||void 0,remarks:X.trim()||void 0})},pe=je=>{const Ve=je.is_complete?0:je.quantity_needed;z.mutate({itemId:je.id,data:{quantity_acquired:Ve}})},ve=(je,Ve)=>{be({isOpen:!0,title:n("projectDetail.bom.deletePart"),message:n("projectDetail.bom.deleteConfirm",{name:Ve}),onConfirm:()=>{be(Ae=>({...Ae,isOpen:!1})),D.mutate(je)}})},xe=je=>{de(je),Ne(je.name),Le(je.quantity_needed),W(je.unit_price?.toString()||""),Z(je.sourcing_url||""),Q(je.remarks||"")},ze=je=>{je.preventDefault(),!(!De||!ce.trim())&&z.mutate({itemId:De.id,data:{name:ce.trim(),quantity_needed:ie,unit_price:$?parseFloat($):void 0,sourcing_url:ue.trim()||void 0,remarks:oe.trim()||void 0}})},_e=()=>{de(null)},He=async()=>{try{const{blob:je,filename:Ve}=await se.exportProjectZip(Number(f)),Ae=URL.createObjectURL(je),Je=document.createElement("a");Je.href=Ae,Je.download=Ve||`${p?.name||"project"}_${new Date().toISOString().split("T")[0]}.zip`,Je.click(),URL.revokeObjectURL(Ae),i(n("projectDetail.toast.projectExported"),"success")}catch(je){i(je.message,"error")}},Ze=lt({mutationFn:()=>se.createTemplateFromProject(f),onSuccess:()=>{r.invalidateQueries({queryKey:["projects"]}),i(n("projectDetail.toast.templateCreated"),"success")},onError:je=>i(je.message,"error")}),we=je=>Ff(je,E,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"});if(x)return a.jsx("div",{className:"flex items-center justify-center py-24",children:a.jsx(at,{className:"w-8 h-8 animate-spin text-bambu-green"})});if(v||!p)return a.jsxs("div",{className:"text-center py-24",children:[a.jsx("p",{className:"text-bambu-gray",children:v?`${n("common.error")}: ${v.message}`:n("projectDetail.notFound")}),a.jsx(Me,{variant:"secondary",className:"mt-4",onClick:()=>e("/projects"),children:n("projectDetail.backToProjects")})]});const ye=p.stats,ke=ye?.progress_percent??0,Ue=ye?.parts_progress_percent??0;return a.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[a.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[a.jsx(Xs,{to:"/projects",className:"hover:text-white transition-colors",children:n("navigation.projects")}),a.jsx(fa,{className:"w-4 h-4"}),a.jsx("span",{className:"text-white",children:p.name})]}),a.jsxs("div",{className:"flex items-start justify-between",children:[a.jsxs("div",{className:"flex items-center gap-4",children:[a.jsx("button",{onClick:()=>e("/projects"),className:"p-2 rounded-lg bg-bambu-card hover:bg-bambu-dark-tertiary transition-colors",children:a.jsx(fde,{className:"w-5 h-5 text-bambu-gray"})}),a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"w-4 h-4 rounded-full flex-shrink-0",style:{backgroundColor:p.color||"#6b7280"}}),a.jsxs("div",{children:[a.jsx("h1",{className:"text-2xl font-bold text-white",children:p.name}),p.description&&a.jsx("p",{className:"text-bambu-gray mt-1",children:p.description})]})]}),a.jsx(BZe,{status:p.status,t:n})]}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{variant:"secondary",onClick:He,disabled:!s("projects:read"),title:s("projects:read")?n("projectDetail.exportProject"):n("projectDetail.noExportPermission"),children:[a.jsx(di,{className:"w-4 h-4 mr-2"}),n("projectDetail.export")]}),a.jsxs(Me,{onClick:()=>l(!0),disabled:!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.noEditPermission"),children:[a.jsx(Pf,{className:"w-4 h-4 mr-2"}),n("common.edit")]})]})]}),(p.target_count||p.target_parts_count)&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4 space-y-4",children:[p.target_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-2",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:n("projectDetail.progress.platesProgress")}),a.jsxs("span",{className:"text-sm font-medium text-white",children:[ye?.total_archives||0," / ",p.target_count," ",n("projectDetail.progress.printJobs")]})]}),a.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:a.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(ke,100)}%`,backgroundColor:ke>=100?"#22c55e":p.color||"#6b7280"}})}),a.jsxs("div",{className:"flex justify-between mt-1",children:[a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.percentComplete",{percent:ke.toFixed(0)})}),ye?.remaining_prints!=null&&ye.remaining_prints>0&&a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.remaining",{count:ye.remaining_prints})})]})]}),p.target_parts_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-2",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:n("projectDetail.progress.partsProgress")}),a.jsxs("span",{className:"text-sm font-medium text-white",children:[ye?.completed_prints||0," / ",p.target_parts_count," ",n("projectDetail.progress.parts")]})]}),a.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:a.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(Ue,100)}%`,backgroundColor:Ue>=100?"#22c55e":p.color||"#6b7280"}})}),a.jsxs("div",{className:"flex justify-between mt-1",children:[a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.percentComplete",{percent:Ue.toFixed(0)})}),ye?.remaining_parts!=null&&ye.remaining_parts>0&&a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.remaining",{count:ye.remaining_parts})})]})]})]})}),ye&&a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[a.jsx(_t,{children:a.jsx(Nt,{className:"p-4",children:a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:a.jsx(ma,{className:"w-5 h-5"})}),a.jsxs("div",{children:[a.jsx("p",{className:"text-sm text-bambu-gray",children:n("projectDetail.stats.printJobs")}),a.jsxs("p",{className:"text-xl font-semibold text-white",children:[ye.total_archives," ",a.jsx("span",{className:"text-sm font-normal text-bambu-gray",children:n("projectDetail.stats.total")})]}),ye.failed_prints>0&&a.jsx("p",{className:"text-sm text-status-error",children:n("projectDetail.stats.failed",{count:ye.failed_prints})}),a.jsx("p",{className:"text-sm text-bambu-gray",children:n("projectDetail.stats.partsPrinted",{count:ye.completed_prints})})]})]})})}),a.jsx(uK,{icon:Qn,label:n("projectDetail.stats.printTime"),value:zZe(ye.total_print_time_hours),color:"text-yellow-400"}),a.jsx(uK,{icon:Cr,label:n("projectDetail.stats.filamentUsed"),value:UZe(ye.total_filament_grams),color:"text-purple-400"})]}),ye&&(ye.estimated_cost>0||p.budget)&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsx("h2",{className:"text-lg font-semibold text-white mb-3",children:n("projectDetail.cost.title")}),a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.filamentCost")}),a.jsxs("p",{className:"text-lg font-semibold text-white",children:[j,ye.estimated_cost.toFixed(2)]})]}),ye.total_energy_kwh>0&&a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.energy")}),a.jsxs("p",{className:"text-lg font-semibold text-white",children:[ye.total_energy_kwh.toFixed(2)," kWh",ye.total_energy_cost>0&&a.jsxs("span",{className:"text-sm text-bambu-gray ml-1",children:["(",j,ye.total_energy_cost.toFixed(2),")"]})]})]}),p.budget&&a.jsxs(a.Fragment,{children:[a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.budget")}),a.jsxs("p",{className:"text-lg font-semibold text-white",children:[j,p.budget.toFixed(2)]})]}),a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.remaining")}),a.jsxs("p",{className:`text-lg font-semibold ${p.budget-ye.estimated_cost>=0?"text-bambu-green":"text-red-400"}`,children:[j,(p.budget-ye.estimated_cost).toFixed(2)]})]})]})]})]})}),p.children&&p.children.length>0&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-3",children:[a.jsx(tme,{className:"w-5 h-5"}),n("projectDetail.subProjects.title",{count:p.children.length})]}),a.jsx("div",{className:"space-y-2",children:p.children.map(je=>a.jsxs(Xs,{to:`/projects/${je.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:je.color||"#6b7280"}}),a.jsx("span",{className:"text-white",children:je.name}),a.jsx("span",{className:`text-xs px-2 py-0.5 rounded ${je.status==="completed"?"bg-status-ok/20 text-status-ok":je.status==="archived"?"bg-bambu-gray/20 text-bambu-gray":"bg-blue-500/20 text-blue-400"}`,children:je.status})]}),je.progress_percent!==null&&a.jsxs("span",{className:"text-sm text-bambu-gray",children:[je.progress_percent.toFixed(0),"%"]})]},je.id))})]})}),p.parent_id&&p.parent_name&&a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(rs,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:"text-bambu-gray",children:n("projectDetail.partOf")}),a.jsx(Xs,{to:`/projects/${p.parent_id}`,className:"text-bambu-green hover:underline",children:p.parent_name})]}),(p.tags||p.due_date||p.priority!=="normal")&&a.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[p.priority&&p.priority!=="normal"&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.priorityLabel")}),a.jsx($Ze,{priority:p.priority,t:n})]}),p.due_date&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(ii,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:"text-sm text-white",children:VZe(p.due_date)}),Gj(p.due_date,n)&&a.jsxs("span",{className:`text-xs ${Gj(p.due_date,n).color}`,children:["(",Gj(p.due_date,n).label,")"]})]}),p.tags&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(Vh,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("div",{className:"flex flex-wrap gap-1",children:p.tags.split(",").map((je,Ve)=>a.jsx("span",{className:"px-2 py-0.5 bg-bambu-dark-tertiary text-bambu-gray text-xs rounded",children:je.trim()},Ve))})]})]}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(Ps,{className:"w-5 h-5"}),n("projectDetail.notes.title")]}),c?a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{variant:"secondary",size:"sm",onClick:B,disabled:P.isPending,children:[a.jsx(Ft,{className:"w-4 h-4 mr-1"}),n("common.cancel")]}),a.jsxs(Me,{size:"sm",onClick:L,disabled:P.isPending,children:[P.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin mr-1"}):a.jsx(bs,{className:"w-4 h-4 mr-1"}),n("common.save")]})]}):a.jsxs(Me,{variant:"secondary",size:"sm",onClick:U,disabled:!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.notes.noEditPermission"),children:[a.jsx(Pf,{className:"w-4 h-4 mr-1"}),n("common.edit")]})]}),c?a.jsx(oF,{content:d,onChange:h,placeholder:n("projectDetail.notes.placeholder")}):p.notes?a.jsx("div",{className:"prose prose-invert prose-sm max-w-none",dangerouslySetInnerHTML:{__html:p.notes}}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.notes.empty")})]})}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsx("div",{className:"flex items-center justify-between mb-3",children:a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(Xl,{className:"w-5 h-5"}),n("projectDetail.files.title")]})}),a.jsxs("p",{className:"text-xs text-bambu-gray mb-3",children:[a.jsx(Xs,{to:"/files",className:"text-bambu-green hover:underline",children:n("projectDetail.files.linkFolders")})," ",n("projectDetail.files.forQuickAccess")]}),k&&k.length>0?a.jsx("div",{className:"space-y-2",children:k.map(je=>a.jsxs(Xs,{to:`/files?folder=${je.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[a.jsx(Xl,{className:"w-5 h-5 text-bambu-green flex-shrink-0"}),a.jsxs("div",{className:"min-w-0",children:[a.jsx("p",{className:"text-sm text-white truncate",children:je.name}),a.jsx("p",{className:"text-xs text-bambu-gray",children:n("projectDetail.files.fileCount",{count:je.file_count})})]})]}),a.jsx(fa,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"})]},je.id))}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.files.empty")})]})}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("div",{className:"flex items-center justify-between mb-4",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(bX,{className:"w-5 h-5"}),n("projectDetail.bom.title"),ye&&ye.bom_total_items>0&&a.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",n("projectDetail.bom.acquired",{completed:ye.bom_completed_items,total:ye.bom_total_items}),")"]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[S&&S.some(je=>je.is_complete)&&a.jsx("button",{onClick:()=>me(!fe),className:`text-xs px-2 py-1 rounded transition-colors ${fe?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:n(fe?"projectDetail.bom.showAll":"projectDetail.bom.hideDone")}),!J&&a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>Se(!0),disabled:!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.bom.noAddPermission"),children:[a.jsx(ar,{className:"w-4 h-4 mr-1"}),n("projectDetail.bom.addPart")]})]})]}),J&&a.jsxs("form",{onSubmit:G,className:"bg-bambu-dark rounded-lg p-4 mb-4 space-y-3",children:[a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[a.jsx("input",{type:"text",value:R,onChange:je=>V(je.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:n("projectDetail.bom.partNamePlaceholder"),autoFocus:!0}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx("input",{type:"number",value:q,onChange:je=>I(parseInt(je.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:n("projectDetail.bom.qty")}),a.jsx("input",{type:"number",step:"0.01",value:K,onChange:je=>Y(je.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:n("projectDetail.bom.price",{currency:j})})]})]}),a.jsx("input",{type:"url",value:le,onChange:je=>H(je.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:n("projectDetail.bom.sourcingUrlPlaceholder")}),a.jsx("input",{type:"text",value:X,onChange:je=>te(je.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:n("projectDetail.bom.remarksPlaceholder")}),a.jsxs("div",{className:"flex justify-end gap-2",children:[a.jsx(Me,{type:"button",variant:"secondary",size:"sm",onClick:()=>Se(!1),children:n("common.cancel")}),a.jsx(Me,{type:"submit",size:"sm",disabled:!R.trim()||ee.isPending,children:ee.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):n("projectDetail.bom.addPart")})]})]}),N?a.jsx("div",{className:"flex items-center justify-center py-4",children:a.jsx(at,{className:"w-6 h-6 animate-spin text-bambu-green"})}):S&&S.length>0?a.jsxs("div",{className:"space-y-2",children:[S.filter(je=>!fe||!je.is_complete).map(je=>a.jsx("div",{className:`p-3 rounded-lg transition-colors ${je.is_complete?"bg-status-ok/10":"bg-bambu-dark"}`,children:De?.id===je.id?a.jsxs("form",{onSubmit:ze,className:"space-y-3",children:[a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[a.jsx("input",{type:"text",value:ce,onChange:Ve=>Ne(Ve.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:n("projectDetail.bom.partName"),autoFocus:!0}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx("input",{type:"number",value:ie,onChange:Ve=>Le(parseInt(Ve.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:n("projectDetail.bom.qty")}),a.jsx("input",{type:"number",step:"0.01",value:$,onChange:Ve=>W(Ve.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:n("projectDetail.bom.price",{currency:j})})]})]}),a.jsx("input",{type:"url",value:ue,onChange:Ve=>Z(Ve.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:n("projectDetail.bom.sourcingUrlPlaceholder")}),a.jsx("input",{type:"text",value:oe,onChange:Ve=>Q(Ve.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:n("projectDetail.bom.remarksPlaceholder")}),a.jsxs("div",{className:"flex justify-end gap-2",children:[a.jsx(Me,{type:"button",variant:"secondary",size:"sm",onClick:_e,children:n("common.cancel")}),a.jsx(Me,{type:"submit",size:"sm",disabled:!ce.trim()||z.isPending,children:z.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):n("common.save")})]})]}):a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx("button",{onClick:()=>s("projects:update")&&pe(je),disabled:z.isPending||!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.bom.noUpdatePermission"),className:`w-5 h-5 mt-0.5 rounded border-2 flex items-center justify-center transition-colors flex-shrink-0 ${je.is_complete?"bg-status-ok border-status-ok text-white":s("projects:update")?"border-bambu-gray hover:border-bambu-green":"border-bambu-gray/50 cursor-not-allowed"}`,children:je.is_complete&&a.jsx(gr,{className:"w-3 h-3"})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center justify-between gap-2",children:[a.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[a.jsxs("p",{className:`text-sm font-medium ${je.is_complete?"text-bambu-gray line-through":"text-white"}`,children:[je.name,a.jsxs("span",{className:"text-bambu-gray font-normal ml-2",children:["x",je.quantity_needed]})]}),je.unit_price!==null&&a.jsxs("span",{className:"text-xs text-bambu-green whitespace-nowrap",children:[j,(je.unit_price*je.quantity_needed).toFixed(2)]})]}),a.jsxs("div",{className:"flex items-center gap-1",children:[a.jsx("button",{onClick:()=>s("projects:update")&&xe(je),disabled:!s("projects:update"),className:`p-1 rounded transition-colors flex-shrink-0 ${s("projects:update")?"hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("projects:update")?n("common.edit"):n("projectDetail.bom.noEditPermission"),children:a.jsx($i,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>s("projects:update")&&ve(je.id,je.name),disabled:!s("projects:update"),className:`p-1 rounded transition-colors flex-shrink-0 ${s("projects:update")?"hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-red-400":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("projects:update")?n("common.delete"):n("projectDetail.bom.noDeletePermission"),children:a.jsx(en,{className:"w-4 h-4"})})]})]}),je.sourcing_url&&a.jsxs("a",{href:je.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:Ve=>Ve.stopPropagation(),children:[a.jsx(Kr,{className:"w-3 h-3 flex-shrink-0"}),a.jsx("span",{className:"truncate",children:(()=>{try{return new URL(je.sourcing_url).hostname.replace("www.","")}catch{return je.sourcing_url}})()})]}),je.remarks&&a.jsx("p",{className:"mt-1 text-xs text-bambu-gray/80 italic",children:je.remarks})]})]})},je.id)),S.some(je=>je.unit_price!==null)&&a.jsxs("div",{className:"pt-2 mt-2 border-t border-bambu-dark-tertiary flex justify-between text-sm",children:[a.jsx("span",{className:"text-bambu-gray",children:n("projectDetail.bom.totalCost")}),a.jsxs("span",{className:"text-white font-medium",children:[j,S.reduce((je,Ve)=>je+(Ve.unit_price||0)*Ve.quantity_needed,0).toFixed(2)]})]})]}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.bom.empty")})]})}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsx("div",{className:"flex items-center justify-between mb-3",children:a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(N0,{className:"w-5 h-5"}),n("projectDetail.timeline.title")]})}),M?a.jsx("div",{className:"flex items-center justify-center py-4",children:a.jsx(at,{className:"w-6 h-6 animate-spin text-bambu-green"})}):A&&A.length>0?a.jsx("div",{className:"space-y-3",children:A.slice(0,10).map((je,Ve)=>a.jsxs("div",{className:"flex gap-3",children:[a.jsxs("div",{className:`w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 ${je.event_type==="print_completed"?"bg-status-ok/20 text-status-ok":je.event_type==="print_failed"?"bg-status-error/20 text-status-error":je.event_type==="print_started"?"bg-yellow-500/20 text-yellow-400":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[je.event_type==="print_completed"&&a.jsx(gr,{className:"w-4 h-4"}),je.event_type==="print_failed"&&a.jsx(ha,{className:"w-4 h-4"}),je.event_type==="print_started"&&a.jsx(Cr,{className:"w-4 h-4"}),je.event_type==="queued"&&a.jsx(g3,{className:"w-4 h-4"}),je.event_type==="project_created"&&a.jsx(ar,{className:"w-4 h-4"})]}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm text-white",children:je.title}),je.description&&a.jsx("p",{className:"text-xs text-bambu-gray truncate",children:je.description}),a.jsx("p",{className:"text-xs text-bambu-gray/70",children:we(je.timestamp)})]})]},Ve))}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.timeline.empty")})]})}),!p.is_template&&a.jsx("div",{className:"flex justify-end",children:a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>Ze.mutate(),disabled:Ze.isPending||!s("projects:create"),title:s("projects:create")?void 0:n("projectDetail.template.noCreatePermission"),children:[Ze.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin mr-2"}):a.jsx(Wl,{className:"w-4 h-4 mr-2"}),n("projectDetail.template.saveAsTemplate")]})}),ye&&(ye.queued_prints>0||ye.in_progress_prints>0)&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(g3,{className:"w-5 h-5"}),n("projectDetail.queue.title")]}),a.jsx(Xs,{to:`/queue?project=${f}`,className:"text-sm text-bambu-green hover:underline",children:n("projectDetail.queue.viewAll")})]}),a.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[ye.in_progress_prints>0&&a.jsx("span",{className:"text-yellow-400",children:n("projectDetail.queue.printing",{count:ye.in_progress_prints})}),ye.queued_prints>0&&a.jsx("span",{className:"text-bambu-gray",children:n("projectDetail.queue.queued",{count:ye.queued_prints})})]})]})}),a.jsxs("div",{children:[a.jsx("div",{className:"flex items-center justify-between mb-4",children:a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(ma,{className:"w-5 h-5"}),n("projectDetail.prints.title",{count:b?.length||0})]})}),y?a.jsx("div",{className:"flex items-center justify-center py-8",children:a.jsx(at,{className:"w-6 h-6 animate-spin text-bambu-green"})}):a.jsx(HZe,{archives:b||[],t:n})]}),o&&a.jsx(Kae,{t:n,project:{...p,archive_count:ye?.total_archives||0,total_items:ye?.total_items||0,completed_count:ye?.completed_prints||0,failed_count:ye?.failed_prints||0,queue_count:ye?.queued_prints||0,progress_percent:ye?.progress_percent||null,archives:[]},onClose:()=>l(!1),onSave:je=>P.mutate(je),isLoading:P.isPending}),he.isOpen&&a.jsx(kn,{title:he.title,message:he.message,confirmText:n("common.delete"),variant:"danger",onConfirm:he.onConfirm,onCancel:()=>be(je=>({...je,isOpen:!1}))})]})}function Bg(t){return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(1)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`}function qZe(t){if(!t)return"-";const e=Math.floor(t/3600),n=Math.floor(t%3600/60);return e>0?`${e}h ${n}m`:`${n}m`}function WZe({parentId:t,onClose:e,onSave:n,isLoading:r,t:i}){const[s,o]=w.useState(""),l=c=>{c.preventDefault(),n({name:s.trim(),parent_id:t})};return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:i("fileManager.newFolder")})}),a.jsxs("form",{onSubmit:l,className:"p-4 space-y-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("fileManager.folderName")}),a.jsx("input",{type:"text",value:s,onChange:c=>o(c.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:i("fileManager.folderNamePlaceholder"),autoFocus:!0,required:!0})]}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!s.trim()||r,children:r?a.jsx(at,{className:"w-4 h-4 animate-spin"}):i("common.create")})]})]})]})})}function KZe({type:t,currentName:e,onClose:n,onSave:r,isLoading:i,t:s}){const[o,l]=w.useState(e),c=u=>{u.preventDefault(),o.trim()&&o.trim()!==e&&r(o.trim())};return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:s(t==="file"?"fileManager.renameFile":"fileManager.renameFolder")})}),a.jsxs("form",{onSubmit:c,className:"p-4 space-y-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:s("common.name")}),a.jsx("input",{type:"text",value:o,onChange:u=>l(u.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",autoFocus:!0,required:!0})]}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:n,children:s("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!o.trim()||o.trim()===e||i,children:i?a.jsx(at,{className:"w-4 h-4 animate-spin"}):s("common.rename")})]})]})]})})}function XZe({folders:t,selectedFiles:e,currentFolderId:n,onClose:r,onMove:i,isLoading:s,t:o}){const[l,c]=w.useState(null),u=(h,f=0)=>{const p=[];for(const x of h)p.push({id:x.id,name:x.name,depth:f}),x.children.length>0&&p.push(...u(x.children,f+1));return p},d=[{id:null,name:o("fileManager.rootNoFolder"),depth:0},...u(t)];return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:o("fileManager.moveFiles",{count:e.length})})}),a.jsxs("div",{className:"p-4 space-y-4",children:[a.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1",children:d.map(h=>a.jsxs("button",{onClick:()=>c(h.id),disabled:h.id===n,className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${l===h.id?"bg-bambu-green/20 text-bambu-green":h.id===n?"opacity-50 cursor-not-allowed text-bambu-gray":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${12+h.depth*16}px`},children:[a.jsx(Xl,{className:"w-4 h-4"}),h.name,h.id===n&&a.jsxs("span",{className:"text-xs text-bambu-gray ml-auto",children:["(",o("fileManager.current"),")"]})]},h.id??"root"))}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:r,children:o("common.cancel")}),a.jsx(Me,{onClick:()=>i(l),disabled:s,children:s?a.jsx(at,{className:"w-4 h-4 animate-spin"}):o("common.move")})]})]})]})})}function YZe({folder:t,onClose:e,onLink:n,isLoading:r,t:i}){const[s,o]=w.useState("project"),[l,c]=w.useState(t.project_id||t.archive_id||null);w.useState(()=>{t.archive_id&&o("archive")});const{data:u}=tt({queryKey:["projects"],queryFn:()=>se.getProjects()}),{data:d}=tt({queryKey:["archives-for-link"],queryFn:()=>se.getArchives(void 0,void 0,100)}),h=()=>{n(s==="project"?{project_id:l,archive_id:0}:{project_id:0,archive_id:l})},f=()=>{n({project_id:0,archive_id:0})},p=t.project_id||t.archive_id;return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(jh,{className:"w-5 h-5 text-bambu-green"}),i("fileManager.linkFolder")]}),a.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:a.jsx(Ft,{className:"w-5 h-5 text-bambu-gray"})})]}),a.jsxs("div",{className:"p-4 space-y-4",children:[a.jsx("p",{className:"text-sm text-bambu-gray",children:i("fileManager.linkFolderDescription",{name:t.name})}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs("button",{onClick:()=>{o("project"),c(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${s==="project"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[a.jsx(oX,{className:"w-4 h-4"}),i("fileManager.project")]}),a.jsxs("button",{onClick:()=>{o("archive"),c(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${s==="archive"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[a.jsx(Rs,{className:"w-4 h-4"}),i("fileManager.archive")]})]}),a.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1 bg-bambu-dark rounded-lg p-2",children:s==="project"?u&&u.length>0?u.map(x=>a.jsxs("button",{onClick:()=>c(x.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${l===x.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[a.jsx("div",{className:"w-3 h-3 rounded-full flex-shrink-0",style:{backgroundColor:x.color||"#00ae42"}}),a.jsx("span",{className:"truncate",children:x.name})]},x.id)):a.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:i("fileManager.noProjectsFound")}):d&&d.length>0?d.map(x=>a.jsxs("button",{onClick:()=>c(x.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${l===x.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[a.jsx(ff,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),a.jsx("span",{className:"truncate",children:x.print_name||x.filename})]},x.id)):a.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:i("fileManager.noArchivesFound")})})]}),a.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-between",children:[p&&a.jsxs(Me,{variant:"danger",onClick:f,disabled:r,children:[a.jsx(uC,{className:"w-4 h-4 mr-2"}),i("fileManager.unlink")]}),a.jsxs("div",{className:`flex gap-2 ${p?"":"ml-auto"}`,children:[a.jsx(Me,{variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{onClick:h,disabled:!l||r,children:r?a.jsx(at,{className:"w-4 h-4 animate-spin"}):i("fileManager.link")})]})]})]})})}function QZe({folderId:t,onClose:e,onUploadComplete:n,t:r}){const[i,s]=w.useState([]),[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,h]=w.useState(!0),[f,p]=w.useState(!1),[x,v]=w.useState(!0),b=w.useRef(null),y=V=>{V.preventDefault(),l(!0)},S=V=>{V.preventDefault(),l(!1)},N=V=>{V.preventDefault(),l(!1);const q=Array.from(V.dataTransfer.files);M(q)},A=V=>{V.target.files&&M(Array.from(V.target.files))},M=V=>{const q=V.map(I=>({file:I,status:"pending",isZip:I.name.toLowerCase().endsWith(".zip"),is3mf:I.name.toLowerCase().endsWith(".3mf")}));s(I=>[...I,...q])},T=V=>{s(q=>q.filter((I,K)=>K!==V))},k=i.some(V=>V.isZip&&V.status==="pending"),j=i.some(V=>V.file.name.toLowerCase().endsWith(".stl")&&V.status==="pending"),E=i.some(V=>V.is3mf&&V.status==="pending"),P=async()=>{if(i.length!==0){u(!0);for(let V=0;Vq.map((I,K)=>K===V?{...I,status:"uploading"}:I));try{if(i[V].isZip){const q=await se.extractZipFile(i[V].file,t,d,f,x);s(I=>I.map((K,Y)=>Y===V?{...K,status:q.errors.length>0&&q.extracted===0?"error":"success",extractedCount:q.extracted,error:q.errors.length>0?`${q.errors.length} files failed`:void 0}:K))}else await se.uploadLibraryFile(i[V].file,t,x),s(q=>q.map((I,K)=>K===V?{...I,status:"success"}:I))}catch(q){s(I=>I.map((K,Y)=>Y===V?{...K,status:"error",error:q instanceof Error?q.message:"Upload failed"}:K))}}u(!1),n(),e()}},U=i.filter(V=>V.status==="pending").length,L=i.filter(V=>V.status==="success").length,B=i.filter(V=>V.status==="error").length,R=i.length>0&&U===0&&!c;return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:r("fileManager.uploadFiles")}),a.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:a.jsx(Ft,{className:"w-5 h-5 text-bambu-gray"})})]}),a.jsxs("div",{className:"p-4 space-y-4",children:[a.jsxs("div",{onDragOver:y,onDragLeave:S,onDrop:N,onClick:()=>b.current?.click(),className:`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors ${o?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,children:[a.jsx(ia,{className:`w-10 h-10 mx-auto mb-3 ${o?"text-bambu-green":"text-bambu-gray"}`}),a.jsx("p",{className:"text-white font-medium",children:r(o?"fileManager.dropFilesHere":"fileManager.dragDropFiles")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:r("fileManager.orClickToBrowse")}),a.jsx("p",{className:"text-xs text-bambu-gray/70 mt-2",children:r("fileManager.allFileTypesSupported")})]}),a.jsx("input",{ref:b,type:"file",multiple:!0,className:"hidden",onChange:A}),k&&a.jsx("div",{className:"p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx(Rs,{className:"w-5 h-5 text-blue-400 mt-0.5 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-blue-300 font-medium",children:r("fileManager.zipFilesDetected")}),a.jsx("p",{className:"text-xs text-blue-300/70 mt-1",children:r("fileManager.zipExtractOptions")}),a.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:d,onChange:V=>h(V.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),a.jsx("span",{className:"text-sm text-white",children:r("fileManager.preserveZipStructure")})]}),a.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:f,onChange:V=>p(V.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),a.jsx("span",{className:"text-sm text-white",children:r("fileManager.createFolderFromZip")})]})]})]})}),E&&a.jsx("div",{className:"p-3 bg-purple-500/10 border border-purple-500/30 rounded-lg",children:a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx(Cr,{className:"w-5 h-5 text-purple-400 mt-0.5 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-purple-300 font-medium",children:r("fileManager.threemfDetected")}),a.jsx("p",{className:"text-xs text-purple-300/70 mt-1",children:r("fileManager.threemfExtractionInfo")})]})]})}),(j||k)&&a.jsx("div",{className:"p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx(ed,{className:"w-5 h-5 text-bambu-green mt-0.5 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-bambu-green font-medium",children:r("fileManager.stlThumbnailGeneration")}),a.jsx("p",{className:"text-xs text-bambu-green/70 mt-1",children:r(k&&!j?"fileManager.zipMayContainStl":"fileManager.thumbnailsCanBeGenerated")}),a.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:x,onChange:V=>v(V.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),a.jsx("span",{className:"text-sm text-white",children:r("fileManager.generateThumbnailsForStl")})]})]})]})}),i.length>0&&a.jsx("div",{className:"max-h-48 overflow-y-auto space-y-2",children:i.map((V,q)=>a.jsxs("div",{className:"flex items-center gap-3 p-2 bg-bambu-dark rounded-lg",children:[V.isZip?a.jsx(Rs,{className:"w-4 h-4 text-blue-400 flex-shrink-0"}):a.jsx(eC,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm text-white truncate",children:V.file.name}),a.jsxs("p",{className:"text-xs text-bambu-gray",children:[(V.file.size/1024/1024).toFixed(2)," MB",V.isZip&&V.status==="pending"&&a.jsxs("span",{className:"text-blue-400 ml-2",children:["• ",r("fileManager.willBeExtracted")]}),V.extractedCount!==void 0&&a.jsxs("span",{className:"text-green-400 ml-2",children:["• ",r("fileManager.filesExtracted",{count:V.extractedCount})]})]})]}),V.status==="pending"&&a.jsx("button",{onClick:()=>T(q),className:"p-1 hover:bg-bambu-dark-tertiary rounded",children:a.jsx(Ft,{className:"w-4 h-4 text-bambu-gray"})}),V.status==="uploading"&&a.jsx(at,{className:"w-4 h-4 text-bambu-green animate-spin"}),V.status==="success"&&a.jsx(gr,{className:"w-4 h-4 text-green-500"}),V.status==="error"&&a.jsx("span",{title:V.error,children:a.jsx(ha,{className:"w-4 h-4 text-red-500"})})]},q))}),R&&a.jsx("div",{className:"p-3 bg-bambu-dark rounded-lg",children:a.jsxs("p",{className:"text-sm text-white",children:[r("fileManager.uploadComplete",{succeeded:L}),B>0&&a.jsxs("span",{className:"text-red-400",children:[", ",r("fileManager.uploadFailed",{count:B})]})]})})]}),a.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-end gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:e,children:r(R?"common.close":"common.cancel")}),!R&&a.jsx(Me,{onClick:P,disabled:U===0||c,children:c?a.jsxs(a.Fragment,{children:[a.jsx(at,{className:"w-4 h-4 mr-2 animate-spin"}),r("fileManager.uploading")]}):a.jsxs(a.Fragment,{children:[a.jsx(ia,{className:"w-4 h-4 mr-2"}),r("common.upload")," ",U>0?`(${U})`:""]})})]})]})})}function Xae({folder:t,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:s,depth:o=0,wrapNames:l=!1,hasPermission:c,t:u}){const[d,h]=w.useState(!0),[f,p]=w.useState(!1),x=t.children.length>0,v=t.project_id||t.archive_id;return a.jsxs("div",{children:[a.jsxs("div",{className:`group flex items-center gap-1 px-2 py-1.5 rounded cursor-pointer transition-colors ${e===t.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${8+o*12}px`},onClick:()=>n(t.id),children:[x?a.jsx("button",{onClick:b=>{b.stopPropagation(),h(!d)},className:"p-0.5 hover:bg-bambu-dark-tertiary rounded",children:a.jsx(fa,{className:`w-3.5 h-3.5 transition-transform ${d?"rotate-90":""}`})}):a.jsx("div",{className:"w-4.5"}),a.jsx(Xl,{className:"w-4 h-4 text-bambu-green flex-shrink-0"}),a.jsx("span",{className:`text-sm flex-1 min-w-0 ${l?"break-all":"truncate"}`,title:t.name,children:t.name}),v&&a.jsxs("button",{onClick:b=>{b.stopPropagation(),i(t)},className:"flex-shrink-0 flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-blue-500/20 text-blue-400 hover:bg-blue-500/30 transition-colors",title:`${t.project_name?`Project: ${t.project_name}`:`Archive: ${t.archive_name}`} (click to change)`,children:[a.jsx(jh,{className:"w-3 h-3"}),t.project_name?a.jsx(oX,{className:"w-3 h-3"}):a.jsx(Rs,{className:"w-3 h-3"})]}),t.file_count>0&&a.jsx("span",{className:"flex-shrink-0 text-xs text-bambu-gray",children:t.file_count}),!v&&a.jsx("button",{onClick:b=>{b.stopPropagation(),i(t)},className:"flex-shrink-0 p-1 rounded hover:bg-bambu-dark-tertiary",title:u("fileManager.linkToProjectOrArchive"),children:a.jsx(jh,{className:"w-3.5 h-3.5 text-bambu-gray hover:text-bambu-green"})}),a.jsx("div",{className:`flex-shrink-0 flex items-center gap-0.5 transition-opacity ${l?"":"opacity-0 group-hover:opacity-100"}`,onClick:b=>b.stopPropagation(),children:a.jsxs("div",{className:"relative",children:[a.jsx("button",{onClick:()=>p(!f),className:"p-1 rounded hover:bg-bambu-dark-tertiary",children:a.jsx(Ah,{className:"w-3.5 h-3.5 text-bambu-gray"})}),f&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>p(!1)}),a.jsxs("div",{className:"absolute right-0 top-full mt-1 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]",children:[a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${c("library:update_all")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{c("library:update_all")&&(s(t),p(!1))},disabled:!c("library:update_all"),title:c("library:update_all")?void 0:u("fileManager.noPermissionRenameFolder"),children:[a.jsx($i,{className:"w-3.5 h-3.5"}),u("common.rename")]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${c("library:update_all")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{c("library:update_all")&&(i(t),p(!1))},disabled:!c("library:update_all"),title:c("library:update_all")?void 0:u("fileManager.noPermissionLinkFolder"),children:[a.jsx(jh,{className:"w-3.5 h-3.5"}),u(v?"fileManager.changeLink":"fileManager.linkTo")]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${c("library:delete_all")?"text-red-400 hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{c("library:delete_all")&&(r(t.id),p(!1))},disabled:!c("library:delete_all"),title:c("library:delete_all")?void 0:u("fileManager.noPermissionDeleteFolder"),children:[a.jsx(en,{className:"w-3.5 h-3.5"}),u("common.delete")]})]})]})]})})]}),x&&d&&a.jsx("div",{children:t.children.map(b=>a.jsx(Xae,{folder:b,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:s,depth:o+1,wrapNames:l,hasPermission:c,t:u},b.id))})]})}function fF(t){const e=t.toLowerCase();return e.endsWith(".gcode")||e.includes(".gcode.")}function ZZe({file:t,isSelected:e,isMobile:n,onSelect:r,onDelete:i,onDownload:s,onAddToQueue:o,onPrint:l,onPreview3d:c,onRename:u,onGenerateThumbnail:d,thumbnailVersion:h,hasPermission:f,canModify:p,authEnabled:x,t:v}){const[b,y]=w.useState(!1);return a.jsxs("div",{className:`group relative bg-bambu-dark-secondary rounded-lg border transition-all cursor-pointer overflow-hidden ${e?"border-bambu-green ring-1 ring-bambu-green":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,onClick:()=>r(t.id),children:[a.jsxs("div",{className:"aspect-square bg-bambu-dark flex items-center justify-center overflow-hidden",children:[t.thumbnail_path?a.jsx("img",{src:`${se.getLibraryFileThumbnailUrl(t.id)}${h?`?v=${h}`:""}`,alt:t.filename,className:"w-full h-full object-cover"}):a.jsx(ff,{className:"w-12 h-12 text-bambu-gray/30"}),a.jsx("div",{className:`absolute top-2 right-2 text-xs px-1.5 py-0.5 rounded font-medium ${t.file_type==="3mf"?"bg-bambu-green/90 text-white":t.file_type==="gcode"?"bg-blue-500/90 text-white":t.file_type==="stl"?"bg-purple-500/90 text-white":"bg-bambu-gray/90 text-white"}`,children:t.file_type.toUpperCase()})]}),a.jsxs("div",{className:"p-3",children:[a.jsx("h3",{className:"text-sm font-medium text-white truncate",title:t.print_name||t.filename,children:t.print_name||t.filename}),a.jsxs("div",{className:"flex items-center gap-3 mt-1 text-xs text-bambu-gray",children:[a.jsx("span",{children:Bg(t.file_size)}),t.print_time_seconds&&a.jsxs("span",{className:"flex items-center gap-1",children:[a.jsx(Qn,{className:"w-3 h-3"}),qZe(t.print_time_seconds)]})]}),t.sliced_for_model&&a.jsxs("div",{className:"mt-1 text-xs text-bambu-gray flex items-center gap-1",children:[a.jsx(Cr,{className:"w-3 h-3"}),t.sliced_for_model]}),t.print_count>0&&a.jsx("div",{className:"mt-1 text-xs text-bambu-green",children:v("fileManager.printedCount",{count:t.print_count})}),x&&t.created_by_username&&a.jsxs("div",{className:"mt-1 text-xs text-bambu-gray flex items-center gap-1",children:[a.jsx(nd,{className:"w-3 h-3"}),t.created_by_username]})]}),a.jsxs("div",{className:`absolute bottom-2 right-2 transition-opacity ${n?"opacity-100":"opacity-0 group-hover:opacity-100"}`,onClick:S=>S.stopPropagation(),children:[a.jsx("button",{onClick:()=>y(!b),className:"p-1.5 rounded bg-bambu-dark-secondary/90 hover:bg-bambu-dark-tertiary",children:a.jsx(Ah,{className:"w-4 h-4 text-bambu-gray"})}),b&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>y(!1)}),a.jsxs("div",{className:"absolute right-0 bottom-8 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[140px]",children:[l&&fF(t.filename)&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("printers:control")?"text-bambu-green hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("printers:control")&&(l(t),y(!1))},disabled:!f("printers:control"),title:f("printers:control")?void 0:v("fileManager.noPermissionPrint"),children:[a.jsx(Cr,{className:"w-3.5 h-3.5"}),v("common.print")]}),o&&fF(t.filename)&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("queue:create")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("queue:create")&&(o(t.id),y(!1))},disabled:!f("queue:create"),title:f("queue:create")?void 0:v("fileManager.noPermissionAddToQueue"),children:[a.jsx(Qn,{className:"w-3.5 h-3.5"}),v("fileManager.schedulePrint")]}),c&&(t.file_type==="3mf"||t.file_type==="gcode"||t.file_type==="stl")&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("library:read")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("library:read")&&(c(t),y(!1))},disabled:!f("library:read"),title:f("library:read")?void 0:"You do not have permission to preview files",children:[a.jsx(Qi,{className:"w-3.5 h-3.5"}),"3D Preview"]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("library:read")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("library:read")&&(s(t.id),y(!1))},disabled:!f("library:read"),title:f("library:read")?void 0:v("fileManager.noPermissionDownload"),children:[a.jsx(di,{className:"w-3.5 h-3.5"}),v("common.download")]}),u&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${p("library","update",t.created_by_id)?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{p("library","update",t.created_by_id)&&(u(t),y(!1))},disabled:!p("library","update",t.created_by_id),title:p("library","update",t.created_by_id)?void 0:v("fileManager.noPermissionRenameFile"),children:[a.jsx($i,{className:"w-3.5 h-3.5"}),v("common.rename")]}),d&&t.file_type==="stl"&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${p("library","update",t.created_by_id)?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{p("library","update",t.created_by_id)&&(d(t),y(!1))},disabled:!p("library","update",t.created_by_id),title:p("library","update",t.created_by_id)?void 0:v("fileManager.noPermissionGenerateThumbnail"),children:[a.jsx(ed,{className:"w-3.5 h-3.5"}),v("fileManager.generateThumbnail")]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${p("library","delete",t.created_by_id)?"text-red-400 hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{p("library","delete",t.created_by_id)&&(i(t.id),y(!1))},disabled:!p("library","delete",t.created_by_id),title:p("library","delete",t.created_by_id)?void 0:v("fileManager.noPermissionDeleteFile"),children:[a.jsx(en,{className:"w-3.5 h-3.5"}),v("common.delete")]})]})]})]}),a.jsx("div",{className:`absolute top-2 left-2 w-5 h-5 rounded border-2 flex items-center justify-center transition-all ${e?"bg-bambu-green border-bambu-green":`border-white/30 bg-black/30 ${n?"opacity-100":"opacity-0 group-hover:opacity-100"}`}`,children:e&&a.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})})]})}function JZe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r,hasAnyPermission:i,canModify:s,authEnabled:o}=Ir(),[l]=NF(),c=l.get("folder"),u=c?parseInt(c,10):null,[d,h]=w.useState(u),[f,p]=w.useState([]),[x,v]=w.useState(!1),[b,y]=w.useState(!1),[S,N]=w.useState(!1),[A,M]=w.useState(null),[T,k]=w.useState(null),[j,E]=w.useState(null),[P,U]=w.useState(null),[L,B]=w.useState(null),[R,V]=w.useState(null),[q,I]=w.useState({}),[K,Y]=w.useState(null),[le,H]=w.useState(()=>localStorage.getItem("library-view-mode")||"grid"),[X,te]=w.useState(()=>localStorage.getItem("library-wrap-folders")==="true"),[J,Se]=w.useState(()=>{const Oe=localStorage.getItem("library-sidebar-width");return Oe?parseInt(Oe,10):256}),[fe,me]=w.useState(!1),De=w.useRef(null);w.useEffect(()=>{if(!fe)return;document.body.style.userSelect="none",document.body.style.cursor="col-resize";const Oe=vt=>{if(!De.current)return;const Bt=De.current.parentElement?.getBoundingClientRect();if(!Bt)return;const Tn=vt.clientX-Bt.left,fr=Math.min(500,Math.max(200,Tn));Se(fr)},xt=()=>{me(!1),document.body.style.userSelect="",document.body.style.cursor="",localStorage.setItem("library-sidebar-width",String(J))};return document.addEventListener("mousemove",Oe),document.addEventListener("mouseup",xt),()=>{document.removeEventListener("mousemove",Oe),document.removeEventListener("mouseup",xt),document.body.style.userSelect="",document.body.style.cursor=""}},[fe,J]);const[de,ce]=w.useState(""),[Ne,ie]=w.useState("all"),[Le,$]=w.useState(""),[W,ue]=w.useState(()=>localStorage.getItem("library-sort-field")||"name"),[Z,oe]=w.useState(()=>localStorage.getItem("library-sort-direction")||"asc"),Q=fne();w.useEffect(()=>{const Oe=l.get("folder");if(Oe){const xt=parseInt(Oe,10);h(xt)}},[l]);const{data:he}=tt({queryKey:["settings"],queryFn:()=>se.getSettings()}),{data:be,isLoading:ee}=tt({queryKey:["library-folders"],queryFn:()=>se.getLibraryFolders()}),{data:z,isLoading:D}=tt({queryKey:["library-files",d],queryFn:()=>se.getLibraryFiles(d,d===null)}),{data:G}=tt({queryKey:["library-stats"],queryFn:()=>se.getLibraryStats()}),{data:pe}=tt({queryKey:["users"],queryFn:()=>se.getUsers()}),ve=w.useMemo(()=>{if(!z)return[];const Oe=new Set(z.map(xt=>xt.file_type));return Array.from(Oe).sort()},[z]),xe=w.useMemo(()=>{if(!z)return[];let Oe=[...z];if(de.trim()){const xt=de.toLowerCase();Oe=Oe.filter(vt=>vt.filename.toLowerCase().includes(xt)||vt.print_name&&vt.print_name.toLowerCase().includes(xt))}if(Ne!=="all"&&(Oe=Oe.filter(xt=>xt.file_type===Ne)),Le.trim()){const xt=Le.toLowerCase();Oe=Oe.filter(vt=>vt.created_by_username&&vt.created_by_username.toLowerCase().includes(xt))}return Oe.sort((xt,vt)=>{let Bt=0;switch(W){case"name":Bt=(xt.print_name||xt.filename).localeCompare(vt.print_name||vt.filename);break;case"date":Bt=new Date(xt.created_at).getTime()-new Date(vt.created_at).getTime();break;case"size":Bt=xt.file_size-vt.file_size;break;case"type":Bt=xt.file_type.localeCompare(vt.file_type);break;case"prints":Bt=xt.print_count-vt.print_count;break}return Z==="asc"?Bt:-Bt}),Oe},[z,de,Ne,Le,W,Z]),ze=w.useMemo(()=>{if(!G||!he)return!1;const Oe=(he.library_disk_warning_gb||5)*1024*1024*1024;return G.disk_free_bytesse.createLibraryFolder(Oe),onSuccess:()=>{e.invalidateQueries({queryKey:["library-folders"]}),v(!1),n(t("fileManager.toast.folderCreated"),"success")},onError:Oe=>n(Oe.message,"error")}),He=lt({mutationFn:Oe=>se.deleteLibraryFolder(Oe),onSuccess:()=>{e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-stats"]}),d===T?.id&&h(null),k(null),n(t("fileManager.toast.folderDeleted"),"success")},onError:Oe=>{k(null),n(Oe.message,"error")}}),Ze=lt({mutationFn:Oe=>se.deleteLibraryFile(Oe),onSuccess:()=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-stats"]}),p(Oe=>Oe.filter(xt=>xt!==T?.id)),k(null),n(t("fileManager.toast.fileDeleted"),"success")},onError:Oe=>{k(null),n(Oe.message,"error")}}),we=lt({mutationFn:Oe=>se.bulkDeleteLibrary(Oe,[]),onSuccess:(Oe,xt)=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-stats"]}),n(t("fileManager.toast.filesDeleted",{count:xt.length}),"success"),p([]),k(null)},onError:Oe=>{k(null),n(Oe.message,"error")}}),ye=lt({mutationFn:({fileIds:Oe,folderId:xt})=>se.moveLibraryFiles(Oe,xt),onSuccess:()=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),p([]),y(!1),n(t("fileManager.toast.filesMoved"),"success")},onError:Oe=>n(Oe.message,"error")}),ke=lt({mutationFn:({id:Oe,data:xt})=>se.updateLibraryFolder(Oe,xt),onSuccess:(Oe,xt)=>{e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["project-folders"]}),e.invalidateQueries({queryKey:["archive-folders"]}),M(null);const vt=xt.data.project_id===0&&xt.data.archive_id===0;n(t(vt?"fileManager.toast.folderUnlinked":"fileManager.toast.folderLinked"),"success")},onError:Oe=>n(Oe.message,"error")}),Ue=lt({mutationFn:({id:Oe,filename:xt})=>se.updateLibraryFile(Oe,{filename:xt}),onSuccess:()=>{e.invalidateQueries({queryKey:["library-files"]}),V(null),n(t("fileManager.toast.fileRenamed"),"success")},onError:Oe=>{V(null),n(Oe.message,"error")}}),je=lt({mutationFn:({id:Oe,name:xt})=>se.updateLibraryFolder(Oe,{name:xt}),onSuccess:()=>{e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-files"]}),V(null),n(t("fileManager.toast.folderRenamed"),"success")},onError:Oe=>{V(null),n(Oe.message,"error")}}),Ve=lt({mutationFn:()=>se.batchGenerateStlThumbnails({all_missing:!0}),onSuccess:Oe=>{if(e.invalidateQueries({queryKey:["library-files"]}),Oe.succeeded>0){const xt=Date.now(),vt={};Oe.results.forEach(Bt=>{Bt.success&&(vt[Bt.file_id]=xt)}),I(Bt=>({...Bt,...vt}))}Oe.succeeded>0&&Oe.failed===0?n(t("fileManager.toast.thumbnailsGenerated",{count:Oe.succeeded}),"success"):Oe.succeeded>0&&Oe.failed>0?n(t("fileManager.toast.thumbnailsGeneratedPartial",{succeeded:Oe.succeeded,failed:Oe.failed}),"success"):Oe.processed===0?n(t("fileManager.toast.noStlMissingThumbnails"),"info"):n(t("fileManager.toast.failedToGenerateThumbnails",{error:Oe.results[0]?.error||"Unknown error"}),"error")},onError:Oe=>n(Oe.message,"error")}),Ae=lt({mutationFn:Oe=>se.batchGenerateStlThumbnails({file_ids:[Oe]}),onSuccess:Oe=>{if(e.invalidateQueries({queryKey:["library-files"]}),Oe.succeeded>0){const xt=Oe.results[0]?.file_id;xt&&I(vt=>({...vt,[xt]:Date.now()})),n(t("fileManager.toast.thumbnailGenerated"),"success")}else n(t("fileManager.toast.failedToGenerateThumbnail",{error:Oe.results[0]?.error||"Unknown error"}),"error")},onError:Oe=>n(Oe.message,"error")}),Je=w.useCallback(Oe=>{const xt=Oe.toLowerCase();return xt.endsWith(".gcode")||xt.includes(".gcode.")},[]),Pe=w.useMemo(()=>z?z.filter(Oe=>f.includes(Oe.id)&&Je(Oe.filename)):[],[z,f,Je]),Ye=w.useCallback(Oe=>{p(xt=>xt.includes(Oe)?xt.filter(vt=>vt!==Oe):[...xt,Oe])},[]),Re=w.useCallback(()=>{xe.length>0&&p(xe.map(Oe=>Oe.id))},[xe]),Fe=w.useCallback(()=>{p([])},[]),We=()=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-stats"]})},pt=Oe=>{se.downloadLibraryFile(Oe).catch(xt=>{console.error("Library file download failed:",xt)})},At=()=>{T&&(T.type==="file"?Ze.mutate(T.id):T.type==="folder"?He.mutate(T.id):T.type==="bulk"&&we.mutate(f))},Rt=He.isPending||Ze.isPending||we.isPending,Wt=Oe=>{H(Oe),localStorage.setItem("library-view-mode",Oe)},Tt=ee||D;return a.jsxs("div",{className:"p-4 md:p-8 min-h-[calc(100vh-64px)] lg:h-[calc(100vh-64px)] flex flex-col",children:[a.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6",children:[a.jsxs("div",{children:[a.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:a.jsx(Xl,{className:"w-6 h-6 text-bambu-green"})}),t("fileManager.title")]}),a.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:t("fileManager.subtitle")})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"flex items-center bg-bambu-dark rounded-lg p-1",children:[a.jsx("button",{onClick:()=>Wt("grid"),className:`p-1.5 rounded transition-colors ${le==="grid"?"bg-bambu-dark-secondary text-white":"text-bambu-gray hover:text-white"}`,title:t("fileManager.gridView"),children:a.jsx(xv,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>Wt("list"),className:`p-1.5 rounded transition-colors ${le==="list"?"bg-bambu-dark-secondary text-white":"text-bambu-gray hover:text-white"}`,title:t("fileManager.listView"),children:a.jsx(rC,{className:"w-4 h-4"})})]}),a.jsxs(Me,{variant:"secondary",onClick:()=>Ve.mutate(),disabled:Ve.isPending||!i("library:update_own","library:update_all"),title:i("library:update_own","library:update_all")?t("fileManager.generateThumbnailsForMissing"):t("fileManager.noPermissionGenerateThumbnail"),children:[Ve.isPending?a.jsx(at,{className:"w-4 h-4 mr-2 animate-spin"}):a.jsx(ed,{className:"w-4 h-4 mr-2"}),t("fileManager.generateThumbnails")]}),a.jsxs(Me,{variant:"secondary",onClick:()=>v(!0),disabled:!r("library:upload"),title:r("library:upload")?void 0:t("fileManager.noPermissionCreateFolder"),children:[a.jsx(Jhe,{className:"w-4 h-4 mr-2"}),t("fileManager.newFolder")]}),a.jsxs(Me,{onClick:()=>N(!0),disabled:!r("library:upload"),title:r("library:upload")?void 0:t("fileManager.noPermissionUpload"),children:[a.jsx(ia,{className:"w-4 h-4 mr-2"}),t("common.upload")]})]})]}),ze&&G&&he&&a.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-amber-500/10 border border-amber-500/30 rounded-lg",children:[a.jsx(Cn,{className:"w-5 h-5 text-amber-500 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-amber-500 font-medium",children:t("fileManager.lowDiskSpaceWarning")}),a.jsx("p",{className:"text-xs text-amber-500/80",children:t("fileManager.lowDiskSpaceDetails",{free:Bg(G.disk_free_bytes),total:Bg(G.disk_total_bytes),threshold:he.library_disk_warning_gb})})]})]}),G&&a.jsxs("div",{className:"flex flex-wrap items-center gap-3 sm:gap-6 mb-6 p-3 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(eC,{className:"w-4 h-4 text-bambu-green"}),a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.files"),":"]}),a.jsx("span",{className:"text-white font-medium",children:G.total_files})]}),a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(Xl,{className:"w-4 h-4 text-blue-400"}),a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.folders"),":"]}),a.jsx("span",{className:"text-white font-medium",children:G.total_folders})]}),a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(qb,{className:"w-4 h-4 text-amber-400"}),a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.size"),":"]}),a.jsx("span",{className:"text-white font-medium",children:Bg(G.total_size_bytes)})]}),a.jsxs("div",{className:"flex items-center gap-2 text-sm sm:ml-auto",children:[a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.free"),":"]}),a.jsx("span",{className:`font-medium ${ze?"text-amber-500":"text-white"}`,children:Bg(G.disk_free_bytes)})]})]}),a.jsxs("div",{className:"flex-1 flex flex-col lg:flex-row gap-4 lg:gap-6 min-h-0",children:[a.jsx("div",{className:"lg:hidden",children:a.jsxs("select",{value:d??"",onChange:Oe=>h(Oe.target.value?parseInt(Oe.target.value,10):null),className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg px-3 py-2.5 text-white focus:outline-none focus:border-bambu-green",children:[a.jsxs("option",{value:"",children:["📁 ",t("fileManager.allFiles")]}),be&&(()=>{const Oe=(xt,vt=0)=>{const Bt=[];for(const Tn of xt)Bt.push({id:Tn.id,name:Tn.name,fileCount:Tn.file_count,depth:vt}),Tn.children.length>0&&Bt.push(...Oe(Tn.children,vt+1));return Bt};return Oe(be).map(xt=>a.jsxs("option",{value:xt.id,children:["│ ".repeat(xt.depth),"📂 ",xt.name," ",xt.fileCount>0?`(${xt.fileCount})`:""]},xt.id))})()]})}),a.jsxs("div",{ref:De,className:"hidden lg:flex flex-shrink-0 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary overflow-hidden flex-col relative",style:{width:`${J}px`},children:[a.jsx("div",{className:`absolute right-0 top-0 bottom-0 w-1.5 cursor-col-resize z-10 group/resize flex items-center justify-center transition-colors ${fe?"bg-bambu-green":"hover:bg-bambu-green/50"}`,onMouseDown:Oe=>{Oe.preventDefault(),me(!0)},onDoubleClick:()=>{Se(256),localStorage.setItem("library-sidebar-width","256")},title:t("fileManager.dragToResizeTooltip"),children:a.jsxs("div",{className:`flex flex-col gap-1 opacity-0 group-hover/resize:opacity-100 transition-opacity ${fe?"opacity-100":""}`,children:[a.jsx("div",{className:"w-0.5 h-0.5 rounded-full bg-white/70"}),a.jsx("div",{className:"w-0.5 h-0.5 rounded-full bg-white/70"}),a.jsx("div",{className:"w-0.5 h-0.5 rounded-full bg-white/70"})]})}),a.jsxs("div",{className:"p-3 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[a.jsx("h2",{className:"text-sm font-medium text-white",children:t("fileManager.folders")}),a.jsx("button",{onClick:()=>{const Oe=!X;te(Oe),localStorage.setItem("library-wrap-folders",String(Oe))},className:`text-xs px-1.5 py-0.5 rounded transition-colors ${X?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:text-white hover:bg-bambu-dark"}`,title:t(X?"fileManager.disableTextWrapping":"fileManager.enableTextWrapping"),children:t("fileManager.wrap")})]}),a.jsxs("div",{className:"flex-1 overflow-y-auto p-2",children:[a.jsxs("div",{className:`flex items-center gap-2 px-2 py-1.5 rounded cursor-pointer transition-colors ${d===null?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,onClick:()=>h(null),children:[a.jsx(ff,{className:"w-4 h-4"}),a.jsx("span",{className:"text-sm",children:t("fileManager.allFiles")})]}),be?.map(Oe=>a.jsx(Xae,{folder:Oe,selectedFolderId:d,onSelect:h,onDelete:xt=>k({type:"folder",id:xt}),onLink:M,onRename:xt=>V({type:"folder",id:xt.id,name:xt.name}),wrapNames:X,hasPermission:r,t},Oe.id))]})]}),a.jsxs("div",{className:"flex-1 flex flex-col min-w-0 min-h-0",children:[z&&z.length>0&&a.jsxs("div",{className:"flex flex-wrap items-center gap-2 sm:gap-3 mb-4 p-2 sm:p-3 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary sticky top-0 z-10 lg:static",children:[a.jsxs("div",{className:"relative w-full sm:w-auto sm:flex-1 sm:max-w-xs",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",placeholder:t("fileManager.searchFiles"),value:de,onChange:Oe=>ce(Oe.target.value),className:"w-full pl-9 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green"})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(tC,{className:"w-4 h-4 text-bambu-gray hidden sm:block"}),a.jsxs("select",{value:Ne,onChange:Oe=>ie(Oe.target.value),className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[a.jsx("option",{value:"all",children:t("fileManager.allTypes")}),ve.map(Oe=>a.jsx("option",{value:Oe,children:Oe.toUpperCase()},Oe))]})]}),o&&a.jsxs("div",{className:"relative",children:[a.jsx("input",{type:"text",placeholder:t("fileManager.filterByUser",{defaultValue:"Filter by user"}),value:Le,onChange:Oe=>$(Oe.target.value),list:"usernames-list",className:`w-32 sm:w-40 px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green ${Le?"pr-7":""}`,style:Le?{WebkitAppearance:"none",MozAppearance:"textfield"}:void 0}),Le&&a.jsx("button",{onClick:()=>$(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white z-10",children:a.jsx(Ft,{className:"w-3 h-3"})}),a.jsx("datalist",{id:"usernames-list",children:pe?.map(Oe=>a.jsx("option",{value:Oe.username},Oe.id))})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("select",{value:W,onChange:Oe=>{const xt=Oe.target.value;ue(xt),localStorage.setItem("library-sort-field",xt)},className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[a.jsx("option",{value:"name",children:t("common.name")}),a.jsx("option",{value:"date",children:t("common.date")}),a.jsx("option",{value:"size",children:t("fileManager.size")}),a.jsx("option",{value:"type",children:t("common.type")}),a.jsx("option",{value:"prints",children:t("fileManager.prints")})]}),a.jsx("button",{onClick:()=>oe(Oe=>{const xt=Oe==="asc"?"desc":"asc";return localStorage.setItem("library-sort-direction",xt),xt}),className:"p-1.5 rounded bg-bambu-dark border border-bambu-dark-tertiary hover:border-bambu-green transition-colors",title:t(Z==="asc"?"fileManager.ascending":"fileManager.descending"),children:Z==="asc"?a.jsx(yde,{className:"w-4 h-4 text-white"}):a.jsx(dde,{className:"w-4 h-4 text-white"})})]}),(de||Ne!=="all"||Le)&&a.jsx("span",{className:"text-sm text-bambu-gray hidden sm:inline",children:t("fileManager.resultsCount",{showing:xe.length,total:z.length})})]}),xe.length>0&&a.jsxs("div",{className:"flex flex-wrap items-center gap-2 mb-4 p-2 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary sticky top-[52px] z-10 lg:static",children:[f.length===xe.length&&f.length>0?a.jsxs(Me,{variant:"secondary",size:"sm",onClick:Fe,children:[a.jsx(gl,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("fileManager.deselectAll")})]}):a.jsxs(Me,{variant:"secondary",size:"sm",onClick:Re,children:[a.jsx(ao,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("fileManager.selectAll")})]}),f.length>0&&a.jsxs(a.Fragment,{children:[a.jsx("span",{className:"text-sm text-bambu-gray ml-2",children:t("fileManager.selected",{count:f.length})}),a.jsx("div",{className:"hidden sm:block flex-1"}),a.jsxs("div",{className:"w-full sm:w-auto flex flex-wrap items-center gap-2 mt-2 sm:mt-0",children:[Pe.length===1&&a.jsxs(Me,{variant:"primary",size:"sm",onClick:()=>U(Pe[0]),disabled:!r("printers:control"),title:r("printers:control")?void 0:t("fileManager.noPermissionPrint"),children:[a.jsx(jo,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.print")})]}),Pe.length===1&&a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>B(Pe[0]),disabled:!r("queue:create"),title:r("queue:create")?void 0:t("fileManager.noPermissionAddToQueue"),children:[a.jsx(Qn,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("fileManager.schedulePrint")})]}),a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>y(!0),disabled:!i("library:update_own","library:update_all"),title:i("library:update_own","library:update_all")?void 0:t("fileManager.noPermissionMoveFiles"),children:[a.jsx(sfe,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.move")})]}),a.jsxs(Me,{variant:"danger",size:"sm",onClick:()=>{f.length===1?k({type:"file",id:f[0]}):k({type:"bulk",id:0,count:f.length})},disabled:!i("library:delete_own","library:delete_all"),title:i("library:delete_own","library:delete_all")?void 0:t("fileManager.noPermissionDeleteFiles"),children:[a.jsx(en,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.delete")})]}),a.jsxs(Me,{variant:"secondary",size:"sm",onClick:Fe,children:[a.jsx(Ft,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.clear")})]})]})]})]}),Tt?a.jsx("div",{className:"flex-1 flex items-center justify-center",children:a.jsxs("div",{className:"flex flex-col items-center gap-3",children:[a.jsx(at,{className:"w-8 h-8 animate-spin text-bambu-green"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:t("fileManager.loadingFiles")})]})}):z?.length===0?a.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[a.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:a.jsx(ff,{className:"w-12 h-12 text-bambu-gray/50"})}),a.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:t(d!==null?"fileManager.folderIsEmpty":"fileManager.noFilesYet")}),a.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:t(d!==null?"fileManager.folderEmptyDescription":"fileManager.noFilesDescription")}),a.jsxs(Me,{onClick:()=>N(!0),disabled:!r("library:upload"),title:r("library:upload")?void 0:t("fileManager.noPermissionUpload"),children:[a.jsx(ar,{className:"w-4 h-4 mr-2"}),t("fileManager.uploadFiles")]})]}):xe.length===0?a.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[a.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:a.jsx(Or,{className:"w-12 h-12 text-bambu-gray/50"})}),a.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:t("fileManager.noMatchingFiles")}),a.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:t("fileManager.noMatchingFilesDescription")}),a.jsx(Me,{variant:"secondary",onClick:()=>{ce(""),ie("all")},children:t("fileManager.clearFilters")})]}):le==="grid"?a.jsx("div",{className:"flex-1 lg:overflow-y-auto",children:a.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6 gap-4",children:xe.map(Oe=>a.jsx(ZZe,{file:Oe,isSelected:f.includes(Oe.id),isMobile:Q,t,onSelect:Ye,onDelete:xt=>k({type:"file",id:xt}),onDownload:pt,onAddToQueue:xt=>{const vt=z?.find(Bt=>Bt.id===xt);vt&&B(vt)},onPrint:E,onPreview3d:Y,onRename:xt=>V({type:"file",id:xt.id,name:xt.filename}),onGenerateThumbnail:xt=>Ae.mutate(xt.id),thumbnailVersion:q[Oe.id],hasPermission:r,canModify:s,authEnabled:o},Oe.id))})}):a.jsx("div",{className:"flex-1 lg:overflow-y-auto",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[a.jsxs("div",{className:`hidden sm:grid ${o?"grid-cols-[auto_1fr_120px_100px_100px_100px_80px]":"grid-cols-[auto_1fr_100px_100px_100px_80px]"} gap-4 px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary text-xs text-bambu-gray font-medium`,children:[a.jsx("div",{className:"w-6"}),a.jsx("div",{children:t("common.name")}),o&&a.jsx("div",{children:t("fileManager.uploadedBy",{defaultValue:"Uploaded By"})}),a.jsx("div",{children:t("common.type")}),a.jsx("div",{children:t("fileManager.size")}),a.jsx("div",{children:t("fileManager.prints")}),a.jsx("div",{})]}),xe.map(Oe=>a.jsxs("div",{className:`grid ${o?"grid-cols-[auto_1fr_120px_100px_100px_100px_80px]":"grid-cols-[auto_1fr_100px_100px_100px_80px]"} gap-4 px-4 py-3 items-center border-b border-bambu-dark-tertiary last:border-b-0 cursor-pointer hover:bg-bambu-dark/50 transition-colors ${f.includes(Oe.id)?"bg-bambu-green/10":""}`,onClick:()=>Ye(Oe.id),children:[a.jsx("div",{className:`w-5 h-5 rounded border-2 flex items-center justify-center ${f.includes(Oe.id)?"bg-bambu-green border-bambu-green":"border-bambu-gray/50"}`,children:f.includes(Oe.id)&&a.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})}),a.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[a.jsxs("div",{className:"relative group/thumb",children:[a.jsx("div",{className:"w-10 h-10 rounded bg-bambu-dark flex-shrink-0 overflow-hidden",children:Oe.thumbnail_path?a.jsx("img",{src:`${se.getLibraryFileThumbnailUrl(Oe.id)}${q[Oe.id]?`?v=${q[Oe.id]}`:""}`,alt:"",className:"w-full h-full object-cover"}):a.jsx("div",{className:"w-full h-full flex items-center justify-center",children:a.jsx(ff,{className:"w-5 h-5 text-bambu-gray/50"})})}),Oe.thumbnail_path&&a.jsx("div",{className:"absolute left-0 top-full mt-2 z-50 hidden group-hover/thumb:block",children:a.jsx("div",{className:"w-48 h-48 rounded-lg bg-bambu-dark-secondary border border-bambu-dark-tertiary shadow-xl overflow-hidden",children:a.jsx("img",{src:`${se.getLibraryFileThumbnailUrl(Oe.id)}${q[Oe.id]?`?v=${q[Oe.id]}`:""}`,alt:Oe.filename,className:"w-full h-full object-contain"})})})]}),a.jsx("div",{className:"min-w-0",children:a.jsx("div",{className:"text-sm text-white truncate",children:Oe.print_name||Oe.filename})})]}),o&&a.jsx("div",{className:"text-sm text-bambu-gray flex items-center gap-1",children:Oe.created_by_username?a.jsxs(a.Fragment,{children:[a.jsx(nd,{className:"w-3 h-3"}),a.jsx("span",{className:"truncate",children:Oe.created_by_username})]}):"-"}),a.jsx("div",{children:a.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded font-medium ${Oe.file_type==="3mf"?"bg-bambu-green/20 text-bambu-green":Oe.file_type==="gcode"?"bg-blue-500/20 text-blue-400":Oe.file_type==="stl"?"bg-purple-500/20 text-purple-400":"bg-bambu-gray/20 text-bambu-gray"}`,children:Oe.file_type.toUpperCase()})}),a.jsx("div",{className:"text-sm text-bambu-gray",children:Bg(Oe.file_size)}),a.jsx("div",{className:"text-sm text-bambu-gray",children:Oe.print_count>0?`${Oe.print_count}x`:"-"}),a.jsxs("div",{className:"flex items-center gap-1",onClick:xt=>xt.stopPropagation(),children:[fF(Oe.filename)&&a.jsxs(a.Fragment,{children:[a.jsx("button",{onClick:()=>r("printers:control")&&E(Oe),className:`p-1.5 rounded transition-colors ${r("printers:control")?"hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("printers:control")?t("common.print"):t("fileManager.noPermissionPrint"),disabled:!r("printers:control"),children:a.jsx(Cr,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>{r("queue:create")&&B(Oe)},className:`p-1.5 rounded transition-colors ${r("queue:create")?"hover:bg-bambu-dark text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("queue:create")?t("fileManager.schedulePrint"):t("fileManager.noPermissionAddToQueue"),disabled:!r("queue:create"),children:a.jsx(Qn,{className:"w-4 h-4"})})]}),(Oe.file_type==="3mf"||Oe.file_type==="gcode"||Oe.file_type==="stl")&&a.jsx("button",{onClick:()=>r("library:read")&&Y(Oe),className:`p-1.5 rounded transition-colors ${r("library:read")?"hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("library:read")?"3D Preview":"You do not have permission to preview files",disabled:!r("library:read"),children:a.jsx(Qi,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>r("library:read")&&pt(Oe.id),className:`p-1.5 rounded transition-colors ${r("library:read")?"hover:bg-bambu-dark text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("library:read")?t("common.download"):t("fileManager.noPermissionDownload"),disabled:!r("library:read"),children:a.jsx(di,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>s("library","update",Oe.created_by_id)&&V({type:"file",id:Oe.id,name:Oe.filename}),className:`p-1.5 rounded transition-colors ${s("library","update",Oe.created_by_id)?"hover:bg-bambu-dark text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("library","update",Oe.created_by_id)?t("common.rename"):t("fileManager.noPermissionRenameFile"),disabled:!s("library","update",Oe.created_by_id),children:a.jsx($i,{className:"w-4 h-4"})}),Oe.file_type==="stl"&&a.jsx("button",{onClick:()=>s("library","update",Oe.created_by_id)&&Ae.mutate(Oe.id),className:`p-1.5 rounded transition-colors ${s("library","update",Oe.created_by_id)?"hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("library","update",Oe.created_by_id)?t("fileManager.generateThumbnail"):t("fileManager.noPermissionGenerateThumbnail"),disabled:Ae.isPending||!s("library","update",Oe.created_by_id),children:a.jsx(ed,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>s("library","delete",Oe.created_by_id)&&k({type:"file",id:Oe.id}),className:`p-1.5 rounded transition-colors ${s("library","delete",Oe.created_by_id)?"hover:bg-bambu-dark text-bambu-gray hover:text-red-400":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("library","delete",Oe.created_by_id)?t("common.delete"):t("fileManager.noPermissionDeleteFile"),disabled:!s("library","delete",Oe.created_by_id),children:a.jsx(en,{className:"w-4 h-4"})})]})]},Oe.id))]})})]})]}),x&&a.jsx(WZe,{parentId:d,onClose:()=>v(!1),onSave:Oe=>_e.mutate(Oe),isLoading:_e.isPending,t}),b&&be&&a.jsx(XZe,{folders:be,selectedFiles:f,currentFolderId:d,onClose:()=>y(!1),onMove:Oe=>ye.mutate({fileIds:f,folderId:Oe}),isLoading:ye.isPending,t}),S&&a.jsx(QZe,{folderId:d,onClose:()=>N(!1),onUploadComplete:We,t}),A&&a.jsx(YZe,{folder:A,onClose:()=>M(null),onLink:Oe=>ke.mutate({id:A.id,data:Oe}),isLoading:ke.isPending,t}),T&&a.jsx(kn,{title:T.type==="folder"?t("fileManager.deleteFolder"):T.type==="bulk"?t("fileManager.deleteFilesCount",{count:T.count}):t("fileManager.deleteFile"),message:T.type==="folder"?t("fileManager.deleteFolderConfirm"):T.type==="bulk"?t("fileManager.deleteFilesConfirm",{count:T.count}):t("fileManager.deleteFileConfirm"),confirmText:t("common.delete"),variant:"danger",isLoading:Rt,loadingText:t("fileManager.deleting"),onConfirm:At,onCancel:()=>k(null)}),j&&a.jsx(Yu,{mode:"reprint",libraryFileId:j.id,archiveName:j.print_name||j.filename,onClose:()=>E(null),onSuccess:()=>{E(null),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["archives"]})}}),P&&a.jsx(Yu,{mode:"reprint",libraryFileId:P.id,archiveName:P.print_name||P.filename,onClose:()=>U(null),onSuccess:()=>{U(null),p([]),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["archives"]})}}),L&&a.jsx(Yu,{mode:"add-to-queue",libraryFileId:L.id,archiveName:L.print_name||L.filename,onClose:()=>B(null),onSuccess:()=>{B(null),p([]),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["queue"]}),e.invalidateQueries({queryKey:["archives"]})}}),K&&a.jsx(S4,{libraryFileId:K.id,title:K.print_name||K.filename,fileType:K.file_type,onClose:()=>Y(null)}),R&&a.jsx(KZe,{type:R.type,currentName:R.name,onClose:()=>V(null),onSave:Oe=>{R.type==="file"?Ue.mutate({id:R.id,filename:Oe}):je.mutate({id:R.id,name:Oe})},isLoading:Ue.isPending||je.isPending,t})]})}const qj=5,eJe=2e3,tJe=3e4,nJe=5e3;function rJe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r}=Ir(),{printerId:i}=Yk(),s=parseInt(i||"0",10),[o,l]=w.useState("stream"),[c,u]=w.useState(!1),[d,h]=w.useState(!1),[f,p]=w.useState(!0),[x,v]=w.useState(Date.now()),[b,y]=w.useState(!1),[S,N]=w.useState(!1),[A,M]=w.useState(0),[T,k]=w.useState(!1),[j,E]=w.useState(0),[P,U]=w.useState(1),[L,B]=w.useState({x:0,y:0}),[R,V]=w.useState(!1),[q,I]=w.useState({x:0,y:0}),[K,Y]=w.useState(null),[le,H]=w.useState(null),X=w.useRef(null),te=w.useRef(null),J=w.useRef(null),Se=w.useRef(null),fe=w.useRef(null),{data:me}=tt({queryKey:["printer",s],queryFn:()=>se.getPrinter(s),enabled:s>0}),{data:De}=tt({queryKey:["printerStatus",s],queryFn:()=>se.getPrinterStatus(s),refetchInterval:3e4,enabled:s>0}),de=lt({mutationFn:ye=>se.setChamberLight(s,ye),onMutate:async ye=>{await e.cancelQueries({queryKey:["printerStatus",s]});const ke=e.getQueryData(["printerStatus",s]);return e.setQueryData(["printerStatus",s],Ue=>({...Ue,chamber_light:ye})),{previousStatus:ke}},onSuccess:(ye,ke)=>{n(`Chamber light ${ke?"on":"off"}`)},onError:(ye,ke,Ue)=>{Ue?.previousStatus&&e.setQueryData(["printerStatus",s],Ue.previousStatus),n(ye.message||t("printers.toast.failedToControlChamberLight"),"error")}}),ce=(De?.state==="RUNNING"||De?.state==="PAUSE"||De?.state==="PAUSED")&&(De?.printable_objects_count??0)>=2;w.useEffect(()=>(me&&(document.title=`${me.name} - Camera`),()=>{document.title="Bambuddy"}),[me]);const Ne=w.useRef(!1);w.useEffect(()=>{const ye=`/api/v1/printers/${s}/camera/stop`;Ne.current=!1;const ke=()=>{if(s>0&&!Ne.current){Ne.current=!0;const Ve={},Ae=rd();Ae&&(Ve.Authorization=`Bearer ${Ae}`),fetch(ye,{method:"POST",keepalive:!0,headers:Ve}).catch(()=>{})}},Ue=()=>{ke()};window.addEventListener("beforeunload",Ue);const je=X.current;return()=>{window.removeEventListener("beforeunload",Ue),je&&(je.src=""),ke()}},[s]),w.useEffect(()=>{if(f&&!b){const ke=setTimeout(()=>{p(!1)},o==="stream"?3e3:2e4);return()=>clearTimeout(ke)}},[o,f,x,b]),w.useEffect(()=>{const ye=()=>{const ke=!!document.fullscreenElement;N(ke),U(1),B({x:0,y:0}),o==="stream"&&!b&&(X.current&&(X.current.src=""),setTimeout(()=>{p(!0),v(Date.now())},200))};return document.addEventListener("fullscreenchange",ye),()=>document.removeEventListener("fullscreenchange",ye)},[o,b]),w.useEffect(()=>{let ye;const ke=()=>{clearTimeout(ye),ye=setTimeout(()=>{localStorage.setItem("cameraWindowState",JSON.stringify({width:window.outerWidth,height:window.outerHeight,left:window.screenX,top:window.screenY}))},500)};return window.addEventListener("resize",ke),()=>{clearTimeout(ye),window.removeEventListener("resize",ke)}},[]),w.useEffect(()=>()=>{J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),fe.current&&clearInterval(fe.current)},[]);const ie=w.useCallback(()=>{if(A>=qj){k(!1),h(!0);return}const ye=Math.min(eJe*Math.pow(2,A),tJe);k(!0),E(Math.ceil(ye/1e3)),Se.current=setInterval(()=>{E(ke=>ke<=1?(Se.current&&clearInterval(Se.current),0):ke-1)},1e3),J.current=setTimeout(()=>{M(ke=>ke+1),k(!1),p(!0),h(!1),X.current&&(X.current.src=""),v(Date.now())},ye)},[A]);w.useEffect(()=>{if(o!=="stream"||f||T||b){fe.current&&(clearInterval(fe.current),fe.current=null);return}return fe.current=setInterval(async()=>{try{const ye=await se.getCameraStatus(s);(ye.stalled||!ye.active&&!d)&&(console.log(`Stream issue detected: stalled=${ye.stalled}, active=${ye.active}, reconnecting...`),fe.current&&(clearInterval(fe.current),fe.current=null),p(!1),ie())}catch{}},nJe),()=>{fe.current&&(clearInterval(fe.current),fe.current=null)}},[o,f,d,T,b,s,ie]);const Le=()=>{p(!1),o==="stream"&&A{if(p(!1),h(!1),M(0),k(!1),J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),X.current&&!localStorage.getItem("cameraWindowState")){const ye=X.current,ke=ye.naturalWidth,Ue=ye.naturalHeight;if(ke>0&&Ue>0){const Ae=window.outerWidth-window.innerWidth,Je=window.outerHeight-window.innerHeight,Pe=ke+16+Ae,Ye=Ue+45+16+Je;try{window.resizeTo(Pe,Ye)}catch{}}}},W=()=>{if(s>0){const ye={},ke=rd();ke&&(ye.Authorization=`Bearer ${ke}`),fetch(`/api/v1/printers/${s}/camera/stop`,{method:"POST",headers:ye}).catch(()=>{})}},ue=ye=>{o===ye||b||(y(!0),p(!0),h(!1),M(0),k(!1),U(1),B({x:0,y:0}),J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),X.current&&(X.current.src=""),o==="stream"&&W(),setTimeout(()=>{l(ye),v(Date.now()),y(!1)},100))},Z=()=>{b||(y(!0),p(!0),h(!1),M(0),k(!1),J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),X.current&&(X.current.src=""),o==="stream"&&W(),setTimeout(()=>{v(Date.now()),y(!1)},100))},oe=()=>{te.current&&(document.fullscreenElement?document.exitFullscreen():te.current.requestFullscreen())},Q=()=>{U(ye=>Math.min(ye+.5,4))},he=()=>{U(ye=>{const ke=Math.max(ye-.5,1);return ke===1&&B({x:0,y:0}),ke})},be=ye=>{ye.preventDefault(),ye.deltaY<0?Q():he()},ee=ye=>{P>1&&(ye.preventDefault(),V(!0),I({x:ye.clientX-L.x,y:ye.clientY-L.y}))},z=w.useCallback(()=>{if(!te.current)return{x:300,y:200};const ye=te.current.getBoundingClientRect(),ke=ye.width*(P-1)/2,Ue=ye.height*(P-1)/2;return{x:Math.max(50,ke),y:Math.max(50,Ue)}},[P]),D=ye=>{if(R&&P>1){const ke=ye.clientX-q.x,Ue=ye.clientY-q.y,je=z();B({x:Math.max(-je.x,Math.min(je.x,ke)),y:Math.max(-je.y,Math.min(je.y,Ue))})}},G=()=>{V(!1)},pe=ye=>{if(ye.length<2)return 0;const ke=ye[0].clientX-ye[1].clientX,Ue=ye[0].clientY-ye[1].clientY;return Math.sqrt(ke*ke+Ue*Ue)},ve=ye=>ye.length<2?{x:ye[0].clientX,y:ye[0].clientY}:{x:(ye[0].clientX+ye[1].clientX)/2,y:(ye[0].clientY+ye[1].clientY)/2},xe=ye=>{ye.touches.length===2?(ye.preventDefault(),Y(pe(ye.touches)),H(ve(ye.touches))):ye.touches.length===1&&P>1&&(ye.preventDefault(),V(!0),I({x:ye.touches[0].clientX-L.x,y:ye.touches[0].clientY-L.y}))},ze=ye=>{if(ye.touches.length===2&&K!==null){ye.preventDefault();const ke=pe(ye.touches),Ue=ke/K;U(Ve=>{const Ae=Math.max(1,Math.min(4,Ve*Ue));return Ae===1&&B({x:0,y:0}),Ae}),Y(ke);const je=ve(ye.touches);if(le){const Ve=z();B(Ae=>({x:Math.max(-Ve.x,Math.min(Ve.x,Ae.x+(je.x-le.x))),y:Math.max(-Ve.y,Math.min(Ve.y,Ae.y+(je.y-le.y)))}))}H(je)}else if(ye.touches.length===1&&R&&P>1){ye.preventDefault();const ke=ye.touches[0].clientX-q.x,Ue=ye.touches[0].clientY-q.y,je=z();B({x:Math.max(-je.x,Math.min(je.x,ke)),y:Math.max(-je.y,Math.min(je.y,Ue))})}},_e=ye=>{ye.touches.length<2&&(Y(null),H(null)),ye.touches.length===0&&V(!1)},He=()=>{U(1),B({x:0,y:0})},Ze=b?"":o==="stream"?`/api/v1/printers/${s}/camera/stream?fps=15&t=${x}`:`/api/v1/printers/${s}/camera/snapshot?t=${x}`,we=f||b||T;return s?a.jsxs("div",{ref:te,className:"min-h-screen bg-black flex flex-col",children:[a.jsxs("div",{className:"flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary",children:[a.jsxs("h1",{className:"text-sm font-medium text-white flex items-center gap-2",children:[a.jsx(mb,{className:"w-4 h-4"}),me?.name||`Printer ${s}`]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"flex bg-bambu-dark rounded p-0.5",children:[a.jsx("button",{onClick:()=>ue("stream"),disabled:we,className:`px-3 py-1 text-xs rounded transition-colors ${o==="stream"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:t("camera.live")}),a.jsx("button",{onClick:()=>ue("snapshot"),disabled:we,className:`px-3 py-1 text-xs rounded transition-colors ${o==="snapshot"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:t("camera.snapshot")})]}),a.jsx("button",{onClick:()=>de.mutate(!De?.chamber_light),disabled:!De?.connected||de.isPending||!r("printers:control"),className:`p-1.5 rounded disabled:opacity-50 ${De?.chamber_light?"bg-yellow-500/20 hover:bg-yellow-500/30":"hover:bg-bambu-dark-tertiary"}`,title:r("printers:control")?t("camera.chamberLight"):t("printers.permission.noControl"),children:a.jsx(bL,{on:De?.chamber_light??!1,className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>u(!0),disabled:!ce||!r("printers:control"),className:`p-1.5 rounded disabled:opacity-50 ${ce&&r("printers:control")?"hover:bg-bambu-dark-tertiary":""}`,title:r("printers:control")?t(ce?"printers.skipObjects.tooltip":"printers.skipObjects.onlyWhilePrinting"):t("printers.permission.noControl"),children:a.jsx(_C,{className:"w-4 h-4 text-bambu-gray"})}),a.jsx("button",{onClick:Z,disabled:we,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50",title:t(o==="stream"?"camera.restartStream":"camera.refreshSnapshot"),children:a.jsx(kr,{className:`w-4 h-4 text-bambu-gray ${we?"animate-spin":""}`})}),a.jsx("button",{onClick:oe,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded",title:t(S?"camera.exitFullscreen":"camera.fullscreen"),children:S?a.jsx(pX,{className:"w-4 h-4 text-bambu-gray"}):a.jsx(Kme,{className:"w-4 h-4 text-bambu-gray"})})]})]}),a.jsx("div",{className:"flex-1 flex items-center justify-center p-2 overflow-hidden",onWheel:be,onMouseMove:D,onMouseUp:G,onMouseLeave:G,onTouchStart:xe,onTouchMove:ze,onTouchEnd:_e,style:{touchAction:"none"},children:a.jsxs("div",{className:"relative w-full h-full flex items-center justify-center",children:[(f||b)&&!T&&a.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/50 z-10",children:a.jsxs("div",{className:"text-center",children:[a.jsx(kr,{className:"w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:t(o==="stream"?"camera.connectingToCamera":"camera.capturingSnapshot")})]})}),T&&a.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/80 z-10",children:a.jsxs("div",{className:"text-center p-4",children:[a.jsx(Df,{className:"w-10 h-10 text-orange-400 mx-auto mb-3"}),a.jsx("p",{className:"text-white mb-2",children:t("camera.connectionLost")}),a.jsx("p",{className:"text-sm text-bambu-gray mb-3",children:t("camera.reconnecting",{countdown:j,attempt:A+1,max:qj})}),a.jsx("button",{onClick:Z,className:"px-4 py-2 bg-bambu-green text-white text-sm rounded hover:bg-bambu-green/80 transition-colors",children:t("camera.reconnectNow")})]})}),d&&!T&&a.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black z-10",children:a.jsxs("div",{className:"text-center p-4",children:[a.jsx(Cn,{className:"w-12 h-12 text-orange-400 mx-auto mb-3"}),a.jsx("p",{className:"text-white mb-2",children:t("camera.cameraUnavailable")}),a.jsx("p",{className:"text-xs text-bambu-gray mb-4 max-w-md",children:t("camera.cameraUnavailableDesc")}),a.jsx("button",{onClick:Z,className:"px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors",children:t("camera.retry")})]})}),a.jsx("img",{ref:X,src:Ze,alt:t("camera.cameraStream"),className:"max-w-full max-h-full object-contain select-none",style:{transform:`scale(${P}) translate(${L.x/P}px, ${L.y/P}px)`,cursor:P>1?R?"grabbing":"grab":"default"},onError:Ze?Le:void 0,onLoad:Ze?$:void 0,onMouseDown:ee,draggable:!1},x),a.jsxs("div",{className:"absolute bottom-4 left-4 flex items-center gap-1.5 bg-black/60 rounded-lg px-2 py-1.5",children:[a.jsx("button",{onClick:he,disabled:P<=1,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:t("camera.zoomOut"),children:a.jsx(HF,{className:"w-4 h-4 text-white"})}),a.jsxs("button",{onClick:He,className:"px-2 py-1 text-sm text-white hover:bg-white/10 rounded min-w-[48px]",title:t("camera.resetZoom"),children:[Math.round(P*100),"%"]}),a.jsx("button",{onClick:Q,disabled:P>=4,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:t("camera.zoomIn"),children:a.jsx(BF,{className:"w-4 h-4 text-white"})})]})]})}),a.jsx(xL,{printerId:s,isOpen:c,onClose:()=>u(!1)})]}):a.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:a.jsx("p",{className:"text-white",children:t("camera.invalidPrinterId")})})}function iJe(t){const e=t.get("show")?.split(",")||["progress","layers","eta","filename","status"],n=parseInt(t.get("fps")||"15",10),r=Math.min(Math.max(isNaN(n)?15:n,1),30),i=t.get("camera"),s=i!=="false"&&i!=="0";return{size:t.get("size")||"medium",fps:r,showCamera:s,showProgress:e.includes("progress"),showLayers:e.includes("layers"),showEta:e.includes("eta"),showFilename:e.includes("filename"),showStatus:e.includes("status"),showPrinter:e.includes("printer")}}function aJe(t){const e=Math.floor(t/3600),n=Math.floor(t%3600/60);return e>0?`${e}h ${n}m`:`${n}m`}function sJe(t,e){const n=new Date,r=new Date(n.getTime()+t*60*1e3),i=new Date;i.setHours(0,0,0,0);const s=new Date(r);s.setHours(0,0,0,0);const o=r.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"});return s.getTime()===i.getTime()?o:s.getTime()===i.getTime()+864e5?`${e("streamOverlay.tomorrow")} ${o}`:r.toLocaleDateString([],{weekday:"short"})+" "+o}function oJe(t,e){if(t.stg_cur_name)return t.stg_cur_name;switch(t.state){case"RUNNING":return e("streamOverlay.status.printing");case"PAUSE":return e("streamOverlay.status.paused");case"FINISH":return e("streamOverlay.status.finished");case"FAILED":return e("streamOverlay.status.failed");case"IDLE":return e("streamOverlay.status.idle");default:return t.state||e("streamOverlay.status.unknown")}}function lJe(t){switch(t){case"small":return{container:"p-3",text:"text-sm",textLarge:"text-lg",progressHeight:"h-2",icon:"w-3 h-3",gap:"gap-2",logoHeight:"h-12"};case"large":return{container:"p-6",text:"text-xl",textLarge:"text-3xl",progressHeight:"h-4",icon:"w-6 h-6",gap:"gap-4",logoHeight:"h-24"};default:return{container:"p-4",text:"text-base",textLarge:"text-xl",progressHeight:"h-3",icon:"w-4 h-4",gap:"gap-3",logoHeight:"h-16"}}}function cJe(){const{printerId:t}=Yk(),[e]=NF(),{t:n}=Vt(),r=tn(),i=parseInt(t||"0",10),[s,o]=w.useState(Date.now()),l=w.useMemo(()=>iJe(e),[e]),c=lJe(l.size),{data:u}=tt({queryKey:["printer",i],queryFn:()=>se.getPrinter(i),enabled:i>0}),{data:d}=tt({queryKey:["printerStatus",i],queryFn:()=>se.getPrinterStatus(i),enabled:i>0,refetchInterval:2e3});w.useEffect(()=>{if(!i)return;const b=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.host}/api/v1/ws`,y=new WebSocket(b);return y.onmessage=S=>{try{const N=JSON.parse(S.data);N.type==="printer_status"&&N.printer_id===i&&r.setQueryData(["printerStatus",i],N.status)}catch{}},y.onerror=()=>{},()=>{y.close()}},[i,r]),w.useEffect(()=>(document.title=u?`${u.name} - ${n("streamOverlay.title")}`:n("streamOverlay.title"),()=>{document.title="Bambuddy"}),[u,n]);const h=()=>{setTimeout(()=>{o(Date.now())},3e3)};if(!i)return a.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:a.jsx("p",{className:"text-white",children:n("streamOverlay.invalidPrinterId")})});if(!d)return a.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:a.jsx("p",{className:"text-gray-400",children:n("common.loading")})});const f=d.state==="RUNNING"||d.state==="PAUSE",p=d.progress||0,x=`/api/v1/printers/${i}/camera/stream?fps=${l.fps}&t=${s}`;return a.jsxs("div",{className:"min-h-screen bg-black relative overflow-hidden",children:[l.showCamera&&a.jsx("img",{src:x,alt:n("streamOverlay.cameraStream"),className:"absolute inset-0 w-full h-full object-contain",onError:h},s),a.jsx("a",{href:"https://github.com/maziggy/bambuddy",target:"_blank",rel:"noopener noreferrer",className:"absolute top-4 right-4 z-10",children:a.jsx("img",{src:"/img/bambuddy_logo_dark_transparent.png",alt:"Bambuddy",className:`${c.logoHeight} object-contain drop-shadow-lg hover:scale-105 transition-transform`})}),a.jsx("div",{className:"absolute bottom-0 left-0 right-0 z-10 bg-gradient-to-t from-black/80 via-black/60 to-transparent",children:a.jsxs("div",{className:`${c.container}`,children:[l.showPrinter&&u&&a.jsxs("div",{className:`flex items-center ${c.gap} mb-2`,children:[a.jsx(Cr,{className:`${c.icon} text-white/70`}),a.jsx("span",{className:`${c.text} text-white font-medium`,children:u.name})]}),l.showFilename&&d.current_print&&a.jsx("div",{className:`${c.textLarge} text-white font-semibold mb-2 truncate drop-shadow-md`,children:d.current_print.replace(/\.gcode\.3mf$|\.3mf$|\.gcode$/i,"")}),l.showStatus&&a.jsx("div",{className:`${c.text} text-white/70 mb-2`,children:oJe(d,n)}),l.showProgress&&f&&a.jsxs("div",{className:"mb-3",children:[a.jsxs("div",{className:`flex items-center justify-between mb-1 ${c.text}`,children:[a.jsx("span",{className:"text-white/70",children:n("streamOverlay.progress")}),a.jsxs("span",{className:"text-white font-bold",children:[Math.round(p),"%"]})]}),a.jsx("div",{className:`w-full bg-white/20 rounded-full ${c.progressHeight}`,children:a.jsx("div",{className:`bg-bambu-green ${c.progressHeight} rounded-full transition-all duration-500`,style:{width:`${p}%`}})})]}),f&&(l.showLayers||l.showEta)&&a.jsxs("div",{className:`flex items-center ${c.gap} flex-wrap`,children:[l.showLayers&&d.layer_num!=null&&d.total_layers!=null&&d.total_layers>0&&a.jsxs("div",{className:`flex items-center ${c.gap} text-white/70`,children:[a.jsx(rs,{className:c.icon}),a.jsxs("span",{className:c.text,children:[a.jsx("span",{className:"text-white",children:d.layer_num}),a.jsx("span",{className:"mx-1",children:"/"}),a.jsx("span",{children:d.total_layers})]})]}),l.showEta&&d.remaining_time!=null&&d.remaining_time>0&&a.jsxs(a.Fragment,{children:[a.jsxs("div",{className:`flex items-center ${c.gap} text-white/70`,children:[a.jsx(Ou,{className:c.icon}),a.jsx("span",{className:`${c.text} text-white`,children:aJe(d.remaining_time*60)})]}),a.jsxs("div",{className:`flex items-center ${c.gap} text-white/70`,children:[a.jsx(Qn,{className:c.icon}),a.jsxs("span",{className:`${c.text} text-white`,children:[n("streamOverlay.eta")," ",sJe(d.remaining_time,n)]})]})]})]}),!f&&a.jsx("div",{className:`${c.text} text-white/70 py-2`,children:d.connected?n("streamOverlay.printerIdle"):n("streamOverlay.printerOffline")})]})})]})}function uJe(){const{t}=Vt(),{id:e}=Yk(),{mode:n}=np(),{data:r,isLoading:i,error:s}=tt({queryKey:["external-link",e],queryFn:()=>se.getExternalLink(Number(e)),enabled:!!e});return i?a.jsx("div",{className:"flex items-center justify-center h-full",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):s||!r?a.jsxs("div",{className:"flex flex-col items-center justify-center h-full gap-4 text-bambu-gray",children:[a.jsx(Cn,{className:"w-12 h-12"}),a.jsx("p",{children:t("common.linkNotFound")})]}):a.jsx("iframe",{src:r.url,className:"h-full w-full border-0",style:{colorScheme:n},title:r.name,sandbox:"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"})}const dK={material:"",subtype:"",brand:"",color_name:"",rgba:"808080FF",label_weight:1e3,core_weight:250,weight_used:0,slicer_filament:"",note:""};function dJe(t){const e={};return t.slicer_filament||(e.slicer_filament="Slicer preset is required"),t.material||(e.material="Material is required"),{isValid:Object.keys(e).length===0,errors:e}}const hJe=["PLA","PETG","ABS","TPU","ASA","PC","PA","PVA","HIPS","PA-CF","PETG-CF","PLA-CF"],mJe=["Bambu","PolyLite","PolyTerra","eSUN","Overture","Fiberon","SUNLU","Inland","Hatchbox","Generic"],y0=["Basic","Matte","Silk","Tough","HF","High Flow","Engineering","Galaxy","Glow","Marble","Metal","Rainbow","Sparkle","Wood","Translucent","Transparent","Clear","Lite","Pro","Plus","Max","Super","Ultra","Flex","Soft","Hard","Strong","Impact","Heat Resistant","UV Resistant","ESD","Conductive","Magnetic","Gradient","Dual Color","Tri Color","Multicolor"],Yae=[{name:"Black",hex:"000000"},{name:"White",hex:"FFFFFF"},{name:"Gray",hex:"808080"},{name:"Red",hex:"FF0000"},{name:"Orange",hex:"FFA500"},{name:"Yellow",hex:"FFFF00"},{name:"Green",hex:"00AE42"},{name:"Blue",hex:"0066FF"},{name:"Purple",hex:"8B00FF"},{name:"Pink",hex:"FF69B4"},{name:"Brown",hex:"8B4513"},{name:"Silver",hex:"C0C0C0"}],fJe=[{name:"Dark Red",hex:"8B0000"},{name:"Crimson",hex:"DC143C"},{name:"Coral",hex:"FF7F50"},{name:"Salmon",hex:"FA8072"},{name:"Dark Orange",hex:"FF8C00"},{name:"Peach",hex:"FFDAB9"},{name:"Gold",hex:"FFD700"},{name:"Khaki",hex:"F0E68C"},{name:"Lemon",hex:"FFF44F"},{name:"Lime",hex:"32CD32"},{name:"Forest Green",hex:"228B22"},{name:"Olive",hex:"808000"},{name:"Mint",hex:"98FF98"},{name:"Teal",hex:"008080"},{name:"Navy",hex:"000080"},{name:"Sky Blue",hex:"87CEEB"},{name:"Royal Blue",hex:"4169E1"},{name:"Cyan",hex:"00FFFF"},{name:"Turquoise",hex:"40E0D0"},{name:"Violet",hex:"EE82EE"},{name:"Magenta",hex:"FF00FF"},{name:"Indigo",hex:"4B0082"},{name:"Lavender",hex:"E6E6FA"},{name:"Plum",hex:"DDA0DD"},{name:"Hot Pink",hex:"FF69B4"},{name:"Rose",hex:"FF007F"},{name:"Blush",hex:"FFB6C1"},{name:"Chocolate",hex:"D2691E"},{name:"Tan",hex:"D2B48C"},{name:"Beige",hex:"F5F5DC"},{name:"Maroon",hex:"800000"},{name:"Dark Gray",hex:"404040"},{name:"Light Gray",hex:"D3D3D3"},{name:"Charcoal",hex:"36454F"},{name:"Ivory",hex:"FFFFF0"},{name:"Bambu Green",hex:"00AE42"},{name:"Jade White",hex:"E8E8E8"},{name:"Titan Gray",hex:"5A5A5A"}],hK=[...Yae,...fJe],Qae="bambuddy-recent-colors",pJe=8,gJe=[{code:"GFL00",name:"Bambu PLA Basic",displayName:"Bambu PLA Basic",isCustom:!1,allCodes:["GFL00"]},{code:"GFL01",name:"Bambu PLA Matte",displayName:"Bambu PLA Matte",isCustom:!1,allCodes:["GFL01"]},{code:"GFL05",name:"Generic PLA",displayName:"Generic PLA",isCustom:!1,allCodes:["GFL05"]},{code:"GFG00",name:"Bambu PETG Basic",displayName:"Bambu PETG Basic",isCustom:!1,allCodes:["GFG00"]},{code:"GFG05",name:"Generic PETG",displayName:"Generic PETG",isCustom:!1,allCodes:["GFG05"]},{code:"GFB00",name:"Bambu ABS Basic",displayName:"Bambu ABS Basic",isCustom:!1,allCodes:["GFB00"]},{code:"GFB05",name:"Generic ABS",displayName:"Generic ABS",isCustom:!1,allCodes:["GFB05"]},{code:"GFA00",name:"Bambu ASA Basic",displayName:"Bambu ASA Basic",isCustom:!1,allCodes:["GFA00"]},{code:"GFU00",name:"Bambu TPU 95A",displayName:"Bambu TPU 95A",isCustom:!1,allCodes:["GFU00"]},{code:"GFU05",name:"Generic TPU",displayName:"Generic TPU",isCustom:!1,allCodes:["GFU05"]},{code:"GFC00",name:"Bambu PC Basic",displayName:"Bambu PC Basic",isCustom:!1,allCodes:["GFC00"]},{code:"GFN00",name:"Bambu PA Basic",displayName:"Bambu PA Basic",isCustom:!1,allCodes:["GFN00"]},{code:"GFN05",name:"Generic PA",displayName:"Generic PA",isCustom:!1,allCodes:["GFN05"]},{code:"GFS00",name:"Bambu PLA-CF",displayName:"Bambu PLA-CF",isCustom:!1,allCodes:["GFS00"]},{code:"GFT00",name:"Bambu PETG-CF",displayName:"Bambu PETG-CF",isCustom:!1,allCodes:["GFT00"]},{code:"GFNC0",name:"Bambu PA-CF",displayName:"Bambu PA-CF",isCustom:!1,allCodes:["GFNC0"]},{code:"GFV00",name:"Bambu PVA",displayName:"Bambu PVA",isCustom:!1,allCodes:["GFV00"]}];function Wk(t){let e=t.replace(/@.*$/,"").trim();e=e.replace(/\(Custom\)/i,"").trim(),e=e.replace(/^[#*]+\s*/,"").trim();const n=["PLA-CF","PETG-CF","ABS-GF","ASA-CF","PA-CF","PAHT-CF","PA6-CF","PA6-GF","PPA-CF","PPA-GF","PET-CF","PPS-CF","PC-CF","PC-ABS","ABS-GF","PETG","PLA","ABS","ASA","PC","PA","TPU","PVA","HIPS","BVOH","PPS","PCTG","PEEK","PEI"];let r="",i=-1;for(const c of n){const u=e.toUpperCase().indexOf(c.toUpperCase());if(u!==-1){r=c,i=u;break}}let s="";i>0&&(s=e.substring(0,i).trim(),s=s.replace(/[-_\s]+$/,""));let o="";i!==-1&&r&&(o=e.substring(i+r.length).trim(),o=o.replace(/^[-_\s]+/,""));let l="";for(const c of y0)if(o.toLowerCase().includes(c.toLowerCase())){l=c;break}if(!l&&s)for(const c of y0){const u=new RegExp(`\\s+${c}$`,"i");if(u.test(s)){l=c,s=s.replace(u,"").trim();break}}return{brand:s,material:r,variant:l}}function bJe(t,e){const n=new Set(mJe);for(const r of t){const{brand:i}=Wk(r.name);i&&i.length>1&&n.add(i)}if(e)for(const r of e)if(r.filament_vendor&&r.filament_vendor.length>1)n.add(r.filament_vendor);else{const{brand:i}=Wk(r.name);i&&i.length>1&&n.add(i)}return Array.from(n).sort((r,i)=>r.localeCompare(i))}function xJe(t){const e=t.filter(r=>r.preset_type==="filament");if(e.length===0)return[];const n=new Map;for(const r of e){const i=r.name.replace(/@.*$/,"").trim(),s=n.get(i);if(s)s.allCodes.push(String(r.id));else{const o=r.filament_type||String(r.id);n.set(i,{code:o,name:i,displayName:i,isCustom:!1,allCodes:[o]})}}return Array.from(n.values()).sort((r,i)=>r.displayName.localeCompare(i.displayName))}function yJe(t,e,n){if(t.length>0){const r=[],i=new Map;for(const s of t)if(s.is_custom){const o=s.name.toUpperCase();(e.size===0||Array.from(e).some(c=>o.includes(c))||!o.includes("@"))&&r.push({code:s.setting_id,name:s.name,displayName:`${s.name} (Custom)`,isCustom:!0,allCodes:[s.setting_id]})}else{const o=s.name.replace(/@.*$/,"").trim(),l=i.get(o);l?l.allCodes.push(s.setting_id):i.set(o,{code:s.setting_id,name:o,displayName:o,isCustom:!1,allCodes:[s.setting_id]})}return[...r,...Array.from(i.values())].sort((s,o)=>s.displayName.localeCompare(o.displayName))}if(n&&n.length>0){const r=xJe(n);if(r.length>0)return r}return gJe}function vJe(t,e){if(!t)return;let n=e.find(r=>r.code===t);if(n||(n=e.find(r=>r.allCodes.includes(t))),!n){const r=t.toLowerCase();n=e.find(i=>i.code.toLowerCase()===r||i.allCodes.some(s=>s.toLowerCase()===r))}return n}function wJe(){try{const t=localStorage.getItem(Qae);if(t)return JSON.parse(t)}catch{}return[]}function _Je(t,e){const n=e.filter(i=>i.hex.toUpperCase()!==t.hex.toUpperCase()),r=[t,...n].slice(0,pJe);try{localStorage.setItem(Qae,JSON.stringify(r))}catch{}return r}function Wj(t,e){if(!e.material)return!1;const r=(t.name||"").replace(/^High Flow[_\s]+/i,"").replace(/^Standard[_\s]+/i,"").replace(/^HF[_\s]+/i,"").replace(/^S[_\s]+/i,"").trim(),i=Wk(r);return!(!(i.material.toUpperCase()===e.material.toUpperCase())||e.brand&&!(i.brand.toLowerCase().includes(e.brand.toLowerCase())||e.brand.toLowerCase().includes(i.brand.toLowerCase()))||e.subtype&&!(i.variant.toLowerCase().includes(e.subtype.toLowerCase())||e.subtype.toLowerCase().includes(i.variant.toLowerCase())||r.toLowerCase().includes(e.subtype.toLowerCase())))}function SJe({formData:t,updateField:e,cloudAuthenticated:n,loadingCloudPresets:r,presetInputValue:i,setPresetInputValue:s,selectedPresetOption:o,filamentOptions:l,availableBrands:c}){const{t:u}=Vt(),[d,h]=w.useState(!1),[f,p]=w.useState(!1),[x,v]=w.useState(!1),[b,y]=w.useState(""),[S,N]=w.useState(""),A=w.useRef(null),M=w.useRef(null),T=w.useRef(null);w.useEffect(()=>{const U=L=>{A.current&&!A.current.contains(L.target)&&h(!1),M.current&&!M.current.contains(L.target)&&p(!1),T.current&&!T.current.contains(L.target)&&v(!1)};return document.addEventListener("mousedown",U),()=>document.removeEventListener("mousedown",U)},[]);const k=w.useMemo(()=>{if(!i)return l;const U=i.toLowerCase();return l.filter(L=>L.displayName.toLowerCase().includes(U)||L.code.toLowerCase().includes(U))},[l,i]),j=w.useMemo(()=>{if(!b)return c;const U=b.toLowerCase();return c.filter(L=>L.toLowerCase().includes(U))},[c,b]),E=w.useMemo(()=>{if(!S)return y0;const U=S.toLowerCase();return y0.filter(L=>L.toLowerCase().includes(U))},[S]),P=U=>{e("slicer_filament",U.code),s(U.displayName),h(!1);const L=Wk(U.name);L.material&&e("material",L.material),L.brand&&e("brand",L.brand),L.variant&&e("subtype",L.variant)};return a.jsxs("div",{className:"space-y-4",children:[a.jsx("div",{className:"flex items-center gap-2 text-xs text-bambu-gray",children:r?a.jsxs(a.Fragment,{children:[a.jsx(at,{className:"w-3 h-3 animate-spin"})," ",u("inventory.loadingPresets")]}):n?a.jsxs(a.Fragment,{children:[a.jsx(Gb,{className:"w-3 h-3 text-bambu-green"})," ",u("inventory.cloudConnected")]}):a.jsxs(a.Fragment,{children:[a.jsx(uhe,{className:"w-3 h-3"})," ",u("inventory.cloudNotConnected")]})}),a.jsxs("div",{children:[a.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[u("inventory.slicerPreset")," *"]}),a.jsxs("div",{className:"relative",ref:A,children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),a.jsx("input",{type:"text",className:"w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:u("inventory.searchPresets"),value:i,onChange:U=>{s(U.target.value),h(!0)},onFocus:()=>{h(!0),s("")}}),d&&a.jsx("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto",children:k.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:u("inventory.noPresetsFound")}):k.map(U=>a.jsxs("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex justify-between items-center ${o?.code===U.code?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>P(U),children:[a.jsx("span",{className:"truncate",children:U.displayName}),a.jsx("span",{className:"font-mono text-xs text-bambu-gray ml-2 shrink-0",children:U.code})]},U.code))})]}),o&&a.jsxs("div",{className:"mt-1 text-xs text-bambu-gray",children:[u("inventory.selectedPreset"),": ",a.jsx("span",{className:"font-mono text-bambu-green",children:o.code})]})]}),a.jsxs("div",{children:[a.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[u("inventory.material")," *"]}),a.jsxs("select",{value:t.material,onChange:U=>e("material",U.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green",children:[a.jsx("option",{value:"",children:u("inventory.selectMaterial")}),hJe.map(U=>a.jsx("option",{value:U,children:U},U))]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:u("inventory.brand")}),a.jsxs("div",{className:"relative",ref:M,children:[a.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:u("inventory.searchBrand"),value:f?b:t.brand,onChange:U=>{y(U.target.value),p(!0)},onFocus:()=>{p(!0),y("")}}),a.jsx(nr,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),f&&a.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[j.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:u("inventory.noResults")}):j.map(U=>a.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.brand===U?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("brand",U),p(!1),y("")},children:U},U)),b&&!j.includes(b)&&a.jsx("button",{type:"button",className:"w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary",onClick:()=>{e("brand",b),p(!1),y("")},children:u("inventory.useCustomBrand",{brand:b})})]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:u("inventory.subtype")}),a.jsxs("div",{className:"relative",ref:T,children:[a.jsx("input",{type:"text",value:x?S:t.subtype,onChange:U=>{N(U.target.value),v(!0)},onFocus:()=>{v(!0),N("")},placeholder:"Basic, Matte, Silk...",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"}),a.jsx(nr,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),x&&a.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[E.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:u("inventory.noResults")}):E.map(U=>a.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.subtype===U?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("subtype",U),v(!1),N("")},children:U},U)),S&&!y0.some(U=>U.toLowerCase()===S.toLowerCase().trim())&&a.jsx("button",{type:"button",className:"w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary",onClick:()=>{e("subtype",S),v(!1),N("")},children:u("inventory.useCustomBrand",{brand:S})})]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:u("inventory.labelWeight")}),a.jsxs("div",{className:"relative",children:[a.jsx("input",{type:"number",value:t.label_weight,onChange:U=>e("label_weight",parseInt(U.target.value)||0),className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),a.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]})]})]})}function NJe({formData:t,updateField:e,recentColors:n,onColorUsed:r,catalogColors:i}){const{t:s}=Vt(),[o,l]=w.useState(!1),[c,u]=w.useState(""),d=t.rgba.replace("#","").substring(0,6),h=y=>d.toUpperCase()===y.toUpperCase(),f=(y,S)=>{e("rgba",y.toUpperCase()+"FF"),e("color_name",S),r({name:S,hex:y})},p=w.useMemo(()=>{if(i.length===0)return[];const y=t.brand?.trim(),S=t.material?.toLowerCase().trim(),N=t.subtype?.toLowerCase().trim();if(!y&&!S)return[];const A=y?y.toLowerCase().split(/[\s\-_]+/).filter(k=>k.length>=2):[],M=k=>{if(A.length===0)return!0;const j=k.toLowerCase();return A.some(E=>j.includes(E))},T=S&&N?`${S} ${N}`:"";if(T){const k=i.filter(E=>M(E.manufacturer)&&E.material?.toLowerCase()===T);if(k.length>0)return k.map(E=>({name:E.color_name,hex:E.hex_color.replace("#","").substring(0,6)}));const j=T.replace(/\+$/,"");if(j!==T){const E=i.filter(P=>M(P.manufacturer)&&P.material?.toLowerCase()===j);if(E.length>0)return E.map(P=>({name:P.color_name,hex:P.hex_color.replace("#","").substring(0,6)}))}}if(S){const k=i.filter(j=>M(j.manufacturer)&&(!j.material||j.material.toLowerCase().startsWith(S)));if(k.length>0)return k.map(j=>({name:j.color_name,hex:j.hex_color.replace("#","").substring(0,6)}))}return[]},[i,t.brand,t.material,t.subtype]),x=p.length>0,v=w.useMemo(()=>{if(!c)return p;const y=c.toLowerCase();return p.filter(S=>S.name.toLowerCase().includes(y))},[p,c]),b=w.useMemo(()=>c?hK.filter(y=>y.name.toLowerCase().includes(c.toLowerCase())):o?hK:Yae,[c,o]);return a.jsxs("div",{className:"space-y-3",children:[a.jsx("div",{className:"h-10 rounded-lg border border-bambu-dark-tertiary",style:{backgroundColor:`#${d}`}}),n.length>0&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-bambu-gray shrink-0",children:[a.jsx(Qn,{className:"w-3 h-3"}),a.jsx("span",{children:s("inventory.recentColors")})]}),a.jsx("div",{className:"flex flex-wrap gap-1.5",children:n.map(y=>a.jsx("button",{type:"button",onClick:()=>f(y.hex,y.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 ${h(y.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${y.hex}`},title:y.name},y.hex))})]}),a.jsxs("div",{className:"relative",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),a.jsx("input",{type:"text",className:"w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.searchColors"),value:c,onChange:y=>u(y.target.value)})]}),x?a.jsxs("div",{className:"space-y-1.5",children:[a.jsx("span",{className:"text-xs text-bambu-gray",children:c?s("inventory.searchResults"):`${t.brand}${t.material?` ${t.material}`:""}`}),a.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[v.map(y=>a.jsx("button",{type:"button",onClick:()=>f(y.hex,y.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 relative group ${h(y.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${y.hex}`},title:y.name,children:a.jsx("span",{className:"absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-10 shadow-lg text-white",children:y.name})},`${y.hex}-${y.name}`)),v.length===0&&a.jsx("p",{className:"text-sm text-bambu-gray py-1",children:s("inventory.noColorsFound")})]})]}):a.jsxs("div",{className:"space-y-1.5",children:[a.jsxs("div",{className:"flex items-center justify-between text-xs text-bambu-gray",children:[a.jsx("span",{children:s(c?"inventory.searchResults":o?"inventory.allColors":"inventory.commonColors")}),!c&&a.jsx("button",{type:"button",onClick:()=>l(!o),className:"flex items-center gap-1 hover:text-white transition-colors",children:o?a.jsxs(a.Fragment,{children:[s("inventory.showLess")," ",a.jsx(ql,{className:"w-3 h-3"})]}):a.jsxs(a.Fragment,{children:[s("inventory.showAll")," ",a.jsx(nr,{className:"w-3 h-3"})]})})]}),a.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[b.map(y=>a.jsx("button",{type:"button",onClick:()=>f(y.hex,y.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 relative group ${h(y.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${y.hex}`},title:y.name,children:a.jsx("span",{className:"absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-10 shadow-lg text-white",children:y.name})},y.hex)),b.length===0&&a.jsx("p",{className:"text-sm text-bambu-gray py-1",children:s("inventory.noColorsFound")})]})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:s("inventory.colorName")}),a.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.colorNamePlaceholder"),value:t.color_name,onChange:y=>e("color_name",y.target.value)})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:s("inventory.hexColor")}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-bambu-gray",children:"#"}),a.jsx("input",{type:"text",className:"w-full pl-7 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm font-mono uppercase focus:outline-none focus:border-bambu-green",placeholder:"RRGGBB",value:d.toUpperCase(),onChange:y=>{const S=y.target.value.replace("#","").replace(/[^0-9A-Fa-f]/g,"");S.length<=8&&e("rgba",S.toUpperCase()+(S.length<=6?"FF":""))}})]}),a.jsx("input",{type:"color",className:"w-11 h-[38px] rounded-lg cursor-pointer border border-bambu-dark-tertiary shrink-0 bg-transparent",value:`#${d}`,onChange:y=>{const S=y.target.value.replace("#","").toUpperCase();e("rgba",S+"FF")},title:s("inventory.pickColor")})]})]})]})]})}function kJe({catalog:t,value:e,onChange:n}){const{t:r}=Vt(),[i,s]=w.useState(!1),[o,l]=w.useState(""),[c,u]=w.useState(null),d=w.useRef(null),h=w.useRef(null);w.useEffect(()=>{const x=v=>{d.current&&!d.current.contains(v.target)&&s(!1)};return document.addEventListener("mousedown",x),()=>document.removeEventListener("mousedown",x)},[]);const f=w.useMemo(()=>{if(!o)return t;const x=o.toLowerCase();return t.filter(v=>v.name.toLowerCase().includes(x)||v.weight.toString().includes(x))},[t,o]),p=w.useMemo(()=>{if(i)return o;if(c){const v=t.find(b=>b.id===c);if(v)return v.name}const x=t.find(v=>v.weight===e);return x?x.name:""},[i,o,c,t,e]);return a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:a.jsxs("span",{className:"flex items-center gap-2",children:[a.jsx(Efe,{className:"w-3.5 h-3.5 text-bambu-gray"}),r("inventory.coreWeight")]})}),a.jsxs("div",{className:"flex gap-2 items-center",children:[a.jsxs("div",{className:"flex-1 min-w-0 relative",ref:d,children:[a.jsx("input",{ref:h,type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:r("inventory.searchSpoolWeight"),value:p,onFocus:()=>{s(!0),l("")},onChange:x=>{l(x.target.value),s(!0)}}),i&&a.jsx("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto",children:f.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:r("inventory.noResults")}):f.map(x=>a.jsxs("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex justify-between items-center ${(c?x.id===c:x.weight===e)?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{u(x.id),n(x.weight),s(!1),l("")},children:[a.jsx("span",{className:"truncate",children:x.name}),a.jsxs("span",{className:"font-mono text-xs text-bambu-gray ml-2 shrink-0",children:[x.weight,"g"]})]},x.id))})]}),a.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[a.jsx("input",{type:"number",className:"w-16 px-2 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm text-center font-mono focus:outline-none focus:border-bambu-green",value:e,min:0,max:2e3,onChange:x=>{const v=parseInt(x.target.value);!isNaN(v)&&v>=0&&n(v)}}),a.jsx("span",{className:"text-bambu-gray text-sm",children:"g"})]})]})]})}function CJe({formData:t,updateField:e,spoolCatalog:n}){const{t:r}=Vt(),{showToast:i}=mn(),[s,o]=w.useState(""),[l,c]=w.useState(!1),u=Math.max(0,t.label_weight-t.weight_used),d=t.core_weight+u;return w.useEffect(()=>{l||o(String(d))},[l,d]),a.jsxs("div",{className:"space-y-4",children:[a.jsx(kJe,{catalog:n,value:t.core_weight,onChange:h=>e("core_weight",h)}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:r("inventory.currentWeight")}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx("input",{type:"number",value:u,min:0,max:t.label_weight,onChange:h=>{const f=parseInt(h.target.value)||0;e("weight_used",Math.max(0,t.label_weight-f))},className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),a.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]}),a.jsxs("span",{className:"text-xs text-bambu-gray shrink-0",children:["/ ",t.label_weight,"g"]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:r("inventory.measuredWeight")}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx("input",{type:"number",value:s,min:0,onFocus:()=>c(!0),onChange:h=>{o(h.target.value)},onBlur:()=>{c(!1);const h=s.trim(),f=Number(h),p=t.core_weight,x=t.core_weight+t.label_weight;if(!h||!Number.isFinite(f)||fx){i(r("inventory.measuredWeightError",{min:p,max:x}),"error"),o(String(d));return}const v=Math.round(f),b=Math.max(0,Math.min(t.label_weight,v-t.core_weight));e("weight_used",Math.max(0,t.label_weight-b)),o(String(v))},className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),a.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]}),a.jsxs("span",{className:"text-xs text-bambu-gray shrink-0",children:["/ ",t.core_weight+t.label_weight,"g"]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:r("inventory.note")}),a.jsx("textarea",{className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green resize-none min-h-[80px]",placeholder:r("inventory.notePlaceholder"),value:t.note,onChange:h=>e("note",h.target.value)})]})]})}function MJe({formData:t,printersWithCalibrations:e,selectedProfiles:n,setSelectedProfiles:r,expandedPrinters:i,setExpandedPrinters:s}){const{t:o}=Vt(),l=f=>{s(p=>{const x=new Set(p);return x.has(f)?x.delete(f):x.add(f),x})},c=(f,p,x)=>{const v=`${f}:${p}:${x??"null"}`,b=`${f}:${x??"null"}`;r(y=>{const S=new Set(y);if(S.has(v))S.delete(v);else{for(const N of Array.from(S)){const A=N.split(":");`${A[0]}:${A[2]}`===b&&S.delete(N)}S.add(v)}return S})},u=()=>{const f=new Set;for(const{printer:p,calibrations:x}of e){if(!p.connected)continue;const v=x.filter(y=>Wj(y,t)),b=new Map;for(const y of v){const S=`${y.extruder_id??"null"}`;b.has(S)||b.set(S,[]),b.get(S).push(y)}for(const[y,S]of b)if(S.length>0){const A=[...S].sort((M,T)=>T.k_value-M.k_value)[0];f.add(`${p.id}:${A.cali_idx}:${y}`)}}r(f)};if(!t.material)return a.jsx("div",{className:"p-6 bg-bambu-dark rounded-lg text-center",children:a.jsx("p",{className:"text-bambu-gray",children:o("inventory.selectMaterialFirst")})});if(e.length===0)return a.jsx("div",{className:"p-6 bg-bambu-dark rounded-lg text-center",children:a.jsx("p",{className:"text-bambu-gray",children:o("inventory.noPrintersConfigured")})});const d=e.reduce((f,{printer:p,calibrations:x})=>p.connected?f+x.filter(v=>Wj(v,t)).length:f,0),h=(f,p)=>{const x=`${f.id}:${p.cali_idx}:${p.extruder_id??"null"}`,v=n.has(x);return a.jsxs("label",{className:`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-all border ${v?"bg-bambu-green/10 border-bambu-green/30":"bg-bambu-dark border-transparent hover:bg-bambu-dark/80"}`,children:[a.jsx("input",{type:"checkbox",checked:v,onChange:()=>c(String(f.id),p.cali_idx,p.extruder_id),className:"w-4 h-4 rounded border-bambu-dark-tertiary text-bambu-green focus:ring-bambu-green"}),a.jsx("div",{className:"flex-1 min-w-0",children:a.jsx("span",{className:`text-sm font-medium ${v?"text-bambu-green":"text-white"}`,children:p.name||p.filament_id})}),a.jsx("div",{className:"flex items-center gap-2 shrink-0",children:a.jsxs("span",{className:"text-xs font-mono px-2 py-0.5 rounded bg-bambu-dark text-bambu-gray",children:["K=",p.k_value.toFixed(3)]})})]},`${p.cali_idx}-${p.extruder_id}`)};return a.jsxs("div",{className:"space-y-4",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("p",{className:"text-xs text-bambu-gray",children:[o("inventory.matchingFilter"),": ",t.brand||o("inventory.anyBrand")," / ",t.material," / ",t.subtype||o("inventory.anyVariant")]}),d>0&&a.jsxs("button",{type:"button",onClick:u,className:"flex items-center gap-1.5 px-2 py-1 text-xs bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white hover:border-bambu-green transition-colors",children:[a.jsx(pb,{className:"w-3.5 h-3.5"}),o("inventory.autoSelect")," (",d,")"]})]}),a.jsx("div",{className:"space-y-3",children:e.map(({printer:f,calibrations:p})=>{const x=i.has(String(f.id)),v=p.filter(A=>Wj(A,t)),b=v.length,y=v.some(A=>A.extruder_id!==void 0&&A.extruder_id!==null&&A.extruder_id>0),S=v.filter(A=>A.extruder_id===1),N=v.filter(A=>A.extruder_id===0||A.extruder_id===void 0||A.extruder_id===null);return a.jsxs("div",{className:"border border-bambu-dark-tertiary rounded-lg overflow-hidden",children:[a.jsxs("button",{type:"button",onClick:()=>l(String(f.id)),className:"w-full px-4 py-3 flex items-center justify-between bg-bambu-dark-secondary hover:bg-bambu-dark-tertiary transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[x?a.jsx(nr,{className:"w-4 h-4 text-bambu-gray"}):a.jsx(fa,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:"font-medium text-white",children:f.name}),b>0?a.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:[b," ",o(b!==1?"inventory.matches":"inventory.match")]}):a.jsx("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray",children:o("inventory.noMatches")})]}),a.jsx("span",{className:`text-xs px-2 py-1 rounded-full ${f.connected?"bg-green-500/20 text-green-500":"bg-bambu-gray/20 text-bambu-gray"}`,children:f.connected?o("inventory.connected"):o("inventory.offline")})]}),x&&a.jsx("div",{className:"px-4 py-3 space-y-3 bg-bambu-dark border-t border-bambu-dark-tertiary",children:f.connected?b===0?a.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.noKProfilesMatch")}):y?a.jsxs(a.Fragment,{children:[S.length>0&&a.jsxs("div",{className:"space-y-2",children:[a.jsx("p",{className:"text-xs font-medium text-bambu-gray uppercase tracking-wide",children:o("inventory.leftNozzle")}),a.jsx("div",{className:"space-y-2",children:S.map(A=>h(f,A))})]}),N.length>0&&a.jsxs("div",{className:"space-y-2",children:[a.jsx("p",{className:"text-xs font-medium text-bambu-gray uppercase tracking-wide",children:o("inventory.rightNozzle")}),a.jsx("div",{className:"space-y-2",children:N.map(A=>h(f,A))})]})]}):a.jsx("div",{className:"space-y-2",children:v.map(A=>h(f,A))}):a.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.printerOffline")})})]},f.id)})}),n.size>0&&a.jsx("div",{className:"p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:a.jsxs("p",{className:"text-sm text-white",children:[a.jsx("span",{className:"font-semibold",children:n.size})," ",o("inventory.profilesSelected")]})})]})}function AJe(t){const e=new Date(t);return e.toLocaleDateString("en-GB",{day:"2-digit",month:"2-digit",year:"2-digit"})+" "+e.toLocaleTimeString("en-GB",{hour:"2-digit",minute:"2-digit"})}const TJe={completed:"text-bambu-green",failed:"text-red-400",aborted:"text-yellow-400"};function EJe({spoolId:t}){const{t:e}=Vt(),n=tn(),{showToast:r}=mn(),{data:i,isLoading:s}=tt({queryKey:["spool-usage",t],queryFn:()=>se.getSpoolUsageHistory(t)}),o=lt({mutationFn:()=>se.clearSpoolUsageHistory(t),onSuccess:()=>{n.invalidateQueries({queryKey:["spool-usage",t]}),r(e("inventory.historyCleared"),"success")}});return s?a.jsx("div",{className:"flex justify-center py-4",children:a.jsx(at,{className:"w-5 h-5 animate-spin text-bambu-green"})}):!i||i.length===0?a.jsxs("div",{className:"text-center py-4 text-bambu-gray text-sm",children:[a.jsx(Qn,{className:"w-5 h-5 mx-auto mb-2 opacity-50"}),e("inventory.noUsageHistory")]}):a.jsxs("div",{className:"space-y-2",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsx("h4",{className:"text-sm font-medium text-white",children:e("inventory.usageHistory")}),a.jsxs(Me,{variant:"ghost",size:"sm",onClick:()=>o.mutate(),disabled:o.isPending,className:"text-xs text-bambu-gray hover:text-red-400",children:[a.jsx(en,{className:"w-3 h-3 mr-1"}),e("inventory.clearHistory")]})]}),a.jsx("div",{className:"max-h-48 overflow-y-auto space-y-1",children:i.map(l=>a.jsxs("div",{className:"flex items-center justify-between p-2 rounded bg-bambu-dark/50 text-xs",children:[a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("span",{className:"text-bambu-gray",children:AJe(l.created_at)}),l.print_name&&a.jsx("span",{className:"text-white ml-2 truncate",title:l.print_name,children:l.print_name})]}),a.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0 ml-2",children:[a.jsxs("span",{className:"text-white font-medium",children:[l.weight_used.toFixed(1),"g"]}),a.jsxs("span",{className:"text-bambu-gray",children:["(",l.percent_used,"%)"]}),a.jsx("span",{className:TJe[l.status]||"text-bambu-gray",children:l.status})]})]},l.id))})]})}function PJe({isOpen:t,onClose:e,spool:n,printersWithCalibrations:r=[]}){const{t:i}=Vt(),s=tn(),{showToast:o}=mn(),l=!!n,[c,u]=w.useState(dK),[d,h]=w.useState({}),[f,p]=w.useState("filament"),[x,v]=w.useState(!1),[b,y]=w.useState(!1),[S,N]=w.useState(!1),[A,M]=w.useState([]),[T,k]=w.useState(""),[j,E]=w.useState([]),[P,U]=w.useState([]),[L,B]=w.useState([]),[R,V]=w.useState([]),[q,I]=w.useState([]),[K,Y]=w.useState(new Set),[le,H]=w.useState(new Set),X=r.length>0?r:q,te=w.useMemo(()=>K.size,[K]);w.useEffect(()=>{V(wJe())},[]),w.useEffect(()=>{t&&((async()=>{N(!0);try{const W=await se.getCloudStatus();if(y(W.is_authenticated),W.is_authenticated){const ue=await se.getFilamentPresets();M(ue)}}catch(W){console.error("Failed to fetch cloud presets:",W),y(!1)}finally{N(!1)}})(),se.getSpoolCatalog().then(E).catch(console.error),se.getColorCatalog().then(B).catch(console.error),se.getLocalPresets().then(W=>U(W.filament)).catch(console.error),r.length===0&&(async()=>{try{const W=await se.getPrinters(),ue=await Promise.all(W.map(oe=>se.getPrinterStatus(oe.id).catch(()=>null))),Z=[];for(let oe=0;oe({cali_idx:D.slot_id,filament_id:D.filament_id,setting_id:D.setting_id||"",name:D.name,k_value:parseFloat(D.k_value)||0,n_coef:parseFloat(D.n_coef)||0,extruder_id:D.extruder_id,nozzle_diameter:D.nozzle_diameter}))}catch{}Z.push({printer:{...Q,connected:be},calibrations:ee})}I(Z)}catch(W){console.error("Failed to fetch printer calibrations:",W)}})())},[t,r.length]);const J=w.useMemo(()=>yJe(A,new Set,P),[A,P]),Se=w.useMemo(()=>bJe(A,P),[A,P]),fe=w.useMemo(()=>vJe(c.slicer_filament,J),[c.slicer_filament,J]);w.useEffect(()=>{if(t){if(n)if(u({material:n.material||"",subtype:n.subtype||"",brand:n.brand||"",color_name:n.color_name||"",rgba:n.rgba||"808080FF",label_weight:n.label_weight||1e3,core_weight:n.core_weight||250,weight_used:n.weight_used||0,slicer_filament:n.slicer_filament||"",note:n.note||""}),k(n.slicer_filament_name||n.slicer_filament||""),n.k_profiles&&n.k_profiles.length>0){const $=new Set;for(const W of n.k_profiles)W.cali_idx!==null&&W.cali_idx!==void 0&&$.add(`${W.printer_id}:${W.cali_idx}:${W.extruder??"null"}`);Y($)}else Y(new Set);else u(dK),k(""),Y(new Set);h({}),p("filament"),v(!1)}},[t,n]),w.useEffect(()=>{t&&X.length>0&&H(new Set(X.map($=>String($.printer.id))))},[t,X]);const me=($,W)=>{u(ue=>({...ue,[$]:W})),$==="weight_used"&&v(!0),d[$]&&h(ue=>({...ue,[$]:void 0}))},De=$=>{V(W=>_Je($,W))},de=lt({mutationFn:$=>se.createSpool($),onSuccess:async $=>{K.size>0&&$?.id&&await Ne($.id),await s.invalidateQueries({queryKey:["inventory-spools"]}),o(i("inventory.spoolCreated"),"success"),e()},onError:$=>{o($.message,"error")}}),ce=lt({mutationFn:$=>se.updateSpool(n.id,$),onSuccess:async()=>{n?.id&&await Ne(n.id),await s.invalidateQueries({queryKey:["inventory-spools"]}),o(i("inventory.spoolUpdated"),"success"),e()},onError:$=>{o($.message,"error")}}),Ne=async $=>{if(K.size===0){try{await se.saveSpoolKProfiles($,[])}catch{}return}const W=[];for(const ue of K){const[Z,oe,Q]=ue.split(":"),he=parseInt(Z),be=parseInt(oe),ee=Q==="null"?0:parseInt(Q),z=X.find(D=>D.printer.id===he);if(z){const D=z.calibrations.find(G=>G.cali_idx===be);D&&W.push({printer_id:he,extruder:ee,nozzle_diameter:D.nozzle_diameter||"0.4",k_value:D.k_value,name:D.name||null,cali_idx:D.cali_idx,setting_id:D.setting_id||null})}}if(W.length>0)try{await se.saveSpoolKProfiles($,W)}catch(ue){console.error("Failed to save K-profiles:",ue)}};if(w.useEffect(()=>{if(!t)return;const $=W=>{W.key==="Escape"&&e()};return document.addEventListener("keydown",$),()=>document.removeEventListener("keydown",$)},[t,e]),!t)return null;const ie=()=>{const $=dJe(c);if(!$.isValid){h($.errors),($.errors.slicer_filament||$.errors.material)&&p("filament");return}const W=fe?.displayName||T||null,ue={material:c.material,subtype:c.subtype||null,brand:c.brand||null,color_name:c.color_name||null,rgba:c.rgba||null,label_weight:c.label_weight,core_weight:c.core_weight,slicer_filament:c.slicer_filament||null,slicer_filament_name:W,nozzle_temp_min:null,nozzle_temp_max:null,note:c.note||null};(!l||x)&&(ue.weight_used=c.weight_used),l?ce.mutate(ue):de.mutate(ue)},Le=de.isPending||ce.isPending;return a.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[a.jsx("div",{className:"absolute inset-0 bg-black/60 backdrop-blur-sm",onClick:e}),a.jsxs("div",{className:"relative w-full max-w-lg mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col",children:[a.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0",children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:i(l?"inventory.editSpool":"inventory.addSpool")}),a.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white rounded transition-colors",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary flex-shrink-0",children:[a.jsxs("button",{onClick:()=>p("filament"),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${f==="filament"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[a.jsx(iC,{className:"w-4 h-4"}),i("inventory.filamentInfoTab")]}),a.jsxs("button",{onClick:()=>p("pa-profile"),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${f==="pa-profile"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[a.jsx(_de,{className:"w-4 h-4"}),i("inventory.paProfileTab"),te>0&&a.jsx("span",{className:"text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:te})]})]}),a.jsx("div",{className:"p-4 overflow-y-auto flex-1",children:f==="filament"?a.jsxs("div",{className:"space-y-6",children:[a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:i("inventory.filamentInfo")}),a.jsx(SJe,{formData:c,updateField:me,cloudAuthenticated:b,loadingCloudPresets:S,presetInputValue:T,setPresetInputValue:k,selectedPresetOption:fe,filamentOptions:J,availableBrands:Se}),d.slicer_filament&&a.jsx("p",{className:"mt-1 text-xs text-red-400",children:d.slicer_filament}),d.material&&a.jsx("p",{className:"mt-1 text-xs text-red-400",children:d.material})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:i("inventory.color")}),a.jsx(NJe,{formData:c,updateField:me,recentColors:R,onColorUsed:De,catalogColors:L})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:i("inventory.additional")}),a.jsx(CJe,{formData:c,updateField:me,spoolCatalog:j})]}),l&&n&&a.jsx("div",{children:a.jsx(EJe,{spoolId:n.id})})]}):a.jsx(MJe,{formData:c,updateField:me,printersWithCalibrations:X,selectedProfiles:K,setSelectedProfiles:Y,expandedPrinters:le,setExpandedPrinters:H})}),a.jsxs("div",{className:"flex justify-end gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0",children:[a.jsx(Me,{variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{onClick:ie,disabled:Le,children:Le?a.jsxs(a.Fragment,{children:[a.jsx(at,{className:"w-4 h-4 animate-spin"}),i("common.saving")]}):a.jsxs(a.Fragment,{children:[a.jsx(bs,{className:"w-4 h-4"}),i(l?"common.save":"inventory.addSpool")]})})]})]})]})}function jJe({isOpen:t,onClose:e,columns:n,defaultColumns:r,onSave:i}){const{t:s}=Vt(),[o,l]=w.useState(n),[c,u]=w.useState(null),d=w.useRef(null);if(w.useEffect(()=>{t&&l(n.map(A=>({...A})))},[t,n]),w.useEffect(()=>{if(!t)return;const A=M=>{M.key==="Escape"&&e()};return window.addEventListener("keydown",A),()=>window.removeEventListener("keydown",A)},[t,e]),!t)return null;const h=A=>{l(M=>M.map((T,k)=>k===A?{...T,visible:!T.visible}:T))},f=(A,M)=>{M<0||M>=o.length||l(T=>{const k=[...T],[j]=k.splice(A,1);return k.splice(M,0,j),k})},p=(A,M)=>{d.current=M,u(M),A.dataTransfer.effectAllowed="move"},x=(A,M)=>{A.preventDefault(),A.dataTransfer.dropEffect="move";const T=d.current;T!==null&&T!==M&&(f(T,M),d.current=M,u(M))},v=A=>{A.preventDefault()},b=()=>{d.current=null,u(null)},y=()=>{l(r.map(A=>({...A})))},S=()=>{i(o),e()},N=o.filter(A=>A.visible).length;return a.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:e,children:a.jsx(_t,{className:"w-full max-w-md max-h-[80vh] flex flex-col",onClick:A=>A.stopPropagation(),children:a.jsxs(Nt,{className:"p-6 flex flex-col min-h-0",children:[a.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:s("inventory.configureColumns")}),a.jsxs("p",{className:"text-sm text-bambu-gray mb-4",children:[s("inventory.configureColumnsDesc"),a.jsxs("span",{className:"ml-2 text-bambu-gray/60",children:["(",N," ",s("inventory.of")," ",o.length," ",s("inventory.visible"),")"]})]}),a.jsx("div",{className:"space-y-1 overflow-y-auto flex-1 min-h-0 pr-1",children:o.map((A,M)=>a.jsxs("div",{className:`flex items-center gap-2 p-2 rounded-lg border transition-colors ${c===M?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary bg-bambu-dark-tertiary/50"} ${A.visible?"":"opacity-50"}`,draggable:!0,onDragStart:T=>p(T,M),onDragOver:T=>x(T,M),onDrop:v,onDragEnd:b,children:[a.jsx("div",{className:"cursor-grab text-bambu-gray/50 hover:text-bambu-gray",children:a.jsx(Ph,{className:"w-4 h-4"})}),a.jsx("span",{className:"flex-1 font-medium text-sm text-white",children:A.label}),a.jsxs("div",{className:"flex items-center gap-0.5",children:[a.jsx("button",{onClick:()=>f(M,M-1),disabled:M===0,className:"p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed",title:s("inventory.moveUp"),children:a.jsx(ql,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>f(M,M+1),disabled:M===o.length-1,className:"p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed",title:s("inventory.moveDown"),children:a.jsx(nr,{className:"w-4 h-4"})})]}),a.jsx("button",{onClick:()=>h(M),className:`p-1.5 rounded transition-colors ${A.visible?"text-bambu-green hover:bg-bambu-green/10":"text-bambu-gray/50 hover:bg-bambu-dark-secondary"}`,title:A.visible?s("inventory.hideColumn"):s("inventory.showColumn"),children:A.visible?a.jsx(Kl,{className:"w-4 h-4"}):a.jsx(gv,{className:"w-4 h-4"})})]},A.id))}),a.jsxs("div",{className:"flex items-center gap-3 mt-4 pt-4 border-t border-bambu-dark-tertiary",children:[a.jsxs(Me,{variant:"secondary",onClick:y,className:"mr-auto",children:[a.jsx(Do,{className:"w-4 h-4"}),s("inventory.reset")]}),a.jsx(Me,{variant:"secondary",onClick:e,children:s("inventory.cancel")}),a.jsx(Me,{onClick:S,children:s("inventory.applyChanges")})]})]})})})}const Zae="bambuddy-inventory-columns",S1=[{id:"id",label:"#",visible:!0},{id:"added_time",label:"Added",visible:!0},{id:"encode_time",label:"Encoded",visible:!1},{id:"last_used_time",label:"Last Used",visible:!1},{id:"rgba",label:"Color",visible:!0},{id:"material",label:"Material",visible:!0},{id:"subtype",label:"Subtype",visible:!0},{id:"color_name",label:"Color Name",visible:!1},{id:"brand",label:"Brand",visible:!0},{id:"slicer_filament",label:"Slicer Filament",visible:!1},{id:"location",label:"Location",visible:!0},{id:"label_weight",label:"Label",visible:!0},{id:"net",label:"Net",visible:!0},{id:"gross",label:"Gross",visible:!1},{id:"added_full",label:"Full",visible:!1},{id:"used",label:"Used",visible:!1},{id:"printed_total",label:"Printed Total",visible:!1},{id:"printed_since_weight",label:"Printed Since Weight",visible:!1},{id:"note",label:"Note",visible:!1},{id:"pa_k",label:"PA(K)",visible:!0},{id:"tag_id",label:"Tag ID",visible:!1},{id:"data_origin",label:"Data Origin",visible:!1},{id:"tag_type",label:"Linked Tag Type",visible:!1},{id:"remaining",label:"Remaining",visible:!0}];function DJe(){try{const t=localStorage.getItem(Zae);if(t){const e=JSON.parse(t),n=new Set(S1.map(o=>o.id)),r=new Set(e.map(o=>o.id)),i=e.filter(o=>n.has(o.id)),s=S1.filter(o=>!r.has(o.id));return[...i,...s]}}catch{}return S1.map(t=>({...t}))}function RJe(t){try{localStorage.setItem(Zae,JSON.stringify(t))}catch{}}function Fu(t,e=!1){return e&&t>=1e3?`${(t/1e3).toFixed(1)}kg`:`${Math.round(t)}g`}const FJe={PLA:"bg-green-500/20 text-green-400",ABS:"bg-red-500/20 text-red-400",PETG:"bg-blue-500/20 text-blue-400",TPU:"bg-purple-500/20 text-purple-400",ASA:"bg-orange-500/20 text-orange-400",PA:"bg-yellow-500/20 text-yellow-400",PC:"bg-cyan-500/20 text-cyan-400",PET:"bg-sky-500/20 text-sky-400"};function Kj(t){return t?new Date(t).toLocaleDateString("en-GB",{day:"2-digit",month:"2-digit",year:"2-digit"}):"-"}const LJe={id:()=>"#",added_time:()=>"Added",encode_time:()=>"Encoded",last_used_time:()=>"Last Used",rgba:t=>t("inventory.color"),material:t=>t("inventory.material"),subtype:t=>t("inventory.subtype"),color_name:t=>t("inventory.colorName"),brand:t=>t("inventory.brand"),slicer_filament:t=>t("inventory.slicerFilament"),location:()=>"Location",label_weight:t=>t("inventory.labelWeight"),net:t=>t("inventory.net"),gross:()=>"Gross",added_full:()=>"Full",used:t=>t("inventory.weightUsed"),printed_total:()=>"Printed Total",printed_since_weight:()=>"Printed Since Weight",note:t=>t("inventory.note"),pa_k:()=>"PA(K)",tag_id:()=>"Tag ID",data_origin:()=>"Data Origin",tag_type:()=>"Linked Tag Type",remaining:t=>t("inventory.remaining")},OJe={id:({spool:t})=>a.jsx("span",{className:"text-sm font-medium text-white",children:t.id}),added_time:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:Kj(t.created_at)}),encode_time:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:Kj(t.encode_time)}),last_used_time:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.last_used?Kj(t.last_used):"Never"}),rgba:({spool:t})=>a.jsx("div",{className:"flex items-center justify-center",children:a.jsx("span",{className:"w-5 h-5 rounded-full border border-white/20 flex-shrink-0",style:{backgroundColor:t.rgba?`#${t.rgba.substring(0,6)}`:"#808080"},title:t.rgba?`#${t.rgba.substring(0,6)}`:void 0})}),material:({spool:t})=>a.jsx("span",{className:"text-sm text-white",children:t.material}),subtype:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.subtype||"-"}),color_name:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:pne(t.color_name,t.rgba)||"-"}),brand:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.brand||"-"}),slicer_filament:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",title:t.slicer_filament||void 0,children:t.slicer_filament_name||t.slicer_filament||"-"}),location:({spool:t,assignmentMap:e})=>{const n=e[t.id];if(!n)return a.jsx("span",{className:"text-sm text-bambu-gray",children:"-"});const r=n.printer_name||`Printer ${n.printer_id}`,i=String.fromCharCode(65+n.ams_id),s=n.tray_id+1;return a.jsxs("span",{className:"inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-purple-500/20 text-purple-400",children:[r," ",i,s]})},label_weight:({spool:t})=>a.jsx("span",{className:"text-sm text-white",children:Fu(t.label_weight)}),net:({remaining:t})=>a.jsx("span",{className:"text-sm text-white",children:Fu(t)}),gross:({spool:t,remaining:e})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:Fu(e+t.core_weight)}),added_full:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.added_full==null?"-":t.added_full?"Yes":"No"}),used:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.weight_used>0?Fu(t.weight_used):"-"}),printed_total:()=>a.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"}),printed_since_weight:()=>a.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"}),note:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray max-w-[150px] truncate block",title:t.note||void 0,children:t.note||"-"}),pa_k:({spool:t})=>(t.k_profiles?.length??0)===0?a.jsx("span",{className:"text-sm text-bambu-gray",children:"-"}):a.jsx("span",{className:"inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-bambu-green/20 text-bambu-green",children:"K"}),tag_id:({spool:t})=>{const e=t.tag_uid||t.tray_uuid;return e?a.jsx("span",{className:"text-sm text-bambu-gray font-mono",title:e,children:e.length>12?`${e.slice(0,6)}...${e.slice(-4)}`:e}):a.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"})},data_origin:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.data_origin||"-"}),tag_type:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.tag_type||"-"}),remaining:({remaining:t,pct:e})=>a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("div",{className:"flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden",children:a.jsx("div",{className:`h-full rounded-full ${e>50?"bg-bambu-green":e>20?"bg-yellow-500":"bg-red-500"}`,style:{width:`${Math.min(e,100)}%`}})}),a.jsxs("span",{className:"text-xs text-bambu-gray min-w-[40px] text-right",children:[Math.round(t),"g"]})]})},Xj={id:t=>t.id,added_time:t=>t.created_at||"",encode_time:t=>t.encode_time||"",last_used_time:t=>t.last_used||"",material:t=>(t.material||"").toLowerCase(),subtype:t=>(t.subtype||"").toLowerCase(),color_name:t=>(t.color_name||"").toLowerCase(),brand:t=>(t.brand||"").toLowerCase(),slicer_filament:t=>(t.slicer_filament_name||t.slicer_filament||"").toLowerCase(),location:(t,e)=>{const n=e[t.id];return n?`${n.printer_name||""} ${String.fromCharCode(65+n.ams_id)}${n.tray_id+1}`:""},label_weight:t=>t.label_weight,net:t=>Math.max(0,t.label_weight-t.weight_used),gross:t=>Math.max(0,t.label_weight-t.weight_used)+t.core_weight,used:t=>t.weight_used,remaining:t=>t.label_weight>0?Math.max(0,t.label_weight-t.weight_used)/t.label_weight:0,note:t=>(t.note||"").toLowerCase(),data_origin:t=>(t.data_origin||"").toLowerCase(),tag_type:t=>(t.tag_type||"").toLowerCase()},pF="bambuddy-inventory-sort";function IJe(){try{const t=localStorage.getItem(pF);if(t)return JSON.parse(t)}catch{}return null}function zJe(t){try{t?localStorage.setItem(pF,JSON.stringify(t)):localStorage.removeItem(pF)}catch{}}function UJe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),[r,i]=w.useState(null),[s,o]=w.useState(null),[l,c]=w.useState("active"),[u,d]=w.useState("all"),[h,f]=w.useState(""),[p,x]=w.useState(""),[v,b]=w.useState(""),[y,S]=w.useState("table"),[N,A]=w.useState(IJe),[M,T]=w.useState(DJe),[k,j]=w.useState(!1),[E,P]=w.useState(0),[U,L]=w.useState(()=>{try{const Q=localStorage.getItem("bambuddy-inventory-pageSize");if(Q){const he=Number(Q);if([15,30,50,100,-1].includes(he))return he}}catch{}return 15}),{data:B,isLoading:R}=tt({queryKey:["inventory-spools"],queryFn:()=>se.getSpools(!0),refetchInterval:3e4}),{data:V}=tt({queryKey:["spool-assignments"],queryFn:()=>se.getAssignments(),refetchInterval:3e4}),q=lt({mutationFn:Q=>se.deleteSpool(Q),onSuccess:()=>{e.invalidateQueries({queryKey:["inventory-spools"]}),n(t("inventory.spoolDeleted"),"success")}}),I=lt({mutationFn:Q=>se.archiveSpool(Q),onSuccess:()=>{e.invalidateQueries({queryKey:["inventory-spools"]}),n(t("inventory.spoolArchived"),"success")}}),K=lt({mutationFn:Q=>se.restoreSpool(Q),onSuccess:()=>{e.invalidateQueries({queryKey:["inventory-spools"]}),n(t("inventory.spoolRestored"),"success")}}),Y=w.useMemo(()=>{if(!B)return null;let Q=0,he=0,be=0,ee=0;const z={};for(const D of B){if(D.archived_at)continue;ee++;const G=Math.max(0,D.label_weight-D.weight_used);Q+=G,he+=D.weight_used,(D.label_weight>0?G/D.label_weight*100:0)<20&&be++;const ve=D.material||"Unknown";z[ve]||(z[ve]={count:0,weight:0}),z[ve].count++,z[ve].weight+=G}return{totalWeight:Q,totalConsumed:he,lowStock:be,byMaterial:z,totalSpools:ee}},[B]),le=V?.length??0,H=w.useMemo(()=>{const Q={};for(const he of V||[])Q[he.spool_id]=he;return Q},[V]),X=w.useMemo(()=>Y?Object.entries(Y.byMaterial).sort((Q,he)=>he[1].weight-Q[1].weight).slice(0,4):[],[Y]),te=w.useMemo(()=>{let Q=B||[];if(l==="active"?Q=Q.filter(he=>!he.archived_at):Q=Q.filter(he=>!!he.archived_at),u==="used"?Q=Q.filter(he=>he.weight_used>0):u==="new"&&(Q=Q.filter(he=>he.weight_used===0)),h&&(Q=Q.filter(he=>he.material===h)),p&&(Q=Q.filter(he=>he.brand===p)),v){const he=v.toLowerCase();Q=Q.filter(be=>be.brand?.toLowerCase().includes(he)||be.material.toLowerCase().includes(he)||be.color_name?.toLowerCase().includes(he)||be.subtype?.toLowerCase().includes(he)||be.note?.toLowerCase().includes(he)||be.slicer_filament_name?.toLowerCase().includes(he))}return Q},[B,l,u,h,p,v]),J=()=>P(0),Se=[...new Set(B?.map(Q=>Q.material)||[])].sort(),fe=[...new Set(B?.map(Q=>Q.brand).filter(Boolean)||[])].sort(),me=l!=="active"||u!=="all"||!!h||!!p||!!v,De=Q=>{T(Q),RJe(Q)},de=w.useMemo(()=>M.filter(Q=>Q.visible).map(Q=>Q.id),[M]),ce=Q=>{Xj[Q]&&(A(he=>{let be;return he?.column===Q?be=he.direction==="asc"?{column:Q,direction:"desc"}:null:be={column:Q,direction:"asc"},zJe(be),be}),J())},Ne=w.useMemo(()=>{if(!N)return te;const Q=Xj[N.column];return Q?[...te].sort((be,ee)=>{const z=Q(be,H),D=Q(ee,H);return zD?N.direction==="asc"?1:-1:0}):te},[te,N,H]),ie=U===-1,Le=ie?Ne.length||1:U,$=Math.max(1,Math.ceil(Ne.length/Le)),W=ie?0:Math.min(E,$-1),ue=ie?Ne:Ne.slice(W*Le,(W+1)*Le),Z=Q=>{L(Q),P(0);try{localStorage.setItem("bambuddy-inventory-pageSize",String(Q))}catch{}},oe=()=>{c("active"),d("all"),f(""),x(""),b(""),J()};return a.jsxs("div",{className:"p-4 md:p-6 space-y-6",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx(ma,{className:"w-6 h-6 text-bambu-green"}),a.jsx("h1",{className:"text-2xl font-bold text-white",children:t("inventory.title")})]}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1 ml-9",children:(t("inventory.noSpools").split(".")[0],"")})]}),a.jsxs(Me,{onClick:()=>i({spool:null}),children:[a.jsx(ar,{className:"w-4 h-4"}),t("inventory.addSpool")]})]}),Y&&!R&&a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3",children:[a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(ma,{className:"w-4 h-4 text-bambu-green"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.totalInventory")})]}),a.jsx("div",{className:"text-xl font-bold text-white",children:Fu(Y.totalWeight,!0)}),a.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:[Y.totalSpools," ",Y.totalSpools!==1?t("inventory.spools"):t("inventory.spool")]})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(zF,{className:"w-4 h-4 text-blue-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.totalConsumed")})]}),a.jsx("div",{className:"text-xl font-bold text-white",children:Fu(Y.totalConsumed,!0)}),a.jsx("div",{className:"text-xs text-bambu-gray mt-1",children:t("inventory.sinceTracking")})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(rs,{className:"w-4 h-4 text-green-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.byMaterial")})]}),a.jsx("div",{className:"flex flex-wrap gap-1.5 mt-1",children:X.map(([Q,he])=>a.jsxs("span",{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${FJe[Q]||"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[Q," ",a.jsx("span",{className:"opacity-70",children:Fu(he.weight,!0)})]},Q))})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(Cr,{className:"w-4 h-4 text-purple-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.inPrinter")})]}),a.jsx("div",{className:"text-xl font-bold text-white",children:le}),a.jsx("div",{className:"text-xs text-bambu-gray mt-1",children:t("inventory.loadedInAms")})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(Cn,{className:"w-4 h-4 text-yellow-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.lowStock")})]}),a.jsx("div",{className:`text-xl font-bold ${Y.lowStock>0?"text-yellow-400":"text-white"}`,children:Y.lowStock}),a.jsx("div",{className:"text-xs text-bambu-gray mt-1",children:t("inventory.lowStockThreshold")})]})]}),a.jsxs("div",{className:"flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between",children:[a.jsxs("div",{className:"relative flex-1 max-w-md",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50"}),a.jsx("input",{type:"text",value:v,onChange:Q=>{b(Q.target.value),J()},placeholder:t("inventory.search"),className:"w-full pl-10 pr-8 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"}),v&&a.jsx("button",{onClick:()=>{b(""),J()},className:"absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[y==="table"&&a.jsxs("button",{onClick:()=>j(!0),className:"flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-bambu-gray border border-bambu-dark-tertiary rounded-lg hover:bg-bambu-dark-tertiary transition-colors",title:t("inventory.configureColumns"),children:[a.jsx(vhe,{className:"w-4 h-4"}),a.jsx("span",{className:"hidden sm:inline",children:t("inventory.columns")})]}),a.jsxs("div",{className:"flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden",children:[a.jsxs("button",{onClick:()=>S("table"),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${y==="table"?"bg-bambu-green text-white":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(npe,{className:"w-4 h-4"}),a.jsx("span",{className:"hidden sm:inline",children:t("inventory.table")})]}),a.jsxs("button",{onClick:()=>S("cards"),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${y==="cards"?"bg-bambu-green text-white":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(xv,{className:"w-4 h-4"}),a.jsx("span",{className:"hidden sm:inline",children:t("inventory.cards")})]})]})]})]}),a.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[a.jsxs("div",{className:"flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[a.jsxs("button",{onClick:()=>{c("active"),J()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${l==="active"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(ma,{className:"w-3.5 h-3.5"}),t("inventory.active")]}),a.jsxs("button",{onClick:()=>{c("archived"),J()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${l==="archived"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(Rs,{className:"w-3.5 h-3.5"}),t("inventory.archived")]})]}),a.jsx("div",{className:"w-px h-5 bg-bambu-dark-tertiary"}),a.jsxs("div",{className:"flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[a.jsx("button",{onClick:()=>{d("all"),J()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${u==="all"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:t("inventory.all")}),a.jsxs("button",{onClick:()=>{d("used"),J()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${u==="used"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(Qn,{className:"w-3.5 h-3.5"}),t("inventory.used")]}),a.jsx("button",{onClick:()=>{d("new"),J()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${u==="new"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:t("inventory.new")})]}),a.jsx("div",{className:"w-px h-5 bg-bambu-dark-tertiary"}),a.jsxs("select",{value:h,onChange:Q=>{f(Q.target.value),J()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${h?"bg-bambu-green/20 text-bambu-green border-bambu-green/30":"bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary"}`,children:[a.jsx("option",{value:"",children:t("inventory.material")}),Se.map(Q=>a.jsx("option",{value:Q,children:Q},Q))]}),a.jsxs("select",{value:p,onChange:Q=>{x(Q.target.value),J()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${p?"bg-bambu-green/20 text-bambu-green border-bambu-green/30":"bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary"}`,children:[a.jsx("option",{value:"",children:t("inventory.brand")}),fe.map(Q=>a.jsx("option",{value:Q,children:Q},Q))]}),me&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"w-px h-5 bg-bambu-dark-tertiary"}),a.jsxs("button",{onClick:oe,className:"flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors",children:[a.jsx(Ft,{className:"w-3.5 h-3.5"}),t("inventory.clearFilters")]})]}),a.jsxs("span",{className:"ml-auto text-xs text-bambu-gray",children:[Ne.length," ",Ne.length!==1?t("inventory.spools"):t("inventory.spool")]})]}),R?a.jsx("div",{className:"flex justify-center py-16",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):y==="cards"?ue.length>0?a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4",children:ue.map(Q=>{const he=Math.max(0,Q.label_weight-Q.weight_used),be=Q.label_weight>0?he/Q.label_weight*100:0,ee=Q.rgba?`#${Q.rgba.substring(0,6)}`:"#808080";return a.jsxs("div",{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary hover:border-bambu-green transition-colors cursor-pointer ${Q.archived_at?"opacity-50":""}`,onClick:()=>i({spool:Q}),children:[a.jsx("div",{className:"h-14 flex items-center justify-center",style:{backgroundColor:ee},children:a.jsx("span",{className:"bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium",children:pne(Q.color_name,Q.rgba)||"-"})}),a.jsxs("div",{className:"p-4 space-y-3",children:[a.jsxs("div",{className:"flex items-start justify-between gap-2",children:[a.jsxs("div",{children:[a.jsxs("h3",{className:"font-semibold text-white",children:[Q.material,Q.subtype?` ${Q.subtype}`:""]}),a.jsx("p",{className:"text-sm text-bambu-gray",children:Q.brand||"-"})]}),a.jsxs("span",{className:"text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded",children:["#",Q.id]})]}),a.jsxs("div",{children:[a.jsxs("div",{className:"flex justify-between text-xs text-bambu-gray mb-1",children:[a.jsx("span",{children:t("inventory.remaining")}),a.jsxs("span",{children:[Math.round(be),"%"]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("div",{className:"flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden",children:a.jsx("div",{className:`h-full rounded-full ${be>50?"bg-bambu-green":be>20?"bg-yellow-500":"bg-red-500"}`,style:{width:`${Math.min(be,100)}%`}})}),a.jsxs("span",{className:"text-xs text-bambu-gray min-w-[40px] text-right",children:[Math.round(he),"g"]})]})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-2 text-xs",children:[a.jsxs("div",{children:[a.jsxs("span",{className:"text-bambu-gray/60",children:[t("inventory.labelWeight"),": "]}),a.jsx("span",{className:"text-bambu-gray",children:Fu(Q.label_weight)})]}),a.jsxs("div",{children:[a.jsxs("span",{className:"text-bambu-gray/60",children:[t("inventory.weightUsed"),": "]}),a.jsx("span",{className:"text-bambu-gray",children:Q.weight_used>0?Fu(Q.weight_used):"-"})]})]}),Q.note&&a.jsx("div",{className:"text-xs text-bambu-gray/60 pt-2 border-t border-bambu-dark-tertiary truncate",title:Q.note,children:Q.note})]})]},Q.id)})}),a.jsx(BJe,{pageIndex:W,pageSize:U,totalRows:Ne.length,totalPages:$,onPageChange:P,onPageSizeChange:Z,t})]}):a.jsx(mK,{hasFilters:me,onAddSpool:()=>i({spool:null}),t}):ue.length>0?a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"overflow-x-auto",children:a.jsxs("table",{className:"w-full",children:[a.jsx("thead",{children:a.jsxs("tr",{className:"border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30",children:[de.map(Q=>{const he=!!Xj[Q],be=N?.column===Q;return a.jsx("th",{className:`text-left py-3 px-4 text-xs font-medium uppercase tracking-wide select-none ${Q==="remaining"?"min-w-[150px]":""} ${he?"cursor-pointer hover:text-bambu-green transition-colors":""} ${be?"text-bambu-green":"text-bambu-gray"}`,onClick:he?()=>ce(Q):void 0,children:a.jsxs("span",{className:"inline-flex items-center gap-1",children:[LJe[Q]?.(t)??Q,he&&(be?N.direction==="asc"?a.jsx(Ef,{className:"w-3 h-3"}):a.jsx(hb,{className:"w-3 h-3"}):a.jsx(DF,{className:"w-3 h-3 opacity-30"}))]})},Q)}),a.jsx("th",{className:"text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide",children:t("common.actions")})]})}),a.jsx("tbody",{children:ue.map(Q=>{const he=Math.max(0,Q.label_weight-Q.weight_used),be=Q.label_weight>0?he/Q.label_weight*100:0;return a.jsxs("tr",{className:`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer ${Q.archived_at?"opacity-50":""}`,onClick:()=>i({spool:Q}),children:[de.map(ee=>a.jsx("td",{className:"py-3 px-4",children:OJe[ee]?.({spool:Q,remaining:he,pct:be,assignmentMap:H})},ee)),a.jsx("td",{className:"py-3 px-4",children:a.jsxs("div",{className:"flex items-center justify-end gap-1",onClick:ee=>ee.stopPropagation(),children:[a.jsx("button",{onClick:()=>i({spool:Q}),className:"p-1.5 text-bambu-gray hover:text-white rounded transition-colors",title:t("inventory.editSpool"),children:a.jsx(rf,{className:"w-4 h-4"})}),Q.archived_at?a.jsx("button",{onClick:()=>K.mutate(Q.id),className:"p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors",title:t("inventory.restore"),children:a.jsx(Do,{className:"w-4 h-4"})}):a.jsx("button",{onClick:()=>o({type:"archive",spoolId:Q.id}),className:"p-1.5 text-bambu-gray hover:text-yellow-400 rounded transition-colors",title:t("inventory.archive"),children:a.jsx(Rs,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>o({type:"delete",spoolId:Q.id}),className:"p-1.5 text-bambu-gray hover:text-red-400 rounded transition-colors",title:t("common.delete"),children:a.jsx(en,{className:"w-4 h-4"})})]})})]},Q.id)})})]})}),a.jsxs("div",{className:"flex items-center justify-between px-4 py-3 bg-bambu-dark-tertiary/50 border-t border-bambu-dark-tertiary text-sm",children:[a.jsx("span",{className:"text-bambu-gray",children:ie?`${Ne.length} ${Ne.length!==1?t("inventory.spools"):t("inventory.spool")}`:a.jsxs(a.Fragment,{children:[t("inventory.showing")," ",W*Le+1," ",t("inventory.to")," ",Math.min((W+1)*Le,Ne.length)," ",t("inventory.of")," ",Ne.length," ",t("inventory.spools")]})}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-bambu-gray",children:t("inventory.show")}),a.jsxs("select",{value:U,onChange:Q=>Z(Number(Q.target.value)),className:"px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green",children:[[15,30,50,100].map(Q=>a.jsx("option",{value:Q,children:Q},Q)),a.jsx("option",{value:-1,children:t("inventory.all")})]}),!ie&&a.jsxs(a.Fragment,{children:[a.jsx("button",{onClick:()=>P(0),disabled:W===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",title:"First page",children:a.jsx(lX,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>P(Q=>Math.max(0,Q-1)),disabled:W===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(Gl,{className:"w-4 h-4"})}),a.jsxs("span",{className:"text-bambu-gray px-2 whitespace-nowrap",children:[t("inventory.page")," ",W+1," ",t("inventory.of")," ",$]}),a.jsx("button",{onClick:()=>P(Q=>Math.min($-1,Q+1)),disabled:W>=$-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(fa,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>P($-1),disabled:W>=$-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",title:"Last page",children:a.jsx(cX,{className:"w-4 h-4"})})]})]})]})]}):a.jsx(mK,{hasFilters:me,onAddSpool:()=>i({spool:null}),t}),r!==null&&a.jsx(PJe,{isOpen:!0,onClose:()=>i(null),spool:r.spool}),s&&a.jsx(kn,{title:s.type==="delete"?t("common.delete"):t("inventory.archive"),message:s.type==="delete"?t("inventory.deleteConfirm"):t("inventory.archiveConfirm"),confirmText:s.type==="delete"?t("common.delete"):t("inventory.archive"),variant:s.type==="delete"?"danger":"warning",onConfirm:()=>{s.type==="delete"?q.mutate(s.spoolId):I.mutate(s.spoolId),o(null)},onCancel:()=>o(null)}),a.jsx(jJe,{isOpen:k,onClose:()=>j(!1),columns:M,defaultColumns:S1,onSave:De})]})}function BJe({pageIndex:t,pageSize:e,totalRows:n,totalPages:r,onPageChange:i,onPageSizeChange:s,t:o}){const l=e===-1;if(r<=1&&!l)return null;const c=l?n||1:e;return a.jsxs("div",{className:"flex items-center justify-between pt-2 text-sm",children:[a.jsx("span",{className:"text-bambu-gray",children:l?`${n} ${o(n!==1?"inventory.spools":"inventory.spool")}`:a.jsxs(a.Fragment,{children:[o("inventory.showing")," ",t*c+1," ",o("inventory.to")," ",Math.min((t+1)*c,n)," ",o("inventory.of")," ",n," ",o("inventory.spools")]})}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-bambu-gray",children:o("inventory.show")}),a.jsxs("select",{value:e,onChange:u=>s(Number(u.target.value)),className:"px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green",children:[[15,30,50,100].map(u=>a.jsx("option",{value:u,children:u},u)),a.jsx("option",{value:-1,children:o("inventory.all")})]}),!l&&a.jsxs(a.Fragment,{children:[a.jsx("button",{onClick:()=>i(0),disabled:t===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(lX,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>i(Math.max(0,t-1)),disabled:t===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(Gl,{className:"w-4 h-4"})}),a.jsxs("span",{className:"text-bambu-gray px-2 whitespace-nowrap",children:[o("inventory.page")," ",t+1," ",o("inventory.of")," ",r]}),a.jsx("button",{onClick:()=>i(Math.min(r-1,t+1)),disabled:t>=r-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(fa,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>i(r-1),disabled:t>=r-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(cX,{className:"w-4 h-4"})})]})]})]})}function mK({hasFilters:t,onAddSpool:e,t:n}){return a.jsxs("div",{className:"flex flex-col items-center justify-center py-16 px-4",children:[a.jsxs("div",{className:"relative mb-6",children:[a.jsx("div",{className:"absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl"}),a.jsxs("div",{className:"relative flex items-center justify-center w-24 h-24 rounded-2xl bg-gradient-to-br from-bambu-dark-secondary to-bambu-dark-tertiary border border-bambu-dark-tertiary shadow-lg",children:[a.jsx("div",{className:"absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30"}),a.jsx("div",{className:"absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20"}),t?a.jsx(Or,{className:"w-10 h-10 text-bambu-gray/40",strokeWidth:1.5}):a.jsxs("div",{className:"relative",children:[a.jsx("div",{className:"w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center",children:a.jsx("div",{className:"w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20"})}),a.jsx("div",{className:"absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-bambu-green flex items-center justify-center shadow-md",children:a.jsx("span",{className:"text-white text-lg font-bold leading-none",children:"+"})})]})]})]}),a.jsx("h3",{className:"text-lg font-semibold text-white mb-2 text-center",children:t?n("inventory.noSpoolsMatch"):n("inventory.noSpools").split(".")[0]}),a.jsx("p",{className:"text-sm text-bambu-gray text-center max-w-sm mb-6",children:n(t?"inventory.noSpoolsMatchDesc":"inventory.noSpools")}),!t&&a.jsxs(Me,{onClick:e,children:[a.jsx(ma,{className:"w-4 h-4"}),n("inventory.addSpool")]})]})}const fK=["DEBUG","INFO","WARNING","ERROR"],Yj={DEBUG:"text-gray-400",INFO:"text-blue-400",WARNING:"text-yellow-400",ERROR:"text-red-400"},HJe={DEBUG:Zk,INFO:Zs,WARNING:Cn,ERROR:Bi};function $Je(){const t=tn(),[e,n]=w.useState(!0),[r,i]=w.useState(new Set),[s,o]=w.useState(""),[l,c]=w.useState("ALL"),[u,d]=w.useState(!1),[h,f]=w.useState(!1),p=w.useRef(null),{data:x,isLoading:v,refetch:b}=tt({queryKey:["application-logs",l,s],queryFn:()=>Zg.getLogs({limit:200,level:l==="ALL"?void 0:l,search:s||void 0}),refetchInterval:h?2e3:!1,enabled:u});w.useEffect(()=>{u||f(!1)},[u]);const y=lt({mutationFn:()=>Zg.clearLogs(),onSuccess:()=>{t.invalidateQueries({queryKey:["application-logs"]})}});w.useEffect(()=>{e&&p.current&&x?.entries&&(p.current.scrollTop=p.current.scrollHeight)},[x?.entries,e]);const S=k=>{i(j=>{const E=new Set(j);return E.has(k)?E.delete(k):E.add(k),E})},N=k=>{const j=k.split(" ");return j.length>=2?j[1]:k},A=w.useMemo(()=>x?.entries??[],[x?.entries]),M=w.useMemo(()=>[...A].reverse(),[A]),T=({level:k})=>{const j=HJe[k]||Zs;return a.jsx(j,{className:`w-3.5 h-3.5 ${Yj[k]||"text-gray-400"}`})};return a.jsxs("div",{className:"bg-bambu-dark rounded-lg overflow-hidden",children:[a.jsxs("button",{onClick:()=>d(!u),className:"w-full flex items-center justify-between p-4 hover:bg-bambu-dark-tertiary/50 transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:`p-2 rounded-lg ${h?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:a.jsx(Zk,{className:"w-5 h-5"})}),a.jsxs("div",{className:"text-left",children:[a.jsx("p",{className:"font-medium text-white",children:"Application Logs"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:h?`Live streaming - ${x?.filtered_count??0} entries`:"View and filter application logs"})]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[h&&a.jsxs("span",{className:"flex items-center gap-1.5 px-2 py-1 bg-bambu-green/20 rounded text-bambu-green text-xs",children:[a.jsx("span",{className:"w-1.5 h-1.5 bg-bambu-green rounded-full animate-pulse"}),"Live"]}),u?a.jsx(ql,{className:"w-5 h-5 text-bambu-gray"}):a.jsx(nr,{className:"w-5 h-5 text-bambu-gray"})]})]}),u&&a.jsxs("div",{className:"border-t border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex flex-col gap-2 p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[h?a.jsxs("button",{onClick:k=>{k.stopPropagation(),f(!1)},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-red-500/20 text-red-400 hover:bg-red-500/30 rounded transition-colors",children:[a.jsx(gl,{className:"w-4 h-4"}),"Stop"]}):a.jsxs("button",{onClick:k=>{k.stopPropagation(),f(!0),b()},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30 rounded transition-colors",children:[a.jsx(jo,{className:"w-4 h-4"}),"Start"]}),a.jsxs("button",{onClick:()=>y.mutate(),disabled:y.isPending||A.length===0,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:[a.jsx(en,{className:"w-4 h-4"}),"Clear"]}),a.jsx("button",{onClick:()=>b(),disabled:v,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:a.jsx(kr,{className:`w-4 h-4 ${v?"animate-spin":""}`})}),a.jsx("div",{className:"flex-1"}),a.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:e,onChange:k=>n(k.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark-tertiary"}),"Auto-scroll"]}),a.jsxs("span",{className:"text-sm text-bambu-gray",children:[x?.filtered_count??0,"/",x?.total_in_file??0]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx(Or,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",placeholder:"Search message or logger name...",value:s,onChange:k=>o(k.target.value),className:"w-full pl-8 pr-8 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),s&&a.jsx("button",{onClick:()=>o(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),a.jsxs("div",{className:"flex items-center gap-1 bg-bambu-dark-secondary rounded border border-bambu-dark-tertiary",children:[a.jsx("button",{onClick:()=>c("ALL"),className:`px-2 py-1.5 text-xs rounded-l transition-colors ${l==="ALL"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:"All"}),fK.map((k,j)=>a.jsx("button",{onClick:()=>c(k),className:`px-2 py-1.5 text-xs transition-colors flex items-center gap-1 ${j===fK.length-1?"rounded-r":""} ${l===k?`${Yj[k]} bg-bambu-dark-tertiary`:"text-bambu-gray hover:text-white"}`,children:k},k))]})]})]}),a.jsx("div",{ref:p,className:"overflow-auto font-mono text-xs bg-black min-h-[300px] max-h-[500px]",children:A.length===0?a.jsxs("div",{className:"flex flex-col items-center justify-center h-[300px] text-bambu-gray",children:[a.jsx("p",{className:"mb-2",children:"No log entries found"}),a.jsx("p",{className:"text-sm",children:"Log file may be empty or cleared"})]}):a.jsx("div",{className:"divide-y divide-bambu-dark-tertiary/30",children:M.map((k,j)=>{const E=r.has(j),P=k.message.includes(` +`+n+"}"}return String(t)}function MZe({setting:t,onClose:e,onDeleted:n,onDuplicate:r,onEdit:i,hasPermission:s,t:o}){const{showToast:l}=mn(),c=tn(),[u,d]=w.useState(!1),{data:h,isLoading:f}=tt({queryKey:["cloudSettingDetail",t.setting_id],queryFn:()=>se.getCloudSettingDetail(t.setting_id)}),p=lt({mutationFn:()=>se.deleteCloudSetting(t.setting_id),onSuccess:()=>{l(o("profiles.presets.toast.deleted")),c.invalidateQueries({queryKey:["cloudSettings"]}),n()},onError:b=>l(b.message,"error")}),x=mv(t.setting_id),v=bI(t.name,h?.setting?.inherits);return a.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:a.jsx(_t,{className:"w-full max-w-3xl max-h-[90vh] flex flex-col overflow-hidden",children:a.jsxs(Nt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[a.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("h2",{className:"text-xl font-semibold text-white truncate",children:t.name}),x&&a.jsx("span",{className:"px-2 py-0.5 text-xs font-medium bg-bambu-green/20 text-bambu-green rounded-full",children:o("profiles.presets.editable")})]}),a.jsxs("div",{className:"flex items-center gap-2 mt-1 text-sm text-bambu-gray",children:[a.jsx("span",{className:"capitalize",children:o(`profiles.presets.types.${t.type}`)}),v.printer&&a.jsxs(a.Fragment,{children:[a.jsx("span",{children:"•"}),a.jsx("span",{children:v.printer})]})]})]}),a.jsx("button",{onClick:e,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto p-4",children:f?a.jsx("div",{className:"flex items-center justify-center py-16",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):h?a.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:mF(h)}):a.jsx("div",{className:"text-center py-16 text-bambu-gray",children:o("profiles.presets.failedToLoadDetails")})}),u?a.jsxs("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary bg-red-500/5",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 text-red-400",children:[a.jsx(Cn,{className:"w-5 h-5"}),a.jsx("span",{className:"font-medium",children:o("profiles.presets.deleteConfirm")})]}),a.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:o("profiles.presets.deleteWarning",{name:t.name})}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:()=>d(!1),disabled:p.isPending,className:"flex-1",children:o("common.cancel")}),a.jsxs(Me,{variant:"danger",onClick:()=>p.mutate(),disabled:p.isPending,className:"flex-1",children:[p.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):a.jsx(en,{className:"w-4 h-4"}),o("common.delete")]})]})]}):a.jsx("div",{className:"flex-shrink-0 p-4 border-t border-bambu-dark-tertiary",children:a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:e,className:"flex-1",children:o("common.close")}),a.jsxs(Me,{variant:"secondary",onClick:r,disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.presets.noDuplicatePermission"),children:[a.jsx(Wl,{className:"w-4 h-4"}),o("profiles.presets.duplicate")]}),x&&a.jsxs(a.Fragment,{children:[a.jsxs(Me,{variant:"secondary",onClick:i,disabled:f||!h||!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.presets.noEditPermission"),children:[a.jsx($i,{className:"w-4 h-4"}),o("common.edit")]}),a.jsx(Me,{variant:"danger",onClick:()=>d(!0),disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.presets.noDeletePermission"),children:a.jsx(en,{className:"w-4 h-4"})})]})]})})]})})})}function Gae(){try{const t=localStorage.getItem("bambusy_preset_templates");return t?JSON.parse(t):[]}catch{return[]}}function qae(t){localStorage.setItem("bambusy_preset_templates",JSON.stringify(t))}function AZe({onClose:t,onApply:e,t:n}){const{showToast:r}=mn(),[i,s]=w.useState(Gae),[o,l]=w.useState("all"),[c,u]=w.useState(null),[d,h]=w.useState(""),[f,p]=w.useState(""),[x,v]=w.useState("{}"),[b,y]=w.useState(null),[S,N]=w.useState(null),A=o==="all"?i:i.filter(B=>B.type===o),M=B=>{s(B),qae(B)},T=B=>{const R=i.filter(V=>V.id!==B);M(R),N(null),r(n("profiles.templates.toast.deleted"))},k=B=>{u(B.id),h(B.name),p(B.description),v(JSON.stringify(B.settings,null,2)),y(null)},j=()=>{if(!(!c||!d.trim()))try{const B=JSON.parse(x),R=i.map(V=>V.id===c?{...V,name:d.trim(),description:f.trim(),settings:B}:V);M(R),u(null),r(n("profiles.templates.toast.updated"))}catch(B){y(B.message)}},E=()=>{u(null),h(""),p(""),v("{}"),y(null)},P=B=>{const R=i.map(V=>V.id===B?{...V,showInModal:!V.showInModal}:V);M(R)},U={filament:{label:n("profiles.presets.types.filament"),icon:Jk,color:"text-amber-400"},print:{label:n("profiles.presets.types.process"),icon:td,color:"text-blue-400"},printer:{label:n("profiles.presets.types.printer"),icon:Cr,color:"text-purple-400"}},L=S?i.find(B=>B.id===S):null;return w.useEffect(()=>{const B=R=>{R.key==="Escape"&&(S?N(null):c?E():t())};return window.addEventListener("keydown",B),()=>window.removeEventListener("keydown",B)},[S,c,t]),a.jsxs("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:[L&&a.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-60",children:a.jsx(_t,{className:"w-full max-w-md",children:a.jsxs(Nt,{className:"p-6",children:[a.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[a.jsx("div",{className:"p-2 bg-red-500/20 rounded-lg",children:a.jsx(Cn,{className:"w-6 h-6 text-red-400"})}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-lg font-semibold text-white",children:n("profiles.templates.deleteTitle")}),a.jsx("p",{className:"text-sm text-bambu-gray",children:n("profiles.templates.deleteWarning")})]})]}),a.jsx("p",{className:"text-white mb-6",children:n("profiles.templates.deleteConfirm",{name:L.name})}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:()=>N(null),className:"flex-1",children:n("common.cancel")}),a.jsxs(Me,{onClick:()=>T(S),className:"flex-1 bg-red-500 hover:bg-red-600",children:[a.jsx(en,{className:"w-4 h-4"}),n("common.delete")]})]})]})})}),a.jsx(_t,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",children:a.jsxs(Nt,{className:"p-0 flex flex-col h-full",children:[a.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(pb,{className:"w-5 h-5 text-amber-400"}),n("profiles.templates.title")]}),a.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsxs("div",{className:"flex items-center gap-2 p-4 border-b border-bambu-dark-tertiary",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:n("profiles.templates.typeFilter")}),["all","filament","print","printer"].map(B=>a.jsx("button",{onClick:()=>l(B),className:`px-3 py-1 text-sm rounded-lg transition-colors ${o===B?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:B==="all"?n("common.all"):U[B].label},B))]}),a.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:A.length===0?a.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[a.jsx(pb,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),a.jsx("p",{children:n("profiles.templates.noTemplates")}),a.jsx("p",{className:"text-sm mt-1",children:n("profiles.templates.createFirst")})]}):a.jsx("div",{className:"space-y-2",children:A.map(B=>{const R=U[B.type],V=R.icon;return c===B.id?a.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg border border-bambu-green",children:[a.jsxs("div",{className:"grid grid-cols-2 gap-3 mb-3",children:[a.jsx("input",{type:"text",value:d,onChange:q=>h(q.target.value),placeholder:n("profiles.templates.namePlaceholder"),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}),a.jsx("input",{type:"text",value:f,onChange:q=>p(q.target.value),placeholder:n("profiles.templates.descriptionPlaceholder"),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"})]}),a.jsxs("div",{className:"mb-3",children:[a.jsx("label",{className:"text-xs text-bambu-gray mb-1 block",children:n("profiles.templates.settingsJson")}),a.jsx("textarea",{value:x,onChange:q=>{v(q.target.value),y(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 ${b?"border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`}),b&&a.jsx("p",{className:"text-xs text-red-400 mt-1",children:b})]}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{size:"sm",onClick:j,disabled:!d.trim(),children:[a.jsx(bs,{className:"w-4 h-4"}),"Save"]}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:E,children:"Cancel"})]})]},B.id):a.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:[a.jsx(V,{className:`w-5 h-5 ${R.color} flex-shrink-0`}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm font-medium text-white",children:B.name}),a.jsx("p",{className:"text-xs text-bambu-gray truncate",children:B.description})]}),a.jsx("span",{className:"text-xs text-bambu-gray-dark px-2 py-1 bg-bambu-dark-secondary rounded",children:n("profiles.templates.fieldsCount",{count:Object.keys(B.settings).length})}),a.jsx("button",{onClick:()=>P(B.id),className:`p-1 transition-colors ${B.showInModal?"text-bambu-green hover:text-bambu-green/70":"text-bambu-gray hover:text-white"}`,title:B.showInModal?n("profiles.templates.shownInModals"):n("profiles.templates.hiddenInModals"),children:B.showInModal?a.jsx(Kl,{className:"w-4 h-4"}):a.jsx(gv,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>e(B),className:"px-3 py-1 text-xs bg-bambu-green/20 text-bambu-green rounded hover:bg-bambu-green/30 transition-colors",children:n("profiles.templates.apply")}),a.jsx("button",{onClick:()=>k(B),className:"p-1 text-bambu-gray hover:text-white",title:n("common.edit"),children:a.jsx($i,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>N(B.id),className:"p-1 text-bambu-gray hover:text-red-400",title:n("common.delete"),children:a.jsx(en,{className:"w-4 h-4"})})]},B.id)})})})]})})]})}function Wae({onClose:t,leftPreset:e,rightPreset:n,leftLabel:r,rightLabel:i,t:s}){const[o,l]=w.useState("changes"),[c,u]=w.useState(""),d=w.useMemo(()=>{const x=new Set([...Object.keys(e),...Object.keys(n)]),v=[];for(const b of x){if(b==="inherits"||b==="version")continue;const y=e[b],S=n[b],N=b in e,A=b in n;let M;!N&&A?M="added":N&&!A?M="removed":JSON.stringify(y)!==JSON.stringify(S)?M="changed":M="same",v.push({key:b,left:y,right:S,status:M})}return v.sort((b,y)=>{const S={changed:0,added:1,removed:2,same:3};return S[b.status]!==S[y.status]?S[b.status]-S[y.status]:b.key.localeCompare(y.key)})},[e,n]),h=w.useMemo(()=>{let x=[...d];if(o==="changes"&&(x=x.filter(v=>v.status!=="same")),c){const v=c.toLowerCase();x=x.filter(b=>b.key.toLowerCase().includes(v)||String(b.left).toLowerCase().includes(v)||String(b.right).toLowerCase().includes(v))}return x},[d,o,c]),f=w.useMemo(()=>({added:d.filter(x=>x.status==="added").length,removed:d.filter(x=>x.status==="removed").length,changed:d.filter(x=>x.status==="changed").length,same:d.filter(x=>x.status==="same").length}),[d]),p=x=>{if(x===void 0)return"—";if(x===null)return"null";if(Array.isArray(x))return x.length===0?"[]":x.length===1?String(x[0]):x.join(", ");if(typeof x=="object")return JSON.stringify(x);const v=String(x);if(v.includes("\\n")||v.length>100){const b=v.split("\\n").length;if(b>1)return`[${b} lines of G-code/script]`;if(v.length>100)return v.substring(0,100)+"…"}return v};return w.useEffect(()=>{const x=v=>{v.key==="Escape"&&t()};return window.addEventListener("keydown",x),()=>window.removeEventListener("keydown",x)},[t]),a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsx(_t,{className:"w-full max-w-4xl max-h-[85vh] flex flex-col overflow-hidden",children:a.jsxs(Nt,{className:"p-0 flex flex-col min-h-0 flex-1",children:[a.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(Yg,{className:"w-5 h-5 text-blue-400"}),s("profiles.diff.title")]}),a.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsxs("div",{className:"flex-shrink-0 grid grid-cols-2 gap-4 p-4 border-b border-bambu-dark-tertiary bg-bambu-dark",children:[a.jsxs("div",{className:"text-center",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:s("profiles.diff.left")}),a.jsx("p",{className:"text-white font-medium truncate",children:r})]}),a.jsxs("div",{className:"text-center",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:s("profiles.diff.right")}),a.jsx("p",{className:"text-white font-medium truncate",children:i})]})]}),a.jsxs("div",{className:"flex-shrink-0 flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[a.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[a.jsx(ar,{className:"w-3.5 h-3.5"}),f.added," ",s("profiles.diff.added")]}),a.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[a.jsx(x3,{className:"w-3.5 h-3.5"}),f.removed," ",s("profiles.diff.removed")]}),a.jsxs("span",{className:"flex items-center gap-1 text-amber-400",children:[a.jsx(d3,{className:"w-3.5 h-3.5"}),f.changed," ",s("profiles.diff.changed")]}),a.jsxs("span",{className:"flex items-center gap-1 text-bambu-gray",children:[a.jsx(ZT,{className:"w-3.5 h-3.5"}),f.same," ",s("profiles.diff.same")]})]}),a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsxs("div",{className:"relative",children:[a.jsx(Or,{className:"absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",value:c,onChange:x=>u(x.target.value),placeholder:s("profiles.diff.searchFields"),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&&a.jsxs("div",{className:"flex rounded overflow-hidden border border-bambu-dark-tertiary",children:[a.jsx("button",{onClick:()=>l("changes"),className:`px-3 py-1.5 text-sm transition-colors ${o==="changes"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:s("profiles.diff.changes")}),a.jsx("button",{onClick:()=>l("all"),className:`px-3 py-1.5 text-sm transition-colors ${o==="all"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:s("common.all")})]})]})]}),a.jsx("div",{className:"flex-1 min-h-0 overflow-y-auto",children:h.length===0?a.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[a.jsx(ZT,{className:"w-12 h-12 mx-auto mb-4 opacity-30"}),a.jsx("p",{children:s(o==="changes"?"profiles.diff.noDifferences":"profiles.diff.noFieldsMatch")})]}):a.jsxs("table",{className:"w-full",children:[a.jsx("thead",{className:"sticky top-0 bg-bambu-dark-secondary",children:a.jsxs("tr",{className:"text-sm text-bambu-gray border-b border-bambu-dark-tertiary",children:[a.jsx("th",{className:"text-left p-3 w-1/3",children:s("profiles.diff.field")}),a.jsx("th",{className:"text-left p-3 w-1/3",children:r}),a.jsx("th",{className:"text-left p-3 w-1/3",children:i})]})}),a.jsx("tbody",{children:h.map(x=>{const v={added:"bg-green-500/10",removed:"bg-red-500/10",changed:"bg-amber-500/10",same:""}[x.status],b={added:a.jsx(ar,{className:"w-3.5 h-3.5 text-green-400"}),removed:a.jsx(x3,{className:"w-3.5 h-3.5 text-red-400"}),changed:a.jsx(d3,{className:"w-3.5 h-3.5 text-amber-400"}),same:a.jsx(ZT,{className:"w-3.5 h-3.5 text-bambu-gray-dark"})}[x.status];return a.jsxs("tr",{className:`border-b border-bambu-dark-tertiary ${v}`,children:[a.jsx("td",{className:"p-3",children:a.jsxs("div",{className:"flex items-center gap-2",children:[b,a.jsx("span",{className:"text-sm text-white font-mono",children:x.key})]})}),a.jsx("td",{className:"p-3",children:a.jsx("span",{className:`text-sm font-mono break-all ${x.status==="removed"?"text-red-300":x.status==="changed"?"text-white":"text-bambu-gray"}`,children:p(x.left)})}),a.jsx("td",{className:"p-3",children:a.jsx("span",{className:`text-sm font-mono break-all ${x.status==="added"?"text-green-300":x.status==="changed"?"text-white":"text-bambu-gray"}`,children:p(x.right)})})]},x.key)})})]})})]})})})}function TZe({onClose:t,initialData:e,allPresets:n,t:r}){const{showToast:i}=mn(),s=tn(),o=!!e?.setting_id,[l,c]=w.useState("common"),[u,d]=w.useState(e?.type||"filament"),[h,f]=w.useState(e?.name?o?e.name:`${e.name} (Copy)`:""),[p,x]=w.useState(e?.base_id||""),[v,b]=w.useState(""),[y,S]=w.useState(e?.setting||{inherits:""}),[N,A]=w.useState(JSON.stringify(e?.setting||{inherits:""},null,2)),[M,T]=w.useState(null),[k,j]=w.useState(""),[E,P]=w.useState(!1),[U,L]=w.useState(""),[B,R]=w.useState(!1),[V,q]=w.useState(Gae),[I,K]=w.useState(!1),[Y,le]=w.useState(""),[H,X]=w.useState(""),[te,J]=w.useState(!0),[Se,fe]=w.useState(null),[me,De]=w.useState(!1),de=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer})[u]||[],[n,u]),ce=w.useMemo(()=>de.filter(ke=>mv(ke.setting_id)),[de]),{data:Ne}=tt({queryKey:["cloudFields",u],queryFn:()=>se.getCloudFields(u==="print"?"process":u),staleTime:1e3*60*60}),{data:ie}=tt({queryKey:["allPresetDetails",u,ce.map(ke=>ke.setting_id).join(",")],queryFn:async()=>{const ke={};for(let je=0;je{try{const Pe=await se.getCloudSettingDetail(Je.setting_id);return{id:Je.setting_id,detail:Pe}}catch{return null}}))).forEach(Je=>{Je&&(ke[Je.id]=Je.detail)})}return ke},enabled:ce.length>0,staleTime:1e3*60*10}),{data:Le,isLoading:$}=tt({queryKey:["cloudSettingDetail",p],queryFn:()=>se.getCloudSettingDetail(p),enabled:!!p});w.useEffect(()=>{l!=="json"&&A(JSON.stringify(y,null,2))},[y,l]);const W=w.useMemo(()=>({filament:n.filament,print:n.process,printer:n.printer}[u]||[]).filter(Ue=>!mv(Ue.setting_id)).sort((Ue,je)=>Ue.name.localeCompare(je.name)),[n,u]);w.useEffect(()=>{if(!p)return;const ke=W.find(Ue=>Ue.setting_id===p);ke&&(b(ke.name),o||(S({inherits:ke.name}),A(JSON.stringify({inherits:ke.name},null,2))))},[p,W,o]);const ue=w.useMemo(()=>{const ke=Ne?.fields||[],Ue=new Set(ke.map(Je=>Je.key)),je=new Set,Ve=new Set(["inherits","updated_time","compatible_printers","compatible_prints"]);ie&&Object.values(ie).forEach(Je=>{Je?.setting&&Object.keys(Je.setting).forEach(Pe=>{!Ue.has(Pe)&&!Ve.has(Pe)&&je.add(Pe)})}),Object.keys(y).forEach(Je=>{!Ue.has(Je)&&!Ve.has(Je)&&je.add(Je)});const Ae=Array.from(je).sort().map(Je=>({key:Je,label:Je.replace(/_/g," ").replace(/\b\w/g,Pe=>Pe.toUpperCase()),type:"text",category:"discovered",description:r("profiles.presets.discoveredFromPresets")}));return[...ke,...Ae]},[Ne,ie,y,r]),Z=ue.filter(ke=>ke.label.toLowerCase().includes(k.toLowerCase())||ke.key.toLowerCase().includes(k.toLowerCase())),oe=()=>{if(U.trim()){const ke=U.trim().toLowerCase().replace(/\s+/g,"_");Q(ke,""),L(""),R(!1),i(r("profiles.presets.toast.fieldAdded",{key:ke}))}},Q=(ke,Ue)=>{S(je=>{const Ve={...je};return Ue===""||Ue===void 0?delete Ve[ke]:Ve[ke]=Ue,Ve})},he=ke=>{S(Ue=>({...Ue,...ke.settings})),fe(ke.name),i(r("profiles.templates.toast.applied"))},be=()=>{if(!Y.trim())return;const ke={...y};if(delete ke.inherits,Object.keys(ke).length===0){i(r("profiles.presets.noOverridesToSave"),"error");return}const Ue={id:Date.now().toString(),name:Y.trim(),description:H.trim()||r("profiles.presets.customTemplate"),type:u,settings:ke,showInModal:te},je=[...V,Ue];q(je),qae(je),K(!1),le(""),X(""),J(!0),i(r("profiles.templates.toast.created"))},ee=w.useMemo(()=>V.filter(ke=>ke.type===u&&ke.showInModal),[u,V]),z=ke=>{A(ke);try{const Ue=JSON.parse(ke);S(Ue),T(null)}catch(Ue){T(Ue.message)}},D=ke=>{ke.preventDefault(),P(!1);const Ue=ke.dataTransfer.files[0];if(Ue&&Ue.name.endsWith(".json")){const je=new FileReader;je.onload=Ve=>{try{const Ae=Ve.target?.result,Je=JSON.parse(Ae),Pe=Je.setting||Je;S(Ye=>({...Ye,...Pe})),A(JSON.stringify({...y,...Pe},null,2)),i(r("profiles.presets.fileImported"))}catch{i(r("profiles.presets.invalidJsonFile"),"error")}},je.readAsText(Ue)}},G=lt({mutationFn:()=>{const ke={...y},Ue=u==="filament"?"filament_settings_id":u==="print"?"print_settings_id":"printer_settings_id";ke[Ue]=`"${h}"`;const je={type:u,name:h,base_id:p,setting:ke};return se.createCloudSetting(je)},onSuccess:async()=>{i(r("profiles.presets.toast.created")),await s.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ke=>i(ke.message,"error")}),pe=lt({mutationFn:()=>{if(!e?.setting_id)throw new Error(r("profiles.presets.noSettingId"));return se.updateCloudSetting(e.setting_id,{name:h,setting:y})},onSuccess:async()=>{i(r("profiles.presets.toast.updated")),s.removeQueries({queryKey:["cloudSettingDetail"]}),await s.refetchQueries({queryKey:["cloudSettings"]}),t()},onError:ke=>i(ke.message,"error")}),ve=o?pe:G,xe=Le?.setting?.inherits,ze=xe?W.find(ke=>ke.name===xe):void 0,{data:_e}=tt({queryKey:["cloudSettingDetail",ze?.setting_id],queryFn:()=>se.getCloudSettingDetail(ze.setting_id),enabled:!!ze?.setting_id}),He=w.useMemo(()=>{const ke=_e?.setting||{},Ue=Le?.setting||{},je=p&&ie?.[p]?.setting?ie[p].setting:{};return{...ke,...Ue,...je}},[p,Le,_e,ie]),Ze=ke=>{if(ke==null)return"";if(Array.isArray(ke)){const Ue=[...new Set(ke.map(je=>String(je)))];return Ue.length===1?Ue[0]:ke.join(", ")}return String(ke)},we=ke=>{const Ue=y[ke.key],je=He[ke.key],Ve=Ze(je),Ae=$?r("common.loading"):Ve||"",Je="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(ke.type==="boolean"){const Pe=Ue==="1"||Ue===void 0&&je==="1";return a.jsx("button",{type:"button",onClick:()=>Q(ke.key,Ue==="1"?"0":"1"),className:`w-8 h-5 rounded-full transition-colors ${Pe?"bg-bambu-green":"bg-bambu-dark-tertiary"}`,children:a.jsx("div",{className:`w-4 h-4 rounded-full bg-white shadow transition-transform ${Pe?"translate-x-3.5":"translate-x-0.5"}`})})}return ke.type==="select"?a.jsxs("select",{value:Ue||"",onChange:Pe=>Q(ke.key,Pe.target.value),className:Je,children:[a.jsx("option",{value:"",children:Ae}),ke.options?.map(Pe=>a.jsx("option",{value:Pe.value,children:Pe.label},Pe.value))]}):a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("input",{type:ke.type==="number"?"number":"text",value:Ue!==void 0?String(Ue):"",onChange:Pe=>Q(ke.key,Pe.target.value),step:ke.step,min:ke.min,max:ke.max,placeholder:Ae,className:Je}),ke.unit&&a.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:ke.unit})]})},ye=w.useMemo(()=>Le?.setting?Le.setting:{},[Le]);return a.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onDragOver:ke=>{ke.preventDefault(),P(!0)},onDragLeave:()=>P(!1),onDrop:D,children:[me&&p&&a.jsx(Wae,{onClose:()=>De(!1),leftPreset:ye,rightPreset:y,leftLabel:r("profiles.presets.baseLabel",{name:v||p}),rightLabel:r("profiles.presets.currentLabel",{name:h||r("profiles.presets.newPreset")}),t:r}),a.jsx(_t,{className:"w-full max-w-6xl max-h-[90vh] flex flex-col overflow-y-auto",children:a.jsxs(Nt,{className:"p-0 flex flex-col h-full",children:[a.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{children:[a.jsx("h2",{className:"text-xl font-semibold text-white",children:r(o?"profiles.presets.editPreset":e?"profiles.presets.duplicatePreset":"profiles.presets.createNewPreset")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:r("profiles.presets.customizeSettings")})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[p&&a.jsxs("button",{onClick:()=>De(!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:r("profiles.presets.compareWithBase"),children:[a.jsx(Yg,{className:"w-4 h-4"}),r("profiles.presets.compare")]}),a.jsx("button",{onClick:t,className:"p-2 text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:a.jsx(Ft,{className:"w-5 h-5"})})]})]}),E&&a.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:a.jsxs("div",{className:"text-center",children:[a.jsx(ia,{className:"w-12 h-12 text-bambu-green mx-auto mb-2"}),a.jsx("p",{className:"text-bambu-green font-medium",children:r("profiles.presets.dropJsonToImport")})]})}),a.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary space-y-3",children:[a.jsxs("div",{className:"grid grid-cols-3 gap-4 max-[640px]:grid-cols-1",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:r("common.type")}),a.jsxs("select",{value:u,onChange:ke=>{d(ke.target.value),x("")},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:[a.jsx("option",{value:"filament",children:r("profiles.presets.types.filament")}),a.jsx("option",{value:"print",children:r("profiles.presets.types.process")}),a.jsx("option",{value:"printer",children:r("profiles.presets.types.printer")})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:r("profiles.presets.basePreset")}),a.jsxs("select",{value:p,onChange:ke=>x(ke.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:[a.jsx("option",{value:"",children:r("profiles.presets.selectBasePreset")}),W.map(ke=>a.jsx("option",{value:ke.setting_id,children:ke.name},ke.setting_id))]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:r("profiles.presets.presetName")}),a.jsx("input",{type:"text",value:h,onChange:ke=>f(ke.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:r("profiles.presets.myCustomPreset")})]})]}),v&&a.jsx("div",{className:"text-xs text-bambu-gray",children:a.jsxs("p",{className:"flex items-center gap-1",children:[a.jsx(Xr,{className:"w-3 h-3 text-bambu-green"}),r("profiles.presets.inheritsFrom")," ",a.jsx("span",{className:"text-white",children:v}),$&&a.jsx(at,{className:"w-3 h-3 animate-spin ml-1"})]})})]}),a.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary max-[640px]:flex-wrap max-[640px]:items-center",children:[a.jsxs("button",{onClick:()=>c("common"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${l==="common"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(HB,{className:"w-4 h-4"}),r("profiles.presets.tabs.common")]}),a.jsxs("button",{onClick:()=>c("fields"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${l==="fields"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(rC,{className:"w-4 h-4"}),r("profiles.presets.tabs.allFields")]}),a.jsxs("button",{onClick:()=>c("json"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${l==="json"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(uX,{className:"w-4 h-4"}),"JSON",M&&a.jsx(Bi,{className:"w-3 h-3 text-red-400"})]}),a.jsx("div",{className:"flex-1 max-[640px]:hidden"}),a.jsxs("button",{onClick:()=>{const ke={name:h,type:u,base_id:p,setting:y},Ue=new Blob([JSON.stringify(ke,null,2)],{type:"application/json"}),je=URL.createObjectURL(Ue),Ve=document.createElement("a");Ve.href=je,Ve.download=`${h||"preset"}.json`,document.body.appendChild(Ve),Ve.click(),document.body.removeChild(Ve),URL.revokeObjectURL(je),i(r("profiles.presets.toast.exported"))},className:"flex items-center gap-2 px-4 py-3 text-sm text-bambu-gray hover:text-white transition-colors",title:r("profiles.presets.exportToJson"),children:[a.jsx(di,{className:"w-4 h-4"}),r("common.download")]}),a.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:r("profiles.presets.importFromJson"),children:[a.jsx(ia,{className:"w-4 h-4"}),r("common.upload")]}),a.jsx("input",{id:"file-import",type:"file",accept:".json",className:"hidden",onChange:ke=>{const Ue=ke.target.files?.[0];if(Ue){const je=new FileReader;je.onload=Ve=>{try{const Ae=JSON.parse(Ve.target?.result),Je=Ae.setting||Ae;S(Pe=>({...Pe,...Je})),i(r("profiles.presets.fileImported"))}catch{i(r("profiles.presets.invalidJson"),"error")}},je.readAsText(Ue)}}})]}),a.jsxs("div",{className:"flex-1 p-4",children:[l==="common"&&a.jsxs("div",{className:"space-y-6",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-3",children:[a.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:[a.jsx(pb,{className:"w-4 h-4 text-amber-400"}),r("profiles.templates.title")]}),Object.keys(y).filter(ke=>ke!=="inherits").length>0&&a.jsxs("button",{onClick:()=>K(!I),className:"text-xs text-bambu-gray hover:text-white flex items-center gap-1 transition-colors",children:[a.jsx(bs,{className:"w-3 h-3"}),r("profiles.presets.saveAsTemplate")]})]}),I&&a.jsxs("div",{className:"mb-3 p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"grid grid-cols-2 gap-2 mb-2",children:[a.jsx("input",{type:"text",value:Y,onChange:ke=>le(ke.target.value),placeholder:r("profiles.templates.namePlaceholder"),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}),a.jsx("input",{type:"text",value:H,onChange:ke=>X(ke.target.value),placeholder:r("profiles.templates.descriptionPlaceholder"),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"})]}),a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{size:"sm",onClick:be,disabled:!Y.trim(),children:[a.jsx(bs,{className:"w-3 h-3"}),r("common.save")]}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>K(!1),children:r("common.cancel")})]}),a.jsxs("button",{onClick:()=>J(!te),className:`flex items-center gap-1.5 text-xs transition-colors ${te?"text-bambu-green":"text-bambu-gray hover:text-white"}`,children:[te?a.jsx(Kl,{className:"w-3.5 h-3.5"}):a.jsx(gv,{className:"w-3.5 h-3.5"}),r(te?"profiles.templates.shownInModals":"profiles.templates.hiddenInModals")]})]})]}),Se&&a.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:[a.jsx(Xr,{className:"w-4 h-4 text-bambu-green"}),a.jsxs("span",{className:"text-sm text-bambu-green",children:[r("profiles.presets.templateApplied")," ",a.jsx("span",{className:"font-medium",children:Se})]}),a.jsx("button",{onClick:()=>fe(null),className:"ml-auto text-bambu-green/70 hover:text-bambu-green",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),a.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[ee.map(ke=>a.jsxs("button",{onClick:()=>he(ke),className:"p-3 text-left bg-bambu-dark border border-bambu-dark-tertiary rounded-lg hover:border-bambu-gray-dark transition-colors",children:[a.jsx("p",{className:"text-sm font-medium text-white",children:ke.name}),a.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:ke.description})]},ke.id)),ee.length===0&&a.jsx("p",{className:"col-span-3 text-center text-bambu-gray text-sm py-4",children:r("profiles.presets.noTemplatesSelected")})]}),a.jsx("p",{className:"text-xs text-bambu-gray-dark mt-2 text-center",children:r("profiles.presets.manageTemplatesHint")})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:r("profiles.presets.commonSettings")}),a.jsx("div",{className:"grid grid-cols-2 gap-x-6 gap-y-3",children:ue.slice(0,10).map(ke=>a.jsxs("div",{className:"flex items-center justify-between gap-4 max-[640px]:flex-col max-[640px]:items-start",children:[a.jsx("label",{className:"text-sm text-bambu-gray flex-shrink-0",children:ke.label}),a.jsx("div",{className:"w-48 max-[640px]:w-full",children:we(ke)})]},ke.key))})]}),Object.keys(y).length>1&&a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:r("profiles.presets.currentOverrides")}),a.jsx("div",{className:"flex flex-wrap gap-2",children:Object.entries(y).filter(([ke])=>ke!=="inherits").map(([ke,Ue])=>a.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-green/10 text-bambu-green text-xs rounded",children:[ke,": ",String(Ue).slice(0,20),a.jsx("button",{onClick:()=>Q(ke,void 0),className:"hover:text-white",children:a.jsx(Ft,{className:"w-3 h-3"})})]},ke))})]})]}),l==="fields"&&a.jsxs("div",{className:"grid grid-cols-2 gap-6",style:{height:"400px"},children:[a.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[a.jsx("h3",{className:"text-sm font-medium text-white",children:r("profiles.presets.availableFields")}),a.jsx("span",{className:"text-xs text-bambu-gray",children:ie?r("profiles.templates.fieldsCount",{count:ue.length}):r("common.loading")})]}),a.jsxs("div",{className:"relative mb-3 flex-shrink-0",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",value:k,onChange:ke=>j(ke.target.value),placeholder:r("profiles.presets.searchFieldsPlaceholder"),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"})]}),a.jsxs("div",{className:"flex-1 overflow-y-auto space-y-1 pr-2 min-h-0",children:[Z.filter(ke=>!(ke.key in y)).map(ke=>{const Ue=He[ke.key],je=Ze(Ue);return a.jsxs("div",{onClick:()=>{S(Ve=>({...Ve,[ke.key]:je||""}))},className:"flex items-center justify-between gap-2 p-2 rounded-lg hover:bg-bambu-dark-tertiary transition-colors cursor-pointer group",children:[a.jsxs("div",{className:"min-w-0 flex-1",children:[a.jsx("p",{className:"text-sm text-white truncate",children:ke.label}),a.jsx("p",{className:"text-xs text-bambu-gray-dark truncate",children:ke.key})]}),a.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[je&&a.jsxs("span",{className:"text-xs text-bambu-gray bg-bambu-dark px-2 py-0.5 rounded max-w-32 truncate",title:je,children:[je.slice(0,20),je.length>20?"...":""]}),a.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:a.jsx(ar,{className:"w-4 h-4 text-bambu-gray group-hover:text-bambu-green transition-colors"})})]})]},ke.key)}),Z.filter(ke=>!(ke.key in y)).length===0&&a.jsx("p",{className:"text-center text-bambu-gray py-4 text-sm",children:r(k?"profiles.presets.noMatchingFields":"profiles.presets.allFieldsAdded")})]}),a.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:B?a.jsxs("div",{className:"flex gap-2",children:[a.jsx("input",{type:"text",value:U,onChange:ke=>L(ke.target.value),onKeyDown:ke=>ke.key==="Enter"&&oe(),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}),a.jsx(Me,{size:"sm",onClick:oe,disabled:!U.trim(),children:a.jsx(ar,{className:"w-4 h-4"})}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>{R(!1),L("")},children:a.jsx(Ft,{className:"w-4 h-4"})})]}):a.jsxs("button",{onClick:()=>R(!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:[a.jsx(ar,{className:"w-4 h-4"}),r("profiles.presets.addCustomField")]})})]}),a.jsxs("div",{className:"flex flex-col h-full overflow-hidden",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3 flex-shrink-0",children:[a.jsx("h3",{className:"text-sm font-medium text-white",children:r("profiles.presets.yourOverrides")}),a.jsx("span",{className:"text-xs text-bambu-gray",children:r("profiles.templates.fieldsCount",{count:Object.keys(y).filter(ke=>ke!=="inherits").length})})]}),a.jsxs("div",{className:"flex-1 overflow-y-auto space-y-2 pr-2 min-h-0",children:[Object.entries(y).filter(([ke])=>ke!=="inherits").map(([ke,Ue])=>{const je=ue.find(Ve=>Ve.key===ke);return a.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center justify-between mb-2",children:[a.jsxs("div",{children:[a.jsx("p",{className:"text-sm font-medium text-white",children:je?.label||ke}),a.jsx("p",{className:"text-xs text-bambu-gray-dark",children:ke})]}),a.jsx("button",{onClick:()=>Q(ke,void 0),className:"p-1 text-bambu-gray hover:text-red-400 transition-colors",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),je?we(je):a.jsx("input",{type:"text",value:String(Ue),onChange:Ve=>Q(ke,Ve.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"})]},ke)}),Object.keys(y).filter(ke=>ke!=="inherits").length===0&&a.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[a.jsx(HB,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),a.jsx("p",{className:"text-sm",children:r("profiles.presets.noOverridesYet")}),a.jsx("p",{className:"text-xs mt-1",children:r("profiles.presets.clickFieldsToAdd")})]})]}),Object.keys(y).filter(ke=>ke!=="inherits").length>0&&a.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary flex-shrink-0",children:a.jsxs("button",{onClick:()=>{K(!0),c("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:[a.jsx(bs,{className:"w-4 h-4"}),r("profiles.presets.saveAsTemplate")]})})]})]}),l==="json"&&a.jsxs("div",{className:"space-y-2",children:[M&&a.jsxs("div",{className:"flex items-center gap-2 text-red-400 text-sm",children:[a.jsx(Bi,{className:"w-4 h-4"}),M]}),a.jsx("textarea",{value:N,onChange:ke=>z(ke.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 ${M?"border-red-500 focus:border-red-500":"border-bambu-dark-tertiary focus:border-bambu-green"}`,spellCheck:!1}),a.jsx("p",{className:"text-xs text-bambu-gray",children:r("profiles.presets.jsonTip")})]})]}),a.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:t,className:"flex-1",children:r("common.cancel")}),a.jsxs(Me,{onClick:()=>ve.mutate(),disabled:ve.isPending||!h.trim()||!o&&!p||!!M,className:"flex-1",children:[ve.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):o?a.jsx(bs,{className:"w-4 h-4"}):a.jsx(ar,{className:"w-4 h-4"}),r(o?"common.save":e?"common.duplicate":"common.create")]})]})]})})]})}function EZe({settings:t,lastSyncTime:e,onRefresh:n,isRefreshing:r,printers:i,hasPermission:s,t:o}){const[l,c]=w.useState(""),[u,d]=w.useState("all"),[h,f]=w.useState("all"),[p,x]=w.useState("all"),[v,b]=w.useState("all"),[y,S]=w.useState("all"),[N,A]=w.useState("all"),[M,T]=w.useState(null),[k,j]=w.useState(!1),[E,P]=w.useState(!1),[U,L]=w.useState(null),[B,R]=w.useState(null),[V,q]=w.useState(null),[I,K]=w.useState(!1),[Y,le]=w.useState([null,null]),[H,X]=w.useState(!1),[te,J]=w.useState(null),Se=tn(),fe=w.useMemo(()=>[...t.filament.map(oe=>({...oe,type:"filament"})),...t.printer.map(oe=>({...oe,type:"printer"})),...t.process.map(oe=>({...oe,type:"process"}))].map(oe=>({...oe,meta:bI(oe.name)})),[t]),me=w.useMemo(()=>{const Z=new Set,oe=new Set,Q=new Set;return fe.forEach(he=>{he.meta.nozzle&&Z.add(he.meta.nozzle),he.meta.filamentType&&oe.add(he.meta.filamentType),he.meta.layerHeight&&Q.add(he.meta.layerHeight)}),{printers:i.map(he=>({id:he.id.toString(),name:he.name})),nozzles:Array.from(Z).sort((he,be)=>parseFloat(he)-parseFloat(be)),filaments:Array.from(oe).sort(),layerHeights:Array.from(Q).sort((he,be)=>parseFloat(he)-parseFloat(be))}},[fe,i]),De=w.useMemo(()=>p==="all"?null:i.find(oe=>oe.id.toString()===p)?.model||null,[p,i]),de=w.useMemo(()=>fe.filter(Z=>u==="all"||Z.type===u).filter(Z=>{if(h==="all")return!0;const oe=mv(Z.setting_id);return h==="custom"?oe:!oe}).filter(Z=>{if(p==="all"||!De)return!0;const oe=Z.meta.printer?.toLowerCase()||"",Q=De.toLowerCase();return oe.includes(Q)||Q.includes(oe)}).filter(Z=>v==="all"||Z.meta.nozzle===v).filter(Z=>y==="all"||Z.meta.filamentType===y).filter(Z=>N==="all"||Z.meta.layerHeight===N).filter(Z=>l===""||Z.name.toLowerCase().includes(l.toLowerCase())).sort((Z,oe)=>Z.name.localeCompare(oe.name)),[fe,u,h,p,De,v,y,N,l]),ce=Z=>{if(I){const oe=Y[0]?.setting_id===Z.setting_id,Q=Y[1]?.setting_id===Z.setting_id;if(oe)le([Y[1],null]);else if(Q)le([Y[0],null]);else if(!Y[0])le([Z,null]);else if(Y[1]){if(Y[0].type!==Z.type)return;le([Y[0],Z])}else{if(Y[0].type!==Z.type)return;le([Y[0],Z])}}else T(Z)},Ne=Z=>{if(Y[0]?.setting_id===Z.setting_id)return 0;if(Y[1]?.setting_id===Z.setting_id)return 1},ie=async Z=>{try{const oe=await se.getCloudSettingDetail(Z.setting_id),Q=Z.type==="process"?"print":Z.type;L({type:Q,name:Z.name,base_id:oe.base_id||"GFSA00",setting:oe.setting||{}}),T(null)}catch(oe){console.error("Failed to fetch preset details for duplication:",oe)}},Le=async Z=>{try{Se.removeQueries({queryKey:["cloudSettingDetail",Z.setting_id]});const oe=await se.getCloudSettingDetail(Z.setting_id),Q=Z.type==="process"?"print":Z.type;R({type:Q,name:Z.name,base_id:oe.base_id||"GFSA00",setting:oe.setting||{},setting_id:Z.setting_id}),T(null)}catch(oe){console.error("Failed to fetch preset details for editing:",oe)}},$=()=>{d("all"),f("all"),x("all"),b("all"),S("all"),A("all"),c("")},W=u!=="all"||h!=="all"||p!=="all"||v!=="all"||y!=="all"||N!=="all"||l!=="",ue=t.filament.length+t.printer.length+t.process.length;return a.jsxs(a.Fragment,{children:[a.jsxs("div",{className:"space-y-4 mb-6",children:[a.jsxs("div",{className:"flex flex-col sm:flex-row gap-3",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",value:l,onChange:Z=>c(Z.target.value),placeholder:o("profiles.cloudView.searchPlaceholder"),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"})]}),a.jsxs("div",{className:"flex gap-2 max-[550px]:flex-wrap max-[550px]:items-center",children:[a.jsxs(Me,{variant:I?"primary":"secondary",onClick:()=>{I?(K(!1),le([null,null])):K(!0)},children:[a.jsx(Yg,{className:"w-4 h-4"}),o(I?"common.cancel":"profiles.presets.compare")]}),a.jsxs(Me,{variant:"secondary",onClick:()=>P(!0),disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.cloudView.noTemplatesPermission"),children:[a.jsx(pb,{className:"w-4 h-4"}),o("profiles.cloudView.templates")]}),a.jsxs(Me,{variant:"secondary",onClick:n,disabled:r||!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.cloudView.noRefreshPermission"),children:[a.jsx(kr,{className:`w-4 h-4 ${r?"animate-spin":""}`}),o("profiles.cloudView.refresh")]}),a.jsxs(Me,{onClick:()=>j(!0),disabled:!s("cloud:auth"),title:s("cloud:auth")?void 0:o("profiles.cloudView.noCreatePermission"),children:[a.jsx(ar,{className:"w-4 h-4"}),o("profiles.cloudView.newPreset")]})]})]}),a.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[a.jsx(tC,{className:"w-4 h-4 text-bambu-gray"}),a.jsx(jg,{label:o("profiles.cloudView.filters.type"),value:u,options:[{value:"all",label:o("profiles.cloudView.filters.all"),count:ue},{value:"filament",label:o("profiles.cloudView.filters.filament"),count:t.filament.length},{value:"printer",label:o("profiles.cloudView.filters.printer"),count:t.printer.length},{value:"process",label:o("profiles.cloudView.filters.process"),count:t.process.length}],onChange:Z=>d(Z)}),a.jsx(jg,{label:o("profiles.cloudView.filters.owner"),value:h,options:[{value:"all",label:o("profiles.cloudView.filters.all")},{value:"custom",label:o("profiles.cloudView.filters.myPresets")},{value:"builtin",label:o("profiles.cloudView.filters.builtIn")}],onChange:Z=>f(Z)}),me.printers.length>0&&a.jsx(jg,{label:o("profiles.cloudView.filters.printer"),value:p,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.printers.map(Z=>({value:Z.id,label:Z.name}))],onChange:x}),me.nozzles.length>0&&a.jsx(jg,{label:o("profiles.cloudView.filters.nozzle"),value:v,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.nozzles.map(Z=>({value:Z,label:Z}))],onChange:b}),me.filaments.length>0&&(u==="all"||u==="filament")&&a.jsx(jg,{label:o("profiles.cloudView.filters.filament"),value:y,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.filaments.map(Z=>({value:Z,label:Z}))],onChange:S}),me.layerHeights.length>0&&(u==="all"||u==="process")&&a.jsx(jg,{label:o("profiles.cloudView.filters.layer"),value:N,options:[{value:"all",label:o("profiles.cloudView.filters.all")},...me.layerHeights.map(Z=>({value:Z,label:Z}))],onChange:A}),W&&a.jsx("button",{onClick:$,className:"px-3 py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:o("profiles.cloudView.clearFilters")})]})]}),I&&a.jsx("div",{className:"mb-4 p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex items-center gap-4",children:[a.jsx(Yg,{className:"w-5 h-5 text-blue-400"}),a.jsx("span",{className:"text-white font-medium",children:o("profiles.cloudView.compareMode")}),a.jsx("span",{className:"text-bambu-gray",children:Y[0]?o("profiles.cloudView.selectAnotherPreset",{type:Y[0].type}):o("profiles.cloudView.clickTwoPresets")})]}),a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${Y[0]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:Y[0]?Y[0].name:o("profiles.cloudView.selectFirst")}),a.jsx(d3,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:`px-2 py-1 text-sm rounded truncate max-w-[200px] ${Y[1]?"bg-blue-500/30 text-blue-700 dark:text-blue-300":"bg-bambu-dark text-bambu-gray"}`,children:Y[1]?Y[1].name:o("profiles.cloudView.selectSecond")})]}),Y[0]&&Y[1]&&a.jsxs(Me,{size:"sm",onClick:async()=>{try{const[Z,oe]=await Promise.all([se.getCloudSettingDetail(Y[0].setting_id),se.getCloudSettingDetail(Y[1].setting_id)]);J([Z.setting||{},oe.setting||{}]),X(!0)}catch{}},children:[a.jsx(Yg,{className:"w-4 h-4"}),o("profiles.cloudView.compareNow")]})]})]})}),a.jsxs("div",{className:"flex flex-wrap items-center gap-4 mb-4 text-sm text-bambu-gray",children:[e&&a.jsxs("div",{className:"flex items-center gap-1",children:[a.jsx(Qn,{className:"w-3 h-3"}),o("profiles.cloudView.lastSynced")," ",NZe(e.toISOString(),o)]}),a.jsx("span",{children:o("profiles.cloudView.showingCount",{showing:de.length,total:ue})}),a.jsxs("div",{className:"flex items-center gap-1",children:[a.jsx("span",{className:"w-1.5 h-1.5 rounded-full bg-bambu-green"}),a.jsxs("span",{children:["= ",o("profiles.presets.myPreset")]})]})]}),de.length===0?a.jsxs("div",{className:"text-center py-16",children:[a.jsx(rs,{className:"w-12 h-12 text-bambu-gray-dark mx-auto mb-4"}),a.jsx("p",{className:"text-bambu-gray",children:o("profiles.cloudView.noPresetsFound")}),W&&a.jsx("button",{onClick:$,className:"mt-2 text-sm text-bambu-green hover:text-bambu-green-light",children:o("profiles.cloudView.clearFilters")})]}):a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[a.jsx(Jk,{className:"w-4 h-4 text-amber-400"}),a.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:o("profiles.cloudView.columns.filament")}),a.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",de.filter(Z=>Z.type==="filament").length,")"]})]}),a.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[de.filter(Z=>Z.type==="filament").map(Z=>a.jsx($j,{setting:Z,onClick:()=>ce(Z),onDuplicate:()=>ie(Z),compareMode:I,isCompareSelected:Ne(Z)!==void 0,compareIndex:Ne(Z),compareDisabled:I&&!!Y[0]&&Y[0].type!==Z.type,t:o},Z.setting_id)),de.filter(Z=>Z.type==="filament").length===0&&a.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:o("profiles.cloudView.noFilamentPresets")})]})]}),a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[a.jsx(td,{className:"w-4 h-4 text-blue-400"}),a.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:o("profiles.cloudView.columns.process")}),a.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",de.filter(Z=>Z.type==="process").length,")"]})]}),a.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[de.filter(Z=>Z.type==="process").map(Z=>a.jsx($j,{setting:Z,onClick:()=>ce(Z),onDuplicate:()=>ie(Z),compareMode:I,isCompareSelected:Ne(Z)!==void 0,compareIndex:Ne(Z),compareDisabled:I&&!!Y[0]&&Y[0].type!==Z.type,t:o},Z.setting_id)),de.filter(Z=>Z.type==="process").length===0&&a.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:o("profiles.cloudView.noProcessPresets")})]})]}),a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-2 mb-3 px-1",children:[a.jsx(Cr,{className:"w-4 h-4 text-purple-400"}),a.jsx("h3",{className:"text-sm font-medium text-bambu-gray",children:o("profiles.cloudView.columns.printer")}),a.jsxs("span",{className:"text-xs text-bambu-gray-dark",children:["(",de.filter(Z=>Z.type==="printer").length,")"]})]}),a.jsxs("div",{className:"space-y-1 max-h-[calc(100vh-320px)] overflow-y-auto pr-1",children:[de.filter(Z=>Z.type==="printer").map(Z=>a.jsx($j,{setting:Z,onClick:()=>ce(Z),onDuplicate:()=>ie(Z),compareMode:I,isCompareSelected:Ne(Z)!==void 0,compareIndex:Ne(Z),compareDisabled:I&&!!Y[0]&&Y[0].type!==Z.type,t:o},Z.setting_id)),de.filter(Z=>Z.type==="printer").length===0&&a.jsx("p",{className:"text-xs text-bambu-gray-dark px-3 py-2",children:o("profiles.cloudView.noPrinterPresets")})]})]})]}),M&&a.jsx(MZe,{setting:M,onClose:()=>T(null),onDeleted:()=>T(null),onDuplicate:()=>ie(M),onEdit:()=>Le(M),hasPermission:s,t:o}),(k||U||B||V)&&a.jsx(TZe,{onClose:()=>{j(!1),L(null),R(null),q(null)},initialData:B||U||(V?{type:V.type,name:"",base_id:"",setting:V.setting}:void 0),allPresets:t,t:o}),E&&a.jsx(AZe,{onClose:()=>P(!1),onApply:Z=>{q({type:Z.type,setting:Z.settings}),P(!1)},t:o}),H&&te&&Y[0]&&Y[1]&&a.jsx(Wae,{onClose:()=>{X(!1),J(null)},leftPreset:te[0],rightPreset:te[1],leftLabel:Y[0].name,rightLabel:Y[1].name,t:o})]})}function PZe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r}=Ir(),[i,s]=w.useState("cloud"),[o,l]=w.useState(),{data:c,isLoading:u}=tt({queryKey:["cloudStatus"],queryFn:se.getCloudStatus}),{data:d=[]}=tt({queryKey:["printers"],queryFn:se.getPrinters}),{data:h,isLoading:f,refetch:p,dataUpdatedAt:x}=tt({queryKey:["cloudSettings"],queryFn:()=>se.getCloudSettings(),enabled:!!c?.is_authenticated,retry:!1,staleTime:1e3*60*5});w.useEffect(()=>{x&&l(new Date(x))},[x]);const v=lt({mutationFn:se.cloudLogout,onSuccess:()=>{e.invalidateQueries({queryKey:["cloudStatus"]}),e.removeQueries({queryKey:["cloudSettings"]}),n(t("profiles.toast.loggedOut"))}}),b=()=>{e.invalidateQueries({queryKey:["cloudStatus"]})};return u?a.jsx("div",{className:"p-4 md:p-8 flex items-center justify-center min-h-[400px]",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):a.jsxs("div",{className:"p-6 lg:p-8",children:[a.jsxs("div",{className:"mb-6",children:[a.jsx("h1",{className:"text-2xl font-bold text-white",children:t("profiles.title")}),a.jsx("p",{className:"text-bambu-gray",children:t("profiles.subtitle")})]}),a.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary mb-6",children:[a.jsxs("button",{onClick:()=>s("cloud"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${i==="cloud"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(Gb,{className:"w-4 h-4"}),t("profiles.tabs.cloud")]}),a.jsxs("button",{onClick:()=>s("local"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${i==="local"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(qb,{className:"w-4 h-4"}),t("profiles.tabs.local")]}),a.jsxs("button",{onClick:()=>s("kprofiles"),className:`flex items-center gap-2 px-4 py-3 text-sm font-medium transition-colors border-b-2 -mb-px ${i==="kprofiles"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[a.jsx(nC,{className:"w-4 h-4"}),t("profiles.tabs.kprofiles")]})]}),i==="cloud"&&a.jsxs(a.Fragment,{children:[c?.is_authenticated&&a.jsxs("div",{className:"flex items-center justify-between p-3 mb-6 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green animate-pulse"}),a.jsxs("span",{className:"text-sm text-bambu-gray",children:[t("profiles.connectedAs")," ",a.jsx("span",{className:"text-white",children:c.email})]})]}),a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>v.mutate(),disabled:v.isPending||!r("cloud:auth"),title:r("cloud:auth")?void 0:t("profiles.noLogoutPermission"),children:[a.jsx(b3,{className:"w-4 h-4"}),t("profiles.logout")]})]}),c?.is_authenticated?f?a.jsx("div",{className:"flex items-center justify-center py-16",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):h?a.jsx(EZe,{settings:h,lastSyncTime:o,onRefresh:()=>p(),isRefreshing:f,printers:d,hasPermission:r,t}):a.jsxs("div",{className:"text-center py-16",children:[a.jsx("p",{className:"text-bambu-gray mb-4",children:t("profiles.failedToLoad")}),a.jsx(Me,{onClick:()=>p(),children:t("profiles.retry")})]}):a.jsx(kZe,{onSuccess:b,t})]}),i==="local"&&a.jsx(SZe,{}),i==="kprofiles"&&a.jsx(yZe,{}),a.jsx(CZe,{})]})}const Ky={Droplet:Jk,Flame:Xhe,Ruler:Mfe,Sparkles:pb,Square:gl,Cable:Rde,Wrench:Rf,Calendar:ii,Timer:Ou,Cog:xhe,Fan:dX,Zap:Gh,Wind:wX,Thermometer:cC,Layers:rs,Box:Qi,Target:yX,RefreshCw:kr,Settings:lC,Filter:tC,CircleDot:the};function x0(t){return t&&Ky[t]||Rf}function Dg(t,e,n){if(e==="days"){if(t<1)return n?n("common.today"):"Today";if(t===1)return n?n("maintenance.day"):"1 day";if(t<7){const i=Math.round(t);return n?n("maintenance.days",{count:i}):`${i} days`}if(t<180){const i=Math.round(t/7);return i===1?n?n("maintenance.week"):"1 week":n?n("maintenance.weeks",{count:i}):`${i} weeks`}const r=Math.round(t/30);return r===1?n?n("maintenance.month"):"1 month":n?n("maintenance.months",{count:r}):`${r} months`}else{if(t<1)return`${Math.round(t*60)}m`;if(t<24)return`${t<10?t.toFixed(1):Math.round(t)}h`;const r=t/24;if(r<7)return`${r<2?r.toFixed(1):Math.round(r)}d`;const i=r/7;return i<12?`${i<2?i.toFixed(1):Math.round(i)}w`:`${Math.round(i/4)}mo`}}function Vj(t,e,n){return e==="days"?t===1?n?n("maintenance.day"):"1 day":t===7?n?n("maintenance.week"):"1 week":t===14?n?n("maintenance.weeks",{count:2}):"2 weeks":t===30?n?n("maintenance.month"):"1 month":t===60?n?n("maintenance.months",{count:2}):"2 months":t===90?n?n("maintenance.months",{count:3}):"3 months":t===180?n?n("maintenance.months",{count:6}):"6 months":t===365?n?n("maintenance.year"):"1 year":n?n("maintenance.days",{count:t}):`${t} days`:`${t}h`}function jZe(t,e){const n=(e||"").toUpperCase().replace(/[- ]/g,""),r=n.includes("X1"),i=n.includes("P1"),s=n.includes("A1MINI"),o=n.includes("A1")&&!s,l=n.includes("H2D"),c=n.includes("H2C"),u=n.includes("H2S"),d=l||c||u,h=n.includes("P2S");switch(t){case"Lubricate Carbon Rods":return r?"https://wiki.bambulab.com/en/x1/maintenance/basic-maintenance":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":h?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":null;case"Lubricate Linear Rails":return s?"https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis":o?"https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis":d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;case"Clean Nozzle/Hotend":return r||i?"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog":s||o?"https://wiki.bambulab.com/en/a1-mini/troubleshooting/nozzle-clog":d?"https://wiki.bambulab.com/en/h2/maintenance/nozzl-cold-pull-maintenance-and-cleaning":h?"https://wiki.bambulab.com/en/p2s/maintenance/cold-pull-maintenance-hotend":"https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog";case"Check Belt Tension":return r?"https://wiki.bambulab.com/en/x1/maintenance/belt-tension":i?"https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance":s?"https://wiki.bambulab.com/en/a1-mini/maintenance/belt_tension":o?"https://wiki.bambulab.com/en/a1/maintenance/belt_tension":l?"https://wiki.bambulab.com/en/h2/maintenance/belt-tension":c?"https://wiki.bambulab.com/en/h2c/maintenance/belt-tension":u?"https://wiki.bambulab.com/en/h2s/maintenance/belt-tension":h?"https://wiki.bambulab.com/en/p2s/maintenance/belt-tension":"https://wiki.bambulab.com/en/x1/maintenance/belt-tension";case"Clean Carbon Rods":return r||i||h?"https://wiki.bambulab.com/en/general/carbon-rods-clearance":null;case"Clean Linear Rails":return s?"https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis":o?"https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis":d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;case"Clean Build Plate":return"https://wiki.bambulab.com/en/filament-acc/acc/pei-plate-clean-guide";case"Check PTFE Tube":return r||i?"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube":s||o?"https://wiki.bambulab.com/en/a1-mini/maintenance/ptfe-tube":l?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":u?"https://wiki.bambulab.com/en/h2s/maintenance/replace-ptfe-tube-on-h2s-printer":c?"https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer":"https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube";case"Replace HEPA Filter":case"HEPA Filter":case"Replace Carbon Filter":case"Carbon Filter":return d?"https://wiki.bambulab.com/en/h2/maintenance/replace-smoke-purifier-air-filte":"https://wiki.bambulab.com/en/x1/maintenance/replace-carbon-filter";case"Lubricate Left Nozzle Rail":case"Left Nozzle Rail":return d?"https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication":null;default:return null}}function DZe({item:t,onPerform:e,onToggle:n,hasPermission:r,t:i}){const s=x0(t.maintenance_type_icon),o=t.interval_type||"hours",c=(()=>{if(o==="days"){const p=t.days_since_maintenance??0;return Math.max(0,Math.min(100,p/t.interval_hours*100))}return Math.max(0,Math.min(100,(t.interval_hours-t.hours_until_due)/t.interval_hours*100))})(),u=()=>t.enabled?t.is_due?"text-red-400":t.is_warning?"text-amber-400":"text-bambu-green":"text-bambu-gray",d=()=>t.enabled?t.is_due?"bg-red-500":t.is_warning?"bg-amber-500":"bg-bambu-green":"bg-bambu-gray/30",h=()=>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",f=()=>{if(!t.enabled)return i("common.disabled");if(o==="days"){const p=t.days_until_due??0;return t.is_due?i("maintenance.overdueBy",{duration:Dg(Math.abs(p),"days",i)}):t.is_warning?i("maintenance.dueIn",{duration:Dg(p,"days",i)}):i("maintenance.timeLeft",{duration:Dg(p,"days",i)})}else return t.is_due?i("maintenance.overdueBy",{duration:Dg(Math.abs(t.hours_until_due),"hours",i)}):t.is_warning?i("maintenance.dueIn",{duration:Dg(t.hours_until_due,"hours",i)}):i("maintenance.timeLeft",{duration:Dg(t.hours_until_due,"hours",i)})};return a.jsx("div",{className:`rounded-xl border p-4 transition-all ${h()}`,children:a.jsxs("div",{className:"flex items-start gap-3 max-[550px]:flex-wrap",children:[a.jsxs("div",{className:`relative p-2.5 rounded-lg shrink-0 ${t.is_due?"bg-red-500/20":t.is_warning?"bg-amber-500/20":t.enabled?"bg-bambu-dark":"bg-bambu-dark/50"}`,children:[a.jsx(s,{className:`w-5 h-5 ${u()}`}),t.enabled&&(t.is_due||t.is_warning)&&a.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`})]}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("h3",{className:`font-medium truncate ${t.enabled?"text-white":"text-bambu-gray"}`,children:t.maintenance_type_name}),o==="days"&&a.jsx("span",{title:i("maintenance.timeBasedInterval"),children:a.jsx(ii,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"})}),(()=>{const p=t.maintenance_type_wiki_url||jZe(t.maintenance_type_name,t.printer_model);return p?a.jsx("a",{href:p,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-bambu-green transition-colors shrink-0",title:i("maintenance.viewDocumentation"),onClick:x=>x.stopPropagation(),children:a.jsx(Kr,{className:"w-3.5 h-3.5"})}):null})()]}),a.jsx("div",{className:"mt-2 mb-1.5",children:a.jsx("div",{className:"w-full h-1.5 bg-bambu-dark rounded-full overflow-hidden",children:a.jsx("div",{className:`h-full rounded-full transition-all duration-500 ${d()}`,style:{width:`${c}%`}})})}),a.jsxs("div",{className:`text-xs flex items-center gap-1 ${u()}`,children:[t.is_due&&a.jsx(Cn,{className:"w-3 h-3"}),t.is_warning&&!t.is_due&&a.jsx(Qn,{className:"w-3 h-3"}),!t.is_due&&!t.is_warning&&t.enabled&&a.jsx(Xr,{className:"w-3 h-3"}),f()]})]}),a.jsxs("div",{className:"flex items-center gap-2 shrink-0 max-[550px]:w-full max-[550px]:justify-end max-[550px]:mt-1",children:[a.jsx("span",{title:r("maintenance:update")?void 0:i("maintenance.noPermissionUpdate"),children:a.jsx(zn,{checked:t.enabled,onChange:p=>n(t.id,p),disabled:!r("maintenance:update")})}),a.jsxs(Me,{size:"sm",variant:t.is_due?"primary":"secondary",onClick:()=>e(t.id),disabled:!t.enabled||!r("maintenance:update"),title:r("maintenance:update")?void 0:i("maintenance.noPermissionPerform"),className:"!px-3",children:[a.jsx(Do,{className:"w-3.5 h-3.5"}),i("common.reset")]})]})]})})}function RZe({overview:t,onPerform:e,onToggle:n,onSetHours:r,hasPermission:i,t:s}){const[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,h]=w.useState(t.total_print_hours.toFixed(1)),f=[...t.maintenance_items].sort((v,b)=>v.is_due&&!b.is_due?-1:!v.is_due&&b.is_due?1:v.is_warning&&!b.is_warning?-1:!v.is_warning&&b.is_warning?1:v.maintenance_type_id-b.maintenance_type_id),p=f.find(v=>v.enabled&&(v.is_due||v.is_warning)),x=()=>{const v=parseFloat(d);!isNaN(v)&&v>=0&&(r(t.printer_id,v),u(!1))};return a.jsxs(_t,{className:"overflow-hidden",children:[a.jsxs("div",{className:"p-5",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex items-center gap-4",children:[a.jsx("h2",{className:"text-xl font-semibold text-white",children:t.printer_name}),a.jsxs("div",{className:"flex items-center gap-2",children:[t.due_count>0&&a.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:[a.jsx(Cn,{className:"w-3 h-3"}),s("maintenance.overdueCount",{count:t.due_count})]}),t.warning_count>0&&a.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:[a.jsx(Qn,{className:"w-3 h-3"}),s("maintenance.dueSoonCount",{count:t.warning_count})]}),t.due_count===0&&t.warning_count===0&&a.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:[a.jsx(Xr,{className:"w-3 h-3"}),s("maintenance.allGood")]})]})]}),a.jsxs("button",{onClick:()=>l(!o),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:[o?a.jsx(ql,{className:"w-4 h-4"}):a.jsx(nr,{className:"w-4 h-4"}),s(o?"common.collapse":"common.expand")]})]}),a.jsxs("div",{className:"flex items-center gap-6 mt-4",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2 bg-bambu-dark/50 rounded-lg",children:a.jsx(Ou,{className:"w-4 h-4 text-bambu-gray"})}),c?a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("input",{type:"number",value:d,onChange:v=>h(v.target.value),onKeyDown:v=>{v.key==="Enter"&&x(),v.key==="Escape"&&u(!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}),a.jsx("span",{className:"text-xs text-bambu-gray",children:s("common.hours")}),a.jsx(Me,{size:"sm",onClick:x,children:s("common.save")}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>u(!1),children:s("common.cancel")})]}):a.jsxs("button",{onClick:()=>{i("maintenance:update")&&(h(Math.round(t.total_print_hours).toString()),u(!0))},className:`group ${i("maintenance:update")?"":"cursor-not-allowed opacity-60"}`,title:i("maintenance:update")?void 0:s("maintenance.noPermissionEditHours"),children:[a.jsxs("div",{className:`text-sm font-medium text-white ${i("maintenance:update")?"group-hover:text-bambu-green":""} transition-colors flex items-center gap-1`,children:[Math.round(t.total_print_hours)," ",s("common.hours"),a.jsx(Pf,{className:`w-3 h-3 text-bambu-gray ${i("maintenance:update")?"group-hover:text-bambu-green":""}`})]}),a.jsx("div",{className:"text-xs text-bambu-gray",children:s("maintenance.totalPrintTime")})]})]}),a.jsx("div",{className:"w-px h-10 bg-bambu-dark-tertiary"}),p&&a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:`p-2 rounded-lg ${p.is_due?"bg-red-500/20":"bg-amber-500/20"}`,children:(()=>{const v=x0(p.maintenance_type_icon);return a.jsx(v,{className:`w-4 h-4 ${p.is_due?"text-red-400":"text-amber-400"}`})})()}),a.jsxs("div",{children:[a.jsx("div",{className:`text-sm font-medium ${p.is_due?"text-red-400":"text-amber-400"}`,children:p.maintenance_type_name}),a.jsx("div",{className:`text-xs ${p.is_due?"text-red-400/70":"text-amber-400/70"}`,children:p.is_due?s("common.overdue"):s("maintenance.dueSoon")})]})]})]})]}),o&&a.jsx(Nt,{className:"pt-0 border-t border-bambu-dark-tertiary",children:a.jsx("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-3 pt-4",children:f.map(v=>a.jsx(DZe,{item:v,onPerform:e,onToggle:n,hasPermission:i,t:s},v.id))})})]})}function FZe({overview:t,types:e,onUpdateInterval:n,onAddType:r,onUpdateType:i,onDeleteType:s,onRestoreDefaults:o,isRestoringDefaults:l,onAssignType:c,onRemoveItem:u,hasPermission:d,t:h}){const[f,p]=w.useState(null),[x,v]=w.useState(""),[b,y]=w.useState("hours"),[S,N]=w.useState(!1),[A,M]=w.useState(""),[T,k]=w.useState("100"),[j,E]=w.useState("hours"),[P,U]=w.useState("Wrench"),[L,B]=w.useState(""),[R,V]=w.useState(new Set),[q,I]=w.useState(null),[K,Y]=w.useState(null),le=w.useMemo(()=>t?t.map(z=>({id:z.printer_id,name:z.printer_name})):[],[t]),H=z=>t?t.filter(D=>D.maintenance_items.some(G=>G.maintenance_type_id===z)).map(D=>({printerId:D.printer_id,printerName:D.printer_name,itemId:D.maintenance_items.find(G=>G.maintenance_type_id===z)?.id})):[],X=z=>{if(!t)return[];const D=new Set(H(z).map(G=>G.printerId));return le.filter(G=>!D.has(G.id))},[te,J]=w.useState(null),[Se,fe]=w.useState(""),[me,De]=w.useState(""),[de,ce]=w.useState("hours"),[Ne,ie]=w.useState("Wrench"),[Le,$]=w.useState(""),W=z=>{J(z),fe(z.name),De(z.default_interval_hours.toString()),ce(z.interval_type||"hours"),ie(z.icon||"Wrench"),$(z.wiki_url||"")},ue=()=>{te&&Se.trim()&&parseFloat(me)>0&&(i(te.id,{name:Se.trim(),default_interval_hours:parseFloat(me),interval_type:de,icon:Ne,wiki_url:Le.trim()||null}),J(null))},Z=(z,D,G)=>{const pe=parseFloat(x);if(!isNaN(pe)&&pe>0){const ve=Math.abs(pe-D)<.01?null:pe;n(z,{custom_interval_hours:ve,custom_interval_type:b!==G?b:null})}p(null)},oe=z=>{z.preventDefault(),A.trim()&&parseFloat(T)>0&&R.size>0&&(r({name:A.trim(),default_interval_hours:parseFloat(T),interval_type:j,icon:P,wiki_url:L.trim()||null},Array.from(R)),M(""),k("100"),E("hours"),B(""),V(new Set),N(!1))},Q=z=>{V(D=>{const G=new Set(D);return G.has(z)?G.delete(z):G.add(z),G})},he=t?.map(z=>({printerId:z.printer_id,printerName:z.printer_name,items:z.maintenance_items.sort((D,G)=>D.maintenance_type_id-G.maintenance_type_id)})).sort((z,D)=>z.printerName.localeCompare(D.printerName))||[],be=e.filter(z=>z.is_system),ee=e.filter(z=>!z.is_system);return a.jsxs("div",{className:"space-y-8",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-4",children:[a.jsxs("div",{children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:h("maintenance.maintenanceTypes")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:h("maintenance.maintenanceTypesDescription")})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:o,disabled:!d("maintenance:delete")||l,title:d("maintenance:delete")?void 0:h("maintenance.noPermissionDeleteTypes"),children:h("maintenance.restoreDefaults")}),a.jsxs(Me,{onClick:()=>N(!S),disabled:!d("maintenance:create"),title:d("maintenance:create")?void 0:h("maintenance.noPermissionEditTypes"),children:[a.jsx(ar,{className:"w-4 h-4"}),h("maintenance.addCustomType")]})]})]}),S&&a.jsx(_t,{className:"mb-6",children:a.jsx(Nt,{className:"py-4",children:a.jsxs("form",{onSubmit:oe,children:[a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[a.jsxs("div",{className:"lg:col-span-2",children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("common.name")}),a.jsx("input",{type:"text",value:A,onChange:z=>M(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:h("maintenance.exampleName"),autoFocus:!0})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.intervalType")}),a.jsxs("select",{value:j,onChange:z=>{E(z.target.value),z.target.value==="days"?k("30"):k("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:[a.jsx("option",{value:"hours",children:h("maintenance.printHours")}),a.jsx("option",{value:"days",children:h("maintenance.calendarDays")})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.intervalValue",{type:j==="days"?h("maintenance.calendarDays").toLowerCase():h("common.hours")})}),a.jsx("input",{type:"number",value:T,onChange:z=>k(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",min:"1"})]})]}),a.jsx("div",{className:"mt-4 flex items-end justify-between",children:a.jsxs("div",{children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.icon")}),a.jsx("div",{className:"flex gap-1",children:Object.keys(Ky).map(z=>{const D=Ky[z];return a.jsx("button",{type:"button",onClick:()=>U(z),className:`p-2 rounded-lg transition-colors ${P===z?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:a.jsx(D,{className:"w-4 h-4"})},z)})})]})}),a.jsxs("div",{className:"mt-4",children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.documentationLink")}),a.jsx("input",{type:"url",value:L,onChange:z=>B(z.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none",placeholder:"https://wiki.bambulab.com/..."})]}),a.jsxs("div",{className:"mt-4",children:[a.jsx("label",{className:"block text-xs text-bambu-gray mb-1.5",children:h("maintenance.assignToPrinters")}),a.jsx("div",{className:"flex flex-wrap gap-2",children:le.map(z=>a.jsx("button",{type:"button",onClick:()=>Q(z.id),className:`px-3 py-1.5 rounded-lg text-sm transition-colors ${R.has(z.id)?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary"}`,children:z.name},z.id))}),R.size===0&&a.jsx("p",{className:"text-xs text-orange-400 mt-1",children:h("maintenance.selectAtLeastOnePrinter")})]}),a.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:()=>{N(!1),V(new Set)},children:h("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!A.trim()||R.size===0,children:h("maintenance.addType")})]})]})})}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3",children:[be.map(z=>{const D=x0(z.icon),G=z.interval_type||"hours";return a.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-dark-tertiary",children:a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-dark rounded-lg",children:a.jsx(D,{className:"w-5 h-5 text-bambu-gray"})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("div",{className:"text-sm font-medium text-white truncate",children:z.name}),a.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[G==="days"?a.jsx(ii,{className:"w-3 h-3"}):a.jsx(Ou,{className:"w-3 h-3"}),Vj(z.default_interval_hours,G,h)]})]}),a.jsx("button",{onClick:()=>{d("maintenance:delete")&&Y(z)},disabled:!d("maintenance:delete"),title:d("maintenance:delete")?void 0:h("maintenance.noPermissionDeleteTypes"),className:`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors ${d("maintenance:delete")?"":"opacity-50 cursor-not-allowed"}`,children:a.jsx(en,{className:"w-4 h-4"})})]})},z.id)}),ee.map(z=>{const D=x0(z.icon),G=z.interval_type||"hours";if(te?.id===z.id)return a.jsx("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green",children:a.jsxs("div",{className:"space-y-3",children:[a.jsx("input",{type:"text",value:Se,onChange:_e=>fe(_e.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:h("common.name"),autoFocus:!0}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs("select",{value:de,onChange:_e=>ce(_e.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:[a.jsx("option",{value:"hours",children:h("maintenance.printHours")}),a.jsx("option",{value:"days",children:h("maintenance.calendarDays")})]}),a.jsx("input",{type:"number",value:me,onChange:_e=>De(_e.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"})]}),a.jsx("div",{className:"flex flex-wrap gap-1",children:Object.keys(Ky).map(_e=>{const He=Ky[_e];return a.jsx("button",{type:"button",onClick:()=>ie(_e),className:`p-1.5 rounded transition-colors ${Ne===_e?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:a.jsx(He,{className:"w-3.5 h-3.5"})},_e)})}),a.jsx("input",{type:"url",value:Le,onChange:_e=>$(_e.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:h("maintenance.documentationLink")}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{size:"sm",onClick:ue,disabled:!Se.trim(),children:h("common.save")}),a.jsx(Me,{size:"sm",variant:"secondary",onClick:()=>J(null),children:h("common.cancel")})]})]})},z.id);const ve=H(z.id),xe=X(z.id),ze=q===z.id;return a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green/30",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-green/20 rounded-lg",children:a.jsx(D,{className:"w-5 h-5 text-bambu-green"})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-sm font-medium text-white truncate",children:z.name}),a.jsx("span",{className:"px-1.5 py-0.5 bg-bambu-green/20 text-bambu-green text-[10px] font-medium rounded",children:h("maintenance.custom")})]}),a.jsxs("div",{className:"text-xs text-bambu-gray mt-0.5 flex items-center gap-1",children:[G==="days"?a.jsx(ii,{className:"w-3 h-3"}):a.jsx(Ou,{className:"w-3 h-3"}),Vj(z.default_interval_hours,G,h)]})]}),a.jsxs("button",{onClick:()=>I(ze?null:z.id),className:`px-2 py-1 rounded-lg border transition-colors flex items-center gap-1 ${ve.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:h("maintenance.printersAssignedClick",{count:ve.length}),children:[a.jsx(Cr,{className:"w-3 h-3"}),a.jsx("span",{className:"text-xs font-medium",children:ve.length}),a.jsx(nr,{className:`w-3 h-3 transition-transform ${ze?"rotate-180":""}`})]}),a.jsx("button",{onClick:()=>W(z),disabled:!d("maintenance:update"),title:d("maintenance:update")?void 0:h("maintenance.noPermissionEditTypes"),className:`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors ${d("maintenance:update")?"":"opacity-50 cursor-not-allowed"}`,children:a.jsx(Pf,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>{confirm(h("maintenance.deleteTypeConfirm",{name:z.name}))&&s(z.id)},disabled:!d("maintenance:delete"),title:d("maintenance:delete")?void 0:h("maintenance.noPermissionDeleteTypes"),className:`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors ${d("maintenance:delete")?"":"opacity-50 cursor-not-allowed"}`,children:a.jsx(en,{className:"w-4 h-4"})})]}),ze&&a.jsxs("div",{className:"mt-3 pt-3 border-t border-bambu-dark-tertiary",children:[a.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:h("maintenance.assignedToPrinters")}),ve.length===0?a.jsx("p",{className:"text-xs text-orange-400",children:h("maintenance.noPrintersAssigned")}):a.jsx("div",{className:"flex flex-wrap gap-1 mb-2",children:ve.map(_e=>a.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark rounded text-xs text-white",children:[_e.printerName,a.jsx("button",{onClick:()=>_e.itemId&&u(_e.itemId),disabled:!d("maintenance:delete"),title:d("maintenance:delete")?h("maintenance.removeFromPrinter"):h("maintenance.noPermissionRemovePrinter"),className:`ml-1 ${d("maintenance:delete")?"hover:text-red-400":"opacity-50 cursor-not-allowed"}`,children:"×"})]},_e.printerId))}),xe.length>0&&a.jsxs("div",{className:"flex flex-wrap gap-1",children:[a.jsx("span",{className:"text-xs text-bambu-gray mr-1",children:h("maintenance.addPrinterShort")}),xe.map(_e=>a.jsxs("button",{onClick:()=>c(_e.id,z.id),disabled:!d("maintenance:create"),title:d("maintenance:create")?void 0:h("maintenance.noPermissionAssignPrinter"),className:`px-2 py-1 bg-bambu-dark rounded text-xs transition-colors ${d("maintenance:create")?"hover:bg-bambu-green/20 text-bambu-gray hover:text-bambu-green":"opacity-50 cursor-not-allowed text-bambu-gray"}`,children:["+ ",_e.name]},_e.id))]})]})]},z.id)})]})]}),he.length>0&&a.jsxs("div",{children:[a.jsxs("div",{className:"mb-4",children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:h("maintenance.intervalOverrides")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:h("maintenance.intervalOverridesDescription")})]}),a.jsx("div",{className:"space-y-4",children:he.map(z=>a.jsx(_t,{children:a.jsxs(Nt,{className:"py-4",children:[a.jsx("h3",{className:"text-sm font-medium text-white mb-3",children:z.printerName}),a.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2",children:z.items.map(D=>{const G=x0(D.maintenance_type_icon),pe=e.find(He=>He.id===D.maintenance_type_id),ve=pe?.default_interval_hours||D.interval_hours,xe=pe?.interval_type||"hours",ze=D.interval_type||"hours",_e=f===D.id;return a.jsxs("div",{className:"flex items-center gap-2 p-2.5 bg-bambu-dark rounded-lg",children:[a.jsx(G,{className:"w-4 h-4 text-bambu-gray shrink-0"}),a.jsx("span",{className:"text-xs text-bambu-gray flex-1 truncate",children:D.maintenance_type_name}),_e?a.jsxs("div",{className:"flex items-center gap-1",children:[b==="days"?a.jsx(ii,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}):a.jsx(Ou,{className:"w-3.5 h-3.5 text-bambu-gray shrink-0"}),a.jsxs("select",{value:b,onChange:He=>y(He.target.value),className:"px-1.5 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",children:[a.jsx("option",{value:"hours",children:h("maintenance.printHours")}),a.jsx("option",{value:"days",children:h("maintenance.calendarDays")})]}),a.jsx("input",{type:"number",value:x,onChange:He=>v(He.target.value),onKeyDown:He=>{He.key==="Enter"&&Z(D.id,ve,xe),He.key==="Escape"&&p(null)},className:"w-16 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs",min:"1"}),a.jsx(Me,{size:"sm",onClick:()=>Z(D.id,ve,xe),children:"OK"})]}):a.jsxs("button",{onClick:()=>{d("maintenance:update")&&(p(D.id),v(D.interval_hours.toString()),y(ze))},disabled:!d("maintenance:update"),title:d("maintenance:update")?void 0:h("maintenance.noPermissionEditIntervals"),className:`px-2 py-1 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-xs font-medium text-white transition-colors flex items-center gap-1 ${d("maintenance:update")?"hover:bg-bambu-dark-secondary hover:border-bambu-green":"opacity-50 cursor-not-allowed"}`,children:[ze==="days"?a.jsx(ii,{className:"w-3 h-3"}):a.jsx(Ou,{className:"w-3 h-3"}),Vj(D.interval_hours,ze,h),a.jsx(Pf,{className:"w-3 h-3 text-bambu-gray"})]})]},D.id)})})]})},z.printerId))})]}),he.length===0&&a.jsx(_t,{children:a.jsxs(Nt,{className:"text-center py-12",children:[a.jsx(Qn,{className:"w-12 h-12 mx-auto mb-4 text-bambu-gray/30"}),a.jsx("p",{className:"text-bambu-gray",children:h("common.noPrinters")}),a.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1",children:h("maintenance.intervalOverridesDescription")})]})}),K&&a.jsx(kn,{title:h("maintenance.deleteSystemTypeTitle"),message:h("maintenance.deleteSystemTypeMessage",{name:K.name}),confirmText:h("common.delete"),cancelText:h("common.cancel"),variant:"danger",cancelVariant:"primary",cardClassName:"bg-red-950/70 border border-red-800/70",onConfirm:()=>{s(K.id),Y(null)},onCancel:()=>Y(null)})]})}function LZe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r}=Ir(),[i,s]=w.useState("status"),{data:o,isLoading:l}=tt({queryKey:["maintenanceOverview"],queryFn:se.getMaintenanceOverview}),{data:c}=tt({queryKey:["maintenanceTypes"],queryFn:se.getMaintenanceTypes}),u=lt({mutationFn:({id:T,notes:k})=>se.performMaintenance(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),e.invalidateQueries({queryKey:["maintenanceSummary"]}),n(t("maintenance.maintenanceComplete"))},onError:T=>{n(T.message,"error")}}),d=lt({mutationFn:({id:T,data:k})=>se.updateMaintenanceItem(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]})},onError:T=>{n(T.message,"error")}}),h=lt({mutationFn:({id:T,data:k})=>se.updateMaintenanceType(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.typeUpdated"))},onError:T=>{n(T.message,"error")}}),f=lt({mutationFn:se.deleteMaintenanceType,onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.typeDeleted"))},onError:T=>{n(T.message,"error")}}),p=lt({mutationFn:se.restoreDefaultMaintenanceTypes,onSuccess:T=>{e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.defaultsRestored",{count:T.restored}))},onError:T=>{n(T.message,"error")}}),x=lt({mutationFn:({printerId:T,hours:k})=>se.setPrinterHours(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),e.invalidateQueries({queryKey:["maintenanceSummary"]}),n(t("maintenance.printHoursUpdated"))},onError:T=>{n(T.message,"error")}}),v=lt({mutationFn:({printerId:T,typeId:k})=>se.assignMaintenanceType(T,k),onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.printerAssigned"))},onError:T=>{n(T.message,"error")}}),b=lt({mutationFn:se.removeMaintenanceItem,onSuccess:()=>{e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.printerRemoved"))},onError:T=>{n(T.message,"error")}}),y=T=>{u.mutate({id:T})},S=(T,k)=>{d.mutate({id:T,data:{enabled:k}})},N=(T,k)=>{x.mutate({printerId:T,hours:k})};if(l)return a.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})});const A=o?.reduce((T,k)=>T+k.due_count,0)||0,M=o?.reduce((T,k)=>T+k.warning_count,0)||0;return a.jsxs("div",{className:"p-4 md:p-8",children:[a.jsxs("div",{className:"mb-6",children:[a.jsx("h1",{className:"text-2xl font-bold text-white",children:t("maintenance.title")}),a.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:i==="status"?a.jsxs(a.Fragment,{children:[A>0&&a.jsx("span",{className:"text-red-400",children:t("maintenance.dueCount",{count:A})}),A>0&&M>0&&" · ",M>0&&a.jsx("span",{className:"text-amber-400",children:t("maintenance.warningCount",{count:M})}),A===0&&M===0&&a.jsx("span",{className:"text-bambu-green",children:t("maintenance.allOk")})]}):t("maintenance.configureSettings")})]}),a.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[a.jsx("button",{onClick:()=>s("status"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${i==="status"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:t("maintenance.statusTab")}),a.jsx("button",{onClick:()=>s("settings"),className:`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${i==="settings"?"text-bambu-green border-bambu-green":"text-bambu-gray border-transparent hover:text-white"}`,children:t("maintenance.settingsTab")})]}),i==="status"?a.jsx("div",{className:"space-y-6",children:o&&o.length>0?[...o].sort((T,k)=>{const j=T.due_count*10+T.warning_count,E=k.due_count*10+k.warning_count;return j!==E?E-j:T.printer_name.localeCompare(k.printer_name)}).map(T=>a.jsx(RZe,{overview:T,onPerform:y,onToggle:S,onSetHours:N,hasPermission:r,t},T.printer_id)):a.jsx(_t,{children:a.jsxs(Nt,{className:"text-center py-16",children:[a.jsx(Rf,{className:"w-16 h-16 mx-auto mb-4 text-bambu-gray/30"}),a.jsx("p",{className:"text-lg font-medium text-white mb-2",children:t("common.noPrinters")}),a.jsx("p",{className:"text-bambu-gray",children:t("maintenance.configureSettings")})]})})}):a.jsx(FZe,{overview:o,types:c||[],onUpdateInterval:(T,k)=>d.mutate({id:T,data:k}),onAddType:async(T,k)=>{const j=await se.createMaintenanceType(T);for(const E of k)await se.assignMaintenanceType(E,j.id);e.invalidateQueries({queryKey:["maintenanceTypes"]}),e.invalidateQueries({queryKey:["maintenanceOverview"]}),n(t("maintenance.typeUpdated"))},onUpdateType:(T,k)=>h.mutate({id:T,data:k}),onDeleteType:T=>f.mutate(T),onRestoreDefaults:()=>p.mutate(),isRestoringDefaults:p.isPending,onAssignType:(T,k)=>v.mutate({printerId:T,typeId:k}),onRemoveItem:T=>b.mutate(T),hasPermission:r,t})]})}const cK=["#ef4444","#f97316","#eab308","#22c55e","#06b6d4","#3b82f6","#8b5cf6","#ec4899","#6b7280"];function Kae({project:t,onClose:e,onSave:n,isLoading:r,t:i}){const[s,o]=w.useState(t?.name||""),[l,c]=w.useState(t?.description||""),[u,d]=w.useState(t?.color||cK[0]),[h,f]=w.useState(t?.target_count?.toString()||""),[p,x]=w.useState(t?.target_parts_count?.toString()||""),[v,b]=w.useState(t?.status||"active"),[y,S]=w.useState(t?.tags||""),[N,A]=w.useState(t?.due_date?.split("T")[0]||""),[M,T]=w.useState(t?.priority||"normal"),k=j=>{j.preventDefault(),n({name:s.trim(),description:l.trim()||void 0,color:u,target_count:h?parseInt(h,10):void 0,target_parts_count:p?parseInt(p,10):void 0,tags:y.trim()||void 0,due_date:N||void 0,priority:M,...t&&{status:v}})};return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:i(t?"projects.editProject":"projects.newProject")})}),a.jsxs("form",{onSubmit:k,className:"p-4 space-y-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("common.name")}),a.jsx("input",{type:"text",value:s,onChange:j=>o(j.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:i("projects.namePlaceholder"),required:!0})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("common.description")}),a.jsx("textarea",{value:l,onChange:j=>c(j.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:i("projects.descriptionPlaceholder"),rows:2})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.color")}),a.jsx("div",{className:"flex gap-2 flex-wrap",children:cK.map(j=>a.jsx("button",{type:"button",onClick:()=>d(j),className:`w-8 h-8 rounded-full transition-transform ${u===j?"ring-2 ring-white ring-offset-2 ring-offset-bambu-dark-secondary scale-110":""}`,style:{backgroundColor:j}},j))})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.targetPlates")}),a.jsx("input",{type:"number",value:h,onChange:j=>f(j.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:i("projects.targetPlatesPlaceholder"),min:"1"}),a.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("projects.targetPlatesHelp")})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.targetParts")}),a.jsx("input",{type:"number",value:p,onChange:j=>x(j.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:i("projects.targetPartsPlaceholder"),min:"1"}),a.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("projects.targetPartsHelp")})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.tagsLabel")}),a.jsx("input",{type:"text",value:y,onChange:j=>S(j.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:i("projects.tagsPlaceholder")})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.dueDate")}),a.jsx("input",{type:"date",value:N,onChange:j=>A(j.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"})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("projects.priority")}),a.jsxs("select",{value:M,onChange:j=>T(j.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:[a.jsx("option",{value:"low",children:i("projects.priorityLow")}),a.jsx("option",{value:"normal",children:i("projects.priorityNormal")}),a.jsx("option",{value:"high",children:i("projects.priorityHigh")}),a.jsx("option",{value:"urgent",children:i("projects.priorityUrgent")})]})]})]}),t&&a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("common.status")}),a.jsxs("select",{value:v,onChange:j=>b(j.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:[a.jsx("option",{value:"active",children:i("projects.statusActive")}),a.jsx("option",{value:"completed",children:i("projects.statusCompleted")}),a.jsx("option",{value:"archived",children:i("projects.statusArchived")})]})]}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!s.trim()||r,children:r?a.jsx(at,{className:"w-4 h-4 animate-spin"}):i(t?"common.save":"projects.create")})]})]})]})})}function OZe({project:t,onClick:e,onEdit:n,onDelete:r,hasPermission:i,t:s}){const o=t.target_count?Math.round(t.archive_count/t.target_count*100):0,l=t.target_parts_count?Math.round(t.completed_count/t.target_parts_count*100):0,c=t.status==="completed",u=t.status==="archived",[d,h]=w.useState(!1),p=c?{icon:fb,color:"text-bambu-green",bg:"bg-bambu-green/10"}:u?{icon:Rs,color:"text-bambu-gray",bg:"bg-bambu-gray/10"}:t.queue_count>0?{icon:Qn,color:"text-blue-400",bg:"bg-blue-400/10"}:{icon:xs,color:"text-bambu-gray",bg:"bg-bambu-gray/10"};return a.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:[a.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`}}),a.jsxs("div",{className:"p-5 pl-6",children:[a.jsxs("div",{className:"flex items-start justify-between mb-4",children:[a.jsxs("div",{className:"flex items-center gap-3 min-w-0 flex-1",children:[a.jsx("div",{className:`p-2 rounded-lg ${p.bg} flex-shrink-0`,children:a.jsx(p.icon,{className:`w-5 h-5 ${p.color}`})}),a.jsxs("div",{className:"min-w-0 flex-1",children:[a.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[a.jsx("h3",{className:"font-semibold text-white truncate",children:t.name}),t.target_parts_count?a.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${l>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.completed_count,"/",t.target_parts_count," ",s("projects.parts")]}):t.target_count?a.jsxs("span",{className:`text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium ${o>=100?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray"}`,children:[t.archive_count,"/",t.target_count," ",s("projects.plates")]}):t.completed_count>0?a.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full whitespace-nowrap font-medium bg-bambu-dark text-bambu-gray",children:[t.completed_count," ",s("projects.parts")]}):null,c&&a.jsx("span",{className:"text-xs bg-bambu-green/20 text-bambu-green px-2 py-0.5 rounded-full whitespace-nowrap",children:s("projects.done")}),u&&a.jsx("span",{className:"text-xs bg-bambu-gray/20 text-bambu-gray px-2 py-0.5 rounded-full whitespace-nowrap",children:s("projects.statusArchived")})]}),t.description&&a.jsx("p",{className:"text-sm text-bambu-gray/70 mt-1 line-clamp-1",children:t.description}),t.archives&&t.archives.length>0&&(()=>{const x=t.archives.map(S=>S.filament_type).filter(Boolean).flatMap(S=>S.split(",").map(N=>N.trim())).filter(Boolean),v=[...new Set(x)],b=t.archives.map(S=>S.filament_color).filter(Boolean).flatMap(S=>S.split(",").map(N=>N.trim())).filter(S=>S.startsWith("#")||/^[0-9A-Fa-f]{6}$/.test(S)),y=[...new Set(b)];return v.length===0&&y.length===0?null:a.jsxs("div",{className:"flex items-center gap-2 mt-1.5",children:[v.slice(0,3).map(S=>a.jsx("span",{className:"text-[10px] px-1.5 py-0.5 bg-bambu-dark text-bambu-gray rounded",children:S},S)),y.length>0&&a.jsxs("div",{className:"flex items-center gap-0.5",children:[y.slice(0,5).map(S=>a.jsx("div",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:S.startsWith("#")?S:`#${S}`},title:S},S)),y.length>5&&a.jsxs("span",{className:"text-[10px] text-bambu-gray ml-0.5",children:["+",y.length-5]})]})]})})()]})]}),a.jsxs("div",{className:"relative",onClick:x=>x.stopPropagation(),children:[a.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:()=>h(!d),children:a.jsx(Ah,{className:"w-4 h-4"})}),d&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>h(!1)}),a.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:[a.jsxs("button",{className:`w-full px-3 py-2 text-left text-sm flex items-center gap-2 ${i("projects:update")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{i("projects:update")&&(n(),h(!1))},disabled:!i("projects:update"),title:i("projects:update")?void 0:s("projects.noEditPermission"),children:[a.jsx(Pf,{className:"w-4 h-4"}),s("common.edit")]}),a.jsxs("button",{className:`w-full px-3 py-2 text-left text-sm flex items-center gap-2 ${i("projects:delete")?"text-red-400 hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{i("projects:delete")&&(r(),h(!1))},disabled:!i("projects:delete"),title:i("projects:delete")?void 0:s("projects.noDeletePermission"),children:[a.jsx(en,{className:"w-4 h-4"}),s("common.delete")]})]})]})]})]}),a.jsx("div",{className:"mb-4",children:t.target_count||t.target_parts_count?a.jsxs("div",{className:"space-y-3",children:[t.target_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[a.jsx("span",{className:"text-bambu-gray",children:s("projects.plates")}),a.jsxs("span",{className:o>=100?"text-bambu-green font-medium":"text-white",children:[t.archive_count," / ",t.target_count]})]}),a.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:a.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(o,100)}%`,background:o>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${o>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.target_parts_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between text-xs mb-1",children:[a.jsx("span",{className:"text-bambu-gray",children:s("projects.parts")}),a.jsxs("span",{className:l>=100?"text-bambu-green font-medium":"text-white",children:[t.completed_count," / ",t.target_parts_count]})]}),a.jsx("div",{className:"h-2 bg-bambu-dark/80 rounded-full overflow-hidden backdrop-blur-sm",children:a.jsx("div",{className:"h-full transition-all duration-500 ease-out rounded-full relative",style:{width:`${Math.min(l,100)}%`,background:l>=100?"linear-gradient(90deg, #22c55e, #4ade80)":`linear-gradient(90deg, ${t.color||"#6b7280"}, ${t.color||"#6b7280"}cc)`,boxShadow:`0 0 8px ${l>=100?"#22c55e":t.color||"#6b7280"}60`}})})]}),t.failed_count>0&&a.jsxs("div",{className:"text-xs text-red-400",children:[t.failed_count," ",s("projects.failed")]})]}):t.completed_count>0||t.failed_count>0?a.jsxs("div",{className:"flex items-center gap-4 text-xs",children:[t.completed_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-bambu-gray",children:[a.jsx(Rs,{className:"w-3.5 h-3.5"}),a.jsxs("span",{children:[t.completed_count," ",s("projects.completed")]})]}),t.failed_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",children:[a.jsx(Cn,{className:"w-3.5 h-3.5"}),a.jsxs("span",{children:[t.failed_count," ",s("projects.failed")]})]}),t.queue_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-blue-400",children:[a.jsx(Qn,{className:"w-3.5 h-3.5"}),a.jsxs("span",{children:[t.queue_count," ",s("projects.inQueue")]})]})]}):a.jsx("div",{className:"text-xs text-bambu-gray/60 italic",children:s("projects.noPrintsYet")})}),t.archives&&t.archives.length>0&&a.jsxs("div",{className:"mb-4",children:[a.jsx("div",{className:"grid grid-cols-4 gap-1.5",children:t.archives.slice(0,4).map(x=>a.jsxs("div",{className:"relative aspect-square rounded-lg bg-bambu-dark overflow-hidden border border-bambu-dark-tertiary",title:x.print_name||"Unknown",children:[x.thumbnail_path?a.jsx("img",{src:se.getArchiveThumbnail(x.id),alt:x.print_name||"",className:"w-full h-full object-cover"}):a.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray/50",children:a.jsx(ma,{className:"w-6 h-6"})}),x.status==="failed"&&a.jsx("div",{className:"absolute inset-0 bg-red-500/40 flex items-center justify-center",children:a.jsx(Cn,{className:"w-4 h-4 text-white"})})]},x.id))}),t.archive_count>4&&a.jsx("p",{className:"text-xs text-bambu-gray mt-1.5 text-center",children:s("common.more",{count:t.archive_count-4})})]}),a.jsxs("div",{className:"flex items-center justify-between pt-3 border-t border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-4 text-xs text-bambu-gray",children:[a.jsxs("div",{className:"flex items-center gap-1.5",title:s("projects.printJobs"),children:[a.jsx(rs,{className:"w-3.5 h-3.5 text-blue-400"}),a.jsxs("span",{children:[t.archive_count," ",s("projects.plates")]})]}),a.jsxs("div",{className:"flex items-center gap-1.5",title:s("projects.partsPrinted"),children:[a.jsx(ma,{className:"w-3.5 h-3.5 text-bambu-green"}),a.jsxs("span",{children:[t.completed_count," ",s("projects.parts")]})]}),t.failed_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-red-400",title:s("projects.failedParts"),children:[a.jsx(Cn,{className:"w-3.5 h-3.5"}),a.jsx("span",{children:t.failed_count})]}),t.queue_count>0&&a.jsxs("div",{className:"flex items-center gap-1.5 text-yellow-400",title:s("projects.inQueue"),children:[a.jsx(g3,{className:"w-3.5 h-3.5"}),a.jsx("span",{children:t.queue_count})]})]}),a.jsx(fa,{className:"w-4 h-4 text-bambu-gray/50 group-hover:text-bambu-gray transition-colors"})]})]})]})}function IZe(){const{t}=Vt(),e=Hc(),n=tn(),{showToast:r}=mn(),{hasPermission:i}=Ir(),[s,o]=w.useState(!1),[l,c]=w.useState(),[u,d]=w.useState("active"),[h,f]=w.useState(null),{data:p,isLoading:x}=tt({queryKey:["projects",u==="all"?void 0:u],queryFn:()=>se.getProjects(u==="all"?void 0:u)}),v=lt({mutationFn:B=>se.createProject(B),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),o(!1),r(t("projects.toast.created"),"success")},onError:B=>{r(B.message,"error")}}),b=lt({mutationFn:({id:B,data:R})=>se.updateProject(B,R),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),o(!1),c(void 0),r(t("projects.toast.updated"),"success")},onError:B=>{r(B.message,"error")}}),y=lt({mutationFn:B=>se.deleteProject(B),onSuccess:()=>{f(null),r(t("projects.toast.deleted"),"success"),setTimeout(()=>window.location.reload(),100)},onError:B=>{f(null),r(B.message,"error")}}),S=lt({mutationFn:B=>se.importProject(B),onSuccess:()=>{n.invalidateQueries({queryKey:["projects"]}),r(t("projects.toast.imported"),"success")},onError:B=>{r(B.message,"error")}}),N=w.useRef(null),A=async()=>{try{const B=await se.getProjects(),R=await Promise.all(B.map(async K=>await se.exportProjectJson(K.id))),V=new Blob([JSON.stringify(R,null,2)],{type:"application/json"}),q=URL.createObjectURL(V),I=document.createElement("a");I.href=q,I.download=`bambuddy_projects_${new Date().toISOString().split("T")[0]}.json`,I.click(),URL.revokeObjectURL(q),r(t("projects.toast.exported"),"success")}catch(B){r(B.message,"error")}},M=()=>{N.current?.click()},T=async B=>{const R=B.target.files?.[0];if(R){try{if(R.name.toLowerCase().endsWith(".zip"))await se.importProjectFile(R),n.invalidateQueries({queryKey:["projects"]}),r(t("projects.toast.imported"),"success");else{const q=await R.text(),I=JSON.parse(q),K=Array.isArray(I)?I:[I];for(const Y of K)await S.mutateAsync(Y);K.length>1&&r(t("projects.toast.multipleImported",{count:K.length}),"success")}}catch(V){r(`${t("projects.toast.importFailed")}: ${V.message}`,"error")}B.target.value=""}},k=B=>{l?b.mutate({id:l.id,data:B}):v.mutate(B)},j=B=>{c(B),o(!0)},E=B=>{e(`/projects/${B.id}`)},P=B=>{f(B)},U=()=>{h!==null&&y.mutate(h)},L=p?.reduce((B,R)=>(B[R.status]=(B[R.status]||0)+1,B.all=(B.all||0)+1,B),{})||{};return a.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[a.jsx("input",{ref:N,type:"file",accept:".json,.zip",onChange:T,className:"hidden"}),a.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4",children:[a.jsxs("div",{children:[a.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:a.jsx(xs,{className:"w-6 h-6 text-bambu-green"})}),t("projects.title")]}),a.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:t("projects.subtitle")})]}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{variant:"secondary",onClick:M,disabled:!i("projects:create"),title:i("projects:create")?t("projects.importProject"):t("projects.noImportPermission"),children:[a.jsx(ia,{className:"w-4 h-4 mr-2"}),t("projects.import")]}),a.jsxs(Me,{variant:"secondary",onClick:A,disabled:!i("projects:read"),title:i("projects:read")?t("projects.exportAll"):t("projects.noExportPermission"),children:[a.jsx(di,{className:"w-4 h-4 mr-2"}),t("projects.export")]}),a.jsxs(Me,{onClick:()=>o(!0),className:"sm:w-auto w-full",disabled:!i("projects:create"),title:i("projects:create")?void 0:t("projects.noCreatePermission"),children:[a.jsx(ar,{className:"w-4 h-4 mr-2"}),t("projects.newProject")]})]})]}),a.jsx("div",{className:"flex gap-1 p-1 bg-bambu-dark rounded-xl w-fit",children:[{key:"active",label:t("projects.statusActive"),icon:Qn},{key:"completed",label:t("projects.statusCompleted"),icon:fb},{key:"archived",label:t("projects.statusArchived"),icon:Rs},{key:"all",label:t("common.all"),icon:xs}].map(({key:B,label:R,icon:V})=>a.jsxs("button",{onClick:()=>d(B),className:`flex items-center gap-2 px-4 py-2 text-sm rounded-lg transition-all ${u===B?"bg-bambu-card text-white shadow-sm":"text-bambu-gray hover:text-white"}`,children:[a.jsx(V,{className:"w-4 h-4"}),a.jsx("span",{children:R}),L[B]>0&&a.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded-full ${u===B?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary"}`,children:L[B]})]},B))}),x?a.jsx("div",{className:"flex items-center justify-center py-20",children:a.jsxs("div",{className:"flex flex-col items-center gap-3",children:[a.jsx(at,{className:"w-8 h-8 animate-spin text-bambu-green"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:t("projects.loading")})]})}):p?.length===0?a.jsxs("div",{className:"flex flex-col items-center justify-center py-20 px-4",children:[a.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:a.jsx(xs,{className:"w-12 h-12 text-bambu-gray/50"})}),a.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:u==="all"?t("projects.noProjects"):t("projects.noProjectsFiltered",{status:u})}),a.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:u==="all"?t("projects.createFirst"):t("projects.noProjectsFilteredHelp",{status:u})}),u==="all"&&a.jsxs(Me,{onClick:()=>o(!0),disabled:!i("projects:create"),title:i("projects:create")?void 0:t("projects.noCreatePermission"),children:[a.jsx(ar,{className:"w-4 h-4 mr-2"}),t("projects.createFirstButton")]})]}):a.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-8",children:p?.map(B=>a.jsx(OZe,{project:B,onClick:()=>E(B),onEdit:()=>j(B),onDelete:()=>P(B.id),hasPermission:i,t},B.id))}),h!==null&&a.jsx(kn,{title:t("projects.deleteProject"),message:t("projects.deleteConfirm"),confirmText:t("projects.deleteProject"),variant:"danger",onConfirm:U,onCancel:()=>f(null)}),s&&a.jsx(Kae,{project:l,onClose:()=>{o(!1),c(void 0)},onSave:k,isLoading:v.isPending||b.isPending,t})]})}function zZe(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 UZe(t){return t>=1e3?`${(t/1e3).toFixed(2)}kg`:`${Math.round(t)}g`}function BZe({status:t,t:e}){const n={active:"bg-bambu-green/20 text-bambu-green",completed:"bg-blue-500/20 text-blue-400",archived:"bg-bambu-gray/20 text-bambu-gray"},r=n[t]||n.active,i={active:e("projectDetail.status.active"),completed:e("projectDetail.status.completed"),archived:e("projectDetail.status.archived")};return a.jsx("span",{className:`px-2 py-1 rounded text-sm font-medium ${r}`,children:i[t]||t.charAt(0).toUpperCase()+t.slice(1)})}function uK({icon:t,label:e,value:n,subValue:r,hint:i,color:s="text-bambu-gray"}){return a.jsx(_t,{children:a.jsx(Nt,{className:"p-4",children:a.jsxs("div",{className:"flex items-center gap-3",title:i,children:[a.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark ${s}`,children:a.jsx(t,{className:"w-5 h-5"})}),a.jsxs("div",{children:[a.jsx("p",{className:"text-sm text-bambu-gray",children:e}),a.jsx("p",{className:"text-xl font-semibold text-white",children:n}),r&&a.jsx("p",{className:"text-xs text-bambu-gray/70",children:r})]})]})})})}function HZe({archives:t,t:e}){return t.length===0?a.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[a.jsx(ma,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),a.jsx("p",{children:e("projectDetail.noPrints")})]}):a.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(n=>a.jsxs(Xs,{to:`/archives?search=${encodeURIComponent(n.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:[n.thumbnail_path?a.jsx("img",{src:se.getArchiveThumbnail(n.id),alt:n.print_name||"Print",className:"w-full h-full object-cover"}):a.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:a.jsx(ma,{className:"w-8 h-8"})}),n.status==="failed"&&a.jsx("div",{className:"absolute inset-0 bg-red-500/30 flex items-center justify-center",children:a.jsx(ha,{className:"w-8 h-8 text-white"})}),n.status==="completed"&&a.jsx("div",{className:"absolute top-1 right-1",children:a.jsx(gr,{className:"w-4 h-4 text-bambu-green"})}),a.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:a.jsx("p",{className:"text-xs text-white truncate",children:n.print_name||"Unknown"})})]},n.id))})}function $Ze({priority:t,t:e}){const n={low:{color:"bg-gray-500/20 text-gray-400",label:e("projectDetail.priority.low")},normal:{color:"bg-blue-500/20 text-blue-400",label:e("projectDetail.priority.normal")},high:{color:"bg-orange-500/20 text-orange-400",label:e("projectDetail.priority.high")},urgent:{color:"bg-red-500/20 text-red-400",label:e("projectDetail.priority.urgent")}},{color:r,label:i}=n[t]||n.normal;return a.jsxs("span",{className:`px-2 py-1 rounded text-xs font-medium flex items-center gap-1 ${r}`,children:[t==="urgent"&&a.jsx(Cn,{className:"w-3 h-3"}),i]})}function VZe(t){return t?gb(t,{year:"numeric",month:"short",day:"numeric"}):""}function Gj(t,e){if(!t)return null;const n=Zi(t);if(!n)return null;const r=new Date,i=Math.ceil((n.getTime()-r.getTime())/(1e3*60*60*24));return i<0?{color:"text-red-400",label:e("projectDetail.dueDate.overdue")}:i===0?{color:"text-orange-400",label:e("projectDetail.dueDate.today")}:i<=3?{color:"text-yellow-400",label:e("projectDetail.dueDate.daysLeft",{count:i})}:{color:"text-bambu-gray",label:e("projectDetail.dueDate.daysLeft",{count:i})}}function GZe(){const{id:t}=Yk(),e=Hc(),{t:n}=Vt(),r=tn(),{showToast:i}=mn(),{hasPermission:s}=Ir(),[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,h]=w.useState(""),f=parseInt(t||"0",10),{data:p,isLoading:x,error:v}=tt({queryKey:["project",f],queryFn:()=>se.getProject(f),enabled:f>0}),{data:b,isLoading:y}=tt({queryKey:["project-archives",f],queryFn:()=>se.getProjectArchives(f),enabled:f>0}),{data:S,isLoading:N}=tt({queryKey:["project-bom",f],queryFn:()=>se.getProjectBOM(f),enabled:f>0}),{data:A,isLoading:M}=tt({queryKey:["project-timeline",f],queryFn:()=>se.getProjectTimeline(f,20),enabled:f>0}),{data:T}=tt({queryKey:["settings"],queryFn:se.getSettings}),{data:k}=tt({queryKey:["project-folders",f],queryFn:()=>se.getLibraryFoldersByProject(f),enabled:f>0}),j=Pu(T?.currency||"USD"),E=T?.time_format||"system",P=lt({mutationFn:je=>se.updateProject(f,je),onSuccess:()=>{r.invalidateQueries({queryKey:["project",f]}),r.invalidateQueries({queryKey:["projects"]}),l(!1),u(!1),i(n("projectDetail.toast.projectUpdated"),"success")},onError:je=>{i(je.message,"error")}}),U=()=>{h(p?.notes||""),u(!0)},L=()=>{P.mutate({notes:d})},B=()=>{u(!1),h("")},[R,V]=w.useState(""),[q,I]=w.useState(1),[K,Y]=w.useState(""),[le,H]=w.useState(""),[X,te]=w.useState(""),[J,Se]=w.useState(!1),[fe,me]=w.useState(!1),[De,de]=w.useState(null),[ce,Ne]=w.useState(""),[ie,Le]=w.useState(1),[$,W]=w.useState(""),[ue,Z]=w.useState(""),[oe,Q]=w.useState(""),[he,be]=w.useState({isOpen:!1,title:"",message:"",onConfirm:()=>{}}),ee=lt({mutationFn:je=>se.createBOMItem(f,je),onSuccess:()=>{r.invalidateQueries({queryKey:["project-bom",f]}),r.invalidateQueries({queryKey:["project",f]}),V(""),I(1),Y(""),H(""),te(""),Se(!1),i(n("projectDetail.toast.partAdded"),"success")},onError:je=>i(je.message,"error")}),z=lt({mutationFn:({itemId:je,data:Ve})=>se.updateBOMItem(f,je,Ve),onSuccess:()=>{r.invalidateQueries({queryKey:["project-bom",f]}),r.invalidateQueries({queryKey:["project",f]}),de(null)},onError:je=>i(je.message,"error")}),D=lt({mutationFn:je=>se.deleteBOMItem(f,je),onSuccess:()=>{r.invalidateQueries({queryKey:["project-bom",f]}),r.invalidateQueries({queryKey:["project",f]}),i(n("projectDetail.toast.partRemoved"),"success")},onError:je=>i(je.message,"error")}),G=je=>{je.preventDefault(),R.trim()&&ee.mutate({name:R.trim(),quantity_needed:q,unit_price:K?parseFloat(K):void 0,sourcing_url:le.trim()||void 0,remarks:X.trim()||void 0})},pe=je=>{const Ve=je.is_complete?0:je.quantity_needed;z.mutate({itemId:je.id,data:{quantity_acquired:Ve}})},ve=(je,Ve)=>{be({isOpen:!0,title:n("projectDetail.bom.deletePart"),message:n("projectDetail.bom.deleteConfirm",{name:Ve}),onConfirm:()=>{be(Ae=>({...Ae,isOpen:!1})),D.mutate(je)}})},xe=je=>{de(je),Ne(je.name),Le(je.quantity_needed),W(je.unit_price?.toString()||""),Z(je.sourcing_url||""),Q(je.remarks||"")},ze=je=>{je.preventDefault(),!(!De||!ce.trim())&&z.mutate({itemId:De.id,data:{name:ce.trim(),quantity_needed:ie,unit_price:$?parseFloat($):void 0,sourcing_url:ue.trim()||void 0,remarks:oe.trim()||void 0}})},_e=()=>{de(null)},He=async()=>{try{const{blob:je,filename:Ve}=await se.exportProjectZip(Number(f)),Ae=URL.createObjectURL(je),Je=document.createElement("a");Je.href=Ae,Je.download=Ve||`${p?.name||"project"}_${new Date().toISOString().split("T")[0]}.zip`,Je.click(),URL.revokeObjectURL(Ae),i(n("projectDetail.toast.projectExported"),"success")}catch(je){i(je.message,"error")}},Ze=lt({mutationFn:()=>se.createTemplateFromProject(f),onSuccess:()=>{r.invalidateQueries({queryKey:["projects"]}),i(n("projectDetail.toast.templateCreated"),"success")},onError:je=>i(je.message,"error")}),we=je=>Ff(je,E,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"});if(x)return a.jsx("div",{className:"flex items-center justify-center py-24",children:a.jsx(at,{className:"w-8 h-8 animate-spin text-bambu-green"})});if(v||!p)return a.jsxs("div",{className:"text-center py-24",children:[a.jsx("p",{className:"text-bambu-gray",children:v?`${n("common.error")}: ${v.message}`:n("projectDetail.notFound")}),a.jsx(Me,{variant:"secondary",className:"mt-4",onClick:()=>e("/projects"),children:n("projectDetail.backToProjects")})]});const ye=p.stats,ke=ye?.progress_percent??0,Ue=ye?.parts_progress_percent??0;return a.jsxs("div",{className:"p-4 md:p-8 space-y-8",children:[a.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[a.jsx(Xs,{to:"/projects",className:"hover:text-white transition-colors",children:n("navigation.projects")}),a.jsx(fa,{className:"w-4 h-4"}),a.jsx("span",{className:"text-white",children:p.name})]}),a.jsxs("div",{className:"flex items-start justify-between",children:[a.jsxs("div",{className:"flex items-center gap-4",children:[a.jsx("button",{onClick:()=>e("/projects"),className:"p-2 rounded-lg bg-bambu-card hover:bg-bambu-dark-tertiary transition-colors",children:a.jsx(fde,{className:"w-5 h-5 text-bambu-gray"})}),a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"w-4 h-4 rounded-full flex-shrink-0",style:{backgroundColor:p.color||"#6b7280"}}),a.jsxs("div",{children:[a.jsx("h1",{className:"text-2xl font-bold text-white",children:p.name}),p.description&&a.jsx("p",{className:"text-bambu-gray mt-1",children:p.description})]})]}),a.jsx(BZe,{status:p.status,t:n})]}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{variant:"secondary",onClick:He,disabled:!s("projects:read"),title:s("projects:read")?n("projectDetail.exportProject"):n("projectDetail.noExportPermission"),children:[a.jsx(di,{className:"w-4 h-4 mr-2"}),n("projectDetail.export")]}),a.jsxs(Me,{onClick:()=>l(!0),disabled:!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.noEditPermission"),children:[a.jsx(Pf,{className:"w-4 h-4 mr-2"}),n("common.edit")]})]})]}),(p.target_count||p.target_parts_count)&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4 space-y-4",children:[p.target_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-2",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:n("projectDetail.progress.platesProgress")}),a.jsxs("span",{className:"text-sm font-medium text-white",children:[ye?.total_archives||0," / ",p.target_count," ",n("projectDetail.progress.printJobs")]})]}),a.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:a.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(ke,100)}%`,backgroundColor:ke>=100?"#22c55e":p.color||"#6b7280"}})}),a.jsxs("div",{className:"flex justify-between mt-1",children:[a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.percentComplete",{percent:ke.toFixed(0)})}),ye?.remaining_prints!=null&&ye.remaining_prints>0&&a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.remaining",{count:ye.remaining_prints})})]})]}),p.target_parts_count&&a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center justify-between mb-2",children:[a.jsx("span",{className:"text-sm text-bambu-gray",children:n("projectDetail.progress.partsProgress")}),a.jsxs("span",{className:"text-sm font-medium text-white",children:[ye?.completed_prints||0," / ",p.target_parts_count," ",n("projectDetail.progress.parts")]})]}),a.jsx("div",{className:"h-3 bg-bambu-dark rounded-full overflow-hidden",children:a.jsx("div",{className:"h-full transition-all duration-500",style:{width:`${Math.min(Ue,100)}%`,backgroundColor:Ue>=100?"#22c55e":p.color||"#6b7280"}})}),a.jsxs("div",{className:"flex justify-between mt-1",children:[a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.percentComplete",{percent:Ue.toFixed(0)})}),ye?.remaining_parts!=null&&ye.remaining_parts>0&&a.jsx("span",{className:"text-xs text-bambu-gray/70",children:n("projectDetail.progress.remaining",{count:ye.remaining_parts})})]})]})]})}),ye&&a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[a.jsx(_t,{children:a.jsx(Nt,{className:"p-4",children:a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:a.jsx(ma,{className:"w-5 h-5"})}),a.jsxs("div",{children:[a.jsx("p",{className:"text-sm text-bambu-gray",children:n("projectDetail.stats.printJobs")}),a.jsxs("p",{className:"text-xl font-semibold text-white",children:[ye.total_archives," ",a.jsx("span",{className:"text-sm font-normal text-bambu-gray",children:n("projectDetail.stats.total")})]}),ye.failed_prints>0&&a.jsx("p",{className:"text-sm text-status-error",children:n("projectDetail.stats.failed",{count:ye.failed_prints})}),a.jsx("p",{className:"text-sm text-bambu-gray",children:n("projectDetail.stats.partsPrinted",{count:ye.completed_prints})})]})]})})}),a.jsx(uK,{icon:Qn,label:n("projectDetail.stats.printTime"),value:zZe(ye.total_print_time_hours),color:"text-yellow-400"}),a.jsx(uK,{icon:Cr,label:n("projectDetail.stats.filamentUsed"),value:UZe(ye.total_filament_grams),color:"text-purple-400"})]}),ye&&(ye.estimated_cost>0||p.budget)&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsx("h2",{className:"text-lg font-semibold text-white mb-3",children:n("projectDetail.cost.title")}),a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.filamentCost")}),a.jsxs("p",{className:"text-lg font-semibold text-white",children:[j,ye.estimated_cost.toFixed(2)]})]}),ye.total_energy_kwh>0&&a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.energy")}),a.jsxs("p",{className:"text-lg font-semibold text-white",children:[ye.total_energy_kwh.toFixed(2)," kWh",ye.total_energy_cost>0&&a.jsxs("span",{className:"text-sm text-bambu-gray ml-1",children:["(",j,ye.total_energy_cost.toFixed(2),")"]})]})]}),p.budget&&a.jsxs(a.Fragment,{children:[a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.budget")}),a.jsxs("p",{className:"text-lg font-semibold text-white",children:[j,p.budget.toFixed(2)]})]}),a.jsxs("div",{children:[a.jsx("p",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.cost.remaining")}),a.jsxs("p",{className:`text-lg font-semibold ${p.budget-ye.estimated_cost>=0?"text-bambu-green":"text-red-400"}`,children:[j,(p.budget-ye.estimated_cost).toFixed(2)]})]})]})]})]})}),p.children&&p.children.length>0&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-3",children:[a.jsx(tme,{className:"w-5 h-5"}),n("projectDetail.subProjects.title",{count:p.children.length})]}),a.jsx("div",{className:"space-y-2",children:p.children.map(je=>a.jsxs(Xs,{to:`/projects/${je.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"w-3 h-3 rounded-full",style:{backgroundColor:je.color||"#6b7280"}}),a.jsx("span",{className:"text-white",children:je.name}),a.jsx("span",{className:`text-xs px-2 py-0.5 rounded ${je.status==="completed"?"bg-status-ok/20 text-status-ok":je.status==="archived"?"bg-bambu-gray/20 text-bambu-gray":"bg-blue-500/20 text-blue-400"}`,children:je.status})]}),je.progress_percent!==null&&a.jsxs("span",{className:"text-sm text-bambu-gray",children:[je.progress_percent.toFixed(0),"%"]})]},je.id))})]})}),p.parent_id&&p.parent_name&&a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(rs,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:"text-bambu-gray",children:n("projectDetail.partOf")}),a.jsx(Xs,{to:`/projects/${p.parent_id}`,className:"text-bambu-green hover:underline",children:p.parent_name})]}),(p.tags||p.due_date||p.priority!=="normal")&&a.jsxs("div",{className:"flex flex-wrap items-center gap-4",children:[p.priority&&p.priority!=="normal"&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-xs text-bambu-gray uppercase",children:n("projectDetail.priorityLabel")}),a.jsx($Ze,{priority:p.priority,t:n})]}),p.due_date&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(ii,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:"text-sm text-white",children:VZe(p.due_date)}),Gj(p.due_date,n)&&a.jsxs("span",{className:`text-xs ${Gj(p.due_date,n).color}`,children:["(",Gj(p.due_date,n).label,")"]})]}),p.tags&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(Vh,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("div",{className:"flex flex-wrap gap-1",children:p.tags.split(",").map((je,Ve)=>a.jsx("span",{className:"px-2 py-0.5 bg-bambu-dark-tertiary text-bambu-gray text-xs rounded",children:je.trim()},Ve))})]})]}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(Ps,{className:"w-5 h-5"}),n("projectDetail.notes.title")]}),c?a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(Me,{variant:"secondary",size:"sm",onClick:B,disabled:P.isPending,children:[a.jsx(Ft,{className:"w-4 h-4 mr-1"}),n("common.cancel")]}),a.jsxs(Me,{size:"sm",onClick:L,disabled:P.isPending,children:[P.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin mr-1"}):a.jsx(bs,{className:"w-4 h-4 mr-1"}),n("common.save")]})]}):a.jsxs(Me,{variant:"secondary",size:"sm",onClick:U,disabled:!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.notes.noEditPermission"),children:[a.jsx(Pf,{className:"w-4 h-4 mr-1"}),n("common.edit")]})]}),c?a.jsx(oF,{content:d,onChange:h,placeholder:n("projectDetail.notes.placeholder")}):p.notes?a.jsx("div",{className:"prose prose-invert prose-sm max-w-none",dangerouslySetInnerHTML:{__html:p.notes}}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.notes.empty")})]})}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsx("div",{className:"flex items-center justify-between mb-3",children:a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(Xl,{className:"w-5 h-5"}),n("projectDetail.files.title")]})}),a.jsxs("p",{className:"text-xs text-bambu-gray mb-3",children:[a.jsx(Xs,{to:"/files",className:"text-bambu-green hover:underline",children:n("projectDetail.files.linkFolders")})," ",n("projectDetail.files.forQuickAccess")]}),k&&k.length>0?a.jsx("div",{className:"space-y-2",children:k.map(je=>a.jsxs(Xs,{to:`/files?folder=${je.id}`,className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg hover:bg-bambu-dark-tertiary transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[a.jsx(Xl,{className:"w-5 h-5 text-bambu-green flex-shrink-0"}),a.jsxs("div",{className:"min-w-0",children:[a.jsx("p",{className:"text-sm text-white truncate",children:je.name}),a.jsx("p",{className:"text-xs text-bambu-gray",children:n("projectDetail.files.fileCount",{count:je.file_count})})]})]}),a.jsx(fa,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"})]},je.id))}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.files.empty")})]})}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("div",{className:"flex items-center justify-between mb-4",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(bX,{className:"w-5 h-5"}),n("projectDetail.bom.title"),ye&&ye.bom_total_items>0&&a.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",n("projectDetail.bom.acquired",{completed:ye.bom_completed_items,total:ye.bom_total_items}),")"]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[S&&S.some(je=>je.is_complete)&&a.jsx("button",{onClick:()=>me(!fe),className:`text-xs px-2 py-1 rounded transition-colors ${fe?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:n(fe?"projectDetail.bom.showAll":"projectDetail.bom.hideDone")}),!J&&a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>Se(!0),disabled:!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.bom.noAddPermission"),children:[a.jsx(ar,{className:"w-4 h-4 mr-1"}),n("projectDetail.bom.addPart")]})]})]}),J&&a.jsxs("form",{onSubmit:G,className:"bg-bambu-dark rounded-lg p-4 mb-4 space-y-3",children:[a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[a.jsx("input",{type:"text",value:R,onChange:je=>V(je.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:n("projectDetail.bom.partNamePlaceholder"),autoFocus:!0}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx("input",{type:"number",value:q,onChange:je=>I(parseInt(je.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:n("projectDetail.bom.qty")}),a.jsx("input",{type:"number",step:"0.01",value:K,onChange:je=>Y(je.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:n("projectDetail.bom.price",{currency:j})})]})]}),a.jsx("input",{type:"url",value:le,onChange:je=>H(je.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:n("projectDetail.bom.sourcingUrlPlaceholder")}),a.jsx("input",{type:"text",value:X,onChange:je=>te(je.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:n("projectDetail.bom.remarksPlaceholder")}),a.jsxs("div",{className:"flex justify-end gap-2",children:[a.jsx(Me,{type:"button",variant:"secondary",size:"sm",onClick:()=>Se(!1),children:n("common.cancel")}),a.jsx(Me,{type:"submit",size:"sm",disabled:!R.trim()||ee.isPending,children:ee.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):n("projectDetail.bom.addPart")})]})]}),N?a.jsx("div",{className:"flex items-center justify-center py-4",children:a.jsx(at,{className:"w-6 h-6 animate-spin text-bambu-green"})}):S&&S.length>0?a.jsxs("div",{className:"space-y-2",children:[S.filter(je=>!fe||!je.is_complete).map(je=>a.jsx("div",{className:`p-3 rounded-lg transition-colors ${je.is_complete?"bg-status-ok/10":"bg-bambu-dark"}`,children:De?.id===je.id?a.jsxs("form",{onSubmit:ze,className:"space-y-3",children:[a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:[a.jsx("input",{type:"text",value:ce,onChange:Ve=>Ne(Ve.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:n("projectDetail.bom.partName"),autoFocus:!0}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx("input",{type:"number",value:ie,onChange:Ve=>Le(parseInt(Ve.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:n("projectDetail.bom.qty")}),a.jsx("input",{type:"number",step:"0.01",value:$,onChange:Ve=>W(Ve.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:n("projectDetail.bom.price",{currency:j})})]})]}),a.jsx("input",{type:"url",value:ue,onChange:Ve=>Z(Ve.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:n("projectDetail.bom.sourcingUrlPlaceholder")}),a.jsx("input",{type:"text",value:oe,onChange:Ve=>Q(Ve.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:n("projectDetail.bom.remarksPlaceholder")}),a.jsxs("div",{className:"flex justify-end gap-2",children:[a.jsx(Me,{type:"button",variant:"secondary",size:"sm",onClick:_e,children:n("common.cancel")}),a.jsx(Me,{type:"submit",size:"sm",disabled:!ce.trim()||z.isPending,children:z.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin"}):n("common.save")})]})]}):a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx("button",{onClick:()=>s("projects:update")&&pe(je),disabled:z.isPending||!s("projects:update"),title:s("projects:update")?void 0:n("projectDetail.bom.noUpdatePermission"),className:`w-5 h-5 mt-0.5 rounded border-2 flex items-center justify-center transition-colors flex-shrink-0 ${je.is_complete?"bg-status-ok border-status-ok text-white":s("projects:update")?"border-bambu-gray hover:border-bambu-green":"border-bambu-gray/50 cursor-not-allowed"}`,children:je.is_complete&&a.jsx(gr,{className:"w-3 h-3"})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsxs("div",{className:"flex items-center justify-between gap-2",children:[a.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[a.jsxs("p",{className:`text-sm font-medium ${je.is_complete?"text-bambu-gray line-through":"text-white"}`,children:[je.name,a.jsxs("span",{className:"text-bambu-gray font-normal ml-2",children:["x",je.quantity_needed]})]}),je.unit_price!==null&&a.jsxs("span",{className:"text-xs text-bambu-green whitespace-nowrap",children:[j,(je.unit_price*je.quantity_needed).toFixed(2)]})]}),a.jsxs("div",{className:"flex items-center gap-1",children:[a.jsx("button",{onClick:()=>s("projects:update")&&xe(je),disabled:!s("projects:update"),className:`p-1 rounded transition-colors flex-shrink-0 ${s("projects:update")?"hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("projects:update")?n("common.edit"):n("projectDetail.bom.noEditPermission"),children:a.jsx($i,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>s("projects:update")&&ve(je.id,je.name),disabled:!s("projects:update"),className:`p-1 rounded transition-colors flex-shrink-0 ${s("projects:update")?"hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-red-400":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("projects:update")?n("common.delete"):n("projectDetail.bom.noDeletePermission"),children:a.jsx(en,{className:"w-4 h-4"})})]})]}),je.sourcing_url&&a.jsxs("a",{href:je.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:Ve=>Ve.stopPropagation(),children:[a.jsx(Kr,{className:"w-3 h-3 flex-shrink-0"}),a.jsx("span",{className:"truncate",children:(()=>{try{return new URL(je.sourcing_url).hostname.replace("www.","")}catch{return je.sourcing_url}})()})]}),je.remarks&&a.jsx("p",{className:"mt-1 text-xs text-bambu-gray/80 italic",children:je.remarks})]})]})},je.id)),S.some(je=>je.unit_price!==null)&&a.jsxs("div",{className:"pt-2 mt-2 border-t border-bambu-dark-tertiary flex justify-between text-sm",children:[a.jsx("span",{className:"text-bambu-gray",children:n("projectDetail.bom.totalCost")}),a.jsxs("span",{className:"text-white font-medium",children:[j,S.reduce((je,Ve)=>je+(Ve.unit_price||0)*Ve.quantity_needed,0).toFixed(2)]})]})]}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.bom.empty")})]})}),a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsx("div",{className:"flex items-center justify-between mb-3",children:a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(N0,{className:"w-5 h-5"}),n("projectDetail.timeline.title")]})}),M?a.jsx("div",{className:"flex items-center justify-center py-4",children:a.jsx(at,{className:"w-6 h-6 animate-spin text-bambu-green"})}):A&&A.length>0?a.jsx("div",{className:"space-y-3",children:A.slice(0,10).map((je,Ve)=>a.jsxs("div",{className:"flex gap-3",children:[a.jsxs("div",{className:`w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 ${je.event_type==="print_completed"?"bg-status-ok/20 text-status-ok":je.event_type==="print_failed"?"bg-status-error/20 text-status-error":je.event_type==="print_started"?"bg-yellow-500/20 text-yellow-400":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[je.event_type==="print_completed"&&a.jsx(gr,{className:"w-4 h-4"}),je.event_type==="print_failed"&&a.jsx(ha,{className:"w-4 h-4"}),je.event_type==="print_started"&&a.jsx(Cr,{className:"w-4 h-4"}),je.event_type==="queued"&&a.jsx(g3,{className:"w-4 h-4"}),je.event_type==="project_created"&&a.jsx(ar,{className:"w-4 h-4"})]}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm text-white",children:je.title}),je.description&&a.jsx("p",{className:"text-xs text-bambu-gray truncate",children:je.description}),a.jsx("p",{className:"text-xs text-bambu-gray/70",children:we(je.timestamp)})]})]},Ve))}):a.jsx("p",{className:"text-bambu-gray/70 text-sm italic",children:n("projectDetail.timeline.empty")})]})}),!p.is_template&&a.jsx("div",{className:"flex justify-end",children:a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>Ze.mutate(),disabled:Ze.isPending||!s("projects:create"),title:s("projects:create")?void 0:n("projectDetail.template.noCreatePermission"),children:[Ze.isPending?a.jsx(at,{className:"w-4 h-4 animate-spin mr-2"}):a.jsx(Wl,{className:"w-4 h-4 mr-2"}),n("projectDetail.template.saveAsTemplate")]})}),ye&&(ye.queued_prints>0||ye.in_progress_prints>0)&&a.jsx(_t,{children:a.jsxs(Nt,{className:"p-4",children:[a.jsxs("div",{className:"flex items-center justify-between mb-3",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(g3,{className:"w-5 h-5"}),n("projectDetail.queue.title")]}),a.jsx(Xs,{to:`/queue?project=${f}`,className:"text-sm text-bambu-green hover:underline",children:n("projectDetail.queue.viewAll")})]}),a.jsxs("div",{className:"flex items-center gap-4 text-sm",children:[ye.in_progress_prints>0&&a.jsx("span",{className:"text-yellow-400",children:n("projectDetail.queue.printing",{count:ye.in_progress_prints})}),ye.queued_prints>0&&a.jsx("span",{className:"text-bambu-gray",children:n("projectDetail.queue.queued",{count:ye.queued_prints})})]})]})}),a.jsxs("div",{children:[a.jsx("div",{className:"flex items-center justify-between mb-4",children:a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(ma,{className:"w-5 h-5"}),n("projectDetail.prints.title",{count:b?.length||0})]})}),y?a.jsx("div",{className:"flex items-center justify-center py-8",children:a.jsx(at,{className:"w-6 h-6 animate-spin text-bambu-green"})}):a.jsx(HZe,{archives:b||[],t:n})]}),o&&a.jsx(Kae,{t:n,project:{...p,archive_count:ye?.total_archives||0,total_items:ye?.total_items||0,completed_count:ye?.completed_prints||0,failed_count:ye?.failed_prints||0,queue_count:ye?.queued_prints||0,progress_percent:ye?.progress_percent||null,archives:[]},onClose:()=>l(!1),onSave:je=>P.mutate(je),isLoading:P.isPending}),he.isOpen&&a.jsx(kn,{title:he.title,message:he.message,confirmText:n("common.delete"),variant:"danger",onConfirm:he.onConfirm,onCancel:()=>be(je=>({...je,isOpen:!1}))})]})}function Bg(t){return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(1)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`}function qZe(t){if(!t)return"-";const e=Math.floor(t/3600),n=Math.floor(t%3600/60);return e>0?`${e}h ${n}m`:`${n}m`}function WZe({parentId:t,onClose:e,onSave:n,isLoading:r,t:i}){const[s,o]=w.useState(""),l=c=>{c.preventDefault(),n({name:s.trim(),parent_id:t})};return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:i("fileManager.newFolder")})}),a.jsxs("form",{onSubmit:l,className:"p-4 space-y-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:i("fileManager.folderName")}),a.jsx("input",{type:"text",value:s,onChange:c=>o(c.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",placeholder:i("fileManager.folderNamePlaceholder"),autoFocus:!0,required:!0})]}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!s.trim()||r,children:r?a.jsx(at,{className:"w-4 h-4 animate-spin"}):i("common.create")})]})]})]})})}function KZe({type:t,currentName:e,onClose:n,onSave:r,isLoading:i,t:s}){const[o,l]=w.useState(e),c=u=>{u.preventDefault(),o.trim()&&o.trim()!==e&&r(o.trim())};return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:s(t==="file"?"fileManager.renameFile":"fileManager.renameFolder")})}),a.jsxs("form",{onSubmit:c,className:"p-4 space-y-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-white mb-1",children:s("common.name")}),a.jsx("input",{type:"text",value:o,onChange:u=>l(u.target.value),className:"w-full bg-bambu-dark border border-bambu-dark-tertiary rounded px-3 py-2 text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green",autoFocus:!0,required:!0})]}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:n,children:s("common.cancel")}),a.jsx(Me,{type:"submit",disabled:!o.trim()||o.trim()===e||i,children:i?a.jsx(at,{className:"w-4 h-4 animate-spin"}):s("common.rename")})]})]})]})})}function XZe({folders:t,selectedFiles:e,currentFolderId:n,onClose:r,onMove:i,isLoading:s,t:o}){const[l,c]=w.useState(null),u=(h,f=0)=>{const p=[];for(const x of h)p.push({id:x.id,name:x.name,depth:f}),x.children.length>0&&p.push(...u(x.children,f+1));return p},d=[{id:null,name:o("fileManager.rootNoFolder"),depth:0},...u(t)];return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-sm border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"p-4 border-b border-bambu-dark-tertiary",children:a.jsx("h2",{className:"text-lg font-semibold text-white",children:o("fileManager.moveFiles",{count:e.length})})}),a.jsxs("div",{className:"p-4 space-y-4",children:[a.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1",children:d.map(h=>a.jsxs("button",{onClick:()=>c(h.id),disabled:h.id===n,className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${l===h.id?"bg-bambu-green/20 text-bambu-green":h.id===n?"opacity-50 cursor-not-allowed text-bambu-gray":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${12+h.depth*16}px`},children:[a.jsx(Xl,{className:"w-4 h-4"}),h.name,h.id===n&&a.jsxs("span",{className:"text-xs text-bambu-gray ml-auto",children:["(",o("fileManager.current"),")"]})]},h.id??"root"))}),a.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[a.jsx(Me,{type:"button",variant:"secondary",onClick:r,children:o("common.cancel")}),a.jsx(Me,{onClick:()=>i(l),disabled:s,children:s?a.jsx(at,{className:"w-4 h-4 animate-spin"}):o("common.move")})]})]})]})})}function YZe({folder:t,onClose:e,onLink:n,isLoading:r,t:i}){const[s,o]=w.useState("project"),[l,c]=w.useState(t.project_id||t.archive_id||null);w.useState(()=>{t.archive_id&&o("archive")});const{data:u}=tt({queryKey:["projects"],queryFn:()=>se.getProjects()}),{data:d}=tt({queryKey:["archives-for-link"],queryFn:()=>se.getArchives(void 0,void 0,100)}),h=()=>{n(s==="project"?{project_id:l,archive_id:0}:{project_id:0,archive_id:l})},f=()=>{n({project_id:0,archive_id:0})},p=t.project_id||t.archive_id;return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-md border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[a.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[a.jsx(jh,{className:"w-5 h-5 text-bambu-green"}),i("fileManager.linkFolder")]}),a.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:a.jsx(Ft,{className:"w-5 h-5 text-bambu-gray"})})]}),a.jsxs("div",{className:"p-4 space-y-4",children:[a.jsx("p",{className:"text-sm text-bambu-gray",children:i("fileManager.linkFolderDescription",{name:t.name})}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs("button",{onClick:()=>{o("project"),c(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${s==="project"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[a.jsx(oX,{className:"w-4 h-4"}),i("fileManager.project")]}),a.jsxs("button",{onClick:()=>{o("archive"),c(null)},className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors ${s==="archive"?"border-bambu-green bg-bambu-green/10 text-bambu-green":"border-bambu-dark-tertiary text-bambu-gray hover:text-white"}`,children:[a.jsx(Rs,{className:"w-4 h-4"}),i("fileManager.archive")]})]}),a.jsx("div",{className:"max-h-64 overflow-y-auto space-y-1 bg-bambu-dark rounded-lg p-2",children:s==="project"?u&&u.length>0?u.map(x=>a.jsxs("button",{onClick:()=>c(x.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${l===x.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[a.jsx("div",{className:"w-3 h-3 rounded-full flex-shrink-0",style:{backgroundColor:x.color||"#00ae42"}}),a.jsx("span",{className:"truncate",children:x.name})]},x.id)):a.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:i("fileManager.noProjectsFound")}):d&&d.length>0?d.map(x=>a.jsxs("button",{onClick:()=>c(x.id),className:`w-full text-left px-3 py-2 rounded transition-colors flex items-center gap-2 ${l===x.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark-tertiary text-white"}`,children:[a.jsx(ff,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),a.jsx("span",{className:"truncate",children:x.print_name||x.filename})]},x.id)):a.jsx("p",{className:"text-sm text-bambu-gray text-center py-4",children:i("fileManager.noArchivesFound")})})]}),a.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-between",children:[p&&a.jsxs(Me,{variant:"danger",onClick:f,disabled:r,children:[a.jsx(uC,{className:"w-4 h-4 mr-2"}),i("fileManager.unlink")]}),a.jsxs("div",{className:`flex gap-2 ${p?"":"ml-auto"}`,children:[a.jsx(Me,{variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{onClick:h,disabled:!l||r,children:r?a.jsx(at,{className:"w-4 h-4 animate-spin"}):i("fileManager.link")})]})]})]})})}function QZe({folderId:t,onClose:e,onUploadComplete:n,t:r}){const[i,s]=w.useState([]),[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,h]=w.useState(!0),[f,p]=w.useState(!1),[x,v]=w.useState(!0),b=w.useRef(null),y=V=>{V.preventDefault(),l(!0)},S=V=>{V.preventDefault(),l(!1)},N=V=>{V.preventDefault(),l(!1);const q=Array.from(V.dataTransfer.files);M(q)},A=V=>{V.target.files&&M(Array.from(V.target.files))},M=V=>{const q=V.map(I=>({file:I,status:"pending",isZip:I.name.toLowerCase().endsWith(".zip"),is3mf:I.name.toLowerCase().endsWith(".3mf")}));s(I=>[...I,...q])},T=V=>{s(q=>q.filter((I,K)=>K!==V))},k=i.some(V=>V.isZip&&V.status==="pending"),j=i.some(V=>V.file.name.toLowerCase().endsWith(".stl")&&V.status==="pending"),E=i.some(V=>V.is3mf&&V.status==="pending"),P=async()=>{if(i.length!==0){u(!0);for(let V=0;Vq.map((I,K)=>K===V?{...I,status:"uploading"}:I));try{if(i[V].isZip){const q=await se.extractZipFile(i[V].file,t,d,f,x);s(I=>I.map((K,Y)=>Y===V?{...K,status:q.errors.length>0&&q.extracted===0?"error":"success",extractedCount:q.extracted,error:q.errors.length>0?`${q.errors.length} files failed`:void 0}:K))}else await se.uploadLibraryFile(i[V].file,t,x),s(q=>q.map((I,K)=>K===V?{...I,status:"success"}:I))}catch(q){s(I=>I.map((K,Y)=>Y===V?{...K,status:"error",error:q instanceof Error?q.message:"Upload failed"}:K))}}u(!1),n(),e()}},U=i.filter(V=>V.status==="pending").length,L=i.filter(V=>V.status==="success").length,B=i.filter(V=>V.status==="error").length,R=i.length>0&&U===0&&!c;return a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:r("fileManager.uploadFiles")}),a.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark rounded",children:a.jsx(Ft,{className:"w-5 h-5 text-bambu-gray"})})]}),a.jsxs("div",{className:"p-4 space-y-4",children:[a.jsxs("div",{onDragOver:y,onDragLeave:S,onDrop:N,onClick:()=>b.current?.click(),className:`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors ${o?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,children:[a.jsx(ia,{className:`w-10 h-10 mx-auto mb-3 ${o?"text-bambu-green":"text-bambu-gray"}`}),a.jsx("p",{className:"text-white font-medium",children:r(o?"fileManager.dropFilesHere":"fileManager.dragDropFiles")}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:r("fileManager.orClickToBrowse")}),a.jsx("p",{className:"text-xs text-bambu-gray/70 mt-2",children:r("fileManager.allFileTypesSupported")})]}),a.jsx("input",{ref:b,type:"file",multiple:!0,className:"hidden",onChange:A}),k&&a.jsx("div",{className:"p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx(Rs,{className:"w-5 h-5 text-blue-400 mt-0.5 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-blue-300 font-medium",children:r("fileManager.zipFilesDetected")}),a.jsx("p",{className:"text-xs text-blue-300/70 mt-1",children:r("fileManager.zipExtractOptions")}),a.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:d,onChange:V=>h(V.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),a.jsx("span",{className:"text-sm text-white",children:r("fileManager.preserveZipStructure")})]}),a.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:f,onChange:V=>p(V.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),a.jsx("span",{className:"text-sm text-white",children:r("fileManager.createFolderFromZip")})]})]})]})}),E&&a.jsx("div",{className:"p-3 bg-purple-500/10 border border-purple-500/30 rounded-lg",children:a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx(Cr,{className:"w-5 h-5 text-purple-400 mt-0.5 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-purple-300 font-medium",children:r("fileManager.threemfDetected")}),a.jsx("p",{className:"text-xs text-purple-300/70 mt-1",children:r("fileManager.threemfExtractionInfo")})]})]})}),(j||k)&&a.jsx("div",{className:"p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:a.jsxs("div",{className:"flex items-start gap-3",children:[a.jsx(ed,{className:"w-5 h-5 text-bambu-green mt-0.5 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-bambu-green font-medium",children:r("fileManager.stlThumbnailGeneration")}),a.jsx("p",{className:"text-xs text-bambu-green/70 mt-1",children:r(k&&!j?"fileManager.zipMayContainStl":"fileManager.thumbnailsCanBeGenerated")}),a.jsxs("label",{className:"flex items-center gap-2 mt-2 cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:x,onChange:V=>v(V.target.checked),className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),a.jsx("span",{className:"text-sm text-white",children:r("fileManager.generateThumbnailsForStl")})]})]})]})}),i.length>0&&a.jsx("div",{className:"max-h-48 overflow-y-auto space-y-2",children:i.map((V,q)=>a.jsxs("div",{className:"flex items-center gap-3 p-2 bg-bambu-dark rounded-lg",children:[V.isZip?a.jsx(Rs,{className:"w-4 h-4 text-blue-400 flex-shrink-0"}):a.jsx(eC,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm text-white truncate",children:V.file.name}),a.jsxs("p",{className:"text-xs text-bambu-gray",children:[(V.file.size/1024/1024).toFixed(2)," MB",V.isZip&&V.status==="pending"&&a.jsxs("span",{className:"text-blue-400 ml-2",children:["• ",r("fileManager.willBeExtracted")]}),V.extractedCount!==void 0&&a.jsxs("span",{className:"text-green-400 ml-2",children:["• ",r("fileManager.filesExtracted",{count:V.extractedCount})]})]})]}),V.status==="pending"&&a.jsx("button",{onClick:()=>T(q),className:"p-1 hover:bg-bambu-dark-tertiary rounded",children:a.jsx(Ft,{className:"w-4 h-4 text-bambu-gray"})}),V.status==="uploading"&&a.jsx(at,{className:"w-4 h-4 text-bambu-green animate-spin"}),V.status==="success"&&a.jsx(gr,{className:"w-4 h-4 text-green-500"}),V.status==="error"&&a.jsx("span",{title:V.error,children:a.jsx(ha,{className:"w-4 h-4 text-red-500"})})]},q))}),R&&a.jsx("div",{className:"p-3 bg-bambu-dark rounded-lg",children:a.jsxs("p",{className:"text-sm text-white",children:[r("fileManager.uploadComplete",{succeeded:L}),B>0&&a.jsxs("span",{className:"text-red-400",children:[", ",r("fileManager.uploadFailed",{count:B})]})]})})]}),a.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary flex justify-end gap-2",children:[a.jsx(Me,{variant:"secondary",onClick:e,children:r(R?"common.close":"common.cancel")}),!R&&a.jsx(Me,{onClick:P,disabled:U===0||c,children:c?a.jsxs(a.Fragment,{children:[a.jsx(at,{className:"w-4 h-4 mr-2 animate-spin"}),r("fileManager.uploading")]}):a.jsxs(a.Fragment,{children:[a.jsx(ia,{className:"w-4 h-4 mr-2"}),r("common.upload")," ",U>0?`(${U})`:""]})})]})]})})}function Xae({folder:t,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:s,depth:o=0,wrapNames:l=!1,hasPermission:c,t:u}){const[d,h]=w.useState(!0),[f,p]=w.useState(!1),x=t.children.length>0,v=t.project_id||t.archive_id;return a.jsxs("div",{children:[a.jsxs("div",{className:`group flex items-center gap-1 px-2 py-1.5 rounded cursor-pointer transition-colors ${e===t.id?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,style:{paddingLeft:`${8+o*12}px`},onClick:()=>n(t.id),children:[x?a.jsx("button",{onClick:b=>{b.stopPropagation(),h(!d)},className:"p-0.5 hover:bg-bambu-dark-tertiary rounded",children:a.jsx(fa,{className:`w-3.5 h-3.5 transition-transform ${d?"rotate-90":""}`})}):a.jsx("div",{className:"w-4.5"}),a.jsx(Xl,{className:"w-4 h-4 text-bambu-green flex-shrink-0"}),a.jsx("span",{className:`text-sm flex-1 min-w-0 ${l?"break-all":"truncate"}`,title:t.name,children:t.name}),v&&a.jsxs("button",{onClick:b=>{b.stopPropagation(),i(t)},className:"flex-shrink-0 flex items-center gap-1 text-xs px-1.5 py-0.5 rounded bg-blue-500/20 text-blue-400 hover:bg-blue-500/30 transition-colors",title:`${t.project_name?`Project: ${t.project_name}`:`Archive: ${t.archive_name}`} (click to change)`,children:[a.jsx(jh,{className:"w-3 h-3"}),t.project_name?a.jsx(oX,{className:"w-3 h-3"}):a.jsx(Rs,{className:"w-3 h-3"})]}),t.file_count>0&&a.jsx("span",{className:"flex-shrink-0 text-xs text-bambu-gray",children:t.file_count}),!v&&a.jsx("button",{onClick:b=>{b.stopPropagation(),i(t)},className:"flex-shrink-0 p-1 rounded hover:bg-bambu-dark-tertiary",title:u("fileManager.linkToProjectOrArchive"),children:a.jsx(jh,{className:"w-3.5 h-3.5 text-bambu-gray hover:text-bambu-green"})}),a.jsx("div",{className:`flex-shrink-0 flex items-center gap-0.5 transition-opacity ${l?"":"opacity-0 group-hover:opacity-100"}`,onClick:b=>b.stopPropagation(),children:a.jsxs("div",{className:"relative",children:[a.jsx("button",{onClick:()=>p(!f),className:"p-1 rounded hover:bg-bambu-dark-tertiary",children:a.jsx(Ah,{className:"w-3.5 h-3.5 text-bambu-gray"})}),f&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>p(!1)}),a.jsxs("div",{className:"absolute right-0 top-full mt-1 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]",children:[a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${c("library:update_all")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{c("library:update_all")&&(s(t),p(!1))},disabled:!c("library:update_all"),title:c("library:update_all")?void 0:u("fileManager.noPermissionRenameFolder"),children:[a.jsx($i,{className:"w-3.5 h-3.5"}),u("common.rename")]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${c("library:update_all")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{c("library:update_all")&&(i(t),p(!1))},disabled:!c("library:update_all"),title:c("library:update_all")?void 0:u("fileManager.noPermissionLinkFolder"),children:[a.jsx(jh,{className:"w-3.5 h-3.5"}),u(v?"fileManager.changeLink":"fileManager.linkTo")]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${c("library:delete_all")?"text-red-400 hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{c("library:delete_all")&&(r(t.id),p(!1))},disabled:!c("library:delete_all"),title:c("library:delete_all")?void 0:u("fileManager.noPermissionDeleteFolder"),children:[a.jsx(en,{className:"w-3.5 h-3.5"}),u("common.delete")]})]})]})]})})]}),x&&d&&a.jsx("div",{children:t.children.map(b=>a.jsx(Xae,{folder:b,selectedFolderId:e,onSelect:n,onDelete:r,onLink:i,onRename:s,depth:o+1,wrapNames:l,hasPermission:c,t:u},b.id))})]})}function fF(t){const e=t.toLowerCase();return e.endsWith(".gcode")||e.includes(".gcode.")}function ZZe({file:t,isSelected:e,isMobile:n,onSelect:r,onDelete:i,onDownload:s,onAddToQueue:o,onPrint:l,onPreview3d:c,onRename:u,onGenerateThumbnail:d,thumbnailVersion:h,hasPermission:f,canModify:p,authEnabled:x,t:v}){const[b,y]=w.useState(!1);return a.jsxs("div",{className:`group relative bg-bambu-dark-secondary rounded-lg border transition-all cursor-pointer overflow-hidden ${e?"border-bambu-green ring-1 ring-bambu-green":"border-bambu-dark-tertiary hover:border-bambu-green/50"}`,onClick:()=>r(t.id),children:[a.jsxs("div",{className:"aspect-square bg-bambu-dark flex items-center justify-center overflow-hidden",children:[t.thumbnail_path?a.jsx("img",{src:`${se.getLibraryFileThumbnailUrl(t.id)}${h?`?v=${h}`:""}`,alt:t.filename,className:"w-full h-full object-cover"}):a.jsx(ff,{className:"w-12 h-12 text-bambu-gray/30"}),a.jsx("div",{className:`absolute top-2 right-2 text-xs px-1.5 py-0.5 rounded font-medium ${t.file_type==="3mf"?"bg-bambu-green/90 text-white":t.file_type==="gcode"?"bg-blue-500/90 text-white":t.file_type==="stl"?"bg-purple-500/90 text-white":"bg-bambu-gray/90 text-white"}`,children:t.file_type.toUpperCase()})]}),a.jsxs("div",{className:"p-3",children:[a.jsx("h3",{className:"text-sm font-medium text-white truncate",title:t.print_name||t.filename,children:t.print_name||t.filename}),a.jsxs("div",{className:"flex items-center gap-3 mt-1 text-xs text-bambu-gray",children:[a.jsx("span",{children:Bg(t.file_size)}),t.print_time_seconds&&a.jsxs("span",{className:"flex items-center gap-1",children:[a.jsx(Qn,{className:"w-3 h-3"}),qZe(t.print_time_seconds)]})]}),t.sliced_for_model&&a.jsxs("div",{className:"mt-1 text-xs text-bambu-gray flex items-center gap-1",children:[a.jsx(Cr,{className:"w-3 h-3"}),t.sliced_for_model]}),t.print_count>0&&a.jsx("div",{className:"mt-1 text-xs text-bambu-green",children:v("fileManager.printedCount",{count:t.print_count})}),x&&t.created_by_username&&a.jsxs("div",{className:"mt-1 text-xs text-bambu-gray flex items-center gap-1",children:[a.jsx(nd,{className:"w-3 h-3"}),t.created_by_username]})]}),a.jsxs("div",{className:`absolute bottom-2 right-2 transition-opacity ${n?"opacity-100":"opacity-0 group-hover:opacity-100"}`,onClick:S=>S.stopPropagation(),children:[a.jsx("button",{onClick:()=>y(!b),className:"p-1.5 rounded bg-bambu-dark-secondary/90 hover:bg-bambu-dark-tertiary",children:a.jsx(Ah,{className:"w-4 h-4 text-bambu-gray"})}),b&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>y(!1)}),a.jsxs("div",{className:"absolute right-0 bottom-8 z-20 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[140px]",children:[l&&fF(t.filename)&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("printers:control")?"text-bambu-green hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("printers:control")&&(l(t),y(!1))},disabled:!f("printers:control"),title:f("printers:control")?void 0:v("fileManager.noPermissionPrint"),children:[a.jsx(Cr,{className:"w-3.5 h-3.5"}),v("common.print")]}),o&&fF(t.filename)&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("queue:create")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("queue:create")&&(o(t.id),y(!1))},disabled:!f("queue:create"),title:f("queue:create")?void 0:v("fileManager.noPermissionAddToQueue"),children:[a.jsx(Qn,{className:"w-3.5 h-3.5"}),v("fileManager.schedulePrint")]}),c&&(t.file_type==="3mf"||t.file_type==="gcode"||t.file_type==="stl")&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("library:read")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("library:read")&&(c(t),y(!1))},disabled:!f("library:read"),title:f("library:read")?void 0:"You do not have permission to preview files",children:[a.jsx(Qi,{className:"w-3.5 h-3.5"}),"3D Preview"]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${f("library:read")?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{f("library:read")&&(s(t.id),y(!1))},disabled:!f("library:read"),title:f("library:read")?void 0:v("fileManager.noPermissionDownload"),children:[a.jsx(di,{className:"w-3.5 h-3.5"}),v("common.download")]}),u&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${p("library","update",t.created_by_id)?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{p("library","update",t.created_by_id)&&(u(t),y(!1))},disabled:!p("library","update",t.created_by_id),title:p("library","update",t.created_by_id)?void 0:v("fileManager.noPermissionRenameFile"),children:[a.jsx($i,{className:"w-3.5 h-3.5"}),v("common.rename")]}),d&&t.file_type==="stl"&&a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${p("library","update",t.created_by_id)?"text-white hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{p("library","update",t.created_by_id)&&(d(t),y(!1))},disabled:!p("library","update",t.created_by_id),title:p("library","update",t.created_by_id)?void 0:v("fileManager.noPermissionGenerateThumbnail"),children:[a.jsx(ed,{className:"w-3.5 h-3.5"}),v("fileManager.generateThumbnail")]}),a.jsxs("button",{className:`w-full px-3 py-1.5 text-left text-sm flex items-center gap-2 ${p("library","delete",t.created_by_id)?"text-red-400 hover:bg-bambu-dark":"text-bambu-gray cursor-not-allowed"}`,onClick:()=>{p("library","delete",t.created_by_id)&&(i(t.id),y(!1))},disabled:!p("library","delete",t.created_by_id),title:p("library","delete",t.created_by_id)?void 0:v("fileManager.noPermissionDeleteFile"),children:[a.jsx(en,{className:"w-3.5 h-3.5"}),v("common.delete")]})]})]})]}),a.jsx("div",{className:`absolute top-2 left-2 w-5 h-5 rounded border-2 flex items-center justify-center transition-all ${e?"bg-bambu-green border-bambu-green":`border-white/30 bg-black/30 ${n?"opacity-100":"opacity-0 group-hover:opacity-100"}`}`,children:e&&a.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})})]})}function JZe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r,hasAnyPermission:i,canModify:s,authEnabled:o}=Ir(),[l]=NF(),c=l.get("folder"),u=c?parseInt(c,10):null,[d,h]=w.useState(u),[f,p]=w.useState([]),[x,v]=w.useState(!1),[b,y]=w.useState(!1),[S,N]=w.useState(!1),[A,M]=w.useState(null),[T,k]=w.useState(null),[j,E]=w.useState(null),[P,U]=w.useState(null),[L,B]=w.useState(null),[R,V]=w.useState(null),[q,I]=w.useState({}),[K,Y]=w.useState(null),[le,H]=w.useState(()=>localStorage.getItem("library-view-mode")||"grid"),[X,te]=w.useState(()=>localStorage.getItem("library-wrap-folders")==="true"),[J,Se]=w.useState(()=>{const Oe=localStorage.getItem("library-sidebar-width");return Oe?parseInt(Oe,10):256}),[fe,me]=w.useState(!1),De=w.useRef(null);w.useEffect(()=>{if(!fe)return;document.body.style.userSelect="none",document.body.style.cursor="col-resize";const Oe=vt=>{if(!De.current)return;const Bt=De.current.parentElement?.getBoundingClientRect();if(!Bt)return;const Tn=vt.clientX-Bt.left,fr=Math.min(500,Math.max(200,Tn));Se(fr)},xt=()=>{me(!1),document.body.style.userSelect="",document.body.style.cursor="",localStorage.setItem("library-sidebar-width",String(J))};return document.addEventListener("mousemove",Oe),document.addEventListener("mouseup",xt),()=>{document.removeEventListener("mousemove",Oe),document.removeEventListener("mouseup",xt),document.body.style.userSelect="",document.body.style.cursor=""}},[fe,J]);const[de,ce]=w.useState(""),[Ne,ie]=w.useState("all"),[Le,$]=w.useState(""),[W,ue]=w.useState(()=>localStorage.getItem("library-sort-field")||"name"),[Z,oe]=w.useState(()=>localStorage.getItem("library-sort-direction")||"asc"),Q=fne();w.useEffect(()=>{const Oe=l.get("folder");if(Oe){const xt=parseInt(Oe,10);h(xt)}},[l]);const{data:he}=tt({queryKey:["settings"],queryFn:()=>se.getSettings()}),{data:be,isLoading:ee}=tt({queryKey:["library-folders"],queryFn:()=>se.getLibraryFolders()}),{data:z,isLoading:D}=tt({queryKey:["library-files",d],queryFn:()=>se.getLibraryFiles(d,d===null)}),{data:G}=tt({queryKey:["library-stats"],queryFn:()=>se.getLibraryStats()}),{data:pe}=tt({queryKey:["users"],queryFn:()=>se.getUsers()}),ve=w.useMemo(()=>{if(!z)return[];const Oe=new Set(z.map(xt=>xt.file_type));return Array.from(Oe).sort()},[z]),xe=w.useMemo(()=>{if(!z)return[];let Oe=[...z];if(de.trim()){const xt=de.toLowerCase();Oe=Oe.filter(vt=>vt.filename.toLowerCase().includes(xt)||vt.print_name&&vt.print_name.toLowerCase().includes(xt))}if(Ne!=="all"&&(Oe=Oe.filter(xt=>xt.file_type===Ne)),Le.trim()){const xt=Le.toLowerCase();Oe=Oe.filter(vt=>vt.created_by_username&&vt.created_by_username.toLowerCase().includes(xt))}return Oe.sort((xt,vt)=>{let Bt=0;switch(W){case"name":Bt=(xt.print_name||xt.filename).localeCompare(vt.print_name||vt.filename);break;case"date":Bt=new Date(xt.created_at).getTime()-new Date(vt.created_at).getTime();break;case"size":Bt=xt.file_size-vt.file_size;break;case"type":Bt=xt.file_type.localeCompare(vt.file_type);break;case"prints":Bt=xt.print_count-vt.print_count;break}return Z==="asc"?Bt:-Bt}),Oe},[z,de,Ne,Le,W,Z]),ze=w.useMemo(()=>{if(!G||!he)return!1;const Oe=(he.library_disk_warning_gb||5)*1024*1024*1024;return G.disk_free_bytesse.createLibraryFolder(Oe),onSuccess:()=>{e.invalidateQueries({queryKey:["library-folders"]}),v(!1),n(t("fileManager.toast.folderCreated"),"success")},onError:Oe=>n(Oe.message,"error")}),He=lt({mutationFn:Oe=>se.deleteLibraryFolder(Oe),onSuccess:()=>{e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-stats"]}),d===T?.id&&h(null),k(null),n(t("fileManager.toast.folderDeleted"),"success")},onError:Oe=>{k(null),n(Oe.message,"error")}}),Ze=lt({mutationFn:Oe=>se.deleteLibraryFile(Oe),onSuccess:()=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-stats"]}),p(Oe=>Oe.filter(xt=>xt!==T?.id)),k(null),n(t("fileManager.toast.fileDeleted"),"success")},onError:Oe=>{k(null),n(Oe.message,"error")}}),we=lt({mutationFn:Oe=>se.bulkDeleteLibrary(Oe,[]),onSuccess:(Oe,xt)=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-stats"]}),n(t("fileManager.toast.filesDeleted",{count:xt.length}),"success"),p([]),k(null)},onError:Oe=>{k(null),n(Oe.message,"error")}}),ye=lt({mutationFn:({fileIds:Oe,folderId:xt})=>se.moveLibraryFiles(Oe,xt),onSuccess:()=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),p([]),y(!1),n(t("fileManager.toast.filesMoved"),"success")},onError:Oe=>n(Oe.message,"error")}),ke=lt({mutationFn:({id:Oe,data:xt})=>se.updateLibraryFolder(Oe,xt),onSuccess:(Oe,xt)=>{e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["project-folders"]}),e.invalidateQueries({queryKey:["archive-folders"]}),M(null);const vt=xt.data.project_id===0&&xt.data.archive_id===0;n(t(vt?"fileManager.toast.folderUnlinked":"fileManager.toast.folderLinked"),"success")},onError:Oe=>n(Oe.message,"error")}),Ue=lt({mutationFn:({id:Oe,filename:xt})=>se.updateLibraryFile(Oe,{filename:xt}),onSuccess:()=>{e.invalidateQueries({queryKey:["library-files"]}),V(null),n(t("fileManager.toast.fileRenamed"),"success")},onError:Oe=>{V(null),n(Oe.message,"error")}}),je=lt({mutationFn:({id:Oe,name:xt})=>se.updateLibraryFolder(Oe,{name:xt}),onSuccess:()=>{e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-files"]}),V(null),n(t("fileManager.toast.folderRenamed"),"success")},onError:Oe=>{V(null),n(Oe.message,"error")}}),Ve=lt({mutationFn:()=>se.batchGenerateStlThumbnails({all_missing:!0}),onSuccess:Oe=>{if(e.invalidateQueries({queryKey:["library-files"]}),Oe.succeeded>0){const xt=Date.now(),vt={};Oe.results.forEach(Bt=>{Bt.success&&(vt[Bt.file_id]=xt)}),I(Bt=>({...Bt,...vt}))}Oe.succeeded>0&&Oe.failed===0?n(t("fileManager.toast.thumbnailsGenerated",{count:Oe.succeeded}),"success"):Oe.succeeded>0&&Oe.failed>0?n(t("fileManager.toast.thumbnailsGeneratedPartial",{succeeded:Oe.succeeded,failed:Oe.failed}),"success"):Oe.processed===0?n(t("fileManager.toast.noStlMissingThumbnails"),"info"):n(t("fileManager.toast.failedToGenerateThumbnails",{error:Oe.results[0]?.error||"Unknown error"}),"error")},onError:Oe=>n(Oe.message,"error")}),Ae=lt({mutationFn:Oe=>se.batchGenerateStlThumbnails({file_ids:[Oe]}),onSuccess:Oe=>{if(e.invalidateQueries({queryKey:["library-files"]}),Oe.succeeded>0){const xt=Oe.results[0]?.file_id;xt&&I(vt=>({...vt,[xt]:Date.now()})),n(t("fileManager.toast.thumbnailGenerated"),"success")}else n(t("fileManager.toast.failedToGenerateThumbnail",{error:Oe.results[0]?.error||"Unknown error"}),"error")},onError:Oe=>n(Oe.message,"error")}),Je=w.useCallback(Oe=>{const xt=Oe.toLowerCase();return xt.endsWith(".gcode")||xt.includes(".gcode.")},[]),Pe=w.useMemo(()=>z?z.filter(Oe=>f.includes(Oe.id)&&Je(Oe.filename)):[],[z,f,Je]),Ye=w.useCallback(Oe=>{p(xt=>xt.includes(Oe)?xt.filter(vt=>vt!==Oe):[...xt,Oe])},[]),Re=w.useCallback(()=>{xe.length>0&&p(xe.map(Oe=>Oe.id))},[xe]),Fe=w.useCallback(()=>{p([])},[]),We=()=>{e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["library-folders"]}),e.invalidateQueries({queryKey:["library-stats"]})},pt=Oe=>{se.downloadLibraryFile(Oe).catch(xt=>{console.error("Library file download failed:",xt)})},At=()=>{T&&(T.type==="file"?Ze.mutate(T.id):T.type==="folder"?He.mutate(T.id):T.type==="bulk"&&we.mutate(f))},Rt=He.isPending||Ze.isPending||we.isPending,Wt=Oe=>{H(Oe),localStorage.setItem("library-view-mode",Oe)},Tt=ee||D;return a.jsxs("div",{className:"p-4 md:p-8 min-h-[calc(100vh-64px)] lg:h-[calc(100vh-64px)] flex flex-col",children:[a.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6",children:[a.jsxs("div",{children:[a.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[a.jsx("div",{className:"p-2.5 bg-bambu-green/10 rounded-xl",children:a.jsx(Xl,{className:"w-6 h-6 text-bambu-green"})}),t("fileManager.title")]}),a.jsx("p",{className:"text-sm text-bambu-gray mt-2 ml-14",children:t("fileManager.subtitle")})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"flex items-center bg-bambu-dark rounded-lg p-1",children:[a.jsx("button",{onClick:()=>Wt("grid"),className:`p-1.5 rounded transition-colors ${le==="grid"?"bg-bambu-dark-secondary text-white":"text-bambu-gray hover:text-white"}`,title:t("fileManager.gridView"),children:a.jsx(xv,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>Wt("list"),className:`p-1.5 rounded transition-colors ${le==="list"?"bg-bambu-dark-secondary text-white":"text-bambu-gray hover:text-white"}`,title:t("fileManager.listView"),children:a.jsx(rC,{className:"w-4 h-4"})})]}),a.jsxs(Me,{variant:"secondary",onClick:()=>Ve.mutate(),disabled:Ve.isPending||!i("library:update_own","library:update_all"),title:i("library:update_own","library:update_all")?t("fileManager.generateThumbnailsForMissing"):t("fileManager.noPermissionGenerateThumbnail"),children:[Ve.isPending?a.jsx(at,{className:"w-4 h-4 mr-2 animate-spin"}):a.jsx(ed,{className:"w-4 h-4 mr-2"}),t("fileManager.generateThumbnails")]}),a.jsxs(Me,{variant:"secondary",onClick:()=>v(!0),disabled:!r("library:upload"),title:r("library:upload")?void 0:t("fileManager.noPermissionCreateFolder"),children:[a.jsx(Jhe,{className:"w-4 h-4 mr-2"}),t("fileManager.newFolder")]}),a.jsxs(Me,{onClick:()=>N(!0),disabled:!r("library:upload"),title:r("library:upload")?void 0:t("fileManager.noPermissionUpload"),children:[a.jsx(ia,{className:"w-4 h-4 mr-2"}),t("common.upload")]})]})]}),ze&&G&&he&&a.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-amber-500/10 border border-amber-500/30 rounded-lg",children:[a.jsx(Cn,{className:"w-5 h-5 text-amber-500 flex-shrink-0"}),a.jsxs("div",{className:"flex-1",children:[a.jsx("p",{className:"text-sm text-amber-500 font-medium",children:t("fileManager.lowDiskSpaceWarning")}),a.jsx("p",{className:"text-xs text-amber-500/80",children:t("fileManager.lowDiskSpaceDetails",{free:Bg(G.disk_free_bytes),total:Bg(G.disk_total_bytes),threshold:he.library_disk_warning_gb})})]})]}),G&&a.jsxs("div",{className:"flex flex-wrap items-center gap-3 sm:gap-6 mb-6 p-3 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(eC,{className:"w-4 h-4 text-bambu-green"}),a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.files"),":"]}),a.jsx("span",{className:"text-white font-medium",children:G.total_files})]}),a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(Xl,{className:"w-4 h-4 text-blue-400"}),a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.folders"),":"]}),a.jsx("span",{className:"text-white font-medium",children:G.total_folders})]}),a.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[a.jsx(qb,{className:"w-4 h-4 text-amber-400"}),a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.size"),":"]}),a.jsx("span",{className:"text-white font-medium",children:Bg(G.total_size_bytes)})]}),a.jsxs("div",{className:"flex items-center gap-2 text-sm sm:ml-auto",children:[a.jsxs("span",{className:"text-bambu-gray",children:[t("fileManager.free"),":"]}),a.jsx("span",{className:`font-medium ${ze?"text-amber-500":"text-white"}`,children:Bg(G.disk_free_bytes)})]})]}),a.jsxs("div",{className:"flex-1 flex flex-col lg:flex-row gap-4 lg:gap-6 min-h-0",children:[a.jsx("div",{className:"lg:hidden",children:a.jsxs("select",{value:d??"",onChange:Oe=>h(Oe.target.value?parseInt(Oe.target.value,10):null),className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg px-3 py-2.5 text-white focus:outline-none focus:border-bambu-green",children:[a.jsxs("option",{value:"",children:["📁 ",t("fileManager.allFiles")]}),be&&(()=>{const Oe=(xt,vt=0)=>{const Bt=[];for(const Tn of xt)Bt.push({id:Tn.id,name:Tn.name,fileCount:Tn.file_count,depth:vt}),Tn.children.length>0&&Bt.push(...Oe(Tn.children,vt+1));return Bt};return Oe(be).map(xt=>a.jsxs("option",{value:xt.id,children:["│ ".repeat(xt.depth),"📂 ",xt.name," ",xt.fileCount>0?`(${xt.fileCount})`:""]},xt.id))})()]})}),a.jsxs("div",{ref:De,className:"hidden lg:flex flex-shrink-0 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary overflow-hidden flex-col relative",style:{width:`${J}px`},children:[a.jsx("div",{className:`absolute right-0 top-0 bottom-0 w-1.5 cursor-col-resize z-10 group/resize flex items-center justify-center transition-colors ${fe?"bg-bambu-green":"hover:bg-bambu-green/50"}`,onMouseDown:Oe=>{Oe.preventDefault(),me(!0)},onDoubleClick:()=>{Se(256),localStorage.setItem("library-sidebar-width","256")},title:t("fileManager.dragToResizeTooltip"),children:a.jsxs("div",{className:`flex flex-col gap-1 opacity-0 group-hover/resize:opacity-100 transition-opacity ${fe?"opacity-100":""}`,children:[a.jsx("div",{className:"w-0.5 h-0.5 rounded-full bg-white/70"}),a.jsx("div",{className:"w-0.5 h-0.5 rounded-full bg-white/70"}),a.jsx("div",{className:"w-0.5 h-0.5 rounded-full bg-white/70"})]})}),a.jsxs("div",{className:"p-3 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[a.jsx("h2",{className:"text-sm font-medium text-white",children:t("fileManager.folders")}),a.jsx("button",{onClick:()=>{const Oe=!X;te(Oe),localStorage.setItem("library-wrap-folders",String(Oe))},className:`text-xs px-1.5 py-0.5 rounded transition-colors ${X?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:text-white hover:bg-bambu-dark"}`,title:t(X?"fileManager.disableTextWrapping":"fileManager.enableTextWrapping"),children:t("fileManager.wrap")})]}),a.jsxs("div",{className:"flex-1 overflow-y-auto p-2",children:[a.jsxs("div",{className:`flex items-center gap-2 px-2 py-1.5 rounded cursor-pointer transition-colors ${d===null?"bg-bambu-green/20 text-bambu-green":"hover:bg-bambu-dark text-white"}`,onClick:()=>h(null),children:[a.jsx(ff,{className:"w-4 h-4"}),a.jsx("span",{className:"text-sm",children:t("fileManager.allFiles")})]}),be?.map(Oe=>a.jsx(Xae,{folder:Oe,selectedFolderId:d,onSelect:h,onDelete:xt=>k({type:"folder",id:xt}),onLink:M,onRename:xt=>V({type:"folder",id:xt.id,name:xt.name}),wrapNames:X,hasPermission:r,t},Oe.id))]})]}),a.jsxs("div",{className:"flex-1 flex flex-col min-w-0 min-h-0",children:[z&&z.length>0&&a.jsxs("div",{className:"flex flex-wrap items-center gap-2 sm:gap-3 mb-4 p-2 sm:p-3 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary sticky top-0 z-10 lg:static",children:[a.jsxs("div",{className:"relative w-full sm:w-auto sm:flex-1 sm:max-w-xs",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",placeholder:t("fileManager.searchFiles"),value:de,onChange:Oe=>ce(Oe.target.value),className:"w-full pl-9 pr-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green"})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(tC,{className:"w-4 h-4 text-bambu-gray hidden sm:block"}),a.jsxs("select",{value:Ne,onChange:Oe=>ie(Oe.target.value),className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[a.jsx("option",{value:"all",children:t("fileManager.allTypes")}),ve.map(Oe=>a.jsx("option",{value:Oe,children:Oe.toUpperCase()},Oe))]})]}),o&&a.jsxs("div",{className:"relative",children:[a.jsx("input",{type:"text",placeholder:t("fileManager.filterByUser",{defaultValue:"Filter by user"}),value:Le,onChange:Oe=>$(Oe.target.value),list:"usernames-list",className:`w-32 sm:w-40 px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-sm text-white placeholder-bambu-gray focus:outline-none focus:border-bambu-green ${Le?"pr-7":""}`,style:Le?{WebkitAppearance:"none",MozAppearance:"textfield"}:void 0}),Le&&a.jsx("button",{onClick:()=>$(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white z-10",children:a.jsx(Ft,{className:"w-3 h-3"})}),a.jsx("datalist",{id:"usernames-list",children:pe?.map(Oe=>a.jsx("option",{value:Oe.username},Oe.id))})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("select",{value:W,onChange:Oe=>{const xt=Oe.target.value;ue(xt),localStorage.setItem("library-sort-field",xt)},className:"bg-bambu-dark border border-bambu-dark-tertiary rounded px-2 py-1.5 text-sm text-white focus:outline-none focus:border-bambu-green",children:[a.jsx("option",{value:"name",children:t("common.name")}),a.jsx("option",{value:"date",children:t("common.date")}),a.jsx("option",{value:"size",children:t("fileManager.size")}),a.jsx("option",{value:"type",children:t("common.type")}),a.jsx("option",{value:"prints",children:t("fileManager.prints")})]}),a.jsx("button",{onClick:()=>oe(Oe=>{const xt=Oe==="asc"?"desc":"asc";return localStorage.setItem("library-sort-direction",xt),xt}),className:"p-1.5 rounded bg-bambu-dark border border-bambu-dark-tertiary hover:border-bambu-green transition-colors",title:t(Z==="asc"?"fileManager.ascending":"fileManager.descending"),children:Z==="asc"?a.jsx(yde,{className:"w-4 h-4 text-white"}):a.jsx(dde,{className:"w-4 h-4 text-white"})})]}),(de||Ne!=="all"||Le)&&a.jsx("span",{className:"text-sm text-bambu-gray hidden sm:inline",children:t("fileManager.resultsCount",{showing:xe.length,total:z.length})})]}),xe.length>0&&a.jsxs("div",{className:"flex flex-wrap items-center gap-2 mb-4 p-2 bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary sticky top-[52px] z-10 lg:static",children:[f.length===xe.length&&f.length>0?a.jsxs(Me,{variant:"secondary",size:"sm",onClick:Fe,children:[a.jsx(gl,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("fileManager.deselectAll")})]}):a.jsxs(Me,{variant:"secondary",size:"sm",onClick:Re,children:[a.jsx(ao,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("fileManager.selectAll")})]}),f.length>0&&a.jsxs(a.Fragment,{children:[a.jsx("span",{className:"text-sm text-bambu-gray ml-2",children:t("fileManager.selected",{count:f.length})}),a.jsx("div",{className:"hidden sm:block flex-1"}),a.jsxs("div",{className:"w-full sm:w-auto flex flex-wrap items-center gap-2 mt-2 sm:mt-0",children:[Pe.length===1&&a.jsxs(Me,{variant:"primary",size:"sm",onClick:()=>U(Pe[0]),disabled:!r("printers:control"),title:r("printers:control")?void 0:t("fileManager.noPermissionPrint"),children:[a.jsx(jo,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.print")})]}),Pe.length===1&&a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>B(Pe[0]),disabled:!r("queue:create"),title:r("queue:create")?void 0:t("fileManager.noPermissionAddToQueue"),children:[a.jsx(Qn,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("fileManager.schedulePrint")})]}),a.jsxs(Me,{variant:"secondary",size:"sm",onClick:()=>y(!0),disabled:!i("library:update_own","library:update_all"),title:i("library:update_own","library:update_all")?void 0:t("fileManager.noPermissionMoveFiles"),children:[a.jsx(sfe,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.move")})]}),a.jsxs(Me,{variant:"danger",size:"sm",onClick:()=>{f.length===1?k({type:"file",id:f[0]}):k({type:"bulk",id:0,count:f.length})},disabled:!i("library:delete_own","library:delete_all"),title:i("library:delete_own","library:delete_all")?void 0:t("fileManager.noPermissionDeleteFiles"),children:[a.jsx(en,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.delete")})]}),a.jsxs(Me,{variant:"secondary",size:"sm",onClick:Fe,children:[a.jsx(Ft,{className:"w-4 h-4 sm:mr-1"}),a.jsx("span",{className:"hidden sm:inline",children:t("common.clear")})]})]})]})]}),Tt?a.jsx("div",{className:"flex-1 flex items-center justify-center",children:a.jsxs("div",{className:"flex flex-col items-center gap-3",children:[a.jsx(at,{className:"w-8 h-8 animate-spin text-bambu-green"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:t("fileManager.loadingFiles")})]})}):z?.length===0?a.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[a.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:a.jsx(ff,{className:"w-12 h-12 text-bambu-gray/50"})}),a.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:t(d!==null?"fileManager.folderIsEmpty":"fileManager.noFilesYet")}),a.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:t(d!==null?"fileManager.folderEmptyDescription":"fileManager.noFilesDescription")}),a.jsxs(Me,{onClick:()=>N(!0),disabled:!r("library:upload"),title:r("library:upload")?void 0:t("fileManager.noPermissionUpload"),children:[a.jsx(ar,{className:"w-4 h-4 mr-2"}),t("fileManager.uploadFiles")]})]}):xe.length===0?a.jsxs("div",{className:"flex-1 flex flex-col items-center justify-center",children:[a.jsx("div",{className:"p-4 bg-bambu-dark rounded-2xl mb-4",children:a.jsx(Or,{className:"w-12 h-12 text-bambu-gray/50"})}),a.jsx("h3",{className:"text-lg font-medium text-white mb-2",children:t("fileManager.noMatchingFiles")}),a.jsx("p",{className:"text-bambu-gray text-center max-w-md mb-6",children:t("fileManager.noMatchingFilesDescription")}),a.jsx(Me,{variant:"secondary",onClick:()=>{ce(""),ie("all")},children:t("fileManager.clearFilters")})]}):le==="grid"?a.jsx("div",{className:"flex-1 lg:overflow-y-auto",children:a.jsx("div",{className:"grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6 gap-4",children:xe.map(Oe=>a.jsx(ZZe,{file:Oe,isSelected:f.includes(Oe.id),isMobile:Q,t,onSelect:Ye,onDelete:xt=>k({type:"file",id:xt}),onDownload:pt,onAddToQueue:xt=>{const vt=z?.find(Bt=>Bt.id===xt);vt&&B(vt)},onPrint:E,onPreview3d:Y,onRename:xt=>V({type:"file",id:xt.id,name:xt.filename}),onGenerateThumbnail:xt=>Ae.mutate(xt.id),thumbnailVersion:q[Oe.id],hasPermission:r,canModify:s,authEnabled:o},Oe.id))})}):a.jsx("div",{className:"flex-1 lg:overflow-y-auto",children:a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[a.jsxs("div",{className:`hidden sm:grid ${o?"grid-cols-[auto_1fr_120px_100px_100px_100px_80px]":"grid-cols-[auto_1fr_100px_100px_100px_80px]"} gap-4 px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary text-xs text-bambu-gray font-medium`,children:[a.jsx("div",{className:"w-6"}),a.jsx("div",{children:t("common.name")}),o&&a.jsx("div",{children:t("fileManager.uploadedBy",{defaultValue:"Uploaded By"})}),a.jsx("div",{children:t("common.type")}),a.jsx("div",{children:t("fileManager.size")}),a.jsx("div",{children:t("fileManager.prints")}),a.jsx("div",{})]}),xe.map(Oe=>a.jsxs("div",{className:`grid ${o?"grid-cols-[auto_1fr_120px_100px_100px_100px_80px]":"grid-cols-[auto_1fr_100px_100px_100px_80px]"} gap-4 px-4 py-3 items-center border-b border-bambu-dark-tertiary last:border-b-0 cursor-pointer hover:bg-bambu-dark/50 transition-colors ${f.includes(Oe.id)?"bg-bambu-green/10":""}`,onClick:()=>Ye(Oe.id),children:[a.jsx("div",{className:`w-5 h-5 rounded border-2 flex items-center justify-center ${f.includes(Oe.id)?"bg-bambu-green border-bambu-green":"border-bambu-gray/50"}`,children:f.includes(Oe.id)&&a.jsx("div",{className:"w-2 h-2 bg-white rounded-sm"})}),a.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[a.jsxs("div",{className:"relative group/thumb",children:[a.jsx("div",{className:"w-10 h-10 rounded bg-bambu-dark flex-shrink-0 overflow-hidden",children:Oe.thumbnail_path?a.jsx("img",{src:`${se.getLibraryFileThumbnailUrl(Oe.id)}${q[Oe.id]?`?v=${q[Oe.id]}`:""}`,alt:"",className:"w-full h-full object-cover"}):a.jsx("div",{className:"w-full h-full flex items-center justify-center",children:a.jsx(ff,{className:"w-5 h-5 text-bambu-gray/50"})})}),Oe.thumbnail_path&&a.jsx("div",{className:"absolute left-0 top-full mt-2 z-50 hidden group-hover/thumb:block",children:a.jsx("div",{className:"w-48 h-48 rounded-lg bg-bambu-dark-secondary border border-bambu-dark-tertiary shadow-xl overflow-hidden",children:a.jsx("img",{src:`${se.getLibraryFileThumbnailUrl(Oe.id)}${q[Oe.id]?`?v=${q[Oe.id]}`:""}`,alt:Oe.filename,className:"w-full h-full object-contain"})})})]}),a.jsx("div",{className:"min-w-0",children:a.jsx("div",{className:"text-sm text-white truncate",children:Oe.print_name||Oe.filename})})]}),o&&a.jsx("div",{className:"text-sm text-bambu-gray flex items-center gap-1",children:Oe.created_by_username?a.jsxs(a.Fragment,{children:[a.jsx(nd,{className:"w-3 h-3"}),a.jsx("span",{className:"truncate",children:Oe.created_by_username})]}):"-"}),a.jsx("div",{children:a.jsx("span",{className:`text-xs px-1.5 py-0.5 rounded font-medium ${Oe.file_type==="3mf"?"bg-bambu-green/20 text-bambu-green":Oe.file_type==="gcode"?"bg-blue-500/20 text-blue-400":Oe.file_type==="stl"?"bg-purple-500/20 text-purple-400":"bg-bambu-gray/20 text-bambu-gray"}`,children:Oe.file_type.toUpperCase()})}),a.jsx("div",{className:"text-sm text-bambu-gray",children:Bg(Oe.file_size)}),a.jsx("div",{className:"text-sm text-bambu-gray",children:Oe.print_count>0?`${Oe.print_count}x`:"-"}),a.jsxs("div",{className:"flex items-center gap-1",onClick:xt=>xt.stopPropagation(),children:[fF(Oe.filename)&&a.jsxs(a.Fragment,{children:[a.jsx("button",{onClick:()=>r("printers:control")&&E(Oe),className:`p-1.5 rounded transition-colors ${r("printers:control")?"hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("printers:control")?t("common.print"):t("fileManager.noPermissionPrint"),disabled:!r("printers:control"),children:a.jsx(Cr,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>{r("queue:create")&&B(Oe)},className:`p-1.5 rounded transition-colors ${r("queue:create")?"hover:bg-bambu-dark text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("queue:create")?t("fileManager.schedulePrint"):t("fileManager.noPermissionAddToQueue"),disabled:!r("queue:create"),children:a.jsx(Qn,{className:"w-4 h-4"})})]}),(Oe.file_type==="3mf"||Oe.file_type==="gcode"||Oe.file_type==="stl")&&a.jsx("button",{onClick:()=>r("library:read")&&Y(Oe),className:`p-1.5 rounded transition-colors ${r("library:read")?"hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("library:read")?"3D Preview":"You do not have permission to preview files",disabled:!r("library:read"),children:a.jsx(Qi,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>r("library:read")&&pt(Oe.id),className:`p-1.5 rounded transition-colors ${r("library:read")?"hover:bg-bambu-dark text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:r("library:read")?t("common.download"):t("fileManager.noPermissionDownload"),disabled:!r("library:read"),children:a.jsx(di,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>s("library","update",Oe.created_by_id)&&V({type:"file",id:Oe.id,name:Oe.filename}),className:`p-1.5 rounded transition-colors ${s("library","update",Oe.created_by_id)?"hover:bg-bambu-dark text-bambu-gray hover:text-white":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("library","update",Oe.created_by_id)?t("common.rename"):t("fileManager.noPermissionRenameFile"),disabled:!s("library","update",Oe.created_by_id),children:a.jsx($i,{className:"w-4 h-4"})}),Oe.file_type==="stl"&&a.jsx("button",{onClick:()=>s("library","update",Oe.created_by_id)&&Ae.mutate(Oe.id),className:`p-1.5 rounded transition-colors ${s("library","update",Oe.created_by_id)?"hover:bg-bambu-dark text-bambu-gray hover:text-bambu-green":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("library","update",Oe.created_by_id)?t("fileManager.generateThumbnail"):t("fileManager.noPermissionGenerateThumbnail"),disabled:Ae.isPending||!s("library","update",Oe.created_by_id),children:a.jsx(ed,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>s("library","delete",Oe.created_by_id)&&k({type:"file",id:Oe.id}),className:`p-1.5 rounded transition-colors ${s("library","delete",Oe.created_by_id)?"hover:bg-bambu-dark text-bambu-gray hover:text-red-400":"text-bambu-gray/50 cursor-not-allowed"}`,title:s("library","delete",Oe.created_by_id)?t("common.delete"):t("fileManager.noPermissionDeleteFile"),disabled:!s("library","delete",Oe.created_by_id),children:a.jsx(en,{className:"w-4 h-4"})})]})]},Oe.id))]})})]})]}),x&&a.jsx(WZe,{parentId:d,onClose:()=>v(!1),onSave:Oe=>_e.mutate(Oe),isLoading:_e.isPending,t}),b&&be&&a.jsx(XZe,{folders:be,selectedFiles:f,currentFolderId:d,onClose:()=>y(!1),onMove:Oe=>ye.mutate({fileIds:f,folderId:Oe}),isLoading:ye.isPending,t}),S&&a.jsx(QZe,{folderId:d,onClose:()=>N(!1),onUploadComplete:We,t}),A&&a.jsx(YZe,{folder:A,onClose:()=>M(null),onLink:Oe=>ke.mutate({id:A.id,data:Oe}),isLoading:ke.isPending,t}),T&&a.jsx(kn,{title:T.type==="folder"?t("fileManager.deleteFolder"):T.type==="bulk"?t("fileManager.deleteFilesCount",{count:T.count}):t("fileManager.deleteFile"),message:T.type==="folder"?t("fileManager.deleteFolderConfirm"):T.type==="bulk"?t("fileManager.deleteFilesConfirm",{count:T.count}):t("fileManager.deleteFileConfirm"),confirmText:t("common.delete"),variant:"danger",isLoading:Rt,loadingText:t("fileManager.deleting"),onConfirm:At,onCancel:()=>k(null)}),j&&a.jsx(Yu,{mode:"reprint",libraryFileId:j.id,archiveName:j.print_name||j.filename,onClose:()=>E(null),onSuccess:()=>{E(null),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["archives"]})}}),P&&a.jsx(Yu,{mode:"reprint",libraryFileId:P.id,archiveName:P.print_name||P.filename,onClose:()=>U(null),onSuccess:()=>{U(null),p([]),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["archives"]})}}),L&&a.jsx(Yu,{mode:"add-to-queue",libraryFileId:L.id,archiveName:L.print_name||L.filename,onClose:()=>B(null),onSuccess:()=>{B(null),p([]),e.invalidateQueries({queryKey:["library-files"]}),e.invalidateQueries({queryKey:["queue"]}),e.invalidateQueries({queryKey:["archives"]})}}),K&&a.jsx(S4,{libraryFileId:K.id,title:K.print_name||K.filename,fileType:K.file_type,onClose:()=>Y(null)}),R&&a.jsx(KZe,{type:R.type,currentName:R.name,onClose:()=>V(null),onSave:Oe=>{R.type==="file"?Ue.mutate({id:R.id,filename:Oe}):je.mutate({id:R.id,name:Oe})},isLoading:Ue.isPending||je.isPending,t})]})}const qj=5,eJe=2e3,tJe=3e4,nJe=5e3;function rJe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),{hasPermission:r}=Ir(),{printerId:i}=Yk(),s=parseInt(i||"0",10),[o,l]=w.useState("stream"),[c,u]=w.useState(!1),[d,h]=w.useState(!1),[f,p]=w.useState(!0),[x,v]=w.useState(Date.now()),[b,y]=w.useState(!1),[S,N]=w.useState(!1),[A,M]=w.useState(0),[T,k]=w.useState(!1),[j,E]=w.useState(0),[P,U]=w.useState(1),[L,B]=w.useState({x:0,y:0}),[R,V]=w.useState(!1),[q,I]=w.useState({x:0,y:0}),[K,Y]=w.useState(null),[le,H]=w.useState(null),X=w.useRef(null),te=w.useRef(null),J=w.useRef(null),Se=w.useRef(null),fe=w.useRef(null),{data:me}=tt({queryKey:["printer",s],queryFn:()=>se.getPrinter(s),enabled:s>0}),{data:De}=tt({queryKey:["printerStatus",s],queryFn:()=>se.getPrinterStatus(s),refetchInterval:3e4,enabled:s>0}),de=lt({mutationFn:ye=>se.setChamberLight(s,ye),onMutate:async ye=>{await e.cancelQueries({queryKey:["printerStatus",s]});const ke=e.getQueryData(["printerStatus",s]);return e.setQueryData(["printerStatus",s],Ue=>({...Ue,chamber_light:ye})),{previousStatus:ke}},onSuccess:(ye,ke)=>{n(`Chamber light ${ke?"on":"off"}`)},onError:(ye,ke,Ue)=>{Ue?.previousStatus&&e.setQueryData(["printerStatus",s],Ue.previousStatus),n(ye.message||t("printers.toast.failedToControlChamberLight"),"error")}}),ce=(De?.state==="RUNNING"||De?.state==="PAUSE"||De?.state==="PAUSED")&&(De?.printable_objects_count??0)>=2;w.useEffect(()=>(me&&(document.title=`${me.name} - Camera`),()=>{document.title="Bambuddy"}),[me]);const Ne=w.useRef(!1);w.useEffect(()=>{const ye=`/api/v1/printers/${s}/camera/stop`;Ne.current=!1;const ke=()=>{if(s>0&&!Ne.current){Ne.current=!0;const Ve={},Ae=rd();Ae&&(Ve.Authorization=`Bearer ${Ae}`),fetch(ye,{method:"POST",keepalive:!0,headers:Ve}).catch(()=>{})}},Ue=()=>{ke()};window.addEventListener("beforeunload",Ue);const je=X.current;return()=>{window.removeEventListener("beforeunload",Ue),je&&(je.src=""),ke()}},[s]),w.useEffect(()=>{if(f&&!b){const ke=setTimeout(()=>{p(!1)},o==="stream"?3e3:2e4);return()=>clearTimeout(ke)}},[o,f,x,b]),w.useEffect(()=>{const ye=()=>{const ke=!!document.fullscreenElement;N(ke),U(1),B({x:0,y:0}),o==="stream"&&!b&&(X.current&&(X.current.src=""),setTimeout(()=>{p(!0),v(Date.now())},200))};return document.addEventListener("fullscreenchange",ye),()=>document.removeEventListener("fullscreenchange",ye)},[o,b]),w.useEffect(()=>{let ye;const ke=()=>{clearTimeout(ye),ye=setTimeout(()=>{localStorage.setItem("cameraWindowState",JSON.stringify({width:window.outerWidth,height:window.outerHeight,left:window.screenX,top:window.screenY}))},500)};return window.addEventListener("resize",ke),()=>{clearTimeout(ye),window.removeEventListener("resize",ke)}},[]),w.useEffect(()=>()=>{J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),fe.current&&clearInterval(fe.current)},[]);const ie=w.useCallback(()=>{if(A>=qj){k(!1),h(!0);return}const ye=Math.min(eJe*Math.pow(2,A),tJe);k(!0),E(Math.ceil(ye/1e3)),Se.current=setInterval(()=>{E(ke=>ke<=1?(Se.current&&clearInterval(Se.current),0):ke-1)},1e3),J.current=setTimeout(()=>{M(ke=>ke+1),k(!1),p(!0),h(!1),X.current&&(X.current.src=""),v(Date.now())},ye)},[A]);w.useEffect(()=>{if(o!=="stream"||f||T||b){fe.current&&(clearInterval(fe.current),fe.current=null);return}return fe.current=setInterval(async()=>{try{const ye=await se.getCameraStatus(s);(ye.stalled||!ye.active&&!d)&&(console.log(`Stream issue detected: stalled=${ye.stalled}, active=${ye.active}, reconnecting...`),fe.current&&(clearInterval(fe.current),fe.current=null),p(!1),ie())}catch{}},nJe),()=>{fe.current&&(clearInterval(fe.current),fe.current=null)}},[o,f,d,T,b,s,ie]);const Le=()=>{p(!1),o==="stream"&&A{if(p(!1),h(!1),M(0),k(!1),J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),X.current&&!localStorage.getItem("cameraWindowState")){const ye=X.current,ke=ye.naturalWidth,Ue=ye.naturalHeight;if(ke>0&&Ue>0){const Ae=window.outerWidth-window.innerWidth,Je=window.outerHeight-window.innerHeight,Pe=ke+16+Ae,Ye=Ue+45+16+Je;try{window.resizeTo(Pe,Ye)}catch{}}}},W=()=>{if(s>0){const ye={},ke=rd();ke&&(ye.Authorization=`Bearer ${ke}`),fetch(`/api/v1/printers/${s}/camera/stop`,{method:"POST",headers:ye}).catch(()=>{})}},ue=ye=>{o===ye||b||(y(!0),p(!0),h(!1),M(0),k(!1),U(1),B({x:0,y:0}),J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),X.current&&(X.current.src=""),o==="stream"&&W(),setTimeout(()=>{l(ye),v(Date.now()),y(!1)},100))},Z=()=>{b||(y(!0),p(!0),h(!1),M(0),k(!1),J.current&&clearTimeout(J.current),Se.current&&clearInterval(Se.current),X.current&&(X.current.src=""),o==="stream"&&W(),setTimeout(()=>{v(Date.now()),y(!1)},100))},oe=()=>{te.current&&(document.fullscreenElement?document.exitFullscreen():te.current.requestFullscreen())},Q=()=>{U(ye=>Math.min(ye+.5,4))},he=()=>{U(ye=>{const ke=Math.max(ye-.5,1);return ke===1&&B({x:0,y:0}),ke})},be=ye=>{ye.preventDefault(),ye.deltaY<0?Q():he()},ee=ye=>{P>1&&(ye.preventDefault(),V(!0),I({x:ye.clientX-L.x,y:ye.clientY-L.y}))},z=w.useCallback(()=>{if(!te.current)return{x:300,y:200};const ye=te.current.getBoundingClientRect(),ke=ye.width*(P-1)/2,Ue=ye.height*(P-1)/2;return{x:Math.max(50,ke),y:Math.max(50,Ue)}},[P]),D=ye=>{if(R&&P>1){const ke=ye.clientX-q.x,Ue=ye.clientY-q.y,je=z();B({x:Math.max(-je.x,Math.min(je.x,ke)),y:Math.max(-je.y,Math.min(je.y,Ue))})}},G=()=>{V(!1)},pe=ye=>{if(ye.length<2)return 0;const ke=ye[0].clientX-ye[1].clientX,Ue=ye[0].clientY-ye[1].clientY;return Math.sqrt(ke*ke+Ue*Ue)},ve=ye=>ye.length<2?{x:ye[0].clientX,y:ye[0].clientY}:{x:(ye[0].clientX+ye[1].clientX)/2,y:(ye[0].clientY+ye[1].clientY)/2},xe=ye=>{ye.touches.length===2?(ye.preventDefault(),Y(pe(ye.touches)),H(ve(ye.touches))):ye.touches.length===1&&P>1&&(ye.preventDefault(),V(!0),I({x:ye.touches[0].clientX-L.x,y:ye.touches[0].clientY-L.y}))},ze=ye=>{if(ye.touches.length===2&&K!==null){ye.preventDefault();const ke=pe(ye.touches),Ue=ke/K;U(Ve=>{const Ae=Math.max(1,Math.min(4,Ve*Ue));return Ae===1&&B({x:0,y:0}),Ae}),Y(ke);const je=ve(ye.touches);if(le){const Ve=z();B(Ae=>({x:Math.max(-Ve.x,Math.min(Ve.x,Ae.x+(je.x-le.x))),y:Math.max(-Ve.y,Math.min(Ve.y,Ae.y+(je.y-le.y)))}))}H(je)}else if(ye.touches.length===1&&R&&P>1){ye.preventDefault();const ke=ye.touches[0].clientX-q.x,Ue=ye.touches[0].clientY-q.y,je=z();B({x:Math.max(-je.x,Math.min(je.x,ke)),y:Math.max(-je.y,Math.min(je.y,Ue))})}},_e=ye=>{ye.touches.length<2&&(Y(null),H(null)),ye.touches.length===0&&V(!1)},He=()=>{U(1),B({x:0,y:0})},Ze=b?"":o==="stream"?`/api/v1/printers/${s}/camera/stream?fps=15&t=${x}`:`/api/v1/printers/${s}/camera/snapshot?t=${x}`,we=f||b||T;return s?a.jsxs("div",{ref:te,className:"min-h-screen bg-black flex flex-col",children:[a.jsxs("div",{className:"flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary",children:[a.jsxs("h1",{className:"text-sm font-medium text-white flex items-center gap-2",children:[a.jsx(mb,{className:"w-4 h-4"}),me?.name||`Printer ${s}`]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"flex bg-bambu-dark rounded p-0.5",children:[a.jsx("button",{onClick:()=>ue("stream"),disabled:we,className:`px-3 py-1 text-xs rounded transition-colors ${o==="stream"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:t("camera.live")}),a.jsx("button",{onClick:()=>ue("snapshot"),disabled:we,className:`px-3 py-1 text-xs rounded transition-colors ${o==="snapshot"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white disabled:opacity-50"}`,children:t("camera.snapshot")})]}),a.jsx("button",{onClick:()=>de.mutate(!De?.chamber_light),disabled:!De?.connected||de.isPending||!r("printers:control"),className:`p-1.5 rounded disabled:opacity-50 ${De?.chamber_light?"bg-yellow-500/20 hover:bg-yellow-500/30":"hover:bg-bambu-dark-tertiary"}`,title:r("printers:control")?t("camera.chamberLight"):t("printers.permission.noControl"),children:a.jsx(bL,{on:De?.chamber_light??!1,className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>u(!0),disabled:!ce||!r("printers:control"),className:`p-1.5 rounded disabled:opacity-50 ${ce&&r("printers:control")?"hover:bg-bambu-dark-tertiary":""}`,title:r("printers:control")?t(ce?"printers.skipObjects.tooltip":"printers.skipObjects.onlyWhilePrinting"):t("printers.permission.noControl"),children:a.jsx(_C,{className:"w-4 h-4 text-bambu-gray"})}),a.jsx("button",{onClick:Z,disabled:we,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50",title:t(o==="stream"?"camera.restartStream":"camera.refreshSnapshot"),children:a.jsx(kr,{className:`w-4 h-4 text-bambu-gray ${we?"animate-spin":""}`})}),a.jsx("button",{onClick:oe,className:"p-1.5 hover:bg-bambu-dark-tertiary rounded",title:t(S?"camera.exitFullscreen":"camera.fullscreen"),children:S?a.jsx(pX,{className:"w-4 h-4 text-bambu-gray"}):a.jsx(Kme,{className:"w-4 h-4 text-bambu-gray"})})]})]}),a.jsx("div",{className:"flex-1 flex items-center justify-center p-2 overflow-hidden",onWheel:be,onMouseMove:D,onMouseUp:G,onMouseLeave:G,onTouchStart:xe,onTouchMove:ze,onTouchEnd:_e,style:{touchAction:"none"},children:a.jsxs("div",{className:"relative w-full h-full flex items-center justify-center",children:[(f||b)&&!T&&a.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/50 z-10",children:a.jsxs("div",{className:"text-center",children:[a.jsx(kr,{className:"w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:t(o==="stream"?"camera.connectingToCamera":"camera.capturingSnapshot")})]})}),T&&a.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black/80 z-10",children:a.jsxs("div",{className:"text-center p-4",children:[a.jsx(Df,{className:"w-10 h-10 text-orange-400 mx-auto mb-3"}),a.jsx("p",{className:"text-white mb-2",children:t("camera.connectionLost")}),a.jsx("p",{className:"text-sm text-bambu-gray mb-3",children:t("camera.reconnecting",{countdown:j,attempt:A+1,max:qj})}),a.jsx("button",{onClick:Z,className:"px-4 py-2 bg-bambu-green text-white text-sm rounded hover:bg-bambu-green/80 transition-colors",children:t("camera.reconnectNow")})]})}),d&&!T&&a.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-black z-10",children:a.jsxs("div",{className:"text-center p-4",children:[a.jsx(Cn,{className:"w-12 h-12 text-orange-400 mx-auto mb-3"}),a.jsx("p",{className:"text-white mb-2",children:t("camera.cameraUnavailable")}),a.jsx("p",{className:"text-xs text-bambu-gray mb-4 max-w-md",children:t("camera.cameraUnavailableDesc")}),a.jsx("button",{onClick:Z,className:"px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors",children:t("camera.retry")})]})}),a.jsx("img",{ref:X,src:Ze,alt:t("camera.cameraStream"),className:"max-w-full max-h-full object-contain select-none",style:{transform:`scale(${P}) translate(${L.x/P}px, ${L.y/P}px)`,cursor:P>1?R?"grabbing":"grab":"default"},onError:Ze?Le:void 0,onLoad:Ze?$:void 0,onMouseDown:ee,draggable:!1},x),a.jsxs("div",{className:"absolute bottom-4 left-4 flex items-center gap-1.5 bg-black/60 rounded-lg px-2 py-1.5",children:[a.jsx("button",{onClick:he,disabled:P<=1,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:t("camera.zoomOut"),children:a.jsx(HF,{className:"w-4 h-4 text-white"})}),a.jsxs("button",{onClick:He,className:"px-2 py-1 text-sm text-white hover:bg-white/10 rounded min-w-[48px]",title:t("camera.resetZoom"),children:[Math.round(P*100),"%"]}),a.jsx("button",{onClick:Q,disabled:P>=4,className:"p-1.5 hover:bg-white/10 rounded disabled:opacity-30",title:t("camera.zoomIn"),children:a.jsx(BF,{className:"w-4 h-4 text-white"})})]})]})}),a.jsx(xL,{printerId:s,isOpen:c,onClose:()=>u(!1)})]}):a.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:a.jsx("p",{className:"text-white",children:t("camera.invalidPrinterId")})})}function iJe(t){const e=t.get("show")?.split(",")||["progress","layers","eta","filename","status"],n=parseInt(t.get("fps")||"15",10),r=Math.min(Math.max(isNaN(n)?15:n,1),30),i=t.get("camera"),s=i!=="false"&&i!=="0";return{size:t.get("size")||"medium",fps:r,showCamera:s,showProgress:e.includes("progress"),showLayers:e.includes("layers"),showEta:e.includes("eta"),showFilename:e.includes("filename"),showStatus:e.includes("status"),showPrinter:e.includes("printer")}}function aJe(t){const e=Math.floor(t/3600),n=Math.floor(t%3600/60);return e>0?`${e}h ${n}m`:`${n}m`}function sJe(t,e){const n=new Date,r=new Date(n.getTime()+t*60*1e3),i=new Date;i.setHours(0,0,0,0);const s=new Date(r);s.setHours(0,0,0,0);const o=r.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"});return s.getTime()===i.getTime()?o:s.getTime()===i.getTime()+864e5?`${e("streamOverlay.tomorrow")} ${o}`:r.toLocaleDateString([],{weekday:"short"})+" "+o}function oJe(t,e){if(t.stg_cur_name)return t.stg_cur_name;switch(t.state){case"RUNNING":return e("streamOverlay.status.printing");case"PAUSE":return e("streamOverlay.status.paused");case"FINISH":return e("streamOverlay.status.finished");case"FAILED":return e("streamOverlay.status.failed");case"IDLE":return e("streamOverlay.status.idle");default:return t.state||e("streamOverlay.status.unknown")}}function lJe(t){switch(t){case"small":return{container:"p-3",text:"text-sm",textLarge:"text-lg",progressHeight:"h-2",icon:"w-3 h-3",gap:"gap-2",logoHeight:"h-12"};case"large":return{container:"p-6",text:"text-xl",textLarge:"text-3xl",progressHeight:"h-4",icon:"w-6 h-6",gap:"gap-4",logoHeight:"h-24"};default:return{container:"p-4",text:"text-base",textLarge:"text-xl",progressHeight:"h-3",icon:"w-4 h-4",gap:"gap-3",logoHeight:"h-16"}}}function cJe(){const{printerId:t}=Yk(),[e]=NF(),{t:n}=Vt(),r=tn(),i=parseInt(t||"0",10),[s,o]=w.useState(Date.now()),l=w.useMemo(()=>iJe(e),[e]),c=lJe(l.size),{data:u}=tt({queryKey:["printer",i],queryFn:()=>se.getPrinter(i),enabled:i>0}),{data:d}=tt({queryKey:["printerStatus",i],queryFn:()=>se.getPrinterStatus(i),enabled:i>0,refetchInterval:2e3});w.useEffect(()=>{if(!i)return;const b=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.host}/api/v1/ws`,y=new WebSocket(b);return y.onmessage=S=>{try{const N=JSON.parse(S.data);N.type==="printer_status"&&N.printer_id===i&&r.setQueryData(["printerStatus",i],N.status)}catch{}},y.onerror=()=>{},()=>{y.close()}},[i,r]),w.useEffect(()=>(document.title=u?`${u.name} - ${n("streamOverlay.title")}`:n("streamOverlay.title"),()=>{document.title="Bambuddy"}),[u,n]);const h=()=>{setTimeout(()=>{o(Date.now())},3e3)};if(!i)return a.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:a.jsx("p",{className:"text-white",children:n("streamOverlay.invalidPrinterId")})});if(!d)return a.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center",children:a.jsx("p",{className:"text-gray-400",children:n("common.loading")})});const f=d.state==="RUNNING"||d.state==="PAUSE",p=d.progress||0,x=`/api/v1/printers/${i}/camera/stream?fps=${l.fps}&t=${s}`;return a.jsxs("div",{className:"min-h-screen bg-black relative overflow-hidden",children:[l.showCamera&&a.jsx("img",{src:x,alt:n("streamOverlay.cameraStream"),className:"absolute inset-0 w-full h-full object-contain",onError:h},s),a.jsx("a",{href:"https://github.com/maziggy/bambuddy",target:"_blank",rel:"noopener noreferrer",className:"absolute top-4 right-4 z-10",children:a.jsx("img",{src:"/img/bambuddy_logo_dark_transparent.png",alt:"Bambuddy",className:`${c.logoHeight} object-contain drop-shadow-lg hover:scale-105 transition-transform`})}),a.jsx("div",{className:"absolute bottom-0 left-0 right-0 z-10 bg-gradient-to-t from-black/80 via-black/60 to-transparent",children:a.jsxs("div",{className:`${c.container}`,children:[l.showPrinter&&u&&a.jsxs("div",{className:`flex items-center ${c.gap} mb-2`,children:[a.jsx(Cr,{className:`${c.icon} text-white/70`}),a.jsx("span",{className:`${c.text} text-white font-medium`,children:u.name})]}),l.showFilename&&d.current_print&&a.jsx("div",{className:`${c.textLarge} text-white font-semibold mb-2 truncate drop-shadow-md`,children:d.current_print.replace(/\.gcode\.3mf$|\.3mf$|\.gcode$/i,"")}),l.showStatus&&a.jsx("div",{className:`${c.text} text-white/70 mb-2`,children:oJe(d,n)}),l.showProgress&&f&&a.jsxs("div",{className:"mb-3",children:[a.jsxs("div",{className:`flex items-center justify-between mb-1 ${c.text}`,children:[a.jsx("span",{className:"text-white/70",children:n("streamOverlay.progress")}),a.jsxs("span",{className:"text-white font-bold",children:[Math.round(p),"%"]})]}),a.jsx("div",{className:`w-full bg-white/20 rounded-full ${c.progressHeight}`,children:a.jsx("div",{className:`bg-bambu-green ${c.progressHeight} rounded-full transition-all duration-500`,style:{width:`${p}%`}})})]}),f&&(l.showLayers||l.showEta)&&a.jsxs("div",{className:`flex items-center ${c.gap} flex-wrap`,children:[l.showLayers&&d.layer_num!=null&&d.total_layers!=null&&d.total_layers>0&&a.jsxs("div",{className:`flex items-center ${c.gap} text-white/70`,children:[a.jsx(rs,{className:c.icon}),a.jsxs("span",{className:c.text,children:[a.jsx("span",{className:"text-white",children:d.layer_num}),a.jsx("span",{className:"mx-1",children:"/"}),a.jsx("span",{children:d.total_layers})]})]}),l.showEta&&d.remaining_time!=null&&d.remaining_time>0&&a.jsxs(a.Fragment,{children:[a.jsxs("div",{className:`flex items-center ${c.gap} text-white/70`,children:[a.jsx(Ou,{className:c.icon}),a.jsx("span",{className:`${c.text} text-white`,children:aJe(d.remaining_time*60)})]}),a.jsxs("div",{className:`flex items-center ${c.gap} text-white/70`,children:[a.jsx(Qn,{className:c.icon}),a.jsxs("span",{className:`${c.text} text-white`,children:[n("streamOverlay.eta")," ",sJe(d.remaining_time,n)]})]})]})]}),!f&&a.jsx("div",{className:`${c.text} text-white/70 py-2`,children:d.connected?n("streamOverlay.printerIdle"):n("streamOverlay.printerOffline")})]})})]})}function uJe(){const{t}=Vt(),{id:e}=Yk(),{mode:n}=np(),{data:r,isLoading:i,error:s}=tt({queryKey:["external-link",e],queryFn:()=>se.getExternalLink(Number(e)),enabled:!!e});return i?a.jsx("div",{className:"flex items-center justify-center h-full",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):s||!r?a.jsxs("div",{className:"flex flex-col items-center justify-center h-full gap-4 text-bambu-gray",children:[a.jsx(Cn,{className:"w-12 h-12"}),a.jsx("p",{children:t("common.linkNotFound")})]}):a.jsx("iframe",{src:r.url,className:"h-full w-full border-0",style:{colorScheme:n},title:r.name,sandbox:"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"})}const dK={material:"",subtype:"",brand:"",color_name:"",rgba:"808080FF",label_weight:1e3,core_weight:250,weight_used:0,slicer_filament:"",note:""};function dJe(t){const e={};return t.slicer_filament||(e.slicer_filament="Slicer preset is required"),t.material||(e.material="Material is required"),{isValid:Object.keys(e).length===0,errors:e}}const hJe=["PLA","PETG","ABS","TPU","ASA","PC","PA","PVA","HIPS","PA-CF","PETG-CF","PLA-CF"],mJe=["Bambu","PolyLite","PolyTerra","eSUN","Overture","Fiberon","SUNLU","Inland","Hatchbox","Generic"],y0=["Basic","Matte","Silk","Tough","HF","High Flow","Engineering","Galaxy","Glow","Marble","Metal","Rainbow","Sparkle","Wood","Translucent","Transparent","Clear","Lite","Pro","Plus","Max","Super","Ultra","Flex","Soft","Hard","Strong","Impact","Heat Resistant","UV Resistant","ESD","Conductive","Magnetic","Gradient","Dual Color","Tri Color","Multicolor"],Yae=[{name:"Black",hex:"000000"},{name:"White",hex:"FFFFFF"},{name:"Gray",hex:"808080"},{name:"Red",hex:"FF0000"},{name:"Orange",hex:"FFA500"},{name:"Yellow",hex:"FFFF00"},{name:"Green",hex:"00AE42"},{name:"Blue",hex:"0066FF"},{name:"Purple",hex:"8B00FF"},{name:"Pink",hex:"FF69B4"},{name:"Brown",hex:"8B4513"},{name:"Silver",hex:"C0C0C0"}],fJe=[{name:"Dark Red",hex:"8B0000"},{name:"Crimson",hex:"DC143C"},{name:"Coral",hex:"FF7F50"},{name:"Salmon",hex:"FA8072"},{name:"Dark Orange",hex:"FF8C00"},{name:"Peach",hex:"FFDAB9"},{name:"Gold",hex:"FFD700"},{name:"Khaki",hex:"F0E68C"},{name:"Lemon",hex:"FFF44F"},{name:"Lime",hex:"32CD32"},{name:"Forest Green",hex:"228B22"},{name:"Olive",hex:"808000"},{name:"Mint",hex:"98FF98"},{name:"Teal",hex:"008080"},{name:"Navy",hex:"000080"},{name:"Sky Blue",hex:"87CEEB"},{name:"Royal Blue",hex:"4169E1"},{name:"Cyan",hex:"00FFFF"},{name:"Turquoise",hex:"40E0D0"},{name:"Violet",hex:"EE82EE"},{name:"Magenta",hex:"FF00FF"},{name:"Indigo",hex:"4B0082"},{name:"Lavender",hex:"E6E6FA"},{name:"Plum",hex:"DDA0DD"},{name:"Hot Pink",hex:"FF69B4"},{name:"Rose",hex:"FF007F"},{name:"Blush",hex:"FFB6C1"},{name:"Chocolate",hex:"D2691E"},{name:"Tan",hex:"D2B48C"},{name:"Beige",hex:"F5F5DC"},{name:"Maroon",hex:"800000"},{name:"Dark Gray",hex:"404040"},{name:"Light Gray",hex:"D3D3D3"},{name:"Charcoal",hex:"36454F"},{name:"Ivory",hex:"FFFFF0"},{name:"Bambu Green",hex:"00AE42"},{name:"Jade White",hex:"E8E8E8"},{name:"Titan Gray",hex:"5A5A5A"}],hK=[...Yae,...fJe],Qae="bambuddy-recent-colors",pJe=8,gJe=[{code:"GFL00",name:"Bambu PLA Basic",displayName:"Bambu PLA Basic",isCustom:!1,allCodes:["GFL00"]},{code:"GFL01",name:"Bambu PLA Matte",displayName:"Bambu PLA Matte",isCustom:!1,allCodes:["GFL01"]},{code:"GFL05",name:"Generic PLA",displayName:"Generic PLA",isCustom:!1,allCodes:["GFL05"]},{code:"GFG00",name:"Bambu PETG Basic",displayName:"Bambu PETG Basic",isCustom:!1,allCodes:["GFG00"]},{code:"GFG05",name:"Generic PETG",displayName:"Generic PETG",isCustom:!1,allCodes:["GFG05"]},{code:"GFB00",name:"Bambu ABS Basic",displayName:"Bambu ABS Basic",isCustom:!1,allCodes:["GFB00"]},{code:"GFB05",name:"Generic ABS",displayName:"Generic ABS",isCustom:!1,allCodes:["GFB05"]},{code:"GFA00",name:"Bambu ASA Basic",displayName:"Bambu ASA Basic",isCustom:!1,allCodes:["GFA00"]},{code:"GFU00",name:"Bambu TPU 95A",displayName:"Bambu TPU 95A",isCustom:!1,allCodes:["GFU00"]},{code:"GFU05",name:"Generic TPU",displayName:"Generic TPU",isCustom:!1,allCodes:["GFU05"]},{code:"GFC00",name:"Bambu PC Basic",displayName:"Bambu PC Basic",isCustom:!1,allCodes:["GFC00"]},{code:"GFN00",name:"Bambu PA Basic",displayName:"Bambu PA Basic",isCustom:!1,allCodes:["GFN00"]},{code:"GFN05",name:"Generic PA",displayName:"Generic PA",isCustom:!1,allCodes:["GFN05"]},{code:"GFS00",name:"Bambu PLA-CF",displayName:"Bambu PLA-CF",isCustom:!1,allCodes:["GFS00"]},{code:"GFT00",name:"Bambu PETG-CF",displayName:"Bambu PETG-CF",isCustom:!1,allCodes:["GFT00"]},{code:"GFNC0",name:"Bambu PA-CF",displayName:"Bambu PA-CF",isCustom:!1,allCodes:["GFNC0"]},{code:"GFV00",name:"Bambu PVA",displayName:"Bambu PVA",isCustom:!1,allCodes:["GFV00"]}];function Wk(t){let e=t.replace(/@.*$/,"").trim();e=e.replace(/\(Custom\)/i,"").trim(),e=e.replace(/^[#*]+\s*/,"").trim();const n=["PLA-CF","PETG-CF","ABS-GF","ASA-CF","PA-CF","PAHT-CF","PA6-CF","PA6-GF","PPA-CF","PPA-GF","PET-CF","PPS-CF","PC-CF","PC-ABS","ABS-GF","PETG","PLA","ABS","ASA","PC","PA","TPU","PVA","HIPS","BVOH","PPS","PCTG","PEEK","PEI"];let r="",i=-1;for(const c of n){const u=e.toUpperCase().indexOf(c.toUpperCase());if(u!==-1){r=c,i=u;break}}let s="";i>0&&(s=e.substring(0,i).trim(),s=s.replace(/[-_\s]+$/,""));let o="";i!==-1&&r&&(o=e.substring(i+r.length).trim(),o=o.replace(/^[-_\s]+/,""));let l="";for(const c of y0)if(o.toLowerCase().includes(c.toLowerCase())){l=c;break}if(!l&&s)for(const c of y0){const u=new RegExp(`\\s+${c}$`,"i");if(u.test(s)){l=c,s=s.replace(u,"").trim();break}}return{brand:s,material:r,variant:l}}function bJe(t,e){const n=new Set(mJe);for(const r of t){const{brand:i}=Wk(r.name);i&&i.length>1&&n.add(i)}if(e)for(const r of e)if(r.filament_vendor&&r.filament_vendor.length>1)n.add(r.filament_vendor);else{const{brand:i}=Wk(r.name);i&&i.length>1&&n.add(i)}return Array.from(n).sort((r,i)=>r.localeCompare(i))}function xJe(t){const e=t.filter(r=>r.preset_type==="filament");if(e.length===0)return[];const n=new Map;for(const r of e){const i=r.name.replace(/@.*$/,"").trim(),s=n.get(i);if(s)s.allCodes.push(String(r.id));else{const o=r.filament_type||String(r.id);n.set(i,{code:o,name:i,displayName:i,isCustom:!1,allCodes:[o]})}}return Array.from(n.values()).sort((r,i)=>r.displayName.localeCompare(i.displayName))}function yJe(t,e,n){if(t.length>0){const r=[],i=new Map;for(const s of t)if(s.is_custom){const o=s.name.toUpperCase();(e.size===0||Array.from(e).some(c=>o.includes(c))||!o.includes("@"))&&r.push({code:s.setting_id,name:s.name,displayName:`${s.name} (Custom)`,isCustom:!0,allCodes:[s.setting_id]})}else{const o=s.name.replace(/@.*$/,"").trim(),l=i.get(o);l?l.allCodes.push(s.setting_id):i.set(o,{code:s.setting_id,name:o,displayName:o,isCustom:!1,allCodes:[s.setting_id]})}return[...r,...Array.from(i.values())].sort((s,o)=>s.displayName.localeCompare(o.displayName))}if(n&&n.length>0){const r=xJe(n);if(r.length>0)return r}return gJe}function vJe(t,e){if(!t)return;let n=e.find(r=>r.code===t);if(n||(n=e.find(r=>r.allCodes.includes(t))),!n){const r=t.toLowerCase();n=e.find(i=>i.code.toLowerCase()===r||i.allCodes.some(s=>s.toLowerCase()===r))}return n}function wJe(){try{const t=localStorage.getItem(Qae);if(t)return JSON.parse(t)}catch{}return[]}function _Je(t,e){const n=e.filter(i=>i.hex.toUpperCase()!==t.hex.toUpperCase()),r=[t,...n].slice(0,pJe);try{localStorage.setItem(Qae,JSON.stringify(r))}catch{}return r}function Wj(t,e){if(!e.material)return!1;const r=(t.name||"").replace(/^High Flow[_\s]+/i,"").replace(/^Standard[_\s]+/i,"").replace(/^HF[_\s]+/i,"").replace(/^S[_\s]+/i,"").trim(),i=Wk(r);return!(!(i.material.toUpperCase()===e.material.toUpperCase())||e.brand&&!(i.brand.toLowerCase().includes(e.brand.toLowerCase())||e.brand.toLowerCase().includes(i.brand.toLowerCase()))||e.subtype&&!(i.variant.toLowerCase().includes(e.subtype.toLowerCase())||e.subtype.toLowerCase().includes(i.variant.toLowerCase())||r.toLowerCase().includes(e.subtype.toLowerCase())))}function SJe({formData:t,updateField:e,cloudAuthenticated:n,loadingCloudPresets:r,presetInputValue:i,setPresetInputValue:s,selectedPresetOption:o,filamentOptions:l,availableBrands:c}){const{t:u}=Vt(),[d,h]=w.useState(!1),[f,p]=w.useState(!1),[x,v]=w.useState(!1),[b,y]=w.useState(""),[S,N]=w.useState(""),A=w.useRef(null),M=w.useRef(null),T=w.useRef(null);w.useEffect(()=>{const U=L=>{A.current&&!A.current.contains(L.target)&&h(!1),M.current&&!M.current.contains(L.target)&&p(!1),T.current&&!T.current.contains(L.target)&&v(!1)};return document.addEventListener("mousedown",U),()=>document.removeEventListener("mousedown",U)},[]);const k=w.useMemo(()=>{if(!i)return l;const U=i.toLowerCase();return l.filter(L=>L.displayName.toLowerCase().includes(U)||L.code.toLowerCase().includes(U))},[l,i]),j=w.useMemo(()=>{if(!b)return c;const U=b.toLowerCase();return c.filter(L=>L.toLowerCase().includes(U))},[c,b]),E=w.useMemo(()=>{if(!S)return y0;const U=S.toLowerCase();return y0.filter(L=>L.toLowerCase().includes(U))},[S]),P=U=>{e("slicer_filament",U.code),s(U.displayName),h(!1);const L=Wk(U.name);L.material&&e("material",L.material),L.brand&&e("brand",L.brand),L.variant&&e("subtype",L.variant)};return a.jsxs("div",{className:"space-y-4",children:[a.jsx("div",{className:"flex items-center gap-2 text-xs text-bambu-gray",children:r?a.jsxs(a.Fragment,{children:[a.jsx(at,{className:"w-3 h-3 animate-spin"})," ",u("inventory.loadingPresets")]}):n?a.jsxs(a.Fragment,{children:[a.jsx(Gb,{className:"w-3 h-3 text-bambu-green"})," ",u("inventory.cloudConnected")]}):a.jsxs(a.Fragment,{children:[a.jsx(uhe,{className:"w-3 h-3"})," ",u("inventory.cloudNotConnected")]})}),a.jsxs("div",{children:[a.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[u("inventory.slicerPreset")," *"]}),a.jsxs("div",{className:"relative",ref:A,children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),a.jsx("input",{type:"text",className:"w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:u("inventory.searchPresets"),value:i,onChange:U=>{s(U.target.value),h(!0)},onFocus:()=>{h(!0),s("")}}),d&&a.jsx("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto",children:k.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:u("inventory.noPresetsFound")}):k.map(U=>a.jsxs("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex justify-between items-center ${o?.code===U.code?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>P(U),children:[a.jsx("span",{className:"truncate",children:U.displayName}),a.jsx("span",{className:"font-mono text-xs text-bambu-gray ml-2 shrink-0",children:U.code})]},U.code))})]}),o&&a.jsxs("div",{className:"mt-1 text-xs text-bambu-gray",children:[u("inventory.selectedPreset"),": ",a.jsx("span",{className:"font-mono text-bambu-green",children:o.code})]})]}),a.jsxs("div",{children:[a.jsxs("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:[u("inventory.material")," *"]}),a.jsxs("select",{value:t.material,onChange:U=>e("material",U.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green",children:[a.jsx("option",{value:"",children:u("inventory.selectMaterial")}),hJe.map(U=>a.jsx("option",{value:U,children:U},U))]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:u("inventory.brand")}),a.jsxs("div",{className:"relative",ref:M,children:[a.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:u("inventory.searchBrand"),value:f?b:t.brand,onChange:U=>{y(U.target.value),p(!0)},onFocus:()=>{p(!0),y("")}}),a.jsx(nr,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),f&&a.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[j.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:u("inventory.noResults")}):j.map(U=>a.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.brand===U?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("brand",U),p(!1),y("")},children:U},U)),b&&!j.includes(b)&&a.jsx("button",{type:"button",className:"w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary",onClick:()=>{e("brand",b),p(!1),y("")},children:u("inventory.useCustomBrand",{brand:b})})]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:u("inventory.subtype")}),a.jsxs("div",{className:"relative",ref:T,children:[a.jsx("input",{type:"text",value:x?S:t.subtype,onChange:U=>{N(U.target.value),v(!0)},onFocus:()=>{v(!0),N("")},placeholder:"Basic, Matte, Silk...",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"}),a.jsx(nr,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),x&&a.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[E.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:u("inventory.noResults")}):E.map(U=>a.jsx("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${t.subtype===U?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{e("subtype",U),v(!1),N("")},children:U},U)),S&&!y0.some(U=>U.toLowerCase()===S.toLowerCase().trim())&&a.jsx("button",{type:"button",className:"w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary text-bambu-green border-t border-bambu-dark-tertiary",onClick:()=>{e("subtype",S),v(!1),N("")},children:u("inventory.useCustomBrand",{brand:S})})]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:u("inventory.labelWeight")}),a.jsxs("div",{className:"relative",children:[a.jsx("input",{type:"number",value:t.label_weight,onChange:U=>e("label_weight",parseInt(U.target.value)||0),className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),a.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]})]})]})}function NJe({formData:t,updateField:e,recentColors:n,onColorUsed:r,catalogColors:i}){const{t:s}=Vt(),[o,l]=w.useState(!1),[c,u]=w.useState(""),d=t.rgba.replace("#","").substring(0,6),h=y=>d.toUpperCase()===y.toUpperCase(),f=(y,S)=>{e("rgba",y.toUpperCase()+"FF"),e("color_name",S),r({name:S,hex:y})},p=w.useMemo(()=>{if(i.length===0)return[];const y=t.brand?.trim(),S=t.material?.toLowerCase().trim(),N=t.subtype?.toLowerCase().trim();if(!y&&!S)return[];const A=y?y.toLowerCase().split(/[\s\-_]+/).filter(k=>k.length>=2):[],M=k=>{if(A.length===0)return!0;const j=k.toLowerCase();return A.some(E=>j.includes(E))},T=S&&N?`${S} ${N}`:"";if(T){const k=i.filter(E=>M(E.manufacturer)&&E.material?.toLowerCase()===T);if(k.length>0)return k.map(E=>({name:E.color_name,hex:E.hex_color.replace("#","").substring(0,6)}));const j=T.replace(/\+$/,"");if(j!==T){const E=i.filter(P=>M(P.manufacturer)&&P.material?.toLowerCase()===j);if(E.length>0)return E.map(P=>({name:P.color_name,hex:P.hex_color.replace("#","").substring(0,6)}))}}if(S){const k=i.filter(j=>M(j.manufacturer)&&(!j.material||j.material.toLowerCase().startsWith(S)));if(k.length>0)return k.map(j=>({name:j.color_name,hex:j.hex_color.replace("#","").substring(0,6)}))}return[]},[i,t.brand,t.material,t.subtype]),x=p.length>0,v=w.useMemo(()=>{if(!c)return p;const y=c.toLowerCase();return p.filter(S=>S.name.toLowerCase().includes(y))},[p,c]),b=w.useMemo(()=>c?hK.filter(y=>y.name.toLowerCase().includes(c.toLowerCase())):o?hK:Yae,[c,o]);return a.jsxs("div",{className:"space-y-3",children:[a.jsx("div",{className:"h-10 rounded-lg border border-bambu-dark-tertiary",style:{backgroundColor:`#${d}`}}),n.length>0&&a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-bambu-gray shrink-0",children:[a.jsx(Qn,{className:"w-3 h-3"}),a.jsx("span",{children:s("inventory.recentColors")})]}),a.jsx("div",{className:"flex flex-wrap gap-1.5",children:n.map(y=>a.jsx("button",{type:"button",onClick:()=>f(y.hex,y.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 ${h(y.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${y.hex}`},title:y.name},y.hex))})]}),a.jsxs("div",{className:"relative",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50 pointer-events-none"}),a.jsx("input",{type:"text",className:"w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.searchColors"),value:c,onChange:y=>u(y.target.value)})]}),x?a.jsxs("div",{className:"space-y-1.5",children:[a.jsx("span",{className:"text-xs text-bambu-gray",children:c?s("inventory.searchResults"):`${t.brand}${t.material?` ${t.material}`:""}`}),a.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[v.map(y=>a.jsx("button",{type:"button",onClick:()=>f(y.hex,y.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 relative group ${h(y.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${y.hex}`},title:y.name,children:a.jsx("span",{className:"absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-10 shadow-lg text-white",children:y.name})},`${y.hex}-${y.name}`)),v.length===0&&a.jsx("p",{className:"text-sm text-bambu-gray py-1",children:s("inventory.noColorsFound")})]})]}):a.jsxs("div",{className:"space-y-1.5",children:[a.jsxs("div",{className:"flex items-center justify-between text-xs text-bambu-gray",children:[a.jsx("span",{children:s(c?"inventory.searchResults":o?"inventory.allColors":"inventory.commonColors")}),!c&&a.jsx("button",{type:"button",onClick:()=>l(!o),className:"flex items-center gap-1 hover:text-white transition-colors",children:o?a.jsxs(a.Fragment,{children:[s("inventory.showLess")," ",a.jsx(ql,{className:"w-3 h-3"})]}):a.jsxs(a.Fragment,{children:[s("inventory.showAll")," ",a.jsx(nr,{className:"w-3 h-3"})]})})]}),a.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[b.map(y=>a.jsx("button",{type:"button",onClick:()=>f(y.hex,y.name),className:`w-6 h-6 rounded border-2 transition-all hover:scale-110 relative group ${h(y.hex)?"border-bambu-green ring-1 ring-bambu-green/30 scale-110":"border-bambu-dark-tertiary"}`,style:{backgroundColor:`#${y.hex}`},title:y.name,children:a.jsx("span",{className:"absolute -bottom-7 left-1/2 -translate-x-1/2 px-2 py-0.5 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-xs whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none z-10 shadow-lg text-white",children:y.name})},y.hex)),b.length===0&&a.jsx("p",{className:"text-sm text-bambu-gray py-1",children:s("inventory.noColorsFound")})]})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:s("inventory.colorName")}),a.jsx("input",{type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:s("inventory.colorNamePlaceholder"),value:t.color_name,onChange:y=>e("color_name",y.target.value)})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:s("inventory.hexColor")}),a.jsxs("div",{className:"flex gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx("span",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-bambu-gray",children:"#"}),a.jsx("input",{type:"text",className:"w-full pl-7 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm font-mono uppercase focus:outline-none focus:border-bambu-green",placeholder:"RRGGBB",value:d.toUpperCase(),onChange:y=>{const S=y.target.value.replace("#","").replace(/[^0-9A-Fa-f]/g,"");S.length<=8&&e("rgba",S.toUpperCase()+(S.length<=6?"FF":""))}})]}),a.jsx("input",{type:"color",className:"w-11 h-[38px] rounded-lg cursor-pointer border border-bambu-dark-tertiary shrink-0 bg-transparent",value:`#${d}`,onChange:y=>{const S=y.target.value.replace("#","").toUpperCase();e("rgba",S+"FF")},title:s("inventory.pickColor")})]})]})]})]})}function kJe({catalog:t,value:e,onChange:n}){const{t:r}=Vt(),[i,s]=w.useState(!1),[o,l]=w.useState(""),[c,u]=w.useState(null),d=w.useRef(null),h=w.useRef(null);w.useEffect(()=>{const x=v=>{d.current&&!d.current.contains(v.target)&&s(!1)};return document.addEventListener("mousedown",x),()=>document.removeEventListener("mousedown",x)},[]);const f=w.useMemo(()=>{if(!o)return t;const x=o.toLowerCase();return t.filter(v=>v.name.toLowerCase().includes(x)||v.weight.toString().includes(x))},[t,o]),p=w.useMemo(()=>{if(i)return o;if(c){const v=t.find(b=>b.id===c);if(v)return v.name}const x=t.find(v=>v.weight===e);return x?x.name:""},[i,o,c,t,e]);return a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:a.jsxs("span",{className:"flex items-center gap-2",children:[a.jsx(Efe,{className:"w-3.5 h-3.5 text-bambu-gray"}),r("inventory.coreWeight")]})}),a.jsxs("div",{className:"flex gap-2 items-center",children:[a.jsxs("div",{className:"flex-1 min-w-0 relative",ref:d,children:[a.jsx("input",{ref:h,type:"text",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green",placeholder:r("inventory.searchSpoolWeight"),value:p,onFocus:()=>{s(!0),l("")},onChange:x=>{l(x.target.value),s(!0)}}),i&&a.jsx("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-64 overflow-y-auto",children:f.length===0?a.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:r("inventory.noResults")}):f.map(x=>a.jsxs("button",{type:"button",className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex justify-between items-center ${(c?x.id===c:x.weight===e)?"bg-bambu-green/10 text-bambu-green":"text-white"}`,onClick:()=>{u(x.id),n(x.weight),s(!1),l("")},children:[a.jsx("span",{className:"truncate",children:x.name}),a.jsxs("span",{className:"font-mono text-xs text-bambu-gray ml-2 shrink-0",children:[x.weight,"g"]})]},x.id))})]}),a.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[a.jsx("input",{type:"number",className:"w-16 px-2 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm text-center font-mono focus:outline-none focus:border-bambu-green",value:e,min:0,max:2e3,onChange:x=>{const v=parseInt(x.target.value);!isNaN(v)&&v>=0&&n(v)}}),a.jsx("span",{className:"text-bambu-gray text-sm",children:"g"})]})]})]})}function CJe({formData:t,updateField:e,spoolCatalog:n}){const{t:r}=Vt(),{showToast:i}=mn(),[s,o]=w.useState(""),[l,c]=w.useState(!1),u=Math.max(0,t.label_weight-t.weight_used),d=t.core_weight+u;return w.useEffect(()=>{l||o(String(d))},[l,d]),a.jsxs("div",{className:"space-y-4",children:[a.jsx(kJe,{catalog:n,value:t.core_weight,onChange:h=>e("core_weight",h)}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:r("inventory.currentWeight")}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx("input",{type:"number",value:u,min:0,max:t.label_weight,onChange:h=>{const f=parseInt(h.target.value)||0;e("weight_used",Math.max(0,t.label_weight-f))},className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),a.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]}),a.jsxs("span",{className:"text-xs text-bambu-gray shrink-0",children:["/ ",t.label_weight,"g"]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:r("inventory.measuredWeight")}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx("input",{type:"number",value:s,min:0,onFocus:()=>c(!0),onChange:h=>{o(h.target.value)},onBlur:()=>{c(!1);const h=s.trim(),f=Number(h),p=t.core_weight,x=t.core_weight+t.label_weight;if(!h||!Number.isFinite(f)||fx){i(r("inventory.measuredWeightError",{min:p,max:x}),"error"),o(String(d));return}const v=Math.round(f),b=Math.max(0,Math.min(t.label_weight,v-t.core_weight));e("weight_used",Math.max(0,t.label_weight-b)),o(String(v))},className:"w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green"}),a.jsx("span",{className:"absolute right-2 top-1/2 -translate-y-1/2 text-xs text-bambu-gray",children:"g"})]}),a.jsxs("span",{className:"text-xs text-bambu-gray shrink-0",children:["/ ",t.core_weight+t.label_weight,"g"]})]})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:r("inventory.note")}),a.jsx("textarea",{className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green resize-none min-h-[80px]",placeholder:r("inventory.notePlaceholder"),value:t.note,onChange:h=>e("note",h.target.value)})]})]})}function MJe({formData:t,printersWithCalibrations:e,selectedProfiles:n,setSelectedProfiles:r,expandedPrinters:i,setExpandedPrinters:s}){const{t:o}=Vt(),l=f=>{s(p=>{const x=new Set(p);return x.has(f)?x.delete(f):x.add(f),x})},c=(f,p,x)=>{const v=`${f}:${p}:${x??"null"}`,b=`${f}:${x??"null"}`;r(y=>{const S=new Set(y);if(S.has(v))S.delete(v);else{for(const N of Array.from(S)){const A=N.split(":");`${A[0]}:${A[2]}`===b&&S.delete(N)}S.add(v)}return S})},u=()=>{const f=new Set;for(const{printer:p,calibrations:x}of e){if(!p.connected)continue;const v=x.filter(y=>Wj(y,t)),b=new Map;for(const y of v){const S=`${y.extruder_id??"null"}`;b.has(S)||b.set(S,[]),b.get(S).push(y)}for(const[y,S]of b)if(S.length>0){const A=[...S].sort((M,T)=>T.k_value-M.k_value)[0];f.add(`${p.id}:${A.cali_idx}:${y}`)}}r(f)};if(!t.material)return a.jsx("div",{className:"p-6 bg-bambu-dark rounded-lg text-center",children:a.jsx("p",{className:"text-bambu-gray",children:o("inventory.selectMaterialFirst")})});if(e.length===0)return a.jsx("div",{className:"p-6 bg-bambu-dark rounded-lg text-center",children:a.jsx("p",{className:"text-bambu-gray",children:o("inventory.noPrintersConfigured")})});const d=e.reduce((f,{printer:p,calibrations:x})=>p.connected?f+x.filter(v=>Wj(v,t)).length:f,0),h=(f,p)=>{const x=`${f.id}:${p.cali_idx}:${p.extruder_id??"null"}`,v=n.has(x);return a.jsxs("label",{className:`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-all border ${v?"bg-bambu-green/10 border-bambu-green/30":"bg-bambu-dark border-transparent hover:bg-bambu-dark/80"}`,children:[a.jsx("input",{type:"checkbox",checked:v,onChange:()=>c(String(f.id),p.cali_idx,p.extruder_id),className:"w-4 h-4 rounded border-bambu-dark-tertiary text-bambu-green focus:ring-bambu-green"}),a.jsx("div",{className:"flex-1 min-w-0",children:a.jsx("span",{className:`text-sm font-medium ${v?"text-bambu-green":"text-white"}`,children:p.name||p.filament_id})}),a.jsx("div",{className:"flex items-center gap-2 shrink-0",children:a.jsxs("span",{className:"text-xs font-mono px-2 py-0.5 rounded bg-bambu-dark text-bambu-gray",children:["K=",p.k_value.toFixed(3)]})})]},`${p.cali_idx}-${p.extruder_id}`)};return a.jsxs("div",{className:"space-y-4",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("p",{className:"text-xs text-bambu-gray",children:[o("inventory.matchingFilter"),": ",t.brand||o("inventory.anyBrand")," / ",t.material," / ",t.subtype||o("inventory.anyVariant")]}),d>0&&a.jsxs("button",{type:"button",onClick:u,className:"flex items-center gap-1.5 px-2 py-1 text-xs bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray hover:text-white hover:border-bambu-green transition-colors",children:[a.jsx(pb,{className:"w-3.5 h-3.5"}),o("inventory.autoSelect")," (",d,")"]})]}),a.jsx("div",{className:"space-y-3",children:e.map(({printer:f,calibrations:p})=>{const x=i.has(String(f.id)),v=p.filter(A=>Wj(A,t)),b=v.length,y=v.some(A=>A.extruder_id!==void 0&&A.extruder_id!==null&&A.extruder_id>0),S=v.filter(A=>A.extruder_id===1),N=v.filter(A=>A.extruder_id===0||A.extruder_id===void 0||A.extruder_id===null);return a.jsxs("div",{className:"border border-bambu-dark-tertiary rounded-lg overflow-hidden",children:[a.jsxs("button",{type:"button",onClick:()=>l(String(f.id)),className:"w-full px-4 py-3 flex items-center justify-between bg-bambu-dark-secondary hover:bg-bambu-dark-tertiary transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[x?a.jsx(nr,{className:"w-4 h-4 text-bambu-gray"}):a.jsx(fa,{className:"w-4 h-4 text-bambu-gray"}),a.jsx("span",{className:"font-medium text-white",children:f.name}),b>0?a.jsxs("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:[b," ",o(b!==1?"inventory.matches":"inventory.match")]}):a.jsx("span",{className:"text-xs px-2 py-0.5 rounded-full bg-bambu-dark-tertiary text-bambu-gray",children:o("inventory.noMatches")})]}),a.jsx("span",{className:`text-xs px-2 py-1 rounded-full ${f.connected?"bg-green-500/20 text-green-500":"bg-bambu-gray/20 text-bambu-gray"}`,children:f.connected?o("inventory.connected"):o("inventory.offline")})]}),x&&a.jsx("div",{className:"px-4 py-3 space-y-3 bg-bambu-dark border-t border-bambu-dark-tertiary",children:f.connected?b===0?a.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.noKProfilesMatch")}):y?a.jsxs(a.Fragment,{children:[S.length>0&&a.jsxs("div",{className:"space-y-2",children:[a.jsx("p",{className:"text-xs font-medium text-bambu-gray uppercase tracking-wide",children:o("inventory.leftNozzle")}),a.jsx("div",{className:"space-y-2",children:S.map(A=>h(f,A))})]}),N.length>0&&a.jsxs("div",{className:"space-y-2",children:[a.jsx("p",{className:"text-xs font-medium text-bambu-gray uppercase tracking-wide",children:o("inventory.rightNozzle")}),a.jsx("div",{className:"space-y-2",children:N.map(A=>h(f,A))})]})]}):a.jsx("div",{className:"space-y-2",children:v.map(A=>h(f,A))}):a.jsx("p",{className:"text-sm text-bambu-gray italic py-2",children:o("inventory.printerOffline")})})]},f.id)})}),n.size>0&&a.jsx("div",{className:"p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:a.jsxs("p",{className:"text-sm text-white",children:[a.jsx("span",{className:"font-semibold",children:n.size})," ",o("inventory.profilesSelected")]})})]})}function AJe(t){const e=new Date(t);return e.toLocaleDateString("en-GB",{day:"2-digit",month:"2-digit",year:"2-digit"})+" "+e.toLocaleTimeString("en-GB",{hour:"2-digit",minute:"2-digit"})}const TJe={completed:"text-bambu-green",failed:"text-red-400",aborted:"text-yellow-400"};function EJe({spoolId:t}){const{t:e}=Vt(),n=tn(),{showToast:r}=mn(),{data:i,isLoading:s}=tt({queryKey:["spool-usage",t],queryFn:()=>se.getSpoolUsageHistory(t)}),o=lt({mutationFn:()=>se.clearSpoolUsageHistory(t),onSuccess:()=>{n.invalidateQueries({queryKey:["spool-usage",t]}),r(e("inventory.historyCleared"),"success")}});return s?a.jsx("div",{className:"flex justify-center py-4",children:a.jsx(at,{className:"w-5 h-5 animate-spin text-bambu-green"})}):!i||i.length===0?a.jsxs("div",{className:"text-center py-4 text-bambu-gray text-sm",children:[a.jsx(Qn,{className:"w-5 h-5 mx-auto mb-2 opacity-50"}),e("inventory.noUsageHistory")]}):a.jsxs("div",{className:"space-y-2",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsx("h4",{className:"text-sm font-medium text-white",children:e("inventory.usageHistory")}),a.jsxs(Me,{variant:"ghost",size:"sm",onClick:()=>o.mutate(),disabled:o.isPending,className:"text-xs text-bambu-gray hover:text-red-400",children:[a.jsx(en,{className:"w-3 h-3 mr-1"}),e("inventory.clearHistory")]})]}),a.jsx("div",{className:"max-h-48 overflow-y-auto space-y-1",children:i.map(l=>a.jsxs("div",{className:"flex items-center justify-between p-2 rounded bg-bambu-dark/50 text-xs",children:[a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("span",{className:"text-bambu-gray",children:AJe(l.created_at)}),l.print_name&&a.jsx("span",{className:"text-white ml-2 truncate",title:l.print_name,children:l.print_name})]}),a.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0 ml-2",children:[a.jsxs("span",{className:"text-white font-medium",children:[l.weight_used.toFixed(1),"g"]}),a.jsxs("span",{className:"text-bambu-gray",children:["(",l.percent_used,"%)"]}),a.jsx("span",{className:TJe[l.status]||"text-bambu-gray",children:l.status})]})]},l.id))})]})}function PJe({isOpen:t,onClose:e,spool:n,printersWithCalibrations:r=[]}){const{t:i}=Vt(),s=tn(),{showToast:o}=mn(),l=!!n,[c,u]=w.useState(dK),[d,h]=w.useState({}),[f,p]=w.useState("filament"),[x,v]=w.useState(!1),[b,y]=w.useState(!1),[S,N]=w.useState(!1),[A,M]=w.useState([]),[T,k]=w.useState(""),[j,E]=w.useState([]),[P,U]=w.useState([]),[L,B]=w.useState([]),[R,V]=w.useState([]),[q,I]=w.useState([]),[K,Y]=w.useState(new Set),[le,H]=w.useState(new Set),X=r.length>0?r:q,te=w.useMemo(()=>K.size,[K]);w.useEffect(()=>{V(wJe())},[]),w.useEffect(()=>{t&&((async()=>{N(!0);try{const W=await se.getCloudStatus();if(y(W.is_authenticated),W.is_authenticated){const ue=await se.getFilamentPresets();M(ue)}}catch(W){console.error("Failed to fetch cloud presets:",W),y(!1)}finally{N(!1)}})(),se.getSpoolCatalog().then(E).catch(console.error),se.getColorCatalog().then(B).catch(console.error),se.getLocalPresets().then(W=>U(W.filament)).catch(console.error),r.length===0&&(async()=>{try{const W=await se.getPrinters(),ue=await Promise.all(W.map(oe=>se.getPrinterStatus(oe.id).catch(()=>null))),Z=[];for(let oe=0;oe({cali_idx:D.slot_id,filament_id:D.filament_id,setting_id:D.setting_id||"",name:D.name,k_value:parseFloat(D.k_value)||0,n_coef:parseFloat(D.n_coef)||0,extruder_id:D.extruder_id,nozzle_diameter:D.nozzle_diameter}))}catch{}Z.push({printer:{...Q,connected:be},calibrations:ee})}I(Z)}catch(W){console.error("Failed to fetch printer calibrations:",W)}})())},[t,r.length]);const J=w.useMemo(()=>yJe(A,new Set,P),[A,P]),Se=w.useMemo(()=>bJe(A,P),[A,P]),fe=w.useMemo(()=>vJe(c.slicer_filament,J),[c.slicer_filament,J]);w.useEffect(()=>{if(t){if(n)if(u({material:n.material||"",subtype:n.subtype||"",brand:n.brand||"",color_name:n.color_name||"",rgba:n.rgba||"808080FF",label_weight:n.label_weight||1e3,core_weight:n.core_weight||250,weight_used:n.weight_used||0,slicer_filament:n.slicer_filament||"",note:n.note||""}),k(n.slicer_filament_name||n.slicer_filament||""),n.k_profiles&&n.k_profiles.length>0){const $=new Set;for(const W of n.k_profiles)W.cali_idx!==null&&W.cali_idx!==void 0&&$.add(`${W.printer_id}:${W.cali_idx}:${W.extruder??"null"}`);Y($)}else Y(new Set);else u(dK),k(""),Y(new Set);h({}),p("filament"),v(!1)}},[t,n]),w.useEffect(()=>{t&&X.length>0&&H(new Set(X.map($=>String($.printer.id))))},[t,X]);const me=($,W)=>{u(ue=>({...ue,[$]:W})),$==="weight_used"&&v(!0),d[$]&&h(ue=>({...ue,[$]:void 0}))},De=$=>{V(W=>_Je($,W))},de=lt({mutationFn:$=>se.createSpool($),onSuccess:async $=>{K.size>0&&$?.id&&await Ne($.id),await s.invalidateQueries({queryKey:["inventory-spools"]}),o(i("inventory.spoolCreated"),"success"),e()},onError:$=>{o($.message,"error")}}),ce=lt({mutationFn:$=>se.updateSpool(n.id,$),onSuccess:async()=>{n?.id&&await Ne(n.id),await s.invalidateQueries({queryKey:["inventory-spools"]}),o(i("inventory.spoolUpdated"),"success"),e()},onError:$=>{o($.message,"error")}}),Ne=async $=>{if(K.size===0){try{await se.saveSpoolKProfiles($,[])}catch{}return}const W=[];for(const ue of K){const[Z,oe,Q]=ue.split(":"),he=parseInt(Z),be=parseInt(oe),ee=Q==="null"?0:parseInt(Q),z=X.find(D=>D.printer.id===he);if(z){const D=z.calibrations.find(G=>G.cali_idx===be);D&&W.push({printer_id:he,extruder:ee,nozzle_diameter:D.nozzle_diameter||"0.4",k_value:D.k_value,name:D.name||null,cali_idx:D.cali_idx,setting_id:D.setting_id||null})}}if(W.length>0)try{await se.saveSpoolKProfiles($,W)}catch(ue){console.error("Failed to save K-profiles:",ue)}};if(w.useEffect(()=>{if(!t)return;const $=W=>{W.key==="Escape"&&e()};return document.addEventListener("keydown",$),()=>document.removeEventListener("keydown",$)},[t,e]),!t)return null;const ie=()=>{const $=dJe(c);if(!$.isValid){h($.errors),($.errors.slicer_filament||$.errors.material)&&p("filament");return}const W=fe?.displayName||T||null,ue={material:c.material,subtype:c.subtype||null,brand:c.brand||null,color_name:c.color_name||null,rgba:c.rgba||null,label_weight:c.label_weight,core_weight:c.core_weight,slicer_filament:c.slicer_filament||null,slicer_filament_name:W,nozzle_temp_min:null,nozzle_temp_max:null,note:c.note||null};(!l||x)&&(ue.weight_used=c.weight_used),l?ce.mutate(ue):de.mutate(ue)},Le=de.isPending||ce.isPending;return a.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center",children:[a.jsx("div",{className:"absolute inset-0 bg-black/60 backdrop-blur-sm",onClick:e}),a.jsxs("div",{className:"relative w-full max-w-lg mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col",children:[a.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary flex-shrink-0",children:[a.jsx("h2",{className:"text-lg font-semibold text-white",children:i(l?"inventory.editSpool":"inventory.addSpool")}),a.jsx("button",{onClick:e,className:"p-1 text-bambu-gray hover:text-white rounded transition-colors",children:a.jsx(Ft,{className:"w-5 h-5"})})]}),a.jsxs("div",{className:"flex border-b border-bambu-dark-tertiary flex-shrink-0",children:[a.jsxs("button",{onClick:()=>p("filament"),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${f==="filament"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[a.jsx(iC,{className:"w-4 h-4"}),i("inventory.filamentInfoTab")]}),a.jsxs("button",{onClick:()=>p("pa-profile"),className:`flex-1 px-4 py-2.5 text-sm font-medium flex items-center justify-center gap-2 transition-colors ${f==="pa-profile"?"text-bambu-green border-b-2 border-bambu-green":"text-bambu-gray hover:text-white"}`,children:[a.jsx(_de,{className:"w-4 h-4"}),i("inventory.paProfileTab"),te>0&&a.jsx("span",{className:"text-xs px-1.5 py-0.5 rounded-full bg-bambu-green/20 text-bambu-green",children:te})]})]}),a.jsx("div",{className:"p-4 overflow-y-auto flex-1",style:{scrollbarGutter:"stable"},children:f==="filament"?a.jsxs("div",{className:"space-y-6",children:[a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:i("inventory.filamentInfo")}),a.jsx(SJe,{formData:c,updateField:me,cloudAuthenticated:b,loadingCloudPresets:S,presetInputValue:T,setPresetInputValue:k,selectedPresetOption:fe,filamentOptions:J,availableBrands:Se}),d.slicer_filament&&a.jsx("p",{className:"mt-1 text-xs text-red-400",children:d.slicer_filament}),d.material&&a.jsx("p",{className:"mt-1 text-xs text-red-400",children:d.material})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:i("inventory.color")}),a.jsx(NJe,{formData:c,updateField:me,recentColors:R,onColorUsed:De,catalogColors:L})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-sm font-semibold text-bambu-gray uppercase tracking-wide mb-3",children:i("inventory.additional")}),a.jsx(CJe,{formData:c,updateField:me,spoolCatalog:j})]}),l&&n&&a.jsx("div",{children:a.jsx(EJe,{spoolId:n.id})})]}):a.jsx(MJe,{formData:c,updateField:me,printersWithCalibrations:X,selectedProfiles:K,setSelectedProfiles:Y,expandedPrinters:le,setExpandedPrinters:H})}),a.jsxs("div",{className:"flex justify-end gap-2 p-4 border-t border-bambu-dark-tertiary flex-shrink-0",children:[a.jsx(Me,{variant:"secondary",onClick:e,children:i("common.cancel")}),a.jsx(Me,{onClick:ie,disabled:Le,children:Le?a.jsxs(a.Fragment,{children:[a.jsx(at,{className:"w-4 h-4 animate-spin"}),i("common.saving")]}):a.jsxs(a.Fragment,{children:[a.jsx(bs,{className:"w-4 h-4"}),i(l?"common.save":"inventory.addSpool")]})})]})]})]})}function jJe({isOpen:t,onClose:e,columns:n,defaultColumns:r,onSave:i}){const{t:s}=Vt(),[o,l]=w.useState(n),[c,u]=w.useState(null),d=w.useRef(null);if(w.useEffect(()=>{t&&l(n.map(A=>({...A})))},[t,n]),w.useEffect(()=>{if(!t)return;const A=M=>{M.key==="Escape"&&e()};return window.addEventListener("keydown",A),()=>window.removeEventListener("keydown",A)},[t,e]),!t)return null;const h=A=>{l(M=>M.map((T,k)=>k===A?{...T,visible:!T.visible}:T))},f=(A,M)=>{M<0||M>=o.length||l(T=>{const k=[...T],[j]=k.splice(A,1);return k.splice(M,0,j),k})},p=(A,M)=>{d.current=M,u(M),A.dataTransfer.effectAllowed="move"},x=(A,M)=>{A.preventDefault(),A.dataTransfer.dropEffect="move";const T=d.current;T!==null&&T!==M&&(f(T,M),d.current=M,u(M))},v=A=>{A.preventDefault()},b=()=>{d.current=null,u(null)},y=()=>{l(r.map(A=>({...A})))},S=()=>{i(o),e()},N=o.filter(A=>A.visible).length;return a.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:e,children:a.jsx(_t,{className:"w-full max-w-md max-h-[80vh] flex flex-col",onClick:A=>A.stopPropagation(),children:a.jsxs(Nt,{className:"p-6 flex flex-col min-h-0",children:[a.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:s("inventory.configureColumns")}),a.jsxs("p",{className:"text-sm text-bambu-gray mb-4",children:[s("inventory.configureColumnsDesc"),a.jsxs("span",{className:"ml-2 text-bambu-gray/60",children:["(",N," ",s("inventory.of")," ",o.length," ",s("inventory.visible"),")"]})]}),a.jsx("div",{className:"space-y-1 overflow-y-auto flex-1 min-h-0 pr-1",children:o.map((A,M)=>a.jsxs("div",{className:`flex items-center gap-2 p-2 rounded-lg border transition-colors ${c===M?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary bg-bambu-dark-tertiary/50"} ${A.visible?"":"opacity-50"}`,draggable:!0,onDragStart:T=>p(T,M),onDragOver:T=>x(T,M),onDrop:v,onDragEnd:b,children:[a.jsx("div",{className:"cursor-grab text-bambu-gray/50 hover:text-bambu-gray",children:a.jsx(Ph,{className:"w-4 h-4"})}),a.jsx("span",{className:"flex-1 font-medium text-sm text-white",children:A.label}),a.jsxs("div",{className:"flex items-center gap-0.5",children:[a.jsx("button",{onClick:()=>f(M,M-1),disabled:M===0,className:"p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed",title:s("inventory.moveUp"),children:a.jsx(ql,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>f(M,M+1),disabled:M===o.length-1,className:"p-1 rounded text-bambu-gray hover:bg-bambu-dark-secondary disabled:opacity-30 disabled:cursor-not-allowed",title:s("inventory.moveDown"),children:a.jsx(nr,{className:"w-4 h-4"})})]}),a.jsx("button",{onClick:()=>h(M),className:`p-1.5 rounded transition-colors ${A.visible?"text-bambu-green hover:bg-bambu-green/10":"text-bambu-gray/50 hover:bg-bambu-dark-secondary"}`,title:A.visible?s("inventory.hideColumn"):s("inventory.showColumn"),children:A.visible?a.jsx(Kl,{className:"w-4 h-4"}):a.jsx(gv,{className:"w-4 h-4"})})]},A.id))}),a.jsxs("div",{className:"flex items-center gap-3 mt-4 pt-4 border-t border-bambu-dark-tertiary",children:[a.jsxs(Me,{variant:"secondary",onClick:y,className:"mr-auto",children:[a.jsx(Do,{className:"w-4 h-4"}),s("inventory.reset")]}),a.jsx(Me,{variant:"secondary",onClick:e,children:s("inventory.cancel")}),a.jsx(Me,{onClick:S,children:s("inventory.applyChanges")})]})]})})})}const Zae="bambuddy-inventory-columns",S1=[{id:"id",label:"#",visible:!0},{id:"added_time",label:"Added",visible:!0},{id:"encode_time",label:"Encoded",visible:!1},{id:"last_used_time",label:"Last Used",visible:!1},{id:"rgba",label:"Color",visible:!0},{id:"material",label:"Material",visible:!0},{id:"subtype",label:"Subtype",visible:!0},{id:"color_name",label:"Color Name",visible:!1},{id:"brand",label:"Brand",visible:!0},{id:"slicer_filament",label:"Slicer Filament",visible:!1},{id:"location",label:"Location",visible:!0},{id:"label_weight",label:"Label",visible:!0},{id:"net",label:"Net",visible:!0},{id:"gross",label:"Gross",visible:!1},{id:"added_full",label:"Full",visible:!1},{id:"used",label:"Used",visible:!1},{id:"printed_total",label:"Printed Total",visible:!1},{id:"printed_since_weight",label:"Printed Since Weight",visible:!1},{id:"note",label:"Note",visible:!1},{id:"pa_k",label:"PA(K)",visible:!0},{id:"tag_id",label:"Tag ID",visible:!1},{id:"data_origin",label:"Data Origin",visible:!1},{id:"tag_type",label:"Linked Tag Type",visible:!1},{id:"remaining",label:"Remaining",visible:!0}];function DJe(){try{const t=localStorage.getItem(Zae);if(t){const e=JSON.parse(t),n=new Set(S1.map(o=>o.id)),r=new Set(e.map(o=>o.id)),i=e.filter(o=>n.has(o.id)),s=S1.filter(o=>!r.has(o.id));return[...i,...s]}}catch{}return S1.map(t=>({...t}))}function RJe(t){try{localStorage.setItem(Zae,JSON.stringify(t))}catch{}}function Fu(t,e=!1){return e&&t>=1e3?`${(t/1e3).toFixed(1)}kg`:`${Math.round(t)}g`}const FJe={PLA:"bg-green-500/20 text-green-400",ABS:"bg-red-500/20 text-red-400",PETG:"bg-blue-500/20 text-blue-400",TPU:"bg-purple-500/20 text-purple-400",ASA:"bg-orange-500/20 text-orange-400",PA:"bg-yellow-500/20 text-yellow-400",PC:"bg-cyan-500/20 text-cyan-400",PET:"bg-sky-500/20 text-sky-400"};function Kj(t){return t?new Date(t).toLocaleDateString("en-GB",{day:"2-digit",month:"2-digit",year:"2-digit"}):"-"}const LJe={id:()=>"#",added_time:()=>"Added",encode_time:()=>"Encoded",last_used_time:()=>"Last Used",rgba:t=>t("inventory.color"),material:t=>t("inventory.material"),subtype:t=>t("inventory.subtype"),color_name:t=>t("inventory.colorName"),brand:t=>t("inventory.brand"),slicer_filament:t=>t("inventory.slicerFilament"),location:()=>"Location",label_weight:t=>t("inventory.labelWeight"),net:t=>t("inventory.net"),gross:()=>"Gross",added_full:()=>"Full",used:t=>t("inventory.weightUsed"),printed_total:()=>"Printed Total",printed_since_weight:()=>"Printed Since Weight",note:t=>t("inventory.note"),pa_k:()=>"PA(K)",tag_id:()=>"Tag ID",data_origin:()=>"Data Origin",tag_type:()=>"Linked Tag Type",remaining:t=>t("inventory.remaining")},OJe={id:({spool:t})=>a.jsx("span",{className:"text-sm font-medium text-white",children:t.id}),added_time:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:Kj(t.created_at)}),encode_time:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:Kj(t.encode_time)}),last_used_time:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.last_used?Kj(t.last_used):"Never"}),rgba:({spool:t})=>a.jsx("div",{className:"flex items-center justify-center",children:a.jsx("span",{className:"w-5 h-5 rounded-full border border-white/20 flex-shrink-0",style:{backgroundColor:t.rgba?`#${t.rgba.substring(0,6)}`:"#808080"},title:t.rgba?`#${t.rgba.substring(0,6)}`:void 0})}),material:({spool:t})=>a.jsx("span",{className:"text-sm text-white",children:t.material}),subtype:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.subtype||"-"}),color_name:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:pne(t.color_name,t.rgba)||"-"}),brand:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.brand||"-"}),slicer_filament:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",title:t.slicer_filament||void 0,children:t.slicer_filament_name||t.slicer_filament||"-"}),location:({spool:t,assignmentMap:e})=>{const n=e[t.id];if(!n)return a.jsx("span",{className:"text-sm text-bambu-gray",children:"-"});const r=n.printer_name||`Printer ${n.printer_id}`,i=String.fromCharCode(65+n.ams_id),s=n.tray_id+1;return a.jsxs("span",{className:"inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-purple-500/20 text-purple-400",children:[r," ",i,s]})},label_weight:({spool:t})=>a.jsx("span",{className:"text-sm text-white",children:Fu(t.label_weight)}),net:({remaining:t})=>a.jsx("span",{className:"text-sm text-white",children:Fu(t)}),gross:({spool:t,remaining:e})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:Fu(e+t.core_weight)}),added_full:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.added_full==null?"-":t.added_full?"Yes":"No"}),used:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.weight_used>0?Fu(t.weight_used):"-"}),printed_total:()=>a.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"}),printed_since_weight:()=>a.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"}),note:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray max-w-[150px] truncate block",title:t.note||void 0,children:t.note||"-"}),pa_k:({spool:t})=>(t.k_profiles?.length??0)===0?a.jsx("span",{className:"text-sm text-bambu-gray",children:"-"}):a.jsx("span",{className:"inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium bg-bambu-green/20 text-bambu-green",children:"K"}),tag_id:({spool:t})=>{const e=t.tag_uid||t.tray_uuid;return e?a.jsx("span",{className:"text-sm text-bambu-gray font-mono",title:e,children:e.length>12?`${e.slice(0,6)}...${e.slice(-4)}`:e}):a.jsx("span",{className:"text-sm text-bambu-gray/50",children:"-"})},data_origin:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.data_origin||"-"}),tag_type:({spool:t})=>a.jsx("span",{className:"text-sm text-bambu-gray",children:t.tag_type||"-"}),remaining:({remaining:t,pct:e})=>a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("div",{className:"flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden",children:a.jsx("div",{className:`h-full rounded-full ${e>50?"bg-bambu-green":e>20?"bg-yellow-500":"bg-red-500"}`,style:{width:`${Math.min(e,100)}%`}})}),a.jsxs("span",{className:"text-xs text-bambu-gray min-w-[40px] text-right",children:[Math.round(t),"g"]})]})},Xj={id:t=>t.id,added_time:t=>t.created_at||"",encode_time:t=>t.encode_time||"",last_used_time:t=>t.last_used||"",material:t=>(t.material||"").toLowerCase(),subtype:t=>(t.subtype||"").toLowerCase(),color_name:t=>(t.color_name||"").toLowerCase(),brand:t=>(t.brand||"").toLowerCase(),slicer_filament:t=>(t.slicer_filament_name||t.slicer_filament||"").toLowerCase(),location:(t,e)=>{const n=e[t.id];return n?`${n.printer_name||""} ${String.fromCharCode(65+n.ams_id)}${n.tray_id+1}`:""},label_weight:t=>t.label_weight,net:t=>Math.max(0,t.label_weight-t.weight_used),gross:t=>Math.max(0,t.label_weight-t.weight_used)+t.core_weight,used:t=>t.weight_used,remaining:t=>t.label_weight>0?Math.max(0,t.label_weight-t.weight_used)/t.label_weight:0,note:t=>(t.note||"").toLowerCase(),data_origin:t=>(t.data_origin||"").toLowerCase(),tag_type:t=>(t.tag_type||"").toLowerCase()},pF="bambuddy-inventory-sort";function IJe(){try{const t=localStorage.getItem(pF);if(t)return JSON.parse(t)}catch{}return null}function zJe(t){try{t?localStorage.setItem(pF,JSON.stringify(t)):localStorage.removeItem(pF)}catch{}}function UJe(){const{t}=Vt(),e=tn(),{showToast:n}=mn(),[r,i]=w.useState(null),[s,o]=w.useState(null),[l,c]=w.useState("active"),[u,d]=w.useState("all"),[h,f]=w.useState(""),[p,x]=w.useState(""),[v,b]=w.useState(""),[y,S]=w.useState("table"),[N,A]=w.useState(IJe),[M,T]=w.useState(DJe),[k,j]=w.useState(!1),[E,P]=w.useState(0),[U,L]=w.useState(()=>{try{const Q=localStorage.getItem("bambuddy-inventory-pageSize");if(Q){const he=Number(Q);if([15,30,50,100,-1].includes(he))return he}}catch{}return 15}),{data:B,isLoading:R}=tt({queryKey:["inventory-spools"],queryFn:()=>se.getSpools(!0),refetchInterval:3e4}),{data:V}=tt({queryKey:["spool-assignments"],queryFn:()=>se.getAssignments(),refetchInterval:3e4}),q=lt({mutationFn:Q=>se.deleteSpool(Q),onSuccess:()=>{e.invalidateQueries({queryKey:["inventory-spools"]}),n(t("inventory.spoolDeleted"),"success")}}),I=lt({mutationFn:Q=>se.archiveSpool(Q),onSuccess:()=>{e.invalidateQueries({queryKey:["inventory-spools"]}),n(t("inventory.spoolArchived"),"success")}}),K=lt({mutationFn:Q=>se.restoreSpool(Q),onSuccess:()=>{e.invalidateQueries({queryKey:["inventory-spools"]}),n(t("inventory.spoolRestored"),"success")}}),Y=w.useMemo(()=>{if(!B)return null;let Q=0,he=0,be=0,ee=0;const z={};for(const D of B){if(D.archived_at)continue;ee++;const G=Math.max(0,D.label_weight-D.weight_used);Q+=G,he+=D.weight_used,(D.label_weight>0?G/D.label_weight*100:0)<20&&be++;const ve=D.material||"Unknown";z[ve]||(z[ve]={count:0,weight:0}),z[ve].count++,z[ve].weight+=G}return{totalWeight:Q,totalConsumed:he,lowStock:be,byMaterial:z,totalSpools:ee}},[B]),le=V?.length??0,H=w.useMemo(()=>{const Q={};for(const he of V||[])Q[he.spool_id]=he;return Q},[V]),X=w.useMemo(()=>Y?Object.entries(Y.byMaterial).sort((Q,he)=>he[1].weight-Q[1].weight).slice(0,4):[],[Y]),te=w.useMemo(()=>{let Q=B||[];if(l==="active"?Q=Q.filter(he=>!he.archived_at):Q=Q.filter(he=>!!he.archived_at),u==="used"?Q=Q.filter(he=>he.weight_used>0):u==="new"&&(Q=Q.filter(he=>he.weight_used===0)),h&&(Q=Q.filter(he=>he.material===h)),p&&(Q=Q.filter(he=>he.brand===p)),v){const he=v.toLowerCase();Q=Q.filter(be=>be.brand?.toLowerCase().includes(he)||be.material.toLowerCase().includes(he)||be.color_name?.toLowerCase().includes(he)||be.subtype?.toLowerCase().includes(he)||be.note?.toLowerCase().includes(he)||be.slicer_filament_name?.toLowerCase().includes(he))}return Q},[B,l,u,h,p,v]),J=()=>P(0),Se=[...new Set(B?.map(Q=>Q.material)||[])].sort(),fe=[...new Set(B?.map(Q=>Q.brand).filter(Boolean)||[])].sort(),me=l!=="active"||u!=="all"||!!h||!!p||!!v,De=Q=>{T(Q),RJe(Q)},de=w.useMemo(()=>M.filter(Q=>Q.visible).map(Q=>Q.id),[M]),ce=Q=>{Xj[Q]&&(A(he=>{let be;return he?.column===Q?be=he.direction==="asc"?{column:Q,direction:"desc"}:null:be={column:Q,direction:"asc"},zJe(be),be}),J())},Ne=w.useMemo(()=>{if(!N)return te;const Q=Xj[N.column];return Q?[...te].sort((be,ee)=>{const z=Q(be,H),D=Q(ee,H);return zD?N.direction==="asc"?1:-1:0}):te},[te,N,H]),ie=U===-1,Le=ie?Ne.length||1:U,$=Math.max(1,Math.ceil(Ne.length/Le)),W=ie?0:Math.min(E,$-1),ue=ie?Ne:Ne.slice(W*Le,(W+1)*Le),Z=Q=>{L(Q),P(0);try{localStorage.setItem("bambuddy-inventory-pageSize",String(Q))}catch{}},oe=()=>{c("active"),d("all"),f(""),x(""),b(""),J()};return a.jsxs("div",{className:"p-4 md:p-6 space-y-6",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx(ma,{className:"w-6 h-6 text-bambu-green"}),a.jsx("h1",{className:"text-2xl font-bold text-white",children:t("inventory.title")})]}),a.jsx("p",{className:"text-sm text-bambu-gray mt-1 ml-9",children:(t("inventory.noSpools").split(".")[0],"")})]}),a.jsxs(Me,{onClick:()=>i({spool:null}),children:[a.jsx(ar,{className:"w-4 h-4"}),t("inventory.addSpool")]})]}),Y&&!R&&a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3",children:[a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(ma,{className:"w-4 h-4 text-bambu-green"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.totalInventory")})]}),a.jsx("div",{className:"text-xl font-bold text-white",children:Fu(Y.totalWeight,!0)}),a.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:[Y.totalSpools," ",Y.totalSpools!==1?t("inventory.spools"):t("inventory.spool")]})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(zF,{className:"w-4 h-4 text-blue-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.totalConsumed")})]}),a.jsx("div",{className:"text-xl font-bold text-white",children:Fu(Y.totalConsumed,!0)}),a.jsx("div",{className:"text-xs text-bambu-gray mt-1",children:t("inventory.sinceTracking")})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(rs,{className:"w-4 h-4 text-green-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.byMaterial")})]}),a.jsx("div",{className:"flex flex-wrap gap-1.5 mt-1",children:X.map(([Q,he])=>a.jsxs("span",{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${FJe[Q]||"bg-bambu-dark-tertiary text-bambu-gray"}`,children:[Q," ",a.jsx("span",{className:"opacity-70",children:Fu(he.weight,!0)})]},Q))})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(Cr,{className:"w-4 h-4 text-purple-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.inPrinter")})]}),a.jsx("div",{className:"text-xl font-bold text-white",children:le}),a.jsx("div",{className:"text-xs text-bambu-gray mt-1",children:t("inventory.loadedInAms")})]}),a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-4",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[a.jsx(Cn,{className:"w-4 h-4 text-yellow-400"}),a.jsx("span",{className:"text-xs text-bambu-gray font-medium uppercase tracking-wide",children:t("inventory.lowStock")})]}),a.jsx("div",{className:`text-xl font-bold ${Y.lowStock>0?"text-yellow-400":"text-white"}`,children:Y.lowStock}),a.jsx("div",{className:"text-xs text-bambu-gray mt-1",children:t("inventory.lowStockThreshold")})]})]}),a.jsxs("div",{className:"flex flex-col sm:flex-row gap-3 items-start sm:items-center justify-between",children:[a.jsxs("div",{className:"relative flex-1 max-w-md",children:[a.jsx(Or,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray/50"}),a.jsx("input",{type:"text",value:v,onChange:Q=>{b(Q.target.value),J()},placeholder:t("inventory.search"),className:"w-full pl-10 pr-8 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green"}),v&&a.jsx("button",{onClick:()=>{b(""),J()},className:"absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[y==="table"&&a.jsxs("button",{onClick:()=>j(!0),className:"flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-bambu-gray border border-bambu-dark-tertiary rounded-lg hover:bg-bambu-dark-tertiary transition-colors",title:t("inventory.configureColumns"),children:[a.jsx(vhe,{className:"w-4 h-4"}),a.jsx("span",{className:"hidden sm:inline",children:t("inventory.columns")})]}),a.jsxs("div",{className:"flex bg-bambu-dark-primary border border-bambu-dark-tertiary rounded-lg overflow-hidden",children:[a.jsxs("button",{onClick:()=>S("table"),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${y==="table"?"bg-bambu-green text-white":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(npe,{className:"w-4 h-4"}),a.jsx("span",{className:"hidden sm:inline",children:t("inventory.table")})]}),a.jsxs("button",{onClick:()=>S("cards"),className:`flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium transition-colors ${y==="cards"?"bg-bambu-green text-white":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(xv,{className:"w-4 h-4"}),a.jsx("span",{className:"hidden sm:inline",children:t("inventory.cards")})]})]})]})]}),a.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[a.jsxs("div",{className:"flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[a.jsxs("button",{onClick:()=>{c("active"),J()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${l==="active"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(ma,{className:"w-3.5 h-3.5"}),t("inventory.active")]}),a.jsxs("button",{onClick:()=>{c("archived"),J()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${l==="archived"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(Rs,{className:"w-3.5 h-3.5"}),t("inventory.archived")]})]}),a.jsx("div",{className:"w-px h-5 bg-bambu-dark-tertiary"}),a.jsxs("div",{className:"flex items-center rounded-lg border border-bambu-dark-tertiary overflow-hidden",children:[a.jsx("button",{onClick:()=>{d("all"),J()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${u==="all"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:t("inventory.all")}),a.jsxs("button",{onClick:()=>{d("used"),J()},className:`flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors ${u==="used"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:[a.jsx(Qn,{className:"w-3.5 h-3.5"}),t("inventory.used")]}),a.jsx("button",{onClick:()=>{d("new"),J()},className:`px-3 py-1.5 text-xs font-medium transition-colors ${u==="new"?"bg-bambu-green/20 text-bambu-green":"text-bambu-gray hover:bg-bambu-dark-tertiary"}`,children:t("inventory.new")})]}),a.jsx("div",{className:"w-px h-5 bg-bambu-dark-tertiary"}),a.jsxs("select",{value:h,onChange:Q=>{f(Q.target.value),J()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${h?"bg-bambu-green/20 text-bambu-green border-bambu-green/30":"bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary"}`,children:[a.jsx("option",{value:"",children:t("inventory.material")}),Se.map(Q=>a.jsx("option",{value:Q,children:Q},Q))]}),a.jsxs("select",{value:p,onChange:Q=>{x(Q.target.value),J()},className:`px-3 py-1.5 rounded-lg border text-xs font-medium transition-colors cursor-pointer focus:outline-none ${p?"bg-bambu-green/20 text-bambu-green border-bambu-green/30":"bg-transparent text-bambu-gray border-bambu-dark-tertiary hover:bg-bambu-dark-tertiary"}`,children:[a.jsx("option",{value:"",children:t("inventory.brand")}),fe.map(Q=>a.jsx("option",{value:Q,children:Q},Q))]}),me&&a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"w-px h-5 bg-bambu-dark-tertiary"}),a.jsxs("button",{onClick:oe,className:"flex items-center gap-1 text-xs text-bambu-gray hover:text-bambu-green transition-colors",children:[a.jsx(Ft,{className:"w-3.5 h-3.5"}),t("inventory.clearFilters")]})]}),a.jsxs("span",{className:"ml-auto text-xs text-bambu-gray",children:[Ne.length," ",Ne.length!==1?t("inventory.spools"):t("inventory.spool")]})]}),R?a.jsx("div",{className:"flex justify-center py-16",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})}):y==="cards"?ue.length>0?a.jsxs(a.Fragment,{children:[a.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4",children:ue.map(Q=>{const he=Math.max(0,Q.label_weight-Q.weight_used),be=Q.label_weight>0?he/Q.label_weight*100:0,ee=Q.rgba?`#${Q.rgba.substring(0,6)}`:"#808080";return a.jsxs("div",{className:`bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary hover:border-bambu-green transition-colors cursor-pointer ${Q.archived_at?"opacity-50":""}`,onClick:()=>i({spool:Q}),children:[a.jsx("div",{className:"h-14 flex items-center justify-center",style:{backgroundColor:ee},children:a.jsx("span",{className:"bg-white/90 text-gray-800 px-3 py-0.5 rounded-full text-sm font-medium",children:pne(Q.color_name,Q.rgba)||"-"})}),a.jsxs("div",{className:"p-4 space-y-3",children:[a.jsxs("div",{className:"flex items-start justify-between gap-2",children:[a.jsxs("div",{children:[a.jsxs("h3",{className:"font-semibold text-white",children:[Q.material,Q.subtype?` ${Q.subtype}`:""]}),a.jsx("p",{className:"text-sm text-bambu-gray",children:Q.brand||"-"})]}),a.jsxs("span",{className:"text-xs font-mono text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded",children:["#",Q.id]})]}),a.jsxs("div",{children:[a.jsxs("div",{className:"flex justify-between text-xs text-bambu-gray mb-1",children:[a.jsx("span",{children:t("inventory.remaining")}),a.jsxs("span",{children:[Math.round(be),"%"]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("div",{className:"flex-1 h-2 bg-bambu-dark-tertiary rounded-full overflow-hidden",children:a.jsx("div",{className:`h-full rounded-full ${be>50?"bg-bambu-green":be>20?"bg-yellow-500":"bg-red-500"}`,style:{width:`${Math.min(be,100)}%`}})}),a.jsxs("span",{className:"text-xs text-bambu-gray min-w-[40px] text-right",children:[Math.round(he),"g"]})]})]}),a.jsxs("div",{className:"grid grid-cols-2 gap-2 text-xs",children:[a.jsxs("div",{children:[a.jsxs("span",{className:"text-bambu-gray/60",children:[t("inventory.labelWeight"),": "]}),a.jsx("span",{className:"text-bambu-gray",children:Fu(Q.label_weight)})]}),a.jsxs("div",{children:[a.jsxs("span",{className:"text-bambu-gray/60",children:[t("inventory.weightUsed"),": "]}),a.jsx("span",{className:"text-bambu-gray",children:Q.weight_used>0?Fu(Q.weight_used):"-"})]})]}),Q.note&&a.jsx("div",{className:"text-xs text-bambu-gray/60 pt-2 border-t border-bambu-dark-tertiary truncate",title:Q.note,children:Q.note})]})]},Q.id)})}),a.jsx(BJe,{pageIndex:W,pageSize:U,totalRows:Ne.length,totalPages:$,onPageChange:P,onPageSizeChange:Z,t})]}):a.jsx(mK,{hasFilters:me,onAddSpool:()=>i({spool:null}),t}):ue.length>0?a.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg overflow-hidden border border-bambu-dark-tertiary",children:[a.jsx("div",{className:"overflow-x-auto",children:a.jsxs("table",{className:"w-full",children:[a.jsx("thead",{children:a.jsxs("tr",{className:"border-b border-bambu-dark-tertiary bg-bambu-dark-tertiary/30",children:[de.map(Q=>{const he=!!Xj[Q],be=N?.column===Q;return a.jsx("th",{className:`text-left py-3 px-4 text-xs font-medium uppercase tracking-wide select-none ${Q==="remaining"?"min-w-[150px]":""} ${he?"cursor-pointer hover:text-bambu-green transition-colors":""} ${be?"text-bambu-green":"text-bambu-gray"}`,onClick:he?()=>ce(Q):void 0,children:a.jsxs("span",{className:"inline-flex items-center gap-1",children:[LJe[Q]?.(t)??Q,he&&(be?N.direction==="asc"?a.jsx(Ef,{className:"w-3 h-3"}):a.jsx(hb,{className:"w-3 h-3"}):a.jsx(DF,{className:"w-3 h-3 opacity-30"}))]})},Q)}),a.jsx("th",{className:"text-right py-3 px-4 text-xs font-medium text-bambu-gray uppercase tracking-wide",children:t("common.actions")})]})}),a.jsx("tbody",{children:ue.map(Q=>{const he=Math.max(0,Q.label_weight-Q.weight_used),be=Q.label_weight>0?he/Q.label_weight*100:0;return a.jsxs("tr",{className:`border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-tertiary/30 transition-colors cursor-pointer ${Q.archived_at?"opacity-50":""}`,onClick:()=>i({spool:Q}),children:[de.map(ee=>a.jsx("td",{className:"py-3 px-4",children:OJe[ee]?.({spool:Q,remaining:he,pct:be,assignmentMap:H})},ee)),a.jsx("td",{className:"py-3 px-4",children:a.jsxs("div",{className:"flex items-center justify-end gap-1",onClick:ee=>ee.stopPropagation(),children:[a.jsx("button",{onClick:()=>i({spool:Q}),className:"p-1.5 text-bambu-gray hover:text-white rounded transition-colors",title:t("inventory.editSpool"),children:a.jsx(rf,{className:"w-4 h-4"})}),Q.archived_at?a.jsx("button",{onClick:()=>K.mutate(Q.id),className:"p-1.5 text-bambu-gray hover:text-bambu-green rounded transition-colors",title:t("inventory.restore"),children:a.jsx(Do,{className:"w-4 h-4"})}):a.jsx("button",{onClick:()=>o({type:"archive",spoolId:Q.id}),className:"p-1.5 text-bambu-gray hover:text-yellow-400 rounded transition-colors",title:t("inventory.archive"),children:a.jsx(Rs,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>o({type:"delete",spoolId:Q.id}),className:"p-1.5 text-bambu-gray hover:text-red-400 rounded transition-colors",title:t("common.delete"),children:a.jsx(en,{className:"w-4 h-4"})})]})})]},Q.id)})})]})}),a.jsxs("div",{className:"flex items-center justify-between px-4 py-3 bg-bambu-dark-tertiary/50 border-t border-bambu-dark-tertiary text-sm",children:[a.jsx("span",{className:"text-bambu-gray",children:ie?`${Ne.length} ${Ne.length!==1?t("inventory.spools"):t("inventory.spool")}`:a.jsxs(a.Fragment,{children:[t("inventory.showing")," ",W*Le+1," ",t("inventory.to")," ",Math.min((W+1)*Le,Ne.length)," ",t("inventory.of")," ",Ne.length," ",t("inventory.spools")]})}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-bambu-gray",children:t("inventory.show")}),a.jsxs("select",{value:U,onChange:Q=>Z(Number(Q.target.value)),className:"px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green",children:[[15,30,50,100].map(Q=>a.jsx("option",{value:Q,children:Q},Q)),a.jsx("option",{value:-1,children:t("inventory.all")})]}),!ie&&a.jsxs(a.Fragment,{children:[a.jsx("button",{onClick:()=>P(0),disabled:W===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",title:"First page",children:a.jsx(lX,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>P(Q=>Math.max(0,Q-1)),disabled:W===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(Gl,{className:"w-4 h-4"})}),a.jsxs("span",{className:"text-bambu-gray px-2 whitespace-nowrap",children:[t("inventory.page")," ",W+1," ",t("inventory.of")," ",$]}),a.jsx("button",{onClick:()=>P(Q=>Math.min($-1,Q+1)),disabled:W>=$-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(fa,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>P($-1),disabled:W>=$-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",title:"Last page",children:a.jsx(cX,{className:"w-4 h-4"})})]})]})]})]}):a.jsx(mK,{hasFilters:me,onAddSpool:()=>i({spool:null}),t}),r!==null&&a.jsx(PJe,{isOpen:!0,onClose:()=>i(null),spool:r.spool}),s&&a.jsx(kn,{title:s.type==="delete"?t("common.delete"):t("inventory.archive"),message:s.type==="delete"?t("inventory.deleteConfirm"):t("inventory.archiveConfirm"),confirmText:s.type==="delete"?t("common.delete"):t("inventory.archive"),variant:s.type==="delete"?"danger":"warning",onConfirm:()=>{s.type==="delete"?q.mutate(s.spoolId):I.mutate(s.spoolId),o(null)},onCancel:()=>o(null)}),a.jsx(jJe,{isOpen:k,onClose:()=>j(!1),columns:M,defaultColumns:S1,onSave:De})]})}function BJe({pageIndex:t,pageSize:e,totalRows:n,totalPages:r,onPageChange:i,onPageSizeChange:s,t:o}){const l=e===-1;if(r<=1&&!l)return null;const c=l?n||1:e;return a.jsxs("div",{className:"flex items-center justify-between pt-2 text-sm",children:[a.jsx("span",{className:"text-bambu-gray",children:l?`${n} ${o(n!==1?"inventory.spools":"inventory.spool")}`:a.jsxs(a.Fragment,{children:[o("inventory.showing")," ",t*c+1," ",o("inventory.to")," ",Math.min((t+1)*c,n)," ",o("inventory.of")," ",n," ",o("inventory.spools")]})}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"text-bambu-gray",children:o("inventory.show")}),a.jsxs("select",{value:e,onChange:u=>s(Number(u.target.value)),className:"px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green",children:[[15,30,50,100].map(u=>a.jsx("option",{value:u,children:u},u)),a.jsx("option",{value:-1,children:o("inventory.all")})]}),!l&&a.jsxs(a.Fragment,{children:[a.jsx("button",{onClick:()=>i(0),disabled:t===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(lX,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>i(Math.max(0,t-1)),disabled:t===0,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(Gl,{className:"w-4 h-4"})}),a.jsxs("span",{className:"text-bambu-gray px-2 whitespace-nowrap",children:[o("inventory.page")," ",t+1," ",o("inventory.of")," ",r]}),a.jsx("button",{onClick:()=>i(Math.min(r-1,t+1)),disabled:t>=r-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(fa,{className:"w-4 h-4"})}),a.jsx("button",{onClick:()=>i(r-1),disabled:t>=r-1,className:"p-1.5 rounded text-bambu-gray hover:text-white disabled:opacity-30 disabled:cursor-not-allowed transition-colors",children:a.jsx(cX,{className:"w-4 h-4"})})]})]})]})}function mK({hasFilters:t,onAddSpool:e,t:n}){return a.jsxs("div",{className:"flex flex-col items-center justify-center py-16 px-4",children:[a.jsxs("div",{className:"relative mb-6",children:[a.jsx("div",{className:"absolute inset-0 -m-4 bg-bambu-green/5 rounded-full blur-2xl"}),a.jsxs("div",{className:"relative flex items-center justify-center w-24 h-24 rounded-2xl bg-gradient-to-br from-bambu-dark-secondary to-bambu-dark-tertiary border border-bambu-dark-tertiary shadow-lg",children:[a.jsx("div",{className:"absolute -top-1 -right-1 w-3 h-3 rounded-full bg-bambu-green/30"}),a.jsx("div",{className:"absolute -bottom-2 -left-2 w-2 h-2 rounded-full bg-bambu-green/20"}),t?a.jsx(Or,{className:"w-10 h-10 text-bambu-gray/40",strokeWidth:1.5}):a.jsxs("div",{className:"relative",children:[a.jsx("div",{className:"w-14 h-14 rounded-full border-4 border-bambu-gray/20 flex items-center justify-center",children:a.jsx("div",{className:"w-6 h-6 rounded-full bg-bambu-gray/10 border-2 border-bambu-gray/20"})}),a.jsx("div",{className:"absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-bambu-green flex items-center justify-center shadow-md",children:a.jsx("span",{className:"text-white text-lg font-bold leading-none",children:"+"})})]})]})]}),a.jsx("h3",{className:"text-lg font-semibold text-white mb-2 text-center",children:t?n("inventory.noSpoolsMatch"):n("inventory.noSpools").split(".")[0]}),a.jsx("p",{className:"text-sm text-bambu-gray text-center max-w-sm mb-6",children:n(t?"inventory.noSpoolsMatchDesc":"inventory.noSpools")}),!t&&a.jsxs(Me,{onClick:e,children:[a.jsx(ma,{className:"w-4 h-4"}),n("inventory.addSpool")]})]})}const fK=["DEBUG","INFO","WARNING","ERROR"],Yj={DEBUG:"text-gray-400",INFO:"text-blue-400",WARNING:"text-yellow-400",ERROR:"text-red-400"},HJe={DEBUG:Zk,INFO:Zs,WARNING:Cn,ERROR:Bi};function $Je(){const t=tn(),[e,n]=w.useState(!0),[r,i]=w.useState(new Set),[s,o]=w.useState(""),[l,c]=w.useState("ALL"),[u,d]=w.useState(!1),[h,f]=w.useState(!1),p=w.useRef(null),{data:x,isLoading:v,refetch:b}=tt({queryKey:["application-logs",l,s],queryFn:()=>Zg.getLogs({limit:200,level:l==="ALL"?void 0:l,search:s||void 0}),refetchInterval:h?2e3:!1,enabled:u});w.useEffect(()=>{u||f(!1)},[u]);const y=lt({mutationFn:()=>Zg.clearLogs(),onSuccess:()=>{t.invalidateQueries({queryKey:["application-logs"]})}});w.useEffect(()=>{e&&p.current&&x?.entries&&(p.current.scrollTop=p.current.scrollHeight)},[x?.entries,e]);const S=k=>{i(j=>{const E=new Set(j);return E.has(k)?E.delete(k):E.add(k),E})},N=k=>{const j=k.split(" ");return j.length>=2?j[1]:k},A=w.useMemo(()=>x?.entries??[],[x?.entries]),M=w.useMemo(()=>[...A].reverse(),[A]),T=({level:k})=>{const j=HJe[k]||Zs;return a.jsx(j,{className:`w-3.5 h-3.5 ${Yj[k]||"text-gray-400"}`})};return a.jsxs("div",{className:"bg-bambu-dark rounded-lg overflow-hidden",children:[a.jsxs("button",{onClick:()=>d(!u),className:"w-full flex items-center justify-between p-4 hover:bg-bambu-dark-tertiary/50 transition-colors",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:`p-2 rounded-lg ${h?"bg-bambu-green/20 text-bambu-green":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:a.jsx(Zk,{className:"w-5 h-5"})}),a.jsxs("div",{className:"text-left",children:[a.jsx("p",{className:"font-medium text-white",children:"Application Logs"}),a.jsx("p",{className:"text-sm text-bambu-gray",children:h?`Live streaming - ${x?.filtered_count??0} entries`:"View and filter application logs"})]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[h&&a.jsxs("span",{className:"flex items-center gap-1.5 px-2 py-1 bg-bambu-green/20 rounded text-bambu-green text-xs",children:[a.jsx("span",{className:"w-1.5 h-1.5 bg-bambu-green rounded-full animate-pulse"}),"Live"]}),u?a.jsx(ql,{className:"w-5 h-5 text-bambu-gray"}):a.jsx(nr,{className:"w-5 h-5 text-bambu-gray"})]})]}),u&&a.jsxs("div",{className:"border-t border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex flex-col gap-2 p-4 border-b border-bambu-dark-tertiary",children:[a.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[h?a.jsxs("button",{onClick:k=>{k.stopPropagation(),f(!1)},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-red-500/20 text-red-400 hover:bg-red-500/30 rounded transition-colors",children:[a.jsx(gl,{className:"w-4 h-4"}),"Stop"]}):a.jsxs("button",{onClick:k=>{k.stopPropagation(),f(!0),b()},className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30 rounded transition-colors",children:[a.jsx(jo,{className:"w-4 h-4"}),"Start"]}),a.jsxs("button",{onClick:()=>y.mutate(),disabled:y.isPending||A.length===0,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:[a.jsx(en,{className:"w-4 h-4"}),"Clear"]}),a.jsx("button",{onClick:()=>b(),disabled:v,className:"flex items-center gap-1.5 px-3 py-1.5 text-sm bg-bambu-dark-tertiary text-bambu-gray hover:text-white hover:bg-bambu-dark-secondary rounded transition-colors disabled:opacity-50",children:a.jsx(kr,{className:`w-4 h-4 ${v?"animate-spin":""}`})}),a.jsx("div",{className:"flex-1"}),a.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[a.jsx("input",{type:"checkbox",checked:e,onChange:k=>n(k.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark-tertiary"}),"Auto-scroll"]}),a.jsxs("span",{className:"text-sm text-bambu-gray",children:[x?.filtered_count??0,"/",x?.total_in_file??0]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs("div",{className:"relative flex-1",children:[a.jsx(Or,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),a.jsx("input",{type:"text",placeholder:"Search message or logger name...",value:s,onChange:k=>o(k.target.value),className:"w-full pl-8 pr-8 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),s&&a.jsx("button",{onClick:()=>o(""),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:a.jsx(Ft,{className:"w-4 h-4"})})]}),a.jsxs("div",{className:"flex items-center gap-1 bg-bambu-dark-secondary rounded border border-bambu-dark-tertiary",children:[a.jsx("button",{onClick:()=>c("ALL"),className:`px-2 py-1.5 text-xs rounded-l transition-colors ${l==="ALL"?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:"All"}),fK.map((k,j)=>a.jsx("button",{onClick:()=>c(k),className:`px-2 py-1.5 text-xs transition-colors flex items-center gap-1 ${j===fK.length-1?"rounded-r":""} ${l===k?`${Yj[k]} bg-bambu-dark-tertiary`:"text-bambu-gray hover:text-white"}`,children:k},k))]})]})]}),a.jsx("div",{ref:p,className:"overflow-auto font-mono text-xs bg-black min-h-[300px] max-h-[500px]",children:A.length===0?a.jsxs("div",{className:"flex flex-col items-center justify-center h-[300px] text-bambu-gray",children:[a.jsx("p",{className:"mb-2",children:"No log entries found"}),a.jsx("p",{className:"text-sm",children:"Log file may be empty or cleared"})]}):a.jsx("div",{className:"divide-y divide-bambu-dark-tertiary/30",children:M.map((k,j)=>{const E=r.has(j),P=k.message.includes(` `);return a.jsx("div",{className:`p-2 cursor-pointer hover:bg-bambu-dark-secondary/50 transition-colors ${E?"bg-bambu-dark-secondary/30":""}`,onClick:()=>P&&S(j),children:a.jsxs("div",{className:"flex items-start gap-2",children:[a.jsx("span",{className:"text-bambu-gray/70 shrink-0 w-20",children:N(k.timestamp)}),a.jsx("span",{className:"shrink-0",children:a.jsx(T,{level:k.level})}),a.jsxs("span",{className:"text-purple-400/80 shrink-0 max-w-[200px] truncate",title:k.logger_name,children:["[",k.logger_name,"]"]}),a.jsx("span",{className:`flex-1 ${Yj[k.level]||"text-white/80"} ${!E&&P?"truncate":""}`,children:E?a.jsx("pre",{className:"whitespace-pre-wrap break-all",children:k.message}):k.message.split(` `)[0]}),P&&a.jsx("span",{className:"text-bambu-gray/50 shrink-0",children:E?a.jsx(ql,{className:"w-3.5 h-3.5"}):a.jsx(nr,{className:"w-3.5 h-3.5"})})]})},j)})})}),a.jsx("div",{className:"flex items-center justify-between p-3 border-t border-bambu-dark-tertiary text-sm text-bambu-gray",children:h?a.jsxs("span",{className:"flex items-center gap-2",children:[a.jsx("span",{className:"w-2 h-2 bg-green-500 rounded-full animate-pulse"}),"Auto-refreshing every 2 seconds"]}):a.jsx("span",{children:"Click Start to enable live log streaming"})})]})]})}function VJe(t){return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(1)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`}function Mi({icon:t,label:e,value:n,subValue:r,color:i="text-bambu-green"}){return a.jsxs("div",{className:"flex items-start gap-3 p-4 bg-bambu-dark rounded-lg",children:[a.jsx("div",{className:`p-2 rounded-lg bg-bambu-dark-tertiary ${i}`,children:a.jsx(t,{className:"w-5 h-5"})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm text-bambu-gray",children:e}),a.jsx("p",{className:"text-lg font-semibold text-white truncate",children:n}),r&&a.jsx("p",{className:"text-xs text-bambu-gray mt-0.5",children:r})]})]})}function pK({percent:t,color:e="bg-bambu-green"}){return a.jsx("div",{className:"w-full h-2 bg-bambu-dark rounded-full overflow-hidden",children:a.jsx("div",{className:`h-full ${e} transition-all duration-300`,style:{width:`${Math.min(100,t)}%`}})})}function ah({title:t,icon:e,children:n}){return a.jsxs(_t,{className:"p-6",children:[a.jsxs("div",{className:"flex items-center gap-2 mb-4",children:[a.jsx(e,{className:"w-5 h-5 text-bambu-green"}),a.jsx("h2",{className:"text-lg font-semibold text-white",children:t})]}),n]})}function GJe(){const{t}=Vt(),e=tn(),[n,r]=w.useState(null),[i,s]=w.useState(!1),[o,l]=w.useState(!1),{data:c,isLoading:u,refetch:d,isFetching:h}=tt({queryKey:["systemInfo"],queryFn:se.getSystemInfo,refetchInterval:3e4}),{data:f}=tt({queryKey:["debugLogging"],queryFn:Zg.getDebugLoggingState,staleTime:10*1e3,refetchInterval:10*1e3}),{data:p}=tt({queryKey:["settings"],queryFn:se.getSettings}),{data:x}=tt({queryKey:["library-stats"],queryFn:se.getLibraryStats}),v=p?.time_format||"system",b=async()=>{l(!0);try{const A=await Zg.setDebugLogging(!f?.enabled);e.setQueryData(["debugLogging"],A)}catch(A){console.error("Failed to toggle debug logging:",A)}finally{l(!1)}},y=async()=>{r(null),s(!0);try{await Zg.downloadSupportBundle()}catch(A){r(A instanceof Error?A.message:"Failed to download support bundle")}finally{s(!1)}};if(u)return a.jsx("div",{className:"flex items-center justify-center h-64",children:a.jsx(at,{className:"w-8 h-8 text-bambu-green animate-spin"})});if(!c)return a.jsx("div",{className:"p-6 text-center text-bambu-gray",children:t("system.failedToLoad","Failed to load system information")});const S=c.storage.disk_percent_used>90?"bg-red-500":c.storage.disk_percent_used>75?"bg-yellow-500":"bg-bambu-green",N=c.memory.percent_used>90?"bg-red-500":c.memory.percent_used>75?"bg-yellow-500":"bg-bambu-green";return a.jsxs("div",{className:"p-6 space-y-6",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{children:[a.jsx("h1",{className:"text-2xl font-bold text-white",children:t("system.title","System Information")}),a.jsx("p",{className:"text-bambu-gray mt-1",children:t("system.subtitle","Monitor system resources and database statistics")})]}),a.jsxs("button",{onClick:()=>d(),disabled:h,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:[a.jsx(kr,{className:`w-4 h-4 ${h?"animate-spin":""}`}),t("common.refresh","Refresh")]})]}),a.jsx(ah,{title:t("system.application","Application"),icon:$m,children:a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[a.jsx(Mi,{icon:$m,label:t("system.version","Version"),value:`v${c.app.version}`}),a.jsx(Mi,{icon:Qn,label:t("system.uptime","System Uptime"),value:c.system.uptime_formatted}),a.jsx(Mi,{icon:$m,label:t("system.hostname","Hostname"),value:c.system.hostname})]})}),a.jsx(ah,{title:t("support.title","Support & Troubleshooting"),icon:xme,children:a.jsxs("div",{className:"space-y-4",children:[a.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.")}),a.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:`p-2 rounded-lg ${f?.enabled?"bg-amber-500/20 text-amber-500":"bg-bambu-dark-tertiary text-bambu-gray"}`,children:a.jsx(Zk,{className:"w-5 h-5"})}),a.jsxs("div",{children:[a.jsx("p",{className:"font-medium text-white",children:t("support.debugLogging","Debug Logging")}),a.jsxs("p",{className:"text-sm text-bambu-gray",children:[f?.enabled?t("support.debugLoggingEnabled","Capturing detailed logs"):t("support.debugLoggingDisabled","Normal logging level"),f?.enabled&&f.duration_seconds!==null&&a.jsxs("span",{className:"text-amber-400 ml-2",children:["(",Math.floor(f.duration_seconds/60),"m ",f.duration_seconds%60,"s)"]})]})]})]}),a.jsxs("button",{onClick:b,disabled:o,className:`px-4 py-2 rounded-lg font-medium transition-colors flex items-center gap-2 ${f?.enabled?"bg-amber-500/20 text-amber-400 hover:bg-amber-500/30":"bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30"} disabled:opacity-50`,children:[o&&a.jsx(at,{className:"w-4 h-4 animate-spin"}),f?.enabled?t("support.disableDebug","Disable"):t("support.enableDebug","Enable")]})]}),a.jsxs("div",{className:"flex items-center justify-between p-4 bg-bambu-dark rounded-lg",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-green",children:a.jsx(di,{className:"w-5 h-5"})}),a.jsxs("div",{children:[a.jsx("p",{className:"font-medium text-white",children:t("support.supportBundle","Support Bundle")}),a.jsx("p",{className:"text-sm text-bambu-gray",children:t("support.supportBundleDescription","Download system info and logs as a ZIP file")})]})]}),a.jsxs("button",{onClick:y,disabled:i||!f?.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:f?.enabled?void 0:t("support.enableDebugFirst","Enable debug logging first"),children:[i&&a.jsx(at,{className:"w-4 h-4 animate-spin"}),t("common.download","Download")]})]}),n&&a.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/30 rounded-lg text-red-400 text-sm",children:n}),!f?.enabled&&a.jsx("div",{className:"p-4 bg-bambu-dark-tertiary/50 rounded-lg",children:a.jsxs("p",{className:"text-sm text-bambu-gray",children:[a.jsx("span",{className:"text-amber-400 font-medium",children:t("support.instructions","To report an issue:")}),a.jsx("br",{}),"1. ",t("support.step1","Enable debug logging"),a.jsx("br",{}),"2. ",t("support.step2","Reproduce the issue"),a.jsx("br",{}),"3. ",t("support.step3","Download the support bundle"),a.jsx("br",{}),"4. ",t("support.step4","Attach the ZIP file to your issue report")]})}),a.jsxs("div",{className:"p-4 bg-bambu-dark rounded-lg space-y-3",children:[a.jsx("p",{className:"text-sm font-medium text-white",children:t("support.privacyTitle","What's in the support bundle?")}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 text-sm",children:[a.jsxs("div",{children:[a.jsx("p",{className:"text-bambu-green font-medium mb-1",children:t("support.collected","Collected:")}),a.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[a.jsxs("li",{children:["• ",t("support.collectItem1","App version and debug mode")]}),a.jsxs("li",{children:["• ",t("support.collectItem2","OS, architecture, Python version")]}),a.jsxs("li",{children:["• ",t("support.collectItem3","Database statistics (counts only)")]}),a.jsxs("li",{children:["• ",t("support.collectItem4","Printer models and nozzle counts")]}),a.jsxs("li",{children:["• ",t("support.collectItem5","Non-sensitive settings (themes, formats)")]}),a.jsxs("li",{children:["• ",t("support.collectItem6","Debug logs (sanitized)")]}),a.jsxs("li",{children:["• ",t("support.collectItem7","Printer connectivity and firmware versions")]}),a.jsxs("li",{children:["• ",t("support.collectItem8","Integration status (Spoolman, MQTT, HA)")]}),a.jsxs("li",{children:["• ",t("support.collectItem9","Network interfaces (subnets only)")]}),a.jsxs("li",{children:["• ",t("support.collectItem10","Python package versions")]}),a.jsxs("li",{children:["• ",t("support.collectItem11","Database health checks")]}),a.jsxs("li",{children:["• ",t("support.collectItem12","Docker environment details")]})]})]}),a.jsxs("div",{children:[a.jsx("p",{className:"text-red-400 font-medium mb-1",children:t("support.notCollected","NOT collected:")}),a.jsxs("ul",{className:"text-bambu-gray space-y-0.5",children:[a.jsxs("li",{children:["• ",t("support.notItem1","Printer names, IPs, serial numbers")]}),a.jsxs("li",{children:["• ",t("support.notItem2","Access codes and passwords")]}),a.jsxs("li",{children:["• ",t("support.notItem3","Email addresses")]}),a.jsxs("li",{children:["• ",t("support.notItem4","API keys and tokens")]}),a.jsxs("li",{children:["• ",t("support.notItem5","Webhook URLs")]}),a.jsxs("li",{children:["• ",t("support.notItem6","Your hostname or username")]})]})]})]}),a.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].")})]}),a.jsx($Je,{})]})}),a.jsxs(ah,{title:t("system.database","Database"),icon:Ac,children:[a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 mb-4",children:[a.jsx(Mi,{icon:Rs,label:t("system.totalArchives","Total Archives"),value:c.database.archives}),a.jsx(Mi,{icon:fb,label:t("system.completed","Completed"),value:c.database.archives_completed,color:"text-green-500"}),a.jsx(Mi,{icon:ha,label:t("system.failed","Failed"),value:c.database.archives_failed,color:"text-red-500"}),a.jsx(Mi,{icon:at,label:t("system.printing","Printing"),value:c.database.archives_printing,color:"text-yellow-500"})]}),a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[a.jsx(Mi,{icon:Cr,label:t("system.printers","Printers"),value:c.database.printers}),a.jsx(Mi,{icon:iC,label:t("system.filaments","Filaments"),value:c.database.filaments}),a.jsx(Mi,{icon:xs,label:t("system.projects","Projects"),value:c.database.projects}),a.jsx(Mi,{icon:dl,label:t("system.smartPlugs","Smart Plugs"),value:c.database.smart_plugs})]}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4 mt-4",children:[a.jsx(Mi,{icon:Qn,label:t("system.totalPrintTime","Total Print Time"),value:c.database.total_print_time_formatted}),a.jsx(Mi,{icon:Rs,label:t("system.totalFilament","Total Filament Used"),value:`${c.database.total_filament_kg} kg`,subValue:`${c.database.total_filament_grams.toLocaleString()} g`})]})]}),a.jsxs(ah,{title:t("system.connectedPrinters","Connected Printers"),icon:Cr,children:[a.jsxs("div",{className:"flex items-center gap-4 mb-4",children:[a.jsx("div",{className:"text-3xl font-bold text-bambu-green",children:c.printers.connected}),a.jsx("div",{className:"text-bambu-gray",children:t("system.ofTotal","of {{total}} printers connected",{total:c.printers.total})})]}),c.printers.connected_list.length>0?a.jsx("div",{className:"space-y-2",children:c.printers.connected_list.map(A=>a.jsxs("div",{className:"flex items-center justify-between p-3 bg-bambu-dark rounded-lg",children:[a.jsxs("div",{className:"flex items-center gap-3",children:[a.jsx("div",{className:"w-2 h-2 rounded-full bg-bambu-green"}),a.jsx("span",{className:"font-medium text-white",children:A.name})]}),a.jsxs("div",{className:"flex items-center gap-4 text-sm text-bambu-gray",children:[a.jsx("span",{children:A.model}),a.jsx("span",{className:`px-2 py-0.5 rounded ${A.state==="RUNNING"?"bg-bambu-green/20 text-bambu-green":A.state==="IDLE"?"bg-blue-500/20 text-blue-400":"bg-bambu-dark-tertiary"}`,children:A.state})]})]},A.id))}):a.jsx("p",{className:"text-bambu-gray",children:t("system.noPrintersConnected","No printers connected")})]}),a.jsx(ah,{title:t("system.storage","Storage"),icon:qb,children:a.jsxs("div",{className:"space-y-4",children:[a.jsxs("div",{children:[a.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[a.jsx("span",{className:"text-bambu-gray",children:t("system.diskUsage","Disk Usage")}),a.jsxs("span",{className:"text-white",children:[c.storage.disk_used_formatted," / ",c.storage.disk_total_formatted]})]}),a.jsx(pK,{percent:c.storage.disk_percent_used,color:S}),a.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),"%)"]})]}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[a.jsx(Mi,{icon:Rs,label:t("system.archiveStorage","Archive Storage"),value:c.storage.archive_size_formatted}),a.jsx(Mi,{icon:Ac,label:t("system.databaseSize","Database Size"),value:c.storage.database_size_formatted}),x&&a.jsx(Mi,{icon:Xl,label:t("system.fileManagerStorage","File Manager"),value:VJe(x.total_size_bytes),subValue:`${x.total_files} files, ${x.total_folders} folders`})]})]})}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[a.jsx(ah,{title:t("system.memory","Memory"),icon:Yme,children:a.jsx("div",{className:"space-y-4",children:a.jsxs("div",{children:[a.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[a.jsx("span",{className:"text-bambu-gray",children:t("system.memoryUsage","Memory Usage")}),a.jsxs("span",{className:"text-white",children:[c.memory.used_formatted," / ",c.memory.total_formatted]})]}),a.jsx(pK,{percent:c.memory.percent_used,color:N}),a.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:[c.memory.available_formatted," ",t("system.available","available")]})]})})}),a.jsx(ah,{title:t("system.cpu","CPU"),icon:h_,children:a.jsx("div",{className:"space-y-4",children:a.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[a.jsx(Mi,{icon:h_,label:t("system.cores","Cores"),value:c.cpu.count,subValue:`${c.cpu.count_logical} logical`}),a.jsx(Mi,{icon:h_,label:t("system.usage","Usage"),value:`${c.cpu.percent}%`})]})})})]}),a.jsx(ah,{title:t("system.systemDetails","System Details"),icon:$m,children:a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[a.jsx(Mi,{icon:$m,label:t("system.os","Operating System"),value:c.system.platform,subValue:c.system.platform_release}),a.jsx(Mi,{icon:h_,label:t("system.architecture","Architecture"),value:c.system.architecture}),a.jsx(Mi,{icon:$m,label:t("system.python","Python"),value:c.system.python_version}),a.jsx(Mi,{icon:Qn,label:t("system.bootTime","Boot Time"),value:Ff(c.system.boot_time,v)})]})})]})}function qJe(){const t=Hc(),{t:e}=Vt(),{login:n}=Ir(),{showToast:r}=mn(),{mode:i}=np(),[s,o]=w.useState(""),[l,c]=w.useState(""),[u,d]=w.useState(!1),[h,f]=w.useState(""),{data:p}=tt({queryKey:["advancedAuthStatus"],queryFn:()=>se.getAdvancedAuthStatus()}),x=lt({mutationFn:()=>n(s,l),onSuccess:()=>{r(e("login.loginSuccess")),t("/")},onError:S=>{r(S.message||e("login.loginFailed"),"error")}}),v=lt({mutationFn:S=>se.forgotPassword({email:S}),onSuccess:S=>{r(S.message,"success"),d(!1),f("")},onError:S=>{r(S.message,"error")}}),b=S=>{if(S.preventDefault(),!s||!l){r(e("login.enterCredentials"),"error");return}x.mutate()},y=S=>{if(S.preventDefault(),!h){r("Please enter your email address","error");return}v.mutate(h)};return a.jsxs("div",{className:"min-h-screen flex items-center justify-center bg-bambu-dark p-4",children:[a.jsxs("div",{className:"max-w-md w-full space-y-8 p-8 bg-gradient-to-br from-bambu-card to-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary shadow-lg",children:[a.jsxs("div",{className:"text-center",children:[a.jsx("div",{className:"flex items-center justify-center mb-6",children:a.jsx("img",{src:i==="dark"?"/img/bambuddy_logo_dark_transparent.png":"/img/bambuddy_logo_light.png",alt:"Bambuddy",className:"h-16"})}),a.jsx("h2",{className:"text-3xl font-bold text-white",children:e("login.title")}),a.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:e("login.subtitle")})]}),a.jsxs("form",{className:"mt-8 space-y-6",onSubmit:b,children:[a.jsxs("div",{className:"space-y-4",children:[a.jsxs("div",{children:[a.jsx("label",{htmlFor:"username",className:"block text-sm font-medium text-white mb-2",children:p?.advanced_auth_enabled?e("login.usernameOrEmail")||"Username or Email":e("login.username")}),a.jsx("input",{id:"username",type:"text",required:!0,value:s,onChange:S=>o(S.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:p?.advanced_auth_enabled?e("login.usernameOrEmailPlaceholder")||"Enter your username or email":e("login.usernamePlaceholder"),autoComplete:"username"})]}),a.jsxs("div",{children:[a.jsx("label",{htmlFor:"password",className:"block text-sm font-medium text-white mb-2",children:e("login.password")}),a.jsx("input",{id:"password",type:"password",required:!0,value:l,onChange:S=>c(S.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:e("login.passwordPlaceholder"),autoComplete:"current-password"})]})]}),a.jsx("div",{children:a.jsx("button",{type:"submit",disabled:x.isPending,className:"w-full flex justify-center py-3 px-4 bg-bambu-green hover:bg-bambu-green-light text-white font-medium rounded-lg shadow-lg shadow-bambu-green/20 hover:shadow-bambu-green/30 focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:ring-offset-2 focus:ring-offset-bambu-dark-secondary transition-all disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-bambu-green",children:x.isPending?e("login.signingIn"):e("login.signIn")})}),p?.advanced_auth_enabled&&a.jsx("div",{className:"text-center",children:a.jsx("button",{type:"button",onClick:()=>d(!0),className:"text-sm text-bambu-gray hover:text-bambu-green transition-colors",children:e("login.forgotPassword")})})]})]}),u&&a.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:()=>d(!1),children:a.jsxs(_t,{className:"w-full max-w-md",onClick:S=>S.stopPropagation(),children:[a.jsx(An,{children:a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsx(k0,{className:"w-5 h-5 text-bambu-green"}),a.jsx("h2",{className:"text-lg font-semibold text-white",children:e("login.forgotPasswordTitle")})]}),a.jsx(Me,{variant:"ghost",size:"sm",onClick:()=>{d(!1),f("")},children:a.jsx(Ft,{className:"w-5 h-5"})})]})}),a.jsx(Nt,{children:p?.advanced_auth_enabled?a.jsxs("form",{onSubmit:y,className:"space-y-4",children:[a.jsx("p",{className:"text-bambu-gray text-sm",children:e("login.forgotPasswordEmailMessage")||"Enter your email address and we'll send you a new password."}),a.jsxs("div",{children:[a.jsx("label",{htmlFor:"forgot-email",className:"block text-sm font-medium text-white mb-2",children:e("login.emailAddress")||"Email Address"}),a.jsx("input",{id:"forgot-email",type:"email",required:!0,value:h,onChange:S=>f(S.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:e("login.emailPlaceholder")||"your.email@example.com"})]}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(Me,{type:"button",variant:"secondary",className:"flex-1",onClick:()=>{d(!1),f("")},children:e("login.cancel")||"Cancel"}),a.jsx(Me,{type:"submit",className:"flex-1",disabled:v.isPending,children:v.isPending?e("login.sending")||"Sending...":e("login.sendResetEmail")||"Send Reset Email"})]})]}):a.jsxs("div",{className:"space-y-4",children:[a.jsx("p",{className:"text-bambu-gray",children:e("login.forgotPasswordMessage")}),a.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4 space-y-2",children:[a.jsx("p",{className:"text-sm text-white font-medium",children:e("login.howToReset")}),a.jsxs("ol",{className:"text-sm text-bambu-gray space-y-1 list-decimal list-inside",children:[a.jsx("li",{children:e("login.resetStep1")}),a.jsx("li",{children:e("login.resetStep2")}),a.jsx("li",{children:e("login.resetStep3")}),a.jsx("li",{children:e("login.resetStep4")})]})]}),a.jsx(Me,{variant:"secondary",className:"w-full",onClick:()=>d(!1),children:e("login.gotIt")})]})})]})})]})}function WJe(){const t=Hc(),{t:e}=Vt(),{showToast:n}=mn(),{mode:r}=np(),{refreshAuth:i}=Ir(),[s,o]=w.useState(!1),[l,c]=w.useState(""),[u,d]=w.useState(""),[h,f]=w.useState(""),p=lt({mutationFn:()=>se.setupAuth({auth_enabled:s,admin_username:s?l:void 0,admin_password:s?u:void 0}),onSuccess:async v=>{await i(),v.auth_enabled?v.admin_created?(n(e("setup.toast.authEnabledAdminCreated")),t("/login")):(n(e("setup.toast.authEnabledExistingAdmins")),t("/login")):(n(e("setup.toast.setupCompleted")),t("/"))},onError:v=>{n(v.message,"error")}}),x=v=>{if(v.preventDefault(),s&&(l||u)){if(!l||!u){n(e("setup.toast.enterBothCredentials"),"error");return}if(u!==h){n(e("setup.toast.passwordsDoNotMatch"),"error");return}if(u.length<6){n(e("setup.toast.passwordTooShort"),"error");return}}p.mutate()};return a.jsx("div",{className:"min-h-screen flex items-center justify-center bg-bambu-dark p-4",children:a.jsxs("div",{className:"max-w-md w-full space-y-8 p-8 bg-gradient-to-br from-bambu-card to-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary shadow-lg",children:[a.jsxs("div",{className:"text-center",children:[a.jsx("div",{className:"flex items-center justify-center mb-6",children:a.jsx("img",{src:r==="dark"?"/img/bambuddy_logo_dark_transparent.png":"/img/bambuddy_logo_light.png",alt:"Bambuddy",className:"h-16"})}),a.jsx("h2",{className:"text-3xl font-bold text-white",children:e("setup.title")}),a.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:e("setup.subtitle")})]}),a.jsxs("form",{className:"mt-8 space-y-6",onSubmit:x,children:[a.jsxs("div",{className:"space-y-4",children:[a.jsxs("div",{className:"flex items-center p-4 bg-bambu-dark-secondary/50 rounded-lg border border-bambu-dark-tertiary",children:[a.jsx("input",{id:"auth-enabled",type:"checkbox",checked:s,onChange:v=>o(v.target.checked),className:"h-4 w-4 text-bambu-green focus:ring-bambu-green border-bambu-dark-tertiary rounded bg-bambu-dark-secondary"}),a.jsx("label",{htmlFor:"auth-enabled",className:"ml-3 block text-sm font-medium text-white",children:e("setup.enableAuth")})]}),s&&a.jsxs("div",{className:"space-y-4 mt-4",children:[a.jsx("div",{className:"p-3 bg-bambu-dark-secondary/50 border border-bambu-dark-tertiary rounded-lg",children:a.jsxs("div",{className:"flex items-start gap-2",children:[a.jsx(Zs,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),a.jsxs("div",{className:"text-sm text-bambu-gray",children:[a.jsx("p",{className:"text-white font-medium mb-1",children:e("setup.adminAccount")}),a.jsx("p",{children:e("setup.adminAccountDesc")})]})]})}),a.jsxs("div",{children:[a.jsxs("label",{htmlFor:"admin-username",className:"block text-sm font-medium text-white mb-2",children:[e("setup.adminUsername")," ",a.jsx("span",{className:"text-bambu-gray text-xs",children:e("setup.optionalIfAdminExists")})]}),a.jsx("input",{id:"admin-username",type:"text",value:l,onChange:v=>c(v.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:e("setup.adminUsernamePlaceholder"),autoComplete:"username"})]}),a.jsxs("div",{children:[a.jsxs("label",{htmlFor:"admin-password",className:"block text-sm font-medium text-white mb-2",children:[e("setup.adminPassword")," ",a.jsx("span",{className:"text-bambu-gray text-xs",children:e("setup.optionalIfAdminExists")})]}),a.jsx("input",{id:"admin-password",type:"password",value:u,onChange:v=>d(v.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:e("setup.adminPasswordPlaceholder"),minLength:6,autoComplete:"new-password"})]}),u&&a.jsxs("div",{children:[a.jsx("label",{htmlFor:"confirm-password",className:"block text-sm font-medium text-white mb-2",children:e("setup.confirmPassword")}),a.jsx("input",{id:"confirm-password",type:"password",value:h,onChange:v=>f(v.target.value),className:"block w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors",placeholder:e("setup.confirmPasswordPlaceholder"),minLength:6,autoComplete:"new-password"})]})]})]}),a.jsx("div",{children:a.jsx("button",{type:"submit",disabled:p.isPending,className:"w-full flex justify-center py-3 px-4 bg-bambu-green hover:bg-bambu-green-light text-white font-medium rounded-lg shadow-lg shadow-bambu-green/20 hover:shadow-bambu-green/30 focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:ring-offset-2 focus:ring-offset-bambu-dark-secondary transition-all disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-bambu-green",children:p.isPending?e("setup.settingUp"):e("setup.completeSetup")})})]})]})})}function KJe(){const t=w.useRef(null),e=w.useRef(null),n=tn(),[r,i]=w.useState(!1),s=w.useRef(new Set),o=w.useRef(null),l=w.useRef(new Map),c=w.useRef(null),u=w.useRef([]),d=w.useRef(!1),h=w.useRef(()=>{}),f=w.useCallback(()=>{if(d.current||u.current.length===0)return;d.current=!0;const S=()=>{const N=u.current.shift();N?requestAnimationFrame(()=>{h.current(N),u.current.length>0?setTimeout(S,16):d.current=!1}):d.current=!1};S()},[]),p=w.useCallback(()=>{if(t.current?.readyState===WebSocket.OPEN)return;const N=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.host}/api/v1/ws`,A=new WebSocket(N);let M=null;A.onopen=()=>{console.log("[WebSocket] Connected"),i(!0),M=window.setInterval(()=>{A.readyState===WebSocket.OPEN&&A.send(JSON.stringify({type:"ping"}))},3e4)},A.onmessage=T=>{try{const k=JSON.parse(T.data);k.type==="printer_status"&&k.printer_id!==void 0&&k.data?h.current(k):(u.current.push(k),f())}catch{}},A.onclose=T=>{console.log("[WebSocket] Closed",T.code,T.reason),M&&(clearInterval(M),M=null),i(!1),t.current=null,e.current=window.setTimeout(()=>{p()},3e3)},A.onerror=T=>{console.error("[WebSocket] Error",T),A.close()},t.current=A},[f]),x=w.useCallback((S,N)=>{const A=l.current.get(S)||{};l.current.set(S,{...A,...N}),c.current||(c.current=window.setTimeout(()=>{const M=new Map(l.current);l.current.clear(),c.current=null,requestAnimationFrame(()=>{M.forEach((T,k)=>{n.setQueryData(["printerStatus",k],j=>{const E={...j,...T};return E.wifi_signal==null&&j?.wifi_signal!=null&&(E.wifi_signal=j.wifi_signal),E})})})},100))},[n]),v=w.useCallback(S=>{s.current.add(S),o.current&&clearTimeout(o.current),o.current=window.setTimeout(()=>{const N=Array.from(s.current);s.current.clear(),o.current=null;let A=0;N.forEach(M=>{setTimeout(()=>{requestAnimationFrame(()=>{n.invalidateQueries({queryKey:[M]})})},A),A+=500})},3e3)},[n]),b=w.useCallback(S=>{switch(S.type){case"printer_status":S.printer_id!==void 0&&S.data&&x(S.printer_id,S.data);break;case"print_start":S.printer_id!==void 0&&n.invalidateQueries({queryKey:["printerStatus",S.printer_id]});break;case"print_complete":v("archives"),v("archiveStats");break;case"archive_created":v("archives"),v("archiveStats");break;case"archive_updated":v("archives");break;case"pong":break;case"plate_not_empty":window.dispatchEvent(new CustomEvent("plate-not-empty",{detail:{printer_id:S.printer_id,printer_name:S.printer_name,message:S.message}}));break;case"spool_auto_assigned":v("inventory-spools"),v("spool-assignments");break;case"spool_usage_logged":v("inventory-spools");break;case"unknown_tag":window.dispatchEvent(new CustomEvent("unknown-tag",{detail:{printer_id:S.printer_id,ams_id:S.ams_id,tray_id:S.tray_id,tag_uid:S.tag_uid,tray_uuid:S.tray_uuid}}));break}},[n,v,x]);w.useEffect(()=>{h.current=b},[b]),w.useEffect(()=>(p(),()=>{e.current&&clearTimeout(e.current),o.current&&clearTimeout(o.current),c.current&&clearTimeout(c.current),t.current&&t.current.close()}),[p]);const y=w.useCallback(S=>{t.current?.readyState===WebSocket.OPEN&&t.current.send(JSON.stringify(S))},[]);return{isConnected:r,sendMessage:y}}const XJe=new Que({defaultOptions:{queries:{staleTime:1e3*60,retry:1}}});function YJe({children:t}){return KJe(),a.jsx(a.Fragment,{children:t})}function QJe({children:t}){const{authEnabled:e,loading:n,user:r}=Ir();return n?a.jsx("div",{className:"min-h-screen flex items-center justify-center",children:"Loading..."}):e&&!r?a.jsx(db,{to:"/login",replace:!0}):a.jsx(a.Fragment,{children:t})}function ZJe({children:t}){const{authEnabled:e,loading:n,user:r,isAdmin:i}=Ir();return n?a.jsx("div",{className:"min-h-screen flex items-center justify-center",children:"Loading..."}):e?r?i?a.jsx(a.Fragment,{children:t}):a.jsx(db,{to:"/",replace:!0}):a.jsx(db,{to:"/login",replace:!0}):a.jsx(a.Fragment,{children:t})}function JJe({children:t}){const{authEnabled:e,loading:n}=Ir();return n?a.jsx("div",{className:"min-h-screen flex items-center justify-center",children:"Loading..."}):e?a.jsx(db,{to:"/login",replace:!0}):a.jsx(a.Fragment,{children:t})}function eet(){return a.jsx(zpe,{children:a.jsx(Kpe,{children:a.jsx(Zue,{client:XJe,children:a.jsx(Gpe,{children:a.jsx(bue,{children:a.jsxs(qce,{children:[a.jsx(zi,{path:"/setup",element:a.jsx(JJe,{children:a.jsx(WJe,{})})}),a.jsx(zi,{path:"/login",element:a.jsx(qJe,{})}),a.jsx(zi,{path:"/camera/:printerId",element:a.jsx(rJe,{})}),a.jsx(zi,{path:"/overlay/:printerId",element:a.jsx(cJe,{})}),a.jsxs(zi,{element:a.jsx(QJe,{children:a.jsx(YJe,{children:a.jsx(Zpe,{})})}),children:[a.jsx(zi,{index:!0,element:a.jsx(e$e,{})}),a.jsx(zi,{path:"archives",element:a.jsx(HXe,{})}),a.jsx(zi,{path:"queue",element:a.jsx(MQe,{})}),a.jsx(zi,{path:"stats",element:a.jsx($Qe,{})}),a.jsx(zi,{path:"profiles",element:a.jsx(PZe,{})}),a.jsx(zi,{path:"maintenance",element:a.jsx(LZe,{})}),a.jsx(zi,{path:"projects",element:a.jsx(IZe,{})}),a.jsx(zi,{path:"projects/:id",element:a.jsx(GZe,{})}),a.jsx(zi,{path:"inventory",element:a.jsx(UJe,{})}),a.jsx(zi,{path:"files",element:a.jsx(JZe,{})}),a.jsx(zi,{path:"settings",element:a.jsx(ZJe,{children:a.jsx(gZe,{})})}),a.jsx(zi,{path:"users",element:a.jsx(db,{to:"/settings?tab=users",replace:!0})}),a.jsx(zi,{path:"groups",element:a.jsx(db,{to:"/settings?tab=users",replace:!0})}),a.jsx(zi,{path:"system",element:a.jsx(GJe,{})}),a.jsx(zi,{path:"external/:id",element:a.jsx(uJe,{})})]})]})})})})})})}Boe.createRoot(document.getElementById("root")).render(a.jsx(w.StrictMode,{children:a.jsx(eet,{})})); diff --git a/static/index.html b/static/index.html index fe5a9df55..05f5cbcde 100644 --- a/static/index.html +++ b/static/index.html @@ -23,7 +23,7 @@ - +