diff --git a/static/assets/index-CJ-thy_3.js b/static/assets/index-B9U7ouPO.js similarity index 97% rename from static/assets/index-CJ-thy_3.js rename to static/assets/index-B9U7ouPO.js index ca1a72963..11fdf1396 100644 --- a/static/assets/index-CJ-thy_3.js +++ b/static/assets/index-B9U7ouPO.js @@ -8712,7 +8712,7 @@ Accuracy: ${t.time_accuracy.toFixed(0)}%`:t.actual_time_seconds?`Actual: ${Fb(t. transition-all duration-200 hover:border-bambu-dark-tertiary/80 ${x?"opacity-50 scale-[1.02] shadow-xl z-50":""} ${S?"border-blue-500/30 bg-gradient-to-r from-blue-500/5 to-transparent":""} - `,children:s.jsxs("div",{className:"flex items-center gap-4 p-4",children:[M&&d&&s.jsx("button",{onClick:T=>{T.stopPropagation(),d()},className:`flex items-center justify-center w-6 h-6 rounded border transition-colors ${u?"bg-bambu-green border-bambu-green text-white":"border-white/30 bg-black/30 hover:border-bambu-green/50"}`,children:u&&s.jsx(ns,{className:"w-4 h-4"})}),M?s.jsx("div",{...f,...m,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:s.jsx(yg,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}):e!==void 0?s.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]}):s.jsx("div",{className:"w-8"}),s.jsx("div",{className:"w-14 h-14 flex-shrink-0 bg-bambu-dark rounded-lg overflow-hidden",children:t.archive_thumbnail?s.jsx("img",{src:he.getArchiveThumbnail(t.archive_id),alt:"",className:"w-full h-full object-cover"}):t.library_file_thumbnail?s.jsx("img",{src:he.getLibraryFileThumbnailUrl(t.library_file_id),alt:"",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:s.jsx(ul,{className:"w-6 h-6"})})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[s.jsx("p",{className:"text-white font-medium truncate",children:t.archive_name||t.library_file_name||`File #${t.archive_id||t.library_file_id}`}),t.archive_id?s.jsx(bs,{to:`/archives?highlight=${t.archive_id}`,className:"text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0",title:"View archive",children:s.jsx(zr,{className:"w-3.5 h-3.5"})}):t.library_file_id?s.jsx(bs,{to:`/library?highlight=${t.library_file_id}`,className:"text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0",title:"View in File Manager",children:s.jsx(zr,{className:"w-3.5 h-3.5"})}):null]}),s.jsxs("div",{className:"flex items-center gap-3 text-sm text-bambu-gray",children:[s.jsxs("span",{className:`flex items-center gap-1.5 ${t.printer_id===null?"text-orange-400":""}`,children:[s.jsx(qr,{className:"w-3.5 h-3.5"}),t.printer_id===null?"Unassigned":t.printer_name||`Printer #${t.printer_id}`]}),t.print_time_seconds&&s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx(jd,{className:"w-3.5 h-3.5"}),Vie(t.print_time_seconds)]}),M&&!t.manual_start&&s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx(Zn,{className:"w-3.5 h-3.5"}),dYe(t.scheduled_time,c)]})]}),s.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[t.manual_start&&s.jsxs("span",{className:"text-xs px-2 py-0.5 bg-purple-500/10 text-purple-400 rounded-full border border-purple-500/20 flex items-center gap-1",children:[s.jsx(yK,{className:"w-3 h-3"}),"Staged"]}),t.require_previous_success&&s.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&&s.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:[s.jsx(Eu,{className:"w-3 h-3"}),"Auto power off"]})]}),S&&s.jsxs("div",{className:"mt-3",children:[s.jsx("div",{className:"h-2 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:"h-full bg-gradient-to-r from-blue-500 to-blue-400 animate-pulse w-full opacity-50"})}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Printing in progress..."})]}),t.error_message&&s.jsxs("p",{className:"text-xs text-red-400 mt-2 flex items-center gap-1",children:[s.jsx(oa,{className:"w-3 h-3"}),t.error_message]})]}),s.jsx(hYe,{status:t.status}),s.jsxs("div",{className:"flex items-center gap-1",children:[S&&s.jsx(Se,{variant:"ghost",size:"sm",onClick:a,title:"Stop Print",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:s.jsx(gde,{className:"w-4 h-4"})}),M&&s.jsxs(s.Fragment,{children:[t.manual_start&&s.jsx(Se,{variant:"ghost",size:"sm",onClick:l,title:"Start Print",className:"text-bambu-green hover:text-bambu-green-light hover:bg-bambu-green/10",children:s.jsx(Oo,{className:"w-4 h-4"})}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:n,title:"Edit",children:s.jsx(la,{className:"w-4 h-4"})}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:r,title:"Cancel",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:s.jsx(Vt,{className:"w-4 h-4"})})]}),A&&s.jsxs(s.Fragment,{children:[s.jsx(Se,{variant:"ghost",size:"sm",onClick:o,title:"Re-queue",className:"text-bambu-green hover:text-bambu-green/80 hover:bg-bambu-green/10",children:s.jsx(xr,{className:"w-4 h-4"})}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:i,title:"Remove",children:s.jsx(dn,{className:"w-4 h-4"})})]})]})]})})}function mYe(){const t=on(),{showToast:e}=qn(),[n,r]=w.useState(null),[i,a]=w.useState(""),[o,l]=w.useState(!1),[c,u]=w.useState(null),[d,f]=w.useState(null),[m,p]=w.useState(null),[b,y]=w.useState([]),[x,v]=w.useState(!1),[S,M]=w.useState(()=>localStorage.getItem("queue.historySortBy")||"date"),[A,T]=w.useState(()=>{const ye=localStorage.getItem("queue.historySortAsc");return ye!==null?ye==="true":!1}),[k,E]=w.useState(()=>localStorage.getItem("queue.pendingSortBy")||"position"),[O,C]=w.useState(()=>{const ye=localStorage.getItem("queue.pendingSortAsc");return ye!==null?ye==="true":!0});w.useEffect(()=>{localStorage.setItem("queue.historySortBy",S)},[S]),w.useEffect(()=>{localStorage.setItem("queue.historySortAsc",String(A))},[A]),w.useEffect(()=>{localStorage.setItem("queue.pendingSortBy",k)},[k]),w.useEffect(()=>{localStorage.setItem("queue.pendingSortAsc",String(O))},[O]);const D=yie(YM(UE,{activationConstraint:{distance:8}}),YM(FE,{coordinateGetter:$ie})),{data:U}=ut({queryKey:["settings"],queryFn:he.getSettings}),L=U?.time_format||"system",{data:q,isLoading:P}=ut({queryKey:["queue",n,i],queryFn:()=>he.getQueue(n||void 0,i||void 0),refetchInterval:5e3}),{data:W}=ut({queryKey:["printers"],queryFn:()=>he.getPrinters()}),K=mt({mutationFn:ye=>he.cancelQueueItem(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item cancelled")},onError:()=>e("Failed to cancel item","error")}),V=mt({mutationFn:ye=>he.removeFromQueue(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item removed")},onError:()=>e("Failed to remove item","error")}),Q=mt({mutationFn:ye=>he.stopQueueItem(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Print stopped")},onError:()=>e("Failed to stop print","error")}),re=mt({mutationFn:ye=>he.startQueueItem(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Print released to queue")},onError:()=>e("Failed to start print","error")}),J=mt({mutationFn:ye=>he.reorderQueue(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]})},onError:()=>e("Failed to reorder queue","error")}),z=mt({mutationFn:async()=>{const ye=q?.filter(le=>["completed","failed","skipped","cancelled"].includes(le.status))||[];for(const le of ye)await he.removeFromQueue(le.id);return ye.length},onSuccess:ye=>{t.invalidateQueries({queryKey:["queue"]}),e(`Cleared ${ye} history item${ye!==1?"s":""}`)},onError:()=>e("Failed to clear history","error")}),G=mt({mutationFn:ye=>he.bulkUpdateQueue(ye),onSuccess:ye=>{t.invalidateQueries({queryKey:["queue"]}),y([]),v(!1),e(ye.message)},onError:()=>e("Failed to update items","error")}),ne=mt({mutationFn:async ye=>{for(const le of ye)await he.cancelQueueItem(le);return ye.length},onSuccess:ye=>{t.invalidateQueries({queryKey:["queue"]}),y([]),e(`Cancelled ${ye} item${ye!==1?"s":""}`)},onError:()=>e("Failed to cancel items","error")}),ae=ye=>{y(le=>le.includes(ye)?le.filter($e=>$e!==ye):[...le,ye])},de=w.useMemo(()=>{const ye=q?.filter($e=>$e.status==="pending")||[],le=$e=>{if(!$e.scheduled_time)return 0;const H=new Date($e.scheduled_time).getTime(),ee=Date.now()+4320*60*60*1e3;return H>ee?0:H};return[...ye].sort(($e,H)=>{let ee;if(k==="name"){const X=$e.archive_name||$e.library_file_name||"",B=H.archive_name||H.library_file_name||"";ee=X.localeCompare(B)}else k==="printer"?ee=($e.printer_name||"").localeCompare(H.printer_name||""):k==="time"?ee=le($e)-le(H):ee=$e.position-H.position;return O?ee:-ee})},[q,k,O]),fe=()=>{const ye=de.map(le=>le.id);b.length===ye.length?y([]):y(ye)},ie=q?.filter(ye=>ye.status==="printing")||[],Ae=w.useMemo(()=>[...q?.filter(le=>["completed","failed","skipped","cancelled"].includes(le.status))||[]].sort((le,$e)=>{let H;if(S==="name"){const ee=le.archive_name||le.library_file_name||"",X=$e.archive_name||$e.library_file_name||"";H=ee.localeCompare(X)}else S==="printer"?H=(le.printer_name||"").localeCompare($e.printer_name||""):H=new Date($e.completed_at||$e.created_at).getTime()-new Date(le.completed_at||le.created_at).getTime();return A?-H:H}),[q,S,A]),me=w.useMemo(()=>de.reduce((ye,le)=>ye+(le.print_time_seconds||0),0),[de]),Ne=ye=>{const{active:le,over:$e}=ye;if(!$e||le.id===$e.id)return;const H=de.findIndex(X=>X.id===le.id),ee=de.findIndex(X=>X.id===$e.id);if(H!==-1&&ee!==-1){const B=$E(de,H,ee).map((I,ce)=>({id:I.id,position:ce+1}));J.mutate(B)}};return s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsx("div",{className:"flex items-center justify-between mb-8",children:s.jsxs("div",{children:[s.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[s.jsx(vK,{className:"w-7 h-7 text-bambu-green"}),"Print Queue"]}),s.jsx("p",{className:"text-bambu-gray mt-1",children:"Schedule and manage your print jobs"})]})}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4 mb-8",children:[s.jsx(St,{className:"bg-gradient-to-br from-blue-500/10 to-transparent border-blue-500/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-blue-500/20 flex items-center justify-center",children:s.jsx(Oo,{className:"w-5 h-5 text-blue-400"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:ie.length}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Printing"})]})]})})}),s.jsx(St,{className:"bg-gradient-to-br from-yellow-500/10 to-transparent border-yellow-500/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-yellow-500/20 flex items-center justify-center",children:s.jsx(Zn,{className:"w-5 h-5 text-yellow-400"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:de.length}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Queued"})]})]})})}),s.jsx(St,{className:"bg-gradient-to-br from-bambu-green/10 to-transparent border-bambu-green/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-bambu-green/20 flex items-center justify-center",children:s.jsx(jd,{className:"w-5 h-5 text-bambu-green"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:Vie(me)}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Total Queue Time"})]})]})})}),s.jsx(St,{className:"bg-gradient-to-br from-gray-500/10 to-transparent border-gray-500/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-gray-500/20 flex items-center justify-center",children:s.jsx(Un,{className:"w-5 h-5 text-gray-400"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:Ae.length}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"History"})]})]})})})]}),s.jsxs("div",{className:"flex items-center gap-4 mb-6",children:[s.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===-1?"unassigned":n||"",onChange:ye=>{const le=ye.target.value;r(le==="unassigned"?-1:le===""?null:Number(le))},children:[s.jsx("option",{value:"",children:"All Printers"}),s.jsx("option",{value:"unassigned",children:"Unassigned"}),W?.map(ye=>s.jsx("option",{value:ye.id,children:ye.name},ye.id))]}),s.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:ye=>a(ye.target.value),children:[s.jsx("option",{value:"",children:"All Status"}),s.jsx("option",{value:"pending",children:"Pending"}),s.jsx("option",{value:"printing",children:"Printing"}),s.jsx("option",{value:"completed",children:"Completed"}),s.jsx("option",{value:"failed",children:"Failed"}),s.jsx("option",{value:"skipped",children:"Skipped"}),s.jsx("option",{value:"cancelled",children:"Cancelled"})]}),s.jsx("div",{className:"flex-1"}),Ae.length>0&&s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>l(!0),children:[s.jsx(dn,{className:"w-4 h-4"}),"Clear History"]})]}),P?s.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading..."}):q?.length===0?s.jsxs(St,{className:"p-12 text-center border-dashed",children:[s.jsx(oi,{className:"w-16 h-16 text-bambu-gray mx-auto mb-4 opacity-50"}),s.jsx("h3",{className:"text-xl font-medium text-white mb-2",children:"No prints scheduled"}),s.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.'})]}):s.jsxs("div",{className:"space-y-8",children:[ie.length>0&&s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white mb-4 flex items-center gap-2",children:[s.jsx("div",{className:"w-2 h-2 rounded-full bg-blue-400 animate-pulse"}),"Currently Printing"]}),s.jsx("div",{className:"space-y-3",children:ie.map(ye=>s.jsx(qj,{item:ye,onEdit:()=>{},onCancel:()=>{},onRemove:()=>{},onStop:()=>p({type:"stop",item:ye}),onRequeue:()=>{},onStart:()=>{},timeFormat:L},ye.id))})]}),de.length>0&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Zn,{className:"w-5 h-5 text-yellow-400"}),"Queued",s.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",de.length," item",de.length!==1?"s":"",")"]}),s.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)"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.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:k,onChange:ye=>E(ye.target.value),children:[s.jsx("option",{value:"position",children:"Sort by Position"}),s.jsx("option",{value:"name",children:"Sort by Name"}),s.jsx("option",{value:"printer",children:"Sort by Printer"}),s.jsx("option",{value:"time",children:"Sort by Schedule"})]}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:()=>C(!O),title:O?"Ascending":"Descending",className:"px-2",children:O?s.jsx(bg,{className:"w-4 h-4"}):s.jsx(j0,{className:"w-4 h-4"})})]})]}),s.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-bambu-dark rounded-lg",children:[s.jsxs(Se,{variant:"ghost",size:"sm",onClick:fe,className:"flex items-center gap-2",children:[b.length===de.length&&de.length>0?s.jsx(Lo,{className:"w-4 h-4 text-bambu-green"}):s.jsx(Io,{className:"w-4 h-4"}),b.length===de.length&&de.length>0?"Deselect All":"Select All"]}),b.length>0&&s.jsxs(s.Fragment,{children:[s.jsxs("span",{className:"text-sm text-bambu-gray",children:[b.length," selected"]}),s.jsx("div",{className:"h-4 w-px bg-bambu-dark-tertiary"}),s.jsxs(Se,{variant:"ghost",size:"sm",onClick:()=>v(!0),className:"flex items-center gap-2 text-bambu-green hover:text-bambu-green-light",children:[s.jsx(la,{className:"w-4 h-4"}),"Edit Selected"]}),s.jsxs(Se,{variant:"ghost",size:"sm",onClick:()=>ne.mutate(b),className:"flex items-center gap-2 text-red-400 hover:text-red-300",disabled:ne.isPending,children:[s.jsx(Vt,{className:"w-4 h-4"}),"Cancel Selected"]})]})]}),s.jsx(Lie,{sensors:D,collisionDetection:Sie,onDragEnd:Ne,children:s.jsx(Uie,{items:de.map(ye=>ye.id),strategy:eYe,children:s.jsx("div",{className:"space-y-3",children:de.map((ye,le)=>s.jsx(qj,{item:ye,position:le+1,onEdit:()=>u(ye),onCancel:()=>p({type:"cancel",item:ye}),onRemove:()=>{},onStop:()=>{},onRequeue:()=>{},onStart:()=>re.mutate(ye.id),timeFormat:L,isSelected:b.includes(ye.id),onToggleSelect:()=>ae(ye.id)},ye.id))})})})]}),Ae.length>0&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Un,{className:"w-5 h-5 text-bambu-gray"}),"History",s.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",Ae.length," item",Ae.length!==1?"s":"",")"]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.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:S,onChange:ye=>M(ye.target.value),children:[s.jsx("option",{value:"date",children:"Sort by Date"}),s.jsx("option",{value:"name",children:"Sort by Name"}),s.jsx("option",{value:"printer",children:"Sort by Printer"})]}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:()=>T(!A),title:A?"Ascending (oldest first)":"Descending (newest first)",className:"px-2",children:A?s.jsx(bg,{className:"w-4 h-4"}):s.jsx(j0,{className:"w-4 h-4"})})]})]}),s.jsx("div",{className:"space-y-3",children:Ae.slice(0,20).map((ye,le)=>s.jsx(qj,{item:ye,position:le+1,onEdit:()=>{},onCancel:()=>{},onRemove:()=>p({type:"remove",item:ye}),onStop:()=>{},onRequeue:()=>f(ye),onStart:()=>{},timeFormat:L},ye.id))})]})]}),c&&s.jsx(ch,{mode:"edit-queue-item",archiveId:c.archive_id??void 0,libraryFileId:c.library_file_id??void 0,archiveName:c.archive_name||c.library_file_name||`File #${c.archive_id||c.library_file_id}`,queueItem:c,onClose:()=>u(null)}),d&&s.jsx(ch,{mode:"add-to-queue",archiveId:d.archive_id??void 0,libraryFileId:d.library_file_id??void 0,archiveName:d.archive_name||d.library_file_name||`File #${d.archive_id||d.library_file_id}`,onClose:()=>f(null)}),m&&s.jsx(Hn,{title:m.type==="cancel"?"Cancel Scheduled Print":m.type==="stop"?"Stop Print":"Remove from History",message:m.type==="cancel"?`Are you sure you want to cancel "${m.item.archive_name||m.item.library_file_name||"this print"}"?`:m.type==="stop"?`Are you sure you want to stop the current print "${m.item.archive_name||m.item.library_file_name||"this print"}"? This will cancel the print job on the printer.`:`Are you sure you want to remove "${m.item.archive_name||m.item.library_file_name||"this item"}" from the queue history?`,confirmText:m.type==="cancel"?"Cancel Print":m.type==="stop"?"Stop Print":"Remove",variant:"danger",onConfirm:()=>{m.type==="cancel"?K.mutate(m.item.id):m.type==="stop"?Q.mutate(m.item.id):V.mutate(m.item.id),p(null)},onCancel:()=>p(null)}),o&&s.jsx(Hn,{title:"Clear History",message:`Are you sure you want to remove all ${Ae.length} item${Ae.length!==1?"s":""} from the history?`,confirmText:"Clear History",variant:"danger",onConfirm:()=>{z.mutate(),l(!1)},onCancel:()=>l(!1)}),x&&s.jsx(fYe,{selectedCount:b.length,printers:W?.map(ye=>({id:ye.id,name:ye.name}))||[],onSave:ye=>{Object.keys(ye).length>0&&G.mutate({item_ids:b,...ye})},onClose:()=>v(!1),isSaving:G.isPending})]})}function pYe({printDates:t,months:e=3}){const n=w.useRef(null),[r,i]=w.useState(0);w.useEffect(()=>{const S=n.current;if(!S)return;const M=new ResizeObserver(A=>{const T=A[0]?.contentRect.width||0;i(T)});return M.observe(S),()=>M.disconnect()},[]);const{weeks:a,monthLabels:o,printCounts:l}=w.useMemo(()=>{const S={};t.forEach(U=>{const L=U.split("T")[0];S[L]=(S[L]||0)+1});const M=new Date,A=new Date(M);A.setMonth(A.getMonth()-e),A.setDate(A.getDate()-A.getDay());const T=[],k=[];let E=[],O=-1;const C=new Date(A);let D=0;for(;C<=M;)C.getDay()===0&&E.length>0&&(T.push(E),E=[],D++),C.getMonth()!==O&&(k.push({month:C.toLocaleDateString("en-US",{month:"short"}),weekIndex:D}),O=C.getMonth()),E.push(new Date(C)),C.setDate(C.getDate()+1);return E.length>0&&T.push(E),{weeks:T,monthLabels:k,printCounts:S}},[t,e]),c=Math.max(1,...Object.values(l)),u=S=>{if(S===0)return"bg-bambu-dark";const M=S/c;return M<=.25?"bg-bambu-green/30":M<=.5?"bg-bambu-green/50":M<=.75?"bg-bambu-green/75":"bg-bambu-green"},d=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],f=a.length,m=32,p=2,b=r-m-16,y=f>0?Math.floor((b-(f-1)*p)/f):12,x=Math.max(8,Math.min(20,y)),v=x<=10?10:12;return s.jsx("div",{ref:n,className:"w-full flex justify-center",children:r>0&&s.jsxs("div",{children:[s.jsx("div",{className:"flex mb-1",style:{marginLeft:m+4},children:o.map(({month:S,weekIndex:M},A)=>s.jsx("div",{className:"text-bambu-gray",style:{fontSize:v,marginLeft:A===0?0:`${(M-(o[A-1]?.weekIndex||0))*(x+p)-24}px`},children:S},A))}),s.jsxs("div",{className:"flex",style:{gap:p},children:[s.jsx("div",{className:"flex flex-col",style:{gap:p,marginRight:4,width:m},children:d.map((S,M)=>s.jsx("div",{className:"text-bambu-gray flex items-center",style:{width:m,height:x,fontSize:v,visibility:M%2===1?"visible":"hidden"},children:S},S))}),a.map((S,M)=>s.jsx("div",{className:"flex flex-col",style:{gap:p},children:[0,1,2,3,4,5,6].map(A=>{const T=S.find(C=>C.getDay()===A);if(!T)return s.jsx("div",{style:{width:x,height:x}},A);const k=T.toISOString().split("T")[0],E=l[k]||0,O=k===new Date().toISOString().split("T")[0];return s.jsx("div",{className:`rounded-sm ${u(E)} ${O?"ring-1 ring-white":""}`,style:{width:x,height:x},title:`${T.toLocaleDateString()}: ${E} print${E!==1?"s":""}`},A)})},M))]}),s.jsxs("div",{className:"flex items-center gap-2 mt-3 text-bambu-gray",style:{fontSize:v},children:[s.jsx("span",{children:"Less"}),s.jsxs("div",{className:"flex",style:{gap:p},children:[s.jsx("div",{className:"rounded-sm bg-bambu-dark",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green/30",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green/50",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green/75",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green",style:{width:x,height:x}})]}),s.jsx("span",{children:"More"})]})]})})}const Qw=["#00ae42","#3b82f6","#f59e0b","#ef4444","#8b5cf6","#ec4899","#14b8a6","#f97316"];function gYe(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 xYe({archives:t,currency:e="$"}){const[n,r]=w.useState("30d"),i=w.useMemo(()=>{const m=gYe(n);return t.filter(p=>(Ni(p.completed_at||p.created_at)||new Date(0))>=m)},[t,n]),a=w.useMemo(()=>{const m=new Map;return i.forEach(p=>{const b=Ni(p.completed_at||p.created_at)||new Date,y=`${b.getFullYear()}-${String(b.getMonth()+1).padStart(2,"0")}-${String(b.getDate()).padStart(2,"0")}`,x=m.get(y)||{date:y,filament:0,cost:0,prints:0};x.filament+=p.filament_used_grams||0,x.cost+=p.cost||0,x.prints+=1,m.set(y,x)}),Array.from(m.values()).sort((p,b)=>p.date.localeCompare(b.date)).map(p=>({...p,dateLabel:new Date(p.date).toLocaleDateString("en-US",{month:"short",day:"numeric"})}))},[i]),o=w.useMemo(()=>{if(n==="7d"||n==="30d")return a;const m=new Map;return i.forEach(p=>{const b=Ni(p.completed_at||p.created_at)||new Date,y=new Date(b);y.setDate(b.getDate()-b.getDay());const x=`${y.getFullYear()}-${String(y.getMonth()+1).padStart(2,"0")}-${String(y.getDate()).padStart(2,"0")}`,v=m.get(x)||{week:x,filament:0,cost:0,prints:0};v.filament+=p.filament_used_grams||0,v.cost+=p.cost||0,v.prints+=1,m.set(x,v)}),Array.from(m.values()).sort((p,b)=>p.week.localeCompare(b.week)).map(p=>({date:p.week,dateLabel:`Week of ${new Date(p.week).toLocaleDateString("en-US",{month:"short",day:"numeric"})}`,...p}))},[i,a,n]),l=w.useMemo(()=>{const m=new Map;return i.forEach(p=>{const y=(p.filament_type||"Unknown").split(", ");y.forEach(x=>{const v=(p.filament_used_grams||0)/y.length;m.set(x,(m.get(x)||0)+v)})}),Array.from(m.entries()).map(([p,b])=>({name:p,value:Math.round(b)})).sort((p,b)=>b.value-p.value)},[i]),c=w.useMemo(()=>{const m=new Date,p=[];for(let b=5;b>=0;b--){const y=new Date(m.getFullYear(),m.getMonth()-b,1),x=new Date(m.getFullYear(),m.getMonth()-b+1,0),v=y.toLocaleDateString("en-US",{month:"short",year:"2-digit"}),S=t.filter(M=>{const A=Ni(M.completed_at||M.created_at)||new Date(0);return A>=y&&A<=x});p.push({month:v,filament:Math.round(S.reduce((M,A)=>M+(A.filament_used_grams||0),0)),cost:S.reduce((M,A)=>M+(A.cost||0),0),prints:S.length})}return p},[t]),u=n==="7d"||n==="30d"?a:o,d=i.reduce((m,p)=>m+(p.filament_used_grams||0),0),f=i.reduce((m,p)=>m+(p.cost||0),0);return s.jsxs("div",{className:"space-y-6",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Filament Usage Trends"}),s.jsx("div",{className:"flex gap-1 bg-bambu-dark rounded-lg p-1",children:["7d","30d","90d","365d","all"].map(m=>s.jsx("button",{onClick:()=>r(m),className:`px-3 py-1 text-sm rounded-md transition-colors ${n===m?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:m==="all"?"All":m.replace("d","D")},m))})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Filament"}),s.jsxs("p",{className:"text-2xl font-bold text-white",children:[(d/1e3).toFixed(2),"kg"]}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[d.toFixed(0),"g total"]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Cost"}),s.jsxs("p",{className:"text-2xl font-bold text-white",children:[e,f.toFixed(2)]}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[i.length," prints"]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Avg per Print"}),s.jsxs("p",{className:"text-2xl font-bold text-white",children:[i.length>0?(d/i.length).toFixed(0):0,"g"]}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[e,i.length>0?(f/i.length).toFixed(2):"0.00"," avg"]})]})]}),u.length>0?s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Usage Over Time"}),s.jsx(l1,{width:"100%",height:250,children:s.jsxs(NPe,{data:u,children:[s.jsx("defs",{children:s.jsxs("linearGradient",{id:"colorFilament",x1:"0",y1:"0",x2:"0",y2:"1",children:[s.jsx("stop",{offset:"5%",stopColor:"#00ae42",stopOpacity:.3}),s.jsx("stop",{offset:"95%",stopColor:"#00ae42",stopOpacity:0})]})}),s.jsx(OS,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),s.jsx(US,{dataKey:"dateLabel",stroke:"#9ca3af",tick:{fontSize:12},interval:"preserveStartEnd"}),s.jsx(zS,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:m=>`${m}g`}),s.jsx(u1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},labelStyle:{color:"#fff"},formatter:m=>[`${Number(m??0).toFixed(0)}g`,"Filament"]}),s.jsx(CJ,{type:"monotone",dataKey:"filament",stroke:"#00ae42",strokeWidth:2,fillOpacity:1,fill:"url(#colorFilament)"})]})})]}):s.jsx("div",{className:"bg-bambu-dark rounded-lg p-8 text-center text-bambu-gray",children:"No data for selected time range"}),s.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"By Filament Type"}),l.length>0?s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx(l1,{width:160,height:160,children:s.jsxs(SPe,{children:[s.jsx(tJ,{data:l,cx:"50%",cy:"50%",innerRadius:40,outerRadius:70,paddingAngle:2,dataKey:"value",children:l.map((m,p)=>s.jsx(qy,{fill:Qw[p%Qw.length]},`cell-${p}`))}),s.jsx(u1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:m=>[`${m??0}g`,"Usage"]})]})}),s.jsx("div",{className:"flex-1 space-y-2 overflow-hidden",children:l.map((m,p)=>{const b=l.reduce((x,v)=>x+v.value,0),y=b>0?(m.value/b*100).toFixed(0):0;return s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx("div",{className:"w-3 h-3 rounded-sm flex-shrink-0",style:{backgroundColor:Qw[p%Qw.length]}}),s.jsx("span",{className:"text-white truncate flex-1",children:m.name}),s.jsxs("span",{className:"text-bambu-gray flex-shrink-0",children:[y,"%"]})]},m.name)})})]}):s.jsx("div",{className:"h-[160px] flex items-center justify-center text-bambu-gray",children:"No filament data"})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Monthly Comparison"}),s.jsx(l1,{width:"100%",height:200,children:s.jsxs(dPe,{data:c,children:[s.jsx(OS,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),s.jsx(US,{dataKey:"month",stroke:"#9ca3af",tick:{fontSize:12}}),s.jsx(zS,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:m=>`${m}g`}),s.jsx(u1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:(m,p)=>[p==="filament"?`${m??0}g`:p==="cost"?`${e}${Number(m??0).toFixed(2)}`:m??0,p==="filament"?"Filament":p==="cost"?"Cost":"Prints"]}),s.jsx(SO,{}),s.jsx(DJ,{dataKey:"filament",name:"Filament (g)",fill:"#00ae42",radius:[4,4,0,0]})]})})]})]})]})}function bYe({id:t,title:e,component:n,isHidden:r,size:i,onToggleVisibility:a,onToggleSize:o}){const{attributes:l,listeners:c,setNodeRef:u,transform:d,transition:f,isDragging:m}=Bie({id:t}),p={transform:qg.Transform.toString(d),transition:f,opacity:m?.5:1};return r?null:s.jsxs("div",{ref:u,style:{...p,gridColumn:`span ${i}`},className:`bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary overflow-hidden ${m?"ring-2 ring-bambu-green shadow-lg":""}`,children:[s.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/30",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("button",{...l,...c,className:"cursor-grab active:cursor-grabbing p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Drag to reorder",children:s.jsx(yg,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}),s.jsx("h3",{className:"text-sm font-medium text-white",children:e})]}),s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx("button",{onClick:o,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?s.jsx(wK,{className:"w-4 h-4 text-bambu-gray hover:text-white"}):s.jsx(_K,{className:"w-4 h-4 text-bambu-gray hover:text-white"})}),s.jsx("button",{onClick:a,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Hide widget",children:s.jsx(My,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]})]}),s.jsx("div",{className:"p-4",children:typeof n=="function"?n(i):n})]})}function yYe({widgets:t,storageKey:e,columns:n=4,hideControls:r=!1,onResetLayout:i,renderControls:a}){const o=()=>{const A={};return t.forEach(T=>{A[T.id]=T.defaultSize||4}),A},[l,c]=w.useState(()=>{const A=localStorage.getItem(e);if(A)try{const T=JSON.parse(A);return T.sizes||(T.sizes=o()),T}catch{}return{order:t.map(T=>T.id),hidden:t.filter(T=>T.defaultVisible===!1).map(T=>T.id),sizes:o()}}),[u,d]=w.useState(!1);w.useEffect(()=>{const A=()=>d(T=>!T);return window.addEventListener("toggle-hidden-panel",A),()=>window.removeEventListener("toggle-hidden-panel",A)},[]),w.useEffect(()=>{localStorage.setItem(e,JSON.stringify(l))},[l,e]),w.useEffect(()=>{const T=t.map(k=>k.id).filter(k=>!l.order.includes(k));T.length>0&&c(k=>({...k,order:[...k.order,...T]}))},[t,l.order]);const f=yie(YM(UE,{activationConstraint:{distance:8}}),YM(FE,{coordinateGetter:$ie})),m=A=>{const{active:T,over:k}=A;k&&T.id!==k.id&&c(E=>{const O=E.order.indexOf(T.id),C=E.order.indexOf(k.id);return{...E,order:$E(E.order,O,C)}})},p=A=>{c(T=>({...T,hidden:T.hidden.includes(A)?T.hidden.filter(k=>k!==A):[...T.hidden,A]}))},b=A=>{c(T=>{const k=T.sizes[A]||4,E=k===1?2:k===2?4:1;return{...T,sizes:{...T.sizes,[A]:E}}})},y=()=>{const A={order:t.map(T=>T.id),hidden:t.filter(T=>T.defaultVisible===!1).map(T=>T.id),sizes:o()};c(A),i?.()},x=l.order.map(A=>t.find(T=>T.id===A)).filter(Boolean),v=x.filter(A=>!l.hidden.includes(A.id)),S=x.filter(A=>l.hidden.includes(A.id)),M=a?.({hiddenCount:S.length,showHiddenPanel:u,setShowHiddenPanel:d,resetLayout:y});return s.jsxs("div",{className:"space-y-4",children:[M,!r&&!a&&s.jsxs("div",{className:"flex items-center justify-end gap-2",children:[s.jsxs(Se,{variant:"secondary",size:"sm",onClick:y,children:[s.jsx(mm,{className:"w-4 h-4"}),"Reset Layout"]}),S.length>0&&s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>d(!u),children:[s.jsx(Qd,{className:"w-4 h-4"}),S.length," Hidden"]})]}),u&&S.length>0&&s.jsxs("div",{className:"p-4 bg-bambu-dark rounded-xl border border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-3",children:"Hidden widgets (click to show):"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:S.map(A=>s.jsxs("button",{onClick:()=>p(A.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:[s.jsx(Qd,{className:"w-3 h-3"}),A.title]},A.id))})]}),s.jsx(Lie,{sensors:f,collisionDetection:Sie,onDragEnd:m,children:s.jsx(Uie,{items:v.map(A=>A.id),strategy:vI,children:s.jsx("div",{className:"grid gap-6",style:{gridTemplateColumns:`repeat(${n}, minmax(0, 1fr))`},children:v.map(A=>s.jsx(bYe,{id:A.id,title:A.title,component:A.component,isHidden:l.hidden.includes(A.id),size:l.sizes[A.id]||2,onToggleVisibility:()=>p(A.id),onToggleSize:()=>b(A.id)},A.id))})})}),v.length===0&&s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx("p",{children:"All widgets are hidden."}),s.jsx(Se,{className:"mt-4",onClick:y,children:"Reset Layout"})]})]})}function vYe({stats:t,currency:e}){return s.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-3 gap-4",children:[s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:s.jsx(Jl,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Total Prints"}),s.jsx("p",{className:"text-xl font-bold text-white",children:t?.total_prints||0})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-blue-400",children:s.jsx(Zn,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Print Time"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_print_time_hours.toFixed(1)||0,"h"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-orange-400",children:s.jsx(Jl,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Used"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[((t?.total_filament_grams||0)/1e3).toFixed(2),"kg"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-green-400",children:s.jsx(xP,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Cost"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_cost.toFixed(2)||"0.00"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-400",children:s.jsx(qf,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Used"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_energy_kwh.toFixed(2)||"0.00"," kWh"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-500",children:s.jsx(xP,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Cost"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_energy_cost.toFixed(2)||"0.00"]})]})]})]})}function _Ye({stats:t,printerMap:e,size:n=1}){const r=t?.total_prints?Math.round(t.successful_prints/t.total_prints*100):0,i=n===1?112:n===2?128:144,a=i/2-8,o=a*2*Math.PI;return s.jsxs("div",{className:"flex items-center gap-6",children:[s.jsxs("div",{className:"relative flex-shrink-0",style:{width:i,height:i},children:[s.jsxs("svg",{className:"w-full h-full -rotate-90",children:[s.jsx("circle",{cx:i/2,cy:i/2,r:a,fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),s.jsx("circle",{cx:i/2,cy:i/2,r:a,fill:"none",stroke:"#00ae42",strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${r/100*o} ${o}`})]}),s.jsx("div",{className:"absolute inset-0 flex items-center justify-center",children:s.jsxs("span",{className:`font-bold text-white ${n>=2?"text-2xl":"text-xl"}`,children:[r,"%"]})})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-status-ok flex-shrink-0"}),s.jsx("span",{className:"text-sm text-bambu-gray",children:"Successful:"}),s.jsx("span",{className:"text-sm text-white font-medium",children:t?.successful_prints||0})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(ia,{className:"w-4 h-4 text-status-error flex-shrink-0"}),s.jsx("span",{className:"text-sm text-bambu-gray",children:"Failed:"}),s.jsx("span",{className:"text-sm text-white font-medium",children:t?.failed_prints||0})]})]}),n>=2&&t?.prints_by_printer&&Object.keys(t.prints_by_printer).length>0&&s.jsxs("div",{className:"mt-4 pt-4 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-xs text-bambu-gray font-medium mb-2",children:"Prints by Printer"}),s.jsx("div",{className:`grid gap-x-6 gap-y-1 ${n===4?"grid-cols-3":"grid-cols-2"}`,style:{width:"fit-content"},children:Object.entries(t.prints_by_printer).map(([l,c])=>s.jsxs("div",{className:"flex items-center gap-3 text-sm",children:[s.jsx("span",{className:"text-bambu-gray truncate max-w-[120px]",children:e.get(l)||`Printer ${l}`}),s.jsx("span",{className:"text-white font-medium",children:c})]},l))})]})]})]})}function wYe({stats:t,printerMap:e,size:n=1}){const r=t?.average_time_accuracy;if(r==null)return s.jsx("div",{className:"flex items-center justify-center h-full",children:s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No time accuracy data yet"})});const a=(Math.min(150,Math.max(50,r))-50)/100*100,l=(b=>b>=95&&b<=105?"#00ae42":b>105?"#3b82f6":"#f97316")(r),c=r-100,u=n===1?112:n===2?128:144,d=u/2-8,f=d*2*Math.PI,m=n===1?3:n===2?6:999,p=t?.time_accuracy_by_printer?Object.entries(t.time_accuracy_by_printer).slice(0,m):[];return s.jsxs("div",{className:"flex items-center gap-6",children:[s.jsxs("div",{className:"relative flex-shrink-0",style:{width:u,height:u},children:[s.jsxs("svg",{className:"w-full h-full -rotate-90",children:[s.jsx("circle",{cx:u/2,cy:u/2,r:d,fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),s.jsx("circle",{cx:u/2,cy:u/2,r:d,fill:"none",stroke:l,strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${a/100*f} ${f}`})]}),s.jsxs("div",{className:"absolute inset-0 flex flex-col items-center justify-center",children:[s.jsxs("span",{className:`font-bold text-white ${n>=2?"text-2xl":"text-xl"}`,children:[r.toFixed(0),"%"]}),s.jsxs("span",{className:`text-xs ${c>=0?"text-blue-400":"text-orange-400"}`,children:[c>=0?"+":"",c.toFixed(0),"%"]})]})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2 text-xs text-bambu-gray",children:[s.jsx(AK,{className:"w-3 h-3 flex-shrink-0"}),s.jsx("span",{children:"100% = perfect estimate"})]}),p.length>0&&s.jsx("div",{className:`mt-2 ${n===4?"grid grid-cols-3 gap-x-6 gap-y-1":n===2?"grid grid-cols-2 gap-x-6 gap-y-1":"space-y-1"}`,style:{width:"fit-content"},children:p.map(([b,y])=>s.jsxs("div",{className:"flex items-center gap-2 text-xs",children:[s.jsx("span",{className:"text-bambu-gray truncate max-w-[100px]",children:e.get(b)||`Printer ${b}`}),s.jsxs("span",{className:`font-medium ${y>=95&&y<=105?"text-status-ok":y>105?"text-blue-400":"text-status-warning"}`,children:[y.toFixed(0),"%"]})]},b))})]})]})}function SYe({stats:t,size:e=1}){if(!t?.prints_by_filament_type||Object.keys(t.prints_by_filament_type).length===0)return s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No filament data available"});const n=Object.entries(t.prints_by_filament_type).sort(([,o],[,l])=>l-o),r=e===1?5:e===2?8:999,i=n.slice(0,r),a=n.length>r;return e===4&&i.length>4?s.jsx("div",{className:"grid grid-cols-2 lg:grid-cols-3 gap-4",children:i.map(([o,l])=>{const c=Math.round(l/(t.total_prints||1)*100);return s.jsxs("div",{children:[s.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[s.jsx("span",{className:"text-white truncate max-w-[120px]",children:o}),s.jsx("span",{className:"text-bambu-gray",children:l})]}),s.jsx("div",{className:"h-2 bg-bambu-dark rounded-full",children:s.jsx("div",{className:"h-full bg-bambu-green rounded-full transition-all",style:{width:`${c}%`}})})]},o)})}):s.jsxs("div",{className:"space-y-3",children:[i.map(([o,l])=>{const c=Math.round(l/(t.total_prints||1)*100);return s.jsxs("div",{children:[s.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[s.jsx("span",{className:"text-white",children:o}),s.jsxs("span",{className:"text-bambu-gray",children:[l," prints"]})]}),s.jsx("div",{className:"h-2 bg-bambu-dark rounded-full",children:s.jsx("div",{className:"h-full bg-bambu-green rounded-full transition-all",style:{width:`${c}%`}})})]},o)}),a&&s.jsxs("p",{className:"text-xs text-bambu-gray text-center pt-1",children:["+",n.length-r," more types"]})]})}function MYe({printDates:t,size:e=2}){const n=e===1?3:e===2?6:12;return s.jsx(pYe,{printDates:t,months:n})}function NYe({stats:t,printerMap:e}){return!t?.prints_by_printer||Object.keys(t.prints_by_printer).length===0?s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No printer data available"}):s.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:Object.entries(t.prints_by_printer).map(([n,r])=>s.jsxs("div",{className:"flex items-center gap-3 p-3 bg-bambu-dark rounded-lg",children:[s.jsx("div",{className:"p-2 bg-bambu-dark-tertiary rounded-lg",children:s.jsx(qr,{className:"w-4 h-4 text-bambu-green"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-white font-medium text-sm",children:e.get(n)||`Printer ${n}`}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[r," prints"]})]})]},n))})}function EYe({archives:t,currency:e}){return!t||t.length===0?s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data available"}):s.jsx(xYe,{archives:t,currency:e})}function AYe({size:t=1}){const{data:e,isLoading:n}=ut({queryKey:["failureAnalysis"],queryFn:()=>he.getFailureAnalysis({days:30})});if(n)return s.jsx("div",{className:"flex justify-center py-4",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})});if(!e||e.total_prints===0)return s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data in the last 30 days"});const r=t===1?5:t===2?8:999,i=Object.entries(e.failures_by_reason).sort(([,l],[,c])=>c-l),a=i.slice(0,r),o=i.length>r;return s.jsxs("div",{className:`${t>=2?"flex gap-8":"space-y-4"}`,children:[s.jsxs("div",{className:t>=2?"flex-shrink-0":"",children:[s.jsx("div",{className:"flex items-center gap-4",children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(kn,{className:`w-5 h-5 ${e.failure_rate>20?"text-status-error":e.failure_rate>10?"text-status-warning":"text-status-ok"}`}),s.jsxs("span",{className:`font-bold text-white ${t>=2?"text-3xl":"text-2xl"}`,children:[e.failure_rate.toFixed(1),"%"]})]})}),s.jsxs("div",{className:"text-sm text-bambu-gray mt-1",children:[e.failed_prints," / ",e.total_prints," prints failed"]}),e.trend&&e.trend.length>=2&&s.jsx("div",{className:`${t>=2?"mt-4":"mt-2 pt-2 border-t border-bambu-dark-tertiary"}`,children:s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(CK,{className:`w-4 h-4 ${e.trend[e.trend.length-1].failure_rate0&&s.jsxs("div",{className:`flex-1 ${t>=2?"border-l border-bambu-dark-tertiary pl-8":"pt-2"}`,children:[s.jsx("p",{className:"text-xs text-bambu-gray font-medium mb-2",children:t>=2?"Failure Reasons":"Top Failure Reasons"}),s.jsx("div",{className:`${t===4?"grid grid-cols-2 gap-x-6 gap-y-1":"space-y-1"}`,children:a.map(([l,c])=>s.jsxs("div",{className:"flex items-center justify-between text-sm",children:[s.jsx("span",{className:`text-white truncate ${t===4?"max-w-[200px]":"max-w-[160px]"}`,children:l||"Unknown"}),s.jsx("span",{className:"text-bambu-gray ml-2",children:c})]},l))}),o&&s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["+",i.length-r," more reasons"]})]})]})}function TYe(){const{showToast:t}=qn(),[e,n]=w.useState(!1),[r,i]=w.useState(!1),[a,o]=w.useState(0),[l,c]=w.useState(0),[u,d]=w.useState(!1);w.useEffect(()=>{const E=()=>{try{const C=localStorage.getItem("bambusy-dashboard-layout");if(C){const D=JSON.parse(C);c(D.hidden?.length||0)}}catch{c(0)}};E(),window.addEventListener("storage",E);const O=setInterval(E,500);return()=>{window.removeEventListener("storage",E),clearInterval(O)}},[a]);const{data:f,isLoading:m,refetch:p}=ut({queryKey:["archiveStats"],queryFn:he.getArchiveStats}),{data:b}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:y}=ut({queryKey:["archives"],queryFn:()=>he.getArchives(void 0,void 0,1e3,0)}),{data:x}=ut({queryKey:["settings"],queryFn:he.getSettings}),v=async E=>{i(!1),n(!0);try{const{blob:O,filename:C}=await he.exportStats({format:E,days:90}),D=URL.createObjectURL(O),U=document.createElement("a");U.href=D,U.download=C,U.click(),URL.revokeObjectURL(D),t("Export downloaded")}catch{t("Export failed","error")}finally{n(!1)}},S=async()=>{d(!0);try{const E=await he.recalculateCosts();await p(),t(`Recalculated costs for ${E.updated} archives`)}catch{t("Failed to recalculate costs","error")}finally{d(!1)}},M=x?.currency||"$",A=new Map(b?.map(E=>[String(E.id),E.name])||[]),T=y?.map(E=>E.created_at)||[];if(m)return s.jsx("div",{className:"p-4 md:p-8",children:s.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading statistics..."})});const k=[{id:"quick-stats",title:"Quick Stats",component:s.jsx(vYe,{stats:f,currency:M}),defaultSize:2},{id:"success-rate",title:"Success Rate",component:E=>s.jsx(_Ye,{stats:f,printerMap:A,size:E}),defaultSize:1},{id:"time-accuracy",title:"Time Accuracy",component:E=>s.jsx(wYe,{stats:f,printerMap:A,size:E}),defaultSize:1},{id:"filament-types",title:"Filament Types",component:E=>s.jsx(SYe,{stats:f,size:E}),defaultSize:1},{id:"failure-analysis",title:"Failure Analysis (30 days)",component:E=>s.jsx(AYe,{size:E}),defaultSize:1},{id:"print-activity",title:"Print Activity",component:E=>s.jsx(MYe,{printDates:T,size:E}),defaultSize:2},{id:"prints-by-printer",title:"Prints by Printer",component:s.jsx(NYe,{stats:f,printerMap:A}),defaultSize:2},{id:"filament-trends",title:"Filament Usage Trends",component:s.jsx(EYe,{archives:y||[],currency:M}),defaultSize:4}];return s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsxs("div",{className:"flex items-center justify-between mb-6",children:[s.jsxs("div",{children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Dashboard"}),s.jsx("p",{className:"text-bambu-gray",children:"Drag widgets to rearrange. Click the eye icon to hide."})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[l>0&&s.jsxs(Se,{variant:"secondary",onClick:()=>{window.dispatchEvent(new CustomEvent("toggle-hidden-panel"))},children:[s.jsx(Qd,{className:"w-4 h-4"}),l," Hidden"]}),s.jsxs(Se,{variant:"secondary",onClick:()=>{localStorage.removeItem("bambusy-dashboard-layout"),o(E=>E+1),t("Layout reset")},children:[s.jsx(mm,{className:"w-4 h-4"}),"Reset Layout"]}),s.jsxs(Se,{variant:"secondary",onClick:S,disabled:u,title:"Recalculate all archive costs using current filament prices",children:[u?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Zue,{className:"w-4 h-4"}),"Recalculate Costs"]}),s.jsxs("div",{className:"relative",children:[s.jsxs(Se,{variant:"secondary",onClick:()=>i(!r),disabled:e,children:[e?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(I1,{className:"w-4 h-4"}),"Export Stats"]}),r&&s.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:[s.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:()=>v("csv"),children:[s.jsx(Za,{className:"w-4 h-4"}),"Export as CSV"]}),s.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:()=>v("xlsx"),children:[s.jsx(I1,{className:"w-4 h-4"}),"Export as Excel"]})]})]})]})]}),s.jsx(yYe,{widgets:k,storageKey:"bambusy-dashboard-layout",hideControls:!0},a)]})}function CYe({plug:t,onEdit:e}){const n=on(),{showToast:r}=qn(),[i,a]=w.useState(!1),[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,f]=w.useState(!1),{data:m,isLoading:p}=ut({queryKey:["smart-plug-status",t.id],queryFn:()=>he.getSmartPlugStatus(t.id),refetchInterval:3e4}),{data:b}=ut({queryKey:["printers"],queryFn:he.getPrinters}),y=b?.find(O=>O.id===t.printer_id),x=mt({mutationFn:O=>he.controlSmartPlug(t.id,O),onMutate:async O=>{await n.cancelQueries({queryKey:["smart-plug-status",t.id]});const C=n.getQueryData(["smart-plug-status",t.id]),D=O==="on"?"ON":O==="off"||m?.state==="ON"?"OFF":"ON";return n.setQueryData(["smart-plug-status",t.id],U=>({...U,state:D})),{previousStatus:C}},onError:(O,C,D)=>{D?.previousStatus&&n.setQueryData(["smart-plug-status",t.id],D.previousStatus),r(`Failed to turn ${C} "${t.name}"`,"error")},onSettled:()=>{setTimeout(()=>{n.invalidateQueries({queryKey:["smart-plug-status",t.id]}),n.invalidateQueries({queryKey:["smart-plugs"]})},1e3)}}),v=mt({mutationFn:O=>he.updateSmartPlug(t.id,O),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),t.printer_id&&n.invalidateQueries({queryKey:["smartPlugByPrinter",t.printer_id]})}}),S=mt({mutationFn:()=>he.deleteSmartPlug(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]})}}),M=m?.state==="ON",A=m?.reachable??!1,T=x.isPending,E=(()=>{if(t.plug_type!=="tasmota"||!t.ip_address)return null;const O=t.ip_address;return t.username&&t.password?`http://${encodeURIComponent(t.username)}:${encodeURIComponent(t.password)}@${O}/`:`http://${O}/`})();return s.jsxs(s.Fragment,{children:[s.jsx(St,{className:"relative",children:s.jsxs(Nt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-start justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${A?M?"bg-bambu-green/20":"bg-bambu-dark":"bg-red-500/20"}`,children:t.plug_type==="homeassistant"?s.jsx(cN,{className:`w-5 h-5 ${A?M?"text-bambu-green":"text-bambu-gray":"text-red-400"}`}):s.jsx(Ks,{className:`w-5 h-5 ${A?M?"text-bambu-green":"text-bambu-gray":"text-red-400"}`})}),s.jsxs("div",{children:[s.jsx("h3",{className:"font-medium text-white",children:t.name}),s.jsx("p",{className:"text-sm text-bambu-gray",children:t.plug_type==="homeassistant"?t.ha_entity_id:t.ip_address})]})]}),s.jsxs("div",{className:"flex flex-col items-end gap-1",children:[p?s.jsx(dt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):A?s.jsxs("div",{className:"flex items-center gap-1 text-sm",children:[s.jsx(ig,{className:"w-4 h-4 text-status-ok"}),s.jsx("span",{className:M?"text-status-ok":"text-bambu-gray",children:m?.state||"Unknown"})]}):s.jsxs("div",{className:"flex items-center gap-1 text-sm text-status-error",children:[s.jsx(ex,{className:"w-4 h-4"}),s.jsx("span",{children:"Offline"})]}),E&&s.jsxs("a",{href:E,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 px-2 py-0.5 bg-bambu-dark hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white text-xs rounded-full transition-colors",title:"Open plug admin page",children:[s.jsx(zr,{className:"w-3 h-3"}),"Admin"]})]})]}),y&&s.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Linked to: "}),s.jsx("span",{className:"text-sm text-white",children:y.name})]}),(t.power_alert_enabled||t.schedule_enabled)&&s.jsxs("div",{className:"flex flex-wrap gap-1.5 mb-3",children:[t.power_alert_enabled&&s.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:[s.jsx(Ef,{className:"w-3 h-3"}),"Alerts"]}),t.schedule_enabled&&s.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:[s.jsx(oi,{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}`]})]}),s.jsxs("div",{className:"flex gap-2 mb-3",children:[s.jsxs(Se,{size:"sm",variant:M?"primary":"secondary",disabled:!A||T,onClick:()=>l(!0),className:"flex-1",children:[T?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Eu,{className:"w-4 h-4"}),"On"]}),s.jsxs(Se,{size:"sm",variant:M?"secondary":"primary",disabled:!A||T,onClick:()=>u(!0),className:"flex-1",children:[T?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(fN,{className:"w-4 h-4"}),"Off"]})]}),s.jsxs("button",{onClick:()=>f(!d),className:"w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:[s.jsxs("span",{className:"flex items-center gap-2",children:[s.jsx(Jd,{className:"w-4 h-4"}),"Automation Settings"]}),s.jsx("span",{children:d?"-":"+"})]}),d&&s.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(uN,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Show in Switchbar"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.show_in_switchbar,onChange:O=>v.mutate({show_in_switchbar:O.target.checked}),className:"sr-only peer"}),s.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"})]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Enabled"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Enable automation for this plug"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.enabled,onChange:O=>v.mutate({enabled:O.target.checked}),className:"sr-only peer"}),s.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"})]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Auto On"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn on when print starts"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.auto_on,onChange:O=>v.mutate({auto_on:O.target.checked}),className:"sr-only peer"}),s.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"})]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Auto Off"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn off when print completes (one-shot)"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.auto_off,onChange:O=>v.mutate({auto_off:O.target.checked}),className:"sr-only peer"}),s.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&&s.jsxs("div",{className:"space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white mb-2",children:"Turn Off Delay Mode"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("button",{onClick:()=>v.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:[s.jsx(Zn,{className:"w-4 h-4"}),"Time"]}),s.jsxs("button",{onClick:()=>v.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:[s.jsx(pN,{className:"w-4 h-4"}),"Temp"]})]})]}),t.off_delay_mode==="time"?s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Delay (minutes)"}),s.jsx("input",{type:"number",min:"1",max:"60",value:t.off_delay_minutes,onChange:O=>v.mutate({off_delay_minutes:parseInt(O.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"})]}):s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Temperature threshold (C)"}),s.jsx("input",{type:"number",min:"30",max:"100",value:t.off_temp_threshold,onChange:O=>v.mutate({off_temp_threshold:parseInt(O.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Turns off when nozzle cools below this temperature"})]})]}),s.jsxs("div",{className:"flex gap-2 pt-2",children:[s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[s.jsx(hN,{className:"w-4 h-4"}),"Edit"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>a(!0),className:"text-red-400 hover:text-red-300",children:s.jsx(dn,{className:"w-4 h-4"})})]})]})]})}),i&&s.jsx(Hn,{title:"Delete Smart Plug",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{S.mutate(),a(!1)},onCancel:()=>a(!1)}),o&&s.jsx(Hn,{title:"Turn On Smart Plug",message:`Are you sure you want to turn on "${t.name}"?`,confirmText:"Turn On",variant:"default",onConfirm:()=>{x.mutate("on"),l(!1)},onCancel:()=>l(!1)}),c&&s.jsx(Hn,{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:()=>{x.mutate("off"),u(!1)},onCancel:()=>u(!1)})]})}function kYe({plug:t,onClose:e}){const n=on(),r=!!t,[i,a]=w.useState(t?.plug_type||"tasmota"),[o,l]=w.useState(t?.name||""),[c,u]=w.useState(t?.ip_address||""),[d,f]=w.useState(t?.username||""),[m,p]=w.useState(t?.password||""),[b,y]=w.useState(t?.ha_entity_id||""),[x,v]=w.useState(t?.ha_power_entity||""),[S,M]=w.useState(t?.ha_energy_today_entity||""),[A,T]=w.useState(t?.ha_energy_total_entity||""),[k,E]=w.useState(""),[O,C]=w.useState(""),[D,U]=w.useState(!1),L=w.useRef(null),[q,P]=w.useState(""),[W,K]=w.useState(!1),V=w.useRef(null),[Q,re]=w.useState(""),[J,z]=w.useState(!1),G=w.useRef(null),[ne,ae]=w.useState(""),[de,fe]=w.useState(!1),ie=w.useRef(null),[Ae,me]=w.useState(t?.printer_id||null),[Ne,ye]=w.useState(null),[le,$e]=w.useState(null),[H,ee]=w.useState(t?.power_alert_enabled||!1),[X,B]=w.useState(t?.power_alert_high?.toString()||""),[I,ce]=w.useState(t?.power_alert_low?.toString()||""),[Te,Le]=w.useState(t?.schedule_enabled||!1),[Ee,$]=w.useState(t?.schedule_on_time||""),[j,Y]=w.useState(t?.schedule_off_time||""),[ge,we]=w.useState(t?.show_in_switchbar||!1),[ve,Ze]=w.useState(!1),[Ie,qe]=w.useState({scanned:0,total:0}),[Je,Qe]=w.useState([]),oe=w.useRef(null),{data:te}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:Me}=ut({queryKey:["smart-plugs"],queryFn:he.getSmartPlugs}),{data:Fe}=ut({queryKey:["settings"],queryFn:he.getSettings}),ot=!!(Fe?.ha_enabled&&Fe?.ha_url&&Fe?.ha_token);w.useEffect(()=>{const gt=setTimeout(()=>{C(k)},300);return()=>clearTimeout(gt)},[k]),w.useEffect(()=>{const gt=Kt=>{L.current&&!L.current.contains(Kt.target)&&U(!1),V.current&&!V.current.contains(Kt.target)&&K(!1),G.current&&!G.current.contains(Kt.target)&&z(!1),ie.current&&!ie.current.contains(Kt.target)&&fe(!1)};return document.addEventListener("mousedown",gt),()=>document.removeEventListener("mousedown",gt)},[]);const{data:se,isLoading:je,error:xe}=ut({queryKey:["ha-entities",O],queryFn:()=>he.getHAEntities(O||void 0),enabled:i==="homeassistant"&&ot,retry:!1,staleTime:0}),{data:Oe}=ut({queryKey:["ha-sensor-entities"],queryFn:he.getHASensorEntities,enabled:i==="homeassistant"&&ot,retry:!1,staleTime:0});w.useEffect(()=>{const gt=Kt=>{Kt.key==="Escape"&&e()};return window.addEventListener("keydown",gt),()=>{window.removeEventListener("keydown",gt),oe.current&&clearInterval(oe.current)}},[e]);const Ue=async()=>{Ze(!0),Qe([]),qe({scanned:0,total:0}),$e(null);try{await he.startTasmotaScan();const gt=async()=>{try{const Kt=await he.getTasmotaScanStatus();qe({scanned:Kt.scanned,total:Kt.total});const bn=await he.getDiscoveredTasmotaDevices();Qe(bn),Kt.running||(Ze(!1),oe.current&&(clearInterval(oe.current),oe.current=null))}catch(Kt){console.error("Polling error:",Kt)}};await gt(),oe.current=setInterval(gt,500)}catch(gt){Ze(!1);const Kt=gt instanceof Error?gt.message:typeof gt=="string"?gt:JSON.stringify(gt);$e(Kt||"Failed to start scan")}},Ce=async()=>{try{await he.stopTasmotaScan()}catch{}Ze(!1),oe.current&&(clearInterval(oe.current),oe.current=null)},We=gt=>{u(gt.ip_address),l(gt.name),ye(null)},pt=mt({mutationFn:()=>he.testSmartPlugConnection(c,d||null,m||null),onSuccess:gt=>{ye(gt),$e(null),!o&>.device_name&&l(gt.device_name)},onError:gt=>{ye(null),$e(gt.message)}}),kt=mt({mutationFn:gt=>he.createSmartPlug(gt),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:gt=>{$e(gt.message)}}),Lt=mt({mutationFn:gt=>he.updateSmartPlug(t.id,gt),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:gt=>{$e(gt.message)}}),ln=te?.filter(gt=>!Me?.some(bn=>bn.printer_id===gt.id&&bn.id!==t?.id)),Gt=gt=>{if(gt.preventDefault(),$e(null),!o.trim()){$e("Name is required");return}if(i==="tasmota"&&!c.trim()){$e("IP address is required for Tasmota plugs");return}if(i==="homeassistant"&&!b){$e("Entity is required for Home Assistant plugs");return}const Kt={name:o.trim(),plug_type:i,ip_address:i==="tasmota"?c.trim():null,ha_entity_id:i==="homeassistant"?b:null,ha_power_entity:i==="homeassistant"&&x||null,ha_energy_today_entity:i==="homeassistant"&&S||null,ha_energy_total_entity:i==="homeassistant"&&A||null,username:i==="tasmota"&&d.trim()||null,password:i==="tasmota"&&m.trim()||null,printer_id:Ae,power_alert_enabled:H,power_alert_high:X?parseFloat(X):null,power_alert_low:I?parseFloat(I):null,schedule_enabled:Te,schedule_on_time:Ee||null,schedule_off_time:j||null,show_in_switchbar:ge};r?Lt.mutate(Kt):kt.mutate(Kt)},_n=kt.isPending||Lt.isPending;return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:s.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:gt=>gt.stopPropagation(),children:[s.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary flex-shrink-0",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Smart Plug":"Add Smart Plug"}),s.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:Gt,className:"p-6 space-y-4 overflow-y-auto",children:[le&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:le}),!r&&s.jsxs("div",{className:"flex gap-2 mb-2",children:[s.jsxs("button",{type:"button",onClick:()=>{a("tasmota"),ye(null),$e(null)},className:`flex-1 flex items-center justify-center gap-2 px-4 py-2.5 rounded-lg font-medium transition-colors ${i==="tasmota"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white border border-bambu-dark-tertiary"}`,children:[s.jsx(Ks,{className:"w-4 h-4"}),"Tasmota"]}),s.jsxs("button",{type:"button",onClick:()=>{a("homeassistant"),ye(null),$e(null)},className:`flex-1 flex items-center justify-center gap-2 px-4 py-2.5 rounded-lg font-medium transition-colors ${i==="homeassistant"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white border border-bambu-dark-tertiary"}`,children:[s.jsx(cN,{className:"w-4 h-4"}),"Home Assistant"]})]}),!r&&i==="tasmota"&&s.jsxs("div",{className:"space-y-3",children:[ve?s.jsxs(Se,{type:"button",variant:"secondary",onClick:Ce,className:"w-full",children:[s.jsx(Vt,{className:"w-4 h-4"}),"Stop Scanning"]}):s.jsxs(Se,{type:"button",variant:"primary",onClick:Ue,className:"w-full",children:[s.jsx(Ea,{className:"w-4 h-4"}),"Discover Tasmota Devices"]}),ve&&Ie.total>0&&s.jsxs("div",{className:"space-y-1",children:[s.jsxs("div",{className:"flex justify-between text-xs text-bambu-gray",children:[s.jsx("span",{children:"Scanning network..."}),s.jsxs("span",{children:[Ie.scanned," / ",Ie.total]})]}),s.jsx("div",{className:"w-full bg-bambu-dark-tertiary rounded-full h-2",children:s.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${Ie.scanned/Ie.total*100}%`}})})]}),Je.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("p",{className:"text-xs text-bambu-gray",children:["Found ",Je.length," device(s) - click to select:"]}),s.jsx("div",{className:"max-h-40 overflow-y-auto space-y-1",children:Je.map(gt=>s.jsxs("button",{type:"button",onClick:()=>We(gt),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:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Ks,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:gt.name}),s.jsx("p",{className:"text-xs text-bambu-gray",children:gt.ip_address})]})]}),gt.state&&s.jsxs("span",{className:`flex items-center gap-1 text-xs ${gt.state==="ON"?"text-bambu-green":"text-bambu-gray"}`,children:[s.jsx(Eu,{className:"w-3 h-3"}),gt.state]})]},gt.ip_address))})]}),!ve&&Je.length===0&&Ie.total>0&&s.jsx("p",{className:"text-xs text-bambu-gray text-center py-2",children:"No Tasmota devices found on your network"})]}),i==="homeassistant"&&s.jsxs("div",{className:"space-y-3",children:[!ot&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"p-3 bg-yellow-500/20 border border-yellow-500/50 rounded-lg text-sm text-yellow-400",children:["Home Assistant is not configured. Set it up in"," ",s.jsx("span",{className:"font-medium",children:"Settings → Network → Home Assistant"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1 opacity-50",children:"Select Entity *"}),s.jsx("select",{disabled:!0,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray cursor-not-allowed opacity-50",children:s.jsx("option",{children:"Choose an entity..."})})]})]}),ot&&s.jsxs(s.Fragment,{children:[je&&s.jsxs("div",{className:"flex items-center justify-center py-4 text-bambu-gray",children:[s.jsx(dt,{className:"w-5 h-5 animate-spin mr-2"}),"Loading entities..."]}),xe&&s.jsxs("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:["Failed to load entities: ",xe.message]}),(()=>{const gt=Me?.filter(At=>At.ha_entity_id&&At.id!==t?.id).map(At=>At.ha_entity_id)||[],Kt=(se||[]).filter(At=>!gt.includes(At.entity_id)),bn=se?.find(At=>At.entity_id===b);return s.jsxs("div",{ref:L,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Select Entity *"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:D?k:bn?`${bn.friendly_name} (${bn.entity_id})`:"",onChange:At=>{E(At.target.value),D||U(!0)},onFocus:()=>{U(!0),E("")},placeholder:"Search entities...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),b&&!D&&s.jsx("button",{type:"button",onClick:()=>{y(""),E("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})}),je&&s.jsx(dt,{className:"absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray animate-spin"})]}),D&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-60 overflow-y-auto",children:[je&&s.jsxs("div",{className:"px-3 py-2 text-sm text-bambu-gray flex items-center gap-2",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),"Loading..."]}),!je&&Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:O?`No entities found matching "${O}"`:"No entities available"}),!je&&Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{y(At.entity_id),U(!1),E(""),o||l(At.friendly_name)},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary transition-colors ${At.entity_id===b?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray flex items-center justify-between",children:[s.jsx("span",{children:At.entity_id}),s.jsx("span",{className:At.state==="on"?"text-bambu-green":"",children:At.state})]})]},At.entity_id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:O?`Searching all entities (${Kt.length} found)`:`Showing switch, light, input_boolean (${Kt.length} available)`})]})})(),b&&Oe&&Oe.length>0&&s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4 mt-4 space-y-3",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white font-medium mb-1",children:"Energy Monitoring (Optional)"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-3",children:"Search and select sensors that provide power/energy data."})]}),(()=>{const gt=Oe.filter(At=>At.unit_of_measurement==="W"||At.unit_of_measurement==="kW"||At.unit_of_measurement==="mW"),Kt=q?gt.filter(At=>At.entity_id.toLowerCase().includes(q.toLowerCase())||At.friendly_name.toLowerCase().includes(q.toLowerCase())):gt,bn=Oe.find(At=>At.entity_id===x);return s.jsxs("div",{ref:V,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Power Sensor (W)"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:W?q:bn?`${bn.friendly_name} (${bn.state} ${bn.unit_of_measurement})`:"",onChange:At=>{P(At.target.value),W||K(!0)},onFocus:()=>{K(!0),P("")},placeholder:"Search power sensors...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),x&&!W&&s.jsx("button",{type:"button",onClick:()=>{v(""),P("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]}),W&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[s.jsx("button",{type:"button",onClick:()=>{v(""),K(!1),P("")},className:"w-full px-3 py-2 text-left text-sm text-bambu-gray hover:bg-bambu-dark-tertiary",children:"None"}),Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{v(At.entity_id),K(!1),P("")},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${At.entity_id===x?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray",children:[At.entity_id," • ",At.state," ",At.unit_of_measurement]})]},At.entity_id)),Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:"No matching sensors"})]})]})})(),(()=>{const gt=Oe.filter(At=>At.unit_of_measurement==="kWh"||At.unit_of_measurement==="Wh"||At.unit_of_measurement==="MWh"),Kt=Q?gt.filter(At=>At.entity_id.toLowerCase().includes(Q.toLowerCase())||At.friendly_name.toLowerCase().includes(Q.toLowerCase())):gt,bn=Oe.find(At=>At.entity_id===S);return s.jsxs("div",{ref:G,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Energy Today (kWh)"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:J?Q:bn?`${bn.friendly_name} (${bn.state} ${bn.unit_of_measurement})`:"",onChange:At=>{re(At.target.value),J||z(!0)},onFocus:()=>{z(!0),re("")},placeholder:"Search energy sensors...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),S&&!J&&s.jsx("button",{type:"button",onClick:()=>{M(""),re("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]}),J&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[s.jsx("button",{type:"button",onClick:()=>{M(""),z(!1),re("")},className:"w-full px-3 py-2 text-left text-sm text-bambu-gray hover:bg-bambu-dark-tertiary",children:"None"}),Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{M(At.entity_id),z(!1),re("")},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${At.entity_id===S?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray",children:[At.entity_id," • ",At.state," ",At.unit_of_measurement]})]},At.entity_id)),Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:"No matching sensors"})]})]})})(),(()=>{const gt=Oe.filter(At=>At.unit_of_measurement==="kWh"||At.unit_of_measurement==="Wh"||At.unit_of_measurement==="MWh"),Kt=ne?gt.filter(At=>At.entity_id.toLowerCase().includes(ne.toLowerCase())||At.friendly_name.toLowerCase().includes(ne.toLowerCase())):gt,bn=Oe.find(At=>At.entity_id===A);return s.jsxs("div",{ref:ie,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Total Energy (kWh)"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:de?ne:bn?`${bn.friendly_name} (${bn.state} ${bn.unit_of_measurement})`:"",onChange:At=>{ae(At.target.value),de||fe(!0)},onFocus:()=>{fe(!0),ae("")},placeholder:"Search energy sensors...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),A&&!de&&s.jsx("button",{type:"button",onClick:()=>{T(""),ae("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]}),de&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[s.jsx("button",{type:"button",onClick:()=>{T(""),fe(!1),ae("")},className:"w-full px-3 py-2 text-left text-sm text-bambu-gray hover:bg-bambu-dark-tertiary",children:"None"}),Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{T(At.entity_id),fe(!1),ae("")},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${At.entity_id===A?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray",children:[At.entity_id," • ",At.state," ",At.unit_of_measurement]})]},At.entity_id)),Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:"No matching sensors"})]})]})})()]})]})]}),i==="tasmota"&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"IP Address *"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx("input",{type:"text",value:c,onChange:gt=>{u(gt.target.value),ye(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"}),s.jsxs(Se,{type:"button",variant:"secondary",onClick:()=>pt.mutate(),disabled:!c.trim()||pt.isPending,children:[pt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(ig,{className:"w-4 h-4"}),"Test"]})]})]}),i==="tasmota"&&Ne&&s.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${Ne.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:Ne.success?s.jsxs(s.Fragment,{children:[s.jsx(Un,{className:"w-5 h-5"}),s.jsxs("div",{children:[s.jsx("p",{className:"font-medium",children:"Connected!"}),s.jsxs("p",{className:"text-sm opacity-80",children:[Ne.device_name&&`Device: ${Ne.device_name} - `,"State: ",Ne.state]})]})]}):s.jsxs(s.Fragment,{children:[s.jsx(ex,{className:"w-5 h-5"}),s.jsx("span",{children:"Connection failed"})]})}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),s.jsx("input",{type:"text",value:o,onChange:gt=>l(gt.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"})]}),i==="tasmota"&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Username"}),s.jsx("input",{type:"text",value:d,onChange:gt=>f(gt.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),s.jsx("input",{type:"password",value:m,onChange:gt=>p(gt.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"})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray -mt-2",children:"Leave empty if your Tasmota device doesn't require authentication"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Link to Printer"}),s.jsxs("select",{value:Ae??"",onChange:gt=>me(gt.target.value?Number(gt.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:[s.jsx("option",{value:"",children:"No printer (manual control only)"}),ln?.map(gt=>s.jsx("option",{value:gt.id,children:gt.name},gt.id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Linking enables automatic on/off when prints start/complete"})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Ef,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"text-white font-medium",children:"Power Alerts"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:H,onChange:gt=>ee(gt.target.checked),className:"sr-only peer"}),s.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"})]})]}),H&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if above (W)"}),s.jsx("input",{type:"number",value:X,onChange:gt=>B(gt.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if below (W)"}),s.jsx("input",{type:"number",value:I,onChange:gt=>ce(gt.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"})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Get notified when power consumption crosses these thresholds. Leave empty to disable that direction."})]})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zn,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"text-white font-medium",children:"Daily Schedule"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:Te,onChange:gt=>Le(gt.target.checked),className:"sr-only peer"}),s.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"})]})]}),Te&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn On at"}),s.jsx("input",{type:"time",value:Ee,onChange:gt=>$(gt.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn Off at"}),s.jsx("input",{type:"time",value:j,onChange:gt=>Y(gt.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"})]})]}),s.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."})]})]}),s.jsx("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(uN,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white font-medium",children:"Show in Switchbar"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:ge,onChange:gt=>we(gt.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"flex gap-3 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:_n,className:"flex-1",children:[_n?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(_s,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function Gn({checked:t,onChange:e,disabled:n}){const r=i=>{i.preventDefault(),i.stopPropagation(),n||e(!t)};return s.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:s.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 jYe={callmebot:"CallMeBot/WhatsApp",ntfy:"ntfy",pushover:"Pushover",telegram:"Telegram",email:"Email",discord:"Discord",webhook:"Webhook"};function PYe({provider:t,onEdit:e}){const n=on(),[r,i]=w.useState(!1),[a,o]=w.useState(!1),[l,c]=w.useState(null),{data:u}=ut({queryKey:["printers"],queryFn:he.getPrinters}),d=u?.find(y=>y.id===t.printer_id),f=mt({mutationFn:y=>he.updateNotificationProvider(t.id,y),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),m=mt({mutationFn:()=>he.deleteNotificationProvider(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),p=mt({mutationFn:()=>he.testNotificationProvider(t.id),onSuccess:y=>{c(y),n.invalidateQueries({queryKey:["notification-providers"]})},onError:y=>{c({success:!1,message:y.message})}}),b=y=>y||"";return s.jsxs(s.Fragment,{children:[s.jsx(St,{className:"relative",children:s.jsxs(Nt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-start justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${t.enabled?"bg-bambu-green/20":"bg-bambu-dark"}`,children:s.jsx(Ef,{className:`w-5 h-5 ${t.enabled?"text-bambu-green":"text-bambu-gray"}`})}),s.jsxs("div",{children:[s.jsx("h3",{className:"font-medium text-white",children:t.name}),s.jsx("p",{className:"text-sm text-bambu-gray",children:jYe[t.provider_type]||t.provider_type})]})]}),s.jsxs("div",{className:"flex items-center gap-3",children:[t.last_success&&s.jsxs("span",{className:"text-xs text-status-ok hidden sm:inline",children:["Last: ",_g(t.last_success)]}),t.last_error&&t.last_error_at&&(!t.last_success||(Ni(t.last_error_at)?.getTime()||0)>(Ni(t.last_success)?.getTime()||0))&&s.jsx("span",{className:"text-xs text-status-error",title:t.last_error,children:"Error"}),s.jsx(Gn,{checked:t.enabled,onChange:y=>f.mutate({enabled:y})})]})]}),d&&s.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Printer: "}),s.jsx("span",{className:"text-sm text-white",children:d.name})]}),!d&&!t.printer_id&&s.jsx("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:s.jsx("span",{className:"text-xs text-bambu-gray",children:"All printers"})}),s.jsxs("div",{className:"mb-3 flex flex-wrap gap-1",children:[t.on_print_start&&s.jsx("span",{className:"px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded",children:"Start"}),t.on_plate_not_empty&&s.jsx("span",{className:"px-2 py-0.5 bg-rose-600/20 text-rose-300 text-xs rounded",children:"Plate Check"}),t.on_print_complete&&s.jsx("span",{className:"px-2 py-0.5 bg-bambu-green/20 text-bambu-green text-xs rounded",children:"Complete"}),t.on_print_failed&&s.jsx("span",{className:"px-2 py-0.5 bg-red-500/20 text-red-400 text-xs rounded",children:"Failed"}),t.on_print_stopped&&s.jsx("span",{className:"px-2 py-0.5 bg-orange-500/20 text-orange-400 text-xs rounded",children:"Stopped"}),t.on_print_progress&&s.jsx("span",{className:"px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded",children:"Progress"}),t.on_printer_offline&&s.jsx("span",{className:"px-2 py-0.5 bg-gray-500/20 text-gray-400 text-xs rounded",children:"Offline"}),t.on_printer_error&&s.jsx("span",{className:"px-2 py-0.5 bg-rose-500/20 text-rose-400 text-xs rounded",children:"Error"}),t.on_filament_low&&s.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&&s.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&&s.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&&s.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&&s.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&&s.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&&s.jsxs("span",{className:"px-2 py-0.5 bg-indigo-500/20 text-indigo-400 text-xs rounded flex items-center gap-1",children:[s.jsx(F1,{className:"w-3 h-3"}),"Quiet"]}),t.daily_digest_enabled&&s.jsxs("span",{className:"px-2 py-0.5 bg-emerald-500/20 text-emerald-400 text-xs rounded flex items-center gap-1",children:[s.jsx(oi,{className:"w-3 h-3"}),"Digest ",t.daily_digest_time]})]}),s.jsx("div",{className:"mb-3",children:s.jsxs(Se,{size:"sm",variant:"secondary",disabled:p.isPending,onClick:()=>{c(null),p.mutate()},className:"w-full",children:[p.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(GD,{className:"w-4 h-4"}),"Send Test Notification"]})}),l&&s.jsxs("div",{className:`mb-3 p-2 rounded-lg flex items-center gap-2 text-sm ${l.success?"bg-bambu-green/20 text-bambu-green":"bg-red-500/20 text-red-400"}`,children:[l.success?s.jsx(Un,{className:"w-4 h-4"}):s.jsx(ia,{className:"w-4 h-4"}),s.jsx("span",{children:l.message})]}),s.jsxs("button",{onClick:()=>o(!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:[s.jsxs("span",{className:"flex items-center gap-2",children:[s.jsx(Jd,{className:"w-4 h-4"}),"Event Settings"]}),a?s.jsx(pu,{className:"w-4 h-4"}):s.jsx(Oi,{className:"w-4 h-4"})]}),a&&s.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Enabled"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Send notifications from this provider"})]}),s.jsx(Gn,{checked:t.enabled,onChange:y=>f.mutate({enabled:y})})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Print Events"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Started"}),s.jsx(Gn,{checked:t.on_print_start,onChange:y=>f.mutate({on_print_start:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Plate Not Empty"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Objects detected before print"})]}),s.jsx(Gn,{checked:t.on_plate_not_empty??!0,onChange:y=>f.mutate({on_plate_not_empty:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Completed"}),s.jsx(Gn,{checked:t.on_print_complete,onChange:y=>f.mutate({on_print_complete:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Failed"}),s.jsx(Gn,{checked:t.on_print_failed,onChange:y=>f.mutate({on_print_failed:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Stopped"}),s.jsx(Gn,{checked:t.on_print_stopped,onChange:y=>f.mutate({on_print_stopped:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Progress Milestones"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify at 25%, 50%, 75%"})]}),s.jsx(Gn,{checked:t.on_print_progress,onChange:y=>f.mutate({on_print_progress:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Printer Status"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Printer Offline"}),s.jsx(Gn,{checked:t.on_printer_offline,onChange:y=>f.mutate({on_printer_offline:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Printer Error"}),s.jsx(Gn,{checked:t.on_printer_error,onChange:y=>f.mutate({on_printer_error:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Low Filament"}),s.jsx(Gn,{checked:t.on_filament_low,onChange:y=>f.mutate({on_filament_low:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Maintenance Due"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify when maintenance is needed"})]}),s.jsx(Gn,{checked:t.on_maintenance_due??!1,onChange:y=>f.mutate({on_maintenance_due:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS Alarms"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS Humidity High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS humidity exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_humidity_high??!1,onChange:y=>f.mutate({on_ams_humidity_high:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS Temperature High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS temperature exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_temperature_high??!1,onChange:y=>f.mutate({on_ams_temperature_high:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS-HT Alarms"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS-HT Humidity High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT humidity exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_ht_humidity_high??!1,onChange:y=>f.mutate({on_ams_ht_humidity_high:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS-HT Temperature High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT temperature exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_ht_temperature_high??!1,onChange:y=>f.mutate({on_ams_ht_temperature_high:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(F1,{className:"w-4 h-4 text-purple-400"}),s.jsx("p",{className:"text-sm text-white",children:"Quiet Hours"})]}),s.jsx(Gn,{checked:t.quiet_hours_enabled,onChange:y=>f.mutate({quiet_hours_enabled:y})})]}),t.quiet_hours_enabled&&s.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"No notifications during these hours"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zn,{className:"w-4 h-4 text-bambu-gray"}),s.jsxs("span",{className:"text-sm text-white",children:[b(t.quiet_hours_start)||"22:00"," - ",b(t.quiet_hours_end)||"07:00"]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change quiet hours"})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(oi,{className:"w-4 h-4 text-emerald-400"}),s.jsx("p",{className:"text-sm text-white",children:"Daily Digest"})]}),s.jsx(Gn,{checked:t.daily_digest_enabled,onChange:y=>f.mutate({daily_digest_enabled:y})})]}),t.daily_digest_enabled&&s.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zn,{className:"w-4 h-4 text-bambu-gray"}),s.jsxs("span",{className:"text-sm text-white",children:["Send at ",b(t.daily_digest_time)||"08:00"]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change digest time"})]})]}),s.jsxs("div",{className:"flex gap-2 pt-2",children:[s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[s.jsx(hN,{className:"w-4 h-4"}),"Edit"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>i(!0),className:"text-red-400 hover:text-red-300",children:s.jsx(dn,{className:"w-4 h-4"})})]})]})]})}),r&&s.jsx(Hn,{title:"Delete Notification Provider",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{m.mutate(),i(!1)},onCancel:()=>i(!1)})]})}const fq=[{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 RYe({provider:t,onClose:e}){const n=on(),r=!!t,[i,a]=w.useState(t?.name||""),[o,l]=w.useState(t?.provider_type||"email"),[c,u]=w.useState(t?.printer_id||null),[d,f]=w.useState(t?.quiet_hours_enabled||!1),[m,p]=w.useState(t?.quiet_hours_start||"22:00"),[b,y]=w.useState(t?.quiet_hours_end||"07:00"),[x,v]=w.useState(t?.daily_digest_enabled||!1),[S,M]=w.useState(t?.daily_digest_time||"08:00"),[A,T]=w.useState(t?.on_print_start??!1),[k,E]=w.useState(t?.on_print_complete??!0),[O,C]=w.useState(t?.on_print_failed??!0),[D,U]=w.useState(t?.on_print_stopped??!0),[L,q]=w.useState(t?.on_print_progress??!1),[P,W]=w.useState(t?.on_printer_offline??!1),[K,V]=w.useState(t?.on_printer_error??!1),[Q,re]=w.useState(t?.on_filament_low??!1),[J,z]=w.useState(t?.on_maintenance_due??!1),[G,ne]=w.useState(t?.config?Object.fromEntries(Object.entries(t.config).map(([B,I])=>[B,String(I)])):{}),[ae,de]=w.useState(null),[fe,ie]=w.useState(null),{data:Ae}=ut({queryKey:["printers"],queryFn:he.getPrinters});w.useEffect(()=>{const B=I=>{I.key==="Escape"&&e()};return window.addEventListener("keydown",B),()=>window.removeEventListener("keydown",B)},[e]);const me=mt({mutationFn:()=>he.testNotificationConfig({provider_type:o,config:G}),onSuccess:B=>{de(B),ie(null)},onError:B=>{de({success:!1,message:B.message})}}),Ne=mt({mutationFn:B=>he.createNotificationProvider(B),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:B=>{ie(B.message)}}),ye=mt({mutationFn:B=>he.updateNotificationProvider(t.id,B),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:B=>{ie(B.message)}}),le=B=>{if(B.preventDefault(),ie(null),!i.trim()){ie("Name is required");return}const I=ee(o);for(const Te of I)if(!G[Te.key]?.trim()){ie(`${Te.label} is required`);return}const ce={name:i.trim(),provider_type:o,config:G,printer_id:c,quiet_hours_enabled:d,quiet_hours_start:d?m:null,quiet_hours_end:d?b:null,daily_digest_enabled:x,daily_digest_time:x?S:null,on_print_start:A,on_print_complete:k,on_print_failed:O,on_print_stopped:D,on_print_progress:L,on_printer_offline:P,on_printer_error:K,on_filament_low:Q,on_maintenance_due:J};r?ye.mutate(ce):Ne.mutate(ce)},$e=Ne.isPending||ye.isPending,H=B=>{switch(B){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:"payload_format",label:"Payload Format",type:"select",required:!1,options:[{value:"generic",label:"Generic JSON"},{value:"slack",label:"Slack / Mattermost"}]},{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,showIf:I=>I.payload_format!=="slack"},{key:"field_message",label:"Message Field Name",placeholder:"message",type:"text",required:!1,showIf:I=>I.payload_format!=="slack"}];default:return[]}},ee=B=>H(B).filter(I=>I.required),X=H(o);return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4 overflow-y-auto",onClick:e,children:s.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:B=>B.stopPropagation(),children:[s.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Notification Provider":"Add Notification Provider"}),s.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:le,className:"p-6 space-y-4",children:[fe&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:fe}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),s.jsx("input",{type:"text",value:i,onChange:B=>a(B.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Provider Type *"}),s.jsx("select",{value:o,onChange:B=>{l(B.target.value),ne({}),de(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:fq.map(B=>s.jsx("option",{value:B.value,children:B.label},B.value))}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:fq.find(B=>B.value===o)?.description})]}),s.jsxs("div",{className:"space-y-3",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Configuration"}),X.filter(B=>!("showIf"in B)||B.showIf?.(G)!==!1).map(B=>s.jsxs("div",{children:[s.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[B.label," ",B.required&&"*"]}),B.type==="select"&&"options"in B&&B.options?s.jsx("select",{value:G[B.key]||B.options[0]?.value||"",onChange:I=>{ne({...G,[B.key]:I.target.value}),de(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:B.options.map(I=>s.jsx("option",{value:I.value,children:I.label},I.value))}):s.jsx("input",{type:B.type,value:G[B.key]||"",onChange:I=>{ne({...G,[B.key]:I.target.value}),de(null)},placeholder:B.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"})]},B.key))]}),s.jsx("div",{className:"flex gap-2",children:s.jsxs(Se,{type:"button",variant:"secondary",onClick:()=>{de(null),me.mutate()},disabled:me.isPending||!G[ee(o)[0]?.key],className:"flex-1",children:[me.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(GD,{className:"w-4 h-4"}),"Test Configuration"]})}),ae&&s.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${ae.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:ae.success?s.jsxs(s.Fragment,{children:[s.jsx(Un,{className:"w-5 h-5"}),s.jsx("span",{children:ae.message})]}):s.jsxs(s.Fragment,{children:[s.jsx(ia,{className:"w-5 h-5"}),s.jsx("span",{children:ae.message})]})}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer Filter"}),s.jsxs("select",{value:c??"",onChange:B=>u(B.target.value?Number(B.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:[s.jsx("option",{value:"",children:"All printers"}),Ae?.map(B=>s.jsx("option",{value:B.id,children:B.name},B.id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Only send notifications for events from this printer"})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("label",{className:"text-sm text-white",children:"Quiet Hours (Do Not Disturb)"}),s.jsx(Gn,{checked:d,onChange:f})]}),d&&s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Start"}),s.jsx("input",{type:"time",value:m,onChange:B=>p(B.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"End"}),s.jsx("input",{type:"time",value:b,onChange:B=>y(B.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"})]})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("label",{className:"text-sm text-white",children:"Daily Digest"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"})]}),s.jsx(Gn,{checked:x,onChange:v})]}),x&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Send digest at"}),s.jsx("input",{type:"time",value:S,onChange:B=>M(B.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Events will be collected and sent as a single summary at this time"})]})]}),s.jsxs("div",{className:"space-y-3",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Notification Events"}),s.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Print Events"}),s.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Start"}),s.jsx(Gn,{checked:A,onChange:T})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Complete"}),s.jsx(Gn,{checked:k,onChange:E})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Failed"}),s.jsx(Gn,{checked:O,onChange:C})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Stopped"}),s.jsx(Gn,{checked:D,onChange:U})]}),s.jsxs("div",{className:"flex items-center justify-between col-span-2",children:[s.jsxs("div",{children:[s.jsx("span",{className:"text-sm text-white",children:"Progress"}),s.jsx("span",{className:"text-xs text-bambu-gray ml-1",children:"(25%, 50%, 75%)"})]}),s.jsx(Gn,{checked:L,onChange:q})]})]})]}),s.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Printer Status"}),s.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Offline"}),s.jsx(Gn,{checked:P,onChange:W})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Error"}),s.jsx(Gn,{checked:K,onChange:V})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Low Filament"}),s.jsx(Gn,{checked:Q,onChange:re})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Maintenance"}),s.jsx(Gn,{checked:J,onChange:z})]})]})]})]}),s.jsxs("div",{className:"flex gap-3 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:$e,className:"flex-1",children:[$e?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(_s,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function DYe({template:t,onClose:e}){const n=on(),r=w.useRef(null),[i,a]=w.useState(t.title_template),[o,l]=w.useState(t.body_template),[c,u]=w.useState(null),[d,f]=w.useState(!0),{data:m}=ut({queryKey:["template-variables"],queryFn:he.getTemplateVariables}),p=m?.find(T=>T.event_type===t.event_type),{data:b,isLoading:y}=ut({queryKey:["template-preview",t.event_type,i,o],queryFn:()=>he.previewTemplate({event_type:t.event_type,title_template:i,body_template:o}),enabled:d&&i.length>0&&o.length>0});w.useEffect(()=>{const T=k=>{k.key==="Escape"&&e()};return window.addEventListener("keydown",T),()=>window.removeEventListener("keydown",T)},[e]);const x=mt({mutationFn:T=>he.updateNotificationTemplate(t.id,T),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-templates"]}),e()},onError:T=>{u(T.message)}}),v=mt({mutationFn:()=>he.resetNotificationTemplate(t.id),onSuccess:T=>{a(T.title_template),l(T.body_template),n.invalidateQueries({queryKey:["notification-templates"]})},onError:T=>{u(T.message)}}),S=T=>{if(T.preventDefault(),u(null),!i.trim()){u("Title is required");return}if(!o.trim()){u("Body is required");return}x.mutate({title_template:i,body_template:o})},M=T=>{const k=r.current;if(!k)return;const E=k.selectionStart,O=k.selectionEnd,C=o,D=C.substring(0,E),U=C.substring(O),L=D+`{${T}}`+U;l(L),setTimeout(()=>{k.focus();const q=E+T.length+2;k.setSelectionRange(q,q)},0)},A=i!==t.title_template||o!==t.body_template;return s.jsx("div",{className:"fixed inset-0 bg-black/80 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-2xl max-h-[90vh] flex flex-col",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary shrink-0",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Edit Template: ",t.name]}),s.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",children:s.jsx(Vt,{className:"w-5 h-5 text-bambu-gray"})})]}),s.jsxs("form",{onSubmit:S,className:"flex-1 overflow-y-auto p-4 space-y-4",children:[c&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded text-red-400 text-sm",children:c}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Title"}),s.jsx("input",{type:"text",value:i,onChange:T=>a(T.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..."})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Body"}),s.jsx("textarea",{ref:r,value:o,onChange:T=>l(T.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&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-2",children:"Available Variables"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:p.variables.map(T=>s.jsxs("button",{type:"button",onClick:()=>M(T),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:[s.jsx(nr,{className:"w-3 h-3"}),T]},T))}),s.jsx("p",{className:"text-xs text-bambu-gray/60 mt-1",children:"Click to insert at cursor position in body"})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsxs("label",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(Qd,{className:"w-4 h-4"}),"Live Preview"]}),s.jsx("button",{type:"button",onClick:()=>f(!d),className:"text-xs text-bambu-green hover:text-bambu-green-light",children:d?"Hide":"Show"})]}),d&&s.jsx("div",{className:"bg-bambu-dark border border-bambu-dark-tertiary rounded p-3 space-y-2",children:y?s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-sm",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),"Loading preview..."]}):b?s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Title:"}),s.jsx("div",{className:"text-white font-medium",children:b.title})]}),s.jsxs("div",{children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Body:"}),s.jsx("div",{className:"text-white whitespace-pre-wrap text-sm",children:b.body})]})]}):s.jsx("div",{className:"text-bambu-gray text-sm",children:"Enter template content to see preview"})})]})]}),s.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary shrink-0",children:[s.jsxs(Se,{type:"button",variant:"ghost",onClick:()=>v.mutate(),disabled:v.isPending,className:"text-orange-400 hover:text-orange-300",children:[v.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin mr-2"}):s.jsx(mm,{className:"w-4 h-4 mr-2"}),"Reset to Default"]}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),s.jsxs(Se,{onClick:S,disabled:x.isPending||!A,children:[x.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin mr-2"}):s.jsx(_s,{className:"w-4 h-4 mr-2"}),"Save"]})]})]})]})})}const OYe={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"},LYe={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 IYe({onClose:t}){const e=on(),{showToast:n}=qn(),[r,i]=w.useState(7),[a,o]=w.useState(null),[l,c]=w.useState(!1),{data:u}=ut({queryKey:["settings"],queryFn:he.getSettings}),d=u?.time_format||"system",{data:f,isLoading:m,refetch:p,isRefetching:b}=ut({queryKey:["notification-logs",r,l],queryFn:()=>he.getNotificationLogs({days:r,limit:100,success:l?!1:void 0})}),{data:y}=ut({queryKey:["notification-log-stats",r],queryFn:()=>he.getNotificationLogStats(r)}),x=mt({mutationFn:()=>he.clearNotificationLogs(30),onSuccess:S=>{n(S.message,"success"),e.invalidateQueries({queryKey:["notification-logs"]}),e.invalidateQueries({queryKey:["notification-log-stats"]})},onError:S=>{n(`Failed to clear logs: ${S.message}`,"error")}}),v=S=>{const M=Ni(S);if(!M)return"";const T=new Date().getTime()-M.getTime();return T<6e4?"Just now":T<36e5?`${Math.floor(T/6e4)}m ago`:T<864e5?`${Math.floor(T/36e5)}h ago`:M.toLocaleDateString()+" "+Yme(M,d)};return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:s.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:[s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx(P0,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Notification Log"})]}),s.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white transition-colors",children:"×"})]}),y&&s.jsx("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/50",children:s.jsxs("div",{className:"flex items-center gap-6 text-sm",children:[s.jsxs("span",{className:"text-bambu-gray",children:["Last ",r," days: ",s.jsx("span",{className:"text-white font-medium",children:y.total})," notifications"]}),s.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[s.jsx(Un,{className:"w-4 h-4"}),y.success_count," sent"]}),y.failure_count>0&&s.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[s.jsx(ia,{className:"w-4 h-4"}),y.failure_count," failed"]})]})}),s.jsxs("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary flex items-center gap-4",children:[s.jsxs("select",{value:r,onChange:S=>i(Number(S.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:[s.jsx("option",{value:1,children:"Last 24 hours"}),s.jsx("option",{value:7,children:"Last 7 days"}),s.jsx("option",{value:30,children:"Last 30 days"}),s.jsx("option",{value:90,children:"Last 90 days"})]}),s.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:l,onChange:S=>c(S.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),"Show failed only"]}),s.jsx("div",{className:"flex-1"}),s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>p(),disabled:b,children:[b?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Refresh"]}),s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>x.mutate(),disabled:x.isPending,className:"text-red-400 hover:text-red-300",children:[x.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"}),"Clear Old"]})]}),s.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:m?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):f&&f.length>0?s.jsx("div",{className:"space-y-2",children:f.map(S=>s.jsx(FYe,{log:S,isExpanded:a===S.id,onToggle:()=>o(a===S.id?null:S.id),formatDate:v,formatFullDate:M=>wg(M,d)},S.id))}):s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx(P0,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),s.jsx("p",{className:"text-sm",children:l?"No failed notifications":"No notifications logged"})]})})]})})}function FYe({log:t,isExpanded:e,onToggle:n,formatDate:r,formatFullDate:i}){return s.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:[s.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?s.jsx(Un,{className:"w-4 h-4 text-bambu-green shrink-0"}):s.jsx(ia,{className:"w-4 h-4 text-red-400 shrink-0"}),s.jsx("span",{className:`text-xs font-medium ${LYe[t.event_type]||"text-bambu-gray"}`,children:OYe[t.event_type]||t.event_type}),s.jsx("span",{className:"text-sm text-white truncate flex-1",children:t.provider_name||"Unknown Provider"}),t.printer_name&&s.jsx("span",{className:"text-xs text-bambu-gray",children:t.printer_name}),s.jsx("span",{className:"text-xs text-bambu-gray shrink-0",children:r(t.created_at)}),e?s.jsx(pu,{className:"w-4 h-4 text-bambu-gray shrink-0"}):s.jsx(Oi,{className:"w-4 h-4 text-bambu-gray shrink-0"})]}),e&&s.jsxs("div",{className:"px-3 py-2 border-t border-bambu-dark-tertiary bg-bambu-dark/20 space-y-2",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Title"}),s.jsx("p",{className:"text-sm text-white",children:t.title})]}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Message"}),s.jsx("p",{className:"text-sm text-white whitespace-pre-wrap",children:t.message})]}),!t.success&&t.error_message&&s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-red-400 mb-1",children:"Error"}),s.jsx("p",{className:"text-sm text-red-300",children:t.error_message})]}),s.jsxs("div",{className:"flex gap-4 text-xs text-bambu-gray pt-1",children:[s.jsxs("span",{children:["Provider: ",t.provider_type]}),s.jsxs("span",{children:["Time: ",i(t.created_at)]})]})]})]})}async function UYe(){const t=await fetch("/api/v1/settings/spoolman");if(!t.ok)throw new Error("Failed to load Spoolman settings");return t.json()}async function zYe(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 BYe(){const t=on(),[e,n]=w.useState(!1),[r,i]=w.useState(""),[a,o]=w.useState("auto"),[l,c]=w.useState(!1),[u,d]=w.useState("all"),[f,m]=w.useState(!1),[p,b]=w.useState(!1),{data:y,isLoading:x}=ut({queryKey:["spoolman-settings"],queryFn:UYe}),{data:v,isLoading:S,refetch:M}=ut({queryKey:["spoolman-status"],queryFn:he.getSpoolmanStatus,refetchInterval:3e4}),{data:A}=ut({queryKey:["printers"],queryFn:he.getPrinters});w.useEffect(()=>{y&&(n(y.spoolman_enabled==="true"),i(y.spoolman_url||""),o(y.spoolman_sync_mode||"auto"),m(!0))},[y]),w.useEffect(()=>{if(!f||!y)return;if(y.spoolman_enabled==="true"!==e||(y.spoolman_url||"")!==r||(y.spoolman_sync_mode||"auto")!==a){const W=setTimeout(()=>{T.mutate()},500);return()=>clearTimeout(W)}},[e,r,a,f]);const T=mt({mutationFn:()=>zYe({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)}}),k=mt({mutationFn:he.connectSpoolman,onSuccess:()=>{M()}}),E=mt({mutationFn:he.disconnectSpoolman,onSuccess:()=>{M()}}),O=mt({mutationFn:he.syncAllPrintersAms,onSuccess:P=>{P.success}}),C=mt({mutationFn:P=>he.syncPrinterAms(P),onSuccess:P=>{P.success}}),D=()=>{u==="all"?O.mutate():C.mutate(u)},U=O.isPending||C.isPending,L=u==="all"?O.data:C.data,q=u==="all"?O.isSuccess:C.isSuccess;return x?s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(su,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]})}),s.jsx(Nt,{children:s.jsx("div",{className:"flex justify-center py-8",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})})})]}):s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(su,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]}),T.isPending&&s.jsx(dt,{className:"w-4 h-4 text-bambu-green animate-spin"}),l&&s.jsx(ns,{className:"w-4 h-4 text-bambu-green"})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Connect to Spoolman for filament inventory tracking. AMS data will sync automatically."}),s.jsx("div",{className:"p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Ql,{className:"w-4 h-4 text-blue-400 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-xs text-blue-300",children:[s.jsx("p",{className:"font-medium mb-1",children:"How Sync Works"}),s.jsxs("ul",{className:"list-disc list-inside space-y-0.5 text-blue-300/80",children:[s.jsx("li",{children:"Only official Bambu Lab spools with RFID are synced"}),s.jsx("li",{children:"New spools are auto-created in Spoolman on first sync"}),s.jsx("li",{children:"Non-Bambu Lab spools (third-party, refilled) are skipped"})]}),s.jsx("p",{className:"font-medium mt-2 mb-1",children:"Linking Existing Spools"}),s.jsx("p",{className:"text-blue-300/80",children:'To link existing Spoolman spools to your AMS, hover over an AMS slot and click "Link to Spoolman".'})]})]})}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable Spoolman"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Sync filament data with Spoolman server"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:e,onChange:P=>n(P.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Spoolman URL"}),s.jsx("input",{type:"text",placeholder:"http://192.168.1.100:7912",value:r,onChange:P=>i(P.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"URL of your Spoolman server (e.g., http://localhost:7912)"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Sync Mode"}),s.jsxs("select",{value:a,onChange:P=>o(P.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:[s.jsx("option",{value:"auto",children:"Automatic"}),s.jsx("option",{value:"manual",children:"Manual Only"})]}),s.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&&s.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Status:"}),S?s.jsx(dt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):v?.connected?s.jsxs("span",{className:"flex items-center gap-1 text-sm text-green-500",children:[s.jsx(ns,{className:"w-4 h-4"}),"Connected"]}):s.jsxs("span",{className:"flex items-center gap-1 text-sm text-red-500",children:[s.jsx(Vt,{className:"w-4 h-4"}),"Disconnected"]})]}),s.jsx("div",{className:"flex gap-2",children:v?.connected?s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>E.mutate(),disabled:E.isPending,children:[E.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Ihe,{className:"w-4 h-4"}),"Disconnect"]}):s.jsxs(Se,{size:"sm",onClick:()=>k.mutate(),disabled:k.isPending||!r,children:[k.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Wl,{className:"w-4 h-4"}),"Connect"]})})]}),k.isError&&s.jsx("div",{className:"mb-3 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400",children:k.error.message}),v?.connected&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Sync AMS Data"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Manually sync printer AMS data to Spoolman"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsxs("select",{value:u,onChange:P=>d(P.target.value==="all"?"all":Number(P.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:[s.jsx("option",{value:"all",children:"All Printers"}),A?.map(P=>s.jsx("option",{value:P.id,children:P.name},P.id))]}),s.jsx(Oi,{className:"absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:D,disabled:U,children:[U?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Sync"]})]})]}),q&&L&&s.jsxs("div",{className:"mt-3 space-y-2",children:[s.jsx("div",{className:`p-2 rounded text-sm ${L.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:L.success?`Synced ${L.synced_count} spool${L.synced_count!==1?"s":""} successfully`:`Synced ${L.synced_count} spool${L.synced_count!==1?"s":""} with ${L.errors.length} error${L.errors.length!==1?"s":""}`}),L.skipped_count>0&&s.jsxs("div",{className:"p-2 bg-amber-500/10 border border-amber-500/30 rounded text-sm",children:[s.jsxs("div",{className:"flex items-center justify-between text-amber-400 mb-1",children:[s.jsxs("div",{className:"flex items-center gap-1.5",children:[s.jsx(kn,{className:"w-3.5 h-3.5"}),s.jsxs("span",{className:"font-medium",children:[L.skipped_count," spool",L.skipped_count!==1?"s":""," skipped"]})]}),L.skipped_count>5&&s.jsx("button",{onClick:()=>b(!p),className:"text-xs text-amber-400 hover:text-amber-300 underline",children:p?"Show less":"Show all"})]}),s.jsxs("ul",{className:"text-xs text-amber-300/80 space-y-0.5",children:[(p?L.skipped:L.skipped.slice(0,5)).map((P,W)=>s.jsxs("li",{className:"flex items-center gap-2",children:[P.color&&s.jsx("span",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:`#${P.color}`}}),s.jsx("span",{children:P.location}),s.jsxs("span",{className:"text-amber-300/60",children:["- ",P.reason]})]},W)),!p&&L.skipped_count>5&&s.jsxs("li",{className:"text-amber-300/60 italic",children:["...and ",L.skipped_count-5," more"]})]})]}),L.errors.length>0&&s.jsxs("div",{className:"p-2 bg-red-500/10 border border-red-500/30 rounded text-sm",children:[s.jsx("div",{className:"text-red-400 font-medium mb-1",children:"Errors:"}),s.jsx("ul",{className:"text-xs text-red-300/80 space-y-0.5",children:L.errors.map((P,W)=>s.jsx("li",{children:P},W))})]})]})]})]})]})}function $Ye({link:t,onClose:e}){const n=on(),{mode:r}=hh(),i=!!t,a=w.useRef(null),[o,l]=w.useState(t?.name||""),[c,u]=w.useState(t?.url||""),[d,f]=w.useState(t?.icon||"link"),[m,p]=w.useState(!!t?.custom_icon),[b,y]=w.useState(t?.custom_icon?he.getExternalLinkIconUrl(t.id):null),[x,v]=w.useState(null),[S,M]=w.useState(null);w.useEffect(()=>{const U=L=>{L.key==="Escape"&&e()};return window.addEventListener("keydown",U),()=>window.removeEventListener("keydown",U)},[e]);const A=mt({mutationFn:async U=>{const L=await he.createExternalLink(U);return x?await he.uploadExternalLinkIcon(L.id,x):L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:U=>{M(U.message)}}),T=mt({mutationFn:async U=>{let L=await he.updateExternalLink(t.id,U);return x?L=await he.uploadExternalLinkIcon(t.id,x):!m&&t?.custom_icon&&(L=await he.deleteExternalLinkIcon(t.id)),L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:U=>{M(U.message)}}),k=U=>{const L=U.target.files?.[0];if(L){if(!["image/png","image/jpeg","image/gif","image/svg+xml","image/webp","image/x-icon"].includes(L.type)){M("Please select a valid image file (PNG, JPG, GIF, SVG, WebP, or ICO)");return}if(L.size>1024*1024){M("Image file must be less than 1MB");return}v(L),p(!0);const P=new FileReader;P.onload=W=>{y(W.target?.result)},P.readAsDataURL(L)}},E=()=>{v(null),y(null),p(!1),a.current&&(a.current.value="")},O=U=>{if(U.preventDefault(),M(null),!o.trim()){M("Name is required");return}if(!c.trim()){M("URL is required");return}if(!c.startsWith("http://")&&!c.startsWith("https://")){M("URL must start with http:// or https://");return}const L={name:o.trim(),url:c.trim(),icon:d};i?T.mutate(L):A.mutate(L)},C=A.isPending||T.isPending,D=gN(d);return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-md",onClick:U=>U.stopPropagation(),children:[s.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:m&&b?s.jsx("img",{src:b,alt:"",className:`w-5 h-5 rounded ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}):s.jsx(D,{className:"w-5 h-5"})}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:i?"Edit Link":"Add External Link"})]}),s.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:O,className:"p-6 space-y-4",children:[S&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:S}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),s.jsx("input",{type:"text",value:o,onChange:U=>l(U.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"URL *"}),s.jsx("input",{type:"text",value:c,onChange:U=>u(U.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"})]}),s.jsxs("div",{className:"space-y-3",children:[s.jsx("label",{className:"block text-sm text-bambu-gray",children:"Icon"}),s.jsxs("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm text-white",children:"Custom Icon"}),s.jsx("input",{ref:a,type:"file",accept:"image/png,image/jpeg,image/gif,image/svg+xml,image/webp,image/x-icon",className:"hidden",onChange:k}),m&&b?s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("img",{src:b,alt:"Custom icon",className:`w-8 h-8 rounded border border-bambu-dark-tertiary ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}),s.jsx("button",{type:"button",onClick:E,className:"p-1 text-red-400 hover:text-red-300 transition-colors",title:"Remove custom icon",children:s.jsx(dn,{className:"w-4 h-4"})})]}):s.jsxs(Se,{type:"button",variant:"secondary",size:"sm",onClick:()=>a.current?.click(),children:[s.jsx(pi,{className:"w-4 h-4"}),"Upload"]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"PNG, JPG, GIF, SVG, WebP, or ICO. Max 1MB."})]}),!m&&s.jsxs("div",{children:[s.jsx("span",{className:"text-sm text-bambu-gray block mb-2",children:"Or choose a preset icon"}),s.jsx(Vme,{value:d,onChange:f})]})]}),s.jsxs("div",{className:"flex gap-3 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:C,className:"flex-1",children:[C?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(_s,{className:"w-4 h-4"}),i?"Save":"Add"]})]})]})]})})}function HYe(){const t=on(),[e,n]=w.useState(!1),[r,i]=w.useState(null),[a,o]=w.useState(null),[l,c]=w.useState(null),{data:u,isLoading:d}=ut({queryKey:["external-links"],queryFn:he.getExternalLinks}),f=mt({mutationFn:S=>he.deleteExternalLink(S),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),m=mt({mutationFn:S=>he.reorderExternalLinks(S),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),p=(S,M)=>{c(M),S.dataTransfer.effectAllowed="move"},b=S=>{S.preventDefault(),S.dataTransfer.dropEffect="move"},y=(S,M)=>{if(S.preventDefault(),l===null||l===M||!u)return;const A=u.map(O=>O.id),T=A.indexOf(l),k=A.indexOf(M);if(T===-1||k===-1)return;const E=[...A];E.splice(T,1),E.splice(k,0,l),m.mutate(E),c(null)},x=S=>{o(S)},v=()=>{a&&(f.mutate(a.id),o(null))};return s.jsxs(s.Fragment,{children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Wl,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Sidebar Links"})]}),s.jsxs(Se,{size:"sm",onClick:()=>n(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Link"]})]})}),s.jsxs(Nt,{children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Add external links to the sidebar navigation. Drag to reorder."}),d?s.jsx("div",{className:"flex justify-center py-8",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):u&&u.length>0?s.jsx("div",{className:"space-y-2",children:u.map(S=>{const M=gN(S.icon);return s.jsxs("div",{draggable:!0,onDragStart:A=>p(A,S.id),onDragOver:b,onDrop:A=>y(A,S.id),className:`flex items-center gap-3 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary transition-colors ${l===S.id?"opacity-50":""}`,children:[s.jsx(yg,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray cursor-grab flex-shrink-0"}),s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-gray",children:s.jsx(M,{className:"w-4 h-4"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-white font-medium truncate",children:S.name}),s.jsx(zr,{className:"w-3 h-3 text-bambu-gray flex-shrink-0"})]}),s.jsx("span",{className:"text-sm text-bambu-gray truncate block",children:S.url})]}),s.jsxs("div",{className:"flex items-center gap-1 flex-shrink-0",children:[s.jsx("button",{onClick:()=>i(S),className:"p-2 rounded-lg hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors",title:"Edit",children:s.jsx(la,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>x(S),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:s.jsx(dn,{className:"w-4 h-4"})})]})]},S.id)})}):s.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[s.jsx(Wl,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),s.jsx("p",{children:"No external links configured"}),s.jsx("p",{className:"text-sm",children:'Click "Add Link" to add one'})]})]})]}),(e||r)&&s.jsx($Ye,{link:r,onClose:()=>{n(!1),i(null)}}),a&&s.jsx(Hn,{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:v,onCancel:()=>o(null)})]})}function VYe(){const t=on(),{showToast:e}=qn(),[n,r]=w.useState(!1),[i,a]=w.useState(""),[o,l]=w.useState("immediate"),[c,u]=w.useState("3DPrinter-X1-Carbon"),[d,f]=w.useState(!1),[m,p]=w.useState(null),{data:b,isLoading:y}=ut({queryKey:["virtual-printer-settings"],queryFn:r1.getSettings,refetchInterval:1e4}),{data:x}=ut({queryKey:["virtual-printer-models"],queryFn:r1.getModels});w.useEffect(()=>{if(b){r(b.enabled);let O=b.mode==="queue"?"review":b.mode;O!=="immediate"&&O!=="review"&&O!=="print_queue"&&(O="immediate"),l(O),u(b.model)}},[b]);const v=mt({mutationFn:O=>r1.updateSettings(O),onSuccess:()=>{t.invalidateQueries({queryKey:["virtual-printer-settings"]}),e("Virtual printer settings updated"),p(null)},onError:O=>{if(e(O.message||"Failed to update settings","error"),b){r(b.enabled);const C=b.mode==="queue"?"review":b.mode;l(C==="print_queue"||C==="review"?C:"immediate"),u(b.model)}p(null)}}),S=()=>{const O=!n;if(O&&!i&&!b?.access_code_set){e("Please set an access code first","error");return}r(O),p("toggle"),v.mutate({enabled:O,access_code:i||void 0,mode:o})},M=()=>{if(!i){e("Access code cannot be empty","error");return}if(i.length!==8){e("Access code must be exactly 8 characters","error");return}p("accessCode"),v.mutate({access_code:i}),a("")},A=O=>{l(O),p("mode"),v.mutate({mode:O})},T=O=>{u(O),p("model"),v.mutate({model:O})};if(y)return s.jsx(St,{children:s.jsx(Nt,{className:"py-8 flex justify-center",children:s.jsx(dt,{className:"w-6 h-6 animate-spin text-bambu-green"})})});const k=b?.status,E=k?.running||!1;return s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[s.jsx("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(qr,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Virtual Printer"})]}),k&&s.jsxs("div",{className:`flex items-center gap-2 text-sm ${E?"text-green-400":"text-bambu-gray"}`,children:[s.jsx("span",{className:`w-2 h-2 rounded-full ${E?"bg-green-400 animate-pulse":"bg-gray-500"}`}),E?"Running":"Stopped"]})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.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."}),s.jsxs("div",{className:"flex items-center justify-between py-3 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("div",{className:"text-white font-medium",children:"Enable Virtual Printer"}),s.jsx("div",{className:"text-sm text-bambu-gray",children:E?'Visible as "Bambuddy" in slicer discovery':"Not visible to slicers"})]}),s.jsx("button",{onClick:S,disabled:m==="toggle",className:`relative w-12 h-6 rounded-full transition-colors ${n?"bg-bambu-green":"bg-bambu-dark-tertiary"} ${m==="toggle"?"opacity-50":""}`,children:s.jsx("span",{className:`absolute top-1 left-1 w-4 h-4 bg-white rounded-full transition-transform ${n?"translate-x-6":""}`})})]}),s.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[s.jsx("div",{className:"text-white font-medium mb-2",children:"Printer Model"}),s.jsx("div",{className:"text-sm text-bambu-gray mb-3",children:"Select which printer model to emulate."}),s.jsxs("div",{className:"relative",children:[s.jsx("select",{value:c,onChange:O=>T(O.target.value),disabled:m==="model",className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-2 text-white appearance-none cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed pr-10",children:x?.models&&Object.entries(x.models).sort(([,O],[,C])=>O.localeCompare(C)).map(([O,C])=>s.jsx("option",{value:O,children:C},O))}),s.jsx(Oi,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"})]}),n&&E&&s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:[s.jsx(Ql,{className:"w-3 h-3 inline mr-1"}),"Changing the model will restart the virtual printer"]})]}),s.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[s.jsx("div",{className:"text-white font-medium mb-2",children:"Access Code"}),s.jsx("div",{className:"text-sm text-bambu-gray mb-3",children:b?.access_code_set?s.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[s.jsx(ns,{className:"w-4 h-4"}),"Access code is set"]}):s.jsxs("span",{className:"flex items-center gap-1 text-yellow-400",children:[s.jsx(kn,{className:"w-4 h-4"}),"No access code set - required to enable"]})}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsx("input",{type:d?"text":"password",value:i,onChange:O=>a(O.target.value),placeholder:b?.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"}),s.jsx("button",{onClick:()=>f(!d),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:d?s.jsx(My,{className:"w-4 h-4"}):s.jsx(Qd,{className:"w-4 h-4"})})]}),s.jsx(Se,{onClick:M,disabled:!i||m==="accessCode",variant:"primary",children:m==="accessCode"?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):"Save"})]}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["Must be exactly 8 characters. Used by slicers to authenticate.",i&&s.jsxs("span",{className:i.length===8?"text-green-400":"text-yellow-400",children:[" ","(",i.length,"/8)"]})]})]}),s.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[s.jsx("div",{className:"text-white font-medium mb-2",children:"Mode"}),s.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[s.jsxs("button",{onClick:()=>A("immediate"),disabled:m==="mode",className:`p-3 rounded-lg border text-left transition-colors ${o==="immediate"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[s.jsx("div",{className:"text-white font-medium",children:"Archive"}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Archive files immediately"})]}),s.jsxs("button",{onClick:()=>A("review"),disabled:m==="mode",className:`p-3 rounded-lg border text-left transition-colors ${o==="review"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[s.jsx("div",{className:"text-white font-medium",children:"Review"}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Review and tag before archiving"})]}),s.jsxs("button",{onClick:()=>A("print_queue"),disabled:m==="mode",className:`p-3 rounded-lg border text-left transition-colors ${o==="print_queue"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[s.jsx("div",{className:"text-white font-medium",children:"Queue"}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Archive and add to print queue"})]})]})]})]})]})}),s.jsxs("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:[s.jsx(St,{className:"border-l-4 border-l-yellow-500",children:s.jsx(Nt,{className:"py-4",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(kn,{className:"w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-sm",children:[s.jsx("p",{className:"text-white font-medium mb-2",children:"Setup Required"}),s.jsx("p",{className:"text-bambu-gray mb-3",children:"The virtual printer feature requires additional system configuration before it will work. This includes port forwarding, firewall rules, and platform-specific settings."}),s.jsxs("a",{href:"https://wiki.bambuddy.cool/features/virtual-printer/",target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-2 px-4 py-2 bg-yellow-500/20 border border-yellow-500/50 rounded-md text-yellow-400 hover:bg-yellow-500/30 transition-colors",children:[s.jsx(zr,{className:"w-4 h-4"}),"Read the setup guide before enabling"]})]})]})})}),s.jsx(St,{children:s.jsx(Nt,{className:"py-4",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(Ql,{className:"w-5 h-5 text-blue-400 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-sm text-bambu-gray",children:[s.jsx("p",{className:"mb-2",children:s.jsx("strong",{className:"text-white",children:"How it works:"})}),s.jsxs("ol",{className:"list-decimal list-inside space-y-1",children:[s.jsx("li",{children:"Complete the setup guide for your platform"}),s.jsx("li",{children:"Enable the virtual printer and set an access code"}),s.jsx("li",{children:'In Bambu Studio or OrcaSlicer, go to "Add Printer"'}),s.jsx("li",{children:'The "Bambuddy" printer should appear in the discovery list'}),s.jsx("li",{children:"Connect using the access code you set"}),s.jsx("li",{children:'When you "print" to Bambuddy, the 3MF file is archived instead'})]})]})]})})}),k&&E&&s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h3",{className:"text-md font-semibold text-white",children:"Status Details"})}),s.jsx(Nt,{children:s.jsxs("div",{className:"grid grid-cols-2 gap-4 text-sm",children:[s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Printer Name"}),s.jsx("div",{className:"text-white",children:k.name})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Model"}),s.jsx("div",{className:"text-white",children:k.model_name||k.model})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Serial Number"}),s.jsx("div",{className:"text-white font-mono",children:k.serial})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Mode"}),s.jsx("div",{className:"text-white capitalize",children:k.mode})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Pending Files"}),s.jsx("div",{className:"text-white",children:k.pending_files})]})]})})]})]})]})}const Jw=[{id:"settings",labelKey:"backup.categories.settings",defaultLabel:"App Settings",icon:s.jsx(mN,{className:"w-4 h-4"}),default:!0,description:"Language, theme, update preferences"},{id:"notifications",labelKey:"backup.categories.notifications",defaultLabel:"Notification Providers",icon:s.jsx(Ef,{className:"w-4 h-4"}),default:!0,description:"ntfy, Pushover, Discord, etc."},{id:"templates",labelKey:"backup.categories.templates",defaultLabel:"Notification Templates",icon:s.jsx(Za,{className:"w-4 h-4"}),default:!0,description:"Custom message templates"},{id:"smart_plugs",labelKey:"backup.categories.smartPlugs",defaultLabel:"Smart Plugs",icon:s.jsx(Ks,{className:"w-4 h-4"}),default:!0,description:"Tasmota plug configurations"},{id:"external_links",labelKey:"backup.categories.externalLinks",defaultLabel:"External Links",icon:s.jsx(Jg,{className:"w-4 h-4"}),default:!0,description:"Sidebar links to external services"},{id:"printers",labelKey:"backup.categories.printers",defaultLabel:"Printers",icon:s.jsx(qr,{className:"w-4 h-4"}),default:!1,description:"Printer info (access codes excluded)"},{id:"plate_calibration",labelKey:"backup.categories.plateCalibration",defaultLabel:"Plate Detection",icon:s.jsx(Hf,{className:"w-4 h-4"}),default:!1,description:"Empty plate reference images",requiresPrinters:!0},{id:"filaments",labelKey:"backup.categories.filaments",defaultLabel:"Filament Inventory",icon:s.jsx(HD,{className:"w-4 h-4"}),default:!1,description:"Filament types and costs"},{id:"maintenance",labelKey:"backup.categories.maintenance",defaultLabel:"Maintenance Types",icon:s.jsx(eh,{className:"w-4 h-4"}),default:!1,description:"Custom maintenance schedules"},{id:"archives",labelKey:"backup.categories.archives",defaultLabel:"Print Archives",icon:s.jsx(es,{className:"w-4 h-4"}),default:!1,description:"All print data + files (3MF, thumbnails, photos)"},{id:"projects",labelKey:"backup.categories.projects",defaultLabel:"Projects",icon:s.jsx(Ta,{className:"w-4 h-4"}),default:!1,description:"Projects, BOM items, and attachments"},{id:"pending_uploads",labelKey:"backup.categories.pendingUploads",defaultLabel:"Pending Uploads",icon:s.jsx(pi,{className:"w-4 h-4"}),default:!1,description:"Virtual printer uploads awaiting review"},{id:"api_keys",labelKey:"backup.categories.apiKeys",defaultLabel:"API Keys",icon:s.jsx(mf,{className:"w-4 h-4"}),default:!1,description:"Webhook API keys (new keys generated on import)"}];function GYe({onClose:t,onExport:e}){const{t:n}=Xg(),[r,i]=w.useState(()=>{const b={};return Jw.forEach(y=>{b[y.id]=y.default}),b}),[a,o]=w.useState(!1),[l,c]=w.useState(!1);w.useEffect(()=>{const b=y=>{y.key==="Escape"&&t()};return window.addEventListener("keydown",b),()=>window.removeEventListener("keydown",b)},[t]);const u=b=>{i(y=>({...y,[b]:!y[b]}))},d=()=>{const b={};Jw.forEach(y=>{b[y.id]=!0}),i(b)},f=()=>{const b={};Jw.forEach(y=>{b[y.id]=!1}),i(b)},m=Object.values(r).filter(Boolean).length,p=async()=>{c(!0);try{await e({...r,access_codes:a&&r.printers})}finally{c(!1)}};return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:l?void 0:t,children:s.jsx(St,{className:"w-full max-w-lg",onClick:b=>b.stopPropagation(),children:s.jsxs(Nt,{className:"p-0",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:s.jsx(Hr,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:n("backup.exportTitle",{defaultValue:"Export Backup"})}),s.jsx("p",{className:"text-sm text-bambu-gray",children:n("backup.selectCategories",{defaultValue:"Select data to include"})})]})]}),s.jsx("button",{onClick:t,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("div",{className:"flex gap-2 px-4 pt-4",children:[s.jsx("button",{onClick:d,disabled:l,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"})}),s.jsx("span",{className:"text-bambu-gray",children:"|"}),s.jsx("button",{onClick:f,disabled:l,className:"text-sm text-bambu-gray hover:text-white disabled:opacity-50 disabled:cursor-not-allowed",children:n("common.selectNone",{defaultValue:"Select None"})})]}),s.jsx("div",{className:`p-4 space-y-2 max-h-[400px] overflow-y-auto ${l?"opacity-50 pointer-events-none":""}`,children:Jw.map(b=>{const y=l||b.requiresPrinters&&!r.printers;return s.jsxs("label",{className:`flex items-center gap-3 p-3 rounded-lg transition-colors ${y?"cursor-not-allowed opacity-50":"cursor-pointer"} ${r[b.id]&&!y?"bg-bambu-green/10 border border-bambu-green/30":"bg-bambu-dark hover:bg-bambu-dark-tertiary border border-transparent"}`,children:[s.jsx("input",{type:"checkbox",checked:r[b.id]&&!y,onChange:()=>u(b.id),disabled:y,className:"w-4 h-4 rounded border-bambu-gray bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0"}),s.jsx("div",{className:`${r[b.id]&&!y?"text-bambu-green":"text-bambu-gray"}`,children:b.icon}),s.jsxs("div",{className:"flex-1",children:[s.jsx("div",{className:"text-white text-sm font-medium",children:n(b.labelKey,{defaultValue:b.defaultLabel})}),s.jsx("div",{className:"text-xs text-bambu-gray",children:b.requiresPrinters&&!r.printers?"Requires Printers to be selected":b.description})]})]},b.id)})}),r.archives&&s.jsx("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[s.jsx(es,{className:"w-4 h-4 text-yellow-500 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"text-yellow-200 dark:text-yellow-200 text-yellow-700",children:[s.jsx("span",{className:"font-medium",children:"ZIP file will be created."}),s.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&&s.jsxs("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-start gap-2",children:[s.jsx(mf,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm font-medium text-white",children:"Include Access Codes"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"For transferring to another machine"})]})]}),s.jsx(Gn,{checked:a,onChange:o})]}),a&&s.jsx("div",{className:"mt-2 p-2 rounded bg-orange-500/10 border border-orange-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-xs",children:[s.jsx(kn,{className:"w-3 h-3 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),s.jsx("span",{className:"text-orange-700 dark:text-orange-200",children:"Access codes will be included in plain text. Keep this backup file secure!"})]})})]}),s.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:n("backup.selectedCount",{count:m,defaultValue:`${m} categories selected`})}),s.jsxs("div",{className:"flex gap-3",children:[s.jsx(Se,{variant:"secondary",onClick:t,disabled:l,children:n("common.cancel",{defaultValue:"Cancel"})}),s.jsx(Se,{onClick:p,disabled:m===0||l,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50 disabled:cursor-not-allowed min-w-[100px]",children:l?s.jsxs(s.Fragment,{children:[s.jsx(dt,{className:"w-4 h-4 mr-2 animate-spin"}),n("backup.exporting",{defaultValue:"Exporting..."})]}):s.jsxs(s.Fragment,{children:[s.jsx(Hr,{className:"w-4 h-4 mr-2"}),n("backup.export",{defaultValue:"Export"})]})})]})]})]})})})}const mq={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",api_keys:"API Keys"};function WYe({onClose:t,onRestore:e,onSuccess:n}){const[r,i]=w.useState("options"),[a,o]=w.useState(!1),[l,c]=w.useState(null),[u,d]=w.useState(null),[f,m]=w.useState(new Set),p=w.useRef(null);w.useEffect(()=>{const A=T=>{T.key==="Escape"&&r!=="restoring"&&(r==="result"&&u?.success&&n(),t())};return window.addEventListener("keydown",A),()=>window.removeEventListener("keydown",A)},[t,n,r,u]);const b=A=>{const T=A.target.files?.[0];T&&c(T)},y=async()=>{if(l){i("restoring");try{const A=await e(l,a);d(A),i("result")}catch{d({success:!1,message:"Failed to restore backup. Please check the file format."}),i("result")}}},x=()=>{u?.success&&n(),t()},v=A=>{m(T=>{const k=new Set(T);return k.has(A)?k.delete(A):k.add(A),k})},S=u?.restored?Object.values(u.restored).reduce((A,T)=>A+T,0)+(u.files_restored||0):0,M=u?.total_skipped||0;return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onMouseDown:A=>{A.target===A.currentTarget&&r!=="restoring"&&t()},children:s.jsx(St,{className:"w-full max-w-lg",children:s.jsxs(Nt,{className:"p-0",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.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?s.jsx(Un,{className:"w-5 h-5"}):r==="result"&&!u?.success?s.jsx(kn,{className:"w-5 h-5"}):s.jsx(pi,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.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")]}),s.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"&&s.jsx("button",{onClick:x,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),r==="options"&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("input",{ref:p,type:"file",accept:".json,.zip",className:"hidden",onChange:b}),s.jsx("button",{type:"button",onClick:()=>p.current?.click(),className:`w-full p-4 border-2 border-dashed rounded-lg transition-colors ${l?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:l?s.jsxs("div",{className:"flex items-center justify-center gap-2 text-bambu-green",children:[s.jsx(Un,{className:"w-5 h-5"}),s.jsx("span",{className:"font-medium",children:l.name})]}):s.jsxs("div",{className:"flex flex-col items-center gap-2 text-bambu-gray",children:[s.jsx(pi,{className:"w-8 h-8"}),s.jsx("span",{children:"Click to select backup file (.json or .zip)"})]})})]}),s.jsx("div",{className:"p-3 rounded-lg bg-blue-500/10 border border-blue-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[s.jsx(kn,{className:"w-4 h-4 text-blue-500 dark:text-blue-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"text-blue-700 dark:text-blue-200",children:[s.jsx("p",{className:"font-medium mb-1",children:"How duplicate handling works:"}),s.jsxs("ul",{className:"text-blue-600 dark:text-blue-200/80 space-y-1 text-xs",children:[s.jsxs("li",{children:[s.jsx("strong",{children:"Printers"})," - matched by serial number"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Smart Plugs"})," - matched by IP address"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Notification Providers"})," - matched by name"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Filaments"})," - matched by name + type + brand"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Archives"})," - matched by content hash (always skipped)"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Pending Uploads"})," - matched by filename"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Settings & Templates"})," - always overwritten"]})]})]})]})}),s.jsx("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsxs("p",{className:"text-white font-medium flex items-center gap-2",children:[a?s.jsx(xr,{className:"w-4 h-4 text-orange-400"}):s.jsx(R0,{className:"w-4 h-4 text-bambu-gray"}),a?"Replace existing data":"Keep existing data"]}),s.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"})]}),s.jsx(Gn,{checked:a,onChange:o})]})}),a&&s.jsx("div",{className:"p-3 rounded-lg bg-orange-500/10 border border-orange-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[s.jsx(kn,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"text-orange-700 dark:text-orange-200",children:[s.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."]})]})})]}),s.jsxs("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:t,children:"Cancel"}),s.jsxs(Se,{type:"button",onClick:y,disabled:!l,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50",children:[s.jsx(pi,{className:"w-4 h-4 mr-2"}),"Restore"]})]})]}),r==="restoring"&&s.jsxs("div",{className:"p-8 flex flex-col items-center gap-4",children:[s.jsx(dt,{className:"w-12 h-12 text-bambu-green animate-spin"}),s.jsx("p",{className:"text-bambu-gray",children:"Processing backup file..."})]}),r==="result"&&u&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"p-4 space-y-4 max-h-[400px] overflow-y-auto",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{className:"p-3 rounded-lg bg-bambu-green/10 border border-bambu-green/30",children:[s.jsx("div",{className:"text-2xl font-bold text-bambu-green",children:S}),s.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Restored"})]}),s.jsxs("div",{className:"p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:[s.jsx("div",{className:"text-2xl font-bold text-yellow-500",children:M}),s.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Skipped"})]})]}),u.restored&&Object.entries(u.restored).some(([,A])=>A>0)&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green"}),"Restored"]}),s.jsxs("div",{className:"space-y-1",children:[Object.entries(u.restored).filter(([,A])=>A>0).map(([A,T])=>s.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[s.jsx("span",{className:"text-white",children:mq[A]||A}),s.jsx("span",{className:"text-bambu-green font-medium",children:T})]},A)),(u.files_restored||0)>0&&s.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[s.jsx("span",{className:"text-white",children:"Files (3MF, thumbnails, etc.)"}),s.jsx("span",{className:"text-bambu-green font-medium",children:u.files_restored})]})]})]}),u.skipped&&Object.entries(u.skipped).some(([,A])=>A>0)&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(R0,{className:"w-4 h-4 text-yellow-500"}),"Skipped (already exist)"]}),s.jsx("div",{className:"space-y-1",children:Object.entries(u.skipped).filter(([,A])=>A>0).map(([A,T])=>{const k=u.skipped_details?.[A]||[],E=f.has(A);return s.jsxs("div",{children:[s.jsxs("button",{onClick:()=>k.length>0&&v(A),className:`w-full flex items-center justify-between text-sm p-2 rounded bg-bambu-dark ${k.length>0?"hover:bg-bambu-dark-tertiary cursor-pointer":""}`,children:[s.jsxs("span",{className:"text-white flex items-center gap-2",children:[mq[A]||A,k.length>0&&(E?s.jsx(pu,{className:"w-3 h-3"}):s.jsx(Oi,{className:"w-3 h-3"}))]}),s.jsx("span",{className:"text-yellow-500 font-medium",children:T})]}),E&&k.length>0&&s.jsxs("div",{className:"mt-1 ml-4 p-2 rounded bg-bambu-dark-tertiary text-xs text-bambu-gray space-y-1",children:[k.slice(0,10).map((O,C)=>s.jsx("div",{children:O},C)),k.length>10&&s.jsxs("div",{className:"text-bambu-gray/60",children:["...and ",k.length-10," more"]})]})]},A)})})]}),u.new_api_keys&&u.new_api_keys.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(kn,{className:"w-4 h-4 text-orange-500"}),"New API Keys Generated"]}),s.jsxs("div",{className:"p-3 rounded bg-orange-500/10 border border-orange-500/30",children:[s.jsx("p",{className:"text-xs text-orange-200 mb-2",children:"These keys are only shown once. Copy them now!"}),s.jsx("div",{className:"space-y-2",children:u.new_api_keys.map((A,T)=>s.jsxs("div",{className:"p-2 rounded bg-bambu-dark",children:[s.jsx("div",{className:"text-sm text-white font-medium mb-1",children:A.name}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("code",{className:"text-xs text-bambu-green bg-bambu-dark-tertiary px-2 py-1 rounded font-mono flex-1 break-all",children:A.key}),s.jsx("button",{onClick:()=>navigator.clipboard.writeText(A.key),className:"text-xs text-bambu-gray hover:text-white px-2 py-1 rounded bg-bambu-dark-tertiary",children:"Copy"})]})]},T))})]})]}),S===0&&M===0&&s.jsx("div",{className:"p-4 text-center text-bambu-gray",children:"No data was found to restore in the backup file."})]}),s.jsx("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:s.jsx(Se,{onClick:x,children:"Close"})})]})]})})})}function pq({status:t}){if(!t)return null;const e={success:"bg-green-500/20 text-green-400",failed:"bg-red-500/20 text-red-400",skipped:"bg-yellow-500/20 text-yellow-400",running:"bg-blue-500/20 text-blue-400"},n={success:s.jsx(Un,{className:"w-3 h-3"}),failed:s.jsx(ia,{className:"w-3 h-3"}),skipped:s.jsx(R0,{className:"w-3 h-3"}),running:s.jsx(dt,{className:"w-3 h-3 animate-spin"})};return s.jsxs("span",{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium ${e[t]||"bg-gray-500/20 text-gray-400"}`,children:[n[t],t.charAt(0).toUpperCase()+t.slice(1)]})}function qYe(t){return t?new Date(t).toLocaleString():"-"}function gq(t){if(!t)return"-";const e=new Date(t),n=new Date,r=e.getTime()-n.getTime(),i=Math.round(r/6e4);if(i<0){const a=Math.abs(i);if(a<60)return`${a}m ago`;const o=Math.floor(a/60);return o<24?`${o}h ago`:`${Math.floor(o/24)}d ago`}else{if(i<60)return`in ${i}m`;const a=Math.floor(i/60);return a<24?`in ${a}h`:`in ${Math.floor(a/24)}d`}}function KYe(){const t=on(),{showToast:e}=qn(),[n,r]=w.useState(""),[i,a]=w.useState(""),[o,l]=w.useState("main"),[c,u]=w.useState(!1),[d,f]=w.useState(!1),[m,p]=w.useState("daily"),[b,y]=w.useState(!0),[x,v]=w.useState(!0),[S,M]=w.useState(!1),[A,T]=w.useState(!0),[k,E]=w.useState(!1),[O,C]=w.useState(!1),[D,U]=w.useState(!1),[L,q]=w.useState(null),P=w.useRef(null),W=w.useRef(!1),{data:K,isLoading:V}=ut({queryKey:["github-backup-config"],queryFn:he.getGitHubBackupConfig}),{data:Q}=ut({queryKey:["github-backup-status"],queryFn:he.getGitHubBackupStatus,refetchInterval:ie=>ie.state.data?.is_running?500:1e4}),{data:re}=ut({queryKey:["github-backup-logs"],queryFn:()=>he.getGitHubBackupLogs(20)}),{data:J}=ut({queryKey:["cloud-status"],queryFn:he.getCloudStatus});w.useEffect(()=>{K&&(r(K.repository_url),l(K.branch),f(K.schedule_enabled),p(K.schedule_type),y(K.backup_kprofiles),v(K.backup_cloud_profiles),M(K.backup_settings),T(K.enabled),a(""),setTimeout(()=>{W.current=!0},100))},[K]);const z=w.useCallback(async(ie=!1)=>{if(K?.has_token)try{ie&&i?(await he.saveGitHubBackupConfig({repository_url:n,access_token:i,branch:o,schedule_enabled:d,schedule_type:m,backup_kprofiles:b,backup_cloud_profiles:x,backup_settings:S,enabled:A}),a(""),e("Token updated")):(await he.updateGitHubBackupConfig({repository_url:n,branch:o,schedule_enabled:d,schedule_type:m,backup_kprofiles:b,backup_cloud_profiles:x,backup_settings:S,enabled:A}),e("Settings saved")),t.invalidateQueries({queryKey:["github-backup-config"]}),t.invalidateQueries({queryKey:["github-backup-status"]})}catch(Ae){e(`Failed to save: ${Ae.message}`,"error")}},[K?.has_token,n,i,o,d,m,b,x,S,A,t,e]);w.useEffect(()=>{if(!(!W.current||!K?.has_token))return P.current&&clearTimeout(P.current),P.current=setTimeout(()=>{z(!1)},500),()=>{P.current&&clearTimeout(P.current)}},[n,o,d,m,b,x,S,A,z,K?.has_token]),w.useEffect(()=>{if(!(!W.current||!K?.has_token||!i))return P.current&&clearTimeout(P.current),P.current=setTimeout(()=>{z(!0)},1e3),()=>{P.current&&clearTimeout(P.current)}},[i,z,K?.has_token]);const G=mt({mutationFn:ie=>he.saveGitHubBackupConfig(ie),onSuccess:()=>{t.invalidateQueries({queryKey:["github-backup-config"]}),t.invalidateQueries({queryKey:["github-backup-status"]}),e("GitHub backup enabled"),a(""),W.current=!0},onError:ie=>{e(`Failed to save: ${ie.message}`,"error")}}),ne=mt({mutationFn:he.triggerGitHubBackup,onSuccess:ie=>{t.invalidateQueries({queryKey:["github-backup-status"]}),t.invalidateQueries({queryKey:["github-backup-logs"]}),ie.success?ie.files_changed>0?e(`Backup complete - ${ie.files_changed} files updated`):e("Backup skipped - no changes"):e(`Backup failed: ${ie.message}`,"error")},onError:ie=>{e(`Backup failed: ${ie.message}`,"error")}}),ae=mt({mutationFn:()=>he.clearGitHubBackupLogs(10),onSuccess:ie=>{t.invalidateQueries({queryKey:["github-backup-logs"]}),e(`Cleared ${ie.deleted} logs`)},onError:ie=>{e(`Failed to clear logs: ${ie.message}`,"error")}}),de=async()=>{U(!0),q(null);try{let ie;if(i){if(!n){e("Enter repository URL","error"),U(!1);return}ie=await he.testGitHubConnection(n,i)}else if(K?.has_token)ie=await he.testGitHubStoredConnection();else{e("Enter repository URL and access token","error"),U(!1);return}q({success:ie.success,message:ie.message})}catch(ie){q({success:!1,message:ie.message})}finally{U(!1)}},fe=()=>{if(!n){e("Repository URL is required","error");return}if(!i){e("Access token is required","error");return}G.mutate({repository_url:n,access_token:i,branch:o,schedule_enabled:d,schedule_type:m,backup_kprofiles:b,backup_cloud_profiles:x,backup_settings:S,enabled:A})};return V?s.jsx("div",{className:"flex items-center justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 animate-spin text-bambu-green"})}):s.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[s.jsxs("div",{className:"space-y-6",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(bP,{className:"w-5 h-5 text-gray-400"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"GitHub Backup"})]}),K&&J?.is_authenticated&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Enabled"}),s.jsx(Gn,{checked:A,onChange:T})]})]})}),s.jsx(Nt,{className:"space-y-4",children:J?.is_authenticated?s.jsxs(s.Fragment,{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically sync your profiles to a private GitHub repository for backup and version history."}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Repository URL"}),s.jsx("input",{type:"text",value:n,onChange:ie=>{r(ie.target.value),q(null)},placeholder:"https://github.com/username/bambuddy-backup",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"})]}),s.jsxs("div",{children:[s.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:["Personal Access Token ",K?.has_token&&s.jsx("span",{className:"text-green-400",children:"(saved)"})]}),s.jsxs("div",{className:"relative",children:[s.jsx("input",{type:c?"text":"password",value:i,onChange:ie=>{a(ie.target.value),q(null)},placeholder:K?.has_token?"Enter new token to update":"ghp_xxxxxxxxxxxx",className:"w-full px-3 py-2 pr-10 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"}),s.jsx("button",{type:"button",onClick:()=>u(!c),className:"absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:c?s.jsx(My,{className:"w-4 h-4"}):s.jsx(Qd,{className:"w-4 h-4"})})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Fine-grained token with Contents read/write permission"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Branch"}),s.jsx("input",{type:"text",value:o,onChange:ie=>l(ie.target.value),placeholder:"main",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Auto-backup"}),s.jsxs("select",{value:d?m:"disabled",onChange:ie=>{ie.target.value==="disabled"?f(!1):(f(!0),p(ie.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:[s.jsx("option",{value:"disabled",children:"Manual only"}),s.jsx("option",{value:"hourly",children:"Hourly"}),s.jsx("option",{value:"daily",children:"Daily"}),s.jsx("option",{value:"weekly",children:"Weekly"})]})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"Include in backup"}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("label",{className:"flex items-start gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:b,onChange:ie=>y(ie.target.checked),className:"w-4 h-4 mt-0.5 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white text-sm",children:"K-Profiles"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Pressure advance calibration from connected printers"})]})]}),s.jsxs("label",{className:"flex items-start gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:x,onChange:ie=>v(ie.target.checked),className:"w-4 h-4 mt-0.5 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green",disabled:!J?.is_authenticated}),s.jsxs("div",{children:[s.jsx("span",{className:`text-sm ${J?.is_authenticated?"text-white":"text-bambu-gray"}`,children:"Cloud Profiles"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament, printer, and process presets from Bambu Cloud"})]})]}),s.jsxs("label",{className:"flex items-start gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:S,onChange:ie=>M(ie.target.checked),className:"w-4 h-4 mt-0.5 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white text-sm",children:"App Settings"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Bambuddy configuration (excludes sensitive data)"})]})]})]})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4 space-y-3",children:[Q?.configured&&s.jsxs("div",{className:"flex items-center justify-between text-sm",children:[s.jsx("div",{className:"flex items-center gap-2 text-bambu-gray",children:Q.last_backup_at?s.jsxs(s.Fragment,{children:[s.jsxs("span",{children:["Last backup: ",gq(Q.last_backup_at)]}),s.jsx(pq,{status:Q.last_backup_status})]}):s.jsx("span",{children:"No backups yet"})}),Q.next_scheduled_run&&s.jsxs("span",{className:"text-bambu-gray",children:[s.jsx(Zn,{className:"w-3 h-3 inline mr-1"}),"Next: ",gq(Q.next_scheduled_run)]})]}),L&&s.jsxs("div",{className:`text-sm flex items-center gap-1 ${L.success?"text-green-400":"text-red-400"}`,children:[L.success?s.jsx(Un,{className:"w-4 h-4"}):s.jsx(ia,{className:"w-4 h-4"}),L.message]}),s.jsx("div",{className:"flex flex-wrap items-center gap-2",children:Q?.configured?s.jsx(s.Fragment,{children:ne.isPending||Q.is_running?s.jsxs("div",{className:"flex items-center gap-2 text-bambu-green",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),s.jsx("span",{className:"text-sm",children:Q.progress||"Starting backup..."})]}):s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"primary",size:"sm",onClick:()=>ne.mutate(),disabled:!K?.enabled,children:[s.jsx(Oo,{className:"w-4 h-4"}),"Backup Now"]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:de,disabled:D,children:[D?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Test"]})]})}):s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"primary",size:"sm",onClick:fe,disabled:G.isPending||!n||!i,children:[G.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Un,{className:"w-4 h-4"}),"Enable Backup"]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:de,disabled:D||!n||!i,children:[D?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Test Connection"]})]})})]})]}):s.jsxs("div",{className:"flex items-start gap-2 p-3 bg-yellow-500/10 border border-yellow-500/30 rounded-lg",children:[s.jsx(kn,{className:"w-4 h-4 text-yellow-400 mt-0.5 flex-shrink-0"}),s.jsx("p",{className:"text-sm text-yellow-400",children:"Bambu Cloud login required. Sign in under Profiles → Cloud Profiles to enable GitHub backup."})]})})]}),re&&re.length>0&&s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(P0,{className:"w-5 h-5 text-gray-400"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"History"})]}),s.jsxs(Se,{variant:"ghost",size:"sm",onClick:()=>ae.mutate(),disabled:ae.isPending,children:[s.jsx(dn,{className:"w-4 h-4"}),"Clear"]})]})}),s.jsx(Nt,{children:s.jsx("div",{className:"overflow-x-auto",children:s.jsxs("table",{className:"w-full text-sm",children:[s.jsx("thead",{children:s.jsxs("tr",{className:"text-bambu-gray border-b border-bambu-dark-tertiary",children:[s.jsx("th",{className:"text-left py-2 px-2",children:"Date"}),s.jsx("th",{className:"text-left py-2 px-2",children:"Status"}),s.jsx("th",{className:"text-left py-2 px-2",children:"Commit"})]})}),s.jsx("tbody",{children:re.slice(0,10).map(ie=>s.jsxs("tr",{className:"border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-secondary",children:[s.jsx("td",{className:"py-2 px-2 text-white",children:qYe(ie.started_at)}),s.jsx("td",{className:"py-2 px-2",children:s.jsx(pq,{status:ie.status})}),s.jsx("td",{className:"py-2 px-2",children:ie.commit_sha?s.jsxs("a",{href:`${K?.repository_url}/commit/${ie.commit_sha}`,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-green hover:underline inline-flex items-center gap-1",children:[ie.commit_sha.substring(0,7),s.jsx(zr,{className:"w-3 h-3"})]}):s.jsx("span",{className:"text-bambu-gray",children:"-"})})]},ie.id))})]})})})]})]}),s.jsx("div",{className:"space-y-6",children:s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(su,{className:"w-5 h-5 text-gray-400"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Local Backup"})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Export or import your Bambuddy data as a local file for manual backup or migration."}),s.jsxs("div",{className:"flex items-center justify-between py-3 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Export Data"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Download all settings, printers, and profiles"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>E(!0),children:[s.jsx(Hr,{className:"w-4 h-4"}),"Export"]})]}),s.jsxs("div",{className:"flex items-center justify-between py-3",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Import Backup"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Restore from a previous export file"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>C(!0),children:[s.jsx(pi,{className:"w-4 h-4"}),"Import"]})]})]})]})}),k&&s.jsx(GYe,{onClose:()=>E(!1),onExport:async ie=>{E(!1);try{const{blob:Ae,filename:me}=await he.exportBackup(ie),Ne=URL.createObjectURL(Ae),ye=document.createElement("a");ye.href=Ne,ye.download=me,ye.click(),URL.revokeObjectURL(Ne),e("Backup downloaded successfully")}catch{e("Failed to create backup","error")}}}),O&&s.jsx(WYe,{onClose:()=>C(!1),onRestore:async(ie,Ae)=>await he.importBackup(ie,Ae),onSuccess:()=>{C(!1),e("Backup restored successfully"),t.invalidateQueries()}})]})}const XYe={get:"bg-blue-500/20 text-blue-400 border-blue-500/30",post:"bg-green-500/20 text-green-400 border-green-500/30",put:"bg-yellow-500/20 text-yellow-400 border-yellow-500/30",patch:"bg-orange-500/20 text-orange-400 border-orange-500/30",delete:"bg-red-500/20 text-red-400 border-red-500/30"};function YYe(t,e){const n=e.replace("#/","").split("/");let r=t;for(const i of n)r=r[i];return r}function t0(t,e,n=0){if(n>5)return"...";if(e.$ref)return t0(t,YYe(t,e.$ref),n+1);if(e.allOf){const r={};for(const i of e.allOf){const a=t0(t,i,n+1);typeof a=="object"&&a!==null&&Object.assign(r,a)}return r}if(e.example!==void 0)return e.example;if(e.default!==void 0)return e.default;switch(e.type){case"string":return e.enum?e.enum[0]:"string";case"integer":case"number":return 0;case"boolean":return!1;case"array":return e.items?[t0(t,e.items,n+1)]:[];case"object":if(e.properties){const r={};for(const[i,a]of Object.entries(e.properties))r[i]=t0(t,a,n+1);return r}return{};default:return null}}function ZYe({path:t,method:e,spec:n,schema:r,apiKey:i}){const[a,o]=w.useState(!1),[l,c]=w.useState({}),[u,d]=w.useState(""),[f,m]=w.useState(null),[p,b]=w.useState(!1),[y,x]=w.useState(!1);w.useEffect(()=>{if(a&&n.parameters){const O={};for(const C of n.parameters)C.schema?.default!==void 0&&(O[C.name]=String(C.schema.default));c(C=>({...O,...C}))}},[a,n.parameters]),w.useEffect(()=>{if(a&&n.requestBody?.content?.["application/json"]?.schema&&!u){const O=n.requestBody.content["application/json"].schema,C=t0(r,O);d(JSON.stringify(C,null,2))}},[a,n.requestBody,r,u]);const S=(()=>{const O=[];for(const C of n.parameters||[])if(C.in==="path"||C.required){const D=l[C.name];(D===void 0||D==="")&&O.push(C.name)}return O})(),M=async()=>{if(S.length>0){m({status:0,statusText:"Validation Error",headers:{},body:`Missing required parameters: ${S.join(", ")}`,duration:0});return}b(!0),m(null);try{let O=t;const C=new URLSearchParams;for(const J of n.parameters||[]){const z=l[J.name];z!==void 0&&z!==""&&(J.in==="path"?O=O.replace(`{${J.name}}`,encodeURIComponent(z)):J.in==="query"&&C.append(J.name,z))}const D=C.toString(),U=`${O}${D?`?${D}`:""}`,L={"Content-Type":"application/json"};i&&(L["X-API-Key"]=i);const q={method:e.toUpperCase(),headers:L};["post","put","patch"].includes(e)&&u&&(q.body=u);const P=performance.now(),W=await fetch(U,q),K=Math.round(performance.now()-P),V={};W.headers.forEach((J,z)=>{V[z]=J});let Q;W.headers.get("content-type")?.includes("application/json")?Q=await W.json():Q=await W.text(),m({status:W.status,statusText:W.statusText,headers:V,body:Q,duration:K})}catch(O){m({status:0,statusText:"Network Error",headers:{},body:O instanceof Error?O.message:"Unknown error",duration:0})}finally{b(!1)}},A=async()=>{if(f){const O=typeof f.body=="string"?f.body:JSON.stringify(f.body,null,2);try{await navigator.clipboard.writeText(O),x(!0),setTimeout(()=>x(!1),2e3)}catch{const C=document.createElement("textarea");C.value=O,C.style.position="fixed",C.style.left="-999999px",document.body.appendChild(C),C.select(),document.execCommand("copy"),document.body.removeChild(C),x(!0),setTimeout(()=>x(!1),2e3)}}},T=(n.parameters||[]).filter(O=>O.in==="path"),k=(n.parameters||[]).filter(O=>O.in==="query"),E=["post","put","patch"].includes(e)&&n.requestBody;return s.jsxs("div",{className:"border border-bambu-dark-tertiary rounded-lg overflow-hidden",children:[s.jsxs("button",{onClick:()=>o(!a),className:"w-full flex items-center gap-3 p-3 hover:bg-bambu-dark-tertiary/50 transition-colors text-left",children:[a?s.jsx(Oi,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}):s.jsx(Js,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),s.jsx("span",{className:`px-2 py-0.5 text-xs font-mono font-semibold uppercase rounded border ${XYe[e]||"bg-gray-500/20 text-gray-400"}`,children:e}),s.jsx("code",{className:"text-sm text-white font-mono flex-1 truncate",children:t}),n.summary&&s.jsx("span",{className:"text-sm text-bambu-gray truncate max-w-[40%]",children:n.summary})]}),a&&s.jsxs("div",{className:"border-t border-bambu-dark-tertiary p-4 space-y-4 bg-bambu-dark/50",children:[n.description&&s.jsx("p",{className:"text-sm text-bambu-gray",children:n.description}),T.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsx("h4",{className:"text-sm font-medium text-white",children:"Path Parameters"}),s.jsx("div",{className:"space-y-2",children:T.map(O=>s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("label",{className:"text-sm text-bambu-gray w-32 flex-shrink-0",children:[O.name,O.required&&s.jsx("span",{className:"text-red-400 ml-1",children:"*"})]}),s.jsx("input",{type:"text",value:l[O.name]||"",onChange:C=>c(D=>({...D,[O.name]:C.target.value})),placeholder:O.description||O.schema?.type||"value",className:"flex-1 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm font-mono focus:border-bambu-green focus:outline-none"})]},O.name))})]}),k.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsx("h4",{className:"text-sm font-medium text-white",children:"Query Parameters"}),s.jsx("div",{className:"space-y-2",children:k.map(O=>s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("label",{className:"text-sm text-bambu-gray w-32 flex-shrink-0",children:[O.name,O.required&&s.jsx("span",{className:"text-red-400 ml-1",children:"*"})]}),O.schema?.enum?s.jsxs("select",{value:l[O.name]||"",onChange:C=>c(D=>({...D,[O.name]:C.target.value})),className:"flex-1 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"",children:"-- Select --"}),O.schema.enum.map(C=>s.jsx("option",{value:C,children:C},C))]}):s.jsx("input",{type:"text",value:l[O.name]||"",onChange:C=>c(D=>({...D,[O.name]:C.target.value})),placeholder:O.description||O.schema?.type||"value",className:"flex-1 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm font-mono focus:border-bambu-green focus:outline-none"})]},O.name))})]}),E&&s.jsxs("div",{className:"space-y-2",children:[s.jsx("h4",{className:"text-sm font-medium text-white",children:"Request Body"}),s.jsx("textarea",{value:u,onChange:O=>d(O.target.value),rows:8,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm font-mono focus:border-bambu-green focus:outline-none resize-y",placeholder:"JSON request body..."})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs(Se,{onClick:M,disabled:p,children:[p?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Oo,{className:"w-4 h-4"}),"Execute"]}),S.length>0&&s.jsxs("span",{className:"text-xs text-yellow-400 flex items-center gap-1",children:[s.jsx(oa,{className:"w-3 h-3"}),"Fill in: ",S.join(", ")]})]}),f&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("h4",{className:"text-sm font-medium text-white flex items-center gap-2",children:["Response",s.jsxs("span",{className:`px-2 py-0.5 text-xs rounded ${f.status>=200&&f.status<300?"bg-green-500/20 text-green-400":f.status>=400?"bg-red-500/20 text-red-400":"bg-yellow-500/20 text-yellow-400"}`,children:[f.status," ",f.statusText]}),s.jsxs("span",{className:"text-xs text-bambu-gray",children:[f.duration,"ms"]})]}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:A,children:y?s.jsx(Un,{className:"w-3 h-3 text-green-400"}):s.jsx(ll,{className:"w-3 h-3"})})]}),s.jsx("pre",{className:"p-3 bg-bambu-dark rounded-lg text-sm font-mono text-white overflow-auto max-h-96 border border-bambu-dark-tertiary",children:typeof f.body=="string"?f.body:JSON.stringify(f.body,null,2)})]})]})]})}function QYe({apiKey:t=""}){const[e,n]=w.useState(null),[r,i]=w.useState(!0),[a,o]=w.useState(null),[l,c]=w.useState(new Set),[u,d]=w.useState("");if(w.useEffect(()=>{async function x(){try{const v=await fetch("/openapi.json");if(!v.ok)throw new Error("Failed to fetch OpenAPI schema");const S=await v.json();n(S)}catch(v){o(v instanceof Error?v.message:"Unknown error")}finally{i(!1)}}x()},[]),r)return s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})});if(a||!e)return s.jsx(St,{children:s.jsx(Nt,{className:"py-8",children:s.jsxs("div",{className:"text-center text-red-400",children:[s.jsx(oa,{className:"w-12 h-12 mx-auto mb-3 opacity-50"}),s.jsx("p",{children:"Failed to load API schema"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:a})]})})});const f={};for(const[x,v]of Object.entries(e.paths))for(const[S,M]of Object.entries(v)){if(S==="parameters")continue;const A=M.tags||["Other"];for(const T of A)f[T]||(f[T]=[]),f[T].push({path:x,method:S,spec:M})}const m=Object.entries(f).map(([x,v])=>{if(!u)return{tag:x,endpoints:v};const S=v.filter(({path:M,method:A,spec:T})=>{const k=u.toLowerCase();return M.toLowerCase().includes(k)||A.toLowerCase().includes(k)||(T.summary?.toLowerCase()||"").includes(k)||(T.description?.toLowerCase()||"").includes(k)});return{tag:x,endpoints:S}}).filter(({endpoints:x})=>x.length>0).sort((x,v)=>x.tag.localeCompare(v.tag)),p=x=>{c(v=>{const S=new Set(v);return S.has(x)?S.delete(x):S.add(x),S})},b=()=>{c(new Set(m.map(x=>x.tag)))},y=()=>{c(new Set)};return s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between gap-4",children:[s.jsx("div",{className:"flex-1",children:s.jsx("input",{type:"text",value:u,onChange:x=>d(x.target.value),placeholder:"Search endpoints...",className:"w-full max-w-md px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"})}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Se,{variant:"secondary",size:"sm",onClick:b,children:"Expand All"}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:y,children:"Collapse All"}),s.jsxs("a",{href:"/docs",target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 text-sm text-bambu-green hover:underline",children:[s.jsx(zr,{className:"w-4 h-4"}),"Swagger UI"]})]})]}),s.jsxs("p",{className:"text-sm text-bambu-gray",children:[m.reduce((x,v)=>x+v.endpoints.length,0)," endpoints in ",m.length," categories"]}),s.jsx("div",{className:"space-y-3",children:m.map(({tag:x,endpoints:v})=>s.jsxs(St,{children:[s.jsx("button",{onClick:()=>p(x),className:"w-full flex items-center justify-between p-4 hover:bg-bambu-dark-tertiary/30 transition-colors text-left",children:s.jsxs("div",{className:"flex items-center gap-2",children:[l.has(x)?s.jsx(Oi,{className:"w-5 h-5 text-bambu-gray"}):s.jsx(Js,{className:"w-5 h-5 text-bambu-gray"}),s.jsx("h3",{className:"text-base font-semibold text-white capitalize",children:x.replace(/-/g," ")}),s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-2 py-0.5 rounded-full text-bambu-gray",children:v.length})]})}),l.has(x)&&s.jsx(Nt,{className:"pt-0 space-y-2",children:v.map(({path:S,method:M,spec:A})=>s.jsx(ZYe,{path:S,method:M,spec:A,schema:e,apiKey:t},`${M}-${S}`))})]},x))})]})}const JYe=["general","network","plugs","notifications","filament","apikeys","virtual-printer","users","backup"];function eZe(){const t=on(),e=gl(),[n,r]=Xq(),{t:i,i18n:a}=Xg(),{showToast:o}=qn(),{authEnabled:l,user:c,refreshAuth:u}=fh(),{mode:d,darkStyle:f,darkBackground:m,darkAccent:p,lightStyle:b,lightBackground:y,lightAccent:x,setDarkStyle:v,setDarkBackground:S,setDarkAccent:M,setLightStyle:A,setLightBackground:T,setLightAccent:k}=hh(),[E,O]=w.useState(null),[C,D]=w.useState(!1),[U,L]=w.useState(null),[q,P]=w.useState(!1),[W,K]=w.useState(null),[V,Q]=w.useState(null),[re,J]=w.useState(!1),[z,G]=w.useState(OK()),ne=n.get("tab"),ae=ne&&JYe.includes(ne)?ne:"general",[de,fe]=w.useState(ae),ie=be=>{fe(be),be==="general"?n.delete("tab"):n.set("tab",be),r(n,{replace:!0})},[Ae,me]=w.useState(!1),[Ne,ye]=w.useState(""),[le,$e]=w.useState({can_queue:!0,can_control_printer:!1,can_read_status:!0}),[H,ee]=w.useState(null),[X,B]=w.useState(null),[I,ce]=w.useState(""),[Te,Le]=w.useState(!1),[Ee,$]=w.useState(!1),[j,Y]=w.useState(null),[ge,we]=w.useState(!1),[ve,Ze]=w.useState(!1),[Ie,qe]=w.useState(null),[Je,Qe]=w.useState(!1),[oe,te]=w.useState({}),[Me,Fe]=w.useState({}),ot=be=>{G(be),Kme(be)},se=()=>{localStorage.removeItem("sidebarOrder"),window.location.reload()},{data:je,isLoading:xe}=ut({queryKey:["settings"],queryFn:he.getSettings}),{data:Oe,isLoading:Ue}=ut({queryKey:["smart-plugs"],queryFn:he.getSmartPlugs}),{data:Ce,isLoading:We}=ut({queryKey:["smart-plugs-energy",Oe?.map(be=>be.id)],queryFn:async()=>{if(!Oe||Oe.length===0)return null;const be=await Promise.all(Oe.filter(Rt=>Rt.enabled).map(async Rt=>{try{const $t=await he.getSmartPlugStatus(Rt.id);return{plug:Rt,status:$t}}catch{return{plug:Rt,status:null}}}));let Pe=0,wt=0,Mt=0,lt=0,Tt=0;for(const{status:Rt}of be)Rt?.reachable&&Rt.energy&&(Tt++,Rt.energy.power!=null&&(Pe+=Rt.energy.power),Rt.energy.today!=null&&(wt+=Rt.energy.today),Rt.energy.yesterday!=null&&(Mt+=Rt.energy.yesterday),Rt.energy.total!=null&&(lt+=Rt.energy.total));return{totalPower:Pe,totalToday:wt,totalYesterday:Mt,totalLifetime:lt,reachableCount:Tt,totalPlugs:Oe.filter(Rt=>Rt.enabled).length}},enabled:de==="plugs"&&!!Oe&&Oe.length>0,refetchInterval:de==="plugs"?1e4:!1}),{data:pt,isLoading:kt}=ut({queryKey:["notification-providers"],queryFn:he.getNotificationProviders}),{data:Lt,isLoading:ln}=ut({queryKey:["api-keys"],queryFn:he.getAPIKeys}),Gt=mt({mutationFn:be=>he.createAPIKey(be),onSuccess:be=>{ee(be.key||null),me(!1),ye(""),t.invalidateQueries({queryKey:["api-keys"]}),o("API key created")},onError:be=>{o(`Failed to create API key: ${be.message}`,"error")}}),_n=mt({mutationFn:be=>he.deleteAPIKey(be),onSuccess:()=>{t.invalidateQueries({queryKey:["api-keys"]}),o("API key deleted")},onError:be=>{o(`Failed to delete API key: ${be.message}`,"error")}}),{data:gt}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:Kt,isLoading:bn}=ut({queryKey:["notification-templates"],queryFn:he.getNotificationTemplates}),{data:At}=ut({queryKey:["virtual-printer-settings"],queryFn:r1.getSettings,refetchInterval:1e4}),Pr=At?.status?.running??!1,{data:yr}=ut({queryKey:["ffmpeg-status"],queryFn:he.checkFfmpeg}),{data:jn}=ut({queryKey:["version"],queryFn:he.getVersion}),{data:sn,refetch:Rr,isRefetching:Gr}=ut({queryKey:["updateCheck"],queryFn:he.checkForUpdates,staleTime:300*1e3}),{data:Dr,refetch:_e}=ut({queryKey:["updateStatus"],queryFn:he.getUpdateStatus,refetchInterval:be=>{const Pe=be.state.data;return Pe?.status==="downloading"||Pe?.status==="installing"?1e3:!1}}),{data:Ve}=ut({queryKey:["mqtt-status"],queryFn:he.getMQTTStatus,refetchInterval:de==="network"?5e3:!1}),{data:at}=ut({queryKey:["github-backup-status"],queryFn:he.getGitHubBackupStatus}),{data:ct}=ut({queryKey:["cloud-status"],queryFn:he.getCloudStatus}),rt=mt({mutationFn:he.applyUpdate,onSuccess:be=>{be.is_docker?o(be.message,"error"):_e()}}),[jt,zt]=w.useState(null),Xt=mt({mutationFn:he.testAllNotificationProviders,onSuccess:be=>{zt(be),t.invalidateQueries({queryKey:["notification-providers"]}),be.failed===0?o(`All ${be.tested} providers tested successfully!`,"success"):o(`${be.success}/${be.tested} providers succeeded`,be.failed>0?"error":"success")},onError:be=>{o(`Failed to test providers: ${be.message}`,"error")}}),Yt=mt({mutationFn:async be=>{if(!Oe)return{success:0,failed:0};const Pe=Oe.filter(Mt=>Mt.enabled),wt=await Promise.all(Pe.map(async Mt=>{try{return await he.controlSmartPlug(Mt.id,be),{success:!0}}catch{return{success:!1}}}));return{success:wt.filter(Mt=>Mt.success).length,failed:wt.filter(Mt=>!Mt.success).length}},onSuccess:(be,Pe)=>{t.invalidateQueries({queryKey:["smart-plugs"]}),t.invalidateQueries({queryKey:["smart-plugs-energy"]}),be.failed===0?o(`All ${be.success} plugs turned ${Pe}`,"success"):o(`${be.success} plugs turned ${Pe}, ${be.failed} failed`,"error")},onError:be=>{o(`Failed: ${be.message}`,"error")}}),ue=w.useRef(null),Re=w.useRef(!1),He=w.useRef(!0);w.useEffect(()=>{if(je&&!E){const be={...je,external_url:je.external_url||window.location.origin};O(be),setTimeout(()=>{He.current=!1},100)}},[je,E]);const Xe=mt({mutationFn:he.updateSettings,onSuccess:be=>{t.setQueryData(["settings"],be),O(be),t.invalidateQueries({queryKey:["archiveStats"]}),o("Settings saved","success")},onError:be=>{o(`Failed to save: ${be.message}`,"error")},onSettled:()=>{Re.current=!1}}),Ge=mt({mutationFn:({id:be,data:Pe})=>he.updatePrinter(be,Pe),onSuccess:()=>{t.invalidateQueries({queryKey:["printers"]}),o("Camera settings saved","success")},onError:be=>{o(`Failed to update printer: ${be.message}`,"error")}});w.useEffect(()=>{if(!(He.current||!E||!je||!(je.auto_archive!==E.auto_archive||je.save_thumbnails!==E.save_thumbnails||je.capture_finish_photo!==E.capture_finish_photo||je.default_filament_cost!==E.default_filament_cost||je.currency!==E.currency||je.energy_cost_per_kwh!==E.energy_cost_per_kwh||je.energy_tracking_mode!==E.energy_tracking_mode||je.check_updates!==E.check_updates||je.notification_language!==E.notification_language||je.ams_humidity_good!==E.ams_humidity_good||je.ams_humidity_fair!==E.ams_humidity_fair||je.ams_temp_good!==E.ams_temp_good||je.ams_temp_fair!==E.ams_temp_fair||je.ams_history_retention_days!==E.ams_history_retention_days||je.per_printer_mapping_expanded!==E.per_printer_mapping_expanded||je.date_format!==E.date_format||je.time_format!==E.time_format||je.default_printer_id!==E.default_printer_id||je.ftp_retry_enabled!==E.ftp_retry_enabled||je.ftp_retry_count!==E.ftp_retry_count||je.ftp_retry_delay!==E.ftp_retry_delay||je.ftp_timeout!==E.ftp_timeout||je.mqtt_enabled!==E.mqtt_enabled||je.mqtt_broker!==E.mqtt_broker||je.mqtt_port!==E.mqtt_port||je.mqtt_username!==E.mqtt_username||je.mqtt_password!==E.mqtt_password||je.mqtt_topic_prefix!==E.mqtt_topic_prefix||je.mqtt_use_tls!==E.mqtt_use_tls||je.external_url!==E.external_url||je.ha_enabled!==E.ha_enabled||je.ha_url!==E.ha_url||je.ha_token!==E.ha_token||(je.library_archive_mode??"ask")!==(E.library_archive_mode??"ask")||Number(je.library_disk_warning_gb??5)!==Number(E.library_disk_warning_gb??5)||(je.camera_view_mode??"window")!==(E.camera_view_mode??"window")||je.prometheus_enabled!==E.prometheus_enabled||je.prometheus_token!==E.prometheus_token))&&!Re.current)return ue.current&&clearTimeout(ue.current),ue.current=setTimeout(()=>{if(Re.current)return;Re.current=!0;const Pe={auto_archive:E.auto_archive,save_thumbnails:E.save_thumbnails,capture_finish_photo:E.capture_finish_photo,default_filament_cost:E.default_filament_cost,currency:E.currency,energy_cost_per_kwh:E.energy_cost_per_kwh,energy_tracking_mode:E.energy_tracking_mode,check_updates:E.check_updates,notification_language:E.notification_language,ams_humidity_good:E.ams_humidity_good,ams_humidity_fair:E.ams_humidity_fair,ams_temp_good:E.ams_temp_good,ams_temp_fair:E.ams_temp_fair,ams_history_retention_days:E.ams_history_retention_days,per_printer_mapping_expanded:E.per_printer_mapping_expanded,date_format:E.date_format,time_format:E.time_format,default_printer_id:E.default_printer_id,ftp_retry_enabled:E.ftp_retry_enabled,ftp_retry_count:E.ftp_retry_count,ftp_retry_delay:E.ftp_retry_delay,ftp_timeout:E.ftp_timeout,mqtt_enabled:E.mqtt_enabled,mqtt_broker:E.mqtt_broker,mqtt_port:E.mqtt_port,mqtt_username:E.mqtt_username,mqtt_password:E.mqtt_password,mqtt_topic_prefix:E.mqtt_topic_prefix,mqtt_use_tls:E.mqtt_use_tls,external_url:E.external_url,ha_enabled:E.ha_enabled,ha_url:E.ha_url,ha_token:E.ha_token,library_archive_mode:E.library_archive_mode,library_disk_warning_gb:E.library_disk_warning_gb,camera_view_mode:E.camera_view_mode,prometheus_enabled:E.prometheus_enabled,prometheus_token:E.prometheus_token};Xe.mutate(Pe)},500),()=>{ue.current&&clearTimeout(ue.current)}},[E,je,Xe]);const ht=w.useCallback((be,Pe)=>{O(wt=>wt?{...wt,[be]:Pe}:null)},[]),Pt=async(be,Pe,wt)=>{if(!Pe){o("Please enter a camera URL","error");return}Fe(Mt=>({...Mt,[be]:!0})),te(Mt=>({...Mt,[be]:null}));try{const Mt=await he.testExternalCamera(be,Pe,wt);te(lt=>({...lt,[be]:Mt})),Mt.success?o(`Camera connected${Mt.resolution?` (${Mt.resolution})`:""}`,"success"):o(Mt.error||"Connection failed","error")}catch(Mt){const lt=Mt instanceof Error?Mt.message:"Test failed";te(Tt=>({...Tt,[be]:{success:!1,error:lt}})),o(lt,"error")}finally{Fe(Mt=>({...Mt,[be]:!1}))}},[_t,yt]=w.useState({}),It=w.useRef({});w.useEffect(()=>{if(gt){const be={};gt.forEach(Pe=>{Pe.external_camera_url&&_t[Pe.id]===void 0&&(be[Pe.id]=Pe.external_camera_url)}),Object.keys(be).length>0&&yt(Pe=>({...Pe,...be}))}},[gt]);const Ot=(be,Pe)=>{yt(wt=>({...wt,[be]:Pe})),It.current[be]&&clearTimeout(It.current[be]),It.current[be]=setTimeout(()=>{Ge.mutate({id:be,data:{external_camera_url:Pe||null}})},800)},Ft=(be,Pe)=>{const wt={};Pe.type!==void 0&&(wt.external_camera_type=Pe.type||null),Pe.enabled!==void 0&&(wt.external_camera_enabled=Pe.enabled),Ge.mutate({id:be,data:wt})};return xe||!E?s.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsxs("div",{className:"mb-8",children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Settings"}),s.jsx("p",{className:"text-bambu-gray",children:"Configure Bambuddy"})]}),s.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary overflow-x-auto",children:[s.jsx("button",{onClick:()=>ie("general"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px ${de==="general"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:"General"}),s.jsxs("button",{onClick:()=>ie("plugs"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="plugs"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(Ks,{className:"w-4 h-4"}),"Smart Plugs",Oe&&Oe.length>0&&s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:Oe.length})]}),s.jsxs("button",{onClick:()=>ie("notifications"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="notifications"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(Ef,{className:"w-4 h-4"}),"Notifications",pt&&pt.length>0&&s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:pt.length})]}),s.jsxs("button",{onClick:()=>ie("filament"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="filament"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(Rde,{className:"w-4 h-4"}),"Filament"]}),s.jsxs("button",{onClick:()=>ie("network"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="network"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(ig,{className:"w-4 h-4"}),"Network",s.jsx("span",{className:`w-2 h-2 rounded-full ${Ve?.enabled?"bg-green-400":"bg-gray-500"}`})]}),s.jsxs("button",{onClick:()=>ie("apikeys"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="apikeys"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(mf,{className:"w-4 h-4"}),"API Keys",Lt&&Lt.length>0&&s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:Lt.length})]}),s.jsxs("button",{onClick:()=>ie("virtual-printer"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="virtual-printer"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(qr,{className:"w-4 h-4"}),"Virtual Printer",s.jsx("span",{className:`w-2 h-2 rounded-full ${Pr?"bg-green-400":"bg-gray-500"}`})]}),s.jsxs("button",{onClick:()=>ie("users"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="users"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(pf,{className:"w-4 h-4"}),"Users",l&&s.jsx("span",{className:`w-2 h-2 rounded-full ${l?"bg-green-400":"bg-gray-500"}`})]}),s.jsxs("button",{onClick:()=>ie("backup"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="backup"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(su,{className:"w-4 h-4"}),"Backup",s.jsx("span",{className:`w-2 h-2 rounded-full ${ct?.is_authenticated&&at?.configured&&at?.enabled?"bg-green-400":"bg-gray-500"}`})]})]}),de==="general"&&s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[s.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-xl",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:i("settings.general")})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[s.jsx($d,{className:"w-4 h-4 inline mr-1"}),i("settings.language")]}),s.jsx("select",{value:a.language,onChange:be=>a.changeLanguage(be.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:jz.map(be=>s.jsxs("option",{value:be.code,children:[be.nativeName," (",be.name,")"]},be.code))}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("settings.languageDescription")})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:i("settings.defaultView")}),s.jsx("select",{value:z,onChange:be=>ot(be.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:O0.map(be=>s.jsx("option",{value:be.to,children:i(be.labelKey)},be.id))}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("settings.defaultViewDescription")})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Date Format"}),s.jsxs("select",{value:E.date_format||"system",onChange:be=>ht("date_format",be.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:[s.jsx("option",{value:"system",children:"System Default"}),s.jsx("option",{value:"us",children:"US (MM/DD/YYYY)"}),s.jsx("option",{value:"eu",children:"EU (DD/MM/YYYY)"}),s.jsx("option",{value:"iso",children:"ISO (YYYY-MM-DD)"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Time Format"}),s.jsxs("select",{value:E.time_format||"system",onChange:be=>ht("time_format",be.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:[s.jsx("option",{value:"system",children:"System Default"}),s.jsx("option",{value:"12h",children:"12-hour (3:30 PM)"}),s.jsx("option",{value:"24h",children:"24-hour (15:30)"})]})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default Printer"}),s.jsxs("select",{value:E.default_printer_id??"",onChange:be=>ht("default_printer_id",be.target.value?Number(be.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:[s.jsx("option",{value:"",children:"No default (ask each time)"}),gt?.map(be=>s.jsx("option",{value:be.id,children:be.name},be.id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Pre-select this printer for uploads, reprints, and other operations."})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Sidebar order"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Drag items in the sidebar to reorder. Reset to default order here."})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:se,children:[s.jsx(mm,{className:"w-4 h-4"}),"Reset"]})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(HD,{className:"w-5 h-5"}),"Appearance"]})}),s.jsxs(Nt,{className:"space-y-6",children:[s.jsxs("div",{className:`space-y-3 p-4 rounded-lg border ${d==="dark"?"border-bambu-green bg-bambu-green/5":"border-bambu-dark-tertiary"}`,children:[s.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:["Dark Mode",d==="dark"&&s.jsx("span",{className:"text-xs text-bambu-green",children:"(active)"})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Background"}),s.jsxs("select",{value:m,onChange:be=>{S(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"neutral",children:"Neutral"}),s.jsx("option",{value:"warm",children:"Warm"}),s.jsx("option",{value:"cool",children:"Cool"}),s.jsx("option",{value:"oled",children:"OLED Black"}),s.jsx("option",{value:"slate",children:"Slate Blue"}),s.jsx("option",{value:"forest",children:"Forest Green"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Accent"}),s.jsxs("select",{value:p,onChange:be=>{M(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"green",children:"Green"}),s.jsx("option",{value:"teal",children:"Teal"}),s.jsx("option",{value:"blue",children:"Blue"}),s.jsx("option",{value:"orange",children:"Orange"}),s.jsx("option",{value:"purple",children:"Purple"}),s.jsx("option",{value:"red",children:"Red"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Style"}),s.jsxs("select",{value:f,onChange:be=>{v(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"classic",children:"Classic"}),s.jsx("option",{value:"glow",children:"Glow"}),s.jsx("option",{value:"vibrant",children:"Vibrant"})]})]})]})]}),s.jsxs("div",{className:`space-y-3 p-4 rounded-lg border ${d==="light"?"border-bambu-green bg-bambu-green/5":"border-bambu-dark-tertiary"}`,children:[s.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:["Light Mode",d==="light"&&s.jsx("span",{className:"text-xs text-bambu-green",children:"(active)"})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Background"}),s.jsxs("select",{value:y,onChange:be=>{T(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"neutral",children:"Neutral"}),s.jsx("option",{value:"warm",children:"Warm"}),s.jsx("option",{value:"cool",children:"Cool"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Accent"}),s.jsxs("select",{value:x,onChange:be=>{k(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"green",children:"Green"}),s.jsx("option",{value:"teal",children:"Teal"}),s.jsx("option",{value:"blue",children:"Blue"}),s.jsx("option",{value:"orange",children:"Orange"}),s.jsx("option",{value:"purple",children:"Purple"}),s.jsx("option",{value:"red",children:"Red"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Style"}),s.jsxs("select",{value:b,onChange:be=>{A(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"classic",children:"Classic"}),s.jsx("option",{value:"glow",children:"Glow"}),s.jsx("option",{value:"vibrant",children:"Vibrant"})]})]})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Toggle between dark and light mode using the sun/moon icon in the sidebar."})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Archive Settings"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Auto-archive prints"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically save 3MF files when prints complete"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.auto_archive,onChange:be=>ht("auto_archive",be.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Save thumbnails"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Extract and save preview images from 3MF files"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.save_thumbnails,onChange:be=>ht("save_thumbnails",be.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Capture finish photo"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Take a photo from printer camera when print completes"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.capture_finish_photo,onChange:be=>ht("capture_finish_photo",be.target.checked),className:"sr-only peer"}),s.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.capture_finish_photo&&yr&&!yr.installed&&s.jsxs("div",{className:"flex items-start gap-2 p-3 bg-yellow-500/10 border border-yellow-500/30 rounded-lg",children:[s.jsx(kn,{className:"w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-sm",children:[s.jsx("p",{className:"text-yellow-500 font-medium",children:"ffmpeg not installed"}),s.jsxs("p",{className:"text-bambu-gray mt-1",children:["Camera capture requires ffmpeg. Install it via"," ",s.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"brew install ffmpeg"})," (macOS) or"," ",s.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"apt install ffmpeg"})," (Linux)."]})]})]})]})]})]}),s.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-md",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(qD,{className:"w-5 h-5 text-bambu-green"}),"Camera"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Camera View Mode"}),s.jsxs("select",{value:E.camera_view_mode??"window",onChange:be=>ht("camera_view_mode",be.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:[s.jsx("option",{value:"window",children:"New Window"}),s.jsx("option",{value:"embedded",children:"Embedded Overlay"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:E.camera_view_mode==="embedded"?"Camera opens in a resizable overlay on the main screen":"Camera opens in a separate browser window"})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4 mt-4",children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-2",children:"External Cameras"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-3",children:"Configure external cameras to replace the built-in printer camera. Supports MJPEG streams, RTSP, HTTP snapshots, and USB cameras (V4L2). When enabled, the external camera is used for live view and finish photos."}),gt&>.length>0?s.jsx("div",{className:"space-y-3",children:gt.map(be=>s.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-white font-medium text-sm",children:be.name}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:be.external_camera_enabled,onChange:Pe=>Ft(be.id,{enabled:Pe.target.checked}),className:"sr-only peer"}),s.jsx("div",{className:"w-9 h-5 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:border-gray-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),be.external_camera_enabled&&s.jsxs("div",{className:"space-y-2 mt-2",children:[s.jsx("input",{type:"text",placeholder:be.external_camera_type==="usb"?"Device path (/dev/video0)":"Camera URL (rtsp://... or http://...)",value:_t[be.id]??be.external_camera_url??"",onChange:Pe=>Ot(be.id,Pe.target.value),className:"w-full px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("select",{value:be.external_camera_type||"mjpeg",onChange:Pe=>Ft(be.id,{type:Pe.target.value}),className:"flex-1 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"mjpeg",children:"MJPEG Stream"}),s.jsx("option",{value:"rtsp",children:"RTSP Stream"}),s.jsx("option",{value:"snapshot",children:"HTTP Snapshot"}),s.jsx("option",{value:"usb",children:"USB Camera (V4L2)"})]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>Pt(be.id,_t[be.id]??be.external_camera_url??"",be.external_camera_type||"mjpeg"),disabled:Me[be.id]||!(_t[be.id]??be.external_camera_url),children:Me[be.id]?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):"Test"})]}),oe[be.id]&&s.jsx("div",{className:`text-xs flex items-center gap-1 ${oe[be.id]?.success?"text-green-500":"text-red-500"}`,children:oe[be.id]?.success?s.jsxs(s.Fragment,{children:[s.jsx(Un,{className:"w-3 h-3"}),"Connected",oe[be.id]?.resolution&&` (${oe[be.id]?.resolution})`]}):s.jsxs(s.Fragment,{children:[s.jsx(ia,{className:"w-3 h-3"}),oe[be.id]?.error||"Connection failed"]})})]})]},be.id))}):s.jsx("p",{className:"text-xs text-bambu-gray italic",children:"No printers configured"})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Cost Tracking"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default filament cost (per kg)"}),s.jsx("input",{type:"number",step:"0.01",min:"0",value:E.default_filament_cost,onChange:be=>ht("default_filament_cost",parseFloat(be.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Currency"}),s.jsxs("select",{value:E.currency,onChange:be=>ht("currency",be.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:[s.jsx("option",{value:"USD",children:"USD ($)"}),s.jsx("option",{value:"EUR",children:"EUR (€)"}),s.jsx("option",{value:"GBP",children:"GBP (£)"}),s.jsx("option",{value:"CHF",children:"CHF (Fr.)"}),s.jsx("option",{value:"JPY",children:"JPY (¥)"}),s.jsx("option",{value:"CNY",children:"CNY (¥)"}),s.jsx("option",{value:"CAD",children:"CAD ($)"}),s.jsx("option",{value:"AUD",children:"AUD ($)"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Electricity cost per kWh"}),s.jsx("input",{type:"number",step:"0.01",min:"0",value:E.energy_cost_per_kwh,onChange:be=>ht("energy_cost_per_kwh",parseFloat(be.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Energy display mode"}),s.jsxs("select",{value:E.energy_tracking_mode||"total",onChange:be=>ht("energy_tracking_mode",be.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:[s.jsx("option",{value:"print",children:"Prints Only"}),s.jsx("option",{value:"total",children:"Total Consumption"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:E.energy_tracking_mode==="print"?"Dashboard shows sum of energy used during prints":"Dashboard shows lifetime energy from smart plugs"})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Za,{className:"w-5 h-5 text-bambu-green"}),"File Manager"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Create Archive Entry When Printing"}),s.jsxs("select",{value:E.library_archive_mode??"ask",onChange:be=>ht("library_archive_mode",be.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:[s.jsx("option",{value:"always",children:"Always create archive entry"}),s.jsx("option",{value:"never",children:"Never create archive entry"}),s.jsx("option",{value:"ask",children:"Ask each time"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"When printing from File Manager, optionally create an archive entry"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Low Disk Space Warning"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"0.5",max:"100",step:"0.5",value:E.library_disk_warning_gb??5,onChange:be=>ht("library_disk_warning_gb",parseFloat(be.target.value)||5),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"}),s.jsx("span",{className:"text-bambu-gray",children:"GB"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Show warning when free disk space falls below this threshold"})]})]})]})]}),s.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-sm",children:[s.jsx(HYe,{}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Updates"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Check for updates"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically check for new versions on startup"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.check_updates,onChange:be=>ht("check_updates",be.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Current version"}),s.jsxs("p",{className:"text-sm text-bambu-gray",children:["v",jn?.version||"..."]})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>Rr(),disabled:Gr,children:[Gr?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Check now"]})]}),sn?.update_available?s.jsxs("div",{className:"mt-4 p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:[s.jsxs("div",{className:"flex items-start justify-between",children:[s.jsxs("div",{children:[s.jsxs("p",{className:"text-bambu-green font-medium",children:["Update available: v",sn.latest_version]}),sn.release_name&&sn.release_name!==sn.latest_version&&s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:sn.release_name})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[sn.release_notes&&s.jsx("button",{onClick:()=>we(!0),className:"text-bambu-gray hover:text-white transition-colors text-sm underline",children:"Release Notes"}),sn.release_url&&s.jsx("a",{href:sn.release_url,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-white transition-colors",title:"View release on GitHub",children:s.jsx(zr,{className:"w-4 h-4"})})]})]}),Dr?.status==="downloading"||Dr?.status==="installing"?s.jsxs("div",{className:"mt-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),s.jsx("span",{children:Dr.message})]}),s.jsx("div",{className:"mt-2 w-full bg-bambu-dark-tertiary rounded-full h-2",children:s.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${Dr.progress}%`}})})]}):Dr?.status==="complete"?s.jsx("div",{className:"mt-3 p-2 bg-bambu-green/20 rounded text-sm text-bambu-green",children:Dr.message}):Dr?.status==="error"?s.jsx("div",{className:"mt-3 p-2 bg-red-500/20 rounded text-sm text-red-400",children:Dr.error||Dr.message}):sn?.is_docker?s.jsxs("div",{className:"mt-3 p-3 bg-bambu-dark-tertiary rounded-lg",children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-2",children:"Update via Docker Compose:"}),s.jsx("code",{className:"block text-xs bg-bambu-dark p-2 rounded text-bambu-green font-mono",children:"docker compose pull && docker compose up -d"})]}):s.jsxs(Se,{className:"mt-3",onClick:()=>rt.mutate(),disabled:rt.isPending,children:[rt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Hr,{className:"w-4 h-4"}),"Install Update"]})]}):sn?.error?s.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: ",sn.error]}):sn&&!sn.update_available?s.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:"You're running the latest version"}):null]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Data Management"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Clear Notification Logs"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Delete notification logs older than 30 days"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>Le(!0),children:[s.jsx(dn,{className:"w-4 h-4"}),"Clear"]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Reset UI Preferences"}),s.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."})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>$(!0),children:[s.jsx(dn,{className:"w-4 h-4"}),"Reset"]})]}),s.jsxs("div",{className:"flex items-center justify-between pt-4 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Backup & Restore"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Export/import settings and configure GitHub backup"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>ie("backup"),children:[s.jsx(su,{className:"w-4 h-4"}),"Go to Backup"]})]})]})]})]})]}),de==="network"&&E&&s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6",children:[s.jsxs("div",{className:"flex-1 lg:max-w-xl space-y-4",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx($d,{className:"w-5 h-5 text-blue-400"}),"External URL"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"The external URL where Bambuddy is accessible. Used for notification images and external integrations."}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Bambuddy URL"}),s.jsx("input",{type:"text",value:E.external_url??"",onChange:be=>ht("external_url",be.target.value),placeholder:"http://192.168.1.100:8000",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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Include protocol and port (e.g., http://192.168.1.100:8000)"})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(xr,{className:"w-5 h-5 text-blue-400"}),"FTP Retry"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Retry FTP operations when printer WiFi is unreliable. Applies to 3MF downloads, print uploads, timelapse downloads, and firmware updates."}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable retry"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically retry failed FTP operations"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.ftp_retry_enabled??!0,onChange:be=>ht("ftp_retry_enabled",be.target.checked),className:"sr-only peer"}),s.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.ftp_retry_enabled&&s.jsxs("div",{className:"space-y-4 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Retry attempts"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"1",max:"10",value:E.ftp_retry_count??3,onChange:be=>ht("ftp_retry_count",Math.min(10,Math.max(1,parseInt(be.target.value)||3))),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"}),s.jsx("span",{className:"text-bambu-gray",children:"times"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Number of retry attempts before giving up (1-10)"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Retry delay"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"1",max:"30",value:E.ftp_retry_delay??2,onChange:be=>ht("ftp_retry_delay",Math.min(30,Math.max(1,parseInt(be.target.value)||2))),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"}),s.jsx("span",{className:"text-bambu-gray",children:"seconds"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Wait time between retries (1-30)"})]})]}),s.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Connection timeout"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"10",max:"120",value:E.ftp_timeout??30,onChange:be=>ht("ftp_timeout",Math.min(120,Math.max(10,parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"seconds"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Socket timeout for slow connections. Increase for A1/A1 Mini printers with weak WiFi (10-120)"})]})]})]})]}),s.jsxs("div",{className:"flex-1 lg:max-w-xl space-y-4",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(cN,{className:"w-5 h-5 text-bambu-green"}),"Home Assistant"]}),E.ha_enabled&&Ie&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:`w-2.5 h-2.5 rounded-full ${Ie.success?"bg-green-400":"bg-red-400"}`}),s.jsx("span",{className:`text-sm ${Ie.success?"text-green-400":"text-red-400"}`,children:Ie.success?"Connected":"Disconnected"})]})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Connect to Home Assistant to control smart plugs via HA's REST API. Supports switch, light, and input_boolean entities."}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable Home Assistant"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Control smart plugs via Home Assistant"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.ha_enabled??!1,onChange:be=>ht("ha_enabled",be.target.checked),className:"sr-only peer"}),s.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.ha_enabled&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Home Assistant URL"}),s.jsx("input",{type:"text",value:E.ha_url??"",onChange:be=>ht("ha_url",be.target.value),placeholder:"http://192.168.1.100:8123",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Long-Lived Access Token"}),s.jsx("input",{type:"password",value:E.ha_token??"",onChange:be=>ht("ha_token",be.target.value),placeholder:"eyJ0eXAiOiJKV1QiLC...",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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Create a token in HA: Profile → Long-Lived Access Tokens → Create Token"})]}),E.ha_url&&E.ha_token&&s.jsx("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:s.jsxs(Se,{variant:"secondary",size:"sm",disabled:Je,onClick:async()=>{Qe(!0),qe(null);try{const be=await he.testHAConnection(E.ha_url,E.ha_token);qe(be)}catch(be){qe({success:!1,message:null,error:be instanceof Error?be.message:"Unknown error"})}finally{Qe(!1)}},children:[Je?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(ig,{className:"w-4 h-4"}),"Test Connection"]})})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(ig,{className:"w-5 h-5 text-blue-400"}),"MQTT Publishing"]}),Ve?.enabled&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:`w-2.5 h-2.5 rounded-full ${Ve.connected?"bg-green-400":"bg-red-400"}`}),s.jsx("span",{className:`text-sm ${Ve.connected?"text-green-400":"text-red-400"}`,children:Ve.connected?"Connected":"Disconnected"})]})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Publish BamBuddy events to an external MQTT broker for integration with Node-RED, Home Assistant, and other automation systems."}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable MQTT"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Publish events to external MQTT broker"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.mqtt_enabled??!1,onChange:be=>ht("mqtt_enabled",be.target.checked),className:"sr-only peer"}),s.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.mqtt_enabled&&s.jsxs("div",{className:"space-y-4 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Broker hostname"}),s.jsx("input",{type:"text",value:E.mqtt_broker??"",onChange:be=>ht("mqtt_broker",be.target.value),placeholder:"mqtt.example.com or 192.168.1.100",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"})]}),s.jsxs("div",{className:"flex items-end gap-4",children:[s.jsxs("div",{className:"flex-1",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Port"}),s.jsx("input",{type:"number",min:"1",max:"65535",value:E.mqtt_port??1883,onChange:be=>ht("mqtt_port",Math.min(65535,Math.max(1,parseInt(be.target.value)||1883))),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"})]}),s.jsxs("div",{className:"flex items-center gap-3 pb-2",children:[s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.mqtt_use_tls??!1,onChange:be=>{const Pe=be.target.checked;ht("mqtt_use_tls",Pe);const wt=E.mqtt_port??1883;Pe&&wt===1883?ht("mqtt_port",8883):!Pe&&wt===8883&&ht("mqtt_port",1883)},className:"sr-only peer"}),s.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.jsx("span",{className:"text-white text-sm",children:"Use TLS"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Username (optional)"}),s.jsx("input",{type:"text",value:E.mqtt_username??"",onChange:be=>ht("mqtt_username",be.target.value),placeholder:"Leave empty for anonymous",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password (optional)"}),s.jsx("input",{type:"password",value:E.mqtt_password??"",onChange:be=>ht("mqtt_password",be.target.value),placeholder:"Leave empty for anonymous",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Topic prefix"}),s.jsx("input",{type:"text",value:E.mqtt_topic_prefix??"bambuddy",onChange:be=>ht("mqtt_topic_prefix",be.target.value),placeholder:"bambuddy",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"}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:["Topics will be: ",E.mqtt_topic_prefix||"bambuddy","/printers//status, etc."]})]}),Ve&&s.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx("span",{className:`w-2 h-2 rounded-full ${Ve.connected?"bg-green-400":"bg-red-400"}`}),s.jsx("span",{className:"text-bambu-gray",children:Ve.connected?s.jsxs(s.Fragment,{children:["Connected to ",s.jsxs("span",{className:"text-white",children:[Ve.broker,":",Ve.port]})]}):"Not connected"})]})})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(NP,{className:"w-5 h-5 text-orange-400"}),"Prometheus Metrics"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("p",{className:"text-sm text-bambu-gray",children:["Expose printer metrics at ",s.jsx("code",{className:"bg-bambu-dark px-1 rounded",children:"/api/v1/metrics"})," for Prometheus/Grafana monitoring."]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable Metrics Endpoint"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Expose printer data in Prometheus format"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.prometheus_enabled??!1,onChange:be=>ht("prometheus_enabled",be.target.checked),className:"sr-only peer"}),s.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.prometheus_enabled&&s.jsxs("div",{className:"space-y-4 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Bearer Token (optional)"}),s.jsx("input",{type:"password",value:E.prometheus_token??"",onChange:be=>ht("prometheus_token",be.target.value),placeholder:"Leave empty for no authentication",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"}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:["If set, requests must include ",s.jsx("code",{className:"bg-bambu-dark px-1 rounded",children:"Authorization: Bearer "})]})]}),s.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-sm text-white mb-2",children:"Available Metrics"}),s.jsxs("div",{className:"text-xs text-bambu-gray space-y-1",children:[s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_printer_connected"})," - Connection status"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_printer_state"})," - Printer state (idle/printing/etc)"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_print_progress"})," - Print progress 0-100%"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_bed_temp_celsius"})," - Bed temperature"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_nozzle_temp_celsius"})," - Nozzle temperature"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_prints_total"})," - Total prints by result"]}),s.jsx("p",{className:"text-bambu-gray/70 italic",children:"...and more (layers, fans, queue, filament usage)"})]})]})]})]})]})]})]}),Ie&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-6 max-w-md w-full mx-4",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[Ie.success?s.jsx(Un,{className:"w-8 h-8 text-green-400"}):s.jsx(ia,{className:"w-8 h-8 text-red-400"}),s.jsx("h3",{className:"text-lg font-medium text-white",children:Ie.success?"Connection Successful":"Connection Failed"})]}),s.jsx("p",{className:"text-bambu-gray mb-6",children:Ie.success?Ie.message||"Successfully connected to Home Assistant.":Ie.error||"Failed to connect to Home Assistant."}),s.jsx("div",{className:"flex justify-end",children:s.jsx(Se,{variant:"primary",onClick:()=>qe(null),children:"OK"})})]})}),de==="plugs"&&s.jsxs("div",{className:"max-w-4xl",children:[s.jsxs("div",{className:"flex items-start justify-between mb-6",children:[s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ks,{className:"w-5 h-5 text-bambu-green"}),"Smart Plugs"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Connect smart plugs (Tasmota or Home Assistant) to automate power control and track energy usage for your printers."})]}),s.jsxs("div",{className:"flex items-center gap-2 pt-1 shrink-0",children:[Oe&&Oe.filter(be=>be.enabled).length>1&&s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Y("on"),disabled:Yt.isPending,title:"Turn all plugs on",children:[Yt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Eu,{className:"w-4 h-4 text-bambu-green"}),"All On"]}),s.jsxs(Se,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Y("off"),disabled:Yt.isPending,title:"Turn all plugs off",children:[Yt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(fN,{className:"w-4 h-4 text-red-400"}),"All Off"]})]}),s.jsxs(Se,{className:"whitespace-nowrap",onClick:()=>{L(null),D(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Smart Plug"]})]})]}),Oe&&Oe.length>0&&s.jsxs(St,{className:"mb-6",children:[s.jsx(er,{children:s.jsxs("h3",{className:"text-base font-semibold text-white flex items-center gap-2",children:[s.jsx(qf,{className:"w-4 h-4 text-yellow-400"}),"Energy Summary",We&&s.jsx(dt,{className:"w-4 h-4 animate-spin text-bambu-gray ml-2"})]})}),s.jsx(Nt,{children:Ce?s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(qf,{className:"w-3 h-3"}),"Current Power"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalPower.toFixed(1),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"W"})]}),s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:[Ce.reachableCount,"/",Ce.totalPlugs," plugs online"]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(oi,{className:"w-3 h-3"}),"Today"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalToday.toFixed(2),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),E&&E.energy_cost_per_kwh>0&&s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Ce.totalToday*E.energy_cost_per_kwh).toFixed(2)," ",E.currency]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(NP,{className:"w-3 h-3"}),"Yesterday"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalYesterday.toFixed(2),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),E&&E.energy_cost_per_kwh>0&&s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Ce.totalYesterday*E.energy_cost_per_kwh).toFixed(2)," ",E.currency]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(xP,{className:"w-3 h-3"}),"Total"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalLifetime.toFixed(1),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),E&&E.energy_cost_per_kwh>0&&s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Ce.totalLifetime*E.energy_cost_per_kwh).toFixed(2)," ",E.currency]})]})]}):We?null:s.jsx("p",{className:"text-sm text-bambu-gray",children:"Enable plugs to see energy summary"})})]}),Ue?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):Oe&&Oe.length>0?s.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:Oe.map(be=>s.jsx(CYe,{plug:be,onEdit:Pe=>{L(Pe),D(!0)}},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-12",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(Ks,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),s.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No smart plugs configured"}),s.jsx("p",{className:"text-sm mb-4",children:"Add a Tasmota-based smart plug to track energy usage and automate power control."}),s.jsxs(Se,{onClick:()=>{L(null),D(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Your First Smart Plug"]})]})})})]}),de==="notifications"&&s.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ef,{className:"w-5 h-5 text-bambu-green"}),"Providers"]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>J(!0),children:[s.jsx(P0,{className:"w-4 h-4"}),"Log"]}),pt&&pt.length>0&&s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>{zt(null),Xt.mutate()},disabled:Xt.isPending,children:[Xt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(GD,{className:"w-4 h-4"}),"Test All"]}),s.jsxs(Se,{size:"sm",onClick:()=>{K(null),P(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add"]})]})]}),s.jsx(St,{className:"mb-4",children:s.jsx(Nt,{className:"py-3",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white text-sm font-medium",children:i("settings.notificationLanguage")}),s.jsx("p",{className:"text-xs text-bambu-gray",children:i("settings.notificationLanguageDescription")})]}),s.jsx("select",{value:E.notification_language||"en",onChange:be=>ht("notification_language",be.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:jz.map(be=>s.jsx("option",{value:be.code,children:be.nativeName},be.code))})]})})}),jt&&s.jsx(St,{className:"mb-4",children:s.jsxs(Nt,{className:"py-3",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm font-medium text-white",children:"Test Results"}),s.jsx("button",{onClick:()=>zt(null),className:"text-bambu-gray hover:text-white text-xs",children:"Dismiss"})]}),s.jsxs("div",{className:"flex items-center gap-4 text-sm mb-2",children:[s.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[s.jsx(Un,{className:"w-4 h-4"}),jt.success," passed"]}),jt.failed>0&&s.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[s.jsx(ia,{className:"w-4 h-4"}),jt.failed," failed"]})]}),jt.results.filter(be=>!be.success).length>0&&s.jsx("div",{className:"space-y-1 mt-2 pt-2 border-t border-bambu-dark-tertiary",children:jt.results.filter(be=>!be.success).map(be=>s.jsxs("div",{className:"text-xs text-red-400",children:[s.jsxs("span",{className:"font-medium",children:[be.provider_name,":"]})," ",be.message]},be.provider_id))})]})}),kt?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):pt&&pt.length>0?s.jsx("div",{className:"space-y-3",children:pt.map(be=>s.jsx(PYe,{provider:be,onEdit:Pe=>{K(Pe),P(!0)}},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-8",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(Ef,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),s.jsx("p",{className:"text-sm font-medium text-white mb-2",children:"No providers configured"}),s.jsx("p",{className:"text-xs mb-3",children:"Add a provider to receive alerts."}),s.jsxs(Se,{size:"sm",onClick:()=>{K(null),P(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Provider"]})]})})})]}),s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-4",children:[s.jsx(Za,{className:"w-5 h-5 text-bambu-green"}),"Message Templates"]}),s.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Customize notification messages for each event."}),bn?s.jsx("div",{className:"flex justify-center py-8",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):Kt&&Kt.length>0?s.jsx("div",{className:"space-y-2",children:Kt.map(be=>s.jsx(St,{className:"cursor-pointer hover:border-bambu-green/50 transition-colors",onClick:()=>Q(be),children:s.jsx(Nt,{className:"py-2.5 px-3",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsx("p",{className:"text-white font-medium text-sm truncate",children:be.name}),s.jsx("p",{className:"text-bambu-gray text-xs truncate mt-0.5",children:be.title_template})]}),s.jsx("button",{className:"p-1.5 hover:bg-bambu-dark-tertiary rounded transition-colors shrink-0 ml-2",onClick:Pe=>{Pe.stopPropagation(),Q(be)},children:s.jsx(hN,{className:"w-4 h-4 text-bambu-gray"})})]})})},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-8",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(Za,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),s.jsx("p",{className:"text-sm",children:"No templates available. Restart the backend to seed default templates."})]})})})]})]}),de==="apikeys"&&s.jsxs("div",{className:"grid grid-cols-1 xl:grid-cols-2 gap-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-start justify-between gap-4 mb-6",children:[s.jsxs("div",{className:"flex-1",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(mf,{className:"w-5 h-5 text-bambu-green"}),"API Keys"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Create API keys for external integrations and webhooks."})]}),s.jsxs(Se,{size:"sm",onClick:()=>me(!0),className:"flex-shrink-0",children:[s.jsx(nr,{className:"w-4 h-4"}),"Create Key"]})]}),H&&s.jsx(St,{className:"mb-6 border-bambu-green",children:s.jsx(Nt,{className:"py-4",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(Un,{className:"w-5 h-5 text-bambu-green flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"flex-1",children:[s.jsx("p",{className:"text-white font-medium mb-1",children:"API Key Created Successfully"}),s.jsx("p",{className:"text-sm text-bambu-gray mb-2",children:"Copy this key now - it won't be shown again!"}),s.jsxs("div",{className:"flex items-center gap-2 bg-bambu-dark rounded-lg p-2",children:[s.jsx("code",{className:"flex-1 text-sm text-bambu-green font-mono break-all",children:H}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:async()=>{try{if(navigator.clipboard&&navigator.clipboard.writeText)await navigator.clipboard.writeText(H);else{const be=document.createElement("textarea");be.value=H,be.style.position="fixed",be.style.left="-999999px",document.body.appendChild(be),be.select(),document.execCommand("copy"),document.body.removeChild(be)}o("Key copied to clipboard")}catch{o("Failed to copy key","error")}},children:s.jsx(ll,{className:"w-4 h-4"})})]}),s.jsxs("div",{className:"flex gap-2 mt-3",children:[s.jsx(Se,{variant:"secondary",size:"sm",onClick:()=>{ce(H),o("Key added to API Browser")},children:"Use in API Browser"}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:()=>ee(null),children:"Dismiss"})]})]})]})})}),Ae&&s.jsxs(St,{className:"mb-6",children:[s.jsx(er,{children:s.jsx("h3",{className:"text-base font-semibold text-white",children:"Create New API Key"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Key Name"}),s.jsx("input",{type:"text",value:Ne,onChange:be=>ye(be.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"Permissions"}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:le.can_read_status,onChange:be=>$e(Pe=>({...Pe,can_read_status:be.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white",children:"Read Status"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"View printer status and queue"})]})]}),s.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:le.can_queue,onChange:be=>$e(Pe=>({...Pe,can_queue:be.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white",children:"Manage Queue"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Add and remove items from print queue"})]})]}),s.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:le.can_control_printer,onChange:be=>$e(Pe=>({...Pe,can_control_printer:be.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white",children:"Control Printer"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Pause, resume, and stop prints"})]})]})]})]}),s.jsxs("div",{className:"flex items-center gap-2 pt-2",children:[s.jsxs(Se,{onClick:()=>Gt.mutate({name:Ne||"Unnamed Key",...le}),disabled:Gt.isPending,children:[Gt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(nr,{className:"w-4 h-4"}),"Create Key"]}),s.jsx(Se,{variant:"secondary",onClick:()=>me(!1),children:"Cancel"})]})]})]}),ln?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):Lt&&Lt.length>0?s.jsx("div",{className:"space-y-3",children:Lt.map(be=>s.jsx(St,{children:s.jsx(Nt,{className:"py-3",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx(mf,{className:`w-5 h-5 ${be.enabled?"text-bambu-green":"text-bambu-gray"}`}),s.jsxs("div",{children:[s.jsx("p",{className:"text-white font-medium",children:be.name}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[be.key_prefix,"••••••••",be.last_used&&` · Last used: ${_g(be.last_used)}`]})]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"flex gap-1 text-xs",children:[be.can_read_status&&s.jsx("span",{className:"px-1.5 py-0.5 bg-blue-500/20 text-blue-400 rounded",children:"Read"}),be.can_queue&&s.jsx("span",{className:"px-1.5 py-0.5 bg-green-500/20 text-green-400 rounded",children:"Queue"}),be.can_control_printer&&s.jsx("span",{className:"px-1.5 py-0.5 bg-orange-500/20 text-orange-400 rounded",children:"Control"})]}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:()=>B(be.id),children:s.jsx(dn,{className:"w-4 h-4 text-red-400"})})]})]})})},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-12",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(mf,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),s.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No API keys"}),s.jsx("p",{className:"text-sm mb-4",children:"Create an API key to integrate with external services."}),s.jsxs(Se,{onClick:()=>me(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Create Your First Key"]})]})})}),s.jsxs(St,{className:"mt-6",children:[s.jsx(er,{children:s.jsx("h3",{className:"text-base font-semibold text-white",children:"Webhook Endpoints"})}),s.jsxs(Nt,{className:"space-y-3 text-sm",children:[s.jsxs("p",{className:"text-bambu-gray",children:["Use your API key in the ",s.jsx("code",{className:"text-bambu-green",children:"X-API-Key"})," header."]}),s.jsxs("div",{className:"space-y-2 font-mono text-xs",children:[s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-blue-400",children:"GET"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/status"}),s.jsx("span",{className:"text-bambu-gray",children:" - Get all printer status"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-blue-400",children:"GET"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/status/:id"}),s.jsx("span",{className:"text-bambu-gray",children:" - Get specific printer status"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-green-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/queue"}),s.jsx("span",{className:"text-bambu-gray",children:" - Add to print queue"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-orange-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/pause"}),s.jsx("span",{className:"text-bambu-gray",children:" - Pause print"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-orange-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/resume"}),s.jsx("span",{className:"text-bambu-gray",children:" - Resume print"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-red-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/stop"}),s.jsx("span",{className:"text-bambu-gray",children:" - Stop print"})]})]})]})]})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"mb-6",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx($d,{className:"w-5 h-5 text-bambu-green"}),"API Browser"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Explore and test all available API endpoints."})]}),s.jsx(St,{className:"mb-4",children:s.jsxs(Nt,{className:"py-3",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"API Key for Testing"}),s.jsx("input",{type:"text",value:I,onChange:be=>ce(be.target.value),placeholder:"Paste your API key here to test authenticated endpoints...",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white font-mono text-sm focus:border-bambu-green focus:outline-none"}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["This key will be sent as ",s.jsx("code",{className:"text-bambu-green",children:"X-API-Key"})," header with requests."]})]})}),s.jsx(QYe,{apiKey:I})]})]}),de==="virtual-printer"&&s.jsx(VYe,{}),de==="filament"&&E&&s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[s.jsx("div",{className:"flex-1 lg:max-w-xl",children:s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"AMS Display Thresholds"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Configure color thresholds for AMS humidity and temperature indicators."}),s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx($D,{className:"w-4 h-4 text-blue-400"}),s.jsx("span",{className:"font-medium",children:"Humidity"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (green) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"0",max:"100",value:E.ams_humidity_good??40,onChange:be=>ht("ams_humidity_good",parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"%"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"0",max:"100",value:E.ams_humidity_fair??60,onChange:be=>ht("ams_humidity_fair",parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"%"})]})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (bad)"})]}),s.jsxs("div",{className:"space-y-3 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx(pN,{className:"w-4 h-4 text-orange-400"}),s.jsx("span",{className:"font-medium",children:"Temperature"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (blue) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:E.ams_temp_good??28,onChange:be=>ht("ams_temp_good",parseFloat(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:E.ams_temp_fair??35,onChange:be=>ht("ams_temp_fair",parseFloat(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (hot)"})]}),s.jsxs("div",{className:"space-y-3 pt-4 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx(su,{className:"w-4 h-4 text-purple-400"}),s.jsx("span",{className:"font-medium",children:"History Retention"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Keep sensor history for"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"1",max:"365",value:E.ams_history_retention_days??30,onChange:be=>ht("ams_history_retention_days",parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"days"})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Older humidity and temperature data will be automatically deleted"})]}),s.jsxs("div",{className:"space-y-3 pt-4 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx(qr,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"font-medium",children:"Print Modal"})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-white",children:"Expand custom mapping by default"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-0.5",children:"When printing to multiple printers, show per-printer AMS mapping expanded"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.per_printer_mapping_expanded??!1,onChange:be=>ht("per_printer_mapping_expanded",be.target.checked),className:"sr-only peer"}),s.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.jsx("div",{className:"flex-1 lg:max-w-xl",children:s.jsx(BYe,{})})]}),X!==null&&s.jsx(Hn,{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:()=>{_n.mutate(X),B(null)},onCancel:()=>B(null)}),C&&s.jsx(kYe,{plug:U,onClose:()=>{D(!1),L(null)}}),q&&s.jsx(RYe,{provider:W,onClose:()=>{P(!1),K(null)}}),V&&s.jsx(DYe,{template:V,onClose:()=>Q(null)}),re&&s.jsx(IYe,{onClose:()=>J(!1)}),Te&&s.jsx(Hn,{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()=>{Le(!1);try{const be=await he.clearNotificationLogs(30);o(be.message,"success")}catch{o("Failed to clear logs","error")}},onCancel:()=>Le(!1)}),Ee&&s.jsx(Hn,{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:()=>{$(!1),localStorage.clear(),o("UI preferences reset. Refreshing...","success"),setTimeout(()=>window.location.reload(),1e3)},onCancel:()=>$(!1)}),j&&s.jsx(Hn,{title:`Turn All Plugs ${j==="on"?"On":"Off"}`,message:`This will turn ${j==="on"?"ON":"OFF"} all ${Oe?.filter(be=>be.enabled).length||0} enabled smart plugs. ${j==="off"?"Any running printers may be affected!":""}`,confirmText:`Turn All ${j==="on"?"On":"Off"}`,variant:j==="off"?"danger":"warning",onConfirm:()=>{const be=j;Y(null),Yt.mutate(be)},onCancel:()=>Y(null)}),ge&&sn?.release_notes&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:()=>we(!1),children:s.jsxs(St,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",onClick:be=>be.stopPropagation(),children:[s.jsxs(er,{className:"flex flex-row items-center justify-between shrink-0",children:[s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Release Notes - v",sn.latest_version]}),sn.release_name&&sn.release_name!==sn.latest_version&&s.jsx("p",{className:"text-sm text-bambu-gray",children:sn.release_name})]}),s.jsx("button",{onClick:()=>we(!1),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsx(Nt,{className:"overflow-y-auto flex-1",children:s.jsx("pre",{className:"text-sm text-bambu-gray whitespace-pre-wrap font-sans",children:sn.release_notes})}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary shrink-0 flex gap-2",children:[sn.release_url&&s.jsx("a",{href:sn.release_url,target:"_blank",rel:"noopener noreferrer",className:"flex-1",children:s.jsxs(Se,{variant:"secondary",className:"w-full",children:[s.jsx(zr,{className:"w-4 h-4"}),"View on GitHub"]})}),s.jsx(Se,{onClick:()=>we(!1),className:"flex-1",children:"Close"})]})]})}),de==="users"&&s.jsxs("div",{className:"grid grid-cols-1 xl:grid-cols-2 gap-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"mb-6",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(pf,{className:"w-5 h-5 text-bambu-green"}),"User Authentication"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Enable authentication to secure your Bambuddy instance and manage user access."})]}),s.jsx(St,{children:s.jsx(Nt,{className:"py-6",children:l?s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-12 h-12 rounded-full flex items-center justify-center bg-green-500/20",children:s.jsx(lT,{className:"w-6 h-6 text-green-400"})}),s.jsxs("div",{className:"flex-1",children:[s.jsx("h3",{className:"text-white font-medium",children:"Authentication Enabled"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Your Bambuddy instance is secured with authentication."})]})]}),c&&s.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Current User"}),s.jsx("p",{className:"text-white font-medium",children:c.username}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:["Role: ",s.jsx("span",{className:"capitalize",children:c.role})]})]}),s.jsx("div",{className:`px-3 py-1 rounded-full text-xs font-medium ${c.role==="admin"?"bg-purple-500/20 text-purple-300":"bg-blue-500/20 text-blue-300"}`,children:c.role==="admin"?"Admin":"User"})]})}),s.jsxs("div",{className:"pt-4 border-t border-bambu-dark-tertiary space-y-3",children:[s.jsxs(Se,{onClick:()=>e("/users"),className:"w-full",variant:"secondary",children:[s.jsx(pf,{className:"w-4 h-4"}),"Manage Users"]}),c?.role==="admin"&&s.jsxs(Se,{onClick:()=>Ze(!0),className:"w-full",variant:"secondary",children:[s.jsx(Yz,{className:"w-4 h-4"}),"Disable Authentication"]})]})]}):s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`w-12 h-12 rounded-full flex items-center justify-center ${l?"bg-green-500/20":"bg-gray-500/20"}`,children:l?s.jsx(lT,{className:"w-6 h-6 text-green-400"}):s.jsx(Yz,{className:"w-6 h-6 text-gray-400"})}),s.jsxs("div",{className:"flex-1",children:[s.jsx("h3",{className:"text-white font-medium",children:"Authentication Disabled"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Your Bambuddy instance is currently accessible without authentication."})]})]}),s.jsxs("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Enable authentication to:"}),s.jsxs("ul",{className:"space-y-2 text-sm text-bambu-gray mb-4",children:[s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Require login to access the system"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage multiple users with different roles"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Control access to printer settings and user management"})]})]}),s.jsxs(Se,{type:"button",onClick:be=>{be.preventDefault(),e("/setup")},className:"w-full",children:[s.jsx(lT,{className:"w-4 h-4"}),"Activate Authentication"]})]})]})})})]}),l&&s.jsxs("div",{children:[s.jsxs("div",{className:"mb-6",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(MP,{className:"w-5 h-5 text-bambu-green"}),"Role Permissions"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Overview of what each role can do."})]}),s.jsxs("div",{className:"space-y-4",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("div",{className:"w-8 h-8 rounded-full bg-purple-500/20 flex items-center justify-center",children:s.jsx(MP,{className:"w-4 h-4 text-purple-300"})}),s.jsx("h3",{className:"text-white font-medium",children:"Admin"})]})}),s.jsx(Nt,{children:s.jsxs("ul",{className:"space-y-2 text-sm text-bambu-gray",children:[s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage printer settings"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Create, edit, and delete users"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Access all system features"})]})]})})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("div",{className:"w-8 h-8 rounded-full bg-blue-500/20 flex items-center justify-center",children:s.jsx(pf,{className:"w-4 h-4 text-blue-300"})}),s.jsx("h3",{className:"text-white font-medium",children:"User"})]})}),s.jsx(Nt,{children:s.jsxs("ul",{className:"space-y-2 text-sm text-bambu-gray",children:[s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Send print jobs"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage files and archives"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage filament"})]})]})})]})]})]})]}),de==="backup"&&s.jsx(KYe,{}),ve&&s.jsx(Hn,{title:"Disable Authentication",message:"Are you sure you want to disable authentication? This will make your Bambuddy instance accessible without login. All users will remain in the database but authentication will be disabled.",confirmText:"Disable Authentication",variant:"danger",onConfirm:async()=>{try{await he.disableAuth(),o("Authentication disabled successfully","success"),await u(),Ze(!1),window.location.href="/"}catch(be){const Pe=be instanceof Error?be.message:"Failed to disable authentication";o(Pe,"error")}},onCancel:()=>Ze(!1)})]})}const Gie=t=>{const e=parseFloat(t);return(Math.trunc(e*1e3)/1e3).toFixed(3)},tZe=t=>t.startsWith("HH")?"HF":"S",nZe=t=>{const e=t.match(/^([A-Z]{2}\d{2})/);return e?e[1]:"HH00"},wD=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 Kj({profile:t,onEdit:e,onCopy:n,selectionMode:r,isSelected:i,onToggleSelect:a,note:o}){const l=tZe(t.nozzle_id),c=t.nozzle_diameter,u=()=>{r&&a?a():e()};return s.jsxs("div",{className:"flex items-center gap-2",children:[r&&s.jsx("button",{onClick:a,className:"text-bambu-gray hover:text-white transition-colors p-1",children:i?s.jsx(Lo,{className:"w-4 h-4 text-bambu-green"}):s.jsx(Io,{className:"w-4 h-4"})}),s.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:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-bambu-green font-mono text-sm font-bold whitespace-nowrap",children:Gie(t.k_value)}),s.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name||"Unnamed"}),o&&s.jsx("span",{title:"Has note",children:s.jsx(EK,{className:"w-3 h-3 text-yellow-500"})}),s.jsxs("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:[l," ",c]})]}),o&&s.jsxs("div",{className:"text-xs mt-0.5 truncate text-yellow-500/70",title:o,children:["Note: ",o.length>50?o.substring(0,50)+"...":o]})]}),!r&&n&&s.jsx("button",{onClick:d=>{d.stopPropagation(),n()},className:"text-bambu-gray hover:text-white transition-colors p-1",title:"Copy profile",children:s.jsx(ll,{className:"w-4 h-4"})})]})}function Xj({profile:t,printerId:e,nozzleDiameter:n,existingProfiles:r=[],isDualNozzle:i=!1,initialNote:a="",initialNoteKey:o=null,onClose:l,onSave:c,onSaveNote:u}){const{showToast:d}=qn(),[f,m]=w.useState(t?.name||""),[p,b]=w.useState(t?.k_value?Gie(t.k_value):"0.020"),[y,x]=w.useState(t?.filament_id||""),[v,S]=w.useState(t?.nozzle_id?nZe(t.nozzle_id):"HH00"),[M,A]=w.useState(t?.nozzle_diameter||n),[T,k]=w.useState(t?[t.extruder_id]:i?[0,1]:[0]),[E,O]=w.useState(!1),[C,D]=w.useState({current:0,total:0}),[U,L]=w.useState(a),q=Ur.useMemo(()=>{const J=new Map;for(const z of r)if(z.filament_id&&!J.has(z.filament_id)){const G=wD(z.name||"");J.set(z.filament_id,{id:z.filament_id,name:G||z.filament_id})}return Array.from(J.values()).sort((z,G)=>z.name.localeCompare(G.name))},[r]),P=mt({mutationFn:J=>(console.log("[KProfile] Calling API..."),he.setKProfile(e,J)),onSuccess:J=>{if(console.log("[KProfile] Save success:",J),d("K-profile saved"),u&&U!==a){let z;U===""&&o?z=o:t&&t.slot_id>0?z=t.setting_id||`slot_${t.slot_id}_${t.filament_id}_${t.extruder_id}`:z=`name_${f}_${y}`,u(z,U)}O(!0),setTimeout(()=>{O(!1),c()},2500)},onError:J=>{console.error("[KProfile] Save error:",J),d(J.message,"error"),O(!1)}}),W=mt({mutationFn:J=>(console.log("[KProfile] Deleting profile..."),he.deleteKProfile(e,J)),onSuccess:J=>{console.log("[KProfile] Delete success:",J),d("K-profile deleted"),O(!0),setTimeout(()=>{O(!1),l()},4e3)},onError:J=>{console.error("[KProfile] Delete error:",J),d(J.message,"error"),O(!1)}}),[K,V]=w.useState(!1),Q=()=>{t&&W.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})},re=async J=>{if(J.preventDefault(),i&&!t&&T.length===0){d("Please select at least one extruder","error");return}const z=parseFloat(p).toFixed(6),G=`${v}-${M}`;if(t||T.length===1){const ae={name:f,k_value:z,filament_id:y,nozzle_id:G,nozzle_diameter:M,extruder_id:t?t.extruder_id:T[0],setting_id:t?.setting_id,slot_id:t?.slot_id??0};console.log("[KProfile] Saving profile:",ae),P.mutate(ae);return}O(!0),D({current:1,total:T.length});const ne=T.map(ae=>({name:f,k_value:z,filament_id:y,nozzle_id:G,nozzle_diameter:M,extruder_id:ae,setting_id:void 0,slot_id:0}));console.log(`[KProfile] Saving ${ne.length} profiles in batch:`,ne);try{if(await he.setKProfilesBatch(e,ne),d(`K-profile saved to ${T.length} extruders`),u&&U){const ae=`name_${f}_${y}`;u(ae,U)}}catch(ae){console.error("[KProfile] Failed to save batch:",ae),d("Failed to save K-profiles","error"),O(!1),D({current:0,total:0});return}D({current:T.length,total:T.length}),setTimeout(()=>{O(!1),D({current:0,total:0}),c()},3e3)};return s.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:[s.jsxs(St,{className:"w-full max-w-md relative",children:[E&&s.jsxs("div",{className:"absolute inset-0 bg-bambu-dark-secondary/90 flex flex-col items-center justify-center z-10 rounded-lg",children:[s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin mb-3"}),s.jsx("p",{className:"text-white font-medium",children:C.total>1?`Saving to extruder ${C.current}/${C.total}...`:"Syncing with printer..."}),s.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:"Please wait"})]}),s.jsxs(Nt,{className:"p-0",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsx("h2",{className:"text-xl font-semibold text-white",children:t?"Edit K-Profile":"Add K-Profile"}),s.jsx("button",{onClick:l,className:"text-bambu-gray hover:text-white transition-colors",disabled:E,children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:re,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Profile Name"}),s.jsx("input",{type:"text",value:f,onChange:J=>m(J.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})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"K-Value"}),s.jsx("input",{type:"text",inputMode:"decimal",value:p,onChange:J=>{const z=J.target.value;(z===""||/^\d*\.?\d*$/.test(z))&&b(z)},onBlur:J=>{const z=parseFloat(J.target.value);isNaN(z)||b((Math.trunc(z*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}),s.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Filament"}),s.jsxs("select",{value:y,onChange:J=>{const z=J.target.value;if(x(z),!t&&z&&!f){const G=q.find(ne=>ne.id===z);G&&m(`${v==="HH00"?"HF":"S"} ${G.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:[s.jsx("option",{value:"",children:"Select filament..."}),t?.filament_id&&s.jsx("option",{value:t.filament_id,children:q.find(J=>J.id===t.filament_id)?.name||t.filament_id},t.filament_id),!t&&q.map(J=>s.jsx("option",{value:J.id,children:J.name},J.id))]}),!t&&q.length===0&&s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"No filaments found. Create a K-profile in Bambu Studio first."})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Flow Type"}),s.jsxs("select",{value:v,onChange:J=>{const z=J.target.value;if(S(z),!t&&y&&!f){const G=q.find(ne=>ne.id===y);G&&m(`${z==="HS00"?"HF":"S"} ${G.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:[s.jsx("option",{value:"HH00",children:"High Flow"}),s.jsx("option",{value:"HS00",children:"Standard"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle Size"}),s.jsxs("select",{value:M,onChange:J=>A(J.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:[s.jsx("option",{value:"0.2",children:"0.2mm"}),s.jsx("option",{value:"0.4",children:"0.4mm"}),s.jsx("option",{value:"0.6",children:"0.6mm"}),s.jsx("option",{value:"0.8",children:"0.8mm"})]})]})]}),i&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:t?"Extruder":"Extruders"}),t?s.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"}):s.jsxs("div",{className:"flex gap-4",children:[s.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:T.includes(1),onChange:J=>{J.target.checked?k([...T,1]):k(T.filter(z=>z!==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"}),s.jsx("span",{className:"text-white",children:"Left"})]}),s.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:T.includes(0),onChange:J=>{J.target.checked?k([...T,0]):k(T.filter(z=>z!==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"}),s.jsx("span",{className:"text-white",children:"Right"})]})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Notes (stored locally)"}),s.jsx("textarea",{value:U,onChange:J=>L(J.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Notes are saved in Bambuddy, not on the printer"})]}),s.jsxs("div",{className:"flex gap-2 pt-4",children:[t&&s.jsx(Se,{type:"button",variant:"secondary",onClick:()=>V(!0),disabled:W.isPending||E,className:"text-red-500 hover:bg-red-500/10",children:W.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"})}),s.jsx(Se,{type:"button",variant:"secondary",onClick:l,disabled:E,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:P.isPending||E,className:"flex-1",children:[P.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(oN,{className:"w-4 h-4"}),"Save"]})]})]})]})]}),K&&s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-[60]",children:s.jsx(St,{className:"w-full max-w-sm",children:s.jsxs(Nt,{className:"p-6",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[s.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:s.jsx(dn,{className:"w-5 h-5 text-red-500"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profile"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),s.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",s.jsxs("span",{className:"text-white font-medium",children:['"',t?.name,'"']})," from the printer?"]}),s.jsxs("div",{className:"flex gap-3",children:[s.jsx(Se,{variant:"secondary",onClick:()=>V(!1),className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:()=>{V(!1),Q()},disabled:W.isPending,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[W.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}const e1={NOZZLE_DIAMETER:"bambusy_kprofiles_nozzle",SORT_OPTION:"bambusy_kprofiles_sort"};function rZe(){const{showToast:t}=qn(),[e,n]=w.useState(null),[r,i]=w.useState(()=>localStorage.getItem(e1.NOZZLE_DIAMETER)||"0.4"),[a,o]=w.useState(null),[l,c]=w.useState(!1),[u,d]=w.useState(null),[f,m]=w.useState(""),[p,b]=w.useState("all"),[y,x]=w.useState("all"),[v,S]=w.useState(()=>localStorage.getItem(e1.SORT_OPTION)||"name"),[M,A]=w.useState(!1),[T,k]=w.useState(new Set),[E,O]=w.useState(!1),[C,D]=w.useState(!1),U=w.useCallback(B=>`${B.slot_id}_${B.extruder_id}`,[]);w.useEffect(()=>{localStorage.setItem(e1.NOZZLE_DIAMETER,r)},[r]),w.useEffect(()=>{localStorage.setItem(e1.SORT_OPTION,v)},[v]);const{data:L,isLoading:q}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:P,isLoading:W,isFetching:K,error:V,refetch:Q}=ut({queryKey:["kprofiles",e,r],queryFn:async()=>{console.log("[KProfiles] Fetching profiles for printer",e,"nozzle",r);const B=await he.getKProfiles(e,r);return console.log("[KProfiles] Received profiles:",B?.profiles?.length||0,"profiles"),B},enabled:!!e,retry:!1,staleTime:0,gcTime:0,refetchOnMount:"always"}),{data:re}=ut({queryKey:["kprofiles",e,"0.4"],queryFn:()=>he.getKProfiles(e,"0.4"),enabled:!!e,staleTime:6e4}),{data:J,refetch:z}=ut({queryKey:["kprofile-notes",e],queryFn:()=>he.getKProfileNotes(e),enabled:!!e,staleTime:3e4}),G=V?.message?.includes("not connected");w.useEffect(()=>{if(!e&&L&&L.length>0){const B=L.find(I=>I.is_active);B&&n(B.id)}},[e,L]),w.useEffect(()=>{if(e){const B=setTimeout(()=>{Q()},150);return()=>clearTimeout(B)}},[e,r]);const ne=L?.filter(B=>B.is_active)||[],ae=Ur.useMemo(()=>P?.profiles?P.profiles.filter(I=>{const ce=f.toLowerCase(),Te=!ce||I.name.toLowerCase().includes(ce)||I.filament_id.toLowerCase().includes(ce),Le=p==="all"||p==="left"&&I.extruder_id===1||p==="right"&&I.extruder_id===0,Ee=y==="all"||y==="hf"&&I.nozzle_id.startsWith("HH")||y==="s"&&I.nozzle_id.startsWith("HS");return Te&&Le&&Ee}).sort((I,ce)=>{switch(v){case"k_value":return parseFloat(I.k_value)-parseFloat(ce.k_value);case"filament":return wD(I.name).localeCompare(wD(ce.name));default:return I.name.localeCompare(ce.name)}}):[],[P?.profiles,f,p,y,v]),de=L?.find(B=>B.id===e),fe=de?.nozzle_count===2;w.useEffect(()=>{const B=I=>{I.target instanceof HTMLInputElement||I.target instanceof HTMLTextAreaElement||I.target instanceof HTMLSelectElement||a||l||u||(I.key==="r"||I.key==="R"?(I.preventDefault(),Q()):I.key==="n"||I.key==="N"?(I.preventDefault(),c(!0)):I.key==="Escape"&&M&&(I.preventDefault(),A(!1),k(new Set)))};return window.addEventListener("keydown",B),()=>window.removeEventListener("keydown",B)},[a,l,u,M,Q]);const ie=w.useCallback(()=>{if(!P?.profiles||P.profiles.length===0){t("No profiles to export","error");return}const B={version:1,exported_at:new Date().toISOString(),printer:de?.name||"Unknown",nozzle_diameter:r,profiles:P.profiles.map(Le=>({name:Le.name,k_value:Le.k_value,filament_id:Le.filament_id,nozzle_id:Le.nozzle_id,nozzle_diameter:Le.nozzle_diameter,extruder_id:Le.extruder_id}))},I=new Blob([JSON.stringify(B,null,2)],{type:"application/json"}),ce=URL.createObjectURL(I),Te=document.createElement("a");Te.href=ce,Te.download=`kprofiles_${de?.name||"printer"}_${r}mm_${new Date().toISOString().split("T")[0]}.json`,document.body.appendChild(Te),Te.click(),document.body.removeChild(Te),URL.revokeObjectURL(ce),t(`Exported ${P.profiles.length} profiles`)},[P?.profiles,de,r,t]),Ae=w.useCallback(()=>{const B=document.createElement("input");B.type="file",B.accept=".json",B.onchange=async I=>{const ce=I.target.files?.[0];if(ce)try{const Te=await ce.text(),Le=JSON.parse(Te);if(!Le.profiles||!Array.isArray(Le.profiles)){t("Invalid file format","error");return}let Ee=0;for(const $ of Le.profiles)if(!(!$.name||!$.k_value||!$.filament_id))try{await he.setKProfile(e,{name:$.name,k_value:parseFloat($.k_value).toFixed(6),filament_id:$.filament_id,nozzle_id:$.nozzle_id||`HH00-${r}`,nozzle_diameter:$.nozzle_diameter||r,extruder_id:$.extruder_id??0,slot_id:0}),Ee++,await new Promise(j=>setTimeout(j,500))}catch(j){console.error("Failed to import profile:",$.name,j)}t(`Imported ${Ee} of ${Le.profiles.length} profiles`),Q()}catch(Te){console.error("Import error:",Te),t("Failed to parse import file","error")}},B.click()},[e,r,t,Q]),me=w.useCallback(B=>{k(I=>{const ce=new Set(I);return ce.has(B)?ce.delete(B):ce.add(B),ce})},[]),Ne=w.useCallback(()=>{k(new Set(ae.map(B=>U(B))))},[ae,U]),ye=w.useCallback(()=>{T.size!==0&&O(!0)},[T.size]),le=w.useCallback(async()=>{const B=ae.filter(ce=>T.has(U(ce)));D(!0);let I=0;for(const ce of B)try{await he.deleteKProfile(e,{slot_id:ce.slot_id,extruder_id:ce.extruder_id,nozzle_id:ce.nozzle_id,nozzle_diameter:ce.nozzle_diameter,filament_id:ce.filament_id,setting_id:ce.setting_id}),I++,await new Promise(Te=>setTimeout(Te,300))}catch(Te){console.error("Failed to delete profile:",ce.name,Te)}t(`Deleted ${I} profiles`),D(!1),O(!1),A(!1),k(new Set),Q()},[e,T,ae,t,Q,U]),$e=w.useCallback(B=>{const I=[];return B.setting_id&&I.push(B.setting_id),I.push(`slot_${B.slot_id}_${B.filament_id}_${B.extruder_id}`),I.push(`name_${B.name}_${B.filament_id}`),I},[]),H=w.useCallback(async(B,I)=>{if(e)try{await he.setKProfileNote(e,B,I),z()}catch(ce){console.error("Failed to save note:",ce),t("Failed to save note","error")}},[e,z,t]),ee=w.useCallback(B=>{if(!J?.notes)return{note:"",key:null};const I=$e(B);for(const ce of I)if(J.notes[ce])return{note:J.notes[ce],key:ce};return{note:"",key:null}},[J,$e]),X=w.useCallback(B=>ee(B).note,[ee]);return q?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):!L||L.length===0?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(oa,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Printers Configured"}),s.jsx("p",{className:"text-bambu-gray",children:"Add a printer in Settings to manage K-profiles"})]})}):ne.length===0?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(qr,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Active Printers"}),s.jsx("p",{className:"text-bambu-gray",children:"Enable a printer connection to view its K-profiles"})]})}):s.jsxs(s.Fragment,{children:[K&&!W&&s.jsxs("div",{className:"fixed inset-0 bg-black/50 flex flex-col items-center justify-center z-40",children:[s.jsx(dt,{className:"w-10 h-10 text-bambu-green animate-spin mb-3"}),s.jsx("p",{className:"text-white font-medium",children:"Loading K-Profiles..."})]}),s.jsxs("div",{className:"flex flex-wrap gap-4 mb-6",children:[s.jsxs("div",{className:"flex-1 min-w-48",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer"}),s.jsx("select",{value:e||"",onChange:B=>n(parseInt(B.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:ne.map(B=>s.jsx("option",{value:B.id,children:B.name},B.id))})]}),s.jsxs("div",{className:"w-32",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle"}),s.jsxs("select",{value:r,onChange:B=>i(B.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:[s.jsx("option",{value:"0.2",children:"0.2mm"}),s.jsx("option",{value:"0.4",children:"0.4mm"}),s.jsx("option",{value:"0.6",children:"0.6mm"}),s.jsx("option",{value:"0.8",children:"0.8mm"})]})]}),s.jsxs("div",{className:"flex items-end gap-2",children:[s.jsxs(Se,{variant:"secondary",onClick:()=>Q(),disabled:K,children:[s.jsx(xr,{className:`w-4 h-4 ${K?"animate-spin":""}`}),"Refresh"]}),s.jsxs(Se,{onClick:()=>c(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Profile"]})]})]}),s.jsxs("div",{className:"flex flex-wrap gap-4 mb-4",children:[s.jsxs("div",{className:"flex-1 min-w-48 relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:f,onChange:B=>m(B.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"})]}),fe&&s.jsx("div",{className:"w-36",children:s.jsxs("select",{value:p,onChange:B=>b(B.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:[s.jsx("option",{value:"all",children:"All Extruders"}),s.jsx("option",{value:"left",children:"Left Only"}),s.jsx("option",{value:"right",children:"Right Only"})]})}),s.jsx("div",{className:"w-32",children:s.jsxs("select",{value:y,onChange:B=>x(B.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:[s.jsx("option",{value:"all",children:"All Flow"}),s.jsx("option",{value:"hf",children:"HF Only"}),s.jsx("option",{value:"s",children:"S Only"})]})}),s.jsx("div",{className:"w-32",children:s.jsxs("select",{value:v,onChange:B=>S(B.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:[s.jsx("option",{value:"name",children:"Sort: Name"}),s.jsx("option",{value:"k_value",children:"Sort: K-Value"}),s.jsx("option",{value:"filament",children:"Sort: Filament"})]})})]}),s.jsxs("div",{className:"flex flex-wrap gap-2 mb-6",children:[s.jsxs(Se,{variant:"secondary",onClick:ie,disabled:!P?.profiles?.length,title:"Export profiles to JSON",children:[s.jsx(Hr,{className:"w-4 h-4"}),"Export"]}),s.jsxs(Se,{variant:"secondary",onClick:Ae,title:"Import profiles from JSON",children:[s.jsx(pi,{className:"w-4 h-4"}),"Import"]}),s.jsx("div",{className:"flex-1"}),M?s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"secondary",onClick:Ne,title:"Select all visible profiles",children:[s.jsx(Lo,{className:"w-4 h-4"}),"Select All"]}),s.jsxs(Se,{variant:"secondary",onClick:ye,disabled:T.size===0,className:"text-red-500 hover:bg-red-500/10",title:`Delete ${T.size} selected profiles`,children:[s.jsx(dn,{className:"w-4 h-4"}),"Delete (",T.size,")"]}),s.jsxs(Se,{variant:"secondary",onClick:()=>{A(!1),k(new Set)},children:[s.jsx(Vt,{className:"w-4 h-4"}),"Cancel"]})]}):s.jsxs(Se,{variant:"secondary",onClick:()=>A(!0),disabled:!ae.length,title:"Enter selection mode for bulk delete",children:[s.jsx(Lo,{className:"w-4 h-4"}),"Select"]})]}),W?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):G?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(ex,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"Printer Offline"}),s.jsx("p",{className:"text-bambu-gray mb-4",children:"The selected printer is not connected. Power it on to view K-profiles."}),s.jsxs(Se,{variant:"secondary",onClick:()=>Q(),children:[s.jsx(xr,{className:"w-4 h-4"}),"Retry"]})]})}):ae.length>0?fe?s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Left Extruder"}),s.jsx("div",{className:"space-y-1",children:ae.filter(B=>B.extruder_id===1).map(B=>s.jsx(Kj,{profile:B,onEdit:()=>o(B),onCopy:()=>d(B),selectionMode:M,isSelected:T.has(U(B)),onToggleSelect:()=>me(U(B)),note:X(B)},U(B)))})]}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Right Extruder"}),s.jsx("div",{className:"space-y-1",children:ae.filter(B=>B.extruder_id===0).map(B=>s.jsx(Kj,{profile:B,onEdit:()=>o(B),onCopy:()=>d(B),selectionMode:M,isSelected:T.has(U(B)),onToggleSelect:()=>me(U(B)),note:X(B)},U(B)))})]})]}):s.jsx("div",{className:"space-y-1",children:ae.map(B=>s.jsx(Kj,{profile:B,onEdit:()=>o(B),onCopy:()=>d(B),selectionMode:M,isSelected:T.has(U(B)),onToggleSelect:()=>me(U(B)),note:X(B)},U(B)))}):f||p!=="all"||y!=="all"?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(Ea,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Matching Profiles"}),s.jsx("p",{className:"text-bambu-gray",children:"No profiles match your search criteria"})]})}):s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(oN,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No K-Profiles"}),s.jsxs("p",{className:"text-bambu-gray mb-4",children:["No pressure advance profiles found for ",r,"mm nozzle"]}),s.jsxs(Se,{onClick:()=>c(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Create First Profile"]})]})}),a&&e&&(()=>{const{note:B,key:I}=ee(a);return s.jsx(Xj,{profile:a,printerId:e,nozzleDiameter:r,existingProfiles:re?.profiles||P?.profiles,isDualNozzle:fe,initialNote:B,initialNoteKey:I,onSaveNote:H,onClose:()=>{console.log("[KProfiles] Edit modal onClose - refetching profiles..."),o(null),Q()},onSave:()=>{o(null),Q()}})})(),l&&e&&s.jsx(Xj,{printerId:e,nozzleDiameter:r,existingProfiles:re?.profiles||P?.profiles,isDualNozzle:fe,onSaveNote:H,onClose:()=>{c(!1),Q()},onSave:()=>{c(!1),Q()}}),u&&e&&s.jsx(Xj,{printerId:e,nozzleDiameter:r,existingProfiles:re?.profiles||P?.profiles,isDualNozzle:fe,onSaveNote:H,profile:{...u,slot_id:0,name:`${u.name} (Copy)`},onClose:()=>{d(null),Q()},onSave:()=>{d(null),Q()}}),E&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:s.jsx(St,{className:"w-full max-w-sm",children:s.jsxs(Nt,{className:"p-6",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[s.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:s.jsx(dn,{className:"w-5 h-5 text-red-500"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profiles"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),s.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",s.jsx("span",{className:"text-white font-medium",children:T.size})," selected profiles from the printer?"]}),s.jsxs("div",{className:"flex gap-3",children:[s.jsx(Se,{variant:"secondary",onClick:()=>O(!1),disabled:C,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:le,disabled:C,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[C?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}function _I(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),o=a?(a[1]||a[2])+"mm":null,l=n.match(/(\d+\.?\d*)mm\s*(?:Standard|Fine|Extra Fine|Draft|Quality)?/i),c=l?l[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:o,layerHeight:c,filamentType:d}}function _y(t){return/^(P[FPM]US|PF\d|PP\d)/.test(t)}function iZe(t){const e=Ni(t);if(!e)return"";const r=new Date().getTime()-e.getTime(),i=Math.floor(r/6e4),a=Math.floor(r/36e5),o=Math.floor(r/864e5);return i<1?"Just now":i<60?`${i}m ago`:a<24?`${a}h ago`:o<7?`${o}d ago`:e.toLocaleDateString()}function aZe({onSuccess:t}){const{showToast:e}=qn(),[n,r]=w.useState("email"),[i,a]=w.useState(""),[o,l]=w.useState(""),[c,u]=w.useState(""),[d,f]=w.useState(""),[m,p]=w.useState("global"),b=mt({mutationFn:()=>he.cloudLogin(i,o,m),onSuccess:M=>{M.success?(e("Logged in successfully"),t()):M.needs_verification?(e("Verification code sent to your email"),r("code")):e(M.message,"error")},onError:M=>e(M.message,"error")}),y=mt({mutationFn:()=>he.cloudVerify(i,c),onSuccess:M=>{M.success?(e("Logged in successfully"),t()):e(M.message,"error")},onError:M=>e(M.message,"error")}),x=mt({mutationFn:()=>he.cloudSetToken(d),onSuccess:()=>{e("Token set successfully"),t()},onError:M=>e(M.message,"error")}),v=M=>{M.preventDefault(),n==="email"?b.mutate():n==="code"?y.mutate():n==="token"&&x.mutate()},S=b.isPending||y.isPending||x.isPending;return s.jsx(St,{className:"max-w-md mx-auto",children:s.jsxs(Nt,{children:[s.jsxs("div",{className:"text-center mb-6",children:[s.jsx("div",{className:"inline-flex items-center justify-center w-12 h-12 rounded-xl bg-bambu-green/20 mb-3",children:s.jsx(iN,{className:"w-6 h-6 text-bambu-green"})}),s.jsx("h2",{className:"text-xl font-semibold text-white",children:"Connect to Bambu Cloud"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Sync your slicer presets across devices"})]}),s.jsxs("form",{onSubmit:v,className:"space-y-4",children:[n==="email"&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Email"}),s.jsx("input",{type:"email",value:i,onChange:M=>a(M.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})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),s.jsx("input",{type:"password",value:o,onChange:M=>l(M.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})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Region"}),s.jsxs("select",{value:m,onChange:M=>p(M.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:[s.jsx("option",{value:"global",children:"Global"}),s.jsx("option",{value:"china",children:"China"})]})]})]}),n==="code"&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Verification Code"}),s.jsxs("p",{className:"text-xs text-bambu-gray mb-2",children:["Check your email (",i,") for a 6-digit code"]}),s.jsx("input",{type:"text",value:c,onChange:M=>u(M.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"&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Access Token"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Paste your Bambu Lab access token (from Bambu Studio)"}),s.jsx("textarea",{value:d,onChange:M=>f(M.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})]}),s.jsxs("div",{className:"flex gap-2",children:[n==="code"&&s.jsx(Se,{type:"button",variant:"secondary",onClick:()=>r("email"),className:"flex-1",children:"Back"}),s.jsxs(Se,{type:"submit",disabled:S,className:"flex-1",children:[S?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Zz,{className:"w-4 h-4"}),n==="email"?"Login":n==="code"?"Verify":"Set Token"]})]}),n==="email"&&s.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:s.jsxs("button",{type:"button",onClick:()=>r("token"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[s.jsx(mf,{className:"w-4 h-4"}),"Use access token instead"]})}),n==="token"&&s.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:s.jsxs("button",{type:"button",onClick:()=>r("email"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[s.jsx(Zz,{className:"w-4 h-4"}),"Login with email instead"]})})]})]})})}function $p({label:t,value:e,options:n,onChange:r}){const[i,a]=w.useState(!1),o=n.find(l=>l.value===e);return s.jsxs("div",{className:"relative",children:[s.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:[s.jsxs("span",{className:"text-bambu-gray",children:[t,":"]}),s.jsx("span",{children:o?.label||"All"}),s.jsx(Oi,{className:`w-4 h-4 text-bambu-gray transition-transform ${i?"rotate-180":""}`})]}),i&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>a(!1)}),s.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(l=>s.jsxs("button",{onClick:()=>{r(l.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===l.value?"text-bambu-green":"text-white"}`,children:[s.jsx("span",{children:l.label}),l.count!==void 0&&s.jsx("span",{className:"text-bambu-gray text-xs",children:l.count})]},l.value))})]})]})}function sZe(){const[t,e]=w.useState(!1);w.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?s.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:s.jsx(bg,{className:"w-5 h-5"})}):null}function Yj({setting:t,onClick:e,onDuplicate:n,compareMode:r,isCompareSelected:i,compareIndex:a,compareDisabled:o}){const l=_I(t.name),c=_y(t.setting_id);return s.jsxs("div",{className:"flex items-center gap-2 group",children:[s.jsx("button",{onClick:e,disabled:o,className:`flex-1 text-left px-3 py-2 rounded transition-colors ${i?"bg-blue-500/20 border border-blue-500/50":o?"bg-bambu-dark/50 opacity-40 cursor-not-allowed":"bg-bambu-dark hover:bg-bambu-dark-tertiary"} ${r&&!o?"cursor-pointer":""}`,children:s.jsxs("div",{className:"flex items-center gap-2",children:[i&&a!==void 0&&s.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&&s.jsx("span",{className:"flex-shrink-0 w-1.5 h-1.5 rounded-full bg-bambu-green",title:"My preset (editable)"}),s.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name}),l.filamentType&&t.type==="filament"&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:l.filamentType}),l.layerHeight&&t.type==="process"&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:l.layerHeight}),l.printer&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:l.printer})]})}),s.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:s.jsx(ll,{className:"w-4 h-4"})})]})}function SD(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:s.jsxs("div",{className:"flex items-center gap-4 p-4",children:[M&&d&&s.jsx("button",{onClick:T=>{T.stopPropagation(),d()},className:`flex items-center justify-center w-6 h-6 rounded border transition-colors ${u?"bg-bambu-green border-bambu-green text-white":"border-white/30 bg-black/30 hover:border-bambu-green/50"}`,children:u&&s.jsx(ns,{className:"w-4 h-4"})}),M?s.jsx("div",{...f,...m,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:s.jsx(yg,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}):e!==void 0?s.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]}):s.jsx("div",{className:"w-8"}),s.jsx("div",{className:"w-14 h-14 flex-shrink-0 bg-bambu-dark rounded-lg overflow-hidden",children:t.archive_thumbnail?s.jsx("img",{src:he.getArchiveThumbnail(t.archive_id),alt:"",className:"w-full h-full object-cover"}):t.library_file_thumbnail?s.jsx("img",{src:he.getLibraryFileThumbnailUrl(t.library_file_id),alt:"",className:"w-full h-full object-cover"}):s.jsx("div",{className:"w-full h-full flex items-center justify-center text-bambu-gray",children:s.jsx(ul,{className:"w-6 h-6"})})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[s.jsx("p",{className:"text-white font-medium truncate",children:t.archive_name||t.library_file_name||`File #${t.archive_id||t.library_file_id}`}),t.archive_id?s.jsx(bs,{to:`/archives?highlight=${t.archive_id}`,className:"text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0",title:"View archive",children:s.jsx(zr,{className:"w-3.5 h-3.5"})}):t.library_file_id?s.jsx(bs,{to:`/library?highlight=${t.library_file_id}`,className:"text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0",title:"View in File Manager",children:s.jsx(zr,{className:"w-3.5 h-3.5"})}):null]}),s.jsxs("div",{className:"flex items-center gap-3 text-sm text-bambu-gray",children:[s.jsxs("span",{className:`flex items-center gap-1.5 ${t.printer_id===null?"text-orange-400":""}`,children:[s.jsx(qr,{className:"w-3.5 h-3.5"}),t.printer_id===null?"Unassigned":t.printer_name||`Printer #${t.printer_id}`]}),t.print_time_seconds&&s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx(jd,{className:"w-3.5 h-3.5"}),Vie(t.print_time_seconds)]}),M&&!t.manual_start&&s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx(Zn,{className:"w-3.5 h-3.5"}),dYe(t.scheduled_time,c)]})]}),s.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[t.manual_start&&s.jsxs("span",{className:"text-xs px-2 py-0.5 bg-purple-500/10 text-purple-400 rounded-full border border-purple-500/20 flex items-center gap-1",children:[s.jsx(yK,{className:"w-3 h-3"}),"Staged"]}),t.require_previous_success&&s.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&&s.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:[s.jsx(Eu,{className:"w-3 h-3"}),"Auto power off"]})]}),S&&s.jsxs("div",{className:"mt-3",children:[s.jsx("div",{className:"h-2 bg-bambu-dark rounded-full overflow-hidden",children:s.jsx("div",{className:"h-full bg-gradient-to-r from-blue-500 to-blue-400 animate-pulse w-full opacity-50"})}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Printing in progress..."})]}),t.error_message&&s.jsxs("p",{className:"text-xs text-red-400 mt-2 flex items-center gap-1",children:[s.jsx(oa,{className:"w-3 h-3"}),t.error_message]})]}),s.jsx(hYe,{status:t.status}),s.jsxs("div",{className:"flex items-center gap-1",children:[S&&s.jsx(Se,{variant:"ghost",size:"sm",onClick:a,title:"Stop Print",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:s.jsx(gde,{className:"w-4 h-4"})}),M&&s.jsxs(s.Fragment,{children:[t.manual_start&&s.jsx(Se,{variant:"ghost",size:"sm",onClick:l,title:"Start Print",className:"text-bambu-green hover:text-bambu-green-light hover:bg-bambu-green/10",children:s.jsx(Oo,{className:"w-4 h-4"})}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:n,title:"Edit",children:s.jsx(la,{className:"w-4 h-4"})}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:r,title:"Cancel",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",children:s.jsx(Vt,{className:"w-4 h-4"})})]}),A&&s.jsxs(s.Fragment,{children:[s.jsx(Se,{variant:"ghost",size:"sm",onClick:o,title:"Re-queue",className:"text-bambu-green hover:text-bambu-green/80 hover:bg-bambu-green/10",children:s.jsx(xr,{className:"w-4 h-4"})}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:i,title:"Remove",children:s.jsx(dn,{className:"w-4 h-4"})})]})]})]})})}function mYe(){const t=on(),{showToast:e}=qn(),[n,r]=w.useState(null),[i,a]=w.useState(""),[o,l]=w.useState(!1),[c,u]=w.useState(null),[d,f]=w.useState(null),[m,p]=w.useState(null),[b,y]=w.useState([]),[x,v]=w.useState(!1),[S,M]=w.useState(()=>localStorage.getItem("queue.historySortBy")||"date"),[A,T]=w.useState(()=>{const ye=localStorage.getItem("queue.historySortAsc");return ye!==null?ye==="true":!1}),[k,E]=w.useState(()=>localStorage.getItem("queue.pendingSortBy")||"position"),[O,C]=w.useState(()=>{const ye=localStorage.getItem("queue.pendingSortAsc");return ye!==null?ye==="true":!0});w.useEffect(()=>{localStorage.setItem("queue.historySortBy",S)},[S]),w.useEffect(()=>{localStorage.setItem("queue.historySortAsc",String(A))},[A]),w.useEffect(()=>{localStorage.setItem("queue.pendingSortBy",k)},[k]),w.useEffect(()=>{localStorage.setItem("queue.pendingSortAsc",String(O))},[O]);const D=yie(YM(UE,{activationConstraint:{distance:8}}),YM(FE,{coordinateGetter:$ie})),{data:U}=ut({queryKey:["settings"],queryFn:he.getSettings}),L=U?.time_format||"system",{data:q,isLoading:P}=ut({queryKey:["queue",n,i],queryFn:()=>he.getQueue(n||void 0,i||void 0),refetchInterval:5e3}),{data:W}=ut({queryKey:["printers"],queryFn:()=>he.getPrinters()}),K=mt({mutationFn:ye=>he.cancelQueueItem(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item cancelled")},onError:()=>e("Failed to cancel item","error")}),V=mt({mutationFn:ye=>he.removeFromQueue(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Queue item removed")},onError:()=>e("Failed to remove item","error")}),Q=mt({mutationFn:ye=>he.stopQueueItem(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Print stopped")},onError:()=>e("Failed to stop print","error")}),re=mt({mutationFn:ye=>he.startQueueItem(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]}),e("Print released to queue")},onError:()=>e("Failed to start print","error")}),J=mt({mutationFn:ye=>he.reorderQueue(ye),onSuccess:()=>{t.invalidateQueries({queryKey:["queue"]})},onError:()=>e("Failed to reorder queue","error")}),z=mt({mutationFn:async()=>{const ye=q?.filter(le=>["completed","failed","skipped","cancelled"].includes(le.status))||[];for(const le of ye)await he.removeFromQueue(le.id);return ye.length},onSuccess:ye=>{t.invalidateQueries({queryKey:["queue"]}),e(`Cleared ${ye} history item${ye!==1?"s":""}`)},onError:()=>e("Failed to clear history","error")}),G=mt({mutationFn:ye=>he.bulkUpdateQueue(ye),onSuccess:ye=>{t.invalidateQueries({queryKey:["queue"]}),y([]),v(!1),e(ye.message)},onError:()=>e("Failed to update items","error")}),ne=mt({mutationFn:async ye=>{for(const le of ye)await he.cancelQueueItem(le);return ye.length},onSuccess:ye=>{t.invalidateQueries({queryKey:["queue"]}),y([]),e(`Cancelled ${ye} item${ye!==1?"s":""}`)},onError:()=>e("Failed to cancel items","error")}),ae=ye=>{y(le=>le.includes(ye)?le.filter($e=>$e!==ye):[...le,ye])},de=w.useMemo(()=>{const ye=q?.filter($e=>$e.status==="pending")||[],le=$e=>{if(!$e.scheduled_time)return 0;const H=new Date($e.scheduled_time).getTime(),ee=Date.now()+4320*60*60*1e3;return H>ee?0:H};return[...ye].sort(($e,H)=>{let ee;if(k==="name"){const X=$e.archive_name||$e.library_file_name||"",B=H.archive_name||H.library_file_name||"";ee=X.localeCompare(B)}else k==="printer"?ee=($e.printer_name||"").localeCompare(H.printer_name||""):k==="time"?ee=le($e)-le(H):ee=$e.position-H.position;return O?ee:-ee})},[q,k,O]),fe=()=>{const ye=de.map(le=>le.id);b.length===ye.length?y([]):y(ye)},ie=q?.filter(ye=>ye.status==="printing")||[],Ae=w.useMemo(()=>[...q?.filter(le=>["completed","failed","skipped","cancelled"].includes(le.status))||[]].sort((le,$e)=>{let H;if(S==="name"){const ee=le.archive_name||le.library_file_name||"",X=$e.archive_name||$e.library_file_name||"";H=ee.localeCompare(X)}else S==="printer"?H=(le.printer_name||"").localeCompare($e.printer_name||""):H=new Date($e.completed_at||$e.created_at).getTime()-new Date(le.completed_at||le.created_at).getTime();return A?-H:H}),[q,S,A]),me=w.useMemo(()=>de.reduce((ye,le)=>ye+(le.print_time_seconds||0),0),[de]),Ne=ye=>{const{active:le,over:$e}=ye;if(!$e||le.id===$e.id)return;const H=de.findIndex(X=>X.id===le.id),ee=de.findIndex(X=>X.id===$e.id);if(H!==-1&&ee!==-1){const B=$E(de,H,ee).map((I,ce)=>({id:I.id,position:ce+1}));J.mutate(B)}};return s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsx("div",{className:"flex items-center justify-between mb-8",children:s.jsxs("div",{children:[s.jsxs("h1",{className:"text-2xl font-bold text-white flex items-center gap-3",children:[s.jsx(vK,{className:"w-7 h-7 text-bambu-green"}),"Print Queue"]}),s.jsx("p",{className:"text-bambu-gray mt-1",children:"Schedule and manage your print jobs"})]})}),s.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4 mb-8",children:[s.jsx(St,{className:"bg-gradient-to-br from-blue-500/10 to-transparent border-blue-500/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-blue-500/20 flex items-center justify-center",children:s.jsx(Oo,{className:"w-5 h-5 text-blue-400"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:ie.length}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Printing"})]})]})})}),s.jsx(St,{className:"bg-gradient-to-br from-yellow-500/10 to-transparent border-yellow-500/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-yellow-500/20 flex items-center justify-center",children:s.jsx(Zn,{className:"w-5 h-5 text-yellow-400"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:de.length}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Queued"})]})]})})}),s.jsx(St,{className:"bg-gradient-to-br from-bambu-green/10 to-transparent border-bambu-green/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-bambu-green/20 flex items-center justify-center",children:s.jsx(jd,{className:"w-5 h-5 text-bambu-green"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:Vie(me)}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Total Queue Time"})]})]})})}),s.jsx(St,{className:"bg-gradient-to-br from-gray-500/10 to-transparent border-gray-500/20",children:s.jsx(Nt,{className:"p-4",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-10 h-10 rounded-lg bg-gray-500/20 flex items-center justify-center",children:s.jsx(Un,{className:"w-5 h-5 text-gray-400"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-2xl font-bold text-white",children:Ae.length}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"History"})]})]})})})]}),s.jsxs("div",{className:"flex items-center gap-4 mb-6",children:[s.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===-1?"unassigned":n||"",onChange:ye=>{const le=ye.target.value;r(le==="unassigned"?-1:le===""?null:Number(le))},children:[s.jsx("option",{value:"",children:"All Printers"}),s.jsx("option",{value:"unassigned",children:"Unassigned"}),W?.map(ye=>s.jsx("option",{value:ye.id,children:ye.name},ye.id))]}),s.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:ye=>a(ye.target.value),children:[s.jsx("option",{value:"",children:"All Status"}),s.jsx("option",{value:"pending",children:"Pending"}),s.jsx("option",{value:"printing",children:"Printing"}),s.jsx("option",{value:"completed",children:"Completed"}),s.jsx("option",{value:"failed",children:"Failed"}),s.jsx("option",{value:"skipped",children:"Skipped"}),s.jsx("option",{value:"cancelled",children:"Cancelled"})]}),s.jsx("div",{className:"flex-1"}),Ae.length>0&&s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>l(!0),children:[s.jsx(dn,{className:"w-4 h-4"}),"Clear History"]})]}),P?s.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading..."}):q?.length===0?s.jsxs(St,{className:"p-12 text-center border-dashed",children:[s.jsx(oi,{className:"w-16 h-16 text-bambu-gray mx-auto mb-4 opacity-50"}),s.jsx("h3",{className:"text-xl font-medium text-white mb-2",children:"No prints scheduled"}),s.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.'})]}):s.jsxs("div",{className:"space-y-8",children:[ie.length>0&&s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white mb-4 flex items-center gap-2",children:[s.jsx("div",{className:"w-2 h-2 rounded-full bg-blue-400 animate-pulse"}),"Currently Printing"]}),s.jsx("div",{className:"space-y-3",children:ie.map(ye=>s.jsx(qj,{item:ye,onEdit:()=>{},onCancel:()=>{},onRemove:()=>{},onStop:()=>p({type:"stop",item:ye}),onRequeue:()=>{},onStart:()=>{},timeFormat:L},ye.id))})]}),de.length>0&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Zn,{className:"w-5 h-5 text-yellow-400"}),"Queued",s.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",de.length," item",de.length!==1?"s":"",")"]}),s.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)"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.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:k,onChange:ye=>E(ye.target.value),children:[s.jsx("option",{value:"position",children:"Sort by Position"}),s.jsx("option",{value:"name",children:"Sort by Name"}),s.jsx("option",{value:"printer",children:"Sort by Printer"}),s.jsx("option",{value:"time",children:"Sort by Schedule"})]}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:()=>C(!O),title:O?"Ascending":"Descending",className:"px-2",children:O?s.jsx(bg,{className:"w-4 h-4"}):s.jsx(j0,{className:"w-4 h-4"})})]})]}),s.jsxs("div",{className:"flex items-center gap-3 mb-4 p-3 bg-bambu-dark rounded-lg",children:[s.jsxs(Se,{variant:"ghost",size:"sm",onClick:fe,className:"flex items-center gap-2",children:[b.length===de.length&&de.length>0?s.jsx(Lo,{className:"w-4 h-4 text-bambu-green"}):s.jsx(Io,{className:"w-4 h-4"}),b.length===de.length&&de.length>0?"Deselect All":"Select All"]}),b.length>0&&s.jsxs(s.Fragment,{children:[s.jsxs("span",{className:"text-sm text-bambu-gray",children:[b.length," selected"]}),s.jsx("div",{className:"h-4 w-px bg-bambu-dark-tertiary"}),s.jsxs(Se,{variant:"ghost",size:"sm",onClick:()=>v(!0),className:"flex items-center gap-2 text-bambu-green hover:text-bambu-green-light",children:[s.jsx(la,{className:"w-4 h-4"}),"Edit Selected"]}),s.jsxs(Se,{variant:"ghost",size:"sm",onClick:()=>ne.mutate(b),className:"flex items-center gap-2 text-red-400 hover:text-red-300",disabled:ne.isPending,children:[s.jsx(Vt,{className:"w-4 h-4"}),"Cancel Selected"]})]})]}),s.jsx(Lie,{sensors:D,collisionDetection:Sie,onDragEnd:Ne,children:s.jsx(Uie,{items:de.map(ye=>ye.id),strategy:eYe,children:s.jsx("div",{className:"space-y-3",children:de.map((ye,le)=>s.jsx(qj,{item:ye,position:le+1,onEdit:()=>u(ye),onCancel:()=>p({type:"cancel",item:ye}),onRemove:()=>{},onStop:()=>{},onRequeue:()=>{},onStart:()=>re.mutate(ye.id),timeFormat:L,isSelected:b.includes(ye.id),onToggleSelect:()=>ae(ye.id)},ye.id))})})})]}),Ae.length>0&&s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Un,{className:"w-5 h-5 text-bambu-gray"}),"History",s.jsxs("span",{className:"text-sm font-normal text-bambu-gray",children:["(",Ae.length," item",Ae.length!==1?"s":"",")"]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.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:S,onChange:ye=>M(ye.target.value),children:[s.jsx("option",{value:"date",children:"Sort by Date"}),s.jsx("option",{value:"name",children:"Sort by Name"}),s.jsx("option",{value:"printer",children:"Sort by Printer"})]}),s.jsx(Se,{variant:"ghost",size:"sm",onClick:()=>T(!A),title:A?"Ascending (oldest first)":"Descending (newest first)",className:"px-2",children:A?s.jsx(bg,{className:"w-4 h-4"}):s.jsx(j0,{className:"w-4 h-4"})})]})]}),s.jsx("div",{className:"space-y-3",children:Ae.slice(0,20).map((ye,le)=>s.jsx(qj,{item:ye,position:le+1,onEdit:()=>{},onCancel:()=>{},onRemove:()=>p({type:"remove",item:ye}),onStop:()=>{},onRequeue:()=>f(ye),onStart:()=>{},timeFormat:L},ye.id))})]})]}),c&&s.jsx(ch,{mode:"edit-queue-item",archiveId:c.archive_id??void 0,libraryFileId:c.library_file_id??void 0,archiveName:c.archive_name||c.library_file_name||`File #${c.archive_id||c.library_file_id}`,queueItem:c,onClose:()=>u(null)}),d&&s.jsx(ch,{mode:"add-to-queue",archiveId:d.archive_id??void 0,libraryFileId:d.library_file_id??void 0,archiveName:d.archive_name||d.library_file_name||`File #${d.archive_id||d.library_file_id}`,onClose:()=>f(null)}),m&&s.jsx(Hn,{title:m.type==="cancel"?"Cancel Scheduled Print":m.type==="stop"?"Stop Print":"Remove from History",message:m.type==="cancel"?`Are you sure you want to cancel "${m.item.archive_name||m.item.library_file_name||"this print"}"?`:m.type==="stop"?`Are you sure you want to stop the current print "${m.item.archive_name||m.item.library_file_name||"this print"}"? This will cancel the print job on the printer.`:`Are you sure you want to remove "${m.item.archive_name||m.item.library_file_name||"this item"}" from the queue history?`,confirmText:m.type==="cancel"?"Cancel Print":m.type==="stop"?"Stop Print":"Remove",variant:"danger",onConfirm:()=>{m.type==="cancel"?K.mutate(m.item.id):m.type==="stop"?Q.mutate(m.item.id):V.mutate(m.item.id),p(null)},onCancel:()=>p(null)}),o&&s.jsx(Hn,{title:"Clear History",message:`Are you sure you want to remove all ${Ae.length} item${Ae.length!==1?"s":""} from the history?`,confirmText:"Clear History",variant:"danger",onConfirm:()=>{z.mutate(),l(!1)},onCancel:()=>l(!1)}),x&&s.jsx(fYe,{selectedCount:b.length,printers:W?.map(ye=>({id:ye.id,name:ye.name}))||[],onSave:ye=>{Object.keys(ye).length>0&&G.mutate({item_ids:b,...ye})},onClose:()=>v(!1),isSaving:G.isPending})]})}function pYe({printDates:t,months:e=3}){const n=w.useRef(null),[r,i]=w.useState(0);w.useEffect(()=>{const S=n.current;if(!S)return;const M=new ResizeObserver(A=>{const T=A[0]?.contentRect.width||0;i(T)});return M.observe(S),()=>M.disconnect()},[]);const{weeks:a,monthLabels:o,printCounts:l}=w.useMemo(()=>{const S={};t.forEach(U=>{const L=U.split("T")[0];S[L]=(S[L]||0)+1});const M=new Date,A=new Date(M);A.setMonth(A.getMonth()-e),A.setDate(A.getDate()-A.getDay());const T=[],k=[];let E=[],O=-1;const C=new Date(A);let D=0;for(;C<=M;)C.getDay()===0&&E.length>0&&(T.push(E),E=[],D++),C.getMonth()!==O&&(k.push({month:C.toLocaleDateString("en-US",{month:"short"}),weekIndex:D}),O=C.getMonth()),E.push(new Date(C)),C.setDate(C.getDate()+1);return E.length>0&&T.push(E),{weeks:T,monthLabels:k,printCounts:S}},[t,e]),c=Math.max(1,...Object.values(l)),u=S=>{if(S===0)return"bg-bambu-dark";const M=S/c;return M<=.25?"bg-bambu-green/30":M<=.5?"bg-bambu-green/50":M<=.75?"bg-bambu-green/75":"bg-bambu-green"},d=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],f=a.length,m=32,p=2,b=r-m-16,y=f>0?Math.floor((b-(f-1)*p)/f):12,x=Math.max(8,Math.min(20,y)),v=x<=10?10:12;return s.jsx("div",{ref:n,className:"w-full flex justify-center",children:r>0&&s.jsxs("div",{children:[s.jsx("div",{className:"flex mb-1",style:{marginLeft:m+4},children:o.map(({month:S,weekIndex:M},A)=>s.jsx("div",{className:"text-bambu-gray",style:{fontSize:v,marginLeft:A===0?0:`${(M-(o[A-1]?.weekIndex||0))*(x+p)-24}px`},children:S},A))}),s.jsxs("div",{className:"flex",style:{gap:p},children:[s.jsx("div",{className:"flex flex-col",style:{gap:p,marginRight:4,width:m},children:d.map((S,M)=>s.jsx("div",{className:"text-bambu-gray flex items-center",style:{width:m,height:x,fontSize:v,visibility:M%2===1?"visible":"hidden"},children:S},S))}),a.map((S,M)=>s.jsx("div",{className:"flex flex-col",style:{gap:p},children:[0,1,2,3,4,5,6].map(A=>{const T=S.find(C=>C.getDay()===A);if(!T)return s.jsx("div",{style:{width:x,height:x}},A);const k=T.toISOString().split("T")[0],E=l[k]||0,O=k===new Date().toISOString().split("T")[0];return s.jsx("div",{className:`rounded-sm ${u(E)} ${O?"ring-1 ring-white":""}`,style:{width:x,height:x},title:`${T.toLocaleDateString()}: ${E} print${E!==1?"s":""}`},A)})},M))]}),s.jsxs("div",{className:"flex items-center gap-2 mt-3 text-bambu-gray",style:{fontSize:v},children:[s.jsx("span",{children:"Less"}),s.jsxs("div",{className:"flex",style:{gap:p},children:[s.jsx("div",{className:"rounded-sm bg-bambu-dark",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green/30",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green/50",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green/75",style:{width:x,height:x}}),s.jsx("div",{className:"rounded-sm bg-bambu-green",style:{width:x,height:x}})]}),s.jsx("span",{children:"More"})]})]})})}const Qw=["#00ae42","#3b82f6","#f59e0b","#ef4444","#8b5cf6","#ec4899","#14b8a6","#f97316"];function gYe(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 xYe({archives:t,currency:e="$"}){const[n,r]=w.useState("30d"),i=w.useMemo(()=>{const m=gYe(n);return t.filter(p=>(Ni(p.completed_at||p.created_at)||new Date(0))>=m)},[t,n]),a=w.useMemo(()=>{const m=new Map;return i.forEach(p=>{const b=Ni(p.completed_at||p.created_at)||new Date,y=`${b.getFullYear()}-${String(b.getMonth()+1).padStart(2,"0")}-${String(b.getDate()).padStart(2,"0")}`,x=m.get(y)||{date:y,filament:0,cost:0,prints:0};x.filament+=p.filament_used_grams||0,x.cost+=p.cost||0,x.prints+=1,m.set(y,x)}),Array.from(m.values()).sort((p,b)=>p.date.localeCompare(b.date)).map(p=>({...p,dateLabel:new Date(p.date).toLocaleDateString("en-US",{month:"short",day:"numeric"})}))},[i]),o=w.useMemo(()=>{if(n==="7d"||n==="30d")return a;const m=new Map;return i.forEach(p=>{const b=Ni(p.completed_at||p.created_at)||new Date,y=new Date(b);y.setDate(b.getDate()-b.getDay());const x=`${y.getFullYear()}-${String(y.getMonth()+1).padStart(2,"0")}-${String(y.getDate()).padStart(2,"0")}`,v=m.get(x)||{week:x,filament:0,cost:0,prints:0};v.filament+=p.filament_used_grams||0,v.cost+=p.cost||0,v.prints+=1,m.set(x,v)}),Array.from(m.values()).sort((p,b)=>p.week.localeCompare(b.week)).map(p=>({date:p.week,dateLabel:`Week of ${new Date(p.week).toLocaleDateString("en-US",{month:"short",day:"numeric"})}`,...p}))},[i,a,n]),l=w.useMemo(()=>{const m=new Map;return i.forEach(p=>{const y=(p.filament_type||"Unknown").split(", ");y.forEach(x=>{const v=(p.filament_used_grams||0)/y.length;m.set(x,(m.get(x)||0)+v)})}),Array.from(m.entries()).map(([p,b])=>({name:p,value:Math.round(b)})).sort((p,b)=>b.value-p.value)},[i]),c=w.useMemo(()=>{const m=new Date,p=[];for(let b=5;b>=0;b--){const y=new Date(m.getFullYear(),m.getMonth()-b,1),x=new Date(m.getFullYear(),m.getMonth()-b+1,0),v=y.toLocaleDateString("en-US",{month:"short",year:"2-digit"}),S=t.filter(M=>{const A=Ni(M.completed_at||M.created_at)||new Date(0);return A>=y&&A<=x});p.push({month:v,filament:Math.round(S.reduce((M,A)=>M+(A.filament_used_grams||0),0)),cost:S.reduce((M,A)=>M+(A.cost||0),0),prints:S.length})}return p},[t]),u=n==="7d"||n==="30d"?a:o,d=i.reduce((m,p)=>m+(p.filament_used_grams||0),0),f=i.reduce((m,p)=>m+(p.cost||0),0);return s.jsxs("div",{className:"space-y-6",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Filament Usage Trends"}),s.jsx("div",{className:"flex gap-1 bg-bambu-dark rounded-lg p-1",children:["7d","30d","90d","365d","all"].map(m=>s.jsx("button",{onClick:()=>r(m),className:`px-3 py-1 text-sm rounded-md transition-colors ${n===m?"bg-bambu-green text-white":"text-bambu-gray hover:text-white"}`,children:m==="all"?"All":m.replace("d","D")},m))})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Filament"}),s.jsxs("p",{className:"text-2xl font-bold text-white",children:[(d/1e3).toFixed(2),"kg"]}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[d.toFixed(0),"g total"]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Period Cost"}),s.jsxs("p",{className:"text-2xl font-bold text-white",children:[e,f.toFixed(2)]}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[i.length," prints"]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Avg per Print"}),s.jsxs("p",{className:"text-2xl font-bold text-white",children:[i.length>0?(d/i.length).toFixed(0):0,"g"]}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[e,i.length>0?(f/i.length).toFixed(2):"0.00"," avg"]})]})]}),u.length>0?s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Usage Over Time"}),s.jsx(l1,{width:"100%",height:250,children:s.jsxs(NPe,{data:u,children:[s.jsx("defs",{children:s.jsxs("linearGradient",{id:"colorFilament",x1:"0",y1:"0",x2:"0",y2:"1",children:[s.jsx("stop",{offset:"5%",stopColor:"#00ae42",stopOpacity:.3}),s.jsx("stop",{offset:"95%",stopColor:"#00ae42",stopOpacity:0})]})}),s.jsx(OS,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),s.jsx(US,{dataKey:"dateLabel",stroke:"#9ca3af",tick:{fontSize:12},interval:"preserveStartEnd"}),s.jsx(zS,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:m=>`${m}g`}),s.jsx(u1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},labelStyle:{color:"#fff"},formatter:m=>[`${Number(m??0).toFixed(0)}g`,"Filament"]}),s.jsx(CJ,{type:"monotone",dataKey:"filament",stroke:"#00ae42",strokeWidth:2,fillOpacity:1,fill:"url(#colorFilament)"})]})})]}):s.jsx("div",{className:"bg-bambu-dark rounded-lg p-8 text-center text-bambu-gray",children:"No data for selected time range"}),s.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"By Filament Type"}),l.length>0?s.jsxs("div",{className:"flex items-center gap-4",children:[s.jsx(l1,{width:160,height:160,children:s.jsxs(SPe,{children:[s.jsx(tJ,{data:l,cx:"50%",cy:"50%",innerRadius:40,outerRadius:70,paddingAngle:2,dataKey:"value",children:l.map((m,p)=>s.jsx(qy,{fill:Qw[p%Qw.length]},`cell-${p}`))}),s.jsx(u1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:m=>[`${m??0}g`,"Usage"]})]})}),s.jsx("div",{className:"flex-1 space-y-2 overflow-hidden",children:l.map((m,p)=>{const b=l.reduce((x,v)=>x+v.value,0),y=b>0?(m.value/b*100).toFixed(0):0;return s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx("div",{className:"w-3 h-3 rounded-sm flex-shrink-0",style:{backgroundColor:Qw[p%Qw.length]}}),s.jsx("span",{className:"text-white truncate flex-1",children:m.name}),s.jsxs("span",{className:"text-bambu-gray flex-shrink-0",children:[y,"%"]})]},m.name)})})]}):s.jsx("div",{className:"h-[160px] flex items-center justify-center text-bambu-gray",children:"No filament data"})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-4",children:[s.jsx("h4",{className:"text-sm font-medium text-bambu-gray mb-4",children:"Monthly Comparison"}),s.jsx(l1,{width:"100%",height:200,children:s.jsxs(dPe,{data:c,children:[s.jsx(OS,{strokeDasharray:"3 3",stroke:"#3d3d3d"}),s.jsx(US,{dataKey:"month",stroke:"#9ca3af",tick:{fontSize:12}}),s.jsx(zS,{stroke:"#9ca3af",tick:{fontSize:12},tickFormatter:m=>`${m}g`}),s.jsx(u1,{contentStyle:{backgroundColor:"#2d2d2d",border:"1px solid #3d3d3d",borderRadius:"8px"},formatter:(m,p)=>[p==="filament"?`${m??0}g`:p==="cost"?`${e}${Number(m??0).toFixed(2)}`:m??0,p==="filament"?"Filament":p==="cost"?"Cost":"Prints"]}),s.jsx(SO,{}),s.jsx(DJ,{dataKey:"filament",name:"Filament (g)",fill:"#00ae42",radius:[4,4,0,0]})]})})]})]})]})}function bYe({id:t,title:e,component:n,isHidden:r,size:i,onToggleVisibility:a,onToggleSize:o}){const{attributes:l,listeners:c,setNodeRef:u,transform:d,transition:f,isDragging:m}=Bie({id:t}),p={transform:qg.Transform.toString(d),transition:f,opacity:m?.5:1};return r?null:s.jsxs("div",{ref:u,style:{...p,gridColumn:`span ${i}`},className:`bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary overflow-hidden ${m?"ring-2 ring-bambu-green shadow-lg":""}`,children:[s.jsxs("div",{className:"flex items-center justify-between px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/30",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("button",{...l,...c,className:"cursor-grab active:cursor-grabbing p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Drag to reorder",children:s.jsx(yg,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray"})}),s.jsx("h3",{className:"text-sm font-medium text-white",children:e})]}),s.jsxs("div",{className:"flex items-center gap-1",children:[s.jsx("button",{onClick:o,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?s.jsx(wK,{className:"w-4 h-4 text-bambu-gray hover:text-white"}):s.jsx(_K,{className:"w-4 h-4 text-bambu-gray hover:text-white"})}),s.jsx("button",{onClick:a,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",title:"Hide widget",children:s.jsx(My,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]})]}),s.jsx("div",{className:"p-4",children:typeof n=="function"?n(i):n})]})}function yYe({widgets:t,storageKey:e,columns:n=4,hideControls:r=!1,onResetLayout:i,renderControls:a}){const o=()=>{const A={};return t.forEach(T=>{A[T.id]=T.defaultSize||4}),A},[l,c]=w.useState(()=>{const A=localStorage.getItem(e);if(A)try{const T=JSON.parse(A);return T.sizes||(T.sizes=o()),T}catch{}return{order:t.map(T=>T.id),hidden:t.filter(T=>T.defaultVisible===!1).map(T=>T.id),sizes:o()}}),[u,d]=w.useState(!1);w.useEffect(()=>{const A=()=>d(T=>!T);return window.addEventListener("toggle-hidden-panel",A),()=>window.removeEventListener("toggle-hidden-panel",A)},[]),w.useEffect(()=>{localStorage.setItem(e,JSON.stringify(l))},[l,e]),w.useEffect(()=>{const T=t.map(k=>k.id).filter(k=>!l.order.includes(k));T.length>0&&c(k=>({...k,order:[...k.order,...T]}))},[t,l.order]);const f=yie(YM(UE,{activationConstraint:{distance:8}}),YM(FE,{coordinateGetter:$ie})),m=A=>{const{active:T,over:k}=A;k&&T.id!==k.id&&c(E=>{const O=E.order.indexOf(T.id),C=E.order.indexOf(k.id);return{...E,order:$E(E.order,O,C)}})},p=A=>{c(T=>({...T,hidden:T.hidden.includes(A)?T.hidden.filter(k=>k!==A):[...T.hidden,A]}))},b=A=>{c(T=>{const k=T.sizes[A]||4,E=k===1?2:k===2?4:1;return{...T,sizes:{...T.sizes,[A]:E}}})},y=()=>{const A={order:t.map(T=>T.id),hidden:t.filter(T=>T.defaultVisible===!1).map(T=>T.id),sizes:o()};c(A),i?.()},x=l.order.map(A=>t.find(T=>T.id===A)).filter(Boolean),v=x.filter(A=>!l.hidden.includes(A.id)),S=x.filter(A=>l.hidden.includes(A.id)),M=a?.({hiddenCount:S.length,showHiddenPanel:u,setShowHiddenPanel:d,resetLayout:y});return s.jsxs("div",{className:"space-y-4",children:[M,!r&&!a&&s.jsxs("div",{className:"flex items-center justify-end gap-2",children:[s.jsxs(Se,{variant:"secondary",size:"sm",onClick:y,children:[s.jsx(mm,{className:"w-4 h-4"}),"Reset Layout"]}),S.length>0&&s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>d(!u),children:[s.jsx(Qd,{className:"w-4 h-4"}),S.length," Hidden"]})]}),u&&S.length>0&&s.jsxs("div",{className:"p-4 bg-bambu-dark rounded-xl border border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-3",children:"Hidden widgets (click to show):"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:S.map(A=>s.jsxs("button",{onClick:()=>p(A.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:[s.jsx(Qd,{className:"w-3 h-3"}),A.title]},A.id))})]}),s.jsx(Lie,{sensors:f,collisionDetection:Sie,onDragEnd:m,children:s.jsx(Uie,{items:v.map(A=>A.id),strategy:vI,children:s.jsx("div",{className:"grid gap-6",style:{gridTemplateColumns:`repeat(${n}, minmax(0, 1fr))`},children:v.map(A=>s.jsx(bYe,{id:A.id,title:A.title,component:A.component,isHidden:l.hidden.includes(A.id),size:l.sizes[A.id]||2,onToggleVisibility:()=>p(A.id),onToggleSize:()=>b(A.id)},A.id))})})}),v.length===0&&s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx("p",{children:"All widgets are hidden."}),s.jsx(Se,{className:"mt-4",onClick:y,children:"Reset Layout"})]})]})}function vYe({stats:t,currency:e}){return s.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-3 gap-4",children:[s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-bambu-green",children:s.jsx(Jl,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Total Prints"}),s.jsx("p",{className:"text-xl font-bold text-white",children:t?.total_prints||0})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-blue-400",children:s.jsx(Zn,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Print Time"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_print_time_hours.toFixed(1)||0,"h"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-orange-400",children:s.jsx(Jl,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Used"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[((t?.total_filament_grams||0)/1e3).toFixed(2),"kg"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-green-400",children:s.jsx(xP,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament Cost"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_cost.toFixed(2)||"0.00"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-400",children:s.jsx(qf,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Used"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[t?.total_energy_kwh.toFixed(2)||"0.00"," kWh"]})]})]}),s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark text-yellow-500",children:s.jsx(xP,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Energy Cost"}),s.jsxs("p",{className:"text-xl font-bold text-white",children:[e," ",t?.total_energy_cost.toFixed(2)||"0.00"]})]})]})]})}function _Ye({stats:t,printerMap:e,size:n=1}){const r=t?.total_prints?Math.round(t.successful_prints/t.total_prints*100):0,i=n===1?112:n===2?128:144,a=i/2-8,o=a*2*Math.PI;return s.jsxs("div",{className:"flex items-center gap-6",children:[s.jsxs("div",{className:"relative flex-shrink-0",style:{width:i,height:i},children:[s.jsxs("svg",{className:"w-full h-full -rotate-90",children:[s.jsx("circle",{cx:i/2,cy:i/2,r:a,fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),s.jsx("circle",{cx:i/2,cy:i/2,r:a,fill:"none",stroke:"#00ae42",strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${r/100*o} ${o}`})]}),s.jsx("div",{className:"absolute inset-0 flex items-center justify-center",children:s.jsxs("span",{className:`font-bold text-white ${n>=2?"text-2xl":"text-xl"}`,children:[r,"%"]})})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-status-ok flex-shrink-0"}),s.jsx("span",{className:"text-sm text-bambu-gray",children:"Successful:"}),s.jsx("span",{className:"text-sm text-white font-medium",children:t?.successful_prints||0})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(ia,{className:"w-4 h-4 text-status-error flex-shrink-0"}),s.jsx("span",{className:"text-sm text-bambu-gray",children:"Failed:"}),s.jsx("span",{className:"text-sm text-white font-medium",children:t?.failed_prints||0})]})]}),n>=2&&t?.prints_by_printer&&Object.keys(t.prints_by_printer).length>0&&s.jsxs("div",{className:"mt-4 pt-4 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-xs text-bambu-gray font-medium mb-2",children:"Prints by Printer"}),s.jsx("div",{className:`grid gap-x-6 gap-y-1 ${n===4?"grid-cols-3":"grid-cols-2"}`,style:{width:"fit-content"},children:Object.entries(t.prints_by_printer).map(([l,c])=>s.jsxs("div",{className:"flex items-center gap-3 text-sm",children:[s.jsx("span",{className:"text-bambu-gray truncate max-w-[120px]",children:e.get(l)||`Printer ${l}`}),s.jsx("span",{className:"text-white font-medium",children:c})]},l))})]})]})]})}function wYe({stats:t,printerMap:e,size:n=1}){const r=t?.average_time_accuracy;if(r==null)return s.jsx("div",{className:"flex items-center justify-center h-full",children:s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No time accuracy data yet"})});const a=(Math.min(150,Math.max(50,r))-50)/100*100,l=(b=>b>=95&&b<=105?"#00ae42":b>105?"#3b82f6":"#f97316")(r),c=r-100,u=n===1?112:n===2?128:144,d=u/2-8,f=d*2*Math.PI,m=n===1?3:n===2?6:999,p=t?.time_accuracy_by_printer?Object.entries(t.time_accuracy_by_printer).slice(0,m):[];return s.jsxs("div",{className:"flex items-center gap-6",children:[s.jsxs("div",{className:"relative flex-shrink-0",style:{width:u,height:u},children:[s.jsxs("svg",{className:"w-full h-full -rotate-90",children:[s.jsx("circle",{cx:u/2,cy:u/2,r:d,fill:"none",stroke:"#3d3d3d",strokeWidth:"10"}),s.jsx("circle",{cx:u/2,cy:u/2,r:d,fill:"none",stroke:l,strokeWidth:"10",strokeLinecap:"round",strokeDasharray:`${a/100*f} ${f}`})]}),s.jsxs("div",{className:"absolute inset-0 flex flex-col items-center justify-center",children:[s.jsxs("span",{className:`font-bold text-white ${n>=2?"text-2xl":"text-xl"}`,children:[r.toFixed(0),"%"]}),s.jsxs("span",{className:`text-xs ${c>=0?"text-blue-400":"text-orange-400"}`,children:[c>=0?"+":"",c.toFixed(0),"%"]})]})]}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2 text-xs text-bambu-gray",children:[s.jsx(AK,{className:"w-3 h-3 flex-shrink-0"}),s.jsx("span",{children:"100% = perfect estimate"})]}),p.length>0&&s.jsx("div",{className:`mt-2 ${n===4?"grid grid-cols-3 gap-x-6 gap-y-1":n===2?"grid grid-cols-2 gap-x-6 gap-y-1":"space-y-1"}`,style:{width:"fit-content"},children:p.map(([b,y])=>s.jsxs("div",{className:"flex items-center gap-2 text-xs",children:[s.jsx("span",{className:"text-bambu-gray truncate max-w-[100px]",children:e.get(b)||`Printer ${b}`}),s.jsxs("span",{className:`font-medium ${y>=95&&y<=105?"text-status-ok":y>105?"text-blue-400":"text-status-warning"}`,children:[y.toFixed(0),"%"]})]},b))})]})]})}function SYe({stats:t,size:e=1}){if(!t?.prints_by_filament_type||Object.keys(t.prints_by_filament_type).length===0)return s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No filament data available"});const n=Object.entries(t.prints_by_filament_type).sort(([,o],[,l])=>l-o),r=e===1?5:e===2?8:999,i=n.slice(0,r),a=n.length>r;return e===4&&i.length>4?s.jsx("div",{className:"grid grid-cols-2 lg:grid-cols-3 gap-4",children:i.map(([o,l])=>{const c=Math.round(l/(t.total_prints||1)*100);return s.jsxs("div",{children:[s.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[s.jsx("span",{className:"text-white truncate max-w-[120px]",children:o}),s.jsx("span",{className:"text-bambu-gray",children:l})]}),s.jsx("div",{className:"h-2 bg-bambu-dark rounded-full",children:s.jsx("div",{className:"h-full bg-bambu-green rounded-full transition-all",style:{width:`${c}%`}})})]},o)})}):s.jsxs("div",{className:"space-y-3",children:[i.map(([o,l])=>{const c=Math.round(l/(t.total_prints||1)*100);return s.jsxs("div",{children:[s.jsxs("div",{className:"flex justify-between text-sm mb-1",children:[s.jsx("span",{className:"text-white",children:o}),s.jsxs("span",{className:"text-bambu-gray",children:[l," prints"]})]}),s.jsx("div",{className:"h-2 bg-bambu-dark rounded-full",children:s.jsx("div",{className:"h-full bg-bambu-green rounded-full transition-all",style:{width:`${c}%`}})})]},o)}),a&&s.jsxs("p",{className:"text-xs text-bambu-gray text-center pt-1",children:["+",n.length-r," more types"]})]})}function MYe({printDates:t,size:e=2}){const n=e===1?3:e===2?6:12;return s.jsx(pYe,{printDates:t,months:n})}function NYe({stats:t,printerMap:e}){return!t?.prints_by_printer||Object.keys(t.prints_by_printer).length===0?s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No printer data available"}):s.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-3",children:Object.entries(t.prints_by_printer).map(([n,r])=>s.jsxs("div",{className:"flex items-center gap-3 p-3 bg-bambu-dark rounded-lg",children:[s.jsx("div",{className:"p-2 bg-bambu-dark-tertiary rounded-lg",children:s.jsx(qr,{className:"w-4 h-4 text-bambu-green"})}),s.jsxs("div",{children:[s.jsx("p",{className:"text-white font-medium text-sm",children:e.get(n)||`Printer ${n}`}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[r," prints"]})]})]},n))})}function EYe({archives:t,currency:e}){return!t||t.length===0?s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data available"}):s.jsx(xYe,{archives:t,currency:e})}function AYe({size:t=1}){const{data:e,isLoading:n}=ut({queryKey:["failureAnalysis"],queryFn:()=>he.getFailureAnalysis({days:30})});if(n)return s.jsx("div",{className:"flex justify-center py-4",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})});if(!e||e.total_prints===0)return s.jsx("p",{className:"text-bambu-gray text-center py-4",children:"No print data in the last 30 days"});const r=t===1?5:t===2?8:999,i=Object.entries(e.failures_by_reason).sort(([,l],[,c])=>c-l),a=i.slice(0,r),o=i.length>r;return s.jsxs("div",{className:`${t>=2?"flex gap-8":"space-y-4"}`,children:[s.jsxs("div",{className:t>=2?"flex-shrink-0":"",children:[s.jsx("div",{className:"flex items-center gap-4",children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(kn,{className:`w-5 h-5 ${e.failure_rate>20?"text-status-error":e.failure_rate>10?"text-status-warning":"text-status-ok"}`}),s.jsxs("span",{className:`font-bold text-white ${t>=2?"text-3xl":"text-2xl"}`,children:[e.failure_rate.toFixed(1),"%"]})]})}),s.jsxs("div",{className:"text-sm text-bambu-gray mt-1",children:[e.failed_prints," / ",e.total_prints," prints failed"]}),e.trend&&e.trend.length>=2&&s.jsx("div",{className:`${t>=2?"mt-4":"mt-2 pt-2 border-t border-bambu-dark-tertiary"}`,children:s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx(CK,{className:`w-4 h-4 ${e.trend[e.trend.length-1].failure_rate0&&s.jsxs("div",{className:`flex-1 ${t>=2?"border-l border-bambu-dark-tertiary pl-8":"pt-2"}`,children:[s.jsx("p",{className:"text-xs text-bambu-gray font-medium mb-2",children:t>=2?"Failure Reasons":"Top Failure Reasons"}),s.jsx("div",{className:`${t===4?"grid grid-cols-2 gap-x-6 gap-y-1":"space-y-1"}`,children:a.map(([l,c])=>s.jsxs("div",{className:"flex items-center justify-between text-sm",children:[s.jsx("span",{className:`text-white truncate ${t===4?"max-w-[200px]":"max-w-[160px]"}`,children:l||"Unknown"}),s.jsx("span",{className:"text-bambu-gray ml-2",children:c})]},l))}),o&&s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["+",i.length-r," more reasons"]})]})]})}function TYe(){const{showToast:t}=qn(),[e,n]=w.useState(!1),[r,i]=w.useState(!1),[a,o]=w.useState(0),[l,c]=w.useState(0),[u,d]=w.useState(!1);w.useEffect(()=>{const E=()=>{try{const C=localStorage.getItem("bambusy-dashboard-layout");if(C){const D=JSON.parse(C);c(D.hidden?.length||0)}}catch{c(0)}};E(),window.addEventListener("storage",E);const O=setInterval(E,500);return()=>{window.removeEventListener("storage",E),clearInterval(O)}},[a]);const{data:f,isLoading:m,refetch:p}=ut({queryKey:["archiveStats"],queryFn:he.getArchiveStats}),{data:b}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:y}=ut({queryKey:["archives"],queryFn:()=>he.getArchives(void 0,void 0,1e3,0)}),{data:x}=ut({queryKey:["settings"],queryFn:he.getSettings}),v=async E=>{i(!1),n(!0);try{const{blob:O,filename:C}=await he.exportStats({format:E,days:90}),D=URL.createObjectURL(O),U=document.createElement("a");U.href=D,U.download=C,U.click(),URL.revokeObjectURL(D),t("Export downloaded")}catch{t("Export failed","error")}finally{n(!1)}},S=async()=>{d(!0);try{const E=await he.recalculateCosts();await p(),t(`Recalculated costs for ${E.updated} archives`)}catch{t("Failed to recalculate costs","error")}finally{d(!1)}},M=x?.currency||"$",A=new Map(b?.map(E=>[String(E.id),E.name])||[]),T=y?.map(E=>E.created_at)||[];if(m)return s.jsx("div",{className:"p-4 md:p-8",children:s.jsx("div",{className:"text-center py-12 text-bambu-gray",children:"Loading statistics..."})});const k=[{id:"quick-stats",title:"Quick Stats",component:s.jsx(vYe,{stats:f,currency:M}),defaultSize:2},{id:"success-rate",title:"Success Rate",component:E=>s.jsx(_Ye,{stats:f,printerMap:A,size:E}),defaultSize:1},{id:"time-accuracy",title:"Time Accuracy",component:E=>s.jsx(wYe,{stats:f,printerMap:A,size:E}),defaultSize:1},{id:"filament-types",title:"Filament Types",component:E=>s.jsx(SYe,{stats:f,size:E}),defaultSize:1},{id:"failure-analysis",title:"Failure Analysis (30 days)",component:E=>s.jsx(AYe,{size:E}),defaultSize:1},{id:"print-activity",title:"Print Activity",component:E=>s.jsx(MYe,{printDates:T,size:E}),defaultSize:2},{id:"prints-by-printer",title:"Prints by Printer",component:s.jsx(NYe,{stats:f,printerMap:A}),defaultSize:2},{id:"filament-trends",title:"Filament Usage Trends",component:s.jsx(EYe,{archives:y||[],currency:M}),defaultSize:4}];return s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsxs("div",{className:"flex items-center justify-between mb-6",children:[s.jsxs("div",{children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Dashboard"}),s.jsx("p",{className:"text-bambu-gray",children:"Drag widgets to rearrange. Click the eye icon to hide."})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[l>0&&s.jsxs(Se,{variant:"secondary",onClick:()=>{window.dispatchEvent(new CustomEvent("toggle-hidden-panel"))},children:[s.jsx(Qd,{className:"w-4 h-4"}),l," Hidden"]}),s.jsxs(Se,{variant:"secondary",onClick:()=>{localStorage.removeItem("bambusy-dashboard-layout"),o(E=>E+1),t("Layout reset")},children:[s.jsx(mm,{className:"w-4 h-4"}),"Reset Layout"]}),s.jsxs(Se,{variant:"secondary",onClick:S,disabled:u,title:"Recalculate all archive costs using current filament prices",children:[u?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Zue,{className:"w-4 h-4"}),"Recalculate Costs"]}),s.jsxs("div",{className:"relative",children:[s.jsxs(Se,{variant:"secondary",onClick:()=>i(!r),disabled:e,children:[e?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(I1,{className:"w-4 h-4"}),"Export Stats"]}),r&&s.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:[s.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:()=>v("csv"),children:[s.jsx(Za,{className:"w-4 h-4"}),"Export as CSV"]}),s.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:()=>v("xlsx"),children:[s.jsx(I1,{className:"w-4 h-4"}),"Export as Excel"]})]})]})]})]}),s.jsx(yYe,{widgets:k,storageKey:"bambusy-dashboard-layout",hideControls:!0},a)]})}function CYe({plug:t,onEdit:e}){const n=on(),{showToast:r}=qn(),[i,a]=w.useState(!1),[o,l]=w.useState(!1),[c,u]=w.useState(!1),[d,f]=w.useState(!1),{data:m,isLoading:p}=ut({queryKey:["smart-plug-status",t.id],queryFn:()=>he.getSmartPlugStatus(t.id),refetchInterval:3e4}),{data:b}=ut({queryKey:["printers"],queryFn:he.getPrinters}),y=b?.find(O=>O.id===t.printer_id),x=mt({mutationFn:O=>he.controlSmartPlug(t.id,O),onMutate:async O=>{await n.cancelQueries({queryKey:["smart-plug-status",t.id]});const C=n.getQueryData(["smart-plug-status",t.id]),D=O==="on"?"ON":O==="off"||m?.state==="ON"?"OFF":"ON";return n.setQueryData(["smart-plug-status",t.id],U=>({...U,state:D})),{previousStatus:C}},onError:(O,C,D)=>{D?.previousStatus&&n.setQueryData(["smart-plug-status",t.id],D.previousStatus),r(`Failed to turn ${C} "${t.name}"`,"error")},onSettled:()=>{setTimeout(()=>{n.invalidateQueries({queryKey:["smart-plug-status",t.id]}),n.invalidateQueries({queryKey:["smart-plugs"]})},1e3)}}),v=mt({mutationFn:O=>he.updateSmartPlug(t.id,O),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),t.printer_id&&n.invalidateQueries({queryKey:["smartPlugByPrinter",t.printer_id]})}}),S=mt({mutationFn:()=>he.deleteSmartPlug(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]})}}),M=m?.state==="ON",A=m?.reachable??!1,T=x.isPending,E=(()=>{if(t.plug_type!=="tasmota"||!t.ip_address)return null;const O=t.ip_address;return t.username&&t.password?`http://${encodeURIComponent(t.username)}:${encodeURIComponent(t.password)}@${O}/`:`http://${O}/`})();return s.jsxs(s.Fragment,{children:[s.jsx(St,{className:"relative",children:s.jsxs(Nt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-start justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${A?M?"bg-bambu-green/20":"bg-bambu-dark":"bg-red-500/20"}`,children:t.plug_type==="homeassistant"?s.jsx(cN,{className:`w-5 h-5 ${A?M?"text-bambu-green":"text-bambu-gray":"text-red-400"}`}):s.jsx(Ks,{className:`w-5 h-5 ${A?M?"text-bambu-green":"text-bambu-gray":"text-red-400"}`})}),s.jsxs("div",{children:[s.jsx("h3",{className:"font-medium text-white",children:t.name}),s.jsx("p",{className:"text-sm text-bambu-gray",children:t.plug_type==="homeassistant"?t.ha_entity_id:t.ip_address})]})]}),s.jsxs("div",{className:"flex flex-col items-end gap-1",children:[p?s.jsx(dt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):A?s.jsxs("div",{className:"flex items-center gap-1 text-sm",children:[s.jsx(ig,{className:"w-4 h-4 text-status-ok"}),s.jsx("span",{className:M?"text-status-ok":"text-bambu-gray",children:m?.state||"Unknown"})]}):s.jsxs("div",{className:"flex items-center gap-1 text-sm text-status-error",children:[s.jsx(ex,{className:"w-4 h-4"}),s.jsx("span",{children:"Offline"})]}),E&&s.jsxs("a",{href:E,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 px-2 py-0.5 bg-bambu-dark hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white text-xs rounded-full transition-colors",title:"Open plug admin page",children:[s.jsx(zr,{className:"w-3 h-3"}),"Admin"]})]})]}),y&&s.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Linked to: "}),s.jsx("span",{className:"text-sm text-white",children:y.name})]}),(t.power_alert_enabled||t.schedule_enabled)&&s.jsxs("div",{className:"flex flex-wrap gap-1.5 mb-3",children:[t.power_alert_enabled&&s.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:[s.jsx(Ef,{className:"w-3 h-3"}),"Alerts"]}),t.schedule_enabled&&s.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:[s.jsx(oi,{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}`]})]}),s.jsxs("div",{className:"flex gap-2 mb-3",children:[s.jsxs(Se,{size:"sm",variant:M?"primary":"secondary",disabled:!A||T,onClick:()=>l(!0),className:"flex-1",children:[T?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Eu,{className:"w-4 h-4"}),"On"]}),s.jsxs(Se,{size:"sm",variant:M?"secondary":"primary",disabled:!A||T,onClick:()=>u(!0),className:"flex-1",children:[T?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(fN,{className:"w-4 h-4"}),"Off"]})]}),s.jsxs("button",{onClick:()=>f(!d),className:"w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors",children:[s.jsxs("span",{className:"flex items-center gap-2",children:[s.jsx(Jd,{className:"w-4 h-4"}),"Automation Settings"]}),s.jsx("span",{children:d?"-":"+"})]}),d&&s.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(uN,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Show in Switchbar"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.show_in_switchbar,onChange:O=>v.mutate({show_in_switchbar:O.target.checked}),className:"sr-only peer"}),s.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"})]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Enabled"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Enable automation for this plug"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.enabled,onChange:O=>v.mutate({enabled:O.target.checked}),className:"sr-only peer"}),s.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"})]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Auto On"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn on when print starts"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.auto_on,onChange:O=>v.mutate({auto_on:O.target.checked}),className:"sr-only peer"}),s.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"})]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Auto Off"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Turn off when print completes (one-shot)"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:t.auto_off,onChange:O=>v.mutate({auto_off:O.target.checked}),className:"sr-only peer"}),s.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&&s.jsxs("div",{className:"space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white mb-2",children:"Turn Off Delay Mode"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("button",{onClick:()=>v.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:[s.jsx(Zn,{className:"w-4 h-4"}),"Time"]}),s.jsxs("button",{onClick:()=>v.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:[s.jsx(pN,{className:"w-4 h-4"}),"Temp"]})]})]}),t.off_delay_mode==="time"?s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Delay (minutes)"}),s.jsx("input",{type:"number",min:"1",max:"60",value:t.off_delay_minutes,onChange:O=>v.mutate({off_delay_minutes:parseInt(O.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"})]}):s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Temperature threshold (C)"}),s.jsx("input",{type:"number",min:"30",max:"100",value:t.off_temp_threshold,onChange:O=>v.mutate({off_temp_threshold:parseInt(O.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Turns off when nozzle cools below this temperature"})]})]}),s.jsxs("div",{className:"flex gap-2 pt-2",children:[s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[s.jsx(hN,{className:"w-4 h-4"}),"Edit"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>a(!0),className:"text-red-400 hover:text-red-300",children:s.jsx(dn,{className:"w-4 h-4"})})]})]})]})}),i&&s.jsx(Hn,{title:"Delete Smart Plug",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{S.mutate(),a(!1)},onCancel:()=>a(!1)}),o&&s.jsx(Hn,{title:"Turn On Smart Plug",message:`Are you sure you want to turn on "${t.name}"?`,confirmText:"Turn On",variant:"default",onConfirm:()=>{x.mutate("on"),l(!1)},onCancel:()=>l(!1)}),c&&s.jsx(Hn,{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:()=>{x.mutate("off"),u(!1)},onCancel:()=>u(!1)})]})}function kYe({plug:t,onClose:e}){const n=on(),r=!!t,[i,a]=w.useState(t?.plug_type||"tasmota"),[o,l]=w.useState(t?.name||""),[c,u]=w.useState(t?.ip_address||""),[d,f]=w.useState(t?.username||""),[m,p]=w.useState(t?.password||""),[b,y]=w.useState(t?.ha_entity_id||""),[x,v]=w.useState(t?.ha_power_entity||""),[S,M]=w.useState(t?.ha_energy_today_entity||""),[A,T]=w.useState(t?.ha_energy_total_entity||""),[k,E]=w.useState(""),[O,C]=w.useState(""),[D,U]=w.useState(!1),L=w.useRef(null),[q,P]=w.useState(""),[W,K]=w.useState(!1),V=w.useRef(null),[Q,re]=w.useState(""),[J,z]=w.useState(!1),G=w.useRef(null),[ne,ae]=w.useState(""),[de,fe]=w.useState(!1),ie=w.useRef(null),[Ae,me]=w.useState(t?.printer_id||null),[Ne,ye]=w.useState(null),[le,$e]=w.useState(null),[H,ee]=w.useState(t?.power_alert_enabled||!1),[X,B]=w.useState(t?.power_alert_high?.toString()||""),[I,ce]=w.useState(t?.power_alert_low?.toString()||""),[Te,Le]=w.useState(t?.schedule_enabled||!1),[Ee,$]=w.useState(t?.schedule_on_time||""),[j,Y]=w.useState(t?.schedule_off_time||""),[ge,we]=w.useState(t?.show_in_switchbar||!1),[ve,Ze]=w.useState(!1),[Ie,qe]=w.useState({scanned:0,total:0}),[Je,Qe]=w.useState([]),oe=w.useRef(null),{data:te}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:Me}=ut({queryKey:["smart-plugs"],queryFn:he.getSmartPlugs}),{data:Fe}=ut({queryKey:["settings"],queryFn:he.getSettings}),ot=!!(Fe?.ha_enabled&&Fe?.ha_url&&Fe?.ha_token);w.useEffect(()=>{const gt=setTimeout(()=>{C(k)},300);return()=>clearTimeout(gt)},[k]),w.useEffect(()=>{const gt=Kt=>{L.current&&!L.current.contains(Kt.target)&&U(!1),V.current&&!V.current.contains(Kt.target)&&K(!1),G.current&&!G.current.contains(Kt.target)&&z(!1),ie.current&&!ie.current.contains(Kt.target)&&fe(!1)};return document.addEventListener("mousedown",gt),()=>document.removeEventListener("mousedown",gt)},[]);const{data:se,isLoading:je,error:xe}=ut({queryKey:["ha-entities",O],queryFn:()=>he.getHAEntities(O||void 0),enabled:i==="homeassistant"&&ot,retry:!1,staleTime:0}),{data:Oe}=ut({queryKey:["ha-sensor-entities"],queryFn:he.getHASensorEntities,enabled:i==="homeassistant"&&ot,retry:!1,staleTime:0});w.useEffect(()=>{const gt=Kt=>{Kt.key==="Escape"&&e()};return window.addEventListener("keydown",gt),()=>{window.removeEventListener("keydown",gt),oe.current&&clearInterval(oe.current)}},[e]);const Ue=async()=>{Ze(!0),Qe([]),qe({scanned:0,total:0}),$e(null);try{await he.startTasmotaScan();const gt=async()=>{try{const Kt=await he.getTasmotaScanStatus();qe({scanned:Kt.scanned,total:Kt.total});const bn=await he.getDiscoveredTasmotaDevices();Qe(bn),Kt.running||(Ze(!1),oe.current&&(clearInterval(oe.current),oe.current=null))}catch(Kt){console.error("Polling error:",Kt)}};await gt(),oe.current=setInterval(gt,500)}catch(gt){Ze(!1);const Kt=gt instanceof Error?gt.message:typeof gt=="string"?gt:JSON.stringify(gt);$e(Kt||"Failed to start scan")}},Ce=async()=>{try{await he.stopTasmotaScan()}catch{}Ze(!1),oe.current&&(clearInterval(oe.current),oe.current=null)},We=gt=>{u(gt.ip_address),l(gt.name),ye(null)},pt=mt({mutationFn:()=>he.testSmartPlugConnection(c,d||null,m||null),onSuccess:gt=>{ye(gt),$e(null),!o&>.device_name&&l(gt.device_name)},onError:gt=>{ye(null),$e(gt.message)}}),kt=mt({mutationFn:gt=>he.createSmartPlug(gt),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:gt=>{$e(gt.message)}}),Lt=mt({mutationFn:gt=>he.updateSmartPlug(t.id,gt),onSuccess:()=>{n.invalidateQueries({queryKey:["smart-plugs"]}),e()},onError:gt=>{$e(gt.message)}}),ln=te?.filter(gt=>!Me?.some(bn=>bn.printer_id===gt.id&&bn.id!==t?.id)),Gt=gt=>{if(gt.preventDefault(),$e(null),!o.trim()){$e("Name is required");return}if(i==="tasmota"&&!c.trim()){$e("IP address is required for Tasmota plugs");return}if(i==="homeassistant"&&!b){$e("Entity is required for Home Assistant plugs");return}const Kt={name:o.trim(),plug_type:i,ip_address:i==="tasmota"?c.trim():null,ha_entity_id:i==="homeassistant"?b:null,ha_power_entity:i==="homeassistant"&&x||null,ha_energy_today_entity:i==="homeassistant"&&S||null,ha_energy_total_entity:i==="homeassistant"&&A||null,username:i==="tasmota"&&d.trim()||null,password:i==="tasmota"&&m.trim()||null,printer_id:Ae,power_alert_enabled:H,power_alert_high:X?parseFloat(X):null,power_alert_low:I?parseFloat(I):null,schedule_enabled:Te,schedule_on_time:Ee||null,schedule_off_time:j||null,show_in_switchbar:ge};r?Lt.mutate(Kt):kt.mutate(Kt)},_n=kt.isPending||Lt.isPending;return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:s.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:gt=>gt.stopPropagation(),children:[s.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary flex-shrink-0",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Smart Plug":"Add Smart Plug"}),s.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:Gt,className:"p-6 space-y-4 overflow-y-auto",children:[le&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:le}),!r&&s.jsxs("div",{className:"flex gap-2 mb-2",children:[s.jsxs("button",{type:"button",onClick:()=>{a("tasmota"),ye(null),$e(null)},className:`flex-1 flex items-center justify-center gap-2 px-4 py-2.5 rounded-lg font-medium transition-colors ${i==="tasmota"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white border border-bambu-dark-tertiary"}`,children:[s.jsx(Ks,{className:"w-4 h-4"}),"Tasmota"]}),s.jsxs("button",{type:"button",onClick:()=>{a("homeassistant"),ye(null),$e(null)},className:`flex-1 flex items-center justify-center gap-2 px-4 py-2.5 rounded-lg font-medium transition-colors ${i==="homeassistant"?"bg-bambu-green text-white":"bg-bambu-dark text-bambu-gray hover:text-white border border-bambu-dark-tertiary"}`,children:[s.jsx(cN,{className:"w-4 h-4"}),"Home Assistant"]})]}),!r&&i==="tasmota"&&s.jsxs("div",{className:"space-y-3",children:[ve?s.jsxs(Se,{type:"button",variant:"secondary",onClick:Ce,className:"w-full",children:[s.jsx(Vt,{className:"w-4 h-4"}),"Stop Scanning"]}):s.jsxs(Se,{type:"button",variant:"primary",onClick:Ue,className:"w-full",children:[s.jsx(Ea,{className:"w-4 h-4"}),"Discover Tasmota Devices"]}),ve&&Ie.total>0&&s.jsxs("div",{className:"space-y-1",children:[s.jsxs("div",{className:"flex justify-between text-xs text-bambu-gray",children:[s.jsx("span",{children:"Scanning network..."}),s.jsxs("span",{children:[Ie.scanned," / ",Ie.total]})]}),s.jsx("div",{className:"w-full bg-bambu-dark-tertiary rounded-full h-2",children:s.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${Ie.scanned/Ie.total*100}%`}})})]}),Je.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("p",{className:"text-xs text-bambu-gray",children:["Found ",Je.length," device(s) - click to select:"]}),s.jsx("div",{className:"max-h-40 overflow-y-auto space-y-1",children:Je.map(gt=>s.jsxs("button",{type:"button",onClick:()=>We(gt),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:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Ks,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:gt.name}),s.jsx("p",{className:"text-xs text-bambu-gray",children:gt.ip_address})]})]}),gt.state&&s.jsxs("span",{className:`flex items-center gap-1 text-xs ${gt.state==="ON"?"text-bambu-green":"text-bambu-gray"}`,children:[s.jsx(Eu,{className:"w-3 h-3"}),gt.state]})]},gt.ip_address))})]}),!ve&&Je.length===0&&Ie.total>0&&s.jsx("p",{className:"text-xs text-bambu-gray text-center py-2",children:"No Tasmota devices found on your network"})]}),i==="homeassistant"&&s.jsxs("div",{className:"space-y-3",children:[!ot&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"p-3 bg-yellow-500/20 border border-yellow-500/50 rounded-lg text-sm text-yellow-400",children:["Home Assistant is not configured. Set it up in"," ",s.jsx("span",{className:"font-medium",children:"Settings → Network → Home Assistant"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1 opacity-50",children:"Select Entity *"}),s.jsx("select",{disabled:!0,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray cursor-not-allowed opacity-50",children:s.jsx("option",{children:"Choose an entity..."})})]})]}),ot&&s.jsxs(s.Fragment,{children:[je&&s.jsxs("div",{className:"flex items-center justify-center py-4 text-bambu-gray",children:[s.jsx(dt,{className:"w-5 h-5 animate-spin mr-2"}),"Loading entities..."]}),xe&&s.jsxs("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:["Failed to load entities: ",xe.message]}),(()=>{const gt=Me?.filter(At=>At.ha_entity_id&&At.id!==t?.id).map(At=>At.ha_entity_id)||[],Kt=(se||[]).filter(At=>!gt.includes(At.entity_id)),bn=se?.find(At=>At.entity_id===b);return s.jsxs("div",{ref:L,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Select Entity *"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:D?k:bn?`${bn.friendly_name} (${bn.entity_id})`:"",onChange:At=>{E(At.target.value),D||U(!0)},onFocus:()=>{U(!0),E("")},placeholder:"Search entities...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),b&&!D&&s.jsx("button",{type:"button",onClick:()=>{y(""),E("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})}),je&&s.jsx(dt,{className:"absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray animate-spin"})]}),D&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-60 overflow-y-auto",children:[je&&s.jsxs("div",{className:"px-3 py-2 text-sm text-bambu-gray flex items-center gap-2",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),"Loading..."]}),!je&&Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:O?`No entities found matching "${O}"`:"No entities available"}),!je&&Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{y(At.entity_id),U(!1),E(""),o||l(At.friendly_name)},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary transition-colors ${At.entity_id===b?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray flex items-center justify-between",children:[s.jsx("span",{children:At.entity_id}),s.jsx("span",{className:At.state==="on"?"text-bambu-green":"",children:At.state})]})]},At.entity_id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:O?`Searching all entities (${Kt.length} found)`:`Showing switch, light, input_boolean (${Kt.length} available)`})]})})(),b&&Oe&&Oe.length>0&&s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4 mt-4 space-y-3",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white font-medium mb-1",children:"Energy Monitoring (Optional)"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-3",children:"Search and select sensors that provide power/energy data."})]}),(()=>{const gt=Oe.filter(At=>At.unit_of_measurement==="W"||At.unit_of_measurement==="kW"||At.unit_of_measurement==="mW"),Kt=q?gt.filter(At=>At.entity_id.toLowerCase().includes(q.toLowerCase())||At.friendly_name.toLowerCase().includes(q.toLowerCase())):gt,bn=Oe.find(At=>At.entity_id===x);return s.jsxs("div",{ref:V,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Power Sensor (W)"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:W?q:bn?`${bn.friendly_name} (${bn.state} ${bn.unit_of_measurement})`:"",onChange:At=>{P(At.target.value),W||K(!0)},onFocus:()=>{K(!0),P("")},placeholder:"Search power sensors...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),x&&!W&&s.jsx("button",{type:"button",onClick:()=>{v(""),P("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]}),W&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[s.jsx("button",{type:"button",onClick:()=>{v(""),K(!1),P("")},className:"w-full px-3 py-2 text-left text-sm text-bambu-gray hover:bg-bambu-dark-tertiary",children:"None"}),Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{v(At.entity_id),K(!1),P("")},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${At.entity_id===x?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray",children:[At.entity_id," • ",At.state," ",At.unit_of_measurement]})]},At.entity_id)),Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:"No matching sensors"})]})]})})(),(()=>{const gt=Oe.filter(At=>At.unit_of_measurement==="kWh"||At.unit_of_measurement==="Wh"||At.unit_of_measurement==="MWh"),Kt=Q?gt.filter(At=>At.entity_id.toLowerCase().includes(Q.toLowerCase())||At.friendly_name.toLowerCase().includes(Q.toLowerCase())):gt,bn=Oe.find(At=>At.entity_id===S);return s.jsxs("div",{ref:G,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Energy Today (kWh)"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:J?Q:bn?`${bn.friendly_name} (${bn.state} ${bn.unit_of_measurement})`:"",onChange:At=>{re(At.target.value),J||z(!0)},onFocus:()=>{z(!0),re("")},placeholder:"Search energy sensors...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),S&&!J&&s.jsx("button",{type:"button",onClick:()=>{M(""),re("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]}),J&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[s.jsx("button",{type:"button",onClick:()=>{M(""),z(!1),re("")},className:"w-full px-3 py-2 text-left text-sm text-bambu-gray hover:bg-bambu-dark-tertiary",children:"None"}),Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{M(At.entity_id),z(!1),re("")},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${At.entity_id===S?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray",children:[At.entity_id," • ",At.state," ",At.unit_of_measurement]})]},At.entity_id)),Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:"No matching sensors"})]})]})})(),(()=>{const gt=Oe.filter(At=>At.unit_of_measurement==="kWh"||At.unit_of_measurement==="Wh"||At.unit_of_measurement==="MWh"),Kt=ne?gt.filter(At=>At.entity_id.toLowerCase().includes(ne.toLowerCase())||At.friendly_name.toLowerCase().includes(ne.toLowerCase())):gt,bn=Oe.find(At=>At.entity_id===A);return s.jsxs("div",{ref:ie,className:"relative",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Total Energy (kWh)"}),s.jsxs("div",{className:"relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"}),s.jsx("input",{type:"text",value:de?ne:bn?`${bn.friendly_name} (${bn.state} ${bn.unit_of_measurement})`:"",onChange:At=>{ae(At.target.value),de||fe(!0)},onFocus:()=>{fe(!0),ae("")},placeholder:"Search energy sensors...",className:"w-full pl-9 pr-8 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:border-bambu-green focus:outline-none"}),A&&!de&&s.jsx("button",{type:"button",onClick:()=>{T(""),ae("")},className:"absolute right-2 top-1/2 -translate-y-1/2 p-1 hover:bg-bambu-dark-tertiary rounded",children:s.jsx(Vt,{className:"w-4 h-4 text-bambu-gray hover:text-white"})})]}),de&&s.jsxs("div",{className:"absolute z-50 w-full mt-1 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg shadow-lg max-h-48 overflow-y-auto",children:[s.jsx("button",{type:"button",onClick:()=>{T(""),fe(!1),ae("")},className:"w-full px-3 py-2 text-left text-sm text-bambu-gray hover:bg-bambu-dark-tertiary",children:"None"}),Kt.map(At=>s.jsxs("button",{type:"button",onClick:()=>{T(At.entity_id),fe(!1),ae("")},className:`w-full px-3 py-2 text-left text-sm hover:bg-bambu-dark-tertiary ${At.entity_id===A?"bg-bambu-green/20 text-bambu-green":"text-white"}`,children:[s.jsx("div",{className:"font-medium",children:At.friendly_name}),s.jsxs("div",{className:"text-xs text-bambu-gray",children:[At.entity_id," • ",At.state," ",At.unit_of_measurement]})]},At.entity_id)),Kt.length===0&&s.jsx("div",{className:"px-3 py-2 text-sm text-bambu-gray",children:"No matching sensors"})]})]})})()]})]})]}),i==="tasmota"&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"IP Address *"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx("input",{type:"text",value:c,onChange:gt=>{u(gt.target.value),ye(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"}),s.jsxs(Se,{type:"button",variant:"secondary",onClick:()=>pt.mutate(),disabled:!c.trim()||pt.isPending,children:[pt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(ig,{className:"w-4 h-4"}),"Test"]})]})]}),i==="tasmota"&&Ne&&s.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${Ne.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:Ne.success?s.jsxs(s.Fragment,{children:[s.jsx(Un,{className:"w-5 h-5"}),s.jsxs("div",{children:[s.jsx("p",{className:"font-medium",children:"Connected!"}),s.jsxs("p",{className:"text-sm opacity-80",children:[Ne.device_name&&`Device: ${Ne.device_name} - `,"State: ",Ne.state]})]})]}):s.jsxs(s.Fragment,{children:[s.jsx(ex,{className:"w-5 h-5"}),s.jsx("span",{children:"Connection failed"})]})}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),s.jsx("input",{type:"text",value:o,onChange:gt=>l(gt.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"})]}),i==="tasmota"&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Username"}),s.jsx("input",{type:"text",value:d,onChange:gt=>f(gt.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),s.jsx("input",{type:"password",value:m,onChange:gt=>p(gt.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"})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray -mt-2",children:"Leave empty if your Tasmota device doesn't require authentication"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Link to Printer"}),s.jsxs("select",{value:Ae??"",onChange:gt=>me(gt.target.value?Number(gt.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:[s.jsx("option",{value:"",children:"No printer (manual control only)"}),ln?.map(gt=>s.jsx("option",{value:gt.id,children:gt.name},gt.id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Linking enables automatic on/off when prints start/complete"})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Ef,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"text-white font-medium",children:"Power Alerts"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:H,onChange:gt=>ee(gt.target.checked),className:"sr-only peer"}),s.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"})]})]}),H&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if above (W)"}),s.jsx("input",{type:"number",value:X,onChange:gt=>B(gt.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Alert if below (W)"}),s.jsx("input",{type:"number",value:I,onChange:gt=>ce(gt.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"})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Get notified when power consumption crosses these thresholds. Leave empty to disable that direction."})]})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zn,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"text-white font-medium",children:"Daily Schedule"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:Te,onChange:gt=>Le(gt.target.checked),className:"sr-only peer"}),s.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"})]})]}),Te&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn On at"}),s.jsx("input",{type:"time",value:Ee,onChange:gt=>$(gt.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Turn Off at"}),s.jsx("input",{type:"time",value:j,onChange:gt=>Y(gt.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"})]})]}),s.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."})]})]}),s.jsx("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(uN,{className:"w-4 h-4 text-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white font-medium",children:"Show in Switchbar"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Quick access from sidebar"})]})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:ge,onChange:gt=>we(gt.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"flex gap-3 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:_n,className:"flex-1",children:[_n?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(_s,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function Gn({checked:t,onChange:e,disabled:n}){const r=i=>{i.preventDefault(),i.stopPropagation(),n||e(!t)};return s.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:s.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 jYe={callmebot:"CallMeBot/WhatsApp",ntfy:"ntfy",pushover:"Pushover",telegram:"Telegram",email:"Email",discord:"Discord",webhook:"Webhook"};function PYe({provider:t,onEdit:e}){const n=on(),[r,i]=w.useState(!1),[a,o]=w.useState(!1),[l,c]=w.useState(null),{data:u}=ut({queryKey:["printers"],queryFn:he.getPrinters}),d=u?.find(y=>y.id===t.printer_id),f=mt({mutationFn:y=>he.updateNotificationProvider(t.id,y),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),m=mt({mutationFn:()=>he.deleteNotificationProvider(t.id),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]})}}),p=mt({mutationFn:()=>he.testNotificationProvider(t.id),onSuccess:y=>{c(y),n.invalidateQueries({queryKey:["notification-providers"]})},onError:y=>{c({success:!1,message:y.message})}}),b=y=>y||"";return s.jsxs(s.Fragment,{children:[s.jsx(St,{className:"relative",children:s.jsxs(Nt,{className:"p-4",children:[s.jsxs("div",{className:"flex items-start justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`p-2 rounded-lg ${t.enabled?"bg-bambu-green/20":"bg-bambu-dark"}`,children:s.jsx(Ef,{className:`w-5 h-5 ${t.enabled?"text-bambu-green":"text-bambu-gray"}`})}),s.jsxs("div",{children:[s.jsx("h3",{className:"font-medium text-white",children:t.name}),s.jsx("p",{className:"text-sm text-bambu-gray",children:jYe[t.provider_type]||t.provider_type})]})]}),s.jsxs("div",{className:"flex items-center gap-3",children:[t.last_success&&s.jsxs("span",{className:"text-xs text-status-ok hidden sm:inline",children:["Last: ",_g(t.last_success)]}),t.last_error&&t.last_error_at&&(!t.last_success||(Ni(t.last_error_at)?.getTime()||0)>(Ni(t.last_success)?.getTime()||0))&&s.jsx("span",{className:"text-xs text-status-error",title:t.last_error,children:"Error"}),s.jsx(Gn,{checked:t.enabled,onChange:y=>f.mutate({enabled:y})})]})]}),d&&s.jsxs("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Printer: "}),s.jsx("span",{className:"text-sm text-white",children:d.name})]}),!d&&!t.printer_id&&s.jsx("div",{className:"mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg",children:s.jsx("span",{className:"text-xs text-bambu-gray",children:"All printers"})}),s.jsxs("div",{className:"mb-3 flex flex-wrap gap-1",children:[t.on_print_start&&s.jsx("span",{className:"px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded",children:"Start"}),t.on_plate_not_empty&&s.jsx("span",{className:"px-2 py-0.5 bg-rose-600/20 text-rose-300 text-xs rounded",children:"Plate Check"}),t.on_print_complete&&s.jsx("span",{className:"px-2 py-0.5 bg-bambu-green/20 text-bambu-green text-xs rounded",children:"Complete"}),t.on_print_failed&&s.jsx("span",{className:"px-2 py-0.5 bg-red-500/20 text-red-400 text-xs rounded",children:"Failed"}),t.on_print_stopped&&s.jsx("span",{className:"px-2 py-0.5 bg-orange-500/20 text-orange-400 text-xs rounded",children:"Stopped"}),t.on_print_progress&&s.jsx("span",{className:"px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded",children:"Progress"}),t.on_printer_offline&&s.jsx("span",{className:"px-2 py-0.5 bg-gray-500/20 text-gray-400 text-xs rounded",children:"Offline"}),t.on_printer_error&&s.jsx("span",{className:"px-2 py-0.5 bg-rose-500/20 text-rose-400 text-xs rounded",children:"Error"}),t.on_filament_low&&s.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&&s.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&&s.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&&s.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&&s.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&&s.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&&s.jsxs("span",{className:"px-2 py-0.5 bg-indigo-500/20 text-indigo-400 text-xs rounded flex items-center gap-1",children:[s.jsx(F1,{className:"w-3 h-3"}),"Quiet"]}),t.daily_digest_enabled&&s.jsxs("span",{className:"px-2 py-0.5 bg-emerald-500/20 text-emerald-400 text-xs rounded flex items-center gap-1",children:[s.jsx(oi,{className:"w-3 h-3"}),"Digest ",t.daily_digest_time]})]}),s.jsx("div",{className:"mb-3",children:s.jsxs(Se,{size:"sm",variant:"secondary",disabled:p.isPending,onClick:()=>{c(null),p.mutate()},className:"w-full",children:[p.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(GD,{className:"w-4 h-4"}),"Send Test Notification"]})}),l&&s.jsxs("div",{className:`mb-3 p-2 rounded-lg flex items-center gap-2 text-sm ${l.success?"bg-bambu-green/20 text-bambu-green":"bg-red-500/20 text-red-400"}`,children:[l.success?s.jsx(Un,{className:"w-4 h-4"}):s.jsx(ia,{className:"w-4 h-4"}),s.jsx("span",{children:l.message})]}),s.jsxs("button",{onClick:()=>o(!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:[s.jsxs("span",{className:"flex items-center gap-2",children:[s.jsx(Jd,{className:"w-4 h-4"}),"Event Settings"]}),a?s.jsx(pu,{className:"w-4 h-4"}):s.jsx(Oi,{className:"w-4 h-4"})]}),a&&s.jsxs("div",{className:"pt-3 border-t border-bambu-dark-tertiary space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Enabled"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Send notifications from this provider"})]}),s.jsx(Gn,{checked:t.enabled,onChange:y=>f.mutate({enabled:y})})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Print Events"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Started"}),s.jsx(Gn,{checked:t.on_print_start,onChange:y=>f.mutate({on_print_start:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Plate Not Empty"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Objects detected before print"})]}),s.jsx(Gn,{checked:t.on_plate_not_empty??!0,onChange:y=>f.mutate({on_plate_not_empty:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Completed"}),s.jsx(Gn,{checked:t.on_print_complete,onChange:y=>f.mutate({on_print_complete:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Failed"}),s.jsx(Gn,{checked:t.on_print_failed,onChange:y=>f.mutate({on_print_failed:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Print Stopped"}),s.jsx(Gn,{checked:t.on_print_stopped,onChange:y=>f.mutate({on_print_stopped:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Progress Milestones"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify at 25%, 50%, 75%"})]}),s.jsx(Gn,{checked:t.on_print_progress,onChange:y=>f.mutate({on_print_progress:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"Printer Status"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Printer Offline"}),s.jsx(Gn,{checked:t.on_printer_offline,onChange:y=>f.mutate({on_printer_offline:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Printer Error"}),s.jsx(Gn,{checked:t.on_printer_error,onChange:y=>f.mutate({on_printer_error:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("p",{className:"text-sm text-white",children:"Low Filament"}),s.jsx(Gn,{checked:t.on_filament_low,onChange:y=>f.mutate({on_filament_low:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Maintenance Due"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Notify when maintenance is needed"})]}),s.jsx(Gn,{checked:t.on_maintenance_due??!1,onChange:y=>f.mutate({on_maintenance_due:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS Alarms"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS Humidity High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS humidity exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_humidity_high??!1,onChange:y=>f.mutate({on_ams_humidity_high:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS Temperature High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Regular AMS temperature exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_temperature_high??!1,onChange:y=>f.mutate({on_ams_temperature_high:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide",children:"AMS-HT Alarms"}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS-HT Humidity High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT humidity exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_ht_humidity_high??!1,onChange:y=>f.mutate({on_ams_ht_humidity_high:y})})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"AMS-HT Temperature High"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"AMS-HT temperature exceeds threshold"})]}),s.jsx(Gn,{checked:t.on_ams_ht_temperature_high??!1,onChange:y=>f.mutate({on_ams_ht_temperature_high:y})})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(F1,{className:"w-4 h-4 text-purple-400"}),s.jsx("p",{className:"text-sm text-white",children:"Quiet Hours"})]}),s.jsx(Gn,{checked:t.quiet_hours_enabled,onChange:y=>f.mutate({quiet_hours_enabled:y})})]}),t.quiet_hours_enabled&&s.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"No notifications during these hours"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zn,{className:"w-4 h-4 text-bambu-gray"}),s.jsxs("span",{className:"text-sm text-white",children:[b(t.quiet_hours_start)||"22:00"," - ",b(t.quiet_hours_end)||"07:00"]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change quiet hours"})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(oi,{className:"w-4 h-4 text-emerald-400"}),s.jsx("p",{className:"text-sm text-white",children:"Daily Digest"})]}),s.jsx(Gn,{checked:t.daily_digest_enabled,onChange:y=>f.mutate({daily_digest_enabled:y})})]}),t.daily_digest_enabled&&s.jsxs("div",{className:"pl-4 border-l-2 border-bambu-dark-tertiary space-y-2",children:[s.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Zn,{className:"w-4 h-4 text-bambu-gray"}),s.jsxs("span",{className:"text-sm text-white",children:["Send at ",b(t.daily_digest_time)||"08:00"]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Edit provider to change digest time"})]})]}),s.jsxs("div",{className:"flex gap-2 pt-2",children:[s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>e(t),className:"flex-1",children:[s.jsx(hN,{className:"w-4 h-4"}),"Edit"]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>i(!0),className:"text-red-400 hover:text-red-300",children:s.jsx(dn,{className:"w-4 h-4"})})]})]})]})}),r&&s.jsx(Hn,{title:"Delete Notification Provider",message:`Are you sure you want to delete "${t.name}"? This cannot be undone.`,confirmText:"Delete",variant:"danger",onConfirm:()=>{m.mutate(),i(!1)},onCancel:()=>i(!1)})]})}const fq=[{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 RYe({provider:t,onClose:e}){const n=on(),r=!!t,[i,a]=w.useState(t?.name||""),[o,l]=w.useState(t?.provider_type||"email"),[c,u]=w.useState(t?.printer_id||null),[d,f]=w.useState(t?.quiet_hours_enabled||!1),[m,p]=w.useState(t?.quiet_hours_start||"22:00"),[b,y]=w.useState(t?.quiet_hours_end||"07:00"),[x,v]=w.useState(t?.daily_digest_enabled||!1),[S,M]=w.useState(t?.daily_digest_time||"08:00"),[A,T]=w.useState(t?.on_print_start??!1),[k,E]=w.useState(t?.on_print_complete??!0),[O,C]=w.useState(t?.on_print_failed??!0),[D,U]=w.useState(t?.on_print_stopped??!0),[L,q]=w.useState(t?.on_print_progress??!1),[P,W]=w.useState(t?.on_printer_offline??!1),[K,V]=w.useState(t?.on_printer_error??!1),[Q,re]=w.useState(t?.on_filament_low??!1),[J,z]=w.useState(t?.on_maintenance_due??!1),[G,ne]=w.useState(t?.config?Object.fromEntries(Object.entries(t.config).map(([B,I])=>[B,String(I)])):{}),[ae,de]=w.useState(null),[fe,ie]=w.useState(null),{data:Ae}=ut({queryKey:["printers"],queryFn:he.getPrinters});w.useEffect(()=>{const B=I=>{I.key==="Escape"&&e()};return window.addEventListener("keydown",B),()=>window.removeEventListener("keydown",B)},[e]);const me=mt({mutationFn:()=>he.testNotificationConfig({provider_type:o,config:G}),onSuccess:B=>{de(B),ie(null)},onError:B=>{de({success:!1,message:B.message})}}),Ne=mt({mutationFn:B=>he.createNotificationProvider(B),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:B=>{ie(B.message)}}),ye=mt({mutationFn:B=>he.updateNotificationProvider(t.id,B),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-providers"]}),e()},onError:B=>{ie(B.message)}}),le=B=>{if(B.preventDefault(),ie(null),!i.trim()){ie("Name is required");return}const I=ee(o);for(const Te of I)if(!G[Te.key]?.trim()){ie(`${Te.label} is required`);return}const ce={name:i.trim(),provider_type:o,config:G,printer_id:c,quiet_hours_enabled:d,quiet_hours_start:d?m:null,quiet_hours_end:d?b:null,daily_digest_enabled:x,daily_digest_time:x?S:null,on_print_start:A,on_print_complete:k,on_print_failed:O,on_print_stopped:D,on_print_progress:L,on_printer_offline:P,on_printer_error:K,on_filament_low:Q,on_maintenance_due:J};r?ye.mutate(ce):Ne.mutate(ce)},$e=Ne.isPending||ye.isPending,H=B=>{switch(B){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:"payload_format",label:"Payload Format",type:"select",required:!1,options:[{value:"generic",label:"Generic JSON"},{value:"slack",label:"Slack / Mattermost"}]},{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,showIf:I=>I.payload_format!=="slack"},{key:"field_message",label:"Message Field Name",placeholder:"message",type:"text",required:!1,showIf:I=>I.payload_format!=="slack"}];default:return[]}},ee=B=>H(B).filter(I=>I.required),X=H(o);return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4 overflow-y-auto",onClick:e,children:s.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:B=>B.stopPropagation(),children:[s.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[s.jsx("h2",{className:"text-lg font-semibold text-white",children:r?"Edit Notification Provider":"Add Notification Provider"}),s.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:le,className:"p-6 space-y-4",children:[fe&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:fe}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),s.jsx("input",{type:"text",value:i,onChange:B=>a(B.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Provider Type *"}),s.jsx("select",{value:o,onChange:B=>{l(B.target.value),ne({}),de(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:fq.map(B=>s.jsx("option",{value:B.value,children:B.label},B.value))}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:fq.find(B=>B.value===o)?.description})]}),s.jsxs("div",{className:"space-y-3",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Configuration"}),X.filter(B=>!("showIf"in B)||B.showIf?.(G)!==!1).map(B=>s.jsxs("div",{children:[s.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[B.label," ",B.required&&"*"]}),B.type==="select"&&"options"in B&&B.options?s.jsx("select",{value:G[B.key]||B.options[0]?.value||"",onChange:I=>{ne({...G,[B.key]:I.target.value}),de(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:B.options.map(I=>s.jsx("option",{value:I.value,children:I.label},I.value))}):s.jsx("input",{type:B.type,value:G[B.key]||"",onChange:I=>{ne({...G,[B.key]:I.target.value}),de(null)},placeholder:B.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"})]},B.key))]}),s.jsx("div",{className:"flex gap-2",children:s.jsxs(Se,{type:"button",variant:"secondary",onClick:()=>{de(null),me.mutate()},disabled:me.isPending||!G[ee(o)[0]?.key],className:"flex-1",children:[me.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(GD,{className:"w-4 h-4"}),"Test Configuration"]})}),ae&&s.jsx("div",{className:`p-3 rounded-lg flex items-center gap-2 ${ae.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:ae.success?s.jsxs(s.Fragment,{children:[s.jsx(Un,{className:"w-5 h-5"}),s.jsx("span",{children:ae.message})]}):s.jsxs(s.Fragment,{children:[s.jsx(ia,{className:"w-5 h-5"}),s.jsx("span",{children:ae.message})]})}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer Filter"}),s.jsxs("select",{value:c??"",onChange:B=>u(B.target.value?Number(B.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:[s.jsx("option",{value:"",children:"All printers"}),Ae?.map(B=>s.jsx("option",{value:B.id,children:B.name},B.id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Only send notifications for events from this printer"})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("label",{className:"text-sm text-white",children:"Quiet Hours (Do Not Disturb)"}),s.jsx(Gn,{checked:d,onChange:f})]}),d&&s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Start"}),s.jsx("input",{type:"time",value:m,onChange:B=>p(B.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"End"}),s.jsx("input",{type:"time",value:b,onChange:B=>y(B.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"})]})]})]}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("label",{className:"text-sm text-white",children:"Daily Digest"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Batch notifications into a single daily summary"})]}),s.jsx(Gn,{checked:x,onChange:v})]}),x&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Send digest at"}),s.jsx("input",{type:"time",value:S,onChange:B=>M(B.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Events will be collected and sent as a single summary at this time"})]})]}),s.jsxs("div",{className:"space-y-3",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Notification Events"}),s.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Print Events"}),s.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Start"}),s.jsx(Gn,{checked:A,onChange:T})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Complete"}),s.jsx(Gn,{checked:k,onChange:E})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Failed"}),s.jsx(Gn,{checked:O,onChange:C})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Stopped"}),s.jsx(Gn,{checked:D,onChange:U})]}),s.jsxs("div",{className:"flex items-center justify-between col-span-2",children:[s.jsxs("div",{children:[s.jsx("span",{className:"text-sm text-white",children:"Progress"}),s.jsx("span",{className:"text-xs text-bambu-gray ml-1",children:"(25%, 50%, 75%)"})]}),s.jsx(Gn,{checked:L,onChange:q})]})]})]}),s.jsxs("div",{className:"space-y-2 p-3 bg-bambu-dark rounded-lg",children:[s.jsx("p",{className:"text-xs text-bambu-gray uppercase tracking-wide mb-2",children:"Printer Status"}),s.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Offline"}),s.jsx(Gn,{checked:P,onChange:W})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Error"}),s.jsx(Gn,{checked:K,onChange:V})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Low Filament"}),s.jsx(Gn,{checked:Q,onChange:re})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsx("span",{className:"text-sm text-white",children:"Maintenance"}),s.jsx(Gn,{checked:J,onChange:z})]})]})]})]}),s.jsxs("div",{className:"flex gap-3 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:$e,className:"flex-1",children:[$e?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(_s,{className:"w-4 h-4"}),r?"Save":"Add"]})]})]})]})})}function DYe({template:t,onClose:e}){const n=on(),r=w.useRef(null),[i,a]=w.useState(t.title_template),[o,l]=w.useState(t.body_template),[c,u]=w.useState(null),[d,f]=w.useState(!0),{data:m}=ut({queryKey:["template-variables"],queryFn:he.getTemplateVariables}),p=m?.find(T=>T.event_type===t.event_type),{data:b,isLoading:y}=ut({queryKey:["template-preview",t.event_type,i,o],queryFn:()=>he.previewTemplate({event_type:t.event_type,title_template:i,body_template:o}),enabled:d&&i.length>0&&o.length>0});w.useEffect(()=>{const T=k=>{k.key==="Escape"&&e()};return window.addEventListener("keydown",T),()=>window.removeEventListener("keydown",T)},[e]);const x=mt({mutationFn:T=>he.updateNotificationTemplate(t.id,T),onSuccess:()=>{n.invalidateQueries({queryKey:["notification-templates"]}),e()},onError:T=>{u(T.message)}}),v=mt({mutationFn:()=>he.resetNotificationTemplate(t.id),onSuccess:T=>{a(T.title_template),l(T.body_template),n.invalidateQueries({queryKey:["notification-templates"]})},onError:T=>{u(T.message)}}),S=T=>{if(T.preventDefault(),u(null),!i.trim()){u("Title is required");return}if(!o.trim()){u("Body is required");return}x.mutate({title_template:i,body_template:o})},M=T=>{const k=r.current;if(!k)return;const E=k.selectionStart,O=k.selectionEnd,C=o,D=C.substring(0,E),U=C.substring(O),L=D+`{${T}}`+U;l(L),setTimeout(()=>{k.focus();const q=E+T.length+2;k.setSelectionRange(q,q)},0)},A=i!==t.title_template||o!==t.body_template;return s.jsx("div",{className:"fixed inset-0 bg-black/80 flex items-center justify-center z-50 p-4",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg w-full max-w-2xl max-h-[90vh] flex flex-col",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary shrink-0",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Edit Template: ",t.name]}),s.jsx("button",{onClick:e,className:"p-1 hover:bg-bambu-dark-tertiary rounded transition-colors",children:s.jsx(Vt,{className:"w-5 h-5 text-bambu-gray"})})]}),s.jsxs("form",{onSubmit:S,className:"flex-1 overflow-y-auto p-4 space-y-4",children:[c&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded text-red-400 text-sm",children:c}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Title"}),s.jsx("input",{type:"text",value:i,onChange:T=>a(T.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..."})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-1",children:"Body"}),s.jsx("textarea",{ref:r,value:o,onChange:T=>l(T.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&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm font-medium text-bambu-gray mb-2",children:"Available Variables"}),s.jsx("div",{className:"flex flex-wrap gap-2",children:p.variables.map(T=>s.jsxs("button",{type:"button",onClick:()=>M(T),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:[s.jsx(nr,{className:"w-3 h-3"}),T]},T))}),s.jsx("p",{className:"text-xs text-bambu-gray/60 mt-1",children:"Click to insert at cursor position in body"})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsxs("label",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(Qd,{className:"w-4 h-4"}),"Live Preview"]}),s.jsx("button",{type:"button",onClick:()=>f(!d),className:"text-xs text-bambu-green hover:text-bambu-green-light",children:d?"Hide":"Show"})]}),d&&s.jsx("div",{className:"bg-bambu-dark border border-bambu-dark-tertiary rounded p-3 space-y-2",children:y?s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-sm",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),"Loading preview..."]}):b?s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Title:"}),s.jsx("div",{className:"text-white font-medium",children:b.title})]}),s.jsxs("div",{children:[s.jsx("span",{className:"text-xs text-bambu-gray",children:"Body:"}),s.jsx("div",{className:"text-white whitespace-pre-wrap text-sm",children:b.body})]})]}):s.jsx("div",{className:"text-bambu-gray text-sm",children:"Enter template content to see preview"})})]})]}),s.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary shrink-0",children:[s.jsxs(Se,{type:"button",variant:"ghost",onClick:()=>v.mutate(),disabled:v.isPending,className:"text-orange-400 hover:text-orange-300",children:[v.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin mr-2"}):s.jsx(mm,{className:"w-4 h-4 mr-2"}),"Reset to Default"]}),s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,children:"Cancel"}),s.jsxs(Se,{onClick:S,disabled:x.isPending||!A,children:[x.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin mr-2"}):s.jsx(_s,{className:"w-4 h-4 mr-2"}),"Save"]})]})]})]})})}const OYe={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"},LYe={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 IYe({onClose:t}){const e=on(),{showToast:n}=qn(),[r,i]=w.useState(7),[a,o]=w.useState(null),[l,c]=w.useState(!1),{data:u}=ut({queryKey:["settings"],queryFn:he.getSettings}),d=u?.time_format||"system",{data:f,isLoading:m,refetch:p,isRefetching:b}=ut({queryKey:["notification-logs",r,l],queryFn:()=>he.getNotificationLogs({days:r,limit:100,success:l?!1:void 0})}),{data:y}=ut({queryKey:["notification-log-stats",r],queryFn:()=>he.getNotificationLogStats(r)}),x=mt({mutationFn:()=>he.clearNotificationLogs(30),onSuccess:S=>{n(S.message,"success"),e.invalidateQueries({queryKey:["notification-logs"]}),e.invalidateQueries({queryKey:["notification-log-stats"]})},onError:S=>{n(`Failed to clear logs: ${S.message}`,"error")}}),v=S=>{const M=Ni(S);if(!M)return"";const T=new Date().getTime()-M.getTime();return T<6e4?"Just now":T<36e5?`${Math.floor(T/6e4)}m ago`:T<864e5?`${Math.floor(T/36e5)}h ago`:M.toLocaleDateString()+" "+Yme(M,d)};return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:s.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:[s.jsxs("div",{className:"p-4 border-b border-bambu-dark-tertiary flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx(P0,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Notification Log"})]}),s.jsx("button",{onClick:t,className:"text-bambu-gray hover:text-white transition-colors",children:"×"})]}),y&&s.jsx("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary bg-bambu-dark/50",children:s.jsxs("div",{className:"flex items-center gap-6 text-sm",children:[s.jsxs("span",{className:"text-bambu-gray",children:["Last ",r," days: ",s.jsx("span",{className:"text-white font-medium",children:y.total})," notifications"]}),s.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[s.jsx(Un,{className:"w-4 h-4"}),y.success_count," sent"]}),y.failure_count>0&&s.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[s.jsx(ia,{className:"w-4 h-4"}),y.failure_count," failed"]})]})}),s.jsxs("div",{className:"px-4 py-3 border-b border-bambu-dark-tertiary flex items-center gap-4",children:[s.jsxs("select",{value:r,onChange:S=>i(Number(S.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:[s.jsx("option",{value:1,children:"Last 24 hours"}),s.jsx("option",{value:7,children:"Last 7 days"}),s.jsx("option",{value:30,children:"Last 30 days"}),s.jsx("option",{value:90,children:"Last 90 days"})]}),s.jsxs("label",{className:"flex items-center gap-2 text-sm text-bambu-gray cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:l,onChange:S=>c(S.target.checked),className:"rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),"Show failed only"]}),s.jsx("div",{className:"flex-1"}),s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>p(),disabled:b,children:[b?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Refresh"]}),s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>x.mutate(),disabled:x.isPending,className:"text-red-400 hover:text-red-300",children:[x.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"}),"Clear Old"]})]}),s.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:m?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):f&&f.length>0?s.jsx("div",{className:"space-y-2",children:f.map(S=>s.jsx(FYe,{log:S,isExpanded:a===S.id,onToggle:()=>o(a===S.id?null:S.id),formatDate:v,formatFullDate:M=>wg(M,d)},S.id))}):s.jsxs("div",{className:"text-center py-12 text-bambu-gray",children:[s.jsx(P0,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),s.jsx("p",{className:"text-sm",children:l?"No failed notifications":"No notifications logged"})]})})]})})}function FYe({log:t,isExpanded:e,onToggle:n,formatDate:r,formatFullDate:i}){return s.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:[s.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?s.jsx(Un,{className:"w-4 h-4 text-bambu-green shrink-0"}):s.jsx(ia,{className:"w-4 h-4 text-red-400 shrink-0"}),s.jsx("span",{className:`text-xs font-medium ${LYe[t.event_type]||"text-bambu-gray"}`,children:OYe[t.event_type]||t.event_type}),s.jsx("span",{className:"text-sm text-white truncate flex-1",children:t.provider_name||"Unknown Provider"}),t.printer_name&&s.jsx("span",{className:"text-xs text-bambu-gray",children:t.printer_name}),s.jsx("span",{className:"text-xs text-bambu-gray shrink-0",children:r(t.created_at)}),e?s.jsx(pu,{className:"w-4 h-4 text-bambu-gray shrink-0"}):s.jsx(Oi,{className:"w-4 h-4 text-bambu-gray shrink-0"})]}),e&&s.jsxs("div",{className:"px-3 py-2 border-t border-bambu-dark-tertiary bg-bambu-dark/20 space-y-2",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Title"}),s.jsx("p",{className:"text-sm text-white",children:t.title})]}),s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-bambu-gray mb-1",children:"Message"}),s.jsx("p",{className:"text-sm text-white whitespace-pre-wrap",children:t.message})]}),!t.success&&t.error_message&&s.jsxs("div",{children:[s.jsx("p",{className:"text-xs text-red-400 mb-1",children:"Error"}),s.jsx("p",{className:"text-sm text-red-300",children:t.error_message})]}),s.jsxs("div",{className:"flex gap-4 text-xs text-bambu-gray pt-1",children:[s.jsxs("span",{children:["Provider: ",t.provider_type]}),s.jsxs("span",{children:["Time: ",i(t.created_at)]})]})]})]})}async function UYe(){const t=await fetch("/api/v1/settings/spoolman");if(!t.ok)throw new Error("Failed to load Spoolman settings");return t.json()}async function zYe(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 BYe(){const t=on(),[e,n]=w.useState(!1),[r,i]=w.useState(""),[a,o]=w.useState("auto"),[l,c]=w.useState(!1),[u,d]=w.useState("all"),[f,m]=w.useState(!1),[p,b]=w.useState(!1),{data:y,isLoading:x}=ut({queryKey:["spoolman-settings"],queryFn:UYe}),{data:v,isLoading:S,refetch:M}=ut({queryKey:["spoolman-status"],queryFn:he.getSpoolmanStatus,refetchInterval:3e4}),{data:A}=ut({queryKey:["printers"],queryFn:he.getPrinters});w.useEffect(()=>{y&&(n(y.spoolman_enabled==="true"),i(y.spoolman_url||""),o(y.spoolman_sync_mode||"auto"),m(!0))},[y]),w.useEffect(()=>{if(!f||!y)return;if(y.spoolman_enabled==="true"!==e||(y.spoolman_url||"")!==r||(y.spoolman_sync_mode||"auto")!==a){const W=setTimeout(()=>{T.mutate()},500);return()=>clearTimeout(W)}},[e,r,a,f]);const T=mt({mutationFn:()=>zYe({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)}}),k=mt({mutationFn:he.connectSpoolman,onSuccess:()=>{M()}}),E=mt({mutationFn:he.disconnectSpoolman,onSuccess:()=>{M()}}),O=mt({mutationFn:he.syncAllPrintersAms,onSuccess:P=>{P.success}}),C=mt({mutationFn:P=>he.syncPrinterAms(P),onSuccess:P=>{P.success}}),D=()=>{u==="all"?O.mutate():C.mutate(u)},U=O.isPending||C.isPending,L=u==="all"?O.data:C.data,q=u==="all"?O.isSuccess:C.isSuccess;return x?s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(su,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]})}),s.jsx(Nt,{children:s.jsx("div",{className:"flex justify-center py-8",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})})})]}):s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(su,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Spoolman Integration"})]}),T.isPending&&s.jsx(dt,{className:"w-4 h-4 text-bambu-green animate-spin"}),l&&s.jsx(ns,{className:"w-4 h-4 text-bambu-green"})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Connect to Spoolman for filament inventory tracking. AMS data will sync automatically."}),s.jsx("div",{className:"p-3 bg-blue-500/10 border border-blue-500/30 rounded-lg",children:s.jsxs("div",{className:"flex gap-2",children:[s.jsx(Ql,{className:"w-4 h-4 text-blue-400 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-xs text-blue-300",children:[s.jsx("p",{className:"font-medium mb-1",children:"How Sync Works"}),s.jsxs("ul",{className:"list-disc list-inside space-y-0.5 text-blue-300/80",children:[s.jsx("li",{children:"Only official Bambu Lab spools with RFID are synced"}),s.jsx("li",{children:"New spools are auto-created in Spoolman on first sync"}),s.jsx("li",{children:"Non-Bambu Lab spools (third-party, refilled) are skipped"})]}),s.jsx("p",{className:"font-medium mt-2 mb-1",children:"Linking Existing Spools"}),s.jsx("p",{className:"text-blue-300/80",children:'To link existing Spoolman spools to your AMS, hover over an AMS slot and click "Link to Spoolman".'})]})]})}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable Spoolman"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Sync filament data with Spoolman server"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:e,onChange:P=>n(P.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Spoolman URL"}),s.jsx("input",{type:"text",placeholder:"http://192.168.1.100:7912",value:r,onChange:P=>i(P.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"URL of your Spoolman server (e.g., http://localhost:7912)"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Sync Mode"}),s.jsxs("select",{value:a,onChange:P=>o(P.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:[s.jsx("option",{value:"auto",children:"Automatic"}),s.jsx("option",{value:"manual",children:"Manual Only"})]}),s.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&&s.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between mb-3",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Status:"}),S?s.jsx(dt,{className:"w-4 h-4 text-bambu-gray animate-spin"}):v?.connected?s.jsxs("span",{className:"flex items-center gap-1 text-sm text-green-500",children:[s.jsx(ns,{className:"w-4 h-4"}),"Connected"]}):s.jsxs("span",{className:"flex items-center gap-1 text-sm text-red-500",children:[s.jsx(Vt,{className:"w-4 h-4"}),"Disconnected"]})]}),s.jsx("div",{className:"flex gap-2",children:v?.connected?s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>E.mutate(),disabled:E.isPending,children:[E.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Ihe,{className:"w-4 h-4"}),"Disconnect"]}):s.jsxs(Se,{size:"sm",onClick:()=>k.mutate(),disabled:k.isPending||!r,children:[k.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Wl,{className:"w-4 h-4"}),"Connect"]})})]}),k.isError&&s.jsx("div",{className:"mb-3 p-2 bg-red-500/20 border border-red-500/50 rounded text-sm text-red-400",children:k.error.message}),v?.connected&&s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-white",children:"Sync AMS Data"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Manually sync printer AMS data to Spoolman"})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsxs("select",{value:u,onChange:P=>d(P.target.value==="all"?"all":Number(P.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:[s.jsx("option",{value:"all",children:"All Printers"}),A?.map(P=>s.jsx("option",{value:P.id,children:P.name},P.id))]}),s.jsx(Oi,{className:"absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:D,disabled:U,children:[U?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Sync"]})]})]}),q&&L&&s.jsxs("div",{className:"mt-3 space-y-2",children:[s.jsx("div",{className:`p-2 rounded text-sm ${L.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:L.success?`Synced ${L.synced_count} spool${L.synced_count!==1?"s":""} successfully`:`Synced ${L.synced_count} spool${L.synced_count!==1?"s":""} with ${L.errors.length} error${L.errors.length!==1?"s":""}`}),L.skipped_count>0&&s.jsxs("div",{className:"p-2 bg-amber-500/10 border border-amber-500/30 rounded text-sm",children:[s.jsxs("div",{className:"flex items-center justify-between text-amber-400 mb-1",children:[s.jsxs("div",{className:"flex items-center gap-1.5",children:[s.jsx(kn,{className:"w-3.5 h-3.5"}),s.jsxs("span",{className:"font-medium",children:[L.skipped_count," spool",L.skipped_count!==1?"s":""," skipped"]})]}),L.skipped_count>5&&s.jsx("button",{onClick:()=>b(!p),className:"text-xs text-amber-400 hover:text-amber-300 underline",children:p?"Show less":"Show all"})]}),s.jsxs("ul",{className:"text-xs text-amber-300/80 space-y-0.5",children:[(p?L.skipped:L.skipped.slice(0,5)).map((P,W)=>s.jsxs("li",{className:"flex items-center gap-2",children:[P.color&&s.jsx("span",{className:"w-3 h-3 rounded-full border border-white/20",style:{backgroundColor:`#${P.color}`}}),s.jsx("span",{children:P.location}),s.jsxs("span",{className:"text-amber-300/60",children:["- ",P.reason]})]},W)),!p&&L.skipped_count>5&&s.jsxs("li",{className:"text-amber-300/60 italic",children:["...and ",L.skipped_count-5," more"]})]})]}),L.errors.length>0&&s.jsxs("div",{className:"p-2 bg-red-500/10 border border-red-500/30 rounded text-sm",children:[s.jsx("div",{className:"text-red-400 font-medium mb-1",children:"Errors:"}),s.jsx("ul",{className:"text-xs text-red-300/80 space-y-0.5",children:L.errors.map((P,W)=>s.jsx("li",{children:P},W))})]})]})]})]})]})}function $Ye({link:t,onClose:e}){const n=on(),{mode:r}=hh(),i=!!t,a=w.useRef(null),[o,l]=w.useState(t?.name||""),[c,u]=w.useState(t?.url||""),[d,f]=w.useState(t?.icon||"link"),[m,p]=w.useState(!!t?.custom_icon),[b,y]=w.useState(t?.custom_icon?he.getExternalLinkIconUrl(t.id):null),[x,v]=w.useState(null),[S,M]=w.useState(null);w.useEffect(()=>{const U=L=>{L.key==="Escape"&&e()};return window.addEventListener("keydown",U),()=>window.removeEventListener("keydown",U)},[e]);const A=mt({mutationFn:async U=>{const L=await he.createExternalLink(U);return x?await he.uploadExternalLinkIcon(L.id,x):L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:U=>{M(U.message)}}),T=mt({mutationFn:async U=>{let L=await he.updateExternalLink(t.id,U);return x?L=await he.uploadExternalLinkIcon(t.id,x):!m&&t?.custom_icon&&(L=await he.deleteExternalLinkIcon(t.id)),L},onSuccess:()=>{n.invalidateQueries({queryKey:["external-links"]}),e()},onError:U=>{M(U.message)}}),k=U=>{const L=U.target.files?.[0];if(L){if(!["image/png","image/jpeg","image/gif","image/svg+xml","image/webp","image/x-icon"].includes(L.type)){M("Please select a valid image file (PNG, JPG, GIF, SVG, WebP, or ICO)");return}if(L.size>1024*1024){M("Image file must be less than 1MB");return}v(L),p(!0);const P=new FileReader;P.onload=W=>{y(W.target?.result)},P.readAsDataURL(L)}},E=()=>{v(null),y(null),p(!1),a.current&&(a.current.value="")},O=U=>{if(U.preventDefault(),M(null),!o.trim()){M("Name is required");return}if(!c.trim()){M("URL is required");return}if(!c.startsWith("http://")&&!c.startsWith("https://")){M("URL must start with http:// or https://");return}const L={name:o.trim(),url:c.trim(),icon:d};i?T.mutate(L):A.mutate(L)},C=A.isPending||T.isPending,D=gN(d);return s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4",onClick:e,children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-md",onClick:U=>U.stopPropagation(),children:[s.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:m&&b?s.jsx("img",{src:b,alt:"",className:`w-5 h-5 rounded ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}):s.jsx(D,{className:"w-5 h-5"})}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:i?"Edit Link":"Add External Link"})]}),s.jsx("button",{onClick:e,className:"text-bambu-gray hover:text-white transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:O,className:"p-6 space-y-4",children:[S&&s.jsx("div",{className:"p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400",children:S}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Name *"}),s.jsx("input",{type:"text",value:o,onChange:U=>l(U.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"URL *"}),s.jsx("input",{type:"text",value:c,onChange:U=>u(U.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"})]}),s.jsxs("div",{className:"space-y-3",children:[s.jsx("label",{className:"block text-sm text-bambu-gray",children:"Icon"}),s.jsxs("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm text-white",children:"Custom Icon"}),s.jsx("input",{ref:a,type:"file",accept:"image/png,image/jpeg,image/gif,image/svg+xml,image/webp,image/x-icon",className:"hidden",onChange:k}),m&&b?s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("img",{src:b,alt:"Custom icon",className:`w-8 h-8 rounded border border-bambu-dark-tertiary ${r==="dark"?"invert opacity-[0.65]":"opacity-60"}`}),s.jsx("button",{type:"button",onClick:E,className:"p-1 text-red-400 hover:text-red-300 transition-colors",title:"Remove custom icon",children:s.jsx(dn,{className:"w-4 h-4"})})]}):s.jsxs(Se,{type:"button",variant:"secondary",size:"sm",onClick:()=>a.current?.click(),children:[s.jsx(pi,{className:"w-4 h-4"}),"Upload"]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"PNG, JPG, GIF, SVG, WebP, or ICO. Max 1MB."})]}),!m&&s.jsxs("div",{children:[s.jsx("span",{className:"text-sm text-bambu-gray block mb-2",children:"Or choose a preset icon"}),s.jsx(Vme,{value:d,onChange:f})]})]}),s.jsxs("div",{className:"flex gap-3 pt-2",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:e,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:C,className:"flex-1",children:[C?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(_s,{className:"w-4 h-4"}),i?"Save":"Add"]})]})]})]})})}function HYe(){const t=on(),[e,n]=w.useState(!1),[r,i]=w.useState(null),[a,o]=w.useState(null),[l,c]=w.useState(null),{data:u,isLoading:d}=ut({queryKey:["external-links"],queryFn:he.getExternalLinks}),f=mt({mutationFn:S=>he.deleteExternalLink(S),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),m=mt({mutationFn:S=>he.reorderExternalLinks(S),onSuccess:()=>{t.invalidateQueries({queryKey:["external-links"]})}}),p=(S,M)=>{c(M),S.dataTransfer.effectAllowed="move"},b=S=>{S.preventDefault(),S.dataTransfer.dropEffect="move"},y=(S,M)=>{if(S.preventDefault(),l===null||l===M||!u)return;const A=u.map(O=>O.id),T=A.indexOf(l),k=A.indexOf(M);if(T===-1||k===-1)return;const E=[...A];E.splice(T,1),E.splice(k,0,l),m.mutate(E),c(null)},x=S=>{o(S)},v=()=>{a&&(f.mutate(a.id),o(null))};return s.jsxs(s.Fragment,{children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Wl,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Sidebar Links"})]}),s.jsxs(Se,{size:"sm",onClick:()=>n(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Link"]})]})}),s.jsxs(Nt,{children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Add external links to the sidebar navigation. Drag to reorder."}),d?s.jsx("div",{className:"flex justify-center py-8",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):u&&u.length>0?s.jsx("div",{className:"space-y-2",children:u.map(S=>{const M=gN(S.icon);return s.jsxs("div",{draggable:!0,onDragStart:A=>p(A,S.id),onDragOver:b,onDrop:A=>y(A,S.id),className:`flex items-center gap-3 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary transition-colors ${l===S.id?"opacity-50":""}`,children:[s.jsx(yg,{className:"w-6 h-6 md:w-4 md:h-4 text-bambu-gray cursor-grab flex-shrink-0"}),s.jsx("div",{className:"p-2 rounded-lg bg-bambu-dark-tertiary text-bambu-gray",children:s.jsx(M,{className:"w-4 h-4"})}),s.jsxs("div",{className:"flex-1 min-w-0",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-white font-medium truncate",children:S.name}),s.jsx(zr,{className:"w-3 h-3 text-bambu-gray flex-shrink-0"})]}),s.jsx("span",{className:"text-sm text-bambu-gray truncate block",children:S.url})]}),s.jsxs("div",{className:"flex items-center gap-1 flex-shrink-0",children:[s.jsx("button",{onClick:()=>i(S),className:"p-2 rounded-lg hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors",title:"Edit",children:s.jsx(la,{className:"w-4 h-4"})}),s.jsx("button",{onClick:()=>x(S),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:s.jsx(dn,{className:"w-4 h-4"})})]})]},S.id)})}):s.jsxs("div",{className:"text-center py-8 text-bambu-gray",children:[s.jsx(Wl,{className:"w-8 h-8 mx-auto mb-2 opacity-50"}),s.jsx("p",{children:"No external links configured"}),s.jsx("p",{className:"text-sm",children:'Click "Add Link" to add one'})]})]})]}),(e||r)&&s.jsx($Ye,{link:r,onClose:()=>{n(!1),i(null)}}),a&&s.jsx(Hn,{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:v,onCancel:()=>o(null)})]})}function VYe(){const t=on(),{showToast:e}=qn(),[n,r]=w.useState(!1),[i,a]=w.useState(""),[o,l]=w.useState("immediate"),[c,u]=w.useState("3DPrinter-X1-Carbon"),[d,f]=w.useState(!1),[m,p]=w.useState(null),{data:b,isLoading:y}=ut({queryKey:["virtual-printer-settings"],queryFn:r1.getSettings,refetchInterval:1e4}),{data:x}=ut({queryKey:["virtual-printer-models"],queryFn:r1.getModels});w.useEffect(()=>{if(b){r(b.enabled);let O=b.mode==="queue"?"review":b.mode;O!=="immediate"&&O!=="review"&&O!=="print_queue"&&(O="immediate"),l(O),u(b.model)}},[b]);const v=mt({mutationFn:O=>r1.updateSettings(O),onSuccess:()=>{t.invalidateQueries({queryKey:["virtual-printer-settings"]}),e("Virtual printer settings updated"),p(null)},onError:O=>{if(e(O.message||"Failed to update settings","error"),b){r(b.enabled);const C=b.mode==="queue"?"review":b.mode;l(C==="print_queue"||C==="review"?C:"immediate"),u(b.model)}p(null)}}),S=()=>{const O=!n;if(O&&!i&&!b?.access_code_set){e("Please set an access code first","error");return}r(O),p("toggle"),v.mutate({enabled:O,access_code:i||void 0,mode:o})},M=()=>{if(!i){e("Access code cannot be empty","error");return}if(i.length!==8){e("Access code must be exactly 8 characters","error");return}p("accessCode"),v.mutate({access_code:i}),a("")},A=O=>{l(O),p("mode"),v.mutate({mode:O})},T=O=>{u(O),p("model"),v.mutate({model:O})};if(y)return s.jsx(St,{children:s.jsx(Nt,{className:"py-8 flex justify-center",children:s.jsx(dt,{className:"w-6 h-6 animate-spin text-bambu-green"})})});const k=b?.status,E=k?.running||!1;return s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[s.jsx("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(qr,{className:"w-5 h-5 text-bambu-green"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Virtual Printer"})]}),k&&s.jsxs("div",{className:`flex items-center gap-2 text-sm ${E?"text-green-400":"text-bambu-gray"}`,children:[s.jsx("span",{className:`w-2 h-2 rounded-full ${E?"bg-green-400 animate-pulse":"bg-gray-500"}`}),E?"Running":"Stopped"]})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.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."}),s.jsxs("div",{className:"flex items-center justify-between py-3 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("div",{className:"text-white font-medium",children:"Enable Virtual Printer"}),s.jsx("div",{className:"text-sm text-bambu-gray",children:E?'Visible as "Bambuddy" in slicer discovery':"Not visible to slicers"})]}),s.jsx("button",{onClick:S,disabled:m==="toggle",className:`relative w-12 h-6 rounded-full transition-colors ${n?"bg-bambu-green":"bg-bambu-dark-tertiary"} ${m==="toggle"?"opacity-50":""}`,children:s.jsx("span",{className:`absolute top-1 left-1 w-4 h-4 bg-white rounded-full transition-transform ${n?"translate-x-6":""}`})})]}),s.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[s.jsx("div",{className:"text-white font-medium mb-2",children:"Printer Model"}),s.jsx("div",{className:"text-sm text-bambu-gray mb-3",children:"Select which printer model to emulate."}),s.jsxs("div",{className:"relative",children:[s.jsx("select",{value:c,onChange:O=>T(O.target.value),disabled:m==="model",className:"w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-2 text-white appearance-none cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed pr-10",children:x?.models&&Object.entries(x.models).sort(([,O],[,C])=>O.localeCompare(C)).map(([O,C])=>s.jsx("option",{value:O,children:C},O))}),s.jsx(Oi,{className:"absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none"})]}),n&&E&&s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:[s.jsx(Ql,{className:"w-3 h-3 inline mr-1"}),"Changing the model will restart the virtual printer"]})]}),s.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[s.jsx("div",{className:"text-white font-medium mb-2",children:"Access Code"}),s.jsx("div",{className:"text-sm text-bambu-gray mb-3",children:b?.access_code_set?s.jsxs("span",{className:"flex items-center gap-1 text-green-400",children:[s.jsx(ns,{className:"w-4 h-4"}),"Access code is set"]}):s.jsxs("span",{className:"flex items-center gap-1 text-yellow-400",children:[s.jsx(kn,{className:"w-4 h-4"}),"No access code set - required to enable"]})}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("div",{className:"relative flex-1",children:[s.jsx("input",{type:d?"text":"password",value:i,onChange:O=>a(O.target.value),placeholder:b?.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"}),s.jsx("button",{onClick:()=>f(!d),className:"absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:d?s.jsx(My,{className:"w-4 h-4"}):s.jsx(Qd,{className:"w-4 h-4"})})]}),s.jsx(Se,{onClick:M,disabled:!i||m==="accessCode",variant:"primary",children:m==="accessCode"?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):"Save"})]}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["Must be exactly 8 characters. Used by slicers to authenticate.",i&&s.jsxs("span",{className:i.length===8?"text-green-400":"text-yellow-400",children:[" ","(",i.length,"/8)"]})]})]}),s.jsxs("div",{className:"py-3 border-t border-bambu-dark-tertiary",children:[s.jsx("div",{className:"text-white font-medium mb-2",children:"Mode"}),s.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[s.jsxs("button",{onClick:()=>A("immediate"),disabled:m==="mode",className:`p-3 rounded-lg border text-left transition-colors ${o==="immediate"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[s.jsx("div",{className:"text-white font-medium",children:"Archive"}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Archive files immediately"})]}),s.jsxs("button",{onClick:()=>A("review"),disabled:m==="mode",className:`p-3 rounded-lg border text-left transition-colors ${o==="review"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[s.jsx("div",{className:"text-white font-medium",children:"Review"}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Review and tag before archiving"})]}),s.jsxs("button",{onClick:()=>A("print_queue"),disabled:m==="mode",className:`p-3 rounded-lg border text-left transition-colors ${o==="print_queue"?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:[s.jsx("div",{className:"text-white font-medium",children:"Queue"}),s.jsx("div",{className:"text-xs text-bambu-gray",children:"Archive and add to print queue"})]})]})]})]})]})}),s.jsxs("div",{className:"space-y-6 lg:w-[480px] lg:flex-shrink-0",children:[s.jsx(St,{className:"border-l-4 border-l-yellow-500",children:s.jsx(Nt,{className:"py-4",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(kn,{className:"w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-sm",children:[s.jsx("p",{className:"text-white font-medium mb-2",children:"Setup Required"}),s.jsx("p",{className:"text-bambu-gray mb-3",children:"The virtual printer feature requires additional system configuration before it will work. This includes port forwarding, firewall rules, and platform-specific settings."}),s.jsxs("a",{href:"https://wiki.bambuddy.cool/features/virtual-printer/",target:"_blank",rel:"noopener noreferrer",className:"inline-flex items-center gap-2 px-4 py-2 bg-yellow-500/20 border border-yellow-500/50 rounded-md text-yellow-400 hover:bg-yellow-500/30 transition-colors",children:[s.jsx(zr,{className:"w-4 h-4"}),"Read the setup guide before enabling"]})]})]})})}),s.jsx(St,{children:s.jsx(Nt,{className:"py-4",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(Ql,{className:"w-5 h-5 text-blue-400 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-sm text-bambu-gray",children:[s.jsx("p",{className:"mb-2",children:s.jsx("strong",{className:"text-white",children:"How it works:"})}),s.jsxs("ol",{className:"list-decimal list-inside space-y-1",children:[s.jsx("li",{children:"Complete the setup guide for your platform"}),s.jsx("li",{children:"Enable the virtual printer and set an access code"}),s.jsx("li",{children:'In Bambu Studio or OrcaSlicer, go to "Add Printer"'}),s.jsx("li",{children:'The "Bambuddy" printer should appear in the discovery list'}),s.jsx("li",{children:"Connect using the access code you set"}),s.jsx("li",{children:'When you "print" to Bambuddy, the 3MF file is archived instead'})]})]})]})})}),k&&E&&s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h3",{className:"text-md font-semibold text-white",children:"Status Details"})}),s.jsx(Nt,{children:s.jsxs("div",{className:"grid grid-cols-2 gap-4 text-sm",children:[s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Printer Name"}),s.jsx("div",{className:"text-white",children:k.name})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Model"}),s.jsx("div",{className:"text-white",children:k.model_name||k.model})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Serial Number"}),s.jsx("div",{className:"text-white font-mono",children:k.serial})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Mode"}),s.jsx("div",{className:"text-white capitalize",children:k.mode})]}),s.jsxs("div",{children:[s.jsx("div",{className:"text-bambu-gray",children:"Pending Files"}),s.jsx("div",{className:"text-white",children:k.pending_files})]})]})})]})]})]})}const Jw=[{id:"settings",labelKey:"backup.categories.settings",defaultLabel:"App Settings",icon:s.jsx(mN,{className:"w-4 h-4"}),default:!0,description:"Language, theme, update preferences"},{id:"notifications",labelKey:"backup.categories.notifications",defaultLabel:"Notification Providers",icon:s.jsx(Ef,{className:"w-4 h-4"}),default:!0,description:"ntfy, Pushover, Discord, etc."},{id:"templates",labelKey:"backup.categories.templates",defaultLabel:"Notification Templates",icon:s.jsx(Za,{className:"w-4 h-4"}),default:!0,description:"Custom message templates"},{id:"smart_plugs",labelKey:"backup.categories.smartPlugs",defaultLabel:"Smart Plugs",icon:s.jsx(Ks,{className:"w-4 h-4"}),default:!0,description:"Tasmota plug configurations"},{id:"external_links",labelKey:"backup.categories.externalLinks",defaultLabel:"External Links",icon:s.jsx(Jg,{className:"w-4 h-4"}),default:!0,description:"Sidebar links to external services"},{id:"printers",labelKey:"backup.categories.printers",defaultLabel:"Printers",icon:s.jsx(qr,{className:"w-4 h-4"}),default:!1,description:"Printer info (access codes excluded)"},{id:"plate_calibration",labelKey:"backup.categories.plateCalibration",defaultLabel:"Plate Detection",icon:s.jsx(Hf,{className:"w-4 h-4"}),default:!1,description:"Empty plate reference images",requiresPrinters:!0},{id:"filaments",labelKey:"backup.categories.filaments",defaultLabel:"Filament Inventory",icon:s.jsx(HD,{className:"w-4 h-4"}),default:!1,description:"Filament types and costs"},{id:"maintenance",labelKey:"backup.categories.maintenance",defaultLabel:"Maintenance Types",icon:s.jsx(eh,{className:"w-4 h-4"}),default:!1,description:"Custom maintenance schedules"},{id:"archives",labelKey:"backup.categories.archives",defaultLabel:"Print Archives",icon:s.jsx(es,{className:"w-4 h-4"}),default:!1,description:"All print data + files (3MF, thumbnails, photos)"},{id:"projects",labelKey:"backup.categories.projects",defaultLabel:"Projects",icon:s.jsx(Ta,{className:"w-4 h-4"}),default:!1,description:"Projects, BOM items, and attachments"},{id:"pending_uploads",labelKey:"backup.categories.pendingUploads",defaultLabel:"Pending Uploads",icon:s.jsx(pi,{className:"w-4 h-4"}),default:!1,description:"Virtual printer uploads awaiting review"},{id:"api_keys",labelKey:"backup.categories.apiKeys",defaultLabel:"API Keys",icon:s.jsx(mf,{className:"w-4 h-4"}),default:!1,description:"Webhook API keys (new keys generated on import)"}];function GYe({onClose:t,onExport:e}){const{t:n}=Xg(),[r,i]=w.useState(()=>{const b={};return Jw.forEach(y=>{b[y.id]=y.default}),b}),[a,o]=w.useState(!1),[l,c]=w.useState(!1);w.useEffect(()=>{const b=y=>{y.key==="Escape"&&t()};return window.addEventListener("keydown",b),()=>window.removeEventListener("keydown",b)},[t]);const u=b=>{i(y=>({...y,[b]:!y[b]}))},d=()=>{const b={};Jw.forEach(y=>{b[y.id]=!0}),i(b)},f=()=>{const b={};Jw.forEach(y=>{b[y.id]=!1}),i(b)},m=Object.values(r).filter(Boolean).length,p=async()=>{c(!0);try{await e({...r,access_codes:a&&r.printers})}finally{c(!1)}};return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:l?void 0:t,children:s.jsx(St,{className:"w-full max-w-lg",onClick:b=>b.stopPropagation(),children:s.jsxs(Nt,{className:"p-0",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"p-2 rounded-full bg-bambu-green/20 text-bambu-green",children:s.jsx(Hr,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:n("backup.exportTitle",{defaultValue:"Export Backup"})}),s.jsx("p",{className:"text-sm text-bambu-gray",children:n("backup.selectCategories",{defaultValue:"Select data to include"})})]})]}),s.jsx("button",{onClick:t,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("div",{className:"flex gap-2 px-4 pt-4",children:[s.jsx("button",{onClick:d,disabled:l,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"})}),s.jsx("span",{className:"text-bambu-gray",children:"|"}),s.jsx("button",{onClick:f,disabled:l,className:"text-sm text-bambu-gray hover:text-white disabled:opacity-50 disabled:cursor-not-allowed",children:n("common.selectNone",{defaultValue:"Select None"})})]}),s.jsx("div",{className:`p-4 space-y-2 max-h-[400px] overflow-y-auto ${l?"opacity-50 pointer-events-none":""}`,children:Jw.map(b=>{const y=l||b.requiresPrinters&&!r.printers;return s.jsxs("label",{className:`flex items-center gap-3 p-3 rounded-lg transition-colors ${y?"cursor-not-allowed opacity-50":"cursor-pointer"} ${r[b.id]&&!y?"bg-bambu-green/10 border border-bambu-green/30":"bg-bambu-dark hover:bg-bambu-dark-tertiary border border-transparent"}`,children:[s.jsx("input",{type:"checkbox",checked:r[b.id]&&!y,onChange:()=>u(b.id),disabled:y,className:"w-4 h-4 rounded border-bambu-gray bg-bambu-dark text-bambu-green focus:ring-bambu-green focus:ring-offset-0"}),s.jsx("div",{className:`${r[b.id]&&!y?"text-bambu-green":"text-bambu-gray"}`,children:b.icon}),s.jsxs("div",{className:"flex-1",children:[s.jsx("div",{className:"text-white text-sm font-medium",children:n(b.labelKey,{defaultValue:b.defaultLabel})}),s.jsx("div",{className:"text-xs text-bambu-gray",children:b.requiresPrinters&&!r.printers?"Requires Printers to be selected":b.description})]})]},b.id)})}),r.archives&&s.jsx("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[s.jsx(es,{className:"w-4 h-4 text-yellow-500 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"text-yellow-200 dark:text-yellow-200 text-yellow-700",children:[s.jsx("span",{className:"font-medium",children:"ZIP file will be created."}),s.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&&s.jsxs("div",{className:"mx-4 mb-2 p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-start gap-2",children:[s.jsx(mf,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{children:[s.jsx("p",{className:"text-sm font-medium text-white",children:"Include Access Codes"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"For transferring to another machine"})]})]}),s.jsx(Gn,{checked:a,onChange:o})]}),a&&s.jsx("div",{className:"mt-2 p-2 rounded bg-orange-500/10 border border-orange-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-xs",children:[s.jsx(kn,{className:"w-3 h-3 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),s.jsx("span",{className:"text-orange-700 dark:text-orange-200",children:"Access codes will be included in plain text. Keep this backup file secure!"})]})})]}),s.jsxs("div",{className:"flex items-center justify-between p-4 border-t border-bambu-dark-tertiary",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:n("backup.selectedCount",{count:m,defaultValue:`${m} categories selected`})}),s.jsxs("div",{className:"flex gap-3",children:[s.jsx(Se,{variant:"secondary",onClick:t,disabled:l,children:n("common.cancel",{defaultValue:"Cancel"})}),s.jsx(Se,{onClick:p,disabled:m===0||l,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50 disabled:cursor-not-allowed min-w-[100px]",children:l?s.jsxs(s.Fragment,{children:[s.jsx(dt,{className:"w-4 h-4 mr-2 animate-spin"}),n("backup.exporting",{defaultValue:"Exporting..."})]}):s.jsxs(s.Fragment,{children:[s.jsx(Hr,{className:"w-4 h-4 mr-2"}),n("backup.export",{defaultValue:"Export"})]})})]})]})]})})})}const mq={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",api_keys:"API Keys"};function WYe({onClose:t,onRestore:e,onSuccess:n}){const[r,i]=w.useState("options"),[a,o]=w.useState(!1),[l,c]=w.useState(null),[u,d]=w.useState(null),[f,m]=w.useState(new Set),p=w.useRef(null);w.useEffect(()=>{const A=T=>{T.key==="Escape"&&r!=="restoring"&&(r==="result"&&u?.success&&n(),t())};return window.addEventListener("keydown",A),()=>window.removeEventListener("keydown",A)},[t,n,r,u]);const b=A=>{const T=A.target.files?.[0];T&&c(T)},y=async()=>{if(l){i("restoring");try{const A=await e(l,a);d(A),i("result")}catch{d({success:!1,message:"Failed to restore backup. Please check the file format."}),i("result")}}},x=()=>{u?.success&&n(),t()},v=A=>{m(T=>{const k=new Set(T);return k.has(A)?k.delete(A):k.add(A),k})},S=u?.restored?Object.values(u.restored).reduce((A,T)=>A+T,0)+(u.files_restored||0):0,M=u?.total_skipped||0;return s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onMouseDown:A=>{A.target===A.currentTarget&&r!=="restoring"&&t()},children:s.jsx(St,{className:"w-full max-w-lg",children:s.jsxs(Nt,{className:"p-0",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.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?s.jsx(Un,{className:"w-5 h-5"}):r==="result"&&!u?.success?s.jsx(kn,{className:"w-5 h-5"}):s.jsx(pi,{className:"w-5 h-5"})}),s.jsxs("div",{children:[s.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")]}),s.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"&&s.jsx("button",{onClick:x,className:"p-2 hover:bg-bambu-dark-tertiary rounded-lg transition-colors",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),r==="options"&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("input",{ref:p,type:"file",accept:".json,.zip",className:"hidden",onChange:b}),s.jsx("button",{type:"button",onClick:()=>p.current?.click(),className:`w-full p-4 border-2 border-dashed rounded-lg transition-colors ${l?"border-bambu-green bg-bambu-green/10":"border-bambu-dark-tertiary hover:border-bambu-gray"}`,children:l?s.jsxs("div",{className:"flex items-center justify-center gap-2 text-bambu-green",children:[s.jsx(Un,{className:"w-5 h-5"}),s.jsx("span",{className:"font-medium",children:l.name})]}):s.jsxs("div",{className:"flex flex-col items-center gap-2 text-bambu-gray",children:[s.jsx(pi,{className:"w-8 h-8"}),s.jsx("span",{children:"Click to select backup file (.json or .zip)"})]})})]}),s.jsx("div",{className:"p-3 rounded-lg bg-blue-500/10 border border-blue-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[s.jsx(kn,{className:"w-4 h-4 text-blue-500 dark:text-blue-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"text-blue-700 dark:text-blue-200",children:[s.jsx("p",{className:"font-medium mb-1",children:"How duplicate handling works:"}),s.jsxs("ul",{className:"text-blue-600 dark:text-blue-200/80 space-y-1 text-xs",children:[s.jsxs("li",{children:[s.jsx("strong",{children:"Printers"})," - matched by serial number"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Smart Plugs"})," - matched by IP address"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Notification Providers"})," - matched by name"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Filaments"})," - matched by name + type + brand"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Archives"})," - matched by content hash (always skipped)"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Pending Uploads"})," - matched by filename"]}),s.jsxs("li",{children:[s.jsx("strong",{children:"Settings & Templates"})," - always overwritten"]})]})]})]})}),s.jsx("div",{className:"p-3 rounded-lg bg-bambu-dark border border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsxs("p",{className:"text-white font-medium flex items-center gap-2",children:[a?s.jsx(xr,{className:"w-4 h-4 text-orange-400"}):s.jsx(R0,{className:"w-4 h-4 text-bambu-gray"}),a?"Replace existing data":"Keep existing data"]}),s.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"})]}),s.jsx(Gn,{checked:a,onChange:o})]})}),a&&s.jsx("div",{className:"p-3 rounded-lg bg-orange-500/10 border border-orange-500/30",children:s.jsxs("div",{className:"flex items-start gap-2 text-sm",children:[s.jsx(kn,{className:"w-4 h-4 text-orange-500 dark:text-orange-400 mt-0.5 flex-shrink-0"}),s.jsxs("div",{className:"text-orange-700 dark:text-orange-200",children:[s.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."]})]})})]}),s.jsxs("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:[s.jsx(Se,{type:"button",variant:"secondary",onClick:t,children:"Cancel"}),s.jsxs(Se,{type:"button",onClick:y,disabled:!l,className:"bg-bambu-green hover:bg-bambu-green-dark disabled:opacity-50",children:[s.jsx(pi,{className:"w-4 h-4 mr-2"}),"Restore"]})]})]}),r==="restoring"&&s.jsxs("div",{className:"p-8 flex flex-col items-center gap-4",children:[s.jsx(dt,{className:"w-12 h-12 text-bambu-green animate-spin"}),s.jsx("p",{className:"text-bambu-gray",children:"Processing backup file..."})]}),r==="result"&&u&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{className:"p-4 space-y-4 max-h-[400px] overflow-y-auto",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{className:"p-3 rounded-lg bg-bambu-green/10 border border-bambu-green/30",children:[s.jsx("div",{className:"text-2xl font-bold text-bambu-green",children:S}),s.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Restored"})]}),s.jsxs("div",{className:"p-3 rounded-lg bg-yellow-500/10 border border-yellow-500/30",children:[s.jsx("div",{className:"text-2xl font-bold text-yellow-500",children:M}),s.jsx("div",{className:"text-sm text-bambu-gray",children:"Items Skipped"})]})]}),u.restored&&Object.entries(u.restored).some(([,A])=>A>0)&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green"}),"Restored"]}),s.jsxs("div",{className:"space-y-1",children:[Object.entries(u.restored).filter(([,A])=>A>0).map(([A,T])=>s.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[s.jsx("span",{className:"text-white",children:mq[A]||A}),s.jsx("span",{className:"text-bambu-green font-medium",children:T})]},A)),(u.files_restored||0)>0&&s.jsxs("div",{className:"flex items-center justify-between text-sm p-2 rounded bg-bambu-dark",children:[s.jsx("span",{className:"text-white",children:"Files (3MF, thumbnails, etc.)"}),s.jsx("span",{className:"text-bambu-green font-medium",children:u.files_restored})]})]})]}),u.skipped&&Object.entries(u.skipped).some(([,A])=>A>0)&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(R0,{className:"w-4 h-4 text-yellow-500"}),"Skipped (already exist)"]}),s.jsx("div",{className:"space-y-1",children:Object.entries(u.skipped).filter(([,A])=>A>0).map(([A,T])=>{const k=u.skipped_details?.[A]||[],E=f.has(A);return s.jsxs("div",{children:[s.jsxs("button",{onClick:()=>k.length>0&&v(A),className:`w-full flex items-center justify-between text-sm p-2 rounded bg-bambu-dark ${k.length>0?"hover:bg-bambu-dark-tertiary cursor-pointer":""}`,children:[s.jsxs("span",{className:"text-white flex items-center gap-2",children:[mq[A]||A,k.length>0&&(E?s.jsx(pu,{className:"w-3 h-3"}):s.jsx(Oi,{className:"w-3 h-3"}))]}),s.jsx("span",{className:"text-yellow-500 font-medium",children:T})]}),E&&k.length>0&&s.jsxs("div",{className:"mt-1 ml-4 p-2 rounded bg-bambu-dark-tertiary text-xs text-bambu-gray space-y-1",children:[k.slice(0,10).map((O,C)=>s.jsx("div",{children:O},C)),k.length>10&&s.jsxs("div",{className:"text-bambu-gray/60",children:["...and ",k.length-10," more"]})]})]},A)})})]}),u.new_api_keys&&u.new_api_keys.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("h4",{className:"text-sm font-medium text-bambu-gray flex items-center gap-2",children:[s.jsx(kn,{className:"w-4 h-4 text-orange-500"}),"New API Keys Generated"]}),s.jsxs("div",{className:"p-3 rounded bg-orange-500/10 border border-orange-500/30",children:[s.jsx("p",{className:"text-xs text-orange-200 mb-2",children:"These keys are only shown once. Copy them now!"}),s.jsx("div",{className:"space-y-2",children:u.new_api_keys.map((A,T)=>s.jsxs("div",{className:"p-2 rounded bg-bambu-dark",children:[s.jsx("div",{className:"text-sm text-white font-medium mb-1",children:A.name}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("code",{className:"text-xs text-bambu-green bg-bambu-dark-tertiary px-2 py-1 rounded font-mono flex-1 break-all",children:A.key}),s.jsx("button",{onClick:()=>navigator.clipboard.writeText(A.key),className:"text-xs text-bambu-gray hover:text-white px-2 py-1 rounded bg-bambu-dark-tertiary",children:"Copy"})]})]},T))})]})]}),S===0&&M===0&&s.jsx("div",{className:"p-4 text-center text-bambu-gray",children:"No data was found to restore in the backup file."})]}),s.jsx("div",{className:"flex items-center justify-end gap-3 p-4 border-t border-bambu-dark-tertiary",children:s.jsx(Se,{onClick:x,children:"Close"})})]})]})})})}function pq({status:t}){if(!t)return null;const e={success:"bg-green-500/20 text-green-400",failed:"bg-red-500/20 text-red-400",skipped:"bg-yellow-500/20 text-yellow-400",running:"bg-blue-500/20 text-blue-400"},n={success:s.jsx(Un,{className:"w-3 h-3"}),failed:s.jsx(ia,{className:"w-3 h-3"}),skipped:s.jsx(R0,{className:"w-3 h-3"}),running:s.jsx(dt,{className:"w-3 h-3 animate-spin"})};return s.jsxs("span",{className:`inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium ${e[t]||"bg-gray-500/20 text-gray-400"}`,children:[n[t],t.charAt(0).toUpperCase()+t.slice(1)]})}function qYe(t){return t?new Date(t).toLocaleString():"-"}function gq(t){if(!t)return"-";const e=new Date(t),n=new Date,r=e.getTime()-n.getTime(),i=Math.round(r/6e4);if(i<0){const a=Math.abs(i);if(a<60)return`${a}m ago`;const o=Math.floor(a/60);return o<24?`${o}h ago`:`${Math.floor(o/24)}d ago`}else{if(i<60)return`in ${i}m`;const a=Math.floor(i/60);return a<24?`in ${a}h`:`in ${Math.floor(a/24)}d`}}function KYe(){const t=on(),{showToast:e}=qn(),[n,r]=w.useState(""),[i,a]=w.useState(""),[o,l]=w.useState("main"),[c,u]=w.useState(!1),[d,f]=w.useState(!1),[m,p]=w.useState("daily"),[b,y]=w.useState(!0),[x,v]=w.useState(!0),[S,M]=w.useState(!1),[A,T]=w.useState(!0),[k,E]=w.useState(!1),[O,C]=w.useState(!1),[D,U]=w.useState(!1),[L,q]=w.useState(null),P=w.useRef(null),W=w.useRef(!1),{data:K,isLoading:V}=ut({queryKey:["github-backup-config"],queryFn:he.getGitHubBackupConfig}),{data:Q}=ut({queryKey:["github-backup-status"],queryFn:he.getGitHubBackupStatus,refetchInterval:ie=>ie.state.data?.is_running?500:1e4}),{data:re}=ut({queryKey:["github-backup-logs"],queryFn:()=>he.getGitHubBackupLogs(20)}),{data:J}=ut({queryKey:["cloud-status"],queryFn:he.getCloudStatus});w.useEffect(()=>{K&&(r(K.repository_url),l(K.branch),f(K.schedule_enabled),p(K.schedule_type),y(K.backup_kprofiles),v(K.backup_cloud_profiles),M(K.backup_settings),T(K.enabled),a(""),setTimeout(()=>{W.current=!0},100))},[K]);const z=w.useCallback(async(ie=!1)=>{if(K?.has_token)try{ie&&i?(await he.saveGitHubBackupConfig({repository_url:n,access_token:i,branch:o,schedule_enabled:d,schedule_type:m,backup_kprofiles:b,backup_cloud_profiles:x,backup_settings:S,enabled:A}),a(""),e("Token updated")):(await he.updateGitHubBackupConfig({repository_url:n,branch:o,schedule_enabled:d,schedule_type:m,backup_kprofiles:b,backup_cloud_profiles:x,backup_settings:S,enabled:A}),e("Settings saved")),t.invalidateQueries({queryKey:["github-backup-config"]}),t.invalidateQueries({queryKey:["github-backup-status"]})}catch(Ae){e(`Failed to save: ${Ae.message}`,"error")}},[K?.has_token,n,i,o,d,m,b,x,S,A,t,e]);w.useEffect(()=>{if(!(!W.current||!K?.has_token))return P.current&&clearTimeout(P.current),P.current=setTimeout(()=>{z(!1)},500),()=>{P.current&&clearTimeout(P.current)}},[n,o,d,m,b,x,S,A,z,K?.has_token]),w.useEffect(()=>{if(!(!W.current||!K?.has_token||!i))return P.current&&clearTimeout(P.current),P.current=setTimeout(()=>{z(!0)},1e3),()=>{P.current&&clearTimeout(P.current)}},[i,z,K?.has_token]);const G=mt({mutationFn:ie=>he.saveGitHubBackupConfig(ie),onSuccess:()=>{t.invalidateQueries({queryKey:["github-backup-config"]}),t.invalidateQueries({queryKey:["github-backup-status"]}),e("GitHub backup enabled"),a(""),W.current=!0},onError:ie=>{e(`Failed to save: ${ie.message}`,"error")}}),ne=mt({mutationFn:he.triggerGitHubBackup,onSuccess:ie=>{t.invalidateQueries({queryKey:["github-backup-status"]}),t.invalidateQueries({queryKey:["github-backup-logs"]}),ie.success?ie.files_changed>0?e(`Backup complete - ${ie.files_changed} files updated`):e("Backup skipped - no changes"):e(`Backup failed: ${ie.message}`,"error")},onError:ie=>{e(`Backup failed: ${ie.message}`,"error")}}),ae=mt({mutationFn:()=>he.clearGitHubBackupLogs(10),onSuccess:ie=>{t.invalidateQueries({queryKey:["github-backup-logs"]}),e(`Cleared ${ie.deleted} logs`)},onError:ie=>{e(`Failed to clear logs: ${ie.message}`,"error")}}),de=async()=>{U(!0),q(null);try{let ie;if(i){if(!n){e("Enter repository URL","error"),U(!1);return}ie=await he.testGitHubConnection(n,i)}else if(K?.has_token)ie=await he.testGitHubStoredConnection();else{e("Enter repository URL and access token","error"),U(!1);return}q({success:ie.success,message:ie.message})}catch(ie){q({success:!1,message:ie.message})}finally{U(!1)}},fe=()=>{if(!n){e("Repository URL is required","error");return}if(!i){e("Access token is required","error");return}G.mutate({repository_url:n,access_token:i,branch:o,schedule_enabled:d,schedule_type:m,backup_kprofiles:b,backup_cloud_profiles:x,backup_settings:S,enabled:A})};return V?s.jsx("div",{className:"flex items-center justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 animate-spin text-bambu-green"})}):s.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[s.jsxs("div",{className:"space-y-6",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(bP,{className:"w-5 h-5 text-gray-400"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"GitHub Backup"})]}),K&&J?.is_authenticated&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-sm text-bambu-gray",children:"Enabled"}),s.jsx(Gn,{checked:A,onChange:T})]})]})}),s.jsx(Nt,{className:"space-y-4",children:J?.is_authenticated?s.jsxs(s.Fragment,{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically sync your profiles to a private GitHub repository for backup and version history."}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Repository URL"}),s.jsx("input",{type:"text",value:n,onChange:ie=>{r(ie.target.value),q(null)},placeholder:"https://github.com/username/bambuddy-backup",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"})]}),s.jsxs("div",{children:[s.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:["Personal Access Token ",K?.has_token&&s.jsx("span",{className:"text-green-400",children:"(saved)"})]}),s.jsxs("div",{className:"relative",children:[s.jsx("input",{type:c?"text":"password",value:i,onChange:ie=>{a(ie.target.value),q(null)},placeholder:K?.has_token?"Enter new token to update":"ghp_xxxxxxxxxxxx",className:"w-full px-3 py-2 pr-10 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"}),s.jsx("button",{type:"button",onClick:()=>u(!c),className:"absolute right-3 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white",children:c?s.jsx(My,{className:"w-4 h-4"}):s.jsx(Qd,{className:"w-4 h-4"})})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Fine-grained token with Contents read/write permission"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Branch"}),s.jsx("input",{type:"text",value:o,onChange:ie=>l(ie.target.value),placeholder:"main",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Auto-backup"}),s.jsxs("select",{value:d?m:"disabled",onChange:ie=>{ie.target.value==="disabled"?f(!1):(f(!0),p(ie.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:[s.jsx("option",{value:"disabled",children:"Manual only"}),s.jsx("option",{value:"hourly",children:"Hourly"}),s.jsx("option",{value:"daily",children:"Daily"}),s.jsx("option",{value:"weekly",children:"Weekly"})]})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"Include in backup"}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("label",{className:"flex items-start gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:b,onChange:ie=>y(ie.target.checked),className:"w-4 h-4 mt-0.5 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white text-sm",children:"K-Profiles"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Pressure advance calibration from connected printers"})]})]}),s.jsxs("label",{className:"flex items-start gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:x,onChange:ie=>v(ie.target.checked),className:"w-4 h-4 mt-0.5 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green",disabled:!J?.is_authenticated}),s.jsxs("div",{children:[s.jsx("span",{className:`text-sm ${J?.is_authenticated?"text-white":"text-bambu-gray"}`,children:"Cloud Profiles"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Filament, printer, and process presets from Bambu Cloud"})]})]}),s.jsxs("label",{className:"flex items-start gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:S,onChange:ie=>M(ie.target.checked),className:"w-4 h-4 mt-0.5 rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white text-sm",children:"App Settings"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Bambuddy configuration (excludes sensitive data)"})]})]})]})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4 space-y-3",children:[Q?.configured&&s.jsxs("div",{className:"flex items-center justify-between text-sm",children:[s.jsx("div",{className:"flex items-center gap-2 text-bambu-gray",children:Q.last_backup_at?s.jsxs(s.Fragment,{children:[s.jsxs("span",{children:["Last backup: ",gq(Q.last_backup_at)]}),s.jsx(pq,{status:Q.last_backup_status})]}):s.jsx("span",{children:"No backups yet"})}),Q.next_scheduled_run&&s.jsxs("span",{className:"text-bambu-gray",children:[s.jsx(Zn,{className:"w-3 h-3 inline mr-1"}),"Next: ",gq(Q.next_scheduled_run)]})]}),L&&s.jsxs("div",{className:`text-sm flex items-center gap-1 ${L.success?"text-green-400":"text-red-400"}`,children:[L.success?s.jsx(Un,{className:"w-4 h-4"}):s.jsx(ia,{className:"w-4 h-4"}),L.message]}),s.jsx("div",{className:"flex flex-wrap items-center gap-2",children:Q?.configured?s.jsx(s.Fragment,{children:ne.isPending||Q.is_running?s.jsxs("div",{className:"flex items-center gap-2 text-bambu-green",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),s.jsx("span",{className:"text-sm",children:Q.progress||"Starting backup..."})]}):s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"primary",size:"sm",onClick:()=>ne.mutate(),disabled:!K?.enabled,children:[s.jsx(Oo,{className:"w-4 h-4"}),"Backup Now"]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:de,disabled:D,children:[D?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Test"]})]})}):s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"primary",size:"sm",onClick:fe,disabled:G.isPending||!n||!i,children:[G.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Un,{className:"w-4 h-4"}),"Enable Backup"]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:de,disabled:D||!n||!i,children:[D?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Test Connection"]})]})})]})]}):s.jsxs("div",{className:"flex items-start gap-2 p-3 bg-yellow-500/10 border border-yellow-500/30 rounded-lg",children:[s.jsx(kn,{className:"w-4 h-4 text-yellow-400 mt-0.5 flex-shrink-0"}),s.jsx("p",{className:"text-sm text-yellow-400",children:"Bambu Cloud login required. Sign in under Profiles → Cloud Profiles to enable GitHub backup."})]})})]}),re&&re.length>0&&s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(P0,{className:"w-5 h-5 text-gray-400"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"History"})]}),s.jsxs(Se,{variant:"ghost",size:"sm",onClick:()=>ae.mutate(),disabled:ae.isPending,children:[s.jsx(dn,{className:"w-4 h-4"}),"Clear"]})]})}),s.jsx(Nt,{children:s.jsx("div",{className:"overflow-x-auto",children:s.jsxs("table",{className:"w-full text-sm",children:[s.jsx("thead",{children:s.jsxs("tr",{className:"text-bambu-gray border-b border-bambu-dark-tertiary",children:[s.jsx("th",{className:"text-left py-2 px-2",children:"Date"}),s.jsx("th",{className:"text-left py-2 px-2",children:"Status"}),s.jsx("th",{className:"text-left py-2 px-2",children:"Commit"})]})}),s.jsx("tbody",{children:re.slice(0,10).map(ie=>s.jsxs("tr",{className:"border-b border-bambu-dark-tertiary/50 hover:bg-bambu-dark-secondary",children:[s.jsx("td",{className:"py-2 px-2 text-white",children:qYe(ie.started_at)}),s.jsx("td",{className:"py-2 px-2",children:s.jsx(pq,{status:ie.status})}),s.jsx("td",{className:"py-2 px-2",children:ie.commit_sha?s.jsxs("a",{href:`${K?.repository_url}/commit/${ie.commit_sha}`,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-green hover:underline inline-flex items-center gap-1",children:[ie.commit_sha.substring(0,7),s.jsx(zr,{className:"w-3 h-3"})]}):s.jsx("span",{className:"text-bambu-gray",children:"-"})})]},ie.id))})]})})})]})]}),s.jsx("div",{className:"space-y-6",children:s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(su,{className:"w-5 h-5 text-gray-400"}),s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Local Backup"})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Export or import your Bambuddy data as a local file for manual backup or migration."}),s.jsxs("div",{className:"flex items-center justify-between py-3 border-b border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Export Data"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Download all settings, printers, and profiles"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>E(!0),children:[s.jsx(Hr,{className:"w-4 h-4"}),"Export"]})]}),s.jsxs("div",{className:"flex items-center justify-between py-3",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Import Backup"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Restore from a previous export file"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>C(!0),children:[s.jsx(pi,{className:"w-4 h-4"}),"Import"]})]})]})]})}),k&&s.jsx(GYe,{onClose:()=>E(!1),onExport:async ie=>{E(!1);try{const{blob:Ae,filename:me}=await he.exportBackup(ie),Ne=URL.createObjectURL(Ae),ye=document.createElement("a");ye.href=Ne,ye.download=me,ye.click(),URL.revokeObjectURL(Ne),e("Backup downloaded successfully")}catch{e("Failed to create backup","error")}}}),O&&s.jsx(WYe,{onClose:()=>C(!1),onRestore:async(ie,Ae)=>await he.importBackup(ie,Ae),onSuccess:()=>{C(!1),e("Backup restored successfully"),t.invalidateQueries()}})]})}const XYe={get:"bg-blue-500/20 text-blue-400 border-blue-500/30",post:"bg-green-500/20 text-green-400 border-green-500/30",put:"bg-yellow-500/20 text-yellow-400 border-yellow-500/30",patch:"bg-orange-500/20 text-orange-400 border-orange-500/30",delete:"bg-red-500/20 text-red-400 border-red-500/30"};function YYe(t,e){const n=e.replace("#/","").split("/");let r=t;for(const i of n)r=r[i];return r}function t0(t,e,n=0){if(n>5)return"...";if(e.$ref)return t0(t,YYe(t,e.$ref),n+1);if(e.allOf){const r={};for(const i of e.allOf){const a=t0(t,i,n+1);typeof a=="object"&&a!==null&&Object.assign(r,a)}return r}if(e.example!==void 0)return e.example;if(e.default!==void 0)return e.default;switch(e.type){case"string":return e.enum?e.enum[0]:"string";case"integer":case"number":return 0;case"boolean":return!1;case"array":return e.items?[t0(t,e.items,n+1)]:[];case"object":if(e.properties){const r={};for(const[i,a]of Object.entries(e.properties))r[i]=t0(t,a,n+1);return r}return{};default:return null}}function ZYe({path:t,method:e,spec:n,schema:r,apiKey:i}){const[a,o]=w.useState(!1),[l,c]=w.useState({}),[u,d]=w.useState(""),[f,m]=w.useState(null),[p,b]=w.useState(!1),[y,x]=w.useState(!1);w.useEffect(()=>{if(a&&n.parameters){const O={};for(const C of n.parameters)C.schema?.default!==void 0&&(O[C.name]=String(C.schema.default));c(C=>({...O,...C}))}},[a,n.parameters]),w.useEffect(()=>{if(a&&n.requestBody?.content?.["application/json"]?.schema&&!u){const O=n.requestBody.content["application/json"].schema,C=t0(r,O);d(JSON.stringify(C,null,2))}},[a,n.requestBody,r,u]);const S=(()=>{const O=[];for(const C of n.parameters||[])if(C.in==="path"||C.required){const D=l[C.name];(D===void 0||D==="")&&O.push(C.name)}return O})(),M=async()=>{if(S.length>0){m({status:0,statusText:"Validation Error",headers:{},body:`Missing required parameters: ${S.join(", ")}`,duration:0});return}b(!0),m(null);try{let O=t;const C=new URLSearchParams;for(const J of n.parameters||[]){const z=l[J.name];z!==void 0&&z!==""&&(J.in==="path"?O=O.replace(`{${J.name}}`,encodeURIComponent(z)):J.in==="query"&&C.append(J.name,z))}const D=C.toString(),U=`${O}${D?`?${D}`:""}`,L={"Content-Type":"application/json"};i&&(L["X-API-Key"]=i);const q={method:e.toUpperCase(),headers:L};["post","put","patch"].includes(e)&&u&&(q.body=u);const P=performance.now(),W=await fetch(U,q),K=Math.round(performance.now()-P),V={};W.headers.forEach((J,z)=>{V[z]=J});let Q;W.headers.get("content-type")?.includes("application/json")?Q=await W.json():Q=await W.text(),m({status:W.status,statusText:W.statusText,headers:V,body:Q,duration:K})}catch(O){m({status:0,statusText:"Network Error",headers:{},body:O instanceof Error?O.message:"Unknown error",duration:0})}finally{b(!1)}},A=async()=>{if(f){const O=typeof f.body=="string"?f.body:JSON.stringify(f.body,null,2);try{await navigator.clipboard.writeText(O),x(!0),setTimeout(()=>x(!1),2e3)}catch{const C=document.createElement("textarea");C.value=O,C.style.position="fixed",C.style.left="-999999px",document.body.appendChild(C),C.select(),document.execCommand("copy"),document.body.removeChild(C),x(!0),setTimeout(()=>x(!1),2e3)}}},T=(n.parameters||[]).filter(O=>O.in==="path"),k=(n.parameters||[]).filter(O=>O.in==="query"),E=["post","put","patch"].includes(e)&&n.requestBody;return s.jsxs("div",{className:"border border-bambu-dark-tertiary rounded-lg overflow-hidden",children:[s.jsxs("button",{onClick:()=>o(!a),className:"w-full flex items-center gap-3 p-3 hover:bg-bambu-dark-tertiary/50 transition-colors text-left",children:[a?s.jsx(Oi,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}):s.jsx(Js,{className:"w-4 h-4 text-bambu-gray flex-shrink-0"}),s.jsx("span",{className:`px-2 py-0.5 text-xs font-mono font-semibold uppercase rounded border ${XYe[e]||"bg-gray-500/20 text-gray-400"}`,children:e}),s.jsx("code",{className:"text-sm text-white font-mono flex-1 truncate",children:t}),n.summary&&s.jsx("span",{className:"text-sm text-bambu-gray truncate max-w-[40%]",children:n.summary})]}),a&&s.jsxs("div",{className:"border-t border-bambu-dark-tertiary p-4 space-y-4 bg-bambu-dark/50",children:[n.description&&s.jsx("p",{className:"text-sm text-bambu-gray",children:n.description}),T.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsx("h4",{className:"text-sm font-medium text-white",children:"Path Parameters"}),s.jsx("div",{className:"space-y-2",children:T.map(O=>s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("label",{className:"text-sm text-bambu-gray w-32 flex-shrink-0",children:[O.name,O.required&&s.jsx("span",{className:"text-red-400 ml-1",children:"*"})]}),s.jsx("input",{type:"text",value:l[O.name]||"",onChange:C=>c(D=>({...D,[O.name]:C.target.value})),placeholder:O.description||O.schema?.type||"value",className:"flex-1 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm font-mono focus:border-bambu-green focus:outline-none"})]},O.name))})]}),k.length>0&&s.jsxs("div",{className:"space-y-2",children:[s.jsx("h4",{className:"text-sm font-medium text-white",children:"Query Parameters"}),s.jsx("div",{className:"space-y-2",children:k.map(O=>s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("label",{className:"text-sm text-bambu-gray w-32 flex-shrink-0",children:[O.name,O.required&&s.jsx("span",{className:"text-red-400 ml-1",children:"*"})]}),O.schema?.enum?s.jsxs("select",{value:l[O.name]||"",onChange:C=>c(D=>({...D,[O.name]:C.target.value})),className:"flex-1 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"",children:"-- Select --"}),O.schema.enum.map(C=>s.jsx("option",{value:C,children:C},C))]}):s.jsx("input",{type:"text",value:l[O.name]||"",onChange:C=>c(D=>({...D,[O.name]:C.target.value})),placeholder:O.description||O.schema?.type||"value",className:"flex-1 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm font-mono focus:border-bambu-green focus:outline-none"})]},O.name))})]}),E&&s.jsxs("div",{className:"space-y-2",children:[s.jsx("h4",{className:"text-sm font-medium text-white",children:"Request Body"}),s.jsx("textarea",{value:u,onChange:O=>d(O.target.value),rows:8,className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm font-mono focus:border-bambu-green focus:outline-none resize-y",placeholder:"JSON request body..."})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs(Se,{onClick:M,disabled:p,children:[p?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Oo,{className:"w-4 h-4"}),"Execute"]}),S.length>0&&s.jsxs("span",{className:"text-xs text-yellow-400 flex items-center gap-1",children:[s.jsx(oa,{className:"w-3 h-3"}),"Fill in: ",S.join(", ")]})]}),f&&s.jsxs("div",{className:"space-y-2",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("h4",{className:"text-sm font-medium text-white flex items-center gap-2",children:["Response",s.jsxs("span",{className:`px-2 py-0.5 text-xs rounded ${f.status>=200&&f.status<300?"bg-green-500/20 text-green-400":f.status>=400?"bg-red-500/20 text-red-400":"bg-yellow-500/20 text-yellow-400"}`,children:[f.status," ",f.statusText]}),s.jsxs("span",{className:"text-xs text-bambu-gray",children:[f.duration,"ms"]})]}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:A,children:y?s.jsx(Un,{className:"w-3 h-3 text-green-400"}):s.jsx(ll,{className:"w-3 h-3"})})]}),s.jsx("pre",{className:"p-3 bg-bambu-dark rounded-lg text-sm font-mono text-white overflow-auto max-h-96 border border-bambu-dark-tertiary",children:typeof f.body=="string"?f.body:JSON.stringify(f.body,null,2)})]})]})]})}function QYe({apiKey:t=""}){const[e,n]=w.useState(null),[r,i]=w.useState(!0),[a,o]=w.useState(null),[l,c]=w.useState(new Set),[u,d]=w.useState("");if(w.useEffect(()=>{async function x(){try{const v=await fetch("/openapi.json");if(!v.ok)throw new Error("Failed to fetch OpenAPI schema");const S=await v.json();n(S)}catch(v){o(v instanceof Error?v.message:"Unknown error")}finally{i(!1)}}x()},[]),r)return s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})});if(a||!e)return s.jsx(St,{children:s.jsx(Nt,{className:"py-8",children:s.jsxs("div",{className:"text-center text-red-400",children:[s.jsx(oa,{className:"w-12 h-12 mx-auto mb-3 opacity-50"}),s.jsx("p",{children:"Failed to load API schema"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:a})]})})});const f={};for(const[x,v]of Object.entries(e.paths))for(const[S,M]of Object.entries(v)){if(S==="parameters")continue;const A=M.tags||["Other"];for(const T of A)f[T]||(f[T]=[]),f[T].push({path:x,method:S,spec:M})}const m=Object.entries(f).map(([x,v])=>{if(!u)return{tag:x,endpoints:v};const S=v.filter(({path:M,method:A,spec:T})=>{const k=u.toLowerCase();return M.toLowerCase().includes(k)||A.toLowerCase().includes(k)||(T.summary?.toLowerCase()||"").includes(k)||(T.description?.toLowerCase()||"").includes(k)});return{tag:x,endpoints:S}}).filter(({endpoints:x})=>x.length>0).sort((x,v)=>x.tag.localeCompare(v.tag)),p=x=>{c(v=>{const S=new Set(v);return S.has(x)?S.delete(x):S.add(x),S})},b=()=>{c(new Set(m.map(x=>x.tag)))},y=()=>{c(new Set)};return s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between gap-4",children:[s.jsx("div",{className:"flex-1",children:s.jsx("input",{type:"text",value:u,onChange:x=>d(x.target.value),placeholder:"Search endpoints...",className:"w-full max-w-md px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"})}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx(Se,{variant:"secondary",size:"sm",onClick:b,children:"Expand All"}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:y,children:"Collapse All"}),s.jsxs("a",{href:"/docs",target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-1 text-sm text-bambu-green hover:underline",children:[s.jsx(zr,{className:"w-4 h-4"}),"Swagger UI"]})]})]}),s.jsxs("p",{className:"text-sm text-bambu-gray",children:[m.reduce((x,v)=>x+v.endpoints.length,0)," endpoints in ",m.length," categories"]}),s.jsx("div",{className:"space-y-3",children:m.map(({tag:x,endpoints:v})=>s.jsxs(St,{children:[s.jsx("button",{onClick:()=>p(x),className:"w-full flex items-center justify-between p-4 hover:bg-bambu-dark-tertiary/30 transition-colors text-left",children:s.jsxs("div",{className:"flex items-center gap-2",children:[l.has(x)?s.jsx(Oi,{className:"w-5 h-5 text-bambu-gray"}):s.jsx(Js,{className:"w-5 h-5 text-bambu-gray"}),s.jsx("h3",{className:"text-base font-semibold text-white capitalize",children:x.replace(/-/g," ")}),s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-2 py-0.5 rounded-full text-bambu-gray",children:v.length})]})}),l.has(x)&&s.jsx(Nt,{className:"pt-0 space-y-2",children:v.map(({path:S,method:M,spec:A})=>s.jsx(ZYe,{path:S,method:M,spec:A,schema:e,apiKey:t},`${M}-${S}`))})]},x))})]})}const JYe=["general","network","plugs","notifications","filament","apikeys","virtual-printer","users","backup"];function eZe(){const t=on(),e=gl(),[n,r]=Xq(),{t:i,i18n:a}=Xg(),{showToast:o}=qn(),{authEnabled:l,user:c,refreshAuth:u}=fh(),{mode:d,darkStyle:f,darkBackground:m,darkAccent:p,lightStyle:b,lightBackground:y,lightAccent:x,setDarkStyle:v,setDarkBackground:S,setDarkAccent:M,setLightStyle:A,setLightBackground:T,setLightAccent:k}=hh(),[E,O]=w.useState(null),[C,D]=w.useState(!1),[U,L]=w.useState(null),[q,P]=w.useState(!1),[W,K]=w.useState(null),[V,Q]=w.useState(null),[re,J]=w.useState(!1),[z,G]=w.useState(OK()),ne=n.get("tab"),ae=ne&&JYe.includes(ne)?ne:"general",[de,fe]=w.useState(ae),ie=be=>{fe(be),be==="general"?n.delete("tab"):n.set("tab",be),r(n,{replace:!0})},[Ae,me]=w.useState(!1),[Ne,ye]=w.useState(""),[le,$e]=w.useState({can_queue:!0,can_control_printer:!1,can_read_status:!0}),[H,ee]=w.useState(null),[X,B]=w.useState(null),[I,ce]=w.useState(""),[Te,Le]=w.useState(!1),[Ee,$]=w.useState(!1),[j,Y]=w.useState(null),[ge,we]=w.useState(!1),[ve,Ze]=w.useState(!1),[Ie,qe]=w.useState(null),[Je,Qe]=w.useState(!1),[oe,te]=w.useState({}),[Me,Fe]=w.useState({}),ot=be=>{G(be),Kme(be)},se=()=>{localStorage.removeItem("sidebarOrder"),window.location.reload()},{data:je,isLoading:xe}=ut({queryKey:["settings"],queryFn:he.getSettings}),{data:Oe,isLoading:Ue}=ut({queryKey:["smart-plugs"],queryFn:he.getSmartPlugs}),{data:Ce,isLoading:We}=ut({queryKey:["smart-plugs-energy",Oe?.map(be=>be.id)],queryFn:async()=>{if(!Oe||Oe.length===0)return null;const be=await Promise.all(Oe.filter(Rt=>Rt.enabled).map(async Rt=>{try{const $t=await he.getSmartPlugStatus(Rt.id);return{plug:Rt,status:$t}}catch{return{plug:Rt,status:null}}}));let Pe=0,wt=0,Mt=0,lt=0,Tt=0;for(const{status:Rt}of be)Rt?.reachable&&Rt.energy&&(Tt++,Rt.energy.power!=null&&(Pe+=Rt.energy.power),Rt.energy.today!=null&&(wt+=Rt.energy.today),Rt.energy.yesterday!=null&&(Mt+=Rt.energy.yesterday),Rt.energy.total!=null&&(lt+=Rt.energy.total));return{totalPower:Pe,totalToday:wt,totalYesterday:Mt,totalLifetime:lt,reachableCount:Tt,totalPlugs:Oe.filter(Rt=>Rt.enabled).length}},enabled:de==="plugs"&&!!Oe&&Oe.length>0,refetchInterval:de==="plugs"?1e4:!1}),{data:pt,isLoading:kt}=ut({queryKey:["notification-providers"],queryFn:he.getNotificationProviders}),{data:Lt,isLoading:ln}=ut({queryKey:["api-keys"],queryFn:he.getAPIKeys}),Gt=mt({mutationFn:be=>he.createAPIKey(be),onSuccess:be=>{ee(be.key||null),me(!1),ye(""),t.invalidateQueries({queryKey:["api-keys"]}),o("API key created")},onError:be=>{o(`Failed to create API key: ${be.message}`,"error")}}),_n=mt({mutationFn:be=>he.deleteAPIKey(be),onSuccess:()=>{t.invalidateQueries({queryKey:["api-keys"]}),o("API key deleted")},onError:be=>{o(`Failed to delete API key: ${be.message}`,"error")}}),{data:gt}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:Kt,isLoading:bn}=ut({queryKey:["notification-templates"],queryFn:he.getNotificationTemplates}),{data:At}=ut({queryKey:["virtual-printer-settings"],queryFn:r1.getSettings,refetchInterval:1e4}),Pr=At?.status?.running??!1,{data:yr}=ut({queryKey:["ffmpeg-status"],queryFn:he.checkFfmpeg}),{data:jn}=ut({queryKey:["version"],queryFn:he.getVersion}),{data:sn,refetch:Rr,isRefetching:Gr}=ut({queryKey:["updateCheck"],queryFn:he.checkForUpdates,staleTime:300*1e3}),{data:Dr,refetch:_e}=ut({queryKey:["updateStatus"],queryFn:he.getUpdateStatus,refetchInterval:be=>{const Pe=be.state.data;return Pe?.status==="downloading"||Pe?.status==="installing"?1e3:!1}}),{data:Ve}=ut({queryKey:["mqtt-status"],queryFn:he.getMQTTStatus,refetchInterval:de==="network"?5e3:!1}),{data:at}=ut({queryKey:["github-backup-status"],queryFn:he.getGitHubBackupStatus}),{data:ct}=ut({queryKey:["cloud-status"],queryFn:he.getCloudStatus}),rt=mt({mutationFn:he.applyUpdate,onSuccess:be=>{be.is_docker?o(be.message,"error"):_e()}}),[jt,zt]=w.useState(null),Xt=mt({mutationFn:he.testAllNotificationProviders,onSuccess:be=>{zt(be),t.invalidateQueries({queryKey:["notification-providers"]}),be.failed===0?o(`All ${be.tested} providers tested successfully!`,"success"):o(`${be.success}/${be.tested} providers succeeded`,be.failed>0?"error":"success")},onError:be=>{o(`Failed to test providers: ${be.message}`,"error")}}),Yt=mt({mutationFn:async be=>{if(!Oe)return{success:0,failed:0};const Pe=Oe.filter(Mt=>Mt.enabled),wt=await Promise.all(Pe.map(async Mt=>{try{return await he.controlSmartPlug(Mt.id,be),{success:!0}}catch{return{success:!1}}}));return{success:wt.filter(Mt=>Mt.success).length,failed:wt.filter(Mt=>!Mt.success).length}},onSuccess:(be,Pe)=>{t.invalidateQueries({queryKey:["smart-plugs"]}),t.invalidateQueries({queryKey:["smart-plugs-energy"]}),be.failed===0?o(`All ${be.success} plugs turned ${Pe}`,"success"):o(`${be.success} plugs turned ${Pe}, ${be.failed} failed`,"error")},onError:be=>{o(`Failed: ${be.message}`,"error")}}),ue=w.useRef(null),Re=w.useRef(!1),He=w.useRef(!0);w.useEffect(()=>{if(je&&!E){const be={...je,external_url:je.external_url||window.location.origin};O(be),setTimeout(()=>{He.current=!1},100)}},[je,E]);const Xe=mt({mutationFn:he.updateSettings,onSuccess:be=>{t.setQueryData(["settings"],be),O(be),t.invalidateQueries({queryKey:["archiveStats"]}),o("Settings saved","success")},onError:be=>{o(`Failed to save: ${be.message}`,"error")},onSettled:()=>{Re.current=!1}}),Ge=mt({mutationFn:({id:be,data:Pe})=>he.updatePrinter(be,Pe),onSuccess:()=>{t.invalidateQueries({queryKey:["printers"]}),o("Camera settings saved","success")},onError:be=>{o(`Failed to update printer: ${be.message}`,"error")}});w.useEffect(()=>{if(!(He.current||!E||!je||!(je.auto_archive!==E.auto_archive||je.save_thumbnails!==E.save_thumbnails||je.capture_finish_photo!==E.capture_finish_photo||je.default_filament_cost!==E.default_filament_cost||je.currency!==E.currency||je.energy_cost_per_kwh!==E.energy_cost_per_kwh||je.energy_tracking_mode!==E.energy_tracking_mode||je.check_updates!==E.check_updates||je.notification_language!==E.notification_language||je.ams_humidity_good!==E.ams_humidity_good||je.ams_humidity_fair!==E.ams_humidity_fair||je.ams_temp_good!==E.ams_temp_good||je.ams_temp_fair!==E.ams_temp_fair||je.ams_history_retention_days!==E.ams_history_retention_days||je.per_printer_mapping_expanded!==E.per_printer_mapping_expanded||je.date_format!==E.date_format||je.time_format!==E.time_format||je.default_printer_id!==E.default_printer_id||je.ftp_retry_enabled!==E.ftp_retry_enabled||je.ftp_retry_count!==E.ftp_retry_count||je.ftp_retry_delay!==E.ftp_retry_delay||je.ftp_timeout!==E.ftp_timeout||je.mqtt_enabled!==E.mqtt_enabled||je.mqtt_broker!==E.mqtt_broker||je.mqtt_port!==E.mqtt_port||je.mqtt_username!==E.mqtt_username||je.mqtt_password!==E.mqtt_password||je.mqtt_topic_prefix!==E.mqtt_topic_prefix||je.mqtt_use_tls!==E.mqtt_use_tls||je.external_url!==E.external_url||je.ha_enabled!==E.ha_enabled||je.ha_url!==E.ha_url||je.ha_token!==E.ha_token||(je.library_archive_mode??"ask")!==(E.library_archive_mode??"ask")||Number(je.library_disk_warning_gb??5)!==Number(E.library_disk_warning_gb??5)||(je.camera_view_mode??"window")!==(E.camera_view_mode??"window")||je.prometheus_enabled!==E.prometheus_enabled||je.prometheus_token!==E.prometheus_token))&&!Re.current)return ue.current&&clearTimeout(ue.current),ue.current=setTimeout(()=>{if(Re.current)return;Re.current=!0;const Pe={auto_archive:E.auto_archive,save_thumbnails:E.save_thumbnails,capture_finish_photo:E.capture_finish_photo,default_filament_cost:E.default_filament_cost,currency:E.currency,energy_cost_per_kwh:E.energy_cost_per_kwh,energy_tracking_mode:E.energy_tracking_mode,check_updates:E.check_updates,notification_language:E.notification_language,ams_humidity_good:E.ams_humidity_good,ams_humidity_fair:E.ams_humidity_fair,ams_temp_good:E.ams_temp_good,ams_temp_fair:E.ams_temp_fair,ams_history_retention_days:E.ams_history_retention_days,per_printer_mapping_expanded:E.per_printer_mapping_expanded,date_format:E.date_format,time_format:E.time_format,default_printer_id:E.default_printer_id,ftp_retry_enabled:E.ftp_retry_enabled,ftp_retry_count:E.ftp_retry_count,ftp_retry_delay:E.ftp_retry_delay,ftp_timeout:E.ftp_timeout,mqtt_enabled:E.mqtt_enabled,mqtt_broker:E.mqtt_broker,mqtt_port:E.mqtt_port,mqtt_username:E.mqtt_username,mqtt_password:E.mqtt_password,mqtt_topic_prefix:E.mqtt_topic_prefix,mqtt_use_tls:E.mqtt_use_tls,external_url:E.external_url,ha_enabled:E.ha_enabled,ha_url:E.ha_url,ha_token:E.ha_token,library_archive_mode:E.library_archive_mode,library_disk_warning_gb:E.library_disk_warning_gb,camera_view_mode:E.camera_view_mode,prometheus_enabled:E.prometheus_enabled,prometheus_token:E.prometheus_token};Xe.mutate(Pe)},500),()=>{ue.current&&clearTimeout(ue.current)}},[E,je,Xe]);const ht=w.useCallback((be,Pe)=>{O(wt=>wt?{...wt,[be]:Pe}:null)},[]),Pt=async(be,Pe,wt)=>{if(!Pe){o("Please enter a camera URL","error");return}Fe(Mt=>({...Mt,[be]:!0})),te(Mt=>({...Mt,[be]:null}));try{const Mt=await he.testExternalCamera(be,Pe,wt);te(lt=>({...lt,[be]:Mt})),Mt.success?o(`Camera connected${Mt.resolution?` (${Mt.resolution})`:""}`,"success"):o(Mt.error||"Connection failed","error")}catch(Mt){const lt=Mt instanceof Error?Mt.message:"Test failed";te(Tt=>({...Tt,[be]:{success:!1,error:lt}})),o(lt,"error")}finally{Fe(Mt=>({...Mt,[be]:!1}))}},[_t,yt]=w.useState({}),It=w.useRef({});w.useEffect(()=>{if(gt){const be={};gt.forEach(Pe=>{Pe.external_camera_url&&_t[Pe.id]===void 0&&(be[Pe.id]=Pe.external_camera_url)}),Object.keys(be).length>0&&yt(Pe=>({...Pe,...be}))}},[gt]);const Ot=(be,Pe)=>{yt(wt=>({...wt,[be]:Pe})),It.current[be]&&clearTimeout(It.current[be]),It.current[be]=setTimeout(()=>{Ge.mutate({id:be,data:{external_camera_url:Pe||null}})},800)},Ft=(be,Pe)=>{const wt={};Pe.type!==void 0&&(wt.external_camera_type=Pe.type||null),Pe.enabled!==void 0&&(wt.external_camera_enabled=Pe.enabled),Ge.mutate({id:be,data:wt})};return xe||!E?s.jsx("div",{className:"p-4 md:p-8 flex justify-center",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):s.jsxs("div",{className:"p-4 md:p-8",children:[s.jsxs("div",{className:"mb-8",children:[s.jsx("h1",{className:"text-2xl font-bold text-white",children:"Settings"}),s.jsx("p",{className:"text-bambu-gray",children:"Configure Bambuddy"})]}),s.jsxs("div",{className:"flex gap-1 mb-6 border-b border-bambu-dark-tertiary overflow-x-auto",children:[s.jsx("button",{onClick:()=>ie("general"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px ${de==="general"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:"General"}),s.jsxs("button",{onClick:()=>ie("plugs"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="plugs"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(Ks,{className:"w-4 h-4"}),"Smart Plugs",Oe&&Oe.length>0&&s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:Oe.length})]}),s.jsxs("button",{onClick:()=>ie("notifications"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="notifications"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(Ef,{className:"w-4 h-4"}),"Notifications",pt&&pt.length>0&&s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:pt.length})]}),s.jsxs("button",{onClick:()=>ie("filament"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="filament"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(Rde,{className:"w-4 h-4"}),"Filament"]}),s.jsxs("button",{onClick:()=>ie("network"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="network"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(ig,{className:"w-4 h-4"}),"Network",s.jsx("span",{className:`w-2 h-2 rounded-full ${Ve?.enabled?"bg-green-400":"bg-gray-500"}`})]}),s.jsxs("button",{onClick:()=>ie("apikeys"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="apikeys"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(mf,{className:"w-4 h-4"}),"API Keys",Lt&&Lt.length>0&&s.jsx("span",{className:"text-xs bg-bambu-dark-tertiary px-1.5 py-0.5 rounded-full",children:Lt.length})]}),s.jsxs("button",{onClick:()=>ie("virtual-printer"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="virtual-printer"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(qr,{className:"w-4 h-4"}),"Virtual Printer",s.jsx("span",{className:`w-2 h-2 rounded-full ${Pr?"bg-green-400":"bg-gray-500"}`})]}),s.jsxs("button",{onClick:()=>ie("users"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="users"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(pf,{className:"w-4 h-4"}),"Users",l&&s.jsx("span",{className:`w-2 h-2 rounded-full ${l?"bg-green-400":"bg-gray-500"}`})]}),s.jsxs("button",{onClick:()=>ie("backup"),className:`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${de==="backup"?"text-bambu-green border-bambu-green":"text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent"}`,children:[s.jsx(su,{className:"w-4 h-4"}),"Backup",s.jsx("span",{className:`w-2 h-2 rounded-full ${ct?.is_authenticated&&at?.configured&&at?.enabled?"bg-green-400":"bg-gray-500"}`})]})]}),de==="general"&&s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[s.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-xl",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:i("settings.general")})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsxs("label",{className:"block text-sm text-bambu-gray mb-1",children:[s.jsx($d,{className:"w-4 h-4 inline mr-1"}),i("settings.language")]}),s.jsx("select",{value:a.language,onChange:be=>a.changeLanguage(be.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:jz.map(be=>s.jsxs("option",{value:be.code,children:[be.nativeName," (",be.name,")"]},be.code))}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("settings.languageDescription")})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:i("settings.defaultView")}),s.jsx("select",{value:z,onChange:be=>ot(be.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:O0.map(be=>s.jsx("option",{value:be.to,children:i(be.labelKey)},be.id))}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:i("settings.defaultViewDescription")})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Date Format"}),s.jsxs("select",{value:E.date_format||"system",onChange:be=>ht("date_format",be.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:[s.jsx("option",{value:"system",children:"System Default"}),s.jsx("option",{value:"us",children:"US (MM/DD/YYYY)"}),s.jsx("option",{value:"eu",children:"EU (DD/MM/YYYY)"}),s.jsx("option",{value:"iso",children:"ISO (YYYY-MM-DD)"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Time Format"}),s.jsxs("select",{value:E.time_format||"system",onChange:be=>ht("time_format",be.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:[s.jsx("option",{value:"system",children:"System Default"}),s.jsx("option",{value:"12h",children:"12-hour (3:30 PM)"}),s.jsx("option",{value:"24h",children:"24-hour (15:30)"})]})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default Printer"}),s.jsxs("select",{value:E.default_printer_id??"",onChange:be=>ht("default_printer_id",be.target.value?Number(be.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:[s.jsx("option",{value:"",children:"No default (ask each time)"}),gt?.map(be=>s.jsx("option",{value:be.id,children:be.name},be.id))]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Pre-select this printer for uploads, reprints, and other operations."})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Sidebar order"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Drag items in the sidebar to reorder. Reset to default order here."})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:se,children:[s.jsx(mm,{className:"w-4 h-4"}),"Reset"]})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(HD,{className:"w-5 h-5"}),"Appearance"]})}),s.jsxs(Nt,{className:"space-y-6",children:[s.jsxs("div",{className:`space-y-3 p-4 rounded-lg border ${d==="dark"?"border-bambu-green bg-bambu-green/5":"border-bambu-dark-tertiary"}`,children:[s.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:["Dark Mode",d==="dark"&&s.jsx("span",{className:"text-xs text-bambu-green",children:"(active)"})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Background"}),s.jsxs("select",{value:m,onChange:be=>{S(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"neutral",children:"Neutral"}),s.jsx("option",{value:"warm",children:"Warm"}),s.jsx("option",{value:"cool",children:"Cool"}),s.jsx("option",{value:"oled",children:"OLED Black"}),s.jsx("option",{value:"slate",children:"Slate Blue"}),s.jsx("option",{value:"forest",children:"Forest Green"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Accent"}),s.jsxs("select",{value:p,onChange:be=>{M(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"green",children:"Green"}),s.jsx("option",{value:"teal",children:"Teal"}),s.jsx("option",{value:"blue",children:"Blue"}),s.jsx("option",{value:"orange",children:"Orange"}),s.jsx("option",{value:"purple",children:"Purple"}),s.jsx("option",{value:"red",children:"Red"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Style"}),s.jsxs("select",{value:f,onChange:be=>{v(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"classic",children:"Classic"}),s.jsx("option",{value:"glow",children:"Glow"}),s.jsx("option",{value:"vibrant",children:"Vibrant"})]})]})]})]}),s.jsxs("div",{className:`space-y-3 p-4 rounded-lg border ${d==="light"?"border-bambu-green bg-bambu-green/5":"border-bambu-dark-tertiary"}`,children:[s.jsxs("h3",{className:"text-sm font-medium text-white flex items-center gap-2",children:["Light Mode",d==="light"&&s.jsx("span",{className:"text-xs text-bambu-green",children:"(active)"})]}),s.jsxs("div",{className:"grid grid-cols-3 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Background"}),s.jsxs("select",{value:y,onChange:be=>{T(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"neutral",children:"Neutral"}),s.jsx("option",{value:"warm",children:"Warm"}),s.jsx("option",{value:"cool",children:"Cool"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Accent"}),s.jsxs("select",{value:x,onChange:be=>{k(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"green",children:"Green"}),s.jsx("option",{value:"teal",children:"Teal"}),s.jsx("option",{value:"blue",children:"Blue"}),s.jsx("option",{value:"orange",children:"Orange"}),s.jsx("option",{value:"purple",children:"Purple"}),s.jsx("option",{value:"red",children:"Red"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-xs text-bambu-gray mb-1",children:"Style"}),s.jsxs("select",{value:b,onChange:be=>{A(be.target.value),o("Settings saved","success")},className:"w-full px-2 py-1.5 text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"classic",children:"Classic"}),s.jsx("option",{value:"glow",children:"Glow"}),s.jsx("option",{value:"vibrant",children:"Vibrant"})]})]})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Toggle between dark and light mode using the sun/moon icon in the sidebar."})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Archive Settings"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Auto-archive prints"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically save 3MF files when prints complete"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.auto_archive,onChange:be=>ht("auto_archive",be.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Save thumbnails"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Extract and save preview images from 3MF files"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.save_thumbnails,onChange:be=>ht("save_thumbnails",be.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Capture finish photo"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Take a photo from printer camera when print completes"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.capture_finish_photo,onChange:be=>ht("capture_finish_photo",be.target.checked),className:"sr-only peer"}),s.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.capture_finish_photo&&yr&&!yr.installed&&s.jsxs("div",{className:"flex items-start gap-2 p-3 bg-yellow-500/10 border border-yellow-500/30 rounded-lg",children:[s.jsx(kn,{className:"w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"text-sm",children:[s.jsx("p",{className:"text-yellow-500 font-medium",children:"ffmpeg not installed"}),s.jsxs("p",{className:"text-bambu-gray mt-1",children:["Camera capture requires ffmpeg. Install it via"," ",s.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"brew install ffmpeg"})," (macOS) or"," ",s.jsx("code",{className:"bg-bambu-dark-tertiary px-1 rounded",children:"apt install ffmpeg"})," (Linux)."]})]})]})]})]})]}),s.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-md",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(qD,{className:"w-5 h-5 text-bambu-green"}),"Camera"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Camera View Mode"}),s.jsxs("select",{value:E.camera_view_mode??"window",onChange:be=>ht("camera_view_mode",be.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:[s.jsx("option",{value:"window",children:"New Window"}),s.jsx("option",{value:"embedded",children:"Embedded Overlay"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:E.camera_view_mode==="embedded"?"Camera opens in a resizable overlay on the main screen":"Camera opens in a separate browser window"})]}),s.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4 mt-4",children:[s.jsx("h3",{className:"text-sm font-medium text-white mb-2",children:"External Cameras"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-3",children:"Configure external cameras to replace the built-in printer camera. Supports MJPEG streams, RTSP, HTTP snapshots, and USB cameras (V4L2). When enabled, the external camera is used for live view and finish photos."}),gt&>.length>0?s.jsx("div",{className:"space-y-3",children:gt.map(be=>s.jsxs("div",{className:"p-3 bg-bambu-dark rounded-lg",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-white font-medium text-sm",children:be.name}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:be.external_camera_enabled,onChange:Pe=>Ft(be.id,{enabled:Pe.target.checked}),className:"sr-only peer"}),s.jsx("div",{className:"w-9 h-5 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:border-gray-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"})]})]}),be.external_camera_enabled&&s.jsxs("div",{className:"space-y-2 mt-2",children:[s.jsx("input",{type:"text",placeholder:be.external_camera_type==="usb"?"Device path (/dev/video0)":"Camera URL (rtsp://... or http://...)",value:_t[be.id]??be.external_camera_url??"",onChange:Pe=>Ot(be.id,Pe.target.value),className:"w-full px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none"}),s.jsxs("div",{className:"flex gap-2",children:[s.jsxs("select",{value:be.external_camera_type||"mjpeg",onChange:Pe=>Ft(be.id,{type:Pe.target.value}),className:"flex-1 px-3 py-2 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-sm focus:border-bambu-green focus:outline-none",children:[s.jsx("option",{value:"mjpeg",children:"MJPEG Stream"}),s.jsx("option",{value:"rtsp",children:"RTSP Stream"}),s.jsx("option",{value:"snapshot",children:"HTTP Snapshot"}),s.jsx("option",{value:"usb",children:"USB Camera (V4L2)"})]}),s.jsx(Se,{size:"sm",variant:"secondary",onClick:()=>Pt(be.id,_t[be.id]??be.external_camera_url??"",be.external_camera_type||"mjpeg"),disabled:Me[be.id]||!(_t[be.id]??be.external_camera_url),children:Me[be.id]?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):"Test"})]}),oe[be.id]&&s.jsx("div",{className:`text-xs flex items-center gap-1 ${oe[be.id]?.success?"text-green-500":"text-red-500"}`,children:oe[be.id]?.success?s.jsxs(s.Fragment,{children:[s.jsx(Un,{className:"w-3 h-3"}),"Connected",oe[be.id]?.resolution&&` (${oe[be.id]?.resolution})`]}):s.jsxs(s.Fragment,{children:[s.jsx(ia,{className:"w-3 h-3"}),oe[be.id]?.error||"Connection failed"]})})]})]},be.id))}):s.jsx("p",{className:"text-xs text-bambu-gray italic",children:"No printers configured"})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Cost Tracking"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Default filament cost (per kg)"}),s.jsx("input",{type:"number",step:"0.01",min:"0",value:E.default_filament_cost,onChange:be=>ht("default_filament_cost",parseFloat(be.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Currency"}),s.jsxs("select",{value:E.currency,onChange:be=>ht("currency",be.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:[s.jsx("option",{value:"USD",children:"USD ($)"}),s.jsx("option",{value:"EUR",children:"EUR (€)"}),s.jsx("option",{value:"GBP",children:"GBP (£)"}),s.jsx("option",{value:"CHF",children:"CHF (Fr.)"}),s.jsx("option",{value:"JPY",children:"JPY (¥)"}),s.jsx("option",{value:"CNY",children:"CNY (¥)"}),s.jsx("option",{value:"CAD",children:"CAD ($)"}),s.jsx("option",{value:"AUD",children:"AUD ($)"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Electricity cost per kWh"}),s.jsx("input",{type:"number",step:"0.01",min:"0",value:E.energy_cost_per_kwh,onChange:be=>ht("energy_cost_per_kwh",parseFloat(be.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Energy display mode"}),s.jsxs("select",{value:E.energy_tracking_mode||"total",onChange:be=>ht("energy_tracking_mode",be.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:[s.jsx("option",{value:"print",children:"Prints Only"}),s.jsx("option",{value:"total",children:"Total Consumption"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:E.energy_tracking_mode==="print"?"Dashboard shows sum of energy used during prints":"Dashboard shows lifetime energy from smart plugs"})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Za,{className:"w-5 h-5 text-bambu-green"}),"File Manager"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Create Archive Entry When Printing"}),s.jsxs("select",{value:E.library_archive_mode??"ask",onChange:be=>ht("library_archive_mode",be.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:[s.jsx("option",{value:"always",children:"Always create archive entry"}),s.jsx("option",{value:"never",children:"Never create archive entry"}),s.jsx("option",{value:"ask",children:"Ask each time"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"When printing from File Manager, optionally create an archive entry"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Low Disk Space Warning"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"0.5",max:"100",step:"0.5",value:E.library_disk_warning_gb??5,onChange:be=>ht("library_disk_warning_gb",parseFloat(be.target.value)||5),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"}),s.jsx("span",{className:"text-bambu-gray",children:"GB"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Show warning when free disk space falls below this threshold"})]})]})]})]}),s.jsxs("div",{className:"space-y-6 flex-1 lg:max-w-sm",children:[s.jsx(HYe,{}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Updates"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Check for updates"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically check for new versions on startup"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.check_updates,onChange:be=>ht("check_updates",be.target.checked),className:"sr-only peer"}),s.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.jsxs("div",{className:"border-t border-bambu-dark-tertiary pt-4",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Current version"}),s.jsxs("p",{className:"text-sm text-bambu-gray",children:["v",jn?.version||"..."]})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>Rr(),disabled:Gr,children:[Gr?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(xr,{className:"w-4 h-4"}),"Check now"]})]}),sn?.update_available?s.jsxs("div",{className:"mt-4 p-3 bg-bambu-green/10 border border-bambu-green/30 rounded-lg",children:[s.jsxs("div",{className:"flex items-start justify-between",children:[s.jsxs("div",{children:[s.jsxs("p",{className:"text-bambu-green font-medium",children:["Update available: v",sn.latest_version]}),sn.release_name&&sn.release_name!==sn.latest_version&&s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:sn.release_name})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[sn.release_notes&&s.jsx("button",{onClick:()=>we(!0),className:"text-bambu-gray hover:text-white transition-colors text-sm underline",children:"Release Notes"}),sn.release_url&&s.jsx("a",{href:sn.release_url,target:"_blank",rel:"noopener noreferrer",className:"text-bambu-gray hover:text-white transition-colors",title:"View release on GitHub",children:s.jsx(zr,{className:"w-4 h-4"})})]})]}),Dr?.status==="downloading"||Dr?.status==="installing"?s.jsxs("div",{className:"mt-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-sm text-bambu-gray",children:[s.jsx(dt,{className:"w-4 h-4 animate-spin"}),s.jsx("span",{children:Dr.message})]}),s.jsx("div",{className:"mt-2 w-full bg-bambu-dark-tertiary rounded-full h-2",children:s.jsx("div",{className:"bg-bambu-green h-2 rounded-full transition-all duration-300",style:{width:`${Dr.progress}%`}})})]}):Dr?.status==="complete"?s.jsx("div",{className:"mt-3 p-2 bg-bambu-green/20 rounded text-sm text-bambu-green",children:Dr.message}):Dr?.status==="error"?s.jsx("div",{className:"mt-3 p-2 bg-red-500/20 rounded text-sm text-red-400",children:Dr.error||Dr.message}):sn?.is_docker?s.jsxs("div",{className:"mt-3 p-3 bg-bambu-dark-tertiary rounded-lg",children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-2",children:"Update via Docker Compose:"}),s.jsx("code",{className:"block text-xs bg-bambu-dark p-2 rounded text-bambu-green font-mono",children:"docker compose pull && docker compose up -d"})]}):s.jsxs(Se,{className:"mt-3",onClick:()=>rt.mutate(),disabled:rt.isPending,children:[rt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Hr,{className:"w-4 h-4"}),"Install Update"]})]}):sn?.error?s.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: ",sn.error]}):sn&&!sn.update_available?s.jsx("p",{className:"mt-2 text-sm text-bambu-gray",children:"You're running the latest version"}):null]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"Data Management"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Clear Notification Logs"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Delete notification logs older than 30 days"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>Le(!0),children:[s.jsx(dn,{className:"w-4 h-4"}),"Clear"]})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Reset UI Preferences"}),s.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."})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>$(!0),children:[s.jsx(dn,{className:"w-4 h-4"}),"Reset"]})]}),s.jsxs("div",{className:"flex items-center justify-between pt-4 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Backup & Restore"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Export/import settings and configure GitHub backup"})]}),s.jsxs(Se,{variant:"secondary",size:"sm",onClick:()=>ie("backup"),children:[s.jsx(su,{className:"w-4 h-4"}),"Go to Backup"]})]})]})]})]})]}),de==="network"&&E&&s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6",children:[s.jsxs("div",{className:"flex-1 lg:max-w-xl space-y-4",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx($d,{className:"w-5 h-5 text-blue-400"}),"External URL"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"The external URL where Bambuddy is accessible. Used for notification images and external integrations."}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Bambuddy URL"}),s.jsx("input",{type:"text",value:E.external_url??"",onChange:be=>ht("external_url",be.target.value),placeholder:"http://192.168.1.100:8000",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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Include protocol and port (e.g., http://192.168.1.100:8000)"})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(xr,{className:"w-5 h-5 text-blue-400"}),"FTP Retry"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Retry FTP operations when printer WiFi is unreliable. Applies to 3MF downloads, print uploads, timelapse downloads, and firmware updates."}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable retry"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Automatically retry failed FTP operations"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.ftp_retry_enabled??!0,onChange:be=>ht("ftp_retry_enabled",be.target.checked),className:"sr-only peer"}),s.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.ftp_retry_enabled&&s.jsxs("div",{className:"space-y-4 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Retry attempts"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"1",max:"10",value:E.ftp_retry_count??3,onChange:be=>ht("ftp_retry_count",Math.min(10,Math.max(1,parseInt(be.target.value)||3))),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"}),s.jsx("span",{className:"text-bambu-gray",children:"times"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Number of retry attempts before giving up (1-10)"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Retry delay"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"1",max:"30",value:E.ftp_retry_delay??2,onChange:be=>ht("ftp_retry_delay",Math.min(30,Math.max(1,parseInt(be.target.value)||2))),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"}),s.jsx("span",{className:"text-bambu-gray",children:"seconds"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Wait time between retries (1-30)"})]})]}),s.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Connection timeout"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"10",max:"120",value:E.ftp_timeout??30,onChange:be=>ht("ftp_timeout",Math.min(120,Math.max(10,parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"seconds"})]}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Socket timeout for slow connections. Increase for A1/A1 Mini printers with weak WiFi (10-120)"})]})]})]})]}),s.jsxs("div",{className:"flex-1 lg:max-w-xl space-y-4",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(cN,{className:"w-5 h-5 text-bambu-green"}),"Home Assistant"]}),E.ha_enabled&&Ie&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:`w-2.5 h-2.5 rounded-full ${Ie.success?"bg-green-400":"bg-red-400"}`}),s.jsx("span",{className:`text-sm ${Ie.success?"text-green-400":"text-red-400"}`,children:Ie.success?"Connected":"Disconnected"})]})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Connect to Home Assistant to control smart plugs via HA's REST API. Supports switch, light, and input_boolean entities."}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable Home Assistant"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Control smart plugs via Home Assistant"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.ha_enabled??!1,onChange:be=>ht("ha_enabled",be.target.checked),className:"sr-only peer"}),s.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.ha_enabled&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Home Assistant URL"}),s.jsx("input",{type:"text",value:E.ha_url??"",onChange:be=>ht("ha_url",be.target.value),placeholder:"http://192.168.1.100:8123",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Long-Lived Access Token"}),s.jsx("input",{type:"password",value:E.ha_token??"",onChange:be=>ht("ha_token",be.target.value),placeholder:"eyJ0eXAiOiJKV1QiLC...",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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Create a token in HA: Profile → Long-Lived Access Tokens → Create Token"})]}),E.ha_url&&E.ha_token&&s.jsx("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:s.jsxs(Se,{variant:"secondary",size:"sm",disabled:Je,onClick:async()=>{Qe(!0),qe(null);try{const be=await he.testHAConnection(E.ha_url,E.ha_token);qe(be)}catch(be){qe({success:!1,message:null,error:be instanceof Error?be.message:"Unknown error"})}finally{Qe(!1)}},children:[Je?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(ig,{className:"w-4 h-4"}),"Test Connection"]})})]})]})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(ig,{className:"w-5 h-5 text-blue-400"}),"MQTT Publishing"]}),Ve?.enabled&&s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:`w-2.5 h-2.5 rounded-full ${Ve.connected?"bg-green-400":"bg-red-400"}`}),s.jsx("span",{className:`text-sm ${Ve.connected?"text-green-400":"text-red-400"}`,children:Ve.connected?"Connected":"Disconnected"})]})]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Publish BamBuddy events to an external MQTT broker for integration with Node-RED, Home Assistant, and other automation systems."}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable MQTT"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Publish events to external MQTT broker"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.mqtt_enabled??!1,onChange:be=>ht("mqtt_enabled",be.target.checked),className:"sr-only peer"}),s.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.mqtt_enabled&&s.jsxs("div",{className:"space-y-4 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Broker hostname"}),s.jsx("input",{type:"text",value:E.mqtt_broker??"",onChange:be=>ht("mqtt_broker",be.target.value),placeholder:"mqtt.example.com or 192.168.1.100",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"})]}),s.jsxs("div",{className:"flex items-end gap-4",children:[s.jsxs("div",{className:"flex-1",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Port"}),s.jsx("input",{type:"number",min:"1",max:"65535",value:E.mqtt_port??1883,onChange:be=>ht("mqtt_port",Math.min(65535,Math.max(1,parseInt(be.target.value)||1883))),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"})]}),s.jsxs("div",{className:"flex items-center gap-3 pb-2",children:[s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.mqtt_use_tls??!1,onChange:be=>{const Pe=be.target.checked;ht("mqtt_use_tls",Pe);const wt=E.mqtt_port??1883;Pe&&wt===1883?ht("mqtt_port",8883):!Pe&&wt===8883&&ht("mqtt_port",1883)},className:"sr-only peer"}),s.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.jsx("span",{className:"text-white text-sm",children:"Use TLS"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Username (optional)"}),s.jsx("input",{type:"text",value:E.mqtt_username??"",onChange:be=>ht("mqtt_username",be.target.value),placeholder:"Leave empty for anonymous",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password (optional)"}),s.jsx("input",{type:"password",value:E.mqtt_password??"",onChange:be=>ht("mqtt_password",be.target.value),placeholder:"Leave empty for anonymous",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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Topic prefix"}),s.jsx("input",{type:"text",value:E.mqtt_topic_prefix??"bambuddy",onChange:be=>ht("mqtt_topic_prefix",be.target.value),placeholder:"bambuddy",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"}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:["Topics will be: ",E.mqtt_topic_prefix||"bambuddy","/printers//status, etc."]})]}),Ve&&s.jsx("div",{className:"pt-3 mt-3 border-t border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[s.jsx("span",{className:`w-2 h-2 rounded-full ${Ve.connected?"bg-green-400":"bg-red-400"}`}),s.jsx("span",{className:"text-bambu-gray",children:Ve.connected?s.jsxs(s.Fragment,{children:["Connected to ",s.jsxs("span",{className:"text-white",children:[Ve.broker,":",Ve.port]})]}):"Not connected"})]})})]})]})]})]}),s.jsx("div",{className:"flex-1 lg:max-w-md space-y-4",children:s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(NP,{className:"w-5 h-5 text-orange-400"}),"Prometheus Metrics"]})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("p",{className:"text-sm text-bambu-gray",children:["Expose printer metrics at ",s.jsx("code",{className:"bg-bambu-dark px-1 rounded",children:"/api/v1/metrics"})," for Prometheus/Grafana monitoring."]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white",children:"Enable Metrics Endpoint"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Expose printer data in Prometheus format"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.prometheus_enabled??!1,onChange:be=>ht("prometheus_enabled",be.target.checked),className:"sr-only peer"}),s.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.prometheus_enabled&&s.jsxs("div",{className:"space-y-4 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Bearer Token (optional)"}),s.jsx("input",{type:"password",value:E.prometheus_token??"",onChange:be=>ht("prometheus_token",be.target.value),placeholder:"Leave empty for no authentication",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"}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:["If set, requests must include ",s.jsx("code",{className:"bg-bambu-dark px-1 rounded",children:"Authorization: Bearer "})]})]}),s.jsxs("div",{className:"pt-2 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-sm text-white mb-2",children:"Available Metrics"}),s.jsxs("div",{className:"text-xs text-bambu-gray space-y-1",children:[s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_printer_connected"})," - Connection status"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_printer_state"})," - Printer state (idle/printing/etc)"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_print_progress"})," - Print progress 0-100%"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_bed_temp_celsius"})," - Bed temperature"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_nozzle_temp_celsius"})," - Nozzle temperature"]}),s.jsxs("p",{children:[s.jsx("code",{className:"text-orange-400",children:"bambuddy_prints_total"})," - Total prints by result"]}),s.jsx("p",{className:"text-bambu-gray/70 italic",children:"...and more (layers, fans, queue, filament usage)"})]})]})]})]})]})})]}),Ie&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:s.jsxs("div",{className:"bg-bambu-dark-secondary rounded-lg p-6 max-w-md w-full mx-4",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[Ie.success?s.jsx(Un,{className:"w-8 h-8 text-green-400"}):s.jsx(ia,{className:"w-8 h-8 text-red-400"}),s.jsx("h3",{className:"text-lg font-medium text-white",children:Ie.success?"Connection Successful":"Connection Failed"})]}),s.jsx("p",{className:"text-bambu-gray mb-6",children:Ie.success?Ie.message||"Successfully connected to Home Assistant.":Ie.error||"Failed to connect to Home Assistant."}),s.jsx("div",{className:"flex justify-end",children:s.jsx(Se,{variant:"primary",onClick:()=>qe(null),children:"OK"})})]})}),de==="plugs"&&s.jsxs("div",{className:"max-w-4xl",children:[s.jsxs("div",{className:"flex items-start justify-between mb-6",children:[s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ks,{className:"w-5 h-5 text-bambu-green"}),"Smart Plugs"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Connect smart plugs (Tasmota or Home Assistant) to automate power control and track energy usage for your printers."})]}),s.jsxs("div",{className:"flex items-center gap-2 pt-1 shrink-0",children:[Oe&&Oe.filter(be=>be.enabled).length>1&&s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Y("on"),disabled:Yt.isPending,title:"Turn all plugs on",children:[Yt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Eu,{className:"w-4 h-4 text-bambu-green"}),"All On"]}),s.jsxs(Se,{variant:"secondary",size:"sm",className:"whitespace-nowrap",onClick:()=>Y("off"),disabled:Yt.isPending,title:"Turn all plugs off",children:[Yt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(fN,{className:"w-4 h-4 text-red-400"}),"All Off"]})]}),s.jsxs(Se,{className:"whitespace-nowrap",onClick:()=>{L(null),D(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Smart Plug"]})]})]}),Oe&&Oe.length>0&&s.jsxs(St,{className:"mb-6",children:[s.jsx(er,{children:s.jsxs("h3",{className:"text-base font-semibold text-white flex items-center gap-2",children:[s.jsx(qf,{className:"w-4 h-4 text-yellow-400"}),"Energy Summary",We&&s.jsx(dt,{className:"w-4 h-4 animate-spin text-bambu-gray ml-2"})]})}),s.jsx(Nt,{children:Ce?s.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(qf,{className:"w-3 h-3"}),"Current Power"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalPower.toFixed(1),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"W"})]}),s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:[Ce.reachableCount,"/",Ce.totalPlugs," plugs online"]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(oi,{className:"w-3 h-3"}),"Today"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalToday.toFixed(2),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),E&&E.energy_cost_per_kwh>0&&s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Ce.totalToday*E.energy_cost_per_kwh).toFixed(2)," ",E.currency]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(NP,{className:"w-3 h-3"}),"Yesterday"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalYesterday.toFixed(2),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),E&&E.energy_cost_per_kwh>0&&s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Ce.totalYesterday*E.energy_cost_per_kwh).toFixed(2)," ",E.currency]})]}),s.jsxs("div",{className:"bg-bambu-dark rounded-lg p-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-bambu-gray text-xs mb-1",children:[s.jsx(xP,{className:"w-3 h-3"}),"Total"]}),s.jsxs("div",{className:"text-xl font-bold text-white",children:[Ce.totalLifetime.toFixed(1),s.jsx("span",{className:"text-sm font-normal text-bambu-gray ml-1",children:"kWh"})]}),E&&E.energy_cost_per_kwh>0&&s.jsxs("div",{className:"text-xs text-bambu-gray mt-1",children:["~",(Ce.totalLifetime*E.energy_cost_per_kwh).toFixed(2)," ",E.currency]})]})]}):We?null:s.jsx("p",{className:"text-sm text-bambu-gray",children:"Enable plugs to see energy summary"})})]}),Ue?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):Oe&&Oe.length>0?s.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:Oe.map(be=>s.jsx(CYe,{plug:be,onEdit:Pe=>{L(Pe),D(!0)}},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-12",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(Ks,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),s.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No smart plugs configured"}),s.jsx("p",{className:"text-sm mb-4",children:"Add a Tasmota-based smart plug to track energy usage and automate power control."}),s.jsxs(Se,{onClick:()=>{L(null),D(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Your First Smart Plug"]})]})})})]}),de==="notifications"&&s.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(Ef,{className:"w-5 h-5 text-bambu-green"}),"Providers"]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>J(!0),children:[s.jsx(P0,{className:"w-4 h-4"}),"Log"]}),pt&&pt.length>0&&s.jsxs(Se,{size:"sm",variant:"secondary",onClick:()=>{zt(null),Xt.mutate()},disabled:Xt.isPending,children:[Xt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(GD,{className:"w-4 h-4"}),"Test All"]}),s.jsxs(Se,{size:"sm",onClick:()=>{K(null),P(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add"]})]})]}),s.jsx(St,{className:"mb-4",children:s.jsx(Nt,{className:"py-3",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-white text-sm font-medium",children:i("settings.notificationLanguage")}),s.jsx("p",{className:"text-xs text-bambu-gray",children:i("settings.notificationLanguageDescription")})]}),s.jsx("select",{value:E.notification_language||"en",onChange:be=>ht("notification_language",be.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:jz.map(be=>s.jsx("option",{value:be.code,children:be.nativeName},be.code))})]})})}),jt&&s.jsx(St,{className:"mb-4",children:s.jsxs(Nt,{className:"py-3",children:[s.jsxs("div",{className:"flex items-center justify-between mb-2",children:[s.jsx("span",{className:"text-sm font-medium text-white",children:"Test Results"}),s.jsx("button",{onClick:()=>zt(null),className:"text-bambu-gray hover:text-white text-xs",children:"Dismiss"})]}),s.jsxs("div",{className:"flex items-center gap-4 text-sm mb-2",children:[s.jsxs("span",{className:"flex items-center gap-1 text-bambu-green",children:[s.jsx(Un,{className:"w-4 h-4"}),jt.success," passed"]}),jt.failed>0&&s.jsxs("span",{className:"flex items-center gap-1 text-red-400",children:[s.jsx(ia,{className:"w-4 h-4"}),jt.failed," failed"]})]}),jt.results.filter(be=>!be.success).length>0&&s.jsx("div",{className:"space-y-1 mt-2 pt-2 border-t border-bambu-dark-tertiary",children:jt.results.filter(be=>!be.success).map(be=>s.jsxs("div",{className:"text-xs text-red-400",children:[s.jsxs("span",{className:"font-medium",children:[be.provider_name,":"]})," ",be.message]},be.provider_id))})]})}),kt?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):pt&&pt.length>0?s.jsx("div",{className:"space-y-3",children:pt.map(be=>s.jsx(PYe,{provider:be,onEdit:Pe=>{K(Pe),P(!0)}},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-8",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(Ef,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),s.jsx("p",{className:"text-sm font-medium text-white mb-2",children:"No providers configured"}),s.jsx("p",{className:"text-xs mb-3",children:"Add a provider to receive alerts."}),s.jsxs(Se,{size:"sm",onClick:()=>{K(null),P(!0)},children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Provider"]})]})})})]}),s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2 mb-4",children:[s.jsx(Za,{className:"w-5 h-5 text-bambu-green"}),"Message Templates"]}),s.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Customize notification messages for each event."}),bn?s.jsx("div",{className:"flex justify-center py-8",children:s.jsx(dt,{className:"w-6 h-6 text-bambu-green animate-spin"})}):Kt&&Kt.length>0?s.jsx("div",{className:"space-y-2",children:Kt.map(be=>s.jsx(St,{className:"cursor-pointer hover:border-bambu-green/50 transition-colors",onClick:()=>Q(be),children:s.jsx(Nt,{className:"py-2.5 px-3",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsx("p",{className:"text-white font-medium text-sm truncate",children:be.name}),s.jsx("p",{className:"text-bambu-gray text-xs truncate mt-0.5",children:be.title_template})]}),s.jsx("button",{className:"p-1.5 hover:bg-bambu-dark-tertiary rounded transition-colors shrink-0 ml-2",onClick:Pe=>{Pe.stopPropagation(),Q(be)},children:s.jsx(hN,{className:"w-4 h-4 text-bambu-gray"})})]})})},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-8",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(Za,{className:"w-12 h-12 mx-auto mb-3 opacity-30"}),s.jsx("p",{className:"text-sm",children:"No templates available. Restart the backend to seed default templates."})]})})})]})]}),de==="apikeys"&&s.jsxs("div",{className:"grid grid-cols-1 xl:grid-cols-2 gap-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"flex items-start justify-between gap-4 mb-6",children:[s.jsxs("div",{className:"flex-1",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(mf,{className:"w-5 h-5 text-bambu-green"}),"API Keys"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Create API keys for external integrations and webhooks."})]}),s.jsxs(Se,{size:"sm",onClick:()=>me(!0),className:"flex-shrink-0",children:[s.jsx(nr,{className:"w-4 h-4"}),"Create Key"]})]}),H&&s.jsx(St,{className:"mb-6 border-bambu-green",children:s.jsx(Nt,{className:"py-4",children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx(Un,{className:"w-5 h-5 text-bambu-green flex-shrink-0 mt-0.5"}),s.jsxs("div",{className:"flex-1",children:[s.jsx("p",{className:"text-white font-medium mb-1",children:"API Key Created Successfully"}),s.jsx("p",{className:"text-sm text-bambu-gray mb-2",children:"Copy this key now - it won't be shown again!"}),s.jsxs("div",{className:"flex items-center gap-2 bg-bambu-dark rounded-lg p-2",children:[s.jsx("code",{className:"flex-1 text-sm text-bambu-green font-mono break-all",children:H}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:async()=>{try{if(navigator.clipboard&&navigator.clipboard.writeText)await navigator.clipboard.writeText(H);else{const be=document.createElement("textarea");be.value=H,be.style.position="fixed",be.style.left="-999999px",document.body.appendChild(be),be.select(),document.execCommand("copy"),document.body.removeChild(be)}o("Key copied to clipboard")}catch{o("Failed to copy key","error")}},children:s.jsx(ll,{className:"w-4 h-4"})})]}),s.jsxs("div",{className:"flex gap-2 mt-3",children:[s.jsx(Se,{variant:"secondary",size:"sm",onClick:()=>{ce(H),o("Key added to API Browser")},children:"Use in API Browser"}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:()=>ee(null),children:"Dismiss"})]})]})]})})}),Ae&&s.jsxs(St,{className:"mb-6",children:[s.jsx(er,{children:s.jsx("h3",{className:"text-base font-semibold text-white",children:"Create New API Key"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Key Name"}),s.jsx("input",{type:"text",value:Ne,onChange:be=>ye(be.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"Permissions"}),s.jsxs("div",{className:"space-y-2",children:[s.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:le.can_read_status,onChange:be=>$e(Pe=>({...Pe,can_read_status:be.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white",children:"Read Status"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"View printer status and queue"})]})]}),s.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:le.can_queue,onChange:be=>$e(Pe=>({...Pe,can_queue:be.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white",children:"Manage Queue"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Add and remove items from print queue"})]})]}),s.jsxs("label",{className:"flex items-center gap-3 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:le.can_control_printer,onChange:be=>$e(Pe=>({...Pe,can_control_printer:be.target.checked})),className:"w-4 h-4 text-bambu-green rounded border-bambu-dark-tertiary bg-bambu-dark focus:ring-bambu-green"}),s.jsxs("div",{children:[s.jsx("span",{className:"text-white",children:"Control Printer"}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Pause, resume, and stop prints"})]})]})]})]}),s.jsxs("div",{className:"flex items-center gap-2 pt-2",children:[s.jsxs(Se,{onClick:()=>Gt.mutate({name:Ne||"Unnamed Key",...le}),disabled:Gt.isPending,children:[Gt.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(nr,{className:"w-4 h-4"}),"Create Key"]}),s.jsx(Se,{variant:"secondary",onClick:()=>me(!1),children:"Cancel"})]})]})]}),ln?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):Lt&&Lt.length>0?s.jsx("div",{className:"space-y-3",children:Lt.map(be=>s.jsx(St,{children:s.jsx(Nt,{className:"py-3",children:s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx(mf,{className:`w-5 h-5 ${be.enabled?"text-bambu-green":"text-bambu-gray"}`}),s.jsxs("div",{children:[s.jsx("p",{className:"text-white font-medium",children:be.name}),s.jsxs("p",{className:"text-xs text-bambu-gray",children:[be.key_prefix,"••••••••",be.last_used&&` · Last used: ${_g(be.last_used)}`]})]})]}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsxs("div",{className:"flex gap-1 text-xs",children:[be.can_read_status&&s.jsx("span",{className:"px-1.5 py-0.5 bg-blue-500/20 text-blue-400 rounded",children:"Read"}),be.can_queue&&s.jsx("span",{className:"px-1.5 py-0.5 bg-green-500/20 text-green-400 rounded",children:"Queue"}),be.can_control_printer&&s.jsx("span",{className:"px-1.5 py-0.5 bg-orange-500/20 text-orange-400 rounded",children:"Control"})]}),s.jsx(Se,{variant:"secondary",size:"sm",onClick:()=>B(be.id),children:s.jsx(dn,{className:"w-4 h-4 text-red-400"})})]})]})})},be.id))}):s.jsx(St,{children:s.jsx(Nt,{className:"py-12",children:s.jsxs("div",{className:"text-center text-bambu-gray",children:[s.jsx(mf,{className:"w-16 h-16 mx-auto mb-4 opacity-30"}),s.jsx("p",{className:"text-lg font-medium text-white mb-2",children:"No API keys"}),s.jsx("p",{className:"text-sm mb-4",children:"Create an API key to integrate with external services."}),s.jsxs(Se,{onClick:()=>me(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Create Your First Key"]})]})})}),s.jsxs(St,{className:"mt-6",children:[s.jsx(er,{children:s.jsx("h3",{className:"text-base font-semibold text-white",children:"Webhook Endpoints"})}),s.jsxs(Nt,{className:"space-y-3 text-sm",children:[s.jsxs("p",{className:"text-bambu-gray",children:["Use your API key in the ",s.jsx("code",{className:"text-bambu-green",children:"X-API-Key"})," header."]}),s.jsxs("div",{className:"space-y-2 font-mono text-xs",children:[s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-blue-400",children:"GET"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/status"}),s.jsx("span",{className:"text-bambu-gray",children:" - Get all printer status"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-blue-400",children:"GET"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/status/:id"}),s.jsx("span",{className:"text-bambu-gray",children:" - Get specific printer status"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-green-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/queue"}),s.jsx("span",{className:"text-bambu-gray",children:" - Add to print queue"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-orange-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/pause"}),s.jsx("span",{className:"text-bambu-gray",children:" - Pause print"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-orange-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/resume"}),s.jsx("span",{className:"text-bambu-gray",children:" - Resume print"})]}),s.jsxs("div",{className:"p-2 bg-bambu-dark rounded",children:[s.jsx("span",{className:"text-red-400",children:"POST"})," ",s.jsx("span",{className:"text-white",children:"/api/v1/webhook/printer/:id/stop"}),s.jsx("span",{className:"text-bambu-gray",children:" - Stop print"})]})]})]})]})]}),s.jsxs("div",{children:[s.jsxs("div",{className:"mb-6",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx($d,{className:"w-5 h-5 text-bambu-green"}),"API Browser"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Explore and test all available API endpoints."})]}),s.jsx(St,{className:"mb-4",children:s.jsxs(Nt,{className:"py-3",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-2",children:"API Key for Testing"}),s.jsx("input",{type:"text",value:I,onChange:be=>ce(be.target.value),placeholder:"Paste your API key here to test authenticated endpoints...",className:"w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white font-mono text-sm focus:border-bambu-green focus:outline-none"}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-2",children:["This key will be sent as ",s.jsx("code",{className:"text-bambu-green",children:"X-API-Key"})," header with requests."]})]})}),s.jsx(QYe,{apiKey:I})]})]}),de==="virtual-printer"&&s.jsx(VYe,{}),de==="filament"&&E&&s.jsxs("div",{className:"flex flex-col lg:flex-row gap-6 lg:gap-8",children:[s.jsx("div",{className:"flex-1 lg:max-w-xl",children:s.jsxs(St,{children:[s.jsx(er,{children:s.jsx("h2",{className:"text-lg font-semibold text-white",children:"AMS Display Thresholds"})}),s.jsxs(Nt,{className:"space-y-4",children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Configure color thresholds for AMS humidity and temperature indicators."}),s.jsxs("div",{className:"space-y-3",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx($D,{className:"w-4 h-4 text-blue-400"}),s.jsx("span",{className:"font-medium",children:"Humidity"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (green) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"0",max:"100",value:E.ams_humidity_good??40,onChange:be=>ht("ams_humidity_good",parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"%"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"0",max:"100",value:E.ams_humidity_fair??60,onChange:be=>ht("ams_humidity_fair",parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"%"})]})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (bad)"})]}),s.jsxs("div",{className:"space-y-3 pt-2 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx(pN,{className:"w-4 h-4 text-orange-400"}),s.jsx("span",{className:"font-medium",children:"Temperature"})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Good (blue) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:E.ams_temp_good??28,onChange:be=>ht("ams_temp_good",parseFloat(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Fair (orange) ≤"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",step:"0.5",min:"0",max:"60",value:E.ams_temp_fair??35,onChange:be=>ht("ams_temp_fair",parseFloat(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"°C"})]})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Above fair threshold shows as red (hot)"})]}),s.jsxs("div",{className:"space-y-3 pt-4 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx(su,{className:"w-4 h-4 text-purple-400"}),s.jsx("span",{className:"font-medium",children:"History Retention"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Keep sensor history for"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("input",{type:"number",min:"1",max:"365",value:E.ams_history_retention_days??30,onChange:be=>ht("ams_history_retention_days",parseInt(be.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"}),s.jsx("span",{className:"text-bambu-gray",children:"days"})]})]}),s.jsx("p",{className:"text-xs text-bambu-gray",children:"Older humidity and temperature data will be automatically deleted"})]}),s.jsxs("div",{className:"space-y-3 pt-4 border-t border-bambu-dark-tertiary",children:[s.jsxs("div",{className:"flex items-center gap-2 text-white",children:[s.jsx(qr,{className:"w-4 h-4 text-bambu-green"}),s.jsx("span",{className:"font-medium",children:"Print Modal"})]}),s.jsxs("div",{className:"flex items-center justify-between",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-white",children:"Expand custom mapping by default"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-0.5",children:"When printing to multiple printers, show per-printer AMS mapping expanded"})]}),s.jsxs("label",{className:"relative inline-flex items-center cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:E.per_printer_mapping_expanded??!1,onChange:be=>ht("per_printer_mapping_expanded",be.target.checked),className:"sr-only peer"}),s.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.jsx("div",{className:"flex-1 lg:max-w-xl",children:s.jsx(BYe,{})})]}),X!==null&&s.jsx(Hn,{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:()=>{_n.mutate(X),B(null)},onCancel:()=>B(null)}),C&&s.jsx(kYe,{plug:U,onClose:()=>{D(!1),L(null)}}),q&&s.jsx(RYe,{provider:W,onClose:()=>{P(!1),K(null)}}),V&&s.jsx(DYe,{template:V,onClose:()=>Q(null)}),re&&s.jsx(IYe,{onClose:()=>J(!1)}),Te&&s.jsx(Hn,{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()=>{Le(!1);try{const be=await he.clearNotificationLogs(30);o(be.message,"success")}catch{o("Failed to clear logs","error")}},onCancel:()=>Le(!1)}),Ee&&s.jsx(Hn,{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:()=>{$(!1),localStorage.clear(),o("UI preferences reset. Refreshing...","success"),setTimeout(()=>window.location.reload(),1e3)},onCancel:()=>$(!1)}),j&&s.jsx(Hn,{title:`Turn All Plugs ${j==="on"?"On":"Off"}`,message:`This will turn ${j==="on"?"ON":"OFF"} all ${Oe?.filter(be=>be.enabled).length||0} enabled smart plugs. ${j==="off"?"Any running printers may be affected!":""}`,confirmText:`Turn All ${j==="on"?"On":"Off"}`,variant:j==="off"?"danger":"warning",onConfirm:()=>{const be=j;Y(null),Yt.mutate(be)},onCancel:()=>Y(null)}),ge&&sn?.release_notes&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",onClick:()=>we(!1),children:s.jsxs(St,{className:"w-full max-w-2xl max-h-[80vh] flex flex-col",onClick:be=>be.stopPropagation(),children:[s.jsxs(er,{className:"flex flex-row items-center justify-between shrink-0",children:[s.jsxs("div",{children:[s.jsxs("h2",{className:"text-lg font-semibold text-white",children:["Release Notes - v",sn.latest_version]}),sn.release_name&&sn.release_name!==sn.latest_version&&s.jsx("p",{className:"text-sm text-bambu-gray",children:sn.release_name})]}),s.jsx("button",{onClick:()=>we(!1),className:"p-1 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white",children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsx(Nt,{className:"overflow-y-auto flex-1",children:s.jsx("pre",{className:"text-sm text-bambu-gray whitespace-pre-wrap font-sans",children:sn.release_notes})}),s.jsxs("div",{className:"p-4 border-t border-bambu-dark-tertiary shrink-0 flex gap-2",children:[sn.release_url&&s.jsx("a",{href:sn.release_url,target:"_blank",rel:"noopener noreferrer",className:"flex-1",children:s.jsxs(Se,{variant:"secondary",className:"w-full",children:[s.jsx(zr,{className:"w-4 h-4"}),"View on GitHub"]})}),s.jsx(Se,{onClick:()=>we(!1),className:"flex-1",children:"Close"})]})]})}),de==="users"&&s.jsxs("div",{className:"grid grid-cols-1 xl:grid-cols-2 gap-8",children:[s.jsxs("div",{children:[s.jsxs("div",{className:"mb-6",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(pf,{className:"w-5 h-5 text-bambu-green"}),"User Authentication"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Enable authentication to secure your Bambuddy instance and manage user access."})]}),s.jsx(St,{children:s.jsx(Nt,{className:"py-6",children:l?s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-12 h-12 rounded-full flex items-center justify-center bg-green-500/20",children:s.jsx(lT,{className:"w-6 h-6 text-green-400"})}),s.jsxs("div",{className:"flex-1",children:[s.jsx("h3",{className:"text-white font-medium",children:"Authentication Enabled"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Your Bambuddy instance is secured with authentication."})]})]}),c&&s.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:s.jsxs("div",{className:"flex items-center justify-between mb-4",children:[s.jsxs("div",{children:[s.jsx("p",{className:"text-sm text-bambu-gray",children:"Current User"}),s.jsx("p",{className:"text-white font-medium",children:c.username}),s.jsxs("p",{className:"text-xs text-bambu-gray mt-1",children:["Role: ",s.jsx("span",{className:"capitalize",children:c.role})]})]}),s.jsx("div",{className:`px-3 py-1 rounded-full text-xs font-medium ${c.role==="admin"?"bg-purple-500/20 text-purple-300":"bg-blue-500/20 text-blue-300"}`,children:c.role==="admin"?"Admin":"User"})]})}),s.jsxs("div",{className:"pt-4 border-t border-bambu-dark-tertiary space-y-3",children:[s.jsxs(Se,{onClick:()=>e("/users"),className:"w-full",variant:"secondary",children:[s.jsx(pf,{className:"w-4 h-4"}),"Manage Users"]}),c?.role==="admin"&&s.jsxs(Se,{onClick:()=>Ze(!0),className:"w-full",variant:"secondary",children:[s.jsx(Yz,{className:"w-4 h-4"}),"Disable Authentication"]})]})]}):s.jsxs("div",{className:"space-y-4",children:[s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:`w-12 h-12 rounded-full flex items-center justify-center ${l?"bg-green-500/20":"bg-gray-500/20"}`,children:l?s.jsx(lT,{className:"w-6 h-6 text-green-400"}):s.jsx(Yz,{className:"w-6 h-6 text-gray-400"})}),s.jsxs("div",{className:"flex-1",children:[s.jsx("h3",{className:"text-white font-medium",children:"Authentication Disabled"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"Your Bambuddy instance is currently accessible without authentication."})]})]}),s.jsxs("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:[s.jsx("p",{className:"text-sm text-bambu-gray mb-4",children:"Enable authentication to:"}),s.jsxs("ul",{className:"space-y-2 text-sm text-bambu-gray mb-4",children:[s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Require login to access the system"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage multiple users with different roles"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Control access to printer settings and user management"})]})]}),s.jsxs(Se,{type:"button",onClick:be=>{be.preventDefault(),e("/setup")},className:"w-full",children:[s.jsx(lT,{className:"w-4 h-4"}),"Activate Authentication"]})]})]})})})]}),l&&s.jsxs("div",{children:[s.jsxs("div",{className:"mb-6",children:[s.jsxs("h2",{className:"text-lg font-semibold text-white flex items-center gap-2",children:[s.jsx(MP,{className:"w-5 h-5 text-bambu-green"}),"Role Permissions"]}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Overview of what each role can do."})]}),s.jsxs("div",{className:"space-y-4",children:[s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("div",{className:"w-8 h-8 rounded-full bg-purple-500/20 flex items-center justify-center",children:s.jsx(MP,{className:"w-4 h-4 text-purple-300"})}),s.jsx("h3",{className:"text-white font-medium",children:"Admin"})]})}),s.jsx(Nt,{children:s.jsxs("ul",{className:"space-y-2 text-sm text-bambu-gray",children:[s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage printer settings"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Create, edit, and delete users"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Access all system features"})]})]})})]}),s.jsxs(St,{children:[s.jsx(er,{children:s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("div",{className:"w-8 h-8 rounded-full bg-blue-500/20 flex items-center justify-center",children:s.jsx(pf,{className:"w-4 h-4 text-blue-300"})}),s.jsx("h3",{className:"text-white font-medium",children:"User"})]})}),s.jsx(Nt,{children:s.jsxs("ul",{className:"space-y-2 text-sm text-bambu-gray",children:[s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Send print jobs"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage files and archives"})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx(Un,{className:"w-4 h-4 text-bambu-green mt-0.5 flex-shrink-0"}),s.jsx("span",{children:"Manage filament"})]})]})})]})]})]})]}),de==="backup"&&s.jsx(KYe,{}),ve&&s.jsx(Hn,{title:"Disable Authentication",message:"Are you sure you want to disable authentication? This will make your Bambuddy instance accessible without login. All users will remain in the database but authentication will be disabled.",confirmText:"Disable Authentication",variant:"danger",onConfirm:async()=>{try{await he.disableAuth(),o("Authentication disabled successfully","success"),await u(),Ze(!1),window.location.href="/"}catch(be){const Pe=be instanceof Error?be.message:"Failed to disable authentication";o(Pe,"error")}},onCancel:()=>Ze(!1)})]})}const Gie=t=>{const e=parseFloat(t);return(Math.trunc(e*1e3)/1e3).toFixed(3)},tZe=t=>t.startsWith("HH")?"HF":"S",nZe=t=>{const e=t.match(/^([A-Z]{2}\d{2})/);return e?e[1]:"HH00"},wD=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 Kj({profile:t,onEdit:e,onCopy:n,selectionMode:r,isSelected:i,onToggleSelect:a,note:o}){const l=tZe(t.nozzle_id),c=t.nozzle_diameter,u=()=>{r&&a?a():e()};return s.jsxs("div",{className:"flex items-center gap-2",children:[r&&s.jsx("button",{onClick:a,className:"text-bambu-gray hover:text-white transition-colors p-1",children:i?s.jsx(Lo,{className:"w-4 h-4 text-bambu-green"}):s.jsx(Io,{className:"w-4 h-4"})}),s.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:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-bambu-green font-mono text-sm font-bold whitespace-nowrap",children:Gie(t.k_value)}),s.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name||"Unnamed"}),o&&s.jsx("span",{title:"Has note",children:s.jsx(EK,{className:"w-3 h-3 text-yellow-500"})}),s.jsxs("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:[l," ",c]})]}),o&&s.jsxs("div",{className:"text-xs mt-0.5 truncate text-yellow-500/70",title:o,children:["Note: ",o.length>50?o.substring(0,50)+"...":o]})]}),!r&&n&&s.jsx("button",{onClick:d=>{d.stopPropagation(),n()},className:"text-bambu-gray hover:text-white transition-colors p-1",title:"Copy profile",children:s.jsx(ll,{className:"w-4 h-4"})})]})}function Xj({profile:t,printerId:e,nozzleDiameter:n,existingProfiles:r=[],isDualNozzle:i=!1,initialNote:a="",initialNoteKey:o=null,onClose:l,onSave:c,onSaveNote:u}){const{showToast:d}=qn(),[f,m]=w.useState(t?.name||""),[p,b]=w.useState(t?.k_value?Gie(t.k_value):"0.020"),[y,x]=w.useState(t?.filament_id||""),[v,S]=w.useState(t?.nozzle_id?nZe(t.nozzle_id):"HH00"),[M,A]=w.useState(t?.nozzle_diameter||n),[T,k]=w.useState(t?[t.extruder_id]:i?[0,1]:[0]),[E,O]=w.useState(!1),[C,D]=w.useState({current:0,total:0}),[U,L]=w.useState(a),q=Ur.useMemo(()=>{const J=new Map;for(const z of r)if(z.filament_id&&!J.has(z.filament_id)){const G=wD(z.name||"");J.set(z.filament_id,{id:z.filament_id,name:G||z.filament_id})}return Array.from(J.values()).sort((z,G)=>z.name.localeCompare(G.name))},[r]),P=mt({mutationFn:J=>(console.log("[KProfile] Calling API..."),he.setKProfile(e,J)),onSuccess:J=>{if(console.log("[KProfile] Save success:",J),d("K-profile saved"),u&&U!==a){let z;U===""&&o?z=o:t&&t.slot_id>0?z=t.setting_id||`slot_${t.slot_id}_${t.filament_id}_${t.extruder_id}`:z=`name_${f}_${y}`,u(z,U)}O(!0),setTimeout(()=>{O(!1),c()},2500)},onError:J=>{console.error("[KProfile] Save error:",J),d(J.message,"error"),O(!1)}}),W=mt({mutationFn:J=>(console.log("[KProfile] Deleting profile..."),he.deleteKProfile(e,J)),onSuccess:J=>{console.log("[KProfile] Delete success:",J),d("K-profile deleted"),O(!0),setTimeout(()=>{O(!1),l()},4e3)},onError:J=>{console.error("[KProfile] Delete error:",J),d(J.message,"error"),O(!1)}}),[K,V]=w.useState(!1),Q=()=>{t&&W.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})},re=async J=>{if(J.preventDefault(),i&&!t&&T.length===0){d("Please select at least one extruder","error");return}const z=parseFloat(p).toFixed(6),G=`${v}-${M}`;if(t||T.length===1){const ae={name:f,k_value:z,filament_id:y,nozzle_id:G,nozzle_diameter:M,extruder_id:t?t.extruder_id:T[0],setting_id:t?.setting_id,slot_id:t?.slot_id??0};console.log("[KProfile] Saving profile:",ae),P.mutate(ae);return}O(!0),D({current:1,total:T.length});const ne=T.map(ae=>({name:f,k_value:z,filament_id:y,nozzle_id:G,nozzle_diameter:M,extruder_id:ae,setting_id:void 0,slot_id:0}));console.log(`[KProfile] Saving ${ne.length} profiles in batch:`,ne);try{if(await he.setKProfilesBatch(e,ne),d(`K-profile saved to ${T.length} extruders`),u&&U){const ae=`name_${f}_${y}`;u(ae,U)}}catch(ae){console.error("[KProfile] Failed to save batch:",ae),d("Failed to save K-profiles","error"),O(!1),D({current:0,total:0});return}D({current:T.length,total:T.length}),setTimeout(()=>{O(!1),D({current:0,total:0}),c()},3e3)};return s.jsxs("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:[s.jsxs(St,{className:"w-full max-w-md relative",children:[E&&s.jsxs("div",{className:"absolute inset-0 bg-bambu-dark-secondary/90 flex flex-col items-center justify-center z-10 rounded-lg",children:[s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin mb-3"}),s.jsx("p",{className:"text-white font-medium",children:C.total>1?`Saving to extruder ${C.current}/${C.total}...`:"Syncing with printer..."}),s.jsx("p",{className:"text-bambu-gray text-sm mt-1",children:"Please wait"})]}),s.jsxs(Nt,{className:"p-0",children:[s.jsxs("div",{className:"flex items-center justify-between p-4 border-b border-bambu-dark-tertiary",children:[s.jsx("h2",{className:"text-xl font-semibold text-white",children:t?"Edit K-Profile":"Add K-Profile"}),s.jsx("button",{onClick:l,className:"text-bambu-gray hover:text-white transition-colors",disabled:E,children:s.jsx(Vt,{className:"w-5 h-5"})})]}),s.jsxs("form",{onSubmit:re,className:"p-4 space-y-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Profile Name"}),s.jsx("input",{type:"text",value:f,onChange:J=>m(J.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})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"K-Value"}),s.jsx("input",{type:"text",inputMode:"decimal",value:p,onChange:J=>{const z=J.target.value;(z===""||/^\d*\.?\d*$/.test(z))&&b(z)},onBlur:J=>{const z=parseFloat(J.target.value);isNaN(z)||b((Math.trunc(z*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}),s.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"})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Filament"}),s.jsxs("select",{value:y,onChange:J=>{const z=J.target.value;if(x(z),!t&&z&&!f){const G=q.find(ne=>ne.id===z);G&&m(`${v==="HH00"?"HF":"S"} ${G.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:[s.jsx("option",{value:"",children:"Select filament..."}),t?.filament_id&&s.jsx("option",{value:t.filament_id,children:q.find(J=>J.id===t.filament_id)?.name||t.filament_id},t.filament_id),!t&&q.map(J=>s.jsx("option",{value:J.id,children:J.name},J.id))]}),!t&&q.length===0&&s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"No filaments found. Create a K-profile in Bambu Studio first."})]}),s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Flow Type"}),s.jsxs("select",{value:v,onChange:J=>{const z=J.target.value;if(S(z),!t&&y&&!f){const G=q.find(ne=>ne.id===y);G&&m(`${z==="HS00"?"HF":"S"} ${G.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:[s.jsx("option",{value:"HH00",children:"High Flow"}),s.jsx("option",{value:"HS00",children:"Standard"})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle Size"}),s.jsxs("select",{value:M,onChange:J=>A(J.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:[s.jsx("option",{value:"0.2",children:"0.2mm"}),s.jsx("option",{value:"0.4",children:"0.4mm"}),s.jsx("option",{value:"0.6",children:"0.6mm"}),s.jsx("option",{value:"0.8",children:"0.8mm"})]})]})]}),i&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:t?"Extruder":"Extruders"}),t?s.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"}):s.jsxs("div",{className:"flex gap-4",children:[s.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:T.includes(1),onChange:J=>{J.target.checked?k([...T,1]):k(T.filter(z=>z!==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"}),s.jsx("span",{className:"text-white",children:"Left"})]}),s.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[s.jsx("input",{type:"checkbox",checked:T.includes(0),onChange:J=>{J.target.checked?k([...T,0]):k(T.filter(z=>z!==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"}),s.jsx("span",{className:"text-white",children:"Right"})]})]})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Notes (stored locally)"}),s.jsx("textarea",{value:U,onChange:J=>L(J.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"}),s.jsx("p",{className:"text-xs text-bambu-gray mt-1",children:"Notes are saved in Bambuddy, not on the printer"})]}),s.jsxs("div",{className:"flex gap-2 pt-4",children:[t&&s.jsx(Se,{type:"button",variant:"secondary",onClick:()=>V(!0),disabled:W.isPending||E,className:"text-red-500 hover:bg-red-500/10",children:W.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"})}),s.jsx(Se,{type:"button",variant:"secondary",onClick:l,disabled:E,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{type:"submit",disabled:P.isPending||E,className:"flex-1",children:[P.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(oN,{className:"w-4 h-4"}),"Save"]})]})]})]})]}),K&&s.jsx("div",{className:"fixed inset-0 bg-black/70 flex items-center justify-center z-[60]",children:s.jsx(St,{className:"w-full max-w-sm",children:s.jsxs(Nt,{className:"p-6",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[s.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:s.jsx(dn,{className:"w-5 h-5 text-red-500"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profile"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),s.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",s.jsxs("span",{className:"text-white font-medium",children:['"',t?.name,'"']})," from the printer?"]}),s.jsxs("div",{className:"flex gap-3",children:[s.jsx(Se,{variant:"secondary",onClick:()=>V(!1),className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:()=>{V(!1),Q()},disabled:W.isPending,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[W.isPending?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}const e1={NOZZLE_DIAMETER:"bambusy_kprofiles_nozzle",SORT_OPTION:"bambusy_kprofiles_sort"};function rZe(){const{showToast:t}=qn(),[e,n]=w.useState(null),[r,i]=w.useState(()=>localStorage.getItem(e1.NOZZLE_DIAMETER)||"0.4"),[a,o]=w.useState(null),[l,c]=w.useState(!1),[u,d]=w.useState(null),[f,m]=w.useState(""),[p,b]=w.useState("all"),[y,x]=w.useState("all"),[v,S]=w.useState(()=>localStorage.getItem(e1.SORT_OPTION)||"name"),[M,A]=w.useState(!1),[T,k]=w.useState(new Set),[E,O]=w.useState(!1),[C,D]=w.useState(!1),U=w.useCallback(B=>`${B.slot_id}_${B.extruder_id}`,[]);w.useEffect(()=>{localStorage.setItem(e1.NOZZLE_DIAMETER,r)},[r]),w.useEffect(()=>{localStorage.setItem(e1.SORT_OPTION,v)},[v]);const{data:L,isLoading:q}=ut({queryKey:["printers"],queryFn:he.getPrinters}),{data:P,isLoading:W,isFetching:K,error:V,refetch:Q}=ut({queryKey:["kprofiles",e,r],queryFn:async()=>{console.log("[KProfiles] Fetching profiles for printer",e,"nozzle",r);const B=await he.getKProfiles(e,r);return console.log("[KProfiles] Received profiles:",B?.profiles?.length||0,"profiles"),B},enabled:!!e,retry:!1,staleTime:0,gcTime:0,refetchOnMount:"always"}),{data:re}=ut({queryKey:["kprofiles",e,"0.4"],queryFn:()=>he.getKProfiles(e,"0.4"),enabled:!!e,staleTime:6e4}),{data:J,refetch:z}=ut({queryKey:["kprofile-notes",e],queryFn:()=>he.getKProfileNotes(e),enabled:!!e,staleTime:3e4}),G=V?.message?.includes("not connected");w.useEffect(()=>{if(!e&&L&&L.length>0){const B=L.find(I=>I.is_active);B&&n(B.id)}},[e,L]),w.useEffect(()=>{if(e){const B=setTimeout(()=>{Q()},150);return()=>clearTimeout(B)}},[e,r]);const ne=L?.filter(B=>B.is_active)||[],ae=Ur.useMemo(()=>P?.profiles?P.profiles.filter(I=>{const ce=f.toLowerCase(),Te=!ce||I.name.toLowerCase().includes(ce)||I.filament_id.toLowerCase().includes(ce),Le=p==="all"||p==="left"&&I.extruder_id===1||p==="right"&&I.extruder_id===0,Ee=y==="all"||y==="hf"&&I.nozzle_id.startsWith("HH")||y==="s"&&I.nozzle_id.startsWith("HS");return Te&&Le&&Ee}).sort((I,ce)=>{switch(v){case"k_value":return parseFloat(I.k_value)-parseFloat(ce.k_value);case"filament":return wD(I.name).localeCompare(wD(ce.name));default:return I.name.localeCompare(ce.name)}}):[],[P?.profiles,f,p,y,v]),de=L?.find(B=>B.id===e),fe=de?.nozzle_count===2;w.useEffect(()=>{const B=I=>{I.target instanceof HTMLInputElement||I.target instanceof HTMLTextAreaElement||I.target instanceof HTMLSelectElement||a||l||u||(I.key==="r"||I.key==="R"?(I.preventDefault(),Q()):I.key==="n"||I.key==="N"?(I.preventDefault(),c(!0)):I.key==="Escape"&&M&&(I.preventDefault(),A(!1),k(new Set)))};return window.addEventListener("keydown",B),()=>window.removeEventListener("keydown",B)},[a,l,u,M,Q]);const ie=w.useCallback(()=>{if(!P?.profiles||P.profiles.length===0){t("No profiles to export","error");return}const B={version:1,exported_at:new Date().toISOString(),printer:de?.name||"Unknown",nozzle_diameter:r,profiles:P.profiles.map(Le=>({name:Le.name,k_value:Le.k_value,filament_id:Le.filament_id,nozzle_id:Le.nozzle_id,nozzle_diameter:Le.nozzle_diameter,extruder_id:Le.extruder_id}))},I=new Blob([JSON.stringify(B,null,2)],{type:"application/json"}),ce=URL.createObjectURL(I),Te=document.createElement("a");Te.href=ce,Te.download=`kprofiles_${de?.name||"printer"}_${r}mm_${new Date().toISOString().split("T")[0]}.json`,document.body.appendChild(Te),Te.click(),document.body.removeChild(Te),URL.revokeObjectURL(ce),t(`Exported ${P.profiles.length} profiles`)},[P?.profiles,de,r,t]),Ae=w.useCallback(()=>{const B=document.createElement("input");B.type="file",B.accept=".json",B.onchange=async I=>{const ce=I.target.files?.[0];if(ce)try{const Te=await ce.text(),Le=JSON.parse(Te);if(!Le.profiles||!Array.isArray(Le.profiles)){t("Invalid file format","error");return}let Ee=0;for(const $ of Le.profiles)if(!(!$.name||!$.k_value||!$.filament_id))try{await he.setKProfile(e,{name:$.name,k_value:parseFloat($.k_value).toFixed(6),filament_id:$.filament_id,nozzle_id:$.nozzle_id||`HH00-${r}`,nozzle_diameter:$.nozzle_diameter||r,extruder_id:$.extruder_id??0,slot_id:0}),Ee++,await new Promise(j=>setTimeout(j,500))}catch(j){console.error("Failed to import profile:",$.name,j)}t(`Imported ${Ee} of ${Le.profiles.length} profiles`),Q()}catch(Te){console.error("Import error:",Te),t("Failed to parse import file","error")}},B.click()},[e,r,t,Q]),me=w.useCallback(B=>{k(I=>{const ce=new Set(I);return ce.has(B)?ce.delete(B):ce.add(B),ce})},[]),Ne=w.useCallback(()=>{k(new Set(ae.map(B=>U(B))))},[ae,U]),ye=w.useCallback(()=>{T.size!==0&&O(!0)},[T.size]),le=w.useCallback(async()=>{const B=ae.filter(ce=>T.has(U(ce)));D(!0);let I=0;for(const ce of B)try{await he.deleteKProfile(e,{slot_id:ce.slot_id,extruder_id:ce.extruder_id,nozzle_id:ce.nozzle_id,nozzle_diameter:ce.nozzle_diameter,filament_id:ce.filament_id,setting_id:ce.setting_id}),I++,await new Promise(Te=>setTimeout(Te,300))}catch(Te){console.error("Failed to delete profile:",ce.name,Te)}t(`Deleted ${I} profiles`),D(!1),O(!1),A(!1),k(new Set),Q()},[e,T,ae,t,Q,U]),$e=w.useCallback(B=>{const I=[];return B.setting_id&&I.push(B.setting_id),I.push(`slot_${B.slot_id}_${B.filament_id}_${B.extruder_id}`),I.push(`name_${B.name}_${B.filament_id}`),I},[]),H=w.useCallback(async(B,I)=>{if(e)try{await he.setKProfileNote(e,B,I),z()}catch(ce){console.error("Failed to save note:",ce),t("Failed to save note","error")}},[e,z,t]),ee=w.useCallback(B=>{if(!J?.notes)return{note:"",key:null};const I=$e(B);for(const ce of I)if(J.notes[ce])return{note:J.notes[ce],key:ce};return{note:"",key:null}},[J,$e]),X=w.useCallback(B=>ee(B).note,[ee]);return q?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):!L||L.length===0?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(oa,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Printers Configured"}),s.jsx("p",{className:"text-bambu-gray",children:"Add a printer in Settings to manage K-profiles"})]})}):ne.length===0?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(qr,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Active Printers"}),s.jsx("p",{className:"text-bambu-gray",children:"Enable a printer connection to view its K-profiles"})]})}):s.jsxs(s.Fragment,{children:[K&&!W&&s.jsxs("div",{className:"fixed inset-0 bg-black/50 flex flex-col items-center justify-center z-40",children:[s.jsx(dt,{className:"w-10 h-10 text-bambu-green animate-spin mb-3"}),s.jsx("p",{className:"text-white font-medium",children:"Loading K-Profiles..."})]}),s.jsxs("div",{className:"flex flex-wrap gap-4 mb-6",children:[s.jsxs("div",{className:"flex-1 min-w-48",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Printer"}),s.jsx("select",{value:e||"",onChange:B=>n(parseInt(B.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:ne.map(B=>s.jsx("option",{value:B.id,children:B.name},B.id))})]}),s.jsxs("div",{className:"w-32",children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Nozzle"}),s.jsxs("select",{value:r,onChange:B=>i(B.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:[s.jsx("option",{value:"0.2",children:"0.2mm"}),s.jsx("option",{value:"0.4",children:"0.4mm"}),s.jsx("option",{value:"0.6",children:"0.6mm"}),s.jsx("option",{value:"0.8",children:"0.8mm"})]})]}),s.jsxs("div",{className:"flex items-end gap-2",children:[s.jsxs(Se,{variant:"secondary",onClick:()=>Q(),disabled:K,children:[s.jsx(xr,{className:`w-4 h-4 ${K?"animate-spin":""}`}),"Refresh"]}),s.jsxs(Se,{onClick:()=>c(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Add Profile"]})]})]}),s.jsxs("div",{className:"flex flex-wrap gap-4 mb-4",children:[s.jsxs("div",{className:"flex-1 min-w-48 relative",children:[s.jsx(Ea,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray"}),s.jsx("input",{type:"text",value:f,onChange:B=>m(B.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"})]}),fe&&s.jsx("div",{className:"w-36",children:s.jsxs("select",{value:p,onChange:B=>b(B.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:[s.jsx("option",{value:"all",children:"All Extruders"}),s.jsx("option",{value:"left",children:"Left Only"}),s.jsx("option",{value:"right",children:"Right Only"})]})}),s.jsx("div",{className:"w-32",children:s.jsxs("select",{value:y,onChange:B=>x(B.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:[s.jsx("option",{value:"all",children:"All Flow"}),s.jsx("option",{value:"hf",children:"HF Only"}),s.jsx("option",{value:"s",children:"S Only"})]})}),s.jsx("div",{className:"w-32",children:s.jsxs("select",{value:v,onChange:B=>S(B.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:[s.jsx("option",{value:"name",children:"Sort: Name"}),s.jsx("option",{value:"k_value",children:"Sort: K-Value"}),s.jsx("option",{value:"filament",children:"Sort: Filament"})]})})]}),s.jsxs("div",{className:"flex flex-wrap gap-2 mb-6",children:[s.jsxs(Se,{variant:"secondary",onClick:ie,disabled:!P?.profiles?.length,title:"Export profiles to JSON",children:[s.jsx(Hr,{className:"w-4 h-4"}),"Export"]}),s.jsxs(Se,{variant:"secondary",onClick:Ae,title:"Import profiles from JSON",children:[s.jsx(pi,{className:"w-4 h-4"}),"Import"]}),s.jsx("div",{className:"flex-1"}),M?s.jsxs(s.Fragment,{children:[s.jsxs(Se,{variant:"secondary",onClick:Ne,title:"Select all visible profiles",children:[s.jsx(Lo,{className:"w-4 h-4"}),"Select All"]}),s.jsxs(Se,{variant:"secondary",onClick:ye,disabled:T.size===0,className:"text-red-500 hover:bg-red-500/10",title:`Delete ${T.size} selected profiles`,children:[s.jsx(dn,{className:"w-4 h-4"}),"Delete (",T.size,")"]}),s.jsxs(Se,{variant:"secondary",onClick:()=>{A(!1),k(new Set)},children:[s.jsx(Vt,{className:"w-4 h-4"}),"Cancel"]})]}):s.jsxs(Se,{variant:"secondary",onClick:()=>A(!0),disabled:!ae.length,title:"Enter selection mode for bulk delete",children:[s.jsx(Lo,{className:"w-4 h-4"}),"Select"]})]}),W?s.jsx("div",{className:"flex justify-center py-12",children:s.jsx(dt,{className:"w-8 h-8 text-bambu-green animate-spin"})}):G?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(ex,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"Printer Offline"}),s.jsx("p",{className:"text-bambu-gray mb-4",children:"The selected printer is not connected. Power it on to view K-profiles."}),s.jsxs(Se,{variant:"secondary",onClick:()=>Q(),children:[s.jsx(xr,{className:"w-4 h-4"}),"Retry"]})]})}):ae.length>0?fe?s.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Left Extruder"}),s.jsx("div",{className:"space-y-1",children:ae.filter(B=>B.extruder_id===1).map(B=>s.jsx(Kj,{profile:B,onEdit:()=>o(B),onCopy:()=>d(B),selectionMode:M,isSelected:T.has(U(B)),onToggleSelect:()=>me(U(B)),note:X(B)},U(B)))})]}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-sm font-medium text-bambu-gray mb-2 px-1",children:"Right Extruder"}),s.jsx("div",{className:"space-y-1",children:ae.filter(B=>B.extruder_id===0).map(B=>s.jsx(Kj,{profile:B,onEdit:()=>o(B),onCopy:()=>d(B),selectionMode:M,isSelected:T.has(U(B)),onToggleSelect:()=>me(U(B)),note:X(B)},U(B)))})]})]}):s.jsx("div",{className:"space-y-1",children:ae.map(B=>s.jsx(Kj,{profile:B,onEdit:()=>o(B),onCopy:()=>d(B),selectionMode:M,isSelected:T.has(U(B)),onToggleSelect:()=>me(U(B)),note:X(B)},U(B)))}):f||p!=="all"||y!=="all"?s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(Ea,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No Matching Profiles"}),s.jsx("p",{className:"text-bambu-gray",children:"No profiles match your search criteria"})]})}):s.jsx(St,{children:s.jsxs(Nt,{className:"py-12 text-center",children:[s.jsx(oN,{className:"w-12 h-12 text-bambu-gray mx-auto mb-4"}),s.jsx("h3",{className:"text-lg font-semibold text-white mb-2",children:"No K-Profiles"}),s.jsxs("p",{className:"text-bambu-gray mb-4",children:["No pressure advance profiles found for ",r,"mm nozzle"]}),s.jsxs(Se,{onClick:()=>c(!0),children:[s.jsx(nr,{className:"w-4 h-4"}),"Create First Profile"]})]})}),a&&e&&(()=>{const{note:B,key:I}=ee(a);return s.jsx(Xj,{profile:a,printerId:e,nozzleDiameter:r,existingProfiles:re?.profiles||P?.profiles,isDualNozzle:fe,initialNote:B,initialNoteKey:I,onSaveNote:H,onClose:()=>{console.log("[KProfiles] Edit modal onClose - refetching profiles..."),o(null),Q()},onSave:()=>{o(null),Q()}})})(),l&&e&&s.jsx(Xj,{printerId:e,nozzleDiameter:r,existingProfiles:re?.profiles||P?.profiles,isDualNozzle:fe,onSaveNote:H,onClose:()=>{c(!1),Q()},onSave:()=>{c(!1),Q()}}),u&&e&&s.jsx(Xj,{printerId:e,nozzleDiameter:r,existingProfiles:re?.profiles||P?.profiles,isDualNozzle:fe,onSaveNote:H,profile:{...u,slot_id:0,name:`${u.name} (Copy)`},onClose:()=>{d(null),Q()},onSave:()=>{d(null),Q()}}),E&&s.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4",children:s.jsx(St,{className:"w-full max-w-sm",children:s.jsxs(Nt,{className:"p-6",children:[s.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[s.jsx("div",{className:"w-10 h-10 rounded-full bg-red-500/20 flex items-center justify-center",children:s.jsx(dn,{className:"w-5 h-5 text-red-500"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold text-white",children:"Delete Profiles"}),s.jsx("p",{className:"text-sm text-bambu-gray",children:"This cannot be undone"})]})]}),s.jsxs("p",{className:"text-bambu-gray mb-6",children:["Are you sure you want to delete ",s.jsx("span",{className:"text-white font-medium",children:T.size})," selected profiles from the printer?"]}),s.jsxs("div",{className:"flex gap-3",children:[s.jsx(Se,{variant:"secondary",onClick:()=>O(!1),disabled:C,className:"flex-1",children:"Cancel"}),s.jsxs(Se,{onClick:le,disabled:C,className:"flex-1 bg-red-500 hover:bg-red-600 text-white",children:[C?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(dn,{className:"w-4 h-4"}),"Delete"]})]})]})})})]})}function _I(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),o=a?(a[1]||a[2])+"mm":null,l=n.match(/(\d+\.?\d*)mm\s*(?:Standard|Fine|Extra Fine|Draft|Quality)?/i),c=l?l[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:o,layerHeight:c,filamentType:d}}function _y(t){return/^(P[FPM]US|PF\d|PP\d)/.test(t)}function iZe(t){const e=Ni(t);if(!e)return"";const r=new Date().getTime()-e.getTime(),i=Math.floor(r/6e4),a=Math.floor(r/36e5),o=Math.floor(r/864e5);return i<1?"Just now":i<60?`${i}m ago`:a<24?`${a}h ago`:o<7?`${o}d ago`:e.toLocaleDateString()}function aZe({onSuccess:t}){const{showToast:e}=qn(),[n,r]=w.useState("email"),[i,a]=w.useState(""),[o,l]=w.useState(""),[c,u]=w.useState(""),[d,f]=w.useState(""),[m,p]=w.useState("global"),b=mt({mutationFn:()=>he.cloudLogin(i,o,m),onSuccess:M=>{M.success?(e("Logged in successfully"),t()):M.needs_verification?(e("Verification code sent to your email"),r("code")):e(M.message,"error")},onError:M=>e(M.message,"error")}),y=mt({mutationFn:()=>he.cloudVerify(i,c),onSuccess:M=>{M.success?(e("Logged in successfully"),t()):e(M.message,"error")},onError:M=>e(M.message,"error")}),x=mt({mutationFn:()=>he.cloudSetToken(d),onSuccess:()=>{e("Token set successfully"),t()},onError:M=>e(M.message,"error")}),v=M=>{M.preventDefault(),n==="email"?b.mutate():n==="code"?y.mutate():n==="token"&&x.mutate()},S=b.isPending||y.isPending||x.isPending;return s.jsx(St,{className:"max-w-md mx-auto",children:s.jsxs(Nt,{children:[s.jsxs("div",{className:"text-center mb-6",children:[s.jsx("div",{className:"inline-flex items-center justify-center w-12 h-12 rounded-xl bg-bambu-green/20 mb-3",children:s.jsx(iN,{className:"w-6 h-6 text-bambu-green"})}),s.jsx("h2",{className:"text-xl font-semibold text-white",children:"Connect to Bambu Cloud"}),s.jsx("p",{className:"text-sm text-bambu-gray mt-1",children:"Sync your slicer presets across devices"})]}),s.jsxs("form",{onSubmit:v,className:"space-y-4",children:[n==="email"&&s.jsxs(s.Fragment,{children:[s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Email"}),s.jsx("input",{type:"email",value:i,onChange:M=>a(M.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})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Password"}),s.jsx("input",{type:"password",value:o,onChange:M=>l(M.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})]}),s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Region"}),s.jsxs("select",{value:m,onChange:M=>p(M.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:[s.jsx("option",{value:"global",children:"Global"}),s.jsx("option",{value:"china",children:"China"})]})]})]}),n==="code"&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Verification Code"}),s.jsxs("p",{className:"text-xs text-bambu-gray mb-2",children:["Check your email (",i,") for a 6-digit code"]}),s.jsx("input",{type:"text",value:c,onChange:M=>u(M.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"&&s.jsxs("div",{children:[s.jsx("label",{className:"block text-sm text-bambu-gray mb-1",children:"Access Token"}),s.jsx("p",{className:"text-xs text-bambu-gray mb-2",children:"Paste your Bambu Lab access token (from Bambu Studio)"}),s.jsx("textarea",{value:d,onChange:M=>f(M.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})]}),s.jsxs("div",{className:"flex gap-2",children:[n==="code"&&s.jsx(Se,{type:"button",variant:"secondary",onClick:()=>r("email"),className:"flex-1",children:"Back"}),s.jsxs(Se,{type:"submit",disabled:S,className:"flex-1",children:[S?s.jsx(dt,{className:"w-4 h-4 animate-spin"}):s.jsx(Zz,{className:"w-4 h-4"}),n==="email"?"Login":n==="code"?"Verify":"Set Token"]})]}),n==="email"&&s.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:s.jsxs("button",{type:"button",onClick:()=>r("token"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[s.jsx(mf,{className:"w-4 h-4"}),"Use access token instead"]})}),n==="token"&&s.jsx("div",{className:"pt-4 border-t border-bambu-dark-tertiary",children:s.jsxs("button",{type:"button",onClick:()=>r("email"),className:"text-sm text-bambu-gray hover:text-white flex items-center gap-2 transition-colors",children:[s.jsx(Zz,{className:"w-4 h-4"}),"Login with email instead"]})})]})]})})}function $p({label:t,value:e,options:n,onChange:r}){const[i,a]=w.useState(!1),o=n.find(l=>l.value===e);return s.jsxs("div",{className:"relative",children:[s.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:[s.jsxs("span",{className:"text-bambu-gray",children:[t,":"]}),s.jsx("span",{children:o?.label||"All"}),s.jsx(Oi,{className:`w-4 h-4 text-bambu-gray transition-transform ${i?"rotate-180":""}`})]}),i&&s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"fixed inset-0 z-10",onClick:()=>a(!1)}),s.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(l=>s.jsxs("button",{onClick:()=>{r(l.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===l.value?"text-bambu-green":"text-white"}`,children:[s.jsx("span",{children:l.label}),l.count!==void 0&&s.jsx("span",{className:"text-bambu-gray text-xs",children:l.count})]},l.value))})]})]})}function sZe(){const[t,e]=w.useState(!1);w.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?s.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:s.jsx(bg,{className:"w-5 h-5"})}):null}function Yj({setting:t,onClick:e,onDuplicate:n,compareMode:r,isCompareSelected:i,compareIndex:a,compareDisabled:o}){const l=_I(t.name),c=_y(t.setting_id);return s.jsxs("div",{className:"flex items-center gap-2 group",children:[s.jsx("button",{onClick:e,disabled:o,className:`flex-1 text-left px-3 py-2 rounded transition-colors ${i?"bg-blue-500/20 border border-blue-500/50":o?"bg-bambu-dark/50 opacity-40 cursor-not-allowed":"bg-bambu-dark hover:bg-bambu-dark-tertiary"} ${r&&!o?"cursor-pointer":""}`,children:s.jsxs("div",{className:"flex items-center gap-2",children:[i&&a!==void 0&&s.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&&s.jsx("span",{className:"flex-shrink-0 w-1.5 h-1.5 rounded-full bg-bambu-green",title:"My preset (editable)"}),s.jsx("span",{className:"text-white text-sm truncate flex-1",title:t.name,children:t.name}),l.filamentType&&t.type==="filament"&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:l.filamentType}),l.layerHeight&&t.type==="process"&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:l.layerHeight}),l.printer&&s.jsx("span",{className:"text-xs text-bambu-gray whitespace-nowrap",children:l.printer})]})}),s.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:s.jsx(ll,{className:"w-4 h-4"})})]})}function SD(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 51abf58dd..79af4d4ea 100644 --- a/static/index.html +++ b/static/index.html @@ -23,7 +23,7 @@ - +