From 0291f096496a8301f7fd98fb9a915d7fd193fd92 Mon Sep 17 00:00:00 2001 From: maziggy Date: Tue, 30 Dec 2025 12:22:22 +0100 Subject: [PATCH] - Fixed bug where add smart plug modal doesn't scroll on lower resolution screens (https://github.com/maziggy/bambuddy/issues/34) --- frontend/src/components/AddSmartPlugModal.tsx | 6 +++--- static/assets/{index-C2EsE1Fc.js => index-VRY-O4QB.js} | 2 +- static/index.html | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename static/assets/{index-C2EsE1Fc.js => index-VRY-O4QB.js} (94%) diff --git a/frontend/src/components/AddSmartPlugModal.tsx b/frontend/src/components/AddSmartPlugModal.tsx index c5952dbb8..580ac152b 100644 --- a/frontend/src/components/AddSmartPlugModal.tsx +++ b/frontend/src/components/AddSmartPlugModal.tsx @@ -222,11 +222,11 @@ export function AddSmartPlugModal({ plug, onClose }: AddSmartPlugModalProps) { onClick={onClose} >
e.stopPropagation()} > {/* Header */} -
+

{isEditing ? 'Edit Smart Plug' : 'Add Smart Plug'}

@@ -239,7 +239,7 @@ export function AddSmartPlugModal({ plug, onClose }: AddSmartPlugModalProps) {
{/* Form */} -
+ {error && (
{error} diff --git a/static/assets/index-C2EsE1Fc.js b/static/assets/index-VRY-O4QB.js similarity index 94% rename from static/assets/index-C2EsE1Fc.js rename to static/assets/index-VRY-O4QB.js index 64037419a..b233c79de 100644 --- a/static/assets/index-C2EsE1Fc.js +++ b/static/assets/index-VRY-O4QB.js @@ -8604,7 +8604,7 @@ Accuracy: ${t.time_accuracy.toFixed(0)}%`:t.actual_time_seconds?`Actual: ${Ux(t. transition-all duration-200 hover:border-bambu-dark-tertiary/80 ${g?"opacity-50 scale-[1.02] shadow-xl z-50":""} ${x?"border-blue-500/30 bg-gradient-to-r from-blue-500/5 to-transparent":""} - `,children:l.jsxs("div",{className:"flex items-center gap-4 p-4",children:[v?l.jsx("div",{...o,...c,className:"flex items-center justify-center w-10 h-10 md:w-8 md:h-8 rounded-lg bg-bambu-dark cursor-grab active:cursor-grabbing hover:bg-bambu-dark-tertiary transition-colors touch-manipulation",children:l.jsx(N0,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}):e!==void 0?l.jsxs("div",{className:"flex items-center justify-center w-8 h-8 rounded-lg bg-bambu-dark text-bambu-gray text-sm font-medium",children:["#",e]}):l.jsx("div",{className:"w-8"}),l.jsx("div",{className:"w-14 h-14 flex-shrink-0 bg-bambu-dark rounded-lg overflow-hidden",children:t.archive_thumbnail?l.jsx("img",{src:Ee.getArchiveThumbnail(t.archive_id),alt:"",className:"w-full h-full object-cover"}):l.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:l.jsx(pd,{className:"w-6 h-6"})})}),l.jsxs("div",{className:"flex-1 min-w-0",children:[l.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[l.jsx("p",{className:"text-white font-medium truncate",children:t.archive_name||`Archive #${t.archive_id}`}),l.jsx(vc,{to:`/archives?highlight=${t.archive_id}`,className:"text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0",title:"View archive",children:l.jsx(ga,{className:"w-3.5 h-3.5"})})]}),l.jsxs("div",{className:"flex items-center gap-3 text-sm text-bambu-gray",children:[l.jsxs("span",{className:"flex items-center gap-1.5",children:[l.jsx(Ui,{className:"w-3.5 h-3.5"}),t.printer_name||`Printer #${t.printer_id}`]}),t.print_time_seconds&&l.jsxs("span",{className:"flex items-center gap-1.5",children:[l.jsx(Zu,{className:"w-3.5 h-3.5"}),Pte(t.print_time_seconds)]}),v&&l.jsxs("span",{className:"flex items-center gap-1.5",children:[l.jsx(tr,{className:"w-3.5 h-3.5"}),uGe(t.scheduled_time)]})]}),l.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[t.require_previous_success&&l.jsx("span",{className:"text-xs px-2 py-0.5 bg-orange-500/10 text-orange-400 rounded-full border border-orange-500/20",children:"Requires previous success"}),t.auto_off_after&&l.jsxs("span",{className:"text-xs px-2 py-0.5 bg-blue-500/10 text-blue-400 rounded-full border border-blue-500/20 flex items-center gap-1",children:[l.jsx(Cl,{className:"w-3 h-3"}),"Auto power off"]})]}),x&&l.jsxs("div",{className:"mt-3",children:[l.jsx("div",{className:"h-2 bg-bambu-dark rounded-full overflow-hidden",children:l.jsx("div",{className:"h-full bg-gradient-to-r from-blue-500 to-blue-400 animate-pulse w-full opacity-50"})}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Printing in progress..."})]}),t.error_message&&l.jsxs("p",{className:"text-xs text-red-400 mt-2 flex items-center gap-1",children:[l.jsx(ho,{className:"w-3 h-3"}),t.error_message]})]}),l.jsx(dGe,{status:t.status}),l.jsxs("div",{className:"flex items-center gap-1",children:[x&&l.jsx(ke,{variant:"ghost",size:"sm",onClick:a,title:"Stop Print",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:l.jsx(Koe,{className:"w-4 h-4"})}),v&&l.jsxs(l.Fragment,{children:[l.jsx(ke,{variant:"ghost",size:"sm",onClick:n,title:"Edit",children:l.jsx(Fc,{className:"w-4 h-4"})}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:r,title:"Cancel",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:l.jsx(Vt,{className:"w-4 h-4"})})]}),y&&l.jsxs(l.Fragment,{children:[l.jsx(ke,{variant:"ghost",size:"sm",onClick:s,title:"Re-queue",className:"text-bambu-green hover:text-bambu-green/80 hover:bg-bambu-green/10",children:l.jsx(ya,{className:"w-4 h-4"})}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:i,title:"Remove",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]})]})})}function hGe(){const t=hn(),{showToast:e}=vr(),[n,r]=M.useState(null),[i,a]=M.useState(""),[s,o]=M.useState(!1),[c,u]=M.useState(null),[d,f]=M.useState(null),[g,p]=M.useState(null),[x,v]=M.useState(()=>localStorage.getItem("queue.historySortBy")||"date"),[y,b]=M.useState(()=>{const Q=localStorage.getItem("queue.historySortAsc");return Q!==null?Q==="true":!1}),[E,w]=M.useState(()=>localStorage.getItem("queue.pendingSortBy")||"position"),[T,A]=M.useState(()=>{const Q=localStorage.getItem("queue.pendingSortAsc");return Q!==null?Q==="true":!0});M.useEffect(()=>{localStorage.setItem("queue.historySortBy",x)},[x]),M.useEffect(()=>{localStorage.setItem("queue.historySortAsc",String(y))},[y]),M.useEffect(()=>{localStorage.setItem("queue.pendingSortBy",E)},[E]),M.useEffect(()=>{localStorage.setItem("queue.pendingSortAsc",String(T))},[T]);const C=ste(BS(wE,{activationConstraint:{distance:8}}),BS(_E,{coordinateGetter:Cte})),{data:N,isLoading:F}=xt({queryKey:["queue",n,i],queryFn:()=>Ee.getQueue(n||void 0,i||void 0),refetchInterval:5e3}),{data:k}=xt({queryKey:["printers"],queryFn:()=>Ee.getPrinters()}),R=_t({mutationFn:Q=>Ee.cancelQueueItem(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item cancelled")},onError:()=>e("Failed to cancel item","error")}),I=_t({mutationFn:Q=>Ee.removeFromQueue(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item removed")},onError:()=>e("Failed to remove item","error")}),L=_t({mutationFn:Q=>Ee.stopQueueItem(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Print stopped")},onError:()=>e("Failed to stop print","error")}),q=_t({mutationFn:Q=>Ee.reorderQueue(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]})},onError:()=>e("Failed to reorder queue","error")}),j=_t({mutationFn:async()=>{const Q=N?.filter(B=>["completed","failed","skipped","cancelled"].includes(B.status))||[];for(const B of Q)await Ee.removeFromQueue(B.id);return Q.length},onSuccess:Q=>{t.invalidateQueries({queryKey:["queue"]}),e(`Cleared ${Q} history item${Q!==1?"s":""}`)},onError:()=>e("Failed to clear history","error")}),V=M.useMemo(()=>{const Q=N?.filter($=>$.status==="pending")||[],B=$=>{if(!$.scheduled_time)return 0;const Z=new Date($.scheduled_time).getTime(),te=Date.now()+4320*60*60*1e3;return Z>te?0:Z};return[...Q].sort(($,Z)=>{let te;return E==="name"?te=($.archive_name||"").localeCompare(Z.archive_name||""):E==="printer"?te=($.printer_name||"").localeCompare(Z.printer_name||""):E==="time"?te=B($)-B(Z):te=$.position-Z.position,T?te:-te})},[N,E,T]),K=N?.filter(Q=>Q.status==="printing")||[],G=M.useMemo(()=>[...N?.filter(B=>["completed","failed","skipped","cancelled"].includes(B.status))||[]].sort((B,$)=>{let Z;return x==="name"?Z=(B.archive_name||"").localeCompare($.archive_name||""):x==="printer"?Z=(B.printer_name||"").localeCompare($.printer_name||""):Z=new Date($.completed_at||$.created_at).getTime()-new Date(B.completed_at||B.created_at).getTime(),y?-Z:Z}),[N,x,y]),J=M.useMemo(()=>V.reduce((Q,B)=>Q+(B.print_time_seconds||0),0),[V]),ee=Q=>{const{active:B,over:$}=Q;if(!$||B.id===$.id)return;const Z=V.findIndex(he=>he.id===B.id),te=V.findIndex(he=>he.id===$.id);if(Z!==-1&&te!==-1){const de=EE(V,Z,te).map((ge,Ae)=>({id:ge.id,position:Ae+1}));q.mutate(de)}};return l.jsxs("div",{className:"p-4 md:p-8",children:[l.jsx("div",{className:"flex items-center justify-between mb-8",children:l.jsxs("div",{children:[l.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[l.jsx(TG,{className:"w-7 h-7 text-bambu-green"}),"Print Queue"]}),l.jsx("p",{className:"text-bambu-gray mt-1",children:"Schedule and manage your print jobs"})]})}),l.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4 mb-8",children:[l.jsx(St,{className:"bg-gradient-to-br from-blue-500/10 to-transparent border-blue-500/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-blue-500/20 flex items-center justify-center",children:l.jsx(wp,{className:"w-5 h-5 text-blue-400"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:K.length}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Printing"})]})]})})}),l.jsx(St,{className:"bg-gradient-to-br from-yellow-500/10 to-transparent border-yellow-500/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-yellow-500/20 flex items-center justify-center",children:l.jsx(tr,{className:"w-5 h-5 text-yellow-400"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:V.length}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Queued"})]})]})})}),l.jsx(St,{className:"bg-gradient-to-br from-bambu-green/10 to-transparent border-bambu-green/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-bambu-green/20 flex items-center justify-center",children:l.jsx(Zu,{className:"w-5 h-5 text-bambu-green"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:Pte(J)}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Total Queue Time"})]})]})})}),l.jsx(St,{className:"bg-gradient-to-br from-gray-500/10 to-transparent border-gray-500/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-gray-500/20 flex items-center justify-center",children:l.jsx(Mr,{className:"w-5 h-5 text-gray-400"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:G.length}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"History"})]})]})})})]}),l.jsxs("div",{className:"flex items-center gap-4 mb-6",children:[l.jsxs("select",{className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:n||"",onChange:Q=>r(Q.target.value?Number(Q.target.value):null),children:[l.jsx("option",{value:"",children:"All Printers"}),k?.map(Q=>l.jsx("option",{value:Q.id,children:Q.name},Q.id))]}),l.jsxs("select",{className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:i,onChange:Q=>a(Q.target.value),children:[l.jsx("option",{value:"",children:"All Status"}),l.jsx("option",{value:"pending",children:"Pending"}),l.jsx("option",{value:"printing",children:"Printing"}),l.jsx("option",{value:"completed",children:"Completed"}),l.jsx("option",{value:"failed",children:"Failed"}),l.jsx("option",{value:"skipped",children:"Skipped"}),l.jsx("option",{value:"cancelled",children:"Cancelled"})]}),l.jsx("div",{className:"flex-1"}),G.length>0&&l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>o(!0),children:[l.jsx(Sn,{className:"w-4 h-4"}),"Clear History"]})]}),F?l.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading..."}):N?.length===0?l.jsxs(St,{className:"p-12 text-center border-dashed",children:[l.jsx(qr,{className:"w-16 h-16 text-bambu-gray mx-auto mb-4 opacity-50"}),l.jsx("h3",{className:"text-xl font-medium text-white mb-2",children:"No prints scheduled"}),l.jsx("p",{className:"text-bambu-gray max-w-md mx-auto",children:'Schedule a print from the Archives page using the "Schedule" option in the context menu, or drag and drop files to get started.'})]}):l.jsxs("div",{className:"space-y-8",children:[K.length>0&&l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white mb-4 flex items-center gap-2",children:[l.jsx("div",{className:"w-2 h-2 rounded-full bg-blue-400 animate-pulse"}),"Currently Printing"]}),l.jsx("div",{className:"space-y-3",children:K.map(Q=>l.jsx(wk,{item:Q,onEdit:()=>{},onCancel:()=>{},onRemove:()=>{},onStop:()=>p({type:"stop",item:Q}),onRequeue:()=>{}},Q.id))})]}),V.length>0&&l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-4",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(tr,{className:"w-5 h-5 text-yellow-400"}),"Queued",l.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",V.length," item",V.length!==1?"s":"",")"]}),l.jsx("span",{className:"text-xs text-bambu-gray ml-2",title:"Position only affects ASAP items. Scheduled items run at their set time.",children:"Drag to reorder (ASAP only)"})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("select",{className:"px-3 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:E,onChange:Q=>w(Q.target.value),children:[l.jsx("option",{value:"position",children:"Sort by Position"}),l.jsx("option",{value:"name",children:"Sort by Name"}),l.jsx("option",{value:"printer",children:"Sort by Printer"}),l.jsx("option",{value:"time",children:"Sort by Schedule"})]}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:()=>A(!T),title:T?"Ascending":"Descending",className:"px-2",children:T?l.jsx(bp,{className:"w-4 h-4"}):l.jsx(E0,{className:"w-4 h-4"})})]})]}),l.jsx(Ste,{sensors:C,collisionDetection:ute,onDragEnd:ee,children:l.jsx(Tte,{items:V.map(Q=>Q.id),strategy:Q9e,children:l.jsx("div",{className:"space-y-3",children:V.map((Q,B)=>l.jsx(wk,{item:Q,position:B+1,onEdit:()=>u(Q),onCancel:()=>p({type:"cancel",item:Q}),onRemove:()=>{},onStop:()=>{},onRequeue:()=>{}},Q.id))})})})]}),G.length>0&&l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-4",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(Mr,{className:"w-5 h-5 text-bambu-gray"}),"History",l.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",G.length," item",G.length!==1?"s":"",")"]})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("select",{className:"px-3 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:x,onChange:Q=>v(Q.target.value),children:[l.jsx("option",{value:"date",children:"Sort by Date"}),l.jsx("option",{value:"name",children:"Sort by Name"}),l.jsx("option",{value:"printer",children:"Sort by Printer"})]}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:()=>b(!y),title:y?"Ascending (oldest first)":"Descending (newest first)",className:"px-2",children:y?l.jsx(bp,{className:"w-4 h-4"}):l.jsx(E0,{className:"w-4 h-4"})})]})]}),l.jsx("div",{className:"space-y-3",children:G.slice(0,20).map((Q,B)=>l.jsx(wk,{item:Q,position:B+1,onEdit:()=>{},onCancel:()=>{},onRemove:()=>p({type:"remove",item:Q}),onStop:()=>{},onRequeue:()=>f(Q)},Q.id))})]})]}),c&&l.jsx(cGe,{item:c,onClose:()=>u(null)}),d&&l.jsx(nte,{archiveId:d.archive_id,archiveName:d.archive_name||`Archive #${d.archive_id}`,onClose:()=>f(null)}),g&&l.jsx(Lr,{title:g.type==="cancel"?"Cancel Scheduled Print":g.type==="stop"?"Stop Print":"Remove from History",message:g.type==="cancel"?`Are you sure you want to cancel "${g.item.archive_name||"this print"}"?`:g.type==="stop"?`Are you sure you want to stop the current print "${g.item.archive_name||"this print"}"? This will cancel the print job on the printer.`:`Are you sure you want to remove "${g.item.archive_name||"this item"}" from the queue history?`,confirmText:g.type==="cancel"?"Cancel Print":g.type==="stop"?"Stop Print":"Remove",variant:"danger",onConfirm:()=>{g.type==="cancel"?R.mutate(g.item.id):g.type==="stop"?L.mutate(g.item.id):I.mutate(g.item.id),p(null)},onCancel:()=>p(null)}),s&&l.jsx(Lr,{title:"Clear History",message:`Are you sure you want to remove all ${G.length} item${G.length!==1?"s":""} from the history?`,confirmText:"Clear History",variant:"danger",onConfirm:()=>{j.mutate(),o(!1)},onCancel:()=>o(!1)})]})}function fGe({printDates:t,months:e=3}){const{weeks:n,monthLabels:r,printCounts:i}=M.useMemo(()=>{const c={};t.forEach(b=>{const E=b.split("T")[0];c[E]=(c[E]||0)+1});const u=new Date,d=new Date(u);d.setMonth(d.getMonth()-e),d.setDate(d.getDate()-d.getDay());const f=[],g=[];let p=[],x=-1;const v=new Date(d);let y=0;for(;v<=u;)v.getDay()===0&&p.length>0&&(f.push(p),p=[],y++),v.getMonth()!==x&&(g.push({month:v.toLocaleDateString("en-US",{month:"short"}),weekIndex:y}),x=v.getMonth()),p.push(new Date(v)),v.setDate(v.getDate()+1);return p.length>0&&f.push(p),{weeks:f,monthLabels:g,printCounts:c}},[t,e]),a=Math.max(1,...Object.values(i)),s=c=>{if(c===0)return"bg-bambu-dark";const u=c/a;return u<=.25?"bg-bambu-green/30":u<=.5?"bg-bambu-green/50":u<=.75?"bg-bambu-green/75":"bg-bambu-green"},o=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];return l.jsxs("div",{className:"overflow-x-auto",children:[l.jsx("div",{className:"flex mb-1 ml-8",children:r.map(({month:c,weekIndex:u},d)=>l.jsx("div",{className:"text-xs text-bambu-gray",style:{marginLeft:d===0?0:`${(u-(r[d-1]?.weekIndex||0))*14-24}px`},children:c},d))}),l.jsxs("div",{className:"flex gap-0.5",children:[l.jsx("div",{className:"flex flex-col gap-0.5 mr-1",children:o.map((c,u)=>l.jsx("div",{className:"h-3 text-xs text-bambu-gray flex items-center",style:{visibility:u%2===1?"visible":"hidden"},children:c},c))}),n.map((c,u)=>l.jsx("div",{className:"flex flex-col gap-0.5",children:[0,1,2,3,4,5,6].map(d=>{const f=c.find(v=>v.getDay()===d);if(!f)return l.jsx("div",{className:"w-3 h-3"},d);const g=f.toISOString().split("T")[0],p=i[g]||0,x=g===new Date().toISOString().split("T")[0];return l.jsx("div",{className:`w-3 h-3 rounded-sm ${s(p)} ${x?"ring-1 ring-white":""}`,title:`${f.toLocaleDateString()}: ${p} print${p!==1?"s":""}`},d)})},u))]}),l.jsxs("div",{className:"flex items-center gap-2 mt-3 text-xs text-bambu-gray",children:[l.jsx("span",{children:"Less"}),l.jsxs("div",{className:"flex gap-0.5",children:[l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-dark"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green/30"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green/50"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green/75"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green"})]}),l.jsx("span",{children:"More"})]})]})}const n1=["#00ae42","#3b82f6","#f59e0b","#ef4444","#8b5cf6","#ec4899","#14b8a6","#f97316"];function mGe(t){const e=new Date;switch(t){case"7d":return new Date(e.getTime()-10080*60*1e3);case"30d":return new Date(e.getTime()-720*60*60*1e3);case"90d":return new Date(e.getTime()-2160*60*60*1e3);case"365d":return new Date(e.getTime()-365*24*60*60*1e3);case"all":return new Date(0)}}function pGe({archives:t,currency:e="$"}){const[n,r]=M.useState("30d"),i=M.useMemo(()=>{const g=mGe(n);return t.filter(p=>new Date(p.completed_at||p.created_at)>=g)},[t,n]),a=M.useMemo(()=>{const g=new Map;return i.forEach(p=>{const v=new Date(p.completed_at||p.created_at).toISOString().split("T")[0],y=g.get(v)||{date:v,filament:0,cost:0,prints:0};y.filament+=p.filament_used_grams||0,y.cost+=p.cost||0,y.prints+=1,g.set(v,y)}),Array.from(g.values()).sort((p,x)=>p.date.localeCompare(x.date)).map(p=>({...p,dateLabel:new Date(p.date).toLocaleDateString("en-US",{month:"short",day:"numeric"})}))},[i]),s=M.useMemo(()=>{if(n==="7d"||n==="30d")return a;const g=new Map;return i.forEach(p=>{const x=new Date(p.completed_at||p.created_at),v=new Date(x);v.setDate(x.getDate()-x.getDay());const y=v.toISOString().split("T")[0],b=g.get(y)||{week:y,filament:0,cost:0,prints:0};b.filament+=p.filament_used_grams||0,b.cost+=p.cost||0,b.prints+=1,g.set(y,b)}),Array.from(g.values()).sort((p,x)=>p.week.localeCompare(x.week)).map(p=>({date:p.week,dateLabel:`Week of ${new Date(p.week).toLocaleDateString("en-US",{month:"short",day:"numeric"})}`,...p}))},[i,a,n]),o=M.useMemo(()=>{const g=new Map;return i.forEach(p=>{const v=(p.filament_type||"Unknown").split(", ");v.forEach(y=>{const b=(p.filament_used_grams||0)/v.length;g.set(y,(g.get(y)||0)+b)})}),Array.from(g.entries()).map(([p,x])=>({name:p,value:Math.round(x)})).sort((p,x)=>x.value-p.value)},[i]),c=M.useMemo(()=>{const g=new Date,p=[];for(let x=5;x>=0;x--){const v=new Date(g.getFullYear(),g.getMonth()-x,1),y=new Date(g.getFullYear(),g.getMonth()-x+1,0),b=v.toLocaleDateString("en-US",{month:"short",year:"2-digit"}),E=t.filter(w=>{const T=new Date(w.completed_at||w.created_at);return T>=v&&T<=y});p.push({month:b,filament:Math.round(E.reduce((w,T)=>w+(T.filament_used_grams||0),0)),cost:E.reduce((w,T)=>w+(T.cost||0),0),prints:E.length})}return p},[t]),u=n==="7d"||n==="30d"?a:s,d=i.reduce((g,p)=>g+(p.filament_used_grams||0),0),f=i.reduce((g,p)=>g+(p.cost||0),0);return l.jsxs("div",{className:"space-y-6",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:"Filament Usage Trends"}),l.jsx("div",{className:"flex gap-1 bg-bambu-dark rounded-lg p-1",children:["7d","30d","90d","365d","all"].map(g=>l.jsx("button",{onClick:()=>r(g),className:`px-3 py-1 text-sm rounded-md transition-colors ${n===g?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:g==="all"?"All":g.replace("d","D")},g))})]}),l.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Filament"}),l.jsxs("p",{className:"text-2xl font-bold text-white",children:[(d/1e3).toFixed(2),"kg"]}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[d.toFixed(0),"g total"]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Cost"}),l.jsxs("p",{className:"text-2xl font-bold text-white",children:[e,f.toFixed(2)]}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[i.length," prints"]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Avg per Print"}),l.jsxs("p",{className:"text-2xl font-bold text-white",children:[i.length>0?(d/i.length).toFixed(0):0,"g"]}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[e,i.length>0?(f/i.length).toFixed(2):"0.00"," avg"]})]})]}),u.length>0?l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Usage Over Time"}),l.jsx(d1,{width:"100%",height:250,children:l.jsxs(ANe,{data:u,children:[l.jsx("defs",{children:l.jsxs("linearGradient",{id:"colorFilament",x1:"0",y1:"0",x2:"0",y2:"1",children:[l.jsx("stop",{offset:"5%",stopColor:"#00ae42",stopOpacity:.3}),l.jsx("stop",{offset:"95%",stopColor:"#00ae42",stopOpacity:0})]})}),l.jsx(jw,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),l.jsx(Dw,{dataKey:"dateLabel",stroke:"#9ca3af",tick:{fontSize:12},interval:"preserveStartEnd"}),l.jsx(Ow,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:g=>`${g}g`}),l.jsx(f1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},labelStyle:{color:"#fff"},formatter:g=>[`${g.toFixed(0)}g`,"Filament"]}),l.jsx(FY,{type:"monotone",dataKey:"filament",stroke:"#00ae42",strokeWidth:2,fillOpacity:1,fill:"url(#colorFilament)"})]})})]}):l.jsx("div",{className:"bg-bambu-dark rounded-lg p-8 text-center text-bambu-gray",children:"No data for selected time range"}),l.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"By Filament Type"}),o.length>0?l.jsxs("div",{className:"flex items-center gap-4",children:[l.jsx(d1,{width:160,height:160,children:l.jsxs(ENe,{children:[l.jsx(lY,{data:o,cx:"50%",cy:"50%",innerRadius:40,outerRadius:70,paddingAngle:2,dataKey:"value",children:o.map((g,p)=>l.jsx(Zb,{fill:n1[p%n1.length]},`cell-${p}`))}),l.jsx(f1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:g=>[`${g}g`,"Usage"]})]})}),l.jsx("div",{className:"flex-1 space-y-2 overflow-hidden",children:o.map((g,p)=>{const x=o.reduce((y,b)=>y+b.value,0),v=x>0?(g.value/x*100).toFixed(0):0;return l.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[l.jsx("div",{className:"w-3 h-3 rounded-sm flex-shrink-0",style:{backgroundColor:n1[p%n1.length]}}),l.jsx("span",{className:"text-white truncate flex-1",children:g.name}),l.jsxs("span",{className:"text-bambu-gray flex-shrink-0",children:[v,"%"]})]},g.name)})})]}):l.jsx("div",{className:"h-[160px] flex items-center justify-center text-bambu-gray",children:"No filament data"})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Monthly Comparison"}),l.jsx(d1,{width:"100%",height:200,children:l.jsxs(fNe,{data:c,children:[l.jsx(jw,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),l.jsx(Dw,{dataKey:"month",stroke:"#9ca3af",tick:{fontSize:12}}),l.jsx(Ow,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:g=>`${g}g`}),l.jsx(f1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:(g,p)=>[p==="filament"?`${g}g`:p==="cost"?`${e}${g.toFixed(2)}`:g,p==="filament"?"Filament":p==="cost"?"Cost":"Prints"]}),l.jsx(FR,{}),l.jsx(mY,{dataKey:"filament",name:"Filament (g)",fill:"#00ae42",radius:[4,4,0,0]})]})})]})]})]})}function gGe({id:t,title:e,children:n,isHidden:r,size:i,onToggleVisibility:a,onToggleSize:s}){const{attributes:o,listeners:c,setNodeRef:u,transform:d,transition:f,isDragging:g}=Nte({id:t}),p={transform:Yp.Transform.toString(d),transition:f,opacity:g?.5:1};return r?null:l.jsxs("div",{ref:u,style:{...p,gridColumn:`span ${i}`},className:`bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary overflow-hidden ${g?"ring-2 ring-bambu-green shadow-lg":""}`,children:[l.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/30",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("button",{...o,...c,className:"cursor-grab active:cursor-grabbing p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Drag to reorder",children:l.jsx(N0,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}),l.jsx("h3",{className:"text-sm font-medium text-white",children:e})]}),l.jsxs("div",{className:"flex items-center gap-1",children:[l.jsx("button",{onClick:s,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:`Size: ${i===1?"1/4":i===2?"1/2":"Full"} - Click to cycle`,children:i===4?l.jsx(jce,{className:"w-4 h-4 text-bambu-gray hover:text-white"}):l.jsx(Mce,{className:"w-4 h-4 text-bambu-gray hover:text-white"})}),l.jsx("button",{onClick:a,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Hide widget",children:l.jsx(KS,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]})]}),l.jsx("div",{className:"p-4",children:n})]})}function xGe({widgets:t,storageKey:e,columns:n=4,hideControls:r=!1,onResetLayout:i,renderControls:a}){const s=()=>{const T={};return t.forEach(A=>{T[A.id]=A.defaultSize||4}),T},[o,c]=M.useState(()=>{const T=localStorage.getItem(e);if(T)try{const A=JSON.parse(T);return A.sizes||(A.sizes=s()),A}catch{}return{order:t.map(A=>A.id),hidden:t.filter(A=>A.defaultVisible===!1).map(A=>A.id),sizes:s()}}),[u,d]=M.useState(!1);M.useEffect(()=>{const T=()=>d(A=>!A);return window.addEventListener("toggle-hidden-panel",T),()=>window.removeEventListener("toggle-hidden-panel",T)},[]),M.useEffect(()=>{localStorage.setItem(e,JSON.stringify(o))},[o,e]),M.useEffect(()=>{const A=t.map(C=>C.id).filter(C=>!o.order.includes(C));A.length>0&&c(C=>({...C,order:[...C.order,...A]}))},[t,o.order]);const f=ste(BS(wE,{activationConstraint:{distance:8}}),BS(_E,{coordinateGetter:Cte})),g=T=>{const{active:A,over:C}=T;C&&A.id!==C.id&&c(N=>{const F=N.order.indexOf(A.id),k=N.order.indexOf(C.id);return{...N,order:EE(N.order,F,k)}})},p=T=>{c(A=>({...A,hidden:A.hidden.includes(T)?A.hidden.filter(C=>C!==T):[...A.hidden,T]}))},x=T=>{c(A=>{const C=A.sizes[T]||4,N=C===1?2:C===2?4:1;return{...A,sizes:{...A.sizes,[T]:N}}})},v=()=>{const T={order:t.map(A=>A.id),hidden:t.filter(A=>A.defaultVisible===!1).map(A=>A.id),sizes:s()};c(T),i?.()},y=o.order.map(T=>t.find(A=>A.id===T)).filter(Boolean),b=y.filter(T=>!o.hidden.includes(T.id)),E=y.filter(T=>o.hidden.includes(T.id)),w=a?.({hiddenCount:E.length,showHiddenPanel:u,setShowHiddenPanel:d,resetLayout:v});return l.jsxs("div",{className:"space-y-4",children:[w,!r&&!a&&l.jsxs("div",{className:"flex items-center justify-end gap-2",children:[l.jsxs(ke,{variant:"secondary",size:"sm",onClick:v,children:[l.jsx(ng,{className:"w-4 h-4"}),"Reset Layout"]}),E.length>0&&l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>d(!u),children:[l.jsx(Yh,{className:"w-4 h-4"}),E.length," Hidden"]})]}),u&&E.length>0&&l.jsxs("div",{className:"p-4 bg-bambu-dark rounded-xl border border-bambu-dark-tertiary",children:[l.jsx("p",{className:"text-sm text-bambu-gray mb-3",children:"Hidden widgets (click to show):"}),l.jsx("div",{className:"flex flex-wrap gap-2",children:E.map(T=>l.jsxs("button",{onClick:()=>p(T.id),className:"px-3 py-1.5 bg-bambu-dark-tertiary hover:bg-bambu-green/20 rounded-lg text-sm text-white transition-colors flex items-center gap-2",children:[l.jsx(Yh,{className:"w-3 h-3"}),T.title]},T.id))})]}),l.jsx(Ste,{sensors:f,collisionDetection:ute,onDragEnd:g,children:l.jsx(Tte,{items:b.map(T=>T.id),strategy:DL,children:l.jsx("div",{className:"grid gap-6",style:{gridTemplateColumns:`repeat(${n}, minmax(0, 1fr))`},children:b.map(T=>l.jsx(gGe,{id:T.id,title:T.title,isHidden:o.hidden.includes(T.id),size:o.sizes[T.id]||2,onToggleVisibility:()=>p(T.id),onToggleSize:()=>x(T.id),children:T.component},T.id))})})}),b.length===0&&l.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[l.jsx("p",{children:"All widgets are hidden."}),l.jsx(ke,{className:"mt-4",onClick:v,children:"Reset Layout"})]})]})}function bGe({stats:t,currency:e}){return l.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-3 gap-4",children:[l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:l.jsx(Ic,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Total Prints"}),l.jsx("p",{className:"text-xl font-bold text-white",children:t?.total_prints||0})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-blue-400",children:l.jsx(tr,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Print Time"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_print_time_hours.toFixed(1)||0,"h"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-orange-400",children:l.jsx(Ic,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Used"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[((t?.total_filament_grams||0)/1e3).toFixed(2),"kg"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-green-400",children:l.jsx(Hk,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Cost"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_cost.toFixed(2)||"0.00"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-400",children:l.jsx(Jh,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Used"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_energy_kwh.toFixed(2)||"0.00"," kWh"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-500",children:l.jsx(Hk,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Cost"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_energy_cost.toFixed(2)||"0.00"]})]})]})]})}function yGe({stats:t}){const e=t?.total_prints?Math.round(t.successful_prints/t.total_prints*100):0;return l.jsxs("div",{className:"flex items-center gap-6",children:[l.jsxs("div",{className:"relative w-28 h-28",children:[l.jsxs("svg",{className:"w-full h-full -rotate-90",children:[l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:"#00ae42",strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${e*3.02} 302`})]}),l.jsx("div",{className:"absolute inset-0 flex items-center justify-center",children:l.jsxs("span",{className:"text-xl font-bold text-white",children:[e,"%"]})})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:"text-sm text-bambu-gray",children:"Successful:"}),l.jsx("span",{className:"text-sm text-white font-medium",children:t?.successful_prints||0})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400"}),l.jsx("span",{className:"text-sm text-bambu-gray",children:"Failed:"}),l.jsx("span",{className:"text-sm text-white font-medium",children:t?.failed_prints||0})]})]})]})}function vGe({stats:t,printerMap:e}){const n=t?.average_time_accuracy;if(n==null)return l.jsx("div",{className:"flex items-center justify-center h-full",children:l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No time accuracy data yet"})});const i=(Math.min(150,Math.max(50,n))-50)/100*100,s=(c=>c>=95&&c<=105?"#00ae42":c>105?"#3b82f6":"#f97316")(n),o=n-100;return l.jsxs("div",{className:"flex items-center gap-6",children:[l.jsxs("div",{className:"relative w-28 h-28",children:[l.jsxs("svg",{className:"w-full h-full -rotate-90",children:[l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:s,strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${i*3.02} 302`})]}),l.jsxs("div",{className:"absolute inset-0 flex flex-col items-center justify-center",children:[l.jsxs("span",{className:"text-xl font-bold text-white",children:[n.toFixed(0),"%"]}),l.jsxs("span",{className:`text-xs ${o>=0?"text-blue-400":"text-orange-400"}`,children:[o>=0?"+":"",o.toFixed(0),"%"]})]})]}),l.jsxs("div",{className:"space-y-2 flex-1",children:[l.jsxs("div",{className:"flex items-center gap-2 text-xs text-bambu-gray",children:[l.jsx(PG,{className:"w-3 h-3"}),l.jsx("span",{children:"100% = perfect estimate"})]}),t?.time_accuracy_by_printer&&Object.keys(t.time_accuracy_by_printer).length>0&&l.jsx("div",{className:"space-y-1 mt-2",children:Object.entries(t.time_accuracy_by_printer).slice(0,3).map(([c,u])=>l.jsxs("div",{className:"flex items-center justify-between text-xs",children:[l.jsx("span",{className:"text-bambu-gray truncate max-w-[100px]",children:e.get(c)||`Printer ${c}`}),l.jsxs("span",{className:`font-medium ${u>=95&&u<=105?"text-bambu-green":u>105?"text-blue-400":"text-orange-400"}`,children:[u.toFixed(0),"%"]})]},c))})]})]})}function _Ge({stats:t}){if(!t?.prints_by_filament_type||Object.keys(t.prints_by_filament_type).length===0)return l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No filament data available"});const e=Object.entries(t.prints_by_filament_type).sort(([,n],[,r])=>r-n);return l.jsx("div",{className:"space-y-3",children:e.map(([n,r])=>{const i=Math.round(r/(t.total_prints||1)*100);return l.jsxs("div",{children:[l.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[l.jsx("span",{className:"text-white",children:n}),l.jsxs("span",{className:"text-bambu-gray",children:[r," prints"]})]}),l.jsx("div",{className:"h-2 bg-bambu-dark rounded-full",children:l.jsx("div",{className:"h-full bg-bambu-green rounded-full transition-all",style:{width:`${i}%`}})})]},n)})})}function wGe({printDates:t}){return l.jsx(fGe,{printDates:t,months:4})}function SGe({stats:t,printerMap:e}){return!t?.prints_by_printer||Object.keys(t.prints_by_printer).length===0?l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No printer data available"}):l.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:Object.entries(t.prints_by_printer).map(([n,r])=>l.jsxs("div",{className:"flex items-center gap-3 p-3 bg-bambu-dark rounded-lg",children:[l.jsx("div",{className:"p-2 bg-bambu-dark-tertiary rounded-lg",children:l.jsx(Ui,{className:"w-4 h-4 text-bambu-green"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-white font-medium text-sm",children:e.get(n)||`Printer ${n}`}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[r," prints"]})]})]},n))})}function MGe({archives:t,currency:e}){return!t||t.length===0?l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data available"}):l.jsx(pGe,{archives:t,currency:e})}function EGe(){const{data:t,isLoading:e}=xt({queryKey:["failureAnalysis"],queryFn:()=>Ee.getFailureAnalysis({days:30})});if(e)return l.jsx("div",{className:"flex justify-center py-4",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})});if(!t||t.total_prints===0)return l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data in the last 30 days"});const n=Object.entries(t.failures_by_reason).sort(([,r],[,i])=>i-r).slice(0,5);return l.jsxs("div",{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center gap-4",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx($n,{className:`w-5 h-5 ${t.failure_rate>20?"text-red-400":t.failure_rate>10?"text-yellow-400":"text-bambu-green"}`}),l.jsxs("span",{className:"text-2xl font-bold text-white",children:[t.failure_rate.toFixed(1),"%"]}),l.jsx("span",{className:"text-sm text-bambu-gray",children:"failure rate"})]}),l.jsxs("div",{className:"text-sm text-bambu-gray",children:[t.failed_prints," / ",t.total_prints," prints failed"]})]}),n.length>0&&l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray font-medium",children:"Top Failure Reasons"}),n.map(([r,i])=>l.jsxs("div",{className:"flex items-center justify-between text-sm",children:[l.jsx("span",{className:"text-white truncate max-w-[200px]",children:r||"Unknown"}),l.jsx("span",{className:"text-bambu-gray",children:i})]},r))]}),t.trend&&t.trend.length>=2&&l.jsx("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:l.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[l.jsx(RG,{className:`w-4 h-4 ${t.trend[t.trend.length-1].failure_rate{const w=()=>{try{const A=localStorage.getItem("bambusy-dashboard-layout");if(A){const C=JSON.parse(A);c(C.hidden?.length||0)}}catch{c(0)}};w(),window.addEventListener("storage",w);const T=setInterval(w,500);return()=>{window.removeEventListener("storage",w),clearInterval(T)}},[a]);const{data:u,isLoading:d}=xt({queryKey:["archiveStats"],queryFn:Ee.getArchiveStats}),{data:f}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:g}=xt({queryKey:["archives"],queryFn:()=>Ee.getArchives(void 0,void 0,1e3,0)}),{data:p}=xt({queryKey:["settings"],queryFn:Ee.getSettings}),x=async w=>{i(!1),n(!0);try{const{blob:T,filename:A}=await Ee.exportStats({format:w,days:90}),C=URL.createObjectURL(T),N=document.createElement("a");N.href=C,N.download=A,N.click(),URL.revokeObjectURL(C),t("Export downloaded")}catch{t("Export failed","error")}finally{n(!1)}},v=p?.currency||"$",y=new Map(f?.map(w=>[String(w.id),w.name])||[]),b=g?.map(w=>w.created_at)||[];if(d)return l.jsx("div",{className:"p-4 md:p-8",children:l.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading statistics..."})});const E=[{id:"quick-stats",title:"Quick Stats",component:l.jsx(bGe,{stats:u,currency:v}),defaultSize:2},{id:"success-rate",title:"Success Rate",component:l.jsx(yGe,{stats:u}),defaultSize:1},{id:"time-accuracy",title:"Time Accuracy",component:l.jsx(vGe,{stats:u,printerMap:y}),defaultSize:1},{id:"filament-types",title:"Filament Types",component:l.jsx(_Ge,{stats:u}),defaultSize:1},{id:"failure-analysis",title:"Failure Analysis (30 days)",component:l.jsx(EGe,{}),defaultSize:1},{id:"print-activity",title:"Print Activity",component:l.jsx(wGe,{printDates:b}),defaultSize:2},{id:"prints-by-printer",title:"Prints by Printer",component:l.jsx(SGe,{stats:u,printerMap:y}),defaultSize:2},{id:"filament-trends",title:"Filament Usage Trends",component:l.jsx(MGe,{archives:g||[],currency:v}),defaultSize:4}];return l.jsxs("div",{className:"p-4 md:p-8",children:[l.jsxs("div",{className:"flex items-center justify-between mb-6",children:[l.jsxs("div",{children:[l.jsx("h1",{className:"text-2xl font-bold text-white",children:"Dashboard"}),l.jsx("p",{className:"text-bambu-gray",children:"Drag widgets to rearrange. Click the eye icon to hide."})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[o>0&&l.jsxs(ke,{variant:"secondary",onClick:()=>{window.dispatchEvent(new CustomEvent("toggle-hidden-panel"))},children:[l.jsx(Yh,{className:"w-4 h-4"}),o," Hidden"]}),l.jsxs(ke,{variant:"secondary",onClick:()=>{localStorage.removeItem("bambusy-dashboard-layout"),s(w=>w+1),t("Layout reset")},children:[l.jsx(ng,{className:"w-4 h-4"}),"Reset Layout"]}),l.jsxs("div",{className:"relative",children:[l.jsxs(ke,{variant:"secondary",onClick:()=>i(!r),disabled:e,children:[e?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(L1,{className:"w-4 h-4"}),"Export Stats"]}),r&&l.jsxs("div",{className:"absolute right-0 top-full mt-1 w-48 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl z-20",children:[l.jsxs("button",{className:"w-full px-4 py-2 text-left text-white hover:bg-bambu-dark-tertiary transition-colors flex items-center gap-2 rounded-t-lg",onClick:()=>x("csv"),children:[l.jsx(Ss,{className:"w-4 h-4"}),"Export as CSV"]}),l.jsxs("button",{className:"w-full px-4 py-2 text-left text-white hover:bg-bambu-dark-tertiary transition-colors flex items-center gap-2 rounded-b-lg",onClick:()=>x("xlsx"),children:[l.jsx(L1,{className:"w-4 h-4"}),"Export as Excel"]})]})]})]})]}),l.jsx(xGe,{widgets:E,storageKey:"bambusy-dashboard-layout",hideControls:!0},a)]})}function AGe({plug:t,onEdit:e}){const n=hn(),[r,i]=M.useState(!1),[a,s]=M.useState(!1),[o,c]=M.useState(!1),[u,d]=M.useState(!1),{data:f,isLoading:g,refetch:p}=xt({queryKey:["smart-plug-status",t.id],queryFn:()=>Ee.getSmartPlugStatus(t.id),refetchInterval:3e4}),{data:x}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),v=x?.find(C=>C.id===t.printer_id),y=_t({mutationFn:C=>Ee.controlSmartPlug(t.id,C),onSuccess:()=>{p()}}),b=_t({mutationFn:C=>Ee.updateSmartPlug(t.id,C),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),t.printer_id&&n.invalidateQueries({queryKey:["smartPlugByPrinter",t.printer_id]})}}),E=_t({mutationFn:()=>Ee.deleteSmartPlug(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]})}}),w=f?.state==="ON",T=f?.reachable??!1,A=y.isPending;return l.jsxs(l.Fragment,{children:[l.jsx(St,{className:"relative",children:l.jsxs(Tt,{className:"p-4",children:[l.jsxs("div",{className:"flex items-start justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:`p-2 rounded-lg ${T?w?"bg-bambu-green/20":"bg-bambu-dark":"bg-red-500/20"}`,children:l.jsx(so,{className:`w-5 h-5 ${T?w?"text-bambu-green":"text-bambu-gray":"text-red-400"}`})}),l.jsxs("div",{children:[l.jsx("h3",{className:"font-medium text-white",children:t.name}),l.jsx("p",{className:"text-sm text-bambu-gray",children:t.ip_address})]})]}),l.jsx("div",{className:"flex items-center gap-2",children:g?l.jsx(gt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):T?l.jsxs("div",{className:"flex items-center gap-1 text-sm",children:[l.jsx(dR,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:w?"text-bambu-green":"text-bambu-gray",children:f?.state||"Unknown"})]}):l.jsxs("div",{className:"flex items-center gap-1 text-sm text-red-400",children:[l.jsx(Mb,{className:"w-4 h-4"}),l.jsx("span",{children:"Offline"})]})})]}),v&&l.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Linked to: "}),l.jsx("span",{className:"text-sm text-white",children:v.name})]}),(t.power_alert_enabled||t.schedule_enabled)&&l.jsxs("div",{className:"flex flex-wrap gap-1.5 mb-3",children:[t.power_alert_enabled&&l.jsxs("span",{className:"flex items-center gap-1 px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded-full",children:[l.jsx(Ph,{className:"w-3 h-3"}),"Alerts"]}),t.schedule_enabled&&l.jsxs("span",{className:"flex items-center gap-1 px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded-full",children:[l.jsx(qr,{className:"w-3 h-3"}),t.schedule_on_time&&t.schedule_off_time?`${t.schedule_on_time} - ${t.schedule_off_time}`:t.schedule_on_time?`On ${t.schedule_on_time}`:`Off ${t.schedule_off_time}`]})]}),l.jsxs("div",{className:"flex gap-2 mb-3",children:[l.jsxs(ke,{size:"sm",variant:w?"primary":"secondary",disabled:!T||A,onClick:()=>s(!0),className:"flex-1",children:[A?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Cl,{className:"w-4 h-4"}),"On"]}),l.jsxs(ke,{size:"sm",variant:w?"secondary":"primary",disabled:!T||A,onClick:()=>c(!0),className:"flex-1",children:[A?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ZS,{className:"w-4 h-4"}),"Off"]})]}),l.jsxs("button",{onClick:()=>d(!u),className:"w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:[l.jsxs("span",{className:"flex items-center gap-2",children:[l.jsx(QS,{className:"w-4 h-4"}),"Automation Settings"]}),l.jsx("span",{children:u?"-":"+"})]}),u&&l.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(YS,{className:"w-4 h-4 text-bambu-green"}),l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Show in Switchbar"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.show_in_switchbar,onChange:C=>b.mutate({show_in_switchbar:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Enabled"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Enable automation for this plug"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.enabled,onChange:C=>b.mutate({enabled:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Auto On"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn on when print starts"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.auto_on,onChange:C=>b.mutate({auto_on:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Auto Off"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn off when print completes (one-shot)"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.auto_off,onChange:C=>b.mutate({auto_off:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),t.auto_off&&l.jsxs("div",{className:"space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white mb-2",children:"Turn Off Delay Mode"}),l.jsxs("div",{className:"flex gap-2",children:[l.jsxs("button",{onClick:()=>b.mutate({off_delay_mode:"time"}),className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${t.off_delay_mode==="time"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:[l.jsx(tr,{className:"w-4 h-4"}),"Time"]}),l.jsxs("button",{onClick:()=>b.mutate({off_delay_mode:"temperature"}),className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${t.off_delay_mode==="temperature"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:[l.jsx(JS,{className:"w-4 h-4"}),"Temp"]})]})]}),t.off_delay_mode==="time"?l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Delay (minutes)"}),l.jsx("input",{type:"number",min:"1",max:"60",value:t.off_delay_minutes,onChange:C=>b.mutate({off_delay_minutes:parseInt(C.target.value)||5}),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"})]}):l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Temperature threshold (C)"}),l.jsx("input",{type:"number",min:"30",max:"100",value:t.off_temp_threshold,onChange:C=>b.mutate({off_temp_threshold:parseInt(C.target.value)||70}),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"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Turns off when nozzle cools below this temperature"})]})]}),l.jsxs("div",{className:"flex gap-2 pt-2",children:[l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[l.jsx(lR,{className:"w-4 h-4"}),"Edit"]}),l.jsx(ke,{size:"sm",variant:"secondary",onClick:()=>i(!0),className:"text-red-400 hover:text-red-300",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]})]})}),r&&l.jsx(Lr,{title:"Delete Smart Plug",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{E.mutate(),i(!1)},onCancel:()=>i(!1)}),a&&l.jsx(Lr,{title:"Turn On Smart Plug",message:`Are you sure you want to turn on "${t.name}"?`,confirmText:"Turn On",variant:"default",onConfirm:()=>{y.mutate("on"),s(!1)},onCancel:()=>s(!1)}),o&&l.jsx(Lr,{title:"Turn Off Smart Plug",message:`Are you sure you want to turn off "${t.name}"? This will cut power to the connected device.`,confirmText:"Turn Off",variant:"danger",onConfirm:()=>{y.mutate("off"),c(!1)},onCancel:()=>c(!1)})]})}function NGe({plug:t,onClose:e}){const n=hn(),r=!!t,[i,a]=M.useState(t?.name||""),[s,o]=M.useState(t?.ip_address||""),[c,u]=M.useState(t?.username||""),[d,f]=M.useState(t?.password||""),[g,p]=M.useState(t?.printer_id||null),[x,v]=M.useState(null),[y,b]=M.useState(null),[E,w]=M.useState(t?.power_alert_enabled||!1),[T,A]=M.useState(t?.power_alert_high?.toString()||""),[C,N]=M.useState(t?.power_alert_low?.toString()||""),[F,k]=M.useState(t?.schedule_enabled||!1),[R,I]=M.useState(t?.schedule_on_time||""),[L,q]=M.useState(t?.schedule_off_time||""),[j,V]=M.useState(t?.show_in_switchbar||!1),[K,G]=M.useState(!1),[J,ee]=M.useState({scanned:0,total:0}),[Q,B]=M.useState([]),$=M.useRef(null),{data:Z}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:te}=xt({queryKey:["smart-plugs"],queryFn:Ee.getSmartPlugs});M.useEffect(()=>{const z=H=>{H.key==="Escape"&&e()};return window.addEventListener("keydown",z),()=>{window.removeEventListener("keydown",z),$.current&&clearInterval($.current)}},[e]);const he=async()=>{G(!0),B([]),ee({scanned:0,total:0}),b(null);try{await Ee.startTasmotaScan();const z=async()=>{try{const H=await Ee.getTasmotaScanStatus();ee({scanned:H.scanned,total:H.total});const ne=await Ee.getDiscoveredTasmotaDevices();B(ne),H.running||(G(!1),$.current&&(clearInterval($.current),$.current=null))}catch(H){console.error("Polling error:",H)}};await z(),$.current=setInterval(z,500)}catch(z){G(!1);const H=z instanceof Error?z.message:typeof z=="string"?z:JSON.stringify(z);b(H||"Failed to start scan")}},de=async()=>{try{await Ee.stopTasmotaScan()}catch{}G(!1),$.current&&(clearInterval($.current),$.current=null)},ge=z=>{o(z.ip_address),a(z.name),v(null)},Ae=_t({mutationFn:()=>Ee.testSmartPlugConnection(s,c||null,d||null),onSuccess:z=>{v(z),b(null),!i&&z.device_name&&a(z.device_name)},onError:z=>{v(null),b(z.message)}}),me=_t({mutationFn:z=>Ee.createSmartPlug(z),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:z=>{b(z.message)}}),ce=_t({mutationFn:z=>Ee.updateSmartPlug(t.id,z),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:z=>{b(z.message)}}),Pe=Z?.filter(z=>!te?.some(ne=>ne.printer_id===z.id&&ne.id!==t?.id)),oe=z=>{if(z.preventDefault(),b(null),!i.trim()){b("Name is required");return}if(!s.trim()){b("IP address is required");return}const H={name:i.trim(),ip_address:s.trim(),username:c.trim()||null,password:d.trim()||null,printer_id:g,power_alert_enabled:E,power_alert_high:T?parseFloat(T):null,power_alert_low:C?parseFloat(C):null,schedule_enabled:F,schedule_on_time:R||null,schedule_off_time:L||null,show_in_switchbar:j};r?ce.mutate(H):me.mutate(H)},Le=me.isPending||ce.isPending;return l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-md",onClick:z=>z.stopPropagation(),children:[l.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[l.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Smart Plug":"Add Smart Plug"}),l.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:oe,className:"p-6 space-y-4",children:[y&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:y}),!r&&l.jsxs("div",{className:"space-y-3",children:[K?l.jsxs(ke,{type:"button",variant:"secondary",onClick:de,className:"w-full",children:[l.jsx(Vt,{className:"w-4 h-4"}),"Stop Scanning"]}):l.jsxs(ke,{type:"button",variant:"primary",onClick:he,className:"w-full",children:[l.jsx(xl,{className:"w-4 h-4"}),"Discover Tasmota Devices"]}),K&&J.total>0&&l.jsxs("div",{className:"space-y-1",children:[l.jsxs("div",{className:"flex justify-between text-xs text-bambu-gray",children:[l.jsx("span",{children:"Scanning network..."}),l.jsxs("span",{children:[J.scanned," / ",J.total]})]}),l.jsx("div",{className:"w-full bg-bambu-dark-tertiary rounded-full h-2",children:l.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${J.scanned/J.total*100}%`}})})]}),Q.length>0&&l.jsxs("div",{className:"space-y-2",children:[l.jsxs("p",{className:"text-xs text-bambu-gray",children:["Found ",Q.length," device(s) - click to select:"]}),l.jsx("div",{className:"max-h-40 overflow-y-auto space-y-1",children:Q.map(z=>l.jsxs("button",{type:"button",onClick:()=>ge(z),className:"w-full flex items-center justify-between p-2 bg-bambu-dark hover:bg-bambu-dark-tertiary rounded-lg transition-colors text-left border border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(so,{className:"w-4 h-4 text-bambu-green"}),l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:z.name}),l.jsx("p",{className:"text-xs text-bambu-gray",children:z.ip_address})]})]}),z.state&&l.jsxs("span",{className:`flex items-center gap-1 text-xs ${z.state==="ON"?"text-bambu-green":"text-bambu-gray"}`,children:[l.jsx(Cl,{className:"w-3 h-3"}),z.state]})]},z.ip_address))})]}),!K&&Q.length===0&&J.total>0&&l.jsx("p",{className:"text-xs text-bambu-gray text-center py-2",children:"No Tasmota devices found on your network"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"IP Address *"}),l.jsxs("div",{className:"flex gap-2",children:[l.jsx("input",{type:"text",value:s,onChange:z=>{o(z.target.value),v(null)},placeholder:"192.168.1.100",className:"flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"}),l.jsxs(ke,{type:"button",variant:"secondary",onClick:()=>Ae.mutate(),disabled:!s.trim()||Ae.isPending,children:[Ae.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(dR,{className:"w-4 h-4"}),"Test"]})]})]}),x&&l.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${x.success?"bg-bambu-green/20 border border-bambu-green/50 text-bambu-green":"bg-red-500/20 border border-red-500/50 text-red-400"}`,children:x.success?l.jsxs(l.Fragment,{children:[l.jsx(Mr,{className:"w-5 h-5"}),l.jsxs("div",{children:[l.jsx("p",{className:"font-medium",children:"Connected!"}),l.jsxs("p",{className:"text-sm opacity-80",children:[x.device_name&&`Device: ${x.device_name} - `,"State: ",x.state]})]})]}):l.jsxs(l.Fragment,{children:[l.jsx(Mb,{className:"w-5 h-5"}),l.jsx("span",{children:"Connection failed"})]})}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),l.jsx("input",{type:"text",value:i,onChange:z=>a(z.target.value),placeholder:"Living Room Plug",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"})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Username"}),l.jsx("input",{type:"text",value:c,onChange:z=>u(z.target.value),placeholder:"admin",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),l.jsx("input",{type:"password",value:d,onChange:z=>f(z.target.value),placeholder:"********",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"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray -mt-2",children:"Leave empty if your Tasmota device doesn't require authentication"}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Link to Printer"}),l.jsxs("select",{value:g??"",onChange:z=>p(z.target.value?Number(z.target.value):null),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:[l.jsx("option",{value:"",children:"No printer (manual control only)"}),Pe?.map(z=>l.jsx("option",{value:z.id,children:z.name},z.id))]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Linking enables automatic on/off when prints start/complete"})]}),l.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[l.jsxs("div",{className:"flex items-center justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Ph,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:"text-white font-medium",children:"Power Alerts"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:E,onChange:z=>w(z.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),E&&l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if above (W)"}),l.jsx("input",{type:"number",value:T,onChange:z=>A(z.target.value),placeholder:"e.g. 200",min:"0",max:"5000",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if below (W)"}),l.jsx("input",{type:"number",value:C,onChange:z=>N(z.target.value),placeholder:"e.g. 10",min:"0",max:"5000",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"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Get notified when power consumption crosses these thresholds. Leave empty to disable that direction."})]})]}),l.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[l.jsxs("div",{className:"flex items-center justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(tr,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:"text-white font-medium",children:"Daily Schedule"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:F,onChange:z=>k(z.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),F&&l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn On at"}),l.jsx("input",{type:"time",value:R,onChange:z=>I(z.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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn Off at"}),l.jsx("input",{type:"time",value:L,onChange:z=>q(z.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"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Automatically turn the plug on/off at these times daily. Leave empty to skip that action."})]})]}),l.jsx("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(YS,{className:"w-4 h-4 text-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white font-medium",children:"Show in Switchbar"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:j,onChange:z=>V(z.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]})}),l.jsxs("div",{className:"flex gap-3 pt-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:Le,className:"flex-1",children:[Le?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Ms,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function On({checked:t,onChange:e,disabled:n}){const r=i=>{i.preventDefault(),i.stopPropagation(),n||e(!t)};return l.jsx("button",{type:"button",role:"switch","aria-checked":t,disabled:n,onClick:r,className:`relative inline-flex w-11 h-7 md:w-9 md:h-5 rounded-full transition-colors flex-shrink-0 focus:outline-none focus:ring-2 focus:ring-bambu-green focus:ring-offset-2 focus:ring-offset-bambu-dark ${n?"bg-bambu-dark-tertiary/50 cursor-not-allowed opacity-50":t?"bg-bambu-green cursor-pointer":"bg-bambu-dark-tertiary cursor-pointer hover:bg-bambu-dark-tertiary/80"}`,children:l.jsx("span",{className:`pointer-events-none absolute top-[3px] md:top-[2px] left-[3px] md:left-[2px] w-5 h-5 md:w-4 md:h-4 bg-white rounded-full shadow transition-transform duration-200 ease-in-out ${t?"translate-x-4":"translate-x-0"}`})})}const CGe={callmebot:"CallMeBot/WhatsApp",ntfy:"ntfy",pushover:"Pushover",telegram:"Telegram",email:"Email",discord:"Discord",webhook:"Webhook"};function kGe({provider:t,onEdit:e}){const n=hn(),[r,i]=M.useState(!1),[a,s]=M.useState(!1),[o,c]=M.useState(null),{data:u}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),d=u?.find(v=>v.id===t.printer_id),f=_t({mutationFn:v=>Ee.updateNotificationProvider(t.id,v),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),g=_t({mutationFn:()=>Ee.deleteNotificationProvider(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),p=_t({mutationFn:()=>Ee.testNotificationProvider(t.id),onSuccess:v=>{c(v),n.invalidateQueries({queryKey:["notification-providers"]})},onError:v=>{c({success:!1,message:v.message})}}),x=v=>v||"";return l.jsxs(l.Fragment,{children:[l.jsx(St,{className:"relative",children:l.jsxs(Tt,{className:"p-4",children:[l.jsxs("div",{className:"flex items-start justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:`p-2 rounded-lg ${t.enabled?"bg-bambu-green/20":"bg-bambu-dark"}`,children:l.jsx(Ph,{className:`w-5 h-5 ${t.enabled?"text-bambu-green":"text-bambu-gray"}`})}),l.jsxs("div",{children:[l.jsx("h3",{className:"font-medium text-white",children:t.name}),l.jsx("p",{className:"text-sm text-bambu-gray",children:CGe[t.provider_type]||t.provider_type})]})]}),l.jsxs("div",{className:"flex items-center gap-3",children:[t.last_success&&l.jsxs("span",{className:"text-xs text-bambu-green hidden sm:inline",children:["Last: ",new Date(t.last_success).toLocaleDateString()]}),t.last_error&&t.last_error_at&&(!t.last_success||new Date(t.last_error_at)>new Date(t.last_success))&&l.jsx("span",{className:"text-xs text-red-400",title:t.last_error,children:"Error"}),l.jsx(On,{checked:t.enabled,onChange:v=>f.mutate({enabled:v})})]})]}),d&&l.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Printer: "}),l.jsx("span",{className:"text-sm text-white",children:d.name})]}),!d&&!t.printer_id&&l.jsx("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:l.jsx("span",{className:"text-xs text-bambu-gray",children:"All printers"})}),l.jsxs("div",{className:"mb-3 flex flex-wrap gap-1",children:[t.on_print_start&&l.jsx("span",{className:"px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded",children:"Start"}),t.on_print_complete&&l.jsx("span",{className:"px-2 py-0.5 bg-bambu-green/20 text-bambu-green text-xs rounded",children:"Complete"}),t.on_print_failed&&l.jsx("span",{className:"px-2 py-0.5 bg-red-500/20 text-red-400 text-xs rounded",children:"Failed"}),t.on_print_stopped&&l.jsx("span",{className:"px-2 py-0.5 bg-orange-500/20 text-orange-400 text-xs rounded",children:"Stopped"}),t.on_print_progress&&l.jsx("span",{className:"px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded",children:"Progress"}),t.on_printer_offline&&l.jsx("span",{className:"px-2 py-0.5 bg-gray-500/20 text-gray-400 text-xs rounded",children:"Offline"}),t.on_printer_error&&l.jsx("span",{className:"px-2 py-0.5 bg-rose-500/20 text-rose-400 text-xs rounded",children:"Error"}),t.on_filament_low&&l.jsx("span",{className:"px-2 py-0.5 bg-cyan-500/20 text-cyan-400 text-xs rounded",children:"Low Filament"}),t.on_maintenance_due&&l.jsx("span",{className:"px-2 py-0.5 bg-purple-500/20 text-purple-400 text-xs rounded",children:"Maintenance"}),t.on_ams_humidity_high&&l.jsx("span",{className:"px-2 py-0.5 bg-blue-600/20 text-blue-300 text-xs rounded",children:"AMS Humidity"}),t.on_ams_temperature_high&&l.jsx("span",{className:"px-2 py-0.5 bg-orange-600/20 text-orange-300 text-xs rounded",children:"AMS Temp"}),t.on_ams_ht_humidity_high&&l.jsx("span",{className:"px-2 py-0.5 bg-cyan-600/20 text-cyan-300 text-xs rounded",children:"AMS-HT Humidity"}),t.on_ams_ht_temperature_high&&l.jsx("span",{className:"px-2 py-0.5 bg-amber-600/20 text-amber-300 text-xs rounded",children:"AMS-HT Temp"}),t.quiet_hours_enabled&&l.jsxs("span",{className:"px-2 py-0.5 bg-indigo-500/20 text-indigo-400 text-xs rounded flex items-center gap-1",children:[l.jsx(F1,{className:"w-3 h-3"}),"Quiet"]}),t.daily_digest_enabled&&l.jsxs("span",{className:"px-2 py-0.5 bg-emerald-500/20 text-emerald-400 text-xs rounded flex items-center gap-1",children:[l.jsx(qr,{className:"w-3 h-3"}),"Digest ",t.daily_digest_time]})]}),l.jsx("div",{className:"mb-3",children:l.jsxs(ke,{size:"sm",variant:"secondary",disabled:p.isPending,onClick:()=>{c(null),p.mutate()},className:"w-full",children:[p.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cR,{className:"w-4 h-4"}),"Send Test Notification"]})}),o&&l.jsxs("div",{className:`mb-3 p-2 rounded-lg flex items-center gap-2 text-sm ${o.success?"bg-bambu-green/20 text-bambu-green":"bg-red-500/20 text-red-400"}`,children:[o.success?l.jsx(Mr,{className:"w-4 h-4"}):l.jsx(Ca,{className:"w-4 h-4"}),l.jsx("span",{children:o.message})]}),l.jsxs("button",{onClick:()=>s(!a),className:"w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors border-t border-bambu-dark-tertiary",children:[l.jsxs("span",{className:"flex items-center gap-2",children:[l.jsx(QS,{className:"w-4 h-4"}),"Event Settings"]}),a?l.jsx(_b,{className:"w-4 h-4"}):l.jsx(Nl,{className:"w-4 h-4"})]}),a&&l.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Enabled"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Send notifications from this provider"})]}),l.jsx(On,{checked:t.enabled,onChange:v=>f.mutate({enabled:v})})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Print Events"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Started"}),l.jsx(On,{checked:t.on_print_start,onChange:v=>f.mutate({on_print_start:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Completed"}),l.jsx(On,{checked:t.on_print_complete,onChange:v=>f.mutate({on_print_complete:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Failed"}),l.jsx(On,{checked:t.on_print_failed,onChange:v=>f.mutate({on_print_failed:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Stopped"}),l.jsx(On,{checked:t.on_print_stopped,onChange:v=>f.mutate({on_print_stopped:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Progress Milestones"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify at 25%, 50%, 75%"})]}),l.jsx(On,{checked:t.on_print_progress,onChange:v=>f.mutate({on_print_progress:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Printer Status"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Printer Offline"}),l.jsx(On,{checked:t.on_printer_offline,onChange:v=>f.mutate({on_printer_offline:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Printer Error"}),l.jsx(On,{checked:t.on_printer_error,onChange:v=>f.mutate({on_printer_error:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Low Filament"}),l.jsx(On,{checked:t.on_filament_low,onChange:v=>f.mutate({on_filament_low:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Maintenance Due"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify when maintenance is needed"})]}),l.jsx(On,{checked:t.on_maintenance_due??!1,onChange:v=>f.mutate({on_maintenance_due:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS Alarms"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS Humidity High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS humidity exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_humidity_high??!1,onChange:v=>f.mutate({on_ams_humidity_high:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS Temperature High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS temperature exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_temperature_high??!1,onChange:v=>f.mutate({on_ams_temperature_high:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS-HT Alarms"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS-HT Humidity High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT humidity exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_ht_humidity_high??!1,onChange:v=>f.mutate({on_ams_ht_humidity_high:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS-HT Temperature High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT temperature exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_ht_temperature_high??!1,onChange:v=>f.mutate({on_ams_ht_temperature_high:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(F1,{className:"w-4 h-4 text-purple-400"}),l.jsx("p",{className:"text-sm text-white",children:"Quiet Hours"})]}),l.jsx(On,{checked:t.quiet_hours_enabled,onChange:v=>f.mutate({quiet_hours_enabled:v})})]}),t.quiet_hours_enabled&&l.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"No notifications during these hours"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(tr,{className:"w-4 h-4 text-bambu-gray"}),l.jsxs("span",{className:"text-sm text-white",children:[x(t.quiet_hours_start)||"22:00"," - ",x(t.quiet_hours_end)||"07:00"]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change quiet hours"})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(qr,{className:"w-4 h-4 text-emerald-400"}),l.jsx("p",{className:"text-sm text-white",children:"Daily Digest"})]}),l.jsx(On,{checked:t.daily_digest_enabled,onChange:v=>f.mutate({daily_digest_enabled:v})})]}),t.daily_digest_enabled&&l.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(tr,{className:"w-4 h-4 text-bambu-gray"}),l.jsxs("span",{className:"text-sm text-white",children:["Send at ",x(t.daily_digest_time)||"08:00"]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change digest time"})]})]}),l.jsxs("div",{className:"flex gap-2 pt-2",children:[l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[l.jsx(lR,{className:"w-4 h-4"}),"Edit"]}),l.jsx(ke,{size:"sm",variant:"secondary",onClick:()=>i(!0),className:"text-red-400 hover:text-red-300",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]})]})}),r&&l.jsx(Lr,{title:"Delete Notification Provider",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{g.mutate(),i(!1)},onCancel:()=>i(!1)})]})}const R9=[{value:"email",label:"Email",description:"SMTP email notifications"},{value:"telegram",label:"Telegram",description:"Notifications via Telegram bot"},{value:"discord",label:"Discord",description:"Send to Discord channel via webhook"},{value:"ntfy",label:"ntfy",description:"Free, self-hostable push notifications"},{value:"pushover",label:"Pushover",description:"Simple, reliable push notifications"},{value:"callmebot",label:"CallMeBot/WhatsApp",description:"Free WhatsApp notifications via CallMeBot"},{value:"webhook",label:"Webhook",description:"Generic HTTP POST to any URL"}];function PGe({provider:t,onClose:e}){const n=hn(),r=!!t,[i,a]=M.useState(t?.name||""),[s,o]=M.useState(t?.provider_type||"email"),[c,u]=M.useState(t?.printer_id||null),[d,f]=M.useState(t?.quiet_hours_enabled||!1),[g,p]=M.useState(t?.quiet_hours_start||"22:00"),[x,v]=M.useState(t?.quiet_hours_end||"07:00"),[y,b]=M.useState(t?.daily_digest_enabled||!1),[E,w]=M.useState(t?.daily_digest_time||"08:00"),[T,A]=M.useState(t?.on_print_start??!1),[C,N]=M.useState(t?.on_print_complete??!0),[F,k]=M.useState(t?.on_print_failed??!0),[R,I]=M.useState(t?.on_print_stopped??!0),[L,q]=M.useState(t?.on_print_progress??!1),[j,V]=M.useState(t?.on_printer_offline??!1),[K,G]=M.useState(t?.on_printer_error??!1),[J,ee]=M.useState(t?.on_filament_low??!1),[Q,B]=M.useState(t?.on_maintenance_due??!1),[$,Z]=M.useState(t?.config?Object.fromEntries(Object.entries(t.config).map(([W,Y])=>[W,String(Y)])):{}),[te,he]=M.useState(null),[de,ge]=M.useState(null),{data:Ae}=xt({queryKey:["printers"],queryFn:Ee.getPrinters});M.useEffect(()=>{const W=Y=>{Y.key==="Escape"&&e()};return window.addEventListener("keydown",W),()=>window.removeEventListener("keydown",W)},[e]);const me=_t({mutationFn:()=>Ee.testNotificationConfig({provider_type:s,config:$}),onSuccess:W=>{he(W),ge(null)},onError:W=>{he({success:!1,message:W.message})}}),ce=_t({mutationFn:W=>Ee.createNotificationProvider(W),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:W=>{ge(W.message)}}),Pe=_t({mutationFn:W=>Ee.updateNotificationProvider(t.id,W),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:W=>{ge(W.message)}}),oe=W=>{if(W.preventDefault(),ge(null),!i.trim()){ge("Name is required");return}const Y=H(s);for(const be of Y)if(!$[be.key]?.trim()){ge(`${be.label} is required`);return}const se={name:i.trim(),provider_type:s,config:$,printer_id:c,quiet_hours_enabled:d,quiet_hours_start:d?g:null,quiet_hours_end:d?x:null,daily_digest_enabled:y,daily_digest_time:y?E:null,on_print_start:T,on_print_complete:C,on_print_failed:F,on_print_stopped:R,on_print_progress:L,on_printer_offline:j,on_printer_error:K,on_filament_low:J,on_maintenance_due:Q};r?Pe.mutate(se):ce.mutate(se)},Le=ce.isPending||Pe.isPending,z=W=>{switch(W){case"callmebot":return[{key:"phone",label:"Phone Number",placeholder:"+1234567890",type:"text",required:!0},{key:"apikey",label:"API Key",placeholder:"Your CallMeBot API key",type:"text",required:!0}];case"ntfy":return[{key:"server",label:"Server URL",placeholder:"https://ntfy.sh",type:"text",required:!1},{key:"topic",label:"Topic",placeholder:"my-bambuddy",type:"text",required:!0},{key:"auth_token",label:"Auth Token",placeholder:"Optional authentication",type:"password",required:!1}];case"pushover":return[{key:"user_key",label:"User Key",placeholder:"Your Pushover user key",type:"text",required:!0},{key:"app_token",label:"App Token",placeholder:"Your Pushover app token",type:"text",required:!0},{key:"priority",label:"Priority",placeholder:"0 (normal)",type:"number",required:!1}];case"telegram":return[{key:"bot_token",label:"Bot Token",placeholder:"Bot token from @BotFather",type:"password",required:!0},{key:"chat_id",label:"Chat ID",placeholder:"Your chat or group ID",type:"text",required:!0}];case"email":return[{key:"smtp_server",label:"SMTP Server",placeholder:"smtp.gmail.com",type:"text",required:!0},{key:"smtp_port",label:"SMTP Port",placeholder:"587",type:"number",required:!1},{key:"security",label:"Security",type:"select",required:!1,options:[{value:"starttls",label:"STARTTLS (Port 587)"},{value:"ssl",label:"SSL/TLS (Port 465)"},{value:"none",label:"None (Port 25)"}]},{key:"auth_enabled",label:"Authentication",type:"select",required:!1,options:[{value:"true",label:"Enabled"},{value:"false",label:"Disabled"}]},{key:"username",label:"Username",placeholder:"your@email.com",type:"text",required:!1},{key:"password",label:"Password",placeholder:"App password",type:"password",required:!1},{key:"from_email",label:"From Email",placeholder:"your@email.com",type:"text",required:!0},{key:"to_email",label:"To Email",placeholder:"recipient@email.com",type:"text",required:!0}];case"discord":return[{key:"webhook_url",label:"Webhook URL",placeholder:"https://discord.com/api/webhooks/...",type:"text",required:!0}];case"webhook":return[{key:"webhook_url",label:"Webhook URL",placeholder:"https://example.com/webhook",type:"text",required:!0},{key:"auth_header",label:"Authorization",placeholder:"Bearer token (optional)",type:"password",required:!1},{key:"field_title",label:"Title Field Name",placeholder:"title",type:"text",required:!1},{key:"field_message",label:"Message Field Name",placeholder:"message",type:"text",required:!1}];default:return[]}},H=W=>z(W).filter(Y=>Y.required),ne=z(s);return l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4 overflow-y-auto",onClick:e,children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-lg my-8 max-h-[90vh] overflow-y-auto",onClick:W=>W.stopPropagation(),children:[l.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[l.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Notification Provider":"Add Notification Provider"}),l.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:oe,className:"p-6 space-y-4",children:[de&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:de}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),l.jsx("input",{type:"text",value:i,onChange:W=>a(W.target.value),placeholder:"My Notifications",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Provider Type *"}),l.jsx("select",{value:s,onChange:W=>{o(W.target.value),Z({}),he(null)},disabled:r,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 disabled:opacity-50",children:R9.map(W=>l.jsx("option",{value:W.value,children:W.label},W.value))}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:R9.find(W=>W.value===s)?.description})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Configuration"}),ne.map(W=>l.jsxs("div",{children:[l.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[W.label," ",W.required&&"*"]}),W.type==="select"&&W.options?l.jsx("select",{value:$[W.key]||W.options[0]?.value||"",onChange:Y=>{Z({...$,[W.key]:Y.target.value}),he(null)},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:W.options.map(Y=>l.jsx("option",{value:Y.value,children:Y.label},Y.value))}):l.jsx("input",{type:W.type,value:$[W.key]||"",onChange:Y=>{Z({...$,[W.key]:Y.target.value}),he(null)},placeholder:W.placeholder,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"})]},W.key))]}),l.jsx("div",{className:"flex gap-2",children:l.jsxs(ke,{type:"button",variant:"secondary",onClick:()=>{he(null),me.mutate()},disabled:me.isPending||!$[H(s)[0]?.key],className:"flex-1",children:[me.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cR,{className:"w-4 h-4"}),"Test Configuration"]})}),te&&l.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${te.success?"bg-bambu-green/20 border border-bambu-green/50 text-bambu-green":"bg-red-500/20 border border-red-500/50 text-red-400"}`,children:te.success?l.jsxs(l.Fragment,{children:[l.jsx(Mr,{className:"w-5 h-5"}),l.jsx("span",{children:te.message})]}):l.jsxs(l.Fragment,{children:[l.jsx(Ca,{className:"w-5 h-5"}),l.jsx("span",{children:te.message})]})}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer Filter"}),l.jsxs("select",{value:c??"",onChange:W=>u(W.target.value?Number(W.target.value):null),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:[l.jsx("option",{value:"",children:"All printers"}),Ae?.map(W=>l.jsx("option",{value:W.id,children:W.name},W.id))]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Only send notifications for events from this printer"})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("label",{className:"text-sm text-white",children:"Quiet Hours (Do Not Disturb)"}),l.jsx(On,{checked:d,onChange:f})]}),d&&l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Start"}),l.jsx("input",{type:"time",value:g,onChange:W=>p(W.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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"End"}),l.jsx("input",{type:"time",value:x,onChange:W=>v(W.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"})]})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("label",{className:"text-sm text-white",children:"Daily Digest"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"})]}),l.jsx(On,{checked:y,onChange:b})]}),y&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Send digest at"}),l.jsx("input",{type:"time",value:E,onChange:W=>w(W.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"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Events will be collected and sent as a single summary at this time"})]})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Notification Events"}),l.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Print Events"}),l.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Start"}),l.jsx(On,{checked:T,onChange:A})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Complete"}),l.jsx(On,{checked:C,onChange:N})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Failed"}),l.jsx(On,{checked:F,onChange:k})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Stopped"}),l.jsx(On,{checked:R,onChange:I})]}),l.jsxs("div",{className:"flex items-center justify-between col-span-2",children:[l.jsxs("div",{children:[l.jsx("span",{className:"text-sm text-white",children:"Progress"}),l.jsx("span",{className:"text-xs text-bambu-gray ml-1",children:"(25%, 50%, 75%)"})]}),l.jsx(On,{checked:L,onChange:q})]})]})]}),l.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Printer Status"}),l.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Offline"}),l.jsx(On,{checked:j,onChange:V})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Error"}),l.jsx(On,{checked:K,onChange:G})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Low Filament"}),l.jsx(On,{checked:J,onChange:ee})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Maintenance"}),l.jsx(On,{checked:Q,onChange:B})]})]})]})]}),l.jsxs("div",{className:"flex gap-3 pt-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:Le,className:"flex-1",children:[Le?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Ms,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function jGe({template:t,onClose:e}){const n=hn(),r=M.useRef(null),[i,a]=M.useState(t.title_template),[s,o]=M.useState(t.body_template),[c,u]=M.useState(null),[d,f]=M.useState(!0),{data:g}=xt({queryKey:["template-variables"],queryFn:Ee.getTemplateVariables}),p=g?.find(A=>A.event_type===t.event_type),{data:x,isLoading:v}=xt({queryKey:["template-preview",t.event_type,i,s],queryFn:()=>Ee.previewTemplate({event_type:t.event_type,title_template:i,body_template:s}),enabled:d&&i.length>0&&s.length>0});M.useEffect(()=>{const A=C=>{C.key==="Escape"&&e()};return window.addEventListener("keydown",A),()=>window.removeEventListener("keydown",A)},[e]);const y=_t({mutationFn:A=>Ee.updateNotificationTemplate(t.id,A),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-templates"]}),e()},onError:A=>{u(A.message)}}),b=_t({mutationFn:()=>Ee.resetNotificationTemplate(t.id),onSuccess:A=>{a(A.title_template),o(A.body_template),n.invalidateQueries({queryKey:["notification-templates"]})},onError:A=>{u(A.message)}}),E=A=>{if(A.preventDefault(),u(null),!i.trim()){u("Title is required");return}if(!s.trim()){u("Body is required");return}y.mutate({title_template:i,body_template:s})},w=A=>{const C=r.current;if(!C)return;const N=C.selectionStart,F=C.selectionEnd,k=s,R=k.substring(0,N),I=k.substring(F),L=R+`{${A}}`+I;o(L),setTimeout(()=>{C.focus();const q=N+A.length+2;C.setSelectionRange(q,q)},0)},T=i!==t.title_template||s!==t.body_template;return l.jsx("div",{className:"fixed inset-0 bg-black/80 flex items-center justify-center z-50 p-4",children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-2xl max-h-[90vh] flex flex-col",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary shrink-0",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Edit Template: ",t.name]}),l.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",children:l.jsx(Vt,{className:"w-5 h-5 text-bambu-gray"})})]}),l.jsxs("form",{onSubmit:E,className:"flex-1 overflow-y-auto p-4 space-y-4",children:[c&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded text-red-400 text-sm",children:c}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Title"}),l.jsx("input",{type:"text",value:i,onChange:A=>a(A.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white focus:outline-none focus:ring-1 focus:ring-bambu-green",placeholder:"Notification title..."})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Body"}),l.jsx("textarea",{ref:r,value:s,onChange:A=>o(A.target.value),rows:4,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white focus:outline-none focus:ring-1 focus:ring-bambu-green font-mono text-sm resize-none",placeholder:"Notification body..."})]}),p&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-2",children:"Available Variables"}),l.jsx("div",{className:"flex flex-wrap gap-2",children:p.variables.map(A=>l.jsxs("button",{type:"button",onClick:()=>w(A),className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark hover:bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-xs text-bambu-gray hover:text-white transition-colors",children:[l.jsx(ir,{className:"w-3 h-3"}),A]},A))}),l.jsx("p",{className:"text-xs text-bambu-gray/60 mt-1",children:"Click to insert at cursor position in body"})]}),l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsxs("label",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[l.jsx(Yh,{className:"w-4 h-4"}),"Live Preview"]}),l.jsx("button",{type:"button",onClick:()=>f(!d),className:"text-xs text-bambu-green hover:text-bambu-green-light",children:d?"Hide":"Show"})]}),d&&l.jsx("div",{className:"bg-bambu-dark border border-bambu-dark-tertiary rounded p-3 space-y-2",children:v?l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-sm",children:[l.jsx(gt,{className:"w-4 h-4 animate-spin"}),"Loading preview..."]}):x?l.jsxs(l.Fragment,{children:[l.jsxs("div",{children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Title:"}),l.jsx("div",{className:"text-white font-medium",children:x.title})]}),l.jsxs("div",{children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Body:"}),l.jsx("div",{className:"text-white whitespace-pre-wrap text-sm",children:x.body})]})]}):l.jsx("div",{className:"text-bambu-gray text-sm",children:"Enter template content to see preview"})})]})]}),l.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary shrink-0",children:[l.jsxs(ke,{type:"button",variant:"ghost",onClick:()=>b.mutate(),disabled:b.isPending,className:"text-orange-400 hover:text-orange-300",children:[b.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin mr-2"}):l.jsx(ng,{className:"w-4 h-4 mr-2"}),"Reset to Default"]}),l.jsxs("div",{className:"flex gap-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),l.jsxs(ke,{onClick:E,disabled:y.isPending||!T,children:[y.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin mr-2"}):l.jsx(Ms,{className:"w-4 h-4 mr-2"}),"Save"]})]})]})]})})}const RGe={print_start:"Print Started",print_complete:"Print Complete",print_failed:"Print Failed",print_stopped:"Print Stopped",print_progress:"Progress",printer_offline:"Printer Offline",printer_error:"Printer Error",filament_low:"Low Filament",maintenance_due:"Maintenance Due",test:"Test"},DGe={print_start:"text-blue-400",print_complete:"text-bambu-green",print_failed:"text-red-400",print_stopped:"text-orange-400",print_progress:"text-yellow-400",printer_offline:"text-gray-400",printer_error:"text-rose-400",filament_low:"text-cyan-400",maintenance_due:"text-purple-400",test:"text-bambu-gray"};function OGe({onClose:t}){const e=hn(),{showToast:n}=vr(),[r,i]=M.useState(7),[a,s]=M.useState(null),[o,c]=M.useState(!1),{data:u,isLoading:d,refetch:f,isRefetching:g}=xt({queryKey:["notification-logs",r,o],queryFn:()=>Ee.getNotificationLogs({days:r,limit:100,success:o?!1:void 0})}),{data:p}=xt({queryKey:["notification-log-stats",r],queryFn:()=>Ee.getNotificationLogStats(r)}),x=_t({mutationFn:()=>Ee.clearNotificationLogs(30),onSuccess:y=>{n(y.message,"success"),e.invalidateQueries({queryKey:["notification-logs"]}),e.invalidateQueries({queryKey:["notification-log-stats"]})},onError:y=>{n(`Failed to clear logs: ${y.message}`,"error")}}),v=y=>{const b=new Date(y),w=new Date().getTime()-b.getTime();return w<6e4?"Just now":w<36e5?`${Math.floor(w/6e4)}m ago`:w<864e5?`${Math.floor(w/36e5)}h ago`:b.toLocaleDateString()+" "+b.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})};return l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:l.jsxs("div",{className:"bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg w-full max-w-3xl max-h-[85vh] flex flex-col",children:[l.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx(I1,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Notification Log"})]}),l.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white transition-colors",children:"×"})]}),p&&l.jsx("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/50",children:l.jsxs("div",{className:"flex items-center gap-6 text-sm",children:[l.jsxs("span",{className:"text-bambu-gray",children:["Last ",r," days: ",l.jsx("span",{className:"text-white font-medium",children:p.total})," notifications"]}),l.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[l.jsx(Mr,{className:"w-4 h-4"}),p.success_count," sent"]}),p.failure_count>0&&l.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[l.jsx(Ca,{className:"w-4 h-4"}),p.failure_count," failed"]})]})}),l.jsxs("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary flex items-center gap-4",children:[l.jsxs("select",{value:r,onChange:y=>i(Number(y.target.value)),className:"px-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:ring-1 focus:ring-bambu-green",children:[l.jsx("option",{value:1,children:"Last 24 hours"}),l.jsx("option",{value:7,children:"Last 7 days"}),l.jsx("option",{value:30,children:"Last 30 days"}),l.jsx("option",{value:90,children:"Last 90 days"})]}),l.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:o,onChange:y=>c(y.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),"Show failed only"]}),l.jsx("div",{className:"flex-1"}),l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>f(),disabled:g,children:[g?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ya,{className:"w-4 h-4"}),"Refresh"]}),l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>x.mutate(),disabled:x.isPending,className:"text-red-400 hover:text-red-300",children:[x.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"}),"Clear Old"]})]}),l.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:d?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):u&&u.length>0?l.jsx("div",{className:"space-y-2",children:u.map(y=>l.jsx(LGe,{log:y,isExpanded:a===y.id,onToggle:()=>s(a===y.id?null:y.id),formatDate:v},y.id))}):l.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[l.jsx(I1,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),l.jsx("p",{className:"text-sm",children:o?"No failed notifications":"No notifications logged"})]})})]})})}function LGe({log:t,isExpanded:e,onToggle:n,formatDate:r}){return l.jsxs("div",{className:`border rounded-lg overflow-hidden transition-colors ${t.success?"border-bambu-dark-tertiary bg-bambu-dark/30":"border-red-500/30 bg-red-500/5"}`,children:[l.jsxs("button",{className:"w-full px-3 py-2 flex items-center gap-3 text-left hover:bg-bambu-dark/50 transition-colors",onClick:n,children:[t.success?l.jsx(Mr,{className:"w-4 h-4 text-bambu-green shrink-0"}):l.jsx(Ca,{className:"w-4 h-4 text-red-400 shrink-0"}),l.jsx("span",{className:`text-xs font-medium ${DGe[t.event_type]||"text-bambu-gray"}`,children:RGe[t.event_type]||t.event_type}),l.jsx("span",{className:"text-sm text-white truncate flex-1",children:t.provider_name||"Unknown Provider"}),t.printer_name&&l.jsx("span",{className:"text-xs text-bambu-gray",children:t.printer_name}),l.jsx("span",{className:"text-xs text-bambu-gray shrink-0",children:r(t.created_at)}),e?l.jsx(_b,{className:"w-4 h-4 text-bambu-gray shrink-0"}):l.jsx(Nl,{className:"w-4 h-4 text-bambu-gray shrink-0"})]}),e&&l.jsxs("div",{className:"px-3 py-2 border-t border-bambu-dark-tertiary bg-bambu-dark/20 space-y-2",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Title"}),l.jsx("p",{className:"text-sm text-white",children:t.title})]}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Message"}),l.jsx("p",{className:"text-sm text-white whitespace-pre-wrap",children:t.message})]}),!t.success&&t.error_message&&l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-red-400 mb-1",children:"Error"}),l.jsx("p",{className:"text-sm text-red-300",children:t.error_message})]}),l.jsxs("div",{className:"flex gap-4 text-xs text-bambu-gray pt-1",children:[l.jsxs("span",{children:["Provider: ",t.provider_type]}),l.jsxs("span",{children:["Time: ",new Date(t.created_at).toLocaleString()]})]})]})]})}const r1=[{id:"settings",labelKey:"backup.categories.settings",defaultLabel:"App Settings",icon:l.jsx(uR,{className:"w-4 h-4"}),default:!0,description:"Language, theme, update preferences"},{id:"notifications",labelKey:"backup.categories.notifications",defaultLabel:"Notification Providers",icon:l.jsx(Ph,{className:"w-4 h-4"}),default:!0,description:"ntfy, Pushover, Discord, etc."},{id:"templates",labelKey:"backup.categories.templates",defaultLabel:"Notification Templates",icon:l.jsx(Ss,{className:"w-4 h-4"}),default:!0,description:"Custom message templates"},{id:"smart_plugs",labelKey:"backup.categories.smartPlugs",defaultLabel:"Smart Plugs",icon:l.jsx(so,{className:"w-4 h-4"}),default:!0,description:"Tasmota plug configurations"},{id:"external_links",labelKey:"backup.categories.externalLinks",defaultLabel:"External Links",icon:l.jsx(Sb,{className:"w-4 h-4"}),default:!0,description:"Sidebar links to external services"},{id:"printers",labelKey:"backup.categories.printers",defaultLabel:"Printers",icon:l.jsx(Ui,{className:"w-4 h-4"}),default:!1,description:"Printer info (access codes excluded)"},{id:"filaments",labelKey:"backup.categories.filaments",defaultLabel:"Filament Inventory",icon:l.jsx(AG,{className:"w-4 h-4"}),default:!1,description:"Filament types and costs"},{id:"maintenance",labelKey:"backup.categories.maintenance",defaultLabel:"Maintenance Types",icon:l.jsx(gd,{className:"w-4 h-4"}),default:!1,description:"Custom maintenance schedules"},{id:"archives",labelKey:"backup.categories.archives",defaultLabel:"Print Archives",icon:l.jsx(ao,{className:"w-4 h-4"}),default:!1,description:"All print data + files (3MF, thumbnails, photos)"},{id:"projects",labelKey:"backup.categories.projects",defaultLabel:"Projects",icon:l.jsx(Fo,{className:"w-4 h-4"}),default:!1,description:"Projects, BOM items, and attachments"},{id:"pending_uploads",labelKey:"backup.categories.pendingUploads",defaultLabel:"Pending Uploads",icon:l.jsx(ia,{className:"w-4 h-4"}),default:!1,description:"Virtual printer uploads awaiting review"}];function IGe({onClose:t,onExport:e}){const{t:n}=Qp(),[r,i]=M.useState(()=>{const x={};return r1.forEach(v=>{x[v.id]=v.default}),x}),[a,s]=M.useState(!1),[o,c]=M.useState(!1);M.useEffect(()=>{const x=v=>{v.key==="Escape"&&t()};return window.addEventListener("keydown",x),()=>window.removeEventListener("keydown",x)},[t]);const u=x=>{i(v=>({...v,[x]:!v[x]}))},d=()=>{const x={};r1.forEach(v=>{x[v.id]=!0}),i(x)},f=()=>{const x={};r1.forEach(v=>{x[v.id]=!1}),i(x)},g=Object.values(r).filter(Boolean).length,p=async()=>{c(!0);try{await e({...r,access_codes:a&&r.printers})}finally{c(!1)}};return l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:o?void 0:t,children:l.jsx(St,{className:"w-full max-w-lg",onClick:x=>x.stopPropagation(),children:l.jsxs(Tt,{className:"p-0",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:l.jsx(ns,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:n("backup.exportTitle",{defaultValue:"Export Backup"})}),l.jsx("p",{className:"text-sm text-bambu-gray",children:n("backup.selectCategories",{defaultValue:"Select data to include"})})]})]}),l.jsx("button",{onClick:t,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("div",{className:"flex gap-2 px-4 pt-4",children:[l.jsx("button",{onClick:d,disabled:o,className:"text-sm text-bambu-green hover:text-bambu-green/80 disabled:opacity-50 disabled:cursor-not-allowed",children:n("common.selectAll",{defaultValue:"Select All"})}),l.jsx("span",{className:"text-bambu-gray",children:"|"}),l.jsx("button",{onClick:f,disabled:o,className:"text-sm text-bambu-gray hover:text-white disabled:opacity-50 disabled:cursor-not-allowed",children:n("common.selectNone",{defaultValue:"Select None"})})]}),l.jsx("div",{className:`p-4 space-y-2 max-h-[400px] overflow-y-auto ${o?"opacity-50 pointer-events-none":""}`,children:r1.map(x=>l.jsxs("label",{className:`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-colors ${r[x.id]?"bg-bambu-green/10 border border-bambu-green/30":"bg-bambu-dark hover:bg-bambu-dark-tertiary border border-transparent"}`,children:[l.jsx("input",{type:"checkbox",checked:r[x.id],onChange:()=>u(x.id),disabled:o,className:"w-4 h-4 rounded border-bambu-gray bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0"}),l.jsx("div",{className:`${r[x.id]?"text-bambu-green":"text-bambu-gray"}`,children:x.icon}),l.jsxs("div",{className:"flex-1",children:[l.jsx("div",{className:"text-white text-sm font-medium",children:n(x.labelKey,{defaultValue:x.defaultLabel})}),l.jsx("div",{className:"text-xs text-bambu-gray",children:x.description})]})]},x.id))}),r.archives&&l.jsx("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[l.jsx(ao,{className:"w-4 h-4 text-yellow-500 mt-0.5 flex-shrink-0"}),l.jsxs("div",{className:"text-yellow-200 dark:text-yellow-200 text-yellow-700",children:[l.jsx("span",{className:"font-medium",children:"ZIP file will be created."}),l.jsx("span",{className:"text-yellow-600 dark:text-yellow-200/70",children:" Includes all 3MF files, thumbnails, timelapses, and photos. This may take a while and result in a large file."})]})]})}),r.printers&&l.jsxs("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-start gap-2",children:[l.jsx(Qm,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),l.jsxs("div",{children:[l.jsx("p",{className:"text-sm font-medium text-white",children:"Include Access Codes"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"For transferring to another machine"})]})]}),l.jsx(On,{checked:a,onChange:s})]}),a&&l.jsx("div",{className:"mt-2 p-2 rounded bg-orange-500/10 border border-orange-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-xs",children:[l.jsx($n,{className:"w-3 h-3 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),l.jsx("span",{className:"text-orange-700 dark:text-orange-200",children:"Access codes will be included in plain text. Keep this backup file secure!"})]})})]}),l.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary",children:[l.jsx("span",{className:"text-sm text-bambu-gray",children:n("backup.selectedCount",{count:g,defaultValue:`${g} categories selected`})}),l.jsxs("div",{className:"flex gap-3",children:[l.jsx(ke,{variant:"secondary",onClick:t,disabled:o,children:n("common.cancel",{defaultValue:"Cancel"})}),l.jsx(ke,{onClick:p,disabled:g===0||o,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50 disabled:cursor-not-allowed min-w-[100px]",children:o?l.jsxs(l.Fragment,{children:[l.jsx(gt,{className:"w-4 h-4 mr-2 animate-spin"}),n("backup.exporting",{defaultValue:"Exporting..."})]}):l.jsxs(l.Fragment,{children:[l.jsx(ns,{className:"w-4 h-4 mr-2"}),n("backup.export",{defaultValue:"Export"})]})})]})]})]})})})}const D9={settings:"Settings",notification_providers:"Notification Providers",notification_templates:"Notification Templates",smart_plugs:"Smart Plugs",printers:"Printers",filaments:"Filaments",maintenance_types:"Maintenance Types",archives:"Archives",projects:"Projects",pending_uploads:"Pending Uploads",external_links:"External Links"};function FGe({onClose:t,onRestore:e,onSuccess:n}){const[r,i]=M.useState("options"),[a,s]=M.useState(!1),[o,c]=M.useState(null),[u,d]=M.useState(null),[f,g]=M.useState(new Set),p=M.useRef(null);M.useEffect(()=>{const w=T=>{T.key==="Escape"&&r!=="restoring"&&t()};return window.addEventListener("keydown",w),()=>window.removeEventListener("keydown",w)},[t,r]);const x=w=>{const T=w.target.files?.[0];T&&c(T)},v=async()=>{if(o){i("restoring");try{const w=await e(o,a);d(w),i("result"),w.success&&n()}catch{d({success:!1,message:"Failed to restore backup. Please check the file format."}),i("result")}}},y=w=>{g(T=>{const A=new Set(T);return A.has(w)?A.delete(w):A.add(w),A})},b=u?.restored?Object.values(u.restored).reduce((w,T)=>w+T,0)+(u.files_restored||0):0,E=u?.total_skipped||0;return l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onMouseDown:w=>{w.target===w.currentTarget&&r!=="restoring"&&t()},children:l.jsx(St,{className:"w-full max-w-lg",children:l.jsxs(Tt,{className:"p-0",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:`p-2 rounded-full ${r==="result"&&u?.success?"bg-bambu-green/20 text-bambu-green":r==="result"&&!u?.success?"bg-red-500/20 text-red-500":"bg-blue-500/20 text-blue-500"}`,children:r==="result"&&u?.success?l.jsx(Mr,{className:"w-5 h-5"}):r==="result"&&!u?.success?l.jsx($n,{className:"w-5 h-5"}):l.jsx(ia,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsxs("h3",{className:"text-lg font-semibold text-white",children:[r==="options"&&"Restore Backup",r==="restoring"&&"Restoring...",r==="result"&&(u?.success?"Restore Complete":"Restore Failed")]}),l.jsxs("p",{className:"text-sm text-bambu-gray",children:[r==="options"&&"Import settings from a backup file",r==="restoring"&&"Please wait while your data is being restored",r==="result"&&u?.message]})]})]}),r!=="restoring"&&l.jsx("button",{onClick:t,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),r==="options"&&l.jsxs(l.Fragment,{children:[l.jsxs("div",{className:"p-4 space-y-4",children:[l.jsxs("div",{children:[l.jsx("input",{ref:p,type:"file",accept:".json,.zip",className:"hidden",onChange:x}),l.jsx("button",{type:"button",onClick:()=>p.current?.click(),className:`w-full p-4 border-2 border-dashed rounded-lg transition-colors ${o?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:o?l.jsxs("div",{className:"flex items-center justify-center gap-2 text-bambu-green",children:[l.jsx(Mr,{className:"w-5 h-5"}),l.jsx("span",{className:"font-medium",children:o.name})]}):l.jsxs("div",{className:"flex flex-col items-center gap-2 text-bambu-gray",children:[l.jsx(ia,{className:"w-8 h-8"}),l.jsx("span",{children:"Click to select backup file (.json or .zip)"})]})})]}),l.jsx("div",{className:"p-3 rounded-lg bg-blue-500/10 border border-blue-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[l.jsx($n,{className:"w-4 h-4 text-blue-500 dark:text-blue-400 mt-0.5 flex-shrink-0"}),l.jsxs("div",{className:"text-blue-700 dark:text-blue-200",children:[l.jsx("p",{className:"font-medium mb-1",children:"How duplicate handling works:"}),l.jsxs("ul",{className:"text-blue-600 dark:text-blue-200/80 space-y-1 text-xs",children:[l.jsxs("li",{children:[l.jsx("strong",{children:"Printers"})," - matched by serial number"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Smart Plugs"})," - matched by IP address"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Notification Providers"})," - matched by name"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Filaments"})," - matched by name + type + brand"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Archives"})," - matched by content hash (always skipped)"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Pending Uploads"})," - matched by filename"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Settings & Templates"})," - always overwritten"]})]})]})]})}),l.jsx("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsxs("p",{className:"text-white font-medium flex items-center gap-2",children:[a?l.jsx(ya,{className:"w-4 h-4 text-orange-400"}):l.jsx(U1,{className:"w-4 h-4 text-bambu-gray"}),a?"Replace existing data":"Keep existing data"]}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:a?"Overwrite items that already exist with backup data":"Only restore items that don't already exist"})]}),l.jsx(On,{checked:a,onChange:s})]})}),a&&l.jsx("div",{className:"p-3 rounded-lg bg-orange-500/10 border border-orange-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[l.jsx($n,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),l.jsxs("div",{className:"text-orange-700 dark:text-orange-200",children:[l.jsx("span",{className:"font-medium",children:"Caution:"})," Overwriting will replace your current configurations with data from the backup. Printer access codes are never overwritten for security."]})]})})]}),l.jsxs("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:t,children:"Cancel"}),l.jsxs(ke,{type:"button",onClick:v,disabled:!o,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50",children:[l.jsx(ia,{className:"w-4 h-4 mr-2"}),"Restore"]})]})]}),r==="restoring"&&l.jsxs("div",{className:"p-8 flex flex-col items-center gap-4",children:[l.jsx(gt,{className:"w-12 h-12 text-bambu-green animate-spin"}),l.jsx("p",{className:"text-bambu-gray",children:"Processing backup file..."})]}),r==="result"&&u&&l.jsxs(l.Fragment,{children:[l.jsxs("div",{className:"p-4 space-y-4 max-h-[400px] overflow-y-auto",children:[l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{className:"p-3 rounded-lg bg-bambu-green/10 border border-bambu-green/30",children:[l.jsx("div",{className:"text-2xl font-bold text-bambu-green",children:b}),l.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Restored"})]}),l.jsxs("div",{className:"p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:[l.jsx("div",{className:"text-2xl font-bold text-yellow-500",children:E}),l.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Skipped"})]})]}),u.restored&&Object.entries(u.restored).some(([,w])=>w>0)&&l.jsxs("div",{className:"space-y-2",children:[l.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green"}),"Restored"]}),l.jsxs("div",{className:"space-y-1",children:[Object.entries(u.restored).filter(([,w])=>w>0).map(([w,T])=>l.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[l.jsx("span",{className:"text-white",children:D9[w]||w}),l.jsx("span",{className:"text-bambu-green font-medium",children:T})]},w)),(u.files_restored||0)>0&&l.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[l.jsx("span",{className:"text-white",children:"Files (3MF, thumbnails, etc.)"}),l.jsx("span",{className:"text-bambu-green font-medium",children:u.files_restored})]})]})]}),u.skipped&&Object.entries(u.skipped).some(([,w])=>w>0)&&l.jsxs("div",{className:"space-y-2",children:[l.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[l.jsx(U1,{className:"w-4 h-4 text-yellow-500"}),"Skipped (already exist)"]}),l.jsx("div",{className:"space-y-1",children:Object.entries(u.skipped).filter(([,w])=>w>0).map(([w,T])=>{const A=u.skipped_details?.[w]||[],C=f.has(w);return l.jsxs("div",{children:[l.jsxs("button",{onClick:()=>A.length>0&&y(w),className:`w-full flex items-center justify-between text-sm p-2 rounded bg-bambu-dark ${A.length>0?"hover:bg-bambu-dark-tertiary cursor-pointer":""}`,children:[l.jsxs("span",{className:"text-white flex items-center gap-2",children:[D9[w]||w,A.length>0&&(C?l.jsx(_b,{className:"w-3 h-3"}):l.jsx(Nl,{className:"w-3 h-3"}))]}),l.jsx("span",{className:"text-yellow-500 font-medium",children:T})]}),C&&A.length>0&&l.jsxs("div",{className:"mt-1 ml-4 p-2 rounded bg-bambu-dark-tertiary text-xs text-bambu-gray space-y-1",children:[A.slice(0,10).map((N,F)=>l.jsx("div",{children:N},F)),A.length>10&&l.jsxs("div",{className:"text-bambu-gray/60",children:["...and ",A.length-10," more"]})]})]},w)})})]}),b===0&&E===0&&l.jsx("div",{className:"p-4 text-center text-bambu-gray",children:"No data was found to restore in the backup file."})]}),l.jsx("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:l.jsx(ke,{onClick:t,children:"Close"})})]})]})})})}async function UGe(){const t=await fetch("/api/v1/settings/spoolman");if(!t.ok)throw new Error("Failed to load Spoolman settings");return t.json()}async function zGe(t){const e=await fetch("/api/v1/settings/spoolman",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(!e.ok)throw new Error("Failed to save Spoolman settings");return e.json()}function BGe(){const t=hn(),[e,n]=M.useState(!1),[r,i]=M.useState(""),[a,s]=M.useState("auto"),[o,c]=M.useState(!1),[u,d]=M.useState("all"),[f,g]=M.useState(!1),{data:p,isLoading:x}=xt({queryKey:["spoolman-settings"],queryFn:UGe}),{data:v,isLoading:y,refetch:b}=xt({queryKey:["spoolman-status"],queryFn:Ee.getSpoolmanStatus,refetchInterval:3e4}),{data:E}=xt({queryKey:["printers"],queryFn:Ee.getPrinters});M.useEffect(()=>{p&&(n(p.spoolman_enabled==="true"),i(p.spoolman_url||""),s(p.spoolman_sync_mode||"auto"),g(!0))},[p]),M.useEffect(()=>{if(!f||!p)return;if(p.spoolman_enabled==="true"!==e||(p.spoolman_url||"")!==r||(p.spoolman_sync_mode||"auto")!==a){const q=setTimeout(()=>{w.mutate()},500);return()=>clearTimeout(q)}},[e,r,a,f]);const w=_t({mutationFn:()=>zGe({spoolman_enabled:e?"true":"false",spoolman_url:r,spoolman_sync_mode:a}),onSuccess:()=>{t.invalidateQueries({queryKey:["spoolman-settings"]}),t.invalidateQueries({queryKey:["spoolman-status"]}),c(!0),setTimeout(()=>c(!1),2e3)}}),T=_t({mutationFn:Ee.connectSpoolman,onSuccess:()=>{b()}}),A=_t({mutationFn:Ee.disconnectSpoolman,onSuccess:()=>{b()}}),C=_t({mutationFn:Ee.syncAllPrintersAms,onSuccess:L=>{L.success}}),N=_t({mutationFn:L=>Ee.syncPrinterAms(L),onSuccess:L=>{L.success}}),F=()=>{u==="all"?C.mutate():N.mutate(u)},k=C.isPending||N.isPending,R=u==="all"?C.data:N.data,I=u==="all"?C.isSuccess:N.isSuccess;return x?l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(vp,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]})}),l.jsx(Tt,{children:l.jsx("div",{className:"flex justify-center py-8",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})})})]}):l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(vp,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]}),w.isPending&&l.jsx(gt,{className:"w-4 h-4 text-bambu-green animate-spin"}),o&&l.jsx(gl,{className:"w-4 h-4 text-bambu-green"})]})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Connect to Spoolman for filament inventory tracking. AMS data will sync automatically."}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Enable Spoolman"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Sync filament data with Spoolman server"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:e,onChange:L=>n(L.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Spoolman URL"}),l.jsx("input",{type:"text",placeholder:"http://192.168.1.100:7912",value:r,onChange:L=>i(L.target.value),disabled:!e,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none disabled:opacity-50"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"URL of your Spoolman server (e.g., http://localhost:7912)"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Sync Mode"}),l.jsxs("select",{value:a,onChange:L=>s(L.target.value),disabled:!e,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 disabled:opacity-50",children:[l.jsx("option",{value:"auto",children:"Automatic"}),l.jsx("option",{value:"manual",children:"Manual Only"})]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:a==="auto"?"AMS data syncs automatically when changes are detected":"Only sync when manually triggered"})]}),e&&l.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("span",{className:"text-sm text-bambu-gray",children:"Status:"}),y?l.jsx(gt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):v?.connected?l.jsxs("span",{className:"flex items-center gap-1 text-sm text-green-500",children:[l.jsx(gl,{className:"w-4 h-4"}),"Connected"]}):l.jsxs("span",{className:"flex items-center gap-1 text-sm text-red-500",children:[l.jsx(Vt,{className:"w-4 h-4"}),"Disconnected"]})]}),l.jsx("div",{className:"flex gap-2",children:v?.connected?l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>A.mutate(),disabled:A.isPending,children:[A.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cce,{className:"w-4 h-4"}),"Disconnect"]}):l.jsxs(ke,{size:"sm",onClick:()=>T.mutate(),disabled:T.isPending||!r,children:[T.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Gk,{className:"w-4 h-4"}),"Connect"]})})]}),T.isError&&l.jsx("div",{className:"mb-3 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400",children:T.error.message}),v?.connected&&l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Sync AMS Data"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Manually sync printer AMS data to Spoolman"})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("div",{className:"relative flex-1",children:[l.jsxs("select",{value:u,onChange:L=>d(L.target.value==="all"?"all":Number(L.target.value)),className:"w-full px-3 py-2 pr-8 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none appearance-none cursor-pointer",children:[l.jsx("option",{value:"all",children:"All Printers"}),E?.map(L=>l.jsx("option",{value:L.id,children:L.name},L.id))]}),l.jsx(Nl,{className:"absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:F,disabled:k,children:[k?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ya,{className:"w-4 h-4"}),"Sync"]})]})]}),I&&R&&l.jsx("div",{className:`mt-2 p-2 rounded text-sm ${R.success?"bg-green-500/20 border border-green-500/50 text-green-400":"bg-yellow-500/20 border border-yellow-500/50 text-yellow-400"}`,children:R.success?`Synced ${R.synced_count} trays successfully`:`Synced ${R.synced_count} trays with ${R.errors.length} errors`})]})]})]})}function HGe({link:t,onClose:e}){const n=hn(),{theme:r}=Eb(),i=!!t,a=M.useRef(null),[s,o]=M.useState(t?.name||""),[c,u]=M.useState(t?.url||""),[d,f]=M.useState(t?.icon||"link"),[g,p]=M.useState(!!t?.custom_icon),[x,v]=M.useState(t?.custom_icon?Ee.getExternalLinkIconUrl(t.id):null),[y,b]=M.useState(null),[E,w]=M.useState(null);M.useEffect(()=>{const I=L=>{L.key==="Escape"&&e()};return window.addEventListener("keydown",I),()=>window.removeEventListener("keydown",I)},[e]);const T=_t({mutationFn:async I=>{const L=await Ee.createExternalLink(I);return y?await Ee.uploadExternalLinkIcon(L.id,y):L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:I=>{w(I.message)}}),A=_t({mutationFn:async I=>{let L=await Ee.updateExternalLink(t.id,I);return y?L=await Ee.uploadExternalLinkIcon(t.id,y):!g&&t?.custom_icon&&(L=await Ee.deleteExternalLinkIcon(t.id)),L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:I=>{w(I.message)}}),C=I=>{const L=I.target.files?.[0];if(L){if(!["image/png","image/jpeg","image/gif","image/svg+xml","image/webp","image/x-icon"].includes(L.type)){w("Please select a valid image file (PNG, JPG, GIF, SVG, WebP, or ICO)");return}if(L.size>1024*1024){w("Image file must be less than 1MB");return}b(L),p(!0);const j=new FileReader;j.onload=V=>{v(V.target?.result)},j.readAsDataURL(L)}},N=()=>{b(null),v(null),p(!1),a.current&&(a.current.value="")},F=I=>{if(I.preventDefault(),w(null),!s.trim()){w("Name is required");return}if(!c.trim()){w("URL is required");return}if(!c.startsWith("http://")&&!c.startsWith("https://")){w("URL must start with http:// or https://");return}const L={name:s.trim(),url:c.trim(),icon:d};i?A.mutate(L):T.mutate(L)},k=T.isPending||A.isPending,R=eM(d);return l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-md",onClick:I=>I.stopPropagation(),children:[l.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:g&&x?l.jsx("img",{src:x,alt:"",className:`w-5 h-5 rounded ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}):l.jsx(R,{className:"w-5 h-5"})}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:i?"Edit Link":"Add External Link"})]}),l.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:F,className:"p-6 space-y-4",children:[E&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:E}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),l.jsx("input",{type:"text",value:s,onChange:I=>o(I.target.value),placeholder:"My Link",maxLength:50,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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"URL *"}),l.jsx("input",{type:"text",value:c,onChange:I=>u(I.target.value),placeholder:"https://example.com",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"})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("label",{className:"block text-sm text-bambu-gray",children:"Icon"}),l.jsxs("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsx("span",{className:"text-sm text-white",children:"Custom Icon"}),l.jsx("input",{ref:a,type:"file",accept:"image/png,image/jpeg,image/gif,image/svg+xml,image/webp,image/x-icon",className:"hidden",onChange:C}),g&&x?l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("img",{src:x,alt:"Custom icon",className:`w-8 h-8 rounded border border-bambu-dark-tertiary ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}),l.jsx("button",{type:"button",onClick:N,className:"p-1 text-red-400 hover:text-red-300 transition-colors",title:"Remove custom icon",children:l.jsx(Sn,{className:"w-4 h-4"})})]}):l.jsxs(ke,{type:"button",variant:"secondary",size:"sm",onClick:()=>a.current?.click(),children:[l.jsx(ia,{className:"w-4 h-4"}),"Upload"]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"PNG, JPG, GIF, SVG, WebP, or ICO. Max 1MB."})]}),!g&&l.jsxs("div",{children:[l.jsx("span",{className:"text-sm text-bambu-gray block mb-2",children:"Or choose a preset icon"}),l.jsx(yde,{value:d,onChange:f})]})]}),l.jsxs("div",{className:"flex gap-3 pt-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:k,className:"flex-1",children:[k?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Ms,{className:"w-4 h-4"}),i?"Save":"Add"]})]})]})]})})}function $Ge(){const t=hn(),[e,n]=M.useState(!1),[r,i]=M.useState(null),[a,s]=M.useState(null),[o,c]=M.useState(null),{data:u,isLoading:d}=xt({queryKey:["external-links"],queryFn:Ee.getExternalLinks}),f=_t({mutationFn:E=>Ee.deleteExternalLink(E),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),g=_t({mutationFn:E=>Ee.reorderExternalLinks(E),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),p=(E,w)=>{c(w),E.dataTransfer.effectAllowed="move"},x=E=>{E.preventDefault(),E.dataTransfer.dropEffect="move"},v=(E,w)=>{if(E.preventDefault(),o===null||o===w||!u)return;const T=u.map(F=>F.id),A=T.indexOf(o),C=T.indexOf(w);if(A===-1||C===-1)return;const N=[...T];N.splice(A,1),N.splice(C,0,o),g.mutate(N),c(null)},y=E=>{s(E)},b=()=>{a&&(f.mutate(a.id),s(null))};return l.jsxs(l.Fragment,{children:[l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Gk,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Sidebar Links"})]}),l.jsxs(ke,{size:"sm",onClick:()=>n(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Link"]})]})}),l.jsxs(Tt,{children:[l.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Add external links to the sidebar navigation. Drag to reorder."}),d?l.jsx("div",{className:"flex justify-center py-8",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):u&&u.length>0?l.jsx("div",{className:"space-y-2",children:u.map(E=>{const w=eM(E.icon);return l.jsxs("div",{draggable:!0,onDragStart:T=>p(T,E.id),onDragOver:x,onDrop:T=>v(T,E.id),className:`flex items-center gap-3 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary transition-colors ${o===E.id?"opacity-50":""}`,children:[l.jsx(N0,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray cursor-grab flex-shrink-0"}),l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-gray",children:l.jsx(w,{className:"w-4 h-4"})}),l.jsxs("div",{className:"flex-1 min-w-0",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("span",{className:"text-white font-medium truncate",children:E.name}),l.jsx(ga,{className:"w-3 h-3 text-bambu-gray flex-shrink-0"})]}),l.jsx("span",{className:"text-sm text-bambu-gray truncate block",children:E.url})]}),l.jsxs("div",{className:"flex items-center gap-1 flex-shrink-0",children:[l.jsx("button",{onClick:()=>i(E),className:"p-2 rounded-lg hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors",title:"Edit",children:l.jsx(Fc,{className:"w-4 h-4"})}),l.jsx("button",{onClick:()=>y(E),disabled:f.isPending,className:"p-2 rounded-lg hover:bg-red-500/20 text-bambu-gray hover:text-red-400 transition-colors disabled:opacity-50",title:"Delete",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]},E.id)})}):l.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[l.jsx(Gk,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),l.jsx("p",{children:"No external links configured"}),l.jsx("p",{className:"text-sm",children:'Click "Add Link" to add one'})]})]})]}),(e||r)&&l.jsx(HGe,{link:r,onClose:()=>{n(!1),i(null)}}),a&&l.jsx(Lr,{title:"Delete Link",message:`Are you sure you want to delete "${a.name}"? This action cannot be undone.`,confirmText:"Delete",cancelText:"Cancel",variant:"danger",onConfirm:b,onCancel:()=>s(null)})]})}function VGe(){const t=hn(),{showToast:e}=vr(),[n,r]=M.useState(!1),[i,a]=M.useState(""),[s,o]=M.useState("immediate"),[c,u]=M.useState(!1),{data:d,isLoading:f}=xt({queryKey:["virtual-printer-settings"],queryFn:Yk.getSettings,refetchInterval:1e4});M.useEffect(()=>{d&&(r(d.enabled),o(d.mode))},[d]);const g=_t({mutationFn:E=>Yk.updateSettings(E),onSuccess:()=>{t.invalidateQueries({queryKey:["virtual-printer-settings"]}),e("Virtual printer settings updated")},onError:E=>{e(E.message||"Failed to update settings","error"),d&&(r(d.enabled),o(d.mode))}}),p=()=>{const E=!n;if(E&&!i&&!d?.access_code_set){e("Please set an access code first","error");return}r(E),g.mutate({enabled:E,access_code:i||void 0,mode:s})},x=()=>{if(!i){e("Access code cannot be empty","error");return}if(i.length!==8){e("Access code must be exactly 8 characters","error");return}g.mutate({access_code:i}),a("")},v=E=>{o(E),g.mutate({mode:E})};if(f)return l.jsx(St,{children:l.jsx(Tt,{className:"py-8 flex justify-center",children:l.jsx(gt,{className:"w-6 h-6 animate-spin text-bambu-green"})})});const y=d?.status,b=y?.running||!1;return l.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[l.jsx("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Ui,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Virtual Printer"})]}),y&&l.jsxs("div",{className:`flex items-center gap-2 text-sm ${b?"text-green-400":"text-bambu-gray"}`,children:[l.jsx("span",{className:`w-2 h-2 rounded-full ${b?"bg-green-400 animate-pulse":"bg-gray-500"}`}),b?"Running":"Stopped"]})]})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Enable a virtual printer that appears in Bambu Studio and OrcaSlicer. Files sent to this printer will be archived directly without printing."}),l.jsxs("div",{className:"flex items-center justify-between py-3 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{children:[l.jsx("div",{className:"text-white font-medium",children:"Enable Virtual Printer"}),l.jsx("div",{className:"text-sm text-bambu-gray",children:b?'Visible as "Bambuddy" in slicer discovery':"Not visible to slicers"})]}),l.jsx("button",{onClick:p,disabled:g.isPending,className:`relative w-12 h-6 rounded-full transition-colors ${n?"bg-bambu-green":"bg-bambu-dark-tertiary"} ${g.isPending?"opacity-50":""}`,children:l.jsx("span",{className:`absolute top-1 left-1 w-4 h-4 bg-white rounded-full transition-transform ${n?"translate-x-6":""}`})})]}),l.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[l.jsx("div",{className:"text-white font-medium mb-2",children:"Access Code"}),l.jsx("div",{className:"text-sm text-bambu-gray mb-3",children:d?.access_code_set?l.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[l.jsx(gl,{className:"w-4 h-4"}),"Access code is set"]}):l.jsxs("span",{className:"flex items-center gap-1 text-yellow-400",children:[l.jsx($n,{className:"w-4 h-4"}),"No access code set - required to enable"]})}),l.jsxs("div",{className:"flex gap-2",children:[l.jsxs("div",{className:"relative flex-1",children:[l.jsx("input",{type:c?"text":"password",value:i,onChange:E=>a(E.target.value),placeholder:d?.access_code_set?"Enter new code to change":"Enter 8-char code",maxLength:8,className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-2 text-white placeholder-bambu-gray pr-10 font-mono"}),l.jsx("button",{onClick:()=>u(!c),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:c?l.jsx(KS,{className:"w-4 h-4"}):l.jsx(Yh,{className:"w-4 h-4"})})]}),l.jsx(ke,{onClick:x,disabled:!i||g.isPending,variant:"primary",children:g.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):"Save"})]}),l.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["Must be exactly 8 characters. Used by slicers to authenticate.",i&&l.jsxs("span",{className:i.length===8?"text-green-400":"text-yellow-400",children:[" ","(",i.length,"/8)"]})]})]}),l.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[l.jsx("div",{className:"text-white font-medium mb-2",children:"Archive Mode"}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("button",{onClick:()=>v("immediate"),disabled:g.isPending,className:`p-3 rounded-lg border text-left transition-colors ${s==="immediate"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[l.jsx("div",{className:"text-white font-medium",children:"Immediate"}),l.jsx("div",{className:"text-xs text-bambu-gray",children:"Archive files as soon as they are uploaded"})]}),l.jsxs("button",{onClick:()=>v("queue"),disabled:g.isPending,className:`p-3 rounded-lg border text-left transition-colors ${s==="queue"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[l.jsx("div",{className:"text-white font-medium",children:"Queue for Review"}),l.jsx("div",{className:"text-xs text-bambu-gray",children:"Review and tag files before archiving"})]})]})]})]})]})}),l.jsxs("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:[l.jsx(St,{children:l.jsx(Tt,{className:"py-4",children:l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx(_p,{className:"w-5 h-5 text-blue-400 flex-shrink-0 mt-0.5"}),l.jsxs("div",{className:"text-sm text-bambu-gray",children:[l.jsx("p",{className:"mb-2",children:l.jsx("strong",{className:"text-white",children:"How it works:"})}),l.jsxs("ol",{className:"list-decimal list-inside space-y-1",children:[l.jsx("li",{children:"Enable the virtual printer and set an access code"}),l.jsx("li",{children:'In Bambu Studio or OrcaSlicer, go to "Add Printer"'}),l.jsx("li",{children:'The "Bambuddy" printer should appear in the discovery list'}),l.jsx("li",{children:"Connect using the access code you set"}),l.jsx("li",{children:'When you "print" to Bambuddy, the 3MF file is archived instead'})]}),l.jsxs("p",{className:"mt-3 text-yellow-400/80",children:[l.jsx($n,{className:"w-4 h-4 inline mr-1"}),"Required ports: 2021 (SSDP), 8883 (MQTT), 990 (FTP)"]}),l.jsxs("div",{className:"mt-2 text-xs text-bambu-gray space-y-1",children:[l.jsx("p",{children:"Port 990 requires root or iptables redirect:"}),l.jsx("code",{className:"block bg-bambu-dark-tertiary px-2 py-1 rounded text-[10px]",children:"sudo iptables -t nat -A PREROUTING -p tcp --dport 990 -j REDIRECT --to-port 9990"}),l.jsx("code",{className:"block bg-bambu-dark-tertiary px-2 py-1 rounded text-[10px]",children:"sudo iptables -t nat -A OUTPUT -o lo -p tcp --dport 990 -j REDIRECT --to-port 9990"})]})]})]})})}),y&&b&&l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h3",{className:"text-md font-semibold text-white",children:"Status Details"})}),l.jsx(Tt,{children:l.jsxs("div",{className:"grid grid-cols-2 gap-4 text-sm",children:[l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Printer Name"}),l.jsx("div",{className:"text-white",children:y.name})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Model"}),l.jsx("div",{className:"text-white",children:y.model?.replace("3DPrinter-","").replace("-"," ")||"X1 Carbon"})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Serial Number"}),l.jsx("div",{className:"text-white font-mono",children:y.serial})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Mode"}),l.jsx("div",{className:"text-white capitalize",children:y.mode})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Pending Files"}),l.jsx("div",{className:"text-white",children:y.pending_files})]})]})})]})]})]})}function GGe(){const t=hn(),{t:e,i18n:n}=Qp(),{showToast:r,showPersistentToast:i,dismissToast:a}=vr(),[s,o]=M.useState(null),[c,u]=M.useState(!1),[d,f]=M.useState(null),[g,p]=M.useState(!1),[x,v]=M.useState(null),[y,b]=M.useState(null),[E,w]=M.useState(!1),[T,A]=M.useState(zG()),[C,N]=M.useState("general"),[F,k]=M.useState(!1),[R,I]=M.useState(""),[L,q]=M.useState({can_queue:!0,can_control_printer:!1,can_read_status:!0}),[j,V]=M.useState(null),[K,G]=M.useState(null),[J,ee]=M.useState(!1),[Q,B]=M.useState(!1),[$,Z]=M.useState(null),[te,he]=M.useState(!1),[de,ge]=M.useState(!1),[Ae,me]=M.useState(!1),[ce,Pe]=M.useState(!1),oe=Se=>{A(Se),wde(Se)},Le=()=>{localStorage.removeItem("sidebarOrder"),window.location.reload()},{data:z,isLoading:H}=xt({queryKey:["settings"],queryFn:Ee.getSettings}),{data:ne,isLoading:W}=xt({queryKey:["smart-plugs"],queryFn:Ee.getSmartPlugs}),{data:Y,isLoading:se}=xt({queryKey:["smart-plugs-energy",ne?.map(Se=>Se.id)],queryFn:async()=>{if(!ne||ne.length===0)return null;const Se=await Promise.all(ne.filter(nn=>nn.enabled).map(async nn=>{try{const Ur=await Ee.getSmartPlugStatus(nn.id);return{plug:nn,status:Ur}}catch{return{plug:nn,status:null}}}));let bt=0,Gt=0,kt=0,Tn=0,Nn=0;for(const{status:nn}of Se)nn?.reachable&&nn.energy&&(Nn++,nn.energy.power!=null&&(bt+=nn.energy.power),nn.energy.today!=null&&(Gt+=nn.energy.today),nn.energy.yesterday!=null&&(kt+=nn.energy.yesterday),nn.energy.total!=null&&(Tn+=nn.energy.total));return{totalPower:bt,totalToday:Gt,totalYesterday:kt,totalLifetime:Tn,reachableCount:Nn,totalPlugs:ne.filter(nn=>nn.enabled).length}},enabled:C==="plugs"&&!!ne&&ne.length>0,refetchInterval:C==="plugs"?1e4:!1}),{data:be,isLoading:Te}=xt({queryKey:["notification-providers"],queryFn:Ee.getNotificationProviders}),{data:Me,isLoading:U}=xt({queryKey:["api-keys"],queryFn:Ee.getAPIKeys}),O=_t({mutationFn:Se=>Ee.createAPIKey(Se),onSuccess:Se=>{V(Se.key||null),k(!1),I(""),t.invalidateQueries({queryKey:["api-keys"]}),r("API key created")},onError:Se=>{r(`Failed to create API key: ${Se.message}`,"error")}}),re=_t({mutationFn:Se=>Ee.deleteAPIKey(Se),onSuccess:()=>{t.invalidateQueries({queryKey:["api-keys"]}),r("API key deleted")},onError:Se=>{r(`Failed to delete API key: ${Se.message}`,"error")}}),{data:ve}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:we,isLoading:ye}=xt({queryKey:["notification-templates"],queryFn:Ee.getNotificationTemplates}),{data:rt}=xt({queryKey:["virtual-printer-settings"],queryFn:Yk.getSettings,refetchInterval:1e4}),Ve=rt?.status?.running??!1,{data:tt}=xt({queryKey:["ffmpeg-status"],queryFn:Ee.checkFfmpeg}),{data:ut}=xt({queryKey:["version"],queryFn:Ee.getVersion}),{data:ue,refetch:ae,isRefetching:$e}=xt({queryKey:["updateCheck"],queryFn:Ee.checkForUpdates,staleTime:300*1e3}),{data:Ue,refetch:Ze}=xt({queryKey:["updateStatus"],queryFn:Ee.getUpdateStatus,refetchInterval:Se=>{const bt=Se.state.data;return bt?.status==="downloading"||bt?.status==="installing"?1e3:!1}}),dt=_t({mutationFn:Ee.applyUpdate,onSuccess:()=>{Ze()}}),[fe,Je]=M.useState(null),xe=_t({mutationFn:Ee.testAllNotificationProviders,onSuccess:Se=>{Je(Se),t.invalidateQueries({queryKey:["notification-providers"]}),Se.failed===0?r(`All ${Se.tested} providers tested successfully!`,"success"):r(`${Se.success}/${Se.tested} providers succeeded`,Se.failed>0?"error":"success")},onError:Se=>{r(`Failed to test providers: ${Se.message}`,"error")}}),ze=_t({mutationFn:async Se=>{if(!ne)return{success:0,failed:0};const bt=ne.filter(kt=>kt.enabled),Gt=await Promise.all(bt.map(async kt=>{try{return await Ee.controlSmartPlug(kt.id,Se),{success:!0}}catch{return{success:!1}}}));return{success:Gt.filter(kt=>kt.success).length,failed:Gt.filter(kt=>!kt.success).length}},onSuccess:(Se,bt)=>{t.invalidateQueries({queryKey:["smart-plugs"]}),t.invalidateQueries({queryKey:["smart-plugs-energy"]}),Se.failed===0?r(`All ${Se.success} plugs turned ${bt}`,"success"):r(`${Se.success} plugs turned ${bt}, ${Se.failed} failed`,"error")},onError:Se=>{r(`Failed: ${Se.message}`,"error")}}),Re=M.useRef(null),_e=M.useRef(!0);M.useEffect(()=>{z&&!s&&(o(z),setTimeout(()=>{_e.current=!1},100))},[z,s]);const qe=_t({mutationFn:Ee.updateSettings,onSuccess:Se=>{t.setQueryData(["settings"],Se),t.invalidateQueries({queryKey:["archiveStats"]}),r("Settings saved","success")},onError:Se=>{r(`Failed to save: ${Se.message}`,"error")}});M.useEffect(()=>{if(!(_e.current||!s||!z||!(z.auto_archive!==s.auto_archive||z.save_thumbnails!==s.save_thumbnails||z.capture_finish_photo!==s.capture_finish_photo||z.default_filament_cost!==s.default_filament_cost||z.currency!==s.currency||z.energy_cost_per_kwh!==s.energy_cost_per_kwh||z.energy_tracking_mode!==s.energy_tracking_mode||z.check_updates!==s.check_updates||z.notification_language!==s.notification_language||z.telemetry_enabled!==s.telemetry_enabled||z.ams_humidity_good!==s.ams_humidity_good||z.ams_humidity_fair!==s.ams_humidity_fair||z.ams_temp_good!==s.ams_temp_good||z.ams_temp_fair!==s.ams_temp_fair||z.ams_history_retention_days!==s.ams_history_retention_days||z.date_format!==s.date_format||z.time_format!==s.time_format||z.default_printer_id!==s.default_printer_id)))return Re.current&&clearTimeout(Re.current),Re.current=setTimeout(()=>{const bt={auto_archive:s.auto_archive,save_thumbnails:s.save_thumbnails,capture_finish_photo:s.capture_finish_photo,default_filament_cost:s.default_filament_cost,currency:s.currency,energy_cost_per_kwh:s.energy_cost_per_kwh,energy_tracking_mode:s.energy_tracking_mode,check_updates:s.check_updates,notification_language:s.notification_language,telemetry_enabled:s.telemetry_enabled,ams_humidity_good:s.ams_humidity_good,ams_humidity_fair:s.ams_humidity_fair,ams_temp_good:s.ams_temp_good,ams_temp_fair:s.ams_temp_fair,ams_history_retention_days:s.ams_history_retention_days,date_format:s.date_format,time_format:s.time_format,default_printer_id:s.default_printer_id};qe.mutate(bt)},500),()=>{Re.current&&clearTimeout(Re.current)}},[s,z,qe]);const lt=M.useCallback((Se,bt)=>{o(Gt=>Gt?{...Gt,[Se]:bt}:null)},[]);return H||!s?l.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):l.jsxs("div",{className:"p-4 md:p-8",children:[l.jsxs("div",{className:"mb-8",children:[l.jsx("h1",{className:"text-2xl font-bold text-white",children:"Settings"}),l.jsx("p",{className:"text-bambu-gray",children:"Configure Bambuddy"})]}),l.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[l.jsx("button",{onClick:()=>N("general"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px ${C==="general"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:"General"}),l.jsxs("button",{onClick:()=>N("plugs"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="plugs"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(so,{className:"w-4 h-4"}),"Smart Plugs",ne&&ne.length>0&&l.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:ne.length})]}),l.jsxs("button",{onClick:()=>N("notifications"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="notifications"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(Ph,{className:"w-4 h-4"}),"Notifications",be&&be.length>0&&l.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:be.length})]}),l.jsxs("button",{onClick:()=>N("apikeys"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="apikeys"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(Qm,{className:"w-4 h-4"}),"API Keys",Me&&Me.length>0&&l.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:Me.length})]}),l.jsxs("button",{onClick:()=>N("virtual-printer"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="virtual-printer"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(Ui,{className:"w-4 h-4"}),"Virtual Printer",l.jsx("span",{className:`w-2 h-2 rounded-full ${Ve?"bg-green-400":"bg-gray-500"}`})]})]}),C==="general"&&l.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[l.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-xl",children:[l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:e("settings.general")})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{children:[l.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[l.jsx(A0,{className:"w-4 h-4 inline mr-1"}),e("settings.language")]}),l.jsx("select",{value:n.language,onChange:Se=>n.changeLanguage(Se.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",children:qF.map(Se=>l.jsxs("option",{value:Se.code,children:[Se.nativeName," (",Se.name,")"]},Se.code))}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:e("settings.languageDescription")})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:e("settings.defaultView")}),l.jsx("select",{value:T,onChange:Se=>oe(Se.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",children:k0.map(Se=>l.jsx("option",{value:Se.to,children:e(Se.labelKey)},Se.id))}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:e("settings.defaultViewDescription")})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Date Format"}),l.jsxs("select",{value:s.date_format||"system",onChange:Se=>lt("date_format",Se.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",children:[l.jsx("option",{value:"system",children:"System Default"}),l.jsx("option",{value:"us",children:"US (MM/DD/YYYY)"}),l.jsx("option",{value:"eu",children:"EU (DD/MM/YYYY)"}),l.jsx("option",{value:"iso",children:"ISO (YYYY-MM-DD)"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Time Format"}),l.jsxs("select",{value:s.time_format||"system",onChange:Se=>lt("time_format",Se.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",children:[l.jsx("option",{value:"system",children:"System Default"}),l.jsx("option",{value:"12h",children:"12-hour (3:30 PM)"}),l.jsx("option",{value:"24h",children:"24-hour (15:30)"})]})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default Printer"}),l.jsxs("select",{value:s.default_printer_id??"",onChange:Se=>lt("default_printer_id",Se.target.value?Number(Se.target.value):null),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:[l.jsx("option",{value:"",children:"No default (ask each time)"}),ve?.map(Se=>l.jsx("option",{value:Se.id,children:Se.name},Se.id))]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Pre-select this printer for uploads, reprints, and other operations."})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Sidebar order"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Drag items in the sidebar to reorder. Reset to default order here."})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:Le,children:[l.jsx(ng,{className:"w-4 h-4"}),"Reset"]})]})]})]}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Archive Settings"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Auto-archive prints"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically save 3MF files when prints complete"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.auto_archive,onChange:Se=>lt("auto_archive",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Save thumbnails"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Extract and save preview images from 3MF files"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.save_thumbnails,onChange:Se=>lt("save_thumbnails",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Capture finish photo"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Take a photo from printer camera when print completes"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.capture_finish_photo,onChange:Se=>lt("capture_finish_photo",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),s.capture_finish_photo&&tt&&!tt.installed&&l.jsxs("div",{className:"flex items-start gap-2 p-3 bg-yellow-500/10 border border-yellow-500/30 rounded-lg",children:[l.jsx($n,{className:"w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5"}),l.jsxs("div",{className:"text-sm",children:[l.jsx("p",{className:"text-yellow-500 font-medium",children:"ffmpeg not installed"}),l.jsxs("p",{className:"text-bambu-gray mt-1",children:["Camera capture requires ffmpeg. Install it via"," ",l.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"brew install ffmpeg"})," (macOS) or"," ",l.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"apt install ffmpeg"})," (Linux)."]})]})]})]})]}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Cost Tracking"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default filament cost (per kg)"}),l.jsx("input",{type:"number",step:"0.01",min:"0",value:s.default_filament_cost,onChange:Se=>lt("default_filament_cost",parseFloat(Se.target.value)||0),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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Currency"}),l.jsxs("select",{value:s.currency,onChange:Se=>lt("currency",Se.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",children:[l.jsx("option",{value:"USD",children:"USD ($)"}),l.jsx("option",{value:"EUR",children:"EUR (€)"}),l.jsx("option",{value:"GBP",children:"GBP (£)"}),l.jsx("option",{value:"CHF",children:"CHF (Fr.)"}),l.jsx("option",{value:"JPY",children:"JPY (¥)"}),l.jsx("option",{value:"CNY",children:"CNY (¥)"}),l.jsx("option",{value:"CAD",children:"CAD ($)"}),l.jsx("option",{value:"AUD",children:"AUD ($)"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Electricity cost per kWh"}),l.jsx("input",{type:"number",step:"0.01",min:"0",value:s.energy_cost_per_kwh,onChange:Se=>lt("energy_cost_per_kwh",parseFloat(Se.target.value)||0),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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Energy display mode"}),l.jsxs("select",{value:s.energy_tracking_mode||"total",onChange:Se=>lt("energy_tracking_mode",Se.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",children:[l.jsx("option",{value:"print",children:"Prints Only"}),l.jsx("option",{value:"total",children:"Total Consumption"})]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:s.energy_tracking_mode==="print"?"Dashboard shows sum of energy used during prints":"Dashboard shows lifetime energy from smart plugs"})]})]})]})]}),l.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-md",children:[l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"AMS Display Thresholds"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Configure color thresholds for AMS humidity and temperature indicators."}),l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-white",children:[l.jsx(SG,{className:"w-4 h-4 text-blue-400"}),l.jsx("span",{className:"font-medium",children:"Humidity"})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (green) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",min:"0",max:"100",value:s.ams_humidity_good??40,onChange:Se=>lt("ams_humidity_good",parseInt(Se.target.value)||40),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"}),l.jsx("span",{className:"text-bambu-gray",children:"%"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",min:"0",max:"100",value:s.ams_humidity_fair??60,onChange:Se=>lt("ams_humidity_fair",parseInt(Se.target.value)||60),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"}),l.jsx("span",{className:"text-bambu-gray",children:"%"})]})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (bad)"})]}),l.jsxs("div",{className:"space-y-3 pt-2 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-2 text-white",children:[l.jsx(JS,{className:"w-4 h-4 text-orange-400"}),l.jsx("span",{className:"font-medium",children:"Temperature"})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (blue) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:s.ams_temp_good??28,onChange:Se=>lt("ams_temp_good",parseFloat(Se.target.value)||28),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"}),l.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:s.ams_temp_fair??35,onChange:Se=>lt("ams_temp_fair",parseFloat(Se.target.value)||35),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"}),l.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (hot)"})]}),l.jsxs("div",{className:"space-y-3 pt-4 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-2 text-white",children:[l.jsx(vp,{className:"w-4 h-4 text-purple-400"}),l.jsx("span",{className:"font-medium",children:"History Retention"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Keep sensor history for"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",min:"1",max:"365",value:s.ams_history_retention_days??30,onChange:Se=>lt("ams_history_retention_days",parseInt(Se.target.value)||30),className:"w-24 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"}),l.jsx("span",{className:"text-bambu-gray",children:"days"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Older humidity and temperature data will be automatically deleted"})]})]})]}),l.jsx(BGe,{})]}),l.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-sm",children:[l.jsx($Ge,{}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Updates"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Check for updates"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically check for new versions on startup"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.check_updates,onChange:Se=>lt("check_updates",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("p",{className:"text-white",children:e("settings.telemetry")}),l.jsxs("button",{onClick:()=>me(!0),className:"inline-flex items-center gap-1 px-2 py-0.5 text-xs bg-bambu-dark rounded-full text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary transition-colors",children:[l.jsx(_p,{className:"w-3 h-3"}),e("settings.telemetryLearnMore")]})]}),l.jsx("p",{className:"text-sm text-bambu-gray",children:e("settings.telemetryDescription")})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.telemetry_enabled,onChange:Se=>lt("telemetry_enabled",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Current version"}),l.jsxs("p",{className:"text-sm text-bambu-gray",children:["v",ut?.version||"..."]})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>ae(),disabled:$e,children:[$e?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ya,{className:"w-4 h-4"}),"Check now"]})]}),ue?.update_available?l.jsxs("div",{className:"mt-4 p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:[l.jsxs("div",{className:"flex items-start justify-between",children:[l.jsxs("div",{children:[l.jsxs("p",{className:"text-bambu-green font-medium",children:["Update available: v",ue.latest_version]}),ue.release_name&&ue.release_name!==ue.latest_version&&l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:ue.release_name})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[ue.release_notes&&l.jsx("button",{onClick:()=>Pe(!0),className:"text-bambu-gray hover:text-white transition-colors text-sm underline",children:"Release Notes"}),ue.release_url&&l.jsx("a",{href:ue.release_url,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-white transition-colors",title:"View release on GitHub",children:l.jsx(ga,{className:"w-4 h-4"})})]})]}),Ue?.status==="downloading"||Ue?.status==="installing"?l.jsxs("div",{className:"mt-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[l.jsx(gt,{className:"w-4 h-4 animate-spin"}),l.jsx("span",{children:Ue.message})]}),l.jsx("div",{className:"mt-2 w-full bg-bambu-dark-tertiary rounded-full h-2",children:l.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${Ue.progress}%`}})})]}):Ue?.status==="complete"?l.jsx("div",{className:"mt-3 p-2 bg-bambu-green/20 rounded text-sm text-bambu-green",children:Ue.message}):Ue?.status==="error"?l.jsx("div",{className:"mt-3 p-2 bg-red-500/20 rounded text-sm text-red-400",children:Ue.error||Ue.message}):l.jsxs(ke,{className:"mt-3",onClick:()=>dt.mutate(),disabled:dt.isPending,children:[dt.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ns,{className:"w-4 h-4"}),"Install Update"]})]}):ue?.error?l.jsxs("div",{className:"mt-2 p-2 bg-red-500/10 border border-red-500/30 rounded text-sm text-red-400",children:["Failed to check for updates: ",ue.error]}):ue&&!ue.update_available?l.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:"You're running the latest version"}):null]})]})]}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Data Management"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Backup Data"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Export settings, providers, printers, and more"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>he(!0),children:[l.jsx(ns,{className:"w-4 h-4"}),"Export"]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Restore Backup"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Import settings from a backup file with duplicate handling options"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>ge(!0),children:[l.jsx(ia,{className:"w-4 h-4"}),"Restore"]})]}),l.jsx("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Clear Notification Logs"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Delete notification logs older than 30 days"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>ee(!0),children:[l.jsx(Sn,{className:"w-4 h-4"}),"Clear"]})]})}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Reset UI Preferences"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Reset sidebar order, theme, view modes, and layout preferences. Printers, archives, and settings are not affected."})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>B(!0),children:[l.jsx(Sn,{className:"w-4 h-4"}),"Reset"]})]})]})]})]})]}),C==="plugs"&&l.jsxs("div",{className:"max-w-4xl",children:[l.jsxs("div",{className:"flex items-start justify-between mb-6",children:[l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(so,{className:"w-5 h-5 text-bambu-green"}),"Smart Plugs"]}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Connect Tasmota-based smart plugs to automate power control and track energy usage for your printers."})]}),l.jsxs("div",{className:"flex items-center gap-2 pt-1 shrink-0",children:[ne&&ne.filter(Se=>Se.enabled).length>1&&l.jsxs(l.Fragment,{children:[l.jsxs(ke,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Z("on"),disabled:ze.isPending,title:"Turn all plugs on",children:[ze.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Cl,{className:"w-4 h-4 text-bambu-green"}),"All On"]}),l.jsxs(ke,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Z("off"),disabled:ze.isPending,title:"Turn all plugs off",children:[ze.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ZS,{className:"w-4 h-4 text-red-400"}),"All Off"]})]}),l.jsxs(ke,{className:"whitespace-nowrap",onClick:()=>{f(null),u(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Smart Plug"]})]})]}),ne&&ne.length>0&&l.jsxs(St,{className:"mb-6",children:[l.jsx(Qi,{children:l.jsxs("h3",{className:"text-base font-semibold text-white flex items-center gap-2",children:[l.jsx(Jh,{className:"w-4 h-4 text-yellow-400"}),"Energy Summary",se&&l.jsx(gt,{className:"w-4 h-4 animate-spin text-bambu-gray ml-2"})]})}),l.jsx(Tt,{children:Y?l.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(Jh,{className:"w-3 h-3"}),"Current Power"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalPower.toFixed(1),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"W"})]}),l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:[Y.reachableCount,"/",Y.totalPlugs," plugs online"]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(qr,{className:"w-3 h-3"}),"Today"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalToday.toFixed(2),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),s&&s.energy_cost_per_kwh>0&&l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Y.totalToday*s.energy_cost_per_kwh).toFixed(2)," ",s.currency]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(DG,{className:"w-3 h-3"}),"Yesterday"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalYesterday.toFixed(2),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),s&&s.energy_cost_per_kwh>0&&l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Y.totalYesterday*s.energy_cost_per_kwh).toFixed(2)," ",s.currency]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(Hk,{className:"w-3 h-3"}),"Total"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalLifetime.toFixed(1),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),s&&s.energy_cost_per_kwh>0&&l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Y.totalLifetime*s.energy_cost_per_kwh).toFixed(2)," ",s.currency]})]})]}):se?null:l.jsx("p",{className:"text-sm text-bambu-gray",children:"Enable plugs to see energy summary"})})]}),W?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):ne&&ne.length>0?l.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:ne.map(Se=>l.jsx(AGe,{plug:Se,onEdit:bt=>{f(bt),u(!0)}},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-12",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(so,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),l.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No smart plugs configured"}),l.jsx("p",{className:"text-sm mb-4",children:"Add a Tasmota-based smart plug to track energy usage and automate power control."}),l.jsxs(ke,{onClick:()=>{f(null),u(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Your First Smart Plug"]})]})})})]}),C==="notifications"&&l.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-8",children:[l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-4",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(Ph,{className:"w-5 h-5 text-bambu-green"}),"Providers"]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>w(!0),children:[l.jsx(I1,{className:"w-4 h-4"}),"Log"]}),be&&be.length>0&&l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>{Je(null),xe.mutate()},disabled:xe.isPending,children:[xe.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cR,{className:"w-4 h-4"}),"Test All"]}),l.jsxs(ke,{size:"sm",onClick:()=>{v(null),p(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add"]})]})]}),l.jsx(St,{className:"mb-4",children:l.jsx(Tt,{className:"py-3",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white text-sm font-medium",children:e("settings.notificationLanguage")}),l.jsx("p",{className:"text-xs text-bambu-gray",children:e("settings.notificationLanguageDescription")})]}),l.jsx("select",{value:s.notification_language||"en",onChange:Se=>lt("notification_language",Se.target.value),className:"px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:ring-1 focus:ring-bambu-green",children:qF.map(Se=>l.jsx("option",{value:Se.code,children:Se.nativeName},Se.code))})]})})}),fe&&l.jsx(St,{className:"mb-4",children:l.jsxs(Tt,{className:"py-3",children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsx("span",{className:"text-sm font-medium text-white",children:"Test Results"}),l.jsx("button",{onClick:()=>Je(null),className:"text-bambu-gray hover:text-white text-xs",children:"Dismiss"})]}),l.jsxs("div",{className:"flex items-center gap-4 text-sm mb-2",children:[l.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[l.jsx(Mr,{className:"w-4 h-4"}),fe.success," passed"]}),fe.failed>0&&l.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[l.jsx(Ca,{className:"w-4 h-4"}),fe.failed," failed"]})]}),fe.results.filter(Se=>!Se.success).length>0&&l.jsx("div",{className:"space-y-1 mt-2 pt-2 border-t border-bambu-dark-tertiary",children:fe.results.filter(Se=>!Se.success).map(Se=>l.jsxs("div",{className:"text-xs text-red-400",children:[l.jsxs("span",{className:"font-medium",children:[Se.provider_name,":"]})," ",Se.message]},Se.provider_id))})]})}),Te?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):be&&be.length>0?l.jsx("div",{className:"space-y-3",children:be.map(Se=>l.jsx(kGe,{provider:Se,onEdit:bt=>{v(bt),p(!0)}},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-8",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(Ph,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),l.jsx("p",{className:"text-sm font-medium text-white mb-2",children:"No providers configured"}),l.jsx("p",{className:"text-xs mb-3",children:"Add a provider to receive alerts."}),l.jsxs(ke,{size:"sm",onClick:()=>{v(null),p(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Provider"]})]})})})]}),l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-4",children:[l.jsx(Ss,{className:"w-5 h-5 text-bambu-green"}),"Message Templates"]}),l.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Customize notification messages for each event."}),ye?l.jsx("div",{className:"flex justify-center py-8",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):we&&we.length>0?l.jsx("div",{className:"space-y-2",children:we.map(Se=>l.jsx(St,{className:"cursor-pointer hover:border-bambu-green/50 transition-colors",onClick:()=>b(Se),children:l.jsx(Tt,{className:"py-2.5 px-3",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"min-w-0 flex-1",children:[l.jsx("p",{className:"text-white font-medium text-sm truncate",children:Se.name}),l.jsx("p",{className:"text-bambu-gray text-xs truncate mt-0.5",children:Se.title_template})]}),l.jsx("button",{className:"p-1.5 hover:bg-bambu-dark-tertiary rounded transition-colors shrink-0 ml-2",onClick:bt=>{bt.stopPropagation(),b(Se)},children:l.jsx(lR,{className:"w-4 h-4 text-bambu-gray"})})]})})},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-8",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(Ss,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),l.jsx("p",{className:"text-sm",children:"No templates available. Restart the backend to seed default templates."})]})})})]})]}),C==="apikeys"&&l.jsxs("div",{className:"max-w-3xl",children:[l.jsxs("div",{className:"flex items-start justify-between gap-4 mb-6",children:[l.jsxs("div",{className:"flex-1",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(Qm,{className:"w-5 h-5 text-bambu-green"}),"API Keys"]}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Create API keys for external integrations and webhooks. Use these keys to control your printers from automation tools like Home Assistant."})]}),l.jsxs(ke,{size:"sm",onClick:()=>k(!0),className:"flex-shrink-0",children:[l.jsx(ir,{className:"w-4 h-4"}),"Create Key"]})]}),j&&l.jsx(St,{className:"mb-6 border-bambu-green",children:l.jsx(Tt,{className:"py-4",children:l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx(Mr,{className:"w-5 h-5 text-bambu-green flex-shrink-0 mt-0.5"}),l.jsxs("div",{className:"flex-1",children:[l.jsx("p",{className:"text-white font-medium mb-1",children:"API Key Created Successfully"}),l.jsx("p",{className:"text-sm text-bambu-gray mb-2",children:"Copy this key now - it won't be shown again!"}),l.jsxs("div",{className:"flex items-center gap-2 bg-bambu-dark rounded-lg p-2",children:[l.jsx("code",{className:"flex-1 text-sm text-bambu-green font-mono break-all",children:j}),l.jsx(ke,{variant:"secondary",size:"sm",onClick:async()=>{try{if(navigator.clipboard&&navigator.clipboard.writeText)await navigator.clipboard.writeText(j);else{const Se=document.createElement("textarea");Se.value=j,Se.style.position="fixed",Se.style.left="-999999px",document.body.appendChild(Se),Se.select(),document.execCommand("copy"),document.body.removeChild(Se)}r("Key copied to clipboard")}catch{r("Failed to copy key","error")}},children:l.jsx(md,{className:"w-4 h-4"})})]}),l.jsx(ke,{variant:"secondary",size:"sm",className:"mt-3",onClick:()=>V(null),children:"Dismiss"})]})]})})}),F&&l.jsxs(St,{className:"mb-6",children:[l.jsx(Qi,{children:l.jsx("h3",{className:"text-base font-semibold text-white",children:"Create New API Key"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Key Name"}),l.jsx("input",{type:"text",value:R,onChange:Se=>I(Se.target.value),placeholder:"e.g., Home Assistant, OctoPrint",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"Permissions"}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:L.can_read_status,onChange:Se=>q(bt=>({...bt,can_read_status:Se.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white",children:"Read Status"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"View printer status and queue"})]})]}),l.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:L.can_queue,onChange:Se=>q(bt=>({...bt,can_queue:Se.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white",children:"Manage Queue"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Add and remove items from print queue"})]})]}),l.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:L.can_control_printer,onChange:Se=>q(bt=>({...bt,can_control_printer:Se.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white",children:"Control Printer"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Pause, resume, and stop prints"})]})]})]})]}),l.jsxs("div",{className:"flex items-center gap-2 pt-2",children:[l.jsxs(ke,{onClick:()=>O.mutate({name:R||"Unnamed Key",...L}),disabled:O.isPending,children:[O.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ir,{className:"w-4 h-4"}),"Create Key"]}),l.jsx(ke,{variant:"secondary",onClick:()=>k(!1),children:"Cancel"})]})]})]}),U?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):Me&&Me.length>0?l.jsx("div",{className:"space-y-3",children:Me.map(Se=>l.jsx(St,{children:l.jsx(Tt,{className:"py-3",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx(Qm,{className:`w-5 h-5 ${Se.enabled?"text-bambu-green":"text-bambu-gray"}`}),l.jsxs("div",{children:[l.jsx("p",{className:"text-white font-medium",children:Se.name}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[Se.key_prefix,"••••••••",Se.last_used&&` · Last used: ${new Date(Se.last_used).toLocaleDateString()}`]})]})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("div",{className:"flex gap-1 text-xs",children:[Se.can_read_status&&l.jsx("span",{className:"px-1.5 py-0.5 bg-blue-500/20 text-blue-400 rounded",children:"Read"}),Se.can_queue&&l.jsx("span",{className:"px-1.5 py-0.5 bg-green-500/20 text-green-400 rounded",children:"Queue"}),Se.can_control_printer&&l.jsx("span",{className:"px-1.5 py-0.5 bg-orange-500/20 text-orange-400 rounded",children:"Control"})]}),l.jsx(ke,{variant:"secondary",size:"sm",onClick:()=>G(Se.id),children:l.jsx(Sn,{className:"w-4 h-4 text-red-400"})})]})]})})},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-12",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(Qm,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),l.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No API keys"}),l.jsx("p",{className:"text-sm mb-4",children:"Create an API key to integrate with external services."}),l.jsxs(ke,{onClick:()=>k(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Create Your First Key"]})]})})}),l.jsxs(St,{className:"mt-6",children:[l.jsx(Qi,{children:l.jsx("h3",{className:"text-base font-semibold text-white",children:"Webhook Endpoints"})}),l.jsxs(Tt,{className:"space-y-3 text-sm",children:[l.jsxs("p",{className:"text-bambu-gray",children:["Use your API key in the ",l.jsx("code",{className:"text-bambu-green",children:"X-API-Key"})," header."]}),l.jsxs("div",{className:"space-y-2 font-mono text-xs",children:[l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-blue-400",children:"GET"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/status"}),l.jsx("span",{className:"text-bambu-gray",children:" - Get all printer status"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-blue-400",children:"GET"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/status/:id"}),l.jsx("span",{className:"text-bambu-gray",children:" - Get specific printer status"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-green-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/queue"}),l.jsx("span",{className:"text-bambu-gray",children:" - Add to print queue"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-orange-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/pause"}),l.jsx("span",{className:"text-bambu-gray",children:" - Pause print"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-orange-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/resume"}),l.jsx("span",{className:"text-bambu-gray",children:" - Resume print"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-red-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/stop"}),l.jsx("span",{className:"text-bambu-gray",children:" - Stop print"})]})]})]})]})]}),C==="virtual-printer"&&l.jsx(VGe,{}),K!==null&&l.jsx(Lr,{title:"Delete API Key",message:"Are you sure you want to delete this API key? Any integrations using this key will stop working.",confirmText:"Delete Key",variant:"danger",onConfirm:()=>{re.mutate(K),G(null)},onCancel:()=>G(null)}),c&&l.jsx(NGe,{plug:d,onClose:()=>{u(!1),f(null)}}),g&&l.jsx(PGe,{provider:x,onClose:()=>{p(!1),v(null)}}),y&&l.jsx(jGe,{template:y,onClose:()=>b(null)}),E&&l.jsx(OGe,{onClose:()=>w(!1)}),J&&l.jsx(Lr,{title:"Clear Notification Logs",message:"This will permanently delete all notification logs older than 30 days. This action cannot be undone.",confirmText:"Clear Logs",variant:"warning",onConfirm:async()=>{ee(!1);try{const Se=await Ee.clearNotificationLogs(30);r(Se.message,"success")}catch{r("Failed to clear logs","error")}},onCancel:()=>ee(!1)}),Q&&l.jsx(Lr,{title:"Reset UI Preferences",message:"This will reset all UI preferences to defaults: sidebar order, theme, dashboard layout, view modes, and sorting preferences. Your printers, archives, and server settings will NOT be affected. The page will reload after clearing.",confirmText:"Reset Preferences",variant:"default",onConfirm:()=>{B(!1),localStorage.clear(),r("UI preferences reset. Refreshing...","success"),setTimeout(()=>window.location.reload(),1e3)},onCancel:()=>B(!1)}),$&&l.jsx(Lr,{title:`Turn All Plugs ${$==="on"?"On":"Off"}`,message:`This will turn ${$==="on"?"ON":"OFF"} all ${ne?.filter(Se=>Se.enabled).length||0} enabled smart plugs. ${$==="off"?"Any running printers may be affected!":""}`,confirmText:`Turn All ${$==="on"?"On":"Off"}`,variant:$==="off"?"danger":"warning",onConfirm:()=>{const Se=$;Z(null),ze.mutate(Se)},onCancel:()=>Z(null)}),te&&l.jsx(IGe,{onClose:()=>he(!1),onExport:async Se=>{he(!1);const bt="backup-progress",Gt=Se.archives;Gt&&i(bt,e("backup.preparing",{defaultValue:"Preparing backup..."}),"loading");try{const{blob:kt,filename:Tn}=await Ee.exportBackup(Se);Gt&&a(bt);const Nn=URL.createObjectURL(kt),nn=document.createElement("a");nn.href=Nn,nn.download=Tn,nn.click(),URL.revokeObjectURL(Nn),r(e("backup.downloaded",{defaultValue:"Backup downloaded"}),"success")}catch{Gt&&a(bt),r(e("backup.failed",{defaultValue:"Failed to create backup"}),"error")}}}),de&&l.jsx(FGe,{onClose:()=>ge(!1),onRestore:async(Se,bt)=>await Ee.importBackup(Se,bt),onSuccess:()=>{o(null),_e.current=!0,t.invalidateQueries()}}),Ae&&l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:()=>me(!1),children:l.jsxs(St,{className:"w-full max-w-lg",onClick:Se=>Se.stopPropagation(),children:[l.jsxs(Qi,{className:"flex flex-row items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(xue,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:e("settings.telemetryInfoTitle")})]}),l.jsx("button",{onClick:()=>me(!1),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-bambu-gray text-sm",children:e("settings.telemetryInfoIntro")}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("h3",{className:"text-white font-medium",children:e("settings.telemetryInfoCollected")}),l.jsxs("ul",{className:"space-y-2 text-sm",children:[l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoItem1")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoItem2")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoItem3")})]})]})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("h3",{className:"text-white font-medium",children:e("settings.telemetryInfoNotCollected")}),l.jsxs("ul",{className:"space-y-2 text-sm",children:[l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem1")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem2")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem3")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem4")})]})]})]}),l.jsx("p",{className:"text-bambu-gray text-xs border-t border-bambu-dark-tertiary pt-4",children:e("settings.telemetryInfoFooter")}),l.jsx(ke,{onClick:()=>me(!1),className:"w-full",children:e("common.close")})]})]})}),ce&&ue?.release_notes&&l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:()=>Pe(!1),children:l.jsxs(St,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",onClick:Se=>Se.stopPropagation(),children:[l.jsxs(Qi,{className:"flex flex-row items-center justify-between shrink-0",children:[l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Release Notes - v",ue.latest_version]}),ue.release_name&&ue.release_name!==ue.latest_version&&l.jsx("p",{className:"text-sm text-bambu-gray",children:ue.release_name})]}),l.jsx("button",{onClick:()=>Pe(!1),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsx(Tt,{className:"overflow-y-auto flex-1",children:l.jsx("pre",{className:"text-sm text-bambu-gray whitespace-pre-wrap font-sans",children:ue.release_notes})}),l.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary shrink-0 flex gap-2",children:[ue.release_url&&l.jsx("a",{href:ue.release_url,target:"_blank",rel:"noopener noreferrer",className:"flex-1",children:l.jsxs(ke,{variant:"secondary",className:"w-full",children:[l.jsx(ga,{className:"w-4 h-4"}),"View on GitHub"]})}),l.jsx(ke,{onClick:()=>Pe(!1),className:"flex-1",children:"Close"})]})]})})]})}const jte=t=>{const e=parseFloat(t);return(Math.trunc(e*1e3)/1e3).toFixed(3)},WGe=t=>t.startsWith("HH")?"HF":"S",qGe=t=>{const e=t.match(/^([A-Z]{2}\d{2})/);return e?e[1]:"HH00"},Hj=t=>{const e=["High Flow_","High Flow ","Standard_","Standard ","HF_","HF ","S_","S "];for(const r of e)if(t.startsWith(r))return t.slice(r.length);const n=t.indexOf("_");return n>0?t.slice(n+1):t};function Sk({profile:t,onEdit:e,onCopy:n,selectionMode:r,isSelected:i,onToggleSelect:a,note:s}){const o=WGe(t.nozzle_id),c=t.nozzle_diameter,u=()=>{r&&a?a():e()};return l.jsxs("div",{className:"flex items-center gap-2",children:[r&&l.jsx("button",{onClick:a,className:"text-bambu-gray hover:text-white transition-colors p-1",children:i?l.jsx(Qh,{className:"w-4 h-4 text-bambu-green"}):l.jsx(Sp,{className:"w-4 h-4"})}),l.jsxs("button",{onClick:u,className:`flex-1 text-left px-3 py-2 bg-bambu-dark rounded hover:bg-bambu-dark-tertiary transition-colors ${i?"ring-1 ring-bambu-green":""}`,children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("span",{className:"text-bambu-green font-mono text-sm font-bold whitespace-nowrap",children:jte(t.k_value)}),l.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name||"Unnamed"}),s&&l.jsx("span",{title:"Has note",children:l.jsx(kG,{className:"w-3 h-3 text-yellow-500"})}),l.jsxs("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:[o," ",c]})]}),s&&l.jsxs("div",{className:"text-xs mt-0.5 truncate text-yellow-500/70",title:s,children:["Note: ",s.length>50?s.substring(0,50)+"...":s]})]}),!r&&n&&l.jsx("button",{onClick:d=>{d.stopPropagation(),n()},className:"text-bambu-gray hover:text-white transition-colors p-1",title:"Copy profile",children:l.jsx(md,{className:"w-4 h-4"})})]})}function Mk({profile:t,printerId:e,nozzleDiameter:n,existingProfiles:r=[],isDualNozzle:i=!1,initialNote:a="",initialNoteKey:s=null,onClose:o,onSave:c,onSaveNote:u}){const{showToast:d}=vr(),[f,g]=M.useState(t?.name||""),[p,x]=M.useState(t?.k_value?jte(t.k_value):"0.020"),[v,y]=M.useState(t?.filament_id||""),[b,E]=M.useState(t?.nozzle_id?qGe(t.nozzle_id):"HH00"),[w,T]=M.useState(t?.nozzle_diameter||n),[A,C]=M.useState(t?[t.extruder_id]:i?[0,1]:[0]),[N,F]=M.useState(!1),[k,R]=M.useState({current:0,total:0}),[I,L]=M.useState(a),q=br.useMemo(()=>{const Q=new Map;for(const B of r)if(B.filament_id&&!Q.has(B.filament_id)){const $=Hj(B.name||"");Q.set(B.filament_id,{id:B.filament_id,name:$||B.filament_id})}return Array.from(Q.values()).sort((B,$)=>B.name.localeCompare($.name))},[r]),j=_t({mutationFn:Q=>(console.log("[KProfile] Calling API..."),Ee.setKProfile(e,Q)),onSuccess:Q=>{if(console.log("[KProfile] Save success:",Q),d("K-profile saved"),u&&I!==a){let B;I===""&&s?B=s:t&&t.slot_id>0?B=t.setting_id||`slot_${t.slot_id}_${t.filament_id}_${t.extruder_id}`:B=`name_${f}_${v}`,u(B,I)}F(!0),setTimeout(()=>{F(!1),c()},2500)},onError:Q=>{console.error("[KProfile] Save error:",Q),d(Q.message,"error"),F(!1)}}),V=_t({mutationFn:Q=>(console.log("[KProfile] Deleting profile..."),Ee.deleteKProfile(e,Q)),onSuccess:Q=>{console.log("[KProfile] Delete success:",Q),d("K-profile deleted"),F(!0),setTimeout(()=>{F(!1),o()},4e3)},onError:Q=>{console.error("[KProfile] Delete error:",Q),d(Q.message,"error"),F(!1)}}),[K,G]=M.useState(!1),J=()=>{t&&V.mutate({slot_id:t.slot_id,extruder_id:t.extruder_id,nozzle_id:t.nozzle_id,nozzle_diameter:t.nozzle_diameter,filament_id:t.filament_id,setting_id:t.setting_id})},ee=async Q=>{if(Q.preventDefault(),i&&!t&&A.length===0){d("Please select at least one extruder","error");return}const B=parseFloat(p).toFixed(6),$=`${b}-${w}`;if(t||A.length===1){const te={name:f,k_value:B,filament_id:v,nozzle_id:$,nozzle_diameter:w,extruder_id:t?t.extruder_id:A[0],setting_id:t?.setting_id,slot_id:t?.slot_id??0};console.log("[KProfile] Saving profile:",te),j.mutate(te);return}F(!0),R({current:1,total:A.length});const Z=A.map(te=>({name:f,k_value:B,filament_id:v,nozzle_id:$,nozzle_diameter:w,extruder_id:te,setting_id:void 0,slot_id:0}));console.log(`[KProfile] Saving ${Z.length} profiles in batch:`,Z);try{if(await Ee.setKProfilesBatch(e,Z),d(`K-profile saved to ${A.length} extruders`),u&&I){const te=`name_${f}_${v}`;u(te,I)}}catch(te){console.error("[KProfile] Failed to save batch:",te),d("Failed to save K-profiles","error"),F(!1),R({current:0,total:0});return}R({current:A.length,total:A.length}),setTimeout(()=>{F(!1),R({current:0,total:0}),c()},3e3)};return l.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:[l.jsxs(St,{className:"w-full max-w-md relative",children:[N&&l.jsxs("div",{className:"absolute inset-0 bg-bambu-dark-secondary/90 flex flex-col items-center justify-center z-10 rounded-lg",children:[l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin mb-3"}),l.jsx("p",{className:"text-white font-medium",children:k.total>1?`Saving to extruder ${k.current}/${k.total}...`:"Syncing with printer..."}),l.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:"Please wait"})]}),l.jsxs(Tt,{className:"p-0",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[l.jsx("h2",{className:"text-xl font-semibold text-white",children:t?"Edit K-Profile":"Add K-Profile"}),l.jsx("button",{onClick:o,className:"text-bambu-gray hover:text-white transition-colors",disabled:N,children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:ee,className:"p-4 space-y-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Profile Name"}),l.jsx("input",{type:"text",value:f,onChange:Q=>g(Q.target.value),disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,placeholder:"My PLA Profile",required:!t})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"K-Value"}),l.jsx("input",{type:"text",inputMode:"decimal",value:p,onChange:Q=>{const B=Q.target.value;(B===""||/^\d*\.?\d*$/.test(B))&&x(B)},onBlur:Q=>{const B=parseFloat(Q.target.value);isNaN(B)||x((Math.trunc(B*1e3)/1e3).toFixed(3))},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 font-mono",placeholder:"0.020",required:!0}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Typical range: 0.01 - 0.06 for PLA, 0.02 - 0.10 for PETG"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Filament"}),l.jsxs("select",{value:v,onChange:Q=>{const B=Q.target.value;if(y(B),!t&&B&&!f){const $=q.find(Z=>Z.id===B);$&&g(`${b==="HH00"?"HF":"S"} ${$.name}`)}},disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,required:!t,children:[l.jsx("option",{value:"",children:"Select filament..."}),t?.filament_id&&l.jsx("option",{value:t.filament_id,children:q.find(Q=>Q.id===t.filament_id)?.name||t.filament_id},t.filament_id),!t&&q.map(Q=>l.jsx("option",{value:Q.id,children:Q.name},Q.id))]}),!t&&q.length===0&&l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"No filaments found. Create a K-profile in Bambu Studio first."})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Flow Type"}),l.jsxs("select",{value:b,onChange:Q=>{const B=Q.target.value;if(E(B),!t&&v&&!f){const $=q.find(Z=>Z.id===v);$&&g(`${B==="HS00"?"HF":"S"} ${$.name}`)}},disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,children:[l.jsx("option",{value:"HH00",children:"High Flow"}),l.jsx("option",{value:"HS00",children:"Standard"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle Size"}),l.jsxs("select",{value:w,onChange:Q=>T(Q.target.value),disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,children:[l.jsx("option",{value:"0.2",children:"0.2mm"}),l.jsx("option",{value:"0.4",children:"0.4mm"}),l.jsx("option",{value:"0.6",children:"0.6mm"}),l.jsx("option",{value:"0.8",children:"0.8mm"})]})]})]}),i&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:t?"Extruder":"Extruders"}),t?l.jsx("div",{className:"px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white opacity-60",children:t.extruder_id===1?"Left":"Right"}):l.jsxs("div",{className:"flex gap-4",children:[l.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:A.includes(1),onChange:Q=>{Q.target.checked?C([...A,1]):C(A.filter(B=>B!==1))},className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0 accent-bambu-green"}),l.jsx("span",{className:"text-white",children:"Left"})]}),l.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:A.includes(0),onChange:Q=>{Q.target.checked?C([...A,0]):C(A.filter(B=>B!==0))},className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0 accent-bambu-green"}),l.jsx("span",{className:"text-white",children:"Right"})]})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Notes (stored locally)"}),l.jsx("textarea",{value:I,onChange:Q=>L(Q.target.value),placeholder:"Add notes about this profile...",rows:2,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none resize-none"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Notes are saved in Bambuddy, not on the printer"})]}),l.jsxs("div",{className:"flex gap-2 pt-4",children:[t&&l.jsx(ke,{type:"button",variant:"secondary",onClick:()=>G(!0),disabled:V.isPending||N,className:"text-red-500 hover:bg-red-500/10",children:V.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"})}),l.jsx(ke,{type:"button",variant:"secondary",onClick:o,disabled:N,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:j.isPending||N,className:"flex-1",children:[j.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(XS,{className:"w-4 h-4"}),"Save"]})]})]})]})]}),K&&l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-[60]",children:l.jsx(St,{className:"w-full max-w-sm",children:l.jsxs(Tt,{className:"p-6",children:[l.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[l.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:l.jsx(Sn,{className:"w-5 h-5 text-red-500"})}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profile"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),l.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",l.jsxs("span",{className:"text-white font-medium",children:['"',t?.name,'"']})," from the printer?"]}),l.jsxs("div",{className:"flex gap-3",children:[l.jsx(ke,{variant:"secondary",onClick:()=>G(!1),className:"flex-1",children:"Cancel"}),l.jsxs(ke,{onClick:()=>{G(!1),J()},disabled:V.isPending,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[V.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}const i1={NOZZLE_DIAMETER:"bambusy_kprofiles_nozzle",SORT_OPTION:"bambusy_kprofiles_sort"};function KGe(){const{showToast:t}=vr(),[e,n]=M.useState(null),[r,i]=M.useState(()=>localStorage.getItem(i1.NOZZLE_DIAMETER)||"0.4"),[a,s]=M.useState(null),[o,c]=M.useState(!1),[u,d]=M.useState(null),[f,g]=M.useState(""),[p,x]=M.useState("all"),[v,y]=M.useState("all"),[b,E]=M.useState(()=>localStorage.getItem(i1.SORT_OPTION)||"name"),[w,T]=M.useState(!1),[A,C]=M.useState(new Set),[N,F]=M.useState(!1),[k,R]=M.useState(!1),I=W=>`${W.slot_id}_${W.extruder_id}`;M.useEffect(()=>{localStorage.setItem(i1.NOZZLE_DIAMETER,r)},[r]),M.useEffect(()=>{localStorage.setItem(i1.SORT_OPTION,b)},[b]);const{data:L,isLoading:q}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:j,isLoading:V,isFetching:K,error:G,refetch:J}=xt({queryKey:["kprofiles",e,r],queryFn:async()=>{console.log("[KProfiles] Fetching profiles for printer",e,"nozzle",r);const W=await Ee.getKProfiles(e,r);return console.log("[KProfiles] Received profiles:",W?.profiles?.length||0,"profiles"),W},enabled:!!e,retry:!1,staleTime:0,gcTime:0,refetchOnMount:"always"}),{data:ee}=xt({queryKey:["kprofiles",e,"0.4"],queryFn:()=>Ee.getKProfiles(e,"0.4"),enabled:!!e,staleTime:6e4}),{data:Q,refetch:B}=xt({queryKey:["kprofile-notes",e],queryFn:()=>Ee.getKProfileNotes(e),enabled:!!e,staleTime:3e4}),$=G?.message?.includes("not connected");M.useEffect(()=>{if(!e&&L&&L.length>0){const W=L.find(Y=>Y.is_active);W&&n(W.id)}},[e,L]),M.useEffect(()=>{if(e){const W=setTimeout(()=>{J()},150);return()=>clearTimeout(W)}},[e,r]);const Z=L?.filter(W=>W.is_active)||[],te=br.useMemo(()=>j?.profiles?j.profiles.filter(Y=>{const se=f.toLowerCase(),be=!se||Y.name.toLowerCase().includes(se)||Y.filament_id.toLowerCase().includes(se),Te=p==="all"||p==="left"&&Y.extruder_id===1||p==="right"&&Y.extruder_id===0,Me=v==="all"||v==="hf"&&Y.nozzle_id.startsWith("HH")||v==="s"&&Y.nozzle_id.startsWith("HS");return be&&Te&&Me}).sort((Y,se)=>{switch(b){case"k_value":return parseFloat(Y.k_value)-parseFloat(se.k_value);case"filament":return Hj(Y.name).localeCompare(Hj(se.name));case"name":default:return Y.name.localeCompare(se.name)}}):[],[j?.profiles,f,p,v,b]),he=L?.find(W=>W.id===e),de=he?.nozzle_count===2;M.useEffect(()=>{const W=Y=>{Y.target instanceof HTMLInputElement||Y.target instanceof HTMLTextAreaElement||Y.target instanceof HTMLSelectElement||a||o||u||(Y.key==="r"||Y.key==="R"?(Y.preventDefault(),J()):Y.key==="n"||Y.key==="N"?(Y.preventDefault(),c(!0)):Y.key==="Escape"&&w&&(Y.preventDefault(),T(!1),C(new Set)))};return window.addEventListener("keydown",W),()=>window.removeEventListener("keydown",W)},[a,o,u,w,J]);const ge=M.useCallback(()=>{if(!j?.profiles||j.profiles.length===0){t("No profiles to export","error");return}const W={version:1,exported_at:new Date().toISOString(),printer:he?.name||"Unknown",nozzle_diameter:r,profiles:j.profiles.map(Te=>({name:Te.name,k_value:Te.k_value,filament_id:Te.filament_id,nozzle_id:Te.nozzle_id,nozzle_diameter:Te.nozzle_diameter,extruder_id:Te.extruder_id}))},Y=new Blob([JSON.stringify(W,null,2)],{type:"application/json"}),se=URL.createObjectURL(Y),be=document.createElement("a");be.href=se,be.download=`kprofiles_${he?.name||"printer"}_${r}mm_${new Date().toISOString().split("T")[0]}.json`,document.body.appendChild(be),be.click(),document.body.removeChild(be),URL.revokeObjectURL(se),t(`Exported ${j.profiles.length} profiles`)},[j?.profiles,he,r,t]),Ae=M.useCallback(()=>{const W=document.createElement("input");W.type="file",W.accept=".json",W.onchange=async Y=>{const se=Y.target.files?.[0];if(se)try{const be=await se.text(),Te=JSON.parse(be);if(!Te.profiles||!Array.isArray(Te.profiles)){t("Invalid file format","error");return}let Me=0;for(const U of Te.profiles)if(!(!U.name||!U.k_value||!U.filament_id))try{await Ee.setKProfile(e,{name:U.name,k_value:parseFloat(U.k_value).toFixed(6),filament_id:U.filament_id,nozzle_id:U.nozzle_id||`HH00-${r}`,nozzle_diameter:U.nozzle_diameter||r,extruder_id:U.extruder_id??0,slot_id:0}),Me++,await new Promise(O=>setTimeout(O,500))}catch(O){console.error("Failed to import profile:",U.name,O)}t(`Imported ${Me} of ${Te.profiles.length} profiles`),J()}catch(be){console.error("Import error:",be),t("Failed to parse import file","error")}},W.click()},[e,r,t,J]),me=M.useCallback(W=>{C(Y=>{const se=new Set(Y);return se.has(W)?se.delete(W):se.add(W),se})},[]),ce=M.useCallback(()=>{C(new Set(te.map(W=>I(W))))},[te,I]),Pe=M.useCallback(()=>{A.size!==0&&F(!0)},[A.size]),oe=M.useCallback(async()=>{const W=te.filter(se=>A.has(I(se)));R(!0);let Y=0;for(const se of W)try{await Ee.deleteKProfile(e,{slot_id:se.slot_id,extruder_id:se.extruder_id,nozzle_id:se.nozzle_id,nozzle_diameter:se.nozzle_diameter,filament_id:se.filament_id,setting_id:se.setting_id}),Y++,await new Promise(be=>setTimeout(be,300))}catch(be){console.error("Failed to delete profile:",se.name,be)}t(`Deleted ${Y} profiles`),R(!1),F(!1),T(!1),C(new Set),J()},[e,A,te,t,J,I]),Le=M.useCallback(W=>{const Y=[];return W.setting_id&&Y.push(W.setting_id),Y.push(`slot_${W.slot_id}_${W.filament_id}_${W.extruder_id}`),Y.push(`name_${W.name}_${W.filament_id}`),Y},[]),z=M.useCallback(async(W,Y)=>{if(e)try{await Ee.setKProfileNote(e,W,Y),B()}catch(se){console.error("Failed to save note:",se),t("Failed to save note","error")}},[e,B,t]),H=M.useCallback(W=>{if(!Q?.notes)return{note:"",key:null};const Y=Le(W);for(const se of Y)if(Q.notes[se])return{note:Q.notes[se],key:se};return{note:"",key:null}},[Q,Le]),ne=M.useCallback(W=>H(W).note,[H]);return q?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):!L||L.length===0?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(ho,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Printers Configured"}),l.jsx("p",{className:"text-bambu-gray",children:"Add a printer in Settings to manage K-profiles"})]})}):Z.length===0?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(Ui,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Active Printers"}),l.jsx("p",{className:"text-bambu-gray",children:"Enable a printer connection to view its K-profiles"})]})}):l.jsxs(l.Fragment,{children:[K&&!V&&l.jsxs("div",{className:"fixed inset-0 bg-black/50 flex flex-col items-center justify-center z-40",children:[l.jsx(gt,{className:"w-10 h-10 text-bambu-green animate-spin mb-3"}),l.jsx("p",{className:"text-white font-medium",children:"Loading K-Profiles..."})]}),l.jsxs("div",{className:"flex flex-wrap gap-4 mb-6",children:[l.jsxs("div",{className:"flex-1 min-w-48",children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer"}),l.jsx("select",{value:e||"",onChange:W=>n(parseInt(W.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",children:Z.map(W=>l.jsx("option",{value:W.id,children:W.name},W.id))})]}),l.jsxs("div",{className:"w-32",children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle"}),l.jsxs("select",{value:r,onChange:W=>i(W.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",children:[l.jsx("option",{value:"0.2",children:"0.2mm"}),l.jsx("option",{value:"0.4",children:"0.4mm"}),l.jsx("option",{value:"0.6",children:"0.6mm"}),l.jsx("option",{value:"0.8",children:"0.8mm"})]})]}),l.jsxs("div",{className:"flex items-end gap-2",children:[l.jsxs(ke,{variant:"secondary",onClick:()=>J(),disabled:K,children:[l.jsx(ya,{className:`w-4 h-4 ${K?"animate-spin":""}`}),"Refresh"]}),l.jsxs(ke,{onClick:()=>c(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Profile"]})]})]}),l.jsxs("div",{className:"flex flex-wrap gap-4 mb-4",children:[l.jsxs("div",{className:"flex-1 min-w-48 relative",children:[l.jsx(xl,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),l.jsx("input",{type:"text",value:f,onChange:W=>g(W.target.value),placeholder:"Search by name or filament...",className:"w-full pl-10 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"})]}),de&&l.jsx("div",{className:"w-36",children:l.jsxs("select",{value:p,onChange:W=>x(W.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",children:[l.jsx("option",{value:"all",children:"All Extruders"}),l.jsx("option",{value:"left",children:"Left Only"}),l.jsx("option",{value:"right",children:"Right Only"})]})}),l.jsx("div",{className:"w-32",children:l.jsxs("select",{value:v,onChange:W=>y(W.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",children:[l.jsx("option",{value:"all",children:"All Flow"}),l.jsx("option",{value:"hf",children:"HF Only"}),l.jsx("option",{value:"s",children:"S Only"})]})}),l.jsx("div",{className:"w-32",children:l.jsxs("select",{value:b,onChange:W=>E(W.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",children:[l.jsx("option",{value:"name",children:"Sort: Name"}),l.jsx("option",{value:"k_value",children:"Sort: K-Value"}),l.jsx("option",{value:"filament",children:"Sort: Filament"})]})})]}),l.jsxs("div",{className:"flex flex-wrap gap-2 mb-6",children:[l.jsxs(ke,{variant:"secondary",onClick:ge,disabled:!j?.profiles?.length,title:"Export profiles to JSON",children:[l.jsx(ns,{className:"w-4 h-4"}),"Export"]}),l.jsxs(ke,{variant:"secondary",onClick:Ae,title:"Import profiles from JSON",children:[l.jsx(ia,{className:"w-4 h-4"}),"Import"]}),l.jsx("div",{className:"flex-1"}),w?l.jsxs(l.Fragment,{children:[l.jsxs(ke,{variant:"secondary",onClick:ce,title:"Select all visible profiles",children:[l.jsx(Qh,{className:"w-4 h-4"}),"Select All"]}),l.jsxs(ke,{variant:"secondary",onClick:Pe,disabled:A.size===0,className:"text-red-500 hover:bg-red-500/10",title:`Delete ${A.size} selected profiles`,children:[l.jsx(Sn,{className:"w-4 h-4"}),"Delete (",A.size,")"]}),l.jsxs(ke,{variant:"secondary",onClick:()=>{T(!1),C(new Set)},children:[l.jsx(Vt,{className:"w-4 h-4"}),"Cancel"]})]}):l.jsxs(ke,{variant:"secondary",onClick:()=>T(!0),disabled:!te.length,title:"Enter selection mode for bulk delete",children:[l.jsx(Qh,{className:"w-4 h-4"}),"Select"]})]}),V?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):$?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(Mb,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"Printer Offline"}),l.jsx("p",{className:"text-bambu-gray mb-4",children:"The selected printer is not connected. Power it on to view K-profiles."}),l.jsxs(ke,{variant:"secondary",onClick:()=>J(),children:[l.jsx(ya,{className:"w-4 h-4"}),"Retry"]})]})}):te.length>0?de?l.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[l.jsxs("div",{children:[l.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Left Extruder"}),l.jsx("div",{className:"space-y-1",children:te.filter(W=>W.extruder_id===1).map(W=>l.jsx(Sk,{profile:W,onEdit:()=>s(W),onCopy:()=>d(W),selectionMode:w,isSelected:A.has(I(W)),onToggleSelect:()=>me(I(W)),note:ne(W)},I(W)))})]}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Right Extruder"}),l.jsx("div",{className:"space-y-1",children:te.filter(W=>W.extruder_id===0).map(W=>l.jsx(Sk,{profile:W,onEdit:()=>s(W),onCopy:()=>d(W),selectionMode:w,isSelected:A.has(I(W)),onToggleSelect:()=>me(I(W)),note:ne(W)},I(W)))})]})]}):l.jsx("div",{className:"space-y-1",children:te.map(W=>l.jsx(Sk,{profile:W,onEdit:()=>s(W),onCopy:()=>d(W),selectionMode:w,isSelected:A.has(I(W)),onToggleSelect:()=>me(I(W)),note:ne(W)},I(W)))}):f||p!=="all"||v!=="all"?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(xl,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Matching Profiles"}),l.jsx("p",{className:"text-bambu-gray",children:"No profiles match your search criteria"})]})}):l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(XS,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No K-Profiles"}),l.jsxs("p",{className:"text-bambu-gray mb-4",children:["No pressure advance profiles found for ",r,"mm nozzle"]}),l.jsxs(ke,{onClick:()=>c(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Create First Profile"]})]})}),a&&e&&(()=>{const{note:W,key:Y}=H(a);return l.jsx(Mk,{profile:a,printerId:e,nozzleDiameter:r,existingProfiles:ee?.profiles||j?.profiles,isDualNozzle:de,initialNote:W,initialNoteKey:Y,onSaveNote:z,onClose:()=>{console.log("[KProfiles] Edit modal onClose - refetching profiles..."),s(null),J()},onSave:()=>{s(null),J()}})})(),o&&e&&l.jsx(Mk,{printerId:e,nozzleDiameter:r,existingProfiles:ee?.profiles||j?.profiles,isDualNozzle:de,onSaveNote:z,onClose:()=>{c(!1),J()},onSave:()=>{c(!1),J()}}),u&&e&&l.jsx(Mk,{printerId:e,nozzleDiameter:r,existingProfiles:ee?.profiles||j?.profiles,isDualNozzle:de,onSaveNote:z,profile:{...u,slot_id:0,name:`${u.name} (Copy)`},onClose:()=>{d(null),J()},onSave:()=>{d(null),J()}}),N&&l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:l.jsx(St,{className:"w-full max-w-sm",children:l.jsxs(Tt,{className:"p-6",children:[l.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[l.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:l.jsx(Sn,{className:"w-5 h-5 text-red-500"})}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profiles"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),l.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",l.jsx("span",{className:"text-white font-medium",children:A.size})," selected profiles from the printer?"]}),l.jsxs("div",{className:"flex gap-3",children:[l.jsx(ke,{variant:"secondary",onClick:()=>F(!1),disabled:k,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{onClick:oe,disabled:k,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[k?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}function OL(t,e){const n=`${t} ${e||""}`,r=n.match(/@?\s*(?:BBL\s+)?(?:Bambu\s+Lab\s+)?([XPAH][1-9][A-Z]?(?:\s*(?:Carbon|mini))?|H2D)/i),i=r?r[1].trim():null,a=n.match(/(\d+\.?\d*)\s*(?:mm\s*)?nozzle|nozzle\s*(\d+\.?\d*)/i),s=a?(a[1]||a[2])+"mm":null,o=n.match(/(\d+\.?\d*)mm\s*(?:Standard|Fine|Extra Fine|Draft|Quality)?/i),c=o?o[1]+"mm":null,u=n.match(/\b(PLA|PETG|ABS|ASA|TPU|PC|PA|PVA|HIPS|PP|PET(?:-?CF)?|PA(?:-?CF)?|PLA(?:-?CF)?)\b/i),d=u?u[1].toUpperCase():null;return{printer:i,nozzle:s,layerHeight:c,filamentType:d}}function gb(t){return/^(P[FPM]US|PF\d|PP\d)/.test(t)}function XGe(t){const e=new Date(t),r=new Date().getTime()-e.getTime(),i=Math.floor(r/6e4),a=Math.floor(r/36e5),s=Math.floor(r/864e5);return i<1?"Just now":i<60?`${i}m ago`:a<24?`${a}h ago`:s<7?`${s}d ago`:e.toLocaleDateString()}function YGe({onSuccess:t}){const{showToast:e}=vr(),[n,r]=M.useState("email"),[i,a]=M.useState(""),[s,o]=M.useState(""),[c,u]=M.useState(""),[d,f]=M.useState(""),[g,p]=M.useState("global"),x=_t({mutationFn:()=>Ee.cloudLogin(i,s,g),onSuccess:w=>{w.success?(e("Logged in successfully"),t()):w.needs_verification?(e("Verification code sent to your email"),r("code")):e(w.message,"error")},onError:w=>e(w.message,"error")}),v=_t({mutationFn:()=>Ee.cloudVerify(i,c),onSuccess:w=>{w.success?(e("Logged in successfully"),t()):e(w.message,"error")},onError:w=>e(w.message,"error")}),y=_t({mutationFn:()=>Ee.cloudSetToken(d),onSuccess:()=>{e("Token set successfully"),t()},onError:w=>e(w.message,"error")}),b=w=>{w.preventDefault(),n==="email"?x.mutate():n==="code"?v.mutate():n==="token"&&y.mutate()},E=x.isPending||v.isPending||y.isPending;return l.jsx(St,{className:"max-w-md mx-auto",children:l.jsxs(Tt,{children:[l.jsxs("div",{className:"text-center mb-6",children:[l.jsx("div",{className:"inline-flex items-center justify-center w-12 h-12 rounded-xl bg-bambu-green/20 mb-3",children:l.jsx(qS,{className:"w-6 h-6 text-bambu-green"})}),l.jsx("h2",{className:"text-xl font-semibold text-white",children:"Connect to Bambu Cloud"}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Sync your slicer presets across devices"})]}),l.jsxs("form",{onSubmit:b,className:"space-y-4",children:[n==="email"&&l.jsxs(l.Fragment,{children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Email"}),l.jsx("input",{type:"email",value:i,onChange:w=>a(w.target.value),className:"w-full px-3 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",placeholder:"your@email.com",required:!0})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),l.jsx("input",{type:"password",value:s,onChange:w=>o(w.target.value),className:"w-full px-3 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",placeholder:"••••••••",required:!0})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Region"}),l.jsxs("select",{value:g,onChange:w=>p(w.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",children:[l.jsx("option",{value:"global",children:"Global"}),l.jsx("option",{value:"china",children:"China"})]})]})]}),n==="code"&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Verification Code"}),l.jsxs("p",{className:"text-xs text-bambu-gray mb-2",children:["Check your email (",i,") for a 6-digit code"]}),l.jsx("input",{type:"text",value:c,onChange:w=>u(w.target.value.replace(/\D/g,"").slice(0,6)),className:"w-full px-3 py-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-center text-2xl tracking-widest font-mono focus:border-bambu-green focus:outline-none",placeholder:"000000",maxLength:6,required:!0})]}),n==="token"&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Access Token"}),l.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Paste your Bambu Lab access token (from Bambu Studio)"}),l.jsx("textarea",{value:d,onChange:w=>f(w.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-xs font-mono placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none resize-none",placeholder:"eyJ...",rows:4,required:!0})]}),l.jsxs("div",{className:"flex gap-2",children:[n==="code"&&l.jsx(ke,{type:"button",variant:"secondary",onClick:()=>r("email"),className:"flex-1",children:"Back"}),l.jsxs(ke,{type:"submit",disabled:E,className:"flex-1",children:[E?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(mU,{className:"w-4 h-4"}),n==="email"?"Login":n==="code"?"Verify":"Set Token"]})]}),n==="email"&&l.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:l.jsxs("button",{type:"button",onClick:()=>r("token"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[l.jsx(Qm,{className:"w-4 h-4"}),"Use access token instead"]})}),n==="token"&&l.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:l.jsxs("button",{type:"button",onClick:()=>r("email"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[l.jsx(mU,{className:"w-4 h-4"}),"Login with email instead"]})})]})]})})}function Vm({label:t,value:e,options:n,onChange:r}){const[i,a]=M.useState(!1),s=n.find(o=>o.value===e);return l.jsxs("div",{className:"relative",children:[l.jsxs("button",{onClick:()=>a(!i),className:"flex items-center gap-2 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-sm text-white hover:border-bambu-gray-dark transition-colors",children:[l.jsxs("span",{className:"text-bambu-gray",children:[t,":"]}),l.jsx("span",{children:s?.label||"All"}),l.jsx(Nl,{className:`w-4 h-4 text-bambu-gray transition-transform ${i?"rotate-180":""}`})]}),i&&l.jsxs(l.Fragment,{children:[l.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>a(!1)}),l.jsx("div",{className:"absolute top-full left-0 mt-1 min-w-[160px] bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl z-20 py-1 max-h-60 overflow-y-auto",children:n.map(o=>l.jsxs("button",{onClick:()=>{r(o.value),a(!1)},className:`w-full px-3 py-2 text-left text-sm flex items-center justify-between hover:bg-bambu-dark-tertiary transition-colors ${e===o.value?"text-bambu-green":"text-white"}`,children:[l.jsx("span",{children:o.label}),o.count!==void 0&&l.jsx("span",{className:"text-bambu-gray text-xs",children:o.count})]},o.value))})]})]})}function ZGe(){const[t,e]=M.useState(!1);M.useEffect(()=>{const r=()=>{e(window.scrollY>300)};return window.addEventListener("scroll",r),()=>window.removeEventListener("scroll",r)},[]);const n=()=>{window.scrollTo({top:0,behavior:"smooth"})};return t?l.jsx("button",{onClick:n,className:"fixed bottom-6 right-6 p-3 bg-bambu-green hover:bg-bambu-green-light text-white rounded-full shadow-lg shadow-bambu-green/25 transition-all z-40","aria-label":"Scroll to top",children:l.jsx(bp,{className:"w-5 h-5"})}):null}function Ek({setting:t,onClick:e,onDuplicate:n,compareMode:r,isCompareSelected:i,compareIndex:a,compareDisabled:s}){const o=OL(t.name),c=gb(t.setting_id);return l.jsxs("div",{className:"flex items-center gap-2 group",children:[l.jsx("button",{onClick:e,disabled:s,className:`flex-1 text-left px-3 py-2 rounded transition-colors ${i?"bg-blue-500/20 border border-blue-500/50":s?"bg-bambu-dark/50 opacity-40 cursor-not-allowed":"bg-bambu-dark hover:bg-bambu-dark-tertiary"} ${r&&!s?"cursor-pointer":""}`,children:l.jsxs("div",{className:"flex items-center gap-2",children:[i&&a!==void 0&&l.jsx("span",{className:"flex-shrink-0 w-5 h-5 rounded-full bg-blue-500 text-white text-xs flex items-center justify-center font-medium",children:a+1}),!i&&c&&l.jsx("span",{className:"flex-shrink-0 w-1.5 h-1.5 rounded-full bg-bambu-green",title:"My preset (editable)"}),l.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name}),o.filamentType&&t.type==="filament"&&l.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:o.filamentType}),o.layerHeight&&t.type==="process"&&l.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:o.layerHeight}),o.printer&&l.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:o.printer})]})}),l.jsx("button",{onClick:u=>{u.stopPropagation(),n()},className:"opacity-0 group-hover:opacity-100 text-bambu-gray hover:text-white transition-all p-1",title:"Duplicate",children:l.jsx(md,{className:"w-4 h-4"})})]})}function $j(t,e=0){const n=" ".repeat(e);if(t===null)return"null";if(t===void 0)return"undefined";if(typeof t=="string"){if(t.includes("\\n")||t.includes(` + `,children:l.jsxs("div",{className:"flex items-center gap-4 p-4",children:[v?l.jsx("div",{...o,...c,className:"flex items-center justify-center w-10 h-10 md:w-8 md:h-8 rounded-lg bg-bambu-dark cursor-grab active:cursor-grabbing hover:bg-bambu-dark-tertiary transition-colors touch-manipulation",children:l.jsx(N0,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}):e!==void 0?l.jsxs("div",{className:"flex items-center justify-center w-8 h-8 rounded-lg bg-bambu-dark text-bambu-gray text-sm font-medium",children:["#",e]}):l.jsx("div",{className:"w-8"}),l.jsx("div",{className:"w-14 h-14 flex-shrink-0 bg-bambu-dark rounded-lg overflow-hidden",children:t.archive_thumbnail?l.jsx("img",{src:Ee.getArchiveThumbnail(t.archive_id),alt:"",className:"w-full h-full object-cover"}):l.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:l.jsx(pd,{className:"w-6 h-6"})})}),l.jsxs("div",{className:"flex-1 min-w-0",children:[l.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[l.jsx("p",{className:"text-white font-medium truncate",children:t.archive_name||`Archive #${t.archive_id}`}),l.jsx(vc,{to:`/archives?highlight=${t.archive_id}`,className:"text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0",title:"View archive",children:l.jsx(ga,{className:"w-3.5 h-3.5"})})]}),l.jsxs("div",{className:"flex items-center gap-3 text-sm text-bambu-gray",children:[l.jsxs("span",{className:"flex items-center gap-1.5",children:[l.jsx(Ui,{className:"w-3.5 h-3.5"}),t.printer_name||`Printer #${t.printer_id}`]}),t.print_time_seconds&&l.jsxs("span",{className:"flex items-center gap-1.5",children:[l.jsx(Zu,{className:"w-3.5 h-3.5"}),Pte(t.print_time_seconds)]}),v&&l.jsxs("span",{className:"flex items-center gap-1.5",children:[l.jsx(tr,{className:"w-3.5 h-3.5"}),uGe(t.scheduled_time)]})]}),l.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[t.require_previous_success&&l.jsx("span",{className:"text-xs px-2 py-0.5 bg-orange-500/10 text-orange-400 rounded-full border border-orange-500/20",children:"Requires previous success"}),t.auto_off_after&&l.jsxs("span",{className:"text-xs px-2 py-0.5 bg-blue-500/10 text-blue-400 rounded-full border border-blue-500/20 flex items-center gap-1",children:[l.jsx(Cl,{className:"w-3 h-3"}),"Auto power off"]})]}),x&&l.jsxs("div",{className:"mt-3",children:[l.jsx("div",{className:"h-2 bg-bambu-dark rounded-full overflow-hidden",children:l.jsx("div",{className:"h-full bg-gradient-to-r from-blue-500 to-blue-400 animate-pulse w-full opacity-50"})}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Printing in progress..."})]}),t.error_message&&l.jsxs("p",{className:"text-xs text-red-400 mt-2 flex items-center gap-1",children:[l.jsx(ho,{className:"w-3 h-3"}),t.error_message]})]}),l.jsx(dGe,{status:t.status}),l.jsxs("div",{className:"flex items-center gap-1",children:[x&&l.jsx(ke,{variant:"ghost",size:"sm",onClick:a,title:"Stop Print",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:l.jsx(Koe,{className:"w-4 h-4"})}),v&&l.jsxs(l.Fragment,{children:[l.jsx(ke,{variant:"ghost",size:"sm",onClick:n,title:"Edit",children:l.jsx(Fc,{className:"w-4 h-4"})}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:r,title:"Cancel",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:l.jsx(Vt,{className:"w-4 h-4"})})]}),y&&l.jsxs(l.Fragment,{children:[l.jsx(ke,{variant:"ghost",size:"sm",onClick:s,title:"Re-queue",className:"text-bambu-green hover:text-bambu-green/80 hover:bg-bambu-green/10",children:l.jsx(ya,{className:"w-4 h-4"})}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:i,title:"Remove",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]})]})})}function hGe(){const t=hn(),{showToast:e}=vr(),[n,r]=M.useState(null),[i,a]=M.useState(""),[s,o]=M.useState(!1),[c,u]=M.useState(null),[d,f]=M.useState(null),[g,p]=M.useState(null),[x,v]=M.useState(()=>localStorage.getItem("queue.historySortBy")||"date"),[y,b]=M.useState(()=>{const Q=localStorage.getItem("queue.historySortAsc");return Q!==null?Q==="true":!1}),[E,w]=M.useState(()=>localStorage.getItem("queue.pendingSortBy")||"position"),[T,A]=M.useState(()=>{const Q=localStorage.getItem("queue.pendingSortAsc");return Q!==null?Q==="true":!0});M.useEffect(()=>{localStorage.setItem("queue.historySortBy",x)},[x]),M.useEffect(()=>{localStorage.setItem("queue.historySortAsc",String(y))},[y]),M.useEffect(()=>{localStorage.setItem("queue.pendingSortBy",E)},[E]),M.useEffect(()=>{localStorage.setItem("queue.pendingSortAsc",String(T))},[T]);const C=ste(BS(wE,{activationConstraint:{distance:8}}),BS(_E,{coordinateGetter:Cte})),{data:N,isLoading:F}=xt({queryKey:["queue",n,i],queryFn:()=>Ee.getQueue(n||void 0,i||void 0),refetchInterval:5e3}),{data:k}=xt({queryKey:["printers"],queryFn:()=>Ee.getPrinters()}),R=_t({mutationFn:Q=>Ee.cancelQueueItem(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item cancelled")},onError:()=>e("Failed to cancel item","error")}),I=_t({mutationFn:Q=>Ee.removeFromQueue(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item removed")},onError:()=>e("Failed to remove item","error")}),L=_t({mutationFn:Q=>Ee.stopQueueItem(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Print stopped")},onError:()=>e("Failed to stop print","error")}),q=_t({mutationFn:Q=>Ee.reorderQueue(Q),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]})},onError:()=>e("Failed to reorder queue","error")}),j=_t({mutationFn:async()=>{const Q=N?.filter(B=>["completed","failed","skipped","cancelled"].includes(B.status))||[];for(const B of Q)await Ee.removeFromQueue(B.id);return Q.length},onSuccess:Q=>{t.invalidateQueries({queryKey:["queue"]}),e(`Cleared ${Q} history item${Q!==1?"s":""}`)},onError:()=>e("Failed to clear history","error")}),V=M.useMemo(()=>{const Q=N?.filter($=>$.status==="pending")||[],B=$=>{if(!$.scheduled_time)return 0;const Z=new Date($.scheduled_time).getTime(),te=Date.now()+4320*60*60*1e3;return Z>te?0:Z};return[...Q].sort(($,Z)=>{let te;return E==="name"?te=($.archive_name||"").localeCompare(Z.archive_name||""):E==="printer"?te=($.printer_name||"").localeCompare(Z.printer_name||""):E==="time"?te=B($)-B(Z):te=$.position-Z.position,T?te:-te})},[N,E,T]),K=N?.filter(Q=>Q.status==="printing")||[],G=M.useMemo(()=>[...N?.filter(B=>["completed","failed","skipped","cancelled"].includes(B.status))||[]].sort((B,$)=>{let Z;return x==="name"?Z=(B.archive_name||"").localeCompare($.archive_name||""):x==="printer"?Z=(B.printer_name||"").localeCompare($.printer_name||""):Z=new Date($.completed_at||$.created_at).getTime()-new Date(B.completed_at||B.created_at).getTime(),y?-Z:Z}),[N,x,y]),J=M.useMemo(()=>V.reduce((Q,B)=>Q+(B.print_time_seconds||0),0),[V]),ee=Q=>{const{active:B,over:$}=Q;if(!$||B.id===$.id)return;const Z=V.findIndex(he=>he.id===B.id),te=V.findIndex(he=>he.id===$.id);if(Z!==-1&&te!==-1){const de=EE(V,Z,te).map((ge,Ae)=>({id:ge.id,position:Ae+1}));q.mutate(de)}};return l.jsxs("div",{className:"p-4 md:p-8",children:[l.jsx("div",{className:"flex items-center justify-between mb-8",children:l.jsxs("div",{children:[l.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[l.jsx(TG,{className:"w-7 h-7 text-bambu-green"}),"Print Queue"]}),l.jsx("p",{className:"text-bambu-gray mt-1",children:"Schedule and manage your print jobs"})]})}),l.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4 mb-8",children:[l.jsx(St,{className:"bg-gradient-to-br from-blue-500/10 to-transparent border-blue-500/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-blue-500/20 flex items-center justify-center",children:l.jsx(wp,{className:"w-5 h-5 text-blue-400"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:K.length}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Printing"})]})]})})}),l.jsx(St,{className:"bg-gradient-to-br from-yellow-500/10 to-transparent border-yellow-500/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-yellow-500/20 flex items-center justify-center",children:l.jsx(tr,{className:"w-5 h-5 text-yellow-400"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:V.length}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Queued"})]})]})})}),l.jsx(St,{className:"bg-gradient-to-br from-bambu-green/10 to-transparent border-bambu-green/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-bambu-green/20 flex items-center justify-center",children:l.jsx(Zu,{className:"w-5 h-5 text-bambu-green"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:Pte(J)}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Total Queue Time"})]})]})})}),l.jsx(St,{className:"bg-gradient-to-br from-gray-500/10 to-transparent border-gray-500/20",children:l.jsx(Tt,{className:"p-4",children:l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"w-10 h-10 rounded-lg bg-gray-500/20 flex items-center justify-center",children:l.jsx(Mr,{className:"w-5 h-5 text-gray-400"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-2xl font-bold text-white",children:G.length}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"History"})]})]})})})]}),l.jsxs("div",{className:"flex items-center gap-4 mb-6",children:[l.jsxs("select",{className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:n||"",onChange:Q=>r(Q.target.value?Number(Q.target.value):null),children:[l.jsx("option",{value:"",children:"All Printers"}),k?.map(Q=>l.jsx("option",{value:Q.id,children:Q.name},Q.id))]}),l.jsxs("select",{className:"px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:i,onChange:Q=>a(Q.target.value),children:[l.jsx("option",{value:"",children:"All Status"}),l.jsx("option",{value:"pending",children:"Pending"}),l.jsx("option",{value:"printing",children:"Printing"}),l.jsx("option",{value:"completed",children:"Completed"}),l.jsx("option",{value:"failed",children:"Failed"}),l.jsx("option",{value:"skipped",children:"Skipped"}),l.jsx("option",{value:"cancelled",children:"Cancelled"})]}),l.jsx("div",{className:"flex-1"}),G.length>0&&l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>o(!0),children:[l.jsx(Sn,{className:"w-4 h-4"}),"Clear History"]})]}),F?l.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading..."}):N?.length===0?l.jsxs(St,{className:"p-12 text-center border-dashed",children:[l.jsx(qr,{className:"w-16 h-16 text-bambu-gray mx-auto mb-4 opacity-50"}),l.jsx("h3",{className:"text-xl font-medium text-white mb-2",children:"No prints scheduled"}),l.jsx("p",{className:"text-bambu-gray max-w-md mx-auto",children:'Schedule a print from the Archives page using the "Schedule" option in the context menu, or drag and drop files to get started.'})]}):l.jsxs("div",{className:"space-y-8",children:[K.length>0&&l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white mb-4 flex items-center gap-2",children:[l.jsx("div",{className:"w-2 h-2 rounded-full bg-blue-400 animate-pulse"}),"Currently Printing"]}),l.jsx("div",{className:"space-y-3",children:K.map(Q=>l.jsx(wk,{item:Q,onEdit:()=>{},onCancel:()=>{},onRemove:()=>{},onStop:()=>p({type:"stop",item:Q}),onRequeue:()=>{}},Q.id))})]}),V.length>0&&l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-4",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(tr,{className:"w-5 h-5 text-yellow-400"}),"Queued",l.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",V.length," item",V.length!==1?"s":"",")"]}),l.jsx("span",{className:"text-xs text-bambu-gray ml-2",title:"Position only affects ASAP items. Scheduled items run at their set time.",children:"Drag to reorder (ASAP only)"})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("select",{className:"px-3 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:E,onChange:Q=>w(Q.target.value),children:[l.jsx("option",{value:"position",children:"Sort by Position"}),l.jsx("option",{value:"name",children:"Sort by Name"}),l.jsx("option",{value:"printer",children:"Sort by Printer"}),l.jsx("option",{value:"time",children:"Sort by Schedule"})]}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:()=>A(!T),title:T?"Ascending":"Descending",className:"px-2",children:T?l.jsx(bp,{className:"w-4 h-4"}):l.jsx(E0,{className:"w-4 h-4"})})]})]}),l.jsx(Ste,{sensors:C,collisionDetection:ute,onDragEnd:ee,children:l.jsx(Tte,{items:V.map(Q=>Q.id),strategy:Q9e,children:l.jsx("div",{className:"space-y-3",children:V.map((Q,B)=>l.jsx(wk,{item:Q,position:B+1,onEdit:()=>u(Q),onCancel:()=>p({type:"cancel",item:Q}),onRemove:()=>{},onStop:()=>{},onRequeue:()=>{}},Q.id))})})})]}),G.length>0&&l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-4",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(Mr,{className:"w-5 h-5 text-bambu-gray"}),"History",l.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",G.length," item",G.length!==1?"s":"",")"]})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("select",{className:"px-3 py-1.5 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",value:x,onChange:Q=>v(Q.target.value),children:[l.jsx("option",{value:"date",children:"Sort by Date"}),l.jsx("option",{value:"name",children:"Sort by Name"}),l.jsx("option",{value:"printer",children:"Sort by Printer"})]}),l.jsx(ke,{variant:"ghost",size:"sm",onClick:()=>b(!y),title:y?"Ascending (oldest first)":"Descending (newest first)",className:"px-2",children:y?l.jsx(bp,{className:"w-4 h-4"}):l.jsx(E0,{className:"w-4 h-4"})})]})]}),l.jsx("div",{className:"space-y-3",children:G.slice(0,20).map((Q,B)=>l.jsx(wk,{item:Q,position:B+1,onEdit:()=>{},onCancel:()=>{},onRemove:()=>p({type:"remove",item:Q}),onStop:()=>{},onRequeue:()=>f(Q)},Q.id))})]})]}),c&&l.jsx(cGe,{item:c,onClose:()=>u(null)}),d&&l.jsx(nte,{archiveId:d.archive_id,archiveName:d.archive_name||`Archive #${d.archive_id}`,onClose:()=>f(null)}),g&&l.jsx(Lr,{title:g.type==="cancel"?"Cancel Scheduled Print":g.type==="stop"?"Stop Print":"Remove from History",message:g.type==="cancel"?`Are you sure you want to cancel "${g.item.archive_name||"this print"}"?`:g.type==="stop"?`Are you sure you want to stop the current print "${g.item.archive_name||"this print"}"? This will cancel the print job on the printer.`:`Are you sure you want to remove "${g.item.archive_name||"this item"}" from the queue history?`,confirmText:g.type==="cancel"?"Cancel Print":g.type==="stop"?"Stop Print":"Remove",variant:"danger",onConfirm:()=>{g.type==="cancel"?R.mutate(g.item.id):g.type==="stop"?L.mutate(g.item.id):I.mutate(g.item.id),p(null)},onCancel:()=>p(null)}),s&&l.jsx(Lr,{title:"Clear History",message:`Are you sure you want to remove all ${G.length} item${G.length!==1?"s":""} from the history?`,confirmText:"Clear History",variant:"danger",onConfirm:()=>{j.mutate(),o(!1)},onCancel:()=>o(!1)})]})}function fGe({printDates:t,months:e=3}){const{weeks:n,monthLabels:r,printCounts:i}=M.useMemo(()=>{const c={};t.forEach(b=>{const E=b.split("T")[0];c[E]=(c[E]||0)+1});const u=new Date,d=new Date(u);d.setMonth(d.getMonth()-e),d.setDate(d.getDate()-d.getDay());const f=[],g=[];let p=[],x=-1;const v=new Date(d);let y=0;for(;v<=u;)v.getDay()===0&&p.length>0&&(f.push(p),p=[],y++),v.getMonth()!==x&&(g.push({month:v.toLocaleDateString("en-US",{month:"short"}),weekIndex:y}),x=v.getMonth()),p.push(new Date(v)),v.setDate(v.getDate()+1);return p.length>0&&f.push(p),{weeks:f,monthLabels:g,printCounts:c}},[t,e]),a=Math.max(1,...Object.values(i)),s=c=>{if(c===0)return"bg-bambu-dark";const u=c/a;return u<=.25?"bg-bambu-green/30":u<=.5?"bg-bambu-green/50":u<=.75?"bg-bambu-green/75":"bg-bambu-green"},o=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];return l.jsxs("div",{className:"overflow-x-auto",children:[l.jsx("div",{className:"flex mb-1 ml-8",children:r.map(({month:c,weekIndex:u},d)=>l.jsx("div",{className:"text-xs text-bambu-gray",style:{marginLeft:d===0?0:`${(u-(r[d-1]?.weekIndex||0))*14-24}px`},children:c},d))}),l.jsxs("div",{className:"flex gap-0.5",children:[l.jsx("div",{className:"flex flex-col gap-0.5 mr-1",children:o.map((c,u)=>l.jsx("div",{className:"h-3 text-xs text-bambu-gray flex items-center",style:{visibility:u%2===1?"visible":"hidden"},children:c},c))}),n.map((c,u)=>l.jsx("div",{className:"flex flex-col gap-0.5",children:[0,1,2,3,4,5,6].map(d=>{const f=c.find(v=>v.getDay()===d);if(!f)return l.jsx("div",{className:"w-3 h-3"},d);const g=f.toISOString().split("T")[0],p=i[g]||0,x=g===new Date().toISOString().split("T")[0];return l.jsx("div",{className:`w-3 h-3 rounded-sm ${s(p)} ${x?"ring-1 ring-white":""}`,title:`${f.toLocaleDateString()}: ${p} print${p!==1?"s":""}`},d)})},u))]}),l.jsxs("div",{className:"flex items-center gap-2 mt-3 text-xs text-bambu-gray",children:[l.jsx("span",{children:"Less"}),l.jsxs("div",{className:"flex gap-0.5",children:[l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-dark"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green/30"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green/50"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green/75"}),l.jsx("div",{className:"w-3 h-3 rounded-sm bg-bambu-green"})]}),l.jsx("span",{children:"More"})]})]})}const n1=["#00ae42","#3b82f6","#f59e0b","#ef4444","#8b5cf6","#ec4899","#14b8a6","#f97316"];function mGe(t){const e=new Date;switch(t){case"7d":return new Date(e.getTime()-10080*60*1e3);case"30d":return new Date(e.getTime()-720*60*60*1e3);case"90d":return new Date(e.getTime()-2160*60*60*1e3);case"365d":return new Date(e.getTime()-365*24*60*60*1e3);case"all":return new Date(0)}}function pGe({archives:t,currency:e="$"}){const[n,r]=M.useState("30d"),i=M.useMemo(()=>{const g=mGe(n);return t.filter(p=>new Date(p.completed_at||p.created_at)>=g)},[t,n]),a=M.useMemo(()=>{const g=new Map;return i.forEach(p=>{const v=new Date(p.completed_at||p.created_at).toISOString().split("T")[0],y=g.get(v)||{date:v,filament:0,cost:0,prints:0};y.filament+=p.filament_used_grams||0,y.cost+=p.cost||0,y.prints+=1,g.set(v,y)}),Array.from(g.values()).sort((p,x)=>p.date.localeCompare(x.date)).map(p=>({...p,dateLabel:new Date(p.date).toLocaleDateString("en-US",{month:"short",day:"numeric"})}))},[i]),s=M.useMemo(()=>{if(n==="7d"||n==="30d")return a;const g=new Map;return i.forEach(p=>{const x=new Date(p.completed_at||p.created_at),v=new Date(x);v.setDate(x.getDate()-x.getDay());const y=v.toISOString().split("T")[0],b=g.get(y)||{week:y,filament:0,cost:0,prints:0};b.filament+=p.filament_used_grams||0,b.cost+=p.cost||0,b.prints+=1,g.set(y,b)}),Array.from(g.values()).sort((p,x)=>p.week.localeCompare(x.week)).map(p=>({date:p.week,dateLabel:`Week of ${new Date(p.week).toLocaleDateString("en-US",{month:"short",day:"numeric"})}`,...p}))},[i,a,n]),o=M.useMemo(()=>{const g=new Map;return i.forEach(p=>{const v=(p.filament_type||"Unknown").split(", ");v.forEach(y=>{const b=(p.filament_used_grams||0)/v.length;g.set(y,(g.get(y)||0)+b)})}),Array.from(g.entries()).map(([p,x])=>({name:p,value:Math.round(x)})).sort((p,x)=>x.value-p.value)},[i]),c=M.useMemo(()=>{const g=new Date,p=[];for(let x=5;x>=0;x--){const v=new Date(g.getFullYear(),g.getMonth()-x,1),y=new Date(g.getFullYear(),g.getMonth()-x+1,0),b=v.toLocaleDateString("en-US",{month:"short",year:"2-digit"}),E=t.filter(w=>{const T=new Date(w.completed_at||w.created_at);return T>=v&&T<=y});p.push({month:b,filament:Math.round(E.reduce((w,T)=>w+(T.filament_used_grams||0),0)),cost:E.reduce((w,T)=>w+(T.cost||0),0),prints:E.length})}return p},[t]),u=n==="7d"||n==="30d"?a:s,d=i.reduce((g,p)=>g+(p.filament_used_grams||0),0),f=i.reduce((g,p)=>g+(p.cost||0),0);return l.jsxs("div",{className:"space-y-6",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:"Filament Usage Trends"}),l.jsx("div",{className:"flex gap-1 bg-bambu-dark rounded-lg p-1",children:["7d","30d","90d","365d","all"].map(g=>l.jsx("button",{onClick:()=>r(g),className:`px-3 py-1 text-sm rounded-md transition-colors ${n===g?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:g==="all"?"All":g.replace("d","D")},g))})]}),l.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Filament"}),l.jsxs("p",{className:"text-2xl font-bold text-white",children:[(d/1e3).toFixed(2),"kg"]}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[d.toFixed(0),"g total"]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Cost"}),l.jsxs("p",{className:"text-2xl font-bold text-white",children:[e,f.toFixed(2)]}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[i.length," prints"]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Avg per Print"}),l.jsxs("p",{className:"text-2xl font-bold text-white",children:[i.length>0?(d/i.length).toFixed(0):0,"g"]}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[e,i.length>0?(f/i.length).toFixed(2):"0.00"," avg"]})]})]}),u.length>0?l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Usage Over Time"}),l.jsx(d1,{width:"100%",height:250,children:l.jsxs(ANe,{data:u,children:[l.jsx("defs",{children:l.jsxs("linearGradient",{id:"colorFilament",x1:"0",y1:"0",x2:"0",y2:"1",children:[l.jsx("stop",{offset:"5%",stopColor:"#00ae42",stopOpacity:.3}),l.jsx("stop",{offset:"95%",stopColor:"#00ae42",stopOpacity:0})]})}),l.jsx(jw,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),l.jsx(Dw,{dataKey:"dateLabel",stroke:"#9ca3af",tick:{fontSize:12},interval:"preserveStartEnd"}),l.jsx(Ow,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:g=>`${g}g`}),l.jsx(f1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},labelStyle:{color:"#fff"},formatter:g=>[`${g.toFixed(0)}g`,"Filament"]}),l.jsx(FY,{type:"monotone",dataKey:"filament",stroke:"#00ae42",strokeWidth:2,fillOpacity:1,fill:"url(#colorFilament)"})]})})]}):l.jsx("div",{className:"bg-bambu-dark rounded-lg p-8 text-center text-bambu-gray",children:"No data for selected time range"}),l.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"By Filament Type"}),o.length>0?l.jsxs("div",{className:"flex items-center gap-4",children:[l.jsx(d1,{width:160,height:160,children:l.jsxs(ENe,{children:[l.jsx(lY,{data:o,cx:"50%",cy:"50%",innerRadius:40,outerRadius:70,paddingAngle:2,dataKey:"value",children:o.map((g,p)=>l.jsx(Zb,{fill:n1[p%n1.length]},`cell-${p}`))}),l.jsx(f1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:g=>[`${g}g`,"Usage"]})]})}),l.jsx("div",{className:"flex-1 space-y-2 overflow-hidden",children:o.map((g,p)=>{const x=o.reduce((y,b)=>y+b.value,0),v=x>0?(g.value/x*100).toFixed(0):0;return l.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[l.jsx("div",{className:"w-3 h-3 rounded-sm flex-shrink-0",style:{backgroundColor:n1[p%n1.length]}}),l.jsx("span",{className:"text-white truncate flex-1",children:g.name}),l.jsxs("span",{className:"text-bambu-gray flex-shrink-0",children:[v,"%"]})]},g.name)})})]}):l.jsx("div",{className:"h-[160px] flex items-center justify-center text-bambu-gray",children:"No filament data"})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[l.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Monthly Comparison"}),l.jsx(d1,{width:"100%",height:200,children:l.jsxs(fNe,{data:c,children:[l.jsx(jw,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),l.jsx(Dw,{dataKey:"month",stroke:"#9ca3af",tick:{fontSize:12}}),l.jsx(Ow,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:g=>`${g}g`}),l.jsx(f1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:(g,p)=>[p==="filament"?`${g}g`:p==="cost"?`${e}${g.toFixed(2)}`:g,p==="filament"?"Filament":p==="cost"?"Cost":"Prints"]}),l.jsx(FR,{}),l.jsx(mY,{dataKey:"filament",name:"Filament (g)",fill:"#00ae42",radius:[4,4,0,0]})]})})]})]})]})}function gGe({id:t,title:e,children:n,isHidden:r,size:i,onToggleVisibility:a,onToggleSize:s}){const{attributes:o,listeners:c,setNodeRef:u,transform:d,transition:f,isDragging:g}=Nte({id:t}),p={transform:Yp.Transform.toString(d),transition:f,opacity:g?.5:1};return r?null:l.jsxs("div",{ref:u,style:{...p,gridColumn:`span ${i}`},className:`bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary overflow-hidden ${g?"ring-2 ring-bambu-green shadow-lg":""}`,children:[l.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/30",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("button",{...o,...c,className:"cursor-grab active:cursor-grabbing p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Drag to reorder",children:l.jsx(N0,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}),l.jsx("h3",{className:"text-sm font-medium text-white",children:e})]}),l.jsxs("div",{className:"flex items-center gap-1",children:[l.jsx("button",{onClick:s,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:`Size: ${i===1?"1/4":i===2?"1/2":"Full"} - Click to cycle`,children:i===4?l.jsx(jce,{className:"w-4 h-4 text-bambu-gray hover:text-white"}):l.jsx(Mce,{className:"w-4 h-4 text-bambu-gray hover:text-white"})}),l.jsx("button",{onClick:a,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Hide widget",children:l.jsx(KS,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]})]}),l.jsx("div",{className:"p-4",children:n})]})}function xGe({widgets:t,storageKey:e,columns:n=4,hideControls:r=!1,onResetLayout:i,renderControls:a}){const s=()=>{const T={};return t.forEach(A=>{T[A.id]=A.defaultSize||4}),T},[o,c]=M.useState(()=>{const T=localStorage.getItem(e);if(T)try{const A=JSON.parse(T);return A.sizes||(A.sizes=s()),A}catch{}return{order:t.map(A=>A.id),hidden:t.filter(A=>A.defaultVisible===!1).map(A=>A.id),sizes:s()}}),[u,d]=M.useState(!1);M.useEffect(()=>{const T=()=>d(A=>!A);return window.addEventListener("toggle-hidden-panel",T),()=>window.removeEventListener("toggle-hidden-panel",T)},[]),M.useEffect(()=>{localStorage.setItem(e,JSON.stringify(o))},[o,e]),M.useEffect(()=>{const A=t.map(C=>C.id).filter(C=>!o.order.includes(C));A.length>0&&c(C=>({...C,order:[...C.order,...A]}))},[t,o.order]);const f=ste(BS(wE,{activationConstraint:{distance:8}}),BS(_E,{coordinateGetter:Cte})),g=T=>{const{active:A,over:C}=T;C&&A.id!==C.id&&c(N=>{const F=N.order.indexOf(A.id),k=N.order.indexOf(C.id);return{...N,order:EE(N.order,F,k)}})},p=T=>{c(A=>({...A,hidden:A.hidden.includes(T)?A.hidden.filter(C=>C!==T):[...A.hidden,T]}))},x=T=>{c(A=>{const C=A.sizes[T]||4,N=C===1?2:C===2?4:1;return{...A,sizes:{...A.sizes,[T]:N}}})},v=()=>{const T={order:t.map(A=>A.id),hidden:t.filter(A=>A.defaultVisible===!1).map(A=>A.id),sizes:s()};c(T),i?.()},y=o.order.map(T=>t.find(A=>A.id===T)).filter(Boolean),b=y.filter(T=>!o.hidden.includes(T.id)),E=y.filter(T=>o.hidden.includes(T.id)),w=a?.({hiddenCount:E.length,showHiddenPanel:u,setShowHiddenPanel:d,resetLayout:v});return l.jsxs("div",{className:"space-y-4",children:[w,!r&&!a&&l.jsxs("div",{className:"flex items-center justify-end gap-2",children:[l.jsxs(ke,{variant:"secondary",size:"sm",onClick:v,children:[l.jsx(ng,{className:"w-4 h-4"}),"Reset Layout"]}),E.length>0&&l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>d(!u),children:[l.jsx(Yh,{className:"w-4 h-4"}),E.length," Hidden"]})]}),u&&E.length>0&&l.jsxs("div",{className:"p-4 bg-bambu-dark rounded-xl border border-bambu-dark-tertiary",children:[l.jsx("p",{className:"text-sm text-bambu-gray mb-3",children:"Hidden widgets (click to show):"}),l.jsx("div",{className:"flex flex-wrap gap-2",children:E.map(T=>l.jsxs("button",{onClick:()=>p(T.id),className:"px-3 py-1.5 bg-bambu-dark-tertiary hover:bg-bambu-green/20 rounded-lg text-sm text-white transition-colors flex items-center gap-2",children:[l.jsx(Yh,{className:"w-3 h-3"}),T.title]},T.id))})]}),l.jsx(Ste,{sensors:f,collisionDetection:ute,onDragEnd:g,children:l.jsx(Tte,{items:b.map(T=>T.id),strategy:DL,children:l.jsx("div",{className:"grid gap-6",style:{gridTemplateColumns:`repeat(${n}, minmax(0, 1fr))`},children:b.map(T=>l.jsx(gGe,{id:T.id,title:T.title,isHidden:o.hidden.includes(T.id),size:o.sizes[T.id]||2,onToggleVisibility:()=>p(T.id),onToggleSize:()=>x(T.id),children:T.component},T.id))})})}),b.length===0&&l.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[l.jsx("p",{children:"All widgets are hidden."}),l.jsx(ke,{className:"mt-4",onClick:v,children:"Reset Layout"})]})]})}function bGe({stats:t,currency:e}){return l.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-3 gap-4",children:[l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:l.jsx(Ic,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Total Prints"}),l.jsx("p",{className:"text-xl font-bold text-white",children:t?.total_prints||0})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-blue-400",children:l.jsx(tr,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Print Time"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_print_time_hours.toFixed(1)||0,"h"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-orange-400",children:l.jsx(Ic,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Used"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[((t?.total_filament_grams||0)/1e3).toFixed(2),"kg"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-green-400",children:l.jsx(Hk,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Cost"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_cost.toFixed(2)||"0.00"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-400",children:l.jsx(Jh,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Used"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_energy_kwh.toFixed(2)||"0.00"," kWh"]})]})]}),l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-500",children:l.jsx(Hk,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Cost"}),l.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_energy_cost.toFixed(2)||"0.00"]})]})]})]})}function yGe({stats:t}){const e=t?.total_prints?Math.round(t.successful_prints/t.total_prints*100):0;return l.jsxs("div",{className:"flex items-center gap-6",children:[l.jsxs("div",{className:"relative w-28 h-28",children:[l.jsxs("svg",{className:"w-full h-full -rotate-90",children:[l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:"#00ae42",strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${e*3.02} 302`})]}),l.jsx("div",{className:"absolute inset-0 flex items-center justify-center",children:l.jsxs("span",{className:"text-xl font-bold text-white",children:[e,"%"]})})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:"text-sm text-bambu-gray",children:"Successful:"}),l.jsx("span",{className:"text-sm text-white font-medium",children:t?.successful_prints||0})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400"}),l.jsx("span",{className:"text-sm text-bambu-gray",children:"Failed:"}),l.jsx("span",{className:"text-sm text-white font-medium",children:t?.failed_prints||0})]})]})]})}function vGe({stats:t,printerMap:e}){const n=t?.average_time_accuracy;if(n==null)return l.jsx("div",{className:"flex items-center justify-center h-full",children:l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No time accuracy data yet"})});const i=(Math.min(150,Math.max(50,n))-50)/100*100,s=(c=>c>=95&&c<=105?"#00ae42":c>105?"#3b82f6":"#f97316")(n),o=n-100;return l.jsxs("div",{className:"flex items-center gap-6",children:[l.jsxs("div",{className:"relative w-28 h-28",children:[l.jsxs("svg",{className:"w-full h-full -rotate-90",children:[l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),l.jsx("circle",{cx:"56",cy:"56",r:"48",fill:"none",stroke:s,strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${i*3.02} 302`})]}),l.jsxs("div",{className:"absolute inset-0 flex flex-col items-center justify-center",children:[l.jsxs("span",{className:"text-xl font-bold text-white",children:[n.toFixed(0),"%"]}),l.jsxs("span",{className:`text-xs ${o>=0?"text-blue-400":"text-orange-400"}`,children:[o>=0?"+":"",o.toFixed(0),"%"]})]})]}),l.jsxs("div",{className:"space-y-2 flex-1",children:[l.jsxs("div",{className:"flex items-center gap-2 text-xs text-bambu-gray",children:[l.jsx(PG,{className:"w-3 h-3"}),l.jsx("span",{children:"100% = perfect estimate"})]}),t?.time_accuracy_by_printer&&Object.keys(t.time_accuracy_by_printer).length>0&&l.jsx("div",{className:"space-y-1 mt-2",children:Object.entries(t.time_accuracy_by_printer).slice(0,3).map(([c,u])=>l.jsxs("div",{className:"flex items-center justify-between text-xs",children:[l.jsx("span",{className:"text-bambu-gray truncate max-w-[100px]",children:e.get(c)||`Printer ${c}`}),l.jsxs("span",{className:`font-medium ${u>=95&&u<=105?"text-bambu-green":u>105?"text-blue-400":"text-orange-400"}`,children:[u.toFixed(0),"%"]})]},c))})]})]})}function _Ge({stats:t}){if(!t?.prints_by_filament_type||Object.keys(t.prints_by_filament_type).length===0)return l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No filament data available"});const e=Object.entries(t.prints_by_filament_type).sort(([,n],[,r])=>r-n);return l.jsx("div",{className:"space-y-3",children:e.map(([n,r])=>{const i=Math.round(r/(t.total_prints||1)*100);return l.jsxs("div",{children:[l.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[l.jsx("span",{className:"text-white",children:n}),l.jsxs("span",{className:"text-bambu-gray",children:[r," prints"]})]}),l.jsx("div",{className:"h-2 bg-bambu-dark rounded-full",children:l.jsx("div",{className:"h-full bg-bambu-green rounded-full transition-all",style:{width:`${i}%`}})})]},n)})})}function wGe({printDates:t}){return l.jsx(fGe,{printDates:t,months:4})}function SGe({stats:t,printerMap:e}){return!t?.prints_by_printer||Object.keys(t.prints_by_printer).length===0?l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No printer data available"}):l.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:Object.entries(t.prints_by_printer).map(([n,r])=>l.jsxs("div",{className:"flex items-center gap-3 p-3 bg-bambu-dark rounded-lg",children:[l.jsx("div",{className:"p-2 bg-bambu-dark-tertiary rounded-lg",children:l.jsx(Ui,{className:"w-4 h-4 text-bambu-green"})}),l.jsxs("div",{children:[l.jsx("p",{className:"text-white font-medium text-sm",children:e.get(n)||`Printer ${n}`}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[r," prints"]})]})]},n))})}function MGe({archives:t,currency:e}){return!t||t.length===0?l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data available"}):l.jsx(pGe,{archives:t,currency:e})}function EGe(){const{data:t,isLoading:e}=xt({queryKey:["failureAnalysis"],queryFn:()=>Ee.getFailureAnalysis({days:30})});if(e)return l.jsx("div",{className:"flex justify-center py-4",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})});if(!t||t.total_prints===0)return l.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data in the last 30 days"});const n=Object.entries(t.failures_by_reason).sort(([,r],[,i])=>i-r).slice(0,5);return l.jsxs("div",{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center gap-4",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx($n,{className:`w-5 h-5 ${t.failure_rate>20?"text-red-400":t.failure_rate>10?"text-yellow-400":"text-bambu-green"}`}),l.jsxs("span",{className:"text-2xl font-bold text-white",children:[t.failure_rate.toFixed(1),"%"]}),l.jsx("span",{className:"text-sm text-bambu-gray",children:"failure rate"})]}),l.jsxs("div",{className:"text-sm text-bambu-gray",children:[t.failed_prints," / ",t.total_prints," prints failed"]})]}),n.length>0&&l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray font-medium",children:"Top Failure Reasons"}),n.map(([r,i])=>l.jsxs("div",{className:"flex items-center justify-between text-sm",children:[l.jsx("span",{className:"text-white truncate max-w-[200px]",children:r||"Unknown"}),l.jsx("span",{className:"text-bambu-gray",children:i})]},r))]}),t.trend&&t.trend.length>=2&&l.jsx("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:l.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[l.jsx(RG,{className:`w-4 h-4 ${t.trend[t.trend.length-1].failure_rate{const w=()=>{try{const A=localStorage.getItem("bambusy-dashboard-layout");if(A){const C=JSON.parse(A);c(C.hidden?.length||0)}}catch{c(0)}};w(),window.addEventListener("storage",w);const T=setInterval(w,500);return()=>{window.removeEventListener("storage",w),clearInterval(T)}},[a]);const{data:u,isLoading:d}=xt({queryKey:["archiveStats"],queryFn:Ee.getArchiveStats}),{data:f}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:g}=xt({queryKey:["archives"],queryFn:()=>Ee.getArchives(void 0,void 0,1e3,0)}),{data:p}=xt({queryKey:["settings"],queryFn:Ee.getSettings}),x=async w=>{i(!1),n(!0);try{const{blob:T,filename:A}=await Ee.exportStats({format:w,days:90}),C=URL.createObjectURL(T),N=document.createElement("a");N.href=C,N.download=A,N.click(),URL.revokeObjectURL(C),t("Export downloaded")}catch{t("Export failed","error")}finally{n(!1)}},v=p?.currency||"$",y=new Map(f?.map(w=>[String(w.id),w.name])||[]),b=g?.map(w=>w.created_at)||[];if(d)return l.jsx("div",{className:"p-4 md:p-8",children:l.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading statistics..."})});const E=[{id:"quick-stats",title:"Quick Stats",component:l.jsx(bGe,{stats:u,currency:v}),defaultSize:2},{id:"success-rate",title:"Success Rate",component:l.jsx(yGe,{stats:u}),defaultSize:1},{id:"time-accuracy",title:"Time Accuracy",component:l.jsx(vGe,{stats:u,printerMap:y}),defaultSize:1},{id:"filament-types",title:"Filament Types",component:l.jsx(_Ge,{stats:u}),defaultSize:1},{id:"failure-analysis",title:"Failure Analysis (30 days)",component:l.jsx(EGe,{}),defaultSize:1},{id:"print-activity",title:"Print Activity",component:l.jsx(wGe,{printDates:b}),defaultSize:2},{id:"prints-by-printer",title:"Prints by Printer",component:l.jsx(SGe,{stats:u,printerMap:y}),defaultSize:2},{id:"filament-trends",title:"Filament Usage Trends",component:l.jsx(MGe,{archives:g||[],currency:v}),defaultSize:4}];return l.jsxs("div",{className:"p-4 md:p-8",children:[l.jsxs("div",{className:"flex items-center justify-between mb-6",children:[l.jsxs("div",{children:[l.jsx("h1",{className:"text-2xl font-bold text-white",children:"Dashboard"}),l.jsx("p",{className:"text-bambu-gray",children:"Drag widgets to rearrange. Click the eye icon to hide."})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[o>0&&l.jsxs(ke,{variant:"secondary",onClick:()=>{window.dispatchEvent(new CustomEvent("toggle-hidden-panel"))},children:[l.jsx(Yh,{className:"w-4 h-4"}),o," Hidden"]}),l.jsxs(ke,{variant:"secondary",onClick:()=>{localStorage.removeItem("bambusy-dashboard-layout"),s(w=>w+1),t("Layout reset")},children:[l.jsx(ng,{className:"w-4 h-4"}),"Reset Layout"]}),l.jsxs("div",{className:"relative",children:[l.jsxs(ke,{variant:"secondary",onClick:()=>i(!r),disabled:e,children:[e?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(L1,{className:"w-4 h-4"}),"Export Stats"]}),r&&l.jsxs("div",{className:"absolute right-0 top-full mt-1 w-48 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl z-20",children:[l.jsxs("button",{className:"w-full px-4 py-2 text-left text-white hover:bg-bambu-dark-tertiary transition-colors flex items-center gap-2 rounded-t-lg",onClick:()=>x("csv"),children:[l.jsx(Ss,{className:"w-4 h-4"}),"Export as CSV"]}),l.jsxs("button",{className:"w-full px-4 py-2 text-left text-white hover:bg-bambu-dark-tertiary transition-colors flex items-center gap-2 rounded-b-lg",onClick:()=>x("xlsx"),children:[l.jsx(L1,{className:"w-4 h-4"}),"Export as Excel"]})]})]})]})]}),l.jsx(xGe,{widgets:E,storageKey:"bambusy-dashboard-layout",hideControls:!0},a)]})}function AGe({plug:t,onEdit:e}){const n=hn(),[r,i]=M.useState(!1),[a,s]=M.useState(!1),[o,c]=M.useState(!1),[u,d]=M.useState(!1),{data:f,isLoading:g,refetch:p}=xt({queryKey:["smart-plug-status",t.id],queryFn:()=>Ee.getSmartPlugStatus(t.id),refetchInterval:3e4}),{data:x}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),v=x?.find(C=>C.id===t.printer_id),y=_t({mutationFn:C=>Ee.controlSmartPlug(t.id,C),onSuccess:()=>{p()}}),b=_t({mutationFn:C=>Ee.updateSmartPlug(t.id,C),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),t.printer_id&&n.invalidateQueries({queryKey:["smartPlugByPrinter",t.printer_id]})}}),E=_t({mutationFn:()=>Ee.deleteSmartPlug(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]})}}),w=f?.state==="ON",T=f?.reachable??!1,A=y.isPending;return l.jsxs(l.Fragment,{children:[l.jsx(St,{className:"relative",children:l.jsxs(Tt,{className:"p-4",children:[l.jsxs("div",{className:"flex items-start justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:`p-2 rounded-lg ${T?w?"bg-bambu-green/20":"bg-bambu-dark":"bg-red-500/20"}`,children:l.jsx(so,{className:`w-5 h-5 ${T?w?"text-bambu-green":"text-bambu-gray":"text-red-400"}`})}),l.jsxs("div",{children:[l.jsx("h3",{className:"font-medium text-white",children:t.name}),l.jsx("p",{className:"text-sm text-bambu-gray",children:t.ip_address})]})]}),l.jsx("div",{className:"flex items-center gap-2",children:g?l.jsx(gt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):T?l.jsxs("div",{className:"flex items-center gap-1 text-sm",children:[l.jsx(dR,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:w?"text-bambu-green":"text-bambu-gray",children:f?.state||"Unknown"})]}):l.jsxs("div",{className:"flex items-center gap-1 text-sm text-red-400",children:[l.jsx(Mb,{className:"w-4 h-4"}),l.jsx("span",{children:"Offline"})]})})]}),v&&l.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Linked to: "}),l.jsx("span",{className:"text-sm text-white",children:v.name})]}),(t.power_alert_enabled||t.schedule_enabled)&&l.jsxs("div",{className:"flex flex-wrap gap-1.5 mb-3",children:[t.power_alert_enabled&&l.jsxs("span",{className:"flex items-center gap-1 px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded-full",children:[l.jsx(Ph,{className:"w-3 h-3"}),"Alerts"]}),t.schedule_enabled&&l.jsxs("span",{className:"flex items-center gap-1 px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded-full",children:[l.jsx(qr,{className:"w-3 h-3"}),t.schedule_on_time&&t.schedule_off_time?`${t.schedule_on_time} - ${t.schedule_off_time}`:t.schedule_on_time?`On ${t.schedule_on_time}`:`Off ${t.schedule_off_time}`]})]}),l.jsxs("div",{className:"flex gap-2 mb-3",children:[l.jsxs(ke,{size:"sm",variant:w?"primary":"secondary",disabled:!T||A,onClick:()=>s(!0),className:"flex-1",children:[A?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Cl,{className:"w-4 h-4"}),"On"]}),l.jsxs(ke,{size:"sm",variant:w?"secondary":"primary",disabled:!T||A,onClick:()=>c(!0),className:"flex-1",children:[A?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ZS,{className:"w-4 h-4"}),"Off"]})]}),l.jsxs("button",{onClick:()=>d(!u),className:"w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:[l.jsxs("span",{className:"flex items-center gap-2",children:[l.jsx(QS,{className:"w-4 h-4"}),"Automation Settings"]}),l.jsx("span",{children:u?"-":"+"})]}),u&&l.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(YS,{className:"w-4 h-4 text-bambu-green"}),l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Show in Switchbar"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.show_in_switchbar,onChange:C=>b.mutate({show_in_switchbar:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Enabled"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Enable automation for this plug"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.enabled,onChange:C=>b.mutate({enabled:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Auto On"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn on when print starts"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.auto_on,onChange:C=>b.mutate({auto_on:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Auto Off"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn off when print completes (one-shot)"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:t.auto_off,onChange:C=>b.mutate({auto_off:C.target.checked}),className:"sr-only peer"}),l.jsx("div",{className:"w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),t.auto_off&&l.jsxs("div",{className:"space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white mb-2",children:"Turn Off Delay Mode"}),l.jsxs("div",{className:"flex gap-2",children:[l.jsxs("button",{onClick:()=>b.mutate({off_delay_mode:"time"}),className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${t.off_delay_mode==="time"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:[l.jsx(tr,{className:"w-4 h-4"}),"Time"]}),l.jsxs("button",{onClick:()=>b.mutate({off_delay_mode:"temperature"}),className:`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${t.off_delay_mode==="temperature"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white"}`,children:[l.jsx(JS,{className:"w-4 h-4"}),"Temp"]})]})]}),t.off_delay_mode==="time"?l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Delay (minutes)"}),l.jsx("input",{type:"number",min:"1",max:"60",value:t.off_delay_minutes,onChange:C=>b.mutate({off_delay_minutes:parseInt(C.target.value)||5}),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"})]}):l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Temperature threshold (C)"}),l.jsx("input",{type:"number",min:"30",max:"100",value:t.off_temp_threshold,onChange:C=>b.mutate({off_temp_threshold:parseInt(C.target.value)||70}),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"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Turns off when nozzle cools below this temperature"})]})]}),l.jsxs("div",{className:"flex gap-2 pt-2",children:[l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[l.jsx(lR,{className:"w-4 h-4"}),"Edit"]}),l.jsx(ke,{size:"sm",variant:"secondary",onClick:()=>i(!0),className:"text-red-400 hover:text-red-300",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]})]})}),r&&l.jsx(Lr,{title:"Delete Smart Plug",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{E.mutate(),i(!1)},onCancel:()=>i(!1)}),a&&l.jsx(Lr,{title:"Turn On Smart Plug",message:`Are you sure you want to turn on "${t.name}"?`,confirmText:"Turn On",variant:"default",onConfirm:()=>{y.mutate("on"),s(!1)},onCancel:()=>s(!1)}),o&&l.jsx(Lr,{title:"Turn Off Smart Plug",message:`Are you sure you want to turn off "${t.name}"? This will cut power to the connected device.`,confirmText:"Turn Off",variant:"danger",onConfirm:()=>{y.mutate("off"),c(!1)},onCancel:()=>c(!1)})]})}function NGe({plug:t,onClose:e}){const n=hn(),r=!!t,[i,a]=M.useState(t?.name||""),[s,o]=M.useState(t?.ip_address||""),[c,u]=M.useState(t?.username||""),[d,f]=M.useState(t?.password||""),[g,p]=M.useState(t?.printer_id||null),[x,v]=M.useState(null),[y,b]=M.useState(null),[E,w]=M.useState(t?.power_alert_enabled||!1),[T,A]=M.useState(t?.power_alert_high?.toString()||""),[C,N]=M.useState(t?.power_alert_low?.toString()||""),[F,k]=M.useState(t?.schedule_enabled||!1),[R,I]=M.useState(t?.schedule_on_time||""),[L,q]=M.useState(t?.schedule_off_time||""),[j,V]=M.useState(t?.show_in_switchbar||!1),[K,G]=M.useState(!1),[J,ee]=M.useState({scanned:0,total:0}),[Q,B]=M.useState([]),$=M.useRef(null),{data:Z}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:te}=xt({queryKey:["smart-plugs"],queryFn:Ee.getSmartPlugs});M.useEffect(()=>{const z=H=>{H.key==="Escape"&&e()};return window.addEventListener("keydown",z),()=>{window.removeEventListener("keydown",z),$.current&&clearInterval($.current)}},[e]);const he=async()=>{G(!0),B([]),ee({scanned:0,total:0}),b(null);try{await Ee.startTasmotaScan();const z=async()=>{try{const H=await Ee.getTasmotaScanStatus();ee({scanned:H.scanned,total:H.total});const ne=await Ee.getDiscoveredTasmotaDevices();B(ne),H.running||(G(!1),$.current&&(clearInterval($.current),$.current=null))}catch(H){console.error("Polling error:",H)}};await z(),$.current=setInterval(z,500)}catch(z){G(!1);const H=z instanceof Error?z.message:typeof z=="string"?z:JSON.stringify(z);b(H||"Failed to start scan")}},de=async()=>{try{await Ee.stopTasmotaScan()}catch{}G(!1),$.current&&(clearInterval($.current),$.current=null)},ge=z=>{o(z.ip_address),a(z.name),v(null)},Ae=_t({mutationFn:()=>Ee.testSmartPlugConnection(s,c||null,d||null),onSuccess:z=>{v(z),b(null),!i&&z.device_name&&a(z.device_name)},onError:z=>{v(null),b(z.message)}}),me=_t({mutationFn:z=>Ee.createSmartPlug(z),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:z=>{b(z.message)}}),ce=_t({mutationFn:z=>Ee.updateSmartPlug(t.id,z),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:z=>{b(z.message)}}),Pe=Z?.filter(z=>!te?.some(ne=>ne.printer_id===z.id&&ne.id!==t?.id)),oe=z=>{if(z.preventDefault(),b(null),!i.trim()){b("Name is required");return}if(!s.trim()){b("IP address is required");return}const H={name:i.trim(),ip_address:s.trim(),username:c.trim()||null,password:d.trim()||null,printer_id:g,power_alert_enabled:E,power_alert_high:T?parseFloat(T):null,power_alert_low:C?parseFloat(C):null,schedule_enabled:F,schedule_on_time:R||null,schedule_off_time:L||null,show_in_switchbar:j};r?ce.mutate(H):me.mutate(H)},Le=me.isPending||ce.isPending;return l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-md max-h-[90vh] flex flex-col",onClick:z=>z.stopPropagation(),children:[l.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary flex-shrink-0",children:[l.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Smart Plug":"Add Smart Plug"}),l.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:oe,className:"p-6 space-y-4 overflow-y-auto",children:[y&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:y}),!r&&l.jsxs("div",{className:"space-y-3",children:[K?l.jsxs(ke,{type:"button",variant:"secondary",onClick:de,className:"w-full",children:[l.jsx(Vt,{className:"w-4 h-4"}),"Stop Scanning"]}):l.jsxs(ke,{type:"button",variant:"primary",onClick:he,className:"w-full",children:[l.jsx(xl,{className:"w-4 h-4"}),"Discover Tasmota Devices"]}),K&&J.total>0&&l.jsxs("div",{className:"space-y-1",children:[l.jsxs("div",{className:"flex justify-between text-xs text-bambu-gray",children:[l.jsx("span",{children:"Scanning network..."}),l.jsxs("span",{children:[J.scanned," / ",J.total]})]}),l.jsx("div",{className:"w-full bg-bambu-dark-tertiary rounded-full h-2",children:l.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${J.scanned/J.total*100}%`}})})]}),Q.length>0&&l.jsxs("div",{className:"space-y-2",children:[l.jsxs("p",{className:"text-xs text-bambu-gray",children:["Found ",Q.length," device(s) - click to select:"]}),l.jsx("div",{className:"max-h-40 overflow-y-auto space-y-1",children:Q.map(z=>l.jsxs("button",{type:"button",onClick:()=>ge(z),className:"w-full flex items-center justify-between p-2 bg-bambu-dark hover:bg-bambu-dark-tertiary rounded-lg transition-colors text-left border border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(so,{className:"w-4 h-4 text-bambu-green"}),l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:z.name}),l.jsx("p",{className:"text-xs text-bambu-gray",children:z.ip_address})]})]}),z.state&&l.jsxs("span",{className:`flex items-center gap-1 text-xs ${z.state==="ON"?"text-bambu-green":"text-bambu-gray"}`,children:[l.jsx(Cl,{className:"w-3 h-3"}),z.state]})]},z.ip_address))})]}),!K&&Q.length===0&&J.total>0&&l.jsx("p",{className:"text-xs text-bambu-gray text-center py-2",children:"No Tasmota devices found on your network"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"IP Address *"}),l.jsxs("div",{className:"flex gap-2",children:[l.jsx("input",{type:"text",value:s,onChange:z=>{o(z.target.value),v(null)},placeholder:"192.168.1.100",className:"flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"}),l.jsxs(ke,{type:"button",variant:"secondary",onClick:()=>Ae.mutate(),disabled:!s.trim()||Ae.isPending,children:[Ae.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(dR,{className:"w-4 h-4"}),"Test"]})]})]}),x&&l.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${x.success?"bg-bambu-green/20 border border-bambu-green/50 text-bambu-green":"bg-red-500/20 border border-red-500/50 text-red-400"}`,children:x.success?l.jsxs(l.Fragment,{children:[l.jsx(Mr,{className:"w-5 h-5"}),l.jsxs("div",{children:[l.jsx("p",{className:"font-medium",children:"Connected!"}),l.jsxs("p",{className:"text-sm opacity-80",children:[x.device_name&&`Device: ${x.device_name} - `,"State: ",x.state]})]})]}):l.jsxs(l.Fragment,{children:[l.jsx(Mb,{className:"w-5 h-5"}),l.jsx("span",{children:"Connection failed"})]})}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),l.jsx("input",{type:"text",value:i,onChange:z=>a(z.target.value),placeholder:"Living Room Plug",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"})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Username"}),l.jsx("input",{type:"text",value:c,onChange:z=>u(z.target.value),placeholder:"admin",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),l.jsx("input",{type:"password",value:d,onChange:z=>f(z.target.value),placeholder:"********",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"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray -mt-2",children:"Leave empty if your Tasmota device doesn't require authentication"}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Link to Printer"}),l.jsxs("select",{value:g??"",onChange:z=>p(z.target.value?Number(z.target.value):null),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:[l.jsx("option",{value:"",children:"No printer (manual control only)"}),Pe?.map(z=>l.jsx("option",{value:z.id,children:z.name},z.id))]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Linking enables automatic on/off when prints start/complete"})]}),l.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[l.jsxs("div",{className:"flex items-center justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Ph,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:"text-white font-medium",children:"Power Alerts"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:E,onChange:z=>w(z.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),E&&l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if above (W)"}),l.jsx("input",{type:"number",value:T,onChange:z=>A(z.target.value),placeholder:"e.g. 200",min:"0",max:"5000",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if below (W)"}),l.jsx("input",{type:"number",value:C,onChange:z=>N(z.target.value),placeholder:"e.g. 10",min:"0",max:"5000",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"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Get notified when power consumption crosses these thresholds. Leave empty to disable that direction."})]})]}),l.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[l.jsxs("div",{className:"flex items-center justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(tr,{className:"w-4 h-4 text-bambu-green"}),l.jsx("span",{className:"text-white font-medium",children:"Daily Schedule"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:F,onChange:z=>k(z.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),F&&l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn On at"}),l.jsx("input",{type:"time",value:R,onChange:z=>I(z.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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn Off at"}),l.jsx("input",{type:"time",value:L,onChange:z=>q(z.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"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Automatically turn the plug on/off at these times daily. Leave empty to skip that action."})]})]}),l.jsx("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(YS,{className:"w-4 h-4 text-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white font-medium",children:"Show in Switchbar"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:j,onChange:z=>V(z.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]})}),l.jsxs("div",{className:"flex gap-3 pt-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:Le,className:"flex-1",children:[Le?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Ms,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function On({checked:t,onChange:e,disabled:n}){const r=i=>{i.preventDefault(),i.stopPropagation(),n||e(!t)};return l.jsx("button",{type:"button",role:"switch","aria-checked":t,disabled:n,onClick:r,className:`relative inline-flex w-11 h-7 md:w-9 md:h-5 rounded-full transition-colors flex-shrink-0 focus:outline-none focus:ring-2 focus:ring-bambu-green focus:ring-offset-2 focus:ring-offset-bambu-dark ${n?"bg-bambu-dark-tertiary/50 cursor-not-allowed opacity-50":t?"bg-bambu-green cursor-pointer":"bg-bambu-dark-tertiary cursor-pointer hover:bg-bambu-dark-tertiary/80"}`,children:l.jsx("span",{className:`pointer-events-none absolute top-[3px] md:top-[2px] left-[3px] md:left-[2px] w-5 h-5 md:w-4 md:h-4 bg-white rounded-full shadow transition-transform duration-200 ease-in-out ${t?"translate-x-4":"translate-x-0"}`})})}const CGe={callmebot:"CallMeBot/WhatsApp",ntfy:"ntfy",pushover:"Pushover",telegram:"Telegram",email:"Email",discord:"Discord",webhook:"Webhook"};function kGe({provider:t,onEdit:e}){const n=hn(),[r,i]=M.useState(!1),[a,s]=M.useState(!1),[o,c]=M.useState(null),{data:u}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),d=u?.find(v=>v.id===t.printer_id),f=_t({mutationFn:v=>Ee.updateNotificationProvider(t.id,v),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),g=_t({mutationFn:()=>Ee.deleteNotificationProvider(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),p=_t({mutationFn:()=>Ee.testNotificationProvider(t.id),onSuccess:v=>{c(v),n.invalidateQueries({queryKey:["notification-providers"]})},onError:v=>{c({success:!1,message:v.message})}}),x=v=>v||"";return l.jsxs(l.Fragment,{children:[l.jsx(St,{className:"relative",children:l.jsxs(Tt,{className:"p-4",children:[l.jsxs("div",{className:"flex items-start justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:`p-2 rounded-lg ${t.enabled?"bg-bambu-green/20":"bg-bambu-dark"}`,children:l.jsx(Ph,{className:`w-5 h-5 ${t.enabled?"text-bambu-green":"text-bambu-gray"}`})}),l.jsxs("div",{children:[l.jsx("h3",{className:"font-medium text-white",children:t.name}),l.jsx("p",{className:"text-sm text-bambu-gray",children:CGe[t.provider_type]||t.provider_type})]})]}),l.jsxs("div",{className:"flex items-center gap-3",children:[t.last_success&&l.jsxs("span",{className:"text-xs text-bambu-green hidden sm:inline",children:["Last: ",new Date(t.last_success).toLocaleDateString()]}),t.last_error&&t.last_error_at&&(!t.last_success||new Date(t.last_error_at)>new Date(t.last_success))&&l.jsx("span",{className:"text-xs text-red-400",title:t.last_error,children:"Error"}),l.jsx(On,{checked:t.enabled,onChange:v=>f.mutate({enabled:v})})]})]}),d&&l.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Printer: "}),l.jsx("span",{className:"text-sm text-white",children:d.name})]}),!d&&!t.printer_id&&l.jsx("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:l.jsx("span",{className:"text-xs text-bambu-gray",children:"All printers"})}),l.jsxs("div",{className:"mb-3 flex flex-wrap gap-1",children:[t.on_print_start&&l.jsx("span",{className:"px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded",children:"Start"}),t.on_print_complete&&l.jsx("span",{className:"px-2 py-0.5 bg-bambu-green/20 text-bambu-green text-xs rounded",children:"Complete"}),t.on_print_failed&&l.jsx("span",{className:"px-2 py-0.5 bg-red-500/20 text-red-400 text-xs rounded",children:"Failed"}),t.on_print_stopped&&l.jsx("span",{className:"px-2 py-0.5 bg-orange-500/20 text-orange-400 text-xs rounded",children:"Stopped"}),t.on_print_progress&&l.jsx("span",{className:"px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded",children:"Progress"}),t.on_printer_offline&&l.jsx("span",{className:"px-2 py-0.5 bg-gray-500/20 text-gray-400 text-xs rounded",children:"Offline"}),t.on_printer_error&&l.jsx("span",{className:"px-2 py-0.5 bg-rose-500/20 text-rose-400 text-xs rounded",children:"Error"}),t.on_filament_low&&l.jsx("span",{className:"px-2 py-0.5 bg-cyan-500/20 text-cyan-400 text-xs rounded",children:"Low Filament"}),t.on_maintenance_due&&l.jsx("span",{className:"px-2 py-0.5 bg-purple-500/20 text-purple-400 text-xs rounded",children:"Maintenance"}),t.on_ams_humidity_high&&l.jsx("span",{className:"px-2 py-0.5 bg-blue-600/20 text-blue-300 text-xs rounded",children:"AMS Humidity"}),t.on_ams_temperature_high&&l.jsx("span",{className:"px-2 py-0.5 bg-orange-600/20 text-orange-300 text-xs rounded",children:"AMS Temp"}),t.on_ams_ht_humidity_high&&l.jsx("span",{className:"px-2 py-0.5 bg-cyan-600/20 text-cyan-300 text-xs rounded",children:"AMS-HT Humidity"}),t.on_ams_ht_temperature_high&&l.jsx("span",{className:"px-2 py-0.5 bg-amber-600/20 text-amber-300 text-xs rounded",children:"AMS-HT Temp"}),t.quiet_hours_enabled&&l.jsxs("span",{className:"px-2 py-0.5 bg-indigo-500/20 text-indigo-400 text-xs rounded flex items-center gap-1",children:[l.jsx(F1,{className:"w-3 h-3"}),"Quiet"]}),t.daily_digest_enabled&&l.jsxs("span",{className:"px-2 py-0.5 bg-emerald-500/20 text-emerald-400 text-xs rounded flex items-center gap-1",children:[l.jsx(qr,{className:"w-3 h-3"}),"Digest ",t.daily_digest_time]})]}),l.jsx("div",{className:"mb-3",children:l.jsxs(ke,{size:"sm",variant:"secondary",disabled:p.isPending,onClick:()=>{c(null),p.mutate()},className:"w-full",children:[p.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cR,{className:"w-4 h-4"}),"Send Test Notification"]})}),o&&l.jsxs("div",{className:`mb-3 p-2 rounded-lg flex items-center gap-2 text-sm ${o.success?"bg-bambu-green/20 text-bambu-green":"bg-red-500/20 text-red-400"}`,children:[o.success?l.jsx(Mr,{className:"w-4 h-4"}):l.jsx(Ca,{className:"w-4 h-4"}),l.jsx("span",{children:o.message})]}),l.jsxs("button",{onClick:()=>s(!a),className:"w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors border-t border-bambu-dark-tertiary",children:[l.jsxs("span",{className:"flex items-center gap-2",children:[l.jsx(QS,{className:"w-4 h-4"}),"Event Settings"]}),a?l.jsx(_b,{className:"w-4 h-4"}):l.jsx(Nl,{className:"w-4 h-4"})]}),a&&l.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Enabled"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Send notifications from this provider"})]}),l.jsx(On,{checked:t.enabled,onChange:v=>f.mutate({enabled:v})})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Print Events"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Started"}),l.jsx(On,{checked:t.on_print_start,onChange:v=>f.mutate({on_print_start:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Completed"}),l.jsx(On,{checked:t.on_print_complete,onChange:v=>f.mutate({on_print_complete:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Failed"}),l.jsx(On,{checked:t.on_print_failed,onChange:v=>f.mutate({on_print_failed:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Print Stopped"}),l.jsx(On,{checked:t.on_print_stopped,onChange:v=>f.mutate({on_print_stopped:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Progress Milestones"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify at 25%, 50%, 75%"})]}),l.jsx(On,{checked:t.on_print_progress,onChange:v=>f.mutate({on_print_progress:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Printer Status"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Printer Offline"}),l.jsx(On,{checked:t.on_printer_offline,onChange:v=>f.mutate({on_printer_offline:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Printer Error"}),l.jsx(On,{checked:t.on_printer_error,onChange:v=>f.mutate({on_printer_error:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("p",{className:"text-sm text-white",children:"Low Filament"}),l.jsx(On,{checked:t.on_filament_low,onChange:v=>f.mutate({on_filament_low:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Maintenance Due"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify when maintenance is needed"})]}),l.jsx(On,{checked:t.on_maintenance_due??!1,onChange:v=>f.mutate({on_maintenance_due:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS Alarms"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS Humidity High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS humidity exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_humidity_high??!1,onChange:v=>f.mutate({on_ams_humidity_high:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS Temperature High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS temperature exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_temperature_high??!1,onChange:v=>f.mutate({on_ams_temperature_high:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS-HT Alarms"}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS-HT Humidity High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT humidity exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_ht_humidity_high??!1,onChange:v=>f.mutate({on_ams_ht_humidity_high:v})})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"AMS-HT Temperature High"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT temperature exceeds threshold"})]}),l.jsx(On,{checked:t.on_ams_ht_temperature_high??!1,onChange:v=>f.mutate({on_ams_ht_temperature_high:v})})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(F1,{className:"w-4 h-4 text-purple-400"}),l.jsx("p",{className:"text-sm text-white",children:"Quiet Hours"})]}),l.jsx(On,{checked:t.quiet_hours_enabled,onChange:v=>f.mutate({quiet_hours_enabled:v})})]}),t.quiet_hours_enabled&&l.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"No notifications during these hours"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(tr,{className:"w-4 h-4 text-bambu-gray"}),l.jsxs("span",{className:"text-sm text-white",children:[x(t.quiet_hours_start)||"22:00"," - ",x(t.quiet_hours_end)||"07:00"]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change quiet hours"})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(qr,{className:"w-4 h-4 text-emerald-400"}),l.jsx("p",{className:"text-sm text-white",children:"Daily Digest"})]}),l.jsx(On,{checked:t.daily_digest_enabled,onChange:v=>f.mutate({daily_digest_enabled:v})})]}),t.daily_digest_enabled&&l.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[l.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(tr,{className:"w-4 h-4 text-bambu-gray"}),l.jsxs("span",{className:"text-sm text-white",children:["Send at ",x(t.daily_digest_time)||"08:00"]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change digest time"})]})]}),l.jsxs("div",{className:"flex gap-2 pt-2",children:[l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[l.jsx(lR,{className:"w-4 h-4"}),"Edit"]}),l.jsx(ke,{size:"sm",variant:"secondary",onClick:()=>i(!0),className:"text-red-400 hover:text-red-300",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]})]})}),r&&l.jsx(Lr,{title:"Delete Notification Provider",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{g.mutate(),i(!1)},onCancel:()=>i(!1)})]})}const R9=[{value:"email",label:"Email",description:"SMTP email notifications"},{value:"telegram",label:"Telegram",description:"Notifications via Telegram bot"},{value:"discord",label:"Discord",description:"Send to Discord channel via webhook"},{value:"ntfy",label:"ntfy",description:"Free, self-hostable push notifications"},{value:"pushover",label:"Pushover",description:"Simple, reliable push notifications"},{value:"callmebot",label:"CallMeBot/WhatsApp",description:"Free WhatsApp notifications via CallMeBot"},{value:"webhook",label:"Webhook",description:"Generic HTTP POST to any URL"}];function PGe({provider:t,onClose:e}){const n=hn(),r=!!t,[i,a]=M.useState(t?.name||""),[s,o]=M.useState(t?.provider_type||"email"),[c,u]=M.useState(t?.printer_id||null),[d,f]=M.useState(t?.quiet_hours_enabled||!1),[g,p]=M.useState(t?.quiet_hours_start||"22:00"),[x,v]=M.useState(t?.quiet_hours_end||"07:00"),[y,b]=M.useState(t?.daily_digest_enabled||!1),[E,w]=M.useState(t?.daily_digest_time||"08:00"),[T,A]=M.useState(t?.on_print_start??!1),[C,N]=M.useState(t?.on_print_complete??!0),[F,k]=M.useState(t?.on_print_failed??!0),[R,I]=M.useState(t?.on_print_stopped??!0),[L,q]=M.useState(t?.on_print_progress??!1),[j,V]=M.useState(t?.on_printer_offline??!1),[K,G]=M.useState(t?.on_printer_error??!1),[J,ee]=M.useState(t?.on_filament_low??!1),[Q,B]=M.useState(t?.on_maintenance_due??!1),[$,Z]=M.useState(t?.config?Object.fromEntries(Object.entries(t.config).map(([W,Y])=>[W,String(Y)])):{}),[te,he]=M.useState(null),[de,ge]=M.useState(null),{data:Ae}=xt({queryKey:["printers"],queryFn:Ee.getPrinters});M.useEffect(()=>{const W=Y=>{Y.key==="Escape"&&e()};return window.addEventListener("keydown",W),()=>window.removeEventListener("keydown",W)},[e]);const me=_t({mutationFn:()=>Ee.testNotificationConfig({provider_type:s,config:$}),onSuccess:W=>{he(W),ge(null)},onError:W=>{he({success:!1,message:W.message})}}),ce=_t({mutationFn:W=>Ee.createNotificationProvider(W),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:W=>{ge(W.message)}}),Pe=_t({mutationFn:W=>Ee.updateNotificationProvider(t.id,W),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:W=>{ge(W.message)}}),oe=W=>{if(W.preventDefault(),ge(null),!i.trim()){ge("Name is required");return}const Y=H(s);for(const be of Y)if(!$[be.key]?.trim()){ge(`${be.label} is required`);return}const se={name:i.trim(),provider_type:s,config:$,printer_id:c,quiet_hours_enabled:d,quiet_hours_start:d?g:null,quiet_hours_end:d?x:null,daily_digest_enabled:y,daily_digest_time:y?E:null,on_print_start:T,on_print_complete:C,on_print_failed:F,on_print_stopped:R,on_print_progress:L,on_printer_offline:j,on_printer_error:K,on_filament_low:J,on_maintenance_due:Q};r?Pe.mutate(se):ce.mutate(se)},Le=ce.isPending||Pe.isPending,z=W=>{switch(W){case"callmebot":return[{key:"phone",label:"Phone Number",placeholder:"+1234567890",type:"text",required:!0},{key:"apikey",label:"API Key",placeholder:"Your CallMeBot API key",type:"text",required:!0}];case"ntfy":return[{key:"server",label:"Server URL",placeholder:"https://ntfy.sh",type:"text",required:!1},{key:"topic",label:"Topic",placeholder:"my-bambuddy",type:"text",required:!0},{key:"auth_token",label:"Auth Token",placeholder:"Optional authentication",type:"password",required:!1}];case"pushover":return[{key:"user_key",label:"User Key",placeholder:"Your Pushover user key",type:"text",required:!0},{key:"app_token",label:"App Token",placeholder:"Your Pushover app token",type:"text",required:!0},{key:"priority",label:"Priority",placeholder:"0 (normal)",type:"number",required:!1}];case"telegram":return[{key:"bot_token",label:"Bot Token",placeholder:"Bot token from @BotFather",type:"password",required:!0},{key:"chat_id",label:"Chat ID",placeholder:"Your chat or group ID",type:"text",required:!0}];case"email":return[{key:"smtp_server",label:"SMTP Server",placeholder:"smtp.gmail.com",type:"text",required:!0},{key:"smtp_port",label:"SMTP Port",placeholder:"587",type:"number",required:!1},{key:"security",label:"Security",type:"select",required:!1,options:[{value:"starttls",label:"STARTTLS (Port 587)"},{value:"ssl",label:"SSL/TLS (Port 465)"},{value:"none",label:"None (Port 25)"}]},{key:"auth_enabled",label:"Authentication",type:"select",required:!1,options:[{value:"true",label:"Enabled"},{value:"false",label:"Disabled"}]},{key:"username",label:"Username",placeholder:"your@email.com",type:"text",required:!1},{key:"password",label:"Password",placeholder:"App password",type:"password",required:!1},{key:"from_email",label:"From Email",placeholder:"your@email.com",type:"text",required:!0},{key:"to_email",label:"To Email",placeholder:"recipient@email.com",type:"text",required:!0}];case"discord":return[{key:"webhook_url",label:"Webhook URL",placeholder:"https://discord.com/api/webhooks/...",type:"text",required:!0}];case"webhook":return[{key:"webhook_url",label:"Webhook URL",placeholder:"https://example.com/webhook",type:"text",required:!0},{key:"auth_header",label:"Authorization",placeholder:"Bearer token (optional)",type:"password",required:!1},{key:"field_title",label:"Title Field Name",placeholder:"title",type:"text",required:!1},{key:"field_message",label:"Message Field Name",placeholder:"message",type:"text",required:!1}];default:return[]}},H=W=>z(W).filter(Y=>Y.required),ne=z(s);return l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4 overflow-y-auto",onClick:e,children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-lg my-8 max-h-[90vh] overflow-y-auto",onClick:W=>W.stopPropagation(),children:[l.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[l.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Notification Provider":"Add Notification Provider"}),l.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:oe,className:"p-6 space-y-4",children:[de&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:de}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),l.jsx("input",{type:"text",value:i,onChange:W=>a(W.target.value),placeholder:"My Notifications",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Provider Type *"}),l.jsx("select",{value:s,onChange:W=>{o(W.target.value),Z({}),he(null)},disabled:r,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 disabled:opacity-50",children:R9.map(W=>l.jsx("option",{value:W.value,children:W.label},W.value))}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:R9.find(W=>W.value===s)?.description})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Configuration"}),ne.map(W=>l.jsxs("div",{children:[l.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[W.label," ",W.required&&"*"]}),W.type==="select"&&W.options?l.jsx("select",{value:$[W.key]||W.options[0]?.value||"",onChange:Y=>{Z({...$,[W.key]:Y.target.value}),he(null)},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:W.options.map(Y=>l.jsx("option",{value:Y.value,children:Y.label},Y.value))}):l.jsx("input",{type:W.type,value:$[W.key]||"",onChange:Y=>{Z({...$,[W.key]:Y.target.value}),he(null)},placeholder:W.placeholder,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"})]},W.key))]}),l.jsx("div",{className:"flex gap-2",children:l.jsxs(ke,{type:"button",variant:"secondary",onClick:()=>{he(null),me.mutate()},disabled:me.isPending||!$[H(s)[0]?.key],className:"flex-1",children:[me.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cR,{className:"w-4 h-4"}),"Test Configuration"]})}),te&&l.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${te.success?"bg-bambu-green/20 border border-bambu-green/50 text-bambu-green":"bg-red-500/20 border border-red-500/50 text-red-400"}`,children:te.success?l.jsxs(l.Fragment,{children:[l.jsx(Mr,{className:"w-5 h-5"}),l.jsx("span",{children:te.message})]}):l.jsxs(l.Fragment,{children:[l.jsx(Ca,{className:"w-5 h-5"}),l.jsx("span",{children:te.message})]})}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer Filter"}),l.jsxs("select",{value:c??"",onChange:W=>u(W.target.value?Number(W.target.value):null),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:[l.jsx("option",{value:"",children:"All printers"}),Ae?.map(W=>l.jsx("option",{value:W.id,children:W.name},W.id))]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Only send notifications for events from this printer"})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("label",{className:"text-sm text-white",children:"Quiet Hours (Do Not Disturb)"}),l.jsx(On,{checked:d,onChange:f})]}),d&&l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Start"}),l.jsx("input",{type:"time",value:g,onChange:W=>p(W.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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"End"}),l.jsx("input",{type:"time",value:x,onChange:W=>v(W.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"})]})]})]}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("label",{className:"text-sm text-white",children:"Daily Digest"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"})]}),l.jsx(On,{checked:y,onChange:b})]}),y&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Send digest at"}),l.jsx("input",{type:"time",value:E,onChange:W=>w(W.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"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Events will be collected and sent as a single summary at this time"})]})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Notification Events"}),l.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Print Events"}),l.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Start"}),l.jsx(On,{checked:T,onChange:A})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Complete"}),l.jsx(On,{checked:C,onChange:N})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Failed"}),l.jsx(On,{checked:F,onChange:k})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Stopped"}),l.jsx(On,{checked:R,onChange:I})]}),l.jsxs("div",{className:"flex items-center justify-between col-span-2",children:[l.jsxs("div",{children:[l.jsx("span",{className:"text-sm text-white",children:"Progress"}),l.jsx("span",{className:"text-xs text-bambu-gray ml-1",children:"(25%, 50%, 75%)"})]}),l.jsx(On,{checked:L,onChange:q})]})]})]}),l.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[l.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Printer Status"}),l.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Offline"}),l.jsx(On,{checked:j,onChange:V})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Error"}),l.jsx(On,{checked:K,onChange:G})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Low Filament"}),l.jsx(On,{checked:J,onChange:ee})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsx("span",{className:"text-sm text-white",children:"Maintenance"}),l.jsx(On,{checked:Q,onChange:B})]})]})]})]}),l.jsxs("div",{className:"flex gap-3 pt-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:Le,className:"flex-1",children:[Le?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Ms,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function jGe({template:t,onClose:e}){const n=hn(),r=M.useRef(null),[i,a]=M.useState(t.title_template),[s,o]=M.useState(t.body_template),[c,u]=M.useState(null),[d,f]=M.useState(!0),{data:g}=xt({queryKey:["template-variables"],queryFn:Ee.getTemplateVariables}),p=g?.find(A=>A.event_type===t.event_type),{data:x,isLoading:v}=xt({queryKey:["template-preview",t.event_type,i,s],queryFn:()=>Ee.previewTemplate({event_type:t.event_type,title_template:i,body_template:s}),enabled:d&&i.length>0&&s.length>0});M.useEffect(()=>{const A=C=>{C.key==="Escape"&&e()};return window.addEventListener("keydown",A),()=>window.removeEventListener("keydown",A)},[e]);const y=_t({mutationFn:A=>Ee.updateNotificationTemplate(t.id,A),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-templates"]}),e()},onError:A=>{u(A.message)}}),b=_t({mutationFn:()=>Ee.resetNotificationTemplate(t.id),onSuccess:A=>{a(A.title_template),o(A.body_template),n.invalidateQueries({queryKey:["notification-templates"]})},onError:A=>{u(A.message)}}),E=A=>{if(A.preventDefault(),u(null),!i.trim()){u("Title is required");return}if(!s.trim()){u("Body is required");return}y.mutate({title_template:i,body_template:s})},w=A=>{const C=r.current;if(!C)return;const N=C.selectionStart,F=C.selectionEnd,k=s,R=k.substring(0,N),I=k.substring(F),L=R+`{${A}}`+I;o(L),setTimeout(()=>{C.focus();const q=N+A.length+2;C.setSelectionRange(q,q)},0)},T=i!==t.title_template||s!==t.body_template;return l.jsx("div",{className:"fixed inset-0 bg-black/80 flex items-center justify-center z-50 p-4",children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-2xl max-h-[90vh] flex flex-col",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary shrink-0",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Edit Template: ",t.name]}),l.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",children:l.jsx(Vt,{className:"w-5 h-5 text-bambu-gray"})})]}),l.jsxs("form",{onSubmit:E,className:"flex-1 overflow-y-auto p-4 space-y-4",children:[c&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded text-red-400 text-sm",children:c}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Title"}),l.jsx("input",{type:"text",value:i,onChange:A=>a(A.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white focus:outline-none focus:ring-1 focus:ring-bambu-green",placeholder:"Notification title..."})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Body"}),l.jsx("textarea",{ref:r,value:s,onChange:A=>o(A.target.value),rows:4,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white focus:outline-none focus:ring-1 focus:ring-bambu-green font-mono text-sm resize-none",placeholder:"Notification body..."})]}),p&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-2",children:"Available Variables"}),l.jsx("div",{className:"flex flex-wrap gap-2",children:p.variables.map(A=>l.jsxs("button",{type:"button",onClick:()=>w(A),className:"inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark hover:bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-xs text-bambu-gray hover:text-white transition-colors",children:[l.jsx(ir,{className:"w-3 h-3"}),A]},A))}),l.jsx("p",{className:"text-xs text-bambu-gray/60 mt-1",children:"Click to insert at cursor position in body"})]}),l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsxs("label",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[l.jsx(Yh,{className:"w-4 h-4"}),"Live Preview"]}),l.jsx("button",{type:"button",onClick:()=>f(!d),className:"text-xs text-bambu-green hover:text-bambu-green-light",children:d?"Hide":"Show"})]}),d&&l.jsx("div",{className:"bg-bambu-dark border border-bambu-dark-tertiary rounded p-3 space-y-2",children:v?l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-sm",children:[l.jsx(gt,{className:"w-4 h-4 animate-spin"}),"Loading preview..."]}):x?l.jsxs(l.Fragment,{children:[l.jsxs("div",{children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Title:"}),l.jsx("div",{className:"text-white font-medium",children:x.title})]}),l.jsxs("div",{children:[l.jsx("span",{className:"text-xs text-bambu-gray",children:"Body:"}),l.jsx("div",{className:"text-white whitespace-pre-wrap text-sm",children:x.body})]})]}):l.jsx("div",{className:"text-bambu-gray text-sm",children:"Enter template content to see preview"})})]})]}),l.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary shrink-0",children:[l.jsxs(ke,{type:"button",variant:"ghost",onClick:()=>b.mutate(),disabled:b.isPending,className:"text-orange-400 hover:text-orange-300",children:[b.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin mr-2"}):l.jsx(ng,{className:"w-4 h-4 mr-2"}),"Reset to Default"]}),l.jsxs("div",{className:"flex gap-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),l.jsxs(ke,{onClick:E,disabled:y.isPending||!T,children:[y.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin mr-2"}):l.jsx(Ms,{className:"w-4 h-4 mr-2"}),"Save"]})]})]})]})})}const RGe={print_start:"Print Started",print_complete:"Print Complete",print_failed:"Print Failed",print_stopped:"Print Stopped",print_progress:"Progress",printer_offline:"Printer Offline",printer_error:"Printer Error",filament_low:"Low Filament",maintenance_due:"Maintenance Due",test:"Test"},DGe={print_start:"text-blue-400",print_complete:"text-bambu-green",print_failed:"text-red-400",print_stopped:"text-orange-400",print_progress:"text-yellow-400",printer_offline:"text-gray-400",printer_error:"text-rose-400",filament_low:"text-cyan-400",maintenance_due:"text-purple-400",test:"text-bambu-gray"};function OGe({onClose:t}){const e=hn(),{showToast:n}=vr(),[r,i]=M.useState(7),[a,s]=M.useState(null),[o,c]=M.useState(!1),{data:u,isLoading:d,refetch:f,isRefetching:g}=xt({queryKey:["notification-logs",r,o],queryFn:()=>Ee.getNotificationLogs({days:r,limit:100,success:o?!1:void 0})}),{data:p}=xt({queryKey:["notification-log-stats",r],queryFn:()=>Ee.getNotificationLogStats(r)}),x=_t({mutationFn:()=>Ee.clearNotificationLogs(30),onSuccess:y=>{n(y.message,"success"),e.invalidateQueries({queryKey:["notification-logs"]}),e.invalidateQueries({queryKey:["notification-log-stats"]})},onError:y=>{n(`Failed to clear logs: ${y.message}`,"error")}}),v=y=>{const b=new Date(y),w=new Date().getTime()-b.getTime();return w<6e4?"Just now":w<36e5?`${Math.floor(w/6e4)}m ago`:w<864e5?`${Math.floor(w/36e5)}h ago`:b.toLocaleDateString()+" "+b.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})};return l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:l.jsxs("div",{className:"bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg w-full max-w-3xl max-h-[85vh] flex flex-col",children:[l.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx(I1,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Notification Log"})]}),l.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white transition-colors",children:"×"})]}),p&&l.jsx("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/50",children:l.jsxs("div",{className:"flex items-center gap-6 text-sm",children:[l.jsxs("span",{className:"text-bambu-gray",children:["Last ",r," days: ",l.jsx("span",{className:"text-white font-medium",children:p.total})," notifications"]}),l.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[l.jsx(Mr,{className:"w-4 h-4"}),p.success_count," sent"]}),p.failure_count>0&&l.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[l.jsx(Ca,{className:"w-4 h-4"}),p.failure_count," failed"]})]})}),l.jsxs("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary flex items-center gap-4",children:[l.jsxs("select",{value:r,onChange:y=>i(Number(y.target.value)),className:"px-3 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:ring-1 focus:ring-bambu-green",children:[l.jsx("option",{value:1,children:"Last 24 hours"}),l.jsx("option",{value:7,children:"Last 7 days"}),l.jsx("option",{value:30,children:"Last 30 days"}),l.jsx("option",{value:90,children:"Last 90 days"})]}),l.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:o,onChange:y=>c(y.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),"Show failed only"]}),l.jsx("div",{className:"flex-1"}),l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>f(),disabled:g,children:[g?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ya,{className:"w-4 h-4"}),"Refresh"]}),l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>x.mutate(),disabled:x.isPending,className:"text-red-400 hover:text-red-300",children:[x.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"}),"Clear Old"]})]}),l.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:d?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):u&&u.length>0?l.jsx("div",{className:"space-y-2",children:u.map(y=>l.jsx(LGe,{log:y,isExpanded:a===y.id,onToggle:()=>s(a===y.id?null:y.id),formatDate:v},y.id))}):l.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[l.jsx(I1,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),l.jsx("p",{className:"text-sm",children:o?"No failed notifications":"No notifications logged"})]})})]})})}function LGe({log:t,isExpanded:e,onToggle:n,formatDate:r}){return l.jsxs("div",{className:`border rounded-lg overflow-hidden transition-colors ${t.success?"border-bambu-dark-tertiary bg-bambu-dark/30":"border-red-500/30 bg-red-500/5"}`,children:[l.jsxs("button",{className:"w-full px-3 py-2 flex items-center gap-3 text-left hover:bg-bambu-dark/50 transition-colors",onClick:n,children:[t.success?l.jsx(Mr,{className:"w-4 h-4 text-bambu-green shrink-0"}):l.jsx(Ca,{className:"w-4 h-4 text-red-400 shrink-0"}),l.jsx("span",{className:`text-xs font-medium ${DGe[t.event_type]||"text-bambu-gray"}`,children:RGe[t.event_type]||t.event_type}),l.jsx("span",{className:"text-sm text-white truncate flex-1",children:t.provider_name||"Unknown Provider"}),t.printer_name&&l.jsx("span",{className:"text-xs text-bambu-gray",children:t.printer_name}),l.jsx("span",{className:"text-xs text-bambu-gray shrink-0",children:r(t.created_at)}),e?l.jsx(_b,{className:"w-4 h-4 text-bambu-gray shrink-0"}):l.jsx(Nl,{className:"w-4 h-4 text-bambu-gray shrink-0"})]}),e&&l.jsxs("div",{className:"px-3 py-2 border-t border-bambu-dark-tertiary bg-bambu-dark/20 space-y-2",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Title"}),l.jsx("p",{className:"text-sm text-white",children:t.title})]}),l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Message"}),l.jsx("p",{className:"text-sm text-white whitespace-pre-wrap",children:t.message})]}),!t.success&&t.error_message&&l.jsxs("div",{children:[l.jsx("p",{className:"text-xs text-red-400 mb-1",children:"Error"}),l.jsx("p",{className:"text-sm text-red-300",children:t.error_message})]}),l.jsxs("div",{className:"flex gap-4 text-xs text-bambu-gray pt-1",children:[l.jsxs("span",{children:["Provider: ",t.provider_type]}),l.jsxs("span",{children:["Time: ",new Date(t.created_at).toLocaleString()]})]})]})]})}const r1=[{id:"settings",labelKey:"backup.categories.settings",defaultLabel:"App Settings",icon:l.jsx(uR,{className:"w-4 h-4"}),default:!0,description:"Language, theme, update preferences"},{id:"notifications",labelKey:"backup.categories.notifications",defaultLabel:"Notification Providers",icon:l.jsx(Ph,{className:"w-4 h-4"}),default:!0,description:"ntfy, Pushover, Discord, etc."},{id:"templates",labelKey:"backup.categories.templates",defaultLabel:"Notification Templates",icon:l.jsx(Ss,{className:"w-4 h-4"}),default:!0,description:"Custom message templates"},{id:"smart_plugs",labelKey:"backup.categories.smartPlugs",defaultLabel:"Smart Plugs",icon:l.jsx(so,{className:"w-4 h-4"}),default:!0,description:"Tasmota plug configurations"},{id:"external_links",labelKey:"backup.categories.externalLinks",defaultLabel:"External Links",icon:l.jsx(Sb,{className:"w-4 h-4"}),default:!0,description:"Sidebar links to external services"},{id:"printers",labelKey:"backup.categories.printers",defaultLabel:"Printers",icon:l.jsx(Ui,{className:"w-4 h-4"}),default:!1,description:"Printer info (access codes excluded)"},{id:"filaments",labelKey:"backup.categories.filaments",defaultLabel:"Filament Inventory",icon:l.jsx(AG,{className:"w-4 h-4"}),default:!1,description:"Filament types and costs"},{id:"maintenance",labelKey:"backup.categories.maintenance",defaultLabel:"Maintenance Types",icon:l.jsx(gd,{className:"w-4 h-4"}),default:!1,description:"Custom maintenance schedules"},{id:"archives",labelKey:"backup.categories.archives",defaultLabel:"Print Archives",icon:l.jsx(ao,{className:"w-4 h-4"}),default:!1,description:"All print data + files (3MF, thumbnails, photos)"},{id:"projects",labelKey:"backup.categories.projects",defaultLabel:"Projects",icon:l.jsx(Fo,{className:"w-4 h-4"}),default:!1,description:"Projects, BOM items, and attachments"},{id:"pending_uploads",labelKey:"backup.categories.pendingUploads",defaultLabel:"Pending Uploads",icon:l.jsx(ia,{className:"w-4 h-4"}),default:!1,description:"Virtual printer uploads awaiting review"}];function IGe({onClose:t,onExport:e}){const{t:n}=Qp(),[r,i]=M.useState(()=>{const x={};return r1.forEach(v=>{x[v.id]=v.default}),x}),[a,s]=M.useState(!1),[o,c]=M.useState(!1);M.useEffect(()=>{const x=v=>{v.key==="Escape"&&t()};return window.addEventListener("keydown",x),()=>window.removeEventListener("keydown",x)},[t]);const u=x=>{i(v=>({...v,[x]:!v[x]}))},d=()=>{const x={};r1.forEach(v=>{x[v.id]=!0}),i(x)},f=()=>{const x={};r1.forEach(v=>{x[v.id]=!1}),i(x)},g=Object.values(r).filter(Boolean).length,p=async()=>{c(!0);try{await e({...r,access_codes:a&&r.printers})}finally{c(!1)}};return l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:o?void 0:t,children:l.jsx(St,{className:"w-full max-w-lg",onClick:x=>x.stopPropagation(),children:l.jsxs(Tt,{className:"p-0",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:l.jsx(ns,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:n("backup.exportTitle",{defaultValue:"Export Backup"})}),l.jsx("p",{className:"text-sm text-bambu-gray",children:n("backup.selectCategories",{defaultValue:"Select data to include"})})]})]}),l.jsx("button",{onClick:t,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("div",{className:"flex gap-2 px-4 pt-4",children:[l.jsx("button",{onClick:d,disabled:o,className:"text-sm text-bambu-green hover:text-bambu-green/80 disabled:opacity-50 disabled:cursor-not-allowed",children:n("common.selectAll",{defaultValue:"Select All"})}),l.jsx("span",{className:"text-bambu-gray",children:"|"}),l.jsx("button",{onClick:f,disabled:o,className:"text-sm text-bambu-gray hover:text-white disabled:opacity-50 disabled:cursor-not-allowed",children:n("common.selectNone",{defaultValue:"Select None"})})]}),l.jsx("div",{className:`p-4 space-y-2 max-h-[400px] overflow-y-auto ${o?"opacity-50 pointer-events-none":""}`,children:r1.map(x=>l.jsxs("label",{className:`flex items-center gap-3 p-3 rounded-lg cursor-pointer transition-colors ${r[x.id]?"bg-bambu-green/10 border border-bambu-green/30":"bg-bambu-dark hover:bg-bambu-dark-tertiary border border-transparent"}`,children:[l.jsx("input",{type:"checkbox",checked:r[x.id],onChange:()=>u(x.id),disabled:o,className:"w-4 h-4 rounded border-bambu-gray bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0"}),l.jsx("div",{className:`${r[x.id]?"text-bambu-green":"text-bambu-gray"}`,children:x.icon}),l.jsxs("div",{className:"flex-1",children:[l.jsx("div",{className:"text-white text-sm font-medium",children:n(x.labelKey,{defaultValue:x.defaultLabel})}),l.jsx("div",{className:"text-xs text-bambu-gray",children:x.description})]})]},x.id))}),r.archives&&l.jsx("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[l.jsx(ao,{className:"w-4 h-4 text-yellow-500 mt-0.5 flex-shrink-0"}),l.jsxs("div",{className:"text-yellow-200 dark:text-yellow-200 text-yellow-700",children:[l.jsx("span",{className:"font-medium",children:"ZIP file will be created."}),l.jsx("span",{className:"text-yellow-600 dark:text-yellow-200/70",children:" Includes all 3MF files, thumbnails, timelapses, and photos. This may take a while and result in a large file."})]})]})}),r.printers&&l.jsxs("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-start gap-2",children:[l.jsx(Qm,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),l.jsxs("div",{children:[l.jsx("p",{className:"text-sm font-medium text-white",children:"Include Access Codes"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"For transferring to another machine"})]})]}),l.jsx(On,{checked:a,onChange:s})]}),a&&l.jsx("div",{className:"mt-2 p-2 rounded bg-orange-500/10 border border-orange-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-xs",children:[l.jsx($n,{className:"w-3 h-3 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),l.jsx("span",{className:"text-orange-700 dark:text-orange-200",children:"Access codes will be included in plain text. Keep this backup file secure!"})]})})]}),l.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary",children:[l.jsx("span",{className:"text-sm text-bambu-gray",children:n("backup.selectedCount",{count:g,defaultValue:`${g} categories selected`})}),l.jsxs("div",{className:"flex gap-3",children:[l.jsx(ke,{variant:"secondary",onClick:t,disabled:o,children:n("common.cancel",{defaultValue:"Cancel"})}),l.jsx(ke,{onClick:p,disabled:g===0||o,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50 disabled:cursor-not-allowed min-w-[100px]",children:o?l.jsxs(l.Fragment,{children:[l.jsx(gt,{className:"w-4 h-4 mr-2 animate-spin"}),n("backup.exporting",{defaultValue:"Exporting..."})]}):l.jsxs(l.Fragment,{children:[l.jsx(ns,{className:"w-4 h-4 mr-2"}),n("backup.export",{defaultValue:"Export"})]})})]})]})]})})})}const D9={settings:"Settings",notification_providers:"Notification Providers",notification_templates:"Notification Templates",smart_plugs:"Smart Plugs",printers:"Printers",filaments:"Filaments",maintenance_types:"Maintenance Types",archives:"Archives",projects:"Projects",pending_uploads:"Pending Uploads",external_links:"External Links"};function FGe({onClose:t,onRestore:e,onSuccess:n}){const[r,i]=M.useState("options"),[a,s]=M.useState(!1),[o,c]=M.useState(null),[u,d]=M.useState(null),[f,g]=M.useState(new Set),p=M.useRef(null);M.useEffect(()=>{const w=T=>{T.key==="Escape"&&r!=="restoring"&&t()};return window.addEventListener("keydown",w),()=>window.removeEventListener("keydown",w)},[t,r]);const x=w=>{const T=w.target.files?.[0];T&&c(T)},v=async()=>{if(o){i("restoring");try{const w=await e(o,a);d(w),i("result"),w.success&&n()}catch{d({success:!1,message:"Failed to restore backup. Please check the file format."}),i("result")}}},y=w=>{g(T=>{const A=new Set(T);return A.has(w)?A.delete(w):A.add(w),A})},b=u?.restored?Object.values(u.restored).reduce((w,T)=>w+T,0)+(u.files_restored||0):0,E=u?.total_skipped||0;return l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onMouseDown:w=>{w.target===w.currentTarget&&r!=="restoring"&&t()},children:l.jsx(St,{className:"w-full max-w-lg",children:l.jsxs(Tt,{className:"p-0",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:`p-2 rounded-full ${r==="result"&&u?.success?"bg-bambu-green/20 text-bambu-green":r==="result"&&!u?.success?"bg-red-500/20 text-red-500":"bg-blue-500/20 text-blue-500"}`,children:r==="result"&&u?.success?l.jsx(Mr,{className:"w-5 h-5"}):r==="result"&&!u?.success?l.jsx($n,{className:"w-5 h-5"}):l.jsx(ia,{className:"w-5 h-5"})}),l.jsxs("div",{children:[l.jsxs("h3",{className:"text-lg font-semibold text-white",children:[r==="options"&&"Restore Backup",r==="restoring"&&"Restoring...",r==="result"&&(u?.success?"Restore Complete":"Restore Failed")]}),l.jsxs("p",{className:"text-sm text-bambu-gray",children:[r==="options"&&"Import settings from a backup file",r==="restoring"&&"Please wait while your data is being restored",r==="result"&&u?.message]})]})]}),r!=="restoring"&&l.jsx("button",{onClick:t,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),r==="options"&&l.jsxs(l.Fragment,{children:[l.jsxs("div",{className:"p-4 space-y-4",children:[l.jsxs("div",{children:[l.jsx("input",{ref:p,type:"file",accept:".json,.zip",className:"hidden",onChange:x}),l.jsx("button",{type:"button",onClick:()=>p.current?.click(),className:`w-full p-4 border-2 border-dashed rounded-lg transition-colors ${o?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:o?l.jsxs("div",{className:"flex items-center justify-center gap-2 text-bambu-green",children:[l.jsx(Mr,{className:"w-5 h-5"}),l.jsx("span",{className:"font-medium",children:o.name})]}):l.jsxs("div",{className:"flex flex-col items-center gap-2 text-bambu-gray",children:[l.jsx(ia,{className:"w-8 h-8"}),l.jsx("span",{children:"Click to select backup file (.json or .zip)"})]})})]}),l.jsx("div",{className:"p-3 rounded-lg bg-blue-500/10 border border-blue-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[l.jsx($n,{className:"w-4 h-4 text-blue-500 dark:text-blue-400 mt-0.5 flex-shrink-0"}),l.jsxs("div",{className:"text-blue-700 dark:text-blue-200",children:[l.jsx("p",{className:"font-medium mb-1",children:"How duplicate handling works:"}),l.jsxs("ul",{className:"text-blue-600 dark:text-blue-200/80 space-y-1 text-xs",children:[l.jsxs("li",{children:[l.jsx("strong",{children:"Printers"})," - matched by serial number"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Smart Plugs"})," - matched by IP address"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Notification Providers"})," - matched by name"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Filaments"})," - matched by name + type + brand"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Archives"})," - matched by content hash (always skipped)"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Pending Uploads"})," - matched by filename"]}),l.jsxs("li",{children:[l.jsx("strong",{children:"Settings & Templates"})," - always overwritten"]})]})]})]})}),l.jsx("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsxs("p",{className:"text-white font-medium flex items-center gap-2",children:[a?l.jsx(ya,{className:"w-4 h-4 text-orange-400"}):l.jsx(U1,{className:"w-4 h-4 text-bambu-gray"}),a?"Replace existing data":"Keep existing data"]}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:a?"Overwrite items that already exist with backup data":"Only restore items that don't already exist"})]}),l.jsx(On,{checked:a,onChange:s})]})}),a&&l.jsx("div",{className:"p-3 rounded-lg bg-orange-500/10 border border-orange-500/30",children:l.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[l.jsx($n,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),l.jsxs("div",{className:"text-orange-700 dark:text-orange-200",children:[l.jsx("span",{className:"font-medium",children:"Caution:"})," Overwriting will replace your current configurations with data from the backup. Printer access codes are never overwritten for security."]})]})})]}),l.jsxs("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:t,children:"Cancel"}),l.jsxs(ke,{type:"button",onClick:v,disabled:!o,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50",children:[l.jsx(ia,{className:"w-4 h-4 mr-2"}),"Restore"]})]})]}),r==="restoring"&&l.jsxs("div",{className:"p-8 flex flex-col items-center gap-4",children:[l.jsx(gt,{className:"w-12 h-12 text-bambu-green animate-spin"}),l.jsx("p",{className:"text-bambu-gray",children:"Processing backup file..."})]}),r==="result"&&u&&l.jsxs(l.Fragment,{children:[l.jsxs("div",{className:"p-4 space-y-4 max-h-[400px] overflow-y-auto",children:[l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{className:"p-3 rounded-lg bg-bambu-green/10 border border-bambu-green/30",children:[l.jsx("div",{className:"text-2xl font-bold text-bambu-green",children:b}),l.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Restored"})]}),l.jsxs("div",{className:"p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:[l.jsx("div",{className:"text-2xl font-bold text-yellow-500",children:E}),l.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Skipped"})]})]}),u.restored&&Object.entries(u.restored).some(([,w])=>w>0)&&l.jsxs("div",{className:"space-y-2",children:[l.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green"}),"Restored"]}),l.jsxs("div",{className:"space-y-1",children:[Object.entries(u.restored).filter(([,w])=>w>0).map(([w,T])=>l.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[l.jsx("span",{className:"text-white",children:D9[w]||w}),l.jsx("span",{className:"text-bambu-green font-medium",children:T})]},w)),(u.files_restored||0)>0&&l.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[l.jsx("span",{className:"text-white",children:"Files (3MF, thumbnails, etc.)"}),l.jsx("span",{className:"text-bambu-green font-medium",children:u.files_restored})]})]})]}),u.skipped&&Object.entries(u.skipped).some(([,w])=>w>0)&&l.jsxs("div",{className:"space-y-2",children:[l.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[l.jsx(U1,{className:"w-4 h-4 text-yellow-500"}),"Skipped (already exist)"]}),l.jsx("div",{className:"space-y-1",children:Object.entries(u.skipped).filter(([,w])=>w>0).map(([w,T])=>{const A=u.skipped_details?.[w]||[],C=f.has(w);return l.jsxs("div",{children:[l.jsxs("button",{onClick:()=>A.length>0&&y(w),className:`w-full flex items-center justify-between text-sm p-2 rounded bg-bambu-dark ${A.length>0?"hover:bg-bambu-dark-tertiary cursor-pointer":""}`,children:[l.jsxs("span",{className:"text-white flex items-center gap-2",children:[D9[w]||w,A.length>0&&(C?l.jsx(_b,{className:"w-3 h-3"}):l.jsx(Nl,{className:"w-3 h-3"}))]}),l.jsx("span",{className:"text-yellow-500 font-medium",children:T})]}),C&&A.length>0&&l.jsxs("div",{className:"mt-1 ml-4 p-2 rounded bg-bambu-dark-tertiary text-xs text-bambu-gray space-y-1",children:[A.slice(0,10).map((N,F)=>l.jsx("div",{children:N},F)),A.length>10&&l.jsxs("div",{className:"text-bambu-gray/60",children:["...and ",A.length-10," more"]})]})]},w)})})]}),b===0&&E===0&&l.jsx("div",{className:"p-4 text-center text-bambu-gray",children:"No data was found to restore in the backup file."})]}),l.jsx("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:l.jsx(ke,{onClick:t,children:"Close"})})]})]})})})}async function UGe(){const t=await fetch("/api/v1/settings/spoolman");if(!t.ok)throw new Error("Failed to load Spoolman settings");return t.json()}async function zGe(t){const e=await fetch("/api/v1/settings/spoolman",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(!e.ok)throw new Error("Failed to save Spoolman settings");return e.json()}function BGe(){const t=hn(),[e,n]=M.useState(!1),[r,i]=M.useState(""),[a,s]=M.useState("auto"),[o,c]=M.useState(!1),[u,d]=M.useState("all"),[f,g]=M.useState(!1),{data:p,isLoading:x}=xt({queryKey:["spoolman-settings"],queryFn:UGe}),{data:v,isLoading:y,refetch:b}=xt({queryKey:["spoolman-status"],queryFn:Ee.getSpoolmanStatus,refetchInterval:3e4}),{data:E}=xt({queryKey:["printers"],queryFn:Ee.getPrinters});M.useEffect(()=>{p&&(n(p.spoolman_enabled==="true"),i(p.spoolman_url||""),s(p.spoolman_sync_mode||"auto"),g(!0))},[p]),M.useEffect(()=>{if(!f||!p)return;if(p.spoolman_enabled==="true"!==e||(p.spoolman_url||"")!==r||(p.spoolman_sync_mode||"auto")!==a){const q=setTimeout(()=>{w.mutate()},500);return()=>clearTimeout(q)}},[e,r,a,f]);const w=_t({mutationFn:()=>zGe({spoolman_enabled:e?"true":"false",spoolman_url:r,spoolman_sync_mode:a}),onSuccess:()=>{t.invalidateQueries({queryKey:["spoolman-settings"]}),t.invalidateQueries({queryKey:["spoolman-status"]}),c(!0),setTimeout(()=>c(!1),2e3)}}),T=_t({mutationFn:Ee.connectSpoolman,onSuccess:()=>{b()}}),A=_t({mutationFn:Ee.disconnectSpoolman,onSuccess:()=>{b()}}),C=_t({mutationFn:Ee.syncAllPrintersAms,onSuccess:L=>{L.success}}),N=_t({mutationFn:L=>Ee.syncPrinterAms(L),onSuccess:L=>{L.success}}),F=()=>{u==="all"?C.mutate():N.mutate(u)},k=C.isPending||N.isPending,R=u==="all"?C.data:N.data,I=u==="all"?C.isSuccess:N.isSuccess;return x?l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(vp,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]})}),l.jsx(Tt,{children:l.jsx("div",{className:"flex justify-center py-8",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})})})]}):l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(vp,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]}),w.isPending&&l.jsx(gt,{className:"w-4 h-4 text-bambu-green animate-spin"}),o&&l.jsx(gl,{className:"w-4 h-4 text-bambu-green"})]})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Connect to Spoolman for filament inventory tracking. AMS data will sync automatically."}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Enable Spoolman"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Sync filament data with Spoolman server"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:e,onChange:L=>n(L.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Spoolman URL"}),l.jsx("input",{type:"text",placeholder:"http://192.168.1.100:7912",value:r,onChange:L=>i(L.target.value),disabled:!e,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray/50 focus:border-bambu-green focus:outline-none disabled:opacity-50"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"URL of your Spoolman server (e.g., http://localhost:7912)"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Sync Mode"}),l.jsxs("select",{value:a,onChange:L=>s(L.target.value),disabled:!e,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 disabled:opacity-50",children:[l.jsx("option",{value:"auto",children:"Automatic"}),l.jsx("option",{value:"manual",children:"Manual Only"})]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:a==="auto"?"AMS data syncs automatically when changes are detected":"Only sync when manually triggered"})]}),e&&l.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center justify-between mb-3",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("span",{className:"text-sm text-bambu-gray",children:"Status:"}),y?l.jsx(gt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):v?.connected?l.jsxs("span",{className:"flex items-center gap-1 text-sm text-green-500",children:[l.jsx(gl,{className:"w-4 h-4"}),"Connected"]}):l.jsxs("span",{className:"flex items-center gap-1 text-sm text-red-500",children:[l.jsx(Vt,{className:"w-4 h-4"}),"Disconnected"]})]}),l.jsx("div",{className:"flex gap-2",children:v?.connected?l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>A.mutate(),disabled:A.isPending,children:[A.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cce,{className:"w-4 h-4"}),"Disconnect"]}):l.jsxs(ke,{size:"sm",onClick:()=>T.mutate(),disabled:T.isPending||!r,children:[T.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Gk,{className:"w-4 h-4"}),"Connect"]})})]}),T.isError&&l.jsx("div",{className:"mb-3 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400",children:T.error.message}),v?.connected&&l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-sm text-white",children:"Sync AMS Data"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Manually sync printer AMS data to Spoolman"})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("div",{className:"relative flex-1",children:[l.jsxs("select",{value:u,onChange:L=>d(L.target.value==="all"?"all":Number(L.target.value)),className:"w-full px-3 py-2 pr-8 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none appearance-none cursor-pointer",children:[l.jsx("option",{value:"all",children:"All Printers"}),E?.map(L=>l.jsx("option",{value:L.id,children:L.name},L.id))]}),l.jsx(Nl,{className:"absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:F,disabled:k,children:[k?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ya,{className:"w-4 h-4"}),"Sync"]})]})]}),I&&R&&l.jsx("div",{className:`mt-2 p-2 rounded text-sm ${R.success?"bg-green-500/20 border border-green-500/50 text-green-400":"bg-yellow-500/20 border border-yellow-500/50 text-yellow-400"}`,children:R.success?`Synced ${R.synced_count} trays successfully`:`Synced ${R.synced_count} trays with ${R.errors.length} errors`})]})]})]})}function HGe({link:t,onClose:e}){const n=hn(),{theme:r}=Eb(),i=!!t,a=M.useRef(null),[s,o]=M.useState(t?.name||""),[c,u]=M.useState(t?.url||""),[d,f]=M.useState(t?.icon||"link"),[g,p]=M.useState(!!t?.custom_icon),[x,v]=M.useState(t?.custom_icon?Ee.getExternalLinkIconUrl(t.id):null),[y,b]=M.useState(null),[E,w]=M.useState(null);M.useEffect(()=>{const I=L=>{L.key==="Escape"&&e()};return window.addEventListener("keydown",I),()=>window.removeEventListener("keydown",I)},[e]);const T=_t({mutationFn:async I=>{const L=await Ee.createExternalLink(I);return y?await Ee.uploadExternalLinkIcon(L.id,y):L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:I=>{w(I.message)}}),A=_t({mutationFn:async I=>{let L=await Ee.updateExternalLink(t.id,I);return y?L=await Ee.uploadExternalLinkIcon(t.id,y):!g&&t?.custom_icon&&(L=await Ee.deleteExternalLinkIcon(t.id)),L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:I=>{w(I.message)}}),C=I=>{const L=I.target.files?.[0];if(L){if(!["image/png","image/jpeg","image/gif","image/svg+xml","image/webp","image/x-icon"].includes(L.type)){w("Please select a valid image file (PNG, JPG, GIF, SVG, WebP, or ICO)");return}if(L.size>1024*1024){w("Image file must be less than 1MB");return}b(L),p(!0);const j=new FileReader;j.onload=V=>{v(V.target?.result)},j.readAsDataURL(L)}},N=()=>{b(null),v(null),p(!1),a.current&&(a.current.value="")},F=I=>{if(I.preventDefault(),w(null),!s.trim()){w("Name is required");return}if(!c.trim()){w("URL is required");return}if(!c.startsWith("http://")&&!c.startsWith("https://")){w("URL must start with http:// or https://");return}const L={name:s.trim(),url:c.trim(),icon:d};i?A.mutate(L):T.mutate(L)},k=T.isPending||A.isPending,R=eM(d);return l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:l.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-md",onClick:I=>I.stopPropagation(),children:[l.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:g&&x?l.jsx("img",{src:x,alt:"",className:`w-5 h-5 rounded ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}):l.jsx(R,{className:"w-5 h-5"})}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:i?"Edit Link":"Add External Link"})]}),l.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:F,className:"p-6 space-y-4",children:[E&&l.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:E}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),l.jsx("input",{type:"text",value:s,onChange:I=>o(I.target.value),placeholder:"My Link",maxLength:50,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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"URL *"}),l.jsx("input",{type:"text",value:c,onChange:I=>u(I.target.value),placeholder:"https://example.com",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"})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("label",{className:"block text-sm text-bambu-gray",children:"Icon"}),l.jsxs("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsx("span",{className:"text-sm text-white",children:"Custom Icon"}),l.jsx("input",{ref:a,type:"file",accept:"image/png,image/jpeg,image/gif,image/svg+xml,image/webp,image/x-icon",className:"hidden",onChange:C}),g&&x?l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("img",{src:x,alt:"Custom icon",className:`w-8 h-8 rounded border border-bambu-dark-tertiary ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}),l.jsx("button",{type:"button",onClick:N,className:"p-1 text-red-400 hover:text-red-300 transition-colors",title:"Remove custom icon",children:l.jsx(Sn,{className:"w-4 h-4"})})]}):l.jsxs(ke,{type:"button",variant:"secondary",size:"sm",onClick:()=>a.current?.click(),children:[l.jsx(ia,{className:"w-4 h-4"}),"Upload"]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"PNG, JPG, GIF, SVG, WebP, or ICO. Max 1MB."})]}),!g&&l.jsxs("div",{children:[l.jsx("span",{className:"text-sm text-bambu-gray block mb-2",children:"Or choose a preset icon"}),l.jsx(yde,{value:d,onChange:f})]})]}),l.jsxs("div",{className:"flex gap-3 pt-2",children:[l.jsx(ke,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:k,className:"flex-1",children:[k?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Ms,{className:"w-4 h-4"}),i?"Save":"Add"]})]})]})]})})}function $Ge(){const t=hn(),[e,n]=M.useState(!1),[r,i]=M.useState(null),[a,s]=M.useState(null),[o,c]=M.useState(null),{data:u,isLoading:d}=xt({queryKey:["external-links"],queryFn:Ee.getExternalLinks}),f=_t({mutationFn:E=>Ee.deleteExternalLink(E),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),g=_t({mutationFn:E=>Ee.reorderExternalLinks(E),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),p=(E,w)=>{c(w),E.dataTransfer.effectAllowed="move"},x=E=>{E.preventDefault(),E.dataTransfer.dropEffect="move"},v=(E,w)=>{if(E.preventDefault(),o===null||o===w||!u)return;const T=u.map(F=>F.id),A=T.indexOf(o),C=T.indexOf(w);if(A===-1||C===-1)return;const N=[...T];N.splice(A,1),N.splice(C,0,o),g.mutate(N),c(null)},y=E=>{s(E)},b=()=>{a&&(f.mutate(a.id),s(null))};return l.jsxs(l.Fragment,{children:[l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Gk,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Sidebar Links"})]}),l.jsxs(ke,{size:"sm",onClick:()=>n(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Link"]})]})}),l.jsxs(Tt,{children:[l.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Add external links to the sidebar navigation. Drag to reorder."}),d?l.jsx("div",{className:"flex justify-center py-8",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):u&&u.length>0?l.jsx("div",{className:"space-y-2",children:u.map(E=>{const w=eM(E.icon);return l.jsxs("div",{draggable:!0,onDragStart:T=>p(T,E.id),onDragOver:x,onDrop:T=>v(T,E.id),className:`flex items-center gap-3 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary transition-colors ${o===E.id?"opacity-50":""}`,children:[l.jsx(N0,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray cursor-grab flex-shrink-0"}),l.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-gray",children:l.jsx(w,{className:"w-4 h-4"})}),l.jsxs("div",{className:"flex-1 min-w-0",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("span",{className:"text-white font-medium truncate",children:E.name}),l.jsx(ga,{className:"w-3 h-3 text-bambu-gray flex-shrink-0"})]}),l.jsx("span",{className:"text-sm text-bambu-gray truncate block",children:E.url})]}),l.jsxs("div",{className:"flex items-center gap-1 flex-shrink-0",children:[l.jsx("button",{onClick:()=>i(E),className:"p-2 rounded-lg hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors",title:"Edit",children:l.jsx(Fc,{className:"w-4 h-4"})}),l.jsx("button",{onClick:()=>y(E),disabled:f.isPending,className:"p-2 rounded-lg hover:bg-red-500/20 text-bambu-gray hover:text-red-400 transition-colors disabled:opacity-50",title:"Delete",children:l.jsx(Sn,{className:"w-4 h-4"})})]})]},E.id)})}):l.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[l.jsx(Gk,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),l.jsx("p",{children:"No external links configured"}),l.jsx("p",{className:"text-sm",children:'Click "Add Link" to add one'})]})]})]}),(e||r)&&l.jsx(HGe,{link:r,onClose:()=>{n(!1),i(null)}}),a&&l.jsx(Lr,{title:"Delete Link",message:`Are you sure you want to delete "${a.name}"? This action cannot be undone.`,confirmText:"Delete",cancelText:"Cancel",variant:"danger",onConfirm:b,onCancel:()=>s(null)})]})}function VGe(){const t=hn(),{showToast:e}=vr(),[n,r]=M.useState(!1),[i,a]=M.useState(""),[s,o]=M.useState("immediate"),[c,u]=M.useState(!1),{data:d,isLoading:f}=xt({queryKey:["virtual-printer-settings"],queryFn:Yk.getSettings,refetchInterval:1e4});M.useEffect(()=>{d&&(r(d.enabled),o(d.mode))},[d]);const g=_t({mutationFn:E=>Yk.updateSettings(E),onSuccess:()=>{t.invalidateQueries({queryKey:["virtual-printer-settings"]}),e("Virtual printer settings updated")},onError:E=>{e(E.message||"Failed to update settings","error"),d&&(r(d.enabled),o(d.mode))}}),p=()=>{const E=!n;if(E&&!i&&!d?.access_code_set){e("Please set an access code first","error");return}r(E),g.mutate({enabled:E,access_code:i||void 0,mode:s})},x=()=>{if(!i){e("Access code cannot be empty","error");return}if(i.length!==8){e("Access code must be exactly 8 characters","error");return}g.mutate({access_code:i}),a("")},v=E=>{o(E),g.mutate({mode:E})};if(f)return l.jsx(St,{children:l.jsx(Tt,{className:"py-8 flex justify-center",children:l.jsx(gt,{className:"w-6 h-6 animate-spin text-bambu-green"})})});const y=d?.status,b=y?.running||!1;return l.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[l.jsx("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(Ui,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Virtual Printer"})]}),y&&l.jsxs("div",{className:`flex items-center gap-2 text-sm ${b?"text-green-400":"text-bambu-gray"}`,children:[l.jsx("span",{className:`w-2 h-2 rounded-full ${b?"bg-green-400 animate-pulse":"bg-gray-500"}`}),b?"Running":"Stopped"]})]})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Enable a virtual printer that appears in Bambu Studio and OrcaSlicer. Files sent to this printer will be archived directly without printing."}),l.jsxs("div",{className:"flex items-center justify-between py-3 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{children:[l.jsx("div",{className:"text-white font-medium",children:"Enable Virtual Printer"}),l.jsx("div",{className:"text-sm text-bambu-gray",children:b?'Visible as "Bambuddy" in slicer discovery':"Not visible to slicers"})]}),l.jsx("button",{onClick:p,disabled:g.isPending,className:`relative w-12 h-6 rounded-full transition-colors ${n?"bg-bambu-green":"bg-bambu-dark-tertiary"} ${g.isPending?"opacity-50":""}`,children:l.jsx("span",{className:`absolute top-1 left-1 w-4 h-4 bg-white rounded-full transition-transform ${n?"translate-x-6":""}`})})]}),l.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[l.jsx("div",{className:"text-white font-medium mb-2",children:"Access Code"}),l.jsx("div",{className:"text-sm text-bambu-gray mb-3",children:d?.access_code_set?l.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[l.jsx(gl,{className:"w-4 h-4"}),"Access code is set"]}):l.jsxs("span",{className:"flex items-center gap-1 text-yellow-400",children:[l.jsx($n,{className:"w-4 h-4"}),"No access code set - required to enable"]})}),l.jsxs("div",{className:"flex gap-2",children:[l.jsxs("div",{className:"relative flex-1",children:[l.jsx("input",{type:c?"text":"password",value:i,onChange:E=>a(E.target.value),placeholder:d?.access_code_set?"Enter new code to change":"Enter 8-char code",maxLength:8,className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-2 text-white placeholder-bambu-gray pr-10 font-mono"}),l.jsx("button",{onClick:()=>u(!c),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:c?l.jsx(KS,{className:"w-4 h-4"}):l.jsx(Yh,{className:"w-4 h-4"})})]}),l.jsx(ke,{onClick:x,disabled:!i||g.isPending,variant:"primary",children:g.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):"Save"})]}),l.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["Must be exactly 8 characters. Used by slicers to authenticate.",i&&l.jsxs("span",{className:i.length===8?"text-green-400":"text-yellow-400",children:[" ","(",i.length,"/8)"]})]})]}),l.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[l.jsx("div",{className:"text-white font-medium mb-2",children:"Archive Mode"}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("button",{onClick:()=>v("immediate"),disabled:g.isPending,className:`p-3 rounded-lg border text-left transition-colors ${s==="immediate"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[l.jsx("div",{className:"text-white font-medium",children:"Immediate"}),l.jsx("div",{className:"text-xs text-bambu-gray",children:"Archive files as soon as they are uploaded"})]}),l.jsxs("button",{onClick:()=>v("queue"),disabled:g.isPending,className:`p-3 rounded-lg border text-left transition-colors ${s==="queue"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[l.jsx("div",{className:"text-white font-medium",children:"Queue for Review"}),l.jsx("div",{className:"text-xs text-bambu-gray",children:"Review and tag files before archiving"})]})]})]})]})]})}),l.jsxs("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:[l.jsx(St,{children:l.jsx(Tt,{className:"py-4",children:l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx(_p,{className:"w-5 h-5 text-blue-400 flex-shrink-0 mt-0.5"}),l.jsxs("div",{className:"text-sm text-bambu-gray",children:[l.jsx("p",{className:"mb-2",children:l.jsx("strong",{className:"text-white",children:"How it works:"})}),l.jsxs("ol",{className:"list-decimal list-inside space-y-1",children:[l.jsx("li",{children:"Enable the virtual printer and set an access code"}),l.jsx("li",{children:'In Bambu Studio or OrcaSlicer, go to "Add Printer"'}),l.jsx("li",{children:'The "Bambuddy" printer should appear in the discovery list'}),l.jsx("li",{children:"Connect using the access code you set"}),l.jsx("li",{children:'When you "print" to Bambuddy, the 3MF file is archived instead'})]}),l.jsxs("p",{className:"mt-3 text-yellow-400/80",children:[l.jsx($n,{className:"w-4 h-4 inline mr-1"}),"Required ports: 2021 (SSDP), 8883 (MQTT), 990 (FTP)"]}),l.jsxs("div",{className:"mt-2 text-xs text-bambu-gray space-y-1",children:[l.jsx("p",{children:"Port 990 requires root or iptables redirect:"}),l.jsx("code",{className:"block bg-bambu-dark-tertiary px-2 py-1 rounded text-[10px]",children:"sudo iptables -t nat -A PREROUTING -p tcp --dport 990 -j REDIRECT --to-port 9990"}),l.jsx("code",{className:"block bg-bambu-dark-tertiary px-2 py-1 rounded text-[10px]",children:"sudo iptables -t nat -A OUTPUT -o lo -p tcp --dport 990 -j REDIRECT --to-port 9990"})]})]})]})})}),y&&b&&l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h3",{className:"text-md font-semibold text-white",children:"Status Details"})}),l.jsx(Tt,{children:l.jsxs("div",{className:"grid grid-cols-2 gap-4 text-sm",children:[l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Printer Name"}),l.jsx("div",{className:"text-white",children:y.name})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Model"}),l.jsx("div",{className:"text-white",children:y.model?.replace("3DPrinter-","").replace("-"," ")||"X1 Carbon"})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Serial Number"}),l.jsx("div",{className:"text-white font-mono",children:y.serial})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Mode"}),l.jsx("div",{className:"text-white capitalize",children:y.mode})]}),l.jsxs("div",{children:[l.jsx("div",{className:"text-bambu-gray",children:"Pending Files"}),l.jsx("div",{className:"text-white",children:y.pending_files})]})]})})]})]})]})}function GGe(){const t=hn(),{t:e,i18n:n}=Qp(),{showToast:r,showPersistentToast:i,dismissToast:a}=vr(),[s,o]=M.useState(null),[c,u]=M.useState(!1),[d,f]=M.useState(null),[g,p]=M.useState(!1),[x,v]=M.useState(null),[y,b]=M.useState(null),[E,w]=M.useState(!1),[T,A]=M.useState(zG()),[C,N]=M.useState("general"),[F,k]=M.useState(!1),[R,I]=M.useState(""),[L,q]=M.useState({can_queue:!0,can_control_printer:!1,can_read_status:!0}),[j,V]=M.useState(null),[K,G]=M.useState(null),[J,ee]=M.useState(!1),[Q,B]=M.useState(!1),[$,Z]=M.useState(null),[te,he]=M.useState(!1),[de,ge]=M.useState(!1),[Ae,me]=M.useState(!1),[ce,Pe]=M.useState(!1),oe=Se=>{A(Se),wde(Se)},Le=()=>{localStorage.removeItem("sidebarOrder"),window.location.reload()},{data:z,isLoading:H}=xt({queryKey:["settings"],queryFn:Ee.getSettings}),{data:ne,isLoading:W}=xt({queryKey:["smart-plugs"],queryFn:Ee.getSmartPlugs}),{data:Y,isLoading:se}=xt({queryKey:["smart-plugs-energy",ne?.map(Se=>Se.id)],queryFn:async()=>{if(!ne||ne.length===0)return null;const Se=await Promise.all(ne.filter(nn=>nn.enabled).map(async nn=>{try{const Ur=await Ee.getSmartPlugStatus(nn.id);return{plug:nn,status:Ur}}catch{return{plug:nn,status:null}}}));let bt=0,Gt=0,kt=0,Tn=0,Nn=0;for(const{status:nn}of Se)nn?.reachable&&nn.energy&&(Nn++,nn.energy.power!=null&&(bt+=nn.energy.power),nn.energy.today!=null&&(Gt+=nn.energy.today),nn.energy.yesterday!=null&&(kt+=nn.energy.yesterday),nn.energy.total!=null&&(Tn+=nn.energy.total));return{totalPower:bt,totalToday:Gt,totalYesterday:kt,totalLifetime:Tn,reachableCount:Nn,totalPlugs:ne.filter(nn=>nn.enabled).length}},enabled:C==="plugs"&&!!ne&&ne.length>0,refetchInterval:C==="plugs"?1e4:!1}),{data:be,isLoading:Te}=xt({queryKey:["notification-providers"],queryFn:Ee.getNotificationProviders}),{data:Me,isLoading:U}=xt({queryKey:["api-keys"],queryFn:Ee.getAPIKeys}),O=_t({mutationFn:Se=>Ee.createAPIKey(Se),onSuccess:Se=>{V(Se.key||null),k(!1),I(""),t.invalidateQueries({queryKey:["api-keys"]}),r("API key created")},onError:Se=>{r(`Failed to create API key: ${Se.message}`,"error")}}),re=_t({mutationFn:Se=>Ee.deleteAPIKey(Se),onSuccess:()=>{t.invalidateQueries({queryKey:["api-keys"]}),r("API key deleted")},onError:Se=>{r(`Failed to delete API key: ${Se.message}`,"error")}}),{data:ve}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:we,isLoading:ye}=xt({queryKey:["notification-templates"],queryFn:Ee.getNotificationTemplates}),{data:rt}=xt({queryKey:["virtual-printer-settings"],queryFn:Yk.getSettings,refetchInterval:1e4}),Ve=rt?.status?.running??!1,{data:tt}=xt({queryKey:["ffmpeg-status"],queryFn:Ee.checkFfmpeg}),{data:ut}=xt({queryKey:["version"],queryFn:Ee.getVersion}),{data:ue,refetch:ae,isRefetching:$e}=xt({queryKey:["updateCheck"],queryFn:Ee.checkForUpdates,staleTime:300*1e3}),{data:Ue,refetch:Ze}=xt({queryKey:["updateStatus"],queryFn:Ee.getUpdateStatus,refetchInterval:Se=>{const bt=Se.state.data;return bt?.status==="downloading"||bt?.status==="installing"?1e3:!1}}),dt=_t({mutationFn:Ee.applyUpdate,onSuccess:()=>{Ze()}}),[fe,Je]=M.useState(null),xe=_t({mutationFn:Ee.testAllNotificationProviders,onSuccess:Se=>{Je(Se),t.invalidateQueries({queryKey:["notification-providers"]}),Se.failed===0?r(`All ${Se.tested} providers tested successfully!`,"success"):r(`${Se.success}/${Se.tested} providers succeeded`,Se.failed>0?"error":"success")},onError:Se=>{r(`Failed to test providers: ${Se.message}`,"error")}}),ze=_t({mutationFn:async Se=>{if(!ne)return{success:0,failed:0};const bt=ne.filter(kt=>kt.enabled),Gt=await Promise.all(bt.map(async kt=>{try{return await Ee.controlSmartPlug(kt.id,Se),{success:!0}}catch{return{success:!1}}}));return{success:Gt.filter(kt=>kt.success).length,failed:Gt.filter(kt=>!kt.success).length}},onSuccess:(Se,bt)=>{t.invalidateQueries({queryKey:["smart-plugs"]}),t.invalidateQueries({queryKey:["smart-plugs-energy"]}),Se.failed===0?r(`All ${Se.success} plugs turned ${bt}`,"success"):r(`${Se.success} plugs turned ${bt}, ${Se.failed} failed`,"error")},onError:Se=>{r(`Failed: ${Se.message}`,"error")}}),Re=M.useRef(null),_e=M.useRef(!0);M.useEffect(()=>{z&&!s&&(o(z),setTimeout(()=>{_e.current=!1},100))},[z,s]);const qe=_t({mutationFn:Ee.updateSettings,onSuccess:Se=>{t.setQueryData(["settings"],Se),t.invalidateQueries({queryKey:["archiveStats"]}),r("Settings saved","success")},onError:Se=>{r(`Failed to save: ${Se.message}`,"error")}});M.useEffect(()=>{if(!(_e.current||!s||!z||!(z.auto_archive!==s.auto_archive||z.save_thumbnails!==s.save_thumbnails||z.capture_finish_photo!==s.capture_finish_photo||z.default_filament_cost!==s.default_filament_cost||z.currency!==s.currency||z.energy_cost_per_kwh!==s.energy_cost_per_kwh||z.energy_tracking_mode!==s.energy_tracking_mode||z.check_updates!==s.check_updates||z.notification_language!==s.notification_language||z.telemetry_enabled!==s.telemetry_enabled||z.ams_humidity_good!==s.ams_humidity_good||z.ams_humidity_fair!==s.ams_humidity_fair||z.ams_temp_good!==s.ams_temp_good||z.ams_temp_fair!==s.ams_temp_fair||z.ams_history_retention_days!==s.ams_history_retention_days||z.date_format!==s.date_format||z.time_format!==s.time_format||z.default_printer_id!==s.default_printer_id)))return Re.current&&clearTimeout(Re.current),Re.current=setTimeout(()=>{const bt={auto_archive:s.auto_archive,save_thumbnails:s.save_thumbnails,capture_finish_photo:s.capture_finish_photo,default_filament_cost:s.default_filament_cost,currency:s.currency,energy_cost_per_kwh:s.energy_cost_per_kwh,energy_tracking_mode:s.energy_tracking_mode,check_updates:s.check_updates,notification_language:s.notification_language,telemetry_enabled:s.telemetry_enabled,ams_humidity_good:s.ams_humidity_good,ams_humidity_fair:s.ams_humidity_fair,ams_temp_good:s.ams_temp_good,ams_temp_fair:s.ams_temp_fair,ams_history_retention_days:s.ams_history_retention_days,date_format:s.date_format,time_format:s.time_format,default_printer_id:s.default_printer_id};qe.mutate(bt)},500),()=>{Re.current&&clearTimeout(Re.current)}},[s,z,qe]);const lt=M.useCallback((Se,bt)=>{o(Gt=>Gt?{...Gt,[Se]:bt}:null)},[]);return H||!s?l.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):l.jsxs("div",{className:"p-4 md:p-8",children:[l.jsxs("div",{className:"mb-8",children:[l.jsx("h1",{className:"text-2xl font-bold text-white",children:"Settings"}),l.jsx("p",{className:"text-bambu-gray",children:"Configure Bambuddy"})]}),l.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary",children:[l.jsx("button",{onClick:()=>N("general"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px ${C==="general"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:"General"}),l.jsxs("button",{onClick:()=>N("plugs"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="plugs"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(so,{className:"w-4 h-4"}),"Smart Plugs",ne&&ne.length>0&&l.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:ne.length})]}),l.jsxs("button",{onClick:()=>N("notifications"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="notifications"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(Ph,{className:"w-4 h-4"}),"Notifications",be&&be.length>0&&l.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:be.length})]}),l.jsxs("button",{onClick:()=>N("apikeys"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="apikeys"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(Qm,{className:"w-4 h-4"}),"API Keys",Me&&Me.length>0&&l.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:Me.length})]}),l.jsxs("button",{onClick:()=>N("virtual-printer"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${C==="virtual-printer"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-white border-transparent"}`,children:[l.jsx(Ui,{className:"w-4 h-4"}),"Virtual Printer",l.jsx("span",{className:`w-2 h-2 rounded-full ${Ve?"bg-green-400":"bg-gray-500"}`})]})]}),C==="general"&&l.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[l.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-xl",children:[l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:e("settings.general")})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{children:[l.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[l.jsx(A0,{className:"w-4 h-4 inline mr-1"}),e("settings.language")]}),l.jsx("select",{value:n.language,onChange:Se=>n.changeLanguage(Se.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",children:qF.map(Se=>l.jsxs("option",{value:Se.code,children:[Se.nativeName," (",Se.name,")"]},Se.code))}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:e("settings.languageDescription")})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:e("settings.defaultView")}),l.jsx("select",{value:T,onChange:Se=>oe(Se.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",children:k0.map(Se=>l.jsx("option",{value:Se.to,children:e(Se.labelKey)},Se.id))}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:e("settings.defaultViewDescription")})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Date Format"}),l.jsxs("select",{value:s.date_format||"system",onChange:Se=>lt("date_format",Se.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",children:[l.jsx("option",{value:"system",children:"System Default"}),l.jsx("option",{value:"us",children:"US (MM/DD/YYYY)"}),l.jsx("option",{value:"eu",children:"EU (DD/MM/YYYY)"}),l.jsx("option",{value:"iso",children:"ISO (YYYY-MM-DD)"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Time Format"}),l.jsxs("select",{value:s.time_format||"system",onChange:Se=>lt("time_format",Se.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",children:[l.jsx("option",{value:"system",children:"System Default"}),l.jsx("option",{value:"12h",children:"12-hour (3:30 PM)"}),l.jsx("option",{value:"24h",children:"24-hour (15:30)"})]})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default Printer"}),l.jsxs("select",{value:s.default_printer_id??"",onChange:Se=>lt("default_printer_id",Se.target.value?Number(Se.target.value):null),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:[l.jsx("option",{value:"",children:"No default (ask each time)"}),ve?.map(Se=>l.jsx("option",{value:Se.id,children:Se.name},Se.id))]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Pre-select this printer for uploads, reprints, and other operations."})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Sidebar order"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Drag items in the sidebar to reorder. Reset to default order here."})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:Le,children:[l.jsx(ng,{className:"w-4 h-4"}),"Reset"]})]})]})]}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Archive Settings"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Auto-archive prints"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically save 3MF files when prints complete"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.auto_archive,onChange:Se=>lt("auto_archive",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Save thumbnails"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Extract and save preview images from 3MF files"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.save_thumbnails,onChange:Se=>lt("save_thumbnails",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Capture finish photo"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Take a photo from printer camera when print completes"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.capture_finish_photo,onChange:Se=>lt("capture_finish_photo",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),s.capture_finish_photo&&tt&&!tt.installed&&l.jsxs("div",{className:"flex items-start gap-2 p-3 bg-yellow-500/10 border border-yellow-500/30 rounded-lg",children:[l.jsx($n,{className:"w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5"}),l.jsxs("div",{className:"text-sm",children:[l.jsx("p",{className:"text-yellow-500 font-medium",children:"ffmpeg not installed"}),l.jsxs("p",{className:"text-bambu-gray mt-1",children:["Camera capture requires ffmpeg. Install it via"," ",l.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"brew install ffmpeg"})," (macOS) or"," ",l.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"apt install ffmpeg"})," (Linux)."]})]})]})]})]}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Cost Tracking"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default filament cost (per kg)"}),l.jsx("input",{type:"number",step:"0.01",min:"0",value:s.default_filament_cost,onChange:Se=>lt("default_filament_cost",parseFloat(Se.target.value)||0),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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Currency"}),l.jsxs("select",{value:s.currency,onChange:Se=>lt("currency",Se.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",children:[l.jsx("option",{value:"USD",children:"USD ($)"}),l.jsx("option",{value:"EUR",children:"EUR (€)"}),l.jsx("option",{value:"GBP",children:"GBP (£)"}),l.jsx("option",{value:"CHF",children:"CHF (Fr.)"}),l.jsx("option",{value:"JPY",children:"JPY (¥)"}),l.jsx("option",{value:"CNY",children:"CNY (¥)"}),l.jsx("option",{value:"CAD",children:"CAD ($)"}),l.jsx("option",{value:"AUD",children:"AUD ($)"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Electricity cost per kWh"}),l.jsx("input",{type:"number",step:"0.01",min:"0",value:s.energy_cost_per_kwh,onChange:Se=>lt("energy_cost_per_kwh",parseFloat(Se.target.value)||0),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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Energy display mode"}),l.jsxs("select",{value:s.energy_tracking_mode||"total",onChange:Se=>lt("energy_tracking_mode",Se.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",children:[l.jsx("option",{value:"print",children:"Prints Only"}),l.jsx("option",{value:"total",children:"Total Consumption"})]}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:s.energy_tracking_mode==="print"?"Dashboard shows sum of energy used during prints":"Dashboard shows lifetime energy from smart plugs"})]})]})]})]}),l.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-md",children:[l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"AMS Display Thresholds"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-sm text-bambu-gray",children:"Configure color thresholds for AMS humidity and temperature indicators."}),l.jsxs("div",{className:"space-y-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-white",children:[l.jsx(SG,{className:"w-4 h-4 text-blue-400"}),l.jsx("span",{className:"font-medium",children:"Humidity"})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (green) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",min:"0",max:"100",value:s.ams_humidity_good??40,onChange:Se=>lt("ams_humidity_good",parseInt(Se.target.value)||40),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"}),l.jsx("span",{className:"text-bambu-gray",children:"%"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",min:"0",max:"100",value:s.ams_humidity_fair??60,onChange:Se=>lt("ams_humidity_fair",parseInt(Se.target.value)||60),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"}),l.jsx("span",{className:"text-bambu-gray",children:"%"})]})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (bad)"})]}),l.jsxs("div",{className:"space-y-3 pt-2 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-2 text-white",children:[l.jsx(JS,{className:"w-4 h-4 text-orange-400"}),l.jsx("span",{className:"font-medium",children:"Temperature"})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (blue) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:s.ams_temp_good??28,onChange:Se=>lt("ams_temp_good",parseFloat(Se.target.value)||28),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"}),l.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:s.ams_temp_fair??35,onChange:Se=>lt("ams_temp_fair",parseFloat(Se.target.value)||35),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"}),l.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (hot)"})]}),l.jsxs("div",{className:"space-y-3 pt-4 border-t border-bambu-dark-tertiary",children:[l.jsxs("div",{className:"flex items-center gap-2 text-white",children:[l.jsx(vp,{className:"w-4 h-4 text-purple-400"}),l.jsx("span",{className:"font-medium",children:"History Retention"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Keep sensor history for"}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("input",{type:"number",min:"1",max:"365",value:s.ams_history_retention_days??30,onChange:Se=>lt("ams_history_retention_days",parseInt(Se.target.value)||30),className:"w-24 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"}),l.jsx("span",{className:"text-bambu-gray",children:"days"})]})]}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Older humidity and temperature data will be automatically deleted"})]})]})]}),l.jsx(BGe,{})]}),l.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-sm",children:[l.jsx($Ge,{}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Updates"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Check for updates"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically check for new versions on startup"})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.check_updates,onChange:Se=>lt("check_updates",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("p",{className:"text-white",children:e("settings.telemetry")}),l.jsxs("button",{onClick:()=>me(!0),className:"inline-flex items-center gap-1 px-2 py-0.5 text-xs bg-bambu-dark rounded-full text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary transition-colors",children:[l.jsx(_p,{className:"w-3 h-3"}),e("settings.telemetryLearnMore")]})]}),l.jsx("p",{className:"text-sm text-bambu-gray",children:e("settings.telemetryDescription")})]}),l.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:s.telemetry_enabled,onChange:Se=>lt("telemetry_enabled",Se.target.checked),className:"sr-only peer"}),l.jsx("div",{className:"w-11 h-6 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-bambu-green"})]})]}),l.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Current version"}),l.jsxs("p",{className:"text-sm text-bambu-gray",children:["v",ut?.version||"..."]})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>ae(),disabled:$e,children:[$e?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ya,{className:"w-4 h-4"}),"Check now"]})]}),ue?.update_available?l.jsxs("div",{className:"mt-4 p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:[l.jsxs("div",{className:"flex items-start justify-between",children:[l.jsxs("div",{children:[l.jsxs("p",{className:"text-bambu-green font-medium",children:["Update available: v",ue.latest_version]}),ue.release_name&&ue.release_name!==ue.latest_version&&l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:ue.release_name})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[ue.release_notes&&l.jsx("button",{onClick:()=>Pe(!0),className:"text-bambu-gray hover:text-white transition-colors text-sm underline",children:"Release Notes"}),ue.release_url&&l.jsx("a",{href:ue.release_url,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-white transition-colors",title:"View release on GitHub",children:l.jsx(ga,{className:"w-4 h-4"})})]})]}),Ue?.status==="downloading"||Ue?.status==="installing"?l.jsxs("div",{className:"mt-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[l.jsx(gt,{className:"w-4 h-4 animate-spin"}),l.jsx("span",{children:Ue.message})]}),l.jsx("div",{className:"mt-2 w-full bg-bambu-dark-tertiary rounded-full h-2",children:l.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${Ue.progress}%`}})})]}):Ue?.status==="complete"?l.jsx("div",{className:"mt-3 p-2 bg-bambu-green/20 rounded text-sm text-bambu-green",children:Ue.message}):Ue?.status==="error"?l.jsx("div",{className:"mt-3 p-2 bg-red-500/20 rounded text-sm text-red-400",children:Ue.error||Ue.message}):l.jsxs(ke,{className:"mt-3",onClick:()=>dt.mutate(),disabled:dt.isPending,children:[dt.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ns,{className:"w-4 h-4"}),"Install Update"]})]}):ue?.error?l.jsxs("div",{className:"mt-2 p-2 bg-red-500/10 border border-red-500/30 rounded text-sm text-red-400",children:["Failed to check for updates: ",ue.error]}):ue&&!ue.update_available?l.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:"You're running the latest version"}):null]})]})]}),l.jsxs(St,{children:[l.jsx(Qi,{children:l.jsx("h2",{className:"text-lg font-semibold text-white",children:"Data Management"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Backup Data"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Export settings, providers, printers, and more"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>he(!0),children:[l.jsx(ns,{className:"w-4 h-4"}),"Export"]})]}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Restore Backup"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Import settings from a backup file with duplicate handling options"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>ge(!0),children:[l.jsx(ia,{className:"w-4 h-4"}),"Restore"]})]}),l.jsx("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Clear Notification Logs"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Delete notification logs older than 30 days"})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>ee(!0),children:[l.jsx(Sn,{className:"w-4 h-4"}),"Clear"]})]})}),l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white",children:"Reset UI Preferences"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"Reset sidebar order, theme, view modes, and layout preferences. Printers, archives, and settings are not affected."})]}),l.jsxs(ke,{variant:"secondary",size:"sm",onClick:()=>B(!0),children:[l.jsx(Sn,{className:"w-4 h-4"}),"Reset"]})]})]})]})]})]}),C==="plugs"&&l.jsxs("div",{className:"max-w-4xl",children:[l.jsxs("div",{className:"flex items-start justify-between mb-6",children:[l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(so,{className:"w-5 h-5 text-bambu-green"}),"Smart Plugs"]}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Connect Tasmota-based smart plugs to automate power control and track energy usage for your printers."})]}),l.jsxs("div",{className:"flex items-center gap-2 pt-1 shrink-0",children:[ne&&ne.filter(Se=>Se.enabled).length>1&&l.jsxs(l.Fragment,{children:[l.jsxs(ke,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Z("on"),disabled:ze.isPending,title:"Turn all plugs on",children:[ze.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Cl,{className:"w-4 h-4 text-bambu-green"}),"All On"]}),l.jsxs(ke,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Z("off"),disabled:ze.isPending,title:"Turn all plugs off",children:[ze.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ZS,{className:"w-4 h-4 text-red-400"}),"All Off"]})]}),l.jsxs(ke,{className:"whitespace-nowrap",onClick:()=>{f(null),u(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Smart Plug"]})]})]}),ne&&ne.length>0&&l.jsxs(St,{className:"mb-6",children:[l.jsx(Qi,{children:l.jsxs("h3",{className:"text-base font-semibold text-white flex items-center gap-2",children:[l.jsx(Jh,{className:"w-4 h-4 text-yellow-400"}),"Energy Summary",se&&l.jsx(gt,{className:"w-4 h-4 animate-spin text-bambu-gray ml-2"})]})}),l.jsx(Tt,{children:Y?l.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(Jh,{className:"w-3 h-3"}),"Current Power"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalPower.toFixed(1),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"W"})]}),l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:[Y.reachableCount,"/",Y.totalPlugs," plugs online"]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(qr,{className:"w-3 h-3"}),"Today"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalToday.toFixed(2),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),s&&s.energy_cost_per_kwh>0&&l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Y.totalToday*s.energy_cost_per_kwh).toFixed(2)," ",s.currency]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(DG,{className:"w-3 h-3"}),"Yesterday"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalYesterday.toFixed(2),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),s&&s.energy_cost_per_kwh>0&&l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Y.totalYesterday*s.energy_cost_per_kwh).toFixed(2)," ",s.currency]})]}),l.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[l.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[l.jsx(Hk,{className:"w-3 h-3"}),"Total"]}),l.jsxs("div",{className:"text-xl font-bold text-white",children:[Y.totalLifetime.toFixed(1),l.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),s&&s.energy_cost_per_kwh>0&&l.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Y.totalLifetime*s.energy_cost_per_kwh).toFixed(2)," ",s.currency]})]})]}):se?null:l.jsx("p",{className:"text-sm text-bambu-gray",children:"Enable plugs to see energy summary"})})]}),W?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):ne&&ne.length>0?l.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:ne.map(Se=>l.jsx(AGe,{plug:Se,onEdit:bt=>{f(bt),u(!0)}},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-12",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(so,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),l.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No smart plugs configured"}),l.jsx("p",{className:"text-sm mb-4",children:"Add a Tasmota-based smart plug to track energy usage and automate power control."}),l.jsxs(ke,{onClick:()=>{f(null),u(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Your First Smart Plug"]})]})})})]}),C==="notifications"&&l.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-8",children:[l.jsxs("div",{children:[l.jsxs("div",{className:"flex items-center justify-between mb-4",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(Ph,{className:"w-5 h-5 text-bambu-green"}),"Providers"]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>w(!0),children:[l.jsx(I1,{className:"w-4 h-4"}),"Log"]}),be&&be.length>0&&l.jsxs(ke,{size:"sm",variant:"secondary",onClick:()=>{Je(null),xe.mutate()},disabled:xe.isPending,children:[xe.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(cR,{className:"w-4 h-4"}),"Test All"]}),l.jsxs(ke,{size:"sm",onClick:()=>{v(null),p(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add"]})]})]}),l.jsx(St,{className:"mb-4",children:l.jsx(Tt,{className:"py-3",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{children:[l.jsx("p",{className:"text-white text-sm font-medium",children:e("settings.notificationLanguage")}),l.jsx("p",{className:"text-xs text-bambu-gray",children:e("settings.notificationLanguageDescription")})]}),l.jsx("select",{value:s.notification_language||"en",onChange:Se=>lt("notification_language",Se.target.value),className:"px-2 py-1.5 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:ring-1 focus:ring-bambu-green",children:qF.map(Se=>l.jsx("option",{value:Se.code,children:Se.nativeName},Se.code))})]})})}),fe&&l.jsx(St,{className:"mb-4",children:l.jsxs(Tt,{className:"py-3",children:[l.jsxs("div",{className:"flex items-center justify-between mb-2",children:[l.jsx("span",{className:"text-sm font-medium text-white",children:"Test Results"}),l.jsx("button",{onClick:()=>Je(null),className:"text-bambu-gray hover:text-white text-xs",children:"Dismiss"})]}),l.jsxs("div",{className:"flex items-center gap-4 text-sm mb-2",children:[l.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[l.jsx(Mr,{className:"w-4 h-4"}),fe.success," passed"]}),fe.failed>0&&l.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[l.jsx(Ca,{className:"w-4 h-4"}),fe.failed," failed"]})]}),fe.results.filter(Se=>!Se.success).length>0&&l.jsx("div",{className:"space-y-1 mt-2 pt-2 border-t border-bambu-dark-tertiary",children:fe.results.filter(Se=>!Se.success).map(Se=>l.jsxs("div",{className:"text-xs text-red-400",children:[l.jsxs("span",{className:"font-medium",children:[Se.provider_name,":"]})," ",Se.message]},Se.provider_id))})]})}),Te?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):be&&be.length>0?l.jsx("div",{className:"space-y-3",children:be.map(Se=>l.jsx(kGe,{provider:Se,onEdit:bt=>{v(bt),p(!0)}},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-8",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(Ph,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),l.jsx("p",{className:"text-sm font-medium text-white mb-2",children:"No providers configured"}),l.jsx("p",{className:"text-xs mb-3",children:"Add a provider to receive alerts."}),l.jsxs(ke,{size:"sm",onClick:()=>{v(null),p(!0)},children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Provider"]})]})})})]}),l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-4",children:[l.jsx(Ss,{className:"w-5 h-5 text-bambu-green"}),"Message Templates"]}),l.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Customize notification messages for each event."}),ye?l.jsx("div",{className:"flex justify-center py-8",children:l.jsx(gt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):we&&we.length>0?l.jsx("div",{className:"space-y-2",children:we.map(Se=>l.jsx(St,{className:"cursor-pointer hover:border-bambu-green/50 transition-colors",onClick:()=>b(Se),children:l.jsx(Tt,{className:"py-2.5 px-3",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"min-w-0 flex-1",children:[l.jsx("p",{className:"text-white font-medium text-sm truncate",children:Se.name}),l.jsx("p",{className:"text-bambu-gray text-xs truncate mt-0.5",children:Se.title_template})]}),l.jsx("button",{className:"p-1.5 hover:bg-bambu-dark-tertiary rounded transition-colors shrink-0 ml-2",onClick:bt=>{bt.stopPropagation(),b(Se)},children:l.jsx(lR,{className:"w-4 h-4 text-bambu-gray"})})]})})},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-8",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(Ss,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),l.jsx("p",{className:"text-sm",children:"No templates available. Restart the backend to seed default templates."})]})})})]})]}),C==="apikeys"&&l.jsxs("div",{className:"max-w-3xl",children:[l.jsxs("div",{className:"flex items-start justify-between gap-4 mb-6",children:[l.jsxs("div",{className:"flex-1",children:[l.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[l.jsx(Qm,{className:"w-5 h-5 text-bambu-green"}),"API Keys"]}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Create API keys for external integrations and webhooks. Use these keys to control your printers from automation tools like Home Assistant."})]}),l.jsxs(ke,{size:"sm",onClick:()=>k(!0),className:"flex-shrink-0",children:[l.jsx(ir,{className:"w-4 h-4"}),"Create Key"]})]}),j&&l.jsx(St,{className:"mb-6 border-bambu-green",children:l.jsx(Tt,{className:"py-4",children:l.jsxs("div",{className:"flex items-start gap-3",children:[l.jsx(Mr,{className:"w-5 h-5 text-bambu-green flex-shrink-0 mt-0.5"}),l.jsxs("div",{className:"flex-1",children:[l.jsx("p",{className:"text-white font-medium mb-1",children:"API Key Created Successfully"}),l.jsx("p",{className:"text-sm text-bambu-gray mb-2",children:"Copy this key now - it won't be shown again!"}),l.jsxs("div",{className:"flex items-center gap-2 bg-bambu-dark rounded-lg p-2",children:[l.jsx("code",{className:"flex-1 text-sm text-bambu-green font-mono break-all",children:j}),l.jsx(ke,{variant:"secondary",size:"sm",onClick:async()=>{try{if(navigator.clipboard&&navigator.clipboard.writeText)await navigator.clipboard.writeText(j);else{const Se=document.createElement("textarea");Se.value=j,Se.style.position="fixed",Se.style.left="-999999px",document.body.appendChild(Se),Se.select(),document.execCommand("copy"),document.body.removeChild(Se)}r("Key copied to clipboard")}catch{r("Failed to copy key","error")}},children:l.jsx(md,{className:"w-4 h-4"})})]}),l.jsx(ke,{variant:"secondary",size:"sm",className:"mt-3",onClick:()=>V(null),children:"Dismiss"})]})]})})}),F&&l.jsxs(St,{className:"mb-6",children:[l.jsx(Qi,{children:l.jsx("h3",{className:"text-base font-semibold text-white",children:"Create New API Key"})}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Key Name"}),l.jsx("input",{type:"text",value:R,onChange:Se=>I(Se.target.value),placeholder:"e.g., Home Assistant, OctoPrint",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"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"Permissions"}),l.jsxs("div",{className:"space-y-2",children:[l.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:L.can_read_status,onChange:Se=>q(bt=>({...bt,can_read_status:Se.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white",children:"Read Status"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"View printer status and queue"})]})]}),l.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:L.can_queue,onChange:Se=>q(bt=>({...bt,can_queue:Se.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white",children:"Manage Queue"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Add and remove items from print queue"})]})]}),l.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:L.can_control_printer,onChange:Se=>q(bt=>({...bt,can_control_printer:Se.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),l.jsxs("div",{children:[l.jsx("span",{className:"text-white",children:"Control Printer"}),l.jsx("p",{className:"text-xs text-bambu-gray",children:"Pause, resume, and stop prints"})]})]})]})]}),l.jsxs("div",{className:"flex items-center gap-2 pt-2",children:[l.jsxs(ke,{onClick:()=>O.mutate({name:R||"Unnamed Key",...L}),disabled:O.isPending,children:[O.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(ir,{className:"w-4 h-4"}),"Create Key"]}),l.jsx(ke,{variant:"secondary",onClick:()=>k(!1),children:"Cancel"})]})]})]}),U?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):Me&&Me.length>0?l.jsx("div",{className:"space-y-3",children:Me.map(Se=>l.jsx(St,{children:l.jsx(Tt,{className:"py-3",children:l.jsxs("div",{className:"flex items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-3",children:[l.jsx(Qm,{className:`w-5 h-5 ${Se.enabled?"text-bambu-green":"text-bambu-gray"}`}),l.jsxs("div",{children:[l.jsx("p",{className:"text-white font-medium",children:Se.name}),l.jsxs("p",{className:"text-xs text-bambu-gray",children:[Se.key_prefix,"••••••••",Se.last_used&&` · Last used: ${new Date(Se.last_used).toLocaleDateString()}`]})]})]}),l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsxs("div",{className:"flex gap-1 text-xs",children:[Se.can_read_status&&l.jsx("span",{className:"px-1.5 py-0.5 bg-blue-500/20 text-blue-400 rounded",children:"Read"}),Se.can_queue&&l.jsx("span",{className:"px-1.5 py-0.5 bg-green-500/20 text-green-400 rounded",children:"Queue"}),Se.can_control_printer&&l.jsx("span",{className:"px-1.5 py-0.5 bg-orange-500/20 text-orange-400 rounded",children:"Control"})]}),l.jsx(ke,{variant:"secondary",size:"sm",onClick:()=>G(Se.id),children:l.jsx(Sn,{className:"w-4 h-4 text-red-400"})})]})]})})},Se.id))}):l.jsx(St,{children:l.jsx(Tt,{className:"py-12",children:l.jsxs("div",{className:"text-center text-bambu-gray",children:[l.jsx(Qm,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),l.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No API keys"}),l.jsx("p",{className:"text-sm mb-4",children:"Create an API key to integrate with external services."}),l.jsxs(ke,{onClick:()=>k(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Create Your First Key"]})]})})}),l.jsxs(St,{className:"mt-6",children:[l.jsx(Qi,{children:l.jsx("h3",{className:"text-base font-semibold text-white",children:"Webhook Endpoints"})}),l.jsxs(Tt,{className:"space-y-3 text-sm",children:[l.jsxs("p",{className:"text-bambu-gray",children:["Use your API key in the ",l.jsx("code",{className:"text-bambu-green",children:"X-API-Key"})," header."]}),l.jsxs("div",{className:"space-y-2 font-mono text-xs",children:[l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-blue-400",children:"GET"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/status"}),l.jsx("span",{className:"text-bambu-gray",children:" - Get all printer status"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-blue-400",children:"GET"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/status/:id"}),l.jsx("span",{className:"text-bambu-gray",children:" - Get specific printer status"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-green-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/queue"}),l.jsx("span",{className:"text-bambu-gray",children:" - Add to print queue"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-orange-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/pause"}),l.jsx("span",{className:"text-bambu-gray",children:" - Pause print"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-orange-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/resume"}),l.jsx("span",{className:"text-bambu-gray",children:" - Resume print"})]}),l.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[l.jsx("span",{className:"text-red-400",children:"POST"})," ",l.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/stop"}),l.jsx("span",{className:"text-bambu-gray",children:" - Stop print"})]})]})]})]})]}),C==="virtual-printer"&&l.jsx(VGe,{}),K!==null&&l.jsx(Lr,{title:"Delete API Key",message:"Are you sure you want to delete this API key? Any integrations using this key will stop working.",confirmText:"Delete Key",variant:"danger",onConfirm:()=>{re.mutate(K),G(null)},onCancel:()=>G(null)}),c&&l.jsx(NGe,{plug:d,onClose:()=>{u(!1),f(null)}}),g&&l.jsx(PGe,{provider:x,onClose:()=>{p(!1),v(null)}}),y&&l.jsx(jGe,{template:y,onClose:()=>b(null)}),E&&l.jsx(OGe,{onClose:()=>w(!1)}),J&&l.jsx(Lr,{title:"Clear Notification Logs",message:"This will permanently delete all notification logs older than 30 days. This action cannot be undone.",confirmText:"Clear Logs",variant:"warning",onConfirm:async()=>{ee(!1);try{const Se=await Ee.clearNotificationLogs(30);r(Se.message,"success")}catch{r("Failed to clear logs","error")}},onCancel:()=>ee(!1)}),Q&&l.jsx(Lr,{title:"Reset UI Preferences",message:"This will reset all UI preferences to defaults: sidebar order, theme, dashboard layout, view modes, and sorting preferences. Your printers, archives, and server settings will NOT be affected. The page will reload after clearing.",confirmText:"Reset Preferences",variant:"default",onConfirm:()=>{B(!1),localStorage.clear(),r("UI preferences reset. Refreshing...","success"),setTimeout(()=>window.location.reload(),1e3)},onCancel:()=>B(!1)}),$&&l.jsx(Lr,{title:`Turn All Plugs ${$==="on"?"On":"Off"}`,message:`This will turn ${$==="on"?"ON":"OFF"} all ${ne?.filter(Se=>Se.enabled).length||0} enabled smart plugs. ${$==="off"?"Any running printers may be affected!":""}`,confirmText:`Turn All ${$==="on"?"On":"Off"}`,variant:$==="off"?"danger":"warning",onConfirm:()=>{const Se=$;Z(null),ze.mutate(Se)},onCancel:()=>Z(null)}),te&&l.jsx(IGe,{onClose:()=>he(!1),onExport:async Se=>{he(!1);const bt="backup-progress",Gt=Se.archives;Gt&&i(bt,e("backup.preparing",{defaultValue:"Preparing backup..."}),"loading");try{const{blob:kt,filename:Tn}=await Ee.exportBackup(Se);Gt&&a(bt);const Nn=URL.createObjectURL(kt),nn=document.createElement("a");nn.href=Nn,nn.download=Tn,nn.click(),URL.revokeObjectURL(Nn),r(e("backup.downloaded",{defaultValue:"Backup downloaded"}),"success")}catch{Gt&&a(bt),r(e("backup.failed",{defaultValue:"Failed to create backup"}),"error")}}}),de&&l.jsx(FGe,{onClose:()=>ge(!1),onRestore:async(Se,bt)=>await Ee.importBackup(Se,bt),onSuccess:()=>{o(null),_e.current=!0,t.invalidateQueries()}}),Ae&&l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:()=>me(!1),children:l.jsxs(St,{className:"w-full max-w-lg",onClick:Se=>Se.stopPropagation(),children:[l.jsxs(Qi,{className:"flex flex-row items-center justify-between",children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx(xue,{className:"w-5 h-5 text-bambu-green"}),l.jsx("h2",{className:"text-lg font-semibold text-white",children:e("settings.telemetryInfoTitle")})]}),l.jsx("button",{onClick:()=>me(!1),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs(Tt,{className:"space-y-4",children:[l.jsx("p",{className:"text-bambu-gray text-sm",children:e("settings.telemetryInfoIntro")}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("h3",{className:"text-white font-medium",children:e("settings.telemetryInfoCollected")}),l.jsxs("ul",{className:"space-y-2 text-sm",children:[l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoItem1")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoItem2")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Mr,{className:"w-4 h-4 text-bambu-green mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoItem3")})]})]})]}),l.jsxs("div",{className:"space-y-3",children:[l.jsx("h3",{className:"text-white font-medium",children:e("settings.telemetryInfoNotCollected")}),l.jsxs("ul",{className:"space-y-2 text-sm",children:[l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem1")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem2")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem3")})]}),l.jsxs("li",{className:"flex items-start gap-2 text-bambu-gray",children:[l.jsx(Ca,{className:"w-4 h-4 text-red-400 mt-0.5 shrink-0"}),l.jsx("span",{children:e("settings.telemetryInfoNotItem4")})]})]})]}),l.jsx("p",{className:"text-bambu-gray text-xs border-t border-bambu-dark-tertiary pt-4",children:e("settings.telemetryInfoFooter")}),l.jsx(ke,{onClick:()=>me(!1),className:"w-full",children:e("common.close")})]})]})}),ce&&ue?.release_notes&&l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:()=>Pe(!1),children:l.jsxs(St,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",onClick:Se=>Se.stopPropagation(),children:[l.jsxs(Qi,{className:"flex flex-row items-center justify-between shrink-0",children:[l.jsxs("div",{children:[l.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Release Notes - v",ue.latest_version]}),ue.release_name&&ue.release_name!==ue.latest_version&&l.jsx("p",{className:"text-sm text-bambu-gray",children:ue.release_name})]}),l.jsx("button",{onClick:()=>Pe(!1),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white",children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsx(Tt,{className:"overflow-y-auto flex-1",children:l.jsx("pre",{className:"text-sm text-bambu-gray whitespace-pre-wrap font-sans",children:ue.release_notes})}),l.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary shrink-0 flex gap-2",children:[ue.release_url&&l.jsx("a",{href:ue.release_url,target:"_blank",rel:"noopener noreferrer",className:"flex-1",children:l.jsxs(ke,{variant:"secondary",className:"w-full",children:[l.jsx(ga,{className:"w-4 h-4"}),"View on GitHub"]})}),l.jsx(ke,{onClick:()=>Pe(!1),className:"flex-1",children:"Close"})]})]})})]})}const jte=t=>{const e=parseFloat(t);return(Math.trunc(e*1e3)/1e3).toFixed(3)},WGe=t=>t.startsWith("HH")?"HF":"S",qGe=t=>{const e=t.match(/^([A-Z]{2}\d{2})/);return e?e[1]:"HH00"},Hj=t=>{const e=["High Flow_","High Flow ","Standard_","Standard ","HF_","HF ","S_","S "];for(const r of e)if(t.startsWith(r))return t.slice(r.length);const n=t.indexOf("_");return n>0?t.slice(n+1):t};function Sk({profile:t,onEdit:e,onCopy:n,selectionMode:r,isSelected:i,onToggleSelect:a,note:s}){const o=WGe(t.nozzle_id),c=t.nozzle_diameter,u=()=>{r&&a?a():e()};return l.jsxs("div",{className:"flex items-center gap-2",children:[r&&l.jsx("button",{onClick:a,className:"text-bambu-gray hover:text-white transition-colors p-1",children:i?l.jsx(Qh,{className:"w-4 h-4 text-bambu-green"}):l.jsx(Sp,{className:"w-4 h-4"})}),l.jsxs("button",{onClick:u,className:`flex-1 text-left px-3 py-2 bg-bambu-dark rounded hover:bg-bambu-dark-tertiary transition-colors ${i?"ring-1 ring-bambu-green":""}`,children:[l.jsxs("div",{className:"flex items-center gap-2",children:[l.jsx("span",{className:"text-bambu-green font-mono text-sm font-bold whitespace-nowrap",children:jte(t.k_value)}),l.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name||"Unnamed"}),s&&l.jsx("span",{title:"Has note",children:l.jsx(kG,{className:"w-3 h-3 text-yellow-500"})}),l.jsxs("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:[o," ",c]})]}),s&&l.jsxs("div",{className:"text-xs mt-0.5 truncate text-yellow-500/70",title:s,children:["Note: ",s.length>50?s.substring(0,50)+"...":s]})]}),!r&&n&&l.jsx("button",{onClick:d=>{d.stopPropagation(),n()},className:"text-bambu-gray hover:text-white transition-colors p-1",title:"Copy profile",children:l.jsx(md,{className:"w-4 h-4"})})]})}function Mk({profile:t,printerId:e,nozzleDiameter:n,existingProfiles:r=[],isDualNozzle:i=!1,initialNote:a="",initialNoteKey:s=null,onClose:o,onSave:c,onSaveNote:u}){const{showToast:d}=vr(),[f,g]=M.useState(t?.name||""),[p,x]=M.useState(t?.k_value?jte(t.k_value):"0.020"),[v,y]=M.useState(t?.filament_id||""),[b,E]=M.useState(t?.nozzle_id?qGe(t.nozzle_id):"HH00"),[w,T]=M.useState(t?.nozzle_diameter||n),[A,C]=M.useState(t?[t.extruder_id]:i?[0,1]:[0]),[N,F]=M.useState(!1),[k,R]=M.useState({current:0,total:0}),[I,L]=M.useState(a),q=br.useMemo(()=>{const Q=new Map;for(const B of r)if(B.filament_id&&!Q.has(B.filament_id)){const $=Hj(B.name||"");Q.set(B.filament_id,{id:B.filament_id,name:$||B.filament_id})}return Array.from(Q.values()).sort((B,$)=>B.name.localeCompare($.name))},[r]),j=_t({mutationFn:Q=>(console.log("[KProfile] Calling API..."),Ee.setKProfile(e,Q)),onSuccess:Q=>{if(console.log("[KProfile] Save success:",Q),d("K-profile saved"),u&&I!==a){let B;I===""&&s?B=s:t&&t.slot_id>0?B=t.setting_id||`slot_${t.slot_id}_${t.filament_id}_${t.extruder_id}`:B=`name_${f}_${v}`,u(B,I)}F(!0),setTimeout(()=>{F(!1),c()},2500)},onError:Q=>{console.error("[KProfile] Save error:",Q),d(Q.message,"error"),F(!1)}}),V=_t({mutationFn:Q=>(console.log("[KProfile] Deleting profile..."),Ee.deleteKProfile(e,Q)),onSuccess:Q=>{console.log("[KProfile] Delete success:",Q),d("K-profile deleted"),F(!0),setTimeout(()=>{F(!1),o()},4e3)},onError:Q=>{console.error("[KProfile] Delete error:",Q),d(Q.message,"error"),F(!1)}}),[K,G]=M.useState(!1),J=()=>{t&&V.mutate({slot_id:t.slot_id,extruder_id:t.extruder_id,nozzle_id:t.nozzle_id,nozzle_diameter:t.nozzle_diameter,filament_id:t.filament_id,setting_id:t.setting_id})},ee=async Q=>{if(Q.preventDefault(),i&&!t&&A.length===0){d("Please select at least one extruder","error");return}const B=parseFloat(p).toFixed(6),$=`${b}-${w}`;if(t||A.length===1){const te={name:f,k_value:B,filament_id:v,nozzle_id:$,nozzle_diameter:w,extruder_id:t?t.extruder_id:A[0],setting_id:t?.setting_id,slot_id:t?.slot_id??0};console.log("[KProfile] Saving profile:",te),j.mutate(te);return}F(!0),R({current:1,total:A.length});const Z=A.map(te=>({name:f,k_value:B,filament_id:v,nozzle_id:$,nozzle_diameter:w,extruder_id:te,setting_id:void 0,slot_id:0}));console.log(`[KProfile] Saving ${Z.length} profiles in batch:`,Z);try{if(await Ee.setKProfilesBatch(e,Z),d(`K-profile saved to ${A.length} extruders`),u&&I){const te=`name_${f}_${v}`;u(te,I)}}catch(te){console.error("[KProfile] Failed to save batch:",te),d("Failed to save K-profiles","error"),F(!1),R({current:0,total:0});return}R({current:A.length,total:A.length}),setTimeout(()=>{F(!1),R({current:0,total:0}),c()},3e3)};return l.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:[l.jsxs(St,{className:"w-full max-w-md relative",children:[N&&l.jsxs("div",{className:"absolute inset-0 bg-bambu-dark-secondary/90 flex flex-col items-center justify-center z-10 rounded-lg",children:[l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin mb-3"}),l.jsx("p",{className:"text-white font-medium",children:k.total>1?`Saving to extruder ${k.current}/${k.total}...`:"Syncing with printer..."}),l.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:"Please wait"})]}),l.jsxs(Tt,{className:"p-0",children:[l.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[l.jsx("h2",{className:"text-xl font-semibold text-white",children:t?"Edit K-Profile":"Add K-Profile"}),l.jsx("button",{onClick:o,className:"text-bambu-gray hover:text-white transition-colors",disabled:N,children:l.jsx(Vt,{className:"w-5 h-5"})})]}),l.jsxs("form",{onSubmit:ee,className:"p-4 space-y-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Profile Name"}),l.jsx("input",{type:"text",value:f,onChange:Q=>g(Q.target.value),disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,placeholder:"My PLA Profile",required:!t})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"K-Value"}),l.jsx("input",{type:"text",inputMode:"decimal",value:p,onChange:Q=>{const B=Q.target.value;(B===""||/^\d*\.?\d*$/.test(B))&&x(B)},onBlur:Q=>{const B=parseFloat(Q.target.value);isNaN(B)||x((Math.trunc(B*1e3)/1e3).toFixed(3))},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 font-mono",placeholder:"0.020",required:!0}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Typical range: 0.01 - 0.06 for PLA, 0.02 - 0.10 for PETG"})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Filament"}),l.jsxs("select",{value:v,onChange:Q=>{const B=Q.target.value;if(y(B),!t&&B&&!f){const $=q.find(Z=>Z.id===B);$&&g(`${b==="HH00"?"HF":"S"} ${$.name}`)}},disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,required:!t,children:[l.jsx("option",{value:"",children:"Select filament..."}),t?.filament_id&&l.jsx("option",{value:t.filament_id,children:q.find(Q=>Q.id===t.filament_id)?.name||t.filament_id},t.filament_id),!t&&q.map(Q=>l.jsx("option",{value:Q.id,children:Q.name},Q.id))]}),!t&&q.length===0&&l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"No filaments found. Create a K-profile in Bambu Studio first."})]}),l.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Flow Type"}),l.jsxs("select",{value:b,onChange:Q=>{const B=Q.target.value;if(E(B),!t&&v&&!f){const $=q.find(Z=>Z.id===v);$&&g(`${B==="HS00"?"HF":"S"} ${$.name}`)}},disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,children:[l.jsx("option",{value:"HH00",children:"High Flow"}),l.jsx("option",{value:"HS00",children:"Standard"})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle Size"}),l.jsxs("select",{value:w,onChange:Q=>T(Q.target.value),disabled:!!t,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 ${t?"opacity-60 cursor-not-allowed":""}`,children:[l.jsx("option",{value:"0.2",children:"0.2mm"}),l.jsx("option",{value:"0.4",children:"0.4mm"}),l.jsx("option",{value:"0.6",children:"0.6mm"}),l.jsx("option",{value:"0.8",children:"0.8mm"})]})]})]}),i&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:t?"Extruder":"Extruders"}),t?l.jsx("div",{className:"px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white opacity-60",children:t.extruder_id===1?"Left":"Right"}):l.jsxs("div",{className:"flex gap-4",children:[l.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:A.includes(1),onChange:Q=>{Q.target.checked?C([...A,1]):C(A.filter(B=>B!==1))},className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0 accent-bambu-green"}),l.jsx("span",{className:"text-white",children:"Left"})]}),l.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[l.jsx("input",{type:"checkbox",checked:A.includes(0),onChange:Q=>{Q.target.checked?C([...A,0]):C(A.filter(B=>B!==0))},className:"w-4 h-4 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0 accent-bambu-green"}),l.jsx("span",{className:"text-white",children:"Right"})]})]})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Notes (stored locally)"}),l.jsx("textarea",{value:I,onChange:Q=>L(Q.target.value),placeholder:"Add notes about this profile...",rows:2,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none resize-none"}),l.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Notes are saved in Bambuddy, not on the printer"})]}),l.jsxs("div",{className:"flex gap-2 pt-4",children:[t&&l.jsx(ke,{type:"button",variant:"secondary",onClick:()=>G(!0),disabled:V.isPending||N,className:"text-red-500 hover:bg-red-500/10",children:V.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"})}),l.jsx(ke,{type:"button",variant:"secondary",onClick:o,disabled:N,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{type:"submit",disabled:j.isPending||N,className:"flex-1",children:[j.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(XS,{className:"w-4 h-4"}),"Save"]})]})]})]})]}),K&&l.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-[60]",children:l.jsx(St,{className:"w-full max-w-sm",children:l.jsxs(Tt,{className:"p-6",children:[l.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[l.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:l.jsx(Sn,{className:"w-5 h-5 text-red-500"})}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profile"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),l.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",l.jsxs("span",{className:"text-white font-medium",children:['"',t?.name,'"']})," from the printer?"]}),l.jsxs("div",{className:"flex gap-3",children:[l.jsx(ke,{variant:"secondary",onClick:()=>G(!1),className:"flex-1",children:"Cancel"}),l.jsxs(ke,{onClick:()=>{G(!1),J()},disabled:V.isPending,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[V.isPending?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}const i1={NOZZLE_DIAMETER:"bambusy_kprofiles_nozzle",SORT_OPTION:"bambusy_kprofiles_sort"};function KGe(){const{showToast:t}=vr(),[e,n]=M.useState(null),[r,i]=M.useState(()=>localStorage.getItem(i1.NOZZLE_DIAMETER)||"0.4"),[a,s]=M.useState(null),[o,c]=M.useState(!1),[u,d]=M.useState(null),[f,g]=M.useState(""),[p,x]=M.useState("all"),[v,y]=M.useState("all"),[b,E]=M.useState(()=>localStorage.getItem(i1.SORT_OPTION)||"name"),[w,T]=M.useState(!1),[A,C]=M.useState(new Set),[N,F]=M.useState(!1),[k,R]=M.useState(!1),I=W=>`${W.slot_id}_${W.extruder_id}`;M.useEffect(()=>{localStorage.setItem(i1.NOZZLE_DIAMETER,r)},[r]),M.useEffect(()=>{localStorage.setItem(i1.SORT_OPTION,b)},[b]);const{data:L,isLoading:q}=xt({queryKey:["printers"],queryFn:Ee.getPrinters}),{data:j,isLoading:V,isFetching:K,error:G,refetch:J}=xt({queryKey:["kprofiles",e,r],queryFn:async()=>{console.log("[KProfiles] Fetching profiles for printer",e,"nozzle",r);const W=await Ee.getKProfiles(e,r);return console.log("[KProfiles] Received profiles:",W?.profiles?.length||0,"profiles"),W},enabled:!!e,retry:!1,staleTime:0,gcTime:0,refetchOnMount:"always"}),{data:ee}=xt({queryKey:["kprofiles",e,"0.4"],queryFn:()=>Ee.getKProfiles(e,"0.4"),enabled:!!e,staleTime:6e4}),{data:Q,refetch:B}=xt({queryKey:["kprofile-notes",e],queryFn:()=>Ee.getKProfileNotes(e),enabled:!!e,staleTime:3e4}),$=G?.message?.includes("not connected");M.useEffect(()=>{if(!e&&L&&L.length>0){const W=L.find(Y=>Y.is_active);W&&n(W.id)}},[e,L]),M.useEffect(()=>{if(e){const W=setTimeout(()=>{J()},150);return()=>clearTimeout(W)}},[e,r]);const Z=L?.filter(W=>W.is_active)||[],te=br.useMemo(()=>j?.profiles?j.profiles.filter(Y=>{const se=f.toLowerCase(),be=!se||Y.name.toLowerCase().includes(se)||Y.filament_id.toLowerCase().includes(se),Te=p==="all"||p==="left"&&Y.extruder_id===1||p==="right"&&Y.extruder_id===0,Me=v==="all"||v==="hf"&&Y.nozzle_id.startsWith("HH")||v==="s"&&Y.nozzle_id.startsWith("HS");return be&&Te&&Me}).sort((Y,se)=>{switch(b){case"k_value":return parseFloat(Y.k_value)-parseFloat(se.k_value);case"filament":return Hj(Y.name).localeCompare(Hj(se.name));case"name":default:return Y.name.localeCompare(se.name)}}):[],[j?.profiles,f,p,v,b]),he=L?.find(W=>W.id===e),de=he?.nozzle_count===2;M.useEffect(()=>{const W=Y=>{Y.target instanceof HTMLInputElement||Y.target instanceof HTMLTextAreaElement||Y.target instanceof HTMLSelectElement||a||o||u||(Y.key==="r"||Y.key==="R"?(Y.preventDefault(),J()):Y.key==="n"||Y.key==="N"?(Y.preventDefault(),c(!0)):Y.key==="Escape"&&w&&(Y.preventDefault(),T(!1),C(new Set)))};return window.addEventListener("keydown",W),()=>window.removeEventListener("keydown",W)},[a,o,u,w,J]);const ge=M.useCallback(()=>{if(!j?.profiles||j.profiles.length===0){t("No profiles to export","error");return}const W={version:1,exported_at:new Date().toISOString(),printer:he?.name||"Unknown",nozzle_diameter:r,profiles:j.profiles.map(Te=>({name:Te.name,k_value:Te.k_value,filament_id:Te.filament_id,nozzle_id:Te.nozzle_id,nozzle_diameter:Te.nozzle_diameter,extruder_id:Te.extruder_id}))},Y=new Blob([JSON.stringify(W,null,2)],{type:"application/json"}),se=URL.createObjectURL(Y),be=document.createElement("a");be.href=se,be.download=`kprofiles_${he?.name||"printer"}_${r}mm_${new Date().toISOString().split("T")[0]}.json`,document.body.appendChild(be),be.click(),document.body.removeChild(be),URL.revokeObjectURL(se),t(`Exported ${j.profiles.length} profiles`)},[j?.profiles,he,r,t]),Ae=M.useCallback(()=>{const W=document.createElement("input");W.type="file",W.accept=".json",W.onchange=async Y=>{const se=Y.target.files?.[0];if(se)try{const be=await se.text(),Te=JSON.parse(be);if(!Te.profiles||!Array.isArray(Te.profiles)){t("Invalid file format","error");return}let Me=0;for(const U of Te.profiles)if(!(!U.name||!U.k_value||!U.filament_id))try{await Ee.setKProfile(e,{name:U.name,k_value:parseFloat(U.k_value).toFixed(6),filament_id:U.filament_id,nozzle_id:U.nozzle_id||`HH00-${r}`,nozzle_diameter:U.nozzle_diameter||r,extruder_id:U.extruder_id??0,slot_id:0}),Me++,await new Promise(O=>setTimeout(O,500))}catch(O){console.error("Failed to import profile:",U.name,O)}t(`Imported ${Me} of ${Te.profiles.length} profiles`),J()}catch(be){console.error("Import error:",be),t("Failed to parse import file","error")}},W.click()},[e,r,t,J]),me=M.useCallback(W=>{C(Y=>{const se=new Set(Y);return se.has(W)?se.delete(W):se.add(W),se})},[]),ce=M.useCallback(()=>{C(new Set(te.map(W=>I(W))))},[te,I]),Pe=M.useCallback(()=>{A.size!==0&&F(!0)},[A.size]),oe=M.useCallback(async()=>{const W=te.filter(se=>A.has(I(se)));R(!0);let Y=0;for(const se of W)try{await Ee.deleteKProfile(e,{slot_id:se.slot_id,extruder_id:se.extruder_id,nozzle_id:se.nozzle_id,nozzle_diameter:se.nozzle_diameter,filament_id:se.filament_id,setting_id:se.setting_id}),Y++,await new Promise(be=>setTimeout(be,300))}catch(be){console.error("Failed to delete profile:",se.name,be)}t(`Deleted ${Y} profiles`),R(!1),F(!1),T(!1),C(new Set),J()},[e,A,te,t,J,I]),Le=M.useCallback(W=>{const Y=[];return W.setting_id&&Y.push(W.setting_id),Y.push(`slot_${W.slot_id}_${W.filament_id}_${W.extruder_id}`),Y.push(`name_${W.name}_${W.filament_id}`),Y},[]),z=M.useCallback(async(W,Y)=>{if(e)try{await Ee.setKProfileNote(e,W,Y),B()}catch(se){console.error("Failed to save note:",se),t("Failed to save note","error")}},[e,B,t]),H=M.useCallback(W=>{if(!Q?.notes)return{note:"",key:null};const Y=Le(W);for(const se of Y)if(Q.notes[se])return{note:Q.notes[se],key:se};return{note:"",key:null}},[Q,Le]),ne=M.useCallback(W=>H(W).note,[H]);return q?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):!L||L.length===0?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(ho,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Printers Configured"}),l.jsx("p",{className:"text-bambu-gray",children:"Add a printer in Settings to manage K-profiles"})]})}):Z.length===0?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(Ui,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Active Printers"}),l.jsx("p",{className:"text-bambu-gray",children:"Enable a printer connection to view its K-profiles"})]})}):l.jsxs(l.Fragment,{children:[K&&!V&&l.jsxs("div",{className:"fixed inset-0 bg-black/50 flex flex-col items-center justify-center z-40",children:[l.jsx(gt,{className:"w-10 h-10 text-bambu-green animate-spin mb-3"}),l.jsx("p",{className:"text-white font-medium",children:"Loading K-Profiles..."})]}),l.jsxs("div",{className:"flex flex-wrap gap-4 mb-6",children:[l.jsxs("div",{className:"flex-1 min-w-48",children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer"}),l.jsx("select",{value:e||"",onChange:W=>n(parseInt(W.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",children:Z.map(W=>l.jsx("option",{value:W.id,children:W.name},W.id))})]}),l.jsxs("div",{className:"w-32",children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle"}),l.jsxs("select",{value:r,onChange:W=>i(W.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",children:[l.jsx("option",{value:"0.2",children:"0.2mm"}),l.jsx("option",{value:"0.4",children:"0.4mm"}),l.jsx("option",{value:"0.6",children:"0.6mm"}),l.jsx("option",{value:"0.8",children:"0.8mm"})]})]}),l.jsxs("div",{className:"flex items-end gap-2",children:[l.jsxs(ke,{variant:"secondary",onClick:()=>J(),disabled:K,children:[l.jsx(ya,{className:`w-4 h-4 ${K?"animate-spin":""}`}),"Refresh"]}),l.jsxs(ke,{onClick:()=>c(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Add Profile"]})]})]}),l.jsxs("div",{className:"flex flex-wrap gap-4 mb-4",children:[l.jsxs("div",{className:"flex-1 min-w-48 relative",children:[l.jsx(xl,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),l.jsx("input",{type:"text",value:f,onChange:W=>g(W.target.value),placeholder:"Search by name or filament...",className:"w-full pl-10 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"})]}),de&&l.jsx("div",{className:"w-36",children:l.jsxs("select",{value:p,onChange:W=>x(W.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",children:[l.jsx("option",{value:"all",children:"All Extruders"}),l.jsx("option",{value:"left",children:"Left Only"}),l.jsx("option",{value:"right",children:"Right Only"})]})}),l.jsx("div",{className:"w-32",children:l.jsxs("select",{value:v,onChange:W=>y(W.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",children:[l.jsx("option",{value:"all",children:"All Flow"}),l.jsx("option",{value:"hf",children:"HF Only"}),l.jsx("option",{value:"s",children:"S Only"})]})}),l.jsx("div",{className:"w-32",children:l.jsxs("select",{value:b,onChange:W=>E(W.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",children:[l.jsx("option",{value:"name",children:"Sort: Name"}),l.jsx("option",{value:"k_value",children:"Sort: K-Value"}),l.jsx("option",{value:"filament",children:"Sort: Filament"})]})})]}),l.jsxs("div",{className:"flex flex-wrap gap-2 mb-6",children:[l.jsxs(ke,{variant:"secondary",onClick:ge,disabled:!j?.profiles?.length,title:"Export profiles to JSON",children:[l.jsx(ns,{className:"w-4 h-4"}),"Export"]}),l.jsxs(ke,{variant:"secondary",onClick:Ae,title:"Import profiles from JSON",children:[l.jsx(ia,{className:"w-4 h-4"}),"Import"]}),l.jsx("div",{className:"flex-1"}),w?l.jsxs(l.Fragment,{children:[l.jsxs(ke,{variant:"secondary",onClick:ce,title:"Select all visible profiles",children:[l.jsx(Qh,{className:"w-4 h-4"}),"Select All"]}),l.jsxs(ke,{variant:"secondary",onClick:Pe,disabled:A.size===0,className:"text-red-500 hover:bg-red-500/10",title:`Delete ${A.size} selected profiles`,children:[l.jsx(Sn,{className:"w-4 h-4"}),"Delete (",A.size,")"]}),l.jsxs(ke,{variant:"secondary",onClick:()=>{T(!1),C(new Set)},children:[l.jsx(Vt,{className:"w-4 h-4"}),"Cancel"]})]}):l.jsxs(ke,{variant:"secondary",onClick:()=>T(!0),disabled:!te.length,title:"Enter selection mode for bulk delete",children:[l.jsx(Qh,{className:"w-4 h-4"}),"Select"]})]}),V?l.jsx("div",{className:"flex justify-center py-12",children:l.jsx(gt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):$?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(Mb,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"Printer Offline"}),l.jsx("p",{className:"text-bambu-gray mb-4",children:"The selected printer is not connected. Power it on to view K-profiles."}),l.jsxs(ke,{variant:"secondary",onClick:()=>J(),children:[l.jsx(ya,{className:"w-4 h-4"}),"Retry"]})]})}):te.length>0?de?l.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[l.jsxs("div",{children:[l.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Left Extruder"}),l.jsx("div",{className:"space-y-1",children:te.filter(W=>W.extruder_id===1).map(W=>l.jsx(Sk,{profile:W,onEdit:()=>s(W),onCopy:()=>d(W),selectionMode:w,isSelected:A.has(I(W)),onToggleSelect:()=>me(I(W)),note:ne(W)},I(W)))})]}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Right Extruder"}),l.jsx("div",{className:"space-y-1",children:te.filter(W=>W.extruder_id===0).map(W=>l.jsx(Sk,{profile:W,onEdit:()=>s(W),onCopy:()=>d(W),selectionMode:w,isSelected:A.has(I(W)),onToggleSelect:()=>me(I(W)),note:ne(W)},I(W)))})]})]}):l.jsx("div",{className:"space-y-1",children:te.map(W=>l.jsx(Sk,{profile:W,onEdit:()=>s(W),onCopy:()=>d(W),selectionMode:w,isSelected:A.has(I(W)),onToggleSelect:()=>me(I(W)),note:ne(W)},I(W)))}):f||p!=="all"||v!=="all"?l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(xl,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Matching Profiles"}),l.jsx("p",{className:"text-bambu-gray",children:"No profiles match your search criteria"})]})}):l.jsx(St,{children:l.jsxs(Tt,{className:"py-12 text-center",children:[l.jsx(XS,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),l.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No K-Profiles"}),l.jsxs("p",{className:"text-bambu-gray mb-4",children:["No pressure advance profiles found for ",r,"mm nozzle"]}),l.jsxs(ke,{onClick:()=>c(!0),children:[l.jsx(ir,{className:"w-4 h-4"}),"Create First Profile"]})]})}),a&&e&&(()=>{const{note:W,key:Y}=H(a);return l.jsx(Mk,{profile:a,printerId:e,nozzleDiameter:r,existingProfiles:ee?.profiles||j?.profiles,isDualNozzle:de,initialNote:W,initialNoteKey:Y,onSaveNote:z,onClose:()=>{console.log("[KProfiles] Edit modal onClose - refetching profiles..."),s(null),J()},onSave:()=>{s(null),J()}})})(),o&&e&&l.jsx(Mk,{printerId:e,nozzleDiameter:r,existingProfiles:ee?.profiles||j?.profiles,isDualNozzle:de,onSaveNote:z,onClose:()=>{c(!1),J()},onSave:()=>{c(!1),J()}}),u&&e&&l.jsx(Mk,{printerId:e,nozzleDiameter:r,existingProfiles:ee?.profiles||j?.profiles,isDualNozzle:de,onSaveNote:z,profile:{...u,slot_id:0,name:`${u.name} (Copy)`},onClose:()=>{d(null),J()},onSave:()=>{d(null),J()}}),N&&l.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:l.jsx(St,{className:"w-full max-w-sm",children:l.jsxs(Tt,{className:"p-6",children:[l.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[l.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:l.jsx(Sn,{className:"w-5 h-5 text-red-500"})}),l.jsxs("div",{children:[l.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profiles"}),l.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),l.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",l.jsx("span",{className:"text-white font-medium",children:A.size})," selected profiles from the printer?"]}),l.jsxs("div",{className:"flex gap-3",children:[l.jsx(ke,{variant:"secondary",onClick:()=>F(!1),disabled:k,className:"flex-1",children:"Cancel"}),l.jsxs(ke,{onClick:oe,disabled:k,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[k?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(Sn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}function OL(t,e){const n=`${t} ${e||""}`,r=n.match(/@?\s*(?:BBL\s+)?(?:Bambu\s+Lab\s+)?([XPAH][1-9][A-Z]?(?:\s*(?:Carbon|mini))?|H2D)/i),i=r?r[1].trim():null,a=n.match(/(\d+\.?\d*)\s*(?:mm\s*)?nozzle|nozzle\s*(\d+\.?\d*)/i),s=a?(a[1]||a[2])+"mm":null,o=n.match(/(\d+\.?\d*)mm\s*(?:Standard|Fine|Extra Fine|Draft|Quality)?/i),c=o?o[1]+"mm":null,u=n.match(/\b(PLA|PETG|ABS|ASA|TPU|PC|PA|PVA|HIPS|PP|PET(?:-?CF)?|PA(?:-?CF)?|PLA(?:-?CF)?)\b/i),d=u?u[1].toUpperCase():null;return{printer:i,nozzle:s,layerHeight:c,filamentType:d}}function gb(t){return/^(P[FPM]US|PF\d|PP\d)/.test(t)}function XGe(t){const e=new Date(t),r=new Date().getTime()-e.getTime(),i=Math.floor(r/6e4),a=Math.floor(r/36e5),s=Math.floor(r/864e5);return i<1?"Just now":i<60?`${i}m ago`:a<24?`${a}h ago`:s<7?`${s}d ago`:e.toLocaleDateString()}function YGe({onSuccess:t}){const{showToast:e}=vr(),[n,r]=M.useState("email"),[i,a]=M.useState(""),[s,o]=M.useState(""),[c,u]=M.useState(""),[d,f]=M.useState(""),[g,p]=M.useState("global"),x=_t({mutationFn:()=>Ee.cloudLogin(i,s,g),onSuccess:w=>{w.success?(e("Logged in successfully"),t()):w.needs_verification?(e("Verification code sent to your email"),r("code")):e(w.message,"error")},onError:w=>e(w.message,"error")}),v=_t({mutationFn:()=>Ee.cloudVerify(i,c),onSuccess:w=>{w.success?(e("Logged in successfully"),t()):e(w.message,"error")},onError:w=>e(w.message,"error")}),y=_t({mutationFn:()=>Ee.cloudSetToken(d),onSuccess:()=>{e("Token set successfully"),t()},onError:w=>e(w.message,"error")}),b=w=>{w.preventDefault(),n==="email"?x.mutate():n==="code"?v.mutate():n==="token"&&y.mutate()},E=x.isPending||v.isPending||y.isPending;return l.jsx(St,{className:"max-w-md mx-auto",children:l.jsxs(Tt,{children:[l.jsxs("div",{className:"text-center mb-6",children:[l.jsx("div",{className:"inline-flex items-center justify-center w-12 h-12 rounded-xl bg-bambu-green/20 mb-3",children:l.jsx(qS,{className:"w-6 h-6 text-bambu-green"})}),l.jsx("h2",{className:"text-xl font-semibold text-white",children:"Connect to Bambu Cloud"}),l.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Sync your slicer presets across devices"})]}),l.jsxs("form",{onSubmit:b,className:"space-y-4",children:[n==="email"&&l.jsxs(l.Fragment,{children:[l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Email"}),l.jsx("input",{type:"email",value:i,onChange:w=>a(w.target.value),className:"w-full px-3 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",placeholder:"your@email.com",required:!0})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),l.jsx("input",{type:"password",value:s,onChange:w=>o(w.target.value),className:"w-full px-3 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",placeholder:"••••••••",required:!0})]}),l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Region"}),l.jsxs("select",{value:g,onChange:w=>p(w.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",children:[l.jsx("option",{value:"global",children:"Global"}),l.jsx("option",{value:"china",children:"China"})]})]})]}),n==="code"&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Verification Code"}),l.jsxs("p",{className:"text-xs text-bambu-gray mb-2",children:["Check your email (",i,") for a 6-digit code"]}),l.jsx("input",{type:"text",value:c,onChange:w=>u(w.target.value.replace(/\D/g,"").slice(0,6)),className:"w-full px-3 py-3 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-center text-2xl tracking-widest font-mono focus:border-bambu-green focus:outline-none",placeholder:"000000",maxLength:6,required:!0})]}),n==="token"&&l.jsxs("div",{children:[l.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Access Token"}),l.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Paste your Bambu Lab access token (from Bambu Studio)"}),l.jsx("textarea",{value:d,onChange:w=>f(w.target.value),className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-xs font-mono placeholder-bambu-gray-dark focus:border-bambu-green focus:outline-none resize-none",placeholder:"eyJ...",rows:4,required:!0})]}),l.jsxs("div",{className:"flex gap-2",children:[n==="code"&&l.jsx(ke,{type:"button",variant:"secondary",onClick:()=>r("email"),className:"flex-1",children:"Back"}),l.jsxs(ke,{type:"submit",disabled:E,className:"flex-1",children:[E?l.jsx(gt,{className:"w-4 h-4 animate-spin"}):l.jsx(mU,{className:"w-4 h-4"}),n==="email"?"Login":n==="code"?"Verify":"Set Token"]})]}),n==="email"&&l.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:l.jsxs("button",{type:"button",onClick:()=>r("token"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[l.jsx(Qm,{className:"w-4 h-4"}),"Use access token instead"]})}),n==="token"&&l.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:l.jsxs("button",{type:"button",onClick:()=>r("email"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[l.jsx(mU,{className:"w-4 h-4"}),"Login with email instead"]})})]})]})})}function Vm({label:t,value:e,options:n,onChange:r}){const[i,a]=M.useState(!1),s=n.find(o=>o.value===e);return l.jsxs("div",{className:"relative",children:[l.jsxs("button",{onClick:()=>a(!i),className:"flex items-center gap-2 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-sm text-white hover:border-bambu-gray-dark transition-colors",children:[l.jsxs("span",{className:"text-bambu-gray",children:[t,":"]}),l.jsx("span",{children:s?.label||"All"}),l.jsx(Nl,{className:`w-4 h-4 text-bambu-gray transition-transform ${i?"rotate-180":""}`})]}),i&&l.jsxs(l.Fragment,{children:[l.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>a(!1)}),l.jsx("div",{className:"absolute top-full left-0 mt-1 min-w-[160px] bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl z-20 py-1 max-h-60 overflow-y-auto",children:n.map(o=>l.jsxs("button",{onClick:()=>{r(o.value),a(!1)},className:`w-full px-3 py-2 text-left text-sm flex items-center justify-between hover:bg-bambu-dark-tertiary transition-colors ${e===o.value?"text-bambu-green":"text-white"}`,children:[l.jsx("span",{children:o.label}),o.count!==void 0&&l.jsx("span",{className:"text-bambu-gray text-xs",children:o.count})]},o.value))})]})]})}function ZGe(){const[t,e]=M.useState(!1);M.useEffect(()=>{const r=()=>{e(window.scrollY>300)};return window.addEventListener("scroll",r),()=>window.removeEventListener("scroll",r)},[]);const n=()=>{window.scrollTo({top:0,behavior:"smooth"})};return t?l.jsx("button",{onClick:n,className:"fixed bottom-6 right-6 p-3 bg-bambu-green hover:bg-bambu-green-light text-white rounded-full shadow-lg shadow-bambu-green/25 transition-all z-40","aria-label":"Scroll to top",children:l.jsx(bp,{className:"w-5 h-5"})}):null}function Ek({setting:t,onClick:e,onDuplicate:n,compareMode:r,isCompareSelected:i,compareIndex:a,compareDisabled:s}){const o=OL(t.name),c=gb(t.setting_id);return l.jsxs("div",{className:"flex items-center gap-2 group",children:[l.jsx("button",{onClick:e,disabled:s,className:`flex-1 text-left px-3 py-2 rounded transition-colors ${i?"bg-blue-500/20 border border-blue-500/50":s?"bg-bambu-dark/50 opacity-40 cursor-not-allowed":"bg-bambu-dark hover:bg-bambu-dark-tertiary"} ${r&&!s?"cursor-pointer":""}`,children:l.jsxs("div",{className:"flex items-center gap-2",children:[i&&a!==void 0&&l.jsx("span",{className:"flex-shrink-0 w-5 h-5 rounded-full bg-blue-500 text-white text-xs flex items-center justify-center font-medium",children:a+1}),!i&&c&&l.jsx("span",{className:"flex-shrink-0 w-1.5 h-1.5 rounded-full bg-bambu-green",title:"My preset (editable)"}),l.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name}),o.filamentType&&t.type==="filament"&&l.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:o.filamentType}),o.layerHeight&&t.type==="process"&&l.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:o.layerHeight}),o.printer&&l.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:o.printer})]})}),l.jsx("button",{onClick:u=>{u.stopPropagation(),n()},className:"opacity-0 group-hover:opacity-100 text-bambu-gray hover:text-white transition-all p-1",title:"Duplicate",children:l.jsx(md,{className:"w-4 h-4"})})]})}function $j(t,e=0){const n=" ".repeat(e);if(t===null)return"null";if(t===void 0)return"undefined";if(typeof t=="string"){if(t.includes("\\n")||t.includes(` `)){const i=t.replace(/\\n/g,` `).replace(/\\"/g,'"').replace(/\\t/g," ").split(` `);if(i.length>1)return`""" diff --git a/static/index.html b/static/index.html index 54fec8121..37777afa9 100644 --- a/static/index.html +++ b/static/index.html @@ -23,7 +23,7 @@ - +