1import{r as d,j as e,c as Fs,e as Bs,d as Ks,t as x}from"./index-DeZ5oCX2.js";import{u as le}from"./useQuery-BkBhHLuO.js";import{u as v}from"./useServerFn-S1fedotE.js";import{l as Gs,a as Qs,g as Us,b as Hs,c as Os,s as Xs,e as Vs,f as Ws,h as Ys,i as Zs,t as Js,m as et,j as st,k as tt,n as at,o as nt,p as it}from"./gmail.functions-Yd0GEmaD.js";import{I as de}from"./input-NPgQ5bSp.js";import{B as f}from"./button-CDuhDpG0.js";import{u as gs,c as os}from"./utils-DbgoUc7f.js";import{P as _e,a as rt,c as ls,b as ct}from"./index-BxRFAMWx.js";import{u as ot,a as lt}from"./index-DNUrICy6.js";import{P as dt}from"./index-DFYoLqTb.js";import{C as mt}from"./check-DZ5Oh4HW.js";import{L as T}from"./loader-circle-CJ_fbZIQ.js";import{M as ut}from"./mail-TJtpnsz-.js";import{P as ds}from"./plus-BXkhE1fR.js";import{c as K}from"./createLucideIcon-Cx-liIvB.js";import{I as xt}from"./inbox-lmQ-nzqR.js";import{R as ht}from"./refresh-cw-DibRRauM.js";import{S as ft}from"./search-CvMXT7xO.js";import{T as se}from"./trash-2-G9yA8wFu.js";import{X as we}from"./x-BZC505SW.js";import{C as pt}from"./chevron-down-ef1XHQdT.js";import{S as ms}from"./send-BQFrPmlU.js";import{S as gt}from"./save-DjghXJon.js";import"./auth-middleware-DagjdbmO.js";const bt=[["path",{d:"m15 18-6-6 6-6",key:"1wnfg3"}]],us=K("chevron-left",bt);const vt=[["path",{d:"m9 18 6-6-6-6",key:"mthhwq"}]],xs=K("chevron-right",vt);const jt=[["path",{d:"M12 15V3",key:"m9g1x1"}],["path",{d:"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4",key:"ih7n3h"}],["path",{d:"m7 10 5 5 5-5",key:"brsn70"}]],yt=K("download",jt);const kt=[["path",{d:"M12.659 22H18a2 2 0 0 0 2-2V8a2.4 2.4 0 0 0-.706-1.706l-3.588-3.588A2.4 2.4 0 0 0 14 2H6a2 2 0 0 0-2 2v9.34",key:"o6klzx"}],["path",{d:"M14 2v5a1 1 0 0 0 1 1h5",key:"wfsgrz"}],["path",{d:"M10.378 12.622a1 1 0 0 1 3 3.003L8.36 20.637a2 2 0 0 1-.854.506l-2.867.837a.5.5 0 0 1-.62-.62l.836-2.869a2 2 0 0 1 .506-.853z",key:"zhnas1"}]],Nt=K("file-pen",kt);const wt=[["path",{d:"M22 13V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v12c0 1.1.9 2 2 2h8",key:"12jkf8"}],["path",{d:"m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7",key:"1ocrg3"}],["path",{d:"m16 19 2 2 4-4",key:"1b14m6"}]],hs=K("mail-check",wt);const Ct=[["path",{d:"M21.2 8.4c.5.38.8.97.8 1.6v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V10a2 2 0 0 1 .8-1.6l8-6a2 2 0 0 1 2.4 0l8 6Z",key:"1jhwl8"}],["path",{d:"m22 10-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 10",key:"1qfld7"}]],Re=K("mail-open",Ct);const zt=[["path",{d:"m16 6-8.414 8.586a2 2 0 0 0 2.829 2.829l8.414-8.586a4 4 0 1 0-5.657-5.657l-8.379 8.551a6 6 0 1 0 8.485 8.485l8.379-8.551",key:"1miecu"}]],te=K("paperclip",zt);const St=[["path",{d:"M13 21h8",key:"1jsn5i"}],["path",{d:"m15 5 4 4",key:"1mk7zo"}],["path",{d:"M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z",key:"1a8usu"}]],It=K("pencil-line",St);const Dt=[["path",{d:"M20 18v-2a4 4 0 0 0-4-4H4",key:"5vmcpk"}],["path",{d:"m9 17-5-5 5-5",key:"nvlc11"}]],Mt=K("reply",Dt);var Ce="Checkbox",[Et]=ct(Ce),[Rt,Ae]=Et(Ce);function Pt(n){const{__scopeCheckbox:r,checked:m,children:p,defaultChecked:g,disabled:u,form:j,name:S,onCheckedChange:I,required:q,value:$="on",internal_do_not_use_render:D}=n,[P,V]=rt({prop:m,defaultProp:g??!1,onChange:I,caller:Ce}),[M,G]=d.useState(null),[Q,N]=d.useState(null),y=d.useRef(!1),o=M?!!j||!!M.closest("form"):!0,W={checked:P,disabled:u,setChecked:V,control:M,setControl:G,name:S,form:j,value:$,hasConsumerStoppedPropagationRef:y,required:q,defaultChecked:X(g)?!1:g,isFormControl:o,bubbleInput:Q,setBubbleInput:N};return e.jsx(Rt,{scope:r,...W,children:Tt(D)?D(W):p})}var bs="CheckboxTrigger",vs=d.forwardRef(({__scopeCheckbox:n,onKeyDown:r,onClick:m,...p},g)=>{const{control:u,value:j,disabled:S,checked:I,required:q,setControl:$,setChecked:D,hasConsumerStoppedPropagationRef:P,isFormControl:V,bubbleInput:M}=Ae(bs,n),G=gs(g,$),Q=d.useRef(I);return d.useEffect(()=>{const N=u?.form;if(N){const y=()=>D(Q.current);return N.addEventListener("reset",y),()=>N.removeEventListener("reset",y)}},[u,D]),e.jsx(_e.button,{type:"button",role:"checkbox","aria-checked":X(I)?"mixed":I,"aria-required":q,"data-state":ws(I),"data-disabled":S?"":void 0,disabled:S,value:j,...p,ref:G,onKeyDown:ls(r,N=>{N.key==="Enter"&&N.preventDefault()}),onClick:ls(m,N=>{D(y=>X(y)?!0:!y),M&&V&&(P.current=N.isPropagationStopped(),P.current||N.stopPropagation())})})});vs.displayName=bs;var qe=d.forwardRef((n,r)=>{const{__scopeCheckbox:m,name:p,checked:g,defaultChecked:u,required:j,disabled:S,value:
1I,onCheckedChange:q,form:$,...D}=n;return e.jsx(Pt,{__scopeCheckbox:m,checked:g,defaultChecked:u,disabled:S,required:j,onCheckedChange:q,name:p,form:$,value:I,internal_do_not_use_render:({isFormControl:P})=>e.jsxs(e.Fragment,{children:[e.jsx(vs,{...D,ref:r,__scopeCheckbox:m}),P&&e.jsx(Ns,{__scopeCheckbox:m})]})})});qe.displayName=Ce;var js="CheckboxIndicator",ys=d.forwardRef((n,r)=>{const{__scopeCheckbox:m,forceMount:p,...g}=n,u=Ae(js,m);return e.jsx(dt,{present:p||X(u.checked)||u.checked===!0,children:e.jsx(_e.span,{"data-state":ws(u.checked),"data-disabled":u.disabled?"":void 0,...g,ref:r,style:{pointerEvents:"none",...n.style}})})});ys.displayName=js;var ks="CheckboxBubbleInput",Ns=d.forwardRef(({__scopeCheckbox:n,...r},m)=>{const{control:p,hasConsumerStoppedPropagationRef:g,checked:u,defaultChecked:j,required:S,disabled:I,name:q,value:$,form:D,bubbleInput:P,setBubbleInput:V}=Ae(ks,n),M=gs(m,V),G=ot(u),Q=lt(p);d.useEffect(()=>{const y=P;if(!y)return;const o=window.HTMLInputElement.prototype,xe=Object.getOwnPropertyDescriptor(o,"checked").set,Y=!g.current;if(G!==u&&xe){const he=new Event("click",{bubbles:Y});y.indeterminate=X(u),xe.call(y,X(u)?!1:u),y.dispatchEvent(he)}},[P,G,u,g]);const N=d.useRef(X(u)?!1:u);return e.jsx(_e.input,{type:"checkbox","aria-hidden":!0,defaultChecked:j??N.current,required:S,disabled:I,name:q,value:$,form:D,...r,tabIndex:-1,ref:M,style:{...r.style,...Q,position:"absolute",pointerEvents:"none",opacity:0,margin:0,transform:"translateX(-100%)"}})});Ns.displayName=ks;function Tt(n){return typeof n=="function"}function X(n){return n==="indeterminate"}function ws(n){return X(n)?"indeterminate":n?"checked":"unchecked"}const me=d.forwardRef(({className:n,...r},m)=>e.jsx(qe,{ref:m,className:os("grid place-content-center peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow cursor-pointer focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",n),...r,children:e.jsx(ys,{className:os("grid place-content-center text-current"),children:e.jsx(mt,{className:"h-4 w-4"})})}));me.displayName=qe.displayName;const Cs=7*1024*1024,Le=20*1024*1024;function fs(n){if(!n)return"";const r=new Date(n),m=new Date;return r.toDateString()===m.toDateString()?r.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}):r.toLocaleDateString([],{month:"short",day:"numeric"})}function ue(n){return n?n<1024?`${n} B`:n<1024*1024?`${(n/1024).toFixed(0)} KB`:`${(n/(1024*1024)).toFixed(1)} MB`:""}function Lt(n){return new Promise((r,m)=>{const p=new FileReader;p.onload=()=>{const g=p.result;r(g.split(",")[1]??"")},p.onerror=m,p.readAsDataURL(n)})}function _t(n,r,m){const p=Uint8Array.from(atob(n),S=>S.charCodeAt(0)),g=new Blob([p],{type:m||"application/octet-stream"}),u=URL.createObjectURL(g),j=document.createElement("a");j.href=u,j.download=r,document.body.appendChild(j),j.click(),j.remove(),URL.revokeObjectURL(u)}const Pe={open:!1,draftId:void 0,to:"",cc:"",bcc:"",subject:"",body:"",showCc:!1,attachments:[]};function At({att:n,onRemove:r}){const p=n.mimeType.startsWith("image/")?`data:${n.mimeType};base64,${n.content}`:null;return e.jsxs("span",{className:"inline-flex items-center gap-1.5 rounded-sm border rule-line px-2 py-1 text-xs bg-background",children:[p?e.jsx("img",{src:p,alt:`Preview of attached image ${n.filename.replace(/\.[^.]+$/,"")}`,className:"size-7 object-cover rounded-sm border rule-line"}):e.jsx(te,{className:"size-3"}),e.jsx("span",{className:"truncate max-w-[140px]",title:n.filename,children:n.filename}),e.jsx("span",{className:"text-ink-muted",children:ue(n.size)}),e.jsx("button",{type:"button",onClick:r,title:"Remove attachment",className:"rounded-sm hover:text-destructive p-0.5",children:e.jsx(we,{className:"size-3"})})]})}function ps({attachments:n,onRemove:r}){if(n.length===0)return null;const m=n.reduce((g,u)=>g+u.size,0),p=m>Le;return e.jsxs("div",{className:"space-y-1",children:[e.jsx("div",{className:"flex flex-wrap gap-2",children:n.map((g,u)=>e.jsx(At,{att:g,onRemove:()=>r(u)},u))}),e.jsxs("p",{className:`text-[10px] font-mono ${p?"text-destructive":"text-ink-muted"}`,children:[ue(m)," / ",ue(Le)," · max"," ",ue(Cs)," per file"]})]})}function oa(){const n=Fs({from:"/mail"}),r=Bs({from:"/_authenticated/mail"}),m=Ks(),p=v(Gs),g=v(Qs),u=v(Us),j=v(Hs),S=v(Os),I=v(Xs),q=v(Vs),$=v(Ws),D=v(Ys),P=v(Zs),V=v(Js);v(et);const M=v(st),G=v(tt),Q=v(at),N=v(nt),y=v(it),o=r.view??"inbox",[W,xe]=d.useState(r.q??""),[Y,he]=d.useState(!1),[ze,Z]=d.useState(!1),[Se,ae]=d.useState(""),[fe,J]=d.useState([]),[Ie,$e]=d.useState(!1),[c,E]=d.use
1State(Pe),[pe,ge]=d.useState(null),[b,C]=d.useState(new Set),[U,H]=d.useState(!1),[zs,Fe]=d.useState([void 0]),[ne,De]=d.useState(0),[Ss,Be]=d.useState([void 0]),[ie,Me]=d.useState(0),[Is,Ke]=d.useState(new Set),Ge=le({queryKey:["gmail-accounts"],queryFn:()=>p({})}),re=Ge.data??[],a=r.accountId??re[0]?.id,L=r.q??"";d.useEffect(()=>{Fe([void 0]),De(0),C(new Set)},[a,L]),d.useEffect(()=>{Be([void 0]),Me(0)},[a]),d.useEffect(()=>{C(new Set)},[o]);const Qe=zs[ne],Ue=Ss[ie],R=le({queryKey:["gmail-messages",a,L,Qe??"p0"],queryFn:()=>g({data:{accountId:a,q:L||void 0,pageToken:Qe}}),enabled:!!a&&o==="inbox"}),F=R.data?.messages??[],_=le({queryKey:["gmail-drafts",a,L,Ue??"p0"],queryFn:()=>q({data:{accountId:a,q:L||void 0,pageToken:Ue}}),enabled:!!a&&o==="drafts"}),A=_.data?.drafts??[],k=r.id,be=r.tid;d.useEffect(()=>{Z(!1),ae(""),J([])},[k,be]);const He=le({queryKey:["gmail-message",a,k],queryFn:()=>u({data:{accountId:a,id:k}}),enabled:!!k&&!!a&&!be&&o==="inbox"}),B=be??He.data?.threadId??void 0,ee=le({queryKey:["gmail-thread",a,B],queryFn:()=>j({data:{accountId:a,id:B}}),enabled:!!B&&!!a&&o==="inbox"}),z=ee.data?.messages??[],w=z[z.length-1];d.useEffect(()=>{w&&Ke(new Set([w.id]))},[ee.data?.id,w?.id]),d.useEffect(()=>{if(o!=="inbox"||!a||z.length===0)return;const s=z.filter(t=>t.unread).map(t=>t.id);s.length!==0&&M({data:{accountId:a,ids:s,removeLabelIds:["UNREAD"]}}).then(()=>m.invalidateQueries({queryKey:["gmail-messages",a,L]})).catch(()=>{})},[ee.data?.id]),d.useEffect(()=>{!r.draftId||!a||$({data:{accountId:a,id:r.draftId}}).then(s=>{E({open:!0,draftId:s.id,to:s.to??"",cc:s.cc??"",bcc:s.bcc??"",subject:s.subject??"",body:s.body??"",showCc:!!(s.cc||s.bcc),attachments:[]})}).catch(s=>x.error(s.message))},[r.draftId,a]);const Oe=d.useMemo(()=>re.find(s=>s.id===a),[re,a]),Xe=async()=>{he(!0);try{const{url:s}=await I({data:{returnTo:"/mail"}});s&&(window.location.href=s)}catch(s){x.error(s.message),he(!1)}},Ve=()=>E({...Pe,open:!0}),ce=()=>{E(Pe),r.draftId&&n({search:{...r,draftId:void 0}})},We=async(s,t)=>{if(!s||s.length===0)return null;const i=[...t];let l=t.reduce((h,O)=>h+O.size,0);for(const h of Array.from(s)){if(h.size>Cs){x.error(`${h.name} exceeds 7 MB limit`);continue}if(l+h.size>Le){x.error("Total attachments exceed 20 MB");break}const O=await Lt(h);i.push({filename:h.name,mimeType:h.type||"application/octet-stream",content:O,size:h.size}),l+=h.size}return i},ve=d.useRef(null),je=d.useRef(null),Ds=async s=>{const t=await We(s,c.attachments);t&&E(i=>({...i,attachments:t})),ve.current&&(ve.current.value="")},Ms=async s=>{const t=await We(s,fe);t&&J(t),je.current&&(je.current.value="")},Es=async(s,t,i,l)=>{if(a)try{x.loading("Downloadingâ¦",{id:"att"});const{data:h}=await S({data:{accountId:a,messageId:s,attachmentId:t}});_t(h,i,l),x.success("Downloaded",{id:"att"})}catch(h){x.error(h.message,{id:"att"})}},oe=()=>{m.invalidateQueries({queryKey:["gmail-messages",a,L]})},Ye=async s=>{if(a)try{await V({data:{accountId:a,id:s}}),x.success("Moved to trash"),oe(),a&&B&&m.invalidateQueries({queryKey:["gmail-thread",a,B]}),(k===s||z.some(t=>t.id===s))&&n({search:{...r,id:void 0,tid:void 0}})}catch(t){x.error(t.message)}},Rs=async()=>{if(!(!a||z.length===0))try{await M({data:{accountId:a,ids:z.map(s=>s.id),addLabelIds:["UNREAD"]}}),x.success("Marked as unread"),oe(),a&&B&&m.invalidateQueries({queryKey:["gmail-thread",a,B]})}catch(s){x.error(s.message)}},Ee=s=>{C(t=>{const i=new Set(t);return i.has(s)?i.delete(s):i.add(s),i})},Ps=F.length>0&&F.every(s=>b.has(s.id)),Ze=()=>{C(s=>{if(F.every(i=>s.has(i.id))){const i=new Set(s);return F.forEach(l=>i.delete(l.id)),i}const t=new Set(s);return F.forEach(i=>t.add(i.id)),t})},Ts=A.length>0&&A.every(s=>b.has(s.id)),Je=()=>{C(s=>{if(A.every(i=>s.has(i.id))){const i=new Set(s);return A.forEach(l=>i.delete(l.id)),i}const t=new Set(s);return A.forEach(i=>t.add(i.id)),t})},ye=async s=>{if(!(!a||b.size===0)){H(!0);try{await M({data:{accountId:a,ids:Array.from(b),...s?{removeLabelIds:["UNREAD"]}:{addLabelIds:["UNREAD"]}}}),x.success(s?"Marked as read":"Marked as unread"),C(new Set),oe()}catch(t){x.error(t.message)}finally{H(!1)}}},es=async()=>
1{if(!(!a||b.size===0)){H(!0);try{const{count:s}=await G({data:{accountId:a,ids:Array.from(b)}});x.success(`Moved ${s} to trash`),k&&b.has(k)&&n({search:{...r,id:void 0,tid:void 0}}),C(new Set),oe()}catch(s){x.error(s.message)}finally{H(!1)}}},ke=async s=>{if(!a||b.size===0)return;const t=A.filter(i=>b.has(i.id)).map(i=>i.messageId).filter(i=>!!i);if(t.length!==0){H(!0);try{await M({data:{accountId:a,ids:t,...s?{removeLabelIds:["UNREAD"]}:{addLabelIds:["UNREAD"]}}}),x.success(s?"Marked as read":"Marked as unread"),C(new Set),m.invalidateQueries({queryKey:["gmail-drafts",a]})}catch(i){x.error(i.message)}finally{H(!1)}}},ss=async()=>{if(!(!a||b.size===0)){H(!0);try{const s=Array.from(b),t=await Promise.allSettled(s.map(h=>y({data:{accountId:a,id:h}}))),i=t.filter(h=>h.status==="fulfilled").length,l=t.length-i;x.success(l===0?`Deleted ${i} drafts`:`Deleted ${i}, ${l} failed`),r.draftId&&b.has(r.draftId)&&ce(),C(new Set),m.invalidateQueries({queryKey:["gmail-drafts",a]})}catch(s){x.error(s.message)}finally{H(!1)}}},ts=()=>{const s=R.data?.nextPageToken;s&&(Fe(t=>[...t.slice(0,ne+1),s]),De(t=>t+1),C(new Set))},as=()=>{ne!==0&&(De(s=>s-1),C(new Set))},ns=()=>{const s=_.data?.nextPageToken;s&&(Be(t=>[...t.slice(0,ie+1),s]),Me(t=>t+1))},is=()=>{ie!==0&&Me(s=>s-1)};d.useEffect(()=>{const s=t=>{const i=t.target;if(!(i&&(i.tagName==="INPUT"||i.tagName==="TEXTAREA"||i.tagName==="SELECT"||i.isContentEditable))&&!(t.metaKey||t.ctrlKey||t.altKey)){if(t.key==="Escape"){if(c.open){ce();return}if(ze){Z(!1);return}if(k||be){n({search:{...r,id:void 0,tid:void 0}});return}b.size>0&&C(new Set);return}switch(t.key){case"c":Ve(),t.preventDefault();break;case"n":o==="inbox"&&R.data?.nextPageToken?ts():o==="drafts"&&_.data?.nextPageToken&&ns(),t.preventDefault();break;case"p":o==="inbox"?as():is(),t.preventDefault();break;case"j":case"k":{const l=o==="inbox"?F:A;if(l.length===0)break;const h=o==="inbox"?k:r.draftId,O=l.findIndex($s=>$s.id===h),qs=t.key==="j"?Math.min(l.length-1,O<0?0:O+1):Math.max(0,O<0?0:O-1),Ne=l[qs];if(!Ne)break;n(o==="inbox"?{search:{accountId:a,q:L||void 0,id:Ne.id,tid:Ne.threadId??void 0,view:o}}:{search:{...r,view:"drafts",draftId:Ne.id}}),t.preventDefault();break}case"x":{const l=o==="inbox"?k:r.draftId;l&&(Ee(l),t.preventDefault());break}case"a":o==="inbox"?Ze():Je(),t.preventDefault();break;case"e":if(b.size===0)break;o==="inbox"?ye(!0):ke(!0),t.preventDefault();break;case"u":if(b.size===0)break;o==="inbox"?ye(!1):ke(!1),t.preventDefault();break;case"#":case"Delete":case"Backspace":if(b.size===0)break;o==="inbox"?es():ss(),t.preventDefault();break;case"r":o==="inbox"&&k&&!ze&&(Z(!0),ae(""),J([]),t.preventDefault());break;case"?":x.info("Shortcuts: c compose · j/k next/prev · n/p page · x select · a select all · e read · u unread · # trash · r reply · Esc close",{duration:6e3}),t.preventDefault();break}}};return window.addEventListener("keydown",s),()=>window.removeEventListener("keydown",s)});const Ls=async()=>{if(a){ge("saving");try{const{id:s}=await Q({data:{accountId:a,id:c.draftId,to:c.to,cc:c.cc||void 0,bcc:c.bcc||void 0,subject:c.subject,body:c.body,attachments:c.attachments.length?Te(c.attachments):void 0}});return E(t=>({...t,draftId:s})),x.success("Draft saved"),m.invalidateQueries({queryKey:["gmail-drafts",a]}),s}catch(s){x.error(s.message)}finally{ge(null)}}},rs=async()=>{if(a){if(!c.to.trim()||!c.subject.trim()||!c.body.trim()){x.error("To, subject, and body are required");return}ge("sending");try{const s=c.attachments.length?Te(c.attachments):void 0;c.draftId&&!s?(await Q({data:{accountId:a,id:c.draftId,to:c.to,cc:c.cc||void 0,bcc:c.bcc||void 0,subject:c.subject,body:c.body}
1}),await N({data:{accountId:a,id:c.draftId}})):(await P({data:{accountId:a,to:c.to.trim(),cc:c.cc.trim()||void 0,bcc:c.bcc.trim()||void 0,subject:c.subject.trim(),body:c.body,attachments:s}}),c.draftId&&await y({data:{accountId:a,id:c.draftId}}).catch(()=>{})),x.success("Email sent"),ce(),m.invalidateQueries({queryKey:["gmail-drafts",a]}),oe()}catch(s){x.error(s.message)}finally{ge(null)}}},cs=async s=>{if(a)try{await y({data:{accountId:a,id:s}}),x.success("Draft deleted"),m.invalidateQueries({queryKey:["gmail-drafts",a]}),c.draftId===s&&ce()}catch(t){x.error(t.message)}};if(Ge.isLoading)return e.jsxs("div",{className:"flex items-center justify-center h-full p-10 text-ink-muted",children:[e.jsx(T,{className:"size-4 animate-spin mr-2"})," Loading inboxesâ¦"]});if(re.length===0)return e.jsxs("div",{className:"max-w-xl mx-auto px-6 py-20 text-center space-y-4",children:[e.jsx(ut,{className:"size-8 mx-auto text-ink-muted"}),e.jsx("h1",{className:"text-2xl font-light",children:"No Gmail accounts connected"}),e.jsx("p",{className:"text-sm text-ink-muted",children:"Connect your Gmail to read and send mail right here. You can add multiple accounts."}),e.jsxs(f,{onClick:Xe,disabled:Y,className:"rounded-sm",children:[Y?e.jsx(T,{className:"size-4 animate-spin"}):e.jsx(ds,{className:"size-4"}),"Connect Gmail"]})]});const _s=!!R.data?.nextPageToken,As=!!_.data?.nextPageToken;return e.jsxs("div",{className:"flex h-[calc(100vh-0px)] min-h-0 relative",children:[e.jsxs("aside",{className:"w-56 border-r rule-line p-3 space-y-3 shrink-0 flex flex-col",children:[e.jsxs(f,{onClick:Ve,className:"rounded-sm w-full",size:"sm",children:[e.jsx(It,{className:"size-3.5 mr-1.5"})," Compose"]}),e.jsxs("div",{className:"space-y-0.5",children:[e.jsxs("button",{onClick:()=>n({search:{...r,view:"inbox",draftId:void 0}}),className:`w-full text-left px-2 py-1.5 text-sm rounded-sm inline-flex items-center gap-2 ${o==="inbox"?"bg-muted":"hover:bg-muted/50"}`,children:[e.jsx(xt,{className:"size-3.5"})," Inbox"]}),e.jsxs("button",{onClick:()=>n({search:{...r,view:"drafts",id:void 0,tid:void 0,draftId:void 0}}),className:`w-full text-left px-2 py-1.5 text-sm rounded-sm inline-flex items-center gap-2 ${o==="drafts"?"bg-muted":"hover:bg-muted/50"}`,children:[e.jsx(Nt,{className:"size-3.5"})," Drafts"]})]}),e.jsxs("div",{className:"pt-2 border-t rule-line space-y-1 flex-1 overflow-y-auto",children:[e.jsx("p",{className:"label-mono px-2 py-1",children:"Inboxes"}),re.map(s=>e.jsxs("button",{onClick:()=>n({search:{accountId:s.id,q:L||void 0,id:void 0,tid:void 0,view:o}}),className:`w-full text-left px-2 py-1.5 text-sm rounded-sm truncate ${s.id===a?"bg-muted":"hover:bg-muted/50"}`,title:s.email,children:[e.jsx("span",{className:"block truncate",children:s.label||s.email}),s.label&&e.jsx("span",{className:"font-mono text-[10px] text-ink-muted truncate block",children:s.email})]},s.id)),e.jsxs("button",{onClick:Xe,disabled:Y,className:"w-full text-left px-2 py-1.5 text-sm rounded-sm hover:bg-muted/50 text-ink-muted inline-flex items-center gap-1.5 mt-2",children:[Y?e.jsx(T,{className:"size-3 animate-spin"}):e.jsx(ds,{className:"size-3"}),"Add Gmail"]})]})]}),e.jsxs("section",{className:"w-96 border-r rule-line flex flex-col shrink-0 min-h-0",children:[e.jsxs("div",{className:"p-3 border-b rule-line space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[o==="inbox"&&F.length>0&&e.jsx(me,{checked:Ps,onCheckedChange:Ze,"aria-label":"Select all"}),o==="drafts"&&A.length>0&&e.jsx(me,{checked:Ts,onCheckedChange:Je,"aria-label":"Select all drafts"}),e.jsx("p",{className:"text-sm font-medium truncate",children:Oe?.email}),e.jsx(f,{size:"icon",variant:"ghost",className:"ml-auto size-7",onClick:()=>o==="inbox"?R.refetch():_.refetch(),title:"Refresh",children:e.jsx(ht,{className:`size-3.5 ${(o==="inbox"?R.isFetching:_.isFetching)?"animate-spin":""}`})})]}),e.jsxs("form",{onSubmit:s=>{s.preventDefault(),n({search:{accountId:a,q:W||void 0,id:void 0,tid:void 0,view:o}})},className:"relative",children:[e.jsx(ft,{className:"size-3.5 absolute left-2 top-1/2 -translate-y-1/2 text-ink-muted"}),e.jsx(de,{value:W,onChange:s=>xe(s.target.value),placeholder:o==="inbox"?"Search (e.g. is:unread from:foo has:attachment)":"Search drafts (e.g. to:foo subject:bar)",className:"pl-7 h-8 text-xs"})]}),o==="inbox"&&b.size>0&&e.jsxs("div",{className:"flex items-center gap-1.5 rounded-sm bg-muted px-2 py-1.5",children:[e.jsxs("span",{className:"text-xs font-medium mr-auto",children:[b.size," selected"]}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",title:"Mark read (e)",disabled:U,onClick:()=>ye(!0),children:e.jsx(hs,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",title:"Mark unread (u)",disabled:U,onClick:()=>ye(!1),children:e.jsx(Re,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7 text-destructive hover:text-destructive",title:"Move to trash (#)",disabled:U,onClick:es,children:U?e.jsx(T,{className:"size-3.5 animate-spin"}):e.jsx(se,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",title:"Clear (Esc)",onClick:()=>C(new Set),children:e.jsx(we,{className:"size-3.5"})})]}),o==="drafts"&&b.size>0&&e.jsxs("div",{className:"flex items-center gap-1.5 rounded-sm bg-muted px-2 py-1.5",children:[e.jsxs("span",{className:"text-xs font-medium mr-auto",children:[b.size," selected"]}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",title:"Mark read (e)",disabled:U,onClick:()=>ke(!0),children:e.jsx(hs,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",title:"Mark unread (u)",disabled:U,onClick:()=>ke(!1),children:e.jsx(Re,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7 text-destructive hover:text-destructive",title:"Delete drafts (#)",disabled:U,onClick:ss,children:U?e.jsx(T,{className:"size-3.5 animate-spin"}):e.jsx(se,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",title:"Clear (Esc)",onClick:()=>C(new Set),children:e.jsx(we,{className:"size-3.5"})})]})]}),e.jsxs("div",{className:"flex-1 overflow-y-auto min-h-0",children:[o==="inbox"&&e.jsxs(e.Fragment,{children:[R.isLoading&&e.jsxs("div",{className:"p-4 text-xs text-ink-muted flex items-center gap-2",children:[e.jsx(T,{className:"size-3 animate-spin"})," Loadingâ¦"]}),R.error&&e.jsx("div",{className:"p-4 text-xs text-destructive",children:R.error.message}),!R.isLoading&&F.length===0&&e.jsx("div",{className:"p-4 text-xs text-ink-muted",children:"No messages."}),F.map(s=>{const t=b.has(s.id),i=k===s.id||z.some(l=>l.id===s.id);return e.jsxs("div",{className:`group relative border-b rule-line flex ${i?"bg-muted":t?"bg-muted/60":"hover:bg-muted/50"}`,children:[e.jsx("div",{className:"pl-3 pt-3",onClick:l=>l.stopPropagation(),children:e.jsx(me,{checked:t,onCheckedChange:()=>Ee(s.id),"aria-label":"Select message"})}),e.jsxs("button",{onClick:()=>n({search:{accountId:a,q:L||void 0,id:s.id,tid:s.threadId??void 0,view:o}}),className:"flex-1 min-w-0 text-left px-3 py-2 block",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:`text-xs truncate flex-1 ${s.unread?"font-semibold":""}`,children:s.from||"(unknown)"}),s.hasAttachments&&e.jsx(te,{className:"size-3 text-ink-muted shrink-0"}),e.jsx("span",{className:"font-mono text-[10px] text-ink-muted shrink-0",children:fs(s.date)})]}),e.jsx("p",{className:`text-sm truncate ${s.unread?"font-medium":""}`,children:s.subject}),e.jsx("p",{className:"text-xs text-ink-muted truncate",children:s.snippet})]}),e.jsx("button",{onClick:l=>{l.stopPropagation(),Ye(s.id)},className:"absolute right-2 top-1/2 -translate-y-1/2 opacity-0 group-hover:opacity-100 p-1.5 rounded-sm hover:bg-background",title:"Move to trash",children:e.jsx(se,{className:"size-3.5 text-ink-muted"})})]},s.id)})]}),o==="drafts"&&e.jsxs(e.Fragment,{children:[_.isLoading&&e.jsxs("div",{className:"p-4 text-xs text-ink-muted flex items-center gap-2",children:[e.jsx(T,{className:"size-3 animate-spin"})," Loadingâ¦"]}),!_.isLoading&&A.length===0&&e.jsx("div",{className:"p-4 text-xs text-ink-muted",children:"No drafts."}),A.map(s=>{const t=b.has(s.id),i=r.draftId===s.id;return e.jsxs("div",{className:`group relative border-b rule-line flex ${i?"bg-muted":t?"bg-muted/60":"hover:bg-muted/50"}`,children:[e.jsx("div",{className:"pl-3 pt-3",onClick:l=>l.stopPropagation(),children:e.jsx(me,{checked:t,onCheckedChange:()=>Ee(s.id),"aria-label":"Select draft"})}),e.jsxs("button",{onClick:()=>n({search:{...r,view:"drafts",draftId:s.id}}),className:"flex-1 min-w-0 text-left px-3 py-2 block",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"text-xs truncate flex-1",children:["To: ",s.to||"(no recipient)"]}),e.jsx("span",{className:"font-mono text-[10px] text-ink-muted shrink-0",children:fs(s.date)})]}),e.jsx("p",{className:"text-sm truncate",children:s.subject}),e.jsx("p",{className:"text-xs text-ink-muted truncate",children:s.snippet})]}),e.jsx("button",{onClick:l=>{l.stopPropagation(),cs(s.id)},className:"absolute right-2 top-1/2 -translate-y-1/2 opacity-0 group-hover:opacity-100 p-1.5 rounded-sm hover:bg-background",title:"Delete draft",children:e.jsx(se,{className:"size-3.5 text-ink-muted"})})]},s.id)})]})]}),e.jsx("div",{className:"flex items-center justify-between gap-2 px-3 py-2 border-t rule-line",children:o==="inbox"?e.jsxs(e.Fragment,{children:[e.jsxs("span",{className:"text-[10px] font-mono text-ink-muted",children:["Page ",ne+1]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",disabled:ne===0||R.isFetching,onClick:as,title:"Previous page",children:e.jsx(us,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",disabled:!_s||R.isFetching,onClick:ts,title:"Next page",children:e.jsx(xs,{className:"size-3.5"})})]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("span",{className:"text-[10px] font-mono text-ink-muted",children:["Page ",ie+1]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",disabled:ie
1===0||_.isFetching,onClick:is,title:"Previous page",children:e.jsx(us,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"size-7",disabled:!As||_.isFetching,onClick:ns,title:"Next page",children:e.jsx(xs,{className:"size-3.5"})})]})]})})]}),e.jsxs("section",{className:"flex-1 min-w-0 overflow-y-auto",children:[o==="inbox"&&!k&&e.jsx("div",{className:"h-full flex items-center justify-center text-sm text-ink-muted",children:"Select a message"}),o==="drafts"&&!c.open&&e.jsx("div",{className:"h-full flex items-center justify-center text-sm text-ink-muted",children:"Select a draft to edit, or compose a new one."}),o==="inbox"&&k&&(ee.isLoading||He.isLoading)&&e.jsxs("div",{className:"p-6 text-sm text-ink-muted flex items-center gap-2",children:[e.jsx(T,{className:"size-4 animate-spin"})," Loading conversationâ¦"]}),o==="inbox"&&k&&ee.error&&e.jsx("div",{className:"p-6 text-sm text-destructive",children:ee.error.message}),o==="inbox"&&k&&z.length>0&&e.jsxs("article",{className:"p-6 space-y-4 max-w-3xl",children:[e.jsx("header",{className:"space-y-1 pb-3 border-b rule-line",children:e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("h1",{className:"text-xl font-medium",children:z[0].subject}),e.jsxs("p",{className:"text-xs text-ink-muted",children:[z.length," message",z.length>1?"s":""," in this conversation"]})]}),e.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[e.jsxs(f,{size:"sm",variant:"outline",className:"rounded-sm",onClick:()=>{Z(!0),ae(""),J([])},children:[e.jsx(Mt,{className:"size-3.5 mr-1.5"})," Reply"]}),e.jsx(f,{size:"icon",variant:"ghost",className:"rounded-sm size-8",onClick:Rs,title:"Mark as unread",children:e.jsx(Re,{className:"size-3.5"})}),e.jsx(f,{size:"icon",variant:"ghost",className:"rounded-sm size-8 text-destructive hover:text-destructive",onClick:()=>w&&Ye(w.id),title:"Move to trash",children:e.jsx(se,{className:"size-3.5"})})]})]})}),e.jsx("div",{className:"space-y-2",children:z.map((s,t)=>{const i=Is.has(s.id);return e.jsxs("div",{className:"border rule-line rounded-sm overflow-hidden",children:[e.jsxs("button",{onClick:()=>Ke(l=>{const h=new Set(l);return h.has(s.id)?h.delete(s.id):h.add(s.id),h}),className:"w-full text-left px-4 py-2.5 flex items-center gap-2 hover:bg-muted/40",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:`text-xs truncate ${s.unread?"font-semibold":"font-medium"}`,children:s.from}),!i&&e.jsx("p",{className:"text-xs text-ink-muted truncate",children:s.snippet})]}),s.attachments.length>0&&e.jsx(te,{className:"size-3 text-ink-muted shrink-0"}),e.jsx("span",{className:"font-mono text-[10px] text-ink-muted shrink-0",children:s.date?new Date(s.date).toLocaleString():""}),e.jsx(pt,{className:`size-3.5 text-ink-muted shrink-0 transition-transform ${i?"rotate-180":""}`})]}),i&&e.jsxs("div",{className:"px-4 py-3 border-t rule-line space-y-3",children:[e.jsxs("p",{className:"text-[11px] text-ink-muted",children:["To: ",s.to||"â",s.cc?` · Cc: ${s.cc}`:""]}),s.bodyHtml?e.jsx("iframe",{title:`email-body-${t}`,sandbox:"",srcDoc:s.bodyHtml,className:"w-full min-h-[40vh] border-0 bg-white rounded-sm"}):e.jsx("pre",{className:"text-sm whitespace-pre-wrap font-sans",children:s.bodyText||s.snippet}),s.attachments.length>0&&e.jsx("div",{className:"flex flex-wrap gap-2 pt-1",children:s.attachments.map(l=>e.jsxs("button",{onClick:()=>Es(s.id,l.attachmentId,l.filename,l.mimeType),className:"inline-flex items-center gap-1.5 rounded-sm border rule-line px-2 py-1 text-xs hover:bg-muted",title:`Download ${l.filename}`,children:[e.jsx(te,{className:"size-3"}),e.jsx("span",{className:"truncate max-w-[160px]",children:l.filename}),e.jsx("span",{className:"text-ink-muted",children:ue(l.size)}),e.jsx(yt,{className:"size-3"})]},l.attachmentId))})]})]},s.id)})}),ze&&w&&e.jsxs("div",{className:"border rule-line p-4 space-y-3 bg-card rounded-sm",children:[e.jsxs("p",{className:"text-xs text-ink-muted",children:["Replying to ",e.jsx("span",{className:"font-medium text-foreground",children:w.from})]}),e.jsx("textarea",{value:Se,onChange:s=>ae(s.target.value),placeholder:"Write your replyâ¦",rows:5,className:"w-full resize-none rounded-sm border rule-line bg-background px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-ring"}),e.jsx(ps,{attachments:fe,onRemove:s=>J(t=>t.filter((i,l)=>l!==s))}),e.jsx("input",{ref:je,type:"file",multiple:!0,className:"hidden",onChange:s=>Ms(s.target.files)}),e.jsxs("div",{className:"flex items-center gap-2 justify-end",children:[e.jsxs(f,{size:"sm",variant:"ghost",className:"rounded-sm mr-auto",onClick:()=>je.current?.click(),children:[e.jsx(te,{className:"size-3.5 mr-1.5"})," Attach"]}),e.jsx(f,{size:"sm",variant:"ghost",className:"rounded-sm",onClick:()=>Z(!1),children:"Cancel"}),e.jsxs(f,{size:"sm",className:"rounded-sm",disabled:!Se.trim()||Ie,onClick:async()=>
1{if(!(!a||!w)){$e(!0);try{const s=w.subject.startsWith("Re:")?w.subject:`Re: ${w.subject}`;await D({data:{accountId:a,to:w.from,subject:s,body:Se.trim(),inReplyTo:w.messageId??void 0,references:w.messageId??void 0,gmailThreadId:w.threadId??void 0,attachments:fe.length?Te(fe):void 0}}),x.success("Reply sent"),Z(!1),ae(""),J([]),a&&B&&m.invalidateQueries({queryKey:["gmail-thread",a,B]})}catch(s){x.error(s.message)}finally{$e(!1)}}},children:[Ie?e.jsx(T,{className:"size-3.5 animate-spin"}):e.jsx(ms,{className:"size-3.5 mr-1.5"}),Ie?"Sendingâ¦":"Send"]})]})]})]})]}),c.open&&e.jsxs("div",{className:"fixed bottom-4 right-4 w-[560px] max-w-[calc(100vw-2rem)] max-h-[80vh] bg-card border rule-line shadow-2xl rounded-sm flex flex-col z-50",children:[e.jsxs("header",{className:"flex items-center justify-between px-3 py-2 border-b rule-line",children:[e.jsx("p",{className:"text-sm font-medium",children:c.draftId?"Edit draft":"New message"}),e.jsx("button",{onClick:ce,className:"p-1 rounded-sm hover:bg-muted",title:"Close",children:e.jsx(we,{className:"size-3.5"})})]}),e.jsxs("div",{className:"p-3 space-y-2 flex-1 overflow-y-auto",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("label",{className:"text-xs text-ink-muted w-12",children:"From"}),e.jsx("p",{className:"text-xs flex-1 truncate",children:Oe?.email})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("label",{className:"text-xs text-ink-muted w-12",children:"To"}),e.jsx(de,{value:c.to,onChange:s=>E(t=>({...t,to:s.target.value})),placeholder:"[email protected]",className:"h-7 text-xs flex-1"}),!c.showCc&&e.jsx("button",{onClick:()=>E(s=>({...s,showCc:!0})),className:"text-[10px] text-ink-muted hover:text-foreground",children:"Cc/Bcc"})]}),c.showCc&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("label",{className:"text-xs text-ink-muted w-12",children:"Cc"}),e.jsx(de,{value:c.cc,onChange:s=>E(t=>({...t,cc:s.target.value})),className:"h-7 text-xs flex-1"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("label",{className:"text-xs text-ink-muted w-12",children:"Bcc"}),e.jsx(de,{value:c.bcc,onChange:s=>E(t=>({...t,bcc:s.target.value})),className:"h-7 text-xs flex-1"})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("label",{className:"text-xs text-ink-muted w-12",children:"Subject"}),e.jsx(de,{value:c.subject,onChange:s=>E(t=>({...t,subject:s.target.value})),className:"h-7 text-xs flex-1"})]}),e.jsx("textarea",{value:c.body,onChange:s=>E(t=>({...t,body:s.target.value})),placeholder:"Write your messageâ¦",rows:9,className:"w-full resize-none rounded-sm border rule-line bg-background px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-ring",onKeyDown:s=>{s.key==="Enter"&&(s.metaKey||s.ctrlKey)&&(s.preventDefault(),rs())}}),e.jsx(ps,{attachments:c.attachments,onRemove:s=>E(t=>({...t,attachments:t.attachments.filter((i,l)=>l!==s)}))})]}),e.jsx("input",{ref:ve,type:"file",multiple:!0,className:"hidden",onChange:s=>Ds(s.target.files)}),e.jsxs("footer",{className:"flex items-center gap-2 px-3 py-2 border-t rule-line",children:[e.jsxs(f,{size:"sm",className:"rounded-sm",onClick:rs,disabled:pe!==null,children:[pe==="sending"?e.jsx(T,{className:"size-3.5 animate-spin"}):e.jsx(ms,{className:"size-3.5 mr-1.5"}),"Send"]}),e.jsxs(f,{size:"sm",variant:"outline",className:"rounded-sm",onClick:Ls,disabled:pe!==null,children:[pe==="saving"?e.jsx(T,{className:"size-3.5 animate-spin"}):e.jsx(gt,{className:"size-3.5 mr-1.5"}),"Save draft"]}),e.jsx(f,{size:"icon",variant:"ghost",className:"rounded-sm size-8",onClick:()=>ve.current?.click(),title:"Attach files",children:e.jsx(te,{className:"size-3.5"})}),c.draftId&&e.jsxs(f,{size:"sm",variant:"ghost",className:"rounded-sm text-destructive hover:text-destructive ml-auto",onClick:()=>cs(c.draftId),children:[e.jsx(se,{className:"size-3.5 mr-1.5"})," Discard"]})]})]})]})}function Te(n){return n.map(r=>({filename:r.filename,mimeType:r.mimeType,content:r.content}))}export{oa as component};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.